1 /* $NetBSD: user.c,v 1.75 2004/01/14 09:35:33 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.75 2004/01/14 09:35:33 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 /* return 1 if `login' is a valid login name */ 637 static int 638 valid_login(char *login_name, int allow_samba) 639 { 640 unsigned char *cp; 641 642 if (strlen(login_name) >= LOGIN_NAME_MAX) { 643 return 0; 644 } 645 for (cp = login_name ; *cp ; cp++) { 646 if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-') { 647 #ifdef EXTENSIONS 648 /* check for a trailing '$' in a Samba user name */ 649 if (allow_samba && *cp == '$' && *(cp + 1) == 0x0) { 650 return 1; 651 } 652 #endif 653 return 0; 654 } 655 } 656 return 1; 657 } 658 659 /* return 1 if `group' is a valid group name */ 660 static int 661 valid_group(char *group) 662 { 663 unsigned char *cp; 664 665 for (cp = group ; *cp ; cp++) { 666 if (!isalnum(*cp)) { 667 return 0; 668 } 669 } 670 return 1; 671 } 672 673 /* find the next gid in the range lo .. hi */ 674 static int 675 getnextgid(int *gidp, int lo, int hi) 676 { 677 for (*gidp = lo ; *gidp < hi ; *gidp += 1) { 678 if (getgrgid((gid_t)*gidp) == NULL) { 679 return 1; 680 } 681 } 682 return 0; 683 } 684 685 #ifdef EXTENSIONS 686 /* save a range of uids */ 687 static int 688 save_range(user_t *up, char *cp) 689 { 690 int from; 691 int to; 692 int i; 693 694 if (up->u_rsize == 0) { 695 up->u_rsize = 32; 696 NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0)); 697 } else if (up->u_rc == up->u_rsize) { 698 up->u_rsize *= 2; 699 RENEW(range_t, up->u_rv, up->u_rsize, return(0)); 700 } 701 if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) { 702 for (i = up->u_defrc ; i < up->u_rc ; i++) { 703 if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) { 704 break; 705 } 706 } 707 if (i == up->u_rc) { 708 up->u_rv[up->u_rc].r_from = from; 709 up->u_rv[up->u_rc].r_to = to; 710 up->u_rc += 1; 711 } 712 } else { 713 warnx("Bad range `%s'", cp); 714 return 0; 715 } 716 return 1; 717 } 718 #endif 719 720 /* set the defaults in the defaults file */ 721 static int 722 setdefaults(user_t *up) 723 { 724 char template[MaxFileNameLen]; 725 FILE *fp; 726 int ret; 727 int fd; 728 #ifdef EXTENSIONS 729 int i; 730 #endif 731 732 (void) snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE); 733 if ((fd = mkstemp(template)) < 0) { 734 warnx("can't mkstemp `%s' for writing", CONFFILE); 735 return 0; 736 } 737 if ((fp = fdopen(fd, "w")) == NULL) { 738 warn("can't fdopen `%s' for writing", CONFFILE); 739 return 0; 740 } 741 ret = 1; 742 if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 || 743 fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 || 744 fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 || 745 fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 || 746 #ifdef EXTENSIONS 747 fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 || 748 #endif 749 fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ? UNSET_INACTIVE : up->u_inactive) <= 0 || 750 fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ? UNSET_EXPIRY : up->u_expire) <= 0 || 751 fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? "false" : "true") <= 0) { 752 warn("can't write to `%s'", CONFFILE); 753 ret = 0; 754 } 755 #ifdef EXTENSIONS 756 for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0 ; i < up->u_rc ; i++) { 757 if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) { 758 warn("can't write to `%s'", CONFFILE); 759 ret = 0; 760 } 761 } 762 #endif 763 (void) fclose(fp); 764 if (ret) { 765 ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0)); 766 } 767 return ret; 768 } 769 770 /* read the defaults file */ 771 static void 772 read_defaults(user_t *up) 773 { 774 struct stat st; 775 size_t lineno; 776 size_t len; 777 FILE *fp; 778 unsigned char *cp; 779 unsigned char *s; 780 781 memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP)); 782 memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR)); 783 memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR)); 784 memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL)); 785 memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT)); 786 #ifdef EXTENSIONS 787 memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS)); 788 #endif 789 up->u_rsize = 16; 790 up->u_defrc = 0; 791 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1)); 792 up->u_inactive = DEF_INACTIVE; 793 up->u_expire = DEF_EXPIRE; 794 if ((fp = fopen(CONFFILE, "r")) == NULL) { 795 if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) { 796 warn("can't create `%s' defaults file", CONFFILE); 797 } 798 fp = fopen(CONFFILE, "r"); 799 } 800 if (fp != NULL) { 801 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) { 802 if (strncmp(s, "group", 5) == 0) { 803 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 804 } 805 memsave(&up->u_primgrp, cp, strlen(cp)); 806 } else if (strncmp(s, "base_dir", 8) == 0) { 807 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 808 } 809 memsave(&up->u_basedir, cp, strlen(cp)); 810 } else if (strncmp(s, "skel_dir", 8) == 0) { 811 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 812 } 813 memsave(&up->u_skeldir, cp, strlen(cp)); 814 } else if (strncmp(s, "shell", 5) == 0) { 815 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 816 } 817 memsave(&up->u_shell, cp, strlen(cp)); 818 #ifdef EXTENSIONS 819 } else if (strncmp(s, "class", 5) == 0) { 820 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 821 } 822 memsave(&up->u_class, cp, strlen(cp)); 823 #endif 824 } else if (strncmp(s, "inactive", 8) == 0) { 825 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 826 } 827 if (strcmp(cp, UNSET_INACTIVE) == 0) { 828 if (up->u_inactive) { 829 FREE(up->u_inactive); 830 } 831 up->u_inactive = NULL; 832 } else { 833 memsave(&up->u_inactive, cp, strlen(cp)); 834 } 835 #ifdef EXTENSIONS 836 } else if (strncmp(s, "range", 5) == 0) { 837 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 838 } 839 (void) save_range(up, cp); 840 #endif 841 #ifdef EXTENSIONS 842 } else if (strncmp(s, "preserve", 8) == 0) { 843 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 844 } 845 up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 : 846 (strncmp(cp, "yes", 3) == 0) ? 1 : 847 atoi(cp); 848 #endif 849 } else if (strncmp(s, "expire", 6) == 0) { 850 for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) { 851 } 852 if (strcmp(cp, UNSET_EXPIRY) == 0) { 853 if (up->u_expire) { 854 FREE(up->u_expire); 855 } 856 up->u_expire = NULL; 857 } else { 858 memsave(&up->u_expire, cp, strlen(cp)); 859 } 860 } 861 (void) free(s); 862 } 863 (void) fclose(fp); 864 } 865 if (up->u_rc == 0) { 866 up->u_rv[up->u_rc].r_from = DEF_LOWUID; 867 up->u_rv[up->u_rc].r_to = DEF_HIGHUID; 868 up->u_rc += 1; 869 } 870 up->u_defrc = up->u_rc; 871 } 872 873 /* return the next valid unused uid */ 874 static int 875 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid) 876 { 877 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) { 878 if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) { 879 if (sync_uid_gid) { 880 if (getgrgid((gid_t)(*uid)) == NULL) { 881 return 1; 882 } 883 } else { 884 return 1; 885 } 886 } 887 } 888 return 0; 889 } 890 891 /* structure which defines a password type */ 892 typedef struct passwd_type_t { 893 const char *type; /* optional type descriptor */ 894 int desc_length; /* length of type descriptor */ 895 int length; /* length of password */ 896 const char *regex; /* regexp to output the password */ 897 int re_sub; /* subscript of regexp to use */ 898 } passwd_type_t; 899 900 static passwd_type_t passwd_types[] = { 901 { "$2a", 3, 54, "\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* Blowfish */ 902 { "$1", 2, 34, NULL, 0 }, /* MD5 */ 903 { "", 0, DES_Len,NULL, 0 }, /* standard DES */ 904 { NULL, -1, -1, NULL, 0 } /* none - terminate search */ 905 }; 906 907 /* return non-zero if it's a valid password - check length for cipher type */ 908 static int 909 valid_password_length(char *newpasswd) 910 { 911 passwd_type_t *pwtp; 912 regmatch_t matchv[10]; 913 regex_t r; 914 915 for (pwtp = passwd_types ; pwtp->desc_length >= 0 ; pwtp++) { 916 if (strncmp(newpasswd, pwtp->type, pwtp->desc_length) == 0) { 917 if (pwtp->regex == NULL) { 918 return strlen(newpasswd) == pwtp->length; 919 } 920 (void) regcomp(&r, pwtp->regex, REG_EXTENDED); 921 if (regexec(&r, newpasswd, 10, matchv, 0) == 0) { 922 regfree(&r); 923 return (int)(matchv[pwtp->re_sub].rm_eo - matchv[pwtp->re_sub].rm_so + 1) == pwtp->length; 924 } 925 regfree(&r); 926 } 927 } 928 return 0; 929 } 930 931 /* look for a valid time, return 0 if it was specified but bad */ 932 static int 933 scantime(time_t *tp, char *s) 934 { 935 struct tm tm; 936 937 *tp = 0; 938 if (s != NULL) { 939 (void) memset(&tm, 0, sizeof(tm)); 940 if (strptime(s, "%c", &tm) != NULL) { 941 *tp = mktime(&tm); 942 } else if (strptime(s, "%B %d %Y", &tm) != NULL) { 943 *tp = mktime(&tm); 944 } else if (isdigit((unsigned char) *s)) { 945 *tp = atoi(s); 946 } else { 947 return 0; 948 } 949 } 950 return 1; 951 } 952 953 /* add a user */ 954 static int 955 adduser(char *login_name, user_t *up) 956 { 957 struct group *grp; 958 struct stat st; 959 time_t expire; 960 time_t inactive; 961 char password[PasswordLength + 1]; 962 char home[MaxFileNameLen]; 963 char buf[MaxFileNameLen]; 964 int sync_uid_gid; 965 int masterfd; 966 int ptmpfd; 967 int gid; 968 int cc; 969 int i; 970 971 if (!valid_login(login_name, up->u_allow_samba)) { 972 errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name); 973 } 974 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 975 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD); 976 } 977 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 978 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD); 979 } 980 pw_init(); 981 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 982 (void) close(masterfd); 983 err(EXIT_FAILURE, "can't obtain pw_lock"); 984 } 985 while ((cc = read(masterfd, buf, sizeof(buf))) > 0) { 986 if (write(ptmpfd, buf, (size_t)(cc)) != cc) { 987 (void) close(masterfd); 988 (void) close(ptmpfd); 989 pw_abort(); 990 err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc); 991 } 992 } 993 /* if no uid was specified, get next one in [low_uid..high_uid] range */ 994 sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0); 995 if (up->u_uid == -1) { 996 int got_id = 0; 997 998 /* 999 * Look for a free UID in the command line ranges (if any). 1000 * These start after the ranges specified in the config file. 1001 */ 1002 for (i = up->u_defrc; !got_id && i < up->u_rc ; i++) { 1003 got_id = getnextuid(sync_uid_gid, &up->u_uid, 1004 up->u_rv[i].r_from, up->u_rv[i].r_to); 1005 } 1006 /* 1007 * If there were no free UIDs in the command line ranges, 1008 * try the ranges from the config file (there will always 1009 * be at least one default). 1010 */ 1011 for (i = 0; !got_id && i < up->u_defrc; i++) { 1012 got_id = getnextuid(sync_uid_gid, &up->u_uid, 1013 up->u_rv[i].r_from, up->u_rv[i].r_to); 1014 } 1015 if (!got_id) { 1016 (void) close(ptmpfd); 1017 pw_abort(); 1018 errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid); 1019 } 1020 } 1021 /* check uid isn't already allocated */ 1022 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) { 1023 (void) close(ptmpfd); 1024 pw_abort(); 1025 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid); 1026 } 1027 /* if -g=uid was specified, check gid is unused */ 1028 if (sync_uid_gid) { 1029 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 1030 (void) close(ptmpfd); 1031 pw_abort(); 1032 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid); 1033 } 1034 gid = up->u_uid; 1035 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 1036 gid = grp->gr_gid; 1037 } else if (is_number(up->u_primgrp) && 1038 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) { 1039 gid = grp->gr_gid; 1040 } else { 1041 (void) close(ptmpfd); 1042 pw_abort(); 1043 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp); 1044 } 1045 /* check name isn't already in use */ 1046 if (!(up->u_flags & F_DUPUID) && getpwnam(login_name) != NULL) { 1047 (void) close(ptmpfd); 1048 pw_abort(); 1049 errx(EXIT_FAILURE, "already a `%s' user", login_name); 1050 } 1051 if (up->u_flags & F_HOMEDIR) { 1052 (void) strlcpy(home, up->u_home, sizeof(home)); 1053 } else { 1054 /* if home directory hasn't been given, make it up */ 1055 (void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login_name); 1056 } 1057 if (!scantime(&inactive, up->u_inactive)) { 1058 warnx("Warning: inactive time `%s' invalid, account expiry off", 1059 up->u_inactive); 1060 } 1061 if (!scantime(&expire, up->u_expire)) { 1062 warnx("Warning: expire time `%s' invalid, password expiry off", 1063 up->u_expire); 1064 } 1065 if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) { 1066 warnx("Warning: home directory `%s' doesn't exist, and -m was not specified", 1067 home); 1068 } 1069 password[sizeof(password) - 1] = '\0'; 1070 if (up->u_password != NULL && valid_password_length(up->u_password)) { 1071 (void) strlcpy(password, up->u_password, sizeof(password)); 1072 } else { 1073 (void) memset(password, '*', DES_Len); 1074 password[DES_Len] = 0; 1075 if (up->u_password != NULL) { 1076 warnx("Password `%s' is invalid: setting it to `%s'", 1077 up->u_password, password); 1078 } 1079 } 1080 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", 1081 login_name, 1082 password, 1083 up->u_uid, 1084 gid, 1085 #ifdef EXTENSIONS 1086 up->u_class, 1087 #else 1088 "", 1089 #endif 1090 (long) inactive, 1091 (long) expire, 1092 up->u_comment, 1093 home, 1094 up->u_shell); 1095 if (write(ptmpfd, buf, (size_t) cc) != cc) { 1096 (void) close(ptmpfd); 1097 pw_abort(); 1098 err(EXIT_FAILURE, "can't add `%s'", buf); 1099 } 1100 if (up->u_flags & F_MKDIR) { 1101 if (lstat(home, &st) == 0) { 1102 (void) close(ptmpfd); 1103 pw_abort(); 1104 errx(EXIT_FAILURE, "home directory `%s' already exists", home); 1105 } else { 1106 if (asystem("%s -p %s", MKDIR, home) != 0) { 1107 (void) close(ptmpfd); 1108 pw_abort(); 1109 err(EXIT_FAILURE, "can't mkdir `%s'", home); 1110 } 1111 (void) copydotfiles(up->u_skeldir, up->u_uid, gid, home); 1112 } 1113 } 1114 if (strcmp(up->u_primgrp, "=uid") == 0 && 1115 getgrnam(login_name) == NULL && 1116 !creategid(login_name, gid, login_name)) { 1117 (void) close(ptmpfd); 1118 pw_abort(); 1119 errx(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login_name); 1120 } 1121 if (up->u_groupc > 0 && !append_group(login_name, up->u_groupc, up->u_groupv)) { 1122 (void) close(ptmpfd); 1123 pw_abort(); 1124 errx(EXIT_FAILURE, "can't append `%s' to new groups", login_name); 1125 } 1126 (void) close(ptmpfd); 1127 #if PW_MKDB_ARGC == 2 1128 if (pw_mkdb(login_name, 0) < 0) { 1129 pw_abort(); 1130 err(EXIT_FAILURE, "pw_mkdb failed"); 1131 } 1132 #else 1133 if (pw_mkdb() < 0) { 1134 pw_abort(); 1135 err(EXIT_FAILURE, "pw_mkdb failed"); 1136 } 1137 #endif 1138 syslog(LOG_INFO, "new user added: name=%s, uid=%d, gid=%d, home=%s, shell=%s", 1139 login_name, up->u_uid, gid, home, up->u_shell); 1140 return 1; 1141 } 1142 1143 /* remove a user from the groups file */ 1144 static int 1145 rm_user_from_groups(char *login_name) 1146 { 1147 struct stat st; 1148 regmatch_t matchv[10]; 1149 regex_t r; 1150 FILE *from; 1151 FILE *to; 1152 char line[MaxEntryLen]; 1153 char buf[MaxEntryLen]; 1154 char f[MaxFileNameLen]; 1155 int fd; 1156 int cc; 1157 int sc; 1158 1159 (void) snprintf(line, sizeof(line), "(:|,)(%s)(,|$)", login_name); 1160 if (regcomp(&r, line, REG_EXTENDED|REG_NEWLINE) != 0) { 1161 warn("can't compile regular expression `%s'", line); 1162 return 0; 1163 } 1164 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 1165 warn("can't remove gid for `%s': can't open `%s'", login_name, _PATH_GROUP); 1166 return 0; 1167 } 1168 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 1169 warn("can't lock `%s'", _PATH_GROUP); 1170 } 1171 (void) fstat(fileno(from), &st); 1172 (void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 1173 if ((fd = mkstemp(f)) < 0) { 1174 (void) fclose(from); 1175 warn("can't create gid: mkstemp failed"); 1176 return 0; 1177 } 1178 if ((to = fdopen(fd, "w")) == NULL) { 1179 (void) fclose(from); 1180 (void) close(fd); 1181 (void) unlink(f); 1182 warn("can't create gid: fdopen `%s' failed", f); 1183 return 0; 1184 } 1185 while (fgets(buf, sizeof(buf), from) > 0) { 1186 cc = strlen(buf); 1187 if (regexec(&r, buf, 10, matchv, 0) == 0) { 1188 if (buf[(int)matchv[1].rm_so] == ',') { 1189 matchv[2].rm_so = matchv[1].rm_so; 1190 } else if (matchv[2].rm_eo != matchv[3].rm_eo) { 1191 matchv[2].rm_eo = matchv[3].rm_eo; 1192 } 1193 cc -= (int) matchv[2].rm_eo; 1194 sc = (int) matchv[2].rm_so; 1195 if (fwrite(buf, sizeof(char), sc, to) != sc || 1196 fwrite(&buf[(int)matchv[2].rm_eo], sizeof(char), cc, to) != cc) { 1197 (void) fclose(from); 1198 (void) close(fd); 1199 (void) unlink(f); 1200 warn("can't create gid: short write to `%s'", f); 1201 return 0; 1202 } 1203 } else if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 1204 (void) fclose(from); 1205 (void) close(fd); 1206 (void) unlink(f); 1207 warn("can't create gid: short write to `%s'", f); 1208 return 0; 1209 } 1210 } 1211 (void) fclose(from); 1212 (void) fclose(to); 1213 if (rename(f, _PATH_GROUP) < 0) { 1214 (void) unlink(f); 1215 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP); 1216 return 0; 1217 } 1218 (void) chmod(_PATH_GROUP, st.st_mode & 07777); 1219 return 1; 1220 } 1221 1222 /* check that the user or group is local, not from YP/NIS */ 1223 static int 1224 is_local(char *name, const char *file) 1225 { 1226 FILE *fp; 1227 char buf[MaxEntryLen]; 1228 int len; 1229 int ret; 1230 1231 if ((fp = fopen(file, "r")) == NULL) { 1232 err(EXIT_FAILURE, "can't open `%s'", file); 1233 } 1234 len = strlen(name); 1235 for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) { 1236 if (strncmp(buf, name, len) == 0 && buf[len] == ':') { 1237 ret = 1; 1238 break; 1239 } 1240 } 1241 (void) fclose(fp); 1242 return ret; 1243 } 1244 1245 /* modify a user */ 1246 static int 1247 moduser(char *login_name, char *newlogin, user_t *up, int allow_samba) 1248 { 1249 struct passwd *pwp; 1250 struct group *grp; 1251 const char *homedir; 1252 size_t colonc; 1253 size_t loginc; 1254 size_t len; 1255 size_t cc; 1256 FILE *master; 1257 char newdir[MaxFileNameLen]; 1258 char buf[MaxEntryLen]; 1259 char *colon; 1260 int masterfd; 1261 int ptmpfd; 1262 int error; 1263 1264 if (!valid_login(newlogin, allow_samba)) { 1265 errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name); 1266 } 1267 if ((pwp = getpwnam(login_name)) == NULL) { 1268 errx(EXIT_FAILURE, "No such user `%s'", login_name); 1269 } 1270 if (!is_local(login_name, _PATH_MASTERPASSWD)) { 1271 errx(EXIT_FAILURE, "User `%s' must be a local user", login_name); 1272 } 1273 /* keep dir name in case we need it for '-m' */ 1274 homedir = pwp->pw_dir; 1275 1276 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 1277 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD); 1278 } 1279 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 1280 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD); 1281 } 1282 pw_init(); 1283 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 1284 (void) close(masterfd); 1285 err(EXIT_FAILURE, "can't obtain pw_lock"); 1286 } 1287 if ((master = fdopen(masterfd, "r")) == NULL) { 1288 (void) close(masterfd); 1289 (void) close(ptmpfd); 1290 pw_abort(); 1291 err(EXIT_FAILURE, "can't fdopen fd for %s", _PATH_MASTERPASSWD); 1292 } 1293 if (up != NULL) { 1294 if (up->u_flags & F_USERNAME) { 1295 /* if changing name, check new name isn't already in use */ 1296 if (strcmp(login_name, newlogin) != 0 && getpwnam(newlogin) != NULL) { 1297 (void) close(ptmpfd); 1298 pw_abort(); 1299 errx(EXIT_FAILURE, "already a `%s' user", newlogin); 1300 } 1301 pwp->pw_name = newlogin; 1302 1303 /* 1304 * Provide a new directory name in case the 1305 * home directory is to be moved. 1306 */ 1307 if (up->u_flags & F_MKDIR) { 1308 snprintf(newdir, sizeof(newdir), "%s/%s", up->u_basedir, newlogin); 1309 pwp->pw_dir = newdir; 1310 } 1311 } 1312 if (up->u_flags & F_PASSWORD) { 1313 if (up->u_password != NULL) { 1314 if (!valid_password_length(up->u_password)) { 1315 (void) close(ptmpfd); 1316 pw_abort(); 1317 errx(EXIT_FAILURE, "Invalid password: `%s'", 1318 up->u_password); 1319 } 1320 pwp->pw_passwd = up->u_password; 1321 } 1322 } 1323 if (up->u_flags & F_UID) { 1324 /* check uid isn't already allocated */ 1325 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) { 1326 (void) close(ptmpfd); 1327 pw_abort(); 1328 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid); 1329 } 1330 pwp->pw_uid = up->u_uid; 1331 } 1332 if (up->u_flags & F_GROUP) { 1333 /* if -g=uid was specified, check gid is unused */ 1334 if (strcmp(up->u_primgrp, "=uid") == 0) { 1335 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 1336 (void) close(ptmpfd); 1337 pw_abort(); 1338 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid); 1339 } 1340 pwp->pw_gid = up->u_uid; 1341 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 1342 pwp->pw_gid = grp->gr_gid; 1343 } else if (is_number(up->u_primgrp) && 1344 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) { 1345 pwp->pw_gid = grp->gr_gid; 1346 } else { 1347 (void) close(ptmpfd); 1348 pw_abort(); 1349 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp); 1350 } 1351 } 1352 if (up->u_flags & F_INACTIVE) { 1353 if (!scantime(&pwp->pw_change, up->u_inactive)) { 1354 warnx("Warning: inactive time `%s' invalid, password expiry off", 1355 up->u_inactive); 1356 } 1357 } 1358 if (up->u_flags & F_EXPIRE) { 1359 if (!scantime(&pwp->pw_expire, up->u_expire)) { 1360 warnx("Warning: expire time `%s' invalid, password expiry off", 1361 up->u_expire); 1362 } 1363 } 1364 if (up->u_flags & F_COMMENT) 1365 pwp->pw_gecos = up->u_comment; 1366 if (up->u_flags & F_HOMEDIR) 1367 pwp->pw_dir = up->u_home; 1368 if (up->u_flags & F_SHELL) 1369 pwp->pw_shell = up->u_shell; 1370 #ifdef EXTENSIONS 1371 if (up->u_flags & F_CLASS) 1372 pwp->pw_class = up->u_class; 1373 #endif 1374 } 1375 loginc = strlen(login_name); 1376 while (fgets(buf, sizeof(buf), master) != NULL) { 1377 if ((colon = strchr(buf, ':')) == NULL) { 1378 warnx("Malformed entry `%s'. Skipping", buf); 1379 continue; 1380 } 1381 colonc = (size_t)(colon - buf); 1382 if (strncmp(login_name, buf, loginc) == 0 && loginc == colonc) { 1383 if (up != NULL) { 1384 len = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:" 1385 #ifdef EXTENSIONS 1386 "%s" 1387 #endif 1388 ":%ld:%ld:%s:%s:%s\n", 1389 newlogin, 1390 pwp->pw_passwd, 1391 pwp->pw_uid, 1392 pwp->pw_gid, 1393 #ifdef EXTENSIONS 1394 pwp->pw_class, 1395 #endif 1396 (long)pwp->pw_change, 1397 (long)pwp->pw_expire, 1398 pwp->pw_gecos, 1399 pwp->pw_dir, 1400 pwp->pw_shell); 1401 if (write(ptmpfd, buf, len) != len) { 1402 (void) close(ptmpfd); 1403 pw_abort(); 1404 err(EXIT_FAILURE, "can't add `%s'", buf); 1405 } 1406 } 1407 } else { 1408 len = strlen(buf); 1409 if ((cc = write(ptmpfd, buf, len)) != len) { 1410 (void) close(masterfd); 1411 (void) close(ptmpfd); 1412 pw_abort(); 1413 err(EXIT_FAILURE, "short write to /etc/ptmp (%lld not %lld chars)", 1414 (long long)cc, 1415 (long long)len); 1416 } 1417 } 1418 } 1419 if (up != NULL) { 1420 if ((up->u_flags & F_MKDIR) && 1421 asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) { 1422 (void) close(ptmpfd); 1423 pw_abort(); 1424 err(EXIT_FAILURE, "can't move `%s' to `%s'", 1425 homedir, pwp->pw_dir); 1426 } 1427 if (up->u_groupc > 0 && 1428 !append_group(newlogin, up->u_groupc, up->u_groupv)) { 1429 (void) close(ptmpfd); 1430 pw_abort(); 1431 errx(EXIT_FAILURE, "can't append `%s' to new groups", 1432 newlogin); 1433 } 1434 } 1435 (void) close(ptmpfd); 1436 #if PW_MKDB_ARGC == 2 1437 if (up != NULL && strcmp(login_name, newlogin) == 0) { 1438 error = pw_mkdb(login_name, 0); 1439 } else { 1440 error = pw_mkdb(NULL, 0); 1441 } 1442 #else 1443 error = pw_mkdb(); 1444 #endif 1445 if (error < 0) { 1446 pw_abort(); 1447 err(EXIT_FAILURE, "pw_mkdb failed"); 1448 } 1449 if (up == NULL) { 1450 syslog(LOG_INFO, "user removed: name=%s", login_name); 1451 } else if (strcmp(login_name, newlogin) == 0) { 1452 syslog(LOG_INFO, "user information modified: name=%s, uid=%d, gid=%d, home=%s, shell=%s", 1453 login_name, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, pwp->pw_shell); 1454 } else { 1455 syslog(LOG_INFO, "user information modified: name=%s, new name=%s, uid=%d, gid=%d, home=%s, shell=%s", 1456 login_name, newlogin, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, pwp->pw_shell); 1457 } 1458 return 1; 1459 } 1460 1461 1462 #ifdef EXTENSIONS 1463 /* see if we can find out the user struct */ 1464 static struct passwd * 1465 find_user_info(char *name) 1466 { 1467 struct passwd *pwp; 1468 1469 if ((pwp = getpwnam(name)) != NULL) { 1470 return pwp; 1471 } 1472 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) { 1473 return pwp; 1474 } 1475 return NULL; 1476 } 1477 #endif 1478 1479 #ifdef EXTENSIONS 1480 /* see if we can find out the group struct */ 1481 static struct group * 1482 find_group_info(char *name) 1483 { 1484 struct group *grp; 1485 1486 if ((grp = getgrnam(name)) != NULL) { 1487 return grp; 1488 } 1489 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) { 1490 return grp; 1491 } 1492 return NULL; 1493 } 1494 #endif 1495 1496 /* print out usage message, and then exit */ 1497 void 1498 usermgmt_usage(const char *prog) 1499 { 1500 if (strcmp(prog, "useradd") == 0) { 1501 (void) fprintf(stderr, "usage: %s -D [-b basedir] [-e expiry] " 1502 "[-f inactive] [-g group]\n\t[-r lowuid..highuid] " 1503 "[-s shell] [-L class]\n", prog); 1504 (void) fprintf(stderr, "usage: %s [-G group] [-b basedir] " 1505 "[-c comment] [-d homedir] [-e expiry]\n\t[-f inactive] " 1506 "[-g group] [-k skeletondir] [-m] [-o] [-p password]\n" 1507 "\t[-r lowuid..highuid] [-s shell]\n\t[-u uid] [-v] user\n", 1508 prog); 1509 } else if (strcmp(prog, "usermod") == 0) { 1510 (void) fprintf(stderr, "usage: %s [-G group] [-c comment] " 1511 "[-d homedir] [-e expire] [-f inactive]\n\t[-g group] " 1512 "[-l newname] [-m] [-o] [-p password] [-s shell] [-u uid]\n" 1513 "\t[-L class] [-v] user\n", prog); 1514 } else if (strcmp(prog, "userdel") == 0) { 1515 (void) fprintf(stderr, "usage: %s -D [-p preserve]\n", prog); 1516 (void) fprintf(stderr, 1517 "usage: %s [-p preserve] [-r] [-v] user\n", prog); 1518 #ifdef EXTENSIONS 1519 } else if (strcmp(prog, "userinfo") == 0) { 1520 (void) fprintf(stderr, "usage: %s [-e] [-v] user\n", prog); 1521 #endif 1522 } else if (strcmp(prog, "groupadd") == 0) { 1523 (void) fprintf(stderr, "usage: %s [-g gid] [-o] [-v] group\n", 1524 prog); 1525 } else if (strcmp(prog, "groupdel") == 0) { 1526 (void) fprintf(stderr, "usage: %s [-v] group\n", prog); 1527 } else if (strcmp(prog, "groupmod") == 0) { 1528 (void) fprintf(stderr, 1529 "usage: %s [-g gid] [-o] [-n newname] [-v] group\n", prog); 1530 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) { 1531 (void) fprintf(stderr, 1532 "usage: %s ( add | del | mod | info ) ...\n", prog); 1533 #ifdef EXTENSIONS 1534 } else if (strcmp(prog, "groupinfo") == 0) { 1535 (void) fprintf(stderr, "usage: %s [-e] [-v] group\n", prog); 1536 #endif 1537 } 1538 exit(EXIT_FAILURE); 1539 /* NOTREACHED */ 1540 } 1541 1542 #ifdef EXTENSIONS 1543 #define ADD_OPT_EXTENSIONS "p:r:vL:S" 1544 #else 1545 #define ADD_OPT_EXTENSIONS 1546 #endif 1547 1548 int 1549 useradd(int argc, char **argv) 1550 { 1551 user_t u; 1552 int defaultfield; 1553 int bigD; 1554 int c; 1555 #ifdef EXTENSIONS 1556 int i; 1557 #endif 1558 1559 (void) memset(&u, 0, sizeof(u)); 1560 read_defaults(&u); 1561 u.u_uid = -1; 1562 defaultfield = bigD = 0; 1563 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) { 1564 switch(c) { 1565 case 'D': 1566 bigD = 1; 1567 break; 1568 case 'G': 1569 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL && 1570 u.u_groupc < NGROUPS_MAX) { 1571 if (u.u_groupv[u.u_groupc][0] != 0) { 1572 u.u_groupc++; 1573 } 1574 } 1575 if (optarg != NULL) { 1576 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX); 1577 } 1578 break; 1579 #ifdef EXTENSIONS 1580 case 'S': 1581 u.u_allow_samba = 1; 1582 break; 1583 #endif 1584 case 'b': 1585 defaultfield = 1; 1586 memsave(&u.u_basedir, optarg, strlen(optarg)); 1587 break; 1588 case 'c': 1589 memsave(&u.u_comment, optarg, strlen(optarg)); 1590 break; 1591 case 'd': 1592 memsave(&u.u_home, optarg, strlen(optarg)); 1593 u.u_flags |= F_HOMEDIR; 1594 break; 1595 case 'e': 1596 defaultfield = 1; 1597 memsave(&u.u_expire, optarg, strlen(optarg)); 1598 break; 1599 case 'f': 1600 defaultfield = 1; 1601 memsave(&u.u_inactive, optarg, strlen(optarg)); 1602 break; 1603 case 'g': 1604 defaultfield = 1; 1605 memsave(&u.u_primgrp, optarg, strlen(optarg)); 1606 break; 1607 case 'k': 1608 defaultfield = 1; 1609 memsave(&u.u_skeldir, optarg, strlen(optarg)); 1610 break; 1611 #ifdef EXTENSIONS 1612 case 'L': 1613 defaultfield = 1; 1614 memsave(&u.u_class, optarg, strlen(optarg)); 1615 break; 1616 #endif 1617 case 'm': 1618 u.u_flags |= F_MKDIR; 1619 break; 1620 case 'o': 1621 u.u_flags |= F_DUPUID; 1622 break; 1623 #ifdef EXTENSIONS 1624 case 'p': 1625 memsave(&u.u_password, optarg, strlen(optarg)); 1626 break; 1627 #endif 1628 #ifdef EXTENSIONS 1629 case 'r': 1630 defaultfield = 1; 1631 (void) save_range(&u, optarg); 1632 break; 1633 #endif 1634 case 's': 1635 defaultfield = 1; 1636 memsave(&u.u_shell, optarg, strlen(optarg)); 1637 break; 1638 case 'u': 1639 if (!is_number(optarg)) { 1640 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric"); 1641 } 1642 u.u_uid = atoi(optarg); 1643 break; 1644 #ifdef EXTENSIONS 1645 case 'v': 1646 verbose = 1; 1647 break; 1648 #endif 1649 default: 1650 usermgmt_usage("useradd"); 1651 /* NOTREACHED */ 1652 } 1653 } 1654 if (bigD) { 1655 if (defaultfield) { 1656 checkeuid(); 1657 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 1658 } 1659 (void) printf("group\t\t%s\n", u.u_primgrp); 1660 (void) printf("base_dir\t%s\n", u.u_basedir); 1661 (void) printf("skel_dir\t%s\n", u.u_skeldir); 1662 (void) printf("shell\t\t%s\n", u.u_shell); 1663 #ifdef EXTENSIONS 1664 (void) printf("class\t\t%s\n", u.u_class); 1665 #endif 1666 (void) printf("inactive\t%s\n", (u.u_inactive == NULL) ? UNSET_INACTIVE : u.u_inactive); 1667 (void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire); 1668 #ifdef EXTENSIONS 1669 for (i = 0 ; i < u.u_rc ; i++) { 1670 (void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to); 1671 } 1672 #endif 1673 return EXIT_SUCCESS; 1674 } 1675 argc -= optind; 1676 argv += optind; 1677 if (argc != 1) { 1678 usermgmt_usage("useradd"); 1679 } 1680 checkeuid(); 1681 openlog("useradd", LOG_PID, LOG_USER); 1682 return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE; 1683 } 1684 1685 #ifdef EXTENSIONS 1686 #define MOD_OPT_EXTENSIONS "p:vL:S" 1687 #else 1688 #define MOD_OPT_EXTENSIONS 1689 #endif 1690 1691 int 1692 usermod(int argc, char **argv) 1693 { 1694 user_t u; 1695 char newuser[MaxUserNameLen + 1]; 1696 int c, have_new_user; 1697 1698 (void) memset(&u, 0, sizeof(u)); 1699 (void) memset(newuser, 0, sizeof(newuser)); 1700 read_defaults(&u); 1701 have_new_user = 0; 1702 while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) { 1703 switch(c) { 1704 case 'G': 1705 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL && 1706 u.u_groupc < NGROUPS_MAX) { 1707 if (u.u_groupv[u.u_groupc][0] != 0) { 1708 u.u_groupc++; 1709 } 1710 } 1711 if (optarg != NULL) { 1712 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX); 1713 } 1714 u.u_flags |= F_SECGROUP; 1715 break; 1716 #ifdef EXTENSIONS 1717 case 'S': 1718 u.u_allow_samba = 1; 1719 break; 1720 #endif 1721 case 'c': 1722 memsave(&u.u_comment, optarg, strlen(optarg)); 1723 u.u_flags |= F_COMMENT; 1724 break; 1725 case 'd': 1726 memsave(&u.u_home, optarg, strlen(optarg)); 1727 u.u_flags |= F_HOMEDIR; 1728 break; 1729 case 'e': 1730 memsave(&u.u_expire, optarg, strlen(optarg)); 1731 u.u_flags |= F_EXPIRE; 1732 break; 1733 case 'f': 1734 memsave(&u.u_inactive, optarg, strlen(optarg)); 1735 u.u_flags |= F_INACTIVE; 1736 break; 1737 case 'g': 1738 memsave(&u.u_primgrp, optarg, strlen(optarg)); 1739 u.u_flags |= F_GROUP; 1740 break; 1741 case 'l': 1742 (void) strlcpy(newuser, optarg, sizeof(newuser)); 1743 have_new_user = 1; 1744 u.u_flags |= F_USERNAME; 1745 break; 1746 #ifdef EXTENSIONS 1747 case 'L': 1748 memsave(&u.u_class, optarg, strlen(optarg)); 1749 u.u_flags |= F_CLASS; 1750 break; 1751 #endif 1752 case 'm': 1753 u.u_flags |= F_MKDIR; 1754 break; 1755 case 'o': 1756 u.u_flags |= F_DUPUID; 1757 break; 1758 #ifdef EXTENSIONS 1759 case 'p': 1760 memsave(&u.u_password, optarg, strlen(optarg)); 1761 u.u_flags |= F_PASSWORD; 1762 break; 1763 #endif 1764 case 's': 1765 memsave(&u.u_shell, optarg, strlen(optarg)); 1766 u.u_flags |= F_SHELL; 1767 break; 1768 case 'u': 1769 if (!is_number(optarg)) { 1770 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric"); 1771 } 1772 u.u_uid = atoi(optarg); 1773 u.u_flags |= F_UID; 1774 break; 1775 #ifdef EXTENSIONS 1776 case 'v': 1777 verbose = 1; 1778 break; 1779 #endif 1780 default: 1781 usermgmt_usage("usermod"); 1782 /* NOTREACHED */ 1783 } 1784 } 1785 if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) && 1786 !(u.u_flags & F_USERNAME)) { 1787 warnx("option 'm' useless without 'd' or 'l' -- ignored"); 1788 u.u_flags &= ~F_MKDIR; 1789 } 1790 argc -= optind; 1791 argv += optind; 1792 if (argc != 1) { 1793 usermgmt_usage("usermod"); 1794 } 1795 checkeuid(); 1796 openlog("usermod", LOG_PID, LOG_USER); 1797 return moduser(*argv, (have_new_user) ? newuser : *argv, &u, u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE; 1798 } 1799 1800 #ifdef EXTENSIONS 1801 #define DEL_OPT_EXTENSIONS "Dp:vS" 1802 #else 1803 #define DEL_OPT_EXTENSIONS 1804 #endif 1805 1806 int 1807 userdel(int argc, char **argv) 1808 { 1809 struct passwd *pwp; 1810 user_t u; 1811 char password[PasswordLength + 1]; 1812 int defaultfield; 1813 int rmhome; 1814 int bigD; 1815 int c; 1816 1817 (void) memset(&u, 0, sizeof(u)); 1818 read_defaults(&u); 1819 defaultfield = bigD = rmhome = 0; 1820 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) { 1821 switch(c) { 1822 #ifdef EXTENSIONS 1823 case 'D': 1824 bigD = 1; 1825 break; 1826 #endif 1827 #ifdef EXTENSIONS 1828 case 'S': 1829 u.u_allow_samba = 1; 1830 break; 1831 #endif 1832 #ifdef EXTENSIONS 1833 case 'p': 1834 defaultfield = 1; 1835 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 : 1836 (strcmp(optarg, "yes") == 0) ? 1 : 1837 atoi(optarg); 1838 break; 1839 #endif 1840 case 'r': 1841 rmhome = 1; 1842 break; 1843 #ifdef EXTENSIONS 1844 case 'v': 1845 verbose = 1; 1846 break; 1847 #endif 1848 default: 1849 usermgmt_usage("userdel"); 1850 /* NOTREACHED */ 1851 } 1852 } 1853 #ifdef EXTENSIONS 1854 if (bigD) { 1855 if (defaultfield) { 1856 checkeuid(); 1857 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 1858 } 1859 (void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false"); 1860 return EXIT_SUCCESS; 1861 } 1862 #endif 1863 argc -= optind; 1864 argv += optind; 1865 if (argc != 1) { 1866 usermgmt_usage("userdel"); 1867 } 1868 checkeuid(); 1869 if ((pwp = getpwnam(*argv)) == NULL) { 1870 warnx("No such user `%s'", *argv); 1871 return EXIT_FAILURE; 1872 } 1873 if (rmhome) 1874 (void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir); 1875 if (u.u_preserve) { 1876 u.u_flags |= F_SHELL; 1877 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN)); 1878 (void) memset(password, '*', DES_Len); 1879 password[DES_Len] = 0; 1880 memsave(&u.u_password, password, strlen(password)); 1881 u.u_flags |= F_PASSWORD; 1882 openlog("userdel", LOG_PID, LOG_USER); 1883 return moduser(*argv, *argv, &u, u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE; 1884 } 1885 if (!rm_user_from_groups(*argv)) { 1886 return 0; 1887 } 1888 openlog("userdel", LOG_PID, LOG_USER); 1889 return moduser(*argv, *argv, NULL, u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE; 1890 } 1891 1892 #ifdef EXTENSIONS 1893 #define GROUP_ADD_OPT_EXTENSIONS "v" 1894 #else 1895 #define GROUP_ADD_OPT_EXTENSIONS 1896 #endif 1897 1898 /* add a group */ 1899 int 1900 groupadd(int argc, char **argv) 1901 { 1902 int dupgid; 1903 int gid; 1904 int c; 1905 1906 gid = -1; 1907 dupgid = 0; 1908 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) { 1909 switch(c) { 1910 case 'g': 1911 if (!is_number(optarg)) { 1912 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric"); 1913 } 1914 gid = atoi(optarg); 1915 break; 1916 case 'o': 1917 dupgid = 1; 1918 break; 1919 #ifdef EXTENSIONS 1920 case 'v': 1921 verbose = 1; 1922 break; 1923 #endif 1924 default: 1925 usermgmt_usage("groupadd"); 1926 /* NOTREACHED */ 1927 } 1928 } 1929 argc -= optind; 1930 argv += optind; 1931 if (argc != 1) { 1932 usermgmt_usage("groupadd"); 1933 } 1934 checkeuid(); 1935 if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) { 1936 err(EXIT_FAILURE, "can't add group: can't get next gid"); 1937 } 1938 if (!dupgid && getgrgid((gid_t) gid) != NULL) { 1939 errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid); 1940 } 1941 if (!valid_group(*argv)) { 1942 warnx("warning - invalid group name `%s'", *argv); 1943 } 1944 openlog("groupadd", LOG_PID, LOG_USER); 1945 if (!creategid(*argv, gid, "")) { 1946 errx(EXIT_FAILURE, "can't add group: problems with %s file", _PATH_GROUP); 1947 } 1948 return EXIT_SUCCESS; 1949 } 1950 1951 #ifdef EXTENSIONS 1952 #define GROUP_DEL_OPT_EXTENSIONS "v" 1953 #else 1954 #define GROUP_DEL_OPT_EXTENSIONS 1955 #endif 1956 1957 /* remove a group */ 1958 int 1959 groupdel(int argc, char **argv) 1960 { 1961 int c; 1962 1963 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) { 1964 switch(c) { 1965 #ifdef EXTENSIONS 1966 case 'v': 1967 verbose = 1; 1968 break; 1969 #endif 1970 default: 1971 usermgmt_usage("groupdel"); 1972 /* NOTREACHED */ 1973 } 1974 } 1975 argc -= optind; 1976 argv += optind; 1977 if (argc != 1) { 1978 usermgmt_usage("groupdel"); 1979 } 1980 checkeuid(); 1981 openlog("groupdel", LOG_PID, LOG_USER); 1982 if (!modify_gid(*argv, NULL)) { 1983 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP); 1984 } 1985 return EXIT_SUCCESS; 1986 } 1987 1988 #ifdef EXTENSIONS 1989 #define GROUP_MOD_OPT_EXTENSIONS "v" 1990 #else 1991 #define GROUP_MOD_OPT_EXTENSIONS 1992 #endif 1993 1994 /* modify a group */ 1995 int 1996 groupmod(int argc, char **argv) 1997 { 1998 struct group *grp; 1999 char buf[MaxEntryLen]; 2000 char *newname; 2001 char **cpp; 2002 int dupgid; 2003 int gid; 2004 int cc; 2005 int c; 2006 2007 gid = -1; 2008 dupgid = 0; 2009 newname = NULL; 2010 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) { 2011 switch(c) { 2012 case 'g': 2013 if (!is_number(optarg)) { 2014 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric"); 2015 } 2016 gid = atoi(optarg); 2017 break; 2018 case 'o': 2019 dupgid = 1; 2020 break; 2021 case 'n': 2022 memsave(&newname, optarg, strlen(optarg)); 2023 break; 2024 #ifdef EXTENSIONS 2025 case 'v': 2026 verbose = 1; 2027 break; 2028 #endif 2029 default: 2030 usermgmt_usage("groupmod"); 2031 /* NOTREACHED */ 2032 } 2033 } 2034 argc -= optind; 2035 argv += optind; 2036 if (argc != 1) { 2037 usermgmt_usage("groupmod"); 2038 } 2039 checkeuid(); 2040 if (gid < 0 && newname == NULL) { 2041 err(EXIT_FAILURE, "Nothing to change"); 2042 } 2043 if (dupgid && gid < 0) { 2044 err(EXIT_FAILURE, "Duplicate which gid?"); 2045 } 2046 if ((grp = getgrnam(*argv)) == NULL) { 2047 err(EXIT_FAILURE, "can't find group `%s' to modify", *argv); 2048 } 2049 if (!is_local(*argv, _PATH_GROUP)) { 2050 errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv); 2051 } 2052 if (newname != NULL && !valid_group(newname)) { 2053 warn("warning - invalid group name `%s'", newname); 2054 } 2055 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:", 2056 (newname) ? newname : grp->gr_name, 2057 grp->gr_passwd, 2058 (gid < 0) ? grp->gr_gid : gid); 2059 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) { 2060 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp, 2061 (cpp[1] == NULL) ? "" : ","); 2062 } 2063 cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n"); 2064 openlog("groupmod", LOG_PID, LOG_USER); 2065 if (!modify_gid(*argv, buf)) { 2066 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP); 2067 } 2068 return EXIT_SUCCESS; 2069 } 2070 2071 #ifdef EXTENSIONS 2072 /* display user information */ 2073 int 2074 userinfo(int argc, char **argv) 2075 { 2076 struct passwd *pwp; 2077 struct group *grp; 2078 char buf[MaxEntryLen]; 2079 char **cpp; 2080 int exists; 2081 int cc; 2082 int i; 2083 2084 exists = 0; 2085 while ((i = getopt(argc, argv, "ev")) != -1) { 2086 switch(i) { 2087 case 'e': 2088 exists = 1; 2089 break; 2090 case 'v': 2091 verbose = 1; 2092 break; 2093 default: 2094 usermgmt_usage("userinfo"); 2095 /* NOTREACHED */ 2096 } 2097 } 2098 argc -= optind; 2099 argv += optind; 2100 if (argc != 1) { 2101 usermgmt_usage("userinfo"); 2102 } 2103 pwp = find_user_info(*argv); 2104 if (exists) { 2105 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE); 2106 } 2107 if (pwp == NULL) { 2108 errx(EXIT_FAILURE, "can't find user `%s'", *argv); 2109 } 2110 (void) printf("login\t%s\n", pwp->pw_name); 2111 (void) printf("passwd\t%s\n", pwp->pw_passwd); 2112 (void) printf("uid\t%d\n", pwp->pw_uid); 2113 for (cc = 0 ; (grp = getgrent()) != NULL ; ) { 2114 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2115 if (strcmp(*cpp, *argv) == 0 && grp->gr_gid != pwp->pw_gid) { 2116 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name); 2117 } 2118 } 2119 } 2120 if ((grp = getgrgid(pwp->pw_gid)) == NULL) { 2121 (void) printf("groups\t%d %s\n", pwp->pw_gid, buf); 2122 } else { 2123 (void) printf("groups\t%s %s\n", grp->gr_name, buf); 2124 } 2125 (void) printf("change\t%s", pwp->pw_change ? ctime(&pwp->pw_change) : "NEVER\n"); 2126 #ifdef EXTENSIONS 2127 (void) printf("class\t%s\n", pwp->pw_class); 2128 #endif 2129 (void) printf("gecos\t%s\n", pwp->pw_gecos); 2130 (void) printf("dir\t%s\n", pwp->pw_dir); 2131 (void) printf("shell\t%s\n", pwp->pw_shell); 2132 (void) printf("expire\t%s", pwp->pw_expire ? ctime(&pwp->pw_expire) : "NEVER\n"); 2133 return EXIT_SUCCESS; 2134 } 2135 #endif 2136 2137 #ifdef EXTENSIONS 2138 /* display user information */ 2139 int 2140 groupinfo(int argc, char **argv) 2141 { 2142 struct group *grp; 2143 char **cpp; 2144 int exists; 2145 int i; 2146 2147 exists = 0; 2148 while ((i = getopt(argc, argv, "ev")) != -1) { 2149 switch(i) { 2150 case 'e': 2151 exists = 1; 2152 break; 2153 case 'v': 2154 verbose = 1; 2155 break; 2156 default: 2157 usermgmt_usage("groupinfo"); 2158 /* NOTREACHED */ 2159 } 2160 } 2161 argc -= optind; 2162 argv += optind; 2163 if (argc != 1) { 2164 usermgmt_usage("groupinfo"); 2165 } 2166 grp = find_group_info(*argv); 2167 if (exists) { 2168 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE); 2169 } 2170 if (grp == NULL) { 2171 errx(EXIT_FAILURE, "can't find group `%s'", *argv); 2172 } 2173 (void) printf("name\t%s\n", grp->gr_name); 2174 (void) printf("passwd\t%s\n", grp->gr_passwd); 2175 (void) printf("gid\t%d\n", grp->gr_gid); 2176 (void) printf("members\t"); 2177 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2178 (void) printf("%s ", *cpp); 2179 } 2180 (void) fputc('\n', stdout); 2181 return EXIT_SUCCESS; 2182 } 2183 #endif 2184