[Pkg-shadow-commits] r1601 - in upstream/trunk: . src

nekral-guest at alioth.debian.org nekral-guest at alioth.debian.org
Tue Jan 1 15:29:47 UTC 2008


Author: nekral-guest
Date: 2008-01-01 15:29:47 +0000 (Tue, 01 Jan 2008)
New Revision: 1601

Modified:
   upstream/trunk/ChangeLog
   upstream/trunk/src/pwck.c
Log:
Also split open_files and close_files out of main().
New global variables use_system_pw_file and use_system_spw_file


Modified: upstream/trunk/ChangeLog
===================================================================
--- upstream/trunk/ChangeLog	2008-01-01 15:07:41 UTC (rev 1600)
+++ upstream/trunk/ChangeLog	2008-01-01 15:29:47 UTC (rev 1601)
@@ -1,7 +1,8 @@
 2008-01-01  Nicolas François  <nicolas.francois at 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 at centraliens.net>
 

Modified: upstream/trunk/src/pwck.c
===================================================================
--- upstream/trunk/src/pwck.c	2008-01-01 15:07:41 UTC (rev 1600)
+++ upstream/trunk/src/pwck.c	2008-01-01 15:29:47 UTC (rev 1601)
@@ -66,7 +66,9 @@
 
 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 @@
 /* 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,40 +150,21 @@
 }
 
 /*
- * 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
 	 */
 	if (!read_only) {
 		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 @@
 		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 @@
 	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 @@
 	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,35 +598,9 @@
 
       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);
-		}
-	}
+      write_and_bye:
+	close_files (changed);
 
-	/*
-	 * Don't be anti-social - unlock the files when you're done.
-	 */
-	if (is_shadow)
-		spw_unlock ();
-	(void) pw_unlock ();
-
 	nscd_flush_cache ("passwd");
 
 	/*




More information about the Pkg-shadow-commits mailing list