FreeBSD-10.3-pw-2.patch 8.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#
# Incorporate some 10-STABLE speed-ups of temp files and a Utah change
# to pw_user to not make so many group file writes.
#
# cd /usr/src
# sudo patch -p0 < patchfile
#

diff -Nur lib/libutil.orig/gr_util.c lib/libutil/gr_util.c
--- lib/libutil.orig/gr_util.c	2016-05-27 09:26:11.000000000 -0600
+++ lib/libutil/gr_util.c	2016-12-19 16:12:41.313784000 -0700
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: releng/10.3/lib/libutil/gr_util.c 285205 2015-07-06 13:01:01Z garga $");
+__FBSDID("$FreeBSD: stable/10/lib/libutil/gr_util.c 310173 2016-12-16 20:10:55Z asomers $");
 
 #include <sys/param.h>
 #include <sys/errno.h>
@@ -141,7 +141,7 @@
 		errno = ENAMETOOLONG;
 		return (-1);
 	}
-	if ((tfd = mkostemp(tempname, O_SYNC)) == -1)
+	if ((tfd = mkostemp(tempname, 0)) == -1)
 		return (-1);
 	if (mfd != -1) {
 		while ((nr = read(mfd, buf, sizeof(buf))) > 0)
diff -Nur lib/libutil.orig/pw_util.c lib/libutil/pw_util.c
--- lib/libutil.orig/pw_util.c	2016-05-27 09:26:11.000000000 -0600
+++ lib/libutil/pw_util.c	2016-12-19 16:12:20.608343000 -0700
@@ -39,7 +39,7 @@
 static const char sccsid[] = "@(#)pw_util.c	8.3 (Berkeley) 4/2/94";
 #endif
 static const char rcsid[] =
-  "$FreeBSD: releng/10.3/lib/libutil/pw_util.c 285205 2015-07-06 13:01:01Z garga $";
+  "$FreeBSD: stable/10/lib/libutil/pw_util.c 310173 2016-12-16 20:10:55Z asomers $";
 #endif /* not lint */
 
 /*
@@ -226,7 +226,7 @@
 		errno = ENAMETOOLONG;
 		return (-1);
 	}
-	if ((tfd = mkostemp(tempname, O_SYNC)) == -1)
+	if ((tfd = mkostemp(tempname, 0)) == -1)
 		return (-1);
 	if (mfd != -1) {
 		while ((nr = read(mfd, buf, sizeof(buf))) > 0)
diff -Nur usr.sbin/pw.orig/Makefile usr.sbin/pw/Makefile
--- usr.sbin/pw.orig/Makefile	2016-05-27 09:23:45.000000000 -0600
+++ usr.sbin/pw/Makefile	2016-12-19 16:06:28.961040000 -0700
54 55 56 57 58 59 60 61 62 63
@@ -8,6 +8,9 @@
 	grupd.c pwupd.c psdate.c bitmap.c cpdir.c rm_r.c strtounum.c \
 	pw_utils.c
 
+# Allow -O option to do usermod group changes the old way
+CFLAGS+= -DOLDCODE_COMPAT
+
 WARNS?=	3
 
 DPADD=	${LIBCRYPT} ${LIBUTIL} ${LIBSBUF}
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
diff -Nur usr.sbin/pw.orig/grupd.c usr.sbin/pw/grupd.c
--- usr.sbin/pw.orig/grupd.c	2016-05-27 09:23:43.000000000 -0600
+++ usr.sbin/pw/grupd.c	2016-12-19 16:10:31.854770000 -0700
@@ -26,7 +26,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-  "$FreeBSD: releng/10.3/usr.sbin/pw/grupd.c 287084 2015-08-23 21:42:27Z bapt $";
+  "$FreeBSD: stable/10/usr.sbin/pw/grupd.c 310173 2016-12-16 20:10:55Z asomers $";
 #endif /* not lint */
 
 #include <err.h>
@@ -34,6 +34,7 @@
 #include <libutil.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include "pwupd.h"
 
@@ -73,8 +74,11 @@
 	}
 	if (gr_copy(pfd, tfd, gr, old_gr) == -1) {
 		gr_fini();
+		close(tfd);
 		err(1, "gr_copy()");
 	}
