From: nekral-guest Date: Fri, 28 Dec 2007 22:18:55 +0000 (+0000) Subject: Other new functions: open_files(), close_files(). X-Git-Tag: 4.1.1~239 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b9eec1ea49c4b0882c8ad6f42ff89b947ab8ac14;p=shadow Other new functions: open_files(), close_files(). This force flushing the password database after the password file is unlocked. --- diff --git a/ChangeLog b/ChangeLog index 02889ae8..97c39a82 100644 --- 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 diff --git a/src/chpasswd.c b/src/chpasswd.c index 4abc5dda..073c7907 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -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);