]> git.cameronkatri.com Git - pw-darwin.git/blob - pw/edgroup.c
Don't assume a_name is a number just because the first character
[pw-darwin.git] / pw / edgroup.c
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
27 #ifndef lint
28 static const char rcsid[] =
29 "$Id: edgroup.c,v 1.5 1997/10/10 06:23:30 charnier Exp $";
30 #endif /* not lint */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <pwd.h>
40 #include <grp.h>
41 #include <fcntl.h>
42 #include <sys/param.h>
43 #include <ctype.h>
44
45 #include "pwupd.h"
46
47 static int
48 isingroup(char const * name, char **mem)
49 {
50 int i;
51
52 for (i = 0; mem[i] != NULL; i++)
53 if (strcmp(name, mem[i]) == 0)
54 return i;
55 return -1;
56 }
57
58 static char groupfile[] = _PATH_GROUP;
59 static char grouptmp[] = _PATH_GROUP ".new";
60
61 int
62 editgroups(char *name, char **groups)
63 {
64 int rc = 0;
65 int infd;
66
67 if ((infd = open(groupfile, O_RDWR | O_CREAT, 0644)) != -1) {
68 FILE *infp;
69
70 if ((infp = fdopen(infd, "r+")) == NULL)
71 close(infd);
72 else {
73 int outfd;
74
75 if ((outfd = open(grouptmp, O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK, 0644)) != -1) {
76 FILE *outfp;
77
78 if ((outfp = fdopen(outfd, "w+")) == NULL)
79 close(outfd);
80 else {
81 int linelen = PWBUFSZ;
82 int outlen = PWBUFSZ;
83 int memlen = 200; /* Arbitrary */
84 char *line = malloc(linelen);
85 char *outl = malloc(outlen);
86 char **mems = malloc(memlen * sizeof(char *));
87 int namlen = strlen(name);
88
89 if (line == NULL || outl == NULL || mems == NULL) {
90 mem_abort:
91 rc = 0;
92 } else {
93 while (fgets(line, linelen, infp) != NULL) {
94 char *p;
95 int l;
96
97 while ((p = strchr(line, '\n')) == NULL)
98 {
99 if (extendline(&line, &linelen, linelen + PWBUFSZ) == -1) {
100 goto mem_abort;
101 }
102 l = strlen(line);
103 if (fgets(line + l, linelen - l, infp) == NULL)
104 break; /* No newline terminator on last line */
105 }
106 l = strlen(line) + namlen + 1;
107 if (extendline(&outl, &outlen, l) == -1) {
108 goto mem_abort;
109 }
110 if (*line == '#')
111 strcpy(outl, line);
112 else if (*line == '\n')
113 *outl = '\0';
114 else {
115 int i,
116 mno = 0;
117 char *cp = line;
118 char const *sep = ":\n";
119 struct group grp;
120
121 memset(&grp, 0, sizeof grp);
122 for (i = 0; (p = strsep(&cp, sep)) != NULL; i++) {
123 switch (i) {
124 case 0: /* Group name */
125 grp.gr_name = p;
126 break;
127 case 1: /* Group password */
128 grp.gr_passwd = p;
129 break;
130 case 2: /* Group id */
131 grp.gr_gid = atoi(p);
132 break;
133 case 3: /* Member list */
134 cp = p;
135 sep = ",\n";
136 break;
137 default: /* Individual members */
138 if (*p) {
139 if (extendarray(&mems, &memlen, mno + 2) == -1) {
140 goto mem_abort;
141 }
142 mems[mno++] = p;
143 }
144 break;
145 }
146 }
147 if (i < 2) /* Bail out - insufficient fields */
148 continue;
149
150 grp.gr_mem = mems;
151 for (i = mno; i < memlen; i++)
152 mems[i] = NULL;
153
154 /*
155 * Delete from group, or add to group?
156 */
157 if (groups == NULL || isingroup(grp.gr_name, groups) == -1) { /* Delete */
158 int idx;
159
160 while ((idx = isingroup(name, mems)) != -1) {
161 for (i = idx; i < (memlen - 1); i++)
162 mems[i] = mems[i + 1];
163 mems[i] = NULL;
164 --mno;
165 }
166 /*
167 * Special case - deleting user and group may be user's own
168 */
169 if (groups == NULL && mems[0] == NULL && strcmp(name, grp.gr_name) == 0) {
170 /*
171 * First, make _sure_ we don't have other members
172 */
173 struct passwd *pwd;
174
175 setpwent();
176 while ((pwd = getpwent()) != NULL && pwd->pw_gid != grp.gr_gid);
177 endpwent();
178 if (pwd == NULL) /* No members at all */
179 continue; /* Drop the group */
180 }
181 } else if (isingroup(name, mems) == -1) {
182 if (extendarray(&mems, &memlen, mno + 2) == -1) {
183 goto mem_abort;
184 }
185 grp.gr_mem = mems; /* May have realloced() */
186 mems[mno++] = name;
187 mems[mno ] = NULL;
188 }
189 fmtgrentry(&outl, &outlen, &grp, PWF_GROUP);
190 }
191 fputs(outl, outfp);
192 }
193 if (fflush(outfp) != EOF) {
194 rc = 1;
195
196 /*
197 * Copy data back into the original file and truncate
198 */
199 rewind(infp);
200 rewind(outfp);
201 while (fgets(outl, outlen, outfp) != NULL)
202 fputs(outl, infp);
203
204 /*
205 * This is a gross hack, but we may have corrupted the
206 * original file. Unfortunately, it will lose preservation
207 * of the inode.
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 }