newusers: Fail early

There's no reason to report all errors.  Bail out at the first one,
which is simpler.

Suggested-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar 2023-02-04 20:54:43 +01:00 committed by Serge Hallyn
parent 1957c8c881
commit 5b117d5526

View File

@ -1041,7 +1041,6 @@ int main (int argc, char **argv)
char *cp; char *cp;
const struct passwd *pw; const struct passwd *pw;
struct passwd newpw; struct passwd newpw;
int errors = 0;
int line = 0; int line = 0;
uid_t uid; uid_t uid;
gid_t gid; gid_t gid;
@ -1100,8 +1099,7 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: line %d: line too long\n"), _("%s: line %d: line too long\n"),
Prog, line); Prog, line);
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
} }
@ -1123,8 +1121,7 @@ int main (int argc, char **argv)
if (nfields != 6) { if (nfields != 6) {
fprintf (stderr, _("%s: line %d: invalid line\n"), fprintf (stderr, _("%s: line %d: invalid line\n"),
Prog, line); Prog, line);
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
/* /*
@ -1134,9 +1131,10 @@ int main (int argc, char **argv)
/* local, no need for xgetpwnam */ /* local, no need for xgetpwnam */
if ( (NULL == pw) if ( (NULL == pw)
&& (getpwnam (fields[0]) != NULL)) { && (getpwnam (fields[0]) != NULL)) {
fprintf (stderr, _("%s: cannot update the entry of user %s (not in the passwd database)\n"), Prog, fields[0]); fprintf (stderr,
errors++; _("%s: cannot update the entry of user %s (not in the passwd database)\n"),
continue; Prog, fields[0]);
fail_exit (EXIT_FAILURE);
} }
if ( (NULL == pw) if ( (NULL == pw)
@ -1144,8 +1142,7 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: line %d: can't create user\n"), _("%s: line %d: can't create user\n"),
Prog, line); Prog, line);
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
/* /*
@ -1165,8 +1162,7 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: line %d: can't create group\n"), _("%s: line %d: can't create group\n"),
Prog, line); Prog, line);
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
/* /*
@ -1181,8 +1177,7 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: line %d: can't create user\n"), _("%s: line %d: can't create user\n"),
Prog, line); Prog, line);
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
/* /*
@ -1194,8 +1189,7 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: line %d: user '%s' does not exist in %s\n"), _("%s: line %d: user '%s' does not exist in %s\n"),
Prog, line, fields[0], pw_dbname ()); Prog, line, fields[0], pw_dbname ());
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
newpw = *pw; newpw = *pw;
@ -1209,8 +1203,7 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: line %d: %s\n"), _("%s: line %d: %s\n"),
Prog, line, strerror(errno)); Prog, line, strerror(errno));
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
lines[nusers-1] = line; lines[nusers-1] = line;
usernames[nusers-1] = strdup (fields[0]); usernames[nusers-1] = strdup (fields[0]);
@ -1220,8 +1213,7 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: line %d: can't update password\n"), _("%s: line %d: can't update password\n"),
Prog, line); Prog, line);
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
if ('\0' != fields[4][0]) { if ('\0' != fields[4][0]) {
newpw.pw_gecos = fields[4]; newpw.pw_gecos = fields[4];
@ -1244,8 +1236,7 @@ int main (int argc, char **argv)
fprintf(stderr, fprintf(stderr,
_("%s: line %d: homedir must be an absolute path\n"), _("%s: line %d: homedir must be an absolute path\n"),
Prog, line); Prog, line);
errors++; fail_exit (EXIT_FAILURE);
continue;
}; };
if (mkdir (newpw.pw_dir, mode) != 0) { if (mkdir (newpw.pw_dir, mode) != 0) {
fprintf (stderr, fprintf (stderr,
@ -1253,8 +1244,7 @@ int main (int argc, char **argv)
Prog, line, newpw.pw_dir, Prog, line, newpw.pw_dir,
strerror (errno)); strerror (errno));
if (errno != EEXIST) { if (errno != EEXIST) {
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
} }
if (chown (newpw.pw_dir, if (chown (newpw.pw_dir,
@ -1264,8 +1254,7 @@ int main (int argc, char **argv)
_("%s: line %d: chown %s failed: %s\n"), _("%s: line %d: chown %s failed: %s\n"),
Prog, line, newpw.pw_dir, Prog, line, newpw.pw_dir,
strerror (errno)); strerror (errno));
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
} }
@ -1276,8 +1265,7 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: line %d: can't update entry\n"), _("%s: line %d: can't update entry\n"),
Prog, line); Prog, line);
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
#ifdef ENABLE_SUBIDS #ifdef ENABLE_SUBIDS
@ -1292,15 +1280,13 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: failed to prepare new %s entry\n"), _("%s: failed to prepare new %s entry\n"),
Prog, sub_uid_dbname ()); Prog, sub_uid_dbname ());
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
} else { } else {
fprintf (stderr, fprintf (stderr,
_("%s: can't find subordinate user range\n"), _("%s: can't find subordinate user range\n"),
Prog); Prog);
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
} }
@ -1315,15 +1301,13 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: failed to prepare new %s entry\n"), _("%s: failed to prepare new %s entry\n"),
Prog, sub_uid_dbname ()); Prog, sub_uid_dbname ());
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
} else { } else {
fprintf (stderr, fprintf (stderr,
_("%s: can't find subordinate group range\n"), _("%s: can't find subordinate group range\n"),
Prog); Prog);
errors++; fail_exit (EXIT_FAILURE);
continue;
} }
} }
#endif /* ENABLE_SUBIDS */ #endif /* ENABLE_SUBIDS */
@ -1336,12 +1320,6 @@ int main (int argc, char **argv)
* changes to be written out all at once, and then unlocked * changes to be written out all at once, and then unlocked
* afterwards. * afterwards.
*/ */
if (0 != errors) {
fprintf (stderr,
_("%s: error detected, changes ignored\n"), Prog);
fail_exit (EXIT_FAILURE);
}
close_files (); close_files ();
nscd_flush_cache ("passwd"); nscd_flush_cache ("passwd");
@ -1356,11 +1334,11 @@ int main (int argc, char **argv)
fprintf (stderr, fprintf (stderr,
_("%s: (line %d, user %s) password not changed\n"), _("%s: (line %d, user %s) password not changed\n"),
Prog, lines[i], usernames[i]); Prog, lines[i], usernames[i]);
errors++; exit (EXIT_FAILURE);
} }
} }
#endif /* USE_PAM */ #endif /* USE_PAM */
return ((0 == errors) ? EXIT_SUCCESS : EXIT_FAILURE); exit (EXIT_SUCCESS);
} }