1 /* $OpenBSD: user.c,v 1.25 2001/09/18 01:50:44 millert Exp $ */ 2 /* $NetBSD: user.c,v 1.40 2001/08/17 08:29:00 joda Exp $ */ 3 4 /* 5 * Copyright (c) 1999 Alistair G. Crooks. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Alistair G. Crooks. 18 * 4. The name of the author may not be used to endorse or promote 19 * products derived from this software without specific prior written 20 * permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 28 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/stat.h> 37 38 #include <ctype.h> 39 #include <dirent.h> 40 #include <err.h> 41 #include <fcntl.h> 42 #include <grp.h> 43 #include <paths.h> 44 #include <pwd.h> 45 #include <stdarg.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <time.h> 50 #include <unistd.h> 51 #include <util.h> 52 53 #include "defs.h" 54 #include "usermgmt.h" 55 56 57 /* this struct describes a uid range */ 58 typedef struct range_t { 59 int r_from; /* low uid */ 60 int r_to; /* high uid */ 61 } range_t; 62 63 /* this struct encapsulates the user information */ 64 typedef struct user_t { 65 int u_flags; /* see below */ 66 int u_uid; /* uid of user */ 67 char *u_password; /* encrypted password */ 68 char *u_comment; /* comment field */ 69 char *u_home; /* home directory */ 70 char *u_primgrp; /* primary group */ 71 int u_groupc; /* # of secondary groups */ 72 char *u_groupv[NGROUPS_MAX]; /* secondary groups */ 73 char *u_shell; /* user's shell */ 74 char *u_basedir; /* base directory for home */ 75 char *u_expire; /* when password will expire */ 76 int u_inactive; /* inactive */ 77 char *u_skeldir; /* directory for startup files */ 78 char *u_class; /* login class */ 79 unsigned u_rsize; /* size of range array */ 80 unsigned u_rc; /* # of ranges */ 81 range_t *u_rv; /* the ranges */ 82 unsigned u_defrc; /* # of ranges in defaults */ 83 int u_preserve; /* preserve uids on deletion */ 84 } user_t; 85 86 /* flags for which fields of the user_t replace the passwd entry */ 87 enum { 88 F_COMMENT = 0x0001, 89 F_DUPUID = 0x0002, 90 F_EXPIRE = 0x0004, 91 F_GROUP = 0x0008, 92 F_HOMEDIR = 0x0010, 93 F_MKDIR = 0x0020, 94 F_INACTIVE = 0x0040, 95 F_PASSWORD = 0x0080, 96 F_SECGROUP = 0x0100, 97 F_SHELL = 0x0200, 98 F_UID = 0x0400, 99 F_USERNAME = 0x0800, 100 F_CLASS = 0x1000 101 }; 102 103 #define CONFFILE "/etc/usermgmt.conf" 104 105 #ifndef DEF_GROUP 106 #define DEF_GROUP "users" 107 #endif 108 109 #ifndef DEF_BASEDIR 110 #define DEF_BASEDIR "/home" 111 #endif 112 113 #ifndef DEF_SKELDIR 114 #define DEF_SKELDIR "/etc/skel" 115 #endif 116 117 #ifndef DEF_SHELL 118 #define DEF_SHELL _PATH_CSHELL 119 #endif 120 121 #ifndef DEF_COMMENT 122 #define DEF_COMMENT "" 123 #endif 124 125 #ifndef DEF_LOWUID 126 #define DEF_LOWUID 1000 127 #endif 128 129 #ifndef DEF_HIGHUID 130 #define DEF_HIGHUID 60000 131 #endif 132 133 #ifndef DEF_INACTIVE 134 #define DEF_INACTIVE 0 135 #endif 136 137 #ifndef DEF_EXPIRE 138 #define DEF_EXPIRE NULL 139 #endif 140 141 #ifndef DEF_CLASS 142 #define DEF_CLASS "" 143 #endif 144 145 #ifndef WAITSECS 146 #define WAITSECS 10 147 #endif 148 149 #ifndef NOBODY_UID 150 #define NOBODY_UID 32767 151 #endif 152 153 /* some useful constants */ 154 enum { 155 MaxShellNameLen = 256, 156 MaxFileNameLen = MAXPATHLEN, 157 MaxUserNameLen = MAXLOGNAME, 158 MaxCommandLen = 2048, 159 PasswordLength = _PASSWORD_LEN, 160 161 LowGid = DEF_LOWUID, 162 HighGid = DEF_HIGHUID 163 }; 164 165 /* Full paths of programs used here */ 166 #define CHMOD "/bin/chmod" 167 #define CHOWN "/sbin/chown" 168 #define MKDIR "/bin/mkdir" 169 #define MV "/bin/mv" 170 #define NOLOGIN "/sbin/nologin" 171 #define PAX "/bin/pax" 172 #define RM "/bin/rm" 173 174 #define UNSET_EXPIRY "Null (unset)" 175 176 static int asystem(const char *fmt, ...) 177 __attribute__((__format__(__printf__, 1, 2))); 178 179 static int verbose; 180 181 /* if *cpp is non-null, free it, then assign `n' chars of `s' to it */ 182 static void 183 memsave(char **cpp, char *s, size_t n) 184 { 185 if (*cpp != NULL) { 186 FREE(*cpp); 187 } 188 NEWARRAY(char, *cpp, n + 1, exit(1)); 189 (void) memcpy(*cpp, s, n); 190 (*cpp)[n] = '\0'; 191 } 192 193 /* a replacement for system(3) */ 194 static int 195 asystem(const char *fmt, ...) 196 { 197 va_list vp; 198 char buf[MaxCommandLen]; 199 int ret; 200 201 va_start(vp, fmt); 202 (void) vsnprintf(buf, sizeof(buf), fmt, vp); 203 va_end(vp); 204 if (verbose) { 205 (void) printf("Command: %s\n", buf); 206 } 207 if ((ret = system(buf)) != 0) { 208 warnx("[Warning] can't system `%s'", buf); 209 } 210 return ret; 211 } 212 213 /* remove a users home directory, returning 1 for success (ie, no problems encountered) */ 214 static int 215 removehomedir(const char *user, int uid, const char *dir) 216 { 217 struct stat st; 218 219 /* userid not root? */ 220 if (uid == 0) { 221 warnx("Not deleting home directory `%s'; userid is 0", dir); 222 return 0; 223 } 224 225 /* directory exists (and is a directory!) */ 226 if (stat(dir, &st) < 0) { 227 warnx("Home directory `%s' doesn't exist", dir); 228 return 0; 229 } 230 if (!S_ISDIR(st.st_mode)) { 231 warnx("Home directory `%s' is not a directory", dir); 232 return 0; 233 } 234 235 /* userid matches directory owner? */ 236 if (st.st_uid != uid) { 237 warnx("User `%s' doesn't own directory `%s', not removed\n", 238 user, dir); 239 return 0; 240 } 241 242 (void) seteuid(uid); 243 /* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */ 244 (void) asystem("%s -rf %s > /dev/null 2>&1 || true", RM, dir); 245 (void) seteuid(0); 246 if (rmdir(dir) < 0) { 247 warnx("Unable to remove all files in `%s'\n", dir); 248 return 0; 249 } 250 return 1; 251 } 252 253 /* return 1 if all of `s' is numeric */ 254 static int 255 is_number(char *s) 256 { 257 for ( ; *s ; s++) { 258 if (!isdigit(*s)) { 259 return 0; 260 } 261 } 262 return 1; 263 } 264 265 /* 266 * check that the effective uid is 0 - called from funcs which will 267 * modify data and config files. 268 */ 269 static void 270 checkeuid(void) 271 { 272 if (geteuid() != 0) { 273 errx(EXIT_FAILURE, "Program must be run as root"); 274 } 275 } 276 277 /* copy any dot files into the user's home directory */ 278 static int 279 copydotfiles(char *skeldir, int uid, int gid, char *dir) 280 { 281 struct dirent *dp; 282 DIR *dirp; 283 int n; 284 285 if ((dirp = opendir(skeldir)) == NULL) { 286 warn("can't open source . files dir `%s'", skeldir); 287 return 0; 288 } 289 for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) { 290 if (strcmp(dp->d_name, ".") == 0 || 291 strcmp(dp->d_name, "..") == 0) { 292 continue; 293 } 294 n = 1; 295 } 296 (void) closedir(dirp); 297 if (n == 0) { 298 warnx("No \"dot\" initialisation files found"); 299 } else { 300 (void) asystem("cd %s; %s -rw -pe %s . %s", 301 skeldir, PAX, (verbose) ? "-v" : "", dir); 302 } 303 (void) asystem("%s -R -P %d:%d %s", CHOWN, uid, gid, dir); 304 (void) asystem("%s -R u+w %s", CHMOD, dir); 305 return n; 306 } 307 308 /* create a group entry with gid `gid' */ 309 static int 310 creategid(char *group, int gid, char *name) 311 { 312 struct stat st; 313 FILE *from; 314 FILE *to; 315 char buf[LINE_MAX]; 316 char f[MaxFileNameLen]; 317 int fd; 318 int cc; 319 320 if (getgrnam(group) != NULL) { 321 warnx("group `%s' already exists", group); 322 return 0; 323 } 324 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 325 warn("can't create gid for %s: can't open %s", group, 326 _PATH_GROUP); 327 return 0; 328 } 329 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 330 warn("can't lock `%s'", _PATH_GROUP); 331 } 332 (void) fstat(fileno(from), &st); 333 (void) snprintf(f, sizeof(f), "%s.XXXXXXXX", _PATH_GROUP); 334 if ((fd = mkstemp(f)) < 0) { 335 (void) fclose(from); 336 warn("can't create gid: mkstemp failed"); 337 return 0; 338 } 339 if ((to = fdopen(fd, "w")) == NULL) { 340 (void) fclose(from); 341 (void) close(fd); 342 (void) unlink(f); 343 warn("can't create gid: fdopen `%s' failed", f); 344 return 0; 345 } 346 while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) { 347 if (fwrite(buf, sizeof(char), cc, to) != cc) { 348 (void) fclose(from); 349 (void) close(fd); 350 (void) unlink(f); 351 warn("can't create gid: short write to `%s'", f); 352 return 0; 353 } 354 } 355 (void) fprintf(to, "%s:*:%d:%s\n", group, gid, name); 356 (void) fclose(from); 357 (void) fclose(to); 358 if (rename(f, _PATH_GROUP) < 0) { 359 warn("can't create gid: can't rename `%s' to `%s'", f, 360 _PATH_GROUP); 361 return 0; 362 } 363 (void) chmod(_PATH_GROUP, st.st_mode & 07777); 364 return 1; 365 } 366 367 /* modify the group entry with name `group' to be newent */ 368 static int 369 modify_gid(char *group, char *newent) 370 { 371 struct stat st; 372 FILE *from; 373 FILE *to; 374 char buf[LINE_MAX]; 375 char f[MaxFileNameLen]; 376 char *colon; 377 int groupc; 378 int entc; 379 int fd; 380 int cc; 381 382 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 383 warn("can't create gid for %s: can't open %s", group, _PATH_GROUP); 384 return 0; 385 } 386 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 387 warn("can't lock `%s'", _PATH_GROUP); 388 } 389 (void) fstat(fileno(from), &st); 390 (void) snprintf(f, sizeof(f), "%s.XXXXXXXX", _PATH_GROUP); 391 if ((fd = mkstemp(f)) < 0) { 392 (void) fclose(from); 393 warn("can't create gid: mkstemp failed"); 394 return 0; 395 } 396 if ((to = fdopen(fd, "w")) == NULL) { 397 (void) fclose(from); 398 (void) close(fd); 399 (void) unlink(f); 400 warn("can't create gid: fdopen `%s' failed", f); 401 return 0; 402 } 403 groupc = strlen(group); 404 while (fgets(buf, sizeof(buf), from) != NULL) { 405 cc = strlen(buf); 406 if (buf[cc - 1] != '\n') { 407 while (!feof(from) && fgetc(from) != '\n') 408 cc++; 409 warn("line `%s' too long (%d bytes), skipping", buf, 410 cc); 411 continue; 412 } 413 if ((colon = strchr(buf, ':')) == NULL) { 414 warn("badly formed entry `%s'", buf); 415 continue; 416 } 417 entc = (int)(colon - buf); 418 if (entc == groupc && strncmp(group, buf, entc) == 0) { 419 if (newent == NULL) { 420 continue; 421 } else { 422 cc = strlen(newent); 423 (void) strlcpy(buf, newent, sizeof(buf)); 424 } 425 } 426 if (fwrite(buf, sizeof(char), cc, to) != cc) { 427 (void) fclose(from); 428 (void) close(fd); 429 (void) unlink(f); 430 warn("can't create gid: short write to `%s'", f); 431 return 0; 432 } 433 } 434 (void) fclose(from); 435 (void) fclose(to); 436 if (rename(f, _PATH_GROUP) < 0) { 437 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP); 438 return 0; 439 } 440 (void) chmod(_PATH_GROUP, st.st_mode & 07777); 441 return 1; 442 } 443 444 /* modify the group entries for all `groups', by adding `user' */ 445 static int 446 append_group(char *user, int ngroups, char **groups) 447 { 448 struct group *grp; 449 struct stat st; 450 FILE *from; 451 FILE *to; 452 char buf[LINE_MAX]; 453 char f[MaxFileNameLen]; 454 char *colon; 455 int groupc; 456 int entc; 457 int fd; 458 int nc; 459 int cc; 460 int i; 461 int j; 462 463 for (i = 0 ; i < ngroups ; i++) { 464 if ((grp = getgrnam(groups[i])) == NULL) { 465 warnx("can't append group `%s' for user `%s'", 466 groups[i], user); 467 } else { 468 for (j = 0 ; grp->gr_mem[j] ; j++) { 469 if (strcmp(user, grp->gr_mem[j]) == 0) { 470 /* already in it */ 471 groups[i] = ""; 472 } 473 } 474 } 475 } 476 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 477 warn("can't append group for %s: can't open %s", user, 478 _PATH_GROUP); 479 return 0; 480 } 481 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 482 warn("can't lock `%s'", _PATH_GROUP); 483 } 484 (void) fstat(fileno(from), &st); 485 (void) snprintf(f, sizeof(f), "%s.XXXXXXXX", _PATH_GROUP); 486 if ((fd = mkstemp(f)) < 0) { 487 (void) fclose(from); 488 warn("can't create gid: mkstemp failed"); 489 return 0; 490 } 491 if ((to = fdopen(fd, "w")) == NULL) { 492 (void) fclose(from); 493 (void) close(fd); 494 (void) unlink(f); 495 warn("can't create gid: fdopen `%s' failed", f); 496 return 0; 497 } 498 while (fgets(buf, sizeof(buf), from) != NULL) { 499 cc = strlen(buf); 500 if (buf[cc - 1] != '\n') { 501 while (!feof(from) && fgetc(from) != '\n') 502 cc++; 503 warn("line `%s' too long (%d bytes), skipping", buf, 504 cc); 505 continue; 506 } 507 if ((colon = strchr(buf, ':')) == NULL) { 508 warn("badly formed entry `%s'", buf); 509 continue; 510 } 511 entc = (int)(colon - buf); 512 for (i = 0 ; i < ngroups ; i++) { 513 if ((groupc = strlen(groups[i])) == 0) { 514 continue; 515 } 516 if (cc >= sizeof(buf)) { 517 warn("line `%s' too long, skipping", buf); 518 continue; 519 } 520 if (entc == groupc && strncmp(groups[i], buf, entc) == 0) { 521 if ((nc = snprintf(&buf[cc - 1], 522 sizeof(buf) - cc + 1, 523 "%s%s\n", 524 (buf[cc - 2] == ':') ? "" : ",", 525 user)) >= sizeof(buf) - cc + 1) { 526 warnx("Warning: group `%s' entry too long", groups[i]); 527 } 528 cc += nc - 1; 529 } 530 } 531 if (fwrite(buf, sizeof(char), cc, to) != cc) { 532 (void) fclose(from); 533 (void) close(fd); 534 (void) unlink(f); 535 warn("can't create gid: short write to `%s'", f); 536 return 0; 537 } 538 } 539 (void) fclose(from); 540 (void) fclose(to); 541 if (rename(f, _PATH_GROUP) < 0) { 542 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP); 543 return 0; 544 } 545 (void) chmod(_PATH_GROUP, st.st_mode & 07777); 546 return 1; 547 } 548 549 /* return 1 if `login' is a valid login name */ 550 static int 551 valid_login(char *login) 552 { 553 char *cp; 554 555 for (cp = login ; *cp ; cp++) { 556 if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-') { 557 return 0; 558 } 559 } 560 return 1; 561 } 562 563 /* return 1 if `group' is a valid group name */ 564 static int 565 valid_group(char *group) 566 { 567 char *cp; 568 569 for (cp = group ; *cp ; cp++) { 570 if (!isalnum(*cp)) { 571 return 0; 572 } 573 } 574 return 1; 575 } 576 577 /* find the next gid in the range lo .. hi */ 578 static int 579 getnextgid(int *gidp, int lo, int hi) 580 { 581 for (*gidp = lo ; *gidp < hi ; *gidp += 1) { 582 if (getgrgid((gid_t)*gidp) == NULL) { 583 return 1; 584 } 585 } 586 return 0; 587 } 588 589 #ifdef EXTENSIONS 590 /* save a range of uids */ 591 static int 592 save_range(user_t *up, char *cp) 593 { 594 int from; 595 int to; 596 int i; 597 598 if (up->u_rsize == 0) { 599 up->u_rsize = 32; 600 NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0)); 601 } else if (up->u_rc == up->u_rsize) { 602 up->u_rsize *= 2; 603 RENEW(range_t, up->u_rv, up->u_rsize, return(0)); 604 } 605 if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) { 606 for (i = up->u_defrc ; i < up->u_rc ; i++) { 607 if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) { 608 break; 609 } 610 } 611 if (i == up->u_rc) { 612 up->u_rv[up->u_rc].r_from = from; 613 up->u_rv[up->u_rc].r_to = to; 614 up->u_rc += 1; 615 } 616 } else { 617 warnx("Bad range `%s'", cp); 618 return 0; 619 } 620 return 1; 621 } 622 #endif 623 624 /* set the defaults in the defaults file */ 625 static int 626 setdefaults(user_t *up) 627 { 628 char template[MaxFileNameLen]; 629 FILE *fp; 630 int ret; 631 int fd; 632 #ifdef EXTENSIONS 633 int i; 634 #endif 635 636 (void) snprintf(template, sizeof(template), "%s.XXXXXXXX", CONFFILE); 637 if ((fd = mkstemp(template)) < 0) { 638 warnx("can't mkstemp `%s' for writing", CONFFILE); 639 return 0; 640 } 641 if ((fp = fdopen(fd, "w")) == NULL) { 642 warn("can't fdopen `%s' for writing", CONFFILE); 643 return 0; 644 } 645 ret = 1; 646 if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 || 647 fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 || 648 fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 || 649 fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 || 650 #ifdef EXTENSIONS 651 fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 || 652 #endif 653 fprintf(fp, "inactive\t%d\n", up->u_inactive) <= 0 || 654 fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ? UNSET_EXPIRY : up->u_expire) <= 0 || 655 fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? "false" : "true") <= 0) { 656 warn("can't write to `%s'", CONFFILE); 657 ret = 0; 658 } 659 #ifdef EXTENSIONS 660 for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0 ; i < up->u_rc ; i++) { 661 if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) { 662 warn("can't write to `%s'", CONFFILE); 663 ret = 0; 664 } 665 } 666 #endif 667 (void) fclose(fp); 668 if (ret) { 669 ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0)); 670 } 671 return ret; 672 } 673 674 /* read the defaults file */ 675 static void 676 read_defaults(user_t *up) 677 { 678 struct stat st; 679 size_t lineno; 680 size_t len; 681 FILE *fp; 682 char *cp; 683 char *s; 684 685 memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP)); 686 memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR)); 687 memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR)); 688 memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL)); 689 memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT)); 690 #ifdef EXTENSIONS 691 memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS)); 692 #endif 693 up->u_rsize = 16; 694 up->u_defrc = 0; 695 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1)); 696 up->u_inactive = DEF_INACTIVE; 697 up->u_expire = DEF_EXPIRE; 698 if ((fp = fopen(CONFFILE, "r")) == NULL) { 699 if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) { 700 warn("can't create `%s' defaults file", CONFFILE); 701 } 702 fp = fopen(CONFFILE, "r"); 703 } 704 if (fp != NULL) { 705 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) { 706 if (strncmp(s, "group", 5) == 0) { 707 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 708 } 709 memsave(&up->u_primgrp, cp, strlen(cp)); 710 } else if (strncmp(s, "base_dir", 8) == 0) { 711 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 712 } 713 memsave(&up->u_basedir, cp, strlen(cp)); 714 } else if (strncmp(s, "skel_dir", 8) == 0) { 715 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 716 } 717 memsave(&up->u_skeldir, cp, strlen(cp)); 718 } else if (strncmp(s, "shell", 5) == 0) { 719 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 720 } 721 memsave(&up->u_shell, cp, strlen(cp)); 722 } else if (strncmp(s, "password", 8) == 0) { 723 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 724 } 725 memsave(&up->u_password, cp, strlen(cp)); 726 #ifdef EXTENSIONS 727 } else if (strncmp(s, "class", 5) == 0) { 728 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 729 } 730 memsave(&up->u_class, cp, strlen(cp)); 731 #endif 732 } else if (strncmp(s, "inactive", 8) == 0) { 733 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 734 } 735 up->u_inactive = atoi(cp); 736 #ifdef EXTENSIONS 737 } else if (strncmp(s, "range", 5) == 0) { 738 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) { 739 } 740 (void) save_range(up, cp); 741 #endif 742 #ifdef EXTENSIONS 743 } else if (strncmp(s, "preserve", 8) == 0) { 744 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) { 745 } 746 up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 : 747 (strncmp(cp, "yes", 3) == 0) ? 1 : 748 atoi(cp); 749 #endif 750 } else if (strncmp(s, "expire", 6) == 0) { 751 for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) { 752 } 753 if (strcmp(cp, UNSET_EXPIRY) == 0) { 754 if (up->u_expire) { 755 FREE(up->u_expire); 756 } 757 up->u_expire = NULL; 758 } else { 759 memsave(&up->u_expire, cp, strlen(cp)); 760 } 761 } 762 (void) free(s); 763 } 764 (void) fclose(fp); 765 } 766 if (up->u_rc == 0) { 767 up->u_rv[up->u_rc].r_from = DEF_LOWUID; 768 up->u_rv[up->u_rc].r_to = DEF_HIGHUID; 769 up->u_rc += 1; 770 } 771 up->u_defrc = up->u_rc; 772 } 773 774 /* return the next valid unused uid */ 775 static int 776 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid) 777 { 778 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) { 779 if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) { 780 if (sync_uid_gid) { 781 if (getgrgid((gid_t)(*uid)) == NULL) { 782 return 1; 783 } 784 } else { 785 return 1; 786 } 787 } 788 } 789 return 0; 790 } 791 792 /* add a user */ 793 static int 794 adduser(char *login, user_t *up) 795 { 796 struct group *grp; 797 struct stat st; 798 struct tm tm; 799 time_t expire; 800 char password[PasswordLength + 1]; 801 char home[MaxFileNameLen]; 802 char buf[MaxFileNameLen]; 803 int sync_uid_gid; 804 int masterfd; 805 int ptmpfd; 806 int gid; 807 int cc; 808 int i; 809 810 if (!valid_login(login)) { 811 errx(EXIT_FAILURE, "`%s' is not a valid login name", login); 812 } 813 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 814 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD); 815 } 816 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 817 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD); 818 } 819 pw_init(); 820 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 821 (void) close(masterfd); 822 err(EXIT_FAILURE, "can't obtain pw_lock"); 823 } 824 while ((cc = read(masterfd, buf, sizeof(buf))) > 0) { 825 if (write(ptmpfd, buf, (size_t)(cc)) != cc) { 826 (void) close(masterfd); 827 (void) close(ptmpfd); 828 (void) pw_abort(); 829 err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc); 830 } 831 } 832 /* if no uid was specified, get next one in [low_uid..high_uid] range */ 833 sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0); 834 if (up->u_uid == -1) { 835 for (i = 0 ; i < up->u_rc ; i++) { 836 if (getnextuid(sync_uid_gid, &up->u_uid, up->u_rv[i].r_from, up->u_rv[i].r_to)) { 837 break; 838 } 839 } 840 if (i == up->u_rc) { 841 (void) close(ptmpfd); 842 (void) pw_abort(); 843 errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid); 844 } 845 } 846 /* check uid isn't already allocated */ 847 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) { 848 (void) close(ptmpfd); 849 (void) pw_abort(); 850 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid); 851 } 852 /* if -g=uid was specified, check gid is unused */ 853 if (sync_uid_gid) { 854 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 855 (void) close(ptmpfd); 856 (void) pw_abort(); 857 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid); 858 } 859 gid = up->u_uid; 860 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 861 gid = grp->gr_gid; 862 } else if (is_number(up->u_primgrp) && 863 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) { 864 gid = grp->gr_gid; 865 } else { 866 (void) close(ptmpfd); 867 (void) pw_abort(); 868 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp); 869 } 870 /* check name isn't already in use */ 871 if (!(up->u_flags & F_DUPUID) && getpwnam(login) != NULL) { 872 (void) close(ptmpfd); 873 (void) pw_abort(); 874 errx(EXIT_FAILURE, "already a `%s' user", login); 875 } 876 if (up->u_flags & F_HOMEDIR) { 877 (void) strlcpy(home, up->u_home, sizeof(home)); 878 } else { 879 /* if home directory hasn't been given, make it up */ 880 (void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login); 881 } 882 expire = 0; 883 if (up->u_expire != NULL) { 884 (void) memset(&tm, 0, sizeof(tm)); 885 if (strptime(up->u_expire, "%c", &tm) == NULL) { 886 warnx("invalid time format `%s'", optarg); 887 } else { 888 expire = mktime(&tm); 889 } 890 } 891 if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) { 892 warnx("Warning: home directory `%s' doesn't exist, and -m was" 893 " not specified", home); 894 } 895 if (up->u_password != NULL && 896 strlen(up->u_password) <= PasswordLength) { 897 (void) strlcpy(password, up->u_password, sizeof(password)); 898 } else { 899 (void) strlcpy(password, "*", sizeof(password)); 900 if (up->u_password != NULL) { 901 warnx("Password `%s' is invalid: setting it to `%s'", 902 up->u_password, password); 903 } 904 } 905 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%d:%ld:%s:%s:%s\n", 906 login, 907 password, 908 up->u_uid, 909 gid, 910 #ifdef EXTENSIONS 911 up->u_class, 912 #else 913 "", 914 #endif 915 up->u_inactive, 916 (long) expire, 917 up->u_comment, 918 home, 919 up->u_shell); 920 if (cc >= sizeof(buf) || 921 (strchr(up->u_comment, '&') != NULL && 922 cc + strlen(login) >= sizeof(buf))) { 923 (void) close(ptmpfd); 924 (void) pw_abort(); 925 errx(EXIT_FAILURE, "can't add `%s', line too long", buf); 926 } 927 if (write(ptmpfd, buf, (size_t) cc) != cc) { 928 (void) close(ptmpfd); 929 (void) pw_abort(); 930 err(EXIT_FAILURE, "can't add `%s'", buf); 931 } 932 if (up->u_flags & F_MKDIR) { 933 if (lstat(home, &st) == 0) { 934 (void) close(ptmpfd); 935 (void) pw_abort(); 936 errx(EXIT_FAILURE, "home directory `%s' already exists", 937 home); 938 } else { 939 if (asystem("%s -p %s", MKDIR, home) != 0) { 940 (void) close(ptmpfd); 941 (void) pw_abort(); 942 err(EXIT_FAILURE, "can't mkdir `%s'", home); 943 } 944 (void) copydotfiles(up->u_skeldir, up->u_uid, gid, home); 945 } 946 } 947 if (strcmp(up->u_primgrp, "=uid") == 0 && getgrnam(login) == NULL && 948 !creategid(login, gid, login)) { 949 (void) close(ptmpfd); 950 (void) pw_abort(); 951 err(EXIT_FAILURE, "can't create gid %d for login name %s", 952 gid, login); 953 } 954 if (up->u_groupc > 0 && !append_group(login, up->u_groupc, up->u_groupv)) { 955 (void) close(ptmpfd); 956 (void) pw_abort(); 957 errx(EXIT_FAILURE, "can't append `%s' to new groups", login); 958 } 959 (void) close(ptmpfd); 960 if (pw_mkdb(login, 0) < 0) { 961 err(EXIT_FAILURE, "pw_mkdb failed"); 962 } 963 return 1; 964 } 965 966 /* modify a user */ 967 static int 968 moduser(char *login, char *newlogin, user_t *up) 969 { 970 struct passwd *pwp; 971 struct group *grp; 972 struct tm tm; 973 const char *homedir; 974 char newdir[MaxFileNameLen]; 975 char buf[LINE_MAX]; 976 size_t colonc, len, loginc; 977 FILE *master; 978 char *colon, *line; 979 int masterfd; 980 int ptmpfd; 981 int rval; 982 983 if (!valid_login(newlogin)) { 984 errx(EXIT_FAILURE, "`%s' is not a valid login name", login); 985 } 986 if ((pwp = getpwnam(login)) == NULL) { 987 errx(EXIT_FAILURE, "No such user `%s'", login); 988 } 989 /* keep dir name in case we need it for '-m' */ 990 homedir = pwp->pw_dir; 991 992 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 993 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD); 994 } 995 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 996 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD); 997 } 998 pw_init(); 999 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 1000 (void) close(masterfd); 1001 err(EXIT_FAILURE, "can't obtain pw_lock"); 1002 } 1003 if ((master = fdopen(masterfd, "r")) == NULL) { 1004 (void) close(masterfd); 1005 (void) close(ptmpfd); 1006 (void) pw_abort(); 1007 err(EXIT_FAILURE, "can't fdopen fd for %s", _PATH_MASTERPASSWD); 1008 } 1009 if (up != NULL) { 1010 if (up->u_flags & F_USERNAME) { 1011 /* if changing name, check new name isn't already in use */ 1012 if (strcmp(login, newlogin) != 0 && getpwnam(newlogin) != NULL) { 1013 (void) close(ptmpfd); 1014 (void) pw_abort(); 1015 errx(EXIT_FAILURE, "already a `%s' user", newlogin); 1016 } 1017 pwp->pw_name = newlogin; 1018 1019 /* 1020 * Provide a new directory name in case the 1021 * home directory is to be moved. 1022 */ 1023 if (up->u_flags & F_MKDIR) { 1024 (void) snprintf(newdir, sizeof(newdir), 1025 "%s/%s", up->u_basedir, newlogin); 1026 pwp->pw_dir = newdir; 1027 } 1028 } 1029 if (up->u_flags & F_PASSWORD) { 1030 if (up->u_password != NULL && strlen(up->u_password) == PasswordLength) 1031 pwp->pw_passwd = up->u_password; 1032 } 1033 if (up->u_flags & F_UID) { 1034 /* check uid isn't already allocated */ 1035 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) { 1036 (void) close(ptmpfd); 1037 (void) pw_abort(); 1038 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid); 1039 } 1040 pwp->pw_uid = up->u_uid; 1041 } 1042 if (up->u_flags & F_GROUP) { 1043 /* if -g=uid was specified, check gid is unused */ 1044 if (strcmp(up->u_primgrp, "=uid") == 0) { 1045 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 1046 (void) close(ptmpfd); 1047 (void) pw_abort(); 1048 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid); 1049 } 1050 pwp->pw_gid = up->u_uid; 1051 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 1052 pwp->pw_gid = grp->gr_gid; 1053 } else if (is_number(up->u_primgrp) && 1054 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) { 1055 pwp->pw_gid = grp->gr_gid; 1056 } else { 1057 (void) close(ptmpfd); 1058 (void) pw_abort(); 1059 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp); 1060 } 1061 } 1062 if (up->u_flags |= F_INACTIVE) 1063 pwp->pw_change = up->u_inactive; 1064 if (up->u_flags & F_EXPIRE) { 1065 (void) memset(&tm, 0, sizeof(tm)); 1066 if (strptime(up->u_expire, "%c", &tm) == NULL) 1067 warnx("invalid time format `%s'", optarg); 1068 else 1069 pwp->pw_expire = mktime(&tm); 1070 } 1071 if (up->u_flags & F_COMMENT) 1072 pwp->pw_gecos = up->u_comment; 1073 if (up->u_flags & F_HOMEDIR) 1074 pwp->pw_dir = up->u_home; 1075 if (up->u_flags & F_SHELL) 1076 pwp->pw_shell = up->u_shell; 1077 #ifdef EXTENSIONS 1078 if (up->u_flags & F_CLASS) 1079 pwp->pw_class = up->u_class; 1080 #endif 1081 } 1082 loginc = strlen(login); 1083 while ((line = fgetln(master, &len)) != NULL) { 1084 if ((colon = strchr(line, ':')) == NULL) { 1085 warnx("Malformed entry `%s'. Skipping", line); 1086 continue; 1087 } 1088 colonc = (size_t)(colon - line); 1089 if (strncmp(login, line, loginc) == 0 && loginc == colonc) { 1090 if (up != NULL) { 1091 len = snprintf(buf, sizeof(buf), 1092 "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", 1093 newlogin, 1094 pwp->pw_passwd, 1095 pwp->pw_uid, 1096 pwp->pw_gid, 1097 #ifdef EXTENSIONS 1098 pwp->pw_class, 1099 #else 1100 "", 1101 #endif 1102 (long)pwp->pw_change, 1103 (long)pwp->pw_expire, 1104 pwp->pw_gecos, 1105 pwp->pw_dir, 1106 pwp->pw_shell); 1107 if (len >= sizeof(buf) || 1108 (strchr(up->u_comment, '&') != NULL && 1109 len + strlen(newlogin) >= sizeof(buf))) { 1110 (void) close(ptmpfd); 1111 (void) pw_abort(); 1112 errx(EXIT_FAILURE, "can't add `%s', line too long (%d bytes)", buf, len + strlen(newlogin)); 1113 } 1114 if (write(ptmpfd, buf, len) != len) { 1115 (void) close(ptmpfd); 1116 (void) pw_abort(); 1117 err(EXIT_FAILURE, "can't add `%s'", buf); 1118 } 1119 } 1120 } else if ((colonc = write(ptmpfd, line, len)) != len) { 1121 (void) close(masterfd); 1122 (void) close(ptmpfd); 1123 (void) pw_abort(); 1124 err(EXIT_FAILURE, "short write to /etc/ptmp (%lld not %lld chars)", 1125 (long long)colonc, (long long)len); 1126 } 1127 } 1128 if (up != NULL) { 1129 if ((up->u_flags & F_MKDIR) && 1130 asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) { 1131 (void) close(ptmpfd); 1132 (void) pw_abort(); 1133 err(EXIT_FAILURE, "can't move `%s' to `%s'", 1134 homedir, pwp->pw_dir); 1135 } 1136 if (up->u_groupc > 0 && 1137 !append_group(newlogin, up->u_groupc, up->u_groupv)) { 1138 (void) close(ptmpfd); 1139 (void) pw_abort(); 1140 errx(EXIT_FAILURE, "can't append `%s' to new groups", 1141 newlogin); 1142 } 1143 } 1144 (void) close(ptmpfd); 1145 if (up != NULL && strcmp(login, newlogin) == 0) 1146 rval = pw_mkdb(login, 0); 1147 else 1148 rval = pw_mkdb(NULL, 0); 1149 if (rval == -1) 1150 err(EXIT_FAILURE, "pw_mkdb failed"); 1151 1152 return 1; 1153 } 1154 1155 1156 #ifdef EXTENSIONS 1157 /* see if we can find out the user struct */ 1158 static struct passwd * 1159 find_user_info(char *name) 1160 { 1161 struct passwd *pwp; 1162 1163 if ((pwp = getpwnam(name)) != NULL) { 1164 return pwp; 1165 } 1166 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) { 1167 return pwp; 1168 } 1169 return NULL; 1170 } 1171 #endif 1172 1173 #ifdef EXTENSIONS 1174 /* see if we can find out the group struct */ 1175 static struct group * 1176 find_group_info(char *name) 1177 { 1178 struct group *grp; 1179 1180 if ((grp = getgrnam(name)) != NULL) { 1181 return grp; 1182 } 1183 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) { 1184 return grp; 1185 } 1186 return NULL; 1187 } 1188 #endif 1189 1190 /* print out usage message, and then exit */ 1191 void 1192 usermgmt_usage(const char *prog) 1193 { 1194 if (strcmp(prog, "useradd") == 0) { 1195 (void) fprintf(stderr, "usage: %s -D [-b basedir] [-e expiry] " 1196 "[-f changetime] [-g group]\n\t\t[-k skeletondir] " 1197 "[-r low..high] [-s shell] [-L class]\n", prog); 1198 (void) fprintf(stderr, "usage: %s [-mov] [-G group[,group,...]]" 1199 " [-b basedir] [-c comment]\n\t\t" 1200 "[-d homedir] [-e expiry] [-f changetime] [-g group]\n\t\t" 1201 "[-k skeletondir] [-p password] " 1202 "[-r lowuid..highuid]\n\t\t[-s shell] [-u uid] [-L class] " 1203 "user\n", prog); 1204 } else if (strcmp(prog, "usermod") == 0) { 1205 (void) fprintf(stderr, "usage: %s [-mov] [-G group[,group,...]]" 1206 " [-c comment] [-d homedir]\n\t\t" 1207 "[-e expire] [-f changetime] [-g group] [-l newname]\n\t\t" 1208 "[-p password] [-s shell] [-u uid] [-L class] user\n", 1209 prog); 1210 } else if (strcmp(prog, "userdel") == 0) { 1211 (void) fprintf(stderr, "usage: %s -D [-p preserve]\n", prog); 1212 (void) fprintf(stderr, "usage: %s [-prv] user\n", prog); 1213 #ifdef EXTENSIONS 1214 } else if (strcmp(prog, "userinfo") == 0) { 1215 (void) fprintf(stderr, "usage: %s [-ev] user\n", prog); 1216 #endif 1217 } else if (strcmp(prog, "groupadd") == 0) { 1218 (void) fprintf(stderr, "usage: %s [-ov] [-g gid] group\n", 1219 prog); 1220 } else if (strcmp(prog, "groupdel") == 0) { 1221 (void) fprintf(stderr, "usage: %s [-v] group\n", prog); 1222 } else if (strcmp(prog, "groupmod") == 0) { 1223 (void) fprintf(stderr, "usage: %s [-ov] [-g gid] [-n newname] " 1224 "group\n", prog); 1225 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) { 1226 (void) fprintf(stderr, "usage: %s [ add | del | mod " 1227 #ifdef EXTENSIONS 1228 "| info " 1229 #endif 1230 "] ...\n", 1231 prog); 1232 #ifdef EXTENSIONS 1233 } else if (strcmp(prog, "groupinfo") == 0) { 1234 (void) fprintf(stderr, "usage: %s [-ev] group\n", prog); 1235 #endif 1236 } else { 1237 (void) fprintf(stderr, "This program must be called as {user,group}{add,del,mod,info},\n%s is not an understood name.\n", prog); 1238 } 1239 exit(EXIT_FAILURE); 1240 /* NOTREACHED */ 1241 } 1242 1243 #ifdef EXTENSIONS 1244 #define ADD_OPT_EXTENSIONS "p:r:vL:" 1245 #else 1246 #define ADD_OPT_EXTENSIONS 1247 #endif 1248 1249 int 1250 useradd(int argc, char **argv) 1251 { 1252 user_t u; 1253 int defaultfield; 1254 int bigD; 1255 int c; 1256 #ifdef EXTENSIONS 1257 int i; 1258 #endif 1259 1260 (void) memset(&u, 0, sizeof(u)); 1261 read_defaults(&u); 1262 u.u_uid = -1; 1263 defaultfield = bigD = 0; 1264 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) { 1265 switch(c) { 1266 case 'D': 1267 bigD = 1; 1268 break; 1269 case 'G': 1270 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL && 1271 u.u_groupc < NGROUPS_MAX - 2) { 1272 if (u.u_groupv[u.u_groupc][0] != 0) { 1273 u.u_groupc++; 1274 } 1275 } 1276 if (optarg != NULL) { 1277 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX - 2); 1278 } 1279 break; 1280 case 'b': 1281 defaultfield = 1; 1282 memsave(&u.u_basedir, optarg, strlen(optarg)); 1283 break; 1284 case 'c': 1285 memsave(&u.u_comment, optarg, strlen(optarg)); 1286 break; 1287 case 'd': 1288 memsave(&u.u_home, optarg, strlen(optarg)); 1289 u.u_flags |= F_HOMEDIR; 1290 break; 1291 case 'e': 1292 defaultfield = 1; 1293 memsave(&u.u_expire, optarg, strlen(optarg)); 1294 break; 1295 case 'f': 1296 defaultfield = 1; 1297 u.u_inactive = atoi(optarg); 1298 break; 1299 case 'g': 1300 defaultfield = 1; 1301 memsave(&u.u_primgrp, optarg, strlen(optarg)); 1302 break; 1303 case 'k': 1304 memsave(&u.u_skeldir, optarg, strlen(optarg)); 1305 break; 1306 #ifdef EXTENSIONS 1307 case 'L': 1308 defaultfield = 1; 1309 memsave(&u.u_class, optarg, strlen(optarg)); 1310 break; 1311 #endif 1312 case 'm': 1313 u.u_flags |= F_MKDIR; 1314 break; 1315 case 'o': 1316 u.u_flags |= F_DUPUID; 1317 break; 1318 #ifdef EXTENSIONS 1319 case 'p': 1320 memsave(&u.u_password, optarg, strlen(optarg)); 1321 break; 1322 #endif 1323 #ifdef EXTENSIONS 1324 case 'r': 1325 defaultfield = 1; 1326 (void) save_range(&u, optarg); 1327 break; 1328 #endif 1329 case 's': 1330 defaultfield = 1; 1331 memsave(&u.u_shell, optarg, strlen(optarg)); 1332 break; 1333 case 'u': 1334 if (!is_number(optarg)) { 1335 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric"); 1336 } 1337 u.u_uid = atoi(optarg); 1338 break; 1339 #ifdef EXTENSIONS 1340 case 'v': 1341 verbose = 1; 1342 break; 1343 #endif 1344 } 1345 } 1346 if (bigD) { 1347 if (defaultfield) { 1348 checkeuid(); 1349 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 1350 } 1351 (void) printf("group\t\t%s\n", u.u_primgrp); 1352 (void) printf("base_dir\t%s\n", u.u_basedir); 1353 (void) printf("skel_dir\t%s\n", u.u_skeldir); 1354 (void) printf("shell\t\t%s\n", u.u_shell); 1355 #ifdef EXTENSIONS 1356 (void) printf("class\t\t%s\n", u.u_class); 1357 #endif 1358 (void) printf("inactive\t%d\n", u.u_inactive); 1359 (void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire); 1360 #ifdef EXTENSIONS 1361 for (i = 0 ; i < u.u_rc ; i++) { 1362 (void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to); 1363 } 1364 #endif 1365 return EXIT_SUCCESS; 1366 } 1367 argc -= optind; 1368 argv += optind; 1369 if (argc != 1) { 1370 usermgmt_usage("useradd"); 1371 } 1372 checkeuid(); 1373 return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE; 1374 } 1375 1376 #ifdef EXTENSIONS 1377 #define MOD_OPT_EXTENSIONS "p:vL:" 1378 #else 1379 #define MOD_OPT_EXTENSIONS 1380 #endif 1381 1382 int 1383 usermod(int argc, char **argv) 1384 { 1385 user_t u; 1386 char newuser[MaxUserNameLen + 1]; 1387 int c, have_new_user; 1388 1389 (void) memset(&u, 0, sizeof(u)); 1390 (void) memset(newuser, 0, sizeof(newuser)); 1391 read_defaults(&u); 1392 free(u.u_primgrp); 1393 u.u_primgrp = NULL; 1394 have_new_user = 0; 1395 while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) { 1396 switch(c) { 1397 case 'G': 1398 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL && 1399 u.u_groupc < NGROUPS_MAX - 2) { 1400 if (u.u_groupv[u.u_groupc][0] != 0) { 1401 u.u_groupc++; 1402 } 1403 } 1404 if (optarg != NULL) { 1405 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX - 2); 1406 } 1407 u.u_flags |= F_SECGROUP; 1408 break; 1409 case 'c': 1410 memsave(&u.u_comment, optarg, strlen(optarg)); 1411 u.u_flags |= F_COMMENT; 1412 break; 1413 case 'd': 1414 memsave(&u.u_home, optarg, strlen(optarg)); 1415 u.u_flags |= F_HOMEDIR; 1416 break; 1417 case 'e': 1418 memsave(&u.u_expire, optarg, strlen(optarg)); 1419 u.u_flags |= F_EXPIRE; 1420 break; 1421 case 'f': 1422 u.u_inactive = atoi(optarg); 1423 u.u_flags |= F_INACTIVE; 1424 break; 1425 case 'g': 1426 memsave(&u.u_primgrp, optarg, strlen(optarg)); 1427 u.u_flags |= F_GROUP; 1428 break; 1429 case 'l': 1430 (void) strlcpy(newuser, optarg, sizeof(newuser)); 1431 have_new_user = 1; 1432 u.u_flags |= F_USERNAME; 1433 break; 1434 #ifdef EXTENSIONS 1435 case 'L': 1436 memsave(&u.u_class, optarg, strlen(optarg)); 1437 u.u_flags |= F_CLASS; 1438 break; 1439 #endif 1440 case 'm': 1441 u.u_flags |= F_MKDIR; 1442 break; 1443 case 'o': 1444 u.u_flags |= F_DUPUID; 1445 break; 1446 #ifdef EXTENSIONS 1447 case 'p': 1448 memsave(&u.u_password, optarg, strlen(optarg)); 1449 u.u_flags |= F_PASSWORD; 1450 break; 1451 #endif 1452 case 's': 1453 memsave(&u.u_shell, optarg, strlen(optarg)); 1454 u.u_flags |= F_SHELL; 1455 break; 1456 case 'u': 1457 if (!is_number(optarg)) { 1458 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric"); 1459 } 1460 u.u_uid = atoi(optarg); 1461 u.u_flags |= F_UID; 1462 break; 1463 #ifdef EXTENSIONS 1464 case 'v': 1465 verbose = 1; 1466 break; 1467 #endif 1468 } 1469 } 1470 if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) && 1471 !(u.u_flags & F_USERNAME)) { 1472 warnx("option 'm' useless without 'd' or 'l' -- ignored"); 1473 u.u_flags &= ~F_MKDIR; 1474 } 1475 argc -= optind; 1476 argv += optind; 1477 if (argc != 1) { 1478 usermgmt_usage("usermod"); 1479 } 1480 checkeuid(); 1481 return moduser(*argv, (have_new_user) ? newuser : *argv, &u) ? 1482 EXIT_SUCCESS : EXIT_FAILURE; 1483 } 1484 1485 #ifdef EXTENSIONS 1486 #define DEL_OPT_EXTENSIONS "Dp:v" 1487 #else 1488 #define DEL_OPT_EXTENSIONS 1489 #endif 1490 1491 int 1492 userdel(int argc, char **argv) 1493 { 1494 struct passwd *pwp; 1495 user_t u; 1496 char password[PasswordLength + 1]; 1497 int defaultfield; 1498 int rmhome; 1499 int bigD; 1500 int c; 1501 1502 (void) memset(&u, 0, sizeof(u)); 1503 read_defaults(&u); 1504 defaultfield = bigD = rmhome = 0; 1505 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) { 1506 switch(c) { 1507 #ifdef EXTENSIONS 1508 case 'D': 1509 bigD = 1; 1510 break; 1511 #endif 1512 #ifdef EXTENSIONS 1513 case 'p': 1514 defaultfield = 1; 1515 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 : 1516 (strcmp(optarg, "yes") == 0) ? 1 : 1517 atoi(optarg); 1518 break; 1519 #endif 1520 case 'r': 1521 rmhome = 1; 1522 break; 1523 #ifdef EXTENSIONS 1524 case 'v': 1525 verbose = 1; 1526 break; 1527 #endif 1528 } 1529 } 1530 #ifdef EXTENSIONS 1531 if (bigD) { 1532 if (defaultfield) { 1533 checkeuid(); 1534 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 1535 } 1536 (void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false"); 1537 return EXIT_SUCCESS; 1538 } 1539 #endif 1540 argc -= optind; 1541 argv += optind; 1542 if (argc != 1) { 1543 usermgmt_usage("userdel"); 1544 } 1545 checkeuid(); 1546 if ((pwp = getpwnam(*argv)) == NULL) { 1547 warnx("No such user `%s'", *argv); 1548 return EXIT_FAILURE; 1549 } 1550 if (rmhome) 1551 (void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir); 1552 if (u.u_preserve) { 1553 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN)); 1554 (void) strlcpy(password, "*", sizeof(password)); 1555 memsave(&u.u_password, password, PasswordLength); 1556 u.u_flags |= F_PASSWORD; 1557 return moduser(*argv, *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE; 1558 } 1559 return moduser(*argv, *argv, NULL) ? EXIT_SUCCESS : EXIT_FAILURE; 1560 } 1561 1562 #ifdef EXTENSIONS 1563 #define GROUP_ADD_OPT_EXTENSIONS "v" 1564 #else 1565 #define GROUP_ADD_OPT_EXTENSIONS 1566 #endif 1567 1568 /* add a group */ 1569 int 1570 groupadd(int argc, char **argv) 1571 { 1572 int dupgid; 1573 int gid; 1574 int c; 1575 1576 gid = -1; 1577 dupgid = 0; 1578 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) { 1579 switch(c) { 1580 case 'g': 1581 if (!is_number(optarg)) { 1582 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric"); 1583 } 1584 gid = atoi(optarg); 1585 break; 1586 case 'o': 1587 dupgid = 1; 1588 break; 1589 #ifdef EXTENSIONS 1590 case 'v': 1591 verbose = 1; 1592 break; 1593 #endif 1594 } 1595 } 1596 argc -= optind; 1597 argv += optind; 1598 if (argc != 1) { 1599 usermgmt_usage("groupadd"); 1600 } 1601 checkeuid(); 1602 if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) { 1603 err(EXIT_FAILURE, "can't add group: can't get next gid"); 1604 } 1605 if (!dupgid && getgrgid((gid_t) gid) != NULL) { 1606 errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid); 1607 } 1608 if (!valid_group(*argv)) { 1609 warnx("warning - invalid group name `%s'", *argv); 1610 } 1611 if (!creategid(*argv, gid, "")) { 1612 err(EXIT_FAILURE, "can't add group: problems with %s file", 1613 _PATH_GROUP); 1614 } 1615 return EXIT_SUCCESS; 1616 } 1617 1618 #ifdef EXTENSIONS 1619 #define GROUP_DEL_OPT_EXTENSIONS "v" 1620 #else 1621 #define GROUP_DEL_OPT_EXTENSIONS 1622 #endif 1623 1624 /* remove a group */ 1625 int 1626 groupdel(int argc, char **argv) 1627 { 1628 int c; 1629 1630 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) { 1631 switch(c) { 1632 #ifdef EXTENSIONS 1633 case 'v': 1634 verbose = 1; 1635 break; 1636 #endif 1637 } 1638 } 1639 argc -= optind; 1640 argv += optind; 1641 if (argc != 1) { 1642 usermgmt_usage("groupdel"); 1643 } 1644 checkeuid(); 1645 if (!modify_gid(*argv, NULL)) { 1646 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP); 1647 } 1648 return EXIT_SUCCESS; 1649 } 1650 1651 #ifdef EXTENSIONS 1652 #define GROUP_MOD_OPT_EXTENSIONS "v" 1653 #else 1654 #define GROUP_MOD_OPT_EXTENSIONS 1655 #endif 1656 1657 /* modify a group */ 1658 int 1659 groupmod(int argc, char **argv) 1660 { 1661 struct group *grp; 1662 char buf[LINE_MAX]; 1663 char *newname; 1664 char **cpp; 1665 int dupgid; 1666 int gid; 1667 int cc; 1668 int c; 1669 1670 gid = -1; 1671 dupgid = 0; 1672 newname = NULL; 1673 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) { 1674 switch(c) { 1675 case 'g': 1676 if (!is_number(optarg)) { 1677 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric"); 1678 } 1679 gid = atoi(optarg); 1680 break; 1681 case 'o': 1682 dupgid = 1; 1683 break; 1684 case 'n': 1685 memsave(&newname, optarg, strlen(optarg)); 1686 break; 1687 #ifdef EXTENSIONS 1688 case 'v': 1689 verbose = 1; 1690 break; 1691 #endif 1692 } 1693 } 1694 argc -= optind; 1695 argv += optind; 1696 if (argc != 1) { 1697 usermgmt_usage("groupmod"); 1698 } 1699 checkeuid(); 1700 if (gid < 0 && newname == NULL) { 1701 err(EXIT_FAILURE, "Nothing to change"); 1702 } 1703 if (dupgid && gid < 0) { 1704 err(EXIT_FAILURE, "Duplicate which gid?"); 1705 } 1706 if ((grp = getgrnam(*argv)) == NULL) { 1707 err(EXIT_FAILURE, "can't find group `%s' to modify", *argv); 1708 } 1709 if (newname != NULL && !valid_group(newname)) { 1710 warn("warning - invalid group name `%s'", newname); 1711 } 1712 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:", 1713 (newname) ? newname : grp->gr_name, grp->gr_passwd, 1714 (gid < 0) ? grp->gr_gid : gid); 1715 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) { 1716 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp, 1717 (cpp[1] == NULL) ? "" : ","); 1718 } 1719 if (cc < sizeof(buf)) 1720 cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n"); 1721 else 1722 err(EXIT_FAILURE, "group `%s' entry too long", grp->gr_name); 1723 1724 if (!modify_gid(*argv, buf)) 1725 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP); 1726 return EXIT_SUCCESS; 1727 } 1728 1729 #ifdef EXTENSIONS 1730 /* display user information */ 1731 int 1732 userinfo(int argc, char **argv) 1733 { 1734 struct passwd *pwp; 1735 struct group *grp; 1736 char **cpp; 1737 int exists; 1738 int i; 1739 1740 exists = 0; 1741 while ((i = getopt(argc, argv, "ev")) != -1) { 1742 switch(i) { 1743 case 'e': 1744 exists = 1; 1745 break; 1746 case 'v': 1747 verbose = 1; 1748 break; 1749 } 1750 } 1751 argc -= optind; 1752 argv += optind; 1753 if (argc != 1) { 1754 usermgmt_usage("userinfo"); 1755 } 1756 pwp = find_user_info(*argv); 1757 if (exists) { 1758 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE); 1759 } 1760 if (pwp == NULL) { 1761 errx(EXIT_FAILURE, "can't find user `%s'", *argv); 1762 } 1763 (void) printf("login\t%s\n", pwp->pw_name); 1764 (void) printf("passwd\t%s\n", pwp->pw_passwd); 1765 (void) printf("uid\t%d\n", pwp->pw_uid); 1766 if ((grp = getgrgid(pwp->pw_gid)) == NULL) 1767 (void) printf("groups\t%d", pwp->pw_gid); 1768 else 1769 (void) printf("groups\t%s", grp->gr_name); 1770 while ((grp = getgrent()) != NULL) { 1771 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 1772 if (strcmp(*cpp, *argv) == 0 && grp->gr_gid != pwp->pw_gid) 1773 (void) printf(" %s", grp->gr_name); 1774 } 1775 } 1776 (void) fputc('\n', stdout); 1777 (void) printf("change\t%s", pwp->pw_change ? ctime(&pwp->pw_change) : "NEVER\n"); 1778 #ifdef EXTENSIONS 1779 (void) printf("class\t%s\n", pwp->pw_class); 1780 #endif 1781 (void) printf("gecos\t%s\n", pwp->pw_gecos); 1782 (void) printf("dir\t%s\n", pwp->pw_dir); 1783 (void) printf("shell\t%s\n", pwp->pw_shell); 1784 (void) printf("expire\t%s", pwp->pw_expire ? ctime(&pwp->pw_expire) : "NEVER\n"); 1785 return EXIT_SUCCESS; 1786 } 1787 #endif 1788 1789 #ifdef EXTENSIONS 1790 /* display user information */ 1791 int 1792 groupinfo(int argc, char **argv) 1793 { 1794 struct group *grp; 1795 char **cpp; 1796 int exists; 1797 int i; 1798 1799 exists = 0; 1800 while ((i = getopt(argc, argv, "ev")) != -1) { 1801 switch(i) { 1802 case 'e': 1803 exists = 1; 1804 break; 1805 case 'v': 1806 verbose = 1; 1807 break; 1808 } 1809 } 1810 argc -= optind; 1811 argv += optind; 1812 if (argc != 1) { 1813 usermgmt_usage("groupinfo"); 1814 } 1815 grp = find_group_info(*argv); 1816 if (exists) { 1817 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE); 1818 } 1819 if (grp == NULL) { 1820 errx(EXIT_FAILURE, "can't find group `%s'", *argv); 1821 } 1822 (void) printf("name\t%s\n", grp->gr_name); 1823 (void) printf("passwd\t%s\n", grp->gr_passwd); 1824 (void) printf("gid\t%d\n", grp->gr_gid); 1825 (void) printf("members\t"); 1826 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 1827 (void) printf("%s ", *cpp); 1828 } 1829 (void) fputc('\n', stdout); 1830 return EXIT_SUCCESS; 1831 } 1832 #endif 1833