Module Name:    src
Committed By:   jmmv
Date:           Wed Jan 27 11:02:03 UTC 2010

Modified Files:
        src/distrib/utils/sysinst: defs.h util.c

Log Message:
Perform in-place replacement of the cypher type instead of renaming the
passwd.conf file to passwd.conf.pre-sysinst file and creating a new one
from scratch:

- This is consistent with all other edits of configuration files performed
  by sysinst.  (E.g. in-place changes of rc.conf.)

- This eases the upgrade of the system to a newer set of files because the
  diffs presented by etcupdate are easier to read.

No objections in tech-inst...@.  Tested installing NetBSD/amd64.


To generate a diff of this commit:
cvs rdiff -u -r1.147 -r1.148 src/distrib/utils/sysinst/defs.h
cvs rdiff -u -r1.162 -r1.163 src/distrib/utils/sysinst/util.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/utils/sysinst/defs.h
diff -u src/distrib/utils/sysinst/defs.h:1.147 src/distrib/utils/sysinst/defs.h:1.148
--- src/distrib/utils/sysinst/defs.h:1.147	Sat Jan  2 21:16:46 2010
+++ src/distrib/utils/sysinst/defs.h	Wed Jan 27 11:02:03 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: defs.h,v 1.147 2010/01/02 21:16:46 dsl Exp $	*/
+/*	$NetBSD: defs.h,v 1.148 2010/01/27 11:02:03 jmmv Exp $	*/
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -409,7 +409,7 @@
 int 	get_and_unpack_sets(int, msg, msg, msg);
 int	sanity_check(void);
 int	set_timezone(void);
-int	set_crypt_type(void);
+void	set_crypt_type(void);
 int	set_root_password(void);
 int	set_root_shell(void);
 void	scripting_fprintf(FILE *, const char *, ...);

Index: src/distrib/utils/sysinst/util.c
diff -u src/distrib/utils/sysinst/util.c:1.162 src/distrib/utils/sysinst/util.c:1.163
--- src/distrib/utils/sysinst/util.c:1.162	Sat Jan  2 18:06:57 2010
+++ src/distrib/utils/sysinst/util.c	Wed Jan 27 11:02:03 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: util.c,v 1.162 2010/01/02 18:06:57 dsl Exp $	*/
+/*	$NetBSD: util.c,v 1.163 2010/01/27 11:02:03 jmmv Exp $	*/
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -47,6 +47,7 @@
 #include <sys/stat.h>
 #include <sys/statvfs.h>
 #include <curses.h>
+#include <err.h>
 #include <errno.h>
 #include <dirent.h>
 #include <util.h>
@@ -250,6 +251,32 @@
 	free(owd);
 }
 
+/*
+ * Performs in-place replacement of a set of patterns in a file that lives
+ * inside the installed system.  The patterns must be separated by a semicolon.
+ * For example:
+ *
+ * replace("/etc/some-file.conf", "s/prop1=NO/prop1=YES/;s/foo/bar/");
+ */
+static
+void
+replace(const char *path, const char *patterns, ...)
+{
+	char *spatterns;
+	va_list ap;
+
+	va_start(ap, patterns);
+	vasprintf(&spatterns, patterns, ap);
+	va_end(ap);
+	if (spatterns == NULL)
+		err(1, "vasprintf(&spatterns, \"%s\", ...)", patterns);
+
+	run_program(RUN_CHROOT, "sed -an -e '%s;H;$!d;g;w %s' %s", spatterns,
+	    path, path);
+
+	free(spatterns);
+}
+
 static int
 floppy_fetch(const char *set_name)
 {
@@ -1152,61 +1179,39 @@
 	return 1;
 }
 
-int
+static
+void
+replace_crypt_type(const char *localcipher, const char *ypcipher)
+{
+
+	replace("/etc/passwd.conf", "s/^.*default:.*$/default:/;"
+	    "s/^.*localcipher.*$/\tlocalcipher = %s/;"
+	    "s/^.*ypcipher.*$/\typcipher = %s/", localcipher, ypcipher);
+}
+
+void
 set_crypt_type(void)
 {
-	FILE *pwc;
-	char *fn;
 
 	msg_display(MSG_choose_crypt);
 	process_menu(MENU_crypttype, NULL);
-	fn = strdup(target_expand("/etc/passwd.conf"));
-	if (fn == NULL)
-		return -1;
 
 	switch (yesno) {
 	case 0:
 		break;
 	case 1:	/* DES */
-		rename(fn, target_expand("/etc/passwd.conf.pre-sysinst"));
-		pwc = fopen(fn, "w");
-		fprintf(pwc,
-		    "default:\n"
-		    "  localcipher = old\n"
-		    "  ypcipher = old\n");
-		fclose(pwc);
+		replace_crypt_type("old", "old");
 		break;
 	case 2:	/* MD5 */
-		rename(fn, target_expand("/etc/passwd.conf.pre-sysinst"));
-		pwc = fopen(fn, "w");
-		fprintf(pwc,
-		    "default:\n"
-		    "  localcipher = md5\n"
-		    "  ypcipher = md5\n");
-		fclose(pwc);
+		replace_crypt_type("md5", "md5");
 		break;
 	case 3:	/* blowfish 2^7 */
-		rename(fn, target_expand("/etc/passwd.conf.pre-sysinst"));
-		pwc = fopen(fn, "w");
-		fprintf(pwc,
-		    "default:\n"
-		    "  localcipher = blowfish,7\n"
-		    "  ypcipher = blowfish,7\n");
-		fclose(pwc);
+		replace_crypt_type("blowfish,7", "blowfish,7");
 		break;
 	case 4:	/* sha1 */
-		rename(fn, target_expand("/etc/passwd.conf.pre-sysinst"));
-		pwc = fopen(fn, "w");
-		fprintf(pwc,
-		    "default:\n"
-		    "  localcipher = sha1\n"
-		    "  ypcipher = sha1\n");
-		fclose(pwc);
+		replace_crypt_type("sha1", "sha1");
 		break;
 	}
-
-	free(fn);
-	return (0);
 }
 
 int
@@ -1291,9 +1296,8 @@
 void
 enable_rc_conf(void)
 {
-	run_program(RUN_CHROOT,
-		    "sed -an -e 's/^rc_configured=NO/rc_configured=YES/;"
-				    "H;$!d;g;w /etc/rc.conf' /etc/rc.conf");
+
+	replace("/etc/rc.conf", "s/^rc_configured=NO/rc_configured=YES/");
 }
 
 int

Reply via email to