1 /* $NetBSD: user.c,v 1.104 2006/02/23 22:45:25 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.104 2006/02/23 22:45:25 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 /* if *cpp is non-null, free it, then assign `n' chars of `s' to it */ 222 static void 223 memsave(char **cpp, const char *s, size_t n) 224 { 225 if (*cpp != NULL) { 226 FREE(*cpp); 227 } 228 NEWARRAY(char, *cpp, n + 1, exit(1)); 229 (void)memcpy(*cpp, s, n); 230 (*cpp)[n] = '\0'; 231 } 232 233 /* a replacement for system(3) */ 234 static int 235 asystem(const char *fmt, ...) 236 { 237 va_list vp; 238 char buf[MaxCommandLen]; 239 int ret; 240 241 va_start(vp, fmt); 242 (void)vsnprintf(buf, sizeof(buf), fmt, vp); 243 va_end(vp); 244 if (verbose) { 245 (void)printf("Command: %s\n", buf); 246 } 247 if ((ret = system(buf)) != 0) { 248 warn("Error running `%s'", buf); 249 } 250 return ret; 251 } 252 253 /* remove a users home directory, returning 1 for success (ie, no problems encountered) */ 254 static int 255 removehomedir(struct passwd *pwp) 256 { 257 struct stat st; 258 259 /* userid not root? */ 260 if (pwp->pw_uid == 0) { 261 warnx("Not deleting home directory `%s'; userid is 0", pwp->pw_dir); 262 return 0; 263 } 264 265 /* directory exists (and is a directory!) */ 266 if (stat(pwp->pw_dir, &st) < 0) { 267 warn("Cannot access home directory `%s'", pwp->pw_dir); 268 return 0; 269 } 270 if (!S_ISDIR(st.st_mode)) { 271 warnx("Home directory `%s' is not a directory", pwp->pw_dir); 272 return 0; 273 } 274 275 /* userid matches directory owner? */ 276 if (st.st_uid != pwp->pw_uid) { 277 warnx("User `%s' doesn't own directory `%s', not removed", 278 pwp->pw_name, pwp->pw_dir); 279 return 0; 280 } 281 282 (void)seteuid(pwp->pw_uid); 283 /* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */ 284 (void)asystem("%s -rf %s > /dev/null 2>&1 || true", RM, pwp->pw_dir); 285 (void)seteuid(0); 286 if (rmdir(pwp->pw_dir) < 0) { 287 warn("Unable to remove all files in `%s'", pwp->pw_dir); 288 return 0; 289 } 290 return 1; 291 } 292 293 /* return 1 if all of `s' is numeric */ 294 static int 295 is_number(const char *s) 296 { 297 for ( ; *s ; s++) { 298 if (!isdigit((unsigned char) *s)) { 299 return 0; 300 } 301 } 302 return 1; 303 } 304 305 /* 306 * check that the effective uid is 0 - called from funcs which will 307 * modify data and config files. 308 */ 309 static void 310 checkeuid(void) 311 { 312 if (geteuid() != 0) { 313 errx(EXIT_FAILURE, "Program must be run as root"); 314 } 315 } 316 317 /* copy any dot files into the user's home directory */ 318 static int 319 copydotfiles(char *skeldir, int uid, int gid, char *dir) 320 { 321 struct dirent *dp; 322 DIR *dirp; 323 int n; 324 325 if ((dirp = opendir(skeldir)) == NULL) { 326 warn("Can't open source . files dir `%s'", skeldir); 327 return 0; 328 } 329 for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) { 330 if (strcmp(dp->d_name, ".") == 0 || 331 strcmp(dp->d_name, "..") == 0) { 332 continue; 333 } 334 n = 1; 335 } 336 (void)closedir(dirp); 337 if (n == 0) { 338 warnx("No \"dot\" initialisation files found"); 339 } else { 340 (void)asystem("cd %s && %s -rw -pe %s . %s", 341 skeldir, PAX, (verbose) ? "-v" : "", dir); 342 } 343 (void)asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir); 344 (void)asystem("%s -R u+w %s", CHMOD, dir); 345 return n; 346 } 347 348 /* create a group entry with gid `gid' */ 349 static int 350 creategid(char *group, int gid, const char *name) 351 { 352 struct stat st; 353 FILE *from; 354 FILE *to; 355 char buf[MaxEntryLen]; 356 char f[MaxFileNameLen]; 357 int fd; 358 int cc; 359 360 if (getgrnam(group) != NULL) { 361 warnx("Can't create group `%s': already exists", group); 362 return 0; 363 } 364 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 365 warn("Can't create group `%s': can't open `%s'", name, 366 _PATH_GROUP); 367 return 0; 368 } 369 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 370 warn("Can't lock `%s'", _PATH_GROUP); 371 return 0; 372 } 373 (void)fstat(fileno(from), &st); 374 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 375 if ((fd = mkstemp(f)) < 0) { 376 warn("Can't create group `%s': mkstemp failed", group); 377 (void)fclose(from); 378 return 0; 379 } 380 if ((to = fdopen(fd, "w")) == NULL) { 381 warn("Can't create group `%s': fdopen `%s' failed", 382 group, f); 383 (void)fclose(from); 384 (void)close(fd); 385 (void)unlink(f); 386 return 0; 387 } 388 while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) { 389 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 390 warn("Can't create group `%s': short write to `%s'", 391 group, f); 392 (void)fclose(from); 393 (void)close(fd); 394 (void)unlink(f); 395 return 0; 396 } 397 } 398 (void)fprintf(to, "%s:*:%d:%s\n", group, gid, name); 399 (void)fclose(from); 400 (void)fclose(to); 401 if (rename(f, _PATH_GROUP) < 0) { 402 warn("Can't create group `%s': can't rename `%s' to `%s'", 403 group, f, _PATH_GROUP); 404 (void)unlink(f); 405 return 0; 406 } 407 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 408 syslog(LOG_INFO, "New group added: name=%s, gid=%d", group, gid); 409 return 1; 410 } 411 412 /* modify the group entry with name `group' to be newent */ 413 static int 414 modify_gid(char *group, char *newent) 415 { 416 struct stat st; 417 FILE *from; 418 FILE *to; 419 char buf[MaxEntryLen]; 420 char f[MaxFileNameLen]; 421 char *colon; 422 int groupc; 423 int entc; 424 int fd; 425 int cc; 426 427 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 428 warn("Can't modify group `%s': can't open `%s'", 429 group, _PATH_GROUP); 430 return 0; 431 } 432 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 433 warn("Can't modify group `%s': can't lock `%s'", 434 group, _PATH_GROUP); 435 return 0; 436 } 437 (void)fstat(fileno(from), &st); 438 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 439 if ((fd = mkstemp(f)) < 0) { 440 warn("Can't modify group `%s': mkstemp failed", group); 441 (void)fclose(from); 442 return 0; 443 } 444 if ((to = fdopen(fd, "w")) == NULL) { 445 warn("Can't modify group `%s': fdopen `%s' failed", group, f); 446 (void)fclose(from); 447 (void)close(fd); 448 (void)unlink(f); 449 return 0; 450 } 451 groupc = strlen(group); 452 while (fgets(buf, sizeof(buf), from) != NULL) { 453 cc = strlen(buf); 454 if ((colon = strchr(buf, ':')) == NULL) { 455 warnx("Badly formed entry `%s'", buf); 456 continue; 457 } 458 entc = (int)(colon - buf); 459 if (entc == groupc && 460 strncmp(group, buf, (unsigned) entc) == 0) { 461 if (newent == NULL) { 462 struct group *grp_rm; 463 struct passwd *user_pwd; 464 465 /* 466 * Check that the group being removed 467 * isn't any user's Primary group. Just 468 * warn if it is. This could cause problems 469 * if the group GID was reused for a 470 * different purpose. 471 */ 472 473 grp_rm = find_group_info(group); 474 while ((user_pwd = getpwent()) != NULL) { 475 if (user_pwd->pw_gid == grp_rm->gr_gid) { 476 warnx("Warning: group `%s'(%d)" 477 " is the primary group of" 478 " `%s'. Use caution if you" 479 " later add this GID.", 480 grp_rm->gr_name, 481 grp_rm->gr_gid, user_pwd->pw_name); 482 } 483 } 484 endpwent(); 485 continue; 486 } else { 487 cc = strlen(newent); 488 (void)strlcpy(buf, newent, sizeof(buf)); 489 } 490 } 491 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 492 warn("Can't modify group `%s': short write to `%s'", 493 group, f); 494 (void)fclose(from); 495 (void)close(fd); 496 (void)unlink(f); 497 return 0; 498 } 499 } 500 (void)fclose(from); 501 (void)fclose(to); 502 if (rename(f, _PATH_GROUP) < 0) { 503 warn("Can't modify group `%s': can't rename `%s' to `%s'", 504 group, f, _PATH_GROUP); 505 (void)unlink(f); 506 return 0; 507 } 508 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 509 if (newent == NULL) { 510 syslog(LOG_INFO, "group deleted: name=%s", group); 511 } else { 512 syslog(LOG_INFO, "group information modified: name=%s", group); 513 } 514 return 1; 515 } 516 517 /* modify the group entries for all `groups', by adding `user' */ 518 static int 519 append_group(char *user, int ngroups, const char **groups) 520 { 521 struct group *grp; 522 struct stat st; 523 FILE *from; 524 FILE *to; 525 char buf[MaxEntryLen]; 526 char f[MaxFileNameLen]; 527 char *colon; 528 int groupc; 529 int entc; 530 int fd; 531 int nc; 532 int cc; 533 int i; 534 int j; 535 536 for (i = 0 ; i < ngroups ; i++) { 537 if ((grp = getgrnam(groups[i])) == NULL) { 538 warnx("Can't append group `%s' for user `%s'", 539 groups[i], user); 540 } else { 541 for (j = 0 ; grp->gr_mem[j] ; j++) { 542 if (strcmp(user, grp->gr_mem[j]) == 0) { 543 /* already in it */ 544 groups[i] = ""; 545 } 546 } 547 } 548 } 549 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 550 warn("Can't append group(s) for `%s': can't open `%s'", 551 user, _PATH_GROUP); 552 return 0; 553 } 554 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 555 warn("Can't append group(s) for `%s': can't lock `%s'", 556 user, _PATH_GROUP); 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 return 0; 1282 } 1283 (void)fstat(fileno(from), &st); 1284 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 1285 if ((fd = mkstemp(f)) < 0) { 1286 warn("Can't remove user `%s' from `%s': mkstemp failed", 1287 login_name, _PATH_GROUP); 1288 (void)fclose(from); 1289 return 0; 1290 } 1291 if ((to = fdopen(fd, "w")) == NULL) { 1292 warn("Can't remove user `%s' from `%s': fdopen `%s' failed", 1293 login_name, _PATH_GROUP, f); 1294 (void)fclose(from); 1295 (void)close(fd); 1296 (void)unlink(f); 1297 return 0; 1298 } 1299 while (fgets(buf, sizeof(buf), from) != NULL) { 1300 cc = strlen(buf); 1301 if (regexec(&r, buf, 10, matchv, 0) == 0) { 1302 if (buf[(int)matchv[1].rm_so] == ',') { 1303 matchv[2].rm_so = matchv[1].rm_so; 1304 } else if (matchv[2].rm_eo != matchv[3].rm_eo) { 1305 matchv[2].rm_eo = matchv[3].rm_eo; 1306 } 1307 cc -= (int) matchv[2].rm_eo; 1308 sc = (int) matchv[2].rm_so; 1309 if (fwrite(buf, sizeof(char), (size_t)sc, to) != sc || 1310 fwrite(&buf[(int)matchv[2].rm_eo], sizeof(char), 1311 (size_t)cc, to) != cc) { 1312 warn("Can't remove user `%s' from `%s': " 1313 "short write to `%s'", login_name, 1314 _PATH_GROUP, f); 1315 (void)fclose(from); 1316 (void)close(fd); 1317 (void)unlink(f); 1318 return 0; 1319 } 1320 } else if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 1321 warn("Can't remove user `%s' from `%s': " 1322 "short write to `%s'", login_name, _PATH_GROUP, f); 1323 (void)fclose(from); 1324 (void)close(fd); 1325 (void)unlink(f); 1326 return 0; 1327 } 1328 } 1329 (void)fclose(from); 1330 (void)fclose(to); 1331 if (rename(f, _PATH_GROUP) < 0) { 1332 warn("Can't remove user `%s' from `%s': " 1333 "can't rename `%s' to `%s'", 1334 login_name, _PATH_GROUP, f, _PATH_GROUP); 1335 (void)unlink(f); 1336 return 0; 1337 } 1338 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 1339 return 1; 1340 } 1341 1342 /* check that the user or group is local, not from YP/NIS */ 1343 static int 1344 is_local(char *name, const char *file) 1345 { 1346 FILE *fp; 1347 char buf[MaxEntryLen]; 1348 size_t len; 1349 int ret; 1350 1351 if ((fp = fopen(file, "r")) == NULL) { 1352 err(EXIT_FAILURE, "Can't open `%s'", file); 1353 } 1354 len = strlen(name); 1355 for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) { 1356 if (strncmp(buf, name, len) == 0 && buf[len] == ':') { 1357 ret = 1; 1358 break; 1359 } 1360 } 1361 (void)fclose(fp); 1362 return ret; 1363 } 1364 1365 /* modify a user */ 1366 static int 1367 moduser(char *login_name, char *newlogin, user_t *up, int allow_samba) 1368 { 1369 struct passwd *pwp; 1370 struct group *grp; 1371 const char *homedir; 1372 char *locked_pwd; 1373 size_t colonc; 1374 size_t loginc; 1375 size_t len; 1376 size_t cc; 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 ((cc = 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 #if PW_MKDB_ARGC == 2 1667 if (up != NULL && strcmp(login_name, newlogin) == 0) { 1668 error = pw_mkdb(login_name, 0); 1669 } else { 1670 error = pw_mkdb(NULL, 0); 1671 } 1672 #else 1673 error = pw_mkdb(); 1674 #endif 1675 if (error < 0) { 1676 (void)pw_abort(); 1677 errx(EXIT_FAILURE, "Can't modify user `%s': pw_mkdb failed", 1678 login_name); 1679 } 1680 if (up == NULL) { 1681 syslog(LOG_INFO, "User removed: name=%s", login_name); 1682 } else if (strcmp(login_name, newlogin) == 0) { 1683 syslog(LOG_INFO, "User information modified: name=%s, uid=%d, " 1684 "gid=%d, home=%s, shell=%s", 1685 login_name, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, 1686 pwp->pw_shell); 1687 } else { 1688 syslog(LOG_INFO, "User information modified: name=%s, " 1689 "new name=%s, uid=%d, gid=%d, home=%s, shell=%s", 1690 login_name, newlogin, pwp->pw_uid, pwp->pw_gid, 1691 pwp->pw_dir, pwp->pw_shell); 1692 } 1693 return 1; 1694 } 1695 1696 #ifdef EXTENSIONS 1697 /* see if we can find out the user struct */ 1698 static struct passwd * 1699 find_user_info(const char *name) 1700 { 1701 struct passwd *pwp; 1702 1703 if ((pwp = getpwnam(name)) != NULL) { 1704 return pwp; 1705 } 1706 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) { 1707 return pwp; 1708 } 1709 return NULL; 1710 } 1711 #endif 1712 1713 /* see if we can find out the group struct */ 1714 static struct group * 1715 find_group_info(const char *name) 1716 { 1717 struct group *grp; 1718 1719 if ((grp = getgrnam(name)) != NULL) { 1720 return grp; 1721 } 1722 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) { 1723 return grp; 1724 } 1725 return NULL; 1726 } 1727 1728 /* print out usage message, and then exit */ 1729 void 1730 usermgmt_usage(const char *prog) 1731 { 1732 if (strcmp(prog, "useradd") == 0) { 1733 (void)fprintf(stderr, "usage: %s -D [-F] [-b base-dir] " 1734 "[-e expiry-time] [-f inactive-time]\n" 1735 "\t[-g gid | name | =uid] [-k skel-dir] [-L login-class]\n" 1736 "\t[-r lowuid..highuid] [-s shell]\n", prog); 1737 (void)fprintf(stderr, "usage: %s [-moSv] [-b base-dir] " 1738 "[-c comment] [-d home-dir] [-e expiry-time]\n" 1739 "\t[-f inactive-time] [-G secondary-group] " 1740 "[-g gid | name | =uid]\n" 1741 "\t[-k skeletondir] [-L login-class] [-p password] " 1742 "[-r lowuid..highuid]\n" 1743 "\t[-s shell] [-u uid] user\n", 1744 prog); 1745 } else if (strcmp(prog, "usermod") == 0) { 1746 (void)fprintf(stderr, "usage: %s [-FmoSv] [-C yes/no] " 1747 "[-c comment] [-d home-dir] [-e expiry-time]\n" 1748 "\t[-f inactive] [-G secondary-group] " 1749 "[-g gid | name | =uid]\n" 1750 "\t[-L login-class] [-l new-login] [-p password] " 1751 "[-s shell] [-u uid]\n" 1752 "\tuser\n", prog); 1753 } else if (strcmp(prog, "userdel") == 0) { 1754 (void)fprintf(stderr, "usage: %s -D [-p preserve-value]\n", prog); 1755 (void)fprintf(stderr, 1756 "usage: %s [-rSv] [-p preserve-value] user\n", prog); 1757 #ifdef EXTENSIONS 1758 } else if (strcmp(prog, "userinfo") == 0) { 1759 (void)fprintf(stderr, "usage: %s [-ev] user\n", prog); 1760 #endif 1761 } else if (strcmp(prog, "groupadd") == 0) { 1762 (void)fprintf(stderr, "usage: %s [-ov] [-g gid]" 1763 " [-r lowgid..highgid] group\n", prog); 1764 } else if (strcmp(prog, "groupdel") == 0) { 1765 (void)fprintf(stderr, "usage: %s [-v] group\n", prog); 1766 } else if (strcmp(prog, "groupmod") == 0) { 1767 (void)fprintf(stderr, 1768 "usage: %s [-ov] [-g gid] [-n newname] group\n", prog); 1769 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) { 1770 (void)fprintf(stderr, 1771 "usage: %s ( add | del | mod | info ) ...\n", prog); 1772 #ifdef EXTENSIONS 1773 } else if (strcmp(prog, "groupinfo") == 0) { 1774 (void)fprintf(stderr, "usage: %s [-ev] group\n", prog); 1775 #endif 1776 } 1777 exit(EXIT_FAILURE); 1778 /* NOTREACHED */ 1779 } 1780 1781 #ifdef EXTENSIONS 1782 #define ADD_OPT_EXTENSIONS "p:r:vL:S" 1783 #else 1784 #define ADD_OPT_EXTENSIONS 1785 #endif 1786 1787 int 1788 useradd(int argc, char **argv) 1789 { 1790 user_t u; 1791 int defaultfield; 1792 int bigD; 1793 int c; 1794 #ifdef EXTENSIONS 1795 int i; 1796 #endif 1797 1798 (void)memset(&u, 0, sizeof(u)); 1799 read_defaults(&u); 1800 u.u_uid = -1; 1801 defaultfield = bigD = 0; 1802 while ((c = getopt(argc, argv, "DFG:b:c:d:e:f:g:k:mou:s:" 1803 ADD_OPT_EXTENSIONS)) != -1) { 1804 switch(c) { 1805 case 'D': 1806 bigD = 1; 1807 break; 1808 case 'F': 1809 /* 1810 * Setting -1 will force the new user to 1811 * change their password as soon as they 1812 * next log in - passwd(5). 1813 */ 1814 defaultfield = 1; 1815 memsave(&u.u_inactive, "-1", strlen("-1")); 1816 break; 1817 case 'G': 1818 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL && 1819 u.u_groupc < NGROUPS_MAX) { 1820 if (u.u_groupv[u.u_groupc][0] != 0) { 1821 u.u_groupc++; 1822 } 1823 } 1824 if (optarg != NULL) { 1825 warnx("Truncated list of secondary groups " 1826 "to %d entries", NGROUPS_MAX); 1827 } 1828 break; 1829 #ifdef EXTENSIONS 1830 case 'S': 1831 u.u_allow_samba = 1; 1832 break; 1833 #endif 1834 case 'b': 1835 defaultfield = 1; 1836 memsave(&u.u_basedir, optarg, strlen(optarg)); 1837 break; 1838 case 'c': 1839 memsave(&u.u_comment, optarg, strlen(optarg)); 1840 break; 1841 case 'd': 1842 memsave(&u.u_home, optarg, strlen(optarg)); 1843 u.u_flags |= F_HOMEDIR; 1844 break; 1845 case 'e': 1846 defaultfield = 1; 1847 memsave(&u.u_expire, optarg, strlen(optarg)); 1848 break; 1849 case 'f': 1850 defaultfield = 1; 1851 memsave(&u.u_inactive, optarg, strlen(optarg)); 1852 break; 1853 case 'g': 1854 defaultfield = 1; 1855 memsave(&u.u_primgrp, optarg, strlen(optarg)); 1856 break; 1857 case 'k': 1858 defaultfield = 1; 1859 memsave(&u.u_skeldir, optarg, strlen(optarg)); 1860 break; 1861 #ifdef EXTENSIONS 1862 case 'L': 1863 defaultfield = 1; 1864 memsave(&u.u_class, optarg, strlen(optarg)); 1865 break; 1866 #endif 1867 case 'm': 1868 u.u_flags |= F_MKDIR; 1869 break; 1870 case 'o': 1871 u.u_flags |= F_DUPUID; 1872 break; 1873 #ifdef EXTENSIONS 1874 case 'p': 1875 memsave(&u.u_password, optarg, strlen(optarg)); 1876 break; 1877 #endif 1878 #ifdef EXTENSIONS 1879 case 'r': 1880 defaultfield = 1; 1881 (void)save_range(&u, optarg); 1882 break; 1883 #endif 1884 case 's': 1885 u.u_flags |= F_SHELL; 1886 defaultfield = 1; 1887 memsave(&u.u_shell, optarg, strlen(optarg)); 1888 break; 1889 case 'u': 1890 u.u_uid = check_numeric(optarg, "uid"); 1891 break; 1892 #ifdef EXTENSIONS 1893 case 'v': 1894 verbose = 1; 1895 break; 1896 #endif 1897 default: 1898 usermgmt_usage("useradd"); 1899 /* NOTREACHED */ 1900 } 1901 } 1902 if (bigD) { 1903 if (defaultfield) { 1904 checkeuid(); 1905 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 1906 } 1907 (void)printf("group\t\t%s\n", u.u_primgrp); 1908 (void)printf("base_dir\t%s\n", u.u_basedir); 1909 (void)printf("skel_dir\t%s\n", u.u_skeldir); 1910 (void)printf("shell\t\t%s\n", u.u_shell); 1911 #ifdef EXTENSIONS 1912 (void)printf("class\t\t%s\n", u.u_class); 1913 #endif 1914 (void)printf("inactive\t%s\n", (u.u_inactive == NULL) ? 1915 UNSET_INACTIVE : u.u_inactive); 1916 (void)printf("expire\t\t%s\n", (u.u_expire == NULL) ? 1917 UNSET_EXPIRY : u.u_expire); 1918 #ifdef EXTENSIONS 1919 for (i = 0 ; i < u.u_rc ; i++) { 1920 (void)printf("range\t\t%d..%d\n", 1921 u.u_rv[i].r_from, u.u_rv[i].r_to); 1922 } 1923 #endif 1924 return EXIT_SUCCESS; 1925 } 1926 argc -= optind; 1927 argv += optind; 1928 if (argc != 1) { 1929 usermgmt_usage("useradd"); 1930 } 1931 checkeuid(); 1932 openlog("useradd", LOG_PID, LOG_USER); 1933 return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE; 1934 } 1935 1936 #ifdef EXTENSIONS 1937 #define MOD_OPT_EXTENSIONS "p:vL:S" 1938 #else 1939 #define MOD_OPT_EXTENSIONS 1940 #endif 1941 1942 int 1943 usermod(int argc, char **argv) 1944 { 1945 user_t u; 1946 char newuser[MaxUserNameLen + 1]; 1947 int c, have_new_user; 1948 1949 (void)memset(&u, 0, sizeof(u)); 1950 (void)memset(newuser, 0, sizeof(newuser)); 1951 read_defaults(&u); 1952 have_new_user = 0; 1953 u.u_locked = -1; 1954 while ((c = getopt(argc, argv, "C:FG:c:d:e:f:g:l:mos:u:" 1955 MOD_OPT_EXTENSIONS)) != -1) { 1956 switch(c) { 1957 case 'G': 1958 while ((u.u_groupv[u.u_groupc] = 1959 strsep(&optarg, ",")) != NULL && 1960 u.u_groupc < NGROUPS_MAX) { 1961 if (u.u_groupv[u.u_groupc][0] != 0) { 1962 u.u_groupc++; 1963 } 1964 } 1965 if (optarg != NULL) { 1966 warnx("Truncated list of secondary groups " 1967 "to %d entries", NGROUPS_MAX); 1968 } 1969 u.u_flags |= F_SECGROUP; 1970 break; 1971 #ifdef EXTENSIONS 1972 case 'S': 1973 u.u_allow_samba = 1; 1974 break; 1975 #endif 1976 case 'c': 1977 memsave(&u.u_comment, optarg, strlen(optarg)); 1978 u.u_flags |= F_COMMENT; 1979 break; 1980 case 'C': 1981 if (strcasecmp(optarg, "yes") == 0) { 1982 u.u_locked = LOCK; 1983 } else if (strcasecmp(optarg, "no") == 0) { 1984 u.u_locked = UNLOCK; 1985 } else { 1986 /* No idea. */ 1987 errx(EXIT_FAILURE, 1988 "Please type 'yes' or 'no'"); 1989 } 1990 break; 1991 case 'F': 1992 memsave(&u.u_inactive, "-1", strlen("-1")); 1993 u.u_flags |= F_INACTIVE; 1994 break; 1995 case 'd': 1996 memsave(&u.u_home, optarg, strlen(optarg)); 1997 u.u_flags |= F_HOMEDIR; 1998 break; 1999 case 'e': 2000 memsave(&u.u_expire, optarg, strlen(optarg)); 2001 u.u_flags |= F_EXPIRE; 2002 break; 2003 case 'f': 2004 memsave(&u.u_inactive, optarg, strlen(optarg)); 2005 u.u_flags |= F_INACTIVE; 2006 break; 2007 case 'g': 2008 memsave(&u.u_primgrp, optarg, strlen(optarg)); 2009 u.u_flags |= F_GROUP; 2010 break; 2011 case 'l': 2012 (void)strlcpy(newuser, optarg, sizeof(newuser)); 2013 have_new_user = 1; 2014 u.u_flags |= F_USERNAME; 2015 break; 2016 #ifdef EXTENSIONS 2017 case 'L': 2018 memsave(&u.u_class, optarg, strlen(optarg)); 2019 u.u_flags |= F_CLASS; 2020 break; 2021 #endif 2022 case 'm': 2023 u.u_flags |= F_MKDIR; 2024 break; 2025 case 'o': 2026 u.u_flags |= F_DUPUID; 2027 break; 2028 #ifdef EXTENSIONS 2029 case 'p': 2030 memsave(&u.u_password, optarg, strlen(optarg)); 2031 u.u_flags |= F_PASSWORD; 2032 break; 2033 #endif 2034 case 's': 2035 memsave(&u.u_shell, optarg, strlen(optarg)); 2036 u.u_flags |= F_SHELL; 2037 break; 2038 case 'u': 2039 u.u_uid = check_numeric(optarg, "uid"); 2040 u.u_flags |= F_UID; 2041 break; 2042 #ifdef EXTENSIONS 2043 case 'v': 2044 verbose = 1; 2045 break; 2046 #endif 2047 default: 2048 usermgmt_usage("usermod"); 2049 /* NOTREACHED */ 2050 } 2051 } 2052 if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) && 2053 !(u.u_flags & F_USERNAME)) { 2054 warnx("Option 'm' useless without 'd' or 'l' -- ignored"); 2055 u.u_flags &= ~F_MKDIR; 2056 } 2057 argc -= optind; 2058 argv += optind; 2059 if (argc != 1) { 2060 usermgmt_usage("usermod"); 2061 } 2062 checkeuid(); 2063 openlog("usermod", LOG_PID, LOG_USER); 2064 return moduser(*argv, (have_new_user) ? newuser : *argv, &u, 2065 u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE; 2066 } 2067 2068 #ifdef EXTENSIONS 2069 #define DEL_OPT_EXTENSIONS "Dp:vS" 2070 #else 2071 #define DEL_OPT_EXTENSIONS 2072 #endif 2073 2074 int 2075 userdel(int argc, char **argv) 2076 { 2077 struct passwd *pwp; 2078 user_t u; 2079 char password[PasswordLength + 1]; 2080 int defaultfield; 2081 int rmhome; 2082 int bigD; 2083 int c; 2084 2085 (void)memset(&u, 0, sizeof(u)); 2086 read_defaults(&u); 2087 defaultfield = bigD = rmhome = 0; 2088 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) { 2089 switch(c) { 2090 #ifdef EXTENSIONS 2091 case 'D': 2092 bigD = 1; 2093 break; 2094 #endif 2095 #ifdef EXTENSIONS 2096 case 'S': 2097 u.u_allow_samba = 1; 2098 break; 2099 #endif 2100 #ifdef EXTENSIONS 2101 case 'p': 2102 defaultfield = 1; 2103 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 : 2104 (strcmp(optarg, "yes") == 0) ? 1 : 2105 atoi(optarg); 2106 break; 2107 #endif 2108 case 'r': 2109 rmhome = 1; 2110 break; 2111 #ifdef EXTENSIONS 2112 case 'v': 2113 verbose = 1; 2114 break; 2115 #endif 2116 default: 2117 usermgmt_usage("userdel"); 2118 /* NOTREACHED */ 2119 } 2120 } 2121 #ifdef EXTENSIONS 2122 if (bigD) { 2123 if (defaultfield) { 2124 checkeuid(); 2125 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 2126 } 2127 (void)printf("preserve\t%s\n", (u.u_preserve) ? "true" : 2128 "false"); 2129 return EXIT_SUCCESS; 2130 } 2131 #endif 2132 argc -= optind; 2133 argv += optind; 2134 if (argc != 1) { 2135 usermgmt_usage("userdel"); 2136 } 2137 checkeuid(); 2138 if ((pwp = getpwnam(*argv)) == NULL) { 2139 warnx("No such user `%s'", *argv); 2140 return EXIT_FAILURE; 2141 } 2142 if (rmhome) { 2143 (void)removehomedir(pwp); 2144 } 2145 if (u.u_preserve) { 2146 u.u_flags |= F_SHELL; 2147 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN)); 2148 (void)memset(password, '*', DES_Len); 2149 password[DES_Len] = 0; 2150 memsave(&u.u_password, password, strlen(password)); 2151 u.u_flags |= F_PASSWORD; 2152 openlog("userdel", LOG_PID, LOG_USER); 2153 return moduser(*argv, *argv, &u, u.u_allow_samba) ? 2154 EXIT_SUCCESS : EXIT_FAILURE; 2155 } 2156 if (!rm_user_from_groups(*argv)) { 2157 return 0; 2158 } 2159 openlog("userdel", LOG_PID, LOG_USER); 2160 return moduser(*argv, *argv, NULL, u.u_allow_samba) ? 2161 EXIT_SUCCESS : EXIT_FAILURE; 2162 } 2163 2164 #ifdef EXTENSIONS 2165 #define GROUP_ADD_OPT_EXTENSIONS "r:v" 2166 #else 2167 #define GROUP_ADD_OPT_EXTENSIONS 2168 #endif 2169 2170 /* add a group */ 2171 int 2172 groupadd(int argc, char **argv) 2173 { 2174 int dupgid; 2175 int gid; 2176 int c; 2177 int lowgid; 2178 int highgid; 2179 2180 gid = -1; 2181 dupgid = 0; 2182 lowgid = LowGid; 2183 highgid = HighGid; 2184 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) { 2185 switch(c) { 2186 case 'g': 2187 gid = check_numeric(optarg, "gid"); 2188 break; 2189 case 'o': 2190 dupgid = 1; 2191 break; 2192 #ifdef EXTENSIONS 2193 case 'r': 2194 if (sscanf(optarg, "%d..%d", &lowgid, &highgid) != 2) { 2195 errx(EXIT_FAILURE, "Bad range `%s'", optarg); 2196 } 2197 break; 2198 case 'v': 2199 verbose = 1; 2200 break; 2201 #endif 2202 default: 2203 usermgmt_usage("groupadd"); 2204 /* NOTREACHED */ 2205 } 2206 } 2207 argc -= optind; 2208 argv += optind; 2209 if (argc != 1) { 2210 usermgmt_usage("groupadd"); 2211 } 2212 checkeuid(); 2213 if (gid < 0 && !getnextgid(&gid, lowgid, highgid)) { 2214 err(EXIT_FAILURE, "Can't add group: can't get next gid"); 2215 } 2216 if (!dupgid && getgrgid((gid_t) gid) != NULL) { 2217 errx(EXIT_FAILURE, "Can't add group: gid %d is a duplicate", 2218 gid); 2219 } 2220 if (!valid_group(*argv)) { 2221 warnx("Invalid group name `%s'", *argv); 2222 } 2223 openlog("groupadd", LOG_PID, LOG_USER); 2224 if (!creategid(*argv, gid, "")) 2225 exit(EXIT_FAILURE); 2226 2227 return EXIT_SUCCESS; 2228 } 2229 2230 #ifdef EXTENSIONS 2231 #define GROUP_DEL_OPT_EXTENSIONS "v" 2232 #else 2233 #define GROUP_DEL_OPT_EXTENSIONS 2234 #endif 2235 2236 /* remove a group */ 2237 int 2238 groupdel(int argc, char **argv) 2239 { 2240 int c; 2241 2242 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) { 2243 switch(c) { 2244 #ifdef EXTENSIONS 2245 case 'v': 2246 verbose = 1; 2247 break; 2248 #endif 2249 default: 2250 usermgmt_usage("groupdel"); 2251 /* NOTREACHED */ 2252 } 2253 } 2254 argc -= optind; 2255 argv += optind; 2256 if (argc != 1) { 2257 usermgmt_usage("groupdel"); 2258 } 2259 if (getgrnam(*argv) == NULL) { 2260 errx(EXIT_FAILURE, "No such group `%s'", *argv); 2261 } 2262 checkeuid(); 2263 openlog("groupdel", LOG_PID, LOG_USER); 2264 if (!modify_gid(*argv, NULL)) 2265 exit(EXIT_FAILURE); 2266 2267 return EXIT_SUCCESS; 2268 } 2269 2270 #ifdef EXTENSIONS 2271 #define GROUP_MOD_OPT_EXTENSIONS "v" 2272 #else 2273 #define GROUP_MOD_OPT_EXTENSIONS 2274 #endif 2275 2276 /* modify a group */ 2277 int 2278 groupmod(int argc, char **argv) 2279 { 2280 struct group *grp; 2281 char buf[MaxEntryLen]; 2282 char *newname; 2283 char **cpp; 2284 int dupgid; 2285 int gid; 2286 int cc; 2287 int c; 2288 2289 gid = -1; 2290 dupgid = 0; 2291 newname = NULL; 2292 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) { 2293 switch(c) { 2294 case 'g': 2295 gid = check_numeric(optarg, "gid"); 2296 break; 2297 case 'o': 2298 dupgid = 1; 2299 break; 2300 case 'n': 2301 memsave(&newname, optarg, strlen(optarg)); 2302 break; 2303 #ifdef EXTENSIONS 2304 case 'v': 2305 verbose = 1; 2306 break; 2307 #endif 2308 default: 2309 usermgmt_usage("groupmod"); 2310 /* NOTREACHED */ 2311 } 2312 } 2313 argc -= optind; 2314 argv += optind; 2315 if (argc != 1) { 2316 usermgmt_usage("groupmod"); 2317 } 2318 checkeuid(); 2319 if (gid < 0 && newname == NULL) { 2320 errx(EXIT_FAILURE, "Nothing to change"); 2321 } 2322 if (dupgid && gid < 0) { 2323 errx(EXIT_FAILURE, "Duplicate which gid?"); 2324 } 2325 if ((grp = find_group_info(*argv)) == NULL) { 2326 errx(EXIT_FAILURE, "Can't find group `%s' to modify", *argv); 2327 } 2328 if (!is_local(*argv, _PATH_GROUP)) { 2329 errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv); 2330 } 2331 if (newname != NULL && !valid_group(newname)) { 2332 warnx("Invalid group name `%s'", newname); 2333 } 2334 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:", 2335 (newname) ? newname : grp->gr_name, 2336 grp->gr_passwd, 2337 (gid < 0) ? grp->gr_gid : gid); 2338 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) { 2339 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp, 2340 (cpp[1] == NULL) ? "" : ","); 2341 } 2342 cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n"); 2343 openlog("groupmod", LOG_PID, LOG_USER); 2344 if (!modify_gid(*argv, buf)) 2345 exit(EXIT_FAILURE); 2346 2347 return EXIT_SUCCESS; 2348 } 2349 2350 #ifdef EXTENSIONS 2351 /* display user information */ 2352 int 2353 userinfo(int argc, char **argv) 2354 { 2355 struct passwd *pwp; 2356 struct group *grp; 2357 char buf[MaxEntryLen]; 2358 char **cpp; 2359 int exists; 2360 int cc; 2361 int i; 2362 2363 exists = 0; 2364 while ((i = getopt(argc, argv, "ev")) != -1) { 2365 switch(i) { 2366 case 'e': 2367 exists = 1; 2368 break; 2369 case 'v': 2370 verbose = 1; 2371 break; 2372 default: 2373 usermgmt_usage("userinfo"); 2374 /* NOTREACHED */ 2375 } 2376 } 2377 argc -= optind; 2378 argv += optind; 2379 if (argc != 1) { 2380 usermgmt_usage("userinfo"); 2381 } 2382 pwp = find_user_info(*argv); 2383 if (exists) { 2384 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE); 2385 } 2386 if (pwp == NULL) { 2387 errx(EXIT_FAILURE, "Can't find user `%s'", *argv); 2388 } 2389 (void)printf("login\t%s\n", pwp->pw_name); 2390 (void)printf("passwd\t%s\n", pwp->pw_passwd); 2391 (void)printf("uid\t%d\n", pwp->pw_uid); 2392 for (cc = 0 ; (grp = getgrent()) != NULL ; ) { 2393 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2394 if (strcmp(*cpp, *argv) == 0 && 2395 grp->gr_gid != pwp->pw_gid) { 2396 cc += snprintf(&buf[cc], sizeof(buf) - cc, 2397 "%s ", grp->gr_name); 2398 } 2399 } 2400 } 2401 if ((grp = getgrgid(pwp->pw_gid)) == NULL) { 2402 (void)printf("groups\t%d %s\n", pwp->pw_gid, buf); 2403 } else { 2404 (void)printf("groups\t%s %s\n", grp->gr_name, buf); 2405 } 2406 (void)printf("change\t%s", pwp->pw_change > 0 ? 2407 ctime(&pwp->pw_change) : pwp->pw_change == -1 ? 2408 "NEXT LOGIN\n" : "NEVER\n"); 2409 (void)printf("class\t%s\n", pwp->pw_class); 2410 (void)printf("gecos\t%s\n", pwp->pw_gecos); 2411 (void)printf("dir\t%s\n", pwp->pw_dir); 2412 (void)printf("shell\t%s\n", pwp->pw_shell); 2413 (void)printf("expire\t%s", pwp->pw_expire ? 2414 ctime(&pwp->pw_expire) : "NEVER\n"); 2415 return EXIT_SUCCESS; 2416 } 2417 #endif 2418 2419 #ifdef EXTENSIONS 2420 /* display user information */ 2421 int 2422 groupinfo(int argc, char **argv) 2423 { 2424 struct group *grp; 2425 char **cpp; 2426 int exists; 2427 int i; 2428 2429 exists = 0; 2430 while ((i = getopt(argc, argv, "ev")) != -1) { 2431 switch(i) { 2432 case 'e': 2433 exists = 1; 2434 break; 2435 case 'v': 2436 verbose = 1; 2437 break; 2438 default: 2439 usermgmt_usage("groupinfo"); 2440 /* NOTREACHED */ 2441 } 2442 } 2443 argc -= optind; 2444 argv += optind; 2445 if (argc != 1) { 2446 usermgmt_usage("groupinfo"); 2447 } 2448 grp = find_group_info(*argv); 2449 if (exists) { 2450 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE); 2451 } 2452 if (grp == NULL) { 2453 errx(EXIT_FAILURE, "Can't find group `%s'", *argv); 2454 } 2455 (void)printf("name\t%s\n", grp->gr_name); 2456 (void)printf("passwd\t%s\n", grp->gr_passwd); 2457 (void)printf("gid\t%d\n", grp->gr_gid); 2458 (void)printf("members\t"); 2459 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2460 (void)printf("%s", *cpp); 2461 if (*(cpp + 1)) { 2462 (void) printf(", "); 2463 } 2464 } 2465 (void)fputc('\n', stdout); 2466 return EXIT_SUCCESS; 2467 } 2468 #endif 2469