+	fsync(tfd);
+	close(tfd);
 	if (gr_mkdb() == -1) {
 		gr_fini();
 		err(1, "gr_mkdb()");
diff -Nur usr.sbin/pw.orig/pw_nis.c usr.sbin/pw/pw_nis.c
--- usr.sbin/pw.orig/pw_nis.c	2016-05-27 09:23:45.000000000 -0600
+++ usr.sbin/pw/pw_nis.c	2016-12-19 16:11:08.110522000 -0700
@@ -26,7 +26,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-  "$FreeBSD: releng/10.3/usr.sbin/pw/pw_nis.c 287084 2015-08-23 21:42:27Z bapt $";
+  "$FreeBSD: stable/10/usr.sbin/pw/pw_nis.c 310173 2016-12-16 20:10:55Z asomers $";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -34,6 +34,7 @@
 #include <err.h>
 #include <pwd.h>
 #include <libutil.h>
+#include <unistd.h>
 
 #include "pw.h"
 
@@ -63,8 +64,11 @@
 	}
 	if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
 		pw_fini();
+		close(tfd);
 		err(1, "pw_copy()");
 	}
+	fsync(tfd);
+	close(tfd);
 	if (chmod(pw_tempname(), 0644) == -1)
 		err(1, "chmod()");
 	if (rename(pw_tempname(), path) == -1)
diff -Nur usr.sbin/pw.orig/pw_user.c usr.sbin/pw/pw_user.c
--- usr.sbin/pw.orig/pw_user.c	2016-05-27 09:23:45.000000000 -0600
+++ usr.sbin/pw/pw_user.c	2016-12-19 16:06:28.962164000 -0700
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
@@ -1486,7 +1486,12 @@
 	struct passwd *pwd;
 	struct group *grp;
 	StringList *groups = NULL;
+#ifdef OLDCODE_COMPAT
+	char args[] = "C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:NPYy:O";
+	int oldcode = 0;
+#else
 	char args[] = "C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:NPYy:";
+#endif
 	const char *cfg;
 	char *gecos, *homedir, *grname, *name, *newname, *walk, *skel, *shell;
 	char *passwd, *class, *nispasswd;
@@ -1605,6 +1610,11 @@
 		case 'Y':
 			nis = true;
 			break;
+#ifdef OLDCODE_COMPAT
+		case 'O':
+			oldcode = 1;
+			break;
+#endif
 		}
 	}
 
