1 /* $NetBSD: user.c,v 1.80 2005/06/14 18:29:58 agc Exp $ */ 2 3 /* 4 * Copyright (c) 1999 Alistair G. Crooks. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Alistair G. Crooks. 17 * 4. The name of the author may not be used to endorse or promote 18 * products derived from this software without specific prior written 19 * permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 #include <sys/cdefs.h> 34 35 #ifndef lint 36 __COPYRIGHT("@(#) Copyright (c) 1999 \ 37 The NetBSD Foundation, Inc. All rights reserved."); 38 __RCSID("$NetBSD: user.c,v 1.80 2005/06/14 18:29:58 agc Exp $"); 39 #endif 40 41 #include <sys/types.h> 42 #include <sys/param.h> 43 #include <sys/stat.h> 44 45 #include <ctype.h> 46 #include <dirent.h> 47 #include <err.h> 48 #include <fcntl.h> 49 #include <grp.h> 50 #include <paths.h> 51 #include <pwd.h> 52 #include <regex.h> 53 #include <stdarg.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <syslog.h> 58 #include <time.h> 59 #include <unistd.h> 60 #include <util.h> 61 62 #include "defs.h" 63 #include "usermgmt.h" 64 65 66 /* this struct describes a uid range */ 67 typedef struct range_t { 68 int r_from; /* low uid */ 69 int r_to; /* high uid */ 70 } range_t; 71 72 /* this struct encapsulates the user information */ 73 typedef struct user_t { 74 int u_flags; /* see below */ 75 int u_uid; /* uid of user */ 76 char *u_password; /* encrypted password */ 77 char *u_comment; /* comment field */ 78 char *u_home; /* home directory */ 79 char *u_primgrp; /* primary group */ 80 int u_groupc; /* # of secondary groups */ 81 const char *u_groupv[NGROUPS_MAX]; /* secondary groups */ 82 char *u_shell; /* user's shell */ 83 char *u_basedir; /* base directory for home */ 84 char *u_expire; /* when password will expire */ 85 char *u_inactive; /* when account will expire */ 86 char *u_skeldir; /* directory for startup files */ 87 char *u_class; /* login class */ 88 unsigned u_rsize; /* size of range array */ 89 unsigned u_rc; /* # of ranges */ 90 range_t *u_rv; /* the ranges */ 91 unsigned u_defrc; /* # of ranges in defaults */ 92 int u_preserve; /* preserve uids on deletion */ 93 int u_allow_samba; /* allow trailing '$' for samba login names */ 94 } user_t; 95 96 /* flags for which fields of the user_t replace the passwd entry */ 97 enum { 98 F_COMMENT = 0x0001, 99 F_DUPUID = 0x0002, 100 F_EXPIRE = 0x0004, 101 F_GROUP = 0x0008, 102 F_HOMEDIR = 0x0010, 103 F_MKDIR = 0x0020, 104 F_INACTIVE = 0x0040, 105 F_PASSWORD = 0x0080, 106 F_SECGROUP = 0x0100, 107 F_SHELL = 0x0200, 108 F_UID = 0x0400, 109 F_USERNAME = 0x0800, 110 F_CLASS = 0x1000 111 }; 112 113 #define CONFFILE "/etc/usermgmt.conf" 114 115 #ifndef DEF_GROUP 116 #define DEF_GROUP "users" 117 #endif 118 119 #ifndef DEF_BASEDIR 120 #define DEF_BASEDIR "/home" 121 #endif 122 123 #ifndef DEF_SKELDIR 124 #define DEF_SKELDIR "/etc/skel" 125 #endif 126 127 #ifndef DEF_SHELL 128 #define DEF_SHELL _PATH_CSHELL 129 #endif 130 131 #ifndef DEF_COMMENT 132 #define DEF_COMMENT "" 133 #endif 134 135 #ifndef DEF_LOWUID 136 #define DEF_LOWUID 1000 137 #endif 138 139 #ifndef DEF_HIGHUID 140 #define DEF_HIGHUID 60000 141 #endif 142 143 #ifndef DEF_INACTIVE 144 #define DEF_INACTIVE 0 145 #endif 146 147 #ifndef DEF_EXPIRE 148 #define DEF_EXPIRE NULL 149 #endif 150 151 #ifndef DEF_CLASS 152 #define DEF_CLASS "" 153 #endif 154 155 #ifndef WAITSECS 156 #define WAITSECS 10 157 #endif 158 159 #ifndef NOBODY_UID 160 #define NOBODY_UID 32767 161 #endif 162 163 /* some useful constants */ 164 enum { 165 MaxShellNameLen = 256, 166 MaxFileNameLen = MAXPATHLEN, 167 MaxUserNameLen = 32, 168 MaxFieldNameLen = 32, 169 MaxCommandLen = 2048, 170 MaxEntryLen = 2048, 171 PasswordLength = 2048, 172 173 DES_Len = 13, 174 175 LowGid = DEF_LOWUID, 176 HighGid = DEF_HIGHUID 177 }; 178 179 /* Full paths of programs used here */ 180 #define CHMOD "/bin/chmod" 181 #define CHOWN "/usr/sbin/chown" 182 #define MKDIR "/bin/mkdir" 183 #define MV "/bin/mv" 184 #define NOLOGIN "/sbin/nologin" 185 #define PAX "/bin/pax" 186 #define RM "/bin/rm" 187 188 #define UNSET_INACTIVE "Null (unset)" 189 #define UNSET_EXPIRY "Null (unset)" 190 191 static int asystem(const char *fmt, ...) 192 __attribute__((__format__(__printf__, 1, 2))); 193 194 static int verbose; 195 196 /* if *cpp is non-null, free it, then assign `n' chars of `s' to it */ 197 static void 198 memsave(char **cpp, const char *s, size_t n) 199 { 200 if (*cpp != NULL) { 201 FREE(*cpp); 202 } 203 NEWARRAY(char, *cpp, n + 1, exit(1)); 204 (void) memcpy(*cpp, s, n); 205 (*cpp)[n] = '\0'; 206 } 207 208 /* a replacement for system(3) */ 209 static int 210 asystem(const char *fmt, ...) 211 { 212 va_list vp; 213 char buf[MaxCommandLen]; 214 int ret; 215 216 va_start(vp, fmt); 217 (void) vsnprintf(buf, sizeof(buf), fmt, vp); 218 va_end(vp); 219 if (verbose) { 220 (void) printf("Command: %s\n", buf); 221 } 222 if ((ret = system(buf)) != 0) { 223 warnx("[Warning] can't system `%s'", buf); 224 } 225 return ret; 226 } 227 228 /* remove a users home directory, returning 1 for success (ie, no problems encountered) */ 229 static int 230 removehomedir(const char *user, int uid, const char *dir) 231 { 232 struct stat st; 233 234 /* userid not root? */ 235 if (uid == 0) { 236 warnx("Not deleting home directory `%s'; userid is 0", dir); 237 return 0; 238 } 239 240 /* directory exists (and is a directory!) */ 241 if (stat(dir, &st) < 0) { 242 warnx("Home directory `%s' doesn't exist", dir); 243 return 0; 244 } 245 if (!S_ISDIR(st.st_mode)) { 246 warnx("Home directory `%s' is not a directory", dir); 247 return 0; 248 } 249 250 /* userid matches directory owner? */ 251 if (st.st_uid != uid) { 252 warnx("User `%s' doesn't own directory `%s', not removed", user, dir); 253 return 0; 254 } 255 256 (void) seteuid(uid); 257 /* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */ 258 (void) asystem("%s -rf %s > /dev/null 2>&1 || true", RM, dir); 259 (void) seteuid(0); 260 if (rmdir(dir) < 0) { 261 warnx("Unable to remove all files in `%s'", dir); 262 return 0; 263 } 264 return 1; 265 } 266 267 #define NetBSD_1_4_K 104110000 268 269 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_4_K) 270 /* bounds checking strncpy */ 271 static int 272 strlcpy(char *to, char *from, size_t tosize) 273 { 274 size_t n; 275 int fromsize; 276 277 fromsize = strlen(from); 278 n = MIN(tosize - 1, fromsize); 279 (void) memcpy(to, from, n); 280 to[n] = '\0'; 281 return fromsize; 282 } 283 #endif /* NetBSD < 1.4K */ 284 285 /* 286 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 287 * All rights reserved. 288 * 289 * Redistribution and use in source and binary forms, with or without 290 * modification, are permitted provided that the following conditions 291 * are met: 292 * 1. Redistributions of source code must retain the above copyright 293 * notice, this list of conditions and the following disclaimer. 294 * 2. Redistributions in binary form must reproduce the above copyright 295 * notice, this list of conditions and the following disclaimer in the 296 * documentation and/or other materials provided with the distribution. 297 * 3. The name of the author may not be used to endorse or promote products 298 * derived from this software without specific prior written permission. 299 * 300 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 301 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 302 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 303 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 304 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 305 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 306 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 307 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 308 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 309 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 310 */ 311 312 /* 313 * research has shown that NetBSD 1.3H was the first version of -current 314 * with asprintf in libc. agc 315 */ 316 #define NetBSD_1_3_H 103080000 317 318 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_3_H) 319 320 int 321 asprintf(char **str, char const *fmt, ...) 322 { 323 int ret; 324 va_list ap; 325 FILE f; 326 unsigned char *_base; 327 328 f._flags = __SWR | __SSTR | __SALC; 329 f._bf._base = f._p = (unsigned char *)malloc(128); 330 if (f._bf._base == NULL) 331 goto err; 332 f._bf._size = f._w = 127; /* Leave room for the NUL */ 333 va_start(ap, fmt); 334 ret = vfprintf(&f, fmt, ap); 335 va_end(ap); 336 if (ret == -1) 337 goto err; 338 *f._p = '\0'; 339 _base = realloc(f._bf._base, (size_t)(ret + 1)); 340 if (_base == NULL) 341 goto err; 342 *str = (char *)_base; 343 return (ret); 344 345 err: 346 if (f._bf._base) 347 free(f._bf._base); 348 *str = NULL; 349 return (-1); 350 } 351 #endif /* NetBSD < 1.3H */ 352 353 /* return 1 if all of `s' is numeric */ 354 static int 355 is_number(char *s) 356 { 357 for ( ; *s ; s++) { 358 if (!isdigit((unsigned char) *s)) { 359 return 0; 360 } 361 } 362 return 1; 363 } 364 365 /* 366 * check that the effective uid is 0 - called from funcs which will 367 * modify data and config files. 368 */ 369 static void 370 checkeuid(void) 371 { 372 if (geteuid() != 0) { 373 errx(EXIT_FAILURE, "Program must be run as root"); 374 } 375 } 376 377 /* copy any dot files into the user's home directory */ 378 static int 379 copydotfiles(char *skeldir, int uid, int gid, char *dir) 380 { 381 struct dirent *dp; 382 DIR *dirp; 383 int n; 384 385 if ((dirp = opendir(skeldir)) == NULL) { 386 warn("can't open source . files dir `%s'", skeldir); 387 return 0; 388 } 389 for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) { 390 if (strcmp(dp->d_name, ".") == 0 || 391 strcmp(dp->d_name, "..") == 0) { 392 continue; 393 } 394 n = 1; 395 } 396 (void) closedir(dirp); 397 if (n == 0) { 398 warnx("No \"dot\" initialisation files found"); 399 } else { 400 (void) asystem("cd %s && %s -rw -pe %s . %s", 401 skeldir, PAX, (verbose) ? "-v" : "", dir); 402 } 403 (void) asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir); 404 (void) asystem("%s -R u+w %s", CHMOD, dir); 405 return n; 406 } 407 408 /* create a group entry with gid `gid' */ 409 static int 410 creategid(char *group, int gid, const char *name) 411 { 412 struct stat st; 413 FILE *from; 414 FILE *to; 415 char buf[MaxEntryLen]; 416 char f[MaxFileNameLen]; 417 int fd; 418 int cc; 419 420 if (getgrnam(group) != NULL) { 421 warnx("group `%s' already exists", group); 422 return 0; 423 } 424 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 425 warn("can't create gid for `%s': can't open `%s'", name, _PATH_GROUP); 426 return 0; 427 } 428 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 429 warn("can't lock `%s'", _PATH_GROUP); 430 } 431 (void) fstat(fileno(from), &st); 432 (void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 433 if ((fd = mkstemp(f)) < 0) { 434 (void) fclose(from); 435 warn("can't create gid: mkstemp failed"); 436 return 0; 437 } 438 if ((to = fdopen(fd, "w")) == NULL) { 439 (void) fclose(from); 440 (void) close(fd); 441 (void) unlink(f); 442 warn("can't create gid: fdopen `%s' failed", f); 443 return 0; 444 } 445 while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) { 446 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 447 (void) fclose(from); 448 (void) close(fd); 449 (void) unlink(f); 450 warn("can't create gid: short write to `%s'", f); 451 return 0; 452 } 453 } 454 (void) fprintf(to, "%s:*:%d:%s\n", group, gid, name); 455 (void) fclose(from); 456 (void) fclose(to); 457 if (rename(f, _PATH_GROUP) < 0) { 458 (void) unlink(f); 459 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP); 460 return 0; 461 } 462 (void) chmod(_PATH_GROUP, st.st_mode & 07777); 463 syslog(LOG_INFO, "new group added: name=%s, gid=%d", group, gid); 464 return 1; 465 } 466 467 /* modify the group entry with name `group' to be newent */ 468 static int 469 modify_gid(char *group, char *newent) 470 { 471 struct stat st; 472 FILE *from; 473 FILE *to; 474 char buf[MaxEntryLen]; 475 char f[MaxFileNameLen]; 476 char *colon; 477 int groupc; 478 int entc; 479 int fd; 480 int cc; 481 482 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 483 warn("can't create gid for `%s': can't open `%s'", group, _PATH_GROUP); 484 return 0; 485 } 486 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 487 warn("can't lock `%s'", _PATH_GROUP); 488 } 489 (void) fstat(fileno(from), &st); 490 (void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 491 if ((fd = mkstemp(f)) < 0) { 492 (void) fclose(from); 493 warn("can't create gid: mkstemp failed"); 494 return 0; 495 } 496 if ((to = fdopen(fd, "w")) == NULL) { 497 (void) fclose(from); 498 (void) close(fd); 499 (void) unlink(f); 500 warn("can't create gid: fdopen `%s' failed", f); 501 return 0; 502 } 503 groupc = strlen(group); 504 while (fgets(buf, sizeof(buf), from) != NULL) { 505 cc = strlen(buf); 506 if ((colon = strchr(buf, ':')) == NULL) { 507 warn("badly formed entry `%s'", buf); 508 continue; 509 } 510 entc = (int)(colon - buf); 511 if (entc == groupc && strncmp(group, buf, (unsigned) entc) == 0) { 512 if (newent == NULL) { 513 continue; 514 } else { 515 cc = strlen(newent); 516 (void) strlcpy(buf, newent, sizeof(buf)); 517 } 518 } 519 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 520 (void) fclose(from); 521 (void) close(fd); 522 (void) unlink(f); 523 warn("can't create gid: short write to `%s'", f); 524 return 0; 525 } 526 } 527 (void) fclose(from); 528 (void) fclose(to); 529 if (rename(f, _PATH_GROUP) < 0) { 530 (void) unlink(f); 531 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP); 532 return 0; 533 } 534 (void) chmod(_PATH_GROUP, st.st_mode & 07777); 535 if (newent == NULL) { 536 syslog(LOG_INFO, "group deleted: name=%s", group); 537 } else { 538 syslog(LOG_INFO, "group information modified: name=%s", group); 539 } 540 return 1; 541 } 542 543 /* modify the group entries for all `groups', by adding `user' */ 544 static int 545 append_group(char *user, int ngroups, const char **groups) 546 { 547 struct group *grp; 548 struct stat st; 549 FILE *from; 550 FILE *to; 551 char buf[MaxEntryLen]; 552 char f[MaxFileNameLen]; 553 char *colon; 554 int groupc; 555 int entc; 556 int fd; 557 int nc; 558 int cc; 559 int i; 560 int j; 561 562 for (i = 0 ; i < ngroups ; i++) { 563 if ((grp = getgrnam(groups[i])) == NULL) { 564 warnx("can't append group `%s' for user `%s'", groups[i], user); 565 } else { 566 for (j = 0 ; grp->gr_mem[j] ; j++) { 567 if (strcmp(user, grp->gr_mem[j]) == 0) { 568 /* already in it */ 569 groups[i] = ""; 570 } 571 } 572 } 573 } 574 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 575 warn("can't append group for `%s': can't open `%s'", user, _PATH_GROUP); 576 return 0; 577 } 578 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 579 warn("can't lock `%s'", _PATH_GROUP); 580 } 581 (void) fstat(fileno(from), &st); 582 (void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 583 if ((fd = mkstemp(f)) < 0) { 584 (void) fclose(from); 585 warn("can't create gid: mkstemp failed"); 586 return 0; 587 } 588 if ((to = fdopen(fd, "w")) == NULL) { 589 (void) fclose(from); 590 (void) close(fd); 591 (void) unlink(f); 592 warn("can't create gid: fdopen `%s' failed", f); 593 return 0; 594 } 595 while (fgets(buf, sizeof(buf), from) != NULL) { 596 cc = strlen(buf); 597 if ((colon = strchr(buf, ':')) == NULL) { 598 warn("badly formed entry `%s'", buf); 599 continue; 600 } 601 entc = (int)(colon - buf); 602 for (i = 0 ; i < ngroups ; i++) { 603 if ((groupc = strlen(groups[i])) == 0) { 604 continue; 605 } 606 if (entc == groupc && strncmp(groups[i], buf, (unsigned) entc) == 0) { 607 if ((nc = snprintf(&buf[cc - 1], 608 sizeof(buf) - cc + 1, 609 "%s%s\n", 610 (buf[cc - 2] == ':') ? "" : ",", 611 user)) < 0) { 612 warnx("Warning: group `%s' entry too long", groups[i]); 613 } 614 cc += nc - 1; 615 } 616 } 617 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 618 (void) fclose(from); 619 (void) close(fd); 620 (void) unlink(f); 621 warn("can't create gid: short write to `%s'", f); 622 return 0; 623 } 624 } 625 (void) fclose(from); 626 (void) fclose(to); 627 if (rename(f, _PATH_GROUP) < 0) { 628 (void) unlink(f); 629 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP); 630 return 0; 631 } 632 (void) chmod(_PATH_GROUP, st.st_mode & 07777); 633 return 1; 634 } 635 636 /* the valid characters for login and group names */ 637 #define VALID_CHAR(c) (isalnum(c) || (c) == '.' || (c) == '_' || (c) == '-') 638 639 /* return 1 if `login' is a valid login name */ 640 static int 641 valid_login(char *login_name, int allow_samba) 642 { 643 unsigned char *cp; 644 645 if (strlen(login_name) >= LOGIN_NAME_MAX) { 646 return 0; 647 } 648 for (cp = login_name ; *cp ; cp++) { 649 if (!VALID_CHAR(*cp)) { 650 #ifdef EXTENSIONS 651 /* check for a trailing '$' in a Samba user name */ 652 if (allow_samba && *cp == '$' && *(cp + 1) == 0x0) { 653 return 1; 654 } 655 #endif 656 return 0; 657 } 658 } 659 return 1; 660 } 661 662 /* return 1 if `group' is a valid group name */ 663 static int 664 valid_group(char *group) 665 { 666 unsigned char *cp; 667 668 for (cp = group ; *cp ; cp++) { 669 if (!VALID_CHAR(*cp)) { 670 return 0; 671 } 672 } 673 return 1; 674 } 675 676 /* find the next gid in the range lo .. hi */ 677 static int 678 getnextgid(int *gidp, int lo, int hi) 679 { 680 for (*gidp = lo ; *gidp < hi ; *gidp += 1) { 681 if (getgrgid((gid_t)*gidp) == NULL) { 682 return 1; 683 } 684 } 685 return 0; 686 } 687 688 #ifdef EXTENSIONS 689 /* save a range of uids */ 690 static int 691 save_range(user_t *up, char *cp) 692 { 693 int from; 694 int to; 695 int i; 696 697 if (up->u_rsize == 0) { 698 up->u_rsize = 32; 699 NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0)); 700 } else if (up->u_rc == up->u_rsize) { 701 up->u_rsize *= 2; 702 RENEW(range_t, up->u_rv, up->u_rsize, return(0)); 703 } 704 if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) { 705 for (i = up->u_defrc ; i < up->u_rc ; i++) { 706 if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) { 707 break; 708 } 709 } 710 if (i == up->u_rc) { 711 up->u_rv[up->u_rc].r_from = from; 712 up->u_rv[up->u_rc].r_to = to; 713 up->u_rc += 1; 714 } 715 } else { 716 warnx("Bad range `%s'", cp); 717 return 0; 718 } 719 return 1; 720 } 721 #endif 722 723 /* set the defaults in the defaults file */ 724 static int 725 setdefaults(user_t *up) 726 { 727 char template[MaxFileNameLen]; 728 FILE *fp; 729 int ret; 730 int fd; 731 #ifdef EXTENSIONS 732 int i; 733 #endif 734 735 (void) snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE); 736 if ((fd = mkstemp(template)) < 0) { 737 warnx("can't mkstemp `%s' for writing", CONFFILE); 738 return 0; 739 } 740 if ((fp = fdopen(fd, "w")) == NULL) { 741 warn("can't fdopen `%s' for writing", CONFFILE); 742 return 0; 743 } 744 ret = 1; 745 if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 || 746 fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 || 747 fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 || 748 fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 || 749 #ifdef EXTENSIONS 750 fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 || 751 #endif 752 fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ? UNSET_INACTIVE : up->u_inactive) <= 0 || 753 fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ? UNSET_EXPIRY : up->u_expire) <= 0 || 754 fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? "false" : "true") <= 0) { 755 warn("can't write to `%s'", CONFFILE); 756 ret = 0; 757 } 758 #ifdef EXTENSIONS 759 for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0 ; i < up->u_rc ; i++) { 760 if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) { 761 warn("can't write to `%s'", CONFFILE); 762 ret = 0; 763 } 764 } 765 #endif 766 (void) fclose(fp); 767 if (ret) { 768 ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0)); 769 } 770 return ret; 771 } 772 773 /* read the defaults file */ 774 static void 775 read_defaults(user_t *up) 776 { 777 struct stat st; 778 size_t lineno; 779 size_t len; 780 FILE *fp; 781 unsigned char *cp; 782 unsigned char *s; 783 784 memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP)); 785 memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR)); 786 memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR)); 787 memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL)); 788 memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT)); 789 #ifdef EXTENSIONS 790 memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS)); 791 #endif 792 up->u_rsize = 16; 793 up->u_defrc = 0; 794 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1)); 795 up->u_inactive = DEF_INACTIVE; 796 up->u_expire = DEF_EXPIRE; 797 if ((fp = fopen(CONFFILE, "r")) == NULL) { 798 if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) { 799 warn("can't create `%s' defaults file", CONFFILE); 800 } 801 fp = fopen(CONFFILE, "r"); 802 } 803 if (fp != NULL) { 804 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) { 805 if (strncmp(s, "group", 5) == 0) { 806 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 807 } 808 memsave(&up->u_primgrp, cp, strlen(cp)); 809 } else if (strncmp(s, "base_dir", 8) == 0) { 810 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 811 } 812 memsave(&up->u_basedir, cp, strlen(cp)); 813 } else if (strncmp(s, "skel_dir", 8) == 0) { 814 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 815 } 816 memsave(&up->u_skeldir, cp, strlen(cp)); 817 } else if (strncmp(s, "shell", 5) == 0) { 818 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 819 } 820 memsave(&up->u_shell, cp, strlen(cp)); 821 #ifdef EXTENSIONS 822 } else if (strncmp(s, "class", 5) == 0) { 823 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 824 } 825 memsave(&up->u_class, cp, strlen(cp)); 826 #endif 827 } else if (strncmp(s, "inactive", 8) == 0) { 828 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 829 } 830 if (strcmp(cp, UNSET_INACTIVE) == 0) { 831 if (up->u_inactive) { 832 FREE(up->u_inactive); 833 } 834 up->u_inactive = NULL; 835 } else { 836 memsave(&up->u_inactive, cp, strlen(cp)); 837 } 838 #ifdef EXTENSIONS 839 } else if (strncmp(s, "range", 5) == 0) { 840 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 841 } 842 (void) save_range(up, cp); 843 #endif 844 #ifdef EXTENSIONS 845 } else if (strncmp(s, "preserve", 8) == 0) { 846 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 847 } 848 up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 : 849 (strncmp(cp, "yes", 3) == 0) ? 1 : 850 atoi(cp); 851 #endif 852 } else if (strncmp(s, "expire", 6) == 0) { 853 for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) { 854 } 855 if (strcmp(cp, UNSET_EXPIRY) == 0) { 856 if (up->u_expire) { 857 FREE(up->u_expire); 858 } 859 up->u_expire = NULL; 860 } else { 861 memsave(&up->u_expire, cp, strlen(cp)); 862 } 863 } 864 (void) free(s); 865 } 866 (void) fclose(fp); 867 } 868 if (up->u_rc == 0) { 869 up->u_rv[up->u_rc].r_from = DEF_LOWUID; 870 up->u_rv[up->u_rc].r_to = DEF_HIGHUID; 871 up->u_rc += 1; 872 } 873 up->u_defrc = up->u_rc; 874 } 875 876 /* return the next valid unused uid */ 877 static int 878 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid) 879 { 880 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) { 881 if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) { 882 if (sync_uid_gid) { 883 if (getgrgid((gid_t)(*uid)) == NULL) { 884 return 1; 885 } 886 } else { 887 return 1; 888 } 889 } 890 } 891 return 0; 892 } 893 894 /* structure which defines a password type */ 895 typedef struct passwd_type_t { 896 const char *type; /* optional type descriptor */ 897 int desc_length; /* length of type descriptor */ 898 int length; /* length of password */ 899 const char *regex; /* regexp to output the password */ 900 int re_sub; /* subscript of regexp to use */ 901 } passwd_type_t; 902 903 static passwd_type_t passwd_types[] = { 904 { "$sha1", 5, 28, "\\$[^$]+\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* SHA1 */ 905 { "$2a", 3, 54, "\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* Blowfish */ 906 { "$1", 2, 34, NULL, 0 }, /* MD5 */ 907 { "", 0, DES_Len,NULL, 0 }, /* standard DES */ 908 { NULL, -1, -1, NULL, 0 } /* none - terminate search */ 909 }; 910 911 /* return non-zero if it's a valid password - check length for cipher type */ 912 static int 913 valid_password_length(char *newpasswd) 914 { 915 passwd_type_t *pwtp; 916 regmatch_t matchv[10]; 917 regex_t r; 918 919 for (pwtp = passwd_types ; pwtp->desc_length >= 0 ; pwtp++) { 920 if (strncmp(newpasswd, pwtp->type, pwtp->desc_length) == 0) { 921 if (pwtp->regex == NULL) { 922 return strlen(newpasswd) == pwtp->length; 923 } 924 (void) regcomp(&r, pwtp->regex, REG_EXTENDED); 925 if (regexec(&r, newpasswd, 10, matchv, 0) == 0) { 926 regfree(&r); 927 return (int)(matchv[pwtp->re_sub].rm_eo - matchv[pwtp->re_sub].rm_so + 1) == pwtp->length; 928 } 929 regfree(&r); 930 } 931 } 932 return 0; 933 } 934 935 /* look for a valid time, return 0 if it was specified but bad */ 936 static int 937 scantime(time_t *tp, char *s) 938 { 939 struct tm tm; 940 941 *tp = 0; 942 if (s != NULL) { 943 (void) memset(&tm, 0, sizeof(tm)); 944 if (strptime(s, "%c", &tm) != NULL) { 945 *tp = mktime(&tm); 946 } else if (strptime(s, "%B %d %Y", &tm) != NULL) { 947 *tp = mktime(&tm); 948 } else if (isdigit((unsigned char) *s)) { 949 *tp = atoi(s); 950 } else { 951 return 0; 952 } 953 } 954 return 1; 955 } 956 957 /* add a user */ 958 static int 959 adduser(char *login_name, user_t *up) 960 { 961 struct group *grp; 962 struct stat st; 963 time_t expire; 964 time_t inactive; 965 char password[PasswordLength + 1]; 966 char home[MaxFileNameLen]; 967 char buf[MaxFileNameLen]; 968 int sync_uid_gid; 969 int masterfd; 970 int ptmpfd; 971 int gid; 972 int cc; 973 int i; 974 975 if (!valid_login(login_name, up->u_allow_samba)) { 976 errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name); 977 } 978 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 979 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD); 980 } 981 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 982 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD); 983 } 984 pw_init(); 985 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 986 (void) close(masterfd); 987 err(EXIT_FAILURE, "can't obtain pw_lock"); 988 } 989 while ((cc = read(masterfd, buf, sizeof(buf))) > 0) { 990 if (write(ptmpfd, buf, (size_t)(cc)) != cc) { 991 (void) close(masterfd); 992 (void) close(ptmpfd); 993 pw_abort(); 994 err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc); 995 } 996 } 997 /* if no uid was specified, get next one in [low_uid..high_uid] range */ 998 sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0); 999 if (up->u_uid == -1) { 1000 int got_id = 0; 1001 1002 /* 1003 * Look for a free UID in the command line ranges (if any). 1004 * These start after the ranges specified in the config file. 1005 */ 1006 for (i = up->u_defrc; !got_id && i < up->u_rc ; i++) { 1007 got_id = getnextuid(sync_uid_gid, &up->u_uid, 1008 up->u_rv[i].r_from, up->u_rv[i].r_to); 1009 } 1010 /* 1011 * If there were no free UIDs in the command line ranges, 1012 * try the ranges from the config file (there will always 1013 * be at least one default). 1014 */ 1015 for (i = 0; !got_id && i < up->u_defrc; i++) { 1016 got_id = getnextuid(sync_uid_gid, &up->u_uid, 1017 up->u_rv[i].r_from, up->u_rv[i].r_to); 1018 } 1019 if (!got_id) { 1020 (void) close(ptmpfd); 1021 pw_abort(); 1022 errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid); 1023 } 1024 } 1025 /* check uid isn't already allocated */ 1026 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) { 1027 (void) close(ptmpfd); 1028 pw_abort(); 1029 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid); 1030 } 1031 /* if -g=uid was specified, check gid is unused */ 1032 if (sync_uid_gid) { 1033 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 1034 (void) close(ptmpfd); 1035 pw_abort(); 1036 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid); 1037 } 1038 gid = up->u_uid; 1039 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 1040 gid = grp->gr_gid; 1041 } else if (is_number(up->u_primgrp) && 1042 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) { 1043 gid = grp->gr_gid; 1044 } else { 1045 (void) close(ptmpfd); 1046 pw_abort(); 1047 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp); 1048 } 1049 /* check name isn't already in use */ 1050 if (!(up->u_flags & F_DUPUID) && getpwnam(login_name) != NULL) { 1051 (void) close(ptmpfd); 1052 pw_abort(); 1053 errx(EXIT_FAILURE, "already a `%s' user", login_name); 1054 } 1055 if (up->u_flags & F_HOMEDIR) { 1056 (void) strlcpy(home, up->u_home, sizeof(home)); 1057 } else { 1058 /* if home directory hasn't been given, make it up */ 1059 (void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login_name); 1060 } 1061 if (!scantime(&inactive, up->u_inactive)) { 1062 warnx("Warning: inactive time `%s' invalid, account expiry off", 1063 up->u_inactive); 1064 } 1065 if (!scantime(&expire, up->u_expire)) { 1066 warnx("Warning: expire time `%s' invalid, password expiry off", 1067 up->u_expire); 1068 } 1069 if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) { 1070 warnx("Warning: home directory `%s' doesn't exist, and -m was not specified", 1071 home); 1072 } 1073 password[sizeof(password) - 1] = '\0'; 1074 if (up->u_password != NULL && valid_password_length(up->u_password)) { 1075 (void) strlcpy(password, up->u_password, sizeof(password)); 1076 } else { 1077 (void) memset(password, '*', DES_Len); 1078 password[DES_Len] = 0; 1079 if (up->u_password != NULL) { 1080 warnx("Password `%s' is invalid: setting it to `%s'", 1081 up->u_password, password); 1082 } 1083 } 1084 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", 1085 login_name, 1086 password, 1087 up->u_uid, 1088 gid, 1089 #ifdef EXTENSIONS 1090 up->u_class, 1091 #else 1092 "", 1093 #endif 1094 (long) inactive, 1095 (long) expire, 1096 up->u_comment, 1097 home, 1098 up->u_shell); 1099 if (write(ptmpfd, buf, (size_t) cc) != cc) { 1100 (void) close(ptmpfd); 1101 pw_abort(); 1102 err(EXIT_FAILURE, "can't add `%s'", buf); 1103 } 1104 if (up->u_flags & F_MKDIR) { 1105 if (lstat(home, &st) == 0) { 1106 (void) close(ptmpfd); 1107 pw_abort(); 1108 errx(EXIT_FAILURE, "home directory `%s' already exists", home); 1109 } else { 1110 if (asystem("%s -p %s", MKDIR, home) != 0) { 1111 (void) close(ptmpfd); 1112 pw_abort(); 1113 err(EXIT_FAILURE, "can't mkdir `%s'", home); 1114 } 1115 (void) copydotfiles(up->u_skeldir, up->u_uid, gid, home); 1116 } 1117 } 1118 if (strcmp(up->u_primgrp, "=uid") == 0 && 1119 getgrnam(login_name) == NULL && 1120 !creategid(login_name, gid, login_name)) { 1121 (void) close(ptmpfd); 1122 pw_abort(); 1123 errx(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login_name); 1124 } 1125 if (up->u_groupc > 0 && !append_group(login_name, up->u_groupc, up->u_groupv)) { 1126 (void) close(ptmpfd); 1127 pw_abort(); 1128 errx(EXIT_FAILURE, "can't append `%s' to new groups", login_name); 1129 } 1130 (void) close(ptmpfd); 1131 #if PW_MKDB_ARGC == 2 1132 if (pw_mkdb(login_name, 0) < 0) { 1133 pw_abort(); 1134 err(EXIT_FAILURE, "pw_mkdb failed"); 1135 } 1136 #else 1137 if (pw_mkdb() < 0) { 1138 pw_abort(); 1139 err(EXIT_FAILURE, "pw_mkdb failed"); 1140 } 1141 #endif 1142 syslog(LOG_INFO, "new user added: name=%s, uid=%d, gid=%d, home=%s, shell=%s", 1143 login_name, up->u_uid, gid, home, up->u_shell); 1144 return 1; 1145 } 1146 1147 /* remove a user from the groups file */ 1148 static int 1149 rm_user_from_groups(char *login_name) 1150 { 1151 struct stat st; 1152 regmatch_t matchv[10]; 1153 regex_t r; 1154 FILE *from; 1155 FILE *to; 1156 char line[MaxEntryLen]; 1157 char buf[MaxEntryLen]; 1158 char f[MaxFileNameLen]; 1159 int fd; 1160 int cc; 1161 int sc; 1162 1163 (void) snprintf(line, sizeof(line), "(:|,)(%s)(,|$)", login_name); 1164 if (regcomp(&r, line, REG_EXTENDED|REG_NEWLINE) != 0) { 1165 warn("can't compile regular expression `%s'", line); 1166 return 0; 1167 } 1168 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 1169 warn("can't remove gid for `%s': can't open `%s'", login_name, _PATH_GROUP); 1170 return 0; 1171 } 1172 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 1173 warn("can't lock `%s'", _PATH_GROUP); 1174 } 1175 (void) fstat(fileno(from), &st); 1176 (void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 1177 if ((fd = mkstemp(f)) < 0) { 1178 (void) fclose(from); 1179 warn("can't create gid: mkstemp failed"); 1180 return 0; 1181 } 1182 if ((to = fdopen(fd, "w")) == NULL) { 1183 (void) fclose(from); 1184 (void) close(fd); 1185 (void) unlink(f); 1186 warn("can't create gid: fdopen `%s' failed", f); 1187 return 0; 1188 } 1189 while (fgets(buf, sizeof(buf), from) > 0) { 1190 cc = strlen(buf); 1191 if (regexec(&r, buf, 10, matchv, 0) == 0) { 1192 if (buf[(int)matchv[1].rm_so] == ',') { 1193 matchv[2].rm_so = matchv[1].rm_so; 1194 } else if (matchv[2].rm_eo != matchv[3].rm_eo) { 1195 matchv[2].rm_eo = matchv[3].rm_eo; 1196 } 1197 cc -= (int) matchv[2].rm_eo; 1198 sc = (int) matchv[2].rm_so; 1199 if (fwrite(buf, sizeof(char), sc, to) != sc || 1200 fwrite(&buf[(int)matchv[2].rm_eo], sizeof(char), cc, to) != cc) { 1201 (void) fclose(from); 1202 (void) close(fd); 1203 (void) unlink(f); 1204 warn("can't create gid: short write to `%s'", f); 1205 return 0; 1206 } 1207 } else if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 1208 (void) fclose(from); 1209 (void) close(fd); 1210 (void) unlink(f); 1211 warn("can't create gid: short write to `%s'", f); 1212 return 0; 1213 } 1214 } 1215 (void) fclose(from); 1216 (void) fclose(to); 1217 if (rename(f, _PATH_GROUP) < 0) { 1218 (void) unlink(f); 1219 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP); 1220 return 0; 1221 } 1222 (void) chmod(_PATH_GROUP, st.st_mode & 07777); 1223 return 1; 1224 } 1225 1226 /* check that the user or group is local, not from YP/NIS */ 1227 static int 1228 is_local(char *name, const char *file) 1229 { 1230 FILE *fp; 1231 char buf[MaxEntryLen]; 1232 int len; 1233 int ret; 1234 1235 if ((fp = fopen(file, "r")) == NULL) { 1236 err(EXIT_FAILURE, "can't open `%s'", file); 1237 } 1238 len = strlen(name); 1239 for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) { 1240 if (strncmp(buf, name, len) == 0 && buf[len] == ':') { 1241 ret = 1; 1242 break; 1243 } 1244 } 1245 (void) fclose(fp); 1246 return ret; 1247 } 1248 1249 /* modify a user */ 1250 static int 1251 moduser(char *login_name, char *newlogin, user_t *up, int allow_samba) 1252 { 1253 struct passwd *pwp; 1254 struct group *grp; 1255 const char *homedir; 1256 size_t colonc; 1257 size_t loginc; 1258 size_t len; 1259 size_t cc; 1260 FILE *master; 1261 char newdir[MaxFileNameLen]; 1262 char buf[MaxEntryLen]; 1263 char *colon; 1264 int masterfd; 1265 int ptmpfd; 1266 int error; 1267 1268 if (!valid_login(newlogin, allow_samba)) { 1269 errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name); 1270 } 1271 if ((pwp = getpwnam(login_name)) == NULL) { 1272 errx(EXIT_FAILURE, "No such user `%s'", login_name); 1273 } 1274 if (!is_local(login_name, _PATH_MASTERPASSWD)) { 1275 errx(EXIT_FAILURE, "User `%s' must be a local user", login_name); 1276 } 1277 /* keep dir name in case we need it for '-m' */ 1278 homedir = pwp->pw_dir; 1279 1280 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 1281 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD); 1282 } 1283 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 1284 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD); 1285 } 1286 pw_init(); 1287 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 1288 (void) close(masterfd); 1289 err(EXIT_FAILURE, "can't obtain pw_lock"); 1290 } 1291 if ((master = fdopen(masterfd, "r")) == NULL) { 1292 (void) close(masterfd); 1293 (void) close(ptmpfd); 1294 pw_abort(); 1295 err(EXIT_FAILURE, "can't fdopen fd for %s", _PATH_MASTERPASSWD); 1296 } 1297 if (up != NULL) { 1298 if (up->u_flags & F_USERNAME) { 1299 /* if changing name, check new name isn't already in use */ 1300 if (strcmp(login_name, newlogin) != 0 && getpwnam(newlogin) != NULL) { 1301 (void) close(ptmpfd); 1302 pw_abort(); 1303 errx(EXIT_FAILURE, "already a `%s' user", newlogin); 1304 } 1305 pwp->pw_name = newlogin; 1306 1307 /* 1308 * Provide a new directory name in case the 1309 * home directory is to be moved. 1310 */ 1311 if (up->u_flags & F_MKDIR) { 1312 snprintf(newdir, sizeof(newdir), "%s/%s", up->u_basedir, newlogin); 1313 pwp->pw_dir = newdir; 1314 } 1315 } 1316 if (up->u_flags & F_PASSWORD) { 1317 if (up->u_password != NULL) { 1318 if (!valid_password_length(up->u_password)) { 1319 (void) close(ptmpfd); 1320 pw_abort(); 1321 errx(EXIT_FAILURE, "Invalid password: `%s'", 1322 up->u_password); 1323 } 1324 pwp->pw_passwd = up->u_password; 1325 } 1326 } 1327 if (up->u_flags & F_UID) { 1328 /* check uid isn't already allocated */ 1329 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) { 1330 (void) close(ptmpfd); 1331 pw_abort(); 1332 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid); 1333 } 1334 pwp->pw_uid = up->u_uid; 1335 } 1336 if (up->u_flags & F_GROUP) { 1337 /* if -g=uid was specified, check gid is unused */ 1338 if (strcmp(up->u_primgrp, "=uid") == 0) { 1339 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 1340 (void) close(ptmpfd); 1341 pw_abort(); 1342 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid); 1343 } 1344 pwp->pw_gid = up->u_uid; 1345 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 1346 pwp->pw_gid = grp->gr_gid; 1347 } else if (is_number(up->u_primgrp) && 1348 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) { 1349 pwp->pw_gid = grp->gr_gid; 1350 } else { 1351 (void) close(ptmpfd); 1352 pw_abort(); 1353 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp); 1354 } 1355 } 1356 if (up->u_flags & F_INACTIVE) { 1357 if (!scantime(&pwp->pw_change, up->u_inactive)) { 1358 warnx("Warning: inactive time `%s' invalid, password expiry off", 1359 up->u_inactive); 1360 } 1361 } 1362 if (up->u_flags & F_EXPIRE) { 1363 if (!scantime(&pwp->pw_expire, up->u_expire)) { 1364 warnx("Warning: expire time `%s' invalid, password expiry off", 1365 up->u_expire); 1366 } 1367 } 1368 if (up->u_flags & F_COMMENT) 1369 pwp->pw_gecos = up->u_comment; 1370 if (up->u_flags & F_HOMEDIR) 1371 pwp->pw_dir = up->u_home; 1372 if (up->u_flags & F_SHELL) 1373 pwp->pw_shell = up->u_shell; 1374 #ifdef EXTENSIONS 1375 if (up->u_flags & F_CLASS) 1376 pwp->pw_class = up->u_class; 1377 #endif 1378 } 1379 loginc = strlen(login_name); 1380 while (fgets(buf, sizeof(buf), master) != NULL) { 1381 if ((colon = strchr(buf, ':')) == NULL) { 1382 warnx("Malformed entry `%s'. Skipping", buf); 1383 continue; 1384 } 1385 colonc = (size_t)(colon - buf); 1386 if (strncmp(login_name, buf, loginc) == 0 && loginc == colonc) { 1387 if (up != NULL) { 1388 len = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:" 1389 #ifdef EXTENSIONS 1390 "%s" 1391 #endif 1392 ":%ld:%ld:%s:%s:%s\n", 1393 newlogin, 1394 pwp->pw_passwd, 1395 pwp->pw_uid, 1396 pwp->pw_gid, 1397 #ifdef EXTENSIONS 1398 pwp->pw_class, 1399 #endif 1400 (long)pwp->pw_change, 1401 (long)pwp->pw_expire, 1402 pwp->pw_gecos, 1403 pwp->pw_dir, 1404 pwp->pw_shell); 1405 if (write(ptmpfd, buf, len) != len) { 1406 (void) close(ptmpfd); 1407 pw_abort(); 1408 err(EXIT_FAILURE, "can't add `%s'", buf); 1409 } 1410 } 1411 } else { 1412 len = strlen(buf); 1413 if ((cc = write(ptmpfd, buf, len)) != len) { 1414 (void) close(masterfd); 1415 (void) close(ptmpfd); 1416 pw_abort(); 1417 err(EXIT_FAILURE, "short write to /etc/ptmp (%lld not %lld chars)", 1418 (long long)cc, 1419 (long long)len); 1420 } 1421 } 1422 } 1423 if (up != NULL) { 1424 if ((up->u_flags & F_MKDIR) && 1425 asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) { 1426 (void) close(ptmpfd); 1427 pw_abort(); 1428 err(EXIT_FAILURE, "can't move `%s' to `%s'", 1429 homedir, pwp->pw_dir); 1430 } 1431 if (up->u_groupc > 0 && 1432 !append_group(newlogin, up->u_groupc, up->u_groupv)) { 1433 (void) close(ptmpfd); 1434 pw_abort(); 1435 errx(EXIT_FAILURE, "can't append `%s' to new groups", 1436 newlogin); 1437 } 1438 } 1439 (void) close(ptmpfd); 1440 #if PW_MKDB_ARGC == 2 1441 if (up != NULL && strcmp(login_name, newlogin) == 0) { 1442 error = pw_mkdb(login_name, 0); 1443 } else { 1444 error = pw_mkdb(NULL, 0); 1445 } 1446 #else 1447 error = pw_mkdb(); 1448 #endif 1449 if (error < 0) { 1450 pw_abort(); 1451 err(EXIT_FAILURE, "pw_mkdb failed"); 1452 } 1453 if (up == NULL) { 1454 syslog(LOG_INFO, "user removed: name=%s", login_name); 1455 } else if (strcmp(login_name, newlogin) == 0) { 1456 syslog(LOG_INFO, "user information modified: name=%s, uid=%d, gid=%d, home=%s, shell=%s", 1457 login_name, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, pwp->pw_shell); 1458 } else { 1459 syslog(LOG_INFO, "user information modified: name=%s, new name=%s, uid=%d, gid=%d, home=%s, shell=%s", 1460 login_name, newlogin, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, pwp->pw_shell); 1461 } 1462 return 1; 1463 } 1464 1465 1466 #ifdef EXTENSIONS 1467 /* see if we can find out the user struct */ 1468 static struct passwd * 1469 find_user_info(char *name) 1470 { 1471 struct passwd *pwp; 1472 1473 if ((pwp = getpwnam(name)) != NULL) { 1474 return pwp; 1475 } 1476 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) { 1477 return pwp; 1478 } 1479 return NULL; 1480 } 1481 #endif 1482 1483 #ifdef EXTENSIONS 1484 /* see if we can find out the group struct */ 1485 static struct group * 1486 find_group_info(char *name) 1487 { 1488 struct group *grp; 1489 1490 if ((grp = getgrnam(name)) != NULL) { 1491 return grp; 1492 } 1493 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) { 1494 return grp; 1495 } 1496 return NULL; 1497 } 1498 #endif 1499 1500 /* print out usage message, and then exit */ 1501 void 1502 usermgmt_usage(const char *prog) 1503 { 1504 if (strcmp(prog, "useradd") == 0) { 1505 (void) fprintf(stderr, "usage: %s -D [-b basedir] [-e expiry] " 1506 "[-f inactive] [-g group]\n\t[-r lowuid..highuid] " 1507 "[-s shell] [-L class]\n", prog); 1508 (void) fprintf(stderr, "usage: %s [-G group] [-b basedir] " 1509 "[-c comment] [-d homedir] [-e expiry]\n\t[-f inactive] " 1510 "[-g group] [-k skeletondir] [-m] [-o] [-p password]\n" 1511 "\t[-r lowuid..highuid] [-s shell]\n\t[-u uid] [-v] user\n", 1512 prog); 1513 } else if (strcmp(prog, "usermod") == 0) { 1514 (void) fprintf(stderr, "usage: %s [-G group] [-c comment] " 1515 "[-d homedir] [-e expire] [-f inactive]\n\t[-g group] " 1516 "[-l newname] [-m] [-o] [-p password] [-s shell] [-u uid]\n" 1517 "\t[-L class] [-v] user\n", prog); 1518 } else if (strcmp(prog, "userdel") == 0) { 1519 (void) fprintf(stderr, "usage: %s -D [-p preserve]\n", prog); 1520 (void) fprintf(stderr, 1521 "usage: %s [-p preserve] [-r] [-v] user\n", prog); 1522 #ifdef EXTENSIONS 1523 } else if (strcmp(prog, "userinfo") == 0) { 1524 (void) fprintf(stderr, "usage: %s [-e] [-v] user\n", prog); 1525 #endif 1526 } else if (strcmp(prog, "groupadd") == 0) { 1527 (void) fprintf(stderr, "usage: %s [-g gid] [-o]" 1528 " [-r lowgid..highgid] [-v] group\n", prog); 1529 } else if (strcmp(prog, "groupdel") == 0) { 1530 (void) fprintf(stderr, "usage: %s [-v] group\n", prog); 1531 } else if (strcmp(prog, "groupmod") == 0) { 1532 (void) fprintf(stderr, 1533 "usage: %s [-g gid] [-o] [-n newname] [-v] group\n", prog); 1534 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) { 1535 (void) fprintf(stderr, 1536 "usage: %s ( add | del | mod | info ) ...\n", prog); 1537 #ifdef EXTENSIONS 1538 } else if (strcmp(prog, "groupinfo") == 0) { 1539 (void) fprintf(stderr, "usage: %s [-e] [-v] group\n", prog); 1540 #endif 1541 } 1542 exit(EXIT_FAILURE); 1543 /* NOTREACHED */ 1544 } 1545 1546 #ifdef EXTENSIONS 1547 #define ADD_OPT_EXTENSIONS "p:r:vL:S" 1548 #else 1549 #define ADD_OPT_EXTENSIONS 1550 #endif 1551 1552 int 1553 useradd(int argc, char **argv) 1554 { 1555 user_t u; 1556 int defaultfield; 1557 int bigD; 1558 int c; 1559 #ifdef EXTENSIONS 1560 int i; 1561 #endif 1562 1563 (void) memset(&u, 0, sizeof(u)); 1564 read_defaults(&u); 1565 u.u_uid = -1; 1566 defaultfield = bigD = 0; 1567 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) { 1568 switch(c) { 1569 case 'D': 1570 bigD = 1; 1571 break; 1572 case 'G': 1573 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL && 1574 u.u_groupc < NGROUPS_MAX) { 1575 if (u.u_groupv[u.u_groupc][0] != 0) { 1576 u.u_groupc++; 1577 } 1578 } 1579 if (optarg != NULL) { 1580 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX); 1581 } 1582 break; 1583 #ifdef EXTENSIONS 1584 case 'S': 1585 u.u_allow_samba = 1; 1586 break; 1587 #endif 1588 case 'b': 1589 defaultfield = 1; 1590 memsave(&u.u_basedir, optarg, strlen(optarg)); 1591 break; 1592 case 'c': 1593 memsave(&u.u_comment, optarg, strlen(optarg)); 1594 break; 1595 case 'd': 1596 memsave(&u.u_home, optarg, strlen(optarg)); 1597 u.u_flags |= F_HOMEDIR; 1598 break; 1599 case 'e': 1600 defaultfield = 1; 1601 memsave(&u.u_expire, optarg, strlen(optarg)); 1602 break; 1603 case 'f': 1604 defaultfield = 1; 1605 memsave(&u.u_inactive, optarg, strlen(optarg)); 1606 break; 1607 case 'g': 1608 defaultfield = 1; 1609 memsave(&u.u_primgrp, optarg, strlen(optarg)); 1610 break; 1611 case 'k': 1612 defaultfield = 1; 1613 memsave(&u.u_skeldir, optarg, strlen(optarg)); 1614 break; 1615 #ifdef EXTENSIONS 1616 case 'L': 1617 defaultfield = 1; 1618 memsave(&u.u_class, optarg, strlen(optarg)); 1619 break; 1620 #endif 1621 case 'm': 1622 u.u_flags |= F_MKDIR; 1623 break; 1624 case 'o': 1625 u.u_flags |= F_DUPUID; 1626 break; 1627 #ifdef EXTENSIONS 1628 case 'p': 1629 memsave(&u.u_password, optarg, strlen(optarg)); 1630 break; 1631 #endif 1632 #ifdef EXTENSIONS 1633 case 'r': 1634 defaultfield = 1; 1635 (void) save_range(&u, optarg); 1636 break; 1637 #endif 1638 case 's': 1639 defaultfield = 1; 1640 memsave(&u.u_shell, optarg, strlen(optarg)); 1641 break; 1642 case 'u': 1643 if (!is_number(optarg)) { 1644 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric"); 1645 } 1646 u.u_uid = atoi(optarg); 1647 break; 1648 #ifdef EXTENSIONS 1649 case 'v': 1650 verbose = 1; 1651 break; 1652 #endif 1653 default: 1654 usermgmt_usage("useradd"); 1655 /* NOTREACHED */ 1656 } 1657 } 1658 if (bigD) { 1659 if (defaultfield) { 1660 checkeuid(); 1661 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 1662 } 1663 (void) printf("group\t\t%s\n", u.u_primgrp); 1664 (void) printf("base_dir\t%s\n", u.u_basedir); 1665 (void) printf("skel_dir\t%s\n", u.u_skeldir); 1666 (void) printf("shell\t\t%s\n", u.u_shell); 1667 #ifdef EXTENSIONS 1668 (void) printf("class\t\t%s\n", u.u_class); 1669 #endif 1670 (void) printf("inactive\t%s\n", (u.u_inactive == NULL) ? UNSET_INACTIVE : u.u_inactive); 1671 (void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire); 1672 #ifdef EXTENSIONS 1673 for (i = 0 ; i < u.u_rc ; i++) { 1674 (void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to); 1675 } 1676 #endif 1677 return EXIT_SUCCESS; 1678 } 1679 argc -= optind; 1680 argv += optind; 1681 if (argc != 1) { 1682 usermgmt_usage("useradd"); 1683 } 1684 checkeuid(); 1685 openlog("useradd", LOG_PID, LOG_USER); 1686 return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE; 1687 } 1688 1689 #ifdef EXTENSIONS 1690 #define MOD_OPT_EXTENSIONS "p:vL:S" 1691 #else 1692 #define MOD_OPT_EXTENSIONS 1693 #endif 1694 1695 int 1696 usermod(int argc, char **argv) 1697 { 1698 user_t u; 1699 char newuser[MaxUserNameLen + 1]; 1700 int c, have_new_user; 1701 1702 (void) memset(&u, 0, sizeof(u)); 1703 (void) memset(newuser, 0, sizeof(newuser)); 1704 read_defaults(&u); 1705 have_new_user = 0; 1706 while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) { 1707 switch(c) { 1708 case 'G': 1709 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL && 1710 u.u_groupc < NGROUPS_MAX) { 1711 if (u.u_groupv[u.u_groupc][0] != 0) { 1712 u.u_groupc++; 1713 } 1714 } 1715 if (optarg != NULL) { 1716 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX); 1717 } 1718 u.u_flags |= F_SECGROUP; 1719 break; 1720 #ifdef EXTENSIONS 1721 case 'S': 1722 u.u_allow_samba = 1; 1723 break; 1724 #endif 1725 case 'c': 1726 memsave(&u.u_comment, optarg, strlen(optarg)); 1727 u.u_flags |= F_COMMENT; 1728 break; 1729 case 'd': 1730 memsave(&u.u_home, optarg, strlen(optarg)); 1731 u.u_flags |= F_HOMEDIR; 1732 break; 1733 case 'e': 1734 memsave(&u.u_expire, optarg, strlen(optarg)); 1735 u.u_flags |= F_EXPIRE; 1736 break; 1737 case 'f': 1738 memsave(&u.u_inactive, optarg, strlen(optarg)); 1739 u.u_flags |= F_INACTIVE; 1740 break; 1741 case 'g': 1742 memsave(&u.u_primgrp, optarg, strlen(optarg)); 1743 u.u_flags |= F_GROUP; 1744 break; 1745 case 'l': 1746 (void) strlcpy(newuser, optarg, sizeof(newuser)); 1747 have_new_user = 1; 1748 u.u_flags |= F_USERNAME; 1749 break; 1750 #ifdef EXTENSIONS 1751 case 'L': 1752 memsave(&u.u_class, optarg, strlen(optarg)); 1753 u.u_flags |= F_CLASS; 1754 break; 1755 #endif 1756 case 'm': 1757 u.u_flags |= F_MKDIR; 1758 break; 1759 case 'o': 1760 u.u_flags |= F_DUPUID; 1761 break; 1762 #ifdef EXTENSIONS 1763 case 'p': 1764 memsave(&u.u_password, optarg, strlen(optarg)); 1765 u.u_flags |= F_PASSWORD; 1766 break; 1767 #endif 1768 case 's': 1769 memsave(&u.u_shell, optarg, strlen(optarg)); 1770 u.u_flags |= F_SHELL; 1771 break; 1772 case 'u': 1773 if (!is_number(optarg)) { 1774 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric"); 1775 } 1776 u.u_uid = atoi(optarg); 1777 u.u_flags |= F_UID; 1778 break; 1779 #ifdef EXTENSIONS 1780 case 'v': 1781 verbose = 1; 1782 break; 1783 #endif 1784 default: 1785 usermgmt_usage("usermod"); 1786 /* NOTREACHED */ 1787 } 1788 } 1789 if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) && 1790 !(u.u_flags & F_USERNAME)) { 1791 warnx("option 'm' useless without 'd' or 'l' -- ignored"); 1792 u.u_flags &= ~F_MKDIR; 1793 } 1794 argc -= optind; 1795 argv += optind; 1796 if (argc != 1) { 1797 usermgmt_usage("usermod"); 1798 } 1799 checkeuid(); 1800 openlog("usermod", LOG_PID, LOG_USER); 1801 return moduser(*argv, (have_new_user) ? newuser : *argv, &u, u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE; 1802 } 1803 1804 #ifdef EXTENSIONS 1805 #define DEL_OPT_EXTENSIONS "Dp:vS" 1806 #else 1807 #define DEL_OPT_EXTENSIONS 1808 #endif 1809 1810 int 1811 userdel(int argc, char **argv) 1812 { 1813 struct passwd *pwp; 1814 user_t u; 1815 char password[PasswordLength + 1]; 1816 int defaultfield; 1817 int rmhome; 1818 int bigD; 1819 int c; 1820 1821 (void) memset(&u, 0, sizeof(u)); 1822 read_defaults(&u); 1823 defaultfield = bigD = rmhome = 0; 1824 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) { 1825 switch(c) { 1826 #ifdef EXTENSIONS 1827 case 'D': 1828 bigD = 1; 1829 break; 1830 #endif 1831 #ifdef EXTENSIONS 1832 case 'S': 1833 u.u_allow_samba = 1; 1834 break; 1835 #endif 1836 #ifdef EXTENSIONS 1837 case 'p': 1838 defaultfield = 1; 1839 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 : 1840 (strcmp(optarg, "yes") == 0) ? 1 : 1841 atoi(optarg); 1842 break; 1843 #endif 1844 case 'r': 1845 rmhome = 1; 1846 break; 1847 #ifdef EXTENSIONS 1848 case 'v': 1849 verbose = 1; 1850 break; 1851 #endif 1852 default: 1853 usermgmt_usage("userdel"); 1854 /* NOTREACHED */ 1855 } 1856 } 1857 #ifdef EXTENSIONS 1858 if (bigD) { 1859 if (defaultfield) { 1860 checkeuid(); 1861 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 1862 } 1863 (void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false"); 1864 return EXIT_SUCCESS; 1865 } 1866 #endif 1867 argc -= optind; 1868 argv += optind; 1869 if (argc != 1) { 1870 usermgmt_usage("userdel"); 1871 } 1872 checkeuid(); 1873 if ((pwp = getpwnam(*argv)) == NULL) { 1874 warnx("No such user `%s'", *argv); 1875 return EXIT_FAILURE; 1876 } 1877 if (rmhome) 1878 (void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir); 1879 if (u.u_preserve) { 1880 u.u_flags |= F_SHELL; 1881 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN)); 1882 (void) memset(password, '*', DES_Len); 1883 password[DES_Len] = 0; 1884 memsave(&u.u_password, password, strlen(password)); 1885 u.u_flags |= F_PASSWORD; 1886 openlog("userdel", LOG_PID, LOG_USER); 1887 return moduser(*argv, *argv, &u, u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE; 1888 } 1889 if (!rm_user_from_groups(*argv)) { 1890 return 0; 1891 } 1892 openlog("userdel", LOG_PID, LOG_USER); 1893 return moduser(*argv, *argv, NULL, u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE; 1894 } 1895 1896 #ifdef EXTENSIONS 1897 #define GROUP_ADD_OPT_EXTENSIONS "r:v" 1898 #else 1899 #define GROUP_ADD_OPT_EXTENSIONS 1900 #endif 1901 1902 /* add a group */ 1903 int 1904 groupadd(int argc, char **argv) 1905 { 1906 int dupgid; 1907 int gid; 1908 int c; 1909 int lowgid; 1910 int highgid; 1911 1912 gid = -1; 1913 dupgid = 0; 1914 lowgid = LowGid; 1915 highgid = HighGid; 1916 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) { 1917 switch(c) { 1918 case 'g': 1919 if (!is_number(optarg)) { 1920 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric"); 1921 } 1922 gid = atoi(optarg); 1923 break; 1924 case 'o': 1925 dupgid = 1; 1926 break; 1927 #ifdef EXTENSIONS 1928 case 'r': 1929 if (sscanf(optarg, "%d..%d", &lowgid, &highgid) != 2) { 1930 errx(EXIT_FAILURE, "Bad range `%s`", optarg); 1931 } 1932 break; 1933 case 'v': 1934 verbose = 1; 1935 break; 1936 #endif 1937 default: 1938 usermgmt_usage("groupadd"); 1939 /* NOTREACHED */ 1940 } 1941 } 1942 argc -= optind; 1943 argv += optind; 1944 if (argc != 1) { 1945 usermgmt_usage("groupadd"); 1946 } 1947 checkeuid(); 1948 if (gid < 0 && !getnextgid(&gid, lowgid, highgid)) { 1949 err(EXIT_FAILURE, "can't add group: can't get next gid"); 1950 } 1951 if (!dupgid && getgrgid((gid_t) gid) != NULL) { 1952 errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid); 1953 } 1954 if (!valid_group(*argv)) { 1955 warnx("warning - invalid group name `%s'", *argv); 1956 } 1957 openlog("groupadd", LOG_PID, LOG_USER); 1958 if (!creategid(*argv, gid, "")) { 1959 errx(EXIT_FAILURE, "can't add group: problems with %s file", _PATH_GROUP); 1960 } 1961 return EXIT_SUCCESS; 1962 } 1963 1964 #ifdef EXTENSIONS 1965 #define GROUP_DEL_OPT_EXTENSIONS "v" 1966 #else 1967 #define GROUP_DEL_OPT_EXTENSIONS 1968 #endif 1969 1970 /* remove a group */ 1971 int 1972 groupdel(int argc, char **argv) 1973 { 1974 int c; 1975 1976 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) { 1977 switch(c) { 1978 #ifdef EXTENSIONS 1979 case 'v': 1980 verbose = 1; 1981 break; 1982 #endif 1983 default: 1984 usermgmt_usage("groupdel"); 1985 /* NOTREACHED */ 1986 } 1987 } 1988 argc -= optind; 1989 argv += optind; 1990 if (argc != 1) { 1991 usermgmt_usage("groupdel"); 1992 } 1993 if (getgrnam(*argv) == NULL) { 1994 errx(EXIT_FAILURE, "No such group `%s'", *argv); 1995 } 1996 checkeuid(); 1997 openlog("groupdel", LOG_PID, LOG_USER); 1998 if (!modify_gid(*argv, NULL)) { 1999 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP); 2000 } 2001 return EXIT_SUCCESS; 2002 } 2003 2004 #ifdef EXTENSIONS 2005 #define GROUP_MOD_OPT_EXTENSIONS "v" 2006 #else 2007 #define GROUP_MOD_OPT_EXTENSIONS 2008 #endif 2009 2010 /* modify a group */ 2011 int 2012 groupmod(int argc, char **argv) 2013 { 2014 struct group *grp; 2015 char buf[MaxEntryLen]; 2016 char *newname; 2017 char **cpp; 2018 int dupgid; 2019 int gid; 2020 int cc; 2021 int c; 2022 2023 gid = -1; 2024 dupgid = 0; 2025 newname = NULL; 2026 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) { 2027 switch(c) { 2028 case 'g': 2029 if (!is_number(optarg)) { 2030 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric"); 2031 } 2032 gid = atoi(optarg); 2033 break; 2034 case 'o': 2035 dupgid = 1; 2036 break; 2037 case 'n': 2038 memsave(&newname, optarg, strlen(optarg)); 2039 break; 2040 #ifdef EXTENSIONS 2041 case 'v': 2042 verbose = 1; 2043 break; 2044 #endif 2045 default: 2046 usermgmt_usage("groupmod"); 2047 /* NOTREACHED */ 2048 } 2049 } 2050 argc -= optind; 2051 argv += optind; 2052 if (argc != 1) { 2053 usermgmt_usage("groupmod"); 2054 } 2055 checkeuid(); 2056 if (gid < 0 && newname == NULL) { 2057 err(EXIT_FAILURE, "Nothing to change"); 2058 } 2059 if (dupgid && gid < 0) { 2060 err(EXIT_FAILURE, "Duplicate which gid?"); 2061 } 2062 if ((grp = getgrnam(*argv)) == NULL) { 2063 errx(EXIT_FAILURE, "can't find group `%s' to modify", *argv); 2064 } 2065 if (!is_local(*argv, _PATH_GROUP)) { 2066 errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv); 2067 } 2068 if (newname != NULL && !valid_group(newname)) { 2069 warn("warning - invalid group name `%s'", newname); 2070 } 2071 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:", 2072 (newname) ? newname : grp->gr_name, 2073 grp->gr_passwd, 2074 (gid < 0) ? grp->gr_gid : gid); 2075 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) { 2076 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp, 2077 (cpp[1] == NULL) ? "" : ","); 2078 } 2079 cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n"); 2080 openlog("groupmod", LOG_PID, LOG_USER); 2081 if (!modify_gid(*argv, buf)) { 2082 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP); 2083 } 2084 return EXIT_SUCCESS; 2085 } 2086 2087 #ifdef EXTENSIONS 2088 /* display user information */ 2089 int 2090 userinfo(int argc, char **argv) 2091 { 2092 struct passwd *pwp; 2093 struct group *grp; 2094 char buf[MaxEntryLen]; 2095 char **cpp; 2096 int exists; 2097 int cc; 2098 int i; 2099 2100 exists = 0; 2101 while ((i = getopt(argc, argv, "ev")) != -1) { 2102 switch(i) { 2103 case 'e': 2104 exists = 1; 2105 break; 2106 case 'v': 2107 verbose = 1; 2108 break; 2109 default: 2110 usermgmt_usage("userinfo"); 2111 /* NOTREACHED */ 2112 } 2113 } 2114 argc -= optind; 2115 argv += optind; 2116 if (argc != 1) { 2117 usermgmt_usage("userinfo"); 2118 } 2119 pwp = find_user_info(*argv); 2120 if (exists) { 2121 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE); 2122 } 2123 if (pwp == NULL) { 2124 errx(EXIT_FAILURE, "can't find user `%s'", *argv); 2125 } 2126 (void) printf("login\t%s\n", pwp->pw_name); 2127 (void) printf("passwd\t%s\n", pwp->pw_passwd); 2128 (void) printf("uid\t%d\n", pwp->pw_uid); 2129 for (cc = 0 ; (grp = getgrent()) != NULL ; ) { 2130 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2131 if (strcmp(*cpp, *argv) == 0 && grp->gr_gid != pwp->pw_gid) { 2132 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name); 2133 } 2134 } 2135 } 2136 if ((grp = getgrgid(pwp->pw_gid)) == NULL) { 2137 (void) printf("groups\t%d %s\n", pwp->pw_gid, buf); 2138 } else { 2139 (void) printf("groups\t%s %s\n", grp->gr_name, buf); 2140 } 2141 (void) printf("change\t%s", pwp->pw_change ? ctime(&pwp->pw_change) : "NEVER\n"); 2142 #ifdef EXTENSIONS 2143 (void) printf("class\t%s\n", pwp->pw_class); 2144 #endif 2145 (void) printf("gecos\t%s\n", pwp->pw_gecos); 2146 (void) printf("dir\t%s\n", pwp->pw_dir); 2147 (void) printf("shell\t%s\n", pwp->pw_shell); 2148 (void) printf("expire\t%s", pwp->pw_expire ? ctime(&pwp->pw_expire) : "NEVER\n"); 2149 return EXIT_SUCCESS; 2150 } 2151 #endif 2152 2153 #ifdef EXTENSIONS 2154 /* display user information */ 2155 int 2156 groupinfo(int argc, char **argv) 2157 { 2158 struct group *grp; 2159 char **cpp; 2160 int exists; 2161 int i; 2162 2163 exists = 0; 2164 while ((i = getopt(argc, argv, "ev")) != -1) { 2165 switch(i) { 2166 case 'e': 2167 exists = 1; 2168 break; 2169 case 'v': 2170 verbose = 1; 2171 break; 2172 default: 2173 usermgmt_usage("groupinfo"); 2174 /* NOTREACHED */ 2175 } 2176 } 2177 argc -= optind; 2178 argv += optind; 2179 if (argc != 1) { 2180 usermgmt_usage("groupinfo"); 2181 } 2182 grp = find_group_info(*argv); 2183 if (exists) { 2184 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE); 2185 } 2186 if (grp == NULL) { 2187 errx(EXIT_FAILURE, "can't find group `%s'", *argv); 2188 } 2189 (void) printf("name\t%s\n", grp->gr_name); 2190 (void) printf("passwd\t%s\n", grp->gr_passwd); 2191 (void) printf("gid\t%d\n", grp->gr_gid); 2192 (void) printf("members\t"); 2193 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2194 (void) printf("%s ", *cpp); 2195 } 2196 (void) fputc('\n', stdout); 2197 return EXIT_SUCCESS; 2198 } 2199 #endif 2200