xref: /freebsd/lib/libutil/gr_util.c (revision 39c64ed6d16d07b32f79513c08e81ad916e22bd9)
10b5e8899SSean Farley /*-
25e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
35e53a4f9SPedro F. Giffuni  *
40b5e8899SSean Farley  * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org>
50b5e8899SSean Farley  * All rights reserved.
60b5e8899SSean Farley  *
70b5e8899SSean Farley  * Redistribution and use in source and binary forms, with or without
80b5e8899SSean Farley  * modification, are permitted provided that the following conditions
90b5e8899SSean Farley  * are met:
100b5e8899SSean Farley  * 1. Redistributions of source code must retain the above copyright
110b5e8899SSean Farley  *    notice, this list of conditions and the following disclaimer,
120b5e8899SSean Farley  *    without modification, immediately at the beginning of the file.
130b5e8899SSean Farley  * 2. Redistributions in binary form must reproduce the above copyright
140b5e8899SSean Farley  *    notice, this list of conditions and the following disclaimer in the
150b5e8899SSean Farley  *    documentation and/or other materials provided with the distribution.
160b5e8899SSean Farley  *
170b5e8899SSean Farley  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
180b5e8899SSean Farley  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
190b5e8899SSean Farley  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
200b5e8899SSean Farley  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
210b5e8899SSean Farley  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
220b5e8899SSean Farley  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
230b5e8899SSean Farley  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
240b5e8899SSean Farley  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
250b5e8899SSean Farley  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
260b5e8899SSean Farley  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
270b5e8899SSean Farley  */
280b5e8899SSean Farley 
290b5e8899SSean Farley #include <sys/cdefs.h>
300b5e8899SSean Farley __FBSDID("$FreeBSD$");
310b5e8899SSean Farley 
320b5e8899SSean Farley #include <sys/param.h>
331926f2f6SBaptiste Daroussin #include <sys/errno.h>
341926f2f6SBaptiste Daroussin #include <sys/stat.h>
35a0671723SSean Farley 
361926f2f6SBaptiste Daroussin #include <ctype.h>
371926f2f6SBaptiste Daroussin #include <err.h>
381926f2f6SBaptiste Daroussin #include <fcntl.h>
390b5e8899SSean Farley #include <grp.h>
400b5e8899SSean Farley #include <inttypes.h>
41a0671723SSean Farley #include <libutil.h>
421926f2f6SBaptiste Daroussin #include <paths.h>
430b5e8899SSean Farley #include <stdbool.h>
440b5e8899SSean Farley #include <stdio.h>
450b5e8899SSean Farley #include <stdlib.h>
460b5e8899SSean Farley #include <string.h>
471926f2f6SBaptiste Daroussin #include <unistd.h>
480b5e8899SSean Farley 
491926f2f6SBaptiste Daroussin static int lockfd = -1;
501926f2f6SBaptiste Daroussin static char group_dir[PATH_MAX];
511926f2f6SBaptiste Daroussin static char group_file[PATH_MAX];
521926f2f6SBaptiste Daroussin static char tempname[PATH_MAX];
531926f2f6SBaptiste Daroussin static int initialized;
5486e2f99dSDiane Bruce static size_t grmemlen(const struct group *, const char *, int *);
555e879837SDiane Bruce static struct group *grcopy(const struct group *gr, char *mem, const char *, int ndx);
561926f2f6SBaptiste Daroussin 
570b5e8899SSean Farley /*
581926f2f6SBaptiste Daroussin  * Initialize statics
591926f2f6SBaptiste Daroussin  */
601926f2f6SBaptiste Daroussin int
611926f2f6SBaptiste Daroussin gr_init(const char *dir, const char *group)
621926f2f6SBaptiste Daroussin {
6329e57550SBaptiste Daroussin 
641926f2f6SBaptiste Daroussin 	if (dir == NULL) {
651926f2f6SBaptiste Daroussin 		strcpy(group_dir, _PATH_ETC);
661926f2f6SBaptiste Daroussin 	} else {
671926f2f6SBaptiste Daroussin 		if (strlen(dir) >= sizeof(group_dir)) {
681926f2f6SBaptiste Daroussin 			errno = ENAMETOOLONG;
691926f2f6SBaptiste Daroussin 			return (-1);
701926f2f6SBaptiste Daroussin 		}
711926f2f6SBaptiste Daroussin 		strcpy(group_dir, dir);
721926f2f6SBaptiste Daroussin 	}
731926f2f6SBaptiste Daroussin 
741926f2f6SBaptiste Daroussin 	if (group == NULL) {
751926f2f6SBaptiste Daroussin 		if (dir == NULL) {
761926f2f6SBaptiste Daroussin 			strcpy(group_file, _PATH_GROUP);
771926f2f6SBaptiste Daroussin 		} else if (snprintf(group_file, sizeof(group_file), "%s/group",
781926f2f6SBaptiste Daroussin 			group_dir) > (int)sizeof(group_file)) {
791926f2f6SBaptiste Daroussin 			errno = ENAMETOOLONG;
801926f2f6SBaptiste Daroussin 			return (-1);
811926f2f6SBaptiste Daroussin 		}
821926f2f6SBaptiste Daroussin 	} else {
831926f2f6SBaptiste Daroussin 		if (strlen(group) >= sizeof(group_file)) {
841926f2f6SBaptiste Daroussin 			errno = ENAMETOOLONG;
851926f2f6SBaptiste Daroussin 			return (-1);
861926f2f6SBaptiste Daroussin 		}
871926f2f6SBaptiste Daroussin 		strcpy(group_file, group);
881926f2f6SBaptiste Daroussin 	}
8929e57550SBaptiste Daroussin 
901926f2f6SBaptiste Daroussin 	initialized = 1;
911926f2f6SBaptiste Daroussin 	return (0);
921926f2f6SBaptiste Daroussin }
931926f2f6SBaptiste Daroussin 
941926f2f6SBaptiste Daroussin /*
951926f2f6SBaptiste Daroussin  * Lock the group file
961926f2f6SBaptiste Daroussin  */
971926f2f6SBaptiste Daroussin int
981926f2f6SBaptiste Daroussin gr_lock(void)
991926f2f6SBaptiste Daroussin {
1001926f2f6SBaptiste Daroussin 	if (*group_file == '\0')
1011926f2f6SBaptiste Daroussin 		return (-1);
1021926f2f6SBaptiste Daroussin 
1031926f2f6SBaptiste Daroussin 	for (;;) {
1041926f2f6SBaptiste Daroussin 		struct stat st;
1051926f2f6SBaptiste Daroussin 
106ede89d5dSBaptiste Daroussin 		lockfd = flopen(group_file, O_RDONLY|O_NONBLOCK|O_CLOEXEC, 0);
10798e79fb1SBaptiste Daroussin 		if (lockfd == -1) {
1081926f2f6SBaptiste Daroussin 			if (errno == EWOULDBLOCK) {
1091926f2f6SBaptiste Daroussin 				errx(1, "the group file is busy");
1101926f2f6SBaptiste Daroussin 			} else {
111*39c64ed6SPiotr Pawel Stefaniak 				err(1, "could not lock the group file");
1121926f2f6SBaptiste Daroussin 			}
1131926f2f6SBaptiste Daroussin 		}
1141926f2f6SBaptiste Daroussin 		if (fstat(lockfd, &st) == -1)
115*39c64ed6SPiotr Pawel Stefaniak 			err(1, "fstat() failed");
1161926f2f6SBaptiste Daroussin 		if (st.st_nlink != 0)
1171926f2f6SBaptiste Daroussin 			break;
1181926f2f6SBaptiste Daroussin 		close(lockfd);
1191926f2f6SBaptiste Daroussin 		lockfd = -1;
1201926f2f6SBaptiste Daroussin 	}
1211926f2f6SBaptiste Daroussin 	return (lockfd);
1221926f2f6SBaptiste Daroussin }
1231926f2f6SBaptiste Daroussin 
1241926f2f6SBaptiste Daroussin /*
1251926f2f6SBaptiste Daroussin  * Create and open a presmuably safe temp file for editing group data
1261926f2f6SBaptiste Daroussin  */
1271926f2f6SBaptiste Daroussin int
1281926f2f6SBaptiste Daroussin gr_tmp(int mfd)
1291926f2f6SBaptiste Daroussin {
1301926f2f6SBaptiste Daroussin 	char buf[8192];
1311926f2f6SBaptiste Daroussin 	ssize_t nr;
1321926f2f6SBaptiste Daroussin 	const char *p;
1331926f2f6SBaptiste Daroussin 	int tfd;
1341926f2f6SBaptiste Daroussin 
1351926f2f6SBaptiste Daroussin 	if (*group_file == '\0')
1361926f2f6SBaptiste Daroussin 		return (-1);
1371926f2f6SBaptiste Daroussin 	if ((p = strrchr(group_file, '/')))
1381926f2f6SBaptiste Daroussin 		++p;
1391926f2f6SBaptiste Daroussin 	else
1401926f2f6SBaptiste Daroussin 		p = group_file;
1411926f2f6SBaptiste Daroussin 	if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX",
1421926f2f6SBaptiste Daroussin 		(int)(p - group_file), group_file) >= (int)sizeof(tempname)) {
1431926f2f6SBaptiste Daroussin 		errno = ENAMETOOLONG;
1441926f2f6SBaptiste Daroussin 		return (-1);
1451926f2f6SBaptiste Daroussin 	}
146cbaba16bSAlan Somers 	if ((tfd = mkostemp(tempname, 0)) == -1)
1471926f2f6SBaptiste Daroussin 		return (-1);
1481926f2f6SBaptiste Daroussin 	if (mfd != -1) {
1491926f2f6SBaptiste Daroussin 		while ((nr = read(mfd, buf, sizeof(buf))) > 0)
1501926f2f6SBaptiste Daroussin 			if (write(tfd, buf, (size_t)nr) != nr)
1511926f2f6SBaptiste Daroussin 				break;
1521926f2f6SBaptiste Daroussin 		if (nr != 0) {
1531926f2f6SBaptiste Daroussin 			unlink(tempname);
1541926f2f6SBaptiste Daroussin 			*tempname = '\0';
1551926f2f6SBaptiste Daroussin 			close(tfd);
1561926f2f6SBaptiste Daroussin 			return (-1);
1571926f2f6SBaptiste Daroussin 		}
1581926f2f6SBaptiste Daroussin 	}
1591926f2f6SBaptiste Daroussin 	return (tfd);
1601926f2f6SBaptiste Daroussin }
1611926f2f6SBaptiste Daroussin 
1621926f2f6SBaptiste Daroussin /*
1631926f2f6SBaptiste Daroussin  * Copy the group file from one descriptor to another, replacing, deleting
1641926f2f6SBaptiste Daroussin  * or adding a single record on the way.
1651926f2f6SBaptiste Daroussin  */
1661926f2f6SBaptiste Daroussin int
1671926f2f6SBaptiste Daroussin gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr)
1681926f2f6SBaptiste Daroussin {
169e68bca50SDag-Erling Smørgrav 	char *buf, *end, *line, *p, *q, *r, *tmp;
1701926f2f6SBaptiste Daroussin 	struct group *fgr;
1711926f2f6SBaptiste Daroussin 	const struct group *sgr;
172e68bca50SDag-Erling Smørgrav 	size_t len, size;
1731926f2f6SBaptiste Daroussin 	int eof, readlen;
174e68bca50SDag-Erling Smørgrav 	char t;
1751926f2f6SBaptiste Daroussin 
1766e6c53f0SBaptiste Daroussin 	if (old_gr == NULL && gr == NULL)
1776e6c53f0SBaptiste Daroussin 		return(-1);
1786e6c53f0SBaptiste Daroussin 
1796e6c53f0SBaptiste Daroussin 	sgr = old_gr;
1806e6c53f0SBaptiste Daroussin 	/* deleting a group */
1811926f2f6SBaptiste Daroussin 	if (gr == NULL) {
1821926f2f6SBaptiste Daroussin 		line = NULL;
1836e6c53f0SBaptiste Daroussin 	} else {
1846e6c53f0SBaptiste Daroussin 		if ((line = gr_make(gr)) == NULL)
1851926f2f6SBaptiste Daroussin 			return (-1);
1866e6c53f0SBaptiste Daroussin 	}
1876e6c53f0SBaptiste Daroussin 
1886e6c53f0SBaptiste Daroussin 	/* adding a group */
1896e6c53f0SBaptiste Daroussin 	if (sgr == NULL)
1906e6c53f0SBaptiste Daroussin 		sgr = gr;
1911926f2f6SBaptiste Daroussin 
192e68bca50SDag-Erling Smørgrav 	/* initialize the buffer */
193e68bca50SDag-Erling Smørgrav 	if ((buf = malloc(size = 1024)) == NULL)
194e68bca50SDag-Erling Smørgrav 		goto err;
195e68bca50SDag-Erling Smørgrav 
1961926f2f6SBaptiste Daroussin 	eof = 0;
1971926f2f6SBaptiste Daroussin 	len = 0;
1981926f2f6SBaptiste Daroussin 	p = q = end = buf;
1991926f2f6SBaptiste Daroussin 	for (;;) {
2001926f2f6SBaptiste Daroussin 		/* find the end of the current line */
2011926f2f6SBaptiste Daroussin 		for (p = q; q < end && *q != '\0'; ++q)
2021926f2f6SBaptiste Daroussin 			if (*q == '\n')
2031926f2f6SBaptiste Daroussin 				break;
2041926f2f6SBaptiste Daroussin 
2051926f2f6SBaptiste Daroussin 		/* if we don't have a complete line, fill up the buffer */
2061926f2f6SBaptiste Daroussin 		if (q >= end) {
2071926f2f6SBaptiste Daroussin 			if (eof)
2081926f2f6SBaptiste Daroussin 				break;
209e68bca50SDag-Erling Smørgrav 			while ((size_t)(q - p) >= size) {
210efa8af7cSPedro F. Giffuni 				if ((tmp = reallocarray(buf, 2, size)) == NULL) {
2111926f2f6SBaptiste Daroussin 					warnx("group line too long");
2121926f2f6SBaptiste Daroussin 					goto err;
2131926f2f6SBaptiste Daroussin 				}
214e68bca50SDag-Erling Smørgrav 				p = tmp + (p - buf);
215e68bca50SDag-Erling Smørgrav 				q = tmp + (q - buf);
216e68bca50SDag-Erling Smørgrav 				end = tmp + (end - buf);
217e68bca50SDag-Erling Smørgrav 				buf = tmp;
218e68bca50SDag-Erling Smørgrav 				size = size * 2;
219e68bca50SDag-Erling Smørgrav 			}
2201926f2f6SBaptiste Daroussin 			if (p < end) {
2211926f2f6SBaptiste Daroussin 				q = memmove(buf, p, end -p);
2221926f2f6SBaptiste Daroussin 				end -= p - buf;
2231926f2f6SBaptiste Daroussin 			} else {
2241926f2f6SBaptiste Daroussin 				p = q = end = buf;
2251926f2f6SBaptiste Daroussin 			}
226e68bca50SDag-Erling Smørgrav 			readlen = read(ffd, end, size - (end - buf));
2271926f2f6SBaptiste Daroussin 			if (readlen == -1)
2281926f2f6SBaptiste Daroussin 				goto err;
2291926f2f6SBaptiste Daroussin 			else
2301926f2f6SBaptiste Daroussin 				len = (size_t)readlen;
2311926f2f6SBaptiste Daroussin 			if (len == 0 && p == buf)
2321926f2f6SBaptiste Daroussin 				break;
2331926f2f6SBaptiste Daroussin 			end += len;
2341926f2f6SBaptiste Daroussin 			len = end - buf;
235e68bca50SDag-Erling Smørgrav 			if (len < size) {
2361926f2f6SBaptiste Daroussin 				eof = 1;
2371926f2f6SBaptiste Daroussin 				if (len > 0 && buf[len -1] != '\n')
2381926f2f6SBaptiste Daroussin 					++len, *end++ = '\n';
2391926f2f6SBaptiste Daroussin 			}
2401926f2f6SBaptiste Daroussin 			continue;
2411926f2f6SBaptiste Daroussin 		}
2421926f2f6SBaptiste Daroussin 
2431926f2f6SBaptiste Daroussin 		/* is it a blank line or a comment? */
2441926f2f6SBaptiste Daroussin 		for (r = p; r < q && isspace(*r); ++r)
2451926f2f6SBaptiste Daroussin 			/* nothing */;
2461926f2f6SBaptiste Daroussin 		if (r == q || *r == '#') {
2471926f2f6SBaptiste Daroussin 			/* yep */
2481926f2f6SBaptiste Daroussin 			if (write(tfd, p, q -p + 1) != q - p + 1)
2491926f2f6SBaptiste Daroussin 				goto err;
2501926f2f6SBaptiste Daroussin 			++q;
2511926f2f6SBaptiste Daroussin 			continue;
2521926f2f6SBaptiste Daroussin 		}
2531926f2f6SBaptiste Daroussin 
2541926f2f6SBaptiste Daroussin 		/* is it the one we're looking for? */
2551926f2f6SBaptiste Daroussin 
2561926f2f6SBaptiste Daroussin 		t = *q;
2571926f2f6SBaptiste Daroussin 		*q = '\0';
2581926f2f6SBaptiste Daroussin 
2591926f2f6SBaptiste Daroussin 		fgr = gr_scan(r);
2601926f2f6SBaptiste Daroussin 
2611926f2f6SBaptiste Daroussin 		/* fgr is either a struct group for the current line,
2621926f2f6SBaptiste Daroussin 		 * or NULL if the line is malformed.
2631926f2f6SBaptiste Daroussin 		 */
2641926f2f6SBaptiste Daroussin 
2651926f2f6SBaptiste Daroussin 		*q = t;
2661926f2f6SBaptiste Daroussin 		if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) {
2671926f2f6SBaptiste Daroussin 			/* nope */
2681926f2f6SBaptiste Daroussin 			if (fgr != NULL)
2691926f2f6SBaptiste Daroussin 				free(fgr);
2701926f2f6SBaptiste Daroussin 			if (write(tfd, p, q - p + 1) != q - p + 1)
2711926f2f6SBaptiste Daroussin 				goto err;
2721926f2f6SBaptiste Daroussin 			++q;
2731926f2f6SBaptiste Daroussin 			continue;
2741926f2f6SBaptiste Daroussin 		}
2751926f2f6SBaptiste Daroussin 		if (old_gr && !gr_equal(fgr, old_gr)) {
2761926f2f6SBaptiste Daroussin 			warnx("entry inconsistent");
2771926f2f6SBaptiste Daroussin 			free(fgr);
2781926f2f6SBaptiste Daroussin 			errno = EINVAL; /* hack */
2791926f2f6SBaptiste Daroussin 			goto err;
2801926f2f6SBaptiste Daroussin 		}
2811926f2f6SBaptiste Daroussin 		free(fgr);
2821926f2f6SBaptiste Daroussin 
2831926f2f6SBaptiste Daroussin 		/* it is, replace or remove it */
2841926f2f6SBaptiste Daroussin 		if (line != NULL) {
2851926f2f6SBaptiste Daroussin 			len = strlen(line);
2861926f2f6SBaptiste Daroussin 			if (write(tfd, line, len) != (int) len)
2871926f2f6SBaptiste Daroussin 				goto err;
2881926f2f6SBaptiste Daroussin 		} else {
2891926f2f6SBaptiste Daroussin 			/* when removed, avoid the \n */
2901926f2f6SBaptiste Daroussin 			q++;
2911926f2f6SBaptiste Daroussin 		}
2921926f2f6SBaptiste Daroussin 		/* we're done, just copy the rest over */
2931926f2f6SBaptiste Daroussin 		for (;;) {
2941926f2f6SBaptiste Daroussin 			if (write(tfd, q, end - q) != end - q)
2951926f2f6SBaptiste Daroussin 				goto err;
2961926f2f6SBaptiste Daroussin 			q = buf;
297e68bca50SDag-Erling Smørgrav 			readlen = read(ffd, buf, size);
2981926f2f6SBaptiste Daroussin 			if (readlen == 0)
2991926f2f6SBaptiste Daroussin 				break;
3001926f2f6SBaptiste Daroussin 			else
3011926f2f6SBaptiste Daroussin 				len = (size_t)readlen;
3021926f2f6SBaptiste Daroussin 			if (readlen == -1)
3031926f2f6SBaptiste Daroussin 				goto err;
3041926f2f6SBaptiste Daroussin 			end = buf + len;
3051926f2f6SBaptiste Daroussin 		}
3061926f2f6SBaptiste Daroussin 		goto done;
3071926f2f6SBaptiste Daroussin 	}
3081926f2f6SBaptiste Daroussin 
3091926f2f6SBaptiste Daroussin 	/* if we got here, we didn't find the old entry */
3101926f2f6SBaptiste Daroussin 	if (line == NULL) {
3111926f2f6SBaptiste Daroussin 		errno = ENOENT;
3121926f2f6SBaptiste Daroussin 		goto err;
3131926f2f6SBaptiste Daroussin 	}
3141926f2f6SBaptiste Daroussin 	len = strlen(line);
3151926f2f6SBaptiste Daroussin 	if ((size_t)write(tfd, line, len) != len ||
3161926f2f6SBaptiste Daroussin 	   write(tfd, "\n", 1) != 1)
3171926f2f6SBaptiste Daroussin 		goto err;
3181926f2f6SBaptiste Daroussin  done:
3191926f2f6SBaptiste Daroussin 	free(line);
320e68bca50SDag-Erling Smørgrav 	free(buf);
3211926f2f6SBaptiste Daroussin 	return (0);
3221926f2f6SBaptiste Daroussin  err:
3231926f2f6SBaptiste Daroussin 	free(line);
324e68bca50SDag-Erling Smørgrav 	free(buf);
3251926f2f6SBaptiste Daroussin 	return (-1);
3261926f2f6SBaptiste Daroussin }
3271926f2f6SBaptiste Daroussin 
3281926f2f6SBaptiste Daroussin /*
3291926f2f6SBaptiste Daroussin  * Regenerate the group file
3301926f2f6SBaptiste Daroussin  */
3311926f2f6SBaptiste Daroussin int
3321926f2f6SBaptiste Daroussin gr_mkdb(void)
3331926f2f6SBaptiste Daroussin {
334d32a66b2SRenato Botelho 	int fd;
335d32a66b2SRenato Botelho 
33609259e6cSBaptiste Daroussin 	if (chmod(tempname, 0644) != 0)
33709259e6cSBaptiste Daroussin 		return (-1);
3382d2b6ad7SBaptiste Daroussin 
339d32a66b2SRenato Botelho 	if (rename(tempname, group_file) != 0)
340d32a66b2SRenato Botelho 		return (-1);
341d32a66b2SRenato Botelho 
342d32a66b2SRenato Botelho 	/*
343d32a66b2SRenato Botelho 	 * Make sure new group file is safe on disk. To improve performance we
344d32a66b2SRenato Botelho 	 * will call fsync() to the directory where file lies
345d32a66b2SRenato Botelho 	 */
346d32a66b2SRenato Botelho 	if ((fd = open(group_dir, O_RDONLY|O_DIRECTORY)) == -1)
347d32a66b2SRenato Botelho 		return (-1);
348d32a66b2SRenato Botelho 
349d32a66b2SRenato Botelho 	if (fsync(fd) != 0) {
350d32a66b2SRenato Botelho 		close(fd);
351d32a66b2SRenato Botelho 		return (-1);
352d32a66b2SRenato Botelho 	}
353d32a66b2SRenato Botelho 
354d32a66b2SRenato Botelho 	close(fd);
355d32a66b2SRenato Botelho 	return(0);
3561926f2f6SBaptiste Daroussin }
3571926f2f6SBaptiste Daroussin 
3581926f2f6SBaptiste Daroussin /*
35908ecf0ccSMateusz Guzik  * Clean up. Preserves errno for the caller's convenience.
3601926f2f6SBaptiste Daroussin  */
3611926f2f6SBaptiste Daroussin void
3621926f2f6SBaptiste Daroussin gr_fini(void)
3631926f2f6SBaptiste Daroussin {
3641926f2f6SBaptiste Daroussin 	int serrno;
3651926f2f6SBaptiste Daroussin 
3661926f2f6SBaptiste Daroussin 	if (!initialized)
3671926f2f6SBaptiste Daroussin 		return;
3681926f2f6SBaptiste Daroussin 	initialized = 0;
3691926f2f6SBaptiste Daroussin 	serrno = errno;
3701926f2f6SBaptiste Daroussin 	if (*tempname != '\0') {
3711926f2f6SBaptiste Daroussin 		unlink(tempname);
3721926f2f6SBaptiste Daroussin 		*tempname = '\0';
3731926f2f6SBaptiste Daroussin 	}
3741926f2f6SBaptiste Daroussin 	if (lockfd != -1)
3751926f2f6SBaptiste Daroussin 		close(lockfd);
3761926f2f6SBaptiste Daroussin 	errno = serrno;
3771926f2f6SBaptiste Daroussin }
3781926f2f6SBaptiste Daroussin 
3791926f2f6SBaptiste Daroussin /*
3800b5e8899SSean Farley  * Compares two struct group's.
3810b5e8899SSean Farley  */
3820b5e8899SSean Farley int
3830b5e8899SSean Farley gr_equal(const struct group *gr1, const struct group *gr2)
3840b5e8899SSean Farley {
3850b5e8899SSean Farley 
3860b5e8899SSean Farley 	/* Check that the non-member information is the same. */
387805da51aSSean Farley 	if (gr1->gr_name == NULL || gr2->gr_name == NULL) {
388805da51aSSean Farley 		if (gr1->gr_name != gr2->gr_name)
389805da51aSSean Farley 			return (false);
390805da51aSSean Farley 	} else if (strcmp(gr1->gr_name, gr2->gr_name) != 0)
391805da51aSSean Farley 		return (false);
392805da51aSSean Farley 	if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) {
393805da51aSSean Farley 		if (gr1->gr_passwd != gr2->gr_passwd)
394805da51aSSean Farley 			return (false);
395805da51aSSean Farley 	} else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0)
396805da51aSSean Farley 		return (false);
397805da51aSSean Farley 	if (gr1->gr_gid != gr2->gr_gid)
398805da51aSSean Farley 		return (false);
3990b5e8899SSean Farley 
400f8c88390SMark Johnston 	/*
401f8c88390SMark Johnston 	 * Check all members in both groups.
4025e879837SDiane Bruce 	 * getgrnam can return gr_mem with a pointer to NULL.
4035e879837SDiane Bruce 	 * gr_dup and gr_add strip out this superfluous NULL, setting
4045e879837SDiane Bruce 	 * gr_mem to NULL for no members.
4055e879837SDiane Bruce 	*/
4065e879837SDiane Bruce 	if (gr1->gr_mem != NULL && gr2->gr_mem != NULL) {
4075e879837SDiane Bruce 		int i;
4080b5e8899SSean Farley 
409f8c88390SMark Johnston 		for (i = 0;
410f8c88390SMark Johnston 		    gr1->gr_mem[i] != NULL && gr2->gr_mem[i] != NULL; i++) {
4115e879837SDiane Bruce 			if (strcmp(gr1->gr_mem[i], gr2->gr_mem[i]) != 0)
412805da51aSSean Farley 				return (false);
4130b5e8899SSean Farley 		}
414f8c88390SMark Johnston 		if (gr1->gr_mem[i] != NULL || gr2->gr_mem[i] != NULL)
4155e879837SDiane Bruce 			return (false);
416f8c88390SMark Johnston 	} else if (gr1->gr_mem != NULL && gr1->gr_mem[0] != NULL) {
417f8c88390SMark Johnston 		return (false);
418f8c88390SMark Johnston 	} else if (gr2->gr_mem != NULL && gr2->gr_mem[0] != NULL) {
419f8c88390SMark Johnston 		return (false);
420f8c88390SMark Johnston 	}
4210b5e8899SSean Farley 
422805da51aSSean Farley 	return (true);
4230b5e8899SSean Farley }
4240b5e8899SSean Farley 
4250b5e8899SSean Farley /*
4260b5e8899SSean Farley  * Make a group line out of a struct group.
4270b5e8899SSean Farley  */
4280b5e8899SSean Farley char *
4290b5e8899SSean Farley gr_make(const struct group *gr)
4300b5e8899SSean Farley {
431fe75b0f0SMateusz Guzik 	const char *group_line_format = "%s:%s:%ju:";
43249013fb4SMateusz Guzik 	const char *sep;
4330b5e8899SSean Farley 	char *line;
43449013fb4SMateusz Guzik 	char *p;
435805da51aSSean Farley 	size_t line_size;
436a0671723SSean Farley 	int ndx;
4370b5e8899SSean Farley 
4380b5e8899SSean Farley 	/* Calculate the length of the group line. */
439805da51aSSean Farley 	line_size = snprintf(NULL, 0, group_line_format, gr->gr_name,
4400b5e8899SSean Farley 	    gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1;
441805da51aSSean Farley 	if (gr->gr_mem != NULL) {
4420b5e8899SSean Farley 		for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++)
443805da51aSSean Farley 			line_size += strlen(gr->gr_mem[ndx]) + 1;
4440b5e8899SSean Farley 		if (ndx > 0)
445805da51aSSean Farley 			line_size--;
446805da51aSSean Farley 	}
4470b5e8899SSean Farley 
4480b5e8899SSean Farley 	/* Create the group line and fill it. */
44949013fb4SMateusz Guzik 	if ((line = p = malloc(line_size)) == NULL)
4500b5e8899SSean Farley 		return (NULL);
45149013fb4SMateusz Guzik 	p += sprintf(p, group_line_format, gr->gr_name, gr->gr_passwd,
4520dd84a53SSean Farley 	    (uintmax_t)gr->gr_gid);
45349013fb4SMateusz Guzik 	if (gr->gr_mem != NULL) {
45449013fb4SMateusz Guzik 		sep = "";
4550b5e8899SSean Farley 		for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) {
45649013fb4SMateusz Guzik 			p = stpcpy(p, sep);
45749013fb4SMateusz Guzik 			p = stpcpy(p, gr->gr_mem[ndx]);
45849013fb4SMateusz Guzik 			sep = ",";
45949013fb4SMateusz Guzik 		}
4600b5e8899SSean Farley 	}
4610b5e8899SSean Farley 
4620b5e8899SSean Farley 	return (line);
4630b5e8899SSean Farley }
4640b5e8899SSean Farley 
4650b5e8899SSean Farley /*
4660b5e8899SSean Farley  * Duplicate a struct group.
4670b5e8899SSean Farley  */
4680b5e8899SSean Farley struct group *
4690b5e8899SSean Farley gr_dup(const struct group *gr)
4700b5e8899SSean Farley {
47186e2f99dSDiane Bruce 	return (gr_add(gr, NULL));
47286e2f99dSDiane Bruce }
47386e2f99dSDiane Bruce /*
47486e2f99dSDiane Bruce  * Add a new member name to a struct group.
47586e2f99dSDiane Bruce  */
47686e2f99dSDiane Bruce struct group *
47786e2f99dSDiane Bruce gr_add(const struct group *gr, const char *newmember)
47886e2f99dSDiane Bruce {
4795e879837SDiane Bruce 	char *mem;
4800b5e8899SSean Farley 	size_t len;
481805da51aSSean Farley 	int num_mem;
4820b5e8899SSean Farley 
48386e2f99dSDiane Bruce 	num_mem = 0;
48486e2f99dSDiane Bruce 	len = grmemlen(gr, newmember, &num_mem);
4850b5e8899SSean Farley 	/* Create new group and copy old group into it. */
4865e879837SDiane Bruce 	if ((mem = malloc(len)) == NULL)
4870b5e8899SSean Farley 		return (NULL);
4885e879837SDiane Bruce 	return (grcopy(gr, mem, newmember, num_mem));
48986e2f99dSDiane Bruce }
49086e2f99dSDiane Bruce 
49186e2f99dSDiane Bruce /* It is safer to walk the pointers given at gr_mem since there is no
4925e879837SDiane Bruce  * guarantee the gr_mem + strings are contiguous in the given struct group
4935e879837SDiane Bruce  * but compactify the new group into the following form.
49486e2f99dSDiane Bruce  *
49586e2f99dSDiane Bruce  * The new struct is laid out like this in memory. The example given is
49686e2f99dSDiane Bruce  * for a group with two members only.
49786e2f99dSDiane Bruce  *
49886e2f99dSDiane Bruce  * {
49986e2f99dSDiane Bruce  * (char *name)
50086e2f99dSDiane Bruce  * (char *passwd)
50186e2f99dSDiane Bruce  * (int gid)
50286e2f99dSDiane Bruce  * (gr_mem * newgrp + sizeof(struct group) + sizeof(**)) points to gr_mem area
50386e2f99dSDiane Bruce  * gr_mem area
50486e2f99dSDiane Bruce  * (member1 *)
50586e2f99dSDiane Bruce  * (member2 *)
50686e2f99dSDiane Bruce  * (NULL)
50786e2f99dSDiane Bruce  * (name string)
50886e2f99dSDiane Bruce  * (passwd string)
50986e2f99dSDiane Bruce  * (member1 string)
51086e2f99dSDiane Bruce  * (member2 string)
51186e2f99dSDiane Bruce  * }
51286e2f99dSDiane Bruce  */
51386e2f99dSDiane Bruce /*
5145e879837SDiane Bruce  * Copy the contents of a group plus given name to a preallocated group struct
51586e2f99dSDiane Bruce  */
51686e2f99dSDiane Bruce static struct group *
5175e879837SDiane Bruce grcopy(const struct group *gr, char *dst, const char *name, int ndx)
51886e2f99dSDiane Bruce {
51986e2f99dSDiane Bruce 	int i;
5205e879837SDiane Bruce 	struct group *newgr;
52186e2f99dSDiane Bruce 
5225e879837SDiane Bruce 	newgr = (struct group *)(void *)dst;	/* avoid alignment warning */
5235e879837SDiane Bruce 	dst += sizeof(*newgr);
5245e879837SDiane Bruce 	if (ndx != 0) {
5255e879837SDiane Bruce 		newgr->gr_mem = (char **)(void *)(dst);	/* avoid alignment warning */
5265e879837SDiane Bruce 		dst += (ndx + 1) * sizeof(*newgr->gr_mem);
5275e879837SDiane Bruce 	} else
5281067c64aSBaptiste Daroussin 		newgr->gr_mem = NULL;
5290b5e8899SSean Farley 	if (gr->gr_name != NULL) {
5301067c64aSBaptiste Daroussin 		newgr->gr_name = dst;
5311067c64aSBaptiste Daroussin 		dst = stpcpy(dst, gr->gr_name) + 1;
53286e2f99dSDiane Bruce 	} else
533167145a1SBaptiste Daroussin 		newgr->gr_name = NULL;
5340b5e8899SSean Farley 	if (gr->gr_passwd != NULL) {
5351067c64aSBaptiste Daroussin 		newgr->gr_passwd = dst;
5361067c64aSBaptiste Daroussin 		dst = stpcpy(dst, gr->gr_passwd) + 1;
53786e2f99dSDiane Bruce 	} else
538167145a1SBaptiste Daroussin 		newgr->gr_passwd = NULL;
5391067c64aSBaptiste Daroussin 	newgr->gr_gid = gr->gr_gid;
5405e879837SDiane Bruce 	i = 0;
5415e879837SDiane Bruce 	/* Original group struct might have a NULL gr_mem */
5425e879837SDiane Bruce 	if (gr->gr_mem != NULL) {
5435e879837SDiane Bruce 		for (; gr->gr_mem[i] != NULL; i++) {
54486e2f99dSDiane Bruce 			newgr->gr_mem[i] = dst;
54586e2f99dSDiane Bruce 			dst = stpcpy(dst, gr->gr_mem[i]) + 1;
5460b5e8899SSean Farley 		}
5475e879837SDiane Bruce 	}
5485e879837SDiane Bruce 	/* If name is not NULL, newgr->gr_mem is known to be not NULL */
54986e2f99dSDiane Bruce 	if (name != NULL) {
55086e2f99dSDiane Bruce 		newgr->gr_mem[i++] = dst;
55186e2f99dSDiane Bruce 		dst = stpcpy(dst, name) + 1;
55286e2f99dSDiane Bruce 	}
5535e879837SDiane Bruce 	/* if newgr->gr_mem is not NULL add NULL marker */
5545e879837SDiane Bruce 	if (newgr->gr_mem != NULL)
55586e2f99dSDiane Bruce 		newgr->gr_mem[i] = NULL;
5565e879837SDiane Bruce 
5571067c64aSBaptiste Daroussin 	return (newgr);
5580b5e8899SSean Farley }
5590b5e8899SSean Farley 
5600b5e8899SSean Farley /*
56186e2f99dSDiane Bruce  *  Calculate length of a struct group + given name
562be49c830SBaptiste Daroussin  */
56386e2f99dSDiane Bruce static size_t
56486e2f99dSDiane Bruce grmemlen(const struct group *gr, const char *name, int *num_mem)
565be49c830SBaptiste Daroussin {
56686e2f99dSDiane Bruce 	size_t len;
56786e2f99dSDiane Bruce 	int i;
568be49c830SBaptiste Daroussin 
56986e2f99dSDiane Bruce 	if (gr == NULL)
57086e2f99dSDiane Bruce 		return (0);
57186e2f99dSDiane Bruce 	/* Calculate size of the group. */
57286e2f99dSDiane Bruce 	len = sizeof(*gr);
57386e2f99dSDiane Bruce 	if (gr->gr_name != NULL)
57486e2f99dSDiane Bruce 		len += strlen(gr->gr_name) + 1;
57586e2f99dSDiane Bruce 	if (gr->gr_passwd != NULL)
57686e2f99dSDiane Bruce 		len += strlen(gr->gr_passwd) + 1;
5775e879837SDiane Bruce 	i = 0;
578be49c830SBaptiste Daroussin 	if (gr->gr_mem != NULL) {
5795e879837SDiane Bruce 		for (; gr->gr_mem[i] != NULL; i++) {
58086e2f99dSDiane Bruce 			len += strlen(gr->gr_mem[i]) + 1;
58186e2f99dSDiane Bruce 			len += sizeof(*gr->gr_mem);
582be49c830SBaptiste Daroussin 		}
583be49c830SBaptiste Daroussin 	}
58486e2f99dSDiane Bruce 	if (name != NULL) {
5855e879837SDiane Bruce 		i++;
58686e2f99dSDiane Bruce 		len += strlen(name) + 1;
58786e2f99dSDiane Bruce 		len += sizeof(*gr->gr_mem);
588be49c830SBaptiste Daroussin 	}
5895e879837SDiane Bruce 	/* Allow for NULL pointer */
5905e879837SDiane Bruce 	if (i != 0)
5915e879837SDiane Bruce 		len += sizeof(*gr->gr_mem);
5925e879837SDiane Bruce 	*num_mem = i;
59386e2f99dSDiane Bruce 	return(len);
594be49c830SBaptiste Daroussin }
595be49c830SBaptiste Daroussin 
596be49c830SBaptiste Daroussin /*
5970b5e8899SSean Farley  * Scan a line and place it into a group structure.
5980b5e8899SSean Farley  */
5990b5e8899SSean Farley static bool
6000b5e8899SSean Farley __gr_scan(char *line, struct group *gr)
6010b5e8899SSean Farley {
6020b5e8899SSean Farley 	char *loc;
6030b5e8899SSean Farley 	int ndx;
6040b5e8899SSean Farley 
6050b5e8899SSean Farley 	/* Assign non-member information to structure. */
6060b5e8899SSean Farley 	gr->gr_name = line;
6070b5e8899SSean Farley 	if ((loc = strchr(line, ':')) == NULL)
6080b5e8899SSean Farley 		return (false);
6090b5e8899SSean Farley 	*loc = '\0';
6100b5e8899SSean Farley 	gr->gr_passwd = loc + 1;
611a0671723SSean Farley 	if (*gr->gr_passwd == ':')
612a0671723SSean Farley 		*gr->gr_passwd = '\0';
6130b5e8899SSean Farley 	else {
6140b5e8899SSean Farley 		if ((loc = strchr(loc + 1, ':')) == NULL)
6150b5e8899SSean Farley 			return (false);
6160b5e8899SSean Farley 		*loc = '\0';
6170b5e8899SSean Farley 	}
618a0671723SSean Farley 	if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1)
6190b5e8899SSean Farley 		return (false);
6200b5e8899SSean Farley 
6210b5e8899SSean Farley 	/* Assign member information to structure. */
6220b5e8899SSean Farley 	if ((loc = strchr(loc + 1, ':')) == NULL)
6230b5e8899SSean Farley 		return (false);
6240b5e8899SSean Farley 	line = loc + 1;
6250b5e8899SSean Farley 	gr->gr_mem = NULL;
6260b5e8899SSean Farley 	ndx = 0;
6270b5e8899SSean Farley 	do {
628805da51aSSean Farley 		gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) *
629805da51aSSean Farley 		    (ndx + 1));
630805da51aSSean Farley 		if (gr->gr_mem == NULL)
6310b5e8899SSean Farley 			return (false);
632805da51aSSean Farley 
633805da51aSSean Farley 		/* Skip locations without members (i.e., empty string). */
634805da51aSSean Farley 		do {
6350b5e8899SSean Farley 			gr->gr_mem[ndx] = strsep(&line, ",");
636805da51aSSean Farley 		} while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0');
6370b5e8899SSean Farley 	} while (gr->gr_mem[ndx++] != NULL);
6380b5e8899SSean Farley 
6390b5e8899SSean Farley 	return (true);
6400b5e8899SSean Farley }
6410b5e8899SSean Farley 
6420b5e8899SSean Farley /*
6430b5e8899SSean Farley  * Create a struct group from a line.
6440b5e8899SSean Farley  */
6450b5e8899SSean Farley struct group *
6460b5e8899SSean Farley gr_scan(const char *line)
6470b5e8899SSean Farley {
648a0671723SSean Farley 	struct group gr;
649805da51aSSean Farley 	char *line_copy;
650805da51aSSean Farley 	struct group *new_gr;
6510b5e8899SSean Farley 
652805da51aSSean Farley 	if ((line_copy = strdup(line)) == NULL)
6530b5e8899SSean Farley 		return (NULL);
654805da51aSSean Farley 	if (!__gr_scan(line_copy, &gr)) {
655805da51aSSean Farley 		free(line_copy);
6560b5e8899SSean Farley 		return (NULL);
6570b5e8899SSean Farley 	}
658805da51aSSean Farley 	new_gr = gr_dup(&gr);
659805da51aSSean Farley 	free(line_copy);
6600b5e8899SSean Farley 	if (gr.gr_mem != NULL)
6610b5e8899SSean Farley 		free(gr.gr_mem);
6620b5e8899SSean Farley 
663805da51aSSean Farley 	return (new_gr);
6640b5e8899SSean Farley }
665