pw_group.c revision 285411
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 285411 2015-07-11 22:35:07Z 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);
45285411Sbaptstatic void	delete_members(struct group *grp, char *list);
46284122Sbaptstatic int	print_group(struct group * grp);
47284133Sbaptstatic gid_t    gr_gidpolicy(struct userconf * cnf, long id);
4820253Sjoerg
49285136Sbaptstatic void
50285136Sbaptset_passwd(struct group *grp, bool update)
51285136Sbapt{
52285136Sbapt	int		 b;
53285136Sbapt	int		 istty;
54285136Sbapt	struct termios	 t, n;
55285136Sbapt	char		*p, line[256];
56285136Sbapt
57285136Sbapt	if (conf.fd == '-') {
58285136Sbapt		grp->gr_passwd = "*";	/* No access */
59285136Sbapt		return;
60285136Sbapt	}
61285136Sbapt
62285136Sbapt	if ((istty = isatty(conf.fd))) {
63285136Sbapt		n = t;
64285136Sbapt		/* Disable echo */
65285136Sbapt		n.c_lflag &= ~(ECHO);
66285136Sbapt		tcsetattr(conf.fd, TCSANOW, &n);
67285136Sbapt		printf("%sassword for group %s:", update ? "New p" : "P",
68285136Sbapt		    grp->gr_name);
69285136Sbapt		fflush(stdout);
70285136Sbapt	}
71285136Sbapt	b = read(conf.fd, line, sizeof(line) - 1);
72285136Sbapt	if (istty) {	/* Restore state */
73285136Sbapt		tcsetattr(conf.fd, TCSANOW, &t);
74285136Sbapt		fputc('\n', stdout);
75285136Sbapt		fflush(stdout);
76285136Sbapt	}
77285136Sbapt	if (b < 0)
78285136Sbapt		err(EX_OSERR, "-h file descriptor");
79285136Sbapt	line[b] = '\0';
80285136Sbapt	if ((p = strpbrk(line, " \t\r\n")) != NULL)
81285136Sbapt		*p = '\0';
82285136Sbapt	if (!*line)
83285136Sbapt		errx(EX_DATAERR, "empty password read on file descriptor %d",
84285136Sbapt		    conf.fd);
85285136Sbapt	if (conf.precrypted) {
86285136Sbapt		if (strchr(line, ':') != 0)
87285136Sbapt			errx(EX_DATAERR, "wrong encrypted passwrd");
88285136Sbapt		grp->gr_passwd = line;
89285136Sbapt	} else
90285136Sbapt		grp->gr_passwd = pw_pwcrypt(line);
91285136Sbapt}
92285136Sbapt
9320253Sjoergint
94285395Sbaptpw_groupnext(struct userconf *cnf, bool quiet)
95285395Sbapt{
96285395Sbapt	gid_t next = gr_gidpolicy(cnf, -1);
97285395Sbapt
98285395Sbapt	if (quiet)
99285395Sbapt		return (next);
100285395Sbapt	printf("%u\n", next);
101285395Sbapt
102285395Sbapt	return (EXIT_SUCCESS);
103285395Sbapt}
104285395Sbapt
105285398Sbaptstatic int
106285398Sbaptpw_groupshow(const char *name, long id, struct group *fakegroup)
107285398Sbapt{
108285398Sbapt	struct group *grp = NULL;
109285398Sbapt
110285398Sbapt	if (id < 0 && name == NULL && !conf.all)
111285398Sbapt		errx(EX_DATAERR, "groupname or id or '-a' required");
112285398Sbapt
113285398Sbapt	if (conf.all) {
114285398Sbapt		SETGRENT();
115285398Sbapt		while ((grp = GETGRENT()) != NULL)
116285398Sbapt			print_group(grp);
117285398Sbapt		ENDGRENT();
118285398Sbapt
119285398Sbapt		return (EXIT_SUCCESS);
120285398Sbapt	}
121285398Sbapt
122285398Sbapt	grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
123285398Sbapt	if (grp == NULL) {
124285398Sbapt		if (conf.force) {
125285398Sbapt			grp = fakegroup;
126285398Sbapt		} else {
127285398Sbapt			if (name == NULL)
128285398Sbapt				errx(EX_DATAERR, "unknown gid `%ld'", id);
129285398Sbapt			errx(EX_DATAERR, "unknown group `%s'", name);
130285398Sbapt		}
131285398Sbapt	}
132285398Sbapt
133285398Sbapt	return (print_group(grp));
134285398Sbapt}
135285398Sbapt
136285401Sbaptstatic int
137285401Sbaptpw_groupdel(const char *name, long id)
138285401Sbapt{
139285401Sbapt	struct group *grp = NULL;
140285401Sbapt	int rc;
141285401Sbapt
142285401Sbapt	grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
143285401Sbapt	if (grp == NULL) {
144285401Sbapt		if (name == NULL)
145285401Sbapt			errx(EX_DATAERR, "unknown gid `%ld'", id);
146285401Sbapt		errx(EX_DATAERR, "unknown group `%s'", name);
147285401Sbapt	}
148285401Sbapt
149285401Sbapt	rc = delgrent(grp);
150285401Sbapt	if (rc == -1)
151285401Sbapt		err(EX_IOERR, "group '%s' not available (NIS?)", name);
152285401Sbapt	else if (rc != 0)
153285401Sbapt		err(EX_IOERR, "group update");
154285401Sbapt	pw_log(conf.userconf, M_DELETE, W_GROUP, "%s(%ld) removed", name, id);
155285401Sbapt
156285401Sbapt	return (EXIT_SUCCESS);
157285401Sbapt}
158285401Sbapt
159285395Sbaptint
160284128Sbaptpw_group(int mode, char *name, long id, struct cargs * args)
16120253Sjoerg{
16252502Sdavidn	int		rc;
16320253Sjoerg	struct carg    *arg;
16420253Sjoerg	struct group   *grp = NULL;
16552502Sdavidn	char           **members = 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
287283842Sbapt	pw_log(cnf, mode, W_GROUP, "%s(%u)", grp->gr_name, grp->gr_gid);
28820253Sjoerg
289243894Seadler	free(members);
29020747Sdavidn
29120267Sjoerg	return EXIT_SUCCESS;
29220253Sjoerg}
29320253Sjoerg
29420253Sjoerg
295176474Sscf/*
296176474Sscf * Lookup a passwd entry using a name or UID.
297176474Sscf */
298176474Sscfstatic struct passwd *
299176474Sscflookup_pwent(const char *user)
300176474Sscf{
301176474Sscf	struct passwd *pwd;
302176474Sscf
303176474Sscf	if ((pwd = GETPWNAM(user)) == NULL &&
304176474Sscf	    (!isdigit((unsigned char)*user) ||
305176474Sscf	    (pwd = getpwuid((uid_t) atoi(user))) == NULL))
306176474Sscf		errx(EX_NOUSER, "user `%s' does not exist", user);
307176474Sscf
308176474Sscf	return (pwd);
309176474Sscf}
310176474Sscf
311176474Sscf
312176474Sscf/*
313176474Sscf * Delete requested members from a group.
314176474Sscf */
315176474Sscfstatic void
316285411Sbaptdelete_members(struct group *grp, char *list)
317176474Sscf{
318285411Sbapt	char *p;
319176474Sscf	int k;
320176474Sscf
321262864Sjulian	if (grp->gr_mem == NULL)
322262864Sjulian		return;
323262864Sjulian
324285411Sbapt	for (p = strtok(list, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
325285411Sbapt		for (k = 0; grp->gr_mem[k] != NULL; k++) {
326285411Sbapt			if (strcmp(grp->gr_mem[k], p) == 0)
327176474Sscf				break;
328176474Sscf		}
329285411Sbapt		if (grp->gr_mem[k] == NULL) /* No match */
330285411Sbapt			continue;
331176474Sscf
332285411Sbapt		for (; grp->gr_mem[k] != NULL; k++)
333285411Sbapt			grp->gr_mem[k] = grp->gr_mem[k+1];
334176474Sscf	}
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