pw_user.c revision 21052
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 * $Id: pw_user.c,v 1.9 1996/12/23 02:27:29 davidn Exp $ 27 */ 28 29#include <unistd.h> 30#include <fcntl.h> 31#include <ctype.h> 32#include <paths.h> 33#include <sys/param.h> 34#include <dirent.h> 35#include <termios.h> 36#include <sys/types.h> 37#include <sys/time.h> 38#include <sys/resource.h> 39#include <md5.h> 40#include "pw.h" 41#include "bitmap.h" 42#include "pwupd.h" 43 44static int print_user(struct passwd * pwd, int pretty); 45static uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 46static uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 47static time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 48static time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 49static char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 50static char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 51static char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 52static char *shell_path(char const * path, char *shells[], char *sh); 53static void rmat(uid_t uid); 54static void rmskey(char const * name); 55 56/*- 57 * -C config configuration file 58 * -q quiet operation 59 * -n name login name 60 * -u uid user id 61 * -c comment user name/comment 62 * -d directory home directory 63 * -e date account expiry date 64 * -p date password expiry date 65 * -g grp primary group 66 * -G grp1,grp2 additional groups 67 * -m [ -k dir ] create and set up home 68 * -s shell name of login shell 69 * -o duplicate uid ok 70 * -L class user class 71 * -l name new login name 72 * -h fd password filehandle 73 * -F force print or add 74 * Setting defaults: 75 * -D set user defaults 76 * -b dir default home root dir 77 * -e period default expiry period 78 * -p period default password change period 79 * -g group default group 80 * -G grp1,grp2.. default additional groups 81 * -L class default login class 82 * -k dir default home skeleton 83 * -s shell default shell 84 * -w method default password method 85 */ 86 87int 88pw_user(struct userconf * cnf, int mode, struct cargs * args) 89{ 90 char *p = NULL; 91 struct carg *a_name; 92 struct carg *a_uid; 93 struct carg *arg; 94 struct passwd *pwd = NULL; 95 struct group *grp; 96 struct stat st; 97 char line[_PASSWORD_LEN+1]; 98 99 static struct passwd fakeuser = 100 { 101 NULL, 102 "*", 103 -1, 104 -1, 105 0, 106 "", 107 "User &", 108 "/bin/sh", 109 0, 110 0 111 }; 112 113 /* 114 * With M_NEXT, we only need to return the 115 * next uid to stdout 116 */ 117 if (mode == M_NEXT) 118 { 119 uid_t next = pw_uidpolicy(cnf, args); 120 if (getarg(args, 'q')) 121 return next; 122 printf("%ld:", (long)next); 123 pw_group(cnf, mode, args); 124 return EXIT_SUCCESS; 125 } 126 127 /* 128 * We can do all of the common legwork here 129 */ 130 131 if ((arg = getarg(args, 'b')) != NULL) { 132 cnf->home = arg->val; 133 } 134 135 /* 136 * If we'll need to use it or we're updating it, 137 * then create the base home directory if necessary 138 */ 139 if (arg != NULL || getarg(args, 'm') != NULL) { 140 int l = strlen(cnf->home); 141 142 if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 143 cnf->home[--l] = '\0'; 144 145 if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 146 cmderr(EX_DATAERR, "invalid base directory for home '%s'\n", cnf->home); 147 148 if (stat(cnf->home, &st) == -1) { 149 char dbuf[MAXPATHLEN]; 150 151 p = strncpy(dbuf, cnf->home, sizeof dbuf); 152 dbuf[MAXPATHLEN-1] = '\0'; 153 while ((p = strchr(++p, '/')) != NULL) { 154 *p = '\0'; 155 if (stat(dbuf, &st) == -1) { 156 if (mkdir(dbuf, 0755) == -1) 157 goto direrr; 158 chown(dbuf, 0, 0); 159 } else if (!S_ISDIR(st.st_mode)) 160 cmderr(EX_OSFILE, "'%s' (root home parent) is not a directory\n", dbuf); 161 *p = '/'; 162 } 163 if (stat(dbuf, &st) == -1) { /* Should not be strictly necessary */ 164 if (mkdir(dbuf, 0755) == -1) { 165 direrr: cmderr(EX_OSFILE, "mkdir '%s': %s\n", dbuf, strerror(errno)); 166 } 167 chown(dbuf, 0, 0); 168 } 169 } else if (!S_ISDIR(st.st_mode)) 170 cmderr(EX_OSFILE, "root home `%s' is not a directory\n", cnf->home); 171 } 172 173 174 if ((arg = getarg(args, 'e')) != NULL) 175 cnf->expire_days = atoi(arg->val); 176 177 if ((arg = getarg(args, 'p')) != NULL && arg->val) 178 cnf->password_days = atoi(arg->val); 179 180 if ((arg = getarg(args, 'g')) != NULL) { 181 p = arg->val; 182 if ((grp = getgrnam(p)) == NULL) { 183 if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 184 cmderr(EX_NOUSER, "group `%s' does not exist\n", p); 185 } 186 cnf->default_group = newstr(grp->gr_name); 187 } 188 if ((arg = getarg(args, 'L')) != NULL) 189 cnf->default_class = pw_checkname((u_char *)arg->val, 0); 190 191 if ((arg = getarg(args, 'G')) != NULL && arg->val) { 192 int i = 0; 193 194 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 195 if ((grp = getgrnam(p)) == NULL) { 196 if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 197 cmderr(EX_NOUSER, "group `%s' does not exist\n", p); 198 } 199 if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 200 cnf->groups[i++] = newstr(grp->gr_name); 201 } 202 while (i < cnf->numgroups) 203 cnf->groups[i++] = NULL; 204 } 205 if ((arg = getarg(args, 'k')) != NULL) { 206 if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode)) 207 cmderr(EX_OSFILE, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir); 208 } 209 if ((arg = getarg(args, 's')) != NULL) 210 cnf->shell_default = arg->val; 211 212 if (mode == M_ADD && getarg(args, 'D')) { 213 if (getarg(args, 'n') != NULL) 214 cmderr(EX_DATAERR, "can't combine `-D' with `-n name'\n"); 215 if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 216 if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 217 cnf->min_uid = 1000; 218 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 219 cnf->max_uid = 32000; 220 } 221 if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 222 if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 223 cnf->min_gid = 1000; 224 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 225 cnf->max_gid = 32000; 226 } 227 if ((arg = getarg(args, 'w')) != NULL) 228 cnf->default_password = boolean_val(arg->val, cnf->default_password); 229 230 arg = getarg(args, 'C'); 231 if (write_userconfig(arg ? arg->val : NULL)) 232 return EXIT_SUCCESS; 233 perror("config update"); 234 return EX_IOERR; 235 } 236 if (mode == M_PRINT && getarg(args, 'a')) { 237 int pretty = getarg(args, 'P') != NULL; 238 239 setpwent(); 240 while ((pwd = getpwent()) != NULL) 241 print_user(pwd, pretty); 242 endpwent(); 243 return EXIT_SUCCESS; 244 } 245 if ((a_name = getarg(args, 'n')) != NULL) 246 pwd = getpwnam(pw_checkname((u_char *)a_name->val, 0)); 247 a_uid = getarg(args, 'u'); 248 249 if (a_uid == NULL) { 250 if (a_name == NULL) 251 cmderr(EX_DATAERR, "user name or id required\n"); 252 253 /* 254 * Determine whether 'n' switch is name or uid - we don't 255 * really don't really care which we have, but we need to 256 * know. 257 */ 258 if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) { /* Assume uid */ 259 (a_uid = a_name)->ch = 'u'; 260 a_name = NULL; 261 } 262 } 263 /* 264 * Update, delete & print require that the user exists 265 */ 266 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 267 if (a_name == NULL && pwd == NULL) /* Try harder */ 268 pwd = getpwuid(atoi(a_uid->val)); 269 270 if (pwd == NULL) { 271 if (mode == M_PRINT && getarg(args, 'F')) { 272 fakeuser.pw_name = a_name ? a_name->val : "nouser"; 273 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 274 return print_user(&fakeuser, getarg(args, 'P') != NULL); 275 } 276 if (a_name == NULL) 277 cmderr(EX_NOUSER, "no such uid `%s'\n", a_uid->val); 278 cmderr(EX_NOUSER, "no such user `%s'\n", a_name->val); 279 } 280 if (a_name == NULL) /* May be needed later */ 281 a_name = addarg(args, 'n', newstr(pwd->pw_name)); 282 283 /* 284 * Handle deletions now 285 */ 286 if (mode == M_DELETE) { 287 char file[MAXPATHLEN]; 288 char home[MAXPATHLEN]; 289 uid_t uid = pwd->pw_uid; 290 291 if (strcmp(pwd->pw_name, "root") == 0) 292 cmderr(EX_DATAERR, "cannot remove user 'root'\n"); 293 294 /* 295 * Remove skey record from /etc/skeykeys 296 */ 297 298 rmskey(pwd->pw_name); 299 300 /* 301 * Remove crontabs 302 */ 303 sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 304 if (access(file, F_OK) == 0) { 305 sprintf(file, "crontab -u %s -r", pwd->pw_name); 306 system(file); 307 } 308 /* 309 * Save these for later, since contents of pwd may be 310 * invalidated by deletion 311 */ 312 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 313 strncpy(home, pwd->pw_dir, sizeof home); 314 home[sizeof home - 1] = '\0'; 315 316 if (!delpwent(pwd)) 317 cmderr(EX_IOERR, "Error updating passwd file: %s\n", strerror(errno)); 318 editgroups(a_name->val, NULL); 319 320 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 321 322 /* 323 * Remove mail file 324 */ 325 remove(file); 326 327 /* 328 * Remove at jobs 329 */ 330 if (getpwuid(uid) == NULL) 331 rmat(uid); 332 333 /* 334 * Remove home directory and contents 335 */ 336 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 337 if (stat(home, &st) != -1) { 338 rm_r(home, uid); 339 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 340 a_name->val, (long) uid, home, 341 stat(home, &st) == -1 ? "" : "not completely "); 342 } 343 } 344 return EXIT_SUCCESS; 345 } else if (mode == M_PRINT) 346 return print_user(pwd, getarg(args, 'P') != NULL); 347 348 /* 349 * The rest is edit code 350 */ 351 if ((arg = getarg(args, 'l')) != NULL) { 352 if (strcmp(pwd->pw_name, "root") == 0) 353 cmderr(EX_DATAERR, "can't rename `root' account\n"); 354 pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 355 } 356 if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) { 357 pwd->pw_uid = (uid_t) atol(arg->val); 358 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 359 cmderr(EX_DATAERR, "can't change uid of `root' account\n"); 360 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 361 fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name); 362 } 363 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */ 364 pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid; 365 366 if ((arg = getarg(args, 'p')) != NULL) { 367 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 368 pwd->pw_change = 0; 369 else { 370 time_t now = time(NULL); 371 time_t expire = parse_date(now, arg->val); 372 373 if (now == expire) 374 cmderr(EX_DATAERR, "Invalid password change date `%s'\n", arg->val); 375 pwd->pw_change = expire; 376 } 377 } 378 if ((arg = getarg(args, 'e')) != NULL) { 379 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 380 pwd->pw_expire = 0; 381 else { 382 time_t now = time(NULL); 383 time_t expire = parse_date(now, arg->val); 384 385 if (now == expire) 386 cmderr(EX_DATAERR, "Invalid account expiry date `%s'\n", arg->val); 387 pwd->pw_expire = expire; 388 } 389 } 390 if ((arg = getarg(args, 's')) != NULL) 391 pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 392 393 if (getarg(args, 'L')) 394 pwd->pw_class = cnf->default_class; 395 396 if ((arg = getarg(args, 'd')) != NULL) { 397 if (stat(pwd->pw_dir = arg->val, &st) == -1) { 398 if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 399 fprintf(stderr, "WARNING: home `%s' does not exist\n", pwd->pw_dir); 400 } else if (!S_ISDIR(st.st_mode)) 401 fprintf(stderr, "WARNING: home `%s' is not a directory\n", pwd->pw_dir); 402 } 403 404 if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) 405 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 406 407 } else { 408 if (a_name == NULL) /* Required */ 409 cmderr(EX_DATAERR, "login name required\n"); 410 else if ((pwd = getpwnam(a_name->val)) != NULL) /* Exists */ 411 cmderr(EX_DATAERR, "login name `%s' already exists\n", a_name->val); 412 413 /* 414 * Now, set up defaults for a new user 415 */ 416 pwd = &fakeuser; 417 pwd->pw_name = a_name->val; 418 pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 419 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 420 pwd->pw_uid = pw_uidpolicy(cnf, args); 421 pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 422 pwd->pw_change = pw_pwdpolicy(cnf, args); 423 pwd->pw_expire = pw_exppolicy(cnf, args); 424 pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 425 pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 426 427 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 428 fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name); 429 } 430 431 /* 432 * Shared add/edit code 433 */ 434 if ((arg = getarg(args, 'c')) != NULL) 435 pwd->pw_gecos = pw_checkname((u_char *)arg->val, 1); 436 437 if ((arg = getarg(args, 'h')) != NULL) { 438 if (strcmp(arg->val, "-") == 0) 439 pwd->pw_passwd = "*"; /* No access */ 440 else { 441 int fd = atoi(arg->val); 442 int b; 443 int istty = isatty(fd); 444 struct termios t; 445 446 if (istty) { 447 if (tcgetattr(fd, &t) == -1) 448 istty = 0; 449 else { 450 struct termios n = t; 451 452 /* Disable echo */ 453 n.c_lflag &= ~(ECHO); 454 tcsetattr(fd, TCSANOW, &n); 455 printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 456 fflush(stdout); 457 } 458 } 459 b = read(fd, line, sizeof(line) - 1); 460 if (istty) { /* Restore state */ 461 tcsetattr(fd, TCSANOW, &t); 462 fputc('\n', stdout); 463 fflush(stdout); 464 } 465 if (b < 0) { 466 perror("-h file descriptor"); 467 return EX_IOERR; 468 } 469 line[b] = '\0'; 470 if ((p = strpbrk(line, " \t\r\n")) != NULL) 471 *p = '\0'; 472 if (!*line) 473 cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd); 474 pwd->pw_passwd = pw_pwcrypt(line); 475 } 476 } 477 478 /* 479 * Special case: -N only displays & exits 480 */ 481 if (getarg(args, 'N') != NULL) 482 return print_user(pwd, getarg(args, 'P') != NULL); 483 484 if ((mode == M_ADD && !addpwent(pwd)) || 485 (mode == M_UPDATE && !chgpwent(a_name->val, pwd))) { 486 perror("password update"); 487 return EX_IOERR; 488 } 489 /* 490 * Ok, user is created or changed - now edit group file 491 */ 492 493 if (mode == M_ADD || getarg(args, 'G') != NULL) 494 editgroups(pwd->pw_name, cnf->groups); 495 496 /* pwd may have been invalidated */ 497 if ((pwd = getpwnam(a_name->val)) == NULL) 498 cmderr(EX_NOUSER, "user '%s' disappeared during update\n", a_name->val); 499 500 grp = getgrgid(pwd->pw_gid); 501 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 502 pwd->pw_name, (long) pwd->pw_uid, 503 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 504 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 505 506 /* 507 * If adding, let's touch and chown the user's mail file. This is not 508 * strictly necessary under BSD with a 0755 maildir but it also 509 * doesn't hurt anything to create the empty mailfile 510 */ 511 if (mode == M_ADD) { 512 FILE *fp; 513 514 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 515 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 516 * mtime */ 517 chown(line, pwd->pw_uid, pwd->pw_gid); 518 519 /* 520 * Send mail to the new user as well, if we are asked to 521 */ 522 if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 523 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 524 525 if (pfp == NULL) 526 perror("sendmail"); 527 else { 528 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 529 while (fgets(line, sizeof(line), fp) != NULL) { 530 /* Do substitutions? */ 531 fputs(line, pfp); 532 } 533 pclose(pfp); 534 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 535 pwd->pw_name, (long) pwd->pw_uid); 536 } 537 fclose(fp); 538 } 539 } 540 /* 541 * Finally, let's create and populate the user's home directory. Note 542 * that this also `works' for editing users if -m is used, but 543 * existing files will *not* be overwritten. 544 */ 545 if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 546 copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 547 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 548 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 549 } 550 return EXIT_SUCCESS; 551} 552 553 554static uid_t 555pw_uidpolicy(struct userconf * cnf, struct cargs * args) 556{ 557 struct passwd *pwd; 558 uid_t uid = (uid_t) - 1; 559 struct carg *a_uid = getarg(args, 'u'); 560 561 /* 562 * Check the given uid, if any 563 */ 564 if (a_uid != NULL) { 565 uid = (uid_t) atol(a_uid->val); 566 567 if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL) 568 cmderr(EX_DATAERR, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid); 569 } else { 570 struct bitmap bm; 571 572 /* 573 * We need to allocate the next available uid under one of 574 * two policies a) Grab the first unused uid b) Grab the 575 * highest possible unused uid 576 */ 577 if (cnf->min_uid >= cnf->max_uid) { /* Sanity 578 * claus^H^H^H^Hheck */ 579 cnf->min_uid = 1000; 580 cnf->max_uid = 32000; 581 } 582 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 583 584 /* 585 * Now, let's fill the bitmap from the password file 586 */ 587 setpwent(); 588 while ((pwd = getpwent()) != NULL) 589 if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid) 590 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 591 endpwent(); 592 593 /* 594 * Then apply the policy, with fallback to reuse if necessary 595 */ 596 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 597 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 598 599 /* 600 * Another sanity check 601 */ 602 if (uid < cnf->min_uid || uid > cnf->max_uid) 603 cmderr(EX_SOFTWARE, "unable to allocate a new uid - range fully used\n"); 604 bm_dealloc(&bm); 605 } 606 return uid; 607} 608 609 610static uid_t 611pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 612{ 613 struct group *grp; 614 gid_t gid = (uid_t) - 1; 615 struct carg *a_gid = getarg(args, 'g'); 616 617 /* 618 * If no arg given, see if default can help out 619 */ 620 if (a_gid == NULL && cnf->default_group && *cnf->default_group) 621 a_gid = addarg(args, 'g', cnf->default_group); 622 623 /* 624 * Check the given gid, if any 625 */ 626 setgrent(); 627 if (a_gid != NULL) { 628 if ((grp = getgrnam(a_gid->val)) == NULL) { 629 gid = (gid_t) atol(a_gid->val); 630 if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL) 631 cmderr(EX_NOUSER, "group `%s' is not defined\n", a_gid->val); 632 } 633 gid = grp->gr_gid; 634 } else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) { 635 gid = grp->gr_gid; /* Already created? Use it anyway... */ 636 } else { 637 struct cargs grpargs; 638 char tmp[32]; 639 640 LIST_INIT(&grpargs); 641 addarg(&grpargs, 'n', nam); 642 643 /* 644 * We need to auto-create a group with the user's name. We 645 * can send all the appropriate output to our sister routine 646 * bit first see if we can create a group with gid==uid so we 647 * can keep the user and group ids in sync. We purposely do 648 * NOT check the gid range if we can force the sync. If the 649 * user's name dups an existing group, then the group add 650 * function will happily handle that case for us and exit. 651 */ 652 if (getgrgid(prefer) == NULL) { 653 sprintf(tmp, "%lu", (unsigned long) prefer); 654 addarg(&grpargs, 'g', tmp); 655 } 656 if (getarg(args, 'N')) 657 { 658 addarg(&grpargs, 'N', NULL); 659 addarg(&grpargs, 'q', NULL); 660 gid = pw_group(cnf, M_NEXT, &grpargs); 661 } 662 else 663 { 664 pw_group(cnf, M_ADD, &grpargs); 665 if ((grp = getgrnam(nam)) != NULL) 666 gid = grp->gr_gid; 667 } 668 a_gid = grpargs.lh_first; 669 while (a_gid != NULL) { 670 struct carg *t = a_gid->list.le_next; 671 LIST_REMOVE(a_gid, list); 672 a_gid = t; 673 } 674 } 675 endgrent(); 676 return gid; 677} 678 679 680static time_t 681pw_pwdpolicy(struct userconf * cnf, struct cargs * args) 682{ 683 time_t result = 0; 684 time_t now = time(NULL); 685 struct carg *arg = getarg(args, 'e'); 686 687 if (arg != NULL) { 688 if ((result = parse_date(now, arg->val)) == now) 689 cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val); 690 } else if (cnf->password_days > 0) 691 result = now + ((long) cnf->password_days * 86400L); 692 return result; 693} 694 695 696static time_t 697pw_exppolicy(struct userconf * cnf, struct cargs * args) 698{ 699 time_t result = 0; 700 time_t now = time(NULL); 701 struct carg *arg = getarg(args, 'e'); 702 703 if (arg != NULL) { 704 if ((result = parse_date(now, arg->val)) == now) 705 cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val); 706 } else if (cnf->expire_days > 0) 707 result = now + ((long) cnf->expire_days * 86400L); 708 return result; 709} 710 711 712static char * 713pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 714{ 715 struct carg *arg = getarg(args, 'd'); 716 717 if (arg) 718 return arg->val; 719 else { 720 static char home[128]; 721 722 if (cnf->home == NULL || *cnf->home == '\0') 723 cmderr(EX_CONFIG, "no base home directory set\n"); 724 sprintf(home, "%s/%s", cnf->home, user); 725 return home; 726 } 727} 728 729static char * 730shell_path(char const * path, char *shells[], char *sh) 731{ 732 if (sh != NULL && (*sh == '/' || *sh == '\0')) 733 return sh; /* specified full path or forced none */ 734 else { 735 char *p; 736 char paths[_UC_MAXLINE]; 737 738 /* 739 * We need to search paths 740 */ 741 strncpy(paths, path, sizeof paths); 742 paths[sizeof paths - 1] = '\0'; 743 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 744 int i; 745 static char shellpath[256]; 746 747 if (sh != NULL) { 748 sprintf(shellpath, "%s/%s", p, sh); 749 if (access(shellpath, X_OK) == 0) 750 return shellpath; 751 } else 752 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 753 sprintf(shellpath, "%s/%s", p, shells[i]); 754 if (access(shellpath, X_OK) == 0) 755 return shellpath; 756 } 757 } 758 if (sh == NULL) 759 cmderr(EX_OSFILE, "can't find shell `%s' in shell paths\n", sh); 760 cmderr(EX_CONFIG, "no default shell available or defined\n"); 761 return NULL; 762 } 763} 764 765 766static char * 767pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 768{ 769 char *sh = newshell; 770 struct carg *arg = getarg(args, 's'); 771 772 if (newshell == NULL && arg != NULL) 773 sh = arg->val; 774 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 775} 776 777static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 778 779char * 780pw_pwcrypt(char *password) 781{ 782 int i; 783 char salt[12]; 784 785 static char buf[256]; 786 787 /* 788 * Calculate a salt value 789 */ 790 srandom((unsigned) (time(NULL) ^ getpid())); 791 for (i = 0; i < 8; i++) 792 salt[i] = chars[random() % 63]; 793 salt[i] = '\0'; 794 795 return strcpy(buf, crypt(password, salt)); 796} 797 798#if defined(__FreeBSD__) 799 800#if defined(USE_MD5RAND) 801u_char * 802pw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 803{ 804 int i; 805 for (i=0;i<len;i+=16) { 806 u_char ubuf[16]; 807 808 MD5_CTX md5_ctx; 809 struct timeval tv, tvo; 810 struct rusage ru; 811 int n=0; 812 int t; 813 814 MD5Init (&md5_ctx); 815 t=getpid(); 816 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 817 t=getppid(); 818 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 819 gettimeofday (&tvo, NULL); 820 do { 821 getrusage (RUSAGE_SELF, &ru); 822 MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 823 gettimeofday (&tv, NULL); 824 MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 825 } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 826 MD5Final (ubuf, &md5_ctx); 827 memcpy(buf+i, ubuf, MIN(16, len-n)); 828 } 829 return buf; 830} 831 832#else /* Use random device (preferred) */ 833 834static u_char * 835pw_getrand(u_char *buf, int len) 836{ 837 int fd; 838 fd = open("/dev/urandom", O_RDONLY); 839 if (fd==-1) 840 cmderr(EX_OSFILE, "can't open /dev/urandom: %s\n", strerror(errno)); 841 else if (read(fd, buf, len)!=len) 842 cmderr(EX_IOERR, "read error on /dev/urandom\n"); 843 close(fd); 844 return buf; 845} 846 847#endif 848 849#else /* Portable version */ 850 851static u_char * 852pw_getrand(u_char *buf, int len) 853{ 854 int i; 855 856 for (i = 0; i < len; i++) { 857 unsigned val = random(); 858 /* Use all bits in the random value */ 859 buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 860 } 861 return buf; 862} 863 864#endif 865 866static char * 867pw_password(struct userconf * cnf, struct cargs * args, char const * user) 868{ 869 int i, l; 870 char pwbuf[32]; 871 u_char rndbuf[sizeof pwbuf]; 872 873 switch (cnf->default_password) { 874 case -1: /* Random password */ 875 srandom((unsigned) (time(NULL) ^ getpid())); 876 l = (random() % 8 + 8); /* 8 - 16 chars */ 877 pw_getrand(rndbuf, l); 878 for (i = 0; i < l; i++) 879 pwbuf[i] = chars[rndbuf[i] % sizeof(chars)]; 880 pwbuf[i] = '\0'; 881 882 /* 883 * We give this information back to the user 884 */ 885 if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) { 886 if (isatty(1)) 887 printf("Password for '%s' is: ", user); 888 printf("%s\n", pwbuf); 889 fflush(stdout); 890 } 891 break; 892 893 case -2: /* No password at all! */ 894 return ""; 895 896 case 0: /* No login - default */ 897 default: 898 return "*"; 899 900 case 1: /* user's name */ 901 strncpy(pwbuf, user, sizeof pwbuf); 902 pwbuf[sizeof pwbuf - 1] = '\0'; 903 break; 904 } 905 return pw_pwcrypt(pwbuf); 906} 907 908 909static int 910print_user(struct passwd * pwd, int pretty) 911{ 912 if (!pretty) { 913 char buf[_UC_MAXLINE]; 914 915 fmtpwent(buf, pwd); 916 fputs(buf, stdout); 917 } else { 918 int j; 919 char *p; 920 struct group *grp = getgrgid(pwd->pw_gid); 921 char uname[60] = "User &", office[60] = "[None]", 922 wphone[60] = "[None]", hphone[60] = "[None]"; 923 char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 924 struct tm * tptr; 925 926 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 927 strncpy(uname, p, sizeof uname); 928 uname[sizeof uname - 1] = '\0'; 929 if ((p = strtok(NULL, ",")) != NULL) { 930 strncpy(office, p, sizeof office); 931 office[sizeof office - 1] = '\0'; 932 if ((p = strtok(NULL, ",")) != NULL) { 933 strncpy(wphone, p, sizeof wphone); 934 wphone[sizeof wphone - 1] = '\0'; 935 if ((p = strtok(NULL, "")) != NULL) { 936 strncpy(hphone, p, sizeof hphone); 937 hphone[sizeof hphone - 1] = '\0'; 938 } 939 } 940 } 941 } 942 /* 943 * Handle '&' in gecos field 944 */ 945 if ((p = strchr(uname, '&')) != NULL) { 946 int l = strlen(pwd->pw_name); 947 int m = strlen(p); 948 949 memmove(p + l, p + 1, m); 950 memmove(p, pwd->pw_name, l); 951 *p = (char) toupper(*p); 952 } 953 if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 954 strftime(acexpire, sizeof acexpire, "%c", tptr); 955 if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL) 956 strftime(pwexpire, sizeof pwexpire, "%c", tptr); 957 printf("Login Name: %-10s #%-16ld Group: %-10s #%ld\n" 958 " Full Name: %s\n" 959 " Home: %-26.26s Class: %s\n" 960 " Shell: %-26.26s Office: %s\n" 961 "Work Phone: %-26.26s Home Phone: %s\n" 962 "Acc Expire: %-26.26s Pwd Expire: %s\n", 963 pwd->pw_name, (long) pwd->pw_uid, 964 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 965 uname, pwd->pw_dir, pwd->pw_class, 966 pwd->pw_shell, office, wphone, hphone, 967 acexpire, pwexpire); 968 setgrent(); 969 j = 0; 970 while ((grp=getgrent()) != NULL) 971 { 972 int i = 0; 973 while (grp->gr_mem[i] != NULL) 974 { 975 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 976 { 977 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 978 break; 979 } 980 ++i; 981 } 982 } 983 endgrent(); 984 printf("%s\n", j ? "\n" : ""); 985 } 986 return EXIT_SUCCESS; 987} 988 989char * 990pw_checkname(u_char *name, int gecos) 991{ 992 int l = 0; 993 char const *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\""; 994 995 while (name[l]) { 996 if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 || 997 (!gecos && l==0 && name[l] == '-') || /* leading '-' */ 998 (!gecos && name[l] & 0x80)) /* 8-bit */ 999 cmderr(EX_DATAERR, (name[l] >= ' ' && name[l] < 127) 1000 ? "invalid character `%c' in field\n" 1001 : "invalid character 0x%02x in field\n", 1002 name[l]); 1003 ++l; 1004 } 1005 if (!gecos && l > MAXLOGNAME) 1006 cmderr(EX_DATAERR, "name too long `%s'\n", name); 1007 return (char *)name; 1008} 1009 1010 1011static void 1012rmat(uid_t uid) 1013{ 1014 DIR *d = opendir("/var/at/jobs"); 1015 1016 if (d != NULL) { 1017 struct dirent *e; 1018 1019 while ((e = readdir(d)) != NULL) { 1020 struct stat st; 1021 1022 if (strncmp(e->d_name, ".lock", 5) != 0 && 1023 stat(e->d_name, &st) == 0 && 1024 !S_ISDIR(st.st_mode) && 1025 st.st_uid == uid) { 1026 char tmp[MAXPATHLEN]; 1027 1028 sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 1029 system(tmp); 1030 } 1031 } 1032 closedir(d); 1033 } 1034} 1035 1036static void 1037rmskey(char const * name) 1038{ 1039 static const char etcskey[] = "/etc/skeykeys"; 1040 FILE *fp = fopen(etcskey, "r+"); 1041 1042 if (fp != NULL) { 1043 char tmp[1024]; 1044 off_t atofs = 0; 1045 int length = strlen(name); 1046 1047 while (fgets(tmp, sizeof tmp, fp) != NULL) { 1048 if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 1049 if (fseek(fp, atofs, SEEK_SET) == 0) { 1050 fwrite("#", 1, 1, fp); /* Comment username out */ 1051 } 1052 break; 1053 } 1054 atofs = ftell(fp); 1055 } 1056 /* 1057 * If we got an error of any sort, don't update! 1058 */ 1059 fclose(fp); 1060 } 1061} 1062 1063