pw_user.c revision 20253
1/*- 2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>. 3 * 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 as 10 * the first lines of this file unmodified. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by David L. Nugent. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $Id$ 33 */ 34 35#include <unistd.h> 36#include <fcntl.h> 37#include <ctype.h> 38#include <paths.h> 39#include <sys/param.h> 40#include <dirent.h> 41#include <termios.h> 42#include "pw.h" 43#include "bitmap.h" 44#include "pwupd.h" 45 46static int print_user(struct passwd * pwd, int pretty); 47static uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 48static uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 49static time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 50static time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 51static char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 52static char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 53static char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 54static char *pw_checkname(char *name, int gecos); 55static char *shell_path(char const * path, char *shells[], char *sh); 56static void rmat(uid_t uid); 57 58/*- 59 * -C config configuration file 60 * -q quiet operation 61 * -n name login name 62 * -u uid user id 63 * -c comment user name/comment 64 * -d directory home directory 65 * -e date account expiry date 66 * -p date password expiry date 67 * -g grp primary group 68 * -G grp1,grp2 additional groups 69 * -m [ -k dir ] create and set up home 70 * -s shell name of login shell 71 * -o duplicate uid ok 72 * -L class user class 73 * -l name new login name 74 * -h fd password filehandle 75 * -F force print or add 76 * Setting defaults: 77 * -D set user defaults 78 * -b dir default home root dir 79 * -e period default expiry period 80 * -p period default password change period 81 * -g group default group 82 * -G grp1,grp2.. default additional groups 83 * -L class default login class 84 * -k dir default home skeleton 85 * -s shell default shell 86 * -w method default password method 87 */ 88 89int 90pw_user(struct userconf * cnf, int mode, struct cargs * args) 91{ 92 char *p = NULL; 93 struct carg *a_name; 94 struct carg *a_uid; 95 struct carg *arg; 96 struct passwd *pwd = NULL; 97 struct group *grp; 98 struct stat st; 99 char line[MAXPWLINE]; 100 101 static struct passwd fakeuser = 102 { 103 NULL, 104 "*", 105 -1, 106 -1, 107 0, 108 "", 109 "User &", 110 "/bin/sh", 111 0, 112 0 113 }; 114 115 /* 116 * We can do all of the common legwork here 117 */ 118 119 if ((arg = getarg(args, 'b')) != NULL) { 120 if (stat(cnf->home = arg->val, &st) == -1 || S_ISDIR(st.st_mode)) 121 cmderr(X_CMDERR, "root home `%s' is not a directory or does not exist\n", cnf->home); 122 } 123 if ((arg = getarg(args, 'e')) != NULL) 124 cnf->expire_days = atoi(arg->val); 125 126 if ((arg = getarg(args, 'p')) != NULL && arg->val) 127 cnf->password_days = atoi(arg->val); 128 129 if ((arg = getarg(args, 'g')) != NULL) { 130 p = arg->val; 131 if ((grp = getgrnam(p)) == NULL) { 132 if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 133 cmderr(X_NOTFOUND, "group `%s' does not exist\n", p); 134 } 135 cnf->default_group = newstr(grp->gr_name); 136 } 137 if ((arg = getarg(args, 'L')) != NULL) 138 cnf->default_class = pw_checkname(arg->val, 0); 139 140 if ((arg = getarg(args, 'G')) != NULL && arg->val) { 141 int i = 0; 142 143 for (p = strtok(arg->val, ", \t"); i < _UC_MAXGROUPS && p != NULL; p = strtok(NULL, ", \t")) { 144 if ((grp = getgrnam(p)) == NULL) { 145 if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 146 cmderr(X_NOTFOUND, "group `%s' does not exist\n", p); 147 } 148 cnf->groups[i++] = newstr(grp->gr_name); 149 } 150 while (i < _UC_MAXGROUPS) 151 cnf->groups[i++] = NULL; 152 } 153 if ((arg = getarg(args, 'k')) != NULL) { 154 if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode)) 155 cmderr(X_CMDERR, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir); 156 } 157 if ((arg = getarg(args, 's')) != NULL) 158 cnf->shell_default = arg->val; 159 160 if (mode == M_ADD && getarg(args, 'D')) { 161 if (getarg(args, 'n') != NULL) 162 cmderr(X_CMDERR, "can't combine `-D' with `-n name'\n"); 163 if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 164 if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 165 cnf->min_uid = 1000; 166 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 167 cnf->max_uid = 32000; 168 } 169 if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 170 if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 171 cnf->min_gid = 1000; 172 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 173 cnf->max_gid = 32000; 174 } 175 if ((arg = getarg(args, 'w')) != NULL) 176 cnf->default_password = boolean_val(arg->val, cnf->default_password); 177 178 arg = getarg(args, 'C'); 179 if (write_userconfig(arg ? arg->val : NULL)) 180 return X_ALLOK; 181 perror("config update"); 182 return X_UPDERROR; 183 } 184 if (mode == M_PRINT && getarg(args, 'a')) { 185 int pretty = getarg(args, 'p') != NULL; 186 187 setpwent(); 188 while ((pwd = getpwent()) != NULL) 189 print_user(pwd, pretty); 190 endpwent(); 191 return X_ALLOK; 192 } 193 if ((a_name = getarg(args, 'n')) != NULL) 194 pwd = getpwnam(pw_checkname(a_name->val, 0)); 195 a_uid = getarg(args, 'u'); 196 197 if (a_uid == NULL) { 198 if (a_name == NULL) 199 cmderr(X_CMDERR, "user name or id required\n"); 200 201 /* 202 * Determine whether 'n' switch is name or uid - we don't 203 * really don't really care which we have, but we need to 204 * know. 205 */ 206 if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) { /* Assume uid */ 207 (a_uid = a_name)->ch = 'u'; 208 a_name = NULL; 209 } 210 } 211 /* 212 * Update, delete & print require that the user exists 213 */ 214 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 215 if (a_name == NULL && pwd == NULL) /* Try harder */ 216 pwd = getpwuid(atoi(a_uid->val)); 217 218 if (pwd == NULL) { 219 if (mode == M_PRINT && getarg(args, 'F')) { 220 fakeuser.pw_name = a_name ? a_name->val : "nouser"; 221 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 222 return print_user(&fakeuser, getarg(args, 'p') != NULL); 223 } 224 if (a_name == NULL) 225 cmderr(X_NOTFOUND, "no such uid `%s'\n", a_uid->val); 226 cmderr(X_NOTFOUND, "no such user `%s'\n", a_name->val); 227 } 228 if (a_name == NULL) /* May be needed later */ 229 a_name = addarg(args, 'n', newstr(pwd->pw_name)); 230 231 /* 232 * Handle deletions now 233 */ 234 if (mode == M_DELETE) { 235 char file[MAXPATHLEN]; 236 char home[MAXPATHLEN]; 237 uid_t uid = pwd->pw_uid; 238 239 if (strcmp(pwd->pw_name, "root") == 0) 240 cmderr(X_CMDERR, "cannot remove user 'root'\n"); 241 242 /* 243 * Remove crontabs 244 */ 245 sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 246 if (access(file, F_OK) == 0) { 247 sprintf(file, "crontab -u %s -r", pwd->pw_name); 248 system(file); 249 } 250 /* 251 * Save these for later, since contents of pwd may be 252 * invalidated by deletion 253 */ 254 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 255 strncpy(home, pwd->pw_dir, sizeof home); 256 home[sizeof home - 1] = '\0'; 257 258 if (!delpwent(pwd)) 259 cmderr(X_NOUPDATE, "Error updating passwd file: %s\n", strerror(errno)); 260 editgroups(a_name->val, NULL); 261 262 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 263 264 /* 265 * Remove mail file 266 */ 267 remove(file); 268 269 /* 270 * Remove at jobs 271 */ 272 if (getpwuid(uid) == NULL) 273 rmat(uid); 274 275 /* 276 * Remove home directory and contents 277 */ 278 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 279 if (stat(home, &st) != -1) { 280 rm_r(home, uid); 281 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 282 a_name->val, (long) uid, home, 283 stat(home, &st) == -1 ? "" : "not completely "); 284 } 285 } 286 return X_ALLOK; 287 } else if (mode == M_PRINT) 288 return print_user(pwd, getarg(args, 'p') != NULL); 289 290 /* 291 * The rest is edit code 292 */ 293 if ((arg = getarg(args, 'l')) != NULL) { 294 if (strcmp(pwd->pw_name, "root") == 0) 295 cmderr(X_CMDERR, "can't rename `root' account\n"); 296 pwd->pw_name = pw_checkname(arg->val, 0); 297 } 298 if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) { 299 pwd->pw_uid = (uid_t) atol(arg->val); 300 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 301 cmderr(X_CMDERR, "can't change uid of `root' account\n"); 302 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 303 fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name); 304 } 305 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */ 306 pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid; 307 308 if ((arg = getarg(args, 'p')) != NULL) { 309 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 310 pwd->pw_change = 0; 311 else { 312 time_t now = time(NULL); 313 time_t expire = parse_date(now, arg->val); 314 315 if (now == expire) 316 cmderr(X_CMDERR, "Invalid password change date `%s'\n", arg->val); 317 pwd->pw_change = expire; 318 } 319 } 320 if ((arg = getarg(args, 'p')) != NULL) { 321 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 322 pwd->pw_expire = 0; 323 else { 324 time_t now = time(NULL); 325 time_t expire = parse_date(now, arg->val); 326 327 if (now == expire) 328 cmderr(X_CMDERR, "Invalid password change date `%s'\n", arg->val); 329 pwd->pw_expire = expire; 330 } 331 } 332 if ((arg = getarg(args, 's')) != NULL) 333 pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 334 335 if (getarg(args, 'L')) 336 pwd->pw_class = cnf->default_class; 337 338 } else { 339 if (a_name == NULL) /* Required */ 340 cmderr(X_CMDERR, "login name required\n"); 341 else if ((pwd = getpwnam(a_name->val)) != NULL) /* Exists */ 342 cmderr(X_EXISTS, "login name `%s' already exists\n", a_name->val); 343 344 /* 345 * Now, set up defaults for a new user 346 */ 347 pwd = &fakeuser; 348 pwd->pw_name = a_name->val; 349 pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 350 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 351 pwd->pw_uid = pw_uidpolicy(cnf, args); 352 pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 353 pwd->pw_change = pw_pwdpolicy(cnf, args); 354 pwd->pw_expire = pw_exppolicy(cnf, args); 355 pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 356 pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 357 358 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 359 fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name); 360 } 361 362 /* 363 * Shared add/edit code 364 */ 365 if ((arg = getarg(args, 'c')) != NULL) 366 pwd->pw_gecos = pw_checkname(arg->val, 1); 367 368 if ((arg = getarg(args, 'h')) != NULL) { 369 if (strcmp(arg->val, "-") == 0) 370 pwd->pw_passwd = "*"; /* No access */ 371 else { 372 int fd = atoi(arg->val); 373 int b; 374 int istty = isatty(fd); 375 struct termios t; 376 377 if (istty) { 378 if (tcgetattr(fd, &t) == -1) 379 istty = 0; 380 else { 381 struct termios n = t; 382 383 /* Disable echo */ 384 n.c_lflag &= ~(ECHO); 385 tcsetattr(fd, TCSANOW, &n); 386 printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 387 fflush(stdout); 388 } 389 } 390 b = read(fd, line, sizeof(line) - 1); 391 if (istty) { /* Restore state */ 392 tcsetattr(fd, TCSANOW, &t); 393 fputc('\n', stdout); 394 fflush(stdout); 395 } 396 if (b < 0) { 397 perror("-h file descriptor"); 398 return X_CMDERR; 399 } 400 line[b] = '\0'; 401 if ((p = strpbrk(line, " \t\r\n")) != NULL) 402 *p = '\0'; 403 if (!*line) 404 cmderr(X_CMDERR, "empty password read on file descriptor %d\n", fd); 405 pwd->pw_passwd = pw_pwcrypt(line); 406 } 407 } 408 if ((mode == M_ADD && !addpwent(pwd)) || 409 (mode == M_UPDATE && !chgpwent(a_name->val, pwd))) { 410 perror("password update"); 411 return X_NOUPDATE; 412 } 413 /* 414 * Ok, user is created or changed - now edit group file 415 */ 416 417 if (mode == M_ADD || getarg(args, 'G') != NULL) 418 editgroups(pwd->pw_name, cnf->groups); 419 420 /* pwd may have been invalidated */ 421 if ((pwd = getpwnam(a_name->val)) == NULL) 422 cmderr(X_NOTFOUND, "user '%s' disappeared during update\n", a_name->val); 423 424 grp = getgrgid(pwd->pw_gid); 425 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 426 pwd->pw_name, (long) pwd->pw_uid, 427 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 428 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 429 430 /* 431 * If adding, let's touch and chown the user's mail file. This is not 432 * strictly necessary under BSD with a 0755 maildir but it also 433 * doesn't hurt anything to create the empty mailfile 434 */ 435 if (mode == M_ADD) { 436 FILE *fp; 437 438 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 439 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 440 * mtime */ 441 chown(line, pwd->pw_uid, pwd->pw_gid); 442 443 /* 444 * Send mail to the new user as well, if we are asked to 445 */ 446 if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 447 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 448 449 if (pfp == NULL) 450 perror("sendmail"); 451 else { 452 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 453 while (fgets(line, sizeof(line), fp) != NULL) { 454 /* Do substitutions? */ 455 fputs(line, pfp); 456 } 457 pclose(pfp); 458 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 459 pwd->pw_name, (long) pwd->pw_uid); 460 } 461 fclose(fp); 462 } 463 } 464 /* 465 * Finally, let's create and populate the user's home directory. Note 466 * that this also `works' for editing users if -m is used, but 467 * existing files will *not* be overwritten. 468 */ 469 if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 470 copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 471 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 472 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 473 } 474 return X_ALLOK; 475} 476 477 478static uid_t 479pw_uidpolicy(struct userconf * cnf, struct cargs * args) 480{ 481 struct passwd *pwd; 482 uid_t uid = (uid_t) - 1; 483 struct carg *a_uid = getarg(args, 'u'); 484 485 /* 486 * Check the given uid, if any 487 */ 488 if (a_uid != NULL) { 489 uid = (uid_t) atol(a_uid->val); 490 491 if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL) 492 cmderr(X_EXISTS, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid); 493 } else { 494 struct bitmap bm; 495 496 /* 497 * We need to allocate the next available uid under one of 498 * two policies a) Grab the first unused uid b) Grab the 499 * highest possible unused uid 500 */ 501 if (cnf->min_uid >= cnf->max_uid) { /* Sanity 502 * claus^H^H^H^Hheck */ 503 cnf->min_uid = 1000; 504 cnf->max_uid = 32000; 505 } 506 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 507 508 /* 509 * Now, let's fill the bitmap from the password file 510 */ 511 setpwent(); 512 while ((pwd = getpwent()) != NULL) 513 if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid) 514 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 515 endpwent(); 516 517 /* 518 * Then apply the policy, with fallback to reuse if necessary 519 */ 520 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 521 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 522 523 /* 524 * Another sanity check 525 */ 526 if (uid < cnf->min_uid || uid > cnf->max_uid) 527 cmderr(X_EXISTS, "unable to allocate a new uid - range fully used\n"); 528 bm_dealloc(&bm); 529 } 530 return uid; 531} 532 533 534static uid_t 535pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 536{ 537 struct group *grp; 538 gid_t gid = (uid_t) - 1; 539 struct carg *a_gid = getarg(args, 'g'); 540 541 /* 542 * If no arg given, see if default can help out 543 */ 544 if (a_gid == NULL && cnf->default_group && *cnf->default_group) 545 a_gid = addarg(args, 'g', cnf->default_group); 546 547 /* 548 * Check the given gid, if any 549 */ 550 if (a_gid != NULL) { 551 setgrent(); 552 if ((grp = getgrnam(a_gid->val)) == NULL) { 553 gid = (gid_t) atol(a_gid->val); 554 if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL) 555 cmderr(X_NOTFOUND, "group `%s' is not defined\n", a_gid->val); 556 } 557 endgrent(); 558 gid = grp->gr_gid; 559 } else { 560 struct cargs grpargs; 561 char tmp[32]; 562 563 LIST_INIT(&grpargs); 564 addarg(&grpargs, 'n', nam); 565 566 /* 567 * We need to auto-create a group with the user's name. We 568 * can send all the appropriate output to our sister routine 569 * bit first see if we can create a group with gid==uid so we 570 * can keep the user and group ids in sync. We purposely do 571 * NOT check the gid range if we can force the sync. If the 572 * user's name dups an existing group, then the group add 573 * function will happily handle that case for us and exit. 574 */ 575 if (getgrgid(prefer) == NULL) { 576 sprintf(tmp, "%lu", (unsigned long) prefer); 577 addarg(&grpargs, 'g', tmp); 578 } 579 endgrent(); 580 pw_group(cnf, M_ADD, &grpargs); 581 a_gid = grpargs.lh_first; 582 while (a_gid != NULL) { 583 struct carg *t = a_gid->list.le_next; 584 585 LIST_REMOVE(a_gid, list); 586 a_gid = t; 587 } 588 if ((grp = getgrnam(nam)) != NULL) 589 gid = grp->gr_gid; 590 } 591 return gid; 592} 593 594 595static time_t 596pw_pwdpolicy(struct userconf * cnf, struct cargs * args) 597{ 598 time_t result = 0; 599 time_t now = time(NULL); 600 struct carg *arg = getarg(args, 'e'); 601 602 if (arg != NULL) { 603 if ((result = parse_date(now, arg->val)) == now) 604 cmderr(X_NOTFOUND, "invalid date/time `%s'\n", arg->val); 605 } else if (cnf->password_days > 0) 606 result = now + ((long) cnf->password_days * 86400L); 607 return result; 608} 609 610 611static time_t 612pw_exppolicy(struct userconf * cnf, struct cargs * args) 613{ 614 time_t result = 0; 615 time_t now = time(NULL); 616 struct carg *arg = getarg(args, 'e'); 617 618 if (arg != NULL) { 619 if ((result = parse_date(now, arg->val)) == now) 620 cmderr(X_NOTFOUND, "invalid date/time `%s'\n", arg->val); 621 } else if (cnf->expire_days > 0) 622 result = now + ((long) cnf->expire_days * 86400L); 623 return result; 624} 625 626 627static char * 628pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 629{ 630 struct carg *arg = getarg(args, 'd'); 631 632 if (arg) 633 return arg->val; 634 else { 635 static char home[128]; 636 637 if (cnf->home == NULL || *cnf->home == '\0') 638 cmderr(X_NOTFOUND, "no base home directory set\n"); 639 sprintf(home, "%s/%s", cnf->home, user); 640 return home; 641 } 642} 643 644static char * 645shell_path(char const * path, char *shells[], char *sh) 646{ 647 if (sh != NULL && (*sh == '/' || *sh == '\0')) 648 return sh; /* specified full path or forced none */ 649 else { 650 char *p; 651 char paths[_UC_MAXLINE]; 652 653 /* 654 * We need to search paths 655 */ 656 strncpy(paths, path, sizeof paths); 657 paths[sizeof paths - 1] = '\0'; 658 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 659 int i; 660 static char shellpath[256]; 661 662 if (sh != NULL) { 663 sprintf(shellpath, "%s/%s", p, sh); 664 if (access(shellpath, X_OK) == 0) 665 return shellpath; 666 } else 667 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 668 sprintf(shellpath, "%s/%s", p, shells[i]); 669 if (access(shellpath, X_OK) == 0) 670 return shellpath; 671 } 672 } 673 if (sh == NULL) 674 cmderr(X_CMDERR, "can't find shell `%s' in shell paths\n", sh); 675 cmderr(X_CMDERR, "no default shell available or defined\n"); 676 return NULL; 677 } 678} 679 680 681static char * 682pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 683{ 684 char *sh = newshell; 685 struct carg *arg = getarg(args, 's'); 686 687 if (newshell == NULL && arg != NULL) 688 sh = arg->val; 689 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 690} 691 692static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 693 694char * 695pw_pwcrypt(char *password) 696{ 697 int i; 698 char salt[12]; 699 700 static char buf[256]; 701 702 /* 703 * Calculate a salt value 704 */ 705 srandom((unsigned) (time(NULL) | getpid())); 706 for (i = 0; i < 8; i++) 707 salt[i] = chars[random() % 63]; 708 salt[i] = '\0'; 709 710 return strcpy(buf, crypt(password, salt)); 711} 712 713 714static char * 715pw_password(struct userconf * cnf, struct cargs * args, char const * user) 716{ 717 int i, l; 718 char pwbuf[32]; 719 720 switch (cnf->default_password) { 721 case -1: /* Random password */ 722 srandom((unsigned) (time(NULL) | getpid())); 723 l = (random() % 8 + 8); /* 8 - 16 chars */ 724 for (i = 0; i < l; i++) 725 pwbuf[i] = chars[random() % sizeof(chars)]; 726 pwbuf[i] = '\0'; 727 728 /* 729 * We give this information back to the user 730 */ 731 if (getarg(args, 'h') == NULL) { 732 if (isatty(0)) 733 printf("Password is: "); 734 printf("%s\n", pwbuf); 735 fflush(stdout); 736 } 737 break; 738 739 case -2: /* No password at all! */ 740 return ""; 741 742 case 0: /* No login - default */ 743 default: 744 return "*"; 745 746 case 1: /* user's name */ 747 strncpy(pwbuf, user, sizeof pwbuf); 748 pwbuf[sizeof pwbuf - 1] = '\0'; 749 break; 750 } 751 return pw_pwcrypt(pwbuf); 752} 753 754 755static int 756print_user(struct passwd * pwd, int pretty) 757{ 758 if (!pretty) { 759 char buf[_UC_MAXLINE]; 760 761 fmtpwent(buf, pwd); 762 fputs(buf, stdout); 763 } else { 764 char *p; 765 struct group *grp = getgrgid(pwd->pw_gid); 766 char uname[60] = "User &", office[60] = "[None]", 767 wphone[60] = "[None]", hphone[60] = "[None]"; 768 769 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 770 strncpy(uname, p, sizeof uname); 771 uname[sizeof uname - 1] = '\0'; 772 if ((p = strtok(NULL, ",")) != NULL) { 773 strncpy(office, p, sizeof office); 774 office[sizeof office - 1] = '\0'; 775 if ((p = strtok(NULL, ",")) != NULL) { 776 strncpy(wphone, p, sizeof wphone); 777 wphone[sizeof wphone - 1] = '\0'; 778 if ((p = strtok(NULL, "")) != NULL) { 779 strncpy(hphone, p, sizeof hphone); 780 hphone[sizeof hphone - 1] = '\0'; 781 } 782 } 783 } 784 } 785 /* 786 * Handle '&' in gecos field 787 */ 788 if ((p = strchr(uname, '&')) != NULL) { 789 int l = strlen(pwd->pw_name); 790 int m = strlen(p); 791 792 memmove(p + l, p + 1, m); 793 memmove(p, pwd->pw_name, l); 794 *p = (char) toupper(*p); 795 } 796 printf("Login Name : %-10s #%-22ld Group : %-10s #%ld\n" 797 " Full Name : %s\n" 798 " Home : %-32.32s Class : %s\n" 799 " Shell : %-32.32s Office : %s\n" 800 "Work Phone : %-32.32s Home Phone : %s\n\n", 801 pwd->pw_name, (long) pwd->pw_uid, 802 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 803 uname, pwd->pw_dir, pwd->pw_class, 804 pwd->pw_shell, office, wphone, hphone); 805 } 806 return X_ALLOK; 807} 808 809static char * 810pw_checkname(char *name, int gecos) 811{ 812 int l = 0; 813 char const *notch = gecos ? ":" : " :+-&#%$^()!@~*?<>=|\\/\""; 814 815 while (name[l]) { 816 if (strchr(notch, name[l]) != NULL || name[l] < ' ') 817 cmderr(X_CMDERR, "invalid character `%c' in field\n", name[l]); 818 ++l; 819 } 820 if (!gecos && l > 8) 821 cmderr(X_CMDERR, "name too long `%s'\n", name); 822 return name; 823} 824 825 826static void 827rmat(uid_t uid) 828{ 829 DIR *d = opendir("/var/at/jobs"); 830 831 if (d != NULL) { 832 struct dirent *e; 833 834 while ((e = readdir(d)) != NULL) { 835 struct stat st; 836 837 if (strncmp(e->d_name, ".lock", 5) != 0 && 838 stat(e->d_name, &st) == 0 && 839 !S_ISDIR(st.st_mode) && 840 st.st_uid == uid) { 841 char tmp[MAXPATHLEN]; 842 843 sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 844 system(tmp); 845 } 846 } 847 closedir(d); 848 } 849} 850