pw_group.c revision 176474
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 176474 2008-02-23 01:25:22Z scf $"; 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 27520747Sdavidn if (members) 27620747Sdavidn free(members); 27720747Sdavidn 27820267Sjoerg return EXIT_SUCCESS; 27920253Sjoerg} 28020253Sjoerg 28120253Sjoerg 282176474Sscf/* 283176474Sscf * Lookup a passwd entry using a name or UID. 284176474Sscf */ 285176474Sscfstatic struct passwd * 286176474Sscflookup_pwent(const char *user) 287176474Sscf{ 288176474Sscf struct passwd *pwd; 289176474Sscf 290176474Sscf if ((pwd = GETPWNAM(user)) == NULL && 291176474Sscf (!isdigit((unsigned char)*user) || 292176474Sscf (pwd = getpwuid((uid_t) atoi(user))) == NULL)) 293176474Sscf errx(EX_NOUSER, "user `%s' does not exist", user); 294176474Sscf 295176474Sscf return (pwd); 296176474Sscf} 297176474Sscf 298176474Sscf 299176474Sscf/* 300176474Sscf * Delete requested members from a group. 301176474Sscf */ 302176474Sscfstatic void 303176474Sscfdelete_members(char ***members, int *grmembers, int *i, struct carg *arg, 304176474Sscf struct group *grp) 305176474Sscf{ 306176474Sscf bool matchFound; 307176474Sscf char *user; 308176474Sscf char *valueCopy; 309176474Sscf char *valuePtr; 310176474Sscf int k; 311176474Sscf struct passwd *pwd; 312176474Sscf 313176474Sscf k = 0; 314176474Sscf while (grp->gr_mem[k] != NULL) { 315176474Sscf matchFound = false; 316176474Sscf if ((valueCopy = strdup(arg->val)) == NULL) 317176474Sscf errx(EX_UNAVAILABLE, "out of memory"); 318176474Sscf valuePtr = valueCopy; 319176474Sscf while ((user = strsep(&valuePtr, ", \t")) != NULL) { 320176474Sscf pwd = lookup_pwent(user); 321176474Sscf if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) { 322176474Sscf matchFound = true; 323176474Sscf break; 324176474Sscf } 325176474Sscf } 326176474Sscf free(valueCopy); 327176474Sscf 328176474Sscf if (!matchFound && 329176474Sscf extendarray(members, grmembers, *i + 2) != -1) 330176474Sscf (*members)[(*i)++] = grp->gr_mem[k]; 331176474Sscf 332176474Sscf k++; 333176474Sscf } 334176474Sscf 335176474Sscf return; 336176474Sscf} 337176474Sscf 338176474Sscf 33920253Sjoergstatic gid_t 34020253Sjoerggr_gidpolicy(struct userconf * cnf, struct cargs * args) 34120253Sjoerg{ 34220253Sjoerg struct group *grp; 34320253Sjoerg gid_t gid = (gid_t) - 1; 34420253Sjoerg struct carg *a_gid = getarg(args, 'g'); 34520253Sjoerg 34620253Sjoerg /* 34720253Sjoerg * Check the given gid, if any 34820253Sjoerg */ 34920253Sjoerg if (a_gid != NULL) { 35020253Sjoerg gid = (gid_t) atol(a_gid->val); 35120253Sjoerg 35244229Sdavidn if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL) 35330259Scharnier errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid); 35420253Sjoerg } else { 35520253Sjoerg struct bitmap bm; 35620253Sjoerg 35720253Sjoerg /* 35820253Sjoerg * We need to allocate the next available gid under one of 35920253Sjoerg * two policies a) Grab the first unused gid b) Grab the 36020253Sjoerg * highest possible unused gid 36120253Sjoerg */ 36220253Sjoerg if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */ 36320253Sjoerg cnf->min_gid = 1000; 36420253Sjoerg cnf->max_gid = 32000; 36520253Sjoerg } 36620253Sjoerg bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1); 36720253Sjoerg 36820253Sjoerg /* 36920253Sjoerg * Now, let's fill the bitmap from the password file 37020253Sjoerg */ 37144229Sdavidn SETGRENT(); 37244229Sdavidn while ((grp = GETGRENT()) != NULL) 37356000Sdavidn if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid && 37456000Sdavidn (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid) 37520253Sjoerg bm_setbit(&bm, grp->gr_gid - cnf->min_gid); 37644229Sdavidn ENDGRENT(); 37720253Sjoerg 37820253Sjoerg /* 37920253Sjoerg * Then apply the policy, with fallback to reuse if necessary 38020253Sjoerg */ 38120253Sjoerg if (cnf->reuse_gids) 38220253Sjoerg gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 38320253Sjoerg else { 38420253Sjoerg gid = (gid_t) (bm_lastset(&bm) + 1); 38520253Sjoerg if (!bm_isset(&bm, gid)) 38620253Sjoerg gid += cnf->min_gid; 38720253Sjoerg else 38820253Sjoerg gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 38920253Sjoerg } 39020253Sjoerg 39120253Sjoerg /* 39220253Sjoerg * Another sanity check 39320253Sjoerg */ 39420253Sjoerg if (gid < cnf->min_gid || gid > cnf->max_gid) 39530259Scharnier errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used"); 39620253Sjoerg bm_dealloc(&bm); 39720253Sjoerg } 39820253Sjoerg return gid; 39920253Sjoerg} 40020253Sjoerg 40120253Sjoerg 40220253Sjoergstatic int 40320253Sjoergprint_group(struct group * grp, int pretty) 40420253Sjoerg{ 40520253Sjoerg if (!pretty) { 40620747Sdavidn int buflen = 0; 40720747Sdavidn char *buf = NULL; 40820253Sjoerg 40920747Sdavidn fmtgrent(&buf, &buflen, grp); 41020253Sjoerg fputs(buf, stdout); 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