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$"; 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> 3720253Sjoerg 3820253Sjoerg#include "pw.h" 3920253Sjoerg#include "bitmap.h" 4020253Sjoerg 4120253Sjoerg 42176474Sscfstatic struct passwd *lookup_pwent(const char *user); 43176474Sscfstatic void delete_members(char ***members, int *grmembers, int *i, 44176474Sscf struct carg *arg, struct group *grp); 4520253Sjoergstatic int print_group(struct group * grp, int pretty); 4620253Sjoergstatic gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args); 4720253Sjoerg 4820253Sjoergint 4920253Sjoergpw_group(struct userconf * cnf, int mode, struct cargs * args) 5020253Sjoerg{ 5152502Sdavidn int rc; 5220253Sjoerg struct carg *a_name = getarg(args, 'n'); 5320253Sjoerg struct carg *a_gid = getarg(args, 'g'); 5420253Sjoerg struct carg *arg; 5520253Sjoerg struct group *grp = NULL; 5620747Sdavidn int grmembers = 0; 5752502Sdavidn char **members = NULL; 5820253Sjoerg 5920253Sjoerg static struct group fakegroup = 6020253Sjoerg { 6120253Sjoerg "nogroup", 6220253Sjoerg "*", 6320253Sjoerg -1, 6420253Sjoerg NULL 6520253Sjoerg }; 6620253Sjoerg 6752512Sdavidn if (mode == M_LOCK || mode == M_UNLOCK) 6852512Sdavidn errx(EX_USAGE, "'lock' command is not available for groups"); 6952512Sdavidn 7020267Sjoerg /* 7120267Sjoerg * With M_NEXT, we only need to return the 7220267Sjoerg * next gid to stdout 7320267Sjoerg */ 7452512Sdavidn if (mode == M_NEXT) { 7520267Sjoerg gid_t next = gr_gidpolicy(cnf, args); 7620267Sjoerg if (getarg(args, 'q')) 7720267Sjoerg return next; 7820267Sjoerg printf("%ld\n", (long)next); 7920267Sjoerg return EXIT_SUCCESS; 8020267Sjoerg } 8120267Sjoerg 8220253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 8320267Sjoerg int pretty = getarg(args, 'P') != NULL; 8420253Sjoerg 8544229Sdavidn SETGRENT(); 8644229Sdavidn while ((grp = GETGRENT()) != NULL) 8720253Sjoerg print_group(grp, pretty); 8844229Sdavidn ENDGRENT(); 8920267Sjoerg return EXIT_SUCCESS; 9020253Sjoerg } 9120253Sjoerg if (a_gid == NULL) { 9220253Sjoerg if (a_name == NULL) 9330259Scharnier errx(EX_DATAERR, "group name or id required"); 9420253Sjoerg 9561957Sache if (mode != M_ADD && grp == NULL && isdigit((unsigned char)*a_name->val)) { 9620253Sjoerg (a_gid = a_name)->ch = 'g'; 9720253Sjoerg a_name = NULL; 9820253Sjoerg } 9920253Sjoerg } 10044229Sdavidn grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val)); 10120253Sjoerg 10220253Sjoerg if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 10320253Sjoerg if (a_name == NULL && grp == NULL) /* Try harder */ 10444229Sdavidn grp = GETGRGID(atoi(a_gid->val)); 10520253Sjoerg 10620253Sjoerg if (grp == NULL) { 10720253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 10820747Sdavidn char *fmems[1]; 10920747Sdavidn fmems[0] = NULL; 11020253Sjoerg fakegroup.gr_name = a_name ? a_name->val : "nogroup"; 11120253Sjoerg fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : -1; 11220747Sdavidn fakegroup.gr_mem = fmems; 11320267Sjoerg return print_group(&fakegroup, getarg(args, 'P') != NULL); 11420253Sjoerg } 11530259Scharnier errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val); 11620253Sjoerg } 11720253Sjoerg if (a_name == NULL) /* Needed later */ 11820253Sjoerg a_name = addarg(args, 'n', grp->gr_name); 11920253Sjoerg 12020253Sjoerg /* 12120253Sjoerg * Handle deletions now 12220253Sjoerg */ 12320253Sjoerg if (mode == M_DELETE) { 12420253Sjoerg gid_t gid = grp->gr_gid; 12520253Sjoerg 12652502Sdavidn rc = delgrent(grp); 12752502Sdavidn if (rc == -1) 12852502Sdavidn err(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name); 12952502Sdavidn else if (rc != 0) { 13056000Sdavidn warn("group update"); 13152502Sdavidn return EX_IOERR; 13252502Sdavidn } 13320253Sjoerg pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", a_name->val, (long) gid); 13420267Sjoerg return EXIT_SUCCESS; 13520253Sjoerg } else if (mode == M_PRINT) 13620267Sjoerg return print_group(grp, getarg(args, 'P') != NULL); 13720253Sjoerg 13820253Sjoerg if (a_gid) 13920253Sjoerg grp->gr_gid = (gid_t) atoi(a_gid->val); 14020253Sjoerg 14120253Sjoerg if ((arg = getarg(args, 'l')) != NULL) 14220679Sdavidn grp->gr_name = pw_checkname((u_char *)arg->val, 0); 14320253Sjoerg } else { 14420253Sjoerg if (a_name == NULL) /* Required */ 14530259Scharnier errx(EX_DATAERR, "group name required"); 14620253Sjoerg else if (grp != NULL) /* Exists */ 14730259Scharnier errx(EX_DATAERR, "group name `%s' already exists", a_name->val); 14820253Sjoerg 14920747Sdavidn extendarray(&members, &grmembers, 200); 15020747Sdavidn members[0] = NULL; 15120253Sjoerg grp = &fakegroup; 15220679Sdavidn grp->gr_name = pw_checkname((u_char *)a_name->val, 0); 15320253Sjoerg grp->gr_passwd = "*"; 15420253Sjoerg grp->gr_gid = gr_gidpolicy(cnf, args); 15520253Sjoerg grp->gr_mem = members; 15620253Sjoerg } 15720253Sjoerg 15820253Sjoerg /* 15920253Sjoerg * This allows us to set a group password Group passwords is an 16020253Sjoerg * antique idea, rarely used and insecure (no secure database) Should 16120253Sjoerg * be discouraged, but it is apparently still supported by some 16220253Sjoerg * software. 16320253Sjoerg */ 16420253Sjoerg 165124382Siedowse if ((arg = getarg(args, 'h')) != NULL || 166124382Siedowse (arg = getarg(args, 'H')) != NULL) { 16720253Sjoerg if (strcmp(arg->val, "-") == 0) 16820253Sjoerg grp->gr_passwd = "*"; /* No access */ 16920253Sjoerg else { 17020253Sjoerg int fd = atoi(arg->val); 171124382Siedowse int precrypt = (arg->ch == 'H'); 17220253Sjoerg int b; 17320253Sjoerg int istty = isatty(fd); 17420253Sjoerg struct termios t; 17520253Sjoerg char *p, line[256]; 17620253Sjoerg 17720253Sjoerg if (istty) { 17820253Sjoerg if (tcgetattr(fd, &t) == -1) 17920253Sjoerg istty = 0; 18020253Sjoerg else { 18120253Sjoerg struct termios n = t; 18220253Sjoerg 18320253Sjoerg /* Disable echo */ 18420253Sjoerg n.c_lflag &= ~(ECHO); 18520253Sjoerg tcsetattr(fd, TCSANOW, &n); 18620253Sjoerg printf("%sassword for group %s:", (mode == M_UPDATE) ? "New p" : "P", grp->gr_name); 18720253Sjoerg fflush(stdout); 18820253Sjoerg } 18920253Sjoerg } 19020253Sjoerg b = read(fd, line, sizeof(line) - 1); 19120253Sjoerg if (istty) { /* Restore state */ 19220253Sjoerg tcsetattr(fd, TCSANOW, &t); 19320253Sjoerg fputc('\n', stdout); 19420253Sjoerg fflush(stdout); 19520253Sjoerg } 19620253Sjoerg if (b < 0) { 19730259Scharnier warn("-h file descriptor"); 19820267Sjoerg return EX_OSERR; 19920253Sjoerg } 20020253Sjoerg line[b] = '\0'; 20120253Sjoerg if ((p = strpbrk(line, " \t\r\n")) != NULL) 20220253Sjoerg *p = '\0'; 20320253Sjoerg if (!*line) 20430259Scharnier errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 205124382Siedowse if (precrypt) { 206124382Siedowse if (strchr(line, ':') != NULL) 207124382Siedowse return EX_DATAERR; 208124382Siedowse grp->gr_passwd = line; 209124382Siedowse } else 210124382Siedowse grp->gr_passwd = pw_pwcrypt(line); 21120253Sjoerg } 21220253Sjoerg } 21320267Sjoerg 214176474Sscf if (((arg = getarg(args, 'M')) != NULL || 215176474Sscf (arg = getarg(args, 'd')) != NULL || 216176474Sscf (arg = getarg(args, 'm')) != NULL) && arg->val) { 21720267Sjoerg int i = 0; 21820267Sjoerg char *p; 21920267Sjoerg struct passwd *pwd; 22020267Sjoerg 22120747Sdavidn /* Make sure this is not stay NULL with -M "" */ 22220747Sdavidn extendarray(&members, &grmembers, 200); 223176474Sscf if (arg->ch == 'd') 224176474Sscf delete_members(&members, &grmembers, &i, arg, grp); 225176474Sscf else if (arg->ch == 'm') { 22620747Sdavidn int k = 0; 22720747Sdavidn 22820747Sdavidn while (grp->gr_mem[k] != NULL) { 229176473Sscf if (extendarray(&members, &grmembers, i + 2) != -1) 23020747Sdavidn members[i++] = grp->gr_mem[k]; 23120747Sdavidn k++; 23220267Sjoerg } 23320267Sjoerg } 234176474Sscf 235176474Sscf if (arg->ch != 'd') 236176474Sscf for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 237176474Sscf int j; 238176474Sscf 239176474Sscf /* 240176474Sscf * Check for duplicates 241176474Sscf */ 242176474Sscf pwd = lookup_pwent(p); 243176474Sscf for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++) 244176474Sscf ; 245176474Sscf if (j == i && extendarray(&members, &grmembers, i + 2) != -1) 246176474Sscf members[i++] = newstr(pwd->pw_name); 24720267Sjoerg } 24820747Sdavidn while (i < grmembers) 24920267Sjoerg members[i++] = NULL; 25020267Sjoerg grp->gr_mem = members; 25120267Sjoerg } 25220267Sjoerg 25320267Sjoerg if (getarg(args, 'N') != NULL) 25420267Sjoerg return print_group(grp, getarg(args, 'P') != NULL); 25520267Sjoerg 25652502Sdavidn if (mode == M_ADD && (rc = addgrent(grp)) != 0) { 25752502Sdavidn if (rc == -1) 25852502Sdavidn warnx("group '%s' already exists", grp->gr_name); 25952502Sdavidn else 26052502Sdavidn warn("group update"); 26120267Sjoerg return EX_IOERR; 26252502Sdavidn } else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) { 26352502Sdavidn if (rc == -1) 26452502Sdavidn warnx("group '%s' not available (NIS?)", grp->gr_name); 26552502Sdavidn else 26656000Sdavidn warn("group update"); 26752502Sdavidn return EX_IOERR; 26820253Sjoerg } 26920253Sjoerg /* grp may have been invalidated */ 27044229Sdavidn if ((grp = GETGRNAM(a_name->val)) == NULL) 27130259Scharnier errx(EX_SOFTWARE, "group disappeared during update"); 27220253Sjoerg 27320253Sjoerg pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid); 27420253Sjoerg 275244459Seadler free(members); 27620747Sdavidn 27720267Sjoerg return EXIT_SUCCESS; 27820253Sjoerg} 27920253Sjoerg 28020253Sjoerg 281176474Sscf/* 282176474Sscf * Lookup a passwd entry using a name or UID. 283176474Sscf */ 284176474Sscfstatic struct passwd * 285176474Sscflookup_pwent(const char *user) 286176474Sscf{ 287176474Sscf struct passwd *pwd; 288176474Sscf 289176474Sscf if ((pwd = GETPWNAM(user)) == NULL && 290176474Sscf (!isdigit((unsigned char)*user) || 291176474Sscf (pwd = getpwuid((uid_t) atoi(user))) == NULL)) 292176474Sscf errx(EX_NOUSER, "user `%s' does not exist", user); 293176474Sscf 294176474Sscf return (pwd); 295176474Sscf} 296176474Sscf 297176474Sscf 298176474Sscf/* 299176474Sscf * Delete requested members from a group. 300176474Sscf */ 301176474Sscfstatic void 302176474Sscfdelete_members(char ***members, int *grmembers, int *i, struct carg *arg, 303176474Sscf struct group *grp) 304176474Sscf{ 305176474Sscf bool matchFound; 306176474Sscf char *user; 307176474Sscf char *valueCopy; 308176474Sscf char *valuePtr; 309176474Sscf int k; 310176474Sscf struct passwd *pwd; 311176474Sscf 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 33920253Sjoerggr_gidpolicy(struct userconf * cnf, struct cargs * args) 34020253Sjoerg{ 34120253Sjoerg struct group *grp; 34220253Sjoerg gid_t gid = (gid_t) - 1; 34320253Sjoerg struct carg *a_gid = getarg(args, 'g'); 34420253Sjoerg 34520253Sjoerg /* 34620253Sjoerg * Check the given gid, if any 34720253Sjoerg */ 34820253Sjoerg if (a_gid != NULL) { 34920253Sjoerg gid = (gid_t) atol(a_gid->val); 35020253Sjoerg 35144229Sdavidn if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL) 35230259Scharnier errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid); 35320253Sjoerg } else { 35420253Sjoerg struct bitmap bm; 35520253Sjoerg 35620253Sjoerg /* 35720253Sjoerg * We need to allocate the next available gid under one of 35820253Sjoerg * two policies a) Grab the first unused gid b) Grab the 35920253Sjoerg * highest possible unused gid 36020253Sjoerg */ 36120253Sjoerg if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */ 36220253Sjoerg cnf->min_gid = 1000; 36320253Sjoerg cnf->max_gid = 32000; 36420253Sjoerg } 36520253Sjoerg bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1); 36620253Sjoerg 36720253Sjoerg /* 36820253Sjoerg * Now, let's fill the bitmap from the password file 36920253Sjoerg */ 37044229Sdavidn SETGRENT(); 37144229Sdavidn while ((grp = GETGRENT()) != NULL) 37256000Sdavidn if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid && 37356000Sdavidn (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid) 37420253Sjoerg bm_setbit(&bm, grp->gr_gid - cnf->min_gid); 37544229Sdavidn ENDGRENT(); 37620253Sjoerg 37720253Sjoerg /* 37820253Sjoerg * Then apply the policy, with fallback to reuse if necessary 37920253Sjoerg */ 38020253Sjoerg if (cnf->reuse_gids) 38120253Sjoerg gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 38220253Sjoerg else { 38320253Sjoerg gid = (gid_t) (bm_lastset(&bm) + 1); 38420253Sjoerg if (!bm_isset(&bm, gid)) 38520253Sjoerg gid += cnf->min_gid; 38620253Sjoerg else 38720253Sjoerg gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 38820253Sjoerg } 38920253Sjoerg 39020253Sjoerg /* 39120253Sjoerg * Another sanity check 39220253Sjoerg */ 39320253Sjoerg if (gid < cnf->min_gid || gid > cnf->max_gid) 39430259Scharnier errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used"); 39520253Sjoerg bm_dealloc(&bm); 39620253Sjoerg } 39720253Sjoerg return gid; 39820253Sjoerg} 39920253Sjoerg 40020253Sjoerg 40120253Sjoergstatic int 40220253Sjoergprint_group(struct group * grp, int pretty) 40320253Sjoerg{ 40420253Sjoerg if (!pretty) { 40520747Sdavidn int buflen = 0; 40620747Sdavidn char *buf = NULL; 40720253Sjoerg 40820747Sdavidn fmtgrent(&buf, &buflen, grp); 40920253Sjoerg fputs(buf, stdout); 41020747Sdavidn free(buf); 41120253Sjoerg } else { 41220253Sjoerg int i; 41320253Sjoerg 41422398Sdavidn printf("Group Name: %-15s #%lu\n" 41520747Sdavidn " Members: ", 41620253Sjoerg grp->gr_name, (long) grp->gr_gid); 41720747Sdavidn for (i = 0; grp->gr_mem[i]; i++) 41820253Sjoerg printf("%s%s", i ? "," : "", grp->gr_mem[i]); 41920253Sjoerg fputs("\n\n", stdout); 42020253Sjoerg } 42120267Sjoerg return EXIT_SUCCESS; 42220253Sjoerg} 423