* NEWS, src/newusers.c, src/usermod.c, src/useradd.c,

src/groupmod.c, src/groupadd.c: Make sure no user or group are
	created with an ID set to -1.
This commit is contained in:
nekral-guest 2009-03-08 20:43:15 +00:00
parent c1052e2df2
commit 28d7f83c87
6 changed files with 87 additions and 125 deletions

View File

@ -6,6 +6,9 @@
group IDs. group IDs.
* NEWS, src/grpck.c, src/pwck.c: Issue a warning if an ID is set * NEWS, src/grpck.c, src/pwck.c: Issue a warning if an ID is set
to -1. to -1.
* NEWS, src/newusers.c, src/usermod.c, src/useradd.c,
src/groupmod.c, src/groupadd.c: Make sure no user or group are
created with an ID set to -1.
2009-03-07 Nicolas François <nicolas.francois@centraliens.net> 2009-03-07 Nicolas François <nicolas.francois@centraliens.net>

View File

@ -2,7 +2,7 @@
* Copyright (c) 1991 - 1993, Julianne Frances Haugh * Copyright (c) 1991 - 1993, Julianne Frances Haugh
* Copyright (c) 1996 - 2000, Marek Michałkiewicz * Copyright (c) 1996 - 2000, Marek Michałkiewicz
* Copyright (c) 2000 - 2006, Tomasz Kłoczko * Copyright (c) 2000 - 2006, Tomasz Kłoczko
* Copyright (c) 2007 - 2008, Nicolas François * Copyright (c) 2007 - 2009, Nicolas François
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -98,7 +98,6 @@ static void grp_update (void);
static void check_new_name (void); static void check_new_name (void);
static void close_files (void); static void close_files (void);
static void open_files (void); static void open_files (void);
static gid_t get_gid (const char *gidstr);
static void process_flags (int argc, char **argv); static void process_flags (int argc, char **argv);
static void check_flags (void); static void check_flags (void);
static void check_perms (void); static void check_perms (void);
@ -362,23 +361,6 @@ static void open_files (void)
#endif /* SHADOWGRP */ #endif /* SHADOWGRP */
} }
/*
* get_id - validate and get group ID
*/
static gid_t get_gid (const char *gidstr)
{
long val;
char *errptr;
val = strtol (gidstr, &errptr, 10);
if (('\0' != *errptr) || (errno == ERANGE) || (val < 0)) {
fprintf (stderr, _("%s: invalid numeric argument '%s'\n"),
Prog, gidstr);
exit (E_BAD_ARG);
}
return (gid_t) val;
}
/* /*
* process_flags - parse the command line options * process_flags - parse the command line options
* *
@ -419,7 +401,13 @@ static void process_flags (int argc, char **argv)
break; break;
case 'g': case 'g':
gflg = true; gflg = true;
group_id = get_gid (optarg); if ( (get_gid (optarg, &group_id) == 0)
|| (group_id == (gid_t)-1)) {
fprintf (stderr,
_("%s: invalid group ID '%s'\n"),
Prog, optarg);
exit (E_BAD_ARG);
}
break; break;
case 'h': case 'h':
usage (); usage ();

View File

@ -2,7 +2,7 @@
* Copyright (c) 1991 - 1994, Julianne Frances Haugh * Copyright (c) 1991 - 1994, Julianne Frances Haugh
* Copyright (c) 1996 - 2000, Marek Michałkiewicz * Copyright (c) 1996 - 2000, Marek Michałkiewicz
* Copyright (c) 2000 - 2006, Tomasz Kłoczko * Copyright (c) 2000 - 2006, Tomasz Kłoczko
* Copyright (c) 2007 - 2008, Nicolas François * Copyright (c) 2007 - 2009, Nicolas François
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -104,7 +104,6 @@ static void lock_files (void);
static void prepare_failure_reports (void); static void prepare_failure_reports (void);
static void open_files (void); static void open_files (void);
static void close_files (void); static void close_files (void);
static gid_t get_gid (const char *gidstr);
static void update_primary_groups (gid_t ogid, gid_t ngid); static void update_primary_groups (gid_t ogid, gid_t ngid);
/* /*
@ -324,24 +323,6 @@ static void check_new_name (void)
exit (E_BAD_ARG); exit (E_BAD_ARG);
} }
/*
* get_id - validate and get group ID
*/
static gid_t get_gid (const char *gidstr)
{
long val;
char *errptr;
val = strtol (gidstr, &errptr, 10); /* FIXME: Should be strtoul ? */
if (('\0' != *errptr) || (ERANGE == errno) || (val < 0)) {
fprintf (stderr,
_("%s: invalid numeric argument '%s'\n"),
Prog, gidstr);
exit (E_BAD_ARG);
}
return (gid_t) val;
}
/* /*
* process_flags - perform command line argument setting * process_flags - perform command line argument setting
* *
@ -367,7 +348,13 @@ static void process_flags (int argc, char **argv)
switch (c) { switch (c) {
case 'g': case 'g':
gflg = true; gflg = true;
group_newid = get_gid (optarg); if ( (get_gid (optarg, &group_newid) == 0)
|| (group_newid == (gid_t)-1)) {
fprintf (stderr,
_("%s: invalid group ID '%s'\n"),
Prog, optarg);
exit (E_BAD_ARG);
}
break; break;
case 'n': case 'n':
nflg = true; nflg = true;

View File

@ -2,7 +2,7 @@
* Copyright (c) 1990 - 1993, Julianne Frances Haugh * Copyright (c) 1990 - 1993, Julianne Frances Haugh
* Copyright (c) 1996 - 2000, Marek Michałkiewicz * Copyright (c) 1996 - 2000, Marek Michałkiewicz
* Copyright (c) 2000 - 2006, Tomasz Kłoczko * Copyright (c) 2000 - 2006, Tomasz Kłoczko
* Copyright (c) 2007 - 2008, Nicolas François * Copyright (c) 2007 - 2009, Nicolas François
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -89,7 +89,7 @@ static bool spw_locked = false;
static void usage (void); static void usage (void);
static void fail_exit (int); static void fail_exit (int);
static int add_group (const char *, const char *, gid_t *, gid_t); static int add_group (const char *, const char *, gid_t *, gid_t);
static int get_uid (const char *, uid_t *); static int get_user_id (const char *, uid_t *);
static int add_user (const char *, uid_t, gid_t); static int add_user (const char *, uid_t, gid_t);
static void update_passwd (struct passwd *, const char *); static void update_passwd (struct passwd *, const char *);
static int add_passwd (struct passwd *, const char *); static int add_passwd (struct passwd *, const char *);
@ -193,25 +193,32 @@ static int add_group (const char *name, const char *gid, gid_t *ngid, uid_t uid)
* The GID is a number, which means either this is a brand * The GID is a number, which means either this is a brand
* new group, or an existing group. * new group, or an existing group.
*/ */
char *endptr;
unsigned long int i = strtoul (gid, &endptr, 10); if (get_gid (gid, &grent.gr_gid) == 0) {
if ((*endptr != '\0') || (ERANGE == errno)) {
fprintf (stderr, fprintf (stderr,
_("%s: group ID '%s' is not valid\n"), _("%s: invalid group ID '%s'\n"),
Prog, gid); Prog, gid);
return -1; return -1;
} }
/* Look in both the system database (getgrgid) and in the /* Look in both the system database (getgrgid) and in the
* internal database (gr_locate_gid), which may contain * internal database (gr_locate_gid), which may contain
* uncommitted changes */ * uncommitted changes */
if ( (getgrgid ((gid_t) i) != NULL) if ( (getgrgid ((gid_t) grent.gr_gid) != NULL)
|| (gr_locate_gid ((gid_t) i) != NULL)) { || (gr_locate_gid ((gid_t) grent.gr_gid) != NULL)) {
/* The user will use this ID for her /* The user will use this ID for her
* primary group */ * primary group */
*ngid = (gid_t) i; *ngid = (gid_t) grent.gr_gid;
return 0; return 0;
} }
grent.gr_gid = (gid_t) i;
/* Do not create groups with GID == (gid_t)-1 */
if (grent.gr_gid == (gid_t)-1) {
fprintf (stderr,
_("%s: invalid group ID '%s'\n"),
Prog, gid);
return -1;
}
} else { } else {
/* The gid parameter can be "" or a name which is not /* The gid parameter can be "" or a name which is not
* already the name of an existing group. * already the name of an existing group.
@ -282,22 +289,19 @@ static int add_group (const char *name, const char *gid, gid_t *ngid, uid_t uid)
return 0; return 0;
} }
static int get_uid (const char *uid, uid_t *nuid) { static int get_user_id (const char *uid, uid_t *nuid) {
/* /*
* The first guess for the UID is either the numerical UID that the * The first guess for the UID is either the numerical UID that the
* caller provided, or the next available UID. * caller provided, or the next available UID.
*/ */
if (isdigit (uid[0])) { if (isdigit (uid[0])) {
char *endptr; if ((get_uid (uid, nuid) == 0) || (*nuid == (uid_t)-1)) {
unsigned long int i = strtoul (uid, &endptr, 10);
if (('\0' != *endptr) || (ERANGE == errno)) {
fprintf (stderr, fprintf (stderr,
_("%s: user ID '%s' is not valid\n"), _("%s: invalid user ID '%s'\n"),
Prog, uid); Prog, uid);
return -1; return -1;
} }
*nuid = (uid_t) i;
} else { } else {
if ('\0' != uid[0]) { if ('\0' != uid[0]) {
const struct passwd *pwd; const struct passwd *pwd;
@ -824,7 +828,7 @@ int main (int argc, char **argv)
} }
if ( (NULL == pw) if ( (NULL == pw)
&& (get_uid (fields[2], &uid) != 0)) { && (get_user_id (fields[2], &uid) != 0)) {
fprintf (stderr, fprintf (stderr,
_("%s: line %d: can't create user\n"), _("%s: line %d: can't create user\n"),
Prog, line); Prog, line);

View File

@ -2,7 +2,7 @@
* Copyright (c) 1991 - 1994, Julianne Frances Haugh * Copyright (c) 1991 - 1994, Julianne Frances Haugh
* Copyright (c) 1996 - 2000, Marek Michałkiewicz * Copyright (c) 1996 - 2000, Marek Michałkiewicz
* Copyright (c) 2000 - 2006, Tomasz Kłoczko * Copyright (c) 2000 - 2006, Tomasz Kłoczko
* Copyright (c) 2007 - 2008, Nicolas François * Copyright (c) 2007 - 2009, Nicolas François
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -171,7 +171,6 @@ static bool home_added = false;
static void fail_exit (int); static void fail_exit (int);
static struct group *getgr_nam_gid (const char *); static struct group *getgr_nam_gid (const char *);
static long get_number (const char *); static long get_number (const char *);
static uid_t get_uid (const char *);
static void get_defaults (void); static void get_defaults (void);
static void show_defaults (void); static void show_defaults (void);
static int set_defaults (void); static int set_defaults (void);
@ -268,11 +267,15 @@ static void fail_exit (int code)
static struct group *getgr_nam_gid (const char *grname) static struct group *getgr_nam_gid (const char *grname)
{ {
long gid; long long int gid;
char *errptr; char *endptr;
gid = strtol (grname, &errptr, 10); errno = 0;
if (*grname != '\0' && *errptr == '\0' && errno != ERANGE && gid >= 0) { gid = strtoll (grname, &errptr, 10);
if ( ('\0' != *grname)
&& ('\0' == *endptr)
&& (ERANGE != errno)
&& (gid == (gid_t)gid)) {
return xgetgrgid ((gid_t) gid); return xgetgrgid ((gid_t) gid);
} }
return xgetgrnam (grname); return xgetgrnam (grname);
@ -284,29 +287,15 @@ static long get_number (const char *numstr)
char *errptr; char *errptr;
val = strtol (numstr, &errptr, 10); val = strtol (numstr, &errptr, 10);
if (('\0' != *errptr) || (ERANGE == errno)) { if (('\0' == *numstr) || ('\0' != *errptr) || (ERANGE == errno)) {
fprintf (stderr, _("%s: invalid numeric argument '%s'\n"), Prog, fprintf (stderr,
numstr); _("%s: invalid numeric argument '%s'\n"),
Prog, numstr);
exit (E_BAD_ARG); exit (E_BAD_ARG);
} }
return val; return val;
} }
static uid_t get_uid (const char *uidstr)
{
long val;
char *errptr;
val = strtol (uidstr, &errptr, 10);
if (('\0' != *errptr) || (ERANGE == errno) || (val < 0)) {
fprintf (stderr,
_("%s: invalid numeric argument '%s'\n"), Prog,
uidstr);
exit (E_BAD_ARG);
}
return (uid_t) val;
}
#define MATCH(x,y) (strncmp((x),(y),strlen(y)) == 0) #define MATCH(x,y) (strncmp((x),(y),strlen(y)) == 0)
/* /*
@ -352,27 +341,14 @@ static void get_defaults (void)
* Primary GROUP identifier * Primary GROUP identifier
*/ */
if (MATCH (buf, DGROUP)) { if (MATCH (buf, DGROUP)) {
unsigned int val = (unsigned int) strtoul (cp, &ep, 10); const struct group *grp = getgr_nam_gid (cp);
const struct group *grp; if (NULL == grp) {
if (*cp != '\0' && *ep == '\0') { /* valid number */
def_group = val;
/* local, no need for xgetgrgid */
grp = getgrgid (def_group);
if (NULL != grp) {
def_gname = xstrdup (grp->gr_name);
} else {
fprintf (stderr,
_("%s: GID '%s' does not exist\n"),
Prog, cp);
}
/* local, no need for xgetgrnam */
} else if ((grp = getgrnam (cp)) != NULL) {
def_group = grp->gr_gid;
def_gname = xstrdup (cp);
} else {
fprintf (stderr, fprintf (stderr,
_("%s: group '%s' does not exist\n"), Prog, cp); _("%s: group '%s' does not exist\n"),
Prog, cp);
} else {
def_group = grp->gr_gid;
def_gname = xstrdup (grp->gr_name);
} }
} }
@ -396,7 +372,10 @@ static void get_defaults (void)
else if (MATCH (buf, INACT)) { else if (MATCH (buf, INACT)) {
long val = strtol (cp, &ep, 10); long val = strtol (cp, &ep, 10);
if (('\0' != *cp) || (ERANGE == errno)) { if ( ('\0' != *cp)
&& ('\0' == *ep)
&& (ERANGE != errno)
&& (val >= 0)) {
def_inactive = val; def_inactive = val;
} else { } else {
def_inactive = -1; def_inactive = -1;
@ -1173,7 +1152,13 @@ static void process_flags (int argc, char **argv)
sflg = true; sflg = true;
break; break;
case 'u': case 'u':
user_id = get_uid (optarg); if ( (get_uid (optarg, &user_id) == 0)
|| (user_id == (gid_t)-1)) {
fprintf (stderr,
_("%s: invalid user ID '%s'\n"),
Prog, optarg);
exit (E_BAD_ARG);
}
uflg = true; uflg = true;
break; break;
case 'U': case 'U':

View File

@ -2,7 +2,7 @@
* Copyright (c) 1991 - 1994, Julianne Frances Haugh * Copyright (c) 1991 - 1994, Julianne Frances Haugh
* Copyright (c) 1996 - 2000, Marek Michałkiewicz * Copyright (c) 1996 - 2000, Marek Michałkiewicz
* Copyright (c) 2000 - 2006, Tomasz Kłoczko * Copyright (c) 2000 - 2006, Tomasz Kłoczko
* Copyright (c) 2007 - 2008, Nicolas François * Copyright (c) 2007 - 2009, Nicolas François
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -154,7 +154,6 @@ static void update_gshadow (void);
static void grp_update (void); static void grp_update (void);
static long get_number (const char *); static long get_number (const char *);
static uid_t get_id (const char *);
static void process_flags (int, char **); static void process_flags (int, char **);
static void close_files (void); static void close_files (void);
static void open_files (void); static void open_files (void);
@ -193,11 +192,15 @@ static void date_to_str (char *buf, size_t maxsize,
*/ */
static struct group *getgr_nam_gid (const char *grname) static struct group *getgr_nam_gid (const char *grname)
{ {
long val; long long int val;
char *errptr; char *endptr;
val = strtol (grname, &errptr, 10); errno = 0;
if (*grname != '\0' && *errptr == '\0' && errno != ERANGE && val >= 0) { val = strtoll (grname, &errptr, 10);
if ( ('\0' != *grname)
&& ('\0' == *endptr)
&& (ERANGE != errno)
&& (val == (gid_t)val)) {
return xgetgrgid ((gid_t) val); return xgetgrgid ((gid_t) val);
} }
return xgetgrnam (grname); return xgetgrnam (grname);
@ -795,20 +798,6 @@ static long get_number (const char *numstr)
return val; return val;
} }
static uid_t get_id (const char *uidstr)
{
long val;
char *errptr;
val = strtol (uidstr, &errptr, 10);
if (('\0' != *errptr) || (ERANGE == errno) || (val < 0)) {
fprintf (stderr, _("%s: invalid numeric argument '%s'\n"), Prog,
uidstr);
exit (E_BAD_ARG);
}
return (uid_t) val;
}
/* /*
* process_flags - perform command line argument setting * process_flags - perform command line argument setting
* *
@ -1003,7 +992,13 @@ static void process_flags (int argc, char **argv)
sflg = true; sflg = true;
break; break;
case 'u': case 'u':
user_newid = get_id (optarg); if ( (get_uid (optarg, &user_newid) ==0)
|| (user_newid == (uid_t)-1)) {
fprintf (stderr,
_("%s: invalid user ID '%s'\n"),
Prog, optarg);
exit (E_BAD_ARG);
}
uflg = true; uflg = true;
break; break;
case 'U': case 'U':