Skip to content

Commit dfb9f11

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. Cc: 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 309cc8f commit dfb9f11

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

lib/chkname.c

Lines changed: 18 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,6 +34,8 @@
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"
3740

3841

@@ -56,6 +59,17 @@ login_name_max_size(void)
5659
static bool
5760
is_valid_name(const char *name)
5861
{
62+
if (streq(name, "")
63+
|| streq(name, ".")
64+
|| streq(name, "..")
65+
|| strpbrk(name, ",:")
66+
|| strchriscntrl(name)
67+
|| strisdigit(name))
68+
{
69+
errno = EINVAL;
70+
return false;
71+
}
72+
5973
if (allow_bad_names) {
6074
return true;
6175
}
@@ -66,26 +80,18 @@ is_valid_name(const char *name)
6680
*
6781
* as a non-POSIX, extension, allow "$" as the last char for
6882
* sake of Samba 3.x "add machine script"
69-
*
70-
* Also do not allow fully numeric names or just "." or "..".
7183
*/
72-
int numeric;
7384

74-
if (streq(name, "") ||
75-
streq(name, ".") ||
76-
streq(name, "..") ||
77-
!((*name >= 'a' && *name <= 'z') ||
85+
if (!((*name >= 'a' && *name <= 'z') ||
7886
(*name >= 'A' && *name <= 'Z') ||
7987
(*name >= '0' && *name <= '9') ||
8088
*name == '_' ||
8189
*name == '.'))
8290
{
83-
errno = EINVAL;
91+
errno = EILSEQ;
8492
return false;
8593
}
8694

87-
numeric = isdigit(*name);
88-
8995
while (!streq(++name, "")) {
9096
if (!((*name >= 'a' && *name <= 'z') ||
9197
(*name >= 'A' && *name <= 'Z') ||
@@ -96,15 +102,9 @@ is_valid_name(const char *name)
96102
streq(name, "$")
97103
))
98104
{
99-
errno = EINVAL;
105+
errno = EILSEQ;
100106
return false;
101107
}
102-
numeric &= isdigit(*name);
103-
}
104-
105-
if (numeric) {
106-
errno = EINVAL;
107-
return false;
108108
}
109109

110110
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
@@ -476,7 +476,7 @@ static void check_pw_file (int *errors, bool *changed)
476476
*/
477477

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

src/useradd.c

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

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

src/usermod.c

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

0 commit comments

Comments
 (0)