From: nekral-guest Date: Tue, 1 Jan 2008 15:29:47 +0000 (+0000) Subject: Also split open_files and close_files out of main(). X-Git-Tag: 4.1.1~178 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ef2c12e5601a65ee96d2f475d86aabb913b525cb;p=shadow Also split open_files and close_files out of main(). New global variables use_system_pw_file and use_system_spw_file --- diff --git a/ChangeLog b/ChangeLog index c60fb9a5..4e7e436c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,8 @@ 2008-01-01 Nicolas François - * src/pwck.c: Split process_flags() out of main(). New global - variables is_shadow, sort_mode. + * src/pwck.c: Split process_flags(), open_files(), and + close_files() out of main(). New global variables is_shadow, + sort_mode, use_system_pw_file, and use_system_spw_file. 2008-01-01 Nicolas François diff --git a/src/pwck.c b/src/pwck.c index be0f33a9..1ed2aec8 100644 --- a/src/pwck.c +++ b/src/pwck.c @@ -66,7 +66,9 @@ extern struct commonio_entry *__spw_get_head (void); static char *Prog; static const char *pwd_file = PASSWD_FILE; +static int use_system_pw_file = 1; static const char *spw_file = SHADOW_FILE; +static int use_system_spw_file = 1; static int is_shadow = 0; @@ -78,6 +80,8 @@ static int quiet = 0; /* don't report warnings, only errors */ /* local function prototypes */ static void usage (void); static void process_flags (int argc, char **argv); +static void open_files (void); +static void close_files (int changed); /* * usage - print syntax message and exit @@ -146,32 +150,13 @@ static void process_flags (int argc, char **argv) } /* - * pwck - verify password file integrity + * open_files - open the shadow database + * + * In read-only mode, the databases are not locked and are opened + * only for reading. */ -int main (int argc, char **argv) +static void open_files (void) { - int errors = 0; - int changed = 0; - struct commonio_entry *pfe, *tpfe; - struct passwd *pwd; - - struct commonio_entry *spe, *tspe; - struct spwd *spw; - - /* - * Get my name so that I can use it to report errors. - */ - Prog = Basename (argv[0]); - - setlocale (LC_ALL, ""); - bindtextdomain (PACKAGE, LOCALEDIR); - textdomain (PACKAGE); - - OPENLOG ("pwck"); - - /* Parse the command line arguments */ - process_flags (argc, argv); - /* * Lock the files if we aren't in "read-only" mode */ @@ -179,7 +164,7 @@ int main (int argc, char **argv) if (!pw_lock ()) { fprintf (stderr, _("%s: cannot lock file %s\n"), Prog, pwd_file); - if (optind == argc) + if (use_system_pw_file) SYSLOG ((LOG_WARN, "cannot lock %s", pwd_file)); closelog (); exit (E_CANTLOCK); @@ -187,7 +172,7 @@ int main (int argc, char **argv) if (is_shadow && !spw_lock ()) { fprintf (stderr, _("%s: cannot lock file %s\n"), Prog, spw_file); - if (optind == argc) + if (use_system_spw_file) SYSLOG ((LOG_WARN, "cannot lock %s", spw_file)); closelog (); exit (E_CANTLOCK); @@ -201,7 +186,7 @@ int main (int argc, char **argv) if (!pw_open (read_only ? O_RDONLY : O_RDWR)) { fprintf (stderr, _("%s: cannot open file %s\n"), Prog, pwd_file); - if (optind == argc) + if (use_system_pw_file) SYSLOG ((LOG_WARN, "cannot open %s", pwd_file)); closelog (); exit (E_CANTOPEN); @@ -209,16 +194,85 @@ int main (int argc, char **argv) if (is_shadow && !spw_open (read_only ? O_RDONLY : O_RDWR)) { fprintf (stderr, _("%s: cannot open file %s\n"), Prog, spw_file); - if (optind == argc) + if (use_system_spw_file) SYSLOG ((LOG_WARN, "cannot open %s", spw_file)); closelog (); exit (E_CANTOPEN); } +} + +/* + * close_files - close and unlock the password/shadow databases + * + * If changed is not set, the databases are not closed, and no + * changes are committed in the databases. The databases are + * unlocked anyway. + */ +static void close_files (int changed) +{ + /* + * All done. If there were no change we can just abandon any + * changes to the files. + */ + if (changed) { + if (!pw_close ()) { + fprintf (stderr, _("%s: cannot update file %s\n"), + Prog, pwd_file); + SYSLOG ((LOG_WARN, "cannot update %s", pwd_file)); + closelog (); + exit (E_CANTUPDATE); + } + if (is_shadow && !spw_close ()) { + fprintf (stderr, _("%s: cannot update file %s\n"), + Prog, spw_file); + SYSLOG ((LOG_WARN, "cannot update %s", spw_file)); + closelog (); + exit (E_CANTUPDATE); + } + } + + /* + * Don't be anti-social - unlock the files when you're done. + */ + if (is_shadow) + spw_unlock (); + (void) pw_unlock (); +} + +/* + * pwck - verify password file integrity + */ +int main (int argc, char **argv) +{ + int errors = 0; + int changed = 0; + struct commonio_entry *pfe, *tpfe; + struct passwd *pwd; + + struct commonio_entry *spe, *tspe; + struct spwd *spw; + + /* + * Get my name so that I can use it to report errors. + */ + Prog = Basename (argv[0]); + + setlocale (LC_ALL, ""); + bindtextdomain (PACKAGE, LOCALEDIR); + textdomain (PACKAGE); + + OPENLOG ("pwck"); + + /* Parse the command line arguments */ + process_flags (argc, argv); + + open_files (); if (sort_mode) { pw_sort (); if (is_shadow) spw_sort (); + changed = 1; goto write_and_bye; } @@ -544,34 +598,8 @@ int main (int argc, char **argv) shadow_done: - /* - * All done. If there were no change we can just abandon any - * changes to the files. - */ - if (changed) { - write_and_bye: - if (!pw_close ()) { - fprintf (stderr, _("%s: cannot update file %s\n"), - Prog, pwd_file); - SYSLOG ((LOG_WARN, "cannot update %s", pwd_file)); - closelog (); - exit (E_CANTUPDATE); - } - if (is_shadow && !spw_close ()) { - fprintf (stderr, _("%s: cannot update file %s\n"), - Prog, spw_file); - SYSLOG ((LOG_WARN, "cannot update %s", spw_file)); - closelog (); - exit (E_CANTUPDATE); - } - } - - /* - * Don't be anti-social - unlock the files when you're done. - */ - if (is_shadow) - spw_unlock (); - (void) pw_unlock (); + write_and_bye: + close_files (changed); nscd_flush_cache ("passwd");