pw_user.c revision 286259
120253Sjoerg/*- 220302Sjoerg * Copyright (C) 1996 320302Sjoerg * David L. Nugent. All rights reserved. 420253Sjoerg * 520253Sjoerg * Redistribution and use in source and binary forms, with or without 620253Sjoerg * modification, are permitted provided that the following conditions 720253Sjoerg * are met: 820253Sjoerg * 1. Redistributions of source code must retain the above copyright 920302Sjoerg * notice, this list of conditions and the following disclaimer. 1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1120253Sjoerg * notice, this list of conditions and the following disclaimer in the 1220253Sjoerg * documentation and/or other materials provided with the distribution. 1320253Sjoerg * 1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1720302Sjoerg * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2420253Sjoerg * SUCH DAMAGE. 2544229Sdavidn * 2620253Sjoerg */ 2720253Sjoerg 2830259Scharnier#ifndef lint 2930259Scharnierstatic const char rcsid[] = 3050479Speter "$FreeBSD: head/usr.sbin/pw/pw_user.c 286259 2015-08-03 22:07:50Z ed $"; 3130259Scharnier#endif /* not lint */ 3230259Scharnier 33286201Sbapt#include <sys/param.h> 34286201Sbapt#include <sys/resource.h> 35286201Sbapt#include <sys/time.h> 36286201Sbapt#include <sys/types.h> 37286201Sbapt 3830259Scharnier#include <ctype.h> 39286201Sbapt#include <dirent.h> 4030259Scharnier#include <err.h> 4120253Sjoerg#include <fcntl.h> 42286201Sbapt#include <grp.h> 43286201Sbapt#include <pwd.h> 44286201Sbapt#include <libutil.h> 45286201Sbapt#include <login_cap.h> 4630259Scharnier#include <paths.h> 47286201Sbapt#include <string.h> 48286201Sbapt#include <sysexits.h> 4920253Sjoerg#include <termios.h> 50286201Sbapt#include <unistd.h> 51286201Sbapt 5220253Sjoerg#include "pw.h" 5320253Sjoerg#include "bitmap.h" 54286201Sbapt#include "psdate.h" 5520253Sjoerg 5623318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 5722394Sdavidn 5852512Sdavidnstatic char locked_str[] = "*LOCKED*"; 5924214Sache 60286196Sbaptstatic struct passwd fakeuser = { 61286196Sbapt "nouser", 62286196Sbapt "*", 63286196Sbapt -1, 64286196Sbapt -1, 65286196Sbapt 0, 66286196Sbapt "", 67286196Sbapt "User &", 68286196Sbapt "/nonexistent", 69286196Sbapt "/bin/sh", 70286196Sbapt 0, 71286196Sbapt 0 72286196Sbapt}; 7320253Sjoerg 74286196Sbaptstatic int print_user(struct passwd *pwd, bool pretty, bool v7); 75286196Sbaptstatic uid_t pw_uidpolicy(struct userconf *cnf, intmax_t id); 76286196Sbaptstatic uid_t pw_gidpolicy(struct userconf *cnf, char *grname, char *nam, 77286196Sbapt gid_t prefer, bool dryrun); 78286196Sbaptstatic char *pw_homepolicy(struct userconf * cnf, char *homedir, 79286196Sbapt const char *user); 80286196Sbaptstatic char *pw_shellpolicy(struct userconf * cnf); 81286196Sbaptstatic char *pw_password(struct userconf * cnf, char const * user, 82286196Sbapt bool dryrun); 83286196Sbaptstatic char *shell_path(char const * path, char *shells[], char *sh); 84286196Sbaptstatic void rmat(uid_t uid); 85286196Sbaptstatic void rmopie(char const * name); 86286196Sbapt 87283961Sbaptstatic void 88286196Sbaptcreate_and_populate_homedir(struct userconf *cnf, struct passwd *pwd, 89286196Sbapt const char *skeldir, mode_t homemode, bool update) 90283961Sbapt{ 91285430Sbapt int skelfd = -1; 92283961Sbapt 93285430Sbapt if (skeldir != NULL && *skeldir != '\0') { 94285434Sbapt if (*skeldir == '/') 95285434Sbapt skeldir++; 96285434Sbapt skelfd = openat(conf.rootfd, skeldir, O_DIRECTORY|O_CLOEXEC); 97283961Sbapt } 98283961Sbapt 99286196Sbapt copymkdir(conf.rootfd, pwd->pw_dir, skelfd, homemode, pwd->pw_uid, 100285430Sbapt pwd->pw_gid, 0); 101286196Sbapt pw_log(cnf, update ? M_UPDATE : M_ADD, W_USER, "%s(%ju) home %s made", 102286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, pwd->pw_dir); 103283961Sbapt} 104283961Sbapt 105285133Sbaptstatic int 106286196Sbaptpw_set_passwd(struct passwd *pwd, int fd, bool precrypted, bool update) 107285133Sbapt{ 108285133Sbapt int b, istty; 109285133Sbapt struct termios t, n; 110285133Sbapt login_cap_t *lc; 111285133Sbapt char line[_PASSWORD_LEN+1]; 112285133Sbapt char *p; 113285133Sbapt 114286196Sbapt if (fd == '-') { 115285133Sbapt if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 116285133Sbapt pwd->pw_passwd = "*"; /* No access */ 117285133Sbapt return (1); 118285133Sbapt } 119285133Sbapt return (0); 120285133Sbapt } 121285133Sbapt 122286196Sbapt if ((istty = isatty(fd))) { 123286196Sbapt if (tcgetattr(fd, &t) == -1) 124285133Sbapt istty = 0; 125285133Sbapt else { 126285137Sbapt n = t; 127285133Sbapt n.c_lflag &= ~(ECHO); 128286196Sbapt tcsetattr(fd, TCSANOW, &n); 129285133Sbapt printf("%s%spassword for user %s:", 130285133Sbapt update ? "new " : "", 131286196Sbapt precrypted ? "encrypted " : "", 132285133Sbapt pwd->pw_name); 133285133Sbapt fflush(stdout); 134285133Sbapt } 135285133Sbapt } 136286196Sbapt b = read(fd, line, sizeof(line) - 1); 137285133Sbapt if (istty) { /* Restore state */ 138286196Sbapt tcsetattr(fd, TCSANOW, &t); 139285133Sbapt fputc('\n', stdout); 140285133Sbapt fflush(stdout); 141285133Sbapt } 142285133Sbapt 143285133Sbapt if (b < 0) 144285133Sbapt err(EX_IOERR, "-%c file descriptor", 145286196Sbapt precrypted ? 'H' : 'h'); 146285133Sbapt line[b] = '\0'; 147285133Sbapt if ((p = strpbrk(line, "\r\n")) != NULL) 148285133Sbapt *p = '\0'; 149285133Sbapt if (!*line) 150285133Sbapt errx(EX_DATAERR, "empty password read on file descriptor %d", 151286196Sbapt fd); 152286196Sbapt if (precrypted) { 153285133Sbapt if (strchr(line, ':') != NULL) 154285137Sbapt errx(EX_DATAERR, "bad encrypted password"); 155286196Sbapt pwd->pw_passwd = strdup(line); 156285133Sbapt } else { 157285133Sbapt lc = login_getpwclass(pwd); 158285133Sbapt if (lc == NULL || 159285133Sbapt login_setcryptfmt(lc, "sha512", NULL) == NULL) 160285133Sbapt warn("setting crypt(3) format"); 161285133Sbapt login_close(lc); 162285133Sbapt pwd->pw_passwd = pw_pwcrypt(line); 163285133Sbapt } 164285133Sbapt return (1); 165285133Sbapt} 166285133Sbapt 167285405Sbaptstatic void 168286196Sbaptperform_chgpwent(const char *name, struct passwd *pwd, char *nispasswd) 169285405Sbapt{ 170285405Sbapt int rc; 171286196Sbapt struct passwd *nispwd; 172285405Sbapt 173286196Sbapt /* duplicate for nis so that chgpwent is not modifying before NIS */ 174286196Sbapt if (nispasswd && *nispasswd == '/') 175286196Sbapt nispwd = pw_dup(pwd); 176286196Sbapt 177285405Sbapt rc = chgpwent(name, pwd); 178285405Sbapt if (rc == -1) 179285405Sbapt errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name); 180285405Sbapt else if (rc != 0) 181285405Sbapt err(EX_IOERR, "passwd file update"); 182285405Sbapt 183286196Sbapt if (nispasswd && *nispasswd == '/') { 184286196Sbapt rc = chgnispwent(nispasswd, name, nispwd); 185285405Sbapt if (rc == -1) 186285405Sbapt warn("User '%s' not found in NIS passwd", pwd->pw_name); 187285984Sbapt else if (rc != 0) 188285405Sbapt warn("NIS passwd update"); 189285405Sbapt /* NOTE: NIS-only update errors are not fatal */ 190285405Sbapt } 191285405Sbapt} 192285405Sbapt 193285405Sbapt/* 194285405Sbapt * The M_LOCK and M_UNLOCK functions simply add or remove 195285405Sbapt * a "*LOCKED*" prefix from in front of the password to 196285405Sbapt * prevent it decoding correctly, and therefore prevents 197285405Sbapt * access. Of course, this only prevents access via 198285405Sbapt * password authentication (not ssh, kerberos or any 199285405Sbapt * other method that does not use the UNIX password) but 200285405Sbapt * that is a known limitation. 201285405Sbapt */ 202285405Sbaptstatic int 203286196Sbaptpw_userlock(char *arg1, int mode) 204285405Sbapt{ 205285405Sbapt struct passwd *pwd = NULL; 206285405Sbapt char *passtmp = NULL; 207286196Sbapt char *name; 208285405Sbapt bool locked = false; 209286196Sbapt uid_t id; 210285405Sbapt 211286196Sbapt if (geteuid() != 0) 212286196Sbapt errx(EX_NOPERM, "you must be root"); 213286196Sbapt 214286196Sbapt if (arg1 == NULL) 215285405Sbapt errx(EX_DATAERR, "username or id required"); 216285405Sbapt 217286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 218286196Sbapt id = pw_checkid(arg1, UID_MAX); 219286196Sbapt else 220286196Sbapt name = arg1; 221286196Sbapt 222285405Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 223285405Sbapt if (pwd == NULL) { 224285405Sbapt if (name == NULL) 225286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", (uintmax_t) id); 226285405Sbapt errx(EX_NOUSER, "no such user `%s'", name); 227285405Sbapt } 228285405Sbapt 229285405Sbapt if (name == NULL) 230285405Sbapt name = pwd->pw_name; 231285405Sbapt 232285405Sbapt if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str) -1) == 0) 233285405Sbapt locked = true; 234285405Sbapt if (mode == M_LOCK && locked) 235285405Sbapt errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 236285405Sbapt if (mode == M_UNLOCK && !locked) 237285405Sbapt errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 238285405Sbapt 239285405Sbapt if (mode == M_LOCK) { 240285405Sbapt asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 241285405Sbapt if (passtmp == NULL) /* disaster */ 242285405Sbapt errx(EX_UNAVAILABLE, "out of memory"); 243285405Sbapt pwd->pw_passwd = passtmp; 244285405Sbapt } else { 245285405Sbapt pwd->pw_passwd += sizeof(locked_str)-1; 246285405Sbapt } 247285405Sbapt 248286196Sbapt perform_chgpwent(name, pwd, NULL); 249285405Sbapt free(passtmp); 250285405Sbapt 251285405Sbapt return (EXIT_SUCCESS); 252285405Sbapt} 253285405Sbapt 254286196Sbaptstatic uid_t 255286196Sbaptpw_uidpolicy(struct userconf * cnf, intmax_t id) 25620253Sjoerg{ 257286196Sbapt struct passwd *pwd; 258286196Sbapt struct bitmap bm; 259286196Sbapt uid_t uid = (uid_t) - 1; 26020253Sjoerg 26120267Sjoerg /* 262286196Sbapt * Check the given uid, if any 26320253Sjoerg */ 264286196Sbapt if (id >= 0) { 265286196Sbapt uid = (uid_t) id; 26620253Sjoerg 267286196Sbapt if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate) 268286196Sbapt errx(EX_DATAERR, "uid `%ju' has already been allocated", 269286196Sbapt (uintmax_t)pwd->pw_uid); 270286196Sbapt return (uid); 27120253Sjoerg } 27221052Sdavidn /* 273286196Sbapt * We need to allocate the next available uid under one of 274286196Sbapt * two policies a) Grab the first unused uid b) Grab the 275286196Sbapt * highest possible unused uid 27621052Sdavidn */ 277286196Sbapt if (cnf->min_uid >= cnf->max_uid) { /* Sanity 278286196Sbapt * claus^H^H^H^Hheck */ 279286196Sbapt cnf->min_uid = 1000; 280286196Sbapt cnf->max_uid = 32000; 28121052Sdavidn } 282286196Sbapt bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 28321052Sdavidn 28420253Sjoerg /* 285286196Sbapt * Now, let's fill the bitmap from the password file 28620253Sjoerg */ 287286196Sbapt SETPWENT(); 288286196Sbapt while ((pwd = GETPWENT()) != NULL) 289286196Sbapt if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 290286196Sbapt bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 291286196Sbapt ENDPWENT(); 29252527Sdavidn 29320253Sjoerg /* 294286196Sbapt * Then apply the policy, with fallback to reuse if necessary 29520253Sjoerg */ 296286196Sbapt if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 297286196Sbapt uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 29820253Sjoerg 29920267Sjoerg /* 300286196Sbapt * Another sanity check 30120267Sjoerg */ 302286196Sbapt if (uid < cnf->min_uid || uid > cnf->max_uid) 303286196Sbapt errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 304286196Sbapt bm_dealloc(&bm); 305286196Sbapt return (uid); 30620253Sjoerg} 30720253Sjoerg 308286196Sbaptstatic uid_t 309286196Sbaptpw_gidpolicy(struct userconf *cnf, char *grname, char *nam, gid_t prefer, bool dryrun) 31020253Sjoerg{ 31120253Sjoerg struct group *grp; 31220253Sjoerg gid_t gid = (uid_t) - 1; 31320253Sjoerg 31420253Sjoerg /* 31520253Sjoerg * Check the given gid, if any 31620253Sjoerg */ 31744229Sdavidn SETGRENT(); 318286196Sbapt if (grname) { 319286196Sbapt if ((grp = GETGRNAM(grname)) == NULL) { 320286196Sbapt gid = pw_checkid(grname, GID_MAX); 321286196Sbapt grp = GETGRGID(gid); 32220253Sjoerg } 32320253Sjoerg gid = grp->gr_gid; 324262865Sjulian } else if ((grp = GETGRNAM(nam)) != NULL && 325262865Sjulian (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 32620267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 32720253Sjoerg } else { 328286196Sbapt intmax_t grid = -1; 32920253Sjoerg 33020253Sjoerg /* 33120253Sjoerg * We need to auto-create a group with the user's name. We 33220253Sjoerg * can send all the appropriate output to our sister routine 33320253Sjoerg * bit first see if we can create a group with gid==uid so we 33420253Sjoerg * can keep the user and group ids in sync. We purposely do 33520253Sjoerg * NOT check the gid range if we can force the sync. If the 33620253Sjoerg * user's name dups an existing group, then the group add 33720253Sjoerg * function will happily handle that case for us and exit. 33820253Sjoerg */ 339285414Sbapt if (GETGRGID(prefer) == NULL) 340285414Sbapt grid = prefer; 341286196Sbapt if (dryrun) { 342285395Sbapt gid = pw_groupnext(cnf, true); 343285395Sbapt } else { 344286196Sbapt if (grid == -1) 345286196Sbapt grid = pw_groupnext(cnf, true); 346286196Sbapt groupadd(cnf, nam, grid, NULL, -1, false, false, false); 34744229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 34820267Sjoerg gid = grp->gr_gid; 34920267Sjoerg } 35020253Sjoerg } 35144229Sdavidn ENDGRENT(); 352286196Sbapt return (gid); 35320253Sjoerg} 35420253Sjoerg 355286196Sbaptstatic char * 356286196Sbaptpw_homepolicy(struct userconf * cnf, char *homedir, const char *user) 35720253Sjoerg{ 358282699Sbapt static char home[128]; 35920253Sjoerg 360286196Sbapt if (homedir) 361286196Sbapt return (homedir); 36220253Sjoerg 363282699Sbapt if (cnf->home == NULL || *cnf->home == '\0') 364282699Sbapt errx(EX_CONFIG, "no base home directory set"); 365282699Sbapt snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 366282699Sbapt 367282699Sbapt return (home); 36820253Sjoerg} 36920253Sjoerg 370286196Sbaptstatic char * 37120253Sjoergshell_path(char const * path, char *shells[], char *sh) 37220253Sjoerg{ 37320253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 37420253Sjoerg return sh; /* specified full path or forced none */ 37520253Sjoerg else { 37620253Sjoerg char *p; 37720253Sjoerg char paths[_UC_MAXLINE]; 37820253Sjoerg 37920253Sjoerg /* 38020253Sjoerg * We need to search paths 38120253Sjoerg */ 382130633Srobert strlcpy(paths, path, sizeof(paths)); 38320253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 38420253Sjoerg int i; 38520253Sjoerg static char shellpath[256]; 38620253Sjoerg 38720253Sjoerg if (sh != NULL) { 388282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); 38920253Sjoerg if (access(shellpath, X_OK) == 0) 39020253Sjoerg return shellpath; 39120253Sjoerg } else 39220253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 393282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); 39420253Sjoerg if (access(shellpath, X_OK) == 0) 39520253Sjoerg return shellpath; 39620253Sjoerg } 39720253Sjoerg } 39820253Sjoerg if (sh == NULL) 39930259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 40030259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 40120253Sjoerg return NULL; 40220253Sjoerg } 40320253Sjoerg} 40420253Sjoerg 405286196Sbaptstatic char * 406286196Sbaptpw_shellpolicy(struct userconf * cnf) 40720253Sjoerg{ 40820253Sjoerg 409286196Sbapt return shell_path(cnf->shelldir, cnf->shells, cnf->shell_default); 41020253Sjoerg} 41120253Sjoerg 412179365Santoine#define SALTSIZE 32 41320253Sjoerg 414179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 415179365Santoine 416286196Sbaptchar * 41720253Sjoergpw_pwcrypt(char *password) 41820253Sjoerg{ 41920253Sjoerg int i; 420179365Santoine char salt[SALTSIZE + 1]; 421231994Skevlo char *cryptpw; 42220253Sjoerg static char buf[256]; 42320253Sjoerg 42420253Sjoerg /* 42520253Sjoerg * Calculate a salt value 42620253Sjoerg */ 427179365Santoine for (i = 0; i < SALTSIZE; i++) 428181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 429179365Santoine salt[SALTSIZE] = '\0'; 43020253Sjoerg 431231994Skevlo cryptpw = crypt(password, salt); 432231994Skevlo if (cryptpw == NULL) 433231994Skevlo errx(EX_CONFIG, "crypt(3) failure"); 434231994Skevlo return strcpy(buf, cryptpw); 43520253Sjoerg} 43620253Sjoerg 437286196Sbaptstatic char * 438286196Sbaptpw_password(struct userconf * cnf, char const * user, bool dryrun) 43920253Sjoerg{ 44020253Sjoerg int i, l; 44120253Sjoerg char pwbuf[32]; 44220253Sjoerg 44320253Sjoerg switch (cnf->default_password) { 44420253Sjoerg case -1: /* Random password */ 44573563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 44620253Sjoerg for (i = 0; i < l; i++) 447181785Sache pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 44820253Sjoerg pwbuf[i] = '\0'; 44920253Sjoerg 45020253Sjoerg /* 45120253Sjoerg * We give this information back to the user 45220253Sjoerg */ 453286196Sbapt if (conf.fd == -1 && !dryrun) { 45461957Sache if (isatty(STDOUT_FILENO)) 45520712Sdavidn printf("Password for '%s' is: ", user); 45620253Sjoerg printf("%s\n", pwbuf); 45720253Sjoerg fflush(stdout); 45820253Sjoerg } 45920253Sjoerg break; 46020253Sjoerg 46120253Sjoerg case -2: /* No password at all! */ 46220253Sjoerg return ""; 46320253Sjoerg 46420253Sjoerg case 0: /* No login - default */ 46520253Sjoerg default: 46620253Sjoerg return "*"; 46720253Sjoerg 46820253Sjoerg case 1: /* user's name */ 469130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 47020253Sjoerg break; 47120253Sjoerg } 47220253Sjoerg return pw_pwcrypt(pwbuf); 47320253Sjoerg} 47420253Sjoerg 475284111Sbaptstatic int 476286196Sbaptprint_user(struct passwd * pwd, bool pretty, bool v7) 477284111Sbapt{ 478286196Sbapt int j; 479286196Sbapt char *p; 480286196Sbapt struct group *grp = GETGRGID(pwd->pw_gid); 481286196Sbapt char uname[60] = "User &", office[60] = "[None]", 482286196Sbapt wphone[60] = "[None]", hphone[60] = "[None]"; 483286196Sbapt char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 484286196Sbapt struct tm * tptr; 485286196Sbapt 486286196Sbapt if (!pretty) { 487286196Sbapt p = v7 ? pw_make_v7(pwd) : pw_make(pwd); 488286196Sbapt printf("%s\n", p); 489286196Sbapt free(p); 490286196Sbapt return (EXIT_SUCCESS); 491286196Sbapt } 492286196Sbapt 493286196Sbapt if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 494286196Sbapt strlcpy(uname, p, sizeof(uname)); 495286196Sbapt if ((p = strtok(NULL, ",")) != NULL) { 496286196Sbapt strlcpy(office, p, sizeof(office)); 497286196Sbapt if ((p = strtok(NULL, ",")) != NULL) { 498286196Sbapt strlcpy(wphone, p, sizeof(wphone)); 499286196Sbapt if ((p = strtok(NULL, "")) != NULL) { 500286196Sbapt strlcpy(hphone, p, sizeof(hphone)); 501286196Sbapt } 502286196Sbapt } 503286196Sbapt } 504286196Sbapt } 505286196Sbapt /* 506286196Sbapt * Handle '&' in gecos field 507286196Sbapt */ 508286196Sbapt if ((p = strchr(uname, '&')) != NULL) { 509286196Sbapt int l = strlen(pwd->pw_name); 510286196Sbapt int m = strlen(p); 511286196Sbapt 512286196Sbapt memmove(p + l, p + 1, m); 513286196Sbapt memmove(p, pwd->pw_name, l); 514286196Sbapt *p = (char) toupper((unsigned char)*p); 515286196Sbapt } 516286196Sbapt if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 517286196Sbapt strftime(acexpire, sizeof acexpire, "%c", tptr); 518286196Sbapt if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 519286196Sbapt strftime(pwexpire, sizeof pwexpire, "%c", tptr); 520286196Sbapt printf("Login Name: %-15s #%-12ju Group: %-15s #%ju\n" 521286196Sbapt " Full Name: %s\n" 522286196Sbapt " Home: %-26.26s Class: %s\n" 523286196Sbapt " Shell: %-26.26s Office: %s\n" 524286196Sbapt "Work Phone: %-26.26s Home Phone: %s\n" 525286196Sbapt "Acc Expire: %-26.26s Pwd Expire: %s\n", 526286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 527286196Sbapt grp ? grp->gr_name : "(invalid)", (uintmax_t)pwd->pw_gid, 528286196Sbapt uname, pwd->pw_dir, pwd->pw_class, 529286196Sbapt pwd->pw_shell, office, wphone, hphone, 530286196Sbapt acexpire, pwexpire); 531286196Sbapt SETGRENT(); 532286196Sbapt j = 0; 533286196Sbapt while ((grp=GETGRENT()) != NULL) { 534286196Sbapt int i = 0; 535286196Sbapt if (grp->gr_mem != NULL) { 536286196Sbapt while (grp->gr_mem[i] != NULL) { 537286196Sbapt if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) { 538286196Sbapt printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 539286196Sbapt break; 540286196Sbapt } 541286196Sbapt ++i; 542286196Sbapt } 543286196Sbapt } 544286196Sbapt } 545286196Sbapt ENDGRENT(); 546286196Sbapt printf("%s", j ? "\n" : ""); 547286196Sbapt return (EXIT_SUCCESS); 548286196Sbapt} 549286196Sbapt 550286196Sbaptchar * 551286196Sbaptpw_checkname(char *name, int gecos) 552286196Sbapt{ 553286196Sbapt char showch[8]; 554286196Sbapt const char *badchars, *ch, *showtype; 555286196Sbapt int reject; 556286196Sbapt 557286196Sbapt ch = name; 558286196Sbapt reject = 0; 559286196Sbapt if (gecos) { 560286196Sbapt /* See if the name is valid as a gecos (comment) field. */ 561286196Sbapt badchars = ":!@"; 562286196Sbapt showtype = "gecos field"; 563286196Sbapt } else { 564286196Sbapt /* See if the name is valid as a userid or group. */ 565286196Sbapt badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 566286196Sbapt showtype = "userid/group name"; 567286196Sbapt /* Userids and groups can not have a leading '-'. */ 568286196Sbapt if (*ch == '-') 569286196Sbapt reject = 1; 570286196Sbapt } 571286196Sbapt if (!reject) { 572286196Sbapt while (*ch) { 573286196Sbapt if (strchr(badchars, *ch) != NULL || *ch < ' ' || 574286196Sbapt *ch == 127) { 575286196Sbapt reject = 1; 576286196Sbapt break; 577286196Sbapt } 578286196Sbapt /* 8-bit characters are only allowed in GECOS fields */ 579286196Sbapt if (!gecos && (*ch & 0x80)) { 580286196Sbapt reject = 1; 581286196Sbapt break; 582286196Sbapt } 583286196Sbapt ch++; 584286196Sbapt } 585286196Sbapt } 586286196Sbapt /* 587286196Sbapt * A `$' is allowed as the final character for userids and groups, 588286196Sbapt * mainly for the benefit of samba. 589286196Sbapt */ 590286196Sbapt if (reject && !gecos) { 591286196Sbapt if (*ch == '$' && *(ch + 1) == '\0') { 592286196Sbapt reject = 0; 593286196Sbapt ch++; 594286196Sbapt } 595286196Sbapt } 596286196Sbapt if (reject) { 597286196Sbapt snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 598286196Sbapt ? "`%c'" : "0x%02x", *ch); 599286196Sbapt errx(EX_DATAERR, "invalid character %s at position %td in %s", 600286196Sbapt showch, (ch - name), showtype); 601286196Sbapt } 602286196Sbapt if (!gecos && (ch - name) > LOGNAMESIZE) 603286196Sbapt errx(EX_USAGE, "name too long `%s' (max is %d)", name, 604286196Sbapt LOGNAMESIZE); 605286196Sbapt 606286196Sbapt return (name); 607286196Sbapt} 608286196Sbapt 609286196Sbaptstatic void 610286196Sbaptrmat(uid_t uid) 611286196Sbapt{ 612286196Sbapt DIR *d = opendir("/var/at/jobs"); 613286196Sbapt 614286196Sbapt if (d != NULL) { 615286196Sbapt struct dirent *e; 616286196Sbapt 617286196Sbapt while ((e = readdir(d)) != NULL) { 618286196Sbapt struct stat st; 619286196Sbapt 620286196Sbapt if (strncmp(e->d_name, ".lock", 5) != 0 && 621286196Sbapt stat(e->d_name, &st) == 0 && 622286196Sbapt !S_ISDIR(st.st_mode) && 623286196Sbapt st.st_uid == uid) { 624286196Sbapt char tmp[MAXPATHLEN]; 625286196Sbapt 626286202Sbapt snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", 627286202Sbapt e->d_name); 628286196Sbapt system(tmp); 629286196Sbapt } 630286196Sbapt } 631286196Sbapt closedir(d); 632286196Sbapt } 633286196Sbapt} 634286196Sbapt 635286196Sbaptstatic void 636286196Sbaptrmopie(char const * name) 637286196Sbapt{ 638286196Sbapt char tmp[1014]; 639286196Sbapt FILE *fp; 640286196Sbapt int fd; 641286196Sbapt size_t len; 642286196Sbapt off_t atofs = 0; 643286196Sbapt 644286196Sbapt if ((fd = openat(conf.rootfd, "etc/opiekeys", O_RDWR)) == -1) 645286196Sbapt return; 646286196Sbapt 647286196Sbapt fp = fdopen(fd, "r+"); 648286196Sbapt len = strlen(name); 649286196Sbapt 650286196Sbapt while (fgets(tmp, sizeof(tmp), fp) != NULL) { 651286196Sbapt if (strncmp(name, tmp, len) == 0 && tmp[len]==' ') { 652286196Sbapt /* Comment username out */ 653286196Sbapt if (fseek(fp, atofs, SEEK_SET) == 0) 654286196Sbapt fwrite("#", 1, 1, fp); 655286196Sbapt break; 656286196Sbapt } 657286196Sbapt atofs = ftell(fp); 658286196Sbapt } 659286196Sbapt /* 660286196Sbapt * If we got an error of any sort, don't update! 661286196Sbapt */ 662286196Sbapt fclose(fp); 663286196Sbapt} 664286196Sbapt 665286196Sbaptint 666286196Sbaptpw_user_next(int argc, char **argv, char *name __unused) 667286196Sbapt{ 668286196Sbapt struct userconf *cnf = NULL; 669286196Sbapt const char *cfg = NULL; 670286196Sbapt int ch; 671286196Sbapt bool quiet = false; 672286196Sbapt uid_t next; 673286196Sbapt 674286196Sbapt while ((ch = getopt(argc, argv, "Cq")) != -1) { 675286196Sbapt switch (ch) { 676286196Sbapt case 'C': 677286196Sbapt cfg = optarg; 678286196Sbapt break; 679286196Sbapt case 'q': 680286217Sadrian quiet = true; 681286196Sbapt break; 682286196Sbapt } 683286196Sbapt } 684286196Sbapt 685286196Sbapt if (quiet) 686286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 687286196Sbapt 688286196Sbapt cnf = get_userconfig(cfg); 689286196Sbapt 690286196Sbapt next = pw_uidpolicy(cnf, -1); 691286196Sbapt 692286196Sbapt printf("%ju:", (uintmax_t)next); 693286196Sbapt pw_groupnext(cnf, quiet); 694286196Sbapt 695286196Sbapt return (EXIT_SUCCESS); 696286196Sbapt} 697286196Sbapt 698286196Sbaptint 699286196Sbaptpw_user_show(int argc, char **argv, char *arg1) 700286196Sbapt{ 701285401Sbapt struct passwd *pwd = NULL; 702286196Sbapt char *name = NULL; 703286218Sbapt intmax_t id = -1; 704286196Sbapt int ch; 705286196Sbapt bool all = false; 706286196Sbapt bool pretty = false; 707286196Sbapt bool force = false; 708286196Sbapt bool v7 = false; 709286196Sbapt bool quiet = false; 71020253Sjoerg 711286196Sbapt if (arg1 != NULL) { 712286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 713286196Sbapt id = pw_checkid(arg1, UID_MAX); 714286196Sbapt else 715286196Sbapt name = arg1; 716286196Sbapt } 717286196Sbapt 718286196Sbapt while ((ch = getopt(argc, argv, "C:qn:u:FPa7")) != -1) { 719286196Sbapt switch (ch) { 720286196Sbapt case 'C': 721286196Sbapt /* ignore compatibility */ 722286196Sbapt break; 723286196Sbapt case 'q': 724286196Sbapt quiet = true; 725286196Sbapt break; 726286196Sbapt case 'n': 727286196Sbapt name = optarg; 728286196Sbapt break; 729286196Sbapt case 'u': 730286196Sbapt id = pw_checkid(optarg, UID_MAX); 731286196Sbapt break; 732286196Sbapt case 'F': 733286196Sbapt force = true; 734286196Sbapt break; 735286196Sbapt case 'P': 736286196Sbapt pretty = true; 737286196Sbapt break; 738286196Sbapt case 'a': 739286196Sbapt all = true; 740286196Sbapt break; 741286196Sbapt case 7: 742286196Sbapt v7 = true; 743286196Sbapt break; 744286196Sbapt } 745286196Sbapt } 746286196Sbapt 747286196Sbapt if (quiet) 748286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 749286196Sbapt 750286196Sbapt if (all) { 751286196Sbapt SETPWENT(); 752286196Sbapt while ((pwd = GETPWENT()) != NULL) 753286196Sbapt print_user(pwd, pretty, v7); 754286196Sbapt ENDPWENT(); 755286196Sbapt return (EXIT_SUCCESS); 756286196Sbapt } 757286196Sbapt 758285401Sbapt if (id < 0 && name == NULL) 759285401Sbapt errx(EX_DATAERR, "username or id required"); 760285401Sbapt 761285401Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 762285401Sbapt if (pwd == NULL) { 763286196Sbapt if (force) { 764286196Sbapt pwd = &fakeuser; 765286196Sbapt } else { 766286196Sbapt if (name == NULL) 767286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", 768286196Sbapt (uintmax_t) id); 769286196Sbapt errx(EX_NOUSER, "no such user `%s'", name); 770286196Sbapt } 771286196Sbapt } 772286196Sbapt 773286196Sbapt return (print_user(pwd, pretty, v7)); 774286196Sbapt} 775286196Sbapt 776286196Sbaptint 777286196Sbaptpw_user_del(int argc, char **argv, char *arg1) 778286196Sbapt{ 779286196Sbapt struct userconf *cnf = NULL; 780286196Sbapt struct passwd *pwd = NULL; 781286196Sbapt struct group *gr, *grp; 782286196Sbapt char *name = NULL; 783286196Sbapt char grname[MAXLOGNAME]; 784286196Sbapt char *nispasswd = NULL; 785286196Sbapt char file[MAXPATHLEN]; 786286196Sbapt char home[MAXPATHLEN]; 787286196Sbapt const char *cfg = NULL; 788286196Sbapt struct stat st; 789286218Sbapt intmax_t id = -1; 790286196Sbapt int ch, rc; 791286196Sbapt bool nis = false; 792286196Sbapt bool deletehome = false; 793286196Sbapt bool quiet = false; 794286196Sbapt 795286196Sbapt if (arg1 != NULL) { 796286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 797286196Sbapt id = pw_checkid(arg1, UID_MAX); 798286196Sbapt else 799286196Sbapt name = arg1; 800286196Sbapt } 801286196Sbapt 802286196Sbapt while ((ch = getopt(argc, argv, "C:qn:u:rYy:")) != -1) { 803286196Sbapt switch (ch) { 804286196Sbapt case 'C': 805286196Sbapt cfg = optarg; 806286196Sbapt break; 807286196Sbapt case 'q': 808286196Sbapt quiet = true; 809286196Sbapt break; 810286196Sbapt case 'n': 811286196Sbapt name = optarg; 812286196Sbapt break; 813286196Sbapt case 'u': 814286196Sbapt id = pw_checkid(optarg, UID_MAX); 815286196Sbapt break; 816286196Sbapt case 'r': 817286196Sbapt deletehome = true; 818286196Sbapt break; 819286196Sbapt case 'y': 820286196Sbapt nispasswd = optarg; 821286196Sbapt break; 822286196Sbapt case 'Y': 823286196Sbapt nis = true; 824286196Sbapt break; 825286196Sbapt } 826286196Sbapt } 827286196Sbapt 828286196Sbapt if (quiet) 829286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 830286196Sbapt 831286196Sbapt if (id < 0 && name == NULL) 832286196Sbapt errx(EX_DATAERR, "username or id required"); 833286196Sbapt 834286196Sbapt cnf = get_userconfig(cfg); 835286196Sbapt 836286196Sbapt if (nispasswd == NULL) 837286196Sbapt nispasswd = cnf->nispasswd; 838286196Sbapt 839286196Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 840286196Sbapt if (pwd == NULL) { 841285401Sbapt if (name == NULL) 842286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", (uintmax_t) id); 843285401Sbapt errx(EX_NOUSER, "no such user `%s'", name); 844285401Sbapt } 845285989Sbapt 846285989Sbapt if (PWF._altdir == PWF_REGULAR && 847286196Sbapt ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) { 848286196Sbapt if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 849286196Sbapt if (!nis && nispasswd && *nispasswd != '/') 850286196Sbapt errx(EX_NOUSER, "Cannot remove NIS user `%s'", 851286196Sbapt name); 852286196Sbapt } else { 853286196Sbapt errx(EX_NOUSER, "Cannot remove non local user `%s'", 854286196Sbapt name); 855286196Sbapt } 856286196Sbapt } 857285989Sbapt 858286196Sbapt id = pwd->pw_uid; 859285401Sbapt if (name == NULL) 860285401Sbapt name = pwd->pw_name; 861285401Sbapt 862284111Sbapt if (strcmp(pwd->pw_name, "root") == 0) 863284111Sbapt errx(EX_DATAERR, "cannot remove user 'root'"); 864284111Sbapt 865286196Sbapt /* Remove opie record from /etc/opiekeys */ 866285433Sbapt if (PWALTDIR() != PWF_ALT) 867284111Sbapt rmopie(pwd->pw_name); 868284111Sbapt 869285433Sbapt if (!PWALTDIR()) { 870285433Sbapt /* Remove crontabs */ 871284111Sbapt snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 872284111Sbapt if (access(file, F_OK) == 0) { 873286202Sbapt snprintf(file, sizeof(file), "crontab -u %s -r", 874286202Sbapt pwd->pw_name); 875284111Sbapt system(file); 876284111Sbapt } 877284111Sbapt } 878286196Sbapt 879284111Sbapt /* 880284111Sbapt * Save these for later, since contents of pwd may be 881284111Sbapt * invalidated by deletion 882284111Sbapt */ 883284111Sbapt snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 884284111Sbapt strlcpy(home, pwd->pw_dir, sizeof(home)); 885284111Sbapt gr = GETGRGID(pwd->pw_gid); 886284111Sbapt if (gr != NULL) 887284111Sbapt strlcpy(grname, gr->gr_name, LOGNAMESIZE); 888284111Sbapt else 889284111Sbapt grname[0] = '\0'; 890284111Sbapt 891284111Sbapt rc = delpwent(pwd); 892284111Sbapt if (rc == -1) 893284111Sbapt err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 894284111Sbapt else if (rc != 0) 895284111Sbapt err(EX_IOERR, "passwd update"); 896284111Sbapt 897286196Sbapt if (nis && nispasswd && *nispasswd=='/') { 898286196Sbapt rc = delnispwent(nispasswd, name); 899284111Sbapt if (rc == -1) 900285401Sbapt warnx("WARNING: user '%s' does not exist in NIS passwd", 901285401Sbapt pwd->pw_name); 902284111Sbapt else if (rc != 0) 903284111Sbapt warn("WARNING: NIS passwd update"); 904284111Sbapt } 905284111Sbapt 906284128Sbapt grp = GETGRNAM(name); 907284111Sbapt if (grp != NULL && 908284111Sbapt (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 909284128Sbapt strcmp(name, grname) == 0) 910284128Sbapt delgrent(GETGRNAM(name)); 911284111Sbapt SETGRENT(); 912284111Sbapt while ((grp = GETGRENT()) != NULL) { 913284111Sbapt int i, j; 914284111Sbapt char group[MAXLOGNAME]; 915284113Sbapt if (grp->gr_mem == NULL) 916284113Sbapt continue; 917284113Sbapt 918284113Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 919284128Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 920284113Sbapt continue; 921284113Sbapt 922284113Sbapt for (j = i; grp->gr_mem[j] != NULL; j++) 923284113Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 924284113Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 925284113Sbapt chggrent(group, grp); 926284111Sbapt } 927284111Sbapt } 928284111Sbapt ENDGRENT(); 929284111Sbapt 930286196Sbapt pw_log(cnf, M_DELETE, W_USER, "%s(%ju) account removed", name, 931286196Sbapt (uintmax_t)id); 932284111Sbapt 933285433Sbapt /* Remove mail file */ 934285433Sbapt if (PWALTDIR() != PWF_ALT) 935285433Sbapt unlinkat(conf.rootfd, file + 1, 0); 936284111Sbapt 937286196Sbapt /* Remove at jobs */ 938286196Sbapt if (!PWALTDIR() && getpwuid(id) == NULL) 939286196Sbapt rmat(id); 940284111Sbapt 941285433Sbapt /* Remove home directory and contents */ 942286196Sbapt if (PWALTDIR() != PWF_ALT && deletehome && *home == '/' && 943286196Sbapt GETPWUID(id) == NULL && 944285433Sbapt fstatat(conf.rootfd, home + 1, &st, 0) != -1) { 945286196Sbapt rm_r(conf.rootfd, home, id); 946286196Sbapt pw_log(cnf, M_DELETE, W_USER, "%s(%ju) home '%s' %s" 947286196Sbapt "removed", name, (uintmax_t)id, home, 948285433Sbapt fstatat(conf.rootfd, home + 1, &st, 0) == -1 ? "" : "not " 949285433Sbapt "completely "); 950284111Sbapt } 951284111Sbapt 952284111Sbapt return (EXIT_SUCCESS); 953284111Sbapt} 954284111Sbapt 955286196Sbaptint 956286196Sbaptpw_user_lock(int argc, char **argv, char *arg1) 95720253Sjoerg{ 958286196Sbapt int ch; 95920253Sjoerg 960286196Sbapt while ((ch = getopt(argc, argv, "Cq")) != -1) { 961286196Sbapt switch (ch) { 962286196Sbapt case 'C': 963286196Sbapt case 'q': 964286196Sbapt /* compatibility */ 965286196Sbapt break; 96620253Sjoerg } 967286196Sbapt } 96820253Sjoerg 969286196Sbapt return (pw_userlock(arg1, M_LOCK)); 970286196Sbapt} 971286196Sbapt 972286196Sbaptint 973286196Sbaptpw_user_unlock(int argc, char **argv, char *arg1) 974286196Sbapt{ 975286196Sbapt int ch; 976286196Sbapt 977286196Sbapt while ((ch = getopt(argc, argv, "Cq")) != -1) { 978286196Sbapt switch (ch) { 979286196Sbapt case 'C': 980286196Sbapt case 'q': 981286196Sbapt /* compatibility */ 982286196Sbapt break; 98320253Sjoerg } 98420253Sjoerg } 985286196Sbapt 986286196Sbapt return (pw_userlock(arg1, M_UNLOCK)); 98720253Sjoerg} 98820253Sjoerg 989286196Sbaptstatic struct group * 990286196Sbaptgroup_from_name_or_id(char *name) 99120253Sjoerg{ 992286196Sbapt const char *errstr = NULL; 993286196Sbapt struct group *grp; 994286196Sbapt uintmax_t id; 99520253Sjoerg 996286196Sbapt if ((grp = GETGRNAM(name)) == NULL) { 997286196Sbapt id = strtounum(name, 0, GID_MAX, &errstr); 998286196Sbapt if (errstr) 999286196Sbapt errx(EX_NOUSER, "group `%s' does not exist", name); 1000286196Sbapt grp = GETGRGID(id); 1001286196Sbapt if (grp == NULL) 1002286196Sbapt errx(EX_NOUSER, "group `%s' does not exist", name); 100320253Sjoerg } 1004286196Sbapt 1005286196Sbapt return (grp); 1006286196Sbapt} 1007286196Sbapt 1008286196Sbaptstatic void 1009286196Sbaptsplit_groups(StringList **groups, char *groupsstr) 1010286196Sbapt{ 1011286196Sbapt struct group *grp; 1012286196Sbapt char *p; 1013286196Sbapt char tok[] = ", \t"; 1014286196Sbapt 1015286196Sbapt for (p = strtok(groupsstr, tok); p != NULL; p = strtok(NULL, tok)) { 1016286196Sbapt grp = group_from_name_or_id(p); 1017286196Sbapt if (*groups == NULL) 1018286196Sbapt *groups = sl_init(); 1019286196Sbapt sl_add(*groups, newstr(grp->gr_name)); 1020286196Sbapt } 1021286196Sbapt} 1022286196Sbapt 1023286196Sbaptstatic void 1024286196Sbaptvalidate_grname(struct userconf *cnf, char *group) 1025286196Sbapt{ 1026286196Sbapt struct group *grp; 1027286196Sbapt 1028286196Sbapt if (group == NULL || *group == '\0') { 1029286196Sbapt cnf->default_group = ""; 1030286196Sbapt return; 1031286196Sbapt } 1032286196Sbapt grp = group_from_name_or_id(group); 1033286196Sbapt cnf->default_group = newstr(grp->gr_name); 1034286196Sbapt} 1035286196Sbapt 1036286196Sbaptstatic mode_t 1037286196Sbaptvalidate_mode(char *mode) 1038286196Sbapt{ 1039286196Sbapt mode_t m; 1040286196Sbapt void *set; 1041286196Sbapt 1042286196Sbapt if ((set = setmode(mode)) == NULL) 1043286196Sbapt errx(EX_DATAERR, "invalid directory creation mode '%s'", mode); 1044286196Sbapt 1045286196Sbapt m = getmode(set, _DEF_DIRMODE); 1046286196Sbapt free(set); 1047286196Sbapt return (m); 1048286196Sbapt} 1049286196Sbapt 1050286196Sbaptstatic void 1051286196Sbaptmix_config(struct userconf *cmdcnf, struct userconf *cfg) 1052286196Sbapt{ 1053286196Sbapt 1054286196Sbapt if (cmdcnf->default_password == 0) 1055286196Sbapt cmdcnf->default_password = cfg->default_password; 1056286196Sbapt if (cmdcnf->reuse_uids == 0) 1057286196Sbapt cmdcnf->reuse_uids = cfg->reuse_uids; 1058286196Sbapt if (cmdcnf->reuse_gids == 0) 1059286196Sbapt cmdcnf->reuse_gids = cfg->reuse_gids; 1060286196Sbapt if (cmdcnf->nispasswd == NULL) 1061286196Sbapt cmdcnf->nispasswd = cfg->nispasswd; 1062286196Sbapt if (cmdcnf->dotdir == NULL) 1063286196Sbapt cmdcnf->dotdir = cfg->dotdir; 1064286196Sbapt if (cmdcnf->newmail == NULL) 1065286196Sbapt cmdcnf->newmail = cfg->newmail; 1066286196Sbapt if (cmdcnf->logfile == NULL) 1067286196Sbapt cmdcnf->logfile = cfg->logfile; 1068286196Sbapt if (cmdcnf->home == NULL) 1069286196Sbapt cmdcnf->home = cfg->home; 1070286196Sbapt if (cmdcnf->homemode == 0) 1071286196Sbapt cmdcnf->homemode = cfg->homemode; 1072286196Sbapt if (cmdcnf->shelldir == NULL) 1073286196Sbapt cmdcnf->shelldir = cfg->shelldir; 1074286196Sbapt if (cmdcnf->shells == NULL) 1075286196Sbapt cmdcnf->shells = cfg->shells; 1076286196Sbapt if (cmdcnf->shell_default == NULL) 1077286196Sbapt cmdcnf->shell_default = cfg->shell_default; 1078286196Sbapt if (cmdcnf->default_group == NULL) 1079286196Sbapt cmdcnf->default_group = cfg->default_group; 1080286196Sbapt if (cmdcnf->groups == NULL) 1081286196Sbapt cmdcnf->groups = cfg->groups; 1082286196Sbapt if (cmdcnf->default_class == NULL) 1083286196Sbapt cmdcnf->default_class = cfg->default_class; 1084286196Sbapt if (cmdcnf->min_uid == 0) 1085286196Sbapt cmdcnf->min_uid = cfg->min_uid; 1086286196Sbapt if (cmdcnf->max_uid == 0) 1087286196Sbapt cmdcnf->max_uid = cfg->max_uid; 1088286196Sbapt if (cmdcnf->min_gid == 0) 1089286196Sbapt cmdcnf->min_gid = cfg->min_gid; 1090286196Sbapt if (cmdcnf->max_gid == 0) 1091286196Sbapt cmdcnf->max_gid = cfg->max_gid; 1092286196Sbapt if (cmdcnf->expire_days == 0) 1093286196Sbapt cmdcnf->expire_days = cfg->expire_days; 1094286196Sbapt if (cmdcnf->password_days == 0) 1095286196Sbapt cmdcnf->password_days = cfg->password_days; 1096286196Sbapt} 1097286196Sbapt 1098286196Sbaptint 1099286196Sbaptpw_user_add(int argc, char **argv, char *arg1) 1100286196Sbapt{ 1101286196Sbapt struct userconf *cnf, *cmdcnf; 1102286196Sbapt struct passwd *pwd; 1103286196Sbapt struct group *grp; 1104286196Sbapt struct stat st; 1105286196Sbapt char args[] = "C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y"; 1106286196Sbapt char line[_PASSWORD_LEN+1], path[MAXPATHLEN]; 1107286196Sbapt char *gecos, *homedir, *skel, *walk, *userid, *groupid, *grname; 1108286196Sbapt char *default_passwd, *name, *p; 1109286196Sbapt const char *cfg; 1110286196Sbapt login_cap_t *lc; 1111286196Sbapt FILE *pfp, *fp; 1112286196Sbapt intmax_t id = -1; 1113286196Sbapt time_t now; 1114286196Sbapt int rc, ch, fd = -1; 1115286196Sbapt size_t i; 1116286196Sbapt bool dryrun, nis, pretty, quiet, createhome, precrypted, genconf; 1117286196Sbapt 1118286196Sbapt dryrun = nis = pretty = quiet = createhome = precrypted = false; 1119286196Sbapt genconf = false; 1120286196Sbapt gecos = homedir = skel = userid = groupid = default_passwd = NULL; 1121286196Sbapt grname = name = NULL; 1122286196Sbapt 1123286196Sbapt if ((cmdcnf = calloc(1, sizeof(struct userconf))) == NULL) 1124286196Sbapt err(EXIT_FAILURE, "calloc()"); 1125286196Sbapt 1126286196Sbapt if (arg1 != NULL) { 1127286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 1128286196Sbapt id = pw_checkid(arg1, UID_MAX); 1129286196Sbapt else 1130286196Sbapt name = arg1; 1131286196Sbapt } 1132286196Sbapt 1133286196Sbapt while ((ch = getopt(argc, argv, args)) != -1) { 1134286196Sbapt switch (ch) { 1135286196Sbapt case 'C': 1136286196Sbapt cfg = optarg; 1137286196Sbapt break; 1138286196Sbapt case 'q': 1139286196Sbapt quiet = true; 1140286196Sbapt break; 1141286196Sbapt case 'n': 1142286196Sbapt name = optarg; 1143286196Sbapt break; 1144286196Sbapt case 'u': 1145286196Sbapt userid = optarg; 1146286196Sbapt break; 1147286196Sbapt case 'c': 1148286196Sbapt gecos = pw_checkname(optarg, 1); 1149286196Sbapt break; 1150286196Sbapt case 'd': 1151286196Sbapt homedir = optarg; 1152286196Sbapt break; 1153286196Sbapt case 'e': 1154286196Sbapt now = time(NULL); 1155286196Sbapt cmdcnf->expire_days = parse_date(now, optarg); 1156286196Sbapt break; 1157286196Sbapt case 'p': 1158286196Sbapt now = time(NULL); 1159286196Sbapt cmdcnf->password_days = parse_date(now, optarg); 1160286196Sbapt break; 1161286196Sbapt case 'g': 1162286196Sbapt validate_grname(cmdcnf, optarg); 1163286196Sbapt grname = optarg; 1164286196Sbapt break; 1165286196Sbapt case 'G': 1166286196Sbapt split_groups(&cmdcnf->groups, optarg); 1167286196Sbapt break; 1168286196Sbapt case 'm': 1169286196Sbapt createhome = true; 1170286196Sbapt break; 1171286196Sbapt case 'M': 1172286196Sbapt cmdcnf->homemode = validate_mode(optarg); 1173286196Sbapt break; 1174286196Sbapt case 'k': 1175286196Sbapt walk = skel = optarg; 1176286196Sbapt if (*walk == '/') 1177286196Sbapt walk++; 1178286196Sbapt if (fstatat(conf.rootfd, walk, &st, 0) == -1) 1179286196Sbapt errx(EX_OSFILE, "skeleton `%s' does not " 1180286196Sbapt "exists", skel); 1181286196Sbapt if (!S_ISDIR(st.st_mode)) 1182286196Sbapt errx(EX_OSFILE, "skeleton `%s' is not a " 1183286196Sbapt "directory", skel); 1184286196Sbapt cmdcnf->dotdir = skel; 1185286196Sbapt break; 1186286196Sbapt case 's': 1187286196Sbapt cmdcnf->shell_default = optarg; 1188286196Sbapt break; 1189286196Sbapt case 'o': 1190286196Sbapt conf.checkduplicate = false; 1191286196Sbapt break; 1192286196Sbapt case 'L': 1193286196Sbapt cmdcnf->default_class = pw_checkname(optarg, 0); 1194286196Sbapt break; 1195286196Sbapt case 'i': 1196286196Sbapt groupid = optarg; 1197286196Sbapt break; 1198286196Sbapt case 'w': 1199286196Sbapt default_passwd = optarg; 1200286196Sbapt break; 1201286196Sbapt case 'H': 1202286196Sbapt if (fd != -1) 1203286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1204286196Sbapt "exclusive options"); 1205286196Sbapt fd = pw_checkfd(optarg); 1206286196Sbapt precrypted = true; 1207286196Sbapt if (fd == '-') 1208286196Sbapt errx(EX_USAGE, "-H expects a file descriptor"); 1209286196Sbapt break; 1210286196Sbapt case 'h': 1211286196Sbapt if (fd != -1) 1212286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1213286196Sbapt "exclusive options"); 1214286196Sbapt fd = pw_checkfd(optarg); 1215286196Sbapt break; 1216286196Sbapt case 'D': 1217286196Sbapt genconf = true; 1218286196Sbapt break; 1219286196Sbapt case 'b': 1220286196Sbapt cmdcnf->home = optarg; 1221286196Sbapt break; 1222286196Sbapt case 'N': 1223286196Sbapt dryrun = true; 1224286196Sbapt break; 1225286196Sbapt case 'P': 1226286196Sbapt pretty = true; 1227286196Sbapt break; 1228286196Sbapt case 'y': 1229286196Sbapt cmdcnf->nispasswd = optarg; 1230286196Sbapt break; 1231286196Sbapt case 'Y': 1232286196Sbapt nis = true; 1233286196Sbapt break; 1234109961Sgad } 1235109961Sgad } 1236286196Sbapt 1237286196Sbapt if (geteuid() != 0 && ! dryrun) 1238286196Sbapt errx(EX_NOPERM, "you must be root"); 1239286196Sbapt 1240286196Sbapt if (quiet) 1241286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 1242286196Sbapt 1243286196Sbapt cnf = get_userconfig(cfg); 1244286196Sbapt 1245286196Sbapt mix_config(cmdcnf, cnf); 1246286196Sbapt if (default_passwd) 1247286196Sbapt cmdcnf->default_password = boolean_val(default_passwd, 1248286196Sbapt cnf->default_password); 1249286196Sbapt if (genconf) { 1250286196Sbapt if (name != NULL) 1251286196Sbapt errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 1252286196Sbapt if (userid != NULL) { 1253286196Sbapt if ((p = strtok(userid, ", \t")) != NULL) 1254286196Sbapt cmdcnf->min_uid = pw_checkid(p, UID_MAX); 1255286196Sbapt if (cmdcnf->min_uid == 0) 1256286196Sbapt cmdcnf->min_uid = 1000; 1257286196Sbapt if ((p = strtok(NULL, " ,\t")) != NULL) 1258286196Sbapt cmdcnf->max_uid = pw_checkid(p, UID_MAX); 1259286196Sbapt if (cmdcnf->max_uid == 0) 1260286196Sbapt cmdcnf->max_uid = 32000; 1261109961Sgad } 1262286196Sbapt if (groupid != NULL) { 1263286196Sbapt if ((p = strtok(groupid, ", \t")) != NULL) 1264286196Sbapt cmdcnf->min_gid = pw_checkid(p, GID_MAX); 1265286196Sbapt if (cmdcnf->min_gid == 0) 1266286196Sbapt cmdcnf->min_gid = 1000; 1267286196Sbapt if ((p = strtok(NULL, " ,\t")) != NULL) 1268286196Sbapt cmdcnf->max_gid = pw_checkid(p, GID_MAX); 1269286196Sbapt if (cmdcnf->max_gid == 0) 1270286196Sbapt cmdcnf->max_gid = 32000; 1271286196Sbapt } 1272286196Sbapt if (write_userconfig(cmdcnf, cfg)) 1273286196Sbapt return (EXIT_SUCCESS); 1274286196Sbapt err(EX_IOERR, "config update"); 1275109961Sgad } 1276286196Sbapt 1277286196Sbapt if (userid) 1278286196Sbapt id = pw_checkid(userid, UID_MAX); 1279286196Sbapt if (id < 0 && name == NULL) 1280286196Sbapt errx(EX_DATAERR, "user name or id required"); 1281286196Sbapt 1282286196Sbapt if (name == NULL) 1283286196Sbapt errx(EX_DATAERR, "login name required"); 1284286196Sbapt 1285286198Sbapt if (GETPWNAM(name) != NULL) 1286286198Sbapt errx(EX_DATAERR, "login name `%s' already exists", name); 1287286198Sbapt 1288286196Sbapt pwd = &fakeuser; 1289286196Sbapt pwd->pw_name = name; 1290286196Sbapt pwd->pw_class = cmdcnf->default_class ? cmdcnf->default_class : ""; 1291286196Sbapt pwd->pw_uid = pw_uidpolicy(cmdcnf, id); 1292286196Sbapt pwd->pw_gid = pw_gidpolicy(cnf, grname, pwd->pw_name, 1293286196Sbapt (gid_t) pwd->pw_uid, dryrun); 1294286196Sbapt pwd->pw_change = cmdcnf->password_days; 1295286196Sbapt pwd->pw_expire = cmdcnf->expire_days; 1296286196Sbapt pwd->pw_dir = pw_homepolicy(cmdcnf, homedir, pwd->pw_name); 1297286196Sbapt pwd->pw_shell = pw_shellpolicy(cmdcnf); 1298286196Sbapt lc = login_getpwclass(pwd); 1299286196Sbapt if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 1300286196Sbapt warn("setting crypt(3) format"); 1301286196Sbapt login_close(lc); 1302286196Sbapt pwd->pw_passwd = pw_password(cmdcnf, pwd->pw_name, dryrun); 1303286196Sbapt if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 1304286196Sbapt warnx("WARNING: new account `%s' has a uid of 0 " 1305286196Sbapt "(superuser access!)", pwd->pw_name); 1306286196Sbapt if (gecos) 1307286196Sbapt pwd->pw_gecos = gecos; 1308286196Sbapt 1309286196Sbapt if (fd != -1) 1310286196Sbapt pw_set_passwd(pwd, fd, precrypted, false); 1311286196Sbapt 1312286196Sbapt if (dryrun) 1313286196Sbapt return (print_user(pwd, pretty, false)); 1314286196Sbapt 1315286196Sbapt if ((rc = addpwent(pwd)) != 0) { 1316286196Sbapt if (rc == -1) 1317286196Sbapt errx(EX_IOERR, "user '%s' already exists", 1318286196Sbapt pwd->pw_name); 1319286196Sbapt else if (rc != 0) 1320286196Sbapt err(EX_IOERR, "passwd file update"); 1321109961Sgad } 1322286196Sbapt if (nis && cmdcnf->nispasswd && *cmdcnf->nispasswd == '/') { 1323286196Sbapt printf("%s\n", cmdcnf->nispasswd); 1324286196Sbapt rc = addnispwent(cmdcnf->nispasswd, pwd); 1325286196Sbapt if (rc == -1) 1326286202Sbapt warnx("User '%s' already exists in NIS passwd", 1327286202Sbapt pwd->pw_name); 1328286196Sbapt else if (rc != 0) 1329286196Sbapt warn("NIS passwd update"); 1330286196Sbapt /* NOTE: we treat NIS-only update errors as non-fatal */ 1331286196Sbapt } 1332284110Sbapt 1333286196Sbapt if (cmdcnf->groups != NULL) { 1334286196Sbapt for (i = 0; i < cmdcnf->groups->sl_cur; i++) { 1335286196Sbapt grp = GETGRNAM(cmdcnf->groups->sl_str[i]); 1336286196Sbapt grp = gr_add(grp, pwd->pw_name); 1337286196Sbapt /* 1338286196Sbapt * grp can only be NULL in 2 cases: 1339286196Sbapt * - the new member is already a member 1340286196Sbapt * - a problem with memory occurs 1341286196Sbapt * in both cases we want to skip now. 1342286196Sbapt */ 1343286196Sbapt if (grp == NULL) 1344286196Sbapt continue; 1345286196Sbapt chggrent(grp->gr_name, grp); 1346286196Sbapt free(grp); 1347286196Sbapt } 1348286196Sbapt } 134920253Sjoerg 1350286196Sbapt pwd = GETPWNAM(name); 1351286196Sbapt if (pwd == NULL) 1352286196Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 135320253Sjoerg 1354286196Sbapt grp = GETGRGID(pwd->pw_gid); 1355286196Sbapt pw_log(cnf, M_ADD, W_USER, "%s(%ju):%s(%ju):%s:%s:%s", 1356286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 1357286202Sbapt grp ? grp->gr_name : "unknown", 1358286202Sbapt (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1), 1359286196Sbapt pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 136020253Sjoerg 1361286196Sbapt /* 1362286196Sbapt * let's touch and chown the user's mail file. This is not 1363286196Sbapt * strictly necessary under BSD with a 0755 maildir but it also 1364286196Sbapt * doesn't hurt anything to create the empty mailfile 1365286196Sbapt */ 1366286196Sbapt if (PWALTDIR() != PWF_ALT) { 1367286196Sbapt snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, 1368286196Sbapt pwd->pw_name); 1369286196Sbapt /* Preserve contents & mtime */ 1370286196Sbapt close(openat(conf.rootfd, path +1, O_RDWR | O_CREAT, 0600)); 1371286196Sbapt fchownat(conf.rootfd, path + 1, pwd->pw_uid, pwd->pw_gid, 1372286196Sbapt AT_SYMLINK_NOFOLLOW); 1373286196Sbapt } 137420253Sjoerg 1375286196Sbapt /* 1376286196Sbapt * Let's create and populate the user's home directory. Note 1377286196Sbapt * that this also `works' for editing users if -m is used, but 1378286196Sbapt * existing files will *not* be overwritten. 1379286196Sbapt */ 1380286196Sbapt if (PWALTDIR() != PWF_ALT && createhome && pwd->pw_dir && 1381286196Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) 1382286196Sbapt create_and_populate_homedir(cmdcnf, pwd, cmdcnf->dotdir, 1383286196Sbapt cmdcnf->homemode, false); 138420253Sjoerg 1385286196Sbapt if (!PWALTDIR() && cmdcnf->newmail && *cmdcnf->newmail && 1386286196Sbapt (fp = fopen(cnf->newmail, "r")) != NULL) { 1387286196Sbapt if ((pfp = popen(_PATH_SENDMAIL " -t", "w")) == NULL) 1388286196Sbapt warn("sendmail"); 1389286196Sbapt else { 1390286196Sbapt fprintf(pfp, "From: root\n" "To: %s\n" 1391286196Sbapt "Subject: Welcome!\n\n", pwd->pw_name); 1392286196Sbapt while (fgets(line, sizeof(line), fp) != NULL) { 1393286196Sbapt /* Do substitutions? */ 1394286196Sbapt fputs(line, pfp); 139520253Sjoerg } 1396286196Sbapt pclose(pfp); 1397286196Sbapt pw_log(cnf, M_ADD, W_USER, "%s(%ju) new user mail sent", 1398286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid); 139920253Sjoerg } 1400286196Sbapt fclose(fp); 140120253Sjoerg } 1402286196Sbapt 1403286196Sbapt if (nis && nis_update() == 0) 1404286196Sbapt pw_log(cnf, M_ADD, W_USER, "NIS maps updated"); 1405286196Sbapt 1406286196Sbapt return (EXIT_SUCCESS); 140720253Sjoerg} 140820747Sdavidn 1409286196Sbaptint 1410286196Sbaptpw_user_mod(int argc, char **argv, char *arg1) 141120747Sdavidn{ 1412286196Sbapt struct userconf *cnf; 1413286196Sbapt struct passwd *pwd; 1414286196Sbapt struct group *grp; 1415286196Sbapt StringList *groups = NULL; 1416286196Sbapt char args[] = "C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:NPYy:"; 1417286196Sbapt const char *cfg; 1418286196Sbapt char *gecos, *homedir, *grname, *name, *newname, *walk, *skel, *shell; 1419286196Sbapt char *passwd, *class, *nispasswd; 1420286196Sbapt login_cap_t *lc; 1421286196Sbapt struct stat st; 1422286196Sbapt intmax_t id = -1; 1423286196Sbapt int ch, fd = -1; 1424286196Sbapt size_t i, j; 1425286196Sbapt bool quiet, createhome, pretty, dryrun, nis, edited, docreatehome; 1426286218Sbapt bool precrypted; 1427286196Sbapt mode_t homemode = 0; 1428286218Sbapt time_t expire_days, password_days, now; 142920747Sdavidn 1430286196Sbapt expire_days = password_days = -1; 1431286196Sbapt gecos = homedir = grname = name = newname = skel = shell =NULL; 1432286196Sbapt passwd = NULL; 1433286196Sbapt class = nispasswd = NULL; 1434286196Sbapt quiet = createhome = pretty = dryrun = nis = precrypted = false; 1435286196Sbapt edited = docreatehome = false; 143620747Sdavidn 1437286196Sbapt if (arg1 != NULL) { 1438286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 1439286196Sbapt id = pw_checkid(arg1, UID_MAX); 1440286196Sbapt else 1441286196Sbapt name = arg1; 1442286196Sbapt } 1443286196Sbapt 1444286196Sbapt while ((ch = getopt(argc, argv, args)) != -1) { 1445286196Sbapt switch (ch) { 1446286196Sbapt case 'C': 1447286196Sbapt cfg = optarg; 1448285433Sbapt break; 1449286196Sbapt case 'q': 1450286196Sbapt quiet = true; 1451286196Sbapt break; 1452286196Sbapt case 'n': 1453286196Sbapt name = optarg; 1454286196Sbapt break; 1455286196Sbapt case 'u': 1456286196Sbapt id = pw_checkid(optarg, UID_MAX); 1457286196Sbapt break; 1458286196Sbapt case 'c': 1459286196Sbapt gecos = pw_checkname(optarg, 1); 1460286196Sbapt break; 1461286196Sbapt case 'd': 1462286196Sbapt homedir = optarg; 1463286196Sbapt break; 1464286196Sbapt case 'e': 1465286196Sbapt now = time(NULL); 1466286196Sbapt expire_days = parse_date(now, optarg); 1467286196Sbapt break; 1468286196Sbapt case 'p': 1469286196Sbapt now = time(NULL); 1470286196Sbapt password_days = parse_date(now, optarg); 1471286196Sbapt break; 1472286196Sbapt case 'g': 1473286196Sbapt group_from_name_or_id(optarg); 1474286196Sbapt grname = optarg; 1475286196Sbapt break; 1476286196Sbapt case 'G': 1477286196Sbapt split_groups(&groups, optarg); 1478286196Sbapt break; 1479286196Sbapt case 'm': 1480286196Sbapt createhome = true; 1481286196Sbapt break; 1482286196Sbapt case 'M': 1483286196Sbapt homemode = validate_mode(optarg); 1484286196Sbapt break; 1485286196Sbapt case 'l': 1486286196Sbapt newname = optarg; 1487286196Sbapt break; 1488286196Sbapt case 'k': 1489286196Sbapt walk = skel = optarg; 1490286196Sbapt if (*walk == '/') 1491286196Sbapt walk++; 1492286196Sbapt if (fstatat(conf.rootfd, walk, &st, 0) == -1) 1493286196Sbapt errx(EX_OSFILE, "skeleton `%s' does not " 1494286196Sbapt "exists", skel); 1495286196Sbapt if (!S_ISDIR(st.st_mode)) 1496286196Sbapt errx(EX_OSFILE, "skeleton `%s' is not a " 1497286196Sbapt "directory", skel); 1498286196Sbapt break; 1499286196Sbapt case 's': 1500286196Sbapt shell = optarg; 1501286196Sbapt break; 1502286196Sbapt case 'w': 1503286196Sbapt passwd = optarg; 1504286196Sbapt break; 1505286196Sbapt case 'L': 1506286196Sbapt class = pw_checkname(optarg, 0); 1507286196Sbapt break; 1508286196Sbapt case 'H': 1509286196Sbapt if (fd != -1) 1510286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1511286196Sbapt "exclusive options"); 1512286196Sbapt fd = pw_checkfd(optarg); 1513286196Sbapt precrypted = true; 1514286196Sbapt if (fd == '-') 1515286196Sbapt errx(EX_USAGE, "-H expects a file descriptor"); 1516286196Sbapt break; 1517286196Sbapt case 'h': 1518286196Sbapt if (fd != -1) 1519286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1520286196Sbapt "exclusive options"); 1521286196Sbapt fd = pw_checkfd(optarg); 1522286196Sbapt break; 1523286196Sbapt case 'N': 1524286196Sbapt dryrun = true; 1525286196Sbapt break; 1526286196Sbapt case 'P': 1527286196Sbapt pretty = true; 1528286196Sbapt break; 1529286196Sbapt case 'y': 1530286196Sbapt nispasswd = optarg; 1531286196Sbapt break; 1532286196Sbapt case 'Y': 1533286196Sbapt nis = true; 1534286196Sbapt break; 153520747Sdavidn } 153620747Sdavidn } 1537286196Sbapt 1538286196Sbapt if (geteuid() != 0 && ! dryrun) 1539286196Sbapt errx(EX_NOPERM, "you must be root"); 1540286196Sbapt 1541286196Sbapt if (quiet) 1542286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 1543286196Sbapt 1544286196Sbapt cnf = get_userconfig(cfg); 1545286196Sbapt 1546286196Sbapt if (id < 0 && name == NULL) 1547286196Sbapt errx(EX_DATAERR, "username or id required"); 1548286196Sbapt 1549286196Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 1550286196Sbapt if (pwd == NULL) { 1551286196Sbapt if (name == NULL) 1552286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", 1553286196Sbapt (uintmax_t) id); 1554286196Sbapt errx(EX_NOUSER, "no such user `%s'", name); 1555286196Sbapt } 1556286196Sbapt 1557286196Sbapt if (name == NULL) 1558286196Sbapt name = pwd->pw_name; 1559286196Sbapt 1560286196Sbapt if (nis && nispasswd == NULL) 1561286196Sbapt nispasswd = cnf->nispasswd; 1562286196Sbapt 1563286196Sbapt if (PWF._altdir == PWF_REGULAR && 1564286196Sbapt ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) { 1565286196Sbapt if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 1566286196Sbapt if (!nis && nispasswd && *nispasswd != '/') 1567286196Sbapt errx(EX_NOUSER, "Cannot modify NIS user `%s'", 1568286196Sbapt name); 1569286196Sbapt } else { 1570286196Sbapt errx(EX_NOUSER, "Cannot modify non local user `%s'", 1571286196Sbapt name); 1572286196Sbapt } 1573286196Sbapt } 1574286196Sbapt 1575286196Sbapt if (newname) { 1576286196Sbapt if (strcmp(pwd->pw_name, "root") == 0) 1577286196Sbapt errx(EX_DATAERR, "can't rename `root' account"); 1578286196Sbapt if (strcmp(pwd->pw_name, newname) != 0) { 1579286196Sbapt pwd->pw_name = pw_checkname(newname, 0); 1580286196Sbapt edited = true; 1581286196Sbapt } 1582286196Sbapt } 1583286196Sbapt 1584286196Sbapt if (id > 0 && pwd->pw_uid != id) { 1585286196Sbapt pwd->pw_uid = id; 1586286196Sbapt edited = true; 1587286196Sbapt if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 1588286196Sbapt errx(EX_DATAERR, "can't change uid of `root' account"); 1589286196Sbapt if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 1590286202Sbapt warnx("WARNING: account `%s' will have a uid of 0 " 1591286202Sbapt "(superuser access!)", pwd->pw_name); 1592286196Sbapt } 1593286196Sbapt 1594286196Sbapt if (grname && pwd->pw_uid != 0) { 1595286196Sbapt grp = GETGRNAM(grname); 1596286196Sbapt if (grp == NULL) 1597286196Sbapt grp = GETGRGID(pw_checkid(grname, GID_MAX)); 1598286196Sbapt if (grp->gr_gid != pwd->pw_gid) { 1599286196Sbapt pwd->pw_gid = grp->gr_gid; 1600286196Sbapt edited = true; 1601286196Sbapt } 1602286196Sbapt } 1603286196Sbapt 1604286196Sbapt if (password_days >= 0 && pwd->pw_change != password_days) { 1605286196Sbapt pwd->pw_change = password_days; 1606286196Sbapt edited = true; 1607286196Sbapt } 1608286196Sbapt 1609286196Sbapt if (expire_days >= 0 && pwd->pw_expire != expire_days) { 1610286196Sbapt pwd->pw_expire = expire_days; 1611286196Sbapt edited = true; 1612286196Sbapt } 1613286196Sbapt 1614286196Sbapt if (shell) { 1615286196Sbapt shell = shell_path(cnf->shelldir, cnf->shells, shell); 1616286196Sbapt if (shell == NULL) 1617286196Sbapt shell = ""; 1618286196Sbapt if (strcmp(shell, pwd->pw_shell) != 0) { 1619286196Sbapt pwd->pw_shell = shell; 1620286196Sbapt edited = true; 1621286196Sbapt } 1622286196Sbapt } 1623286196Sbapt 1624286196Sbapt if (class && strcmp(pwd->pw_class, class) != 0) { 1625286196Sbapt pwd->pw_class = class; 1626286196Sbapt edited = true; 1627286196Sbapt } 1628286196Sbapt 1629286196Sbapt if (homedir && strcmp(pwd->pw_dir, homedir) != 0) { 1630286196Sbapt pwd->pw_dir = homedir; 1631286196Sbapt if (fstatat(conf.rootfd, pwd->pw_dir, &st, 0) == -1) { 1632286196Sbapt if (!createhome) 1633286196Sbapt warnx("WARNING: home `%s' does not exist", 1634286196Sbapt pwd->pw_dir); 1635286196Sbapt else 1636286196Sbapt docreatehome = true; 1637286196Sbapt } else if (!S_ISDIR(st.st_mode)) { 1638286196Sbapt warnx("WARNING: home `%s' is not a directory", 1639286196Sbapt pwd->pw_dir); 1640286196Sbapt } 1641286196Sbapt } 1642286196Sbapt 1643286196Sbapt if (passwd && conf.fd == -1) { 1644286196Sbapt lc = login_getpwclass(pwd); 1645286196Sbapt if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 1646286196Sbapt warn("setting crypt(3) format"); 1647286196Sbapt login_close(lc); 1648286196Sbapt pwd->pw_passwd = pw_password(cnf, pwd->pw_name, dryrun); 1649286196Sbapt edited = true; 1650286196Sbapt } 1651286196Sbapt 1652286196Sbapt if (gecos && strcmp(pwd->pw_gecos, gecos) != 0) { 1653286196Sbapt pwd->pw_gecos = gecos; 1654286196Sbapt edited = true; 1655286196Sbapt } 1656286196Sbapt 1657286196Sbapt if (fd != -1) 1658286196Sbapt edited = pw_set_passwd(pwd, fd, precrypted, true); 1659286196Sbapt 1660286196Sbapt if (dryrun) 1661286196Sbapt return (print_user(pwd, pretty, false)); 1662286196Sbapt 1663286196Sbapt if (edited) /* Only updated this if required */ 1664286196Sbapt perform_chgpwent(name, pwd, nis ? nispasswd : NULL); 1665286196Sbapt /* Now perform the needed changes concern groups */ 1666286196Sbapt if (groups != NULL) { 1667286196Sbapt /* Delete User from groups using old name */ 1668286196Sbapt SETGRENT(); 1669286196Sbapt while ((grp = GETGRENT()) != NULL) { 1670286196Sbapt if (grp->gr_mem == NULL) 1671286196Sbapt continue; 1672286196Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1673286196Sbapt if (strcmp(grp->gr_mem[i] , name) != 0) 1674286196Sbapt continue; 1675286196Sbapt for (j = i; grp->gr_mem[j] != NULL ; j++) 1676286196Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 1677286196Sbapt chggrent(grp->gr_name, grp); 1678286196Sbapt break; 1679286196Sbapt } 1680286196Sbapt } 1681286196Sbapt ENDGRENT(); 1682286196Sbapt /* Add the user to the needed groups */ 1683286196Sbapt for (i = 0; i < groups->sl_cur; i++) { 1684286196Sbapt grp = GETGRNAM(groups->sl_str[i]); 1685286196Sbapt grp = gr_add(grp, pwd->pw_name); 1686286196Sbapt if (grp == NULL) 1687286196Sbapt continue; 1688286196Sbapt chggrent(grp->gr_name, grp); 1689286196Sbapt free(grp); 1690286196Sbapt } 1691286196Sbapt } 1692286196Sbapt /* In case of rename we need to walk over the different groups */ 1693286196Sbapt if (newname) { 1694286196Sbapt SETGRENT(); 1695286196Sbapt while ((grp = GETGRENT()) != NULL) { 1696286196Sbapt if (grp->gr_mem == NULL) 1697286196Sbapt continue; 1698286196Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1699286196Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 1700286196Sbapt continue; 1701286196Sbapt grp->gr_mem[i] = newname; 1702286196Sbapt chggrent(grp->gr_name, grp); 1703286196Sbapt break; 1704286196Sbapt } 1705286196Sbapt } 1706286196Sbapt } 1707286196Sbapt 1708286196Sbapt /* go get a current version of pwd */ 1709286196Sbapt if (newname) 1710286196Sbapt name = newname; 1711286196Sbapt pwd = GETPWNAM(name); 1712286196Sbapt if (pwd == NULL) 1713286196Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 1714286196Sbapt grp = GETGRGID(pwd->pw_gid); 1715286196Sbapt pw_log(cnf, M_UPDATE, W_USER, "%s(%ju):%s(%ju):%s:%s:%s", 1716286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 1717286196Sbapt grp ? grp->gr_name : "unknown", 1718286196Sbapt (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1), 1719286196Sbapt pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 1720286196Sbapt 1721285433Sbapt /* 1722286196Sbapt * Let's create and populate the user's home directory. Note 1723286196Sbapt * that this also `works' for editing users if -m is used, but 1724286196Sbapt * existing files will *not* be overwritten. 1725285433Sbapt */ 1726286196Sbapt if (PWALTDIR() != PWF_ALT && docreatehome && pwd->pw_dir && 1727286196Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 1728286196Sbapt if (!skel) 1729286196Sbapt skel = cnf->dotdir; 1730286196Sbapt if (homemode == 0) 1731286196Sbapt homemode = cnf->homemode; 1732286196Sbapt create_and_populate_homedir(cnf, pwd, skel, homemode, true); 1733286196Sbapt } 1734286196Sbapt 1735286196Sbapt if (nis && nis_update() == 0) 1736286196Sbapt pw_log(cnf, M_UPDATE, W_USER, "NIS maps updated"); 1737286196Sbapt 1738286196Sbapt return (EXIT_SUCCESS); 173920747Sdavidn} 1740