xref: /freebsd/lib/libutil/gr_util.c (revision cbaba16b23124331498f0a6d48a4d71f4f20b508)
10b5e8899SSean Farley /*-
20b5e8899SSean Farley  * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org>
30b5e8899SSean Farley  * All rights reserved.
40b5e8899SSean Farley  *
50b5e8899SSean Farley  * Redistribution and use in source and binary forms, with or without
60b5e8899SSean Farley  * modification, are permitted provided that the following conditions
70b5e8899SSean Farley  * are met:
80b5e8899SSean Farley  * 1. Redistributions of source code must retain the above copyright
90b5e8899SSean Farley  *    notice, this list of conditions and the following disclaimer,
100b5e8899SSean Farley  *    without modification, immediately at the beginning of the file.
110b5e8899SSean Farley  * 2. Redistributions in binary form must reproduce the above copyright
120b5e8899SSean Farley  *    notice, this list of conditions and the following disclaimer in the
130b5e8899SSean Farley  *    documentation and/or other materials provided with the distribution.
140b5e8899SSean Farley  *
150b5e8899SSean Farley  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
160b5e8899SSean Farley  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
170b5e8899SSean Farley  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
180b5e8899SSean Farley  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
190b5e8899SSean Farley  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
200b5e8899SSean Farley  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
210b5e8899SSean Farley  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
220b5e8899SSean Farley  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
230b5e8899SSean Farley  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
240b5e8899SSean Farley  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
250b5e8899SSean Farley  */
260b5e8899SSean Farley 
270b5e8899SSean Farley #include <sys/cdefs.h>
280b5e8899SSean Farley __FBSDID("$FreeBSD$");
290b5e8899SSean Farley 
300b5e8899SSean Farley #include <sys/param.h>
311926f2f6SBaptiste Daroussin #include <sys/errno.h>
321926f2f6SBaptiste Daroussin #include <sys/stat.h>
33a0671723SSean Farley 
341926f2f6SBaptiste Daroussin #include <ctype.h>
351926f2f6SBaptiste Daroussin #include <err.h>
361926f2f6SBaptiste Daroussin #include <fcntl.h>
370b5e8899SSean Farley #include <grp.h>
380b5e8899SSean Farley #include <inttypes.h>
39a0671723SSean Farley #include <libutil.h>
401926f2f6SBaptiste Daroussin #include <paths.h>
410b5e8899SSean Farley #include <stdbool.h>
420b5e8899SSean Farley #include <stdio.h>
430b5e8899SSean Farley #include <stdlib.h>
440b5e8899SSean Farley #include <string.h>
451926f2f6SBaptiste Daroussin #include <unistd.h>
460b5e8899SSean Farley 
471926f2f6SBaptiste Daroussin static int lockfd = -1;
481926f2f6SBaptiste Daroussin static char group_dir[PATH_MAX];
491926f2f6SBaptiste Daroussin static char group_file[PATH_MAX];
501926f2f6SBaptiste Daroussin static char tempname[PATH_MAX];
511926f2f6SBaptiste Daroussin static int initialized;
5286e2f99dSDiane Bruce static size_t grmemlen(const struct group *, const char *, int *);
535e879837SDiane Bruce static struct group *grcopy(const struct group *gr, char *mem, const char *, int ndx);
541926f2f6SBaptiste Daroussin 
550b5e8899SSean Farley /*
561926f2f6SBaptiste Daroussin  * Initialize statics
571926f2f6SBaptiste Daroussin  */
581926f2f6SBaptiste Daroussin int
591926f2f6SBaptiste Daroussin gr_init(const char *dir, const char *group)
601926f2f6SBaptiste Daroussin {
6129e57550SBaptiste Daroussin 
621926f2f6SBaptiste Daroussin 	if (dir == NULL) {
631926f2f6SBaptiste Daroussin 		strcpy(group_dir, _PATH_ETC);
641926f2f6SBaptiste Daroussin 	} else {
651926f2f6SBaptiste Daroussin 		if (strlen(dir) >= sizeof(group_dir)) {
661926f2f6SBaptiste Daroussin 			errno = ENAMETOOLONG;
671926f2f6SBaptiste Daroussin 			return (-1);
681926f2f6SBaptiste Daroussin 		}
691926f2f6SBaptiste Daroussin 		strcpy(group_dir, dir);
701926f2f6SBaptiste Daroussin 	}
711926f2f6SBaptiste Daroussin 
721926f2f6SBaptiste Daroussin 	if (group == NULL) {
731926f2f6SBaptiste Daroussin 		if (dir == NULL) {
741926f2f6SBaptiste Daroussin 			strcpy(group_file, _PATH_GROUP);
751926f2f6SBaptiste Daroussin 		} else if (snprintf(group_file, sizeof(group_file), "%s/group",
761926f2f6SBaptiste Daroussin 			group_dir) > (int)sizeof(group_file)) {
771926f2f6SBaptiste Daroussin 			errno = ENAMETOOLONG;
781926f2f6SBaptiste Daroussin 			return (-1);
791926f2f6SBaptiste Daroussin 		}
801926f2f6SBaptiste Daroussin 	} else {
811926f2f6SBaptiste Daroussin 		if (strlen(group) >= sizeof(group_file)) {
821926f2f6SBaptiste Daroussin 			errno = ENAMETOOLONG;
831926f2f6SBaptiste Daroussin 			return (-1);
841926f2f6SBaptiste Daroussin 		}
851926f2f6SBaptiste Daroussin 		strcpy(group_file, group);
861926f2f6SBaptiste Daroussin 	}
8729e57550SBaptiste Daroussin 
881926f2f6SBaptiste Daroussin 	initialized = 1;
891926f2f6SBaptiste Daroussin 	return (0);
901926f2f6SBaptiste Daroussin }
911926f2f6SBaptiste Daroussin 
921926f2f6SBaptiste Daroussin /*
931926f2f6SBaptiste Daroussin  * Lock the group file
941926f2f6SBaptiste Daroussin  */
951926f2f6SBaptiste Daroussin int
961926f2f6SBaptiste Daroussin gr_lock(void)
971926f2f6SBaptiste Daroussin {
981926f2f6SBaptiste Daroussin 	if (*group_file == '\0')
991926f2f6SBaptiste Daroussin 		return (-1);
1001926f2f6SBaptiste Daroussin 
1011926f2f6SBaptiste Daroussin 	for (;;) {
1021926f2f6SBaptiste Daroussin 		struct stat st;
1031926f2f6SBaptiste Daroussin 
104ede89d5dSBaptiste Daroussin 		lockfd = flopen(group_file, O_RDONLY|O_NONBLOCK|O_CLOEXEC, 0);
10598e79fb1SBaptiste Daroussin 		if (lockfd == -1) {
1061926f2f6SBaptiste Daroussin 			if (errno == EWOULDBLOCK) {
1071926f2f6SBaptiste Daroussin 				errx(1, "the group file is busy");
1081926f2f6SBaptiste Daroussin 			} else {
1091926f2f6SBaptiste Daroussin 				err(1, "could not lock the group file: ");
1101926f2f6SBaptiste Daroussin 			}
1111926f2f6SBaptiste Daroussin 		}
1121926f2f6SBaptiste Daroussin 		if (fstat(lockfd, &st) == -1)
1131926f2f6SBaptiste Daroussin 			err(1, "fstat() failed: ");
1141926f2f6SBaptiste Daroussin 		if (st.st_nlink != 0)
1151926f2f6SBaptiste Daroussin 			break;
1161926f2f6SBaptiste Daroussin 		close(lockfd);
1171926f2f6SBaptiste Daroussin 		lockfd = -1;
1181926f2f6SBaptiste Daroussin 	}
1191926f2f6SBaptiste Daroussin 	return (lockfd);
1201926f2f6SBaptiste Daroussin }
1211926f2f6SBaptiste Daroussin 
1221926f2f6SBaptiste Daroussin /*
1231926f2f6SBaptiste Daroussin  * Create and open a presmuably safe temp file for editing group data
1241926f2f6SBaptiste Daroussin  */
1251926f2f6SBaptiste Daroussin int
1261926f2f6SBaptiste Daroussin gr_tmp(int mfd)
1271926f2f6SBaptiste Daroussin {
1281926f2f6SBaptiste Daroussin 	char buf[8192];
1291926f2f6SBaptiste Daroussin 	ssize_t nr;
1301926f2f6SBaptiste Daroussin 	const char *p;
1311926f2f6SBaptiste Daroussin 	int tfd;
1321926f2f6SBaptiste Daroussin 
1331926f2f6SBaptiste Daroussin 	if (*group_file == '\0')
1341926f2f6SBaptiste Daroussin 		return (-1);
1351926f2f6SBaptiste Daroussin 	if ((p = strrchr(group_file, '/')))
1361926f2f6SBaptiste Daroussin 		++p;
1371926f2f6SBaptiste Daroussin 	else
1381926f2f6SBaptiste Daroussin 		p = group_file;
1391926f2f6SBaptiste Daroussin 	if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX",
1401926f2f6SBaptiste Daroussin 		(int)(p - group_file), group_file) >= (int)sizeof(tempname)) {
1411926f2f6SBaptiste Daroussin 		errno = ENAMETOOLONG;
1421926f2f6SBaptiste Daroussin 		return (-1);
1431926f2f6SBaptiste Daroussin 	}
144*cbaba16bSAlan Somers 	if ((tfd = mkostemp(tempname, 0)) == -1)
1451926f2f6SBaptiste Daroussin 		return (-1);
1461926f2f6SBaptiste Daroussin 	if (mfd != -1) {
1471926f2f6SBaptiste Daroussin 		while ((nr = read(mfd, buf, sizeof(buf))) > 0)
1481926f2f6SBaptiste Daroussin 			if (write(tfd, buf, (size_t)nr) != nr)
1491926f2f6SBaptiste Daroussin 				break;
1501926f2f6SBaptiste Daroussin 		if (nr != 0) {
1511926f2f6SBaptiste Daroussin 			unlink(tempname);
1521926f2f6SBaptiste Daroussin 			*tempname = '\0';
1531926f2f6SBaptiste Daroussin 			close(tfd);
1541926f2f6SBaptiste Daroussin 			return (-1);
1551926f2f6SBaptiste Daroussin 		}
1561926f2f6SBaptiste Daroussin 	}
1571926f2f6SBaptiste Daroussin 	return (tfd);
1581926f2f6SBaptiste Daroussin }
1591926f2f6SBaptiste Daroussin 
1601926f2f6SBaptiste Daroussin /*
1611926f2f6SBaptiste Daroussin  * Copy the group file from one descriptor to another, replacing, deleting
1621926f2f6SBaptiste Daroussin  * or adding a single record on the way.
1631926f2f6SBaptiste Daroussin  */
1641926f2f6SBaptiste Daroussin int
1651926f2f6SBaptiste Daroussin gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr)
1661926f2f6SBaptiste Daroussin {
1671926f2f6SBaptiste Daroussin 	char buf[8192], *end, *line, *p, *q, *r, t;
1681926f2f6SBaptiste Daroussin 	struct group *fgr;
1691926f2f6SBaptiste Daroussin 	const struct group *sgr;
1701926f2f6SBaptiste Daroussin 	size_t len;
1711926f2f6SBaptiste Daroussin 	int eof, readlen;
1721926f2f6SBaptiste Daroussin 
1736e6c53f0SBaptiste Daroussin 	if (old_gr == NULL && gr == NULL)
1746e6c53f0SBaptiste Daroussin 		return(-1);
1756e6c53f0SBaptiste Daroussin 
1766e6c53f0SBaptiste Daroussin 	sgr = old_gr;
1776e6c53f0SBaptiste Daroussin 	/* deleting a group */
1781926f2f6SBaptiste Daroussin 	if (gr == NULL) {
1791926f2f6SBaptiste Daroussin 		line = NULL;
1806e6c53f0SBaptiste Daroussin 	} else {
1816e6c53f0SBaptiste Daroussin 		if ((line = gr_make(gr)) == NULL)
1821926f2f6SBaptiste Daroussin 			return (-1);
1836e6c53f0SBaptiste Daroussin 	}
1846e6c53f0SBaptiste Daroussin 
1856e6c53f0SBaptiste Daroussin 	/* adding a group */
1866e6c53f0SBaptiste Daroussin 	if (sgr == NULL)
1876e6c53f0SBaptiste Daroussin 		sgr = gr;
1881926f2f6SBaptiste Daroussin 
1891926f2f6SBaptiste Daroussin 	eof = 0;
1901926f2f6SBaptiste Daroussin 	len = 0;
1911926f2f6SBaptiste Daroussin 	p = q = end = buf;
1921926f2f6SBaptiste Daroussin 	for (;;) {
1931926f2f6SBaptiste Daroussin 		/* find the end of the current line */
1941926f2f6SBaptiste Daroussin 		for (p = q; q < end && *q != '\0'; ++q)
1951926f2f6SBaptiste Daroussin 			if (*q == '\n')
1961926f2f6SBaptiste Daroussin 				break;
1971926f2f6SBaptiste Daroussin 
1981926f2f6SBaptiste Daroussin 		/* if we don't have a complete line, fill up the buffer */
1991926f2f6SBaptiste Daroussin 		if (q >= end) {
2001926f2f6SBaptiste Daroussin 			if (eof)
2011926f2f6SBaptiste Daroussin 				break;
2021926f2f6SBaptiste Daroussin 			if ((size_t)(q - p) >= sizeof(buf)) {
2031926f2f6SBaptiste Daroussin 				warnx("group line too long");
2041926f2f6SBaptiste Daroussin 				errno = EINVAL; /* hack */
2051926f2f6SBaptiste Daroussin 				goto err;
2061926f2f6SBaptiste Daroussin 			}
2071926f2f6SBaptiste Daroussin 			if (p < end) {
2081926f2f6SBaptiste Daroussin 				q = memmove(buf, p, end -p);
2091926f2f6SBaptiste Daroussin 				end -= p - buf;
2101926f2f6SBaptiste Daroussin 			} else {
2111926f2f6SBaptiste Daroussin 				p = q = end = buf;
2121926f2f6SBaptiste Daroussin 			}
2131926f2f6SBaptiste Daroussin 			readlen = read(ffd, end, sizeof(buf) - (end -buf));
2141926f2f6SBaptiste Daroussin 			if (readlen == -1)
2151926f2f6SBaptiste Daroussin 				goto err;
2161926f2f6SBaptiste Daroussin 			else
2171926f2f6SBaptiste Daroussin 				len = (size_t)readlen;
2181926f2f6SBaptiste Daroussin 			if (len == 0 && p == buf)
2191926f2f6SBaptiste Daroussin 				break;
2201926f2f6SBaptiste Daroussin 			end += len;
2211926f2f6SBaptiste Daroussin 			len = end - buf;
2221926f2f6SBaptiste Daroussin 			if (len < (ssize_t)sizeof(buf)) {
2231926f2f6SBaptiste Daroussin 				eof = 1;
2241926f2f6SBaptiste Daroussin 				if (len > 0 && buf[len -1] != '\n')
2251926f2f6SBaptiste Daroussin 					++len, *end++ = '\n';
2261926f2f6SBaptiste Daroussin 			}
2271926f2f6SBaptiste Daroussin 			continue;
2281926f2f6SBaptiste Daroussin 		}
2291926f2f6SBaptiste Daroussin 
2301926f2f6SBaptiste Daroussin 		/* is it a blank line or a comment? */
2311926f2f6SBaptiste Daroussin 		for (r = p; r < q && isspace(*r); ++r)
2321926f2f6SBaptiste Daroussin 			/* nothing */;
2331926f2f6SBaptiste Daroussin 		if (r == q || *r == '#') {
2341926f2f6SBaptiste Daroussin 			/* yep */
2351926f2f6SBaptiste Daroussin 			if (write(tfd, p, q -p + 1) != q - p + 1)
2361926f2f6SBaptiste Daroussin 				goto err;
2371926f2f6SBaptiste Daroussin 			++q;
2381926f2f6SBaptiste Daroussin 			continue;
2391926f2f6SBaptiste Daroussin 		}
2401926f2f6SBaptiste Daroussin 
2411926f2f6SBaptiste Daroussin 		/* is it the one we're looking for? */
2421926f2f6SBaptiste Daroussin 
2431926f2f6SBaptiste Daroussin 		t = *q;
2441926f2f6SBaptiste Daroussin 		*q = '\0';
2451926f2f6SBaptiste Daroussin 
2461926f2f6SBaptiste Daroussin 		fgr = gr_scan(r);
2471926f2f6SBaptiste Daroussin 
2481926f2f6SBaptiste Daroussin 		/* fgr is either a struct group for the current line,
2491926f2f6SBaptiste Daroussin 		 * or NULL if the line is malformed.
2501926f2f6SBaptiste Daroussin 		 */
2511926f2f6SBaptiste Daroussin 
2521926f2f6SBaptiste Daroussin 		*q = t;
2531926f2f6SBaptiste Daroussin 		if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) {
2541926f2f6SBaptiste Daroussin 			/* nope */
2551926f2f6SBaptiste Daroussin 			if (fgr != NULL)
2561926f2f6SBaptiste Daroussin 				free(fgr);
2571926f2f6SBaptiste Daroussin 			if (write(tfd, p, q - p + 1) != q - p + 1)
2581926f2f6SBaptiste Daroussin 				goto err;
2591926f2f6SBaptiste Daroussin 			++q;
2601926f2f6SBaptiste Daroussin 			continue;
2611926f2f6SBaptiste Daroussin 		}
2621926f2f6SBaptiste Daroussin 		if (old_gr && !gr_equal(fgr, old_gr)) {
2631926f2f6SBaptiste Daroussin 			warnx("entry inconsistent");
2641926f2f6SBaptiste Daroussin 			free(fgr);
2651926f2f6SBaptiste Daroussin 			errno = EINVAL; /* hack */
2661926f2f6SBaptiste Daroussin 			goto err;
2671926f2f6SBaptiste Daroussin 		}
2681926f2f6SBaptiste Daroussin 		free(fgr);
2691926f2f6SBaptiste Daroussin 
2701926f2f6SBaptiste Daroussin 		/* it is, replace or remove it */
2711926f2f6SBaptiste Daroussin 		if (line != NULL) {
2721926f2f6SBaptiste Daroussin 			len = strlen(line);
2731926f2f6SBaptiste Daroussin 			if (write(tfd, line, len) != (int) len)
2741926f2f6SBaptiste Daroussin 				goto err;
2751926f2f6SBaptiste Daroussin 		} else {
2761926f2f6SBaptiste Daroussin 			/* when removed, avoid the \n */
2771926f2f6SBaptiste Daroussin 			q++;
2781926f2f6SBaptiste Daroussin 		}
2791926f2f6SBaptiste Daroussin 		/* we're done, just copy the rest over */
2801926f2f6SBaptiste Daroussin 		for (;;) {
2811926f2f6SBaptiste Daroussin 			if (write(tfd, q, end - q) != end - q)
2821926f2f6SBaptiste Daroussin 				goto err;
2831926f2f6SBaptiste Daroussin 			q = buf;
2841926f2f6SBaptiste Daroussin 			readlen = read(ffd, buf, sizeof(buf));
2851926f2f6SBaptiste Daroussin 			if (readlen == 0)
2861926f2f6SBaptiste Daroussin 				break;
2871926f2f6SBaptiste Daroussin 			else
2881926f2f6SBaptiste Daroussin 				len = (size_t)readlen;
2891926f2f6SBaptiste Daroussin 			if (readlen == -1)
2901926f2f6SBaptiste Daroussin 				goto err;
2911926f2f6SBaptiste Daroussin 			end = buf + len;
2921926f2f6SBaptiste Daroussin 		}
2931926f2f6SBaptiste Daroussin 		goto done;
2941926f2f6SBaptiste Daroussin 	}
2951926f2f6SBaptiste Daroussin 
2961926f2f6SBaptiste Daroussin 	/* if we got here, we didn't find the old entry */
2971926f2f6SBaptiste Daroussin 	if (line == NULL) {
2981926f2f6SBaptiste Daroussin 		errno = ENOENT;
2991926f2f6SBaptiste Daroussin 		goto err;
3001926f2f6SBaptiste Daroussin 	}
3011926f2f6SBaptiste Daroussin 	len = strlen(line);
3021926f2f6SBaptiste Daroussin 	if ((size_t)write(tfd, line, len) != len ||
3031926f2f6SBaptiste Daroussin 	   write(tfd, "\n", 1) != 1)
3041926f2f6SBaptiste Daroussin 		goto err;
3051926f2f6SBaptiste Daroussin  done:
3061926f2f6SBaptiste Daroussin 	if (line != NULL)
3071926f2f6SBaptiste Daroussin 		free(line);
3081926f2f6SBaptiste Daroussin 	return (0);
3091926f2f6SBaptiste Daroussin  err:
3101926f2f6SBaptiste Daroussin 	if (line != NULL)
3111926f2f6SBaptiste Daroussin 		free(line);
3121926f2f6SBaptiste Daroussin 	return (-1);
3131926f2f6SBaptiste Daroussin }
3141926f2f6SBaptiste Daroussin 
3151926f2f6SBaptiste Daroussin /*
3161926f2f6SBaptiste Daroussin  * Regenerate the group file
3171926f2f6SBaptiste Daroussin  */
3181926f2f6SBaptiste Daroussin int
3191926f2f6SBaptiste Daroussin gr_mkdb(void)
3201926f2f6SBaptiste Daroussin {
321d32a66b2SRenato Botelho 	int fd;
322d32a66b2SRenato Botelho 
32309259e6cSBaptiste Daroussin 	if (chmod(tempname, 0644) != 0)
32409259e6cSBaptiste Daroussin 		return (-1);
3252d2b6ad7SBaptiste Daroussin 
326d32a66b2SRenato Botelho 	if (rename(tempname, group_file) != 0)
327d32a66b2SRenato Botelho 		return (-1);
328d32a66b2SRenato Botelho 
329d32a66b2SRenato Botelho 	/*
330d32a66b2SRenato Botelho 	 * Make sure new group file is safe on disk. To improve performance we
331d32a66b2SRenato Botelho 	 * will call fsync() to the directory where file lies
332d32a66b2SRenato Botelho 	 */
333d32a66b2SRenato Botelho 	if ((fd = open(group_dir, O_RDONLY|O_DIRECTORY)) == -1)
334d32a66b2SRenato Botelho 		return (-1);
335d32a66b2SRenato Botelho 
336d32a66b2SRenato Botelho 	if (fsync(fd) != 0) {
337d32a66b2SRenato Botelho 		close(fd);
338d32a66b2SRenato Botelho 		return (-1);
339d32a66b2SRenato Botelho 	}
340d32a66b2SRenato Botelho 
341d32a66b2SRenato Botelho 	close(fd);
342d32a66b2SRenato Botelho 	return(0);
3431926f2f6SBaptiste Daroussin }
3441926f2f6SBaptiste Daroussin 
3451926f2f6SBaptiste Daroussin /*
34608ecf0ccSMateusz Guzik  * Clean up. Preserves errno for the caller's convenience.
3471926f2f6SBaptiste Daroussin  */
3481926f2f6SBaptiste Daroussin void
3491926f2f6SBaptiste Daroussin gr_fini(void)
3501926f2f6SBaptiste Daroussin {
3511926f2f6SBaptiste Daroussin 	int serrno;
3521926f2f6SBaptiste Daroussin 
3531926f2f6SBaptiste Daroussin 	if (!initialized)
3541926f2f6SBaptiste Daroussin 		return;
3551926f2f6SBaptiste Daroussin 	initialized = 0;
3561926f2f6SBaptiste Daroussin 	serrno = errno;
3571926f2f6SBaptiste Daroussin 	if (*tempname != '\0') {
3581926f2f6SBaptiste Daroussin 		unlink(tempname);
3591926f2f6SBaptiste Daroussin 		*tempname = '\0';
3601926f2f6SBaptiste Daroussin 	}
3611926f2f6SBaptiste Daroussin 	if (lockfd != -1)
3621926f2f6SBaptiste Daroussin 		close(lockfd);
3631926f2f6SBaptiste Daroussin 	errno = serrno;
3641926f2f6SBaptiste Daroussin }
3651926f2f6SBaptiste Daroussin 
3661926f2f6SBaptiste Daroussin /*
3670b5e8899SSean Farley  * Compares two struct group's.
3680b5e8899SSean Farley  */
3690b5e8899SSean Farley int
3700b5e8899SSean Farley gr_equal(const struct group *gr1, const struct group *gr2)
3710b5e8899SSean Farley {
3720b5e8899SSean Farley 
3730b5e8899SSean Farley 	/* Check that the non-member information is the same. */
374805da51aSSean Farley 	if (gr1->gr_name == NULL || gr2->gr_name == NULL) {
375805da51aSSean Farley 		if (gr1->gr_name != gr2->gr_name)
376805da51aSSean Farley 			return (false);
377805da51aSSean Farley 	} else if (strcmp(gr1->gr_name, gr2->gr_name) != 0)
378805da51aSSean Farley 		return (false);
379805da51aSSean Farley 	if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) {
380805da51aSSean Farley 		if (gr1->gr_passwd != gr2->gr_passwd)
381805da51aSSean Farley 			return (false);
382805da51aSSean Farley 	} else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0)
383805da51aSSean Farley 		return (false);
384805da51aSSean Farley 	if (gr1->gr_gid != gr2->gr_gid)
385805da51aSSean Farley 		return (false);
3860b5e8899SSean Farley 
387f8c88390SMark Johnston 	/*
388f8c88390SMark Johnston 	 * Check all members in both groups.
3895e879837SDiane Bruce 	 * getgrnam can return gr_mem with a pointer to NULL.
3905e879837SDiane Bruce 	 * gr_dup and gr_add strip out this superfluous NULL, setting
3915e879837SDiane Bruce 	 * gr_mem to NULL for no members.
3925e879837SDiane Bruce 	*/
3935e879837SDiane Bruce 	if (gr1->gr_mem != NULL && gr2->gr_mem != NULL) {
3945e879837SDiane Bruce 		int i;
3950b5e8899SSean Farley 
396f8c88390SMark Johnston 		for (i = 0;
397f8c88390SMark Johnston 		    gr1->gr_mem[i] != NULL && gr2->gr_mem[i] != NULL; i++) {
3985e879837SDiane Bruce 			if (strcmp(gr1->gr_mem[i], gr2->gr_mem[i]) != 0)
399805da51aSSean Farley 				return (false);
4000b5e8899SSean Farley 		}
401f8c88390SMark Johnston 		if (gr1->gr_mem[i] != NULL || gr2->gr_mem[i] != NULL)
4025e879837SDiane Bruce 			return (false);
403f8c88390SMark Johnston 	} else if (gr1->gr_mem != NULL && gr1->gr_mem[0] != NULL) {
404f8c88390SMark Johnston 		return (false);
405f8c88390SMark Johnston 	} else if (gr2->gr_mem != NULL && gr2->gr_mem[0] != NULL) {
406f8c88390SMark Johnston 		return (false);
407f8c88390SMark Johnston 	}
4080b5e8899SSean Farley 
409805da51aSSean Farley 	return (true);
4100b5e8899SSean Farley }
4110b5e8899SSean Farley 
4120b5e8899SSean Farley /*
4130b5e8899SSean Farley  * Make a group line out of a struct group.
4140b5e8899SSean Farley  */
4150b5e8899SSean Farley char *
4160b5e8899SSean Farley gr_make(const struct group *gr)
4170b5e8899SSean Farley {
418fe75b0f0SMateusz Guzik 	const char *group_line_format = "%s:%s:%ju:";
41949013fb4SMateusz Guzik 	const char *sep;
4200b5e8899SSean Farley 	char *line;
42149013fb4SMateusz Guzik 	char *p;
422805da51aSSean Farley 	size_t line_size;
423a0671723SSean Farley 	int ndx;
4240b5e8899SSean Farley 
4250b5e8899SSean Farley 	/* Calculate the length of the group line. */
426805da51aSSean Farley 	line_size = snprintf(NULL, 0, group_line_format, gr->gr_name,
4270b5e8899SSean Farley 	    gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1;
428805da51aSSean Farley 	if (gr->gr_mem != NULL) {
4290b5e8899SSean Farley 		for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++)
430805da51aSSean Farley 			line_size += strlen(gr->gr_mem[ndx]) + 1;
4310b5e8899SSean Farley 		if (ndx > 0)
432805da51aSSean Farley 			line_size--;
433805da51aSSean Farley 	}
4340b5e8899SSean Farley 
4350b5e8899SSean Farley 	/* Create the group line and fill it. */
43649013fb4SMateusz Guzik 	if ((line = p = malloc(line_size)) == NULL)
4370b5e8899SSean Farley 		return (NULL);
43849013fb4SMateusz Guzik 	p += sprintf(p, group_line_format, gr->gr_name, gr->gr_passwd,
4390dd84a53SSean Farley 	    (uintmax_t)gr->gr_gid);
44049013fb4SMateusz Guzik 	if (gr->gr_mem != NULL) {
44149013fb4SMateusz Guzik 		sep = "";
4420b5e8899SSean Farley 		for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) {
44349013fb4SMateusz Guzik 			p = stpcpy(p, sep);
44449013fb4SMateusz Guzik 			p = stpcpy(p, gr->gr_mem[ndx]);
44549013fb4SMateusz Guzik 			sep = ",";
44649013fb4SMateusz Guzik 		}
4470b5e8899SSean Farley 	}
4480b5e8899SSean Farley 
4490b5e8899SSean Farley 	return (line);
4500b5e8899SSean Farley }
4510b5e8899SSean Farley 
4520b5e8899SSean Farley /*
4530b5e8899SSean Farley  * Duplicate a struct group.
4540b5e8899SSean Farley  */
4550b5e8899SSean Farley struct group *
4560b5e8899SSean Farley gr_dup(const struct group *gr)
4570b5e8899SSean Farley {
45886e2f99dSDiane Bruce 	return (gr_add(gr, NULL));
45986e2f99dSDiane Bruce }
46086e2f99dSDiane Bruce /*
46186e2f99dSDiane Bruce  * Add a new member name to a struct group.
46286e2f99dSDiane Bruce  */
46386e2f99dSDiane Bruce struct group *
46486e2f99dSDiane Bruce gr_add(const struct group *gr, const char *newmember)
46586e2f99dSDiane Bruce {
4665e879837SDiane Bruce 	char *mem;
4670b5e8899SSean Farley 	size_t len;
468805da51aSSean Farley 	int num_mem;
4690b5e8899SSean Farley 
47086e2f99dSDiane Bruce 	num_mem = 0;
47186e2f99dSDiane Bruce 	len = grmemlen(gr, newmember, &num_mem);
4720b5e8899SSean Farley 	/* Create new group and copy old group into it. */
4735e879837SDiane Bruce 	if ((mem = malloc(len)) == NULL)
4740b5e8899SSean Farley 		return (NULL);
4755e879837SDiane Bruce 	return (grcopy(gr, mem, newmember, num_mem));
47686e2f99dSDiane Bruce }
47786e2f99dSDiane Bruce 
47886e2f99dSDiane Bruce /* It is safer to walk the pointers given at gr_mem since there is no
4795e879837SDiane Bruce  * guarantee the gr_mem + strings are contiguous in the given struct group
4805e879837SDiane Bruce  * but compactify the new group into the following form.
48186e2f99dSDiane Bruce  *
48286e2f99dSDiane Bruce  * The new struct is laid out like this in memory. The example given is
48386e2f99dSDiane Bruce  * for a group with two members only.
48486e2f99dSDiane Bruce  *
48586e2f99dSDiane Bruce  * {
48686e2f99dSDiane Bruce  * (char *name)
48786e2f99dSDiane Bruce  * (char *passwd)
48886e2f99dSDiane Bruce  * (int gid)
48986e2f99dSDiane Bruce  * (gr_mem * newgrp + sizeof(struct group) + sizeof(**)) points to gr_mem area
49086e2f99dSDiane Bruce  * gr_mem area
49186e2f99dSDiane Bruce  * (member1 *)
49286e2f99dSDiane Bruce  * (member2 *)
49386e2f99dSDiane Bruce  * (NULL)
49486e2f99dSDiane Bruce  * (name string)
49586e2f99dSDiane Bruce  * (passwd string)
49686e2f99dSDiane Bruce  * (member1 string)
49786e2f99dSDiane Bruce  * (member2 string)
49886e2f99dSDiane Bruce  * }
49986e2f99dSDiane Bruce  */
50086e2f99dSDiane Bruce /*
5015e879837SDiane Bruce  * Copy the contents of a group plus given name to a preallocated group struct
50286e2f99dSDiane Bruce  */
50386e2f99dSDiane Bruce static struct group *
5045e879837SDiane Bruce grcopy(const struct group *gr, char *dst, const char *name, int ndx)
50586e2f99dSDiane Bruce {
50686e2f99dSDiane Bruce 	int i;
5075e879837SDiane Bruce 	struct group *newgr;
50886e2f99dSDiane Bruce 
5095e879837SDiane Bruce 	newgr = (struct group *)(void *)dst;	/* avoid alignment warning */
5105e879837SDiane Bruce 	dst += sizeof(*newgr);
5115e879837SDiane Bruce 	if (ndx != 0) {
5125e879837SDiane Bruce 		newgr->gr_mem = (char **)(void *)(dst);	/* avoid alignment warning */
5135e879837SDiane Bruce 		dst += (ndx + 1) * sizeof(*newgr->gr_mem);
5145e879837SDiane Bruce 	} else
5151067c64aSBaptiste Daroussin 		newgr->gr_mem = NULL;
5160b5e8899SSean Farley 	if (gr->gr_name != NULL) {
5171067c64aSBaptiste Daroussin 		newgr->gr_name = dst;
5181067c64aSBaptiste Daroussin 		dst = stpcpy(dst, gr->gr_name) + 1;
51986e2f99dSDiane Bruce 	} else
520167145a1SBaptiste Daroussin 		newgr->gr_name = NULL;
5210b5e8899SSean Farley 	if (gr->gr_passwd != NULL) {
5221067c64aSBaptiste Daroussin 		newgr->gr_passwd = dst;
5231067c64aSBaptiste Daroussin 		dst = stpcpy(dst, gr->gr_passwd) + 1;
52486e2f99dSDiane Bruce 	} else
525167145a1SBaptiste Daroussin 		newgr->gr_passwd = NULL;
5261067c64aSBaptiste Daroussin 	newgr->gr_gid = gr->gr_gid;
5275e879837SDiane Bruce 	i = 0;
5285e879837SDiane Bruce 	/* Original group struct might have a NULL gr_mem */
5295e879837SDiane Bruce 	if (gr->gr_mem != NULL) {
5305e879837SDiane Bruce 		for (; gr->gr_mem[i] != NULL; i++) {
53186e2f99dSDiane Bruce 			newgr->gr_mem[i] = dst;
53286e2f99dSDiane Bruce 			dst = stpcpy(dst, gr->gr_mem[i]) + 1;
5330b5e8899SSean Farley 		}
5345e879837SDiane Bruce 	}
5355e879837SDiane Bruce 	/* If name is not NULL, newgr->gr_mem is known to be not NULL */
53686e2f99dSDiane Bruce 	if (name != NULL) {
53786e2f99dSDiane Bruce 		newgr->gr_mem[i++] = dst;
53886e2f99dSDiane Bruce 		dst = stpcpy(dst, name) + 1;
53986e2f99dSDiane Bruce 	}
5405e879837SDiane Bruce 	/* if newgr->gr_mem is not NULL add NULL marker */
5415e879837SDiane Bruce 	if (newgr->gr_mem != NULL)
54286e2f99dSDiane Bruce 		newgr->gr_mem[i] = NULL;
5435e879837SDiane Bruce 
5441067c64aSBaptiste Daroussin 	return (newgr);
5450b5e8899SSean Farley }
5460b5e8899SSean Farley 
5470b5e8899SSean Farley /*
54886e2f99dSDiane Bruce  *  Calculate length of a struct group + given name
549be49c830SBaptiste Daroussin  */
55086e2f99dSDiane Bruce static size_t
55186e2f99dSDiane Bruce grmemlen(const struct group *gr, const char *name, int *num_mem)
552be49c830SBaptiste Daroussin {
55386e2f99dSDiane Bruce 	size_t len;
55486e2f99dSDiane Bruce 	int i;
555be49c830SBaptiste Daroussin 
55686e2f99dSDiane Bruce 	if (gr == NULL)
55786e2f99dSDiane Bruce 		return (0);
55886e2f99dSDiane Bruce 	/* Calculate size of the group. */
55986e2f99dSDiane Bruce 	len = sizeof(*gr);
56086e2f99dSDiane Bruce 	if (gr->gr_name != NULL)
56186e2f99dSDiane Bruce 		len += strlen(gr->gr_name) + 1;
56286e2f99dSDiane Bruce 	if (gr->gr_passwd != NULL)
56386e2f99dSDiane Bruce 		len += strlen(gr->gr_passwd) + 1;
5645e879837SDiane Bruce 	i = 0;
565be49c830SBaptiste Daroussin 	if (gr->gr_mem != NULL) {
5665e879837SDiane Bruce 		for (; gr->gr_mem[i] != NULL; i++) {
56786e2f99dSDiane Bruce 			len += strlen(gr->gr_mem[i]) + 1;
56886e2f99dSDiane Bruce 			len += sizeof(*gr->gr_mem);
569be49c830SBaptiste Daroussin 		}
570be49c830SBaptiste Daroussin 	}
57186e2f99dSDiane Bruce 	if (name != NULL) {
5725e879837SDiane Bruce 		i++;
57386e2f99dSDiane Bruce 		len += strlen(name) + 1;
57486e2f99dSDiane Bruce 		len += sizeof(*gr->gr_mem);
575be49c830SBaptiste Daroussin 	}
5765e879837SDiane Bruce 	/* Allow for NULL pointer */
5775e879837SDiane Bruce 	if (i != 0)
5785e879837SDiane Bruce 		len += sizeof(*gr->gr_mem);
5795e879837SDiane Bruce 	*num_mem = i;
58086e2f99dSDiane Bruce 	return(len);
581be49c830SBaptiste Daroussin }
582be49c830SBaptiste Daroussin 
583be49c830SBaptiste Daroussin /*
5840b5e8899SSean Farley  * Scan a line and place it into a group structure.
5850b5e8899SSean Farley  */
5860b5e8899SSean Farley static bool
5870b5e8899SSean Farley __gr_scan(char *line, struct group *gr)
5880b5e8899SSean Farley {
5890b5e8899SSean Farley 	char *loc;
5900b5e8899SSean Farley 	int ndx;
5910b5e8899SSean Farley 
5920b5e8899SSean Farley 	/* Assign non-member information to structure. */
5930b5e8899SSean Farley 	gr->gr_name = line;
5940b5e8899SSean Farley 	if ((loc = strchr(line, ':')) == NULL)
5950b5e8899SSean Farley 		return (false);
5960b5e8899SSean Farley 	*loc = '\0';
5970b5e8899SSean Farley 	gr->gr_passwd = loc + 1;
598a0671723SSean Farley 	if (*gr->gr_passwd == ':')
599a0671723SSean Farley 		*gr->gr_passwd = '\0';
6000b5e8899SSean Farley 	else {
6010b5e8899SSean Farley 		if ((loc = strchr(loc + 1, ':')) == NULL)
6020b5e8899SSean Farley 			return (false);
6030b5e8899SSean Farley 		*loc = '\0';
6040b5e8899SSean Farley 	}
605a0671723SSean Farley 	if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1)
6060b5e8899SSean Farley 		return (false);
6070b5e8899SSean Farley 
6080b5e8899SSean Farley 	/* Assign member information to structure. */
6090b5e8899SSean Farley 	if ((loc = strchr(loc + 1, ':')) == NULL)
6100b5e8899SSean Farley 		return (false);
6110b5e8899SSean Farley 	line = loc + 1;
6120b5e8899SSean Farley 	gr->gr_mem = NULL;
6130b5e8899SSean Farley 	ndx = 0;
6140b5e8899SSean Farley 	do {
615805da51aSSean Farley 		gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) *
616805da51aSSean Farley 		    (ndx + 1));
617805da51aSSean Farley 		if (gr->gr_mem == NULL)
6180b5e8899SSean Farley 			return (false);
619805da51aSSean Farley 
620805da51aSSean Farley 		/* Skip locations without members (i.e., empty string). */
621805da51aSSean Farley 		do {
6220b5e8899SSean Farley 			gr->gr_mem[ndx] = strsep(&line, ",");
623805da51aSSean Farley 		} while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0');
6240b5e8899SSean Farley 	} while (gr->gr_mem[ndx++] != NULL);
6250b5e8899SSean Farley 
6260b5e8899SSean Farley 	return (true);
6270b5e8899SSean Farley }
6280b5e8899SSean Farley 
6290b5e8899SSean Farley /*
6300b5e8899SSean Farley  * Create a struct group from a line.
6310b5e8899SSean Farley  */
6320b5e8899SSean Farley struct group *
6330b5e8899SSean Farley gr_scan(const char *line)
6340b5e8899SSean Farley {
635a0671723SSean Farley 	struct group gr;
636805da51aSSean Farley 	char *line_copy;
637805da51aSSean Farley 	struct group *new_gr;
6380b5e8899SSean Farley 
639805da51aSSean Farley 	if ((line_copy = strdup(line)) == NULL)
6400b5e8899SSean Farley 		return (NULL);
641805da51aSSean Farley 	if (!__gr_scan(line_copy, &gr)) {
642805da51aSSean Farley 		free(line_copy);
6430b5e8899SSean Farley 		return (NULL);
6440b5e8899SSean Farley 	}
645805da51aSSean Farley 	new_gr = gr_dup(&gr);
646805da51aSSean Farley 	free(line_copy);
6470b5e8899SSean Farley 	if (gr.gr_mem != NULL)
6480b5e8899SSean Farley 		free(gr.gr_mem);
6490b5e8899SSean Farley 
650805da51aSSean Farley 	return (new_gr);
6510b5e8899SSean Farley }
652