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