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