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