Skip to content

Commit 5369182

Browse files
lib/chkname.c, src/: Strictly disallow really bad names
Some names are bad, and some names are really bad. '--badname' should only allow the mildly bad ones, which we can handle. Some names are too bad, and it's not possible to deal with them. Reject them unconditionally. Acked-by: Chris Hofstaedtler <[email protected]> Cc: Marc 'Zugschlus' Haber <[email protected]> Cc: Iker Pedrosa <[email protected]> Cc: Serge Hallyn <[email protected]> Signed-off-by: Alejandro Colomar <[email protected]>
1 parent dda02b8 commit 5369182

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

lib/chkname.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
* true - OK
1414
* false - bad name
1515
* errors:
16-
* EINVAL Invalid name characters or sequences
16+
* EINVAL Invalid name
17+
* EILSEQ Invalid name character sequence (acceptable with --badname)
1718
* EOVERFLOW Name longer than maximum size
1819
*/
1920

@@ -33,7 +34,10 @@
3334

3435
#include "defines.h"
3536
#include "chkname.h"
37+
#include "string/ctype/strchrisascii/strchriscntrl.h"
38+
#include "string/ctype/strisascii/strisdigit.h"
3639
#include "string/strcmp/streq.h"
40+
#include "string/strcmp/strprefix.h"
3741

3842

3943
int allow_bad_names = false;
@@ -56,6 +60,18 @@ login_name_max_size(void)
5660
static bool
5761
is_valid_name(const char *name)
5862
{
63+
if (streq(name, "")
64+
|| streq(name, ".")
65+
|| streq(name, "..")
66+
|| strpbrk(name, ",: ")
67+
|| strprefix(name, "-")
68+
|| strchriscntrl(name)
69+
|| strisdigit(name))
70+
{
71+
errno = EINVAL;
72+
return false;
73+
}
74+
5975
if (allow_bad_names) {
6076
return true;
6177
}
@@ -66,26 +82,18 @@ is_valid_name(const char *name)
6682
*
6783
* as a non-POSIX, extension, allow "$" as the last char for
6884
* sake of Samba 3.x "add machine script"
69-
*
70-
* Also do not allow fully numeric names or just "." or "..".
7185
*/
72-
int numeric;
7386

74-
if (streq(name, "") ||
75-
streq(name, ".") ||
76-
streq(name, "..") ||
77-
!((*name >= 'a' && *name <= 'z') ||
87+
if (!((*name >= 'a' && *name <= 'z') ||
7888
(*name >= 'A' && *name <= 'Z') ||
7989
(*name >= '0' && *name <= '9') ||
8090
*name == '_' ||
8191
*name == '.'))
8292
{
83-
errno = EINVAL;
93+
errno = EILSEQ;
8494
return false;
8595
}
8696

87-
numeric = isdigit(*name);
88-
8997
while (!streq(++name, "")) {
9098
if (!((*name >= 'a' && *name <= 'z') ||
9199
(*name >= 'A' && *name <= 'Z') ||
@@ -96,15 +104,9 @@ is_valid_name(const char *name)
96104
streq(name, "$")
97105
))
98106
{
99-
errno = EINVAL;
107+
errno = EILSEQ;
100108
return false;
101109
}
102-
numeric &= isdigit(*name);
103-
}
104-
105-
if (numeric) {
106-
errno = EINVAL;
107-
return false;
108110
}
109111

110112
return true;

src/newusers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ static int add_user (const char *name, uid_t uid, gid_t gid)
388388

389389
/* Check if this is a valid user name */
390390
if (!is_valid_user_name(name)) {
391-
if (errno == EINVAL) {
391+
if (errno == EILSEQ) {
392392
fprintf(stderr,
393393
_("%s: invalid user name '%s': use --badname to ignore\n"),
394394
Prog, name);

src/pwck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ static void check_pw_file (int *errors, bool *changed)
475475
*/
476476

477477
if (!is_valid_user_name(pwd->pw_name)) {
478-
if (errno == EINVAL) {
478+
if (errno == EILSEQ) {
479479
printf(_("invalid user name '%s': use --badname to ignore\n"),
480480
pwd->pw_name);
481481
} else {

src/useradd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ static void process_flags (int argc, char **argv)
15341534

15351535
user_name = argv[optind];
15361536
if (!is_valid_user_name(user_name)) {
1537-
if (errno == EINVAL) {
1537+
if (errno == EILSEQ) {
15381538
fprintf(stderr,
15391539
_("%s: invalid user name '%s': use --badname to ignore\n"),
15401540
Prog, user_name);

src/usermod.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ process_flags(int argc, char **argv)
11181118
/*@notreached@*/break;
11191119
case 'l':
11201120
if (!is_valid_user_name(optarg)) {
1121-
if (errno == EINVAL) {
1121+
if (errno == EILSEQ) {
11221122
fprintf(stderr,
11231123
_("%s: invalid user name '%s': use --badname to ignore\n"),
11241124
Prog, optarg);

0 commit comments

Comments
 (0)