2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/errno.h>
46 static int lockfd = -1;
47 static char group_dir[PATH_MAX];
48 static char group_file[PATH_MAX];
49 static char tempname[PATH_MAX];
50 static int initialized;
51 static size_t grmemlen(const struct group *, const char *, int *);
52 static struct group *grcopy(const struct group *gr, char *mem, const char *, int ndx);
58 gr_init(const char *dir, const char *group)
62 strcpy(group_dir, _PATH_ETC);
64 if (strlen(dir) >= sizeof(group_dir)) {
68 strcpy(group_dir, dir);
73 strcpy(group_file, _PATH_GROUP);
74 } else if (snprintf(group_file, sizeof(group_file), "%s/group",
75 group_dir) > (int)sizeof(group_file)) {
80 if (strlen(group) >= sizeof(group_file)) {
84 strcpy(group_file, group);
97 if (*group_file == '\0')
103 lockfd = flopen(group_file, O_RDONLY|O_NONBLOCK|O_CLOEXEC, 0);
105 if (errno == EWOULDBLOCK) {
106 errx(1, "the group file is busy");
108 err(1, "could not lock the group file");
111 if (fstat(lockfd, &st) == -1)
112 err(1, "fstat() failed");
113 if (st.st_nlink != 0)
122 * Create and open a presmuably safe temp file for editing group data
132 if (*group_file == '\0')
134 if ((p = strrchr(group_file, '/')))
138 if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX",
139 (int)(p - group_file), group_file) >= (int)sizeof(tempname)) {
140 errno = ENAMETOOLONG;
143 if ((tfd = mkostemp(tempname, 0)) == -1)
146 while ((nr = read(mfd, buf, sizeof(buf))) > 0)
147 if (write(tfd, buf, (size_t)nr) != nr)
160 * Copy the group file from one descriptor to another, replacing, deleting
161 * or adding a single record on the way.
164 gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr)
166 char *buf, *end, *line, *p, *q, *r, *tmp;
168 const struct group *sgr;
173 if (old_gr == NULL && gr == NULL)
177 /* deleting a group */
181 if ((line = gr_make(gr)) == NULL)
189 /* initialize the buffer */
190 if ((buf = malloc(size = 1024)) == NULL)
197 /* find the end of the current line */
198 for (p = q; q < end && *q != '\0'; ++q)
202 /* if we don't have a complete line, fill up the buffer */
206 while ((size_t)(q - p) >= size) {
207 if ((tmp = reallocarray(buf, 2, size)) == NULL) {
208 warnx("group line too long");
213 end = tmp + (end - buf);
218 q = memmove(buf, p, end -p);
223 readlen = read(ffd, end, size - (end - buf));
227 len = (size_t)readlen;
228 if (len == 0 && p == buf)
234 if (len > 0 && buf[len -1] != '\n')
235 ++len, *end++ = '\n';
240 /* is it a blank line or a comment? */
241 for (r = p; r < q && isspace(*r); ++r)
243 if (r == q || *r == '#') {
245 if (write(tfd, p, q -p + 1) != q - p + 1)
251 /* is it the one we're looking for? */
258 /* fgr is either a struct group for the current line,
259 * or NULL if the line is malformed.
263 if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) {
267 if (write(tfd, p, q - p + 1) != q - p + 1)
272 if (old_gr && !gr_equal(fgr, old_gr)) {
273 warnx("entry inconsistent");
275 errno = EINVAL; /* hack */
280 /* it is, replace or remove it */
283 if (write(tfd, line, len) != (int) len)
286 /* when removed, avoid the \n */
289 /* we're done, just copy the rest over */
291 if (write(tfd, q, end - q) != end - q)
294 readlen = read(ffd, buf, size);
298 len = (size_t)readlen;
306 /* if we got here, we didn't find the old entry */
312 if ((size_t)write(tfd, line, len) != len ||
313 write(tfd, "\n", 1) != 1)
326 * Regenerate the group file
333 if (chmod(tempname, 0644) != 0)
336 if (rename(tempname, group_file) != 0)
340 * Make sure new group file is safe on disk. To improve performance we
341 * will call fsync() to the directory where file lies
343 if ((fd = open(group_dir, O_RDONLY|O_DIRECTORY)) == -1)
346 if (fsync(fd) != 0) {
356 * Clean up. Preserves errno for the caller's convenience.
367 if (*tempname != '\0') {
377 * Compares two struct group's.
380 gr_equal(const struct group *gr1, const struct group *gr2)
383 /* Check that the non-member information is the same. */
384 if (gr1->gr_name == NULL || gr2->gr_name == NULL) {
385 if (gr1->gr_name != gr2->gr_name)
387 } else if (strcmp(gr1->gr_name, gr2->gr_name) != 0)
389 if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) {
390 if (gr1->gr_passwd != gr2->gr_passwd)
392 } else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0)
394 if (gr1->gr_gid != gr2->gr_gid)
398 * Check all members in both groups.
399 * getgrnam can return gr_mem with a pointer to NULL.
400 * gr_dup and gr_add strip out this superfluous NULL, setting
401 * gr_mem to NULL for no members.
403 if (gr1->gr_mem != NULL && gr2->gr_mem != NULL) {
407 gr1->gr_mem[i] != NULL && gr2->gr_mem[i] != NULL; i++) {
408 if (strcmp(gr1->gr_mem[i], gr2->gr_mem[i]) != 0)
411 if (gr1->gr_mem[i] != NULL || gr2->gr_mem[i] != NULL)
413 } else if (gr1->gr_mem != NULL && gr1->gr_mem[0] != NULL) {
415 } else if (gr2->gr_mem != NULL && gr2->gr_mem[0] != NULL) {
423 * Make a group line out of a struct group.
426 gr_make(const struct group *gr)
428 const char *group_line_format = "%s:%s:%ju:";
435 /* Calculate the length of the group line. */
436 line_size = snprintf(NULL, 0, group_line_format, gr->gr_name,
437 gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1;
438 if (gr->gr_mem != NULL) {
439 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++)
440 line_size += strlen(gr->gr_mem[ndx]) + 1;
445 /* Create the group line and fill it. */
446 if ((line = p = malloc(line_size)) == NULL)
448 p += sprintf(p, group_line_format, gr->gr_name, gr->gr_passwd,
449 (uintmax_t)gr->gr_gid);
450 if (gr->gr_mem != NULL) {
452 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) {
454 p = stpcpy(p, gr->gr_mem[ndx]);
463 * Duplicate a struct group.
466 gr_dup(const struct group *gr)
468 return (gr_add(gr, NULL));
471 * Add a new member name to a struct group.
474 gr_add(const struct group *gr, const char *newmember)
481 len = grmemlen(gr, newmember, &num_mem);
482 /* Create new group and copy old group into it. */
483 if ((mem = malloc(len)) == NULL)
485 return (grcopy(gr, mem, newmember, num_mem));
488 /* It is safer to walk the pointers given at gr_mem since there is no
489 * guarantee the gr_mem + strings are contiguous in the given struct group
490 * but compactify the new group into the following form.
492 * The new struct is laid out like this in memory. The example given is
493 * for a group with two members only.
499 * (gr_mem * newgrp + sizeof(struct group) + sizeof(**)) points to gr_mem area
511 * Copy the contents of a group plus given name to a preallocated group struct
513 static struct group *
514 grcopy(const struct group *gr, char *dst, const char *name, int ndx)
519 newgr = (struct group *)(void *)dst; /* avoid alignment warning */
520 dst += sizeof(*newgr);
522 newgr->gr_mem = (char **)(void *)(dst); /* avoid alignment warning */
523 dst += (ndx + 1) * sizeof(*newgr->gr_mem);
525 newgr->gr_mem = NULL;
526 if (gr->gr_name != NULL) {
527 newgr->gr_name = dst;
528 dst = stpcpy(dst, gr->gr_name) + 1;
530 newgr->gr_name = NULL;
531 if (gr->gr_passwd != NULL) {
532 newgr->gr_passwd = dst;
533 dst = stpcpy(dst, gr->gr_passwd) + 1;
535 newgr->gr_passwd = NULL;
536 newgr->gr_gid = gr->gr_gid;
538 /* Original group struct might have a NULL gr_mem */
539 if (gr->gr_mem != NULL) {
540 for (; gr->gr_mem[i] != NULL; i++) {
541 newgr->gr_mem[i] = dst;
542 dst = stpcpy(dst, gr->gr_mem[i]) + 1;
545 /* If name is not NULL, newgr->gr_mem is known to be not NULL */
547 newgr->gr_mem[i++] = dst;
548 dst = stpcpy(dst, name) + 1;
550 /* if newgr->gr_mem is not NULL add NULL marker */
551 if (newgr->gr_mem != NULL)
552 newgr->gr_mem[i] = NULL;
558 * Calculate length of a struct group + given name
561 grmemlen(const struct group *gr, const char *name, int *num_mem)
568 /* Calculate size of the group. */
570 if (gr->gr_name != NULL)
571 len += strlen(gr->gr_name) + 1;
572 if (gr->gr_passwd != NULL)
573 len += strlen(gr->gr_passwd) + 1;
575 if (gr->gr_mem != NULL) {
576 for (; gr->gr_mem[i] != NULL; i++) {
577 len += strlen(gr->gr_mem[i]) + 1;
578 len += sizeof(*gr->gr_mem);
583 len += strlen(name) + 1;
584 len += sizeof(*gr->gr_mem);
586 /* Allow for NULL pointer */
588 len += sizeof(*gr->gr_mem);
594 * Scan a line and place it into a group structure.
597 __gr_scan(char *line, struct group *gr)
602 /* Assign non-member information to structure. */
604 if ((loc = strchr(line, ':')) == NULL)
607 gr->gr_passwd = loc + 1;
608 if (*gr->gr_passwd == ':')
609 *gr->gr_passwd = '\0';
611 if ((loc = strchr(loc + 1, ':')) == NULL)
615 if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1)
618 /* Assign member information to structure. */
619 if ((loc = strchr(loc + 1, ':')) == NULL)
625 gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) *
627 if (gr->gr_mem == NULL)
630 /* Skip locations without members (i.e., empty string). */
632 gr->gr_mem[ndx] = strsep(&line, ",");
633 } while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0');
634 } while (gr->gr_mem[ndx++] != NULL);
640 * Create a struct group from a line.
643 gr_scan(const char *line)
647 struct group *new_gr;
649 if ((line_copy = strdup(line)) == NULL)
651 if (!__gr_scan(line_copy, &gr)) {
655 new_gr = gr_dup(&gr);
657 if (gr.gr_mem != NULL)