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