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