1 /*- 2 * Copyright (C) 1996 3 * David L. Nugent. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #ifndef lint 29 static const char rcsid[] = 30 "$FreeBSD$"; 31 #endif /* not lint */ 32 33 #include <ctype.h> 34 #include <err.h> 35 #include <fcntl.h> 36 #include <sys/param.h> 37 #include <dirent.h> 38 #include <paths.h> 39 #include <termios.h> 40 #include <sys/types.h> 41 #include <sys/time.h> 42 #include <sys/resource.h> 43 #include <unistd.h> 44 #include <utmp.h> 45 #include <login_cap.h> 46 #if defined(USE_MD5RAND) 47 #include <md5.h> 48 #endif 49 #include "pw.h" 50 #include "bitmap.h" 51 52 #if (MAXLOGNAME-1) > UT_NAMESIZE 53 #define LOGNAMESIZE UT_NAMESIZE 54 #else 55 #define LOGNAMESIZE (MAXLOGNAME-1) 56 #endif 57 58 static int randinit; 59 static char locked_str[] = "*LOCKED*"; 60 61 static int print_user(struct passwd * pwd, int pretty, int v7); 62 static uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 63 static uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 64 static time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 65 static time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 66 static char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 67 static char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 68 static char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 69 static char *shell_path(char const * path, char *shells[], char *sh); 70 static void rmat(uid_t uid); 71 static void rmskey(char const * name); 72 73 /*- 74 * -C config configuration file 75 * -q quiet operation 76 * -n name login name 77 * -u uid user id 78 * -c comment user name/comment 79 * -d directory home directory 80 * -e date account expiry date 81 * -p date password expiry date 82 * -g grp primary group 83 * -G grp1,grp2 additional groups 84 * -m [ -k dir ] create and set up home 85 * -s shell name of login shell 86 * -o duplicate uid ok 87 * -L class user class 88 * -l name new login name 89 * -h fd password filehandle 90 * -F force print or add 91 * Setting defaults: 92 * -D set user defaults 93 * -b dir default home root dir 94 * -e period default expiry period 95 * -p period default password change period 96 * -g group default group 97 * -G grp1,grp2.. default additional groups 98 * -L class default login class 99 * -k dir default home skeleton 100 * -s shell default shell 101 * -w method default password method 102 */ 103 104 int 105 pw_user(struct userconf * cnf, int mode, struct cargs * args) 106 { 107 int rc, edited = 0; 108 char *p = NULL; 109 char *passtmp; 110 struct carg *a_name; 111 struct carg *a_uid; 112 struct carg *arg; 113 struct passwd *pwd = NULL; 114 struct group *grp; 115 struct stat st; 116 char line[_PASSWORD_LEN+1]; 117 118 static struct passwd fakeuser = 119 { 120 NULL, 121 "*", 122 -1, 123 -1, 124 0, 125 "", 126 "User &", 127 "/nonexistent", 128 "/bin/sh", 129 0 130 #if defined(__FreeBSD__) 131 ,0 132 #endif 133 }; 134 135 136 /* 137 * With M_NEXT, we only need to return the 138 * next uid to stdout 139 */ 140 if (mode == M_NEXT) 141 { 142 uid_t next = pw_uidpolicy(cnf, args); 143 if (getarg(args, 'q')) 144 return next; 145 printf("%ld:", (long)next); 146 pw_group(cnf, mode, args); 147 return EXIT_SUCCESS; 148 } 149 150 /* 151 * We can do all of the common legwork here 152 */ 153 154 if ((arg = getarg(args, 'b')) != NULL) { 155 cnf->home = arg->val; 156 } 157 158 /* 159 * If we'll need to use it or we're updating it, 160 * then create the base home directory if necessary 161 */ 162 if (arg != NULL || getarg(args, 'm') != NULL) { 163 int l = strlen(cnf->home); 164 165 if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 166 cnf->home[--l] = '\0'; 167 168 if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 169 errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 170 171 if (stat(cnf->home, &st) == -1) { 172 char dbuf[MAXPATHLEN]; 173 174 /* 175 * This is a kludge especially for Joerg :) 176 * If the home directory would be created in the root partition, then 177 * we really create it under /usr which is likely to have more space. 178 * But we create a symlink from cnf->home -> "/usr" -> cnf->home 179 */ 180 if (strchr(cnf->home+1, '/') == NULL) { 181 strcpy(dbuf, "/usr"); 182 strncat(dbuf, cnf->home, MAXPATHLEN-5); 183 if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) { 184 chown(dbuf, 0, 0); 185 symlink(dbuf, cnf->home); 186 } 187 /* If this falls, fall back to old method */ 188 } 189 p = strncpy(dbuf, cnf->home, sizeof dbuf); 190 dbuf[MAXPATHLEN-1] = '\0'; 191 if (stat(dbuf, &st) == -1) { 192 while ((p = strchr(++p, '/')) != NULL) { 193 *p = '\0'; 194 if (stat(dbuf, &st) == -1) { 195 if (mkdir(dbuf, 0755) == -1) 196 goto direrr; 197 chown(dbuf, 0, 0); 198 } else if (!S_ISDIR(st.st_mode)) 199 errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 200 *p = '/'; 201 } 202 } 203 if (stat(dbuf, &st) == -1) { 204 if (mkdir(dbuf, 0755) == -1) { 205 direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 206 } 207 chown(dbuf, 0, 0); 208 } 209 } else if (!S_ISDIR(st.st_mode)) 210 errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 211 } 212 213 if ((arg = getarg(args, 'e')) != NULL) 214 cnf->expire_days = atoi(arg->val); 215 216 if ((arg = getarg(args, 'y')) != NULL) 217 cnf->nispasswd = arg->val; 218 219 if ((arg = getarg(args, 'p')) != NULL && arg->val) 220 cnf->password_days = atoi(arg->val); 221 222 if ((arg = getarg(args, 'g')) != NULL) { 223 if (!*(p = arg->val)) /* Handle empty group list specially */ 224 cnf->default_group = ""; 225 else { 226 if ((grp = GETGRNAM(p)) == NULL) { 227 if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 228 errx(EX_NOUSER, "group `%s' does not exist", p); 229 } 230 cnf->default_group = newstr(grp->gr_name); 231 } 232 } 233 if ((arg = getarg(args, 'L')) != NULL) 234 cnf->default_class = pw_checkname((u_char *)arg->val, 0); 235 236 if ((arg = getarg(args, 'G')) != NULL && arg->val) { 237 int i = 0; 238 239 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 240 if ((grp = GETGRNAM(p)) == NULL) { 241 if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 242 errx(EX_NOUSER, "group `%s' does not exist", p); 243 } 244 if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 245 cnf->groups[i++] = newstr(grp->gr_name); 246 } 247 while (i < cnf->numgroups) 248 cnf->groups[i++] = NULL; 249 } 250 251 if ((arg = getarg(args, 'k')) != NULL) { 252 if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 253 errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 254 } 255 256 if ((arg = getarg(args, 's')) != NULL) 257 cnf->shell_default = arg->val; 258 259 if ((arg = getarg(args, 'w')) != NULL) 260 cnf->default_password = boolean_val(arg->val, cnf->default_password); 261 if (mode == M_ADD && getarg(args, 'D')) { 262 if (getarg(args, 'n') != NULL) 263 errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 264 if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 265 if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 266 cnf->min_uid = 1000; 267 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 268 cnf->max_uid = 32000; 269 } 270 if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 271 if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 272 cnf->min_gid = 1000; 273 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 274 cnf->max_gid = 32000; 275 } 276 277 arg = getarg(args, 'C'); 278 if (write_userconfig(arg ? arg->val : NULL)) 279 return EXIT_SUCCESS; 280 warn("config update"); 281 return EX_IOERR; 282 } 283 284 if (mode == M_PRINT && getarg(args, 'a')) { 285 int pretty = getarg(args, 'P') != NULL; 286 int v7 = getarg(args, '7') != NULL; 287 288 SETPWENT(); 289 while ((pwd = GETPWENT()) != NULL) 290 print_user(pwd, pretty, v7); 291 ENDPWENT(); 292 return EXIT_SUCCESS; 293 } 294 295 if ((a_name = getarg(args, 'n')) != NULL) 296 pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0)); 297 a_uid = getarg(args, 'u'); 298 299 if (a_uid == NULL) { 300 if (a_name == NULL) 301 errx(EX_DATAERR, "user name or id required"); 302 303 /* 304 * Determine whether 'n' switch is name or uid - we don't 305 * really don't really care which we have, but we need to 306 * know. 307 */ 308 if (mode != M_ADD && pwd == NULL 309 && strspn(a_name->val, "0123456789") == strlen(a_name->val) 310 && atoi(a_name->val) > 0) { /* Assume uid */ 311 (a_uid = a_name)->ch = 'u'; 312 a_name = NULL; 313 } 314 } 315 316 /* 317 * Update, delete & print require that the user exists 318 */ 319 if (mode == M_UPDATE || mode == M_DELETE || 320 mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 321 322 if (a_name == NULL && pwd == NULL) /* Try harder */ 323 pwd = GETPWUID(atoi(a_uid->val)); 324 325 if (pwd == NULL) { 326 if (mode == M_PRINT && getarg(args, 'F')) { 327 fakeuser.pw_name = a_name ? a_name->val : "nouser"; 328 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 329 return print_user(&fakeuser, 330 getarg(args, 'P') != NULL, 331 getarg(args, '7') != NULL); 332 } 333 if (a_name == NULL) 334 errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 335 errx(EX_NOUSER, "no such user `%s'", a_name->val); 336 } 337 338 if (a_name == NULL) /* May be needed later */ 339 a_name = addarg(args, 'n', newstr(pwd->pw_name)); 340 341 /* 342 * The M_LOCK and M_UNLOCK functions simply add or remove 343 * a "*LOCKED*" prefix from in front of the password to 344 * prevent it decoding correctly, and therefore prevents 345 * access. Of course, this only prevents access via 346 * password authentication (not ssh, kerberos or any 347 * other method that does not use the UNIX password) but 348 * that is a known limitation. 349 */ 350 351 if (mode == M_LOCK) { 352 if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 353 errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 354 passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str)); 355 if (passtmp == NULL) /* disaster */ 356 errx(EX_UNAVAILABLE, "out of memory"); 357 strcpy(passtmp, locked_str); 358 strcat(passtmp, pwd->pw_passwd); 359 pwd->pw_passwd = passtmp; 360 edited = 1; 361 } else if (mode == M_UNLOCK) { 362 if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 363 errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 364 pwd->pw_passwd += sizeof(locked_str)-1; 365 edited = 1; 366 } else if (mode == M_DELETE) { 367 /* 368 * Handle deletions now 369 */ 370 char file[MAXPATHLEN]; 371 char home[MAXPATHLEN]; 372 uid_t uid = pwd->pw_uid; 373 374 if (strcmp(pwd->pw_name, "root") == 0) 375 errx(EX_DATAERR, "cannot remove user 'root'"); 376 377 if (!PWALTDIR()) { 378 /* 379 * Remove skey record from /etc/skeykeys 380 */ 381 382 rmskey(pwd->pw_name); 383 384 /* 385 * Remove crontabs 386 */ 387 sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 388 if (access(file, F_OK) == 0) { 389 sprintf(file, "crontab -u %s -r", pwd->pw_name); 390 system(file); 391 } 392 } 393 /* 394 * Save these for later, since contents of pwd may be 395 * invalidated by deletion 396 */ 397 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 398 strncpy(home, pwd->pw_dir, sizeof home); 399 home[sizeof home - 1] = '\0'; 400 401 rc = delpwent(pwd); 402 if (rc == -1) 403 err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 404 else if (rc != 0) { 405 warn("passwd update"); 406 return EX_IOERR; 407 } 408 409 if (cnf->nispasswd && *cnf->nispasswd=='/') { 410 rc = delnispwent(cnf->nispasswd, a_name->val); 411 if (rc == -1) 412 warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 413 else if (rc != 0) 414 warn("WARNING: NIS passwd update"); 415 /* non-fatal */ 416 } 417 418 editgroups(a_name->val, NULL); 419 420 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 421 422 if (!PWALTDIR()) { 423 /* 424 * Remove mail file 425 */ 426 remove(file); 427 428 /* 429 * Remove at jobs 430 */ 431 if (getpwuid(uid) == NULL) 432 rmat(uid); 433 434 /* 435 * Remove home directory and contents 436 */ 437 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 438 if (stat(home, &st) != -1) { 439 rm_r(home, uid); 440 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 441 a_name->val, (long) uid, home, 442 stat(home, &st) == -1 ? "" : "not completely "); 443 } 444 } 445 } 446 return EXIT_SUCCESS; 447 } else if (mode == M_PRINT) 448 return print_user(pwd, 449 getarg(args, 'P') != NULL, 450 getarg(args, '7') != NULL); 451 452 /* 453 * The rest is edit code 454 */ 455 if ((arg = getarg(args, 'l')) != NULL) { 456 if (strcmp(pwd->pw_name, "root") == 0) 457 errx(EX_DATAERR, "can't rename `root' account"); 458 pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 459 edited = 1; 460 } 461 462 if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { 463 pwd->pw_uid = (uid_t) atol(arg->val); 464 edited = 1; 465 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 466 errx(EX_DATAERR, "can't change uid of `root' account"); 467 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 468 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 469 } 470 471 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 472 gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 473 if (newgid != pwd->pw_gid) { 474 edited = 1; 475 pwd->pw_gid = newgid; 476 } 477 } 478 479 if ((arg = getarg(args, 'p')) != NULL) { 480 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 481 if (pwd->pw_change != 0) { 482 pwd->pw_change = 0; 483 edited = 1; 484 } 485 } 486 else { 487 time_t now = time(NULL); 488 time_t expire = parse_date(now, arg->val); 489 490 if (now == expire) 491 errx(EX_DATAERR, "invalid password change date `%s'", arg->val); 492 if (pwd->pw_change != expire) { 493 pwd->pw_change = expire; 494 edited = 1; 495 } 496 } 497 } 498 499 if ((arg = getarg(args, 'e')) != NULL) { 500 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 501 if (pwd->pw_expire != 0) { 502 pwd->pw_expire = 0; 503 edited = 1; 504 } 505 } 506 else { 507 time_t now = time(NULL); 508 time_t expire = parse_date(now, arg->val); 509 510 if (now == expire) 511 errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val); 512 if (pwd->pw_expire != expire) { 513 pwd->pw_expire = expire; 514 edited = 1; 515 } 516 } 517 } 518 519 if ((arg = getarg(args, 's')) != NULL) { 520 char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 521 if (shell == NULL) 522 shell = ""; 523 if (strcmp(shell, pwd->pw_shell) != 0) { 524 pwd->pw_shell = shell; 525 edited = 1; 526 } 527 } 528 529 if (getarg(args, 'L')) { 530 if (cnf->default_class == NULL) 531 cnf->default_class = ""; 532 if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 533 pwd->pw_class = cnf->default_class; 534 edited = 1; 535 } 536 } 537 538 if ((arg = getarg(args, 'd')) != NULL) { 539 edited = strcmp(pwd->pw_dir, arg->val) != 0; 540 if (stat(pwd->pw_dir = arg->val, &st) == -1) { 541 if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 542 warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 543 } else if (!S_ISDIR(st.st_mode)) 544 warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 545 } 546 547 if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) { 548 login_cap_t *lc; 549 550 lc = login_getpwclass(pwd); 551 if (lc == NULL || 552 login_setcryptfmt(lc, "md5", NULL) == NULL) 553 warn("setting crypt(3) format"); 554 login_close(lc); 555 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 556 edited = 1; 557 } 558 559 } else { 560 login_cap_t *lc; 561 562 /* 563 * Add code 564 */ 565 566 if (a_name == NULL) /* Required */ 567 errx(EX_DATAERR, "login name required"); 568 else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ 569 errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 570 571 /* 572 * Now, set up defaults for a new user 573 */ 574 pwd = &fakeuser; 575 pwd->pw_name = a_name->val; 576 pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 577 pwd->pw_uid = pw_uidpolicy(cnf, args); 578 pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 579 pwd->pw_change = pw_pwdpolicy(cnf, args); 580 pwd->pw_expire = pw_exppolicy(cnf, args); 581 pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 582 pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 583 lc = login_getpwclass(pwd); 584 if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL) 585 warn("setting crypt(3) format"); 586 login_close(lc); 587 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 588 edited = 1; 589 590 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 591 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 592 } 593 594 /* 595 * Shared add/edit code 596 */ 597 if ((arg = getarg(args, 'c')) != NULL) { 598 char *gecos = pw_checkname((u_char *)arg->val, 1); 599 if (strcmp(pwd->pw_gecos, gecos) != 0) { 600 pwd->pw_gecos = gecos; 601 edited = 1; 602 } 603 } 604 605 if ((arg = getarg(args, 'h')) != NULL) { 606 if (strcmp(arg->val, "-") == 0) { 607 if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 608 pwd->pw_passwd = "*"; /* No access */ 609 edited = 1; 610 } 611 } else { 612 int fd = atoi(arg->val); 613 int b; 614 int istty = isatty(fd); 615 struct termios t; 616 login_cap_t *lc; 617 618 if (istty) { 619 if (tcgetattr(fd, &t) == -1) 620 istty = 0; 621 else { 622 struct termios n = t; 623 624 /* Disable echo */ 625 n.c_lflag &= ~(ECHO); 626 tcsetattr(fd, TCSANOW, &n); 627 printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 628 fflush(stdout); 629 } 630 } 631 b = read(fd, line, sizeof(line) - 1); 632 if (istty) { /* Restore state */ 633 tcsetattr(fd, TCSANOW, &t); 634 fputc('\n', stdout); 635 fflush(stdout); 636 } 637 if (b < 0) { 638 warn("-h file descriptor"); 639 return EX_IOERR; 640 } 641 line[b] = '\0'; 642 if ((p = strpbrk(line, " \t\r\n")) != NULL) 643 *p = '\0'; 644 if (!*line) 645 errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 646 lc = login_getpwclass(pwd); 647 if (lc == NULL || 648 login_setcryptfmt(lc, "md5", NULL) == NULL) 649 warn("setting crypt(3) format"); 650 login_close(lc); 651 pwd->pw_passwd = pw_pwcrypt(line); 652 edited = 1; 653 } 654 } 655 656 /* 657 * Special case: -N only displays & exits 658 */ 659 if (getarg(args, 'N') != NULL) 660 return print_user(pwd, 661 getarg(args, 'P') != NULL, 662 getarg(args, '7') != NULL); 663 664 if (mode == M_ADD) { 665 edited = 1; /* Always */ 666 rc = addpwent(pwd); 667 if (rc == -1) { 668 warnx("user '%s' already exists", pwd->pw_name); 669 return EX_IOERR; 670 } else if (rc != 0) { 671 warn("passwd file update"); 672 return EX_IOERR; 673 } 674 if (cnf->nispasswd && *cnf->nispasswd=='/') { 675 rc = addnispwent(cnf->nispasswd, pwd); 676 if (rc == -1) 677 warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 678 else 679 warn("NIS passwd update"); 680 /* NOTE: we treat NIS-only update errors as non-fatal */ 681 } 682 } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 683 if (edited) { /* Only updated this if required */ 684 rc = chgpwent(a_name->val, pwd); 685 if (rc == -1) { 686 warnx("user '%s' does not exist (NIS?)", pwd->pw_name); 687 return EX_IOERR; 688 } else if (rc != 0) { 689 warn("passwd file update"); 690 return EX_IOERR; 691 } 692 if ( cnf->nispasswd && *cnf->nispasswd=='/') { 693 rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); 694 if (rc == -1) 695 warn("User '%s' not found in NIS passwd", pwd->pw_name); 696 else 697 warn("NIS passwd update"); 698 /* NOTE: NIS-only update errors are not fatal */ 699 } 700 } 701 } 702 703 /* 704 * Ok, user is created or changed - now edit group file 705 */ 706 707 if (mode == M_ADD || getarg(args, 'G') != NULL) 708 editgroups(pwd->pw_name, cnf->groups); 709 710 /* go get a current version of pwd */ 711 pwd = GETPWNAM(a_name->val); 712 if (pwd == NULL) { 713 /* This will fail when we rename, so special case that */ 714 if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { 715 a_name->val = arg->val; /* update new name */ 716 pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ 717 } 718 } 719 if (pwd == NULL) /* can't go on without this */ 720 errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 721 722 grp = GETGRGID(pwd->pw_gid); 723 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 724 pwd->pw_name, (long) pwd->pw_uid, 725 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 726 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 727 728 /* 729 * If adding, let's touch and chown the user's mail file. This is not 730 * strictly necessary under BSD with a 0755 maildir but it also 731 * doesn't hurt anything to create the empty mailfile 732 */ 733 if (mode == M_ADD) { 734 FILE *fp; 735 736 if (!PWALTDIR()) { 737 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 738 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 739 * mtime */ 740 chown(line, pwd->pw_uid, pwd->pw_gid); 741 742 /* 743 * Send mail to the new user as well, if we are asked to 744 */ 745 if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 746 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 747 748 if (pfp == NULL) 749 warn("sendmail"); 750 else { 751 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 752 while (fgets(line, sizeof(line), fp) != NULL) { 753 /* Do substitutions? */ 754 fputs(line, pfp); 755 } 756 pclose(pfp); 757 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 758 pwd->pw_name, (long) pwd->pw_uid); 759 } 760 fclose(fp); 761 } 762 } 763 } 764 765 /* 766 * Finally, let's create and populate the user's home directory. Note 767 * that this also `works' for editing users if -m is used, but 768 * existing files will *not* be overwritten. 769 */ 770 if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 771 copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 772 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 773 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 774 } 775 776 return EXIT_SUCCESS; 777 } 778 779 780 static uid_t 781 pw_uidpolicy(struct userconf * cnf, struct cargs * args) 782 { 783 struct passwd *pwd; 784 uid_t uid = (uid_t) - 1; 785 struct carg *a_uid = getarg(args, 'u'); 786 787 /* 788 * Check the given uid, if any 789 */ 790 if (a_uid != NULL) { 791 uid = (uid_t) atol(a_uid->val); 792 793 if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL) 794 errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 795 } else { 796 struct bitmap bm; 797 798 /* 799 * We need to allocate the next available uid under one of 800 * two policies a) Grab the first unused uid b) Grab the 801 * highest possible unused uid 802 */ 803 if (cnf->min_uid >= cnf->max_uid) { /* Sanity 804 * claus^H^H^H^Hheck */ 805 cnf->min_uid = 1000; 806 cnf->max_uid = 32000; 807 } 808 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 809 810 /* 811 * Now, let's fill the bitmap from the password file 812 */ 813 SETPWENT(); 814 while ((pwd = GETPWENT()) != NULL) 815 if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 816 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 817 ENDPWENT(); 818 819 /* 820 * Then apply the policy, with fallback to reuse if necessary 821 */ 822 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 823 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 824 825 /* 826 * Another sanity check 827 */ 828 if (uid < cnf->min_uid || uid > cnf->max_uid) 829 errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 830 bm_dealloc(&bm); 831 } 832 return uid; 833 } 834 835 836 static uid_t 837 pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 838 { 839 struct group *grp; 840 gid_t gid = (uid_t) - 1; 841 struct carg *a_gid = getarg(args, 'g'); 842 843 /* 844 * If no arg given, see if default can help out 845 */ 846 if (a_gid == NULL && cnf->default_group && *cnf->default_group) 847 a_gid = addarg(args, 'g', cnf->default_group); 848 849 /* 850 * Check the given gid, if any 851 */ 852 SETGRENT(); 853 if (a_gid != NULL) { 854 if ((grp = GETGRNAM(a_gid->val)) == NULL) { 855 gid = (gid_t) atol(a_gid->val); 856 if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 857 errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 858 } 859 gid = grp->gr_gid; 860 } else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) { 861 gid = grp->gr_gid; /* Already created? Use it anyway... */ 862 } else { 863 struct cargs grpargs; 864 char tmp[32]; 865 866 LIST_INIT(&grpargs); 867 addarg(&grpargs, 'n', nam); 868 869 /* 870 * We need to auto-create a group with the user's name. We 871 * can send all the appropriate output to our sister routine 872 * bit first see if we can create a group with gid==uid so we 873 * can keep the user and group ids in sync. We purposely do 874 * NOT check the gid range if we can force the sync. If the 875 * user's name dups an existing group, then the group add 876 * function will happily handle that case for us and exit. 877 */ 878 if (GETGRGID(prefer) == NULL) { 879 sprintf(tmp, "%lu", (unsigned long) prefer); 880 addarg(&grpargs, 'g', tmp); 881 } 882 if (getarg(args, 'N')) 883 { 884 addarg(&grpargs, 'N', NULL); 885 addarg(&grpargs, 'q', NULL); 886 gid = pw_group(cnf, M_NEXT, &grpargs); 887 } 888 else 889 { 890 pw_group(cnf, M_ADD, &grpargs); 891 if ((grp = GETGRNAM(nam)) != NULL) 892 gid = grp->gr_gid; 893 } 894 a_gid = grpargs.lh_first; 895 while (a_gid != NULL) { 896 struct carg *t = a_gid->list.le_next; 897 LIST_REMOVE(a_gid, list); 898 a_gid = t; 899 } 900 } 901 ENDGRENT(); 902 return gid; 903 } 904 905 906 static time_t 907 pw_pwdpolicy(struct userconf * cnf, struct cargs * args) 908 { 909 time_t result = 0; 910 time_t now = time(NULL); 911 struct carg *arg = getarg(args, 'p'); 912 913 if (arg != NULL) { 914 if ((result = parse_date(now, arg->val)) == now) 915 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 916 } else if (cnf->password_days > 0) 917 result = now + ((long) cnf->password_days * 86400L); 918 return result; 919 } 920 921 922 static time_t 923 pw_exppolicy(struct userconf * cnf, struct cargs * args) 924 { 925 time_t result = 0; 926 time_t now = time(NULL); 927 struct carg *arg = getarg(args, 'e'); 928 929 if (arg != NULL) { 930 if ((result = parse_date(now, arg->val)) == now) 931 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 932 } else if (cnf->expire_days > 0) 933 result = now + ((long) cnf->expire_days * 86400L); 934 return result; 935 } 936 937 938 static char * 939 pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 940 { 941 struct carg *arg = getarg(args, 'd'); 942 943 if (arg) 944 return arg->val; 945 else { 946 static char home[128]; 947 948 if (cnf->home == NULL || *cnf->home == '\0') 949 errx(EX_CONFIG, "no base home directory set"); 950 sprintf(home, "%s/%s", cnf->home, user); 951 return home; 952 } 953 } 954 955 static char * 956 shell_path(char const * path, char *shells[], char *sh) 957 { 958 if (sh != NULL && (*sh == '/' || *sh == '\0')) 959 return sh; /* specified full path or forced none */ 960 else { 961 char *p; 962 char paths[_UC_MAXLINE]; 963 964 /* 965 * We need to search paths 966 */ 967 strncpy(paths, path, sizeof paths); 968 paths[sizeof paths - 1] = '\0'; 969 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 970 int i; 971 static char shellpath[256]; 972 973 if (sh != NULL) { 974 sprintf(shellpath, "%s/%s", p, sh); 975 if (access(shellpath, X_OK) == 0) 976 return shellpath; 977 } else 978 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 979 sprintf(shellpath, "%s/%s", p, shells[i]); 980 if (access(shellpath, X_OK) == 0) 981 return shellpath; 982 } 983 } 984 if (sh == NULL) 985 errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 986 errx(EX_CONFIG, "no default shell available or defined"); 987 return NULL; 988 } 989 } 990 991 992 static char * 993 pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 994 { 995 char *sh = newshell; 996 struct carg *arg = getarg(args, 's'); 997 998 if (newshell == NULL && arg != NULL) 999 sh = arg->val; 1000 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 1001 } 1002 1003 static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 1004 1005 char * 1006 pw_pwcrypt(char *password) 1007 { 1008 int i; 1009 char salt[12]; 1010 1011 static char buf[256]; 1012 1013 /* 1014 * Calculate a salt value 1015 */ 1016 if (!randinit) { 1017 randinit = 1; 1018 #ifdef __FreeBSD__ 1019 srandomdev(); 1020 #else 1021 srandom((unsigned long) (time(NULL) ^ getpid())); 1022 #endif 1023 } 1024 for (i = 0; i < 8; i++) 1025 salt[i] = chars[random() % 63]; 1026 salt[i] = '\0'; 1027 1028 return strcpy(buf, crypt(password, salt)); 1029 } 1030 1031 #if defined(USE_MD5RAND) 1032 u_char * 1033 pw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 1034 { 1035 int i; 1036 for (i=0;i<len;i+=16) { 1037 u_char ubuf[16]; 1038 1039 MD5_CTX md5_ctx; 1040 struct timeval tv, tvo; 1041 struct rusage ru; 1042 int n=0; 1043 int t; 1044 1045 MD5Init (&md5_ctx); 1046 t=getpid(); 1047 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 1048 t=getppid(); 1049 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 1050 gettimeofday (&tvo, NULL); 1051 do { 1052 getrusage (RUSAGE_SELF, &ru); 1053 MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 1054 gettimeofday (&tv, NULL); 1055 MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 1056 } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 1057 MD5Final (ubuf, &md5_ctx); 1058 memcpy(buf+i, ubuf, MIN(16, len-n)); 1059 } 1060 return buf; 1061 } 1062 1063 #else /* Portable version */ 1064 1065 static u_char * 1066 pw_getrand(u_char *buf, int len) 1067 { 1068 int i; 1069 1070 for (i = 0; i < len; i++) { 1071 unsigned long val = random(); 1072 /* Use all bits in the random value */ 1073 buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 1074 } 1075 return buf; 1076 } 1077 1078 #endif 1079 1080 static char * 1081 pw_password(struct userconf * cnf, struct cargs * args, char const * user) 1082 { 1083 int i, l; 1084 char pwbuf[32]; 1085 u_char rndbuf[sizeof pwbuf]; 1086 1087 switch (cnf->default_password) { 1088 case -1: /* Random password */ 1089 if (!randinit) { 1090 randinit = 1; 1091 #ifdef __FreeBSD__ 1092 srandomdev(); 1093 #else 1094 srandom((unsigned long) (time(NULL) ^ getpid())); 1095 #endif 1096 } 1097 l = (random() % 8 + 8); /* 8 - 16 chars */ 1098 pw_getrand(rndbuf, l); 1099 for (i = 0; i < l; i++) 1100 pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)]; 1101 pwbuf[i] = '\0'; 1102 1103 /* 1104 * We give this information back to the user 1105 */ 1106 if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) { 1107 if (isatty(STDOUT_FILENO)) 1108 printf("Password for '%s' is: ", user); 1109 printf("%s\n", pwbuf); 1110 fflush(stdout); 1111 } 1112 break; 1113 1114 case -2: /* No password at all! */ 1115 return ""; 1116 1117 case 0: /* No login - default */ 1118 default: 1119 return "*"; 1120 1121 case 1: /* user's name */ 1122 strncpy(pwbuf, user, sizeof pwbuf); 1123 pwbuf[sizeof pwbuf - 1] = '\0'; 1124 break; 1125 } 1126 return pw_pwcrypt(pwbuf); 1127 } 1128 1129 1130 static int 1131 print_user(struct passwd * pwd, int pretty, int v7) 1132 { 1133 if (!pretty) { 1134 char buf[_UC_MAXLINE]; 1135 1136 fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD); 1137 fputs(buf, stdout); 1138 } else { 1139 int j; 1140 char *p; 1141 struct group *grp = GETGRGID(pwd->pw_gid); 1142 char uname[60] = "User &", office[60] = "[None]", 1143 wphone[60] = "[None]", hphone[60] = "[None]"; 1144 char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 1145 struct tm * tptr; 1146 1147 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1148 strncpy(uname, p, sizeof uname); 1149 uname[sizeof uname - 1] = '\0'; 1150 if ((p = strtok(NULL, ",")) != NULL) { 1151 strncpy(office, p, sizeof office); 1152 office[sizeof office - 1] = '\0'; 1153 if ((p = strtok(NULL, ",")) != NULL) { 1154 strncpy(wphone, p, sizeof wphone); 1155 wphone[sizeof wphone - 1] = '\0'; 1156 if ((p = strtok(NULL, "")) != NULL) { 1157 strncpy(hphone, p, sizeof hphone); 1158 hphone[sizeof hphone - 1] = '\0'; 1159 } 1160 } 1161 } 1162 } 1163 /* 1164 * Handle '&' in gecos field 1165 */ 1166 if ((p = strchr(uname, '&')) != NULL) { 1167 int l = strlen(pwd->pw_name); 1168 int m = strlen(p); 1169 1170 memmove(p + l, p + 1, m); 1171 memmove(p, pwd->pw_name, l); 1172 *p = (char) toupper((unsigned char)*p); 1173 } 1174 if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 1175 strftime(acexpire, sizeof acexpire, "%a %Ef %Y %X", tptr); 1176 if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 1177 strftime(pwexpire, sizeof pwexpire, "%a %Ef %Y %X", tptr); 1178 printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 1179 " Full Name: %s\n" 1180 " Home: %-26.26s Class: %s\n" 1181 " Shell: %-26.26s Office: %s\n" 1182 "Work Phone: %-26.26s Home Phone: %s\n" 1183 "Acc Expire: %-26.26s Pwd Expire: %s\n", 1184 pwd->pw_name, (long) pwd->pw_uid, 1185 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 1186 uname, pwd->pw_dir, pwd->pw_class, 1187 pwd->pw_shell, office, wphone, hphone, 1188 acexpire, pwexpire); 1189 SETGRENT(); 1190 j = 0; 1191 while ((grp=GETGRENT()) != NULL) 1192 { 1193 int i = 0; 1194 while (grp->gr_mem[i] != NULL) 1195 { 1196 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1197 { 1198 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1199 break; 1200 } 1201 ++i; 1202 } 1203 } 1204 ENDGRENT(); 1205 printf("%s", j ? "\n" : ""); 1206 } 1207 return EXIT_SUCCESS; 1208 } 1209 1210 char * 1211 pw_checkname(u_char *name, int gecos) 1212 { 1213 int l = 0; 1214 char const *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1215 1216 while (name[l]) { 1217 if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 || 1218 (!gecos && l==0 && name[l] == '-') || /* leading '-' */ 1219 (!gecos && name[l] & 0x80)) /* 8-bit */ 1220 errx(EX_DATAERR, (name[l] >= ' ' && name[l] < 127) 1221 ? "invalid character `%c' in field" 1222 : "invalid character 0x%02x in field", 1223 name[l]); 1224 ++l; 1225 } 1226 if (!gecos && l > LOGNAMESIZE) 1227 errx(EX_DATAERR, "name too long `%s'", name); 1228 return (char *)name; 1229 } 1230 1231 1232 static void 1233 rmat(uid_t uid) 1234 { 1235 DIR *d = opendir("/var/at/jobs"); 1236 1237 if (d != NULL) { 1238 struct dirent *e; 1239 1240 while ((e = readdir(d)) != NULL) { 1241 struct stat st; 1242 1243 if (strncmp(e->d_name, ".lock", 5) != 0 && 1244 stat(e->d_name, &st) == 0 && 1245 !S_ISDIR(st.st_mode) && 1246 st.st_uid == uid) { 1247 char tmp[MAXPATHLEN]; 1248 1249 sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 1250 system(tmp); 1251 } 1252 } 1253 closedir(d); 1254 } 1255 } 1256 1257 static void 1258 rmskey(char const * name) 1259 { 1260 static const char etcskey[] = "/etc/skeykeys"; 1261 FILE *fp = fopen(etcskey, "r+"); 1262 1263 if (fp != NULL) { 1264 char tmp[1024]; 1265 off_t atofs = 0; 1266 int length = strlen(name); 1267 1268 while (fgets(tmp, sizeof tmp, fp) != NULL) { 1269 if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 1270 if (fseek(fp, atofs, SEEK_SET) == 0) { 1271 fwrite("#", 1, 1, fp); /* Comment username out */ 1272 } 1273 break; 1274 } 1275 atofs = ftell(fp); 1276 } 1277 /* 1278 * If we got an error of any sort, don't update! 1279 */ 1280 fclose(fp); 1281 } 1282 } 1283 1284