* src/useradd.c: Use a bool when possible instead of int integers.

* src/useradd.c: Avoid implicit conversion of pointers / integers
	/ chars to booleans.
	* src/useradd.c: Add brackets and parenthesis.
	* src/useradd.c: Avoid assignments in comparisons.
	* src/useradd.c: Ignore the return value of fclose() for read-only
	files.
	* src/useradd.c: Ignore the return value of fflush() before
	closing the files.
	* src/useradd.c: Avoid multi-statements lines.
	* src/useradd.c: Ignore return value of setlocale(),
	bindtextdomain(), and textdomain().
	* src/useradd.c: Ignore the return value of pam_end() before
	exiting.
This commit is contained in:
nekral-guest 2008-06-09 22:08:08 +00:00
parent fdb577e0a0
commit 63f0e5e2c0
2 changed files with 201 additions and 154 deletions

View File

@ -1,3 +1,20 @@
2008-06-09 Nicolas François <nicolas.francois@centraliens.net>
* src/useradd.c: Use a bool when possible instead of int integers.
* src/useradd.c: Avoid implicit conversion of pointers / integers
/ chars to booleans.
* src/useradd.c: Add brackets and parenthesis.
* src/useradd.c: Avoid assignments in comparisons.
* src/useradd.c: Ignore the return value of fclose() for read-only
files.
* src/useradd.c: Ignore the return value of fflush() before
closing the files.
* src/useradd.c: Avoid multi-statements lines.
* src/useradd.c: Ignore return value of setlocale(),
bindtextdomain(), and textdomain().
* src/useradd.c: Ignore the return value of pam_end() before
exiting.
2008-06-09 Nicolas François <nicolas.francois@centraliens.net> 2008-06-09 Nicolas François <nicolas.francois@centraliens.net>
* src/vipw.c: Use a bool when possible instead of int integers. * src/vipw.c: Use a bool when possible instead of int integers.

View File

