Skip to content

Commit 8be46b3

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 ddc631e commit 8be46b3

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

@@ -31,7 +32,10 @@
3132

3233
#include "defines.h"
3334
#include "chkname.h"
35+
#include "string/ctype/strchrisascii/strchriscntrl.h"
36+
#include "string/ctype/strisascii/strisdigit.h"
3437
#include "string/strcmp/streq.h"
38+
#include "string/strcmp/strprefix.h"
3539

3640

3741
#ifndef LOGIN_NAME_MAX
@@ -58,6 +62,18 @@ login_name_max_size(void)
5862
static bool
5963
is_valid_name(const char *name)
6064
{
65+
if (streq(name, "")
66+
|| streq(name, ".")
67+
|| streq(name, "..")
68+
|| strpbrk(name, ",: /")
69+
|| strprefix(name, "-")
70+
|| strchriscntrl(name)
71+
|| strisdigit(name))
72+
{
73+
errno = EINVAL;
74+
return false;
75+
}
76+
6177
if (allow_bad_names) {
6278
return true;
6379
}
@@ -68,26 +84,18 @@ is_valid_name(const char *name)
6884
*
6985
* as a non-POSIX, extension, allow "$" as the last char for
7086
* sake of Samba 3.x "add machine script"
71-
*
72-
* Also do not allow fully numeric names or just "." or "..".
7387
*/
74-
int numeric;
7588

76-
if (streq(name, "") ||
77-
streq(name, ".") ||
78-
streq(name, "..") ||
79-
!((*name >= 'a' && *name <= 'z') ||
89+
if (!((*name >= 'a' && *name <= 'z') ||
8090
(*name >= 'A' && *name <= 'Z') ||
8191
(*name >= '0' && *name <= '9') ||
8292
*name == '_' ||
8393
*name == '.'))
8494
{
85-
errno = EINVAL;
95+
errno = EILSEQ;
8696
return false;
8797
}
8898

89-
numeric = isdigit(*name);
90-
9199
while (!streq(++name, "")) {
92100
if (!((*name >= 'a' && *name <= 'z') ||
93101
(*name >= 'A' && *name <= 'Z') ||
@@ -98,15 +106,9 @@ is_valid_name(const char *name)
98106
streq(name, "$")
99107
))
100108
{
101-
errno = EINVAL;
109+
errno = EILSEQ;
102110
return false;
103111
}
104-
numeric &= isdigit(*name);
105-
}
106-
107-
if (numeric) {
108-
errno = EINVAL;
109-
return false;
110112
}
111113

112114
return true;

src/newusers.c

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

391391
/* Check if this is a valid user name */
392392
if (!is_valid_user_name(name)) {
393-
if (errno == EINVAL) {
393+
if (errno == EILSEQ) {
394394
fprintf(stderr,
395395
_("%s: invalid user name '%s': use --badname to ignore\n"),
396396
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)