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