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