pw_group.c revision 284121
1/*- 2 * Copyright (C) 1996 3 * David L. Nugent. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#ifndef lint 28static const char rcsid[] = 29 "$FreeBSD: head/usr.sbin/pw/pw_group.c 284121 2015-06-07 15:09:53Z bapt $"; 30#endif /* not lint */ 31 32#include <ctype.h> 33#include <err.h> 34#include <termios.h> 35#include <stdbool.h> 36#include <unistd.h> 37#include <grp.h> 38#include <libutil.h> 39 40#include "pw.h" 41#include "bitmap.h" 42 43 44static struct passwd *lookup_pwent(const char *user); 45static void delete_members(char ***members, int *grmembers, int *i, 46 struct carg *arg, struct group *grp); 47static int print_group(struct group * grp, int pretty); 48static gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args); 49 50int 51pw_group(int mode, struct cargs * args) 52{ 53 int rc; 54 struct carg *a_newname = getarg(args, 'l'); 55 struct carg *a_name = getarg(args, 'n'); 56 struct carg *a_gid = getarg(args, 'g'); 57 struct carg *arg; 58 struct group *grp = NULL; 59 int grmembers = 0; 60 char **members = NULL; 61 struct userconf *cnf = conf.userconf; 62 63 static struct group fakegroup = 64 { 65 "nogroup", 66 "*", 67 -1, 68 NULL 69 }; 70 71 if (a_gid != NULL) { 72 if (strspn(a_gid->val, "0123456789") != strlen(a_gid->val)) 73 errx(EX_USAGE, "-g expects a number"); 74 } 75 76 if (mode == M_LOCK || mode == M_UNLOCK) 77 errx(EX_USAGE, "'lock' command is not available for groups"); 78 79 /* 80 * With M_NEXT, we only need to return the 81 * next gid to stdout 82 */ 83 if (mode == M_NEXT) { 84 gid_t next = gr_gidpolicy(cnf, args); 85 if (getarg(args, 'q')) 86 return next; 87 printf("%u\n", next); 88 return EXIT_SUCCESS; 89 } 90 91 if (mode == M_PRINT && getarg(args, 'a')) { 92 int pretty = getarg(args, 'P') != NULL; 93 94 SETGRENT(); 95 while ((grp = GETGRENT()) != NULL) 96 print_group(grp, pretty); 97 ENDGRENT(); 98 return EXIT_SUCCESS; 99 } 100 if (a_gid == NULL) { 101 if (a_name == NULL) 102 errx(EX_DATAERR, "group name or id required"); 103 104 if (mode != M_ADD && grp == NULL && isdigit((unsigned char)*a_name->val)) { 105 (a_gid = a_name)->ch = 'g'; 106 a_name = NULL; 107 } 108 } 109 grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val)); 110 111 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 112 if (a_name == NULL && grp == NULL) /* Try harder */ 113 grp = GETGRGID(atoi(a_gid->val)); 114 115 if (grp == NULL) { 116 if (mode == M_PRINT && getarg(args, 'F')) { 117 char *fmems[1]; 118 fmems[0] = NULL; 119 fakegroup.gr_name = a_name ? a_name->val : "nogroup"; 120 fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : (gid_t)-1; 121 fakegroup.gr_mem = fmems; 122 return print_group(&fakegroup, getarg(args, 'P') != NULL); 123 } 124 errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val); 125 } 126 if (a_name == NULL) /* Needed later */ 127 a_name = addarg(args, 'n', grp->gr_name); 128 129 /* 130 * Handle deletions now 131 */ 132 if (mode == M_DELETE) { 133 gid_t gid = grp->gr_gid; 134 135 rc = delgrent(grp); 136 if (rc == -1) 137 err(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name); 138 else if (rc != 0) { 139 err(EX_IOERR, "group update"); 140 } 141 pw_log(cnf, mode, W_GROUP, "%s(%u) removed", a_name->val, gid); 142 return EXIT_SUCCESS; 143 } else if (mode == M_PRINT) 144 return print_group(grp, getarg(args, 'P') != NULL); 145 146 if (a_gid) 147 grp->gr_gid = (gid_t) atoi(a_gid->val); 148 149 if (a_newname != NULL) 150 grp->gr_name = pw_checkname(a_newname->val, 0); 151 } else { 152 if (a_name == NULL) /* Required */ 153 errx(EX_DATAERR, "group name required"); 154 else if (grp != NULL) /* Exists */ 155 errx(EX_DATAERR, "group name `%s' already exists", a_name->val); 156 157 extendarray(&members, &grmembers, 200); 158 members[0] = NULL; 159 grp = &fakegroup; 160 grp->gr_name = pw_checkname(a_name->val, 0); 161 grp->gr_passwd = "*"; 162 grp->gr_gid = gr_gidpolicy(cnf, args); 163 grp->gr_mem = members; 164 } 165 166 /* 167 * This allows us to set a group password Group passwords is an 168 * antique idea, rarely used and insecure (no secure database) Should 169 * be discouraged, but it is apparently still supported by some 170 * software. 171 */ 172 173 if ((arg = getarg(args, 'h')) != NULL || 174 (arg = getarg(args, 'H')) != NULL) { 175 if (strcmp(arg->val, "-") == 0) 176 grp->gr_passwd = "*"; /* No access */ 177 else { 178 int fd = atoi(arg->val); 179 int precrypt = (arg->ch == 'H'); 180 int b; 181 int istty = isatty(fd); 182 struct termios t; 183 char *p, line[256]; 184 185 if (istty) { 186 if (tcgetattr(fd, &t) == -1) 187 istty = 0; 188 else { 189 struct termios n = t; 190 191 /* Disable echo */ 192 n.c_lflag &= ~(ECHO); 193 tcsetattr(fd, TCSANOW, &n); 194 printf("%sassword for group %s:", (mode == M_UPDATE) ? "New p" : "P", grp->gr_name); 195 fflush(stdout); 196 } 197 } 198 b = read(fd, line, sizeof(line) - 1); 199 if (istty) { /* Restore state */ 200 tcsetattr(fd, TCSANOW, &t); 201 fputc('\n', stdout); 202 fflush(stdout); 203 } 204 if (b < 0) 205 err(EX_OSERR, "-h file descriptor"); 206 line[b] = '\0'; 207 if ((p = strpbrk(line, " \t\r\n")) != NULL) 208 *p = '\0'; 209 if (!*line) 210 errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 211 if (precrypt) { 212 if (strchr(line, ':') != NULL) 213 return EX_DATAERR; 214 grp->gr_passwd = line; 215 } else 216 grp->gr_passwd = pw_pwcrypt(line); 217 } 218 } 219 220 if (((arg = getarg(args, 'M')) != NULL || 221 (arg = getarg(args, 'd')) != NULL || 222 (arg = getarg(args, 'm')) != NULL) && arg->val) { 223 int i = 0; 224 char *p; 225 struct passwd *pwd; 226 227 /* Make sure this is not stay NULL with -M "" */ 228 extendarray(&members, &grmembers, 200); 229 if (arg->ch == 'd') 230 delete_members(&members, &grmembers, &i, arg, grp); 231 else if (arg->ch == 'm') { 232 int k = 0; 233 234 if (grp->gr_mem != NULL) { 235 while (grp->gr_mem[k] != NULL) { 236 if (extendarray(&members, &grmembers, i + 2) != -1) 237 members[i++] = grp->gr_mem[k]; 238 k++; 239 } 240 } 241 } 242 243 if (arg->ch != 'd') 244 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 245 int j; 246 247 /* 248 * Check for duplicates 249 */ 250 pwd = lookup_pwent(p); 251 for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++) 252 ; 253 if (j == i && extendarray(&members, &grmembers, i + 2) != -1) 254 members[i++] = newstr(pwd->pw_name); 255 } 256 while (i < grmembers) 257 members[i++] = NULL; 258 grp->gr_mem = members; 259 } 260 261 if (conf.dryrun) 262 return print_group(grp, getarg(args, 'P') != NULL); 263 264 if (mode == M_ADD && (rc = addgrent(grp)) != 0) { 265 if (rc == -1) 266 errx(EX_IOERR, "group '%s' already exists", 267 grp->gr_name); 268 else 269 err(EX_IOERR, "group update"); 270 } else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) { 271 if (rc == -1) 272 errx(EX_IOERR, "group '%s' not available (NIS?)", 273 grp->gr_name); 274 else 275 err(EX_IOERR, "group update"); 276 } 277 278 arg = a_newname != NULL ? a_newname : a_name; 279 /* grp may have been invalidated */ 280 if ((grp = GETGRNAM(arg->val)) == NULL) 281 errx(EX_SOFTWARE, "group disappeared during update"); 282 283 pw_log(cnf, mode, W_GROUP, "%s(%u)", grp->gr_name, grp->gr_gid); 284 285 free(members); 286 287 return EXIT_SUCCESS; 288} 289 290 291/* 292 * Lookup a passwd entry using a name or UID. 293 */ 294static struct passwd * 295lookup_pwent(const char *user) 296{ 297 struct passwd *pwd; 298 299 if ((pwd = GETPWNAM(user)) == NULL && 300 (!isdigit((unsigned char)*user) || 301 (pwd = getpwuid((uid_t) atoi(user))) == NULL)) 302 errx(EX_NOUSER, "user `%s' does not exist", user); 303 304 return (pwd); 305} 306 307 308/* 309 * Delete requested members from a group. 310 */ 311static void 312delete_members(char ***members, int *grmembers, int *i, struct carg *arg, 313 struct group *grp) 314{ 315 bool matchFound; 316 char *user; 317 char *valueCopy; 318 char *valuePtr; 319 int k; 320 struct passwd *pwd; 321 322 if (grp->gr_mem == NULL) 323 return; 324 325 k = 0; 326 while (grp->gr_mem[k] != NULL) { 327 matchFound = false; 328 if ((valueCopy = strdup(arg->val)) == NULL) 329 errx(EX_UNAVAILABLE, "out of memory"); 330 valuePtr = valueCopy; 331 while ((user = strsep(&valuePtr, ", \t")) != NULL) { 332 pwd = lookup_pwent(user); 333 if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) { 334 matchFound = true; 335 break; 336 } 337 } 338 free(valueCopy); 339 340 if (!matchFound && 341 extendarray(members, grmembers, *i + 2) != -1) 342 (*members)[(*i)++] = grp->gr_mem[k]; 343 344 k++; 345 } 346 347 return; 348} 349 350 351static gid_t 352gr_gidpolicy(struct userconf * cnf, struct cargs * args) 353{ 354 struct group *grp; 355 gid_t gid = (gid_t) - 1; 356 struct carg *a_gid = getarg(args, 'g'); 357 358 /* 359 * Check the given gid, if any 360 */ 361 if (a_gid != NULL) { 362 gid = (gid_t) atol(a_gid->val); 363 364 if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL) 365 errx(EX_DATAERR, "gid `%u' has already been allocated", grp->gr_gid); 366 } else { 367 struct bitmap bm; 368 369 /* 370 * We need to allocate the next available gid under one of 371 * two policies a) Grab the first unused gid b) Grab the 372 * highest possible unused gid 373 */ 374 if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */ 375 cnf->min_gid = 1000; 376 cnf->max_gid = 32000; 377 } 378 bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1); 379 380 /* 381 * Now, let's fill the bitmap from the password file 382 */ 383 SETGRENT(); 384 while ((grp = GETGRENT()) != NULL) 385 if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid && 386 (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid) 387 bm_setbit(&bm, grp->gr_gid - cnf->min_gid); 388 ENDGRENT(); 389 390 /* 391 * Then apply the policy, with fallback to reuse if necessary 392 */ 393 if (cnf->reuse_gids) 394 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 395 else { 396 gid = (gid_t) (bm_lastset(&bm) + 1); 397 if (!bm_isset(&bm, gid)) 398 gid += cnf->min_gid; 399 else 400 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid); 401 } 402 403 /* 404 * Another sanity check 405 */ 406 if (gid < cnf->min_gid || gid > cnf->max_gid) 407 errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used"); 408 bm_dealloc(&bm); 409 } 410 return gid; 411} 412 413 414static int 415print_group(struct group * grp, int pretty) 416{ 417 if (!pretty) { 418 char *buf = NULL; 419 420 buf = gr_make(grp); 421 printf("%s\n", buf); 422 free(buf); 423 } else { 424 int i; 425 426 printf("Group Name: %-15s #%lu\n" 427 " Members: ", 428 grp->gr_name, (long) grp->gr_gid); 429 if (grp->gr_mem != NULL) { 430 for (i = 0; grp->gr_mem[i]; i++) 431 printf("%s%s", i ? "," : "", grp->gr_mem[i]); 432 } 433 fputs("\n\n", stdout); 434 } 435 return EXIT_SUCCESS; 436} 437