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