xref: /freebsd/lib/libutil/gr_util.c (revision e68bca507d4a7f69ac65c90a70ea90d545d0ec94)
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 	}
144cbaba16bSAlan 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 {
167*e68bca50SDag-Erling Smørgrav 	char *buf, *end, *line, *p, *q, *r, *tmp;
1681926f2f6SBaptiste Daroussin 	struct group *fgr;
1691926f2f6SBaptiste Daroussin 	const struct group *sgr;
170*e68bca50SDag-Erling Smørgrav 	size_t len, size;
1711926f2f6SBaptiste Daroussin 	int eof, readlen;
172*e68bca50SDag-Erling Smørgrav 	char t;
1731926f2f6SBaptiste Daroussin 
1746e6c53f0SBaptiste Daroussin 	if (old_gr == NULL && gr == NULL)
1756e6c53f0SBaptiste Daroussin 		return(-1);
1766e6c53f0SBaptiste Daroussin 
1776e6c53f0SBaptiste Daroussin 	sgr = old_gr;
1786e6c53f0SBaptiste Daroussin 	/* deleting a group */
1791926f2f6SBaptiste Daroussin 	if (gr == NULL) {
1801926f2f6SBaptiste Daroussin 		line = NULL;
1816e6c53f0SBaptiste Daroussin 	} else {
1826e6c53f0SBaptiste Daroussin 		if ((line = gr_make(gr)) == NULL)
1831926f2f6SBaptiste Daroussin 			return (-1);
1846e6c53f0SBaptiste Daroussin 	}
1856e6c53f0SBaptiste Daroussin 
1866e6c53f0SBaptiste Daroussin 	/* adding a group */
1876e6c53f0SBaptiste Daroussin 	if (sgr == NULL)
1886e6c53f0SBaptiste Daroussin 		sgr = gr;
1891926f2f6SBaptiste Daroussin 
190*e68bca50SDag-Erling Smørgrav 	/* initialize the buffer */
191*e68bca50SDag-Erling Smørgrav 	if ((buf = malloc(size = 1024)) == NULL)
192*e68bca50SDag-Erling Smørgrav 		goto err;
193*e68bca50SDag-Erling Smørgrav 
1941926f2f6SBaptiste Daroussin 	eof = 0;
1951926f2f6SBaptiste Daroussin 	len = 0;
1961926f2f6SBaptiste Daroussin 	p = q = end = buf;
1971926f2f6SBaptiste Daroussin 	for (;;) {
1981926f2f6SBaptiste Daroussin 		/* find the end of the current line */
1991926f2f6SBaptiste Daroussin 		for (p = q; q < end && *q != '\0'; ++q)
2001926f2f6SBaptiste Daroussin 			if (*q == '\n')
2011926f2f6SBaptiste Daroussin 				break;
2021926f2f6SBaptiste Daroussin 
2031926f2f6SBaptiste Daroussin 		/* if we don't have a complete line, fill up the buffer */
2041926f2f6SBaptiste Daroussin 		if (q >= end) {
2051926f2f6SBaptiste Daroussin 			if (eof)
2061926f2f6SBaptiste Daroussin 				break;
207*e68bca50SDag-Erling Smørgrav 			while ((size_t)(q - p) >= size) {
208*e68bca50SDag-Erling Smørgrav 				if ((tmp = realloc(buf, size * 2)) == NULL) {
2091926f2f6SBaptiste Daroussin 					warnx("group line too long");
2101926f2f6SBaptiste Daroussin 					goto err;
2111926f2f6SBaptiste Daroussin 				}
212*e68bca50SDag-Erling Smørgrav 				p = tmp + (p - buf);
213*e68bca50SDag-Erling Smørgrav 				q = tmp + (q - buf);
214*e68bca50SDag-Erling Smørgrav 				end = tmp + (end - buf);
215*e68bca50SDag-Erling Smørgrav 				buf = tmp;
216*e68bca50SDag-Erling Smørgrav 				size = size * 2;
217*e68bca50SDag-Erling Smørgrav 			}
2181926f2f6SBaptiste Daroussin 			if (p < end) {
2191926f2f6SBaptiste Daroussin 				q = memmove(buf, p, end -p);
2201926f2f6SBaptiste Daroussin 				end -= p - buf;
2211926f2f6SBaptiste Daroussin 			} else {
2221926f2f6SBaptiste Daroussin 				p = q = end = buf;
2231926f2f6SBaptiste Daroussin 			}
224*e68bca50SDag-Erling Smørgrav 			readlen = read(ffd, end, size - (end - buf));
2251926f2f6SBaptiste Daroussin 			if (readlen == -1)
2261926f2f6SBaptiste Daroussin 				goto err;
2271926f2f6SBaptiste Daroussin 			else
2281926f2f6SBaptiste Daroussin 				len = (size_t)readlen;
2291926f2f6SBaptiste Daroussin 			if (len == 0 && p == buf)
2301926f2f6SBaptiste Daroussin 				break;
2311926f2f6SBaptiste Daroussin 			end += len;
2321926f2f6SBaptiste Daroussin 			len = end - buf;
233*e68bca50SDag-Erling Smørgrav 			if (len < size) {
2341926f2f6SBaptiste Daroussin 				eof = 1;
2351926f2f6SBaptiste Daroussin 				if (len > 0 && buf[len -1] != '\n')
2361926f2f6SBaptiste Daroussin 					++len, *end++ = '\n';
2371926f2f6SBaptiste Daroussin 			}
2381926f2f6SBaptiste Daroussin 			continue;
2391926f2f6SBaptiste Daroussin 		}
2401926f2f6SBaptiste Daroussin 
2411926f2f6SBaptiste Daroussin 		/* is it a blank line or a comment? */
2421926f2f6SBaptiste Daroussin 		for (r = p; r < q && isspace(*r); ++r)
2431926f2f6SBaptiste Daroussin 			/* nothing */;
2441926f2f6SBaptiste Daroussin 		if (r == q || *r == '#') {
2451926f2f6SBaptiste Daroussin 			/* yep */
2461926f2f6SBaptiste Daroussin 			if (write(tfd, p, q -p + 1) != q - p + 1)
2471926f2f6SBaptiste Daroussin 				goto err;
2481926f2f6SBaptiste Daroussin 			++q;
2491926f2f6SBaptiste Daroussin 			continue;
2501926f2f6SBaptiste Daroussin 		}
2511926f2f6SBaptiste Daroussin 
2521926f2f6SBaptiste Daroussin 		/* is it the one we're looking for? */
2531926f2f6SBaptiste Daroussin 
2541926f2f6SBaptiste Daroussin 		t = *q;
2551926f2f6SBaptiste Daroussin 		*q = '\0';
2561926f2f6SBaptiste Daroussin 
2571926f2f6SBaptiste Daroussin 		fgr = gr_scan(r);
2581926f2f6SBaptiste Daroussin 
2591926f2f6SBaptiste Daroussin 		/* fgr is either a struct group for the current line,
2601926f2f6SBaptiste Daroussin 		 * or NULL if the line is malformed.
2611926f2f6SBaptiste Daroussin 		 */
2621926f2f6SBaptiste Daroussin 
2631926f2f6SBaptiste Daroussin 		*q = t;
2641926f2f6SBaptiste Daroussin 		if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) {
2651926f2f6SBaptiste Daroussin 			/* nope */
2661926f2f6SBaptiste Daroussin 			if (fgr != NULL)
2671926f2f6SBaptiste Daroussin 				free(fgr);
2681926f2f6SBaptiste Daroussin 			if (write(tfd, p, q - p + 1) != q - p + 1)
2691926f2f6SBaptiste Daroussin 				goto err;
2701926f2f6SBaptiste Daroussin 			++q;
2711926f2f6SBaptiste Daroussin 			continue;
2721926f2f6SBaptiste Daroussin 		}
2731926f2f6SBaptiste Daroussin 		if (old_gr && !gr_equal(fgr, old_gr)) {
2741926f2f6SBaptiste Daroussin 			warnx("entry inconsistent");
2751926f2f6SBaptiste Daroussin 			free(fgr);
2761926f2f6SBaptiste Daroussin 			errno = EINVAL; /* hack */
2771926f2f6SBaptiste Daroussin 			goto err;
2781926f2f6SBaptiste Daroussin 		}
2791926f2f6SBaptiste Daroussin 		free(fgr);
2801926f2f6SBaptiste Daroussin 
2811926f2f6SBaptiste Daroussin 		/* it is, replace or remove it */
2821926f2f6SBaptiste Daroussin 		if (line != NULL) {
2831926f2f6SBaptiste Daroussin 			len = strlen(line);
2841926f2f6SBaptiste Daroussin 			if (write(tfd, line, len) != (int) len)
2851926f2f6SBaptiste Daroussin 				goto err;
2861926f2f6SBaptiste Daroussin 		} else {
2871926f2f6SBaptiste Daroussin 			/* when removed, avoid the \n */
2881926f2f6SBaptiste Daroussin 			q++;
2891926f2f6SBaptiste Daroussin 		}
2901926f2f6SBaptiste Daroussin 		/* we're done, just copy the rest over */
2911926f2f6SBaptiste Daroussin 		for (;;) {
2921926f2f6SBaptiste Daroussin 			if (write(tfd, q, end - q) != end - q)
2931926f2f6SBaptiste Daroussin 				goto err;
2941926f2f6SBaptiste Daroussin 			q = buf;
295*e68bca50SDag-Erling Smørgrav 			readlen = read(ffd, buf, size);
2961926f2f6SBaptiste Daroussin 			if (readlen == 0)
2971926f2f6SBaptiste Daroussin 				break;
2981926f2f6SBaptiste Daroussin 			else
2991926f2f6SBaptiste Daroussin 				len = (size_t)readlen;
3001926f2f6SBaptiste Daroussin 			if (readlen == -1)
3011926f2f6SBaptiste Daroussin 				goto err;
3021926f2f6SBaptiste Daroussin 			end = buf + len;
3031926f2f6SBaptiste Daroussin 		}
3041926f2f6SBaptiste Daroussin 		goto done;
3051926f2f6SBaptiste Daroussin 	}
3061926f2f6SBaptiste Daroussin 
3071926f2f6SBaptiste Daroussin 	/* if we got here, we didn't find the old entry */
3081926f2f6SBaptiste Daroussin 	if (line == NULL) {
3091926f2f6SBaptiste Daroussin 		errno = ENOENT;
3101926f2f6SBaptiste Daroussin 		goto err;
3111926f2f6SBaptiste Daroussin 	}
3121926f2f6SBaptiste Daroussin 	len = strlen(line);
3131926f2f6SBaptiste Daroussin 	if ((size_t)write(tfd, line, len) != len ||
3141926f2f6SBaptiste Daroussin 	   write(tfd, "\n", 1) != 1)
3151926f2f6SBaptiste Daroussin 		goto err;
3161926f2f6SBaptiste Daroussin  done:
3171926f2f6SBaptiste Daroussin 	free(line);
318*e68bca50SDag-Erling Smørgrav 	free(buf);
3191926f2f6SBaptiste Daroussin 	return (0);
3201926f2f6SBaptiste Daroussin  err:
3211926f2f6SBaptiste Daroussin 	free(line);
322*e68bca50SDag-Erling Smørgrav 	free(buf);
3231926f2f6SBaptiste Daroussin 	return (-1);
3241926f2f6SBaptiste Daroussin }
3251926f2f6SBaptiste Daroussin 
3261926f2f6SBaptiste Daroussin /*
3271926f2f6SBaptiste Daroussin  * Regenerate the group file
3281926f2f6SBaptiste Daroussin  */
3291926f2f6SBaptiste Daroussin int
3301926f2f6SBaptiste Daroussin gr_mkdb(void)
3311926f2f6SBaptiste Daroussin {
332d32a66b2SRenato Botelho 	int fd;
333d32a66b2SRenato Botelho 
33409259e6cSBaptiste Daroussin 	if (chmod(tempname, 0644) != 0)
33509259e6cSBaptiste Daroussin 		return (-1);
3362d2b6ad7SBaptiste Daroussin 
337d32a66b2SRenato Botelho 	if (rename(tempname, group_file) != 0)
338d32a66b2SRenato Botelho 		return (-1);
339d32a66b2SRenato Botelho 
340d32a66b2SRenato Botelho 	/*
341d32a66b2SRenato Botelho 	 * Make sure new group file is safe on disk. To improve performance we
342d32a66b2SRenato Botelho 	 * will call fsync() to the directory where file lies
343d32a66b2SRenato Botelho 	 */
344d32a66b2SRenato Botelho 	if ((fd = open(group_dir, O_RDONLY|O_DIRECTORY)) == -1)
345d32a66b2SRenato Botelho 		return (-1);
346d32a66b2SRenato Botelho 
347d32a66b2SRenato Botelho 	if (fsync(fd) != 0) {
348d32a66b2SRenato Botelho 		close(fd);
349d32a66b2SRenato Botelho 		return (-1);
350d32a66b2SRenato Botelho 	}
351d32a66b2SRenato Botelho 
352d32a66b2SRenato Botelho 	close(fd);
353d32a66b2SRenato Botelho 	return(0);
3541926f2f6SBaptiste Daroussin }
3551926f2f6SBaptiste Daroussin 
3561926f2f6SBaptiste Daroussin /*
35708ecf0ccSMateusz Guzik  * Clean up. Preserves errno for the caller's convenience.
3581926f2f6SBaptiste Daroussin  */
3591926f2f6SBaptiste Daroussin void
3601926f2f6SBaptiste Daroussin gr_fini(void)
3611926f2f6SBaptiste Daroussin {
3621926f2f6SBaptiste Daroussin 	int serrno;
3631926f2f6SBaptiste Daroussin 
3641926f2f6SBaptiste Daroussin 	if (!initialized)
3651926f2f6SBaptiste Daroussin 		return;
3661926f2f6SBaptiste Daroussin 	initialized = 0;
3671926f2f6SBaptiste Daroussin 	serrno = errno;
3681926f2f6SBaptiste Daroussin 	if (*tempname != '\0') {
3691926f2f6SBaptiste Daroussin 		unlink(tempname);
3701926f2f6SBaptiste Daroussin 		*tempname = '\0';
3711926f2f6SBaptiste Daroussin 	}
3721926f2f6SBaptiste Daroussin 	if (lockfd != -1)
3731926f2f6SBaptiste Daroussin 		close(lockfd);
3741926f2f6SBaptiste Daroussin 	errno = serrno;
3751926f2f6SBaptiste Daroussin }
3761926f2f6SBaptiste Daroussin 
3771926f2f6SBaptiste Daroussin /*
3780b5e8899SSean Farley  * Compares two struct group's.
3790b5e8899SSean Farley  */
3800b5e8899SSean Farley int
3810b5e8899SSean Farley gr_equal(const struct group *gr1, const struct group *gr2)
3820b5e8899SSean Farley {
3830b5e8899SSean Farley 
3840b5e8899SSean Farley 	/* Check that the non-member information is the same. */
385805da51aSSean Farley 	if (gr1->gr_name == NULL || gr2->gr_name == NULL) {
386805da51aSSean Farley 		if (gr1->gr_name != gr2->gr_name)
387805da51aSSean Farley 			return (false);
388805da51aSSean Farley 	} else if (strcmp(gr1->gr_name, gr2->gr_name) != 0)
389805da51aSSean Farley 		return (false);
390805da51aSSean Farley 	if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) {
391805da51aSSean Farley 		if (gr1->gr_passwd != gr2->gr_passwd)
392805da51aSSean Farley 			return (false);
393805da51aSSean Farley 	} else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0)
394805da51aSSean Farley 		return (false);
395805da51aSSean Farley 	if (gr1->gr_gid != gr2->gr_gid)
396805da51aSSean Farley 		return (false);
3970b5e8899SSean Farley 
398f8c88390SMark Johnston 	/*
399f8c88390SMark Johnston 	 * Check all members in both groups.
4005e879837SDiane Bruce 	 * getgrnam can return gr_mem with a pointer to NULL.
4015e879837SDiane Bruce 	 * gr_dup and gr_add strip out this superfluous NULL, setting
4025e879837SDiane Bruce 	 * gr_mem to NULL for no members.
4035e879837SDiane Bruce 	*/
4045e879837SDiane Bruce 	if (gr1->gr_mem != NULL && gr2->gr_mem != NULL) {
4055e879837SDiane Bruce 		int i;
4060b5e8899SSean Farley 
407f8c88390SMark Johnston 		for (i = 0;
408f8c88390SMark Johnston 		    gr1->gr_mem[i] != NULL && gr2->gr_mem[i] != NULL; i++) {
4095e879837SDiane Bruce 			if (strcmp(gr1->gr_mem[i], gr2->gr_mem[i]) != 0)
410805da51aSSean Farley 				return (false);
4110b5e8899SSean Farley 		}
412f8c88390SMark Johnston 		if (gr1->gr_mem[i] != NULL || gr2->gr_mem[i] != NULL)
4135e879837SDiane Bruce 			return (false);
414f8c88390SMark Johnston 	} else if (gr1->gr_mem != NULL && gr1->gr_mem[0] != NULL) {
415f8c88390SMark Johnston 		return (false);
416f8c88390SMark Johnston 	} else if (gr2->gr_mem != NULL && gr2->gr_mem[0] != NULL) {
417f8c88390SMark Johnston 		return (false);
418f8c88390SMark Johnston 	}
4190b5e8899SSean Farley 
420805da51aSSean Farley 	return (true);
4210b5e8899SSean Farley }
4220b5e8899SSean Farley 
4230b5e8899SSean Farley /*
4240b5e8899SSean Farley  * Make a group line out of a struct group.
4250b5e8899SSean Farley  */
4260b5e8899SSean Farley char *
4270b5e8899SSean Farley gr_make(const struct group *gr)
4280b5e8899SSean Farley {
429fe75b0f0SMateusz Guzik 	const char *group_line_format = "%s:%s:%ju:";
43049013fb4SMateusz Guzik 	const char *sep;
4310b5e8899SSean Farley 	char *line;
43249013fb4SMateusz Guzik 	char *p;
433805da51aSSean Farley 	size_t line_size;
434a0671723SSean Farley 	int ndx;
4350b5e8899SSean Farley 
4360b5e8899SSean Farley 	/* Calculate the length of the group line. */
437805da51aSSean Farley 	line_size = snprintf(NULL, 0, group_line_format, gr->gr_name,
4380b5e8899SSean Farley 	    gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1;
439805da51aSSean Farley 	if (gr->gr_mem != NULL) {
4400b5e8899SSean Farley 		for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++)
441805da51aSSean Farley 			line_size += strlen(gr->gr_mem[ndx]) + 1;
4420b5e8899SSean Farley 		if (ndx > 0)
443805da51aSSean Farley 			line_size--;
444805da51aSSean Farley 	}
4450b5e8899SSean Farley 
4460b5e8899SSean Farley 	/* Create the group line and fill it. */
44749013fb4SMateusz Guzik 	if ((line = p = malloc(line_size)) == NULL)
4480b5e8899SSean Farley 		return (NULL);
44949013fb4SMateusz Guzik 	p += sprintf(p, group_line_format, gr->gr_name, gr->gr_passwd,
4500dd84a53SSean Farley 	    (uintmax_t)gr->gr_gid);
45149013fb4SMateusz Guzik 	if (gr->gr_mem != NULL) {
45249013fb4SMateusz Guzik 		sep = "";
4530b5e8899SSean Farley 		for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) {
45449013fb4SMateusz Guzik 			p = stpcpy(p, sep);
45549013fb4SMateusz Guzik 			p = stpcpy(p, gr->gr_mem[ndx]);
45649013fb4SMateusz Guzik 			sep = ",";
45749013fb4SMateusz Guzik 		}
4580b5e8899SSean Farley 	}
4590b5e8899SSean Farley 
4600b5e8899SSean Farley 	return (line);
4610b5e8899SSean Farley }
4620b5e8899SSean Farley 
4630b5e8899SSean Farley /*
4640b5e8899SSean Farley  * Duplicate a struct group.
4650b5e8899SSean Farley  */
4660b5e8899SSean Farley struct group *
4670b5e8899SSean Farley gr_dup(const struct group *gr)
4680b5e8899SSean Farley {
46986e2f99dSDiane Bruce 	return (gr_add(gr, NULL));
47086e2f99dSDiane Bruce }
47186e2f99dSDiane Bruce /*
47286e2f99dSDiane Bruce  * Add a new member name to a struct group.
47386e2f99dSDiane Bruce  */
47486e2f99dSDiane Bruce struct group *
47586e2f99dSDiane Bruce gr_add(const struct group *gr, const char *newmember)
47686e2f99dSDiane Bruce {
4775e879837SDiane Bruce 	char *mem;
4780b5e8899SSean Farley 	size_t len;
479805da51aSSean Farley 	int num_mem;
4800b5e8899SSean Farley 
48186e2f99dSDiane Bruce 	num_mem = 0;
48286e2f99dSDiane Bruce 	len = grmemlen(gr, newmember, &num_mem);
4830b5e8899SSean Farley 	/* Create new group and copy old group into it. */
4845e879837SDiane Bruce 	if ((mem = malloc(len)) == NULL)
4850b5e8899SSean Farley 		return (NULL);
4865e879837SDiane Bruce 	return (grcopy(gr, mem, newmember, num_mem));
48786e2f99dSDiane Bruce }
48886e2f99dSDiane Bruce 
48986e2f99dSDiane Bruce /* It is safer to walk the pointers given at gr_mem since there is no
4905e879837SDiane Bruce  * guarantee the gr_mem + strings are contiguous in the given struct group
4915e879837SDiane Bruce  * but compactify the new group into the following form.
49286e2f99dSDiane Bruce  *
49386e2f99dSDiane Bruce  * The new struct is laid out like this in memory. The example given is
49486e2f99dSDiane Bruce  * for a group with two members only.
49586e2f99dSDiane Bruce  *
49686e2f99dSDiane Bruce  * {
49786e2f99dSDiane Bruce  * (char *name)
49886e2f99dSDiane Bruce  * (char *passwd)
49986e2f99dSDiane Bruce  * (int gid)
50086e2f99dSDiane Bruce  * (gr_mem * newgrp + sizeof(struct group) + sizeof(**)) points to gr_mem area
50186e2f99dSDiane Bruce  * gr_mem area
50286e2f99dSDiane Bruce  * (member1 *)
50386e2f99dSDiane Bruce  * (member2 *)
50486e2f99dSDiane Bruce  * (NULL)
50586e2f99dSDiane Bruce  * (name string)
50686e2f99dSDiane Bruce  * (passwd string)
50786e2f99dSDiane Bruce  * (member1 string)
50886e2f99dSDiane Bruce  * (member2 string)
50986e2f99dSDiane Bruce  * }
51086e2f99dSDiane Bruce  */
51186e2f99dSDiane Bruce /*
5125e879837SDiane Bruce  * Copy the contents of a group plus given name to a preallocated group struct
51386e2f99dSDiane Bruce  */
51486e2f99dSDiane Bruce static struct group *
5155e879837SDiane Bruce grcopy(const struct group *gr, char *dst, const char *name, int ndx)
51686e2f99dSDiane Bruce {
51786e2f99dSDiane Bruce 	int i;
5185e879837SDiane Bruce 	struct group *newgr;
51986e2f99dSDiane Bruce 
5205e879837SDiane Bruce 	newgr = (struct group *)(void *)dst;	/* avoid alignment warning */
5215e879837SDiane Bruce 	dst += sizeof(*newgr);
5225e879837SDiane Bruce 	if (ndx != 0) {
5235e879837SDiane Bruce 		newgr->gr_mem = (char **)(void *)(dst);	/* avoid alignment warning */
5245e879837SDiane Bruce 		dst += (ndx + 1) * sizeof(*newgr->gr_mem);
5255e879837SDiane Bruce 	} else
5261067c64aSBaptiste Daroussin 		newgr->gr_mem = NULL;
5270b5e8899SSean Farley 	if (gr->gr_name != NULL) {
5281067c64aSBaptiste Daroussin 		newgr->gr_name = dst;
5291067c64aSBaptiste Daroussin 		dst = stpcpy(dst, gr->gr_name) + 1;
53086e2f99dSDiane Bruce 	} else
531167145a1SBaptiste Daroussin 		newgr->gr_name = NULL;
5320b5e8899SSean Farley 	if (gr->gr_passwd != NULL) {
5331067c64aSBaptiste Daroussin 		newgr->gr_passwd = dst;
5341067c64aSBaptiste Daroussin 		dst = stpcpy(dst, gr->gr_passwd) + 1;
53586e2f99dSDiane Bruce 	} else
536167145a1SBaptiste Daroussin 		newgr->gr_passwd = NULL;
5371067c64aSBaptiste Daroussin 	newgr->gr_gid = gr->gr_gid;
5385e879837SDiane Bruce 	i = 0;
5395e879837SDiane Bruce 	/* Original group struct might have a NULL gr_mem */
5405e879837SDiane Bruce 	if (gr->gr_mem != NULL) {
5415e879837SDiane Bruce 		for (; gr->gr_mem[i] != NULL; i++) {
54286e2f99dSDiane Bruce 			newgr->gr_mem[i] = dst;
54386e2f99dSDiane Bruce 			dst = stpcpy(dst, gr->gr_mem[i]) + 1;
5440b5e8899SSean Farley 		}
5455e879837SDiane Bruce 	}
5465e879837SDiane Bruce 	/* If name is not NULL, newgr->gr_mem is known to be not NULL */
54786e2f99dSDiane Bruce 	if (name != NULL) {
54886e2f99dSDiane Bruce 		newgr->gr_mem[i++] = dst;
54986e2f99dSDiane Bruce 		dst = stpcpy(dst, name) + 1;
55086e2f99dSDiane Bruce 	}
5515e879837SDiane Bruce 	/* if newgr->gr_mem is not NULL add NULL marker */
5525e879837SDiane Bruce 	if (newgr->gr_mem != NULL)
55386e2f99dSDiane Bruce 		newgr->gr_mem[i] = NULL;
5545e879837SDiane Bruce 
5551067c64aSBaptiste Daroussin 	return (newgr);
5560b5e8899SSean Farley }
5570b5e8899SSean Farley 
5580b5e8899SSean Farley /*
55986e2f99dSDiane Bruce  *  Calculate length of a struct group + given name
560be49c830SBaptiste Daroussin  */
56186e2f99dSDiane Bruce static size_t
56286e2f99dSDiane Bruce grmemlen(const struct group *gr, const char *name, int *num_mem)
563be49c830SBaptiste Daroussin {
56486e2f99dSDiane Bruce 	size_t len;
56586e2f99dSDiane Bruce 	int i;
566be49c830SBaptiste Daroussin 
56786e2f99dSDiane Bruce 	if (gr == NULL)
56886e2f99dSDiane Bruce 		return (0);
56986e2f99dSDiane Bruce 	/* Calculate size of the group. */
57086e2f99dSDiane Bruce 	len = sizeof(*gr);
57186e2f99dSDiane Bruce 	if (gr->gr_name != NULL)
57286e2f99dSDiane Bruce 		len += strlen(gr->gr_name) + 1;
57386e2f99dSDiane Bruce 	if (gr->gr_passwd != NULL)
57486e2f99dSDiane Bruce 		len += strlen(gr->gr_passwd) + 1;
5755e879837SDiane Bruce 	i = 0;
576be49c830SBaptiste Daroussin 	if (gr->gr_mem != NULL) {
5775e879837SDiane Bruce 		for (; gr->gr_mem[i] != NULL; i++) {
57886e2f99dSDiane Bruce 			len += strlen(gr->gr_mem[i]) + 1;
57986e2f99dSDiane Bruce 			len += sizeof(*gr->gr_mem);
580be49c830SBaptiste Daroussin 		}
581be49c830SBaptiste Daroussin 	}
58286e2f99dSDiane Bruce 	if (name != NULL) {
5835e879837SDiane Bruce 		i++;
58486e2f99dSDiane Bruce 		len += strlen(name) + 1;
58586e2f99dSDiane Bruce 		len += sizeof(*gr->gr_mem);
586be49c830SBaptiste Daroussin 	}
5875e879837SDiane Bruce 	/* Allow for NULL pointer */
5885e879837SDiane Bruce 	if (i != 0)
5895e879837SDiane Bruce 		len += sizeof(*gr->gr_mem);
5905e879837SDiane Bruce 	*num_mem = i;
59186e2f99dSDiane Bruce 	return(len);
592be49c830SBaptiste Daroussin }
593be49c830SBaptiste Daroussin 
594be49c830SBaptiste Daroussin /*
5950b5e8899SSean Farley  * Scan a line and place it into a group structure.
5960b5e8899SSean Farley  */
5970b5e8899SSean Farley static bool
5980b5e8899SSean Farley __gr_scan(char *line, struct group *gr)
5990b5e8899SSean Farley {
6000b5e8899SSean Farley 	char *loc;
6010b5e8899SSean Farley 	int ndx;
6020b5e8899SSean Farley 
6030b5e8899SSean Farley 	/* Assign non-member information to structure. */
6040b5e8899SSean Farley 	gr->gr_name = line;
6050b5e8899SSean Farley 	if ((loc = strchr(line, ':')) == NULL)
6060b5e8899SSean Farley 		return (false);
6070b5e8899SSean Farley 	*loc = '\0';
6080b5e8899SSean Farley 	gr->gr_passwd = loc + 1;
609a0671723SSean Farley 	if (*gr->gr_passwd == ':')
610a0671723SSean Farley 		*gr->gr_passwd = '\0';
6110b5e8899SSean Farley 	else {
6120b5e8899SSean Farley 		if ((loc = strchr(loc + 1, ':')) == NULL)
6130b5e8899SSean Farley 			return (false);
6140b5e8899SSean Farley 		*loc = '\0';
6150b5e8899SSean Farley 	}
616a0671723SSean Farley 	if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1)
6170b5e8899SSean Farley 		return (false);
6180b5e8899SSean Farley 
6190b5e8899SSean Farley 	/* Assign member information to structure. */
6200b5e8899SSean Farley 	if ((loc = strchr(loc + 1, ':')) == NULL)
6210b5e8899SSean Farley 		return (false);
6220b5e8899SSean Farley 	line = loc + 1;
6230b5e8899SSean Farley 	gr->gr_mem = NULL;
6240b5e8899SSean Farley 	ndx = 0;
6250b5e8899SSean Farley 	do {
626805da51aSSean Farley 		gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) *
627805da51aSSean Farley 		    (ndx + 1));
628805da51aSSean Farley 		if (gr->gr_mem == NULL)
6290b5e8899SSean Farley 			return (false);
630805da51aSSean Farley 
631805da51aSSean Farley 		/* Skip locations without members (i.e., empty string). */
632805da51aSSean Farley 		do {
6330b5e8899SSean Farley 			gr->gr_mem[ndx] = strsep(&line, ",");
634805da51aSSean Farley 		} while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0');
6350b5e8899SSean Farley 	} while (gr->gr_mem[ndx++] != NULL);
6360b5e8899SSean Farley 
6370b5e8899SSean Farley 	return (true);
6380b5e8899SSean Farley }
6390b5e8899SSean Farley 
6400b5e8899SSean Farley /*
6410b5e8899SSean Farley  * Create a struct group from a line.
6420b5e8899SSean Farley  */
6430b5e8899SSean Farley struct group *
6440b5e8899SSean Farley gr_scan(const char *line)
6450b5e8899SSean Farley {
646a0671723SSean Farley 	struct group gr;
647805da51aSSean Farley 	char *line_copy;
648805da51aSSean Farley 	struct group *new_gr;
6490b5e8899SSean Farley 
650805da51aSSean Farley 	if ((line_copy = strdup(line)) == NULL)
6510b5e8899SSean Farley 		return (NULL);
652805da51aSSean Farley 	if (!__gr_scan(line_copy, &gr)) {
653805da51aSSean Farley 		free(line_copy);
6540b5e8899SSean Farley 		return (NULL);
6550b5e8899SSean Farley 	}
656805da51aSSean Farley 	new_gr = gr_dup(&gr);
657805da51aSSean Farley 	free(line_copy);
6580b5e8899SSean Farley 	if (gr.gr_mem != NULL)
6590b5e8899SSean Farley 		free(gr.gr_mem);
6600b5e8899SSean Farley 
661805da51aSSean Farley 	return (new_gr);
6620b5e8899SSean Farley }
663