1 /* $NetBSD: user.c,v 1.111 2006/06/27 12:39:23 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1999 Alistair G. Crooks. All rights reserved. 5 * Copyright (c) 2005 Liam J. Foy. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote 16 * products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #include <sys/cdefs.h> 32 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1999 \ 35 The NetBSD Foundation, Inc. All rights reserved."); 36 __RCSID("$NetBSD: user.c,v 1.111 2006/06/27 12:39:23 christos Exp $"); 37 #endif 38 39 #include <sys/types.h> 40 #include <sys/param.h> 41 #include <sys/stat.h> 42 43 #include <ctype.h> 44 #include <dirent.h> 45 #include <err.h> 46 #include <fcntl.h> 47 #include <grp.h> 48 #ifdef EXTENSIONS 49 #include <login_cap.h> 50 #endif 51 #include <paths.h> 52 #include <pwd.h> 53 #include <regex.h> 54 #include <stdarg.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <syslog.h> 59 #include <time.h> 60 #include <unistd.h> 61 #include <util.h> 62 #include <errno.h> 63 64 #include "defs.h" 65 #include "usermgmt.h" 66 67 68 /* this struct describes a uid range */ 69 typedef struct range_t { 70 int r_from; /* low uid */ 71 int r_to; /* high uid */ 72 } range_t; 73 74 /* this struct encapsulates the user information */ 75 typedef struct user_t { 76 int u_flags; /* see below */ 77 int u_uid; /* uid of user */ 78 char *u_password; /* encrypted password */ 79 char *u_comment; /* comment field */ 80 char *u_home; /* home directory */ 81 char *u_primgrp; /* primary group */ 82 int u_groupc; /* # of secondary groups */ 83 const char *u_groupv[NGROUPS_MAX]; /* secondary groups */ 84 char *u_shell; /* user's shell */ 85 char *u_basedir; /* base directory for home */ 86 char *u_expire; /* when password will expire */ 87 char *u_inactive; /* when account will expire */ 88 char *u_skeldir; /* directory for startup files */ 89 char *u_class; /* login class */ 90 unsigned u_rsize; /* size of range array */ 91 unsigned u_rc; /* # of ranges */ 92 range_t *u_rv; /* the ranges */ 93 unsigned u_defrc; /* # of ranges in defaults */ 94 int u_preserve; /* preserve uids on deletion */ 95 int u_allow_samba; /* allow trailing '$' for samba login names */ 96 int u_locked; /* user account lock */ 97 } user_t; 98 99 /* flags for which fields of the user_t replace the passwd entry */ 100 enum { 101 F_COMMENT = 0x0001, 102 F_DUPUID = 0x0002, 103 F_EXPIRE = 0x0004, 104 F_GROUP = 0x0008, 105 F_HOMEDIR = 0x0010, 106 F_MKDIR = 0x0020, 107 F_INACTIVE = 0x0040, 108 F_PASSWORD = 0x0080, 109 F_SECGROUP = 0x0100, 110 F_SHELL = 0x0200, 111 F_UID = 0x0400, 112 F_USERNAME = 0x0800, 113 F_CLASS = 0x1000 114 }; 115 116 #define UNLOCK 0 117 #define LOCK 1 118 #define LOCKED "*LOCKED*" 119 120 #define PATH_LOGINCONF "/etc/login.conf" 121 122 #ifndef DEF_GROUP 123 #define DEF_GROUP "users" 124 #endif 125 126 #ifndef DEF_BASEDIR 127 #define DEF_BASEDIR "/home" 128 #endif 129 130 #ifndef DEF_SKELDIR 131 #define DEF_SKELDIR "/etc/skel" 132 #endif 133 134 #ifndef DEF_SHELL 135 #define DEF_SHELL _PATH_CSHELL 136 #endif 137 138 #ifndef DEF_COMMENT 139 #define DEF_COMMENT "" 140 #endif 141 142 #ifndef DEF_LOWUID 143 #define DEF_LOWUID 1000 144 #endif 145 146 #ifndef DEF_HIGHUID 147 #define DEF_HIGHUID 60000 148 #endif 149 150 #ifndef DEF_INACTIVE 151 #define DEF_INACTIVE 0 152 #endif 153 154 #ifndef DEF_EXPIRE 155 #define DEF_EXPIRE NULL 156 #endif 157 158 #ifndef DEF_CLASS 159 #define DEF_CLASS "" 160 #endif 161 162 #ifndef WAITSECS 163 #define WAITSECS 10 164 #endif 165 166 #ifndef NOBODY_UID 167 #define NOBODY_UID 32767 168 #endif 169 170 /* some useful constants */ 171 enum { 172 MaxShellNameLen = 256, 173 MaxFileNameLen = MAXPATHLEN, 174 MaxUserNameLen = LOGIN_NAME_MAX - 1, 175 MaxCommandLen = 2048, 176 MaxEntryLen = 2048, 177 PasswordLength = 2048, 178 179 DES_Len = 13, 180 181 LowGid = DEF_LOWUID, 182 HighGid = DEF_HIGHUID 183 }; 184 185 /* Full paths of programs used here */ 186 #define CHMOD "/bin/chmod" 187 #define CHOWN "/usr/sbin/chown" 188 #define MKDIR "/bin/mkdir" 189 #define MV "/bin/mv" 190 #define NOLOGIN "/sbin/nologin" 191 #define PAX "/bin/pax" 192 #define RM "/bin/rm" 193 194 #define UNSET_INACTIVE "Null (unset)" 195 #define UNSET_EXPIRY "Null (unset)" 196 197 static int asystem(const char *fmt, ...) 198 __attribute__((__format__(__printf__, 1, 2))); 199 static int is_number(const char *); 200 static struct group *find_group_info(const char *); 201 static int verbose; 202 203 static char * 204 skipspace(char *s) 205 { 206 for (; *s && isspace((unsigned char)*s) ; s++) { 207 } 208 return s; 209 } 210 211 static int 212 check_numeric(const char *val, const char *name) 213 { 214 if (!is_number(val)) { 215 errx(EXIT_FAILURE, "When using [-%c %s], " 216 "the %s must be numeric", *name, name, name); 217 } 218 return atoi(val); 219 } 220 221 /* resize *cpp appropriately then assign `n' chars of `s' to it */ 222 static void 223 memsave(char **cpp, const char *s, size_t n) 224 { 225 RENEW(char, *cpp, n + 1, exit(1)); 226 (void)memcpy(*cpp, s, n); 227 (*cpp)[n] = '\0'; 228 } 229 230 /* a replacement for system(3) */ 231 static int 232 asystem(const char *fmt, ...) 233 { 234 va_list vp; 235 char buf[MaxCommandLen]; 236 int ret; 237 238 va_start(vp, fmt); 239 (void)vsnprintf(buf, sizeof(buf), fmt, vp); 240 va_end(vp); 241 if (verbose) { 242 (void)printf("Command: %s\n", buf); 243 } 244 if ((ret = system(buf)) != 0) { 245 warn("Error running `%s'", buf); 246 } 247 return ret; 248 } 249 250 /* remove a users home directory, returning 1 for success (ie, no problems encountered) */ 251 static int 252 removehomedir(struct passwd *pwp) 253 { 254 struct stat st; 255 256 /* userid not root? */ 257 if (pwp->pw_uid == 0) { 258 warnx("Not deleting home directory `%s'; userid is 0", pwp->pw_dir); 259 return 0; 260 } 261 262 /* directory exists (and is a directory!) */ 263 if (stat(pwp->pw_dir, &st) < 0) { 264 warn("Cannot access home directory `%s'", pwp->pw_dir); 265 return 0; 266 } 267 if (!S_ISDIR(st.st_mode)) { 268 warnx("Home directory `%s' is not a directory", pwp->pw_dir); 269 return 0; 270 } 271 272 /* userid matches directory owner? */ 273 if (st.st_uid != pwp->pw_uid) { 274 warnx("User `%s' doesn't own directory `%s', not removed", 275 pwp->pw_name, pwp->pw_dir); 276 return 0; 277 } 278 279 (void)seteuid(pwp->pw_uid); 280 /* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */ 281 (void)asystem("%s -rf %s > /dev/null 2>&1 || true", RM, pwp->pw_dir); 282 (void)seteuid(0); 283 if (rmdir(pwp->pw_dir) < 0) { 284 warn("Unable to remove all files in `%s'", pwp->pw_dir); 285 return 0; 286 } 287 return 1; 288 } 289 290 /* return 1 if all of `s' is numeric */ 291 static int 292 is_number(const char *s) 293 { 294 for ( ; *s ; s++) { 295 if (!isdigit((unsigned char) *s)) { 296 return 0; 297 } 298 } 299 return 1; 300 } 301 302 /* 303 * check that the effective uid is 0 - called from funcs which will 304 * modify data and config files. 305 */ 306 static void 307 checkeuid(void) 308 { 309 if (geteuid() != 0) { 310 errx(EXIT_FAILURE, "Program must be run as root"); 311 } 312 } 313 314 /* copy any dot files into the user's home directory */ 315 static int 316 copydotfiles(char *skeldir, int uid, int gid, char *dir) 317 { 318 struct dirent *dp; 319 DIR *dirp; 320 int n; 321 322 if ((dirp = opendir(skeldir)) == NULL) { 323 warn("Can't open source . files dir `%s'", skeldir); 324 return 0; 325 } 326 for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) { 327 if (strcmp(dp->d_name, ".") == 0 || 328 strcmp(dp->d_name, "..") == 0) { 329 continue; 330 } 331 n = 1; 332 } 333 (void)closedir(dirp); 334 if (n == 0) { 335 warnx("No \"dot\" initialisation files found"); 336 } else { 337 (void)asystem("cd %s && %s -rw -pe %s . %s", 338 skeldir, PAX, (verbose) ? "-v" : "", dir); 339 } 340 (void)asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir); 341 (void)asystem("%s -R u+w %s", CHMOD, dir); 342 return n; 343 } 344 345 /* create a group entry with gid `gid' */ 346 static int 347 creategid(char *group, int gid, const char *name) 348 { 349 struct stat st; 350 FILE *from; 351 FILE *to; 352 char buf[MaxEntryLen]; 353 char f[MaxFileNameLen]; 354 int fd; 355 int cc; 356 357 if (getgrnam(group) != NULL) { 358 warnx("Can't create group `%s': already exists", group); 359 return 0; 360 } 361 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 362 warn("Can't create group `%s': can't open `%s'", name, 363 _PATH_GROUP); 364 return 0; 365 } 366 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 367 warn("Can't lock `%s'", _PATH_GROUP); 368 (void)fclose(from); 369 return 0; 370 } 371 (void)fstat(fileno(from), &st); 372 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 373 if ((fd = mkstemp(f)) < 0) { 374 warn("Can't create group `%s': mkstemp failed", group); 375 (void)fclose(from); 376 return 0; 377 } 378 if ((to = fdopen(fd, "w")) == NULL) { 379 warn("Can't create group `%s': fdopen `%s' failed", 380 group, f); 381 (void)fclose(from); 382 (void)close(fd); 383 (void)unlink(f); 384 return 0; 385 } 386 while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) { 387 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 388 warn("Can't create group `%s': short write to `%s'", 389 group, f); 390 (void)fclose(from); 391 (void)close(fd); 392 (void)unlink(f); 393 return 0; 394 } 395 } 396 (void)fprintf(to, "%s:*:%d:%s\n", group, gid, name); 397 (void)fclose(from); 398 (void)fclose(to); 399 if (rename(f, _PATH_GROUP) < 0) { 400 warn("Can't create group `%s': can't rename `%s' to `%s'", 401 group, f, _PATH_GROUP); 402 (void)unlink(f); 403 return 0; 404 } 405 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 406 syslog(LOG_INFO, "New group added: name=%s, gid=%d", group, gid); 407 return 1; 408 } 409 410 /* modify the group entry with name `group' to be newent */ 411 static int 412 modify_gid(char *group, char *newent) 413 { 414 struct stat st; 415 FILE *from; 416 FILE *to; 417 char buf[MaxEntryLen]; 418 char f[MaxFileNameLen]; 419 char *colon; 420 int groupc; 421 int entc; 422 int fd; 423 int cc; 424 425 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 426 warn("Can't modify group `%s': can't open `%s'", 427 group, _PATH_GROUP); 428 return 0; 429 } 430 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 431 warn("Can't modify group `%s': can't lock `%s'", 432 group, _PATH_GROUP); 433 (void)fclose(from); 434 return 0; 435 } 436 (void)fstat(fileno(from), &st); 437 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 438 if ((fd = mkstemp(f)) < 0) { 439 warn("Can't modify group `%s': mkstemp failed", group); 440 (void)fclose(from); 441 return 0; 442 } 443 if ((to = fdopen(fd, "w")) == NULL) { 444 warn("Can't modify group `%s': fdopen `%s' failed", group, f); 445 (void)fclose(from); 446 (void)close(fd); 447 (void)unlink(f); 448 return 0; 449 } 450 groupc = strlen(group); 451 while (fgets(buf, sizeof(buf), from) != NULL) { 452 cc = strlen(buf); 453 if ((colon = strchr(buf, ':')) == NULL) { 454 warnx("Badly formed entry `%s'", buf); 455 continue; 456 } 457 entc = (int)(colon - buf); 458 if (entc == groupc && 459 strncmp(group, buf, (unsigned) entc) == 0) { 460 if (newent == NULL) { 461 struct group *grp_rm; 462 struct passwd *user_pwd; 463 464 /* 465 * Check that the group being removed 466 * isn't any user's Primary group. Just 467 * warn if it is. This could cause problems 468 * if the group GID was reused for a 469 * different purpose. 470 */ 471 472 grp_rm = find_group_info(group); 473 while ((user_pwd = getpwent()) != NULL) { 474 if (user_pwd->pw_gid == grp_rm->gr_gid) { 475 warnx("Warning: group `%s'(%d)" 476 " is the primary group of" 477 " `%s'. Use caution if you" 478 " later add this GID.", 479 grp_rm->gr_name, 480 grp_rm->gr_gid, user_pwd->pw_name); 481 } 482 } 483 endpwent(); 484 continue; 485 } else { 486 cc = strlen(newent); 487 (void)strlcpy(buf, newent, sizeof(buf)); 488 } 489 } 490 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 491 warn("Can't modify group `%s': short write to `%s'", 492 group, f); 493 (void)fclose(from); 494 (void)close(fd); 495 (void)unlink(f); 496 return 0; 497 } 498 } 499 (void)fclose(from); 500 (void)fclose(to); 501 if (rename(f, _PATH_GROUP) < 0) { 502 warn("Can't modify group `%s': can't rename `%s' to `%s'", 503 group, f, _PATH_GROUP); 504 (void)unlink(f); 505 return 0; 506 } 507 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 508 if (newent == NULL) { 509 syslog(LOG_INFO, "group deleted: name=%s", group); 510 } else { 511 syslog(LOG_INFO, "group information modified: name=%s", group); 512 } 513 return 1; 514 } 515 516 /* modify the group entries for all `groups', by adding `user' */ 517 static int 518 append_group(char *user, int ngroups, const char **groups) 519 { 520 struct group *grp; 521 struct stat st; 522 FILE *from; 523 FILE *to; 524 char buf[MaxEntryLen]; 525 char f[MaxFileNameLen]; 526 char *colon; 527 int groupc; 528 int entc; 529 int fd; 530 int nc; 531 int cc; 532 int i; 533 int j; 534 535 for (i = 0 ; i < ngroups ; i++) { 536 if ((grp = getgrnam(groups[i])) == NULL) { 537 warnx("Can't append group `%s' for user `%s'", 538 groups[i], user); 539 } else { 540 for (j = 0 ; grp->gr_mem[j] ; j++) { 541 if (strcmp(user, grp->gr_mem[j]) == 0) { 542 /* already in it */ 543 groups[i] = ""; 544 } 545 } 546 } 547 } 548 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 549 warn("Can't append group(s) for `%s': can't open `%s'", 550 user, _PATH_GROUP); 551 return 0; 552 } 553 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 554 warn("Can't append group(s) for `%s': can't lock `%s'", 555 user, _PATH_GROUP); 556 (void)fclose(from); 557 return 0; 558 } 559 (void)fstat(fileno(from), &st); 560 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 561 if ((fd = mkstemp(f)) < 0) { 562 warn("Can't append group(s) for `%s': mkstemp failed", 563 user); 564 (void)fclose(from); 565 return 0; 566 } 567 if ((to = fdopen(fd, "w")) == NULL) { 568 warn("Can't append group(s) for `%s': fdopen `%s' failed", 569 user, f); 570 (void)fclose(from); 571 (void)close(fd); 572 (void)unlink(f); 573 return 0; 574 } 575 while (fgets(buf, sizeof(buf), from) != NULL) { 576 cc = strlen(buf); 577 if ((colon = strchr(buf, ':')) == NULL) { 578 warnx("Badly formed entry `%s'", buf); 579 continue; 580 } 581 entc = (int)(colon - buf); 582 for (i = 0 ; i < ngroups ; i++) { 583 if ((groupc = strlen(groups[i])) == 0) { 584 continue; 585 } 586 if (entc == groupc && 587 strncmp(groups[i], buf, (unsigned) entc) == 0) { 588 if ((nc = snprintf(&buf[cc - 1], 589 sizeof(buf) - cc + 1, "%s%s\n", 590 (buf[cc - 2] == ':') ? "" : ",", user)) < 0) { 591 warnx("Warning: group `%s' " 592 "entry too long", groups[i]); 593 } 594 cc += nc - 1; 595 } 596 } 597 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 598 warn("Can't append group(s) for `%s':" 599 " short write to `%s'", user, f); 600 (void)fclose(from); 601 (void)close(fd); 602 (void)unlink(f); 603 return 0; 604 } 605 } 606 (void)fclose(from); 607 (void)fclose(to); 608 if (rename(f, _PATH_GROUP) < 0) { 609 warn("Can't append group(s) for `%s': " 610 "can't rename `%s' to `%s'", user, f, _PATH_GROUP); 611 (void)unlink(f); 612 return 0; 613 } 614 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 615 return 1; 616 } 617 618 /* the valid characters for login and group names */ 619 #define VALID_CHAR(c) (isalnum(c) || (c) == '.' || (c) == '_' || (c) == '-') 620 621 /* return 1 if `login' is a valid login name */ 622 static int 623 valid_login(char *login_name, int allow_samba) 624 { 625 unsigned char *cp; 626 627 /* First character of a login name cannot be '-'. */ 628 if (*login_name == '-') { 629 return 0; 630 } 631 if (strlen(login_name) >= LOGIN_NAME_MAX) { 632 return 0; 633 } 634 for (cp = (unsigned char *)login_name ; *cp ; cp++) { 635 if (!VALID_CHAR(*cp)) { 636 #ifdef EXTENSIONS 637 /* check for a trailing '$' in a Samba user name */ 638 if (allow_samba && *cp == '$' && *(cp + 1) == 0x0) { 639 return 1; 640 } 641 #endif 642 return 0; 643 } 644 } 645 return 1; 646 } 647 648 /* return 1 if `group' is a valid group name */ 649 static int 650 valid_group(char *group) 651 { 652 unsigned char *cp; 653 654 for (cp = (unsigned char *)group; *cp; cp++) { 655 if (!VALID_CHAR(*cp)) { 656 return 0; 657 } 658 } 659 return 1; 660 } 661 662 /* find the next gid in the range lo .. hi */ 663 static int 664 getnextgid(int *gidp, int lo, int hi) 665 { 666 for (*gidp = lo ; *gidp < hi ; *gidp += 1) { 667 if (getgrgid((gid_t)*gidp) == NULL) { 668 return 1; 669 } 670 } 671 return 0; 672 } 673 674 #ifdef EXTENSIONS 675 /* save a range of uids */ 676 static int 677 save_range(user_t *up, char *cp) 678 { 679 int from; 680 int to; 681 int i; 682 683 if (up->u_rsize == 0) { 684 up->u_rsize = 32; 685 NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0)); 686 } else if (up->u_rc == up->u_rsize) { 687 up->u_rsize *= 2; 688 RENEW(range_t, up->u_rv, up->u_rsize, return(0)); 689 } 690 if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) { 691 for (i = up->u_defrc ; i < up->u_rc ; i++) { 692 if (up->u_rv[i].r_from == from && 693 up->u_rv[i].r_to == to) { 694 break; 695 } 696 } 697 if (i == up->u_rc) { 698 up->u_rv[up->u_rc].r_from = from; 699 up->u_rv[up->u_rc].r_to = to; 700 up->u_rc += 1; 701 } 702 } else { 703 warnx("Bad range `%s'", cp); 704 return 0; 705 } 706 return 1; 707 } 708 #endif 709 710 /* set the defaults in the defaults file */ 711 static int 712 setdefaults(user_t *up) 713 { 714 char template[MaxFileNameLen]; 715 FILE *fp; 716 int ret; 717 int fd; 718 #ifdef EXTENSIONS 719 int i; 720 #endif 721 722 (void)snprintf(template, sizeof(template), "%s.XXXXXX", 723 _PATH_USERMGMT_CONF); 724 if ((fd = mkstemp(template)) < 0) { 725 warn("Can't set defaults: can't mkstemp `%s' for writing", 726 _PATH_USERMGMT_CONF); 727 return 0; 728 } 729 if ((fp = fdopen(fd, "w")) == NULL) { 730 warn("Can't set defaults: can't fdopen `%s' for writing", 731 _PATH_USERMGMT_CONF); 732 return 0; 733 } 734 ret = 1; 735 if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 || 736 fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 || 737 fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 || 738 fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 || 739 #ifdef EXTENSIONS 740 fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 || 741 #endif 742 fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ? 743 UNSET_INACTIVE : up->u_inactive) <= 0 || 744 fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ? 745 UNSET_EXPIRY : up->u_expire) <= 0 || 746 fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? 747 "false" : "true") <= 0) { 748 warn("Can't write to `%s'", _PATH_USERMGMT_CONF); 749 ret = 0; 750 } 751 #ifdef EXTENSIONS 752 for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0; 753 i < up->u_rc ; i++) { 754 if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, 755 up->u_rv[i].r_to) <= 0) { 756 warn("Can't set defaults: can't write to `%s'", 757 _PATH_USERMGMT_CONF); 758 ret = 0; 759 } 760 } 761 #endif 762 (void)fclose(fp); 763 if (ret) { 764 ret = ((rename(template, _PATH_USERMGMT_CONF) == 0) && 765 (chmod(_PATH_USERMGMT_CONF, 0644) == 0)); 766 } 767 return ret; 768 } 769 770 /* read the defaults file */ 771 static void 772 read_defaults(user_t *up) 773 { 774 struct stat st; 775 size_t lineno; 776 size_t len; 777 FILE *fp; 778 char *cp; 779 char *s; 780 781 memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP)); 782 memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR)); 783 memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR)); 784 memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL)); 785 memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT)); 786 #ifdef EXTENSIONS 787 memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS)); 788 #endif 789 up->u_rsize = 16; 790 up->u_defrc = 0; 791 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1)); 792 up->u_inactive = DEF_INACTIVE; 793 up->u_expire = DEF_EXPIRE; 794 if ((fp = fopen(_PATH_USERMGMT_CONF, "r")) == NULL) { 795 if (stat(_PATH_USERMGMT_CONF, &st) < 0 && !setdefaults(up)) { 796 warn("Can't create `%s' defaults file", 797 _PATH_USERMGMT_CONF); 798 } 799 fp = fopen(_PATH_USERMGMT_CONF, "r"); 800 } 801 if (fp != NULL) { 802 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) { 803 if (strncmp(s, "group", 5) == 0) { 804 cp = skipspace(s + 5); 805 memsave(&up->u_primgrp, (char *)cp, strlen(cp)); 806 } else if (strncmp(s, "base_dir", 8) == 0) { 807 cp = skipspace(s + 8); 808 memsave(&up->u_basedir, (char *)cp, strlen(cp)); 809 } else if (strncmp(s, "skel_dir", 8) == 0) { 810 cp = skipspace(s + 8); 811 memsave(&up->u_skeldir, (char *)cp, strlen(cp)); 812 } else if (strncmp(s, "shell", 5) == 0) { 813 cp = skipspace(s + 5); 814 memsave(&up->u_shell, cp, strlen(cp)); 815 #ifdef EXTENSIONS 816 } else if (strncmp((char *)s, "class", 5) == 0) { 817 cp = skipspace(s + 5); 818 memsave(&up->u_class, cp, strlen(cp)); 819 #endif 820 } else if (strncmp(s, "inactive", 8) == 0) { 821 cp = skipspace(s + 8); 822 if (strcmp(cp, UNSET_INACTIVE) == 0) { 823 if (up->u_inactive) { 824 FREE(up->u_inactive); 825 } 826 up->u_inactive = NULL; 827 } else { 828 memsave(&up->u_inactive, cp, strlen(cp)); 829 } 830 #ifdef EXTENSIONS 831 } else if (strncmp(s, "range", 5) == 0) { 832 cp = skipspace(s + 5); 833 (void)save_range(up, cp); 834 #endif 835 #ifdef EXTENSIONS 836 } else if (strncmp(s, "preserve", 8) == 0) { 837 cp = skipspace(s + 8); 838 up->u_preserve = 839 (strncmp(cp, "true", 4) == 0) ? 1 : 840 (strncmp(cp, "yes", 3) == 0) ? 1 : atoi(cp); 841 #endif 842 } else if (strncmp(s, "expire", 6) == 0) { 843 cp = skipspace(s + 6); 844 if (strcmp(cp, UNSET_EXPIRY) == 0) { 845 if (up->u_expire) { 846 FREE(up->u_expire); 847 } 848 up->u_expire = NULL; 849 } else { 850 memsave(&up->u_expire, cp, strlen(cp)); 851 } 852 } 853 (void)free(s); 854 } 855 (void)fclose(fp); 856 } 857 if (up->u_rc == 0) { 858 up->u_rv[up->u_rc].r_from = DEF_LOWUID; 859 up->u_rv[up->u_rc].r_to = DEF_HIGHUID; 860 up->u_rc += 1; 861 } 862 up->u_defrc = up->u_rc; 863 } 864 865 /* return the next valid unused uid */ 866 static int 867 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid) 868 { 869 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) { 870 if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) { 871 if (sync_uid_gid) { 872 if (getgrgid((gid_t)(*uid)) == NULL) { 873 return 1; 874 } 875 } else { 876 return 1; 877 } 878 } 879 } 880 return 0; 881 } 882 883 /* structure which defines a password type */ 884 typedef struct passwd_type_t { 885 const char *type; /* optional type descriptor */ 886 size_t desc_length; /* length of type descriptor */ 887 size_t length; /* length of password */ 888 const char *regex; /* regexp to output the password */ 889 size_t re_sub; /* subscript of regexp to use */ 890 } passwd_type_t; 891 892 static passwd_type_t passwd_types[] = { 893 { "$sha1", 5, 28, "\\$[^$]+\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* SHA1 */ 894 { "$2a", 3, 54, "\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* Blowfish */ 895 { "$1", 2, 34, NULL, 0 }, /* MD5 */ 896 { "", 0, DES_Len,NULL, 0 }, /* standard DES */ 897 { NULL, (size_t)~0, (size_t)~0, NULL, 0 } 898 /* none - terminate search */ 899 }; 900 901 /* return non-zero if it's a valid password - check length for cipher type */ 902 static int 903 valid_password_length(char *newpasswd) 904 { 905 passwd_type_t *pwtp; 906 regmatch_t matchv[10]; 907 regex_t r; 908 909 for (pwtp = passwd_types; pwtp->desc_length != (size_t)~0; pwtp++) { 910 if (strncmp(newpasswd, pwtp->type, pwtp->desc_length) == 0) { 911 if (pwtp->regex == NULL) { 912 return strlen(newpasswd) == pwtp->length; 913 } 914 (void)regcomp(&r, pwtp->regex, REG_EXTENDED); 915 if (regexec(&r, newpasswd, 10, matchv, 0) == 0) { 916 regfree(&r); 917 return (int)(matchv[pwtp->re_sub].rm_eo - 918 matchv[pwtp->re_sub].rm_so + 1) == 919 pwtp->length; 920 } 921 regfree(&r); 922 } 923 } 924 return 0; 925 } 926 927 #ifdef EXTENSIONS 928 /* return 1 if `class' is a valid login class */ 929 static int 930 valid_class(char *class) 931 { 932 login_cap_t *lc; 933 934 if (class == NULL || *class == '\0') { 935 return 1; 936 } 937 /* 938 * Check if /etc/login.conf exists. login_getclass() will 939 * return 1 due to it not existing, so not informing the 940 * user the actual login class does not exist. 941 */ 942 943 if (access(PATH_LOGINCONF, R_OK) == -1) { 944 warn("Access failed for `%s'; will not validate class `%s'", 945 PATH_LOGINCONF, class); 946 return 1; 947 } 948 949 if ((lc = login_getclass(class)) != NULL) { 950 login_close(lc); 951 return 1; 952 } 953 return 0; 954 } 955 956 /* return 1 if the `shellname' is a valid user shell */ 957 static int 958 valid_shell(const char *shellname) 959 { 960 char *shellp; 961 962 if (access(_PATH_SHELLS, R_OK) == -1) { 963 /* Don't exit */ 964 warn("Access failed for `%s'; will not validate shell `%s'", 965 _PATH_SHELLS, shellname); 966 return 1; 967 } 968 969 /* if nologin is used as a shell, consider it a valid shell */ 970 if (strcmp(shellname, NOLOGIN) == 0) 971 return 1; 972 973 while ((shellp = getusershell()) != NULL) 974 if (strcmp(shellp, shellname) == 0) 975 return 1; 976 977 warnx("Shell `%s' not found in `%s'", shellname, _PATH_SHELLS); 978 979 return access(shellname, X_OK) != -1; 980 } 981 #endif 982 983 /* look for a valid time, return 0 if it was specified but bad */ 984 static int 985 scantime(time_t *tp, char *s) 986 { 987 struct tm tm; 988 char *ep; 989 long val; 990 991 *tp = 0; 992 if (s != NULL) { 993 (void)memset(&tm, 0, sizeof(tm)); 994 if (strptime(s, "%c", &tm) != NULL) { 995 *tp = mktime(&tm); 996 return (*tp == -1) ? 0 : 1; 997 } else if (strptime(s, "%B %d %Y", &tm) != NULL) { 998 *tp = mktime(&tm); 999 return (*tp == -1) ? 0 : 1; 1000 } else { 1001 errno = 0; 1002 *tp = val = strtol(s, &ep, 10); 1003 if (*ep != '\0' || *tp < -1 || errno == ERANGE) { 1004 *tp = 0; 1005 return 0; 1006 } 1007 if (*tp != val) { 1008 return 0; 1009 } 1010 } 1011 } 1012 return 1; 1013 } 1014 1015 /* add a user */ 1016 static int 1017 adduser(char *login_name, user_t *up) 1018 { 1019 struct group *grp; 1020 struct stat st; 1021 time_t expire; 1022 time_t inactive; 1023 char password[PasswordLength + 1]; 1024 char home[MaxFileNameLen]; 1025 char buf[MaxFileNameLen]; 1026 int sync_uid_gid; 1027 int masterfd; 1028 int ptmpfd; 1029 int gid; 1030 int cc; 1031 int i; 1032 1033 if (!valid_login(login_name, up->u_allow_samba)) { 1034 errx(EXIT_FAILURE, "Can't add user `%s': invalid login name", login_name); 1035 } 1036 #ifdef EXTENSIONS 1037 if (!valid_class(up->u_class)) { 1038 errx(EXIT_FAILURE, "Can't add user `%s': no such login class `%s'", 1039 login_name, up->u_class); 1040 } 1041 #endif 1042 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 1043 err(EXIT_FAILURE, "Can't add user `%s': can't open `%s'", 1044 login_name, _PATH_MASTERPASSWD); 1045 } 1046 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 1047 err(EXIT_FAILURE, "Can't add user `%s': can't lock `%s'", 1048 login_name, _PATH_MASTERPASSWD); 1049 } 1050 pw_init(); 1051 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 1052 int serrno = errno; 1053 (void)close(masterfd); 1054 errno = serrno; 1055 err(EXIT_FAILURE, "Can't add user `%s': can't obtain pw_lock", 1056 login_name); 1057 } 1058 while ((cc = read(masterfd, buf, sizeof(buf))) > 0) { 1059 if (write(ptmpfd, buf, (size_t)(cc)) != cc) { 1060 int serrno = errno; 1061 (void)close(masterfd); 1062 (void)close(ptmpfd); 1063 (void)pw_abort(); 1064 errno = serrno; 1065 err(EXIT_FAILURE, "Can't add user `%s': " 1066 "short write to /etc/ptmp", login_name); 1067 } 1068 } 1069 /* if no uid was specified, get next one in [low_uid..high_uid] range */ 1070 sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0); 1071 if (up->u_uid == -1) { 1072 int got_id = 0; 1073 1074 /* 1075 * Look for a free UID in the command line ranges (if any). 1076 * These start after the ranges specified in the config file. 1077 */ 1078 for (i = up->u_defrc; !got_id && i < up->u_rc ; i++) { 1079 got_id = getnextuid(sync_uid_gid, &up->u_uid, 1080 up->u_rv[i].r_from, up->u_rv[i].r_to); 1081 } 1082 /* 1083 * If there were no free UIDs in the command line ranges, 1084 * try the ranges from the config file (there will always 1085 * be at least one default). 1086 */ 1087 for (i = 0; !got_id && i < up->u_defrc; i++) { 1088 got_id = getnextuid(sync_uid_gid, &up->u_uid, 1089 up->u_rv[i].r_from, up->u_rv[i].r_to); 1090 } 1091 if (!got_id) { 1092 (void)close(ptmpfd); 1093 (void)pw_abort(); 1094 errx(EXIT_FAILURE, "Can't add user `%s': " 1095 "can't get next uid for %d", login_name, 1096 up->u_uid); 1097 } 1098 } 1099 /* check uid isn't already allocated */ 1100 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) { 1101 (void)close(ptmpfd); 1102 (void)pw_abort(); 1103 errx(EXIT_FAILURE, "Can't add user `%s': " 1104 "uid %d is already in use", login_name, up->u_uid); 1105 } 1106 /* if -g=uid was specified, check gid is unused */ 1107 if (sync_uid_gid) { 1108 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 1109 (void)close(ptmpfd); 1110 (void)pw_abort(); 1111 errx(EXIT_FAILURE, "Can't add user `%s': " 1112 "gid %d is already in use", login_name, 1113 up->u_uid); 1114 } 1115 gid = up->u_uid; 1116 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 1117 gid = grp->gr_gid; 1118 } else if (is_number(up->u_primgrp) && 1119 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) { 1120 gid = grp->gr_gid; 1121 } else { 1122 (void)close(ptmpfd); 1123 (void)pw_abort(); 1124 errx(EXIT_FAILURE, "Can't add user `%s': group %s not found", 1125 login_name, up->u_primgrp); 1126 } 1127 /* check name isn't already in use */ 1128 if (!(up->u_flags & F_DUPUID) && getpwnam(login_name) != NULL) { 1129 (void)close(ptmpfd); 1130 (void)pw_abort(); 1131 errx(EXIT_FAILURE, "Can't add user `%s': " 1132 "`%s' is already a user", login_name, login_name); 1133 } 1134 if (up->u_flags & F_HOMEDIR) { 1135 (void)strlcpy(home, up->u_home, sizeof(home)); 1136 } else { 1137 /* if home directory hasn't been given, make it up */ 1138 (void)snprintf(home, sizeof(home), "%s/%s", up->u_basedir, 1139 login_name); 1140 } 1141 if (up->u_flags & F_SHELL) { 1142 #ifdef EXTENSIONS 1143 if (!valid_shell(up->u_shell)) { 1144 int oerrno = errno; 1145 (void)close(ptmpfd); 1146 (void)pw_abort(); 1147 errno = oerrno; 1148 errx(EXIT_FAILURE, "Can't add user `%s': " 1149 "Cannot access shell `%s'", 1150 login_name, up->u_shell); 1151 } 1152 #endif 1153 } 1154 1155 if (!scantime(&inactive, up->u_inactive)) { 1156 warnx("Warning: inactive time `%s' invalid, password expiry off", 1157 up->u_inactive); 1158 } 1159 if (!scantime(&expire, up->u_expire) || expire == -1) { 1160 warnx("Warning: expire time `%s' invalid, account expiry off", 1161 up->u_expire); 1162 expire = 0; /* Just in case. */ 1163 } 1164 if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) { 1165 warnx("Warning: home directory `%s' doesn't exist, " 1166 "and -m was not specified", home); 1167 } 1168 password[sizeof(password) - 1] = '\0'; 1169 if (up->u_password != NULL && valid_password_length(up->u_password)) { 1170 (void)strlcpy(password, up->u_password, sizeof(password)); 1171 } else { 1172 (void)memset(password, '*', DES_Len); 1173 password[DES_Len] = 0; 1174 if (up->u_password != NULL) { 1175 warnx("Password `%s' is invalid: setting it to `%s'", 1176 up->u_password, password); 1177 } 1178 } 1179 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", 1180 login_name, 1181 password, 1182 up->u_uid, 1183 gid, 1184 #ifdef EXTENSIONS 1185 up->u_class, 1186 #else 1187 "", 1188 #endif 1189 (long) inactive, 1190 (long) expire, 1191 up->u_comment, 1192 home, 1193 up->u_shell); 1194 if (write(ptmpfd, buf, (size_t) cc) != cc) { 1195 int serrno = errno; 1196 (void)close(ptmpfd); 1197 (void)pw_abort(); 1198 errno = serrno; 1199 err(EXIT_FAILURE, "Can't add user `%s': write failed", 1200 login_name); 1201 } 1202 if (up->u_flags & F_MKDIR) { 1203 if (lstat(home, &st) == 0) { 1204 (void)close(ptmpfd); 1205 (void)pw_abort(); 1206 errx(EXIT_FAILURE, 1207 "Can't add user `%s': home directory `%s' " 1208 "already exists", login_name, home); 1209 } else { 1210 if (asystem("%s -p %s", MKDIR, home) != 0) { 1211 (void)close(ptmpfd); 1212 (void)pw_abort(); 1213 errx(EXIT_FAILURE, "Can't add user `%s': " 1214 "can't mkdir `%s'", login_name, home); 1215 } 1216 (void)copydotfiles(up->u_skeldir, up->u_uid, gid, home); 1217 } 1218 } 1219 if (strcmp(up->u_primgrp, "=uid") == 0 && 1220 getgrnam(login_name) == NULL && 1221 !creategid(login_name, gid, login_name)) { 1222 (void)close(ptmpfd); 1223 (void)pw_abort(); 1224 errx(EXIT_FAILURE, "Can't add user `%s': can't create gid %d ", 1225 login_name, gid); 1226 } 1227 if (up->u_groupc > 0 && 1228 !append_group(login_name, up->u_groupc, up->u_groupv)) { 1229 (void)close(ptmpfd); 1230 (void)pw_abort(); 1231 errx(EXIT_FAILURE, "Can't add user `%s': can't append " 1232 "to new groups", login_name); 1233 } 1234 (void)close(ptmpfd); 1235 #if PW_MKDB_ARGC == 2 1236 if (pw_mkdb(login_name, 0) < 0) 1237 #else 1238 if (pw_mkdb() < 0) 1239 #endif 1240 { 1241 (void)pw_abort(); 1242 errx(EXIT_FAILURE, "Can't add user `%s': pw_mkdb failed", 1243 login_name); 1244 } 1245 syslog(LOG_INFO, "New user added: name=%s, uid=%d, gid=%d, home=%s, " 1246 "shell=%s", login_name, up->u_uid, gid, home, up->u_shell); 1247 return 1; 1248 } 1249 1250 /* remove a user from the groups file */ 1251 static int 1252 rm_user_from_groups(char *login_name) 1253 { 1254 struct stat st; 1255 regmatch_t matchv[10]; 1256 regex_t r; 1257 FILE *from; 1258 FILE *to; 1259 char line[MaxEntryLen]; 1260 char buf[MaxEntryLen]; 1261 char f[MaxFileNameLen]; 1262 int fd; 1263 int cc; 1264 int sc; 1265 1266 (void)snprintf(line, sizeof(line), "(:|,)(%s)(,|$)", login_name); 1267 if ((sc = regcomp(&r, line, REG_EXTENDED|REG_NEWLINE)) != 0) { 1268 (void)regerror(sc, &r, buf, sizeof(buf)); 1269 warnx("Can't compile regular expression `%s' (%s)", line, 1270 buf); 1271 return 0; 1272 } 1273 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 1274 warn("Can't remove user `%s' from `%s': can't open `%s'", 1275 login_name, _PATH_GROUP, _PATH_GROUP); 1276 return 0; 1277 } 1278 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 1279 warn("Can't remove user `%s' from `%s': can't lock `%s'", 1280 login_name, _PATH_GROUP, _PATH_GROUP); 1281 (void)fclose(from); 1282 return 0; 1283 } 1284 (void)fstat(fileno(from), &st); 1285 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 1286 if ((fd = mkstemp(f)) < 0) { 1287 warn("Can't remove user `%s' from `%s': mkstemp failed", 1288 login_name, _PATH_GROUP); 1289 (void)fclose(from); 1290 return 0; 1291 } 1292 if ((to = fdopen(fd, "w")) == NULL) { 1293 warn("Can't remove user `%s' from `%s': fdopen `%s' failed", 1294 login_name, _PATH_GROUP, f); 1295 (void)fclose(from); 1296 (void)close(fd); 1297 (void)unlink(f); 1298 return 0; 1299 } 1300 while (fgets(buf, sizeof(buf), from) != NULL) { 1301 cc = strlen(buf); 1302 if (regexec(&r, buf, 10, matchv, 0) == 0) { 1303 if (buf[(int)matchv[1].rm_so] == ',') { 1304 matchv[2].rm_so = matchv[1].rm_so; 1305 } else if (matchv[2].rm_eo != matchv[3].rm_eo) { 1306 matchv[2].rm_eo = matchv[3].rm_eo; 1307 } 1308 cc -= (int) matchv[2].rm_eo; 1309 sc = (int) matchv[2].rm_so; 1310 if (fwrite(buf, sizeof(char), (size_t)sc, to) != sc || 1311 fwrite(&buf[(int)matchv[2].rm_eo], sizeof(char), 1312 (size_t)cc, to) != cc) { 1313 warn("Can't remove user `%s' from `%s': " 1314 "short write to `%s'", login_name, 1315 _PATH_GROUP, f); 1316 (void)fclose(from); 1317 (void)close(fd); 1318 (void)unlink(f); 1319 return 0; 1320 } 1321 } else if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 1322 warn("Can't remove user `%s' from `%s': " 1323 "short write to `%s'", login_name, _PATH_GROUP, f); 1324 (void)fclose(from); 1325 (void)close(fd); 1326 (void)unlink(f); 1327 return 0; 1328 } 1329 } 1330 (void)fclose(from); 1331 (void)fclose(to); 1332 if (rename(f, _PATH_GROUP) < 0) { 1333 warn("Can't remove user `%s' from `%s': " 1334 "can't rename `%s' to `%s'", 1335 login_name, _PATH_GROUP, f, _PATH_GROUP); 1336 (void)unlink(f); 1337 return 0; 1338 } 1339 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 1340 return 1; 1341 } 1342 1343 /* check that the user or group is local, not from YP/NIS */ 1344 static int 1345 is_local(char *name, const char *file) 1346 { 1347 FILE *fp; 1348 char buf[MaxEntryLen]; 1349 size_t len; 1350 int ret; 1351 1352 if ((fp = fopen(file, "r")) == NULL) { 1353 err(EXIT_FAILURE, "Can't open `%s'", file); 1354 } 1355 len = strlen(name); 1356 for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) { 1357 if (strncmp(buf, name, len) == 0 && buf[len] == ':') { 1358 ret = 1; 1359 break; 1360 } 1361 } 1362 (void)fclose(fp); 1363 return ret; 1364 } 1365 1366 /* modify a user */ 1367 static int 1368 moduser(char *login_name, char *newlogin, user_t *up, int allow_samba) 1369 { 1370 struct passwd *pwp; 1371 struct group *grp; 1372 const char *homedir; 1373 char *locked_pwd; 1374 size_t colonc; 1375 size_t loginc; 1376 size_t len; 1377 FILE *master; 1378 char newdir[MaxFileNameLen]; 1379 char buf[MaxEntryLen]; 1380 char *colon; 1381 int masterfd; 1382 int ptmpfd; 1383 int error; 1384 1385 if (!valid_login(newlogin, allow_samba)) { 1386 errx(EXIT_FAILURE, "Can't modify user `%s': invalid login name", 1387 login_name); 1388 } 1389 if ((pwp = getpwnam(login_name)) == NULL) { 1390 errx(EXIT_FAILURE, "Can't modify user `%s': no such user", 1391 login_name); 1392 } 1393 if (!is_local(login_name, _PATH_MASTERPASSWD)) { 1394 errx(EXIT_FAILURE, "Can't modify user `%s': must be a local user", 1395 login_name); 1396 } 1397 /* keep dir name in case we need it for '-m' */ 1398 homedir = pwp->pw_dir; 1399 1400 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 1401 err(EXIT_FAILURE, "Can't modify user `%s': can't open `%s'", 1402 login_name, _PATH_MASTERPASSWD); 1403 } 1404 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 1405 err(EXIT_FAILURE, "Can't modify user `%s': can't lock `%s'", 1406 login_name, _PATH_MASTERPASSWD); 1407 } 1408 pw_init(); 1409 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 1410 int serrno = errno; 1411 (void)close(masterfd); 1412 errno = serrno; 1413 err(EXIT_FAILURE, "Can't modify user `%s': " 1414 "can't obtain pw_lock", login_name); 1415 } 1416 if ((master = fdopen(masterfd, "r")) == NULL) { 1417 int serrno = errno; 1418 (void)close(masterfd); 1419 (void)close(ptmpfd); 1420 (void)pw_abort(); 1421 errno = serrno; 1422 err(EXIT_FAILURE, "Can't modify user `%s': " 1423 "fdopen fd for %s", login_name, _PATH_MASTERPASSWD); 1424 } 1425 if (up != NULL) { 1426 if (up->u_flags & F_USERNAME) { 1427 /* 1428 * If changing name, 1429 * check new name isn't already in use 1430 */ 1431 if (strcmp(login_name, newlogin) != 0 && 1432 getpwnam(newlogin) != NULL) { 1433 (void)close(ptmpfd); 1434 (void)pw_abort(); 1435 errx(EXIT_FAILURE, "Can't modify user `%s': " 1436 "`%s' is already a user", login_name, 1437 newlogin); 1438 } 1439 pwp->pw_name = newlogin; 1440 1441 /* 1442 * Provide a new directory name in case the 1443 * home directory is to be moved. 1444 */ 1445 if (up->u_flags & F_MKDIR) { 1446 (void)snprintf(newdir, sizeof(newdir), "%s/%s", 1447 up->u_basedir, newlogin); 1448 pwp->pw_dir = newdir; 1449 } 1450 } 1451 if (up->u_flags & F_PASSWORD) { 1452 if (up->u_password != NULL) { 1453 if (!valid_password_length(up->u_password)) { 1454 (void)close(ptmpfd); 1455 (void)pw_abort(); 1456 errx(EXIT_FAILURE, 1457 "Can't modify user `%s': " 1458 "invalid password: `%s'", 1459 login_name, up->u_password); 1460 } 1461 if ((locked_pwd = 1462 strstr(pwp->pw_passwd, LOCKED)) != NULL) { 1463 /* 1464 * account is locked - keep it locked 1465 * and just change the password. 1466 */ 1467 if (asprintf(&locked_pwd, "%s%s", 1468 LOCKED, up->u_password) == -1) { 1469 (void)close(ptmpfd); 1470 (void)pw_abort(); 1471 err(EXIT_FAILURE, 1472 "Can't modify user `%s': " 1473 "asprintf failed", 1474 login_name); 1475 } 1476 pwp->pw_passwd = locked_pwd; 1477 } else { 1478 pwp->pw_passwd = up->u_password; 1479 } 1480 } 1481 } 1482 1483 /* check whether we should lock the account. */ 1484 if (up->u_locked == LOCK) { 1485 /* check to see account if already locked. */ 1486 if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED)) 1487 != NULL) { 1488 warnx("Account is already locked"); 1489 } else { 1490 if (asprintf(&locked_pwd, "%s%s", LOCKED, 1491 pwp->pw_passwd) == -1) { 1492 (void)close(ptmpfd); 1493 (void)pw_abort(); 1494 err(EXIT_FAILURE, 1495 "Can't modify user `%s': " 1496 "asprintf failed", login_name); 1497 } 1498 pwp->pw_passwd = locked_pwd; 1499 } 1500 } else if (up->u_locked == UNLOCK) { 1501 if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED)) 1502 == NULL) { 1503 warnx("Can't modify user `%s': " 1504 "account is not locked", login_name); 1505 } else { 1506 pwp->pw_passwd = locked_pwd + strlen(LOCKED); 1507 } 1508 } 1509 1510 if (up->u_flags & F_UID) { 1511 /* check uid isn't already allocated */ 1512 if (!(up->u_flags & F_DUPUID) && 1513 getpwuid((uid_t)(up->u_uid)) != NULL) { 1514 (void)close(ptmpfd); 1515 (void)pw_abort(); 1516 errx(EXIT_FAILURE, "Can't modify user `%s': " 1517 "uid `%d' is already in use", login_name, 1518 up->u_uid); 1519 } 1520 pwp->pw_uid = up->u_uid; 1521 } 1522 if (up->u_flags & F_GROUP) { 1523 /* if -g=uid was specified, check gid is unused */ 1524 if (strcmp(up->u_primgrp, "=uid") == 0) { 1525 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 1526 (void)close(ptmpfd); 1527 (void)pw_abort(); 1528 errx(EXIT_FAILURE, 1529 "Can't modify user `%s': " 1530 "gid %d is already in use", 1531 login_name, up->u_uid); 1532 } 1533 pwp->pw_gid = up->u_uid; 1534 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 1535 pwp->pw_gid = grp->gr_gid; 1536 } else if (is_number(up->u_primgrp) && 1537 (grp = getgrgid( 1538 (gid_t)atoi(up->u_primgrp))) != NULL) { 1539 pwp->pw_gid = grp->gr_gid; 1540 } else { 1541 (void)close(ptmpfd); 1542 (void)pw_abort(); 1543 errx(EXIT_FAILURE, "Can't modify user `%s': " 1544 "group %s not found", login_name, 1545 up->u_primgrp); 1546 } 1547 } 1548 if (up->u_flags & F_INACTIVE) { 1549 if (!scantime(&pwp->pw_change, up->u_inactive)) { 1550 warnx("Warning: inactive time `%s' invalid, " 1551 "password expiry off", 1552 up->u_inactive); 1553 } 1554 } 1555 if (up->u_flags & F_EXPIRE) { 1556 if (!scantime(&pwp->pw_expire, up->u_expire) || 1557 pwp->pw_expire == -1) { 1558 warnx("Warning: expire time `%s' invalid, " 1559 "account expiry off", 1560 up->u_expire); 1561 pwp->pw_expire = 0; 1562 } 1563 } 1564 if (up->u_flags & F_COMMENT) { 1565 pwp->pw_gecos = up->u_comment; 1566 } 1567 if (up->u_flags & F_HOMEDIR) { 1568 pwp->pw_dir = up->u_home; 1569 } 1570 if (up->u_flags & F_SHELL) { 1571 #ifdef EXTENSIONS 1572 if (!valid_shell(up->u_shell)) { 1573 int oerrno = errno; 1574 (void)close(ptmpfd); 1575 (void)pw_abort(); 1576 errno = oerrno; 1577 errx(EXIT_FAILURE, "Can't modify user `%s': " 1578 "Cannot access shell `%s'", 1579 login_name, up->u_shell); 1580 } 1581 pwp->pw_shell = up->u_shell; 1582 #else 1583 pwp->pw_shell = up->u_shell; 1584 #endif 1585 } 1586 #ifdef EXTENSIONS 1587 if (up->u_flags & F_CLASS) { 1588 if (!valid_class(up->u_class)) { 1589 (void)close(ptmpfd); 1590 (void)pw_abort(); 1591 errx(EXIT_FAILURE, "Can't modify user `%s': " 1592 "no such login class `%s'", login_name, 1593 up->u_class); 1594 } 1595 pwp->pw_class = up->u_class; 1596 } 1597 #endif 1598 } 1599 loginc = strlen(login_name); 1600 while (fgets(buf, sizeof(buf), master) != NULL) { 1601 if ((colon = strchr(buf, ':')) == NULL) { 1602 warnx("Malformed entry `%s'. Skipping", buf); 1603 continue; 1604 } 1605 colonc = (size_t)(colon - buf); 1606 if (strncmp(login_name, buf, loginc) == 0 && loginc == colonc) { 1607 if (up != NULL) { 1608 len = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:" 1609 #ifdef EXTENSIONS 1610 "%s" 1611 #endif 1612 ":%ld:%ld:%s:%s:%s\n", 1613 newlogin, 1614 pwp->pw_passwd, 1615 pwp->pw_uid, 1616 pwp->pw_gid, 1617 #ifdef EXTENSIONS 1618 pwp->pw_class, 1619 #endif 1620 (long)pwp->pw_change, 1621 (long)pwp->pw_expire, 1622 pwp->pw_gecos, 1623 pwp->pw_dir, 1624 pwp->pw_shell); 1625 if (write(ptmpfd, buf, len) != len) { 1626 int serrno = errno; 1627 (void)close(ptmpfd); 1628 (void)pw_abort(); 1629 errno = serrno; 1630 err(EXIT_FAILURE, "Can't modify user " 1631 "`%s': write", login_name); 1632 } 1633 } 1634 } else { 1635 len = strlen(buf); 1636 if (write(ptmpfd, buf, len) != len) { 1637 int serrno = errno; 1638 (void)close(masterfd); 1639 (void)close(ptmpfd); 1640 (void)pw_abort(); 1641 errno = serrno; 1642 err(EXIT_FAILURE, "Can't modify `%s': " 1643 "write", login_name); 1644 } 1645 } 1646 } 1647 if (up != NULL) { 1648 if ((up->u_flags & F_MKDIR) && 1649 asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) { 1650 (void)close(ptmpfd); 1651 (void)pw_abort(); 1652 errx(EXIT_FAILURE, "Can't modify user `%s': " 1653 "can't move `%s' to `%s'", 1654 login_name, homedir, pwp->pw_dir); 1655 } 1656 if (up->u_groupc > 0 && 1657 !append_group(newlogin, up->u_groupc, up->u_groupv)) { 1658 (void)close(ptmpfd); 1659 (void)pw_abort(); 1660 errx(EXIT_FAILURE, "Can't modify user `%s': " 1661 "can't append `%s' to new groups", 1662 login_name, newlogin); 1663 } 1664 } 1665 (void)close(ptmpfd); 1666 (void)fclose(master); 1667 #if PW_MKDB_ARGC == 2 1668 if (up != NULL && strcmp(login_name, newlogin) == 0) { 1669 error = pw_mkdb(login_name, 0); 1670 } else { 1671 error = pw_mkdb(NULL, 0); 1672 } 1673 #else 1674 error = pw_mkdb(); 1675 #endif 1676 if (error < 0) { 1677 (void)pw_abort(); 1678 errx(EXIT_FAILURE, "Can't modify user `%s': pw_mkdb failed", 1679 login_name); 1680 } 1681 if (up == NULL) { 1682 syslog(LOG_INFO, "User removed: name=%s", login_name); 1683 } else if (strcmp(login_name, newlogin) == 0) { 1684 syslog(LOG_INFO, "User information modified: name=%s, uid=%d, " 1685 "gid=%d, home=%s, shell=%s", 1686 login_name, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, 1687 pwp->pw_shell); 1688 } else { 1689 syslog(LOG_INFO, "User information modified: name=%s, " 1690 "new name=%s, uid=%d, gid=%d, home=%s, shell=%s", 1691 login_name, newlogin, pwp->pw_uid, pwp->pw_gid, 1692 pwp->pw_dir, pwp->pw_shell); 1693 } 1694 return 1; 1695 } 1696 1697 #ifdef EXTENSIONS 1698 /* see if we can find out the user struct */ 1699 static struct passwd * 1700 find_user_info(const char *name) 1701 { 1702 struct passwd *pwp; 1703 1704 if ((pwp = getpwnam(name)) != NULL) { 1705 return pwp; 1706 } 1707 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) { 1708 return pwp; 1709 } 1710 return NULL; 1711 } 1712 #endif 1713 1714 /* see if we can find out the group struct */ 1715 static struct group * 1716 find_group_info(const char *name) 1717 { 1718 struct group *grp; 1719 1720 if ((grp = getgrnam(name)) != NULL) { 1721 return grp; 1722 } 1723 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) { 1724 return grp; 1725 } 1726 return NULL; 1727 } 1728 1729 /* print out usage message, and then exit */ 1730 void 1731 usermgmt_usage(const char *prog) 1732 { 1733 if (strcmp(prog, "useradd") == 0) { 1734 (void)fprintf(stderr, "usage: %s -D [-F] [-b base-dir] " 1735 "[-e expiry-time] [-f inactive-time]\n" 1736 "\t[-g gid | name | =uid] [-k skel-dir] [-L login-class]\n" 1737 "\t[-r lowuid..highuid] [-s shell]\n", prog); 1738 (void)fprintf(stderr, "usage: %s [-moSv] [-b base-dir] " 1739 "[-c comment] [-d home-dir] [-e expiry-time]\n" 1740 "\t[-f inactive-time] [-G secondary-group] " 1741 "[-g gid | name | =uid]\n" 1742 "\t[-k skeletondir] [-L login-class] [-p password] " 1743 "[-r lowuid..highuid]\n" 1744 "\t[-s shell] [-u uid] user\n", 1745 prog); 1746 } else if (strcmp(prog, "usermod") == 0) { 1747 (void)fprintf(stderr, "usage: %s [-FmoSv] [-C yes/no] " 1748 "[-c comment] [-d home-dir] [-e expiry-time]\n" 1749 "\t[-f inactive] [-G secondary-group] " 1750 "[-g gid | name | =uid]\n" 1751 "\t[-L login-class] [-l new-login] [-p password] " 1752 "[-s shell] [-u uid]\n" 1753 "\tuser\n", prog); 1754 } else if (strcmp(prog, "userdel") == 0) { 1755 (void)fprintf(stderr, "usage: %s -D [-p preserve-value]\n", prog); 1756 (void)fprintf(stderr, 1757 "usage: %s [-rSv] [-p preserve-value] user\n", prog); 1758 #ifdef EXTENSIONS 1759 } else if (strcmp(prog, "userinfo") == 0) { 1760 (void)fprintf(stderr, "usage: %s [-ev] user\n", prog); 1761 #endif 1762 } else if (strcmp(prog, "groupadd") == 0) { 1763 (void)fprintf(stderr, "usage: %s [-ov] [-g gid]" 1764 " [-r lowgid..highgid] group\n", prog); 1765 } else if (strcmp(prog, "groupdel") == 0) { 1766 (void)fprintf(stderr, "usage: %s [-v] group\n", prog); 1767 } else if (strcmp(prog, "groupmod") == 0) { 1768 (void)fprintf(stderr, 1769 "usage: %s [-ov] [-g gid] [-n newname] group\n", prog); 1770 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) { 1771 (void)fprintf(stderr, 1772 "usage: %s ( add | del | mod | info ) ...\n", prog); 1773 #ifdef EXTENSIONS 1774 } else if (strcmp(prog, "groupinfo") == 0) { 1775 (void)fprintf(stderr, "usage: %s [-ev] group\n", prog); 1776 #endif 1777 } 1778 exit(EXIT_FAILURE); 1779 /* NOTREACHED */ 1780 } 1781 1782 #ifdef EXTENSIONS 1783 #define ADD_OPT_EXTENSIONS "p:r:vL:S" 1784 #else 1785 #define ADD_OPT_EXTENSIONS 1786 #endif 1787 1788 int 1789 useradd(int argc, char **argv) 1790 { 1791 user_t u; 1792 int defaultfield; 1793 int bigD; 1794 int c; 1795 #ifdef EXTENSIONS 1796 int i; 1797 #endif 1798 1799 (void)memset(&u, 0, sizeof(u)); 1800 read_defaults(&u); 1801 u.u_uid = -1; 1802 defaultfield = bigD = 0; 1803 while ((c = getopt(argc, argv, "DFG:b:c:d:e:f:g:k:mou:s:" 1804 ADD_OPT_EXTENSIONS)) != -1) { 1805 switch(c) { 1806 case 'D': 1807 bigD = 1; 1808 break; 1809 case 'F': 1810 /* 1811 * Setting -1 will force the new user to 1812 * change their password as soon as they 1813 * next log in - passwd(5). 1814 */ 1815 defaultfield = 1; 1816 memsave(&u.u_inactive, "-1", strlen("-1")); 1817 break; 1818 case 'G': 1819 while (u.u_groupc < NGROUPS_MAX && 1820 (u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL) { 1821 if (u.u_groupv[u.u_groupc][0] != 0) { 1822 u.u_groupc++; 1823 } 1824 } 1825 if (optarg != NULL) { 1826 warnx("Truncated list of secondary groups " 1827 "to %d entries", NGROUPS_MAX); 1828 } 1829 break; 1830 #ifdef EXTENSIONS 1831 case 'S': 1832 u.u_allow_samba = 1; 1833 break; 1834 #endif 1835 case 'b': 1836 defaultfield = 1; 1837 memsave(&u.u_basedir, optarg, strlen(optarg)); 1838 break; 1839 case 'c': 1840 memsave(&u.u_comment, optarg, strlen(optarg)); 1841 break; 1842 case 'd': 1843 memsave(&u.u_home, optarg, strlen(optarg)); 1844 u.u_flags |= F_HOMEDIR; 1845 break; 1846 case 'e': 1847 defaultfield = 1; 1848 memsave(&u.u_expire, optarg, strlen(optarg)); 1849 break; 1850 case 'f': 1851 defaultfield = 1; 1852 memsave(&u.u_inactive, optarg, strlen(optarg)); 1853 break; 1854 case 'g': 1855 defaultfield = 1; 1856 memsave(&u.u_primgrp, optarg, strlen(optarg)); 1857 break; 1858 case 'k': 1859 defaultfield = 1; 1860 memsave(&u.u_skeldir, optarg, strlen(optarg)); 1861 break; 1862 #ifdef EXTENSIONS 1863 case 'L': 1864 defaultfield = 1; 1865 memsave(&u.u_class, optarg, strlen(optarg)); 1866 break; 1867 #endif 1868 case 'm': 1869 u.u_flags |= F_MKDIR; 1870 break; 1871 case 'o': 1872 u.u_flags |= F_DUPUID; 1873 break; 1874 #ifdef EXTENSIONS 1875 case 'p': 1876 memsave(&u.u_password, optarg, strlen(optarg)); 1877 break; 1878 #endif 1879 #ifdef EXTENSIONS 1880 case 'r': 1881 defaultfield = 1; 1882 (void)save_range(&u, optarg); 1883 break; 1884 #endif 1885 case 's': 1886 u.u_flags |= F_SHELL; 1887 defaultfield = 1; 1888 memsave(&u.u_shell, optarg, strlen(optarg)); 1889 break; 1890 case 'u': 1891 u.u_uid = check_numeric(optarg, "uid"); 1892 break; 1893 #ifdef EXTENSIONS 1894 case 'v': 1895 verbose = 1; 1896 break; 1897 #endif 1898 default: 1899 usermgmt_usage("useradd"); 1900 /* NOTREACHED */ 1901 } 1902 } 1903 if (bigD) { 1904 if (defaultfield) { 1905 checkeuid(); 1906 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 1907 } 1908 (void)printf("group\t\t%s\n", u.u_primgrp); 1909 (void)printf("base_dir\t%s\n", u.u_basedir); 1910 (void)printf("skel_dir\t%s\n", u.u_skeldir); 1911 (void)printf("shell\t\t%s\n", u.u_shell); 1912 #ifdef EXTENSIONS 1913 (void)printf("class\t\t%s\n", u.u_class); 1914 #endif 1915 (void)printf("inactive\t%s\n", (u.u_inactive == NULL) ? 1916 UNSET_INACTIVE : u.u_inactive); 1917 (void)printf("expire\t\t%s\n", (u.u_expire == NULL) ? 1918 UNSET_EXPIRY : u.u_expire); 1919 #ifdef EXTENSIONS 1920 for (i = 0 ; i < u.u_rc ; i++) { 1921 (void)printf("range\t\t%d..%d\n", 1922 u.u_rv[i].r_from, u.u_rv[i].r_to); 1923 } 1924 #endif 1925 return EXIT_SUCCESS; 1926 } 1927 argc -= optind; 1928 argv += optind; 1929 if (argc != 1) { 1930 usermgmt_usage("useradd"); 1931 } 1932 checkeuid(); 1933 openlog("useradd", LOG_PID, LOG_USER); 1934 return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE; 1935 } 1936 1937 #ifdef EXTENSIONS 1938 #define MOD_OPT_EXTENSIONS "p:vL:S" 1939 #else 1940 #define MOD_OPT_EXTENSIONS 1941 #endif 1942 1943 int 1944 usermod(int argc, char **argv) 1945 { 1946 user_t u; 1947 char newuser[MaxUserNameLen + 1]; 1948 int c, have_new_user; 1949 1950 (void)memset(&u, 0, sizeof(u)); 1951 (void)memset(newuser, 0, sizeof(newuser)); 1952 read_defaults(&u); 1953 have_new_user = 0; 1954 u.u_locked = -1; 1955 while ((c = getopt(argc, argv, "C:FG:c:d:e:f:g:l:mos:u:" 1956 MOD_OPT_EXTENSIONS)) != -1) { 1957 switch(c) { 1958 case 'G': 1959 while (u.u_groupc < NGROUPS_MAX && 1960 (u.u_groupv[u.u_groupc] = 1961 strsep(&optarg, ",")) != NULL) { 1962 if (u.u_groupv[u.u_groupc][0] != 0) { 1963 u.u_groupc++; 1964 } 1965 } 1966 if (optarg != NULL) { 1967 warnx("Truncated list of secondary groups " 1968 "to %d entries", NGROUPS_MAX); 1969 } 1970 u.u_flags |= F_SECGROUP; 1971 break; 1972 #ifdef EXTENSIONS 1973 case 'S': 1974 u.u_allow_samba = 1; 1975 break; 1976 #endif 1977 case 'c': 1978 memsave(&u.u_comment, optarg, strlen(optarg)); 1979 u.u_flags |= F_COMMENT; 1980 break; 1981 case 'C': 1982 if (strcasecmp(optarg, "yes") == 0) { 1983 u.u_locked = LOCK; 1984 } else if (strcasecmp(optarg, "no") == 0) { 1985 u.u_locked = UNLOCK; 1986 } else { 1987 /* No idea. */ 1988 errx(EXIT_FAILURE, 1989 "Please type 'yes' or 'no'"); 1990 } 1991 break; 1992 case 'F': 1993 memsave(&u.u_inactive, "-1", strlen("-1")); 1994 u.u_flags |= F_INACTIVE; 1995 break; 1996 case 'd': 1997 memsave(&u.u_home, optarg, strlen(optarg)); 1998 u.u_flags |= F_HOMEDIR; 1999 break; 2000 case 'e': 2001 memsave(&u.u_expire, optarg, strlen(optarg)); 2002 u.u_flags |= F_EXPIRE; 2003 break; 2004 case 'f': 2005 memsave(&u.u_inactive, optarg, strlen(optarg)); 2006 u.u_flags |= F_INACTIVE; 2007 break; 2008 case 'g': 2009 memsave(&u.u_primgrp, optarg, strlen(optarg)); 2010 u.u_flags |= F_GROUP; 2011 break; 2012 case 'l': 2013 (void)strlcpy(newuser, optarg, sizeof(newuser)); 2014 have_new_user = 1; 2015 u.u_flags |= F_USERNAME; 2016 break; 2017 #ifdef EXTENSIONS 2018 case 'L': 2019 memsave(&u.u_class, optarg, strlen(optarg)); 2020 u.u_flags |= F_CLASS; 2021 break; 2022 #endif 2023 case 'm': 2024 u.u_flags |= F_MKDIR; 2025 break; 2026 case 'o': 2027 u.u_flags |= F_DUPUID; 2028 break; 2029 #ifdef EXTENSIONS 2030 case 'p': 2031 memsave(&u.u_password, optarg, strlen(optarg)); 2032 u.u_flags |= F_PASSWORD; 2033 break; 2034 #endif 2035 case 's': 2036 memsave(&u.u_shell, optarg, strlen(optarg)); 2037 u.u_flags |= F_SHELL; 2038 break; 2039 case 'u': 2040 u.u_uid = check_numeric(optarg, "uid"); 2041 u.u_flags |= F_UID; 2042 break; 2043 #ifdef EXTENSIONS 2044 case 'v': 2045 verbose = 1; 2046 break; 2047 #endif 2048 default: 2049 usermgmt_usage("usermod"); 2050 /* NOTREACHED */ 2051 } 2052 } 2053 if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) && 2054 !(u.u_flags & F_USERNAME)) { 2055 warnx("Option 'm' useless without 'd' or 'l' -- ignored"); 2056 u.u_flags &= ~F_MKDIR; 2057 } 2058 argc -= optind; 2059 argv += optind; 2060 if (argc != 1) { 2061 usermgmt_usage("usermod"); 2062 } 2063 checkeuid(); 2064 openlog("usermod", LOG_PID, LOG_USER); 2065 return moduser(*argv, (have_new_user) ? newuser : *argv, &u, 2066 u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE; 2067 } 2068 2069 #ifdef EXTENSIONS 2070 #define DEL_OPT_EXTENSIONS "Dp:vS" 2071 #else 2072 #define DEL_OPT_EXTENSIONS 2073 #endif 2074 2075 int 2076 userdel(int argc, char **argv) 2077 { 2078 struct passwd *pwp; 2079 user_t u; 2080 char password[PasswordLength + 1]; 2081 int defaultfield; 2082 int rmhome; 2083 int bigD; 2084 int c; 2085 2086 (void)memset(&u, 0, sizeof(u)); 2087 read_defaults(&u); 2088 defaultfield = bigD = rmhome = 0; 2089 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) { 2090 switch(c) { 2091 #ifdef EXTENSIONS 2092 case 'D': 2093 bigD = 1; 2094 break; 2095 #endif 2096 #ifdef EXTENSIONS 2097 case 'S': 2098 u.u_allow_samba = 1; 2099 break; 2100 #endif 2101 #ifdef EXTENSIONS 2102 case 'p': 2103 defaultfield = 1; 2104 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 : 2105 (strcmp(optarg, "yes") == 0) ? 1 : 2106 atoi(optarg); 2107 break; 2108 #endif 2109 case 'r': 2110 rmhome = 1; 2111 break; 2112 #ifdef EXTENSIONS 2113 case 'v': 2114 verbose = 1; 2115 break; 2116 #endif 2117 default: 2118 usermgmt_usage("userdel"); 2119 /* NOTREACHED */ 2120 } 2121 } 2122 #ifdef EXTENSIONS 2123 if (bigD) { 2124 if (defaultfield) { 2125 checkeuid(); 2126 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 2127 } 2128 (void)printf("preserve\t%s\n", (u.u_preserve) ? "true" : 2129 "false"); 2130 return EXIT_SUCCESS; 2131 } 2132 #endif 2133 argc -= optind; 2134 argv += optind; 2135 if (argc != 1) { 2136 usermgmt_usage("userdel"); 2137 } 2138 checkeuid(); 2139 if ((pwp = getpwnam(*argv)) == NULL) { 2140 warnx("No such user `%s'", *argv); 2141 return EXIT_FAILURE; 2142 } 2143 if (rmhome) { 2144 (void)removehomedir(pwp); 2145 } 2146 if (u.u_preserve) { 2147 u.u_flags |= F_SHELL; 2148 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN)); 2149 (void)memset(password, '*', DES_Len); 2150 password[DES_Len] = 0; 2151 memsave(&u.u_password, password, strlen(password)); 2152 u.u_flags |= F_PASSWORD; 2153 openlog("userdel", LOG_PID, LOG_USER); 2154 return moduser(*argv, *argv, &u, u.u_allow_samba) ? 2155 EXIT_SUCCESS : EXIT_FAILURE; 2156 } 2157 if (!rm_user_from_groups(*argv)) { 2158 return 0; 2159 } 2160 openlog("userdel", LOG_PID, LOG_USER); 2161 return moduser(*argv, *argv, NULL, u.u_allow_samba) ? 2162 EXIT_SUCCESS : EXIT_FAILURE; 2163 } 2164 2165 #ifdef EXTENSIONS 2166 #define GROUP_ADD_OPT_EXTENSIONS "r:v" 2167 #else 2168 #define GROUP_ADD_OPT_EXTENSIONS 2169 #endif 2170 2171 /* add a group */ 2172 int 2173 groupadd(int argc, char **argv) 2174 { 2175 int dupgid; 2176 int gid; 2177 int c; 2178 int lowgid; 2179 int highgid; 2180 2181 gid = -1; 2182 dupgid = 0; 2183 lowgid = LowGid; 2184 highgid = HighGid; 2185 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) { 2186 switch(c) { 2187 case 'g': 2188 gid = check_numeric(optarg, "gid"); 2189 break; 2190 case 'o': 2191 dupgid = 1; 2192 break; 2193 #ifdef EXTENSIONS 2194 case 'r': 2195 if (sscanf(optarg, "%d..%d", &lowgid, &highgid) != 2) { 2196 errx(EXIT_FAILURE, "Bad range `%s'", optarg); 2197 } 2198 break; 2199 case 'v': 2200 verbose = 1; 2201 break; 2202 #endif 2203 default: 2204 usermgmt_usage("groupadd"); 2205 /* NOTREACHED */ 2206 } 2207 } 2208 argc -= optind; 2209 argv += optind; 2210 if (argc != 1) { 2211 usermgmt_usage("groupadd"); 2212 } 2213 checkeuid(); 2214 if (gid < 0 && !getnextgid(&gid, lowgid, highgid)) { 2215 err(EXIT_FAILURE, "Can't add group: can't get next gid"); 2216 } 2217 if (!dupgid && getgrgid((gid_t) gid) != NULL) { 2218 errx(EXIT_FAILURE, "Can't add group: gid %d is a duplicate", 2219 gid); 2220 } 2221 if (!valid_group(*argv)) { 2222 warnx("Invalid group name `%s'", *argv); 2223 } 2224 openlog("groupadd", LOG_PID, LOG_USER); 2225 if (!creategid(*argv, gid, "")) 2226 exit(EXIT_FAILURE); 2227 2228 return EXIT_SUCCESS; 2229 } 2230 2231 #ifdef EXTENSIONS 2232 #define GROUP_DEL_OPT_EXTENSIONS "v" 2233 #else 2234 #define GROUP_DEL_OPT_EXTENSIONS 2235 #endif 2236 2237 /* remove a group */ 2238 int 2239 groupdel(int argc, char **argv) 2240 { 2241 int c; 2242 2243 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) { 2244 switch(c) { 2245 #ifdef EXTENSIONS 2246 case 'v': 2247 verbose = 1; 2248 break; 2249 #endif 2250 default: 2251 usermgmt_usage("groupdel"); 2252 /* NOTREACHED */ 2253 } 2254 } 2255 argc -= optind; 2256 argv += optind; 2257 if (argc != 1) { 2258 usermgmt_usage("groupdel"); 2259 } 2260 if (getgrnam(*argv) == NULL) { 2261 errx(EXIT_FAILURE, "No such group `%s'", *argv); 2262 } 2263 checkeuid(); 2264 openlog("groupdel", LOG_PID, LOG_USER); 2265 if (!modify_gid(*argv, NULL)) 2266 exit(EXIT_FAILURE); 2267 2268 return EXIT_SUCCESS; 2269 } 2270 2271 #ifdef EXTENSIONS 2272 #define GROUP_MOD_OPT_EXTENSIONS "v" 2273 #else 2274 #define GROUP_MOD_OPT_EXTENSIONS 2275 #endif 2276 2277 /* modify a group */ 2278 int 2279 groupmod(int argc, char **argv) 2280 { 2281 struct group *grp; 2282 char buf[MaxEntryLen]; 2283 char *newname; 2284 char **cpp; 2285 int dupgid; 2286 int gid; 2287 int cc; 2288 int c; 2289 2290 gid = -1; 2291 dupgid = 0; 2292 newname = NULL; 2293 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) { 2294 switch(c) { 2295 case 'g': 2296 gid = check_numeric(optarg, "gid"); 2297 break; 2298 case 'o': 2299 dupgid = 1; 2300 break; 2301 case 'n': 2302 memsave(&newname, optarg, strlen(optarg)); 2303 break; 2304 #ifdef EXTENSIONS 2305 case 'v': 2306 verbose = 1; 2307 break; 2308 #endif 2309 default: 2310 usermgmt_usage("groupmod"); 2311 /* NOTREACHED */ 2312 } 2313 } 2314 argc -= optind; 2315 argv += optind; 2316 if (argc != 1) { 2317 usermgmt_usage("groupmod"); 2318 } 2319 checkeuid(); 2320 if (gid < 0 && newname == NULL) { 2321 errx(EXIT_FAILURE, "Nothing to change"); 2322 } 2323 if (dupgid && gid < 0) { 2324 errx(EXIT_FAILURE, "Duplicate which gid?"); 2325 } 2326 if ((grp = find_group_info(*argv)) == NULL) { 2327 errx(EXIT_FAILURE, "Can't find group `%s' to modify", *argv); 2328 } 2329 if (!is_local(*argv, _PATH_GROUP)) { 2330 errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv); 2331 } 2332 if (newname != NULL && !valid_group(newname)) { 2333 warnx("Invalid group name `%s'", newname); 2334 } 2335 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:", 2336 (newname) ? newname : grp->gr_name, 2337 grp->gr_passwd, 2338 (gid < 0) ? grp->gr_gid : gid); 2339 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) { 2340 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp, 2341 (cpp[1] == NULL) ? "" : ","); 2342 } 2343 cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n"); 2344 if (newname != NULL) 2345 free(newname); 2346 openlog("groupmod", LOG_PID, LOG_USER); 2347 if (!modify_gid(*argv, buf)) 2348 exit(EXIT_FAILURE); 2349 2350 return EXIT_SUCCESS; 2351 } 2352 2353 #ifdef EXTENSIONS 2354 /* display user information */ 2355 int 2356 userinfo(int argc, char **argv) 2357 { 2358 struct passwd *pwp; 2359 struct group *grp; 2360 char buf[MaxEntryLen]; 2361 char **cpp; 2362 int exists; 2363 int cc; 2364 int i; 2365 2366 exists = 0; 2367 while ((i = getopt(argc, argv, "ev")) != -1) { 2368 switch(i) { 2369 case 'e': 2370 exists = 1; 2371 break; 2372 case 'v': 2373 verbose = 1; 2374 break; 2375 default: 2376 usermgmt_usage("userinfo"); 2377 /* NOTREACHED */ 2378 } 2379 } 2380 argc -= optind; 2381 argv += optind; 2382 if (argc != 1) { 2383 usermgmt_usage("userinfo"); 2384 } 2385 pwp = find_user_info(*argv); 2386 if (exists) { 2387 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE); 2388 } 2389 if (pwp == NULL) { 2390 errx(EXIT_FAILURE, "Can't find user `%s'", *argv); 2391 } 2392 (void)printf("login\t%s\n", pwp->pw_name); 2393 (void)printf("passwd\t%s\n", pwp->pw_passwd); 2394 (void)printf("uid\t%d\n", pwp->pw_uid); 2395 for (cc = 0 ; (grp = getgrent()) != NULL ; ) { 2396 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2397 if (strcmp(*cpp, *argv) == 0 && 2398 grp->gr_gid != pwp->pw_gid) { 2399 cc += snprintf(&buf[cc], sizeof(buf) - cc, 2400 "%s ", grp->gr_name); 2401 } 2402 } 2403 } 2404 if ((grp = getgrgid(pwp->pw_gid)) == NULL) { 2405 (void)printf("groups\t%d %s\n", pwp->pw_gid, buf); 2406 } else { 2407 (void)printf("groups\t%s %s\n", grp->gr_name, buf); 2408 } 2409 (void)printf("change\t%s", pwp->pw_change > 0 ? 2410 ctime(&pwp->pw_change) : pwp->pw_change == -1 ? 2411 "NEXT LOGIN\n" : "NEVER\n"); 2412 (void)printf("class\t%s\n", pwp->pw_class); 2413 (void)printf("gecos\t%s\n", pwp->pw_gecos); 2414 (void)printf("dir\t%s\n", pwp->pw_dir); 2415 (void)printf("shell\t%s\n", pwp->pw_shell); 2416 (void)printf("expire\t%s", pwp->pw_expire ? 2417 ctime(&pwp->pw_expire) : "NEVER\n"); 2418 return EXIT_SUCCESS; 2419 } 2420 #endif 2421 2422 #ifdef EXTENSIONS 2423 /* display user information */ 2424 int 2425 groupinfo(int argc, char **argv) 2426 { 2427 struct group *grp; 2428 char **cpp; 2429 int exists; 2430 int i; 2431 2432 exists = 0; 2433 while ((i = getopt(argc, argv, "ev")) != -1) { 2434 switch(i) { 2435 case 'e': 2436 exists = 1; 2437 break; 2438 case 'v': 2439 verbose = 1; 2440 break; 2441 default: 2442 usermgmt_usage("groupinfo"); 2443 /* NOTREACHED */ 2444 } 2445 } 2446 argc -= optind; 2447 argv += optind; 2448 if (argc != 1) { 2449 usermgmt_usage("groupinfo"); 2450 } 2451 grp = find_group_info(*argv); 2452 if (exists) { 2453 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE); 2454 } 2455 if (grp == NULL) { 2456 errx(EXIT_FAILURE, "Can't find group `%s'", *argv); 2457 } 2458 (void)printf("name\t%s\n", grp->gr_name); 2459 (void)printf("passwd\t%s\n", grp->gr_passwd); 2460 (void)printf("gid\t%d\n", grp->gr_gid); 2461 (void)printf("members\t"); 2462 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2463 (void)printf("%s", *cpp); 2464 if (*(cpp + 1)) { 2465 (void) printf(", "); 2466 } 2467 } 2468 (void)fputc('\n', stdout); 2469 return EXIT_SUCCESS; 2470 } 2471 #endif 2472