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