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