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