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> 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); 4720253Sjoergstatic int print_group(struct group * grp, int pretty); 4820253Sjoergstatic gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args); 4920253Sjoerg 5020253Sjoergint 5120253Sjoergpw_group(struct userconf * cnf, int mode, struct cargs * args) 5220253Sjoerg{ 5352502Sdavidn int rc; 5420253Sjoerg struct carg *a_name = getarg(args, 'n'); 5520253Sjoerg struct carg *a_gid = getarg(args, 'g'); 5620253Sjoerg struct carg *arg; 5720253Sjoerg struct group *grp = NULL; 5820747Sdavidn int grmembers = 0; 5952502Sdavidn char **members = NULL; 6020253Sjoerg 6120253Sjoerg static struct group fakegroup = 6220253Sjoerg { 6320253Sjoerg "nogroup", 6420253Sjoerg "*", 6520253Sjoerg -1, 6620253Sjoerg NULL 6720253Sjoerg }; 6820253Sjoerg 6952512Sdavidn if (mode == M_LOCK || mode == M_UNLOCK) 7052512Sdavidn errx(EX_USAGE, "'lock' command is not available for groups"); 7152512Sdavidn 7220267Sjoerg /* 7320267Sjoerg * With M_NEXT, we only need to return the 7420267Sjoerg * next gid to stdout 7520267Sjoerg */ 7652512Sdavidn if (mode == M_NEXT) { 7720267Sjoerg gid_t next = gr_gidpolicy(cnf, args); 7820267Sjoerg if (getarg(args, 'q')) 7920267Sjoerg return next; 8020267Sjoerg printf("%ld\n", (long)next); 8120267Sjoerg return EXIT_SUCCESS; 8220267Sjoerg } 8320267Sjoerg 8420253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 8520267Sjoerg int pretty = getarg(args, 'P') != NULL; 8620253Sjoerg 8744229Sdavidn SETGRENT(); 8844229Sdavidn while ((grp = GETGRENT()) != NULL) 8920253Sjoerg print_group(grp, pretty); 9044229Sdavidn ENDGRENT(); 9120267Sjoerg return EXIT_SUCCESS; 9220253Sjoerg } 9320253Sjoerg if (a_gid == NULL) { 9420253Sjoerg if (a_name == NULL) 9530259Scharnier errx(EX_DATAERR, "group name or id required"); 9620253Sjoerg 9761957Sache if (mode != M_ADD && grp == NULL && isdigit((unsigned char)*a_name->val)) { 9820253Sjoerg (a_gid = a_name)->ch = 'g'; 9920253Sjoerg a_name = NULL; 10020253Sjoerg } 10120253Sjoerg } 10244229Sdavidn grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val)); 10320253Sjoerg 10420253Sjoerg if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 10520253Sjoerg if (a_name == NULL && grp == NULL) /* Try harder */ 10644229Sdavidn grp = GETGRGID(atoi(a_gid->val)); 10720253Sjoerg 10820253Sjoerg if (grp == NULL) { 10920253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 11020747Sdavidn char *fmems[1]; 11120747Sdavidn fmems[0] = NULL; 11220253Sjoerg fakegroup.gr_name = a_name ? a_name->val : "nogroup"; 11320253Sjoerg fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : -1; 11420747Sdavidn fakegroup.gr_mem = fmems; 11520267Sjoerg return print_group(&fakegroup, getarg(args, 'P') != NULL); 11620253Sjoerg } 11730259Scharnier errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val); 11820253Sjoerg } 11920253Sjoerg if (a_name == NULL) /* Needed later */ 12020253Sjoerg a_name = addarg(args, 'n', grp->gr_name); 12120253Sjoerg 12220253Sjoerg /* 12320253Sjoerg * Handle deletions now 12420253Sjoerg */ 12520253Sjoerg if (mode == M_DELETE) { 12620253Sjoerg gid_t gid = grp->gr_gid; 12720253Sjoerg 12852502Sdavidn rc = delgrent(grp); 12952502Sdavidn if (rc == -1) 13052502Sdavidn err(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name); 13152502Sdavidn else if (rc != 0) { 13256000Sdavidn warn("group update"); 13352502Sdavidn return EX_IOERR; 13452502Sdavidn } 13520253Sjoerg pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", a_name->val, (long) gid); 13620267Sjoerg return EXIT_SUCCESS; 13720253Sjoerg } else if (mode == M_PRINT) 13820267Sjoerg return print_group(grp, getarg(args, 'P') != NULL); 13920253Sjoerg 14020253Sjoerg if (a_gid) 14120253Sjoerg grp->gr_gid = (gid_t) atoi(a_gid->val); 14220253Sjoerg 14320253Sjoerg if ((arg = getarg(args, 'l')) != NULL) 14420679Sdavidn grp->gr_name = pw_checkname((u_char *)arg->val, 0); 14520253Sjoerg } else { 14620253Sjoerg if (a_name == NULL) /* Required */ 14730259Scharnier errx(EX_DATAERR, "group name required"); 14820253Sjoerg else if (grp != NULL) /* Exists */ 14930259Scharnier errx(EX_DATAERR, "group name `%s' already exists", a_name->val); 15020253Sjoerg 15120747Sdavidn extendarray(&members, &grmembers, 200); 15220747Sdavidn members[0] = NULL; 15320253Sjoerg grp = &fakegroup; 15420679Sdavidn grp->gr_name = pw_checkname((u_char *)a_name->val, 0); 15520253Sjoerg grp->gr_passwd = "*"; 15620253Sjoerg grp->gr_gid = gr_gidpolicy(cnf, args); 15720253Sjoerg grp->gr_mem = members; 15820253Sjoerg } 15920253Sjoerg 16020253Sjoerg /* 16120253Sjoerg * This allows us to set a group password Group passwords is an 16220253Sjoerg * antique idea, rarely used and insecure (no secure database) Should 16320253Sjoerg * be discouraged, but it is apparently still supported by some 16420253Sjoerg * software. 16520253Sjoerg */ 16620253Sjoerg 167124382Siedowse if ((arg = getarg(args, 'h')) != NULL || 168124382Siedowse (arg = getarg(args, 'H')) != NULL) { 16920253Sjoerg if (strcmp(arg->val, "-") == 0) 17020253Sjoerg grp->gr_passwd = "*"; /* No access */ 17120253Sjoerg else { 17220253Sjoerg int fd = atoi(arg->val); 173124382Siedowse int precrypt = (arg->ch == 'H'); 17420253Sjoerg int b; 17520253Sjoerg int istty = isatty(fd); 17620253Sjoerg struct termios t; 17720253Sjoerg char *p, line[256]; 17820253Sjoerg 17920253Sjoerg if (istty) { 18020253Sjoerg if (tcgetattr(fd, &t) == -1) 18120253Sjoerg istty = 0; 18220253Sjoerg else { 18320253Sjoerg struct termios n = t; 18420253Sjoerg 18520253Sjoerg /* Disable echo */ 18620253Sjoerg n.c_lflag &= ~(ECHO); 18720253Sjoerg tcsetattr(fd, TCSANOW, &n); 18820253Sjoerg printf("%sassword for group %s:", (mode == M_UPDATE) ? "New p" : "P", grp->gr_name); 18920253Sjoerg fflush(stdout); 19020253Sjoerg } 19120253Sjoerg } 19220253Sjoerg b = read(fd, line, sizeof(line) - 1); 19320253Sjoerg if (istty) { /* Restore state */ 19420253Sjoerg tcsetattr(fd, TCSANOW, &t); 19520253Sjoerg fputc('\n', stdout); 19620253Sjoerg fflush(stdout); 19720253Sjoerg } 19820253Sjoerg if (b < 0) { 19930259Scharnier warn("-h file descriptor"); 20020267Sjoerg return EX_OSERR; 20120253Sjoerg } 20220253Sjoerg line[b] = '\0'; 20320253Sjoerg if ((p = strpbrk(line, " \t\r\n")) != NULL) 20420253Sjoerg *p = '\0'; 20520253Sjoerg if (!*line) 20630259Scharnier errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 207124382Siedowse if (precrypt) { 208124382Siedowse if (strchr(line, ':') != NULL) 209124382Siedowse return EX_DATAERR; 210124382Siedowse grp->gr_passwd = line; 211124382Siedowse } else 212124382Siedowse grp->gr_passwd = pw_pwcrypt(line); 21320253Sjoerg } 21420253Sjoerg } 21520267Sjoerg 216176474Sscf if (((arg = getarg(args, 'M')) != NULL || 217176474Sscf (arg = getarg(args, 'd')) != NULL || 218176474Sscf (arg = getarg(args, 'm')) != NULL) && arg->val) { 21920267Sjoerg int i = 0; 22020267Sjoerg char *p; 22120267Sjoerg struct passwd *pwd; 22220267Sjoerg 22320747Sdavidn /* Make sure this is not stay NULL with -M "" */ 22420747Sdavidn extendarray(&members, &grmembers, 200); 225176474Sscf if (arg->ch == 'd') 226176474Sscf delete_members(&members, &grmembers, &i, arg, grp); 227176474Sscf else if (arg->ch == 'm') { 22820747Sdavidn int k = 0; 22920747Sdavidn 23020747Sdavidn while (grp->gr_mem[k] != NULL) { 231176473Sscf if (extendarray(&members, &grmembers, i + 2) != -1) 23220747Sdavidn members[i++] = grp->gr_mem[k]; 23320747Sdavidn k++; 23420267Sjoerg } 23520267Sjoerg } 236176474Sscf 237176474Sscf if (arg->ch != 'd') 238176474Sscf for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 239176474Sscf int j; 240176474Sscf 241176474Sscf /* 242176474Sscf * Check for duplicates 243176474Sscf */ 244176474Sscf pwd = lookup_pwent(p); 245176474Sscf for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++) 246176474Sscf ; 247176474Sscf if (j == i && extendarray(&members, &grmembers, i + 2) != -1) 248176474Sscf members[i++] = newstr(pwd->pw_name); 24920267Sjoerg } 25020747Sdavidn while (i < grmembers) 25120267Sjoerg members[i++] = NULL; 25220267Sjoerg grp->gr_mem = members; 25320267Sjoerg } 25420267Sjoerg 25520267Sjoerg if (getarg(args, 'N') != NULL) 25620267Sjoerg return print_group(grp, getarg(args, 'P') != NULL); 25720267Sjoerg 25852502Sdavidn if (mode == M_ADD && (rc = addgrent(grp)) != 0) { 25952502Sdavidn if (rc == -1) 26052502Sdavidn warnx("group '%s' already exists", grp->gr_name); 26152502Sdavidn else 26252502Sdavidn warn("group update"); 26320267Sjoerg return EX_IOERR; 26452502Sdavidn } else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) { 26552502Sdavidn if (rc == -1) 26652502Sdavidn warnx("group '%s' not available (NIS?)", grp->gr_name); 26752502Sdavidn else 26856000Sdavidn warn("group update"); 26952502Sdavidn return EX_IOERR; 27020253Sjoerg } 27120253Sjoerg /* grp may have been invalidated */ 27244229Sdavidn if ((grp = GETGRNAM(a_name->val)) == NULL) 27330259Scharnier errx(EX_SOFTWARE, "group disappeared during update"); 27420253Sjoerg 27520253Sjoerg pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid); 27620253Sjoerg 277243894Seadler free(members); 27820747Sdavidn 27920267Sjoerg return EXIT_SUCCESS; 28020253Sjoerg} 28120253Sjoerg 28220253Sjoerg 283176474Sscf/* 284176474Sscf * Lookup a passwd entry using a name or UID. 285176474Sscf */ 286176474Sscfstatic struct passwd * 287176474Sscflookup_pwent(const char *user) 288176474Sscf{ 289176474Sscf struct passwd *pwd; 290176474Sscf 291176474Sscf if ((pwd = GETPWNAM(user)) == NULL && 292176474Sscf (!isdigit((unsigned char)*user) || 293176474Sscf (pwd = getpwuid((uid_t) atoi(user))) == NULL)) 294176474Sscf errx(EX_NOUSER, "user `%s' does not exist", user); 295176474Sscf 296176474Sscf return (pwd); 297176474Sscf} 298176474Sscf 299176474Sscf 300176474Sscf/* 301176474Sscf * Delete requested members from a group. 302176474Sscf */ 303176474Sscfstatic void 304176474Sscfdelete_members(char ***members, int *grmembers, int *i, struct carg *arg, 305176474Sscf struct group *grp) 306176474Sscf{ 307176474Sscf bool matchFound; 308176474Sscf char *user; 309176474Sscf char *valueCopy; 310176474Sscf char *valuePtr; 311176474Sscf int k; 312176474Sscf struct passwd *pwd; 313176474Sscf 314176474Sscf k = 0; 315176474Sscf while (grp->gr_mem[k] != NULL) { 316176474Sscf matchFound = false; 317176474Sscf if ((valueCopy = strdup(arg->val)) == NULL) 318176474Sscf errx(EX_UNAVAILABLE, "out of memory"); 319176474Sscf valuePtr = valueCopy; 320176474Sscf while ((user = strsep(&valuePtr, ", \t")) != NULL) { 321176474Sscf pwd = lookup_pwent(user); 322176474Sscf if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) { 323176474Sscf matchFound = true; 324176474Sscf break; 325176474Sscf } 326176474Sscf } 327176474Sscf free(valueCopy); 328176474Sscf 329176474Sscf if (!matchFound && 330176474Sscf extendarray(members, grmembers, *i + 2) != -1) 331176474Sscf (*members)[(*i)++] = grp->gr_mem[k]; 332176474Sscf 333176474Sscf k++; 334176474Sscf } 335176474Sscf 336176474Sscf return; 337176474Sscf} 338176474Sscf 339176474Sscf 34020253Sjoergstatic gid_t 34120253Sjoerggr_gidpolicy(struct userconf * cnf, struct cargs * args) 34220253Sjoerg{ 34320253Sjoerg struct group *grp; 34420253Sjoerg gid_t gid = (gid_t) - 1; 34520253Sjoerg struct carg *a_gid = getarg(args, 'g'); 34620253Sjoerg 34720253Sjoerg /* 34820253Sjoerg * Check the given gid, if any 34920253Sjoerg */ 35020253Sjoerg if (a_gid != NULL) { 35120253Sjoerg gid = (gid_t) atol(a_gid->val); 35220253Sjoerg 35344229Sdavidn if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL) 35430259Scharnier errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid); 35520253Sjoerg } else { 35620253Sjoerg struct bitmap bm; 35720253Sjoerg 35820253Sjoerg /* 35920253Sjoerg * We need to allocate the next available gid under one of 36020253Sjoerg * two policies a) Grab the first unused gid b) Grab the 36120253Sjoerg * highest possible unused gid 36220253Sjoerg */ 36320253Sjoerg if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */ 36420253Sjoerg cnf->min_gid = 1000; 36520253Sjoerg cnf->max_gid = 32000; 36620253Sjoerg } 36720253Sjoerg bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1); 36820253Sjoerg 36920253Sjoerg /* 37020253Sjoerg * Now, let's fill the bitmap from the password file 37120253Sjoerg */ 37244229Sdavidn SETGRENT(); 37344229Sdavidn while ((grp = GETGRENT()) != NULL) 37456000Sdavidn if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid && 37556000Sdavidn (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid) 37620253Sjoerg bm_setbit(&bm, grp->gr_gid - cnf->min_gid); 37744229Sdavidn ENDGRENT(); 37820253Sjoerg 37920253Sjoerg /* 38020253Sjoerg * Then apply the policy, with fallback to reuse if necessary 38120253Sjoerg */ 38220253Sjoerg if (cnf->reuse_gids) 38320253Sjoerg gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 38420253Sjoerg else { 38520253Sjoerg gid = (gid_t) (bm_lastset(&bm) + 1); 38620253Sjoerg if (!bm_isset(&bm, gid)) 38720253Sjoerg gid += cnf->min_gid; 38820253Sjoerg else 38920253Sjoerg gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 39020253Sjoerg } 39120253Sjoerg 39220253Sjoerg /* 39320253Sjoerg * Another sanity check 39420253Sjoerg */ 39520253Sjoerg if (gid < cnf->min_gid || gid > cnf->max_gid) 39630259Scharnier errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used"); 39720253Sjoerg bm_dealloc(&bm); 39820253Sjoerg } 39920253Sjoerg return gid; 40020253Sjoerg} 40120253Sjoerg 40220253Sjoerg 40320253Sjoergstatic int 40420253Sjoergprint_group(struct group * grp, int pretty) 40520253Sjoerg{ 40620253Sjoerg if (!pretty) { 40720747Sdavidn char *buf = NULL; 40820253Sjoerg 409242349Sbapt buf = gr_make(grp); 410244738Sbapt printf("%s\n", buf); 41120747Sdavidn free(buf); 41220253Sjoerg } else { 41320253Sjoerg int i; 41420253Sjoerg 41522398Sdavidn printf("Group Name: %-15s #%lu\n" 41620747Sdavidn " Members: ", 41720253Sjoerg grp->gr_name, (long) grp->gr_gid); 41820747Sdavidn for (i = 0; grp->gr_mem[i]; i++) 41920253Sjoerg printf("%s%s", i ? "," : "", grp->gr_mem[i]); 42020253Sjoerg fputs("\n\n", stdout); 42120253Sjoerg } 42220267Sjoerg return EXIT_SUCCESS; 42320253Sjoerg} 424