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