@@ -1666,12 +1676,27 @@
 
 	if (grname && pwd->pw_uid != 0) {
 		grp = GETGRNAM(grname);
-		if (grp == NULL)
+		if (grp == NULL) {
 			grp = GETGRGID(pw_checkid(grname, GID_MAX));
+			grname = grp->gr_name;
+		}
 		if (grp->gr_gid != pwd->pw_gid) {
 			pwd->pw_gid = grp->gr_gid;
 			edited = true;
 		}
+		/* if grname is in group list, remove it */
+		if (groups && sl_find(groups, grname)) {
+			StringList *ngroups = NULL;
+			if (groups->sl_cur > 1) {
+				ngroups = sl_init();
+				for (i = 0; i < groups->sl_cur; i++) {
+					if (strcmp(grname, groups->sl_str[i]))
+						sl_add(ngroups, groups->sl_str[i]);
+				}
+			}
+			sl_free(groups, 0);
+			groups = ngroups;
+		}
 	}
 
 	if (password_days >= 0 && pwd->pw_change != password_days) {
@@ -1740,31 +1765,77 @@
 		perform_chgpwent(name, pwd, nis ? nispasswd : NULL);
 	/* Now perform the needed changes concern groups */
 	if (groups != NULL) {
-		/* Delete User from groups using old name */
+#ifdef OLDCODE_COMPAT
+		if (oldcode) {
+			/* Delete User from groups using old name */
+			SETGRENT();
+			while ((grp = GETGRENT()) != NULL) {
+				if (grp->gr_mem == NULL)
+					continue;
+				for (i = 0; grp->gr_mem[i] != NULL; i++) {
+					if (strcmp(grp->gr_mem[i] , name) != 0)
+						continue;
+					for (j = i; grp->gr_mem[j] != NULL ; j++)
+						grp->gr_mem[j] = grp->gr_mem[j+1];
+					chggrent(grp->gr_name, grp);
+					break;
+				}
+			}
+			ENDGRENT();
+			/* Add the user to the needed groups */
+			for (i = 0; i < groups->sl_cur; i++) {
+				grp = GETGRNAM(groups->sl_str[i]);
+				grp = gr_add(grp, pwd->pw_name);
+				if (grp == NULL)
+					continue;
+				chggrent(grp->gr_name, grp);
+				free(grp);
+			}
+			goto finished;
+		}
+#endif
+
+		/*
+		 * N.B. every chggrent() call causes a copy of the entire group
+		 * file, so minimize the number of calls we make!
+		 */
 		SETGRENT();
 		while ((grp = GETGRENT()) != NULL) {
-			if (grp->gr_mem == NULL)
-				continue;
-			for (i = 0; grp->gr_mem[i] != NULL; i++) {
-				if (strcmp(grp->gr_mem[i] , name) != 0)
-					continue;
-				for (j = i; grp->gr_mem[j] != NULL ; j++)
+			int oingroup, ningroup;
+
+			/* Are we supposed to be in this group? */
+			ningroup = (sl_find(groups, grp->gr_name) != NULL) ? 1 : 0;
+
+			/* Are we already in this group? */
+			oingroup = -1;
+			for (i = 0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
+				if (strcmp(grp->gr_mem[i], name) == 0) {
+					oingroup = i;
+					break;
+				}
+			}
+
+			/* If we found the user and they should not be here, remove */
+			if (oingroup >= 0 && ningroup == 0) {
+				for (j = oingroup; grp->gr_mem[j] != NULL; j++)
 					grp->gr_mem[j] = grp->gr_mem[j+1];
 				chggrent(grp->gr_name, grp);
-				break;
+			}
+
+			/* If we did not find the user and they should be here, add */
+			else if (oingroup < 0 && ningroup != 0) {
+				grp = gr_add(grp, pwd->pw_name);
+				if (grp != NULL) {
+					chggrent(grp->gr_name, grp);
+					free(grp);
+				}
 			}
 		}
 		ENDGRENT();
-		/* Add the user to the needed groups */
-		for (i = 0; i < groups->sl_cur; i++) {
-			grp = GETGRNAM(groups->sl_str[i]);
-			grp = gr_add(grp, pwd->pw_name);
-			if (grp == NULL)
-				continue;
-			chggrent(grp->gr_name, grp);
-			free(grp);
-		}
 	}
+#ifdef OLDCODE_COMPAT
+ finished:
+#endif
 	/* In case of rename we need to walk over the different groups */
 	if (newname) {
 		SETGRENT();
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
diff -Nur usr.sbin/pw.orig/pwupd.c usr.sbin/pw/pwupd.c
--- usr.sbin/pw.orig/pwupd.c	2016-05-27 09:23:45.000000000 -0600
+++ usr.sbin/pw/pwupd.c	2016-12-19 16:09:52.094478000 -0700
@@ -26,7 +26,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-  "$FreeBSD: releng/10.3/usr.sbin/pw/pwupd.c 287084 2015-08-23 21:42:27Z bapt $";
+  "$FreeBSD: stable/10/usr.sbin/pw/pwupd.c 310173 2016-12-16 20:10:55Z asomers $";
 #endif /* not lint */
 
 #include <sys/wait.h>
@@ -111,8 +111,11 @@
 	}
 	if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
 		pw_fini();
+		close(tfd);
 		err(1, "pw_copy()");
 	}
+	fsync(tfd);
+	close(tfd);
 	/*
 	 * in case of deletion of a user, the whole database
 	 * needs to be regenerated