1 /* $NetBSD: user.c,v 1.84 2005/07/29 18:16:21 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.84 2005/07/29 18:16:21 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 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 continue; 547 } else { 548 cc = strlen(newent); 549 (void)strlcpy(buf, newent, sizeof(buf)); 550 } 551 } 552 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 553 warn("Can't create gid: short write to `%s'", f); 554 (void)fclose(from); 555 (void)close(fd); 556 (void)unlink(f); 557 return 0; 558 } 559 } 560 (void)fclose(from); 561 (void)fclose(to); 562 if (rename(f, _PATH_GROUP) < 0) { 563 warn("Can't create gid: can't rename `%s' to `%s'", f, 564 _PATH_GROUP); 565 (void)unlink(f); 566 return 0; 567 } 568 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 569 if (newent == NULL) { 570 syslog(LOG_INFO, "group deleted: name=%s", group); 571 } else { 572 syslog(LOG_INFO, "group information modified: name=%s", group); 573 } 574 return 1; 575 } 576 577 /* modify the group entries for all `groups', by adding `user' */ 578 static int 579 append_group(char *user, int ngroups, const char **groups) 580 { 581 struct group *grp; 582 struct stat st; 583 FILE *from; 584 FILE *to; 585 char buf[MaxEntryLen]; 586 char f[MaxFileNameLen]; 587 char *colon; 588 int groupc; 589 int entc; 590 int fd; 591 int nc; 592 int cc; 593 int i; 594 int j; 595 596 for (i = 0 ; i < ngroups ; i++) { 597 if ((grp = getgrnam(groups[i])) == NULL) { 598 warnx("Can't append group `%s' for user `%s'", 599 groups[i], user); 600 } else { 601 for (j = 0 ; grp->gr_mem[j] ; j++) { 602 if (strcmp(user, grp->gr_mem[j]) == 0) { 603 /* already in it */ 604 groups[i] = ""; 605 } 606 } 607 } 608 } 609 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 610 warn("Can't append group for `%s': can't open `%s'", 611 user, _PATH_GROUP); 612 return 0; 613 } 614 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 615 warn("Can't lock `%s'", _PATH_GROUP); 616 } 617 (void)fstat(fileno(from), &st); 618 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 619 if ((fd = mkstemp(f)) < 0) { 620 warn("Can't create gid: mkstemp failed"); 621 (void)fclose(from); 622 return 0; 623 } 624 if ((to = fdopen(fd, "w")) == NULL) { 625 warn("Can't create gid: fdopen `%s' failed", f); 626 (void)fclose(from); 627 (void)close(fd); 628 (void)unlink(f); 629 return 0; 630 } 631 while (fgets(buf, sizeof(buf), from) != NULL) { 632 cc = strlen(buf); 633 if ((colon = strchr(buf, ':')) == NULL) { 634 warnx("Badly formed entry `%s'", buf); 635 continue; 636 } 637 entc = (int)(colon - buf); 638 for (i = 0 ; i < ngroups ; i++) { 639 if ((groupc = strlen(groups[i])) == 0) { 640 continue; 641 } 642 if (entc == groupc && 643 strncmp(groups[i], buf, (unsigned) entc) == 0) { 644 if ((nc = snprintf(&buf[cc - 1], 645 sizeof(buf) - cc + 1, "%s%s\n", 646 (buf[cc - 2] == ':') ? "" : ",", user)) < 0) 647 warnx("Warning: group `%s' " 648 "entry too long", groups[i]); 649 cc += nc - 1; 650 } 651 } 652 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 653 warn("Can't create gid: short write to `%s'", f); 654 (void)fclose(from); 655 (void)close(fd); 656 (void)unlink(f); 657 return 0; 658 } 659 } 660 (void)fclose(from); 661 (void)fclose(to); 662 if (rename(f, _PATH_GROUP) < 0) { 663 warn("Can't create gid: can't rename `%s' to `%s'", 664 f, _PATH_GROUP); 665 (void)unlink(f); 666 return 0; 667 } 668 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 669 return 1; 670 } 671 672 /* the valid characters for login and group names */ 673 #define VALID_CHAR(c) (isalnum(c) || (c) == '.' || (c) == '_' || (c) == '-') 674 675 /* return 1 if `login' is a valid login name */ 676 static int 677 valid_login(char *login_name, int allow_samba) 678 { 679 unsigned char *cp; 680 681 if (strlen(login_name) >= LOGIN_NAME_MAX) { 682 return 0; 683 } 684 for (cp = (unsigned char *)login_name ; *cp ; cp++) { 685 if (!VALID_CHAR(*cp)) { 686 #ifdef EXTENSIONS 687 /* check for a trailing '$' in a Samba user name */ 688 if (allow_samba && *cp == '$' && *(cp + 1) == 0x0) { 689 return 1; 690 } 691 #endif 692 return 0; 693 } 694 } 695 return 1; 696 } 697 698 /* return 1 if `group' is a valid group name */ 699 static int 700 valid_group(char *group) 701 { 702 unsigned char *cp; 703 704 for (cp = (unsigned char *)group; *cp; cp++) { 705 if (!VALID_CHAR(*cp)) { 706 return 0; 707 } 708 } 709 return 1; 710 } 711 712 /* find the next gid in the range lo .. hi */ 713 static int 714 getnextgid(int *gidp, int lo, int hi) 715 { 716 for (*gidp = lo ; *gidp < hi ; *gidp += 1) { 717 if (getgrgid((gid_t)*gidp) == NULL) { 718 return 1; 719 } 720 } 721 return 0; 722 } 723 724 #ifdef EXTENSIONS 725 /* save a range of uids */ 726 static int 727 save_range(user_t *up, char *cp) 728 { 729 int from; 730 int to; 731 int i; 732 733 if (up->u_rsize == 0) { 734 up->u_rsize = 32; 735 NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0)); 736 } else if (up->u_rc == up->u_rsize) { 737 up->u_rsize *= 2; 738 RENEW(range_t, up->u_rv, up->u_rsize, return(0)); 739 } 740 if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) { 741 for (i = up->u_defrc ; i < up->u_rc ; i++) { 742 if (up->u_rv[i].r_from == from && 743 up->u_rv[i].r_to == to) { 744 break; 745 } 746 } 747 if (i == up->u_rc) { 748 up->u_rv[up->u_rc].r_from = from; 749 up->u_rv[up->u_rc].r_to = to; 750 up->u_rc += 1; 751 } 752 } else { 753 warnx("Bad range `%s'", cp); 754 return 0; 755 } 756 return 1; 757 } 758 #endif 759 760 /* set the defaults in the defaults file */ 761 static int 762 setdefaults(user_t *up) 763 { 764 char template[MaxFileNameLen]; 765 FILE *fp; 766 int ret; 767 int fd; 768 #ifdef EXTENSIONS 769 int i; 770 #endif 771 772 (void)snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE); 773 if ((fd = mkstemp(template)) < 0) { 774 warnx("Can't mkstemp `%s' for writing", CONFFILE); 775 return 0; 776 } 777 if ((fp = fdopen(fd, "w")) == NULL) { 778 warn("Can't fdopen `%s' for writing", CONFFILE); 779 return 0; 780 } 781 ret = 1; 782 if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 || 783 fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 || 784 fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 || 785 fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 || 786 #ifdef EXTENSIONS 787 fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 || 788 #endif 789 fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ? 790 UNSET_INACTIVE : up->u_inactive) <= 0 || 791 fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ? 792 UNSET_EXPIRY : up->u_expire) <= 0 || 793 fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? 794 "false" : "true") <= 0) { 795 warn("Can't write to `%s'", CONFFILE); 796 ret = 0; 797 } 798 #ifdef EXTENSIONS 799 for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0; 800 i < up->u_rc ; i++) { 801 if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, 802 up->u_rv[i].r_to) <= 0) { 803 warn("Can't write to `%s'", CONFFILE); 804 ret = 0; 805 } 806 } 807 #endif 808 (void)fclose(fp); 809 if (ret) { 810 ret = ((rename(template, CONFFILE) == 0) && 811 (chmod(CONFFILE, 0644) == 0)); 812 } 813 return ret; 814 } 815 816 /* read the defaults file */ 817 static void 818 read_defaults(user_t *up) 819 { 820 struct stat st; 821 size_t lineno; 822 size_t len; 823 FILE *fp; 824 char *cp; 825 char *s; 826 827 memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP)); 828 memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR)); 829 memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR)); 830 memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL)); 831 memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT)); 832 #ifdef EXTENSIONS 833 memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS)); 834 #endif 835 up->u_rsize = 16; 836 up->u_defrc = 0; 837 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1)); 838 up->u_inactive = DEF_INACTIVE; 839 up->u_expire = DEF_EXPIRE; 840 if ((fp = fopen(CONFFILE, "r")) == NULL) { 841 if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) { 842 warn("Can't create `%s' defaults file", CONFFILE); 843 } 844 fp = fopen(CONFFILE, "r"); 845 } 846 if (fp != NULL) { 847 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) { 848 if (strncmp(s, "group", 5) == 0) { 849 cp = skipspace(s + 5); 850 memsave(&up->u_primgrp, (char *)cp, strlen(cp)); 851 } else if (strncmp(s, "base_dir", 8) == 0) { 852 cp = skipspace(s + 8); 853 memsave(&up->u_basedir, (char *)cp, strlen(cp)); 854 } else if (strncmp(s, "skel_dir", 8) == 0) { 855 cp = skipspace(s + 8); 856 memsave(&up->u_skeldir, (char *)cp, strlen(cp)); 857 } else if (strncmp(s, "shell", 5) == 0) { 858 cp = skipspace(s + 5); 859 memsave(&up->u_shell, cp, strlen(cp)); 860 #ifdef EXTENSIONS 861 } else if (strncmp((char *)s, "class", 5) == 0) { 862 cp = skipspace(s + 5); 863 memsave(&up->u_class, cp, strlen(cp)); 864 #endif 865 } else if (strncmp(s, "inactive", 8) == 0) { 866 cp = skipspace(s + 8); 867 if (strcmp(cp, UNSET_INACTIVE) == 0) { 868 if (up->u_inactive) { 869 FREE(up->u_inactive); 870 } 871 up->u_inactive = NULL; 872 } else { 873 memsave(&up->u_inactive, cp, strlen(cp)); 874 } 875 #ifdef EXTENSIONS 876 } else if (strncmp(s, "range", 5) == 0) { 877 cp = skipspace(s + 5); 878 (void)save_range(up, cp); 879 #endif 880 #ifdef EXTENSIONS 881 } else if (strncmp(s, "preserve", 8) == 0) { 882 cp = skipspace(s + 8); 883 up->u_preserve = 884 (strncmp(cp, "true", 4) == 0) ? 1 : 885 (strncmp(cp, "yes", 3) == 0) ? 1 : atoi(cp); 886 #endif 887 } else if (strncmp(s, "expire", 6) == 0) { 888 cp = skipspace(s + 6); 889 if (strcmp(cp, UNSET_EXPIRY) == 0) { 890 if (up->u_expire) { 891 FREE(up->u_expire); 892 } 893 up->u_expire = NULL; 894 } else { 895 memsave(&up->u_expire, cp, strlen(cp)); 896 } 897 } 898 (void)free(s); 899 } 900 (void)fclose(fp); 901 } 902 if (up->u_rc == 0) { 903 up->u_rv[up->u_rc].r_from = DEF_LOWUID; 904 up->u_rv[up->u_rc].r_to = DEF_HIGHUID; 905 up->u_rc += 1; 906 } 907 up->u_defrc = up->u_rc; 908 } 909 910 /* return the next valid unused uid */ 911 static int 912 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid) 913 { 914 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) { 915 if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) { 916 if (sync_uid_gid) { 917 if (getgrgid((gid_t)(*uid)) == NULL) { 918 return 1; 919 } 920 } else { 921 return 1; 922 } 923 } 924 } 925 return 0; 926 } 927 928 /* structure which defines a password type */ 929 typedef struct passwd_type_t { 930 const char *type; /* optional type descriptor */ 931 size_t desc_length; /* length of type descriptor */ 932 size_t length; /* length of password */ 933 const char *regex; /* regexp to output the password */ 934 size_t re_sub; /* subscript of regexp to use */ 935 } passwd_type_t; 936 937 static passwd_type_t passwd_types[] = { 938 { "$sha1", 5, 28, "\\$[^$]+\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* SHA1 */ 939 { "$2a", 3, 54, "\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* Blowfish */ 940 { "$1", 2, 34, NULL, 0 }, /* MD5 */ 941 { "", 0, DES_Len,NULL, 0 }, /* standard DES */ 942 { NULL, (size_t)~0, (size_t)~0, NULL, 0 } 943 /* none - terminate search */ 944 }; 945 946 /* return non-zero if it's a valid password - check length for cipher type */ 947 static int 948 valid_password_length(char *newpasswd) 949 { 950 passwd_type_t *pwtp; 951 regmatch_t matchv[10]; 952 regex_t r; 953 954 for (pwtp = passwd_types; pwtp->desc_length != (size_t)~0; pwtp++) { 955 if (strncmp(newpasswd, pwtp->type, pwtp->desc_length) == 0) { 956 if (pwtp->regex == NULL) { 957 return strlen(newpasswd) == pwtp->length; 958 } 959 (void)regcomp(&r, pwtp->regex, REG_EXTENDED); 960 if (regexec(&r, newpasswd, 10, matchv, 0) == 0) { 961 regfree(&r); 962 return (int)(matchv[pwtp->re_sub].rm_eo - 963 matchv[pwtp->re_sub].rm_so + 1) == 964 pwtp->length; 965 } 966 regfree(&r); 967 } 968 } 969 return 0; 970 } 971 972 #ifdef EXTENSIONS 973 static int 974 valid_class(char *class) 975 { 976 login_cap_t *lc; 977 978 /* 979 * Check if /etc/login.conf exists. login_getclass() will 980 * return 1 due to it not existing, so not informing the 981 * user the actual login class does not exist. 982 */ 983 984 if (access(PATH_LOGINCONF, R_OK) == -1) { 985 warn("Access failed for `%s'; will not validate class `%s'", 986 PATH_LOGINCONF, class); 987 return 1; 988 } 989 990 if ((lc = login_getclass(class)) != NULL) 991 login_close(lc); 992 993 return lc != NULL; 994 } 995 #endif 996 997 /* look for a valid time, return 0 if it was specified but bad */ 998 static int 999 scantime(time_t *tp, char *s) 1000 { 1001 struct tm tm; 1002 1003 *tp = 0; 1004 if (s != NULL) { 1005 (void)memset(&tm, 0, sizeof(tm)); 1006 if (strptime(s, "%c", &tm) != NULL) { 1007 *tp = mktime(&tm); 1008 } else if (strptime(s, "%B %d %Y", &tm) != NULL) { 1009 *tp = mktime(&tm); 1010 } else if (isdigit((unsigned char) *s)) { 1011 *tp = atoi(s); 1012 } else { 1013 return 0; 1014 } 1015 } 1016 return 1; 1017 } 1018 1019 /* add a user */ 1020 static int 1021 adduser(char *login_name, user_t *up) 1022 { 1023 struct group *grp; 1024 struct stat st; 1025 time_t expire; 1026 time_t inactive; 1027 char password[PasswordLength + 1]; 1028 char home[MaxFileNameLen]; 1029 char buf[MaxFileNameLen]; 1030 int sync_uid_gid; 1031 int masterfd; 1032 int ptmpfd; 1033 int gid; 1034 int cc; 1035 int i; 1036 1037 if (!valid_login(login_name, up->u_allow_samba)) { 1038 errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name); 1039 } 1040 #ifdef EXTENSIONS 1041 if (up->u_class != NULL && !valid_class(up->u_class)) { 1042 errx(EXIT_FAILURE, "No such login class `%s'", up->u_class); 1043 } 1044 #endif 1045 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 1046 err(EXIT_FAILURE, "Can't open `%s'", _PATH_MASTERPASSWD); 1047 } 1048 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 1049 err(EXIT_FAILURE, "Can't lock `%s'", _PATH_MASTERPASSWD); 1050 } 1051 pw_init(); 1052 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 1053 int serrno = errno; 1054 (void)close(masterfd); 1055 errno = serrno; 1056 err(EXIT_FAILURE, "Can't obtain pw_lock"); 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, "Short write to /etc/ptmp " 1066 "(not %d chars)", cc); 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 get next uid for %d", 1095 up->u_uid); 1096 } 1097 } 1098 /* check uid isn't already allocated */ 1099 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) { 1100 (void)close(ptmpfd); 1101 (void)pw_abort(); 1102 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid); 1103 } 1104 /* if -g=uid was specified, check gid is unused */ 1105 if (sync_uid_gid) { 1106 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 1107 (void)close(ptmpfd); 1108 (void)pw_abort(); 1109 errx(EXIT_FAILURE, "gid %d is already in use", 1110 up->u_uid); 1111 } 1112 gid = up->u_uid; 1113 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 1114 gid = grp->gr_gid; 1115 } else if (is_number(up->u_primgrp) && 1116 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) { 1117 gid = grp->gr_gid; 1118 } else { 1119 (void)close(ptmpfd); 1120 (void)pw_abort(); 1121 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp); 1122 } 1123 /* check name isn't already in use */ 1124 if (!(up->u_flags & F_DUPUID) && getpwnam(login_name) != NULL) { 1125 (void)close(ptmpfd); 1126 (void)pw_abort(); 1127 errx(EXIT_FAILURE, "already a `%s' user", login_name); 1128 } 1129 if (up->u_flags & F_HOMEDIR) { 1130 (void)strlcpy(home, up->u_home, sizeof(home)); 1131 } else { 1132 /* if home directory hasn't been given, make it up */ 1133 (void)snprintf(home, sizeof(home), "%s/%s", up->u_basedir, 1134 login_name); 1135 } 1136 if (!scantime(&inactive, up->u_inactive)) { 1137 warnx("Warning: inactive time `%s' invalid, account expiry off", 1138 up->u_inactive); 1139 } 1140 if (!scantime(&expire, up->u_expire)) { 1141 warnx("Warning: expire time `%s' invalid, password expiry off", 1142 up->u_expire); 1143 } 1144 if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) { 1145 warnx("Warning: home directory `%s' doesn't exist, " 1146 "and -m was not specified", home); 1147 } 1148 password[sizeof(password) - 1] = '\0'; 1149 if (up->u_password != NULL && valid_password_length(up->u_password)) { 1150 (void)strlcpy(password, up->u_password, sizeof(password)); 1151 } else { 1152 (void)memset(password, '*', DES_Len); 1153 password[DES_Len] = 0; 1154 if (up->u_password != NULL) { 1155 warnx("Password `%s' is invalid: setting it to `%s'", 1156 up->u_password, password); 1157 } 1158 } 1159 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", 1160 login_name, 1161 password, 1162 up->u_uid, 1163 gid, 1164 #ifdef EXTENSIONS 1165 up->u_class, 1166 #else 1167 "", 1168 #endif 1169 (long) inactive, 1170 (long) expire, 1171 up->u_comment, 1172 home, 1173 up->u_shell); 1174 if (write(ptmpfd, buf, (size_t) cc) != cc) { 1175 int serrno = errno; 1176 (void)close(ptmpfd); 1177 (void)pw_abort(); 1178 errno = serrno; 1179 err(EXIT_FAILURE, "Can't add `%s'", buf); 1180 } 1181 if (up->u_flags & F_MKDIR) { 1182 if (lstat(home, &st) == 0) { 1183 (void)close(ptmpfd); 1184 (void)pw_abort(); 1185 errx(EXIT_FAILURE, 1186 "Home directory `%s' already exists", home); 1187 } else { 1188 if (asystem("%s -p %s", MKDIR, home) != 0) { 1189 int serrno = errno; 1190 (void)close(ptmpfd); 1191 (void)pw_abort(); 1192 errno = serrno; 1193 err(EXIT_FAILURE, "Can't mkdir `%s'", home); 1194 } 1195 (void)copydotfiles(up->u_skeldir, up->u_uid, gid, home); 1196 } 1197 } 1198 if (strcmp(up->u_primgrp, "=uid") == 0 && 1199 getgrnam(login_name) == NULL && 1200 !creategid(login_name, gid, login_name)) { 1201 (void)close(ptmpfd); 1202 (void)pw_abort(); 1203 errx(EXIT_FAILURE, "Can't create gid %d for login name %s", 1204 gid, login_name); 1205 } 1206 if (up->u_groupc > 0 && 1207 !append_group(login_name, up->u_groupc, up->u_groupv)) { 1208 (void)close(ptmpfd); 1209 (void)pw_abort(); 1210 errx(EXIT_FAILURE, "Can't append `%s' to new groups", 1211 login_name); 1212 } 1213 (void)close(ptmpfd); 1214 #if PW_MKDB_ARGC == 2 1215 if (pw_mkdb(login_name, 0) < 0) 1216 #else 1217 if (pw_mkdb() < 0) 1218 #endif 1219 { 1220 (void)pw_abort(); 1221 errx(EXIT_FAILURE, "pw_mkdb failed"); 1222 } 1223 syslog(LOG_INFO, "New user added: name=%s, uid=%d, gid=%d, home=%s, " 1224 "shell=%s", login_name, up->u_uid, gid, home, up->u_shell); 1225 return 1; 1226 } 1227 1228 /* remove a user from the groups file */ 1229 static int 1230 rm_user_from_groups(char *login_name) 1231 { 1232 struct stat st; 1233 regmatch_t matchv[10]; 1234 regex_t r; 1235 FILE *from; 1236 FILE *to; 1237 char line[MaxEntryLen]; 1238 char buf[MaxEntryLen]; 1239 char f[MaxFileNameLen]; 1240 int fd; 1241 int cc; 1242 int sc; 1243 1244 (void)snprintf(line, sizeof(line), "(:|,)(%s)(,|$)", login_name); 1245 if ((sc = regcomp(&r, line, REG_EXTENDED|REG_NEWLINE)) != 0) { 1246 (void)regerror(sc, &r, buf, sizeof(buf)); 1247 warnx("Can't compile regular expression `%s' (%s)", line, 1248 buf); 1249 return 0; 1250 } 1251 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 1252 warn("Can't remove gid for `%s': can't open `%s'", 1253 login_name, _PATH_GROUP); 1254 return 0; 1255 } 1256 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 1257 warn("can't lock `%s'", _PATH_GROUP); 1258 } 1259 (void)fstat(fileno(from), &st); 1260 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 1261 if ((fd = mkstemp(f)) < 0) { 1262 warn("Can't create gid: mkstemp failed"); 1263 (void)fclose(from); 1264 return 0; 1265 } 1266 if ((to = fdopen(fd, "w")) == NULL) { 1267 warn("Can't create gid: fdopen `%s' failed", f); 1268 (void)fclose(from); 1269 (void)close(fd); 1270 (void)unlink(f); 1271 return 0; 1272 } 1273 while (fgets(buf, sizeof(buf), from) != NULL) { 1274 cc = strlen(buf); 1275 if (regexec(&r, buf, 10, matchv, 0) == 0) { 1276 if (buf[(int)matchv[1].rm_so] == ',') { 1277 matchv[2].rm_so = matchv[1].rm_so; 1278 } else if (matchv[2].rm_eo != matchv[3].rm_eo) { 1279 matchv[2].rm_eo = matchv[3].rm_eo; 1280 } 1281 cc -= (int) matchv[2].rm_eo; 1282 sc = (int) matchv[2].rm_so; 1283 if (fwrite(buf, sizeof(char), (size_t)sc, to) != sc || 1284 fwrite(&buf[(int)matchv[2].rm_eo], sizeof(char), 1285 (size_t)cc, to) != cc) { 1286 warn("Can't create gid: short write to `%s'", 1287 f); 1288 (void)fclose(from); 1289 (void)close(fd); 1290 (void)unlink(f); 1291 return 0; 1292 } 1293 } else if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 1294 warn("Can't create gid: short write to `%s'", f); 1295 (void)fclose(from); 1296 (void)close(fd); 1297 (void)unlink(f); 1298 return 0; 1299 } 1300 } 1301 (void)fclose(from); 1302 (void)fclose(to); 1303 if (rename(f, _PATH_GROUP) < 0) { 1304 warn("Can't create gid: can't rename `%s' to `%s'", 1305 f, _PATH_GROUP); 1306 (void)unlink(f); 1307 return 0; 1308 } 1309 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 1310 return 1; 1311 } 1312 1313 /* check that the user or group is local, not from YP/NIS */ 1314 static int 1315 is_local(char *name, const char *file) 1316 { 1317 FILE *fp; 1318 char buf[MaxEntryLen]; 1319 size_t len; 1320 int ret; 1321 1322 if ((fp = fopen(file, "r")) == NULL) { 1323 err(EXIT_FAILURE, "Can't open `%s'", file); 1324 } 1325 len = strlen(name); 1326 for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) { 1327 if (strncmp(buf, name, len) == 0 && buf[len] == ':') { 1328 ret = 1; 1329 break; 1330 } 1331 } 1332 (void)fclose(fp); 1333 return ret; 1334 } 1335 1336 /* modify a user */ 1337 static int 1338 moduser(char *login_name, char *newlogin, user_t *up, int allow_samba) 1339 { 1340 struct passwd *pwp; 1341 struct group *grp; 1342 const char *homedir; 1343 char *locked_pwd; 1344 size_t colonc; 1345 size_t loginc; 1346 size_t len; 1347 size_t cc; 1348 FILE *master; 1349 char newdir[MaxFileNameLen]; 1350 char buf[MaxEntryLen]; 1351 char *colon; 1352 int masterfd; 1353 int ptmpfd; 1354 int error; 1355 1356 if (!valid_login(newlogin, allow_samba)) { 1357 errx(EXIT_FAILURE, "`%s' is not a valid login name", 1358 login_name); 1359 } 1360 if ((pwp = getpwnam(login_name)) == NULL) { 1361 errx(EXIT_FAILURE, "No such user `%s'", login_name); 1362 } 1363 if (!is_local(login_name, _PATH_MASTERPASSWD)) { 1364 errx(EXIT_FAILURE, "User `%s' must be a local user", 1365 login_name); 1366 } 1367 /* keep dir name in case we need it for '-m' */ 1368 homedir = pwp->pw_dir; 1369 1370 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 1371 err(EXIT_FAILURE, "Can't open `%s'", _PATH_MASTERPASSWD); 1372 } 1373 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 1374 err(EXIT_FAILURE, "Can't lock `%s'", _PATH_MASTERPASSWD); 1375 } 1376 pw_init(); 1377 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 1378 int serrno = errno; 1379 (void)close(masterfd); 1380 errno = serrno; 1381 err(EXIT_FAILURE, "Can't obtain pw_lock"); 1382 } 1383 if ((master = fdopen(masterfd, "r")) == NULL) { 1384 int serrno = errno; 1385 (void)close(masterfd); 1386 (void)close(ptmpfd); 1387 (void)pw_abort(); 1388 errno = serrno; 1389 err(EXIT_FAILURE, "Can't fdopen fd for %s", _PATH_MASTERPASSWD); 1390 } 1391 if (up != NULL) { 1392 if (up->u_flags & F_USERNAME) { 1393 /* 1394 * If changing name, 1395 * check new name isn't already in use 1396 */ 1397 if (strcmp(login_name, newlogin) != 0 && 1398 getpwnam(newlogin) != NULL) { 1399 (void)close(ptmpfd); 1400 (void)pw_abort(); 1401 errx(EXIT_FAILURE, "Already a `%s' user", 1402 newlogin); 1403 } 1404 pwp->pw_name = newlogin; 1405 1406 /* 1407 * Provide a new directory name in case the 1408 * home directory is to be moved. 1409 */ 1410 if (up->u_flags & F_MKDIR) { 1411 (void)snprintf(newdir, sizeof(newdir), "%s/%s", 1412 up->u_basedir, newlogin); 1413 pwp->pw_dir = newdir; 1414 } 1415 } 1416 if (up->u_flags & F_PASSWORD) { 1417 if (up->u_password != NULL) { 1418 if (!valid_password_length(up->u_password)) { 1419 (void)close(ptmpfd); 1420 (void)pw_abort(); 1421 errx(EXIT_FAILURE, 1422 "Invalid password: `%s'", 1423 up->u_password); 1424 } 1425 if ((locked_pwd = 1426 strstr(pwp->pw_passwd, LOCKED)) != NULL) { 1427 /* 1428 * account is locked - keep it locked 1429 * and just change the password. 1430 */ 1431 if (asprintf(&locked_pwd, "%s%s", 1432 LOCKED, up->u_password) == -1) 1433 err(EXIT_FAILURE, 1434 "asprintf failed"); 1435 pwp->pw_passwd = locked_pwd; 1436 } else 1437 pwp->pw_passwd = up->u_password; 1438 } 1439 } 1440 1441 /* check whether we should lock the account. */ 1442 if (up->u_locked == LOCK) { 1443 /* check to see account if already locked. */ 1444 if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED)) 1445 != NULL) 1446 warnx("Account is already locked"); 1447 else { 1448 if (asprintf(&locked_pwd, "%s%s", LOCKED, 1449 pwp->pw_passwd) == -1) 1450 err(EXIT_FAILURE, "asprintf failed"); 1451 pwp->pw_passwd = locked_pwd; 1452 } 1453 } else if (up->u_locked == UNLOCK) { 1454 if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED)) 1455 == NULL) 1456 warnx("Account '%s' is not locked", login_name); 1457 else 1458 pwp->pw_passwd = locked_pwd + strlen(LOCKED); 1459 } 1460 1461 if (up->u_flags & F_UID) { 1462 /* check uid isn't already allocated */ 1463 if (!(up->u_flags & F_DUPUID) && 1464 getpwuid((uid_t)(up->u_uid)) != NULL) { 1465 (void)close(ptmpfd); 1466 (void)pw_abort(); 1467 errx(EXIT_FAILURE, "Uid %d is already in use", 1468 up->u_uid); 1469 } 1470 pwp->pw_uid = up->u_uid; 1471 } 1472 if (up->u_flags & F_GROUP) { 1473 /* if -g=uid was specified, check gid is unused */ 1474 if (strcmp(up->u_primgrp, "=uid") == 0) { 1475 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 1476 (void)close(ptmpfd); 1477 (void)pw_abort(); 1478 errx(EXIT_FAILURE, 1479 "Gid %d is already in use", 1480 up->u_uid); 1481 } 1482 pwp->pw_gid = up->u_uid; 1483 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 1484 pwp->pw_gid = grp->gr_gid; 1485 } else if (is_number(up->u_primgrp) && 1486 (grp = getgrgid( 1487 (gid_t)atoi(up->u_primgrp))) != NULL) { 1488 pwp->pw_gid = grp->gr_gid; 1489 } else { 1490 (void)close(ptmpfd); 1491 (void)pw_abort(); 1492 errx(EXIT_FAILURE, "Group %s not found", 1493 up->u_primgrp); 1494 } 1495 } 1496 if (up->u_flags & F_INACTIVE) { 1497 if (!scantime(&pwp->pw_change, up->u_inactive)) { 1498 warnx("Warning: inactive time `%s' invalid, " 1499 "password expiry off", 1500 up->u_inactive); 1501 } 1502 } 1503 if (up->u_flags & F_EXPIRE) { 1504 if (!scantime(&pwp->pw_expire, up->u_expire)) { 1505 warnx("Warning: expire time `%s' invalid, " 1506 "password expiry off", 1507 up->u_expire); 1508 } 1509 } 1510 if (up->u_flags & F_COMMENT) 1511 pwp->pw_gecos = up->u_comment; 1512 if (up->u_flags & F_HOMEDIR) 1513 pwp->pw_dir = up->u_home; 1514 if (up->u_flags & F_SHELL) 1515 pwp->pw_shell = up->u_shell; 1516 #ifdef EXTENSIONS 1517 if (up->u_flags & F_CLASS) { 1518 if (!valid_class(up->u_class)) { 1519 (void)close(ptmpfd); 1520 (void)pw_abort(); 1521 errx(EXIT_FAILURE, "No such login class `%s'", 1522 up->u_class); 1523 } 1524 pwp->pw_class = up->u_class; 1525 } 1526 #endif 1527 } 1528 loginc = strlen(login_name); 1529 while (fgets(buf, sizeof(buf), master) != NULL) { 1530 if ((colon = strchr(buf, ':')) == NULL) { 1531 warnx("Malformed entry `%s'. Skipping", buf); 1532 continue; 1533 } 1534 colonc = (size_t)(colon - buf); 1535 if (strncmp(login_name, buf, loginc) == 0 && loginc == colonc) { 1536 if (up != NULL) { 1537 len = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:" 1538 #ifdef EXTENSIONS 1539 "%s" 1540 #endif 1541 ":%ld:%ld:%s:%s:%s\n", 1542 newlogin, 1543 pwp->pw_passwd, 1544 pwp->pw_uid, 1545 pwp->pw_gid, 1546 #ifdef EXTENSIONS 1547 pwp->pw_class, 1548 #endif 1549 (long)pwp->pw_change, 1550 (long)pwp->pw_expire, 1551 pwp->pw_gecos, 1552 pwp->pw_dir, 1553 pwp->pw_shell); 1554 if (write(ptmpfd, buf, len) != len) { 1555 int serrno = errno; 1556 (void)close(ptmpfd); 1557 (void)pw_abort(); 1558 errno = serrno; 1559 err(EXIT_FAILURE, "Can't add `%s'", 1560 buf); 1561 } 1562 } 1563 } else { 1564 len = strlen(buf); 1565 if ((cc = write(ptmpfd, buf, len)) != len) { 1566 int serrno = errno; 1567 (void)close(masterfd); 1568 (void)close(ptmpfd); 1569 (void)pw_abort(); 1570 errno = serrno; 1571 err(EXIT_FAILURE, "Short write to /etc/ptmp " 1572 "(%ld not %ld chars)", 1573 (unsigned long)cc, 1574 (unsigned long)len); 1575 } 1576 } 1577 } 1578 if (up != NULL) { 1579 if ((up->u_flags & F_MKDIR) && 1580 asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) { 1581 int serrno = errno; 1582 (void)close(ptmpfd); 1583 (void)pw_abort(); 1584 errno = serrno; 1585 err(EXIT_FAILURE, "Can't move `%s' to `%s'", 1586 homedir, pwp->pw_dir); 1587 } 1588 if (up->u_groupc > 0 && 1589 !append_group(newlogin, up->u_groupc, up->u_groupv)) { 1590 (void)close(ptmpfd); 1591 (void)pw_abort(); 1592 errx(EXIT_FAILURE, "Can't append `%s' to new groups", 1593 newlogin); 1594 } 1595 } 1596 (void)close(ptmpfd); 1597 #if PW_MKDB_ARGC == 2 1598 if (up != NULL && strcmp(login_name, newlogin) == 0) { 1599 error = pw_mkdb(login_name, 0); 1600 } else { 1601 error = pw_mkdb(NULL, 0); 1602 } 1603 #else 1604 error = pw_mkdb(); 1605 #endif 1606 if (error < 0) { 1607 (void)pw_abort(); 1608 errx(EXIT_FAILURE, "pw_mkdb failed"); 1609 } 1610 if (up == NULL) { 1611 syslog(LOG_INFO, "User removed: name=%s", login_name); 1612 } else if (strcmp(login_name, newlogin) == 0) { 1613 syslog(LOG_INFO, "User information modified: name=%s, uid=%d, " 1614 "gid=%d, home=%s, shell=%s", 1615 login_name, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, 1616 pwp->pw_shell); 1617 } else { 1618 syslog(LOG_INFO, "User information modified: name=%s, " 1619 "new name=%s, uid=%d, gid=%d, home=%s, shell=%s", 1620 login_name, newlogin, pwp->pw_uid, pwp->pw_gid, 1621 pwp->pw_dir, pwp->pw_shell); 1622 } 1623 return 1; 1624 } 1625 1626 #ifdef EXTENSIONS 1627 /* see if we can find out the user struct */ 1628 static struct passwd * 1629 find_user_info(const char *name) 1630 { 1631 struct passwd *pwp; 1632 1633 if ((pwp = getpwnam(name)) != NULL) { 1634 return pwp; 1635 } 1636 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) { 1637 return pwp; 1638 } 1639 return NULL; 1640 } 1641 #endif 1642 1643 #ifdef EXTENSIONS 1644 /* see if we can find out the group struct */ 1645 static struct group * 1646 find_group_info(const char *name) 1647 { 1648 struct group *grp; 1649 1650 if ((grp = getgrnam(name)) != NULL) { 1651 return grp; 1652 } 1653 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) { 1654 return grp; 1655 } 1656 return NULL; 1657 } 1658 #endif 1659 1660 /* print out usage message, and then exit */ 1661 void 1662 usermgmt_usage(const char *prog) 1663 { 1664 if (strcmp(prog, "useradd") == 0) { 1665 (void)fprintf(stderr, "usage: %s -D [-b basedir] [-e expiry] " 1666 "[-f inactive] [-g group]\n\t[-r lowuid..highuid] " 1667 "[-s shell] [-L class]\n", prog); 1668 (void)fprintf(stderr, "usage: %s [-G group] [-b basedir] " 1669 "[-c comment] [-d homedir] [-e expiry]\n\t[-f inactive] " 1670 "[-g group] [-k skeletondir] [-m] [-o] [-p password]\n" 1671 "\t[-r lowuid..highuid] [-s shell] [-u uid] [-v] user\n", 1672 prog); 1673 } else if (strcmp(prog, "usermod") == 0) { 1674 (void)fprintf(stderr, "usage: %s [-G group] [-c comment] " 1675 "[-C yes/no] [-d homedir] [-e expire] \n\t[-f inactive] " 1676 "[-g group] [-l newname] [-m] [-o] [-p password]\n" 1677 "\t[-s shell] [-u uid] [-L class] [-v] user\n", prog); 1678 } else if (strcmp(prog, "userdel") == 0) { 1679 (void)fprintf(stderr, "usage: %s -D [-p preserve]\n", prog); 1680 (void)fprintf(stderr, 1681 "usage: %s [-p preserve] [-r] [-v] user\n", prog); 1682 #ifdef EXTENSIONS 1683 } else if (strcmp(prog, "userinfo") == 0) { 1684 (void)fprintf(stderr, "usage: %s [-e] [-v] user\n", prog); 1685 #endif 1686 } else if (strcmp(prog, "groupadd") == 0) { 1687 (void)fprintf(stderr, "usage: %s [-g gid] [-o]" 1688 " [-r lowgid..highgid] [-v] group\n", prog); 1689 } else if (strcmp(prog, "groupdel") == 0) { 1690 (void)fprintf(stderr, "usage: %s [-v] group\n", prog); 1691 } else if (strcmp(prog, "groupmod") == 0) { 1692 (void)fprintf(stderr, 1693 "usage: %s [-g gid] [-o] [-n newname] [-v] group\n", prog); 1694 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) { 1695 (void)fprintf(stderr, 1696 "usage: %s ( add | del | mod | info ) ...\n", prog); 1697 #ifdef EXTENSIONS 1698 } else if (strcmp(prog, "groupinfo") == 0) { 1699 (void)fprintf(stderr, "usage: %s [-e] [-v] group\n", prog); 1700 #endif 1701 } 1702 exit(EXIT_FAILURE); 1703 /* NOTREACHED */ 1704 } 1705 1706 #ifdef EXTENSIONS 1707 #define ADD_OPT_EXTENSIONS "p:r:vL:S" 1708 #else 1709 #define ADD_OPT_EXTENSIONS 1710 #endif 1711 1712 int 1713 useradd(int argc, char **argv) 1714 { 1715 user_t u; 1716 int defaultfield; 1717 int bigD; 1718 int c; 1719 #ifdef EXTENSIONS 1720 int i; 1721 #endif 1722 1723 (void)memset(&u, 0, sizeof(u)); 1724 read_defaults(&u); 1725 u.u_uid = -1; 1726 defaultfield = bigD = 0; 1727 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" 1728 ADD_OPT_EXTENSIONS)) != -1) { 1729 switch(c) { 1730 case 'D': 1731 bigD = 1; 1732 break; 1733 case 'G': 1734 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL && 1735 u.u_groupc < NGROUPS_MAX) { 1736 if (u.u_groupv[u.u_groupc][0] != 0) { 1737 u.u_groupc++; 1738 } 1739 } 1740 if (optarg != NULL) { 1741 warnx("Truncated list of secondary groups " 1742 "to %d entries", NGROUPS_MAX); 1743 } 1744 break; 1745 #ifdef EXTENSIONS 1746 case 'S': 1747 u.u_allow_samba = 1; 1748 break; 1749 #endif 1750 case 'b': 1751 defaultfield = 1; 1752 memsave(&u.u_basedir, optarg, strlen(optarg)); 1753 break; 1754 case 'c': 1755 memsave(&u.u_comment, optarg, strlen(optarg)); 1756 break; 1757 case 'd': 1758 memsave(&u.u_home, optarg, strlen(optarg)); 1759 u.u_flags |= F_HOMEDIR; 1760 break; 1761 case 'e': 1762 defaultfield = 1; 1763 memsave(&u.u_expire, optarg, strlen(optarg)); 1764 break; 1765 case 'f': 1766 defaultfield = 1; 1767 memsave(&u.u_inactive, optarg, strlen(optarg)); 1768 break; 1769 case 'g': 1770 defaultfield = 1; 1771 memsave(&u.u_primgrp, optarg, strlen(optarg)); 1772 break; 1773 case 'k': 1774 defaultfield = 1; 1775 memsave(&u.u_skeldir, optarg, strlen(optarg)); 1776 break; 1777 #ifdef EXTENSIONS 1778 case 'L': 1779 defaultfield = 1; 1780 memsave(&u.u_class, optarg, strlen(optarg)); 1781 break; 1782 #endif 1783 case 'm': 1784 u.u_flags |= F_MKDIR; 1785 break; 1786 case 'o': 1787 u.u_flags |= F_DUPUID; 1788 break; 1789 #ifdef EXTENSIONS 1790 case 'p': 1791 memsave(&u.u_password, optarg, strlen(optarg)); 1792 break; 1793 #endif 1794 #ifdef EXTENSIONS 1795 case 'r': 1796 defaultfield = 1; 1797 (void)save_range(&u, optarg); 1798 break; 1799 #endif 1800 case 's': 1801 defaultfield = 1; 1802 memsave(&u.u_shell, optarg, strlen(optarg)); 1803 break; 1804 case 'u': 1805 u.u_uid = check_numeric(optarg, "uid"); 1806 break; 1807 #ifdef EXTENSIONS 1808 case 'v': 1809 verbose = 1; 1810 break; 1811 #endif 1812 default: 1813 usermgmt_usage("useradd"); 1814 /* NOTREACHED */ 1815 } 1816 } 1817 if (bigD) { 1818 if (defaultfield) { 1819 checkeuid(); 1820 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 1821 } 1822 (void)printf("group\t\t%s\n", u.u_primgrp); 1823 (void)printf("base_dir\t%s\n", u.u_basedir); 1824 (void)printf("skel_dir\t%s\n", u.u_skeldir); 1825 (void)printf("shell\t\t%s\n", u.u_shell); 1826 #ifdef EXTENSIONS 1827 (void)printf("class\t\t%s\n", u.u_class); 1828 #endif 1829 (void)printf("inactive\t%s\n", (u.u_inactive == NULL) ? 1830 UNSET_INACTIVE : u.u_inactive); 1831 (void)printf("expire\t\t%s\n", (u.u_expire == NULL) ? 1832 UNSET_EXPIRY : u.u_expire); 1833 #ifdef EXTENSIONS 1834 for (i = 0 ; i < u.u_rc ; i++) { 1835 (void)printf("range\t\t%d..%d\n", 1836 u.u_rv[i].r_from, u.u_rv[i].r_to); 1837 } 1838 #endif 1839 return EXIT_SUCCESS; 1840 } 1841 argc -= optind; 1842 argv += optind; 1843 if (argc != 1) { 1844 usermgmt_usage("useradd"); 1845 } 1846 checkeuid(); 1847 openlog("useradd", LOG_PID, LOG_USER); 1848 return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE; 1849 } 1850 1851 #ifdef EXTENSIONS 1852 #define MOD_OPT_EXTENSIONS "p:vL:S" 1853 #else 1854 #define MOD_OPT_EXTENSIONS 1855 #endif 1856 1857 int 1858 usermod(int argc, char **argv) 1859 { 1860 user_t u; 1861 char newuser[MaxUserNameLen + 1]; 1862 int c, have_new_user; 1863 1864 (void)memset(&u, 0, sizeof(u)); 1865 (void)memset(newuser, 0, sizeof(newuser)); 1866 read_defaults(&u); 1867 have_new_user = 0; 1868 u.u_locked = -1; 1869 while ((c = getopt(argc, argv, "G:C:c:d:e:f:g:l:mos:u:" 1870 MOD_OPT_EXTENSIONS)) != -1) { 1871 switch(c) { 1872 case 'G': 1873 while ((u.u_groupv[u.u_groupc] = 1874 strsep(&optarg, ",")) != NULL && 1875 u.u_groupc < NGROUPS_MAX) { 1876 if (u.u_groupv[u.u_groupc][0] != 0) { 1877 u.u_groupc++; 1878 } 1879 } 1880 if (optarg != NULL) { 1881 warnx("Truncated list of secondary groups " 1882 "to %d entries", NGROUPS_MAX); 1883 } 1884 u.u_flags |= F_SECGROUP; 1885 break; 1886 #ifdef EXTENSIONS 1887 case 'S': 1888 u.u_allow_samba = 1; 1889 break; 1890 #endif 1891 case 'c': 1892 memsave(&u.u_comment, optarg, strlen(optarg)); 1893 u.u_flags |= F_COMMENT; 1894 break; 1895 case 'C': 1896 if (strcasecmp(optarg, "yes") == 0) 1897 u.u_locked = LOCK; 1898 else if (strcasecmp(optarg, "no") == 0) 1899 u.u_locked = UNLOCK; 1900 else 1901 /* No idea. */ 1902 errx(1, "Please type 'yes' or 'no'"); 1903 1904 break; 1905 case 'd': 1906 memsave(&u.u_home, optarg, strlen(optarg)); 1907 u.u_flags |= F_HOMEDIR; 1908 break; 1909 case 'e': 1910 memsave(&u.u_expire, optarg, strlen(optarg)); 1911 u.u_flags |= F_EXPIRE; 1912 break; 1913 case 'f': 1914 memsave(&u.u_inactive, optarg, strlen(optarg)); 1915 u.u_flags |= F_INACTIVE; 1916 break; 1917 case 'g': 1918 memsave(&u.u_primgrp, optarg, strlen(optarg)); 1919 u.u_flags |= F_GROUP; 1920 break; 1921 case 'l': 1922 (void)strlcpy(newuser, optarg, sizeof(newuser)); 1923 have_new_user = 1; 1924 u.u_flags |= F_USERNAME; 1925 break; 1926 #ifdef EXTENSIONS 1927 case 'L': 1928 memsave(&u.u_class, optarg, strlen(optarg)); 1929 u.u_flags |= F_CLASS; 1930 break; 1931 #endif 1932 case 'm': 1933 u.u_flags |= F_MKDIR; 1934 break; 1935 case 'o': 1936 u.u_flags |= F_DUPUID; 1937 break; 1938 #ifdef EXTENSIONS 1939 case 'p': 1940 memsave(&u.u_password, optarg, strlen(optarg)); 1941 u.u_flags |= F_PASSWORD; 1942 break; 1943 #endif 1944 case 's': 1945 memsave(&u.u_shell, optarg, strlen(optarg)); 1946 u.u_flags |= F_SHELL; 1947 break; 1948 case 'u': 1949 u.u_uid = check_numeric(optarg, "uid"); 1950 u.u_flags |= F_UID; 1951 break; 1952 #ifdef EXTENSIONS 1953 case 'v': 1954 verbose = 1; 1955 break; 1956 #endif 1957 default: 1958 usermgmt_usage("usermod"); 1959 /* NOTREACHED */ 1960 } 1961 } 1962 if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) && 1963 !(u.u_flags & F_USERNAME)) { 1964 warnx("Option 'm' useless without 'd' or 'l' -- ignored"); 1965 u.u_flags &= ~F_MKDIR; 1966 } 1967 argc -= optind; 1968 argv += optind; 1969 if (argc != 1) { 1970 usermgmt_usage("usermod"); 1971 } 1972 checkeuid(); 1973 openlog("usermod", LOG_PID, LOG_USER); 1974 return moduser(*argv, (have_new_user) ? newuser : *argv, &u, 1975 u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE; 1976 } 1977 1978 #ifdef EXTENSIONS 1979 #define DEL_OPT_EXTENSIONS "Dp:vS" 1980 #else 1981 #define DEL_OPT_EXTENSIONS 1982 #endif 1983 1984 int 1985 userdel(int argc, char **argv) 1986 { 1987 struct passwd *pwp; 1988 user_t u; 1989 char password[PasswordLength + 1]; 1990 int defaultfield; 1991 int rmhome; 1992 int bigD; 1993 int c; 1994 1995 (void)memset(&u, 0, sizeof(u)); 1996 read_defaults(&u); 1997 defaultfield = bigD = rmhome = 0; 1998 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) { 1999 switch(c) { 2000 #ifdef EXTENSIONS 2001 case 'D': 2002 bigD = 1; 2003 break; 2004 #endif 2005 #ifdef EXTENSIONS 2006 case 'S': 2007 u.u_allow_samba = 1; 2008 break; 2009 #endif 2010 #ifdef EXTENSIONS 2011 case 'p': 2012 defaultfield = 1; 2013 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 : 2014 (strcmp(optarg, "yes") == 0) ? 1 : 2015 atoi(optarg); 2016 break; 2017 #endif 2018 case 'r': 2019 rmhome = 1; 2020 break; 2021 #ifdef EXTENSIONS 2022 case 'v': 2023 verbose = 1; 2024 break; 2025 #endif 2026 default: 2027 usermgmt_usage("userdel"); 2028 /* NOTREACHED */ 2029 } 2030 } 2031 #ifdef EXTENSIONS 2032 if (bigD) { 2033 if (defaultfield) { 2034 checkeuid(); 2035 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 2036 } 2037 (void)printf("preserve\t%s\n", (u.u_preserve) ? "true" : 2038 "false"); 2039 return EXIT_SUCCESS; 2040 } 2041 #endif 2042 argc -= optind; 2043 argv += optind; 2044 if (argc != 1) { 2045 usermgmt_usage("userdel"); 2046 } 2047 checkeuid(); 2048 if ((pwp = getpwnam(*argv)) == NULL) { 2049 warnx("No such user `%s'", *argv); 2050 return EXIT_FAILURE; 2051 } 2052 if (rmhome) 2053 (void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir); 2054 if (u.u_preserve) { 2055 u.u_flags |= F_SHELL; 2056 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN)); 2057 (void)memset(password, '*', DES_Len); 2058 password[DES_Len] = 0; 2059 memsave(&u.u_password, password, strlen(password)); 2060 u.u_flags |= F_PASSWORD; 2061 openlog("userdel", LOG_PID, LOG_USER); 2062 return moduser(*argv, *argv, &u, u.u_allow_samba) ? 2063 EXIT_SUCCESS : EXIT_FAILURE; 2064 } 2065 if (!rm_user_from_groups(*argv)) { 2066 return 0; 2067 } 2068 openlog("userdel", LOG_PID, LOG_USER); 2069 return moduser(*argv, *argv, NULL, u.u_allow_samba) ? 2070 EXIT_SUCCESS : EXIT_FAILURE; 2071 } 2072 2073 #ifdef EXTENSIONS 2074 #define GROUP_ADD_OPT_EXTENSIONS "r:v" 2075 #else 2076 #define GROUP_ADD_OPT_EXTENSIONS 2077 #endif 2078 2079 /* add a group */ 2080 int 2081 groupadd(int argc, char **argv) 2082 { 2083 int dupgid; 2084 int gid; 2085 int c; 2086 int lowgid; 2087 int highgid; 2088 2089 gid = -1; 2090 dupgid = 0; 2091 lowgid = LowGid; 2092 highgid = HighGid; 2093 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) { 2094 switch(c) { 2095 case 'g': 2096 gid = check_numeric(optarg, "gid"); 2097 break; 2098 case 'o': 2099 dupgid = 1; 2100 break; 2101 #ifdef EXTENSIONS 2102 case 'r': 2103 if (sscanf(optarg, "%d..%d", &lowgid, &highgid) != 2) { 2104 errx(EXIT_FAILURE, "Bad range `%s'", optarg); 2105 } 2106 break; 2107 case 'v': 2108 verbose = 1; 2109 break; 2110 #endif 2111 default: 2112 usermgmt_usage("groupadd"); 2113 /* NOTREACHED */ 2114 } 2115 } 2116 argc -= optind; 2117 argv += optind; 2118 if (argc != 1) { 2119 usermgmt_usage("groupadd"); 2120 } 2121 checkeuid(); 2122 if (gid < 0 && !getnextgid(&gid, lowgid, highgid)) { 2123 err(EXIT_FAILURE, "Can't add group: can't get next gid"); 2124 } 2125 if (!dupgid && getgrgid((gid_t) gid) != NULL) { 2126 errx(EXIT_FAILURE, "Can't add group: gid %d is a duplicate", 2127 gid); 2128 } 2129 if (!valid_group(*argv)) { 2130 warnx("Invalid group name `%s'", *argv); 2131 } 2132 openlog("groupadd", LOG_PID, LOG_USER); 2133 if (!creategid(*argv, gid, "")) { 2134 errx(EXIT_FAILURE, "Can't add group: problems with %s file", 2135 _PATH_GROUP); 2136 } 2137 return EXIT_SUCCESS; 2138 } 2139 2140 #ifdef EXTENSIONS 2141 #define GROUP_DEL_OPT_EXTENSIONS "v" 2142 #else 2143 #define GROUP_DEL_OPT_EXTENSIONS 2144 #endif 2145 2146 /* remove a group */ 2147 int 2148 groupdel(int argc, char **argv) 2149 { 2150 int c; 2151 2152 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) { 2153 switch(c) { 2154 #ifdef EXTENSIONS 2155 case 'v': 2156 verbose = 1; 2157 break; 2158 #endif 2159 default: 2160 usermgmt_usage("groupdel"); 2161 /* NOTREACHED */ 2162 } 2163 } 2164 argc -= optind; 2165 argv += optind; 2166 if (argc != 1) { 2167 usermgmt_usage("groupdel"); 2168 } 2169 if (getgrnam(*argv) == NULL) { 2170 errx(EXIT_FAILURE, "No such group `%s'", *argv); 2171 } 2172 checkeuid(); 2173 openlog("groupdel", LOG_PID, LOG_USER); 2174 if (!modify_gid(*argv, NULL)) { 2175 errx(EXIT_FAILURE, "Can't change `%s' file", _PATH_GROUP); 2176 } 2177 return EXIT_SUCCESS; 2178 } 2179 2180 #ifdef EXTENSIONS 2181 #define GROUP_MOD_OPT_EXTENSIONS "v" 2182 #else 2183 #define GROUP_MOD_OPT_EXTENSIONS 2184 #endif 2185 2186 /* modify a group */ 2187 int 2188 groupmod(int argc, char **argv) 2189 { 2190 struct group *grp; 2191 char buf[MaxEntryLen]; 2192 char *newname; 2193 char **cpp; 2194 int dupgid; 2195 int gid; 2196 int cc; 2197 int c; 2198 2199 gid = -1; 2200 dupgid = 0; 2201 newname = NULL; 2202 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) { 2203 switch(c) { 2204 case 'g': 2205 gid = check_numeric(optarg, "gid"); 2206 break; 2207 case 'o': 2208 dupgid = 1; 2209 break; 2210 case 'n': 2211 memsave(&newname, optarg, strlen(optarg)); 2212 break; 2213 #ifdef EXTENSIONS 2214 case 'v': 2215 verbose = 1; 2216 break; 2217 #endif 2218 default: 2219 usermgmt_usage("groupmod"); 2220 /* NOTREACHED */ 2221 } 2222 } 2223 argc -= optind; 2224 argv += optind; 2225 if (argc != 1) { 2226 usermgmt_usage("groupmod"); 2227 } 2228 checkeuid(); 2229 if (gid < 0 && newname == NULL) { 2230 errx(EXIT_FAILURE, "Nothing to change"); 2231 } 2232 if (dupgid && gid < 0) { 2233 errx(EXIT_FAILURE, "Duplicate which gid?"); 2234 } 2235 if ((grp = find_group_info(*argv)) == NULL) { 2236 errx(EXIT_FAILURE, "Can't find group `%s' to modify", *argv); 2237 } 2238 if (!is_local(*argv, _PATH_GROUP)) { 2239 errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv); 2240 } 2241 if (newname != NULL && !valid_group(newname)) { 2242 warnx("Invalid group name `%s'", newname); 2243 } 2244 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:", 2245 (newname) ? newname : grp->gr_name, 2246 grp->gr_passwd, 2247 (gid < 0) ? grp->gr_gid : gid); 2248 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) { 2249 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp, 2250 (cpp[1] == NULL) ? "" : ","); 2251 } 2252 cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n"); 2253 openlog("groupmod", LOG_PID, LOG_USER); 2254 if (!modify_gid(*argv, buf)) { 2255 errx(EXIT_FAILURE, "Can't change %s file", _PATH_GROUP); 2256 } 2257 return EXIT_SUCCESS; 2258 } 2259 2260 #ifdef EXTENSIONS 2261 /* display user information */ 2262 int 2263 userinfo(int argc, char **argv) 2264 { 2265 struct passwd *pwp; 2266 struct group *grp; 2267 char buf[MaxEntryLen]; 2268 char **cpp; 2269 int exists; 2270 int cc; 2271 int i; 2272 2273 exists = 0; 2274 while ((i = getopt(argc, argv, "ev")) != -1) { 2275 switch(i) { 2276 case 'e': 2277 exists = 1; 2278 break; 2279 case 'v': 2280 verbose = 1; 2281 break; 2282 default: 2283 usermgmt_usage("userinfo"); 2284 /* NOTREACHED */ 2285 } 2286 } 2287 argc -= optind; 2288 argv += optind; 2289 if (argc != 1) { 2290 usermgmt_usage("userinfo"); 2291 } 2292 pwp = find_user_info(*argv); 2293 if (exists) { 2294 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE); 2295 } 2296 if (pwp == NULL) { 2297 errx(EXIT_FAILURE, "Can't find user `%s'", *argv); 2298 } 2299 (void)printf("login\t%s\n", pwp->pw_name); 2300 (void)printf("passwd\t%s\n", pwp->pw_passwd); 2301 (void)printf("uid\t%d\n", pwp->pw_uid); 2302 for (cc = 0 ; (grp = getgrent()) != NULL ; ) { 2303 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2304 if (strcmp(*cpp, *argv) == 0 && 2305 grp->gr_gid != pwp->pw_gid) { 2306 cc += snprintf(&buf[cc], sizeof(buf) - cc, 2307 "%s ", grp->gr_name); 2308 } 2309 } 2310 } 2311 if ((grp = getgrgid(pwp->pw_gid)) == NULL) { 2312 (void)printf("groups\t%d %s\n", pwp->pw_gid, buf); 2313 } else { 2314 (void)printf("groups\t%s %s\n", grp->gr_name, buf); 2315 } 2316 (void)printf("change\t%s", pwp->pw_change ? 2317 ctime(&pwp->pw_change) : "NEVER\n"); 2318 #ifdef EXTENSIONS 2319 (void)printf("class\t%s\n", pwp->pw_class); 2320 #endif 2321 (void)printf("gecos\t%s\n", pwp->pw_gecos); 2322 (void)printf("dir\t%s\n", pwp->pw_dir); 2323 (void)printf("shell\t%s\n", pwp->pw_shell); 2324 (void)printf("expire\t%s", pwp->pw_expire ? 2325 ctime(&pwp->pw_expire) : "NEVER\n"); 2326 return EXIT_SUCCESS; 2327 } 2328 #endif 2329 2330 #ifdef EXTENSIONS 2331 /* display user information */ 2332 int 2333 groupinfo(int argc, char **argv) 2334 { 2335 struct group *grp; 2336 char **cpp; 2337 int exists; 2338 int i; 2339 2340 exists = 0; 2341 while ((i = getopt(argc, argv, "ev")) != -1) { 2342 switch(i) { 2343 case 'e': 2344 exists = 1; 2345 break; 2346 case 'v': 2347 verbose = 1; 2348 break; 2349 default: 2350 usermgmt_usage("groupinfo"); 2351 /* NOTREACHED */ 2352 } 2353 } 2354 argc -= optind; 2355 argv += optind; 2356 if (argc != 1) { 2357 usermgmt_usage("groupinfo"); 2358 } 2359 grp = find_group_info(*argv); 2360 if (exists) { 2361 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE); 2362 } 2363 if (grp == NULL) { 2364 errx(EXIT_FAILURE, "Can't find group `%s'", *argv); 2365 } 2366 (void)printf("name\t%s\n", grp->gr_name); 2367 (void)printf("passwd\t%s\n", grp->gr_passwd); 2368 (void)printf("gid\t%d\n", grp->gr_gid); 2369 (void)printf("members\t"); 2370 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2371 (void)printf("%s", *cpp); 2372 if (*(cpp + 1)) 2373 (void) printf(", "); 2374 } 2375 (void)fputc('\n', stdout); 2376 return EXIT_SUCCESS; 2377 } 2378 #endif 2379