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