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