2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * David L. Nugent. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 static const char rcsid[] =
47 static struct passwd *lookup_pwent(const char *user);
48 static void delete_members(struct group *grp, char *list);
49 static int print_group(struct group * grp, bool pretty);
50 static gid_t gr_gidpolicy(struct userconf * cnf, intmax_t id);
53 grp_set_passwd(struct group *grp, bool update, int fd, bool precrypted)
64 grp->gr_passwd = "*"; /* No access */
68 if ((istty = isatty(fd))) {
69 if (tcgetattr(fd, &t) == -1)
75 tcsetattr(fd, TCSANOW, &n);
76 printf("%sassword for group %s:",
77 update ? "New p" : "P",
82 b = read(fd, line, sizeof(line) - 1);
83 if (istty) { /* Restore state */
84 tcsetattr(fd, TCSANOW, &t);
89 err(EX_OSERR, "-h file descriptor");
91 if ((p = strpbrk(line, " \t\r\n")) != NULL)
94 errx(EX_DATAERR, "empty password read on file descriptor %d",
97 if (strchr(line, ':') != 0)
98 errx(EX_DATAERR, "wrong encrypted passwrd");
99 grp->gr_passwd = line;
101 grp->gr_passwd = pw_pwcrypt(line);
105 pw_groupnext(struct userconf *cnf, bool quiet)
107 gid_t next = gr_gidpolicy(cnf, -1);
111 printf("%ju\n", (uintmax_t)next);
113 return (EXIT_SUCCESS);
116 static struct group *
117 getgroup(char *name, intmax_t id, bool fatal)
121 if (id < 0 && name == NULL)
122 errx(EX_DATAERR, "groupname or id required");
123 grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
128 errx(EX_DATAERR, "unknown gid `%ju'", id);
129 errx(EX_DATAERR, "unknown group `%s'", name);
135 * Lookup a passwd entry using a name or UID.
137 static struct passwd *
138 lookup_pwent(const char *user)
142 if ((pwd = GETPWNAM(user)) == NULL &&
143 (!isdigit((unsigned char)*user) ||
144 (pwd = getpwuid((uid_t) atoi(user))) == NULL))
145 errx(EX_NOUSER, "user `%s' does not exist", user);
152 * Delete requested members from a group.
155 delete_members(struct group *grp, char *list)
160 if (grp->gr_mem == NULL)
163 for (p = strtok(list, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
164 for (k = 0; grp->gr_mem[k] != NULL; k++) {
165 if (strcmp(grp->gr_mem[k], p) == 0)
168 if (grp->gr_mem[k] == NULL) /* No match */
171 for (; grp->gr_mem[k] != NULL; k++)
172 grp->gr_mem[k] = grp->gr_mem[k+1];
177 gr_gidpolicy(struct userconf * cnf, intmax_t id)
181 gid_t gid = (gid_t) - 1;
184 * Check the given gid, if any
189 if ((grp = GETGRGID(gid)) != NULL && conf.checkduplicate)
190 errx(EX_DATAERR, "gid `%ju' has already been allocated",
191 (uintmax_t)grp->gr_gid);
196 * We need to allocate the next available gid under one of
197 * two policies a) Grab the first unused gid b) Grab the
198 * highest possible unused gid
200 if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */
202 cnf->max_gid = 32000;
204 bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1);
207 * Now, let's fill the bitmap from the password file
210 while ((grp = GETGRENT()) != NULL)
211 if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid &&
212 (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid)
213 bm_setbit(&bm, grp->gr_gid - cnf->min_gid);
217 * Then apply the policy, with fallback to reuse if necessary
220 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
222 gid = (gid_t) (bm_lastset(&bm) + 1);
223 if (!bm_isset(&bm, gid))
226 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
230 * Another sanity check
232 if (gid < cnf->min_gid || gid > cnf->max_gid)
233 errx(EX_SOFTWARE, "unable to allocate a new gid - range fully "
240 print_group(struct group * grp, bool pretty)
246 printf("Group Name: %-15s #%lu\n"
248 grp->gr_name, (long) grp->gr_gid);
249 if (grp->gr_mem != NULL) {
250 for (i = 0; grp->gr_mem[i]; i++)
251 printf("%s%s", i ? "," : "", grp->gr_mem[i]);
253 fputs("\n\n", stdout);
254 return (EXIT_SUCCESS);
260 return (EXIT_SUCCESS);
264 pw_group_next(int argc, char **argv, char *arg1 __unused)
266 struct userconf *cnf;
267 const char *cfg = NULL;
271 while ((ch = getopt(argc, argv, "C:q")) != -1) {
283 freopen(_PATH_DEVNULL, "w", stderr);
284 cnf = get_userconfig(cfg);
285 return (pw_groupnext(cnf, quiet));
289 pw_group_show(int argc, char **argv, char *arg1)
291 struct group *grp = NULL;
295 bool all, force, quiet, pretty;
297 all = force = quiet = pretty = false;
299 struct group fakegroup = {
307 if (arg1[strspn(arg1, "0123456789")] == '\0')
308 id = pw_checkid(arg1, GID_MAX);
313 while ((ch = getopt(argc, argv, "C:qn:g:FPa")) != -1) {
316 /* ignore compatibility */
325 id = pw_checkid(optarg, GID_MAX);
340 freopen(_PATH_DEVNULL, "w", stderr);
344 while ((grp = GETGRENT()) != NULL)
345 print_group(grp, pretty);
347 return (EXIT_SUCCESS);
350 grp = getgroup(name, id, !force);
354 return (print_group(grp, pretty));
358 pw_group_del(int argc, char **argv, char *arg1)
360 struct userconf *cnf = NULL;
361 struct group *grp = NULL;
363 const char *cfg = NULL;
370 if (arg1[strspn(arg1, "0123456789")] == '\0')
371 id = pw_checkid(arg1, GID_MAX);
376 while ((ch = getopt(argc, argv, "C:qn:g:Y")) != -1) {
388 id = pw_checkid(optarg, GID_MAX);
397 freopen(_PATH_DEVNULL, "w", stderr);
398 grp = getgroup(name, id, true);
399 cnf = get_userconfig(cfg);
402 err(EX_IOERR, "group '%s' not available (NIS?)", name);
404 err(EX_IOERR, "group update");
405 pw_log(cnf, M_DELETE, W_GROUP, "%s(%ju) removed", name,
408 if (nis && nis_update() == 0)
409 pw_log(cnf, M_DELETE, W_GROUP, "NIS maps updated");
411 return (EXIT_SUCCESS);
415 grp_has_member(struct group *grp, const char *name)
419 for (j = 0; grp->gr_mem != NULL && grp->gr_mem[j] != NULL; j++)
420 if (strcmp(grp->gr_mem[j], name) == 0)
426 grp_add_members(struct group **grp, char *members)
434 for (p = strtok(members, tok); p != NULL; p = strtok(NULL, tok)) {
435 pwd = lookup_pwent(p);
436 if (grp_has_member(*grp, pwd->pw_name))
438 *grp = gr_add(*grp, pwd->pw_name);
443 groupadd(struct userconf *cnf, char *name, gid_t id, char *members, int fd,
444 bool dryrun, bool pretty, bool precrypted)
449 struct group fakegroup = {
457 grp->gr_name = pw_checkname(name, 0);
458 grp->gr_passwd = "*";
459 grp->gr_gid = gr_gidpolicy(cnf, id);
463 * This allows us to set a group password Group passwords is an
464 * antique idea, rarely used and insecure (no secure database) Should
465 * be discouraged, but it is apparently still supported by some
468 grp_set_passwd(grp, false, fd, precrypted);
469 grp_add_members(&grp, members);
471 return (print_group(grp, pretty));
473 if ((rc = addgrent(grp)) != 0) {
475 errx(EX_IOERR, "group '%s' already exists",
478 err(EX_IOERR, "group update");
481 pw_log(cnf, M_ADD, W_GROUP, "%s(%ju)", grp->gr_name,
482 (uintmax_t)grp->gr_gid);
484 return (EXIT_SUCCESS);
488 pw_group_add(int argc, char **argv, char *arg1)
490 struct userconf *cnf = NULL;
492 char *members = NULL;
493 const char *cfg = NULL;
496 bool quiet, precrypted, dryrun, pretty, nis;
498 quiet = precrypted = dryrun = pretty = nis = false;
501 if (arg1[strspn(arg1, "0123456789")] == '\0')
502 id = pw_checkid(arg1, GID_MAX);
507 while ((ch = getopt(argc, argv, "C:qn:g:h:H:M:oNPY")) != -1) {
519 id = pw_checkid(optarg, GID_MAX);
523 errx(EX_USAGE, "'-h' and '-H' are mutually "
524 "exclusive options");
525 fd = pw_checkfd(optarg);
528 errx(EX_USAGE, "-H expects a file descriptor");
532 errx(EX_USAGE, "'-h' and '-H' are mutually "
533 "exclusive options");
534 fd = pw_checkfd(optarg);
540 conf.checkduplicate = false;
555 freopen(_PATH_DEVNULL, "w", stderr);
557 errx(EX_DATAERR, "group name required");
558 if (GETGRNAM(name) != NULL)
559 errx(EX_DATAERR, "group name `%s' already exists", name);
560 cnf = get_userconfig(cfg);
561 rc = groupadd(cnf, name, gr_gidpolicy(cnf, id), members, fd, dryrun,
563 if (nis && rc == EXIT_SUCCESS && nis_update() == 0)
564 pw_log(cnf, M_ADD, W_GROUP, "NIS maps updated");
570 pw_group_mod(int argc, char **argv, char *arg1)
572 struct userconf *cnf;
573 struct group *grp = NULL;
574 const char *cfg = NULL;
575 char *oldmembers = NULL;
576 char *members = NULL;
577 char *newmembers = NULL;
578 char *newname = NULL;
582 bool quiet, pretty, dryrun, nis, precrypted;
584 quiet = pretty = dryrun = nis = precrypted = false;
587 if (arg1[strspn(arg1, "0123456789")] == '\0')
588 id = pw_checkid(arg1, GID_MAX);
593 while ((ch = getopt(argc, argv, "C:qn:d:g:l:h:H:M:m:NPY")) != -1) {
605 id = pw_checkid(optarg, GID_MAX);
615 errx(EX_USAGE, "'-h' and '-H' are mutually "
616 "exclusive options");
617 fd = pw_checkfd(optarg);
620 errx(EX_USAGE, "-H expects a file descriptor");
624 errx(EX_USAGE, "'-h' and '-H' are mutually "
625 "exclusive options");
626 fd = pw_checkfd(optarg);
646 freopen(_PATH_DEVNULL, "w", stderr);
647 cnf = get_userconfig(cfg);
648 grp = getgroup(name, id, true);
655 grp->gr_name = pw_checkname(newname, 0);
657 grp_set_passwd(grp, true, fd, precrypted);
659 * Keep the same logic as old code for now:
660 * if -M is passed, -d and -m are ignored
661 * then id -d, -m is ignored
667 grp_add_members(&grp, members);
668 } else if (oldmembers) {
669 delete_members(grp, oldmembers);
670 } else if (newmembers) {
671 grp_add_members(&grp, newmembers);
675 print_group(grp, pretty);
676 return (EXIT_SUCCESS);
679 if ((rc = chggrent(name, grp)) != 0) {
681 errx(EX_IOERR, "group '%s' not available (NIS?)",
684 err(EX_IOERR, "group update");
690 /* grp may have been invalidated */
691 if ((grp = GETGRNAM(name)) == NULL)
692 errx(EX_SOFTWARE, "group disappeared during update");
694 pw_log(cnf, M_UPDATE, W_GROUP, "%s(%ju)", grp->gr_name,
695 (uintmax_t)grp->gr_gid);
697 if (nis && nis_update() == 0)
698 pw_log(cnf, M_UPDATE, W_GROUP, "NIS maps updated");
700 return (EXIT_SUCCESS);