]> granicus.if.org Git - shadow/commitdiff
Other new functions: open_files(), close_files().
authornekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Fri, 28 Dec 2007 22:18:55 +0000 (22:18 +0000)
committernekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Fri, 28 Dec 2007 22:18:55 +0000 (22:18 +0000)
This force flushing the password database after the password file is unlocked.

ChangeLog
src/chpasswd.c

index 02889ae84d65d4943e5c2977337f399f9727f702..97c39a82652c3b452edb69abe1490fe4a2307a30 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,9 @@
        pam API was already checked. No need to validate it again.
        * src/chpasswd.c: New functions: process_flags(), check_flags(),
        check_perms(). Split out of main().
+       * src/chpasswd.c: Other new functions: open_files(), close_files().
+       This force flushing the password database after the password file is
+       unlocked.
 
 2007-12-28  Nicolas François  <nicolas.francois@centraliens.net>
 
index 4abc5ddae5c7845a21b694bd2c66505c3570488c..073c79078bff8f32ed48a5a6cc1d6b6f08393d81 100644 (file)
@@ -64,6 +64,8 @@ static void usage (void);
 static void process_flags (int argc, char **argv);
 static void check_flags (void);
 static void check_perms (void);
+static void open_files (void);
+static void close_files (void);
 
 /*
  * usage - display usage message and exit
@@ -243,36 +245,14 @@ static void check_perms (void)
 #endif                         /* USE_PAM */
 }
 
-int main (int argc, char **argv)
+/*
+ * open_files - lock and open the password databases
+ */
+static void open_files (void)
 {
-       char buf[BUFSIZ];
-       char *name;
-       char *newpwd;
-       char *cp;
-
-       const struct spwd *sp;
-       struct spwd newsp;
-
-       const struct passwd *pw;
-       struct passwd newpw;
-       int errors = 0;
-       int line = 0;
-       long now = time ((long *) 0) / (24L * 3600L);
-       int ok;
-
-       Prog = Basename (argv[0]);
-
-       setlocale (LC_ALL, "");
-       bindtextdomain (PACKAGE, LOCALEDIR);
-       textdomain (PACKAGE);
-
-       process_flags (argc, argv);
-
-       check_perms ();
-
        /*
-        * Lock the password file and open it for reading. This will bring
-        * all of the entries into memory where they may be updated.
+        * Lock the password file and open it for reading and writing. This
+        * will bring all of the entries into memory where they may be updated.
         */
        if (!pw_lock ()) {
                fprintf (stderr, _("%s: can't lock password file\n"), Prog);
@@ -284,7 +264,7 @@ int main (int argc, char **argv)
                exit (1);
        }
 
-       is_shadow_pwd = spw_file_present ();
+       /* Do the same for the shadowed database, if it exist */
        if (is_shadow_pwd) {
                if (!spw_lock ()) {
                        fprintf (stderr, _("%s: can't lock shadow file\n"),
@@ -300,6 +280,61 @@ int main (int argc, char **argv)
                        exit (1);
                }
        }
+}
+
+/*
+ * close_files - close and unlock the password databases
+ */
+static void close_files (void)
+{
+       if (is_shadow_pwd) {
+               if (!spw_close ()) {
+                       fprintf (stderr,
+                                _("%s: error updating shadow file\n"), Prog);
+                       pw_unlock ();
+                       exit (1);
+               }
+               spw_unlock ();
+       }
+
+       if (!pw_close ()) {
+               fprintf (stderr, _("%s: error updating password file\n"), Prog);
+               exit (1);
+       }
+       pw_unlock ();
+
+}
+
+int main (int argc, char **argv)
+{
+       char buf[BUFSIZ];
+       char *name;
+       char *newpwd;
+       char *cp;
+
+       const struct spwd *sp;
+       struct spwd newsp;
+
+       const struct passwd *pw;
+       struct passwd newpw;
+       int errors = 0;
+       int line = 0;
+       long now = time ((long *) 0) / (24L * 3600L);
+       int ok;
+
+       Prog = Basename (argv[0]);
+
+       setlocale (LC_ALL, "");
+       bindtextdomain (PACKAGE, LOCALEDIR);
+       textdomain (PACKAGE);
+
+       process_flags (argc, argv);
+
+       check_perms ();
+
+       is_shadow_pwd = spw_file_present ();
+
+       open_files ();
 
        /*
         * Read each line, separating the user name from the password. The
@@ -421,23 +456,10 @@ int main (int argc, char **argv)
                pw_unlock ();
                exit (1);
        }
-       if (is_shadow_pwd) {
-               if (!spw_close ()) {
-                       fprintf (stderr,
-                                _("%s: error updating shadow file\n"), Prog);
-                       pw_unlock ();
-                       exit (1);
-               }
-               spw_unlock ();
-       }
-       if (!pw_close ()) {
-               fprintf (stderr, _("%s: error updating password file\n"), Prog);
-               exit (1);
-       }
 
-       nscd_flush_cache ("passwd");
+       close_files ();
 
-       pw_unlock ();
+       nscd_flush_cache ("passwd");
 
 #ifdef USE_PAM
        pam_end (pamh, PAM_SUCCESS);