diff --git a/ChangeLog b/ChangeLog index 38825b55..d0e07da2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2008-05-25 Nicolas François + + * libmisc/chkname.h, libmisc/chkname.c: check_group_name (resp. + check_user_name) renamed to is_valid_user_name (resp. + is_valid_group_name). is_valid_user_name and is_valid_group_name + return a bool. + * src/grpck.c, src/newusers.c, src/usermod.c, src/useradd.c, + src/groupmod.c, src/pwck.c, src/groupadd.c: Use is_valid_user_name + and is_valid_group_name, following above change. + * libmisc/chkname.c: Avoid implicit conversion of chars to + booleans. Add brackets and parenthesis. + 2008-05-25 Nicolas François * libmisc/xmalloc.c: Avoid implicit conversion of integers / diff --git a/libmisc/chkname.c b/libmisc/chkname.c index a0aa738f..2bdc06b6 100644 --- a/libmisc/chkname.c +++ b/libmisc/chkname.c @@ -31,8 +31,11 @@ */ /* - * check_user_name(), check_group_name() - check the new user/group - * name for validity; return value: 1 - OK, 0 - bad name + * is_valid_user_name(), is_valid_group_name() - check the new user/group + * name for validity; + * return values: + * true - OK + * false - bad name */ #include @@ -47,26 +50,32 @@ #else #include #endif -static int good_name (const char *name) + +static bool is_valid_name (const char *name) { /* * User/group names must match [a-z_][a-z0-9_-]*[$] */ - if (!*name || !((*name >= 'a' && *name <= 'z') || *name == '_')) - return 0; - - while (*++name) { - if (!((*name >= 'a' && *name <= 'z') || - (*name >= '0' && *name <= '9') || - *name == '_' || *name == '-' || - (*name == '$' && *(name + 1) == '\0'))) - return 0; + if (('\0' == *name) || + !((('a' <= *name) && ('z' >= *name)) || ('_' == *name))) { + return false; } - return 1; + while ('\0' != *++name) { + if (!(( ('a' <= *name) && ('z' >= *name) ) || + ( ('0' <= *name) && ('9' >= *name) ) || + ('_' == *name) || + ('-' == *name) || + ( ('$' == *name) && ('\0' == *(name + 1)) ) + )) { + return false; + } + } + + return true; } -int check_user_name (const char *name) +bool is_valid_user_name (const char *name) { #if HAVE_UTMPX_H struct utmpx ut; @@ -78,21 +87,23 @@ int check_user_name (const char *name) * User names are limited by whatever utmp can * handle (usually max 8 characters). */ - if (strlen (name) > sizeof (ut.ut_user)) - return 0; + if (strlen (name) > sizeof (ut.ut_user)) { + return false; + } - return good_name (name); + return is_valid_name (name); } -int check_group_name (const char *name) +bool is_valid_group_name (const char *name) { /* * Arbitrary limit for group names - max 16 * characters (same as on HP-UX 10). */ - if (strlen (name) > 16) - return 0; + if (strlen (name) > 16) { + return false; + } - return good_name (name); + return is_valid_name (name); } diff --git a/libmisc/chkname.h b/libmisc/chkname.h index 7660eefe..82c91598 100644 --- a/libmisc/chkname.h +++ b/libmisc/chkname.h @@ -34,13 +34,16 @@ #define _CHKNAME_H_ /* - * check_user_name(), check_group_name() - check the new user/group - * name for validity; return value: 1 - OK, 0 - bad name + * is_valid_user_name(), is_valid_group_name() - check the new user/group + * name for validity; + * return values: + * true - OK + * false - bad name */ #include "defines.h" -extern int check_user_name (const char *); -extern int check_group_name (const char *name); +extern bool is_valid_user_name (const char *name); +extern bool is_valid_group_name (const char *name); #endif diff --git a/src/groupadd.c b/src/groupadd.c index 07802452..709884fa 100644 --- a/src/groupadd.c +++ b/src/groupadd.c @@ -220,7 +220,7 @@ static void grp_update (void) */ static void check_new_name (void) { - if (check_group_name (group_name)) { + if (is_valid_group_name (group_name)) { return; } diff --git a/src/groupmod.c b/src/groupmod.c index bdd929af..ddd5eb91 100644 --- a/src/groupmod.c +++ b/src/groupmod.c @@ -328,7 +328,7 @@ static void check_new_name (void) return; } - if (check_group_name (group_newname)) { + if (is_valid_group_name (group_newname)) { /* * If the entry is found, too bad. diff --git a/src/grpck.c b/src/grpck.c index 02924fce..cb12d6ab 100644 --- a/src/grpck.c +++ b/src/grpck.c @@ -497,7 +497,7 @@ static void check_grp_file (int *errors, int *changed) /* * Check for invalid group names. --marekm */ - if (!check_group_name (grp->gr_name)) { + if (!is_valid_group_name (grp->gr_name)) { *errors += 1; printf (_("invalid group name '%s'\n"), grp->gr_name); } diff --git a/src/newusers.c b/src/newusers.c index 97a97071..82550dab 100644 --- a/src/newusers.c +++ b/src/newusers.c @@ -214,7 +214,7 @@ static int add_group (const char *name, const char *gid, gid_t *ngid, uid_t uid) } /* Check if this is a valid group name */ - if (check_group_name (grent.gr_name) == 0) { + if (!is_valid_group_name (grent.gr_name)) { fprintf (stderr, _("%s: invalid group name `%s'\n"), Prog, grent.gr_name); @@ -318,7 +318,7 @@ static int add_user (const char *name, uid_t uid, gid_t gid) struct passwd pwent; /* Check if this is a valid user name */ - if (check_user_name (name) == 0) { + if (!is_valid_user_name (name)) { fprintf (stderr, _("%s: invalid user name `%s'\n"), Prog, name); diff --git a/src/pwck.c b/src/pwck.c index 0b42a86d..d319008c 100644 --- a/src/pwck.c +++ b/src/pwck.c @@ -354,7 +354,7 @@ static void check_pw_file (int *errors, int *changed) /* * Check for invalid usernames. --marekm */ - if (!check_user_name (pwd->pw_name)) { + if (!is_valid_user_name (pwd->pw_name)) { printf (_("invalid user name '%s'\n"), pwd->pw_name); *errors += 1; } diff --git a/src/useradd.c b/src/useradd.c index b606c90e..b4211769 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -1123,7 +1123,7 @@ static void process_flags (int argc, char **argv) usage (); user_name = argv[optind]; - if (!check_user_name (user_name)) { + if (!is_valid_user_name (user_name)) { fprintf (stderr, _ ("%s: invalid user name '%s'\n"), diff --git a/src/usermod.c b/src/usermod.c index c09907a9..e75111a5 100644 --- a/src/usermod.c +++ b/src/usermod.c @@ -917,7 +917,7 @@ static void process_flags (int argc, char **argv) Gflg++; break; case 'l': - if (!check_user_name (optarg)) { + if (!is_valid_user_name (optarg)) { fprintf (stderr, _("%s: invalid field '%s'\n"), Prog, optarg);