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