* 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.
This commit is contained in:
nekral-guest 2008-05-25 20:58:16 +00:00
parent cb8d416b37
commit 623d9e2ab3
10 changed files with 59 additions and 33 deletions

View File

@ -1,3 +1,15 @@
2008-05-25 Nicolas François <nicolas.francois@centraliens.net>
* 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 <nicolas.francois@centraliens.net> 2008-05-25 Nicolas François <nicolas.francois@centraliens.net>
* libmisc/xmalloc.c: Avoid implicit conversion of integers / * libmisc/xmalloc.c: Avoid implicit conversion of integers /

View File

@ -31,8 +31,11 @@
*/ */
/* /*
* check_user_name(), check_group_name() - check the new user/group * is_valid_user_name(), is_valid_group_name() - check the new user/group
* name for validity; return value: 1 - OK, 0 - bad name * name for validity;
* return values:
* true - OK
* false - bad name
*/ */
#include <config.h> #include <config.h>
@ -47,26 +50,32 @@
#else #else
#include <utmp.h> #include <utmp.h>
#endif #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_-]*[$] * User/group names must match [a-z_][a-z0-9_-]*[$]
*/ */
if (!*name || !((*name >= 'a' && *name <= 'z') || *name == '_')) if (('\0' == *name) ||
return 0; !((('a' <= *name) && ('z' >= *name)) || ('_' == *name))) {
return false;
while (*++name) {
if (!((*name >= 'a' && *name <= 'z') ||
(*name >= '0' && *name <= '9') ||
*name == '_' || *name == '-' ||
(*name == '$' && *(name + 1) == '\0')))
return 0;
} }
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 #if HAVE_UTMPX_H
struct utmpx ut; struct utmpx ut;
@ -78,21 +87,23 @@ int check_user_name (const char *name)
* User names are limited by whatever utmp can * User names are limited by whatever utmp can
* handle (usually max 8 characters). * handle (usually max 8 characters).
*/ */
if (strlen (name) > sizeof (ut.ut_user)) if (strlen (name) > sizeof (ut.ut_user)) {
return 0; 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 * Arbitrary limit for group names - max 16
* characters (same as on HP-UX 10). * characters (same as on HP-UX 10).
*/ */
if (strlen (name) > 16) if (strlen (name) > 16) {
return 0; return false;
}
return good_name (name); return is_valid_name (name);
} }

View File

@ -34,13 +34,16 @@
#define _CHKNAME_H_ #define _CHKNAME_H_
/* /*
* check_user_name(), check_group_name() - check the new user/group * is_valid_user_name(), is_valid_group_name() - check the new user/group
* name for validity; return value: 1 - OK, 0 - bad name * name for validity;
* return values:
* true - OK
* false - bad name
*/ */
#include "defines.h" #include "defines.h"
extern int check_user_name (const char *); extern bool is_valid_user_name (const char *name);
extern int check_group_name (const char *name); extern bool is_valid_group_name (const char *name);
#endif #endif

View File

@ -220,7 +220,7 @@ static void grp_update (void)
*/ */
static void check_new_name (void) static void check_new_name (void)
{ {
if (check_group_name (group_name)) { if (is_valid_group_name (group_name)) {
return; return;
} }

View File

@ -328,7 +328,7 @@ static void check_new_name (void)
return; return;
} }
if (check_group_name (group_newname)) { if (is_valid_group_name (group_newname)) {
/* /*
* If the entry is found, too bad. * If the entry is found, too bad.

View File

@ -497,7 +497,7 @@ static void check_grp_file (int *errors, int *changed)
/* /*
* Check for invalid group names. --marekm * Check for invalid group names. --marekm
*/ */
if (!check_group_name (grp->gr_name)) { if (!is_valid_group_name (grp->gr_name)) {
*errors += 1; *errors += 1;
printf (_("invalid group name '%s'\n"), grp->gr_name); printf (_("invalid group name '%s'\n"), grp->gr_name);
} }

View File

@ -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 */ /* 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, fprintf (stderr,
_("%s: invalid group name `%s'\n"), _("%s: invalid group name `%s'\n"),
Prog, grent.gr_name); Prog, grent.gr_name);
@ -318,7 +318,7 @@ static int add_user (const char *name, uid_t uid, gid_t gid)
struct passwd pwent; struct passwd pwent;
/* Check if this is a valid user name */ /* Check if this is a valid user name */
if (check_user_name (name) == 0) { if (!is_valid_user_name (name)) {
fprintf (stderr, fprintf (stderr,
_("%s: invalid user name `%s'\n"), _("%s: invalid user name `%s'\n"),
Prog, name); Prog, name);

View File

@ -354,7 +354,7 @@ static void check_pw_file (int *errors, int *changed)
/* /*
* Check for invalid usernames. --marekm * 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); printf (_("invalid user name '%s'\n"), pwd->pw_name);
*errors += 1; *errors += 1;
} }

View File

@ -1123,7 +1123,7 @@ static void process_flags (int argc, char **argv)
usage (); usage ();
user_name = argv[optind]; user_name = argv[optind];
if (!check_user_name (user_name)) { if (!is_valid_user_name (user_name)) {
fprintf (stderr, fprintf (stderr,
_ _
("%s: invalid user name '%s'\n"), ("%s: invalid user name '%s'\n"),

View File

@ -917,7 +917,7 @@ static void process_flags (int argc, char **argv)
Gflg++; Gflg++;
break; break;
case 'l': case 'l':
if (!check_user_name (optarg)) { if (!is_valid_user_name (optarg)) {
fprintf (stderr, fprintf (stderr,
_("%s: invalid field '%s'\n"), _("%s: invalid field '%s'\n"),
Prog, optarg); Prog, optarg);