pw_user.c revision 43780
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 28static const char rcsid[] = 29 "$Id: pw_user.c,v 1.25 1999/01/04 14:07:53 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 57static randinit; 58 59static int print_user(struct passwd * pwd, int pretty); 60static uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 61static uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 62static time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 63static time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 64static char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 65static char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 66static char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 67static char *shell_path(char const * path, char *shells[], char *sh); 68static void rmat(uid_t uid); 69static 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 102int 103pw_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 295 && strspn(a_name->val, "0123456789") == strlen(a_name->val) 296 && atoi(a_name->val) > 0) { /* Assume uid */ 297 (a_uid = a_name)->ch = 'u'; 298 a_name = NULL; 299 } 300 } 301 /* 302 * Update, delete & print require that the user exists 303 */ 304 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 305 if (a_name == NULL && pwd == NULL) /* Try harder */ 306 pwd = getpwuid(atoi(a_uid->val)); 307 308 if (pwd == NULL) { 309 if (mode == M_PRINT && getarg(args, 'F')) { 310 fakeuser.pw_name = a_name ? a_name->val : "nouser"; 311 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 312 return print_user(&fakeuser, getarg(args, 'P') != NULL); 313 } 314 if (a_name == NULL) 315 errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 316 errx(EX_NOUSER, "no such user `%s'", a_name->val); 317 } 318 if (a_name == NULL) /* May be needed later */ 319 a_name = addarg(args, 'n', newstr(pwd->pw_name)); 320 321 /* 322 * Handle deletions now 323 */ 324 if (mode == M_DELETE) { 325 char file[MAXPATHLEN]; 326 char home[MAXPATHLEN]; 327 uid_t uid = pwd->pw_uid; 328 329 if (strcmp(pwd->pw_name, "root") == 0) 330 errx(EX_DATAERR, "cannot remove user 'root'"); 331 332 /* 333 * Remove skey record from /etc/skeykeys 334 */ 335 336 rmskey(pwd->pw_name); 337 338 /* 339 * Remove crontabs 340 */ 341 sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 342 if (access(file, F_OK) == 0) { 343 sprintf(file, "crontab -u %s -r", pwd->pw_name); 344 system(file); 345 } 346 /* 347 * Save these for later, since contents of pwd may be 348 * invalidated by deletion 349 */ 350 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 351 strncpy(home, pwd->pw_dir, sizeof home); 352 home[sizeof home - 1] = '\0'; 353 354 if (!delpwent(pwd)) 355 err(EX_IOERR, "error updating passwd file"); 356 357 if (cnf->nispasswd && *cnf->nispasswd=='/' && !delnispwent(cnf->nispasswd, a_name->val)) 358 warn("WARNING: NIS passwd update"); 359 360 editgroups(a_name->val, NULL); 361 362 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 363 364 /* 365 * Remove mail file 366 */ 367 remove(file); 368 369 /* 370 * Remove at jobs 371 */ 372 if (getpwuid(uid) == NULL) 373 rmat(uid); 374 375 /* 376 * Remove home directory and contents 377 */ 378 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 379 if (stat(home, &st) != -1) { 380 rm_r(home, uid); 381 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 382 a_name->val, (long) uid, home, 383 stat(home, &st) == -1 ? "" : "not completely "); 384 } 385 } 386 return EXIT_SUCCESS; 387 } else if (mode == M_PRINT) 388 return print_user(pwd, getarg(args, 'P') != NULL); 389 390 /* 391 * The rest is edit code 392 */ 393 if ((arg = getarg(args, 'l')) != NULL) { 394 if (strcmp(pwd->pw_name, "root") == 0) 395 errx(EX_DATAERR, "can't rename `root' account"); 396 pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 397 } 398 if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) { 399 pwd->pw_uid = (uid_t) atol(arg->val); 400 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 401 errx(EX_DATAERR, "can't change uid of `root' account"); 402 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 403 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 404 } 405 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */ 406 pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid; 407 408 if ((arg = getarg(args, 'p')) != NULL) { 409 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 410 pwd->pw_change = 0; 411 else { 412 time_t now = time(NULL); 413 time_t expire = parse_date(now, arg->val); 414 415 if (now == expire) 416 errx(EX_DATAERR, "invalid password change date `%s'", arg->val); 417 pwd->pw_change = expire; 418 } 419 } 420 if ((arg = getarg(args, 'e')) != NULL) { 421 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 422 pwd->pw_expire = 0; 423 else { 424 time_t now = time(NULL); 425 time_t expire = parse_date(now, arg->val); 426 427 if (now == expire) 428 errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val); 429 pwd->pw_expire = expire; 430 } 431 } 432 if ((arg = getarg(args, 's')) != NULL) 433 pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 434 435 if (getarg(args, 'L')) 436 pwd->pw_class = cnf->default_class; 437 438 if ((arg = getarg(args, 'd')) != NULL) { 439 if (stat(pwd->pw_dir = arg->val, &st) == -1) { 440 if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 441 warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 442 } else if (!S_ISDIR(st.st_mode)) 443 warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 444 } 445 446 if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) 447 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 448 449 } else { 450 if (a_name == NULL) /* Required */ 451 errx(EX_DATAERR, "login name required"); 452 else if ((pwd = getpwnam(a_name->val)) != NULL) /* Exists */ 453 errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 454 455 /* 456 * Now, set up defaults for a new user 457 */ 458 pwd = &fakeuser; 459 pwd->pw_name = a_name->val; 460 pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 461 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 462 pwd->pw_uid = pw_uidpolicy(cnf, args); 463 pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 464 pwd->pw_change = pw_pwdpolicy(cnf, args); 465 pwd->pw_expire = pw_exppolicy(cnf, args); 466 pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 467 pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 468 469 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 470 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 471 } 472 473 /* 474 * Shared add/edit code 475 */ 476 if ((arg = getarg(args, 'c')) != NULL) 477 pwd->pw_gecos = pw_checkname((u_char *)arg->val, 1); 478 479 if ((arg = getarg(args, 'h')) != NULL) { 480 if (strcmp(arg->val, "-") == 0) 481 pwd->pw_passwd = "*"; /* No access */ 482 else { 483 int fd = atoi(arg->val); 484 int b; 485 int istty = isatty(fd); 486 struct termios t; 487 488 if (istty) { 489 if (tcgetattr(fd, &t) == -1) 490 istty = 0; 491 else { 492 struct termios n = t; 493 494 /* Disable echo */ 495 n.c_lflag &= ~(ECHO); 496 tcsetattr(fd, TCSANOW, &n); 497 printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 498 fflush(stdout); 499 } 500 } 501 b = read(fd, line, sizeof(line) - 1); 502 if (istty) { /* Restore state */ 503 tcsetattr(fd, TCSANOW, &t); 504 fputc('\n', stdout); 505 fflush(stdout); 506 } 507 if (b < 0) { 508 warn("-h file descriptor"); 509 return EX_IOERR; 510 } 511 line[b] = '\0'; 512 if ((p = strpbrk(line, " \t\r\n")) != NULL) 513 *p = '\0'; 514 if (!*line) 515 errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 516 pwd->pw_passwd = pw_pwcrypt(line); 517 } 518 } 519 520 /* 521 * Special case: -N only displays & exits 522 */ 523 if (getarg(args, 'N') != NULL) 524 return print_user(pwd, getarg(args, 'P') != NULL); 525 526 r = r1 = 1; 527 if (mode == M_ADD) { 528 r = addpwent(pwd); 529 if (r && cnf->nispasswd && *cnf->nispasswd=='/') 530 r1 = addnispwent(cnf->nispasswd, pwd); 531 } else if (mode == M_UPDATE) { 532 r = chgpwent(a_name->val, pwd); 533 if (r && cnf->nispasswd && *cnf->nispasswd=='/') 534 r1 = chgnispwent(cnf->nispasswd, a_name->val, pwd); 535 } 536 537 if (!r) { 538 warn("password update"); 539 return EX_IOERR; 540 } else if (!r1) { 541 warn("WARNING: NIS password update"); 542 /* Keep on trucking */ 543 } 544 545 /* 546 * Ok, user is created or changed - now edit group file 547 */ 548 549 if (mode == M_ADD || getarg(args, 'G') != NULL) 550 editgroups(pwd->pw_name, cnf->groups); 551 552 /* pwd may have been invalidated */ 553 if ((pwd = getpwnam(a_name->val)) == NULL) 554 errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 555 556 grp = getgrgid(pwd->pw_gid); 557 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 558 pwd->pw_name, (long) pwd->pw_uid, 559 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 560 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 561 562 /* 563 * If adding, let's touch and chown the user's mail file. This is not 564 * strictly necessary under BSD with a 0755 maildir but it also 565 * doesn't hurt anything to create the empty mailfile 566 */ 567 if (mode == M_ADD) { 568 FILE *fp; 569 570 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 571 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 572 * mtime */ 573 chown(line, pwd->pw_uid, pwd->pw_gid); 574 575 /* 576 * Send mail to the new user as well, if we are asked to 577 */ 578 if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 579 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 580 581 if (pfp == NULL) 582 warn("sendmail"); 583 else { 584 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 585 while (fgets(line, sizeof(line), fp) != NULL) { 586 /* Do substitutions? */ 587 fputs(line, pfp); 588 } 589 pclose(pfp); 590 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 591 pwd->pw_name, (long) pwd->pw_uid); 592 } 593 fclose(fp); 594 } 595 } 596 /* 597 * Finally, let's create and populate the user's home directory. Note 598 * that this also `works' for editing users if -m is used, but 599 * existing files will *not* be overwritten. 600 */ 601 if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 602 copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 603 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 604 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 605 } 606 return EXIT_SUCCESS; 607} 608 609 610static uid_t 611pw_uidpolicy(struct userconf * cnf, struct cargs * args) 612{ 613 struct passwd *pwd; 614 uid_t uid = (uid_t) - 1; 615 struct carg *a_uid = getarg(args, 'u'); 616 617 /* 618 * Check the given uid, if any 619 */ 620 if (a_uid != NULL) { 621 uid = (uid_t) atol(a_uid->val); 622 623 if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL) 624 errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 625 } else { 626 struct bitmap bm; 627 628 /* 629 * We need to allocate the next available uid under one of 630 * two policies a) Grab the first unused uid b) Grab the 631 * highest possible unused uid 632 */ 633 if (cnf->min_uid >= cnf->max_uid) { /* Sanity 634 * claus^H^H^H^Hheck */ 635 cnf->min_uid = 1000; 636 cnf->max_uid = 32000; 637 } 638 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 639 640 /* 641 * Now, let's fill the bitmap from the password file 642 */ 643 setpwent(); 644 while ((pwd = getpwent()) != NULL) 645 if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid) 646 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 647 endpwent(); 648 649 /* 650 * Then apply the policy, with fallback to reuse if necessary 651 */ 652 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 653 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 654 655 /* 656 * Another sanity check 657 */ 658 if (uid < cnf->min_uid || uid > cnf->max_uid) 659 errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 660 bm_dealloc(&bm); 661 } 662 return uid; 663} 664 665 666static uid_t 667pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 668{ 669 struct group *grp; 670 gid_t gid = (uid_t) - 1; 671 struct carg *a_gid = getarg(args, 'g'); 672 673 /* 674 * If no arg given, see if default can help out 675 */ 676 if (a_gid == NULL && cnf->default_group && *cnf->default_group) 677 a_gid = addarg(args, 'g', cnf->default_group); 678 679 /* 680 * Check the given gid, if any 681 */ 682 setgrent(); 683 if (a_gid != NULL) { 684 if ((grp = getgrnam(a_gid->val)) == NULL) { 685 gid = (gid_t) atol(a_gid->val); 686 if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL) 687 errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 688 } 689 gid = grp->gr_gid; 690 } else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) { 691 gid = grp->gr_gid; /* Already created? Use it anyway... */ 692 } else { 693 struct cargs grpargs; 694 char tmp[32]; 695 696 LIST_INIT(&grpargs); 697 addarg(&grpargs, 'n', nam); 698 699 /* 700 * We need to auto-create a group with the user's name. We 701 * can send all the appropriate output to our sister routine 702 * bit first see if we can create a group with gid==uid so we 703 * can keep the user and group ids in sync. We purposely do 704 * NOT check the gid range if we can force the sync. If the 705 * user's name dups an existing group, then the group add 706 * function will happily handle that case for us and exit. 707 */ 708 if (getgrgid(prefer) == NULL) { 709 sprintf(tmp, "%lu", (unsigned long) prefer); 710 addarg(&grpargs, 'g', tmp); 711 } 712 if (getarg(args, 'N')) 713 { 714 addarg(&grpargs, 'N', NULL); 715 addarg(&grpargs, 'q', NULL); 716 gid = pw_group(cnf, M_NEXT, &grpargs); 717 } 718 else 719 { 720 pw_group(cnf, M_ADD, &grpargs); 721 if ((grp = getgrnam(nam)) != NULL) 722 gid = grp->gr_gid; 723 } 724 a_gid = grpargs.lh_first; 725 while (a_gid != NULL) { 726 struct carg *t = a_gid->list.le_next; 727 LIST_REMOVE(a_gid, list); 728 a_gid = t; 729 } 730 } 731 endgrent(); 732 return gid; 733} 734 735 736static time_t 737pw_pwdpolicy(struct userconf * cnf, struct cargs * args) 738{ 739 time_t result = 0; 740 time_t now = time(NULL); 741 struct carg *arg = getarg(args, 'p'); 742 743 if (arg != NULL) { 744 if ((result = parse_date(now, arg->val)) == now) 745 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 746 } else if (cnf->password_days > 0) 747 result = now + ((long) cnf->password_days * 86400L); 748 return result; 749} 750 751 752static time_t 753pw_exppolicy(struct userconf * cnf, struct cargs * args) 754{ 755 time_t result = 0; 756 time_t now = time(NULL); 757 struct carg *arg = getarg(args, 'e'); 758 759 if (arg != NULL) { 760 if ((result = parse_date(now, arg->val)) == now) 761 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 762 } else if (cnf->expire_days > 0) 763 result = now + ((long) cnf->expire_days * 86400L); 764 return result; 765} 766 767 768static char * 769pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 770{ 771 struct carg *arg = getarg(args, 'd'); 772 773 if (arg) 774 return arg->val; 775 else { 776 static char home[128]; 777 778 if (cnf->home == NULL || *cnf->home == '\0') 779 errx(EX_CONFIG, "no base home directory set"); 780 sprintf(home, "%s/%s", cnf->home, user); 781 return home; 782 } 783} 784 785static char * 786shell_path(char const * path, char *shells[], char *sh) 787{ 788 if (sh != NULL && (*sh == '/' || *sh == '\0')) 789 return sh; /* specified full path or forced none */ 790 else { 791 char *p; 792 char paths[_UC_MAXLINE]; 793 794 /* 795 * We need to search paths 796 */ 797 strncpy(paths, path, sizeof paths); 798 paths[sizeof paths - 1] = '\0'; 799 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 800 int i; 801 static char shellpath[256]; 802 803 if (sh != NULL) { 804 sprintf(shellpath, "%s/%s", p, sh); 805 if (access(shellpath, X_OK) == 0) 806 return shellpath; 807 } else 808 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 809 sprintf(shellpath, "%s/%s", p, shells[i]); 810 if (access(shellpath, X_OK) == 0) 811 return shellpath; 812 } 813 } 814 if (sh == NULL) 815 errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 816 errx(EX_CONFIG, "no default shell available or defined"); 817 return NULL; 818 } 819} 820 821 822static char * 823pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 824{ 825 char *sh = newshell; 826 struct carg *arg = getarg(args, 's'); 827 828 if (newshell == NULL && arg != NULL) 829 sh = arg->val; 830 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 831} 832 833static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 834 835char * 836pw_pwcrypt(char *password) 837{ 838 int i; 839 char salt[12]; 840 841 static char buf[256]; 842 843 /* 844 * Calculate a salt value 845 */ 846 if (!randinit) { 847 randinit = 1; 848#ifdef __FreeBSD__ 849 srandomdev(); 850#else 851 srandom((unsigned long) (time(NULL) ^ getpid())); 852#endif 853 } 854 for (i = 0; i < 8; i++) 855 salt[i] = chars[random() % 63]; 856 salt[i] = '\0'; 857 858 return strcpy(buf, crypt(password, salt)); 859} 860 861#if defined(USE_MD5RAND) 862u_char * 863pw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 864{ 865 int i; 866 for (i=0;i<len;i+=16) { 867 u_char ubuf[16]; 868 869 MD5_CTX md5_ctx; 870 struct timeval tv, tvo; 871 struct rusage ru; 872 int n=0; 873 int t; 874 875 MD5Init (&md5_ctx); 876 t=getpid(); 877 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 878 t=getppid(); 879 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 880 gettimeofday (&tvo, NULL); 881 do { 882 getrusage (RUSAGE_SELF, &ru); 883 MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 884 gettimeofday (&tv, NULL); 885 MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 886 } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 887 MD5Final (ubuf, &md5_ctx); 888 memcpy(buf+i, ubuf, MIN(16, len-n)); 889 } 890 return buf; 891} 892 893#else /* Portable version */ 894 895static u_char * 896pw_getrand(u_char *buf, int len) 897{ 898 int i; 899 900 for (i = 0; i < len; i++) { 901 unsigned long val = random(); 902 /* Use all bits in the random value */ 903 buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 904 } 905 return buf; 906} 907 908#endif 909 910static char * 911pw_password(struct userconf * cnf, struct cargs * args, char const * user) 912{ 913 int i, l; 914 char pwbuf[32]; 915 u_char rndbuf[sizeof pwbuf]; 916 917 switch (cnf->default_password) { 918 case -1: /* Random password */ 919 if (!randinit) { 920 randinit = 1; 921#ifdef __FreeBSD__ 922 srandomdev(); 923#else 924 srandom((unsigned long) (time(NULL) ^ getpid())); 925#endif 926 } 927 l = (random() % 8 + 8); /* 8 - 16 chars */ 928 pw_getrand(rndbuf, l); 929 for (i = 0; i < l; i++) 930 pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)]; 931 pwbuf[i] = '\0'; 932 933 /* 934 * We give this information back to the user 935 */ 936 if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) { 937 if (isatty(1)) 938 printf("Password for '%s' is: ", user); 939 printf("%s\n", pwbuf); 940 fflush(stdout); 941 } 942 break; 943 944 case -2: /* No password at all! */ 945 return ""; 946 947 case 0: /* No login - default */ 948 default: 949 return "*"; 950 951 case 1: /* user's name */ 952 strncpy(pwbuf, user, sizeof pwbuf); 953 pwbuf[sizeof pwbuf - 1] = '\0'; 954 break; 955 } 956 return pw_pwcrypt(pwbuf); 957} 958 959 960static int 961print_user(struct passwd * pwd, int pretty) 962{ 963 if (!pretty) { 964 char buf[_UC_MAXLINE]; 965 966 fmtpwent(buf, pwd); 967 fputs(buf, stdout); 968 } else { 969 int j; 970 char *p; 971 struct group *grp = getgrgid(pwd->pw_gid); 972 char uname[60] = "User &", office[60] = "[None]", 973 wphone[60] = "[None]", hphone[60] = "[None]"; 974 char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 975 struct tm * tptr; 976 977 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 978 strncpy(uname, p, sizeof uname); 979 uname[sizeof uname - 1] = '\0'; 980 if ((p = strtok(NULL, ",")) != NULL) { 981 strncpy(office, p, sizeof office); 982 office[sizeof office - 1] = '\0'; 983 if ((p = strtok(NULL, ",")) != NULL) { 984 strncpy(wphone, p, sizeof wphone); 985 wphone[sizeof wphone - 1] = '\0'; 986 if ((p = strtok(NULL, "")) != NULL) { 987 strncpy(hphone, p, sizeof hphone); 988 hphone[sizeof hphone - 1] = '\0'; 989 } 990 } 991 } 992 } 993 /* 994 * Handle '&' in gecos field 995 */ 996 if ((p = strchr(uname, '&')) != NULL) { 997 int l = strlen(pwd->pw_name); 998 int m = strlen(p); 999 1000 memmove(p + l, p + 1, m); 1001 memmove(p, pwd->pw_name, l); 1002 *p = (char) toupper(*p); 1003 } 1004 if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 1005 strftime(acexpire, sizeof acexpire, "%c", tptr); 1006 if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL) 1007 strftime(pwexpire, sizeof pwexpire, "%c", tptr); 1008 printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 1009 " Full Name: %s\n" 1010 " Home: %-26.26s Class: %s\n" 1011 " Shell: %-26.26s Office: %s\n" 1012 "Work Phone: %-26.26s Home Phone: %s\n" 1013 "Acc Expire: %-26.26s Pwd Expire: %s\n", 1014 pwd->pw_name, (long) pwd->pw_uid, 1015 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 1016 uname, pwd->pw_dir, pwd->pw_class, 1017 pwd->pw_shell, office, wphone, hphone, 1018 acexpire, pwexpire); 1019 setgrent(); 1020 j = 0; 1021 while ((grp=getgrent()) != NULL) 1022 { 1023 int i = 0; 1024 while (grp->gr_mem[i] != NULL) 1025 { 1026 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1027 { 1028 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1029 break; 1030 } 1031 ++i; 1032 } 1033 } 1034 endgrent(); 1035 printf("%s\n", j ? "\n" : ""); 1036 } 1037 return EXIT_SUCCESS; 1038} 1039 1040char * 1041pw_checkname(u_char *name, int gecos) 1042{ 1043 int l = 0; 1044 char const *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1045 1046 while (name[l]) { 1047 if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 || 1048 (!gecos && l==0 && name[l] == '-') || /* leading '-' */ 1049 (!gecos && name[l] & 0x80)) /* 8-bit */ 1050 errx(EX_DATAERR, (name[l] >= ' ' && name[l] < 127) 1051 ? "invalid character `%c' in field" 1052 : "invalid character 0x%02x in field", 1053 name[l]); 1054 ++l; 1055 } 1056 if (!gecos && l > LOGNAMESIZE) 1057 errx(EX_DATAERR, "name too long `%s'", name); 1058 return (char *)name; 1059} 1060 1061 1062static void 1063rmat(uid_t uid) 1064{ 1065 DIR *d = opendir("/var/at/jobs"); 1066 1067 if (d != NULL) { 1068 struct dirent *e; 1069 1070 while ((e = readdir(d)) != NULL) { 1071 struct stat st; 1072 1073 if (strncmp(e->d_name, ".lock", 5) != 0 && 1074 stat(e->d_name, &st) == 0 && 1075 !S_ISDIR(st.st_mode) && 1076 st.st_uid == uid) { 1077 char tmp[MAXPATHLEN]; 1078 1079 sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 1080 system(tmp); 1081 } 1082 } 1083 closedir(d); 1084 } 1085} 1086 1087static void 1088rmskey(char const * name) 1089{ 1090 static const char etcskey[] = "/etc/skeykeys"; 1091 FILE *fp = fopen(etcskey, "r+"); 1092 1093 if (fp != NULL) { 1094 char tmp[1024]; 1095 off_t atofs = 0; 1096 int length = strlen(name); 1097 1098 while (fgets(tmp, sizeof tmp, fp) != NULL) { 1099 if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 1100 if (fseek(fp, atofs, SEEK_SET) == 0) { 1101 fwrite("#", 1, 1, fp); /* Comment username out */ 1102 } 1103 break; 1104 } 1105 atofs = ftell(fp); 1106 } 1107 /* 1108 * If we got an error of any sort, don't update! 1109 */ 1110 fclose(fp); 1111 } 1112} 1113 1114