Deleted Added
full compact
gr_util.c (244739) gr_util.c (244742)
1/*-
2 * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org>
3 * 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 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org>
3 * 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 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/lib/libutil/gr_util.c 244739 2012-12-27 16:51:29Z bapt $");
28__FBSDID("$FreeBSD: head/lib/libutil/gr_util.c 244742 2012-12-27 19:33:43Z bapt $");
29
30#include <sys/param.h>
31#include <sys/errno.h>
32#include <sys/stat.h>
33
34#include <ctype.h>
35#include <err.h>
36#include <fcntl.h>
37#include <grp.h>
38#include <inttypes.h>
39#include <libutil.h>
40#include <paths.h>
41#include <stdbool.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
29
30#include <sys/param.h>
31#include <sys/errno.h>
32#include <sys/stat.h>
33
34#include <ctype.h>
35#include <err.h>
36#include <fcntl.h>
37#include <grp.h>
38#include <inttypes.h>
39#include <libutil.h>
40#include <paths.h>
41#include <stdbool.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47struct group_storage {
48 struct group gr;
49 char *members[];
50};
51
52static int lockfd = -1;
53static char group_dir[PATH_MAX];
54static char group_file[PATH_MAX];
55static char tempname[PATH_MAX];
56static int initialized;
57
58static const char group_line_format[] = "%s:%s:%ju:";
59
60/*
61 * Initialize statics
62 */
63int
64gr_init(const char *dir, const char *group)
65{
66
67 if (dir == NULL) {
68 strcpy(group_dir, _PATH_ETC);
69 } else {
70 if (strlen(dir) >= sizeof(group_dir)) {
71 errno = ENAMETOOLONG;
72 return (-1);
73 }
74 strcpy(group_dir, dir);
75 }
76
77 if (group == NULL) {
78 if (dir == NULL) {
79 strcpy(group_file, _PATH_GROUP);
80 } else if (snprintf(group_file, sizeof(group_file), "%s/group",
81 group_dir) > (int)sizeof(group_file)) {
82 errno = ENAMETOOLONG;
83 return (-1);
84 }
85 } else {
86 if (strlen(group) >= sizeof(group_file)) {
87 errno = ENAMETOOLONG;
88 return (-1);
89 }
90 strcpy(group_file, group);
91 }
92
93 initialized = 1;
94 return (0);
95}
96
97/*
98 * Lock the group file
99 */
100int
101gr_lock(void)
102{
103 if (*group_file == '\0')
104 return (-1);
105
106 for (;;) {
107 struct stat st;
108
109 lockfd = flopen(group_file, O_RDONLY|O_NONBLOCK, 0);
110 if (lockfd == -1) {
111 if (errno == EWOULDBLOCK) {
112 errx(1, "the group file is busy");
113 } else {
114 err(1, "could not lock the group file: ");
115 }
116 }
117 if (fstat(lockfd, &st) == -1)
118 err(1, "fstat() failed: ");
119 if (st.st_nlink != 0)
120 break;
121 close(lockfd);
122 lockfd = -1;
123 }
124 return (lockfd);
125}
126
127/*
128 * Create and open a presmuably safe temp file for editing group data
129 */
130int
131gr_tmp(int mfd)
132{
133 char buf[8192];
134 ssize_t nr;
135 const char *p;
136 int tfd;
137
138 if (*group_file == '\0')
139 return (-1);
140 if ((p = strrchr(group_file, '/')))
141 ++p;
142 else
143 p = group_file;
144 if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX",
145 (int)(p - group_file), group_file) >= (int)sizeof(tempname)) {
146 errno = ENAMETOOLONG;
147 return (-1);
148 }
149 if ((tfd = mkstemp(tempname)) == -1)
150 return (-1);
151 if (mfd != -1) {
152 while ((nr = read(mfd, buf, sizeof(buf))) > 0)
153 if (write(tfd, buf, (size_t)nr) != nr)
154 break;
155 if (nr != 0) {
156 unlink(tempname);
157 *tempname = '\0';
158 close(tfd);
159 return (-1);
160 }
161 }
162 return (tfd);
163}
164
165/*
166 * Copy the group file from one descriptor to another, replacing, deleting
167 * or adding a single record on the way.
168 */
169int
170gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr)
171{
172 char buf[8192], *end, *line, *p, *q, *r, t;
173 struct group *fgr;
174 const struct group *sgr;
175 size_t len;
176 int eof, readlen;
177
178 sgr = gr;
179 if (gr == NULL) {
180 line = NULL;
181 if (old_gr == NULL)
182 return (-1);
183 sgr = old_gr;
184 } else if ((line = gr_make(gr)) == NULL)
185 return (-1);
186
187 eof = 0;
188 len = 0;
189 p = q = end = buf;
190 for (;;) {
191 /* find the end of the current line */
192 for (p = q; q < end && *q != '\0'; ++q)
193 if (*q == '\n')
194 break;
195
196 /* if we don't have a complete line, fill up the buffer */
197 if (q >= end) {
198 if (eof)
199 break;
200 if ((size_t)(q - p) >= sizeof(buf)) {
201 warnx("group line too long");
202 errno = EINVAL; /* hack */
203 goto err;
204 }
205 if (p < end) {
206 q = memmove(buf, p, end -p);
207 end -= p - buf;
208 } else {
209 p = q = end = buf;
210 }
211 readlen = read(ffd, end, sizeof(buf) - (end -buf));
212 if (readlen == -1)
213 goto err;
214 else
215 len = (size_t)readlen;
216 if (len == 0 && p == buf)
217 break;
218 end += len;
219 len = end - buf;
220 if (len < (ssize_t)sizeof(buf)) {
221 eof = 1;
222 if (len > 0 && buf[len -1] != '\n')
223 ++len, *end++ = '\n';
224 }
225 continue;
226 }
227
228 /* is it a blank line or a comment? */
229 for (r = p; r < q && isspace(*r); ++r)
230 /* nothing */;
231 if (r == q || *r == '#') {
232 /* yep */
233 if (write(tfd, p, q -p + 1) != q - p + 1)
234 goto err;
235 ++q;
236 continue;
237 }
238
239 /* is it the one we're looking for? */
240
241 t = *q;
242 *q = '\0';
243
244 fgr = gr_scan(r);
245
246 /* fgr is either a struct group for the current line,
247 * or NULL if the line is malformed.
248 */
249
250 *q = t;
251 if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) {
252 /* nope */
253 if (fgr != NULL)
254 free(fgr);
255 if (write(tfd, p, q - p + 1) != q - p + 1)
256 goto err;
257 ++q;
258 continue;
259 }
260 if (old_gr && !gr_equal(fgr, old_gr)) {
261 warnx("entry inconsistent");
262 free(fgr);
263 errno = EINVAL; /* hack */
264 goto err;
265 }
266 free(fgr);
267
268 /* it is, replace or remove it */
269 if (line != NULL) {
270 len = strlen(line);
271 if (write(tfd, line, len) != (int) len)
272 goto err;
273 } else {
274 /* when removed, avoid the \n */
275 q++;
276 }
277 /* we're done, just copy the rest over */
278 for (;;) {
279 if (write(tfd, q, end - q) != end - q)
280 goto err;
281 q = buf;
282 readlen = read(ffd, buf, sizeof(buf));
283 if (readlen == 0)
284 break;
285 else
286 len = (size_t)readlen;
287 if (readlen == -1)
288 goto err;
289 end = buf + len;
290 }
291 goto done;
292 }
293
294 /* if we got here, we didn't find the old entry */
295 if (line == NULL) {
296 errno = ENOENT;
297 goto err;
298 }
299 len = strlen(line);
300 if ((size_t)write(tfd, line, len) != len ||
301 write(tfd, "\n", 1) != 1)
302 goto err;
303 done:
304 if (line != NULL)
305 free(line);
306 return (0);
307 err:
308 if (line != NULL)
309 free(line);
310 return (-1);
311}
312
313/*
314 * Regenerate the group file
315 */
316int
317gr_mkdb(void)
318{
319 if (chmod(tempname, 0644) != 0)
320 return (-1);
321
322 return (rename(tempname, group_file));
323}
324
325/*
326 * Clean up. Preserver errno for the caller's convenience.
327 */
328void
329gr_fini(void)
330{
331 int serrno;
332
333 if (!initialized)
334 return;
335 initialized = 0;
336 serrno = errno;
337 if (*tempname != '\0') {
338 unlink(tempname);
339 *tempname = '\0';
340 }
341 if (lockfd != -1)
342 close(lockfd);
343 errno = serrno;
344}
345
346/*
347 * Compares two struct group's.
348 */
349int
350gr_equal(const struct group *gr1, const struct group *gr2)
351{
352 int gr1_ndx;
353 int gr2_ndx;
354 bool found;
355
356 /* Check that the non-member information is the same. */
357 if (gr1->gr_name == NULL || gr2->gr_name == NULL) {
358 if (gr1->gr_name != gr2->gr_name)
359 return (false);
360 } else if (strcmp(gr1->gr_name, gr2->gr_name) != 0)
361 return (false);
362 if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) {
363 if (gr1->gr_passwd != gr2->gr_passwd)
364 return (false);
365 } else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0)
366 return (false);
367 if (gr1->gr_gid != gr2->gr_gid)
368 return (false);
369
370 /* Check all members in both groups. */
371 if (gr1->gr_mem == NULL || gr2->gr_mem == NULL) {
372 if (gr1->gr_mem != gr2->gr_mem)
373 return (false);
374 } else {
375 for (found = false, gr1_ndx = 0; gr1->gr_mem[gr1_ndx] != NULL;
376 gr1_ndx++) {
377 for (gr2_ndx = 0; gr2->gr_mem[gr2_ndx] != NULL;
378 gr2_ndx++)
379 if (strcmp(gr1->gr_mem[gr1_ndx],
380 gr2->gr_mem[gr2_ndx]) == 0) {
381 found = true;
382 break;
383 }
384 if (!found)
385 return (false);
386 }
387
388 /* Check that group2 does not have more members than group1. */
389 if (gr2->gr_mem[gr1_ndx] != NULL)
390 return (false);
391 }
392
393 return (true);
394}
395
396/*
397 * Make a group line out of a struct group.
398 */
399char *
400gr_make(const struct group *gr)
401{
402 char *line;
403 size_t line_size;
404 int ndx;
405
406 /* Calculate the length of the group line. */
407 line_size = snprintf(NULL, 0, group_line_format, gr->gr_name,
408 gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1;
409 if (gr->gr_mem != NULL) {
410 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++)
411 line_size += strlen(gr->gr_mem[ndx]) + 1;
412 if (ndx > 0)
413 line_size--;
414 }
415
416 /* Create the group line and fill it. */
417 if ((line = malloc(line_size)) == NULL)
418 return (NULL);
419 snprintf(line, line_size, group_line_format, gr->gr_name, gr->gr_passwd,
420 (uintmax_t)gr->gr_gid);
421 if (gr->gr_mem != NULL)
422 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) {
423 strcat(line, gr->gr_mem[ndx]);
424 if (gr->gr_mem[ndx + 1] != NULL)
425 strcat(line, ",");
426 }
427
428 return (line);
429}
430
431/*
432 * Duplicate a struct group.
433 */
434struct group *
435gr_dup(const struct group *gr)
436{
47static int lockfd = -1;
48static char group_dir[PATH_MAX];
49static char group_file[PATH_MAX];
50static char tempname[PATH_MAX];
51static int initialized;
52
53static const char group_line_format[] = "%s:%s:%ju:";
54
55/*
56 * Initialize statics
57 */
58int
59gr_init(const char *dir, const char *group)
60{
61
62 if (dir == NULL) {
63 strcpy(group_dir, _PATH_ETC);
64 } else {
65 if (strlen(dir) >= sizeof(group_dir)) {
66 errno = ENAMETOOLONG;
67 return (-1);
68 }
69 strcpy(group_dir, dir);
70 }
71
72 if (group == NULL) {
73 if (dir == NULL) {
74 strcpy(group_file, _PATH_GROUP);
75 } else if (snprintf(group_file, sizeof(group_file), "%s/group",
76 group_dir) > (int)sizeof(group_file)) {
77 errno = ENAMETOOLONG;
78 return (-1);
79 }
80 } else {
81 if (strlen(group) >= sizeof(group_file)) {
82 errno = ENAMETOOLONG;
83 return (-1);
84 }
85 strcpy(group_file, group);
86 }
87
88 initialized = 1;
89 return (0);
90}
91
92/*
93 * Lock the group file
94 */
95int
96gr_lock(void)
97{
98 if (*group_file == '\0')
99 return (-1);
100
101 for (;;) {
102 struct stat st;
103
104 lockfd = flopen(group_file, O_RDONLY|O_NONBLOCK, 0);
105 if (lockfd == -1) {
106 if (errno == EWOULDBLOCK) {
107 errx(1, "the group file is busy");
108 } else {
109 err(1, "could not lock the group file: ");
110 }
111 }
112 if (fstat(lockfd, &st) == -1)
113 err(1, "fstat() failed: ");
114 if (st.st_nlink != 0)
115 break;
116 close(lockfd);
117 lockfd = -1;
118 }
119 return (lockfd);
120}
121
122/*
123 * Create and open a presmuably safe temp file for editing group data
124 */
125int
126gr_tmp(int mfd)
127{
128 char buf[8192];
129 ssize_t nr;
130 const char *p;
131 int tfd;
132
133 if (*group_file == '\0')
134 return (-1);
135 if ((p = strrchr(group_file, '/')))
136 ++p;
137 else
138 p = group_file;
139 if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX",
140 (int)(p - group_file), group_file) >= (int)sizeof(tempname)) {
141 errno = ENAMETOOLONG;
142 return (-1);
143 }
144 if ((tfd = mkstemp(tempname)) == -1)
145 return (-1);
146 if (mfd != -1) {
147 while ((nr = read(mfd, buf, sizeof(buf))) > 0)
148 if (write(tfd, buf, (size_t)nr) != nr)
149 break;
150 if (nr != 0) {
151 unlink(tempname);
152 *tempname = '\0';
153 close(tfd);
154 return (-1);
155 }
156 }
157 return (tfd);
158}
159
160/*
161 * Copy the group file from one descriptor to another, replacing, deleting
162 * or adding a single record on the way.
163 */
164int
165gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr)
166{
167 char buf[8192], *end, *line, *p, *q, *r, t;
168 struct group *fgr;
169 const struct group *sgr;
170 size_t len;
171 int eof, readlen;
172
173 sgr = gr;
174 if (gr == NULL) {
175 line = NULL;
176 if (old_gr == NULL)
177 return (-1);
178 sgr = old_gr;
179 } else if ((line = gr_make(gr)) == NULL)
180 return (-1);
181
182 eof = 0;
183 len = 0;
184 p = q = end = buf;
185 for (;;) {
186 /* find the end of the current line */
187 for (p = q; q < end && *q != '\0'; ++q)
188 if (*q == '\n')
189 break;
190
191 /* if we don't have a complete line, fill up the buffer */
192 if (q >= end) {
193 if (eof)
194 break;
195 if ((size_t)(q - p) >= sizeof(buf)) {
196 warnx("group line too long");
197 errno = EINVAL; /* hack */
198 goto err;
199 }
200 if (p < end) {
201 q = memmove(buf, p, end -p);
202 end -= p - buf;
203 } else {
204 p = q = end = buf;
205 }
206 readlen = read(ffd, end, sizeof(buf) - (end -buf));
207 if (readlen == -1)
208 goto err;
209 else
210 len = (size_t)readlen;
211 if (len == 0 && p == buf)
212 break;
213 end += len;
214 len = end - buf;
215 if (len < (ssize_t)sizeof(buf)) {
216 eof = 1;
217 if (len > 0 && buf[len -1] != '\n')
218 ++len, *end++ = '\n';
219 }
220 continue;
221 }
222
223 /* is it a blank line or a comment? */
224 for (r = p; r < q && isspace(*r); ++r)
225 /* nothing */;
226 if (r == q || *r == '#') {
227 /* yep */
228 if (write(tfd, p, q -p + 1) != q - p + 1)
229 goto err;
230 ++q;
231 continue;
232 }
233
234 /* is it the one we're looking for? */
235
236 t = *q;
237 *q = '\0';
238
239 fgr = gr_scan(r);
240
241 /* fgr is either a struct group for the current line,
242 * or NULL if the line is malformed.
243 */
244
245 *q = t;
246 if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) {
247 /* nope */
248 if (fgr != NULL)
249 free(fgr);
250 if (write(tfd, p, q - p + 1) != q - p + 1)
251 goto err;
252 ++q;
253 continue;
254 }
255 if (old_gr && !gr_equal(fgr, old_gr)) {
256 warnx("entry inconsistent");
257 free(fgr);
258 errno = EINVAL; /* hack */
259 goto err;
260 }
261 free(fgr);
262
263 /* it is, replace or remove it */
264 if (line != NULL) {
265 len = strlen(line);
266 if (write(tfd, line, len) != (int) len)
267 goto err;
268 } else {
269 /* when removed, avoid the \n */
270 q++;
271 }
272 /* we're done, just copy the rest over */
273 for (;;) {
274 if (write(tfd, q, end - q) != end - q)
275 goto err;
276 q = buf;
277 readlen = read(ffd, buf, sizeof(buf));
278 if (readlen == 0)
279 break;
280 else
281 len = (size_t)readlen;
282 if (readlen == -1)
283 goto err;
284 end = buf + len;
285 }
286 goto done;
287 }
288
289 /* if we got here, we didn't find the old entry */
290 if (line == NULL) {
291 errno = ENOENT;
292 goto err;
293 }
294 len = strlen(line);
295 if ((size_t)write(tfd, line, len) != len ||
296 write(tfd, "\n", 1) != 1)
297 goto err;
298 done:
299 if (line != NULL)
300 free(line);
301 return (0);
302 err:
303 if (line != NULL)
304 free(line);
305 return (-1);
306}
307
308/*
309 * Regenerate the group file
310 */
311int
312gr_mkdb(void)
313{
314 if (chmod(tempname, 0644) != 0)
315 return (-1);
316
317 return (rename(tempname, group_file));
318}
319
320/*
321 * Clean up. Preserver errno for the caller's convenience.
322 */
323void
324gr_fini(void)
325{
326 int serrno;
327
328 if (!initialized)
329 return;
330 initialized = 0;
331 serrno = errno;
332 if (*tempname != '\0') {
333 unlink(tempname);
334 *tempname = '\0';
335 }
336 if (lockfd != -1)
337 close(lockfd);
338 errno = serrno;
339}
340
341/*
342 * Compares two struct group's.
343 */
344int
345gr_equal(const struct group *gr1, const struct group *gr2)
346{
347 int gr1_ndx;
348 int gr2_ndx;
349 bool found;
350
351 /* Check that the non-member information is the same. */
352 if (gr1->gr_name == NULL || gr2->gr_name == NULL) {
353 if (gr1->gr_name != gr2->gr_name)
354 return (false);
355 } else if (strcmp(gr1->gr_name, gr2->gr_name) != 0)
356 return (false);
357 if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) {
358 if (gr1->gr_passwd != gr2->gr_passwd)
359 return (false);
360 } else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0)
361 return (false);
362 if (gr1->gr_gid != gr2->gr_gid)
363 return (false);
364
365 /* Check all members in both groups. */
366 if (gr1->gr_mem == NULL || gr2->gr_mem == NULL) {
367 if (gr1->gr_mem != gr2->gr_mem)
368 return (false);
369 } else {
370 for (found = false, gr1_ndx = 0; gr1->gr_mem[gr1_ndx] != NULL;
371 gr1_ndx++) {
372 for (gr2_ndx = 0; gr2->gr_mem[gr2_ndx] != NULL;
373 gr2_ndx++)
374 if (strcmp(gr1->gr_mem[gr1_ndx],
375 gr2->gr_mem[gr2_ndx]) == 0) {
376 found = true;
377 break;
378 }
379 if (!found)
380 return (false);
381 }
382
383 /* Check that group2 does not have more members than group1. */
384 if (gr2->gr_mem[gr1_ndx] != NULL)
385 return (false);
386 }
387
388 return (true);
389}
390
391/*
392 * Make a group line out of a struct group.
393 */
394char *
395gr_make(const struct group *gr)
396{
397 char *line;
398 size_t line_size;
399 int ndx;
400
401 /* Calculate the length of the group line. */
402 line_size = snprintf(NULL, 0, group_line_format, gr->gr_name,
403 gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1;
404 if (gr->gr_mem != NULL) {
405 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++)
406 line_size += strlen(gr->gr_mem[ndx]) + 1;
407 if (ndx > 0)
408 line_size--;
409 }
410
411 /* Create the group line and fill it. */
412 if ((line = malloc(line_size)) == NULL)
413 return (NULL);
414 snprintf(line, line_size, group_line_format, gr->gr_name, gr->gr_passwd,
415 (uintmax_t)gr->gr_gid);
416 if (gr->gr_mem != NULL)
417 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) {
418 strcat(line, gr->gr_mem[ndx]);
419 if (gr->gr_mem[ndx + 1] != NULL)
420 strcat(line, ",");
421 }
422
423 return (line);
424}
425
426/*
427 * Duplicate a struct group.
428 */
429struct group *
430gr_dup(const struct group *gr)
431{
432 struct group *newgr;
437 char *dst;
438 size_t len;
433 char *dst;
434 size_t len;
439 struct group_storage *gs;
440 int ndx;
441 int num_mem;
442
443 /* Calculate size of the group. */
435 int ndx;
436 int num_mem;
437
438 /* Calculate size of the group. */
444 len = sizeof(*gs);
439 len = sizeof(*newgr);
445 if (gr->gr_name != NULL)
446 len += strlen(gr->gr_name) + 1;
447 if (gr->gr_passwd != NULL)
448 len += strlen(gr->gr_passwd) + 1;
449 if (gr->gr_mem != NULL) {
450 for (num_mem = 0; gr->gr_mem[num_mem] != NULL; num_mem++)
451 len += strlen(gr->gr_mem[num_mem]) + 1;
452 len += (num_mem + 1) * sizeof(*gr->gr_mem);
453 } else
454 num_mem = -1;
440 if (gr->gr_name != NULL)
441 len += strlen(gr->gr_name) + 1;
442 if (gr->gr_passwd != NULL)
443 len += strlen(gr->gr_passwd) + 1;
444 if (gr->gr_mem != NULL) {
445 for (num_mem = 0; gr->gr_mem[num_mem] != NULL; num_mem++)
446 len += strlen(gr->gr_mem[num_mem]) + 1;
447 len += (num_mem + 1) * sizeof(*gr->gr_mem);
448 } else
449 num_mem = -1;
455
456 /* Create new group and copy old group into it. */
450 /* Create new group and copy old group into it. */
457 if ((gs = calloc(1, len)) == NULL)
451 if ((newgr = malloc(len)) == NULL)
458 return (NULL);
452 return (NULL);
459 dst = (char *)&gs->members[num_mem + 1];
453 /* point new gr_mem to end of struct + 1 */
454 if (gr->gr_mem != NULL)
455 newgr->gr_mem = (char **)newgr + sizeof(struct group);
456 else
457 newgr->gr_mem = NULL;
458 /* point dst after the end of all the gr_mem pointers in newgr */
459 dst = (char *)newgr + sizeof(struct group) +
460 (num_mem + 1) * sizeof(*gr->gr_mem);
460 if (gr->gr_name != NULL) {
461 if (gr->gr_name != NULL) {
461 gs->gr.gr_name = dst;
462 dst = stpcpy(gs->gr.gr_name, gr->gr_name) + 1;
462 newgr->gr_name = dst;
463 dst = stpcpy(dst, gr->gr_name) + 1;
463 }
464 if (gr->gr_passwd != NULL) {
464 }
465 if (gr->gr_passwd != NULL) {
465 gs->gr.gr_passwd = dst;
466 dst = stpcpy(gs->gr.gr_passwd, gr->gr_passwd) + 1;
466 newgr->gr_passwd = dst;
467 dst = stpcpy(dst, gr->gr_passwd) + 1;
467 }
468 }
468 gs->gr.gr_gid = gr->gr_gid;
469 newgr->gr_gid = gr->gr_gid;
469 if (gr->gr_mem != NULL) {
470 if (gr->gr_mem != NULL) {
470 gs->gr.gr_mem = gs->members;
471 for (ndx = 0; ndx < num_mem; ndx++) {
471 for (ndx = 0; ndx < num_mem; ndx++) {
472 gs->gr.gr_mem[ndx] = dst;
473 dst = stpcpy(gs->gr.gr_mem[ndx], gr->gr_mem[ndx]) + 1;
472 newgr->gr_mem[ndx] = dst;
473 dst = stpcpy(dst, gr->gr_mem[ndx]) + 1;
474 }
474 }
475 gs->gr.gr_mem[ndx] = NULL;
475 newgr->gr_mem[ndx] = NULL;
476 }
476 }
477
478 return (&gs->gr);
477 return (newgr);
479}
480
481/*
482 * Add a new member name to a struct group.
483 */
484struct group *
485gr_add(struct group *gr, char *newmember)
486{
487 size_t mlen;
488 int num_mem=0;
489 char **members;
490 struct group *newgr;
491
492 if (newmember == NULL)
493 return(gr_dup(gr));
494
495 if (gr->gr_mem != NULL) {
496 for (num_mem = 0; gr->gr_mem[num_mem] != NULL; num_mem++) {
497 if (strcmp(gr->gr_mem[num_mem], newmember) == 0) {
498 errno = EEXIST;
499 return (NULL);
500 }
501 }
502 }
503 /* Allocate enough for current pointers + 1 more and NULL marker */
504 mlen = (num_mem + 2) * sizeof(*gr->gr_mem);
505 if ((members = malloc(mlen)) == NULL) {
506 errno = ENOMEM;
507 return (NULL);
508 }
509 memcpy(members, gr->gr_mem, num_mem * sizeof(*gr->gr_mem));
510 members[num_mem++] = newmember;
511 members[num_mem] = NULL;
512 gr->gr_mem = members;
513 newgr = gr_dup(gr);
514 if (newgr == NULL)
515 errno = ENOMEM;
516 free(members);
517 return (newgr);
518}
519
520/*
521 * Scan a line and place it into a group structure.
522 */
523static bool
524__gr_scan(char *line, struct group *gr)
525{
526 char *loc;
527 int ndx;
528
529 /* Assign non-member information to structure. */
530 gr->gr_name = line;
531 if ((loc = strchr(line, ':')) == NULL)
532 return (false);
533 *loc = '\0';
534 gr->gr_passwd = loc + 1;
535 if (*gr->gr_passwd == ':')
536 *gr->gr_passwd = '\0';
537 else {
538 if ((loc = strchr(loc + 1, ':')) == NULL)
539 return (false);
540 *loc = '\0';
541 }
542 if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1)
543 return (false);
544
545 /* Assign member information to structure. */
546 if ((loc = strchr(loc + 1, ':')) == NULL)
547 return (false);
548 line = loc + 1;
549 gr->gr_mem = NULL;
550 ndx = 0;
551 do {
552 gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) *
553 (ndx + 1));
554 if (gr->gr_mem == NULL)
555 return (false);
556
557 /* Skip locations without members (i.e., empty string). */
558 do {
559 gr->gr_mem[ndx] = strsep(&line, ",");
560 } while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0');
561 } while (gr->gr_mem[ndx++] != NULL);
562
563 return (true);
564}
565
566/*
567 * Create a struct group from a line.
568 */
569struct group *
570gr_scan(const char *line)
571{
572 struct group gr;
573 char *line_copy;
574 struct group *new_gr;
575
576 if ((line_copy = strdup(line)) == NULL)
577 return (NULL);
578 if (!__gr_scan(line_copy, &gr)) {
579 free(line_copy);
580 return (NULL);
581 }
582 new_gr = gr_dup(&gr);
583 free(line_copy);
584 if (gr.gr_mem != NULL)
585 free(gr.gr_mem);
586
587 return (new_gr);
588}
478}
479
480/*
481 * Add a new member name to a struct group.
482 */
483struct group *
484gr_add(struct group *gr, char *newmember)
485{
486 size_t mlen;
487 int num_mem=0;
488 char **members;
489 struct group *newgr;
490
491 if (newmember == NULL)
492 return(gr_dup(gr));
493
494 if (gr->gr_mem != NULL) {
495 for (num_mem = 0; gr->gr_mem[num_mem] != NULL; num_mem++) {
496 if (strcmp(gr->gr_mem[num_mem], newmember) == 0) {
497 errno = EEXIST;
498 return (NULL);
499 }
500 }
501 }
502 /* Allocate enough for current pointers + 1 more and NULL marker */
503 mlen = (num_mem + 2) * sizeof(*gr->gr_mem);
504 if ((members = malloc(mlen)) == NULL) {
505 errno = ENOMEM;
506 return (NULL);
507 }
508 memcpy(members, gr->gr_mem, num_mem * sizeof(*gr->gr_mem));
509 members[num_mem++] = newmember;
510 members[num_mem] = NULL;
511 gr->gr_mem = members;
512 newgr = gr_dup(gr);
513 if (newgr == NULL)
514 errno = ENOMEM;
515 free(members);
516 return (newgr);
517}
518
519/*
520 * Scan a line and place it into a group structure.
521 */
522static bool
523__gr_scan(char *line, struct group *gr)
524{
525 char *loc;
526 int ndx;
527
528 /* Assign non-member information to structure. */
529 gr->gr_name = line;
530 if ((loc = strchr(line, ':')) == NULL)
531 return (false);
532 *loc = '\0';
533 gr->gr_passwd = loc + 1;
534 if (*gr->gr_passwd == ':')
535 *gr->gr_passwd = '\0';
536 else {
537 if ((loc = strchr(loc + 1, ':')) == NULL)
538 return (false);
539 *loc = '\0';
540 }
541 if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1)
542 return (false);
543
544 /* Assign member information to structure. */
545 if ((loc = strchr(loc + 1, ':')) == NULL)
546 return (false);
547 line = loc + 1;
548 gr->gr_mem = NULL;
549 ndx = 0;
550 do {
551 gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) *
552 (ndx + 1));
553 if (gr->gr_mem == NULL)
554 return (false);
555
556 /* Skip locations without members (i.e., empty string). */
557 do {
558 gr->gr_mem[ndx] = strsep(&line, ",");
559 } while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0');
560 } while (gr->gr_mem[ndx++] != NULL);
561
562 return (true);
563}
564
565/*
566 * Create a struct group from a line.
567 */
568struct group *
569gr_scan(const char *line)
570{
571 struct group gr;
572 char *line_copy;
573 struct group *new_gr;
574
575 if ((line_copy = strdup(line)) == NULL)
576 return (NULL);
577 if (!__gr_scan(line_copy, &gr)) {
578 free(line_copy);
579 return (NULL);
580 }
581 new_gr = gr_dup(&gr);
582 free(line_copy);
583 if (gr.gr_mem != NULL)
584 free(gr.gr_mem);
585
586 return (new_gr);
587}