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