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