2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/errno.h>
49 static int lockfd = -1;
50 static char group_dir[PATH_MAX];
51 static char group_file[PATH_MAX];
52 static char tempname[PATH_MAX];
53 static int initialized;
54 static size_t grmemlen(const struct group *, const char *, int *);
55 static struct group *grcopy(const struct group *gr, char *mem, const char *, int ndx);
61 gr_init(const char *dir, const char *group)
65 strcpy(group_dir, _PATH_ETC);
67 if (strlen(dir) >= sizeof(group_dir)) {
71 strcpy(group_dir, dir);
76 strcpy(group_file, _PATH_GROUP);
77 } else if (snprintf(group_file, sizeof(group_file), "%s/group",
78 group_dir) > (int)sizeof(group_file)) {
83 if (strlen(group) >= sizeof(group_file)) {
87 strcpy(group_file, group);
100 if (*group_file == '\0')
106 lockfd = flopen(group_file, O_RDONLY|O_NONBLOCK|O_CLOEXEC, 0);
108 if (errno == EWOULDBLOCK) {
109 errx(1, "the group file is busy");
111 err(1, "could not lock the group file: ");
114 if (fstat(lockfd, &st) == -1)
115 err(1, "fstat() failed: ");
116 if (st.st_nlink != 0)
125 * Create and open a presmuably safe temp file for editing group data
135 if (*group_file == '\0')
137 if ((p = strrchr(group_file, '/')))
141 if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX",
142 (int)(p - group_file), group_file) >= (int)sizeof(tempname)) {
143 errno = ENAMETOOLONG;
146 if ((tfd = mkostemp(tempname, 0)) == -1)
149 while ((nr = read(mfd, buf, sizeof(buf))) > 0)
150 if (write(tfd, buf, (size_t)nr) != nr)
163 * Copy the group file from one descriptor to another, replacing, deleting
164 * or adding a single record on the way.
167 gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr)
169 char *buf, *end, *line, *p, *q, *r, *tmp;
171 const struct group *sgr;
176 if (old_gr == NULL && gr == NULL)
180 /* deleting a group */
184 if ((line = gr_make(gr)) == NULL)
192 /* initialize the buffer */
193 if ((buf = malloc(size = 1024)) == NULL)
200 /* find the end of the current line */
201 for (p = q; q < end && *q != '\0'; ++q)
205 /* if we don't have a complete line, fill up the buffer */
209 while ((size_t)(q - p) >= size) {
210 if ((tmp = reallocarray(buf, 2, size)) == NULL) {
211 warnx("group line too long");
216 end = tmp + (end - buf);
221 q = memmove(buf, p, end -p);
226 readlen = read(ffd, end, size - (end - buf));
230 len = (size_t)readlen;
231 if (len == 0 && p == buf)
237 if (len > 0 && buf[len -1] != '\n')
238 ++len, *end++ = '\n';
243 /* is it a blank line or a comment? */
244 for (r = p; r < q && isspace(*r); ++r)
246 if (r == q || *r == '#') {
248 if (write(tfd, p, q -p + 1) != q - p + 1)
254 /* is it the one we're looking for? */
261 /* fgr is either a struct group for the current line,
262 * or NULL if the line is malformed.
266 if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) {
270 if (write(tfd, p, q - p + 1) != q - p + 1)
275 if (old_gr && !gr_equal(fgr, old_gr)) {
276 warnx("entry inconsistent");
278 errno = EINVAL; /* hack */
283 /* it is, replace or remove it */
286 if (write(tfd, line, len) != (int) len)
289 /* when removed, avoid the \n */
292 /* we're done, just copy the rest over */
294 if (write(tfd, q, end - q) != end - q)
297 readlen = read(ffd, buf, size);
301 len = (size_t)readlen;
309 /* if we got here, we didn't find the old entry */
315 if ((size_t)write(tfd, line, len) != len ||
316 write(tfd, "\n", 1) != 1)
329 * Regenerate the group file
336 if (chmod(tempname, 0644) != 0)
339 if (rename(tempname, group_file) != 0)
343 * Make sure new group file is safe on disk. To improve performance we
344 * will call fsync() to the directory where file lies
346 if ((fd = open(group_dir, O_RDONLY|O_DIRECTORY)) == -1)
349 if (fsync(fd) != 0) {
359 * Clean up. Preserves errno for the caller's convenience.
370 if (*tempname != '\0') {
380 * Compares two struct group's.
383 gr_equal(const struct group *gr1, const struct group *gr2)
386 /* Check that the non-member information is the same. */
387 if (gr1->gr_name == NULL || gr2->gr_name == NULL) {
388 if (gr1->gr_name != gr2->gr_name)
390 } else if (strcmp(gr1->gr_name, gr2->gr_name) != 0)
392 if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) {
393 if (gr1->gr_passwd != gr2->gr_passwd)
395 } else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0)
397 if (gr1->gr_gid != gr2->gr_gid)
401 * Check all members in both groups.
402 * getgrnam can return gr_mem with a pointer to NULL.
403 * gr_dup and gr_add strip out this superfluous NULL, setting
404 * gr_mem to NULL for no members.
406 if (gr1->gr_mem != NULL && gr2->gr_mem != NULL) {
410 gr1->gr_mem[i] != NULL && gr2->gr_mem[i] != NULL; i++) {
411 if (strcmp(gr1->gr_mem[i], gr2->gr_mem[i]) != 0)
414 if (gr1->gr_mem[i] != NULL || gr2->gr_mem[i] != NULL)
416 } else if (gr1->gr_mem != NULL && gr1->gr_mem[0] != NULL) {
418 } else if (gr2->gr_mem != NULL && gr2->gr_mem[0] != NULL) {
426 * Make a group line out of a struct group.
429 gr_make(const struct group *gr)
431 const char *group_line_format = "%s:%s:%ju:";
438 /* Calculate the length of the group line. */
439 line_size = snprintf(NULL, 0, group_line_format, gr->gr_name,
440 gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1;
441 if (gr->gr_mem != NULL) {
442 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++)
443 line_size += strlen(gr->gr_mem[ndx]) + 1;
448 /* Create the group line and fill it. */
449 if ((line = p = malloc(line_size)) == NULL)
451 p += sprintf(p, group_line_format, gr->gr_name, gr->gr_passwd,
452 (uintmax_t)gr->gr_gid);
453 if (gr->gr_mem != NULL) {
455 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) {
457 p = stpcpy(p, gr->gr_mem[ndx]);
466 * Duplicate a struct group.
469 gr_dup(const struct group *gr)
471 return (gr_add(gr, NULL));
474 * Add a new member name to a struct group.
477 gr_add(const struct group *gr, const char *newmember)
484 len = grmemlen(gr, newmember, &num_mem);
485 /* Create new group and copy old group into it. */
486 if ((mem = malloc(len)) == NULL)
488 return (grcopy(gr, mem, newmember, num_mem));
491 /* It is safer to walk the pointers given at gr_mem since there is no
492 * guarantee the gr_mem + strings are contiguous in the given struct group
493 * but compactify the new group into the following form.
495 * The new struct is laid out like this in memory. The example given is
496 * for a group with two members only.
502 * (gr_mem * newgrp + sizeof(struct group) + sizeof(**)) points to gr_mem area
514 * Copy the contents of a group plus given name to a preallocated group struct
516 static struct group *
517 grcopy(const struct group *gr, char *dst, const char *name, int ndx)
522 newgr = (struct group *)(void *)dst; /* avoid alignment warning */
523 dst += sizeof(*newgr);
525 newgr->gr_mem = (char **)(void *)(dst); /* avoid alignment warning */
526 dst += (ndx + 1) * sizeof(*newgr->gr_mem);
528 newgr->gr_mem = NULL;
529 if (gr->gr_name != NULL) {
530 newgr->gr_name = dst;
531 dst = stpcpy(dst, gr->gr_name) + 1;
533 newgr->gr_name = NULL;
534 if (gr->gr_passwd != NULL) {
535 newgr->gr_passwd = dst;
536 dst = stpcpy(dst, gr->gr_passwd) + 1;
538 newgr->gr_passwd = NULL;
539 newgr->gr_gid = gr->gr_gid;
541 /* Original group struct might have a NULL gr_mem */
542 if (gr->gr_mem != NULL) {
543 for (; gr->gr_mem[i] != NULL; i++) {
544 newgr->gr_mem[i] = dst;
545 dst = stpcpy(dst, gr->gr_mem[i]) + 1;
548 /* If name is not NULL, newgr->gr_mem is known to be not NULL */
550 newgr->gr_mem[i++] = dst;
551 dst = stpcpy(dst, name) + 1;
553 /* if newgr->gr_mem is not NULL add NULL marker */
554 if (newgr->gr_mem != NULL)
555 newgr->gr_mem[i] = NULL;
561 * Calculate length of a struct group + given name
564 grmemlen(const struct group *gr, const char *name, int *num_mem)
571 /* Calculate size of the group. */
573 if (gr->gr_name != NULL)
574 len += strlen(gr->gr_name) + 1;
575 if (gr->gr_passwd != NULL)
576 len += strlen(gr->gr_passwd) + 1;
578 if (gr->gr_mem != NULL) {
579 for (; gr->gr_mem[i] != NULL; i++) {
580 len += strlen(gr->gr_mem[i]) + 1;
581 len += sizeof(*gr->gr_mem);
586 len += strlen(name) + 1;
587 len += sizeof(*gr->gr_mem);
589 /* Allow for NULL pointer */
591 len += sizeof(*gr->gr_mem);
597 * Scan a line and place it into a group structure.
600 __gr_scan(char *line, struct group *gr)
605 /* Assign non-member information to structure. */
607 if ((loc = strchr(line, ':')) == NULL)
610 gr->gr_passwd = loc + 1;
611 if (*gr->gr_passwd == ':')
612 *gr->gr_passwd = '\0';
614 if ((loc = strchr(loc + 1, ':')) == NULL)
618 if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1)
621 /* Assign member information to structure. */
622 if ((loc = strchr(loc + 1, ':')) == NULL)
628 gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) *
630 if (gr->gr_mem == NULL)
633 /* Skip locations without members (i.e., empty string). */
635 gr->gr_mem[ndx] = strsep(&line, ",");
636 } while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0');
637 } while (gr->gr_mem[ndx++] != NULL);
643 * Create a struct group from a line.
646 gr_scan(const char *line)
650 struct group *new_gr;
652 if ((line_copy = strdup(line)) == NULL)
654 if (!__gr_scan(line_copy, &gr)) {
658 new_gr = gr_dup(&gr);
660 if (gr.gr_mem != NULL)