]> granicus.if.org Git - shadow/commitdiff
Also split open_files and close_files out of main().
authornekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Tue, 1 Jan 2008 15:29:47 +0000 (15:29 +0000)
committernekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Tue, 1 Jan 2008 15:29:47 +0000 (15:29 +0000)
New global variables use_system_pw_file and use_system_spw_file

ChangeLog
src/pwck.c

index c60fb9a5d892db1bb2425d9ac83bfe2b01e980df..4e7e436c42fdcbe862e18840d3dd334ba00b3931 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,7 +1,8 @@
 2008-01-01  Nicolas François  <nicolas.francois@centraliens.net>
 
-       * 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  <nicolas.francois@centraliens.net>
 
index be0f33a9cf47b9aac094359dd0f5caf9e1b6776d..1ed2aec8705e6fcc7d532afa6f52b274a6a0e22f 100644 (file)
@@ -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");