]> git.cameronkatri.com Git - pw-darwin.git/blob - libutil/gr_util.c
IFC @ r242684
[pw-darwin.git] / libutil / gr_util.c
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$");
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
47 struct group_storage {
48 struct group gr;
49 char *members[];
50 };
51
52 static int lockfd = -1;
53 static char group_dir[PATH_MAX];
54 static char group_file[PATH_MAX];
55 static char tempname[PATH_MAX];
56 static int initialized;
57
58 static const char group_line_format[] = "%s:%s:%ju:";
59
60 /*
61 * Initialize statics
62 */
63 int
64 gr_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 */
100 int
101 gr_lock(void)
102 {
103 if (*group_file == '\0')
104 return (-1);
105
106 for (;;) {
107 struct stat st;
108
109 lockfd = open(group_file, O_RDONLY, 0);
110 if (lockfd < 0 || fcntl(lockfd, F_SETFD, 1) == -1)
111 err(1, "%s", group_file);
112 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
113 if (errno == EWOULDBLOCK) {
114 errx(1, "the group file is busy");
115 } else {
116 err(1, "could not lock the group file: ");
117 }
118 }
119 if (fstat(lockfd, &st) == -1)
120 err(1, "fstat() failed: ");
121 if (st.st_nlink != 0)
122 break;
123 close(lockfd);
124 lockfd = -1;
125 }
126 return (lockfd);
127 }
128
129 /*
130 * Create and open a presmuably safe temp file for editing group data
131 */
132 int
133 gr_tmp(int mfd)
134 {
135 char buf[8192];
136 ssize_t nr;
137 const char *p;
138 int tfd;
139
140 if (*group_file == '\0')
141 return (-1);
142 if ((p = strrchr(group_file, '/')))
143 ++p;
144 else
145 p = group_file;
146 if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX",
147 (int)(p - group_file), group_file) >= (int)sizeof(tempname)) {
148 errno = ENAMETOOLONG;
149 return (-1);
150 }
151 if ((tfd = mkstemp(tempname)) == -1)
152 return (-1);
153 if (mfd != -1) {
154 while ((nr = read(mfd, buf, sizeof(buf))) > 0)
155 if (write(tfd, buf, (size_t)nr) != nr)
156 break;
157 if (nr != 0) {
158 unlink(tempname);
159 *tempname = '\0';
160 close(tfd);
161 return (-1);
162 }
163 }
164 return (tfd);
165 }
166
167 /*
168 * Copy the group file from one descriptor to another, replacing, deleting
169 * or adding a single record on the way.
170 */
171 int
172 gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr)
173 {
174 char buf[8192], *end, *line, *p, *q, *r, t;
175 struct group *fgr;
176 const struct group *sgr;
177 size_t len;
178 int eof, readlen;
179
180 sgr = gr;
181 if (gr == NULL) {
182 line = NULL;
183 if (old_gr == NULL)
184 return (-1);
185 sgr = old_gr;
186 } else if ((line = gr_make(gr)) == NULL)
187 return (-1);
188
189 eof = 0;
190 len = 0;
191 p = q = end = buf;
192 for (;;) {
193 /* find the end of the current line */
194 for (p = q; q < end && *q != '\0'; ++q)
195 if (*q == '\n')
196 break;
197
198 /* if we don't have a complete line, fill up the buffer */
199 if (q >= end) {
200 if (eof)
201 break;
202 if ((size_t)(q - p) >= sizeof(buf)) {
203 warnx("group line too long");
204 errno = EINVAL; /* hack */
205 goto err;
206 }
207 if (p < end) {
208 q = memmove(buf, p, end -p);
209 end -= p - buf;
210 } else {
211 p = q = end = buf;
212 }
213 readlen = read(ffd, end, sizeof(buf) - (end -buf));
214 if (readlen == -1)
215 goto err;
216 else
217 len = (size_t)readlen;
218 if (len == 0 && p == buf)
219 break;
220 end += len;
221 len = end - buf;
222 if (len < (ssize_t)sizeof(buf)) {
223 eof = 1;
224 if (len > 0 && buf[len -1] != '\n')
225 ++len, *end++ = '\n';
226 }
227 continue;
228 }
229
230 /* is it a blank line or a comment? */
231 for (r = p; r < q && isspace(*r); ++r)
232 /* nothing */;
233 if (r == q || *r == '#') {
234 /* yep */
235 if (write(tfd, p, q -p + 1) != q - p + 1)
236 goto err;
237 ++q;
238 continue;
239 }
240
241 /* is it the one we're looking for? */
242
243 t = *q;
244 *q = '\0';
245
246 fgr = gr_scan(r);
247
248 /* fgr is either a struct group for the current line,
249 * or NULL if the line is malformed.
250 */
251
252 *q = t;
253 if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) {
254 /* nope */
255 if (fgr != NULL)
256 free(fgr);
257 if (write(tfd, p, q - p + 1) != q - p + 1)
258 goto err;
259 ++q;
260 continue;
261 }
262 if (old_gr && !gr_equal(fgr, old_gr)) {
263 warnx("entry inconsistent");
264 free(fgr);
265 errno = EINVAL; /* hack */
266 goto err;
267 }
268 free(fgr);
269
270 /* it is, replace or remove it */
271 if (line != NULL) {
272 len = strlen(line);
273 if (write(tfd, line, len) != (int) len)
274 goto err;
275 } else {
276 /* when removed, avoid the \n */
277 q++;
278 }
279 /* we're done, just copy the rest over */
280 for (;;) {
281 if (write(tfd, q, end - q) != end - q)
282 goto err;
283 q = buf;
284 readlen = read(ffd, buf, sizeof(buf));
285 if (readlen == 0)
286 break;
287 else
288 len = (size_t)readlen;
289 if (readlen == -1)
290 goto err;
291 end = buf + len;
292 }
293 goto done;
294 }
295
296 /* if we got here, we didn't find the old entry */
297 if (line == NULL) {
298 errno = ENOENT;
299 goto err;
300 }
301 len = strlen(line);
302 if ((size_t)write(tfd, line, len) != len ||
303 write(tfd, "\n", 1) != 1)
304 goto err;
305 done:
306 if (line != NULL)
307 free(line);
308 return (0);
309 err:
310 if (line != NULL)
311 free(line);
312 return (-1);
313 }
314
315 /*
316 * Regenerate the group file
317 */
318 int
319 gr_mkdb(void)
320 {
321 return (rename(tempname, group_file));
322 }
323
324 /*
325 * Clean up. Preserver errno for the caller's convenience.
326 */
327 void
328 gr_fini(void)
329 {
330 int serrno;
331
332 if (!initialized)
333 return;
334 initialized = 0;
335 serrno = errno;
336 if (*tempname != '\0') {
337 unlink(tempname);
338 *tempname = '\0';
339 }
340 if (lockfd != -1)
341 close(lockfd);
342 errno = serrno;
343 }
344
345 /*
346 * Compares two struct group's.
347 */
348 int
349 gr_equal(const struct group *gr1, const struct group *gr2)
350 {
351 int gr1_ndx;
352 int gr2_ndx;
353 bool found;
354
355 /* Check that the non-member information is the same. */
356 if (gr1->gr_name == NULL || gr2->gr_name == NULL) {
357 if (gr1->gr_name != gr2->gr_name)
358 return (false);
359 } else if (strcmp(gr1->gr_name, gr2->gr_name) != 0)
360 return (false);
361 if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) {
362 if (gr1->gr_passwd != gr2->gr_passwd)
363 return (false);
364 } else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0)
365 return (false);
366 if (gr1->gr_gid != gr2->gr_gid)
367 return (false);
368
369 /* Check all members in both groups. */
370 if (gr1->gr_mem == NULL || gr2->gr_mem == NULL) {
371 if (gr1->gr_mem != gr2->gr_mem)
372 return (false);
373 } else {
374 for (found = false, gr1_ndx = 0; gr1->gr_mem[gr1_ndx] != NULL;
375 gr1_ndx++) {
376 for (gr2_ndx = 0; gr2->gr_mem[gr2_ndx] != NULL;
377 gr2_ndx++)
378 if (strcmp(gr1->gr_mem[gr1_ndx],
379 gr2->gr_mem[gr2_ndx]) == 0) {
380 found = true;
381 break;
382 }
383 if (!found)
384 return (false);
385 }
386
387 /* Check that group2 does not have more members than group1. */
388 if (gr2->gr_mem[gr1_ndx] != NULL)
389 return (false);
390 }
391
392 return (true);
393 }
394
395 /*
396 * Make a group line out of a struct group.
397 */
398 char *
399 gr_make(const struct group *gr)
400 {
401 char *line;
402 size_t line_size;
403 int ndx;
404
405 /* Calculate the length of the group line. */
406 line_size = snprintf(NULL, 0, group_line_format, gr->gr_name,
407 gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1;
408 if (gr->gr_mem != NULL) {
409 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++)
410 line_size += strlen(gr->gr_mem[ndx]) + 1;
411 if (ndx > 0)
412 line_size--;
413 }
414
415 /* Create the group line and fill it. */
416 if ((line = malloc(line_size)) == NULL)
417 return (NULL);
418 snprintf(line, line_size, group_line_format, gr->gr_name, gr->gr_passwd,
419 (uintmax_t)gr->gr_gid);
420 if (gr->gr_mem != NULL)
421 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) {
422 strcat(line, gr->gr_mem[ndx]);
423 if (gr->gr_mem[ndx + 1] != NULL)
424 strcat(line, ",");
425 }
426
427 return (line);
428 }
429
430 /*
431 * Duplicate a struct group.
432 */
433 struct group *
434 gr_dup(const struct group *gr)
435 {
436 char *dst;
437 size_t len;
438 struct group_storage *gs;
439 int ndx;
440 int num_mem;
441
442 /* Calculate size of the group. */
443 len = sizeof(*gs);
444 if (gr->gr_name != NULL)
445 len += strlen(gr->gr_name) + 1;
446 if (gr->gr_passwd != NULL)
447 len += strlen(gr->gr_passwd) + 1;
448 if (gr->gr_mem != NULL) {
449 for (num_mem = 0; gr->gr_mem[num_mem] != NULL; num_mem++)
450 len += strlen(gr->gr_mem[num_mem]) + 1;
451 len += (num_mem + 1) * sizeof(*gr->gr_mem);
452 } else
453 num_mem = -1;
454
455 /* Create new group and copy old group into it. */
456 if ((gs = calloc(1, len)) == NULL)
457 return (NULL);
458 dst = (char *)&gs->members[num_mem + 1];
459 if (gr->gr_name != NULL) {
460 gs->gr.gr_name = dst;
461 dst = stpcpy(gs->gr.gr_name, gr->gr_name) + 1;
462 }
463 if (gr->gr_passwd != NULL) {
464 gs->gr.gr_passwd = dst;
465 dst = stpcpy(gs->gr.gr_passwd, gr->gr_passwd) + 1;
466 }
467 gs->gr.gr_gid = gr->gr_gid;
468 if (gr->gr_mem != NULL) {
469 gs->gr.gr_mem = gs->members;
470 for (ndx = 0; ndx < num_mem; ndx++) {
471 gs->gr.gr_mem[ndx] = dst;
472 dst = stpcpy(gs->gr.gr_mem[ndx], gr->gr_mem[ndx]) + 1;
473 }
474 gs->gr.gr_mem[ndx] = NULL;
475 }
476
477 return (&gs->gr);
478 }
479
480 /*
481 * Scan a line and place it into a group structure.
482 */
483 static bool
484 __gr_scan(char *line, struct group *gr)
485 {
486 char *loc;
487 int ndx;
488
489 /* Assign non-member information to structure. */
490 gr->gr_name = line;
491 if ((loc = strchr(line, ':')) == NULL)
492 return (false);
493 *loc = '\0';
494 gr->gr_passwd = loc + 1;
495 if (*gr->gr_passwd == ':')
496 *gr->gr_passwd = '\0';
497 else {
498 if ((loc = strchr(loc + 1, ':')) == NULL)
499 return (false);
500 *loc = '\0';
501 }
502 if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1)
503 return (false);
504
505 /* Assign member information to structure. */
506 if ((loc = strchr(loc + 1, ':')) == NULL)
507 return (false);
508 line = loc + 1;
509 gr->gr_mem = NULL;
510 ndx = 0;
511 do {
512 gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) *
513 (ndx + 1));
514 if (gr->gr_mem == NULL)
515 return (false);
516
517 /* Skip locations without members (i.e., empty string). */
518 do {
519 gr->gr_mem[ndx] = strsep(&line, ",");
520 } while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0');
521 } while (gr->gr_mem[ndx++] != NULL);
522
523 return (true);
524 }
525
526 /*
527 * Create a struct group from a line.
528 */
529 struct group *
530 gr_scan(const char *line)
531 {
532 struct group gr;
533 char *line_copy;
534 struct group *new_gr;
535
536 if ((line_copy = strdup(line)) == NULL)
537 return (NULL);
538 if (!__gr_scan(line_copy, &gr)) {
539 free(line_copy);
540 return (NULL);
541 }
542 new_gr = gr_dup(&gr);
543 free(line_copy);
544 if (gr.gr_mem != NULL)
545 free(gr.gr_mem);
546
547 return (new_gr);
548 }