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