1 /*- 2 * Copyright (C) 1996 3 * David L. Nugent. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/usr.sbin/pw/edgroup.c,v 1.9 2004/03/08 20:31:37 kensmith Exp $ 27 */ 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <unistd.h> 33 #include <stdarg.h> 34 #include <sys/types.h> 35 #include <sys/stat.h> 36 #include <pwd.h> 37 #include <grp.h> 38 #include <fcntl.h> 39 #include <sys/param.h> 40 #include <ctype.h> 41 42 #include "pwupd.h" 43 44 static int 45 isingroup(char const * name, char **mem) 46 { 47 int i; 48 49 for (i = 0; mem[i] != NULL; i++) 50 if (strcmp(name, mem[i]) == 0) 51 return i; 52 return -1; 53 } 54 55 int 56 editgroups(char *name, char **groups) 57 { 58 int rc = 0; 59 int infd; 60 char groupfile[MAXPATHLEN]; 61 char grouptmp[MAXPATHLEN]; 62 63 strncpy(groupfile, getgrpath(_GROUP), MAXPATHLEN - 5); 64 groupfile[MAXPATHLEN - 5] = '\0'; 65 strcpy(grouptmp, groupfile); 66 strcat(grouptmp, ".new"); 67 68 if ((infd = open(groupfile, O_RDWR | O_CREAT | O_EXLOCK, 0644)) != -1) { 69 FILE *infp; 70 71 if ((infp = fdopen(infd, "r+")) == NULL) 72 close(infd); 73 else { 74 int outfd; 75 76 if ((outfd = open(grouptmp, O_RDWR | O_CREAT | O_TRUNC, 0644)) != -1) { 77 FILE *outfp; 78 79 if ((outfp = fdopen(outfd, "w+")) == NULL) 80 close(outfd); 81 else { 82 int linelen = PWBUFSZ; 83 int outlen = PWBUFSZ; 84 int memlen = 200; /* Arbitrary */ 85 char *line = malloc(linelen); 86 char *outl = malloc(outlen); 87 char **mems = malloc(memlen * sizeof(char *)); 88 int namlen = strlen(name); 89 90 if (line == NULL || outl == NULL || mems == NULL) { 91 mem_abort: 92 rc = 0; 93 } else { 94 while (fgets(line, linelen, infp) != NULL) { 95 char *p; 96 int l; 97 98 while ((p = strchr(line, '\n')) == NULL) 99 { 100 if (extendline(&line, &linelen, linelen + PWBUFSZ) == -1) { 101 goto mem_abort; 102 } 103 l = strlen(line); 104 if (fgets(line + l, linelen - l, infp) == NULL) 105 break; /* No newline terminator on last line */ 106 } 107 l = strlen(line) + namlen + 1; 108 if (extendline(&outl, &outlen, l) == -1) { 109 goto mem_abort; 110 } 111 if (*line == '#') 112 strcpy(outl, line); 113 else if (*line == '\n') 114 *outl = '\0'; 115 else { 116 int i, 117 mno = 0; 118 char *cp = line; 119 char const *sep = ":\n"; 120 struct group grp; 121 122 memset(&grp, 0, sizeof grp); 123 for (i = 0; (p = strsep(&cp, sep)) != NULL; i++) { 124 switch (i) { 125 case 0: /* Group name */ 126 grp.gr_name = p; 127 break; 128 case 1: /* Group password */ 129 grp.gr_passwd = p; 130 break; 131 case 2: /* Group id */ 132 grp.gr_gid = atoi(p); 133 break; 134 case 3: /* Member list */ 135 cp = p; 136 sep = ",\n"; 137 break; 138 default: /* Individual members */ 139 if (*p) { 140 if (extendarray(&mems, &memlen, mno + 2) == -1) { 141 goto mem_abort; 142 } 143 mems[mno++] = p; 144 } 145 break; 146 } 147 } 148 if (i < 2) /* Bail out - insufficient fields */ 149 continue; 150 151 grp.gr_mem = mems; 152 for (i = mno; i < memlen; i++) 153 mems[i] = NULL; 154 155 /* 156 * Delete from group, or add to group? 157 */ 158 if (groups == NULL || isingroup(grp.gr_name, groups) == -1) { /* Delete */ 159 int idx; 160 161 while ((idx = isingroup(name, mems)) != -1) { 162 for (i = idx; i < (memlen - 1); i++) 163 mems[i] = mems[i + 1]; 164 mems[i] = NULL; 165 --mno; 166 } 167 /* 168 * Special case - deleting user and group may be user's own 169 */ 170 if (groups == NULL && mems[0] == NULL && strcmp(name, grp.gr_name) == 0) { 171 /* 172 * First, make _sure_ we don't have other members 173 */ 174 struct passwd *pwd; 175 176 SETPWENT(); 177 while ((pwd = GETPWENT()) != NULL && (gid_t)pwd->pw_gid != (gid_t)grp.gr_gid); 178 ENDPWENT(); 179 if (pwd == NULL) /* No members at all */ 180 continue; /* Drop the group */ 181 } 182 } else if (isingroup(name, mems) == -1) { 183 if (extendarray(&mems, &memlen, mno + 2) == -1) { 184 goto mem_abort; 185 } 186 grp.gr_mem = mems; /* May have realloced() */ 187 mems[mno++] = name; 188 mems[mno ] = NULL; 189 } 190 fmtgrentry(&outl, &outlen, &grp, PWF_GROUP); 191 } 192 fputs(outl, outfp); 193 } 194 if (fflush(outfp) != EOF) { 195 rc = 1; 196 197 /* 198 * Copy data back into the original file and truncate 199 */ 200 rewind(infp); 201 rewind(outfp); 202 while (fgets(outl, outlen, outfp) != NULL) 203 fputs(outl, infp); 204 205 /* 206 * This is a gross hack, but we may have corrupted the 207 * original file. 208 */ 209 if (fflush(infp) == EOF || ferror(infp)) 210 rc = rename(grouptmp, groupfile) == 0; 211 else 212 ftruncate(infd, ftell(infp)); 213 } 214 } 215 free(mems); 216 free(outl); 217 free(line); 218 fclose(outfp); 219 } 220 remove(grouptmp); 221 } 222 fclose(infp); 223 } 224 } 225 return rc; 226 } 227