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