@ -106,41 +106,41 @@ static const char *user_shell = "";
static const char *create_mail_spool = ""; static const char *create_mail_spool = "";
static long user_expire = -1; static long user_expire = -1;
static int is_shadow_pwd; static bool is_shadow_pwd;
#ifdef SHADOWGRP #ifdef SHADOWGRP
static int is_shadow_grp; static bool is_shadow_grp;
static int gshadow_locked = 0; static bool gshadow_locked = false;
#endif #endif
static int passwd_locked = 0; static bool passwd_locked = false;
static int group_locked = 0; static bool group_locked = false;
static int shadow_locked = 0; static bool shadow_locked = false;
static char **user_groups; /* NULL-terminated list */ static char **user_groups; /* NULL-terminated list */
static long sys_ngroups; static long sys_ngroups;
static int do_grp_update = 0; /* group files need to be updated */ static bool do_grp_update = false; /* group files need to be updated */
static char *Prog; static char *Prog;
static int static bool
bflg = 0, /* new default root of home directory */ bflg = false, /* new default root of home directory */
cflg = 0, /* comment (GECOS) field for new account */ cflg = false, /* comment (GECOS) field for new account */
dflg = 0, /* home directory for new account */ dflg = false, /* home directory for new account */
Dflg = 0, /* set/show new user default values */ Dflg = false, /* set/show new user default values */
eflg = 0, /* days since 1970-01-01 when account is locked */ eflg = false, /* days since 1970-01-01 when account is locked */
fflg = 0, /* days until account with expired password is locked */ fflg = false, /* days until account with expired password is locked */
gflg = 0, /* primary group ID for new account */ gflg = false, /* primary group ID for new account */
Gflg = 0, /* secondary group set for new account */ Gflg = false, /* secondary group set for new account */
kflg = 0, /* specify a directory to fill new user directory */ kflg = false, /* specify a directory to fill new user directory */
lflg = 0, /* do not add user to lastlog database file */ lflg = false, /* do not add user to lastlog database file */
mflg = 0, /* create user's home directory if it doesn't exist */ mflg = false, /* create user's home directory if it doesn't exist */
Nflg = 0, /* do not create a group having the same name as the user, but add the user to def_group (or the group specified with -g) */ Nflg = false, /* do not create a group having the same name as the user, but add the user to def_group (or the group specified with -g) */
oflg = 0, /* permit non-unique user ID to be specified with -u */ oflg = false, /* permit non-unique user ID to be specified with -u */
rflg = 0, /* create a system account */ rflg = false, /* create a system account */
sflg = 0, /* shell program for new account */ sflg = false, /* shell program for new account */
uflg = 0, /* specify user ID for new account */ uflg = false, /* specify user ID for new account */
Uflg = 0; /* create a group having the same name as the user */ Uflg = false; /* create a group having the same name as the user */
static int home_added; static bool home_added = false;
/* /*
* exit status values * exit status values
@ -237,7 +237,7 @@ static long get_number (const char *numstr)
char *errptr; char *errptr;
val = strtol (numstr, &errptr, 10); val = strtol (numstr, &errptr, 10);
if (*errptr || errno == ERANGE) { if (('\0' != *errptr) || (ERANGE == errno)) {
fprintf (stderr, _("%s: invalid numeric argument '%s'\n"), Prog, fprintf (stderr, _("%s: invalid numeric argument '%s'\n"), Prog,
numstr); numstr);
exit (E_BAD_ARG); exit (E_BAD_ARG);
@ -251,7 +251,7 @@ static uid_t get_uid (const char *uidstr)
char *errptr; char *errptr;
val = strtol (uidstr, &errptr, 10); val = strtol (uidstr, &errptr, 10);
if (*errptr || errno == ERANGE || val < 0) { if (('\0' != *errptr) || (ERANGE == errno) || (val < 0)) {
fprintf (stderr, fprintf (stderr,
_("%s: invalid numeric argument '%s'\n"), Prog, _("%s: invalid numeric argument '%s'\n"), Prog,
uidstr); uidstr);
@ -279,19 +279,25 @@ static void get_defaults (void)
* Open the defaults file for reading. * Open the defaults file for reading.
*/ */
if (!(fp = fopen (def_file, "r"))) fp = fopen (def_file, "r");
if (NULL == fp) {
return; return;
}
/* /*
* Read the file a line at a time. Only the lines that have relevant * Read the file a line at a time. Only the lines that have relevant
* values are used, everything else can be ignored. * values are used, everything else can be ignored.
*/ */
while (fgets (buf, sizeof buf, fp)) { while (fgets (buf, sizeof buf, fp) == buf) {
if ((cp = strrchr (buf, '\n'))) cp = strrchr (buf, '\n');
if (NULL != cp) {
*cp = '\0'; *cp = '\0';
}
if (!(cp = strchr (buf, '='))) cp = strchr (buf, '=');
if (NULL == cp) {
continue; continue;
}
cp++; cp++;
@ -305,7 +311,8 @@ static void get_defaults (void)
if (*cp != '\0' && *ep == '\0') { /* valid number */ if (*cp != '\0' && *ep == '\0') { /* valid number */
def_group = val; def_group = val;
/* local, no need for xgetgrgid */ /* local, no need for xgetgrgid */
if ((grp = getgrgid (def_group))) { grp = getgrgid (def_group);
if (NULL != grp) {
def_gname = xstrdup (grp->gr_name); def_gname = xstrdup (grp->gr_name);
} else { } else {
fprintf (stderr, fprintf (stderr,
@ -313,7 +320,7 @@ static void get_defaults (void)
Prog, cp); Prog, cp);
} }
/* local, no need for xgetgrnam */ /* local, no need for xgetgrnam */
} else if ((grp = getgrnam (cp))) { } else if ((grp = getgrnam (cp)) != NULL) {
def_group = grp->gr_gid; def_group = grp->gr_gid;
def_gname = xstrdup (cp); def_gname = xstrdup (cp);
} else { } else {
@ -342,10 +349,11 @@ 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 (*cp || errno == ERANGE) if (('\0' != *cp) || (ERANGE == errno)) {
def_inactive = val; def_inactive = val;
else } else {
def_inactive = -1; def_inactive = -1;
}
} }
/* /*
@ -359,8 +367,9 @@ static void get_defaults (void)
* Default Skeleton information * Default Skeleton information
*/ */
else if (MATCH (buf, SKEL)) { else if (MATCH (buf, SKEL)) {
if (*cp == '\0') if ('\0' == *cp) {
cp = SKEL_DIR; /* XXX warning: const */ cp = SKEL_DIR; /* XXX warning: const */
}
def_template = xstrdup (cp); def_template = xstrdup (cp);
} }
@ -369,8 +378,9 @@ static void get_defaults (void)
* Create by default user mail spool or not ? * Create by default user mail spool or not ?
*/ */
else if (MATCH (buf, CREATE_MAIL_SPOOL)) { else if (MATCH (buf, CREATE_MAIL_SPOOL)) {
if (*cp == '\0') if (*cp == '\0') {
cp = CREATE_MAIL_SPOOL; /* XXX warning: const */ cp = CREATE_MAIL_SPOOL; /* XXX warning: const */
}
def_create_mail_spool = xstrdup (cp); def_create_mail_spool = xstrdup (cp);
} }
@ -409,24 +419,26 @@ static int set_defaults (void)
static char new_file[] = NEW_USER_FILE; static char new_file[] = NEW_USER_FILE;
char *cp; char *cp;
int ofd; int ofd;
int out_group = 0; bool out_group = false;
int out_home = 0; bool out_home = false;
int out_inactive = 0; bool out_inactive = false;
int out_expire = 0; bool out_expire = false;
int out_shell = 0; bool out_shell = false;
int out_skel = 0; bool out_skel = false;
int out_create_mail_spool = 0; bool out_create_mail_spool = false;
/* /*
* Create a temporary file to copy the new output to. * Create a temporary file to copy the new output to.
*/ */
if ((ofd = mkstemp (new_file)) == -1) { ofd = mkstemp (new_file);
if (-1 == ofd) {
fprintf (stderr, fprintf (stderr,
_("%s: cannot create new defaults file\n"), Prog); _("%s: cannot create new defaults file\n"), Prog);
return -1; return -1;
} }
if (!(ofp = fdopen (ofd, "w"))) { ofp = fdopen (ofd, "w");
if (NULL == ofp) {
fprintf (stderr, _("%s: cannot open new defaults file\n"), fprintf (stderr, _("%s: cannot open new defaults file\n"),
Prog); Prog);
return -1; return -1;
@ -437,42 +449,45 @@ static int set_defaults (void)
* temporary file, using any new values. Each line is checked * temporary file, using any new values. Each line is checked
* to insure that it is not output more than once. * to insure that it is not output more than once.
*/ */
if (!(ifp = fopen (def_file, "r"))) { ifp = fopen (def_file, "r");
if (NULL == ifp) {
fprintf (ofp, "# useradd defaults file\n"); fprintf (ofp, "# useradd defaults file\n");
goto skip; goto skip;
} }
while (fgets (buf, sizeof buf, ifp)) { while (fgets (buf, sizeof buf, ifp) == buf) {
if ((cp = strrchr (buf, '\n'))) cp = strrchr (buf, '\n');
if (NULL != cp) {
*cp = '\0'; *cp = '\0';
}
if (!out_group && MATCH (buf, DGROUP)) { if (!out_group && MATCH (buf, DGROUP)) {
fprintf (ofp, DGROUP "%u\n", (unsigned int) def_group); fprintf (ofp, DGROUP "%u\n", (unsigned int) def_group);
out_group++; out_group = true;
} else if (!out_home && MATCH (buf, HOME)) { } else if (!out_home && MATCH (buf, HOME)) {
fprintf (ofp, HOME "%s\n", def_home); fprintf (ofp, HOME "%s\n", def_home);
out_home++; out_home = true;
} else if (!out_inactive && MATCH (buf, INACT)) { } else if (!out_inactive && MATCH (buf, INACT)) {
fprintf (ofp, INACT "%ld\n", def_inactive); fprintf (ofp, INACT "%ld\n", def_inactive);
out_inactive++; out_inactive = true;
} else if (!out_expire && MATCH (buf, EXPIRE)) { } else if (!out_expire && MATCH (buf, EXPIRE)) {
fprintf (ofp, EXPIRE "%s\n", def_expire); fprintf (ofp, EXPIRE "%s\n", def_expire);
out_expire++; out_expire = true;
} else if (!out_shell && MATCH (buf, SHELL)) { } else if (!out_shell && MATCH (buf, SHELL)) {
fprintf (ofp, SHELL "%s\n", def_shell); fprintf (ofp, SHELL "%s\n", def_shell);
out_shell++; out_shell = true;
} else if (!out_skel && MATCH (buf, SKEL)) { } else if (!out_skel && MATCH (buf, SKEL)) {
fprintf (ofp, SKEL "%s\n", def_template); fprintf (ofp, SKEL "%s\n", def_template);
out_skel++; out_skel = true;
} else if (!out_create_mail_spool } else if (!out_create_mail_spool
&& MATCH (buf, CREATE_MAIL_SPOOL)) { && MATCH (buf, CREATE_MAIL_SPOOL)) {
fprintf (ofp, CREATE_MAIL_SPOOL "%s\n", fprintf (ofp, CREATE_MAIL_SPOOL "%s\n",
def_create_mail_spool); def_create_mail_spool);
out_create_mail_spool++; out_create_mail_spool = true;
} else } else
fprintf (ofp, "%s\n", buf); fprintf (ofp, "%s\n", buf);
} }
fclose (ifp); (void) fclose (ifp);
skip: skip:
/* /*
@ -500,8 +515,8 @@ static int set_defaults (void)
* Flush and close the file. Check for errors to make certain * Flush and close the file. Check for errors to make certain
* the new file is intact. * the new file is intact.
*/ */
fflush (ofp); (void) fflush (ofp);
if (ferror (ofp) || fclose (ofp)) { if ((ferror (ofp) != 0) || (fclose (ofp) != 0)) {
unlink (new_file); unlink (new_file);
return -1; return -1;
} }
@ -510,7 +525,7 @@ static int set_defaults (void)
* Rename the current default file to its backup name. * Rename the current default file to its backup name.
*/ */
snprintf (buf, sizeof buf, "%s-", def_file); snprintf (buf, sizeof buf, "%s-", def_file);
if (rename (def_file, buf) && errno != ENOENT) { if ((rename (def_file, buf) != 0) && (ENOENT != errno)) {
snprintf (buf, sizeof buf, _("%s: rename: %s"), Prog, def_file); snprintf (buf, sizeof buf, _("%s: rename: %s"), Prog, def_file);
perror (buf); perror (buf);
unlink (new_file); unlink (new_file);
@ -520,7 +535,7 @@ static int set_defaults (void)
/* /*
* Rename the new default file to its correct name. * Rename the new default file to its correct name.
*/ */
if (rename (new_file, def_file)) { if (rename (new_file, def_file) != 0) {
snprintf (buf, sizeof buf, _("%s: rename: %s"), Prog, new_file); snprintf (buf, sizeof buf, _("%s: rename: %s"), Prog, new_file);
perror (buf); perror (buf);
return -1; return -1;
@ -552,8 +567,9 @@ static int get_groups (char *list)
int errors = 0; int errors = 0;
int ngroups = 0; int ngroups = 0;
if (!*list) if ('\0' == *list) {
return 0; return 0;
}
/* /*
* So long as there is some data to be converted, strip off * So long as there is some data to be converted, strip off
@ -564,8 +580,10 @@ static int get_groups (char *list)
/* /*
* Strip off a single name from the list * Strip off a single name from the list
*/ */
if ((cp = strchr (list, ','))) cp = strchr (list, ',');
if (NULL != cp) {
*cp++ = '\0'; *cp++ = '\0';
}
/* /*
* Names starting with digits are treated as numerical * Names starting with digits are treated as numerical
@ -577,7 +595,7 @@ static int get_groups (char *list)
* There must be a match, either by GID value or by * There must be a match, either by GID value or by
* string name. * string name.
*/ */
if (!grp) { if (NULL == grp) {
fprintf (stderr, _("%s: unknown group %s\n"), fprintf (stderr, _("%s: unknown group %s\n"),
Prog, list); Prog, list);
errors++; errors++;
@ -588,8 +606,9 @@ static int get_groups (char *list)
* If the group doesn't exist, don't dump core... * If the group doesn't exist, don't dump core...
* Instead, try the next one. --marekm * Instead, try the next one. --marekm
*/ */
if (!grp) if (NULL == grp) {
continue; continue;
}
#ifdef USE_NIS #ifdef USE_NIS
/* /*
@ -616,15 +635,16 @@ static int get_groups (char *list)
* Add the group name to the user's list of groups. * Add the group name to the user's list of groups.
*/ */
user_groups[ngroups++] = xstrdup (grp->gr_name); user_groups[ngroups++] = xstrdup (grp->gr_name);
} while (list); } while (NULL != list);
user_groups[ngroups] = (char *) 0; user_groups[ngroups] = (char *) 0;
/* /*
* Any errors in finding group names are fatal * Any errors in finding group names are fatal
*/ */
if (errors) if (0 != errors) {
return -1; return -1;
}
return 0; return 0;
} }
@ -767,7 +787,7 @@ static void grp_update (void)
* from the old entry, and we need it later. * from the old entry, and we need it later.
*/ */
ngrp = __gr_dup (grp); ngrp = __gr_dup (grp);
if (!ngrp) { if (NULL == ngrp) {
fprintf (stderr, fprintf (stderr,
_("%s: Out of memory. Cannot update the group database.\n"), _("%s: Out of memory. Cannot update the group database.\n"),
Prog); Prog);
@ -779,7 +799,7 @@ static void grp_update (void)
* update the group entry to reflect the change. * update the group entry to reflect the change.
*/ */
ngrp->gr_mem = add_list (ngrp->gr_mem, user_name); ngrp->gr_mem = add_list (ngrp->gr_mem, user_name);
if (!gr_update (ngrp)) { if (gr_update (ngrp) == 0) {
fprintf (stderr, fprintf (stderr,
_("%s: error adding new group entry\n"), Prog); _("%s: error adding new group entry\n"), Prog);
fail_exit (E_GRP_UPDATE); fail_exit (E_GRP_UPDATE);
@ -801,13 +821,13 @@ static void grp_update (void)
* that the user is a member of. The administrative list isn't * that the user is a member of. The administrative list isn't
* modified. * modified.
*/ */
for (sgr_rewind (), sgrp = sgr_next (); sgrp; sgrp = sgr_next ()) { for (sgr_rewind (), sgrp = sgr_next (); NULL != sgrp; sgrp = sgr_next ()) {
/* /*
* See if the user specified this group as one of their * See if the user specified this group as one of their
* concurrent groups. * concurrent groups.
*/ */
if (!gr_locate (sgrp->sg_name)) if (gr_locate (sgrp->sg_name) == NULL)
continue; continue;
if (!is_on_list (user_groups, sgrp->sg_name)) if (!is_on_list (user_groups, sgrp->sg_name))
@ -818,7 +838,7 @@ static void grp_update (void)
* from the old entry, and we need it later. * from the old entry, and we need it later.
*/ */
nsgrp = __sgr_dup (sgrp); nsgrp = __sgr_dup (sgrp);
if (!nsgrp) { if (NULL == nsgrp) {
fprintf (stderr, fprintf (stderr,
_("%s: Out of memory. Cannot update the shadow group database.\n"), _("%s: Out of memory. Cannot update the shadow group database.\n"),
Prog); Prog);
@ -830,7 +850,7 @@ static void grp_update (void)
* update the group entry to reflect the change. * update the group entry to reflect the change.
*/ */
nsgrp->sg_mem = add_list (nsgrp->sg_mem, user_name); nsgrp->sg_mem = add_list (nsgrp->sg_mem, user_name);
if (!sgr_update (nsgrp)) { if (sgr_update (nsgrp) == 0) {
fprintf (stderr, fprintf (stderr,
_("%s: error adding new group entry\n"), Prog); _("%s: error adding new group entry\n"), Prog);
fail_exit (E_GRP_UPDATE); fail_exit (E_GRP_UPDATE);
@ -855,7 +875,7 @@ static void grp_update (void)
static void process_flags (int argc, char **argv) static void process_flags (int argc, char **argv)
{ {
const struct group *grp; const struct group *grp;
int anyflag = 0; bool anyflag = false;
char *cp; char *cp;
{ {
@ -899,7 +919,7 @@ static void process_flags (int argc, char **argv)
exit (E_BAD_ARG); exit (E_BAD_ARG);
} }
def_home = optarg; def_home = optarg;
bflg++; bflg = true;
break; break;
case 'c': case 'c':
if (!VALID (optarg)) { if (!VALID (optarg)) {
@ -910,7 +930,7 @@ static void process_flags (int argc, char **argv)
exit (E_BAD_ARG); exit (E_BAD_ARG);
} }
user_comment = optarg; user_comment = optarg;
cflg++; cflg = true;
break; break;
case 'd': case 'd':
if (!VALID (optarg) if (!VALID (optarg)
@ -922,15 +942,15 @@ static void process_flags (int argc, char **argv)
exit (E_BAD_ARG); exit (E_BAD_ARG);
} }
user_home = optarg; user_home = optarg;
dflg++; dflg = true;
break; break;
case 'D': case 'D':
if (anyflag) if (anyflag)
usage (); usage ();
Dflg++; Dflg = true;
break; break;
case 'e': case 'e':
if (*optarg) { if ('\0' != *optarg) {
user_expire = strtoday (optarg); user_expire = strtoday (optarg);
if (user_expire == -1) { if (user_expire == -1) {
fprintf (stderr, fprintf (stderr,
@ -945,7 +965,7 @@ static void process_flags (int argc, char **argv)
/* /*
* -e "" is allowed - it's a no-op without /etc/shadow * -e "" is allowed - it's a no-op without /etc/shadow
*/ */
if (*optarg && !is_shadow_pwd) { if (('\0' != *optarg) && !is_shadow_pwd) {
fprintf (stderr, fprintf (stderr,
_ _
("%s: shadow passwords required for -e\n"), ("%s: shadow passwords required for -e\n"),
@ -954,25 +974,25 @@ static void process_flags (int argc, char **argv)
} }
if (Dflg) if (Dflg)
def_expire = optarg; def_expire = optarg;
eflg++; eflg = true;
break; break;
case 'f': case 'f':
def_inactive = get_number (optarg); def_inactive = get_number (optarg);
/* /*
* -f -1 is allowed - it's a no-op without /etc/shadow * -f -1 is allowed - it's a no-op without /etc/shadow
*/ */
if (def_inactive != -1 && !is_shadow_pwd) { if ((-1 != def_inactive) && !is_shadow_pwd) {
fprintf (stderr, fprintf (stderr,
_ _
("%s: shadow passwords required for -f\n"), ("%s: shadow passwords required for -f\n"),
Prog); Prog);
exit (E_USAGE); exit (E_USAGE);
} }
fflg++; fflg = true;
break; break;
case 'g': case 'g':
grp = getgr_nam_gid (optarg); grp = getgr_nam_gid (optarg);
if (!grp) { if (NULL == grp) {
fprintf (stderr, fprintf (stderr,
_ _
("%s: unknown group %s\n"), ("%s: unknown group %s\n"),
@ -985,21 +1005,23 @@ static void process_flags (int argc, char **argv)
} else { } else {
user_gid = grp->gr_gid; user_gid = grp->gr_gid;
} }
gflg++; gflg = true;
break; break;
case 'G': case 'G':
if (get_groups (optarg)) if (get_groups (optarg) != 0) {
exit (E_NOTFOUND); exit (E_NOTFOUND);
if (user_groups[0]) }
do_grp_update++; if (NULL != user_groups[0]) {
Gflg++; do_grp_update = true;
}
Gflg = true;
break; break;
case 'h': case 'h':
usage (); usage ();
break; break;
case 'k': case 'k':
def_template = optarg; def_template = optarg;
kflg++; kflg = true;
break; break;
case 'K': case 'K':
/* /*
@ -1008,7 +1030,7 @@ static void process_flags (int argc, char **argv)
* note: -K UID_MIN=10,UID_MAX=499 doesn't work yet * note: -K UID_MIN=10,UID_MAX=499 doesn't work yet
*/ */
cp = strchr (optarg, '='); cp = strchr (optarg, '=');
if (!cp) { if (NULL == cp) {
fprintf (stderr, fprintf (stderr,
_ _
("%s: -K requires KEY=VALUE\n"), ("%s: -K requires KEY=VALUE\n"),
@ -1016,21 +1038,22 @@ static void process_flags (int argc, char **argv)
exit (E_BAD_ARG); exit (E_BAD_ARG);
} }
/* terminate name, point to value */ /* terminate name, point to value */
*cp++ = '\0'; *cp = '\0';
cp++;
if (putdef_str (optarg, cp) < 0) if (putdef_str (optarg, cp) < 0)
exit (E_BAD_ARG); exit (E_BAD_ARG);
break; break;
case 'l': case 'l':
lflg++; lflg = true;
break; break;
case 'm': case 'm':
mflg++; mflg = true;
break; break;
case 'N': case 'N':
Nflg++; Nflg = true;
break; break;
case 'o': case 'o':
oflg++; oflg = true;
break; break;
case 'p': /* set encrypted password */ case 'p': /* set encrypted password */
if (!VALID (optarg)) { if (!VALID (optarg)) {
@ -1043,13 +1066,13 @@ static void process_flags (int argc, char **argv)
user_pass = optarg; user_pass = optarg;
break; break;
case 'r': case 'r':
rflg++; rflg = true;
break; break;
case 's': case 's':
if (!VALID (optarg) if (!VALID (optarg)
|| (optarg[0] || ( ('\0' != optarg[0])
&& (optarg[0] != '/' && ('/' != optarg[0])
&& optarg[0] != '*'))) { && ('*' != optarg[0]))) {
fprintf (stderr, fprintf (stderr,
_ _
("%s: invalid shell '%s'\n"), ("%s: invalid shell '%s'\n"),
@ -1058,19 +1081,19 @@ static void process_flags (int argc, char **argv)
} }
user_shell = optarg; user_shell = optarg;
def_shell = optarg; def_shell = optarg;
sflg++; sflg = true;
break; break;
case 'u': case 'u':
user_id = get_uid (optarg); user_id = get_uid (optarg);
uflg++; uflg = true;
break; break;
case 'U': case 'U':
Uflg++; Uflg = true;
break; break;
default: default:
usage (); usage ();
} }
anyflag++; anyflag = true;
} }
} }
@ -1167,23 +1190,23 @@ static void process_flags (int argc, char **argv)
*/ */
static void close_files (void) static void close_files (void)
{ {
if (!pw_close ()) { if (pw_close () == 0) {
fprintf (stderr, _("%s: cannot rewrite password file\n"), Prog); fprintf (stderr, _("%s: cannot rewrite password file\n"), Prog);
fail_exit (E_PW_UPDATE); fail_exit (E_PW_UPDATE);
} }
if (is_shadow_pwd && !spw_close ()) { if (is_shadow_pwd && (spw_close () == 0)) {
fprintf (stderr, fprintf (stderr,
_("%s: cannot rewrite shadow password file\n"), Prog); _("%s: cannot rewrite shadow password file\n"), Prog);
fail_exit (E_PW_UPDATE); fail_exit (E_PW_UPDATE);
} }
if (do_grp_update) { if (do_grp_update) {
if (!gr_close ()) { if (gr_close () == 0) {
fprintf (stderr, fprintf (stderr,
_("%s: cannot rewrite group file\n"), Prog); _("%s: cannot rewrite group file\n"), Prog);
fail_exit (E_GRP_UPDATE); fail_exit (E_GRP_UPDATE);
} }
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow_grp && !sgr_close ()) { if (is_shadow_grp && (sgr_close () == 0)) {
fprintf (stderr, fprintf (stderr,
_ _
("%s: cannot rewrite shadow group file\n"), ("%s: cannot rewrite shadow group file\n"),
@ -1194,16 +1217,16 @@ static void close_files (void)
} }
if (is_shadow_pwd) { if (is_shadow_pwd) {
spw_unlock (); spw_unlock ();
shadow_locked--; shadow_locked = false;
} }
pw_unlock (); pw_unlock ();
passwd_locked--; passwd_locked = false;
gr_unlock (); gr_unlock ();
group_locked--; group_locked = false;
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow_grp) { if (is_shadow_grp) {
sgr_unlock (); sgr_unlock ();
gshadow_locked--; gshadow_locked = false;
} }
#endif #endif
} }
@ -1215,7 +1238,7 @@ static void close_files (void)
*/ */
static void open_files (void) static void open_files (void)
{ {
if (!pw_lock ()) { if (pw_lock () == 0) {
fprintf (stderr, _("%s: unable to lock password file\n"), Prog); fprintf (stderr, _("%s: unable to lock password file\n"), Prog);
#ifdef WITH_AUDIT #ifdef WITH_AUDIT
audit_logger (AUDIT_USER_CHAUTHTOK, Prog, audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
@ -1223,8 +1246,8 @@ static void open_files (void)
#endif #endif
exit (E_PW_UPDATE); exit (E_PW_UPDATE);
} }
passwd_locked++; passwd_locked = true;
if (!pw_open (O_RDWR)) { if (pw_open (O_RDWR) == 0) {
fprintf (stderr, _("%s: unable to open password file\n"), Prog); fprintf (stderr, _("%s: unable to open password file\n"), Prog);
#ifdef WITH_AUDIT #ifdef WITH_AUDIT
audit_logger (AUDIT_USER_CHAUTHTOK, Prog, audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
@ -1233,7 +1256,7 @@ static void open_files (void)
fail_exit (E_PW_UPDATE); fail_exit (E_PW_UPDATE);
} }
if (is_shadow_pwd) { if (is_shadow_pwd) {
if (!spw_lock ()) { if (spw_lock () == 0) {
fprintf (stderr, fprintf (stderr,
_("%s: cannot lock shadow password file\n"), _("%s: cannot lock shadow password file\n"),
Prog); Prog);
@ -1244,8 +1267,8 @@ static void open_files (void)
#endif #endif
fail_exit (E_PW_UPDATE); fail_exit (E_PW_UPDATE);
} }
shadow_locked++; shadow_locked = true;
if (!spw_open (O_RDWR)) { if (spw_open (O_RDWR) == 0) {
fprintf (stderr, fprintf (stderr,
_("%s: cannot open shadow password file\n"), _("%s: cannot open shadow password file\n"),
Prog); Prog);
@ -1261,25 +1284,25 @@ static void open_files (void)
/* /*
* Lock and open the group file. * Lock and open the group file.
*/ */
if (!gr_lock ()) { if (gr_lock () == 0) {
fprintf (stderr, _("%s: error locking group file\n"), Prog); fprintf (stderr, _("%s: error locking group file\n"), Prog);
fail_exit (E_GRP_UPDATE); fail_exit (E_GRP_UPDATE);
} }
group_locked++; group_locked = true;
if (!gr_open (O_RDWR)) { if (gr_open (O_RDWR) == 0) {
fprintf (stderr, _("%s: error opening group file\n"), Prog); fprintf (stderr, _("%s: error opening group file\n"), Prog);
fail_exit (E_GRP_UPDATE); fail_exit (E_GRP_UPDATE);
} }
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow_grp) { if (is_shadow_grp) {
if (!sgr_lock ()) { if (sgr_lock () == 0) {
fprintf (stderr, fprintf (stderr,
_("%s: error locking shadow group file\n"), _("%s: error locking shadow group file\n"),
Prog); Prog);
fail_exit (E_GRP_UPDATE); fail_exit (E_GRP_UPDATE);
} }
gshadow_locked++; gshadow_locked = true;
if (!sgr_open (O_RDWR)) { if (sgr_open (O_RDWR) == 0) {
fprintf (stderr, fprintf (stderr,
_("%s: error opening shadow group file\n"), _("%s: error opening shadow group file\n"),
Prog); Prog);
@ -1351,7 +1374,7 @@ static void grp_add (void)
/* /*
* Write out the new group file entry. * Write out the new group file entry.
*/ */
if (!gr_update (&grp)) { if (gr_update (&grp) == 0) {
fprintf (stderr, _("%s: error adding new group entry\n"), Prog); fprintf (stderr, _("%s: error adding new group entry\n"), Prog);
fail_exit (E_GRP_UPDATE); fail_exit (E_GRP_UPDATE);
} }
@ -1359,13 +1382,13 @@ static void grp_add (void)
/* /*
* Write out the new shadow group entries as well. * Write out the new shadow group entries as well.
*/ */
if (is_shadow_grp && !sgr_update (&sgrp)) { if (is_shadow_grp && (sgr_update (&sgrp) == 0)) {
fprintf (stderr, _("%s: error adding new group entry\n"), Prog); fprintf (stderr, _("%s: error adding new group entry\n"), Prog);
fail_exit (E_GRP_UPDATE); fail_exit (E_GRP_UPDATE);
} }
#endif /* SHADOWGRP */ #endif /* SHADOWGRP */
SYSLOG ((LOG_INFO, "new group: name=%s, GID=%u", user_name, user_gid)); SYSLOG ((LOG_INFO, "new group: name=%s, GID=%u", user_name, user_gid));
do_grp_update++; do_grp_update = true;
} }
static void faillog_reset (uid_t uid) static void faillog_reset (uid_t uid)
@ -1438,7 +1461,7 @@ static void usr_update (void)
/* /*
* Put the new (struct passwd) in the table. * Put the new (struct passwd) in the table.
*/ */
if (!pw_update (&pwent)) { if (pw_update (&pwent) == 0) {
fprintf (stderr, fprintf (stderr,
_("%s: error adding new password entry\n"), Prog); _("%s: error adding new password entry\n"), Prog);
fail_exit (E_PW_UPDATE); fail_exit (E_PW_UPDATE);
@ -1447,7 +1470,7 @@ static void usr_update (void)
/* /*
* Put the new (struct spwd) in the table. * Put the new (struct spwd) in the table.
*/ */
if (is_shadow_pwd && !spw_update (&spent)) { if (is_shadow_pwd && (spw_update (&spent) == 0)) {
fprintf (stderr, fprintf (stderr,
_ _
("%s: error adding new shadow password entry\n"), ("%s: error adding new shadow password entry\n"),
@ -1466,8 +1489,9 @@ static void usr_update (void)
/* /*
* Do any group file updates for this user. * Do any group file updates for this user.
*/ */
if (do_grp_update) if (do_grp_update) {
grp_update (); grp_update ();
}
} }
/* /*
@ -1479,9 +1503,9 @@ static void usr_update (void)
*/ */
static void create_home (void) static void create_home (void)
{ {
if (access (user_home, F_OK)) { if (access (user_home, F_OK) != 0) {
/* XXX - create missing parent directories. --marekm */ /* XXX - create missing parent directories. --marekm */
if (mkdir (user_home, 0)) { if (mkdir (user_home, 0) != 0) {
fprintf (stderr, fprintf (stderr,
_ _
("%s: cannot create directory %s\n"), ("%s: cannot create directory %s\n"),
@ -1496,7 +1520,7 @@ static void create_home (void)
chown (user_home, user_id, user_gid); chown (user_home, user_id, user_gid);
chmod (user_home, chmod (user_home,
0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK)); 0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK));
home_added++; home_added = true;
#ifdef WITH_AUDIT #ifdef WITH_AUDIT
audit_logger (AUDIT_USER_CHAUTHTOK, Prog, audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
"adding home directory", user_name, user_id, 1); "adding home directory", user_name, user_id, 1);
@ -1533,7 +1557,7 @@ static void create_mail (void)
} }
gr = getgrnam ("mail"); /* local, no need for xgetgrnam */ gr = getgrnam ("mail"); /* local, no need for xgetgrnam */
if (!gr) { if (NULL == gr) {
fputs (_("Group 'mail' not found. Creating the user mailbox file with 0600 mode.\n"), fputs (_("Group 'mail' not found. Creating the user mailbox file with 0600 mode.\n"),
stderr); stderr);
gid = user_gid; gid = user_gid;
@ -1543,8 +1567,10 @@ static void create_mail (void)
mode = 0660; mode = 0660;
} }
if (fchown (fd, user_id, gid) || fchmod (fd, mode)) if ( (fchown (fd, user_id, gid) != 0)
|| (fchmod (fd, mode) != 0)) {
perror (_("Setting mailbox file permissions")); perror (_("Setting mailbox file permissions"));
}
close (fd); close (fd);
} }
@ -1569,9 +1595,9 @@ int main (int argc, char **argv)
*/ */
Prog = Basename (argv[0]); Prog = Basename (argv[0]);
setlocale (LC_ALL, ""); (void) setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR); (void) bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE); (void) textdomain (PACKAGE);
OPENLOG ("useradd"); OPENLOG ("useradd");
@ -1602,27 +1628,27 @@ int main (int argc, char **argv)
retval = PAM_USER_UNKNOWN; retval = PAM_USER_UNKNOWN;
} }
if (retval == PAM_SUCCESS) { if (PAM_SUCCESS == retval) {
retval = pam_start ("useradd", pampw->pw_name, retval = pam_start ("useradd", pampw->pw_name,
&conv, &pamh); &conv, &pamh);
} }
} }
if (retval == PAM_SUCCESS) { if (PAM_SUCCESS == retval) {
retval = pam_authenticate (pamh, 0); retval = pam_authenticate (pamh, 0);
if (retval != PAM_SUCCESS) { if (PAM_SUCCESS != retval) {
pam_end (pamh, retval); (void) pam_end (pamh, retval);
} }
} }
if (retval == PAM_SUCCESS) { if (PAM_SUCCESS == retval) {
retval = pam_acct_mgmt (pamh, 0); retval = pam_acct_mgmt (pamh, 0);
if (retval != PAM_SUCCESS) { if (PAM_SUCCESS != retval) {
pam_end (pamh, retval); (void) pam_end (pamh, retval);
} }
} }
if (retval != PAM_SUCCESS) { if (PAM_SUCCESS != retval) {
fprintf (stderr, _("%s: PAM authentication failed\n"), Prog); fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
fail_exit (1); fail_exit (1);
} }
@ -1633,8 +1659,9 @@ int main (int argc, char **argv)
* a new user. * a new user.
*/ */
if (Dflg) { if (Dflg) {
if (gflg || bflg || fflg || eflg || sflg) if (gflg || bflg || fflg || eflg || sflg) {
exit (set_defaults ()? 1 : 0); exit ((set_defaults () != 0) ? 1 : 0);
}
show_defaults (); show_defaults ();
exit (E_SUCCESS); exit (E_SUCCESS);
@ -1643,7 +1670,7 @@ int main (int argc, char **argv)
/* /*
* Start with a quick check to see if the user exists. * Start with a quick check to see if the user exists.
*/ */
if (getpwnam (user_name)) { /* local, no need for xgetpwnam */ if (getpwnam (user_name) != NULL) { /* local, no need for xgetpwnam */
fprintf (stderr, _("%s: user %s exists\n"), Prog, user_name); fprintf (stderr, _("%s: user %s exists\n"), Prog, user_name);
#ifdef WITH_AUDIT #ifdef WITH_AUDIT
audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "adding user", audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "adding user",
@ -1659,7 +1686,8 @@ int main (int argc, char **argv)
* --bero * --bero
*/ */
if (Uflg) { if (Uflg) {
if (getgrnam (user_name)) { /* local, no need for xgetgrnam */ /* local, no need for xgetgrnam */
if (getgrnam (user_name) != NULL) {
fprintf (stderr, fprintf (stderr,
_ _
("%s: group %s exists - if you want to add this user to that group, use -g.\n"), ("%s: group %s exists - if you want to add this user to that group, use -g.\n"),
@ -1728,7 +1756,7 @@ int main (int argc, char **argv)
"Not copying any file from skel directory into it.\n"), "Not copying any file from skel directory into it.\n"),
Prog); Prog);
} else if (getdef_str ("CREATE_HOME")) { } else if (getdef_str ("CREATE_HOME") != NULL) {
/* /*
* RedHat added the CREATE_HOME option in login.defs in their * RedHat added the CREATE_HOME option in login.defs in their
* version of shadow-utils (which makes -m the default, with * version of shadow-utils (which makes -m the default, with
@ -1755,9 +1783,11 @@ int main (int argc, char **argv)
nscd_flush_cache ("group"); nscd_flush_cache ("group");
#ifdef USE_PAM #ifdef USE_PAM
if (retval == PAM_SUCCESS) if (PAM_SUCCESS == retval) {
pam_end (pamh, PAM_SUCCESS); (void) pam_end (pamh, PAM_SUCCESS);
}
#endif /* USE_PAM */ #endif /* USE_PAM */
return E_SUCCESS; return E_SUCCESS;
} }