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