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
#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);
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"),
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
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);