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 230272192Sdteske if (grp->gr_mem != NULL) { 231272192Sdteske while (grp->gr_mem[k] != NULL) { 232272192Sdteske if (extendarray(&members, &grmembers, i + 2) != -1) 233272192Sdteske members[i++] = grp->gr_mem[k]; 234272192Sdteske k++; 235272192Sdteske } 23620267Sjoerg } 23720267Sjoerg } 238176474Sscf 239176474Sscf if (arg->ch != 'd') 240176474Sscf for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 241176474Sscf int j; 242176474Sscf 243176474Sscf /* 244176474Sscf * Check for duplicates 245176474Sscf */ 246176474Sscf pwd = lookup_pwent(p); 247176474Sscf for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++) 248176474Sscf ; 249176474Sscf if (j == i && extendarray(&members, &grmembers, i + 2) != -1) 250176474Sscf members[i++] = newstr(pwd->pw_name); 25120267Sjoerg } 25220747Sdavidn while (i < grmembers) 25320267Sjoerg members[i++] = NULL; 25420267Sjoerg grp->gr_mem = members; 25520267Sjoerg } 25620267Sjoerg 25720267Sjoerg if (getarg(args, 'N') != NULL) 25820267Sjoerg return print_group(grp, getarg(args, 'P') != NULL); 25920267Sjoerg 26052502Sdavidn if (mode == M_ADD && (rc = addgrent(grp)) != 0) { 26152502Sdavidn if (rc == -1) 26252502Sdavidn warnx("group '%s' already exists", grp->gr_name); 26352502Sdavidn else 26452502Sdavidn warn("group update"); 26520267Sjoerg return EX_IOERR; 26652502Sdavidn } else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) { 26752502Sdavidn if (rc == -1) 26852502Sdavidn warnx("group '%s' not available (NIS?)", grp->gr_name); 26952502Sdavidn else 27056000Sdavidn warn("group update"); 27152502Sdavidn return EX_IOERR; 27220253Sjoerg } 27320253Sjoerg /* grp may have been invalidated */ 27444229Sdavidn if ((grp = GETGRNAM(a_name->val)) == NULL) 27530259Scharnier errx(EX_SOFTWARE, "group disappeared during update"); 27620253Sjoerg 27720253Sjoerg pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid); 27820253Sjoerg 279243894Seadler free(members); 28020747Sdavidn 28120267Sjoerg return EXIT_SUCCESS; 28220253Sjoerg} 28320253Sjoerg 28420253Sjoerg 285176474Sscf/* 286176474Sscf * Lookup a passwd entry using a name or UID. 287176474Sscf */ 288176474Sscfstatic struct passwd * 289176474Sscflookup_pwent(const char *user) 290176474Sscf{ 291176474Sscf struct passwd *pwd; 292176474Sscf 293176474Sscf if ((pwd = GETPWNAM(user)) == NULL && 294176474Sscf (!isdigit((unsigned char)*user) || 295176474Sscf (pwd = getpwuid((uid_t) atoi(user))) == NULL)) 296176474Sscf errx(EX_NOUSER, "user `%s' does not exist", user); 297176474Sscf 298176474Sscf return (pwd); 299176474Sscf} 300176474Sscf 301176474Sscf 302176474Sscf/* 303176474Sscf * Delete requested members from a group. 304176474Sscf */ 305176474Sscfstatic void 306176474Sscfdelete_members(char ***members, int *grmembers, int *i, struct carg *arg, 307176474Sscf struct group *grp) 308176474Sscf{ 309176474Sscf bool matchFound; 310176474Sscf char *user; 311176474Sscf char *valueCopy; 312176474Sscf char *valuePtr; 313176474Sscf int k; 314176474Sscf struct passwd *pwd; 315176474Sscf 316272192Sdteske if (grp->gr_mem == NULL) 317272192Sdteske return; 318272192Sdteske 319176474Sscf k = 0; 320176474Sscf while (grp->gr_mem[k] != NULL) { 321176474Sscf matchFound = false; 322176474Sscf if ((valueCopy = strdup(arg->val)) == NULL) 323176474Sscf errx(EX_UNAVAILABLE, "out of memory"); 324176474Sscf valuePtr = valueCopy; 325176474Sscf while ((user = strsep(&valuePtr, ", \t")) != NULL) { 326176474Sscf pwd = lookup_pwent(user); 327176474Sscf if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) { 328176474Sscf matchFound = true; 329176474Sscf break; 330176474Sscf } 331176474Sscf } 332176474Sscf free(valueCopy); 333176474Sscf 334176474Sscf if (!matchFound && 335176474Sscf extendarray(members, grmembers, *i + 2) != -1) 336176474Sscf (*members)[(*i)++] = grp->gr_mem[k]; 337176474Sscf 338176474Sscf k++; 339176474Sscf } 340176474Sscf 341176474Sscf return; 342176474Sscf} 343176474Sscf 344176474Sscf 34520253Sjoergstatic gid_t 34620253Sjoerggr_gidpolicy(struct userconf * cnf, struct cargs * args) 34720253Sjoerg{ 34820253Sjoerg struct group *grp; 34920253Sjoerg gid_t gid = (gid_t) - 1; 35020253Sjoerg struct carg *a_gid = getarg(args, 'g'); 35120253Sjoerg 35220253Sjoerg /* 35320253Sjoerg * Check the given gid, if any 35420253Sjoerg */ 35520253Sjoerg if (a_gid != NULL) { 35620253Sjoerg gid = (gid_t) atol(a_gid->val); 35720253Sjoerg 35844229Sdavidn if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL) 35930259Scharnier errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid); 36020253Sjoerg } else { 36120253Sjoerg struct bitmap bm; 36220253Sjoerg 36320253Sjoerg /* 36420253Sjoerg * We need to allocate the next available gid under one of 36520253Sjoerg * two policies a) Grab the first unused gid b) Grab the 36620253Sjoerg * highest possible unused gid 36720253Sjoerg */ 36820253Sjoerg if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */ 36920253Sjoerg cnf->min_gid = 1000; 37020253Sjoerg cnf->max_gid = 32000; 37120253Sjoerg } 37220253Sjoerg bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1); 37320253Sjoerg 37420253Sjoerg /* 37520253Sjoerg * Now, let's fill the bitmap from the password file 37620253Sjoerg */ 37744229Sdavidn SETGRENT(); 37844229Sdavidn while ((grp = GETGRENT()) != NULL) 37956000Sdavidn if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid && 38056000Sdavidn (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid) 38120253Sjoerg bm_setbit(&bm, grp->gr_gid - cnf->min_gid); 38244229Sdavidn ENDGRENT(); 38320253Sjoerg 38420253Sjoerg /* 38520253Sjoerg * Then apply the policy, with fallback to reuse if necessary 38620253Sjoerg */ 38720253Sjoerg if (cnf->reuse_gids) 38820253Sjoerg gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 38920253Sjoerg else { 39020253Sjoerg gid = (gid_t) (bm_lastset(&bm) + 1); 39120253Sjoerg if (!bm_isset(&bm, gid)) 39220253Sjoerg gid += cnf->min_gid; 39320253Sjoerg else 39420253Sjoerg gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 39520253Sjoerg } 39620253Sjoerg 39720253Sjoerg /* 39820253Sjoerg * Another sanity check 39920253Sjoerg */ 40020253Sjoerg if (gid < cnf->min_gid || gid > cnf->max_gid) 40130259Scharnier errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used"); 40220253Sjoerg bm_dealloc(&bm); 40320253Sjoerg } 40420253Sjoerg return gid; 40520253Sjoerg} 40620253Sjoerg 40720253Sjoerg 40820253Sjoergstatic int 40920253Sjoergprint_group(struct group * grp, int pretty) 41020253Sjoerg{ 41120253Sjoerg if (!pretty) { 41220747Sdavidn char *buf = NULL; 41320253Sjoerg 414242349Sbapt buf = gr_make(grp); 415244738Sbapt printf("%s\n", buf); 41620747Sdavidn free(buf); 41720253Sjoerg } else { 41820253Sjoerg int i; 41920253Sjoerg 42022398Sdavidn printf("Group Name: %-15s #%lu\n" 42120747Sdavidn " Members: ", 42220253Sjoerg grp->gr_name, (long) grp->gr_gid); 423272192Sdteske if (grp->gr_mem != NULL) { 424272192Sdteske for (i = 0; grp->gr_mem[i]; i++) 425272192Sdteske printf("%s%s", i ? "," : "", grp->gr_mem[i]); 426272192Sdteske } 42720253Sjoerg fputs("\n\n", stdout); 42820253Sjoerg } 42920267Sjoerg return EXIT_SUCCESS; 43020253Sjoerg} 431