2 * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/errno.h>
47 struct group_storage {
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;
58 static const char group_line_format[] = "%s:%s:%ju:";
64 gr_init(const char *dir, const char *group)
68 strcpy(group_dir, _PATH_ETC);
70 if (strlen(dir) >= sizeof(group_dir)) {
74 strcpy(group_dir, dir);
79 strcpy(group_file, _PATH_GROUP);
80 } else if (snprintf(group_file, sizeof(group_file), "%s/group",
81 group_dir) > (int)sizeof(group_file)) {
86 if (strlen(group) >= sizeof(group_file)) {
90 strcpy(group_file, group);
103 if (*group_file == '\0')
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");
116 err(1, "could not lock the group file: ");
119 if (fstat(lockfd, &st) == -1)
120 err(1, "fstat() failed: ");
121 if (st.st_nlink != 0)
130 * Create and open a presmuably safe temp file for editing group data
140 if (*group_file == '\0')
142 if ((p = strrchr(group_file, '/')))
146 if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX",
147 (int)(p - group_file), group_file) >= (int)sizeof(tempname)) {
148 errno = ENAMETOOLONG;
151 if ((tfd = mkstemp(tempname)) == -1)
154 while ((nr = read(mfd, buf, sizeof(buf))) > 0)
155 if (write(tfd, buf, (size_t)nr) != nr)
168 * Copy the group file from one descriptor to another, replacing, deleting
169 * or adding a single record on the way.
172 gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr)
174 char buf[8192], *end, *line, *p, *q, *r, t;
176 const struct group *sgr;
186 } else if ((line = gr_make(gr)) == NULL)
193 /* find the end of the current line */
194 for (p = q; q < end && *q != '\0'; ++q)
198 /* if we don't have a complete line, fill up the buffer */
202 if ((size_t)(q - p) >= sizeof(buf)) {
203 warnx("group line too long");
204 errno = EINVAL; /* hack */
208 q = memmove(buf, p, end -p);
213 readlen = read(ffd, end, sizeof(buf) - (end -buf));
217 len = (size_t)readlen;
218 if (len == 0 && p == buf)
222 if (len < (ssize_t)sizeof(buf)) {
224 if (len > 0 && buf[len -1] != '\n')
225 ++len, *end++ = '\n';
230 /* is it a blank line or a comment? */
231 for (r = p; r < q && isspace(*r); ++r)
233 if (r == q || *r == '#') {
235 if (write(tfd, p, q -p + 1) != q - p + 1)
241 /* is it the one we're looking for? */
248 /* fgr is either a struct group for the current line,
249 * or NULL if the line is malformed.
253 if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) {
257 if (write(tfd, p, q - p + 1) != q - p + 1)
262 if (old_gr && !gr_equal(fgr, old_gr)) {
263 warnx("entry inconsistent");
265 errno = EINVAL; /* hack */
270 /* it is, replace or remove it */
273 if (write(tfd, line, len) != (int) len)
276 /* when removed, avoid the \n */
279 /* we're done, just copy the rest over */
281 if (write(tfd, q, end - q) != end - q)
284 readlen = read(ffd, buf, sizeof(buf));
288 len = (size_t)readlen;
296 /* if we got here, we didn't find the old entry */
302 if ((size_t)write(tfd, line, len) != len ||
303 write(tfd, "\n", 1) != 1)
316 * Regenerate the group file
321 return (rename(tempname, group_file));
325 * Clean up. Preserver errno for the caller's convenience.
336 if (*tempname != '\0') {
346 * Compares two struct group's.
349 gr_equal(const struct group *gr1, const struct group *gr2)
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)
359 } else if (strcmp(gr1->gr_name, gr2->gr_name) != 0)
361 if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) {
362 if (gr1->gr_passwd != gr2->gr_passwd)
364 } else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0)
366 if (gr1->gr_gid != gr2->gr_gid)
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)
374 for (found = false, gr1_ndx = 0; gr1->gr_mem[gr1_ndx] != NULL;
376 for (gr2_ndx = 0; gr2->gr_mem[gr2_ndx] != NULL;
378 if (strcmp(gr1->gr_mem[gr1_ndx],
379 gr2->gr_mem[gr2_ndx]) == 0) {
387 /* Check that group2 does not have more members than group1. */
388 if (gr2->gr_mem[gr1_ndx] != NULL)
396 * Make a group line out of a struct group.
399 gr_make(const struct group *gr)
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;
415 /* Create the group line and fill it. */
416 if ((line = malloc(line_size)) == 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)
431 * Duplicate a struct group.
434 gr_dup(const struct group *gr)
438 struct group_storage *gs;
442 /* Calculate size of the group. */
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);
455 /* Create new group and copy old group into it. */
456 if ((gs = calloc(1, len)) == 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;
463 if (gr->gr_passwd != NULL) {
464 gs->gr.gr_passwd = dst;
465 dst = stpcpy(gs->gr.gr_passwd, gr->gr_passwd) + 1;
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;
474 gs->gr.gr_mem[ndx] = NULL;
481 * Scan a line and place it into a group structure.
484 __gr_scan(char *line, struct group *gr)
489 /* Assign non-member information to structure. */
491 if ((loc = strchr(line, ':')) == NULL)
494 gr->gr_passwd = loc + 1;
495 if (*gr->gr_passwd == ':')
496 *gr->gr_passwd = '\0';
498 if ((loc = strchr(loc + 1, ':')) == NULL)
502 if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1)
505 /* Assign member information to structure. */
506 if ((loc = strchr(loc + 1, ':')) == NULL)
512 gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) *
514 if (gr->gr_mem == NULL)
517 /* Skip locations without members (i.e., empty string). */
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);
527 * Create a struct group from a line.
530 gr_scan(const char *line)
534 struct group *new_gr;
536 if ((line_copy = strdup(line)) == NULL)
538 if (!__gr_scan(line_copy, &gr)) {
542 new_gr = gr_dup(&gr);
544 if (gr.gr_mem != NULL)