pw_group.c revision 286150
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 286150 2015-08-01 09:55:47Z bapt $";
3030259Scharnier#endif /* not lint */
3130259Scharnier
3220253Sjoerg#include <ctype.h>
3330259Scharnier#include <err.h>
34286150Sbapt#include <inttypes.h>
3520253Sjoerg#include <termios.h>
36176474Sscf#include <stdbool.h>
3730259Scharnier#include <unistd.h>
38242349Sbapt#include <grp.h>
39242349Sbapt#include <libutil.h>
4020253Sjoerg
4120253Sjoerg#include "pw.h"
4220253Sjoerg#include "bitmap.h"
4320253Sjoerg
4420253Sjoerg
45176474Sscfstatic struct passwd *lookup_pwent(const char *user);
46285411Sbaptstatic void	delete_members(struct group *grp, char *list);
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
95285395Sbaptpw_groupnext(struct userconf *cnf, bool quiet)
96285395Sbapt{
97285395Sbapt	gid_t next = gr_gidpolicy(cnf, -1);
98285395Sbapt
99285395Sbapt	if (quiet)
100285395Sbapt		return (next);
101286150Sbapt	printf("%ju\n", (uintmax_t)next);
102285395Sbapt
103285395Sbapt	return (EXIT_SUCCESS);
104285395Sbapt}
105285395Sbapt
106285398Sbaptstatic int
107285398Sbaptpw_groupshow(const char *name, long id, struct group *fakegroup)
108285398Sbapt{
109285398Sbapt	struct group *grp = NULL;
110285398Sbapt
111285398Sbapt	if (id < 0 && name == NULL && !conf.all)
112285398Sbapt		errx(EX_DATAERR, "groupname or id or '-a' required");
113285398Sbapt
114285398Sbapt	if (conf.all) {
115285398Sbapt		SETGRENT();
116285398Sbapt		while ((grp = GETGRENT()) != NULL)
117285398Sbapt			print_group(grp);
118285398Sbapt		ENDGRENT();
119285398Sbapt
120285398Sbapt		return (EXIT_SUCCESS);
121285398Sbapt	}
122285398Sbapt
123285398Sbapt	grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
124285398Sbapt	if (grp == NULL) {
125285398Sbapt		if (conf.force) {
126285398Sbapt			grp = fakegroup;
127285398Sbapt		} else {
128285398Sbapt			if (name == NULL)
129285398Sbapt				errx(EX_DATAERR, "unknown gid `%ld'", id);
130285398Sbapt			errx(EX_DATAERR, "unknown group `%s'", name);
131285398Sbapt		}
132285398Sbapt	}
133285398Sbapt
134285398Sbapt	return (print_group(grp));
135285398Sbapt}
136285398Sbapt
137285401Sbaptstatic int
138285401Sbaptpw_groupdel(const char *name, long id)
139285401Sbapt{
140285401Sbapt	struct group *grp = NULL;
141285401Sbapt	int rc;
142285401Sbapt
143285401Sbapt	grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
144285401Sbapt	if (grp == NULL) {
145285401Sbapt		if (name == NULL)
146285401Sbapt			errx(EX_DATAERR, "unknown gid `%ld'", id);
147285401Sbapt		errx(EX_DATAERR, "unknown group `%s'", name);
148285401Sbapt	}
149285401Sbapt
150285401Sbapt	rc = delgrent(grp);
151285401Sbapt	if (rc == -1)
152285401Sbapt		err(EX_IOERR, "group '%s' not available (NIS?)", name);
153285401Sbapt	else if (rc != 0)
154285401Sbapt		err(EX_IOERR, "group update");
155285401Sbapt	pw_log(conf.userconf, M_DELETE, W_GROUP, "%s(%ld) removed", name, id);
156285401Sbapt
157285401Sbapt	return (EXIT_SUCCESS);
158285401Sbapt}
159285401Sbapt
160285395Sbaptint
161284128Sbaptpw_group(int mode, char *name, long id, struct cargs * args)
16220253Sjoerg{
16352502Sdavidn	int		rc;
16420253Sjoerg	struct carg    *arg;
16520253Sjoerg	struct group   *grp = NULL;
166284118Sbapt	struct userconf	*cnf = conf.userconf;
16720253Sjoerg
16820253Sjoerg	static struct group fakegroup =
16920253Sjoerg	{
17020253Sjoerg		"nogroup",
17120253Sjoerg		"*",
17220253Sjoerg		-1,
17320253Sjoerg		NULL
17420253Sjoerg	};
17520253Sjoerg
176285395Sbapt	if (mode == M_NEXT)
177285396Sbapt		return (pw_groupnext(cnf, conf.quiet));
178285395Sbapt
179285398Sbapt	if (mode == M_PRINT)
180285398Sbapt		return (pw_groupshow(name, id, &fakegroup));
181285398Sbapt
182285401Sbapt	if (mode == M_DELETE)
183285401Sbapt		return (pw_groupdel(name, id));
184285401Sbapt
18552512Sdavidn	if (mode == M_LOCK || mode == M_UNLOCK)
18652512Sdavidn		errx(EX_USAGE, "'lock' command is not available for groups");
18752512Sdavidn
188284128Sbapt	if (id < 0 && name == NULL)
189284128Sbapt		errx(EX_DATAERR, "group name or id required");
19020253Sjoerg
191284128Sbapt	grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
19220253Sjoerg
193285401Sbapt	if (mode == M_UPDATE) {
194284128Sbapt		if (name == NULL && grp == NULL)	/* Try harder */
195284128Sbapt			grp = GETGRGID(id);
19620253Sjoerg
19720253Sjoerg		if (grp == NULL) {
198284128Sbapt			if (name == NULL)
199284128Sbapt				errx(EX_DATAERR, "unknown group `%s'", name);
200284128Sbapt			else
201284128Sbapt				errx(EX_DATAERR, "unknown group `%ld'", id);
20220253Sjoerg		}
203284128Sbapt		if (name == NULL)	/* Needed later */
204284128Sbapt			name = grp->gr_name;
20520253Sjoerg
206284128Sbapt		if (id > 0)
207284128Sbapt			grp->gr_gid = (gid_t) id;
20820253Sjoerg
209284129Sbapt		if (conf.newname != NULL)
210284129Sbapt			grp->gr_name = pw_checkname(conf.newname, 0);
21120253Sjoerg	} else {
212284128Sbapt		if (name == NULL)	/* Required */
21330259Scharnier			errx(EX_DATAERR, "group name required");
21420253Sjoerg		else if (grp != NULL)	/* Exists */
215284128Sbapt			errx(EX_DATAERR, "group name `%s' already exists", name);
21620253Sjoerg
21720253Sjoerg		grp = &fakegroup;
218284128Sbapt		grp->gr_name = pw_checkname(name, 0);
21920253Sjoerg		grp->gr_passwd = "*";
220284133Sbapt		grp->gr_gid = gr_gidpolicy(cnf, id);
221285411Sbapt		grp->gr_mem = NULL;
22220253Sjoerg	}
22320253Sjoerg
22420253Sjoerg	/*
22520253Sjoerg	 * This allows us to set a group password Group passwords is an
22620253Sjoerg	 * antique idea, rarely used and insecure (no secure database) Should
22720253Sjoerg	 * be discouraged, but it is apparently still supported by some
22820253Sjoerg	 * software.
22920253Sjoerg	 */
23020253Sjoerg
231285318Sbapt	if (conf.which == W_GROUP && conf.fd != -1)
232285136Sbapt		set_passwd(grp, mode == M_UPDATE);
23320253Sjoerg
234176474Sscf	if (((arg = getarg(args, 'M')) != NULL ||
235176474Sscf	    (arg = getarg(args, 'd')) != NULL ||
236176474Sscf	    (arg = getarg(args, 'm')) != NULL) && arg->val) {
23720267Sjoerg		char   *p;
23820267Sjoerg		struct passwd	*pwd;
23920267Sjoerg
24020747Sdavidn		/* Make sure this is not stay NULL with -M "" */
241176474Sscf		if (arg->ch == 'd')
242285411Sbapt			delete_members(grp, arg->val);
243285411Sbapt		else if (arg->ch == 'M')
244285411Sbapt			grp->gr_mem = NULL;
24520747Sdavidn
246285411Sbapt		for (p = strtok(arg->val, ", \t"); arg->ch != 'd' &&  p != NULL;
247285411Sbapt		    p = strtok(NULL, ", \t")) {
248285411Sbapt			int	j;
249285411Sbapt
250285411Sbapt			/*
251285411Sbapt			 * Check for duplicates
252285411Sbapt			 */
253285411Sbapt			pwd = lookup_pwent(p);
254285411Sbapt			for (j = 0; grp->gr_mem != NULL && grp->gr_mem[j] != NULL; j++) {
255285411Sbapt				if (strcmp(grp->gr_mem[j], pwd->pw_name) == 0)
256285411Sbapt					break;
25720267Sjoerg			}
258285411Sbapt			if (grp->gr_mem != NULL && grp->gr_mem[j] != NULL)
259285411Sbapt				continue;
260285411Sbapt			grp = gr_add(grp, pwd->pw_name);
26120267Sjoerg		}
26220267Sjoerg	}
26320267Sjoerg
264284121Sbapt	if (conf.dryrun)
265284122Sbapt		return print_group(grp);
26620267Sjoerg
26752502Sdavidn	if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
26852502Sdavidn		if (rc == -1)
269283814Sbapt			errx(EX_IOERR, "group '%s' already exists",
270283814Sbapt			    grp->gr_name);
27152502Sdavidn		else
272283814Sbapt			err(EX_IOERR, "group update");
273284128Sbapt	} else if (mode == M_UPDATE && (rc = chggrent(name, grp)) != 0) {
27452502Sdavidn		if (rc == -1)
275283814Sbapt			errx(EX_IOERR, "group '%s' not available (NIS?)",
276283814Sbapt			    grp->gr_name);
27752502Sdavidn		else
278283814Sbapt			err(EX_IOERR, "group update");
27920253Sjoerg	}
280273772Sbapt
281284129Sbapt	if (conf.newname != NULL)
282284129Sbapt		name = conf.newname;
28320253Sjoerg	/* grp may have been invalidated */
284284128Sbapt	if ((grp = GETGRNAM(name)) == NULL)
28530259Scharnier		errx(EX_SOFTWARE, "group disappeared during update");
28620253Sjoerg
287286150Sbapt	pw_log(cnf, mode, W_GROUP, "%s(%ju)", grp->gr_name, (uintmax_t)grp->gr_gid);
28820253Sjoerg
28920267Sjoerg	return EXIT_SUCCESS;
29020253Sjoerg}
29120253Sjoerg
29220253Sjoerg
293176474Sscf/*
294176474Sscf * Lookup a passwd entry using a name or UID.
295176474Sscf */
296176474Sscfstatic struct passwd *
297176474Sscflookup_pwent(const char *user)
298176474Sscf{
299176474Sscf	struct passwd *pwd;
300176474Sscf
301176474Sscf	if ((pwd = GETPWNAM(user)) == NULL &&
302176474Sscf	    (!isdigit((unsigned char)*user) ||
303176474Sscf	    (pwd = getpwuid((uid_t) atoi(user))) == NULL))
304176474Sscf		errx(EX_NOUSER, "user `%s' does not exist", user);
305176474Sscf
306176474Sscf	return (pwd);
307176474Sscf}
308176474Sscf
309176474Sscf
310176474Sscf/*
311176474Sscf * Delete requested members from a group.
312176474Sscf */
313176474Sscfstatic void
314285411Sbaptdelete_members(struct group *grp, char *list)
315176474Sscf{
316285411Sbapt	char *p;
317176474Sscf	int k;
318176474Sscf
319262864Sjulian	if (grp->gr_mem == NULL)
320262864Sjulian		return;
321262864Sjulian
322285411Sbapt	for (p = strtok(list, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
323285411Sbapt		for (k = 0; grp->gr_mem[k] != NULL; k++) {
324285411Sbapt			if (strcmp(grp->gr_mem[k], p) == 0)
325176474Sscf				break;
326176474Sscf		}
327285411Sbapt		if (grp->gr_mem[k] == NULL) /* No match */
328285411Sbapt			continue;
329176474Sscf
330285411Sbapt		for (; grp->gr_mem[k] != NULL; k++)
331285411Sbapt			grp->gr_mem[k] = grp->gr_mem[k+1];
332176474Sscf	}
333176474Sscf}
334176474Sscf
335176474Sscf
33620253Sjoergstatic          gid_t
337284133Sbaptgr_gidpolicy(struct userconf * cnf, long id)
33820253Sjoerg{
33920253Sjoerg	struct group   *grp;
34020253Sjoerg	gid_t           gid = (gid_t) - 1;
34120253Sjoerg
34220253Sjoerg	/*
34320253Sjoerg	 * Check the given gid, if any
34420253Sjoerg	 */
345284133Sbapt	if (id > 0) {
346284133Sbapt		gid = (gid_t) id;
34720253Sjoerg
348284133Sbapt		if ((grp = GETGRGID(gid)) != NULL && conf.checkduplicate)
349286150Sbapt			errx(EX_DATAERR, "gid `%ju' has already been allocated", (uintmax_t)grp->gr_gid);
35020253Sjoerg	} else {
35120253Sjoerg		struct bitmap   bm;
35220253Sjoerg
35320253Sjoerg		/*
35420253Sjoerg		 * We need to allocate the next available gid under one of
35520253Sjoerg		 * two policies a) Grab the first unused gid b) Grab the
35620253Sjoerg		 * highest possible unused gid
35720253Sjoerg		 */
35820253Sjoerg		if (cnf->min_gid >= cnf->max_gid) {	/* Sanity claus^H^H^H^Hheck */
35920253Sjoerg			cnf->min_gid = 1000;
36020253Sjoerg			cnf->max_gid = 32000;
36120253Sjoerg		}
36220253Sjoerg		bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1);
36320253Sjoerg
36420253Sjoerg		/*
36520253Sjoerg		 * Now, let's fill the bitmap from the password file
36620253Sjoerg		 */
36744229Sdavidn		SETGRENT();
36844229Sdavidn		while ((grp = GETGRENT()) != NULL)
36956000Sdavidn			if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid &&
37056000Sdavidn                            (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid)
37120253Sjoerg				bm_setbit(&bm, grp->gr_gid - cnf->min_gid);
37244229Sdavidn		ENDGRENT();
37320253Sjoerg
37420253Sjoerg		/*
37520253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
37620253Sjoerg		 */
37720253Sjoerg		if (cnf->reuse_gids)
37820253Sjoerg			gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
37920253Sjoerg		else {
38020253Sjoerg			gid = (gid_t) (bm_lastset(&bm) + 1);
38120253Sjoerg			if (!bm_isset(&bm, gid))
38220253Sjoerg				gid += cnf->min_gid;
38320253Sjoerg			else
38420253Sjoerg				gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
38520253Sjoerg		}
38620253Sjoerg
38720253Sjoerg		/*
38820253Sjoerg		 * Another sanity check
38920253Sjoerg		 */
39020253Sjoerg		if (gid < cnf->min_gid || gid > cnf->max_gid)
39130259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used");
39220253Sjoerg		bm_dealloc(&bm);
39320253Sjoerg	}
39420253Sjoerg	return gid;
39520253Sjoerg}
39620253Sjoerg
39720253Sjoerg
39820253Sjoergstatic int
399284122Sbaptprint_group(struct group * grp)
40020253Sjoerg{
401284122Sbapt	if (!conf.pretty) {
40220747Sdavidn		char           *buf = NULL;
40320253Sjoerg
404242349Sbapt		buf = gr_make(grp);
405244738Sbapt		printf("%s\n", buf);
40620747Sdavidn		free(buf);
40720253Sjoerg	} else {
40820253Sjoerg		int             i;
40920253Sjoerg
41022398Sdavidn		printf("Group Name: %-15s   #%lu\n"
41120747Sdavidn		       "   Members: ",
41220253Sjoerg		       grp->gr_name, (long) grp->gr_gid);
413262864Sjulian		if (grp->gr_mem != NULL) {
414262864Sjulian			for (i = 0; grp->gr_mem[i]; i++)
415262864Sjulian				printf("%s%s", i ? "," : "", grp->gr_mem[i]);
416262864Sjulian		}
41720253Sjoerg		fputs("\n\n", stdout);
41820253Sjoerg	}
41920267Sjoerg	return EXIT_SUCCESS;
42020253Sjoerg}
421