pw_group.c revision 285136
120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  All rights reserved.
420253Sjoerg *
520253Sjoerg * Redistribution and use in source and binary forms, with or without
620253Sjoerg * modification, are permitted provided that the following conditions
720253Sjoerg * are met:
820253Sjoerg * 1. Redistributions of source code must retain the above copyright
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2520253Sjoerg */
2620253Sjoerg
2730259Scharnier#ifndef lint
2830259Scharnierstatic const char rcsid[] =
2950479Speter  "$FreeBSD: head/usr.sbin/pw/pw_group.c 285136 2015-07-04 15:54:11Z bapt $";
3030259Scharnier#endif /* not lint */
3130259Scharnier
3220253Sjoerg#include <ctype.h>
3330259Scharnier#include <err.h>
3420253Sjoerg#include <termios.h>
35176474Sscf#include <stdbool.h>
3630259Scharnier#include <unistd.h>
37242349Sbapt#include <grp.h>
38242349Sbapt#include <libutil.h>
3920253Sjoerg
4020253Sjoerg#include "pw.h"
4120253Sjoerg#include "bitmap.h"
4220253Sjoerg
4320253Sjoerg
44176474Sscfstatic struct passwd *lookup_pwent(const char *user);
45176474Sscfstatic void	delete_members(char ***members, int *grmembers, int *i,
46176474Sscf    struct carg *arg, struct group *grp);
47284122Sbaptstatic int	print_group(struct group * grp);
48284133Sbaptstatic gid_t    gr_gidpolicy(struct userconf * cnf, long id);
4920253Sjoerg
50285136Sbaptstatic void
51285136Sbaptset_passwd(struct group *grp, bool update)
52285136Sbapt{
53285136Sbapt	int		 b;
54285136Sbapt	int		 istty;
55285136Sbapt	struct termios	 t, n;
56285136Sbapt	char		*p, line[256];
57285136Sbapt
58285136Sbapt	if (conf.fd == '-') {
59285136Sbapt		grp->gr_passwd = "*";	/* No access */
60285136Sbapt		return;
61285136Sbapt	}
62285136Sbapt
63285136Sbapt	if ((istty = isatty(conf.fd))) {
64285136Sbapt		n = t;
65285136Sbapt		/* Disable echo */
66285136Sbapt		n.c_lflag &= ~(ECHO);
67285136Sbapt		tcsetattr(conf.fd, TCSANOW, &n);
68285136Sbapt		printf("%sassword for group %s:", update ? "New p" : "P",
69285136Sbapt		    grp->gr_name);
70285136Sbapt		fflush(stdout);
71285136Sbapt	}
72285136Sbapt	b = read(conf.fd, line, sizeof(line) - 1);
73285136Sbapt	if (istty) {	/* Restore state */
74285136Sbapt		tcsetattr(conf.fd, TCSANOW, &t);
75285136Sbapt		fputc('\n', stdout);
76285136Sbapt		fflush(stdout);
77285136Sbapt	}
78285136Sbapt	if (b < 0)
79285136Sbapt		err(EX_OSERR, "-h file descriptor");
80285136Sbapt	line[b] = '\0';
81285136Sbapt	if ((p = strpbrk(line, " \t\r\n")) != NULL)
82285136Sbapt		*p = '\0';
83285136Sbapt	if (!*line)
84285136Sbapt		errx(EX_DATAERR, "empty password read on file descriptor %d",
85285136Sbapt		    conf.fd);
86285136Sbapt	if (conf.precrypted) {
87285136Sbapt		if (strchr(line, ':') != 0)
88285136Sbapt			errx(EX_DATAERR, "wrong encrypted passwrd");
89285136Sbapt		grp->gr_passwd = line;
90285136Sbapt	} else
91285136Sbapt		grp->gr_passwd = pw_pwcrypt(line);
92285136Sbapt}
93285136Sbapt
9420253Sjoergint
95284128Sbaptpw_group(int mode, char *name, long id, struct cargs * args)
9620253Sjoerg{
9752502Sdavidn	int		rc;
9820253Sjoerg	struct carg    *arg;
9920253Sjoerg	struct group   *grp = NULL;
10020747Sdavidn	int	        grmembers = 0;
10152502Sdavidn	char           **members = NULL;
102284118Sbapt	struct userconf	*cnf = conf.userconf;
10320253Sjoerg
10420253Sjoerg	static struct group fakegroup =
10520253Sjoerg	{
10620253Sjoerg		"nogroup",
10720253Sjoerg		"*",
10820253Sjoerg		-1,
10920253Sjoerg		NULL
11020253Sjoerg	};
11120253Sjoerg
11252512Sdavidn	if (mode == M_LOCK || mode == M_UNLOCK)
11352512Sdavidn		errx(EX_USAGE, "'lock' command is not available for groups");
11452512Sdavidn
11520267Sjoerg	/*
11620267Sjoerg	 * With M_NEXT, we only need to return the
11720267Sjoerg	 * next gid to stdout
11820267Sjoerg	 */
11952512Sdavidn	if (mode == M_NEXT) {
120284149Sbapt		gid_t next = gr_gidpolicy(cnf, id);
121284149Sbapt		if (getarg(args, 'q'))
122284149Sbapt			return next;
123284149Sbapt		printf("%u\n", next);
124284149Sbapt		return EXIT_SUCCESS;
12520267Sjoerg	}
12620267Sjoerg
12720253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
12844229Sdavidn		SETGRENT();
12944229Sdavidn		while ((grp = GETGRENT()) != NULL)
130284122Sbapt			print_group(grp);
13144229Sdavidn		ENDGRENT();
13220267Sjoerg		return EXIT_SUCCESS;
13320253Sjoerg	}
134284128Sbapt	if (id < 0 && name == NULL)
135284128Sbapt		errx(EX_DATAERR, "group name or id required");
13620253Sjoerg
137284128Sbapt	grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
13820253Sjoerg
13920253Sjoerg	if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
140284128Sbapt		if (name == NULL && grp == NULL)	/* Try harder */
141284128Sbapt			grp = GETGRGID(id);
14220253Sjoerg
14320253Sjoerg		if (grp == NULL) {
14420253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
14520747Sdavidn				char	*fmems[1];
14620747Sdavidn				fmems[0] = NULL;
147284128Sbapt				fakegroup.gr_name = name ? name : "nogroup";
148284128Sbapt				fakegroup.gr_gid = (gid_t) id;
14920747Sdavidn				fakegroup.gr_mem = fmems;
150284122Sbapt				return print_group(&fakegroup);
15120253Sjoerg			}
152284128Sbapt			if (name == NULL)
153284128Sbapt				errx(EX_DATAERR, "unknown group `%s'", name);
154284128Sbapt			else
155284128Sbapt				errx(EX_DATAERR, "unknown group `%ld'", id);
15620253Sjoerg		}
157284128Sbapt		if (name == NULL)	/* Needed later */
158284128Sbapt			name = grp->gr_name;
15920253Sjoerg
16020253Sjoerg		/*
16120253Sjoerg		 * Handle deletions now
16220253Sjoerg		 */
16320253Sjoerg		if (mode == M_DELETE) {
16452502Sdavidn			rc = delgrent(grp);
16552502Sdavidn			if (rc == -1)
166284128Sbapt				err(EX_IOERR, "group '%s' not available (NIS?)",
167284128Sbapt				    name);
16852502Sdavidn			else if (rc != 0) {
169283814Sbapt				err(EX_IOERR, "group update");
17052502Sdavidn			}
171284128Sbapt			pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", name, id);
17220267Sjoerg			return EXIT_SUCCESS;
17320253Sjoerg		} else if (mode == M_PRINT)
174284122Sbapt			return print_group(grp);
17520253Sjoerg
176284128Sbapt		if (id > 0)
177284128Sbapt			grp->gr_gid = (gid_t) id;
17820253Sjoerg
179284129Sbapt		if (conf.newname != NULL)
180284129Sbapt			grp->gr_name = pw_checkname(conf.newname, 0);
18120253Sjoerg	} else {
182284128Sbapt		if (name == NULL)	/* Required */
18330259Scharnier			errx(EX_DATAERR, "group name required");
18420253Sjoerg		else if (grp != NULL)	/* Exists */
185284128Sbapt			errx(EX_DATAERR, "group name `%s' already exists", name);
18620253Sjoerg
18720747Sdavidn		extendarray(&members, &grmembers, 200);
18820747Sdavidn		members[0] = NULL;
18920253Sjoerg		grp = &fakegroup;
190284128Sbapt		grp->gr_name = pw_checkname(name, 0);
19120253Sjoerg		grp->gr_passwd = "*";
192284133Sbapt		grp->gr_gid = gr_gidpolicy(cnf, id);
19320253Sjoerg		grp->gr_mem = members;
19420253Sjoerg	}
19520253Sjoerg
19620253Sjoerg	/*
19720253Sjoerg	 * This allows us to set a group password Group passwords is an
19820253Sjoerg	 * antique idea, rarely used and insecure (no secure database) Should
19920253Sjoerg	 * be discouraged, but it is apparently still supported by some
20020253Sjoerg	 * software.
20120253Sjoerg	 */
20220253Sjoerg
203285136Sbapt	if (conf.fd != -1)
204285136Sbapt		set_passwd(grp, mode == M_UPDATE);
20520253Sjoerg
206176474Sscf	if (((arg = getarg(args, 'M')) != NULL ||
207176474Sscf	    (arg = getarg(args, 'd')) != NULL ||
208176474Sscf	    (arg = getarg(args, 'm')) != NULL) && arg->val) {
20920267Sjoerg		int	i = 0;
21020267Sjoerg		char   *p;
21120267Sjoerg		struct passwd	*pwd;
21220267Sjoerg
21320747Sdavidn		/* Make sure this is not stay NULL with -M "" */
21420747Sdavidn		extendarray(&members, &grmembers, 200);
215176474Sscf		if (arg->ch == 'd')
216176474Sscf			delete_members(&members, &grmembers, &i, arg, grp);
217176474Sscf		else if (arg->ch == 'm') {
21820747Sdavidn			int	k = 0;
21920747Sdavidn
220262864Sjulian			if (grp->gr_mem != NULL) {
221262864Sjulian				while (grp->gr_mem[k] != NULL) {
222262864Sjulian					if (extendarray(&members, &grmembers, i + 2) != -1)
223262864Sjulian						members[i++] = grp->gr_mem[k];
224262864Sjulian					k++;
225262864Sjulian				}
22620267Sjoerg			}
22720267Sjoerg		}
228176474Sscf
229176474Sscf		if (arg->ch != 'd')
230176474Sscf			for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
231176474Sscf				int	j;
232176474Sscf
233176474Sscf				/*
234176474Sscf				 * Check for duplicates
235176474Sscf				 */
236176474Sscf				pwd = lookup_pwent(p);
237176474Sscf				for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++)
238176474Sscf					;
239176474Sscf				if (j == i && extendarray(&members, &grmembers, i + 2) != -1)
240176474Sscf					members[i++] = newstr(pwd->pw_name);
24120267Sjoerg			}
24220747Sdavidn		while (i < grmembers)
24320267Sjoerg			members[i++] = NULL;
24420267Sjoerg		grp->gr_mem = members;
24520267Sjoerg	}
24620267Sjoerg
247284121Sbapt	if (conf.dryrun)
248284122Sbapt		return print_group(grp);
24920267Sjoerg
25052502Sdavidn	if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
25152502Sdavidn		if (rc == -1)
252283814Sbapt			errx(EX_IOERR, "group '%s' already exists",
253283814Sbapt			    grp->gr_name);
25452502Sdavidn		else
255283814Sbapt			err(EX_IOERR, "group update");
256284128Sbapt	} else if (mode == M_UPDATE && (rc = chggrent(name, grp)) != 0) {
25752502Sdavidn		if (rc == -1)
258283814Sbapt			errx(EX_IOERR, "group '%s' not available (NIS?)",
259283814Sbapt			    grp->gr_name);
26052502Sdavidn		else
261283814Sbapt			err(EX_IOERR, "group update");
26220253Sjoerg	}
263273772Sbapt
264284129Sbapt	if (conf.newname != NULL)
265284129Sbapt		name = conf.newname;
26620253Sjoerg	/* grp may have been invalidated */
267284128Sbapt	if ((grp = GETGRNAM(name)) == NULL)
26830259Scharnier		errx(EX_SOFTWARE, "group disappeared during update");
26920253Sjoerg
270283842Sbapt	pw_log(cnf, mode, W_GROUP, "%s(%u)", grp->gr_name, grp->gr_gid);
27120253Sjoerg
272243894Seadler	free(members);
27320747Sdavidn
27420267Sjoerg	return EXIT_SUCCESS;
27520253Sjoerg}
27620253Sjoerg
27720253Sjoerg
278176474Sscf/*
279176474Sscf * Lookup a passwd entry using a name or UID.
280176474Sscf */
281176474Sscfstatic struct passwd *
282176474Sscflookup_pwent(const char *user)
283176474Sscf{
284176474Sscf	struct passwd *pwd;
285176474Sscf
286176474Sscf	if ((pwd = GETPWNAM(user)) == NULL &&
287176474Sscf	    (!isdigit((unsigned char)*user) ||
288176474Sscf	    (pwd = getpwuid((uid_t) atoi(user))) == NULL))
289176474Sscf		errx(EX_NOUSER, "user `%s' does not exist", user);
290176474Sscf
291176474Sscf	return (pwd);
292176474Sscf}
293176474Sscf
294176474Sscf
295176474Sscf/*
296176474Sscf * Delete requested members from a group.
297176474Sscf */
298176474Sscfstatic void
299176474Sscfdelete_members(char ***members, int *grmembers, int *i, struct carg *arg,
300176474Sscf    struct group *grp)
301176474Sscf{
302176474Sscf	bool matchFound;
303176474Sscf	char *user;
304176474Sscf	char *valueCopy;
305176474Sscf	char *valuePtr;
306176474Sscf	int k;
307176474Sscf	struct passwd *pwd;
308176474Sscf
309262864Sjulian	if (grp->gr_mem == NULL)
310262864Sjulian		return;
311262864Sjulian
312176474Sscf	k = 0;
313176474Sscf	while (grp->gr_mem[k] != NULL) {
314176474Sscf		matchFound = false;
315176474Sscf		if ((valueCopy = strdup(arg->val)) == NULL)
316176474Sscf			errx(EX_UNAVAILABLE, "out of memory");
317176474Sscf		valuePtr = valueCopy;
318176474Sscf		while ((user = strsep(&valuePtr, ", \t")) != NULL) {
319176474Sscf			pwd = lookup_pwent(user);
320176474Sscf			if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) {
321176474Sscf				matchFound = true;
322176474Sscf				break;
323176474Sscf			}
324176474Sscf		}
325176474Sscf		free(valueCopy);
326176474Sscf
327176474Sscf		if (!matchFound &&
328176474Sscf		    extendarray(members, grmembers, *i + 2) != -1)
329176474Sscf			(*members)[(*i)++] = grp->gr_mem[k];
330176474Sscf
331176474Sscf		k++;
332176474Sscf	}
333176474Sscf
334176474Sscf	return;
335176474Sscf}
336176474Sscf
337176474Sscf
33820253Sjoergstatic          gid_t
339284133Sbaptgr_gidpolicy(struct userconf * cnf, long id)
34020253Sjoerg{
34120253Sjoerg	struct group   *grp;
34220253Sjoerg	gid_t           gid = (gid_t) - 1;
34320253Sjoerg
34420253Sjoerg	/*
34520253Sjoerg	 * Check the given gid, if any
34620253Sjoerg	 */
347284133Sbapt	if (id > 0) {
348284133Sbapt		gid = (gid_t) id;
34920253Sjoerg
350284133Sbapt		if ((grp = GETGRGID(gid)) != NULL && conf.checkduplicate)
351283842Sbapt			errx(EX_DATAERR, "gid `%u' has already been allocated", grp->gr_gid);
35220253Sjoerg	} else {
35320253Sjoerg		struct bitmap   bm;
35420253Sjoerg
35520253Sjoerg		/*
35620253Sjoerg		 * We need to allocate the next available gid under one of
35720253Sjoerg		 * two policies a) Grab the first unused gid b) Grab the
35820253Sjoerg		 * highest possible unused gid
35920253Sjoerg		 */
36020253Sjoerg		if (cnf->min_gid >= cnf->max_gid) {	/* Sanity claus^H^H^H^Hheck */
36120253Sjoerg			cnf->min_gid = 1000;
36220253Sjoerg			cnf->max_gid = 32000;
36320253Sjoerg		}
36420253Sjoerg		bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1);
36520253Sjoerg
36620253Sjoerg		/*
36720253Sjoerg		 * Now, let's fill the bitmap from the password file
36820253Sjoerg		 */
36944229Sdavidn		SETGRENT();
37044229Sdavidn		while ((grp = GETGRENT()) != NULL)
37156000Sdavidn			if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid &&
37256000Sdavidn                            (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid)
37320253Sjoerg				bm_setbit(&bm, grp->gr_gid - cnf->min_gid);
37444229Sdavidn		ENDGRENT();
37520253Sjoerg
37620253Sjoerg		/*
37720253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
37820253Sjoerg		 */
37920253Sjoerg		if (cnf->reuse_gids)
38020253Sjoerg			gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
38120253Sjoerg		else {
38220253Sjoerg			gid = (gid_t) (bm_lastset(&bm) + 1);
38320253Sjoerg			if (!bm_isset(&bm, gid))
38420253Sjoerg				gid += cnf->min_gid;
38520253Sjoerg			else
38620253Sjoerg				gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
38720253Sjoerg		}
38820253Sjoerg
38920253Sjoerg		/*
39020253Sjoerg		 * Another sanity check
39120253Sjoerg		 */
39220253Sjoerg		if (gid < cnf->min_gid || gid > cnf->max_gid)
39330259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used");
39420253Sjoerg		bm_dealloc(&bm);
39520253Sjoerg	}
39620253Sjoerg	return gid;
39720253Sjoerg}
39820253Sjoerg
39920253Sjoerg
40020253Sjoergstatic int
401284122Sbaptprint_group(struct group * grp)
40220253Sjoerg{
403284122Sbapt	if (!conf.pretty) {
40420747Sdavidn		char           *buf = NULL;
40520253Sjoerg
406242349Sbapt		buf = gr_make(grp);
407244738Sbapt		printf("%s\n", buf);
40820747Sdavidn		free(buf);
40920253Sjoerg	} else {
41020253Sjoerg		int             i;
41120253Sjoerg
41222398Sdavidn		printf("Group Name: %-15s   #%lu\n"
41320747Sdavidn		       "   Members: ",
41420253Sjoerg		       grp->gr_name, (long) grp->gr_gid);
415262864Sjulian		if (grp->gr_mem != NULL) {
416262864Sjulian			for (i = 0; grp->gr_mem[i]; i++)
417262864Sjulian				printf("%s%s", i ? "," : "", grp->gr_mem[i]);
418262864Sjulian		}
41920253Sjoerg		fputs("\n\n", stdout);
42020253Sjoerg	}
42120267Sjoerg	return EXIT_SUCCESS;
42220253Sjoerg}
423