pw_group.c revision 284133
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 284133 2015-06-07 19:59:01Z bapt $"; 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); 47284122Sbaptstatic int print_group(struct group * grp); 48284133Sbaptstatic gid_t gr_gidpolicy(struct userconf * cnf, long id); 4920253Sjoerg 5020253Sjoergint 51284128Sbaptpw_group(int mode, char *name, long id, struct cargs * args) 5220253Sjoerg{ 5352502Sdavidn int rc; 5420253Sjoerg struct carg *arg; 5520253Sjoerg struct group *grp = NULL; 5620747Sdavidn int grmembers = 0; 5752502Sdavidn char **members = NULL; 58284118Sbapt struct userconf *cnf = conf.userconf; 5920253Sjoerg 6020253Sjoerg static struct group fakegroup = 6120253Sjoerg { 6220253Sjoerg "nogroup", 6320253Sjoerg "*", 6420253Sjoerg -1, 6520253Sjoerg NULL 6620253Sjoerg }; 6720253Sjoerg 6852512Sdavidn if (mode == M_LOCK || mode == M_UNLOCK) 6952512Sdavidn errx(EX_USAGE, "'lock' command is not available for groups"); 7052512Sdavidn 7120267Sjoerg /* 7220267Sjoerg * With M_NEXT, we only need to return the 7320267Sjoerg * next gid to stdout 7420267Sjoerg */ 7552512Sdavidn if (mode == M_NEXT) { 76284133Sbapt gid_t next = gr_gidpolicy(cnf, id); 7720267Sjoerg if (getarg(args, 'q')) 7820267Sjoerg return next; 79283842Sbapt printf("%u\n", next); 8020267Sjoerg return EXIT_SUCCESS; 8120267Sjoerg } 8220267Sjoerg 8320253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 8444229Sdavidn SETGRENT(); 8544229Sdavidn while ((grp = GETGRENT()) != NULL) 86284122Sbapt print_group(grp); 8744229Sdavidn ENDGRENT(); 8820267Sjoerg return EXIT_SUCCESS; 8920253Sjoerg } 90284128Sbapt if (id < 0 && name == NULL) 91284128Sbapt errx(EX_DATAERR, "group name or id required"); 9220253Sjoerg 93284128Sbapt grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id); 9420253Sjoerg 9520253Sjoerg if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 96284128Sbapt if (name == NULL && grp == NULL) /* Try harder */ 97284128Sbapt grp = GETGRGID(id); 9820253Sjoerg 9920253Sjoerg if (grp == NULL) { 10020253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 10120747Sdavidn char *fmems[1]; 10220747Sdavidn fmems[0] = NULL; 103284128Sbapt fakegroup.gr_name = name ? name : "nogroup"; 104284128Sbapt fakegroup.gr_gid = (gid_t) id; 10520747Sdavidn fakegroup.gr_mem = fmems; 106284122Sbapt return print_group(&fakegroup); 10720253Sjoerg } 108284128Sbapt if (name == NULL) 109284128Sbapt errx(EX_DATAERR, "unknown group `%s'", name); 110284128Sbapt else 111284128Sbapt errx(EX_DATAERR, "unknown group `%ld'", id); 11220253Sjoerg } 113284128Sbapt if (name == NULL) /* Needed later */ 114284128Sbapt name = grp->gr_name; 11520253Sjoerg 11620253Sjoerg /* 11720253Sjoerg * Handle deletions now 11820253Sjoerg */ 11920253Sjoerg if (mode == M_DELETE) { 12052502Sdavidn rc = delgrent(grp); 12152502Sdavidn if (rc == -1) 122284128Sbapt err(EX_IOERR, "group '%s' not available (NIS?)", 123284128Sbapt name); 12452502Sdavidn else if (rc != 0) { 125283814Sbapt err(EX_IOERR, "group update"); 12652502Sdavidn } 127284128Sbapt pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", name, id); 12820267Sjoerg return EXIT_SUCCESS; 12920253Sjoerg } else if (mode == M_PRINT) 130284122Sbapt return print_group(grp); 13120253Sjoerg 132284128Sbapt if (id > 0) 133284128Sbapt grp->gr_gid = (gid_t) id; 13420253Sjoerg 135284129Sbapt if (conf.newname != NULL) 136284129Sbapt grp->gr_name = pw_checkname(conf.newname, 0); 13720253Sjoerg } else { 138284128Sbapt if (name == NULL) /* Required */ 13930259Scharnier errx(EX_DATAERR, "group name required"); 14020253Sjoerg else if (grp != NULL) /* Exists */ 141284128Sbapt errx(EX_DATAERR, "group name `%s' already exists", name); 14220253Sjoerg 14320747Sdavidn extendarray(&members, &grmembers, 200); 14420747Sdavidn members[0] = NULL; 14520253Sjoerg grp = &fakegroup; 146284128Sbapt grp->gr_name = pw_checkname(name, 0); 14720253Sjoerg grp->gr_passwd = "*"; 148284133Sbapt grp->gr_gid = gr_gidpolicy(cnf, id); 14920253Sjoerg grp->gr_mem = members; 15020253Sjoerg } 15120253Sjoerg 15220253Sjoerg /* 15320253Sjoerg * This allows us to set a group password Group passwords is an 15420253Sjoerg * antique idea, rarely used and insecure (no secure database) Should 15520253Sjoerg * be discouraged, but it is apparently still supported by some 15620253Sjoerg * software. 15720253Sjoerg */ 15820253Sjoerg 159124382Siedowse if ((arg = getarg(args, 'h')) != NULL || 160124382Siedowse (arg = getarg(args, 'H')) != NULL) { 16120253Sjoerg if (strcmp(arg->val, "-") == 0) 16220253Sjoerg grp->gr_passwd = "*"; /* No access */ 16320253Sjoerg else { 16420253Sjoerg int fd = atoi(arg->val); 165124382Siedowse int precrypt = (arg->ch == 'H'); 16620253Sjoerg int b; 16720253Sjoerg int istty = isatty(fd); 16820253Sjoerg struct termios t; 16920253Sjoerg char *p, line[256]; 17020253Sjoerg 17120253Sjoerg if (istty) { 17220253Sjoerg if (tcgetattr(fd, &t) == -1) 17320253Sjoerg istty = 0; 17420253Sjoerg else { 17520253Sjoerg struct termios n = t; 17620253Sjoerg 17720253Sjoerg /* Disable echo */ 17820253Sjoerg n.c_lflag &= ~(ECHO); 17920253Sjoerg tcsetattr(fd, TCSANOW, &n); 18020253Sjoerg printf("%sassword for group %s:", (mode == M_UPDATE) ? "New p" : "P", grp->gr_name); 18120253Sjoerg fflush(stdout); 18220253Sjoerg } 18320253Sjoerg } 18420253Sjoerg b = read(fd, line, sizeof(line) - 1); 18520253Sjoerg if (istty) { /* Restore state */ 18620253Sjoerg tcsetattr(fd, TCSANOW, &t); 18720253Sjoerg fputc('\n', stdout); 18820253Sjoerg fflush(stdout); 18920253Sjoerg } 190283814Sbapt if (b < 0) 191283814Sbapt err(EX_OSERR, "-h file descriptor"); 19220253Sjoerg line[b] = '\0'; 19320253Sjoerg if ((p = strpbrk(line, " \t\r\n")) != NULL) 19420253Sjoerg *p = '\0'; 19520253Sjoerg if (!*line) 19630259Scharnier errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 197124382Siedowse if (precrypt) { 198124382Siedowse if (strchr(line, ':') != NULL) 199124382Siedowse return EX_DATAERR; 200124382Siedowse grp->gr_passwd = line; 201124382Siedowse } else 202124382Siedowse grp->gr_passwd = pw_pwcrypt(line); 20320253Sjoerg } 20420253Sjoerg } 20520267Sjoerg 206176474Sscf if (((arg = getarg(args, 'M')) != NULL || 207176474Sscf (arg = getarg(args, 'd')) != NULL || 208176474Sscf (arg = getarg(args, 'm')) != NULL) && arg->val) { 20920267Sjoerg int i = 0; 21020267Sjoerg char *p; 21120267Sjoerg struct passwd *pwd; 21220267Sjoerg 21320747Sdavidn /* Make sure this is not stay NULL with -M "" */ 21420747Sdavidn extendarray(&members, &grmembers, 200); 215176474Sscf if (arg->ch == 'd') 216176474Sscf delete_members(&members, &grmembers, &i, arg, grp); 217176474Sscf else if (arg->ch == 'm') { 21820747Sdavidn int k = 0; 21920747Sdavidn 220262864Sjulian if (grp->gr_mem != NULL) { 221262864Sjulian while (grp->gr_mem[k] != NULL) { 222262864Sjulian if (extendarray(&members, &grmembers, i + 2) != -1) 223262864Sjulian members[i++] = grp->gr_mem[k]; 224262864Sjulian k++; 225262864Sjulian } 22620267Sjoerg } 22720267Sjoerg } 228176474Sscf 229176474Sscf if (arg->ch != 'd') 230176474Sscf for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 231176474Sscf int j; 232176474Sscf 233176474Sscf /* 234176474Sscf * Check for duplicates 235176474Sscf */ 236176474Sscf pwd = lookup_pwent(p); 237176474Sscf for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++) 238176474Sscf ; 239176474Sscf if (j == i && extendarray(&members, &grmembers, i + 2) != -1) 240176474Sscf members[i++] = newstr(pwd->pw_name); 24120267Sjoerg } 24220747Sdavidn while (i < grmembers) 24320267Sjoerg members[i++] = NULL; 24420267Sjoerg grp->gr_mem = members; 24520267Sjoerg } 24620267Sjoerg 247284121Sbapt if (conf.dryrun) 248284122Sbapt return print_group(grp); 24920267Sjoerg 25052502Sdavidn if (mode == M_ADD && (rc = addgrent(grp)) != 0) { 25152502Sdavidn if (rc == -1) 252283814Sbapt errx(EX_IOERR, "group '%s' already exists", 253283814Sbapt grp->gr_name); 25452502Sdavidn else 255283814Sbapt err(EX_IOERR, "group update"); 256284128Sbapt } else if (mode == M_UPDATE && (rc = chggrent(name, grp)) != 0) { 25752502Sdavidn if (rc == -1) 258283814Sbapt errx(EX_IOERR, "group '%s' not available (NIS?)", 259283814Sbapt grp->gr_name); 26052502Sdavidn else 261283814Sbapt err(EX_IOERR, "group update"); 26220253Sjoerg } 263273772Sbapt 264284129Sbapt if (conf.newname != NULL) 265284129Sbapt name = conf.newname; 26620253Sjoerg /* grp may have been invalidated */ 267284128Sbapt if ((grp = GETGRNAM(name)) == NULL) 26830259Scharnier errx(EX_SOFTWARE, "group disappeared during update"); 26920253Sjoerg 270283842Sbapt pw_log(cnf, mode, W_GROUP, "%s(%u)", grp->gr_name, grp->gr_gid); 27120253Sjoerg 272243894Seadler free(members); 27320747Sdavidn 27420267Sjoerg return EXIT_SUCCESS; 27520253Sjoerg} 27620253Sjoerg 27720253Sjoerg 278176474Sscf/* 279176474Sscf * Lookup a passwd entry using a name or UID. 280176474Sscf */ 281176474Sscfstatic struct passwd * 282176474Sscflookup_pwent(const char *user) 283176474Sscf{ 284176474Sscf struct passwd *pwd; 285176474Sscf 286176474Sscf if ((pwd = GETPWNAM(user)) == NULL && 287176474Sscf (!isdigit((unsigned char)*user) || 288176474Sscf (pwd = getpwuid((uid_t) atoi(user))) == NULL)) 289176474Sscf errx(EX_NOUSER, "user `%s' does not exist", user); 290176474Sscf 291176474Sscf return (pwd); 292176474Sscf} 293176474Sscf 294176474Sscf 295176474Sscf/* 296176474Sscf * Delete requested members from a group. 297176474Sscf */ 298176474Sscfstatic void 299176474Sscfdelete_members(char ***members, int *grmembers, int *i, struct carg *arg, 300176474Sscf struct group *grp) 301176474Sscf{ 302176474Sscf bool matchFound; 303176474Sscf char *user; 304176474Sscf char *valueCopy; 305176474Sscf char *valuePtr; 306176474Sscf int k; 307176474Sscf struct passwd *pwd; 308176474Sscf 309262864Sjulian if (grp->gr_mem == NULL) 310262864Sjulian return; 311262864Sjulian 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 339284133Sbaptgr_gidpolicy(struct userconf * cnf, long id) 34020253Sjoerg{ 34120253Sjoerg struct group *grp; 34220253Sjoerg gid_t gid = (gid_t) - 1; 34320253Sjoerg 34420253Sjoerg /* 34520253Sjoerg * Check the given gid, if any 34620253Sjoerg */ 347284133Sbapt if (id > 0) { 348284133Sbapt gid = (gid_t) id; 34920253Sjoerg 350284133Sbapt if ((grp = GETGRGID(gid)) != NULL && conf.checkduplicate) 351283842Sbapt errx(EX_DATAERR, "gid `%u' has already been allocated", grp->gr_gid); 35220253Sjoerg } else { 35320253Sjoerg struct bitmap bm; 35420253Sjoerg 35520253Sjoerg /* 35620253Sjoerg * We need to allocate the next available gid under one of 35720253Sjoerg * two policies a) Grab the first unused gid b) Grab the 35820253Sjoerg * highest possible unused gid 35920253Sjoerg */ 36020253Sjoerg if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */ 36120253Sjoerg cnf->min_gid = 1000; 36220253Sjoerg cnf->max_gid = 32000; 36320253Sjoerg } 36420253Sjoerg bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1); 36520253Sjoerg 36620253Sjoerg /* 36720253Sjoerg * Now, let's fill the bitmap from the password file 36820253Sjoerg */ 36944229Sdavidn SETGRENT(); 37044229Sdavidn while ((grp = GETGRENT()) != NULL) 37156000Sdavidn if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid && 37256000Sdavidn (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid) 37320253Sjoerg bm_setbit(&bm, grp->gr_gid - cnf->min_gid); 37444229Sdavidn ENDGRENT(); 37520253Sjoerg 37620253Sjoerg /* 37720253Sjoerg * Then apply the policy, with fallback to reuse if necessary 37820253Sjoerg */ 37920253Sjoerg if (cnf->reuse_gids) 38020253Sjoerg gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 38120253Sjoerg else { 38220253Sjoerg gid = (gid_t) (bm_lastset(&bm) + 1); 38320253Sjoerg if (!bm_isset(&bm, gid)) 38420253Sjoerg gid += cnf->min_gid; 38520253Sjoerg else 38620253Sjoerg gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 38720253Sjoerg } 38820253Sjoerg 38920253Sjoerg /* 39020253Sjoerg * Another sanity check 39120253Sjoerg */ 39220253Sjoerg if (gid < cnf->min_gid || gid > cnf->max_gid) 39330259Scharnier errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used"); 39420253Sjoerg bm_dealloc(&bm); 39520253Sjoerg } 39620253Sjoerg return gid; 39720253Sjoerg} 39820253Sjoerg 39920253Sjoerg 40020253Sjoergstatic int 401284122Sbaptprint_group(struct group * grp) 40220253Sjoerg{ 403284122Sbapt if (!conf.pretty) { 40420747Sdavidn char *buf = NULL; 40520253Sjoerg 406242349Sbapt buf = gr_make(grp); 407244738Sbapt printf("%s\n", buf); 40820747Sdavidn free(buf); 40920253Sjoerg } else { 41020253Sjoerg int i; 41120253Sjoerg 41222398Sdavidn printf("Group Name: %-15s #%lu\n" 41320747Sdavidn " Members: ", 41420253Sjoerg grp->gr_name, (long) grp->gr_gid); 415262864Sjulian if (grp->gr_mem != NULL) { 416262864Sjulian for (i = 0; grp->gr_mem[i]; i++) 417262864Sjulian printf("%s%s", i ? "," : "", grp->gr_mem[i]); 418262864Sjulian } 41920253Sjoerg fputs("\n\n", stdout); 42020253Sjoerg } 42120267Sjoerg return EXIT_SUCCESS; 42220253Sjoerg} 423