pw_user.c revision 286196
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 286196 2015-08-02 12:47:50Z bapt $"; 3130259Scharnier#endif /* not lint */ 3230259Scharnier 3330259Scharnier#include <ctype.h> 3430259Scharnier#include <err.h> 3520253Sjoerg#include <fcntl.h> 36286150Sbapt#include <inttypes.h> 3720253Sjoerg#include <sys/param.h> 3820253Sjoerg#include <dirent.h> 3930259Scharnier#include <paths.h> 4020253Sjoerg#include <termios.h> 4120555Sdavidn#include <sys/types.h> 4220555Sdavidn#include <sys/time.h> 4320555Sdavidn#include <sys/resource.h> 4464918Sgreen#include <login_cap.h> 45242349Sbapt#include <pwd.h> 46242349Sbapt#include <grp.h> 47242349Sbapt#include <libutil.h> 4820253Sjoerg#include "pw.h" 4920253Sjoerg#include "bitmap.h" 5020253Sjoerg 5123318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 5222394Sdavidn 5352512Sdavidnstatic char locked_str[] = "*LOCKED*"; 5424214Sache 55286196Sbaptstatic struct passwd fakeuser = { 56286196Sbapt "nouser", 57286196Sbapt "*", 58286196Sbapt -1, 59286196Sbapt -1, 60286196Sbapt 0, 61286196Sbapt "", 62286196Sbapt "User &", 63286196Sbapt "/nonexistent", 64286196Sbapt "/bin/sh", 65286196Sbapt 0, 66286196Sbapt 0 67286196Sbapt}; 6820253Sjoerg 69286196Sbaptstatic int print_user(struct passwd *pwd, bool pretty, bool v7); 70286196Sbaptstatic uid_t pw_uidpolicy(struct userconf *cnf, intmax_t id); 71286196Sbaptstatic uid_t pw_gidpolicy(struct userconf *cnf, char *grname, char *nam, 72286196Sbapt gid_t prefer, bool dryrun); 73286196Sbaptstatic char *pw_homepolicy(struct userconf * cnf, char *homedir, 74286196Sbapt const char *user); 75286196Sbaptstatic char *pw_shellpolicy(struct userconf * cnf); 76286196Sbaptstatic char *pw_password(struct userconf * cnf, char const * user, 77286196Sbapt bool dryrun); 78286196Sbaptstatic char *shell_path(char const * path, char *shells[], char *sh); 79286196Sbaptstatic void rmat(uid_t uid); 80286196Sbaptstatic void rmopie(char const * name); 81286196Sbapt 82283961Sbaptstatic void 83286196Sbaptcreate_and_populate_homedir(struct userconf *cnf, struct passwd *pwd, 84286196Sbapt const char *skeldir, mode_t homemode, bool update) 85283961Sbapt{ 86285430Sbapt int skelfd = -1; 87283961Sbapt 88285430Sbapt if (skeldir != NULL && *skeldir != '\0') { 89285434Sbapt if (*skeldir == '/') 90285434Sbapt skeldir++; 91285434Sbapt skelfd = openat(conf.rootfd, skeldir, O_DIRECTORY|O_CLOEXEC); 92283961Sbapt } 93283961Sbapt 94286196Sbapt copymkdir(conf.rootfd, pwd->pw_dir, skelfd, homemode, pwd->pw_uid, 95285430Sbapt pwd->pw_gid, 0); 96286196Sbapt pw_log(cnf, update ? M_UPDATE : M_ADD, W_USER, "%s(%ju) home %s made", 97286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, pwd->pw_dir); 98283961Sbapt} 99283961Sbapt 100285133Sbaptstatic int 101286196Sbaptpw_set_passwd(struct passwd *pwd, int fd, bool precrypted, bool update) 102285133Sbapt{ 103285133Sbapt int b, istty; 104285133Sbapt struct termios t, n; 105285133Sbapt login_cap_t *lc; 106285133Sbapt char line[_PASSWORD_LEN+1]; 107285133Sbapt char *p; 108285133Sbapt 109286196Sbapt if (fd == '-') { 110285133Sbapt if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 111285133Sbapt pwd->pw_passwd = "*"; /* No access */ 112285133Sbapt return (1); 113285133Sbapt } 114285133Sbapt return (0); 115285133Sbapt } 116285133Sbapt 117286196Sbapt if ((istty = isatty(fd))) { 118286196Sbapt if (tcgetattr(fd, &t) == -1) 119285133Sbapt istty = 0; 120285133Sbapt else { 121285137Sbapt n = t; 122285133Sbapt n.c_lflag &= ~(ECHO); 123286196Sbapt tcsetattr(fd, TCSANOW, &n); 124285133Sbapt printf("%s%spassword for user %s:", 125285133Sbapt update ? "new " : "", 126286196Sbapt precrypted ? "encrypted " : "", 127285133Sbapt pwd->pw_name); 128285133Sbapt fflush(stdout); 129285133Sbapt } 130285133Sbapt } 131286196Sbapt b = read(fd, line, sizeof(line) - 1); 132285133Sbapt if (istty) { /* Restore state */ 133286196Sbapt tcsetattr(fd, TCSANOW, &t); 134285133Sbapt fputc('\n', stdout); 135285133Sbapt fflush(stdout); 136285133Sbapt } 137285133Sbapt 138285133Sbapt if (b < 0) 139285133Sbapt err(EX_IOERR, "-%c file descriptor", 140286196Sbapt precrypted ? 'H' : 'h'); 141285133Sbapt line[b] = '\0'; 142285133Sbapt if ((p = strpbrk(line, "\r\n")) != NULL) 143285133Sbapt *p = '\0'; 144285133Sbapt if (!*line) 145285133Sbapt errx(EX_DATAERR, "empty password read on file descriptor %d", 146286196Sbapt fd); 147286196Sbapt if (precrypted) { 148285133Sbapt if (strchr(line, ':') != NULL) 149285137Sbapt errx(EX_DATAERR, "bad encrypted password"); 150286196Sbapt pwd->pw_passwd = strdup(line); 151285133Sbapt } else { 152285133Sbapt lc = login_getpwclass(pwd); 153285133Sbapt if (lc == NULL || 154285133Sbapt login_setcryptfmt(lc, "sha512", NULL) == NULL) 155285133Sbapt warn("setting crypt(3) format"); 156285133Sbapt login_close(lc); 157285133Sbapt pwd->pw_passwd = pw_pwcrypt(line); 158285133Sbapt } 159285133Sbapt return (1); 160285133Sbapt} 161285133Sbapt 162285405Sbaptstatic void 163286196Sbaptperform_chgpwent(const char *name, struct passwd *pwd, char *nispasswd) 164285405Sbapt{ 165285405Sbapt int rc; 166286196Sbapt struct passwd *nispwd; 167285405Sbapt 168286196Sbapt /* duplicate for nis so that chgpwent is not modifying before NIS */ 169286196Sbapt if (nispasswd && *nispasswd == '/') 170286196Sbapt nispwd = pw_dup(pwd); 171286196Sbapt 172285405Sbapt rc = chgpwent(name, pwd); 173285405Sbapt if (rc == -1) 174285405Sbapt errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name); 175285405Sbapt else if (rc != 0) 176285405Sbapt err(EX_IOERR, "passwd file update"); 177285405Sbapt 178286196Sbapt if (nispasswd && *nispasswd == '/') { 179286196Sbapt rc = chgnispwent(nispasswd, name, nispwd); 180285405Sbapt if (rc == -1) 181285405Sbapt warn("User '%s' not found in NIS passwd", pwd->pw_name); 182285984Sbapt else if (rc != 0) 183285405Sbapt warn("NIS passwd update"); 184285405Sbapt /* NOTE: NIS-only update errors are not fatal */ 185285405Sbapt } 186285405Sbapt} 187285405Sbapt 188285405Sbapt/* 189285405Sbapt * The M_LOCK and M_UNLOCK functions simply add or remove 190285405Sbapt * a "*LOCKED*" prefix from in front of the password to 191285405Sbapt * prevent it decoding correctly, and therefore prevents 192285405Sbapt * access. Of course, this only prevents access via 193285405Sbapt * password authentication (not ssh, kerberos or any 194285405Sbapt * other method that does not use the UNIX password) but 195285405Sbapt * that is a known limitation. 196285405Sbapt */ 197285405Sbaptstatic int 198286196Sbaptpw_userlock(char *arg1, int mode) 199285405Sbapt{ 200285405Sbapt struct passwd *pwd = NULL; 201285405Sbapt char *passtmp = NULL; 202286196Sbapt char *name; 203285405Sbapt bool locked = false; 204286196Sbapt uid_t id; 205285405Sbapt 206286196Sbapt if (geteuid() != 0) 207286196Sbapt errx(EX_NOPERM, "you must be root"); 208286196Sbapt 209286196Sbapt if (arg1 == NULL) 210285405Sbapt errx(EX_DATAERR, "username or id required"); 211285405Sbapt 212286196Sbapt if (strspn(arg1, "0123456789") == strlen(arg1)) 213286196Sbapt id = pw_checkid(arg1, UID_MAX); 214286196Sbapt else 215286196Sbapt name = arg1; 216286196Sbapt 217285405Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 218285405Sbapt if (pwd == NULL) { 219285405Sbapt if (name == NULL) 220286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", (uintmax_t) id); 221285405Sbapt errx(EX_NOUSER, "no such user `%s'", name); 222285405Sbapt } 223285405Sbapt 224285405Sbapt if (name == NULL) 225285405Sbapt name = pwd->pw_name; 226285405Sbapt 227285405Sbapt if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str) -1) == 0) 228285405Sbapt locked = true; 229285405Sbapt if (mode == M_LOCK && locked) 230285405Sbapt errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 231285405Sbapt if (mode == M_UNLOCK && !locked) 232285405Sbapt errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 233285405Sbapt 234285405Sbapt if (mode == M_LOCK) { 235285405Sbapt asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 236285405Sbapt if (passtmp == NULL) /* disaster */ 237285405Sbapt errx(EX_UNAVAILABLE, "out of memory"); 238285405Sbapt pwd->pw_passwd = passtmp; 239285405Sbapt } else { 240285405Sbapt pwd->pw_passwd += sizeof(locked_str)-1; 241285405Sbapt } 242285405Sbapt 243286196Sbapt perform_chgpwent(name, pwd, NULL); 244285405Sbapt free(passtmp); 245285405Sbapt 246285405Sbapt return (EXIT_SUCCESS); 247285405Sbapt} 248285405Sbapt 249286196Sbaptstatic uid_t 250286196Sbaptpw_uidpolicy(struct userconf * cnf, intmax_t id) 25120253Sjoerg{ 252286196Sbapt struct passwd *pwd; 253286196Sbapt struct bitmap bm; 254286196Sbapt uid_t uid = (uid_t) - 1; 25520253Sjoerg 25620267Sjoerg /* 257286196Sbapt * Check the given uid, if any 25820253Sjoerg */ 259286196Sbapt if (id >= 0) { 260286196Sbapt uid = (uid_t) id; 26120253Sjoerg 262286196Sbapt if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate) 263286196Sbapt errx(EX_DATAERR, "uid `%ju' has already been allocated", 264286196Sbapt (uintmax_t)pwd->pw_uid); 265286196Sbapt return (uid); 26620253Sjoerg } 26721052Sdavidn /* 268286196Sbapt * We need to allocate the next available uid under one of 269286196Sbapt * two policies a) Grab the first unused uid b) Grab the 270286196Sbapt * highest possible unused uid 27121052Sdavidn */ 272286196Sbapt if (cnf->min_uid >= cnf->max_uid) { /* Sanity 273286196Sbapt * claus^H^H^H^Hheck */ 274286196Sbapt cnf->min_uid = 1000; 275286196Sbapt cnf->max_uid = 32000; 27621052Sdavidn } 277286196Sbapt bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 27821052Sdavidn 27920253Sjoerg /* 280286196Sbapt * Now, let's fill the bitmap from the password file 28120253Sjoerg */ 282286196Sbapt SETPWENT(); 283286196Sbapt while ((pwd = GETPWENT()) != NULL) 284286196Sbapt if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 285286196Sbapt bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 286286196Sbapt ENDPWENT(); 28752527Sdavidn 28820253Sjoerg /* 289286196Sbapt * Then apply the policy, with fallback to reuse if necessary 29020253Sjoerg */ 291286196Sbapt if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 292286196Sbapt uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 29320253Sjoerg 29420267Sjoerg /* 295286196Sbapt * Another sanity check 29620267Sjoerg */ 297286196Sbapt if (uid < cnf->min_uid || uid > cnf->max_uid) 298286196Sbapt errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 299286196Sbapt bm_dealloc(&bm); 300286196Sbapt return (uid); 30120253Sjoerg} 30220253Sjoerg 303286196Sbaptstatic uid_t 304286196Sbaptpw_gidpolicy(struct userconf *cnf, char *grname, char *nam, gid_t prefer, bool dryrun) 30520253Sjoerg{ 30620253Sjoerg struct group *grp; 30720253Sjoerg gid_t gid = (uid_t) - 1; 30820253Sjoerg 30920253Sjoerg /* 31020253Sjoerg * Check the given gid, if any 31120253Sjoerg */ 31244229Sdavidn SETGRENT(); 313286196Sbapt if (grname) { 314286196Sbapt if ((grp = GETGRNAM(grname)) == NULL) { 315286196Sbapt gid = pw_checkid(grname, GID_MAX); 316286196Sbapt grp = GETGRGID(gid); 31720253Sjoerg } 31820253Sjoerg gid = grp->gr_gid; 319262865Sjulian } else if ((grp = GETGRNAM(nam)) != NULL && 320262865Sjulian (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 32120267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 32220253Sjoerg } else { 323286196Sbapt intmax_t grid = -1; 32420253Sjoerg 32520253Sjoerg /* 32620253Sjoerg * We need to auto-create a group with the user's name. We 32720253Sjoerg * can send all the appropriate output to our sister routine 32820253Sjoerg * bit first see if we can create a group with gid==uid so we 32920253Sjoerg * can keep the user and group ids in sync. We purposely do 33020253Sjoerg * NOT check the gid range if we can force the sync. If the 33120253Sjoerg * user's name dups an existing group, then the group add 33220253Sjoerg * function will happily handle that case for us and exit. 33320253Sjoerg */ 334285414Sbapt if (GETGRGID(prefer) == NULL) 335285414Sbapt grid = prefer; 336286196Sbapt if (dryrun) { 337285395Sbapt gid = pw_groupnext(cnf, true); 338285395Sbapt } else { 339286196Sbapt if (grid == -1) 340286196Sbapt grid = pw_groupnext(cnf, true); 341286196Sbapt groupadd(cnf, nam, grid, NULL, -1, false, false, false); 34244229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 34320267Sjoerg gid = grp->gr_gid; 34420267Sjoerg } 34520253Sjoerg } 34644229Sdavidn ENDGRENT(); 347286196Sbapt return (gid); 34820253Sjoerg} 34920253Sjoerg 350286196Sbaptstatic char * 351286196Sbaptpw_homepolicy(struct userconf * cnf, char *homedir, const char *user) 35220253Sjoerg{ 353282699Sbapt static char home[128]; 35420253Sjoerg 355286196Sbapt if (homedir) 356286196Sbapt return (homedir); 35720253Sjoerg 358282699Sbapt if (cnf->home == NULL || *cnf->home == '\0') 359282699Sbapt errx(EX_CONFIG, "no base home directory set"); 360282699Sbapt snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 361282699Sbapt 362282699Sbapt return (home); 36320253Sjoerg} 36420253Sjoerg 365286196Sbaptstatic char * 36620253Sjoergshell_path(char const * path, char *shells[], char *sh) 36720253Sjoerg{ 36820253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 36920253Sjoerg return sh; /* specified full path or forced none */ 37020253Sjoerg else { 37120253Sjoerg char *p; 37220253Sjoerg char paths[_UC_MAXLINE]; 37320253Sjoerg 37420253Sjoerg /* 37520253Sjoerg * We need to search paths 37620253Sjoerg */ 377130633Srobert strlcpy(paths, path, sizeof(paths)); 37820253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 37920253Sjoerg int i; 38020253Sjoerg static char shellpath[256]; 38120253Sjoerg 38220253Sjoerg if (sh != NULL) { 383282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); 38420253Sjoerg if (access(shellpath, X_OK) == 0) 38520253Sjoerg return shellpath; 38620253Sjoerg } else 38720253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 388282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); 38920253Sjoerg if (access(shellpath, X_OK) == 0) 39020253Sjoerg return shellpath; 39120253Sjoerg } 39220253Sjoerg } 39320253Sjoerg if (sh == NULL) 39430259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 39530259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 39620253Sjoerg return NULL; 39720253Sjoerg } 39820253Sjoerg} 39920253Sjoerg 400286196Sbaptstatic char * 401286196Sbaptpw_shellpolicy(struct userconf * cnf) 40220253Sjoerg{ 40320253Sjoerg 404286196Sbapt return shell_path(cnf->shelldir, cnf->shells, cnf->shell_default); 40520253Sjoerg} 40620253Sjoerg 407179365Santoine#define SALTSIZE 32 40820253Sjoerg 409179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 410179365Santoine 411286196Sbaptchar * 41220253Sjoergpw_pwcrypt(char *password) 41320253Sjoerg{ 41420253Sjoerg int i; 415179365Santoine char salt[SALTSIZE + 1]; 416231994Skevlo char *cryptpw; 41720253Sjoerg static char buf[256]; 41820253Sjoerg 41920253Sjoerg /* 42020253Sjoerg * Calculate a salt value 42120253Sjoerg */ 422179365Santoine for (i = 0; i < SALTSIZE; i++) 423181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 424179365Santoine salt[SALTSIZE] = '\0'; 42520253Sjoerg 426231994Skevlo cryptpw = crypt(password, salt); 427231994Skevlo if (cryptpw == NULL) 428231994Skevlo errx(EX_CONFIG, "crypt(3) failure"); 429231994Skevlo return strcpy(buf, cryptpw); 43020253Sjoerg} 43120253Sjoerg 432286196Sbaptstatic char * 433286196Sbaptpw_password(struct userconf * cnf, char const * user, bool dryrun) 43420253Sjoerg{ 43520253Sjoerg int i, l; 43620253Sjoerg char pwbuf[32]; 43720253Sjoerg 43820253Sjoerg switch (cnf->default_password) { 43920253Sjoerg case -1: /* Random password */ 44073563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 44120253Sjoerg for (i = 0; i < l; i++) 442181785Sache pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 44320253Sjoerg pwbuf[i] = '\0'; 44420253Sjoerg 44520253Sjoerg /* 44620253Sjoerg * We give this information back to the user 44720253Sjoerg */ 448286196Sbapt if (conf.fd == -1 && !dryrun) { 44961957Sache if (isatty(STDOUT_FILENO)) 45020712Sdavidn printf("Password for '%s' is: ", user); 45120253Sjoerg printf("%s\n", pwbuf); 45220253Sjoerg fflush(stdout); 45320253Sjoerg } 45420253Sjoerg break; 45520253Sjoerg 45620253Sjoerg case -2: /* No password at all! */ 45720253Sjoerg return ""; 45820253Sjoerg 45920253Sjoerg case 0: /* No login - default */ 46020253Sjoerg default: 46120253Sjoerg return "*"; 46220253Sjoerg 46320253Sjoerg case 1: /* user's name */ 464130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 46520253Sjoerg break; 46620253Sjoerg } 46720253Sjoerg return pw_pwcrypt(pwbuf); 46820253Sjoerg} 46920253Sjoerg 470284111Sbaptstatic int 471286196Sbaptprint_user(struct passwd * pwd, bool pretty, bool v7) 472284111Sbapt{ 473286196Sbapt int j; 474286196Sbapt char *p; 475286196Sbapt struct group *grp = GETGRGID(pwd->pw_gid); 476286196Sbapt char uname[60] = "User &", office[60] = "[None]", 477286196Sbapt wphone[60] = "[None]", hphone[60] = "[None]"; 478286196Sbapt char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 479286196Sbapt struct tm * tptr; 480286196Sbapt 481286196Sbapt if (!pretty) { 482286196Sbapt p = v7 ? pw_make_v7(pwd) : pw_make(pwd); 483286196Sbapt printf("%s\n", p); 484286196Sbapt free(p); 485286196Sbapt return (EXIT_SUCCESS); 486286196Sbapt } 487286196Sbapt 488286196Sbapt if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 489286196Sbapt strlcpy(uname, p, sizeof(uname)); 490286196Sbapt if ((p = strtok(NULL, ",")) != NULL) { 491286196Sbapt strlcpy(office, p, sizeof(office)); 492286196Sbapt if ((p = strtok(NULL, ",")) != NULL) { 493286196Sbapt strlcpy(wphone, p, sizeof(wphone)); 494286196Sbapt if ((p = strtok(NULL, "")) != NULL) { 495286196Sbapt strlcpy(hphone, p, sizeof(hphone)); 496286196Sbapt } 497286196Sbapt } 498286196Sbapt } 499286196Sbapt } 500286196Sbapt /* 501286196Sbapt * Handle '&' in gecos field 502286196Sbapt */ 503286196Sbapt if ((p = strchr(uname, '&')) != NULL) { 504286196Sbapt int l = strlen(pwd->pw_name); 505286196Sbapt int m = strlen(p); 506286196Sbapt 507286196Sbapt memmove(p + l, p + 1, m); 508286196Sbapt memmove(p, pwd->pw_name, l); 509286196Sbapt *p = (char) toupper((unsigned char)*p); 510286196Sbapt } 511286196Sbapt if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 512286196Sbapt strftime(acexpire, sizeof acexpire, "%c", tptr); 513286196Sbapt if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 514286196Sbapt strftime(pwexpire, sizeof pwexpire, "%c", tptr); 515286196Sbapt printf("Login Name: %-15s #%-12ju Group: %-15s #%ju\n" 516286196Sbapt " Full Name: %s\n" 517286196Sbapt " Home: %-26.26s Class: %s\n" 518286196Sbapt " Shell: %-26.26s Office: %s\n" 519286196Sbapt "Work Phone: %-26.26s Home Phone: %s\n" 520286196Sbapt "Acc Expire: %-26.26s Pwd Expire: %s\n", 521286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 522286196Sbapt grp ? grp->gr_name : "(invalid)", (uintmax_t)pwd->pw_gid, 523286196Sbapt uname, pwd->pw_dir, pwd->pw_class, 524286196Sbapt pwd->pw_shell, office, wphone, hphone, 525286196Sbapt acexpire, pwexpire); 526286196Sbapt SETGRENT(); 527286196Sbapt j = 0; 528286196Sbapt while ((grp=GETGRENT()) != NULL) { 529286196Sbapt int i = 0; 530286196Sbapt if (grp->gr_mem != NULL) { 531286196Sbapt while (grp->gr_mem[i] != NULL) { 532286196Sbapt if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) { 533286196Sbapt printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 534286196Sbapt break; 535286196Sbapt } 536286196Sbapt ++i; 537286196Sbapt } 538286196Sbapt } 539286196Sbapt } 540286196Sbapt ENDGRENT(); 541286196Sbapt printf("%s", j ? "\n" : ""); 542286196Sbapt return (EXIT_SUCCESS); 543286196Sbapt} 544286196Sbapt 545286196Sbaptchar * 546286196Sbaptpw_checkname(char *name, int gecos) 547286196Sbapt{ 548286196Sbapt char showch[8]; 549286196Sbapt const char *badchars, *ch, *showtype; 550286196Sbapt int reject; 551286196Sbapt 552286196Sbapt ch = name; 553286196Sbapt reject = 0; 554286196Sbapt if (gecos) { 555286196Sbapt /* See if the name is valid as a gecos (comment) field. */ 556286196Sbapt badchars = ":!@"; 557286196Sbapt showtype = "gecos field"; 558286196Sbapt } else { 559286196Sbapt /* See if the name is valid as a userid or group. */ 560286196Sbapt badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 561286196Sbapt showtype = "userid/group name"; 562286196Sbapt /* Userids and groups can not have a leading '-'. */ 563286196Sbapt if (*ch == '-') 564286196Sbapt reject = 1; 565286196Sbapt } 566286196Sbapt if (!reject) { 567286196Sbapt while (*ch) { 568286196Sbapt if (strchr(badchars, *ch) != NULL || *ch < ' ' || 569286196Sbapt *ch == 127) { 570286196Sbapt reject = 1; 571286196Sbapt break; 572286196Sbapt } 573286196Sbapt /* 8-bit characters are only allowed in GECOS fields */ 574286196Sbapt if (!gecos && (*ch & 0x80)) { 575286196Sbapt reject = 1; 576286196Sbapt break; 577286196Sbapt } 578286196Sbapt ch++; 579286196Sbapt } 580286196Sbapt } 581286196Sbapt /* 582286196Sbapt * A `$' is allowed as the final character for userids and groups, 583286196Sbapt * mainly for the benefit of samba. 584286196Sbapt */ 585286196Sbapt if (reject && !gecos) { 586286196Sbapt if (*ch == '$' && *(ch + 1) == '\0') { 587286196Sbapt reject = 0; 588286196Sbapt ch++; 589286196Sbapt } 590286196Sbapt } 591286196Sbapt if (reject) { 592286196Sbapt snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 593286196Sbapt ? "`%c'" : "0x%02x", *ch); 594286196Sbapt errx(EX_DATAERR, "invalid character %s at position %td in %s", 595286196Sbapt showch, (ch - name), showtype); 596286196Sbapt } 597286196Sbapt if (!gecos && (ch - name) > LOGNAMESIZE) 598286196Sbapt errx(EX_USAGE, "name too long `%s' (max is %d)", name, 599286196Sbapt LOGNAMESIZE); 600286196Sbapt 601286196Sbapt return (name); 602286196Sbapt} 603286196Sbapt 604286196Sbaptstatic void 605286196Sbaptrmat(uid_t uid) 606286196Sbapt{ 607286196Sbapt DIR *d = opendir("/var/at/jobs"); 608286196Sbapt 609286196Sbapt if (d != NULL) { 610286196Sbapt struct dirent *e; 611286196Sbapt 612286196Sbapt while ((e = readdir(d)) != NULL) { 613286196Sbapt struct stat st; 614286196Sbapt 615286196Sbapt if (strncmp(e->d_name, ".lock", 5) != 0 && 616286196Sbapt stat(e->d_name, &st) == 0 && 617286196Sbapt !S_ISDIR(st.st_mode) && 618286196Sbapt st.st_uid == uid) { 619286196Sbapt char tmp[MAXPATHLEN]; 620286196Sbapt 621286196Sbapt snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name); 622286196Sbapt system(tmp); 623286196Sbapt } 624286196Sbapt } 625286196Sbapt closedir(d); 626286196Sbapt } 627286196Sbapt} 628286196Sbapt 629286196Sbaptstatic void 630286196Sbaptrmopie(char const * name) 631286196Sbapt{ 632286196Sbapt char tmp[1014]; 633286196Sbapt FILE *fp; 634286196Sbapt int fd; 635286196Sbapt size_t len; 636286196Sbapt off_t atofs = 0; 637286196Sbapt 638286196Sbapt if ((fd = openat(conf.rootfd, "etc/opiekeys", O_RDWR)) == -1) 639286196Sbapt return; 640286196Sbapt 641286196Sbapt fp = fdopen(fd, "r+"); 642286196Sbapt len = strlen(name); 643286196Sbapt 644286196Sbapt while (fgets(tmp, sizeof(tmp), fp) != NULL) { 645286196Sbapt if (strncmp(name, tmp, len) == 0 && tmp[len]==' ') { 646286196Sbapt /* Comment username out */ 647286196Sbapt if (fseek(fp, atofs, SEEK_SET) == 0) 648286196Sbapt fwrite("#", 1, 1, fp); 649286196Sbapt break; 650286196Sbapt } 651286196Sbapt atofs = ftell(fp); 652286196Sbapt } 653286196Sbapt /* 654286196Sbapt * If we got an error of any sort, don't update! 655286196Sbapt */ 656286196Sbapt fclose(fp); 657286196Sbapt} 658286196Sbapt 659286196Sbaptint 660286196Sbaptpw_user_next(int argc, char **argv, char *name __unused) 661286196Sbapt{ 662286196Sbapt struct userconf *cnf = NULL; 663286196Sbapt const char *cfg = NULL; 664286196Sbapt int ch; 665286196Sbapt bool quiet = false; 666286196Sbapt uid_t next; 667286196Sbapt 668286196Sbapt while ((ch = getopt(argc, argv, "Cq")) != -1) { 669286196Sbapt switch (ch) { 670286196Sbapt case 'C': 671286196Sbapt cfg = optarg; 672286196Sbapt break; 673286196Sbapt case 'q': 674286196Sbapt quiet; 675286196Sbapt break; 676286196Sbapt } 677286196Sbapt } 678286196Sbapt 679286196Sbapt if (quiet) 680286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 681286196Sbapt 682286196Sbapt cnf = get_userconfig(cfg); 683286196Sbapt 684286196Sbapt next = pw_uidpolicy(cnf, -1); 685286196Sbapt 686286196Sbapt printf("%ju:", (uintmax_t)next); 687286196Sbapt pw_groupnext(cnf, quiet); 688286196Sbapt 689286196Sbapt return (EXIT_SUCCESS); 690286196Sbapt} 691286196Sbapt 692286196Sbaptint 693286196Sbaptpw_user_show(int argc, char **argv, char *arg1) 694286196Sbapt{ 695285401Sbapt struct passwd *pwd = NULL; 696286196Sbapt char *name = NULL; 697286196Sbapt uid_t id = -1; 698286196Sbapt int ch; 699286196Sbapt bool all = false; 700286196Sbapt bool pretty = false; 701286196Sbapt bool force = false; 702286196Sbapt bool v7 = false; 703286196Sbapt bool quiet = false; 70420253Sjoerg 705286196Sbapt if (arg1 != NULL) { 706286196Sbapt if (strspn(arg1, "0123456789") == strlen(arg1)) 707286196Sbapt id = pw_checkid(arg1, UID_MAX); 708286196Sbapt else 709286196Sbapt name = arg1; 710286196Sbapt } 711286196Sbapt 712286196Sbapt while ((ch = getopt(argc, argv, "C:qn:u:FPa7")) != -1) { 713286196Sbapt switch (ch) { 714286196Sbapt case 'C': 715286196Sbapt /* ignore compatibility */ 716286196Sbapt break; 717286196Sbapt case 'q': 718286196Sbapt quiet = true; 719286196Sbapt break; 720286196Sbapt case 'n': 721286196Sbapt name = optarg; 722286196Sbapt break; 723286196Sbapt case 'u': 724286196Sbapt id = pw_checkid(optarg, UID_MAX); 725286196Sbapt break; 726286196Sbapt case 'F': 727286196Sbapt force = true; 728286196Sbapt break; 729286196Sbapt case 'P': 730286196Sbapt pretty = true; 731286196Sbapt break; 732286196Sbapt case 'a': 733286196Sbapt all = true; 734286196Sbapt break; 735286196Sbapt case 7: 736286196Sbapt v7 = true; 737286196Sbapt break; 738286196Sbapt } 739286196Sbapt } 740286196Sbapt 741286196Sbapt if (quiet) 742286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 743286196Sbapt 744286196Sbapt if (all) { 745286196Sbapt SETPWENT(); 746286196Sbapt while ((pwd = GETPWENT()) != NULL) 747286196Sbapt print_user(pwd, pretty, v7); 748286196Sbapt ENDPWENT(); 749286196Sbapt return (EXIT_SUCCESS); 750286196Sbapt } 751286196Sbapt 752285401Sbapt if (id < 0 && name == NULL) 753285401Sbapt errx(EX_DATAERR, "username or id required"); 754285401Sbapt 755285401Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 756285401Sbapt if (pwd == NULL) { 757286196Sbapt if (force) { 758286196Sbapt pwd = &fakeuser; 759286196Sbapt } else { 760286196Sbapt if (name == NULL) 761286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", 762286196Sbapt (uintmax_t) id); 763286196Sbapt errx(EX_NOUSER, "no such user `%s'", name); 764286196Sbapt } 765286196Sbapt } 766286196Sbapt 767286196Sbapt return (print_user(pwd, pretty, v7)); 768286196Sbapt} 769286196Sbapt 770286196Sbaptint 771286196Sbaptpw_user_del(int argc, char **argv, char *arg1) 772286196Sbapt{ 773286196Sbapt struct userconf *cnf = NULL; 774286196Sbapt struct passwd *pwd = NULL; 775286196Sbapt struct group *gr, *grp; 776286196Sbapt char *name = NULL; 777286196Sbapt char grname[MAXLOGNAME]; 778286196Sbapt char *nispasswd = NULL; 779286196Sbapt char file[MAXPATHLEN]; 780286196Sbapt char home[MAXPATHLEN]; 781286196Sbapt const char *cfg = NULL; 782286196Sbapt struct stat st; 783286196Sbapt uid_t id; 784286196Sbapt int ch, rc; 785286196Sbapt bool nis = false; 786286196Sbapt bool deletehome = false; 787286196Sbapt bool quiet = false; 788286196Sbapt 789286196Sbapt if (arg1 != NULL) { 790286196Sbapt if (strspn(arg1, "0123456789") == strlen(arg1)) 791286196Sbapt id = pw_checkid(arg1, UID_MAX); 792286196Sbapt else 793286196Sbapt name = arg1; 794286196Sbapt } 795286196Sbapt 796286196Sbapt while ((ch = getopt(argc, argv, "C:qn:u:rYy:")) != -1) { 797286196Sbapt switch (ch) { 798286196Sbapt case 'C': 799286196Sbapt cfg = optarg; 800286196Sbapt break; 801286196Sbapt case 'q': 802286196Sbapt quiet = true; 803286196Sbapt break; 804286196Sbapt case 'n': 805286196Sbapt name = optarg; 806286196Sbapt break; 807286196Sbapt case 'u': 808286196Sbapt id = pw_checkid(optarg, UID_MAX); 809286196Sbapt break; 810286196Sbapt case 'r': 811286196Sbapt deletehome = true; 812286196Sbapt break; 813286196Sbapt case 'y': 814286196Sbapt nispasswd = optarg; 815286196Sbapt break; 816286196Sbapt case 'Y': 817286196Sbapt nis = true; 818286196Sbapt break; 819286196Sbapt } 820286196Sbapt } 821286196Sbapt 822286196Sbapt if (quiet) 823286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 824286196Sbapt 825286196Sbapt if (id < 0 && name == NULL) 826286196Sbapt errx(EX_DATAERR, "username or id required"); 827286196Sbapt 828286196Sbapt cnf = get_userconfig(cfg); 829286196Sbapt 830286196Sbapt if (nispasswd == NULL) 831286196Sbapt nispasswd = cnf->nispasswd; 832286196Sbapt 833286196Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 834286196Sbapt if (pwd == NULL) { 835285401Sbapt if (name == NULL) 836286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", (uintmax_t) id); 837285401Sbapt errx(EX_NOUSER, "no such user `%s'", name); 838285401Sbapt } 839285989Sbapt 840285989Sbapt if (PWF._altdir == PWF_REGULAR && 841286196Sbapt ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) { 842286196Sbapt if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 843286196Sbapt if (!nis && nispasswd && *nispasswd != '/') 844286196Sbapt errx(EX_NOUSER, "Cannot remove NIS user `%s'", 845286196Sbapt name); 846286196Sbapt } else { 847286196Sbapt errx(EX_NOUSER, "Cannot remove non local user `%s'", 848286196Sbapt name); 849286196Sbapt } 850286196Sbapt } 851285989Sbapt 852286196Sbapt id = pwd->pw_uid; 853285401Sbapt if (name == NULL) 854285401Sbapt name = pwd->pw_name; 855285401Sbapt 856284111Sbapt if (strcmp(pwd->pw_name, "root") == 0) 857284111Sbapt errx(EX_DATAERR, "cannot remove user 'root'"); 858284111Sbapt 859286196Sbapt /* Remove opie record from /etc/opiekeys */ 860285433Sbapt if (PWALTDIR() != PWF_ALT) 861284111Sbapt rmopie(pwd->pw_name); 862284111Sbapt 863285433Sbapt if (!PWALTDIR()) { 864285433Sbapt /* Remove crontabs */ 865284111Sbapt snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 866284111Sbapt if (access(file, F_OK) == 0) { 867284111Sbapt snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name); 868284111Sbapt system(file); 869284111Sbapt } 870284111Sbapt } 871286196Sbapt 872284111Sbapt /* 873284111Sbapt * Save these for later, since contents of pwd may be 874284111Sbapt * invalidated by deletion 875284111Sbapt */ 876284111Sbapt snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 877284111Sbapt strlcpy(home, pwd->pw_dir, sizeof(home)); 878284111Sbapt gr = GETGRGID(pwd->pw_gid); 879284111Sbapt if (gr != NULL) 880284111Sbapt strlcpy(grname, gr->gr_name, LOGNAMESIZE); 881284111Sbapt else 882284111Sbapt grname[0] = '\0'; 883284111Sbapt 884284111Sbapt rc = delpwent(pwd); 885284111Sbapt if (rc == -1) 886284111Sbapt err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 887284111Sbapt else if (rc != 0) 888284111Sbapt err(EX_IOERR, "passwd update"); 889284111Sbapt 890286196Sbapt if (nis && nispasswd && *nispasswd=='/') { 891286196Sbapt rc = delnispwent(nispasswd, name); 892284111Sbapt if (rc == -1) 893285401Sbapt warnx("WARNING: user '%s' does not exist in NIS passwd", 894285401Sbapt pwd->pw_name); 895284111Sbapt else if (rc != 0) 896284111Sbapt warn("WARNING: NIS passwd update"); 897284111Sbapt } 898284111Sbapt 899284128Sbapt grp = GETGRNAM(name); 900284111Sbapt if (grp != NULL && 901284111Sbapt (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 902284128Sbapt strcmp(name, grname) == 0) 903284128Sbapt delgrent(GETGRNAM(name)); 904284111Sbapt SETGRENT(); 905284111Sbapt while ((grp = GETGRENT()) != NULL) { 906284111Sbapt int i, j; 907284111Sbapt char group[MAXLOGNAME]; 908284113Sbapt if (grp->gr_mem == NULL) 909284113Sbapt continue; 910284113Sbapt 911284113Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 912284128Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 913284113Sbapt continue; 914284113Sbapt 915284113Sbapt for (j = i; grp->gr_mem[j] != NULL; j++) 916284113Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 917284113Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 918284113Sbapt chggrent(group, grp); 919284111Sbapt } 920284111Sbapt } 921284111Sbapt ENDGRENT(); 922284111Sbapt 923286196Sbapt pw_log(cnf, M_DELETE, W_USER, "%s(%ju) account removed", name, 924286196Sbapt (uintmax_t)id); 925284111Sbapt 926285433Sbapt /* Remove mail file */ 927285433Sbapt if (PWALTDIR() != PWF_ALT) 928285433Sbapt unlinkat(conf.rootfd, file + 1, 0); 929284111Sbapt 930286196Sbapt /* Remove at jobs */ 931286196Sbapt if (!PWALTDIR() && getpwuid(id) == NULL) 932286196Sbapt rmat(id); 933284111Sbapt 934285433Sbapt /* Remove home directory and contents */ 935286196Sbapt if (PWALTDIR() != PWF_ALT && deletehome && *home == '/' && 936286196Sbapt GETPWUID(id) == NULL && 937285433Sbapt fstatat(conf.rootfd, home + 1, &st, 0) != -1) { 938286196Sbapt rm_r(conf.rootfd, home, id); 939286196Sbapt pw_log(cnf, M_DELETE, W_USER, "%s(%ju) home '%s' %s" 940286196Sbapt "removed", name, (uintmax_t)id, home, 941285433Sbapt fstatat(conf.rootfd, home + 1, &st, 0) == -1 ? "" : "not " 942285433Sbapt "completely "); 943284111Sbapt } 944284111Sbapt 945284111Sbapt return (EXIT_SUCCESS); 946284111Sbapt} 947284111Sbapt 948286196Sbaptint 949286196Sbaptpw_user_lock(int argc, char **argv, char *arg1) 95020253Sjoerg{ 951286196Sbapt int ch; 95220253Sjoerg 953286196Sbapt while ((ch = getopt(argc, argv, "Cq")) != -1) { 954286196Sbapt switch (ch) { 955286196Sbapt case 'C': 956286196Sbapt case 'q': 957286196Sbapt /* compatibility */ 958286196Sbapt break; 95920253Sjoerg } 960286196Sbapt } 96120253Sjoerg 962286196Sbapt return (pw_userlock(arg1, M_LOCK)); 963286196Sbapt} 964286196Sbapt 965286196Sbaptint 966286196Sbaptpw_user_unlock(int argc, char **argv, char *arg1) 967286196Sbapt{ 968286196Sbapt int ch; 969286196Sbapt 970286196Sbapt while ((ch = getopt(argc, argv, "Cq")) != -1) { 971286196Sbapt switch (ch) { 972286196Sbapt case 'C': 973286196Sbapt case 'q': 974286196Sbapt /* compatibility */ 975286196Sbapt break; 97620253Sjoerg } 97720253Sjoerg } 978286196Sbapt 979286196Sbapt return (pw_userlock(arg1, M_UNLOCK)); 98020253Sjoerg} 98120253Sjoerg 982286196Sbaptstatic struct group * 983286196Sbaptgroup_from_name_or_id(char *name) 98420253Sjoerg{ 985286196Sbapt const char *errstr = NULL; 986286196Sbapt struct group *grp; 987286196Sbapt uintmax_t id; 98820253Sjoerg 989286196Sbapt if ((grp = GETGRNAM(name)) == NULL) { 990286196Sbapt id = strtounum(name, 0, GID_MAX, &errstr); 991286196Sbapt if (errstr) 992286196Sbapt errx(EX_NOUSER, "group `%s' does not exist", name); 993286196Sbapt grp = GETGRGID(id); 994286196Sbapt if (grp == NULL) 995286196Sbapt errx(EX_NOUSER, "group `%s' does not exist", name); 99620253Sjoerg } 997286196Sbapt 998286196Sbapt return (grp); 999286196Sbapt} 1000286196Sbapt 1001286196Sbaptstatic void 1002286196Sbaptsplit_groups(StringList **groups, char *groupsstr) 1003286196Sbapt{ 1004286196Sbapt struct group *grp; 1005286196Sbapt char *p; 1006286196Sbapt char tok[] = ", \t"; 1007286196Sbapt 1008286196Sbapt for (p = strtok(groupsstr, tok); p != NULL; p = strtok(NULL, tok)) { 1009286196Sbapt grp = group_from_name_or_id(p); 1010286196Sbapt if (*groups == NULL) 1011286196Sbapt *groups = sl_init(); 1012286196Sbapt sl_add(*groups, newstr(grp->gr_name)); 1013286196Sbapt } 1014286196Sbapt} 1015286196Sbapt 1016286196Sbaptstatic void 1017286196Sbaptvalidate_grname(struct userconf *cnf, char *group) 1018286196Sbapt{ 1019286196Sbapt struct group *grp; 1020286196Sbapt 1021286196Sbapt if (group == NULL || *group == '\0') { 1022286196Sbapt cnf->default_group = ""; 1023286196Sbapt return; 1024286196Sbapt } 1025286196Sbapt grp = group_from_name_or_id(group); 1026286196Sbapt cnf->default_group = newstr(grp->gr_name); 1027286196Sbapt} 1028286196Sbapt 1029286196Sbaptstatic mode_t 1030286196Sbaptvalidate_mode(char *mode) 1031286196Sbapt{ 1032286196Sbapt mode_t m; 1033286196Sbapt void *set; 1034286196Sbapt 1035286196Sbapt if ((set = setmode(mode)) == NULL) 1036286196Sbapt errx(EX_DATAERR, "invalid directory creation mode '%s'", mode); 1037286196Sbapt 1038286196Sbapt m = getmode(set, _DEF_DIRMODE); 1039286196Sbapt free(set); 1040286196Sbapt return (m); 1041286196Sbapt} 1042286196Sbapt 1043286196Sbaptstatic void 1044286196Sbaptmix_config(struct userconf *cmdcnf, struct userconf *cfg) 1045286196Sbapt{ 1046286196Sbapt 1047286196Sbapt if (cmdcnf->default_password == 0) 1048286196Sbapt cmdcnf->default_password = cfg->default_password; 1049286196Sbapt if (cmdcnf->reuse_uids == 0) 1050286196Sbapt cmdcnf->reuse_uids = cfg->reuse_uids; 1051286196Sbapt if (cmdcnf->reuse_gids == 0) 1052286196Sbapt cmdcnf->reuse_gids = cfg->reuse_gids; 1053286196Sbapt if (cmdcnf->nispasswd == NULL) 1054286196Sbapt cmdcnf->nispasswd = cfg->nispasswd; 1055286196Sbapt if (cmdcnf->dotdir == NULL) 1056286196Sbapt cmdcnf->dotdir = cfg->dotdir; 1057286196Sbapt if (cmdcnf->newmail == NULL) 1058286196Sbapt cmdcnf->newmail = cfg->newmail; 1059286196Sbapt if (cmdcnf->logfile == NULL) 1060286196Sbapt cmdcnf->logfile = cfg->logfile; 1061286196Sbapt if (cmdcnf->home == NULL) 1062286196Sbapt cmdcnf->home = cfg->home; 1063286196Sbapt if (cmdcnf->homemode == 0) 1064286196Sbapt cmdcnf->homemode = cfg->homemode; 1065286196Sbapt if (cmdcnf->shelldir == NULL) 1066286196Sbapt cmdcnf->shelldir = cfg->shelldir; 1067286196Sbapt if (cmdcnf->shells == NULL) 1068286196Sbapt cmdcnf->shells = cfg->shells; 1069286196Sbapt if (cmdcnf->shell_default == NULL) 1070286196Sbapt cmdcnf->shell_default = cfg->shell_default; 1071286196Sbapt if (cmdcnf->default_group == NULL) 1072286196Sbapt cmdcnf->default_group = cfg->default_group; 1073286196Sbapt if (cmdcnf->groups == NULL) 1074286196Sbapt cmdcnf->groups = cfg->groups; 1075286196Sbapt if (cmdcnf->default_class == NULL) 1076286196Sbapt cmdcnf->default_class = cfg->default_class; 1077286196Sbapt if (cmdcnf->min_uid == 0) 1078286196Sbapt cmdcnf->min_uid = cfg->min_uid; 1079286196Sbapt if (cmdcnf->max_uid == 0) 1080286196Sbapt cmdcnf->max_uid = cfg->max_uid; 1081286196Sbapt if (cmdcnf->min_gid == 0) 1082286196Sbapt cmdcnf->min_gid = cfg->min_gid; 1083286196Sbapt if (cmdcnf->max_gid == 0) 1084286196Sbapt cmdcnf->max_gid = cfg->max_gid; 1085286196Sbapt if (cmdcnf->expire_days == 0) 1086286196Sbapt cmdcnf->expire_days = cfg->expire_days; 1087286196Sbapt if (cmdcnf->password_days == 0) 1088286196Sbapt cmdcnf->password_days = cfg->password_days; 1089286196Sbapt} 1090286196Sbapt 1091286196Sbaptint 1092286196Sbaptpw_user_add(int argc, char **argv, char *arg1) 1093286196Sbapt{ 1094286196Sbapt struct userconf *cnf, *cmdcnf; 1095286196Sbapt struct passwd *pwd; 1096286196Sbapt struct group *grp; 1097286196Sbapt struct stat st; 1098286196Sbapt char args[] = "C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y"; 1099286196Sbapt char line[_PASSWORD_LEN+1], path[MAXPATHLEN]; 1100286196Sbapt char *gecos, *homedir, *skel, *walk, *userid, *groupid, *grname; 1101286196Sbapt char *default_passwd, *name, *p; 1102286196Sbapt const char *cfg; 1103286196Sbapt login_cap_t *lc; 1104286196Sbapt FILE *pfp, *fp; 1105286196Sbapt intmax_t id = -1; 1106286196Sbapt time_t now; 1107286196Sbapt int rc, ch, fd = -1; 1108286196Sbapt size_t i; 1109286196Sbapt bool dryrun, nis, pretty, quiet, createhome, precrypted, genconf; 1110286196Sbapt 1111286196Sbapt dryrun = nis = pretty = quiet = createhome = precrypted = false; 1112286196Sbapt genconf = false; 1113286196Sbapt gecos = homedir = skel = userid = groupid = default_passwd = NULL; 1114286196Sbapt grname = name = NULL; 1115286196Sbapt 1116286196Sbapt if ((cmdcnf = calloc(1, sizeof(struct userconf))) == NULL) 1117286196Sbapt err(EXIT_FAILURE, "calloc()"); 1118286196Sbapt 1119286196Sbapt if (arg1 != NULL) { 1120286196Sbapt if (strspn(arg1, "0123456789") == strlen(arg1)) 1121286196Sbapt id = pw_checkid(arg1, UID_MAX); 1122286196Sbapt else 1123286196Sbapt name = arg1; 1124286196Sbapt } 1125286196Sbapt 1126286196Sbapt while ((ch = getopt(argc, argv, args)) != -1) { 1127286196Sbapt switch (ch) { 1128286196Sbapt case 'C': 1129286196Sbapt cfg = optarg; 1130286196Sbapt break; 1131286196Sbapt case 'q': 1132286196Sbapt quiet = true; 1133286196Sbapt break; 1134286196Sbapt case 'n': 1135286196Sbapt name = optarg; 1136286196Sbapt break; 1137286196Sbapt case 'u': 1138286196Sbapt userid = optarg; 1139286196Sbapt break; 1140286196Sbapt case 'c': 1141286196Sbapt gecos = pw_checkname(optarg, 1); 1142286196Sbapt break; 1143286196Sbapt case 'd': 1144286196Sbapt homedir = optarg; 1145286196Sbapt break; 1146286196Sbapt case 'e': 1147286196Sbapt now = time(NULL); 1148286196Sbapt cmdcnf->expire_days = parse_date(now, optarg); 1149286196Sbapt break; 1150286196Sbapt case 'p': 1151286196Sbapt now = time(NULL); 1152286196Sbapt cmdcnf->password_days = parse_date(now, optarg); 1153286196Sbapt break; 1154286196Sbapt case 'g': 1155286196Sbapt validate_grname(cmdcnf, optarg); 1156286196Sbapt grname = optarg; 1157286196Sbapt break; 1158286196Sbapt case 'G': 1159286196Sbapt split_groups(&cmdcnf->groups, optarg); 1160286196Sbapt break; 1161286196Sbapt case 'm': 1162286196Sbapt createhome = true; 1163286196Sbapt break; 1164286196Sbapt case 'M': 1165286196Sbapt cmdcnf->homemode = validate_mode(optarg); 1166286196Sbapt break; 1167286196Sbapt case 'k': 1168286196Sbapt walk = skel = optarg; 1169286196Sbapt if (*walk == '/') 1170286196Sbapt walk++; 1171286196Sbapt if (fstatat(conf.rootfd, walk, &st, 0) == -1) 1172286196Sbapt errx(EX_OSFILE, "skeleton `%s' does not " 1173286196Sbapt "exists", skel); 1174286196Sbapt if (!S_ISDIR(st.st_mode)) 1175286196Sbapt errx(EX_OSFILE, "skeleton `%s' is not a " 1176286196Sbapt "directory", skel); 1177286196Sbapt cmdcnf->dotdir = skel; 1178286196Sbapt break; 1179286196Sbapt case 's': 1180286196Sbapt cmdcnf->shell_default = optarg; 1181286196Sbapt break; 1182286196Sbapt case 'o': 1183286196Sbapt conf.checkduplicate = false; 1184286196Sbapt break; 1185286196Sbapt case 'L': 1186286196Sbapt cmdcnf->default_class = pw_checkname(optarg, 0); 1187286196Sbapt break; 1188286196Sbapt case 'i': 1189286196Sbapt groupid = optarg; 1190286196Sbapt break; 1191286196Sbapt case 'w': 1192286196Sbapt default_passwd = optarg; 1193286196Sbapt break; 1194286196Sbapt case 'H': 1195286196Sbapt if (fd != -1) 1196286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1197286196Sbapt "exclusive options"); 1198286196Sbapt fd = pw_checkfd(optarg); 1199286196Sbapt precrypted = true; 1200286196Sbapt if (fd == '-') 1201286196Sbapt errx(EX_USAGE, "-H expects a file descriptor"); 1202286196Sbapt break; 1203286196Sbapt case 'h': 1204286196Sbapt if (fd != -1) 1205286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1206286196Sbapt "exclusive options"); 1207286196Sbapt fd = pw_checkfd(optarg); 1208286196Sbapt break; 1209286196Sbapt case 'D': 1210286196Sbapt genconf = true; 1211286196Sbapt break; 1212286196Sbapt case 'b': 1213286196Sbapt cmdcnf->home = optarg; 1214286196Sbapt break; 1215286196Sbapt case 'N': 1216286196Sbapt dryrun = true; 1217286196Sbapt break; 1218286196Sbapt case 'P': 1219286196Sbapt pretty = true; 1220286196Sbapt break; 1221286196Sbapt case 'y': 1222286196Sbapt cmdcnf->nispasswd = optarg; 1223286196Sbapt break; 1224286196Sbapt case 'Y': 1225286196Sbapt nis = true; 1226286196Sbapt break; 1227109961Sgad } 1228109961Sgad } 1229286196Sbapt 1230286196Sbapt if (geteuid() != 0 && ! dryrun) 1231286196Sbapt errx(EX_NOPERM, "you must be root"); 1232286196Sbapt 1233286196Sbapt if (quiet) 1234286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 1235286196Sbapt 1236286196Sbapt cnf = get_userconfig(cfg); 1237286196Sbapt 1238286196Sbapt mix_config(cmdcnf, cnf); 1239286196Sbapt if (default_passwd) 1240286196Sbapt cmdcnf->default_password = boolean_val(default_passwd, 1241286196Sbapt cnf->default_password); 1242286196Sbapt if (genconf) { 1243286196Sbapt if (name != NULL) 1244286196Sbapt errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 1245286196Sbapt if (userid != NULL) { 1246286196Sbapt if ((p = strtok(userid, ", \t")) != NULL) 1247286196Sbapt cmdcnf->min_uid = pw_checkid(p, UID_MAX); 1248286196Sbapt if (cmdcnf->min_uid == 0) 1249286196Sbapt cmdcnf->min_uid = 1000; 1250286196Sbapt if ((p = strtok(NULL, " ,\t")) != NULL) 1251286196Sbapt cmdcnf->max_uid = pw_checkid(p, UID_MAX); 1252286196Sbapt if (cmdcnf->max_uid == 0) 1253286196Sbapt cmdcnf->max_uid = 32000; 1254109961Sgad } 1255286196Sbapt if (groupid != NULL) { 1256286196Sbapt if ((p = strtok(groupid, ", \t")) != NULL) 1257286196Sbapt cmdcnf->min_gid = pw_checkid(p, GID_MAX); 1258286196Sbapt if (cmdcnf->min_gid == 0) 1259286196Sbapt cmdcnf->min_gid = 1000; 1260286196Sbapt if ((p = strtok(NULL, " ,\t")) != NULL) 1261286196Sbapt cmdcnf->max_gid = pw_checkid(p, GID_MAX); 1262286196Sbapt if (cmdcnf->max_gid == 0) 1263286196Sbapt cmdcnf->max_gid = 32000; 1264286196Sbapt } 1265286196Sbapt if (write_userconfig(cmdcnf, cfg)) 1266286196Sbapt return (EXIT_SUCCESS); 1267286196Sbapt err(EX_IOERR, "config update"); 1268109961Sgad } 1269286196Sbapt 1270286196Sbapt if (userid) 1271286196Sbapt id = pw_checkid(userid, UID_MAX); 1272286196Sbapt if (id < 0 && name == NULL) 1273286196Sbapt errx(EX_DATAERR, "user name or id required"); 1274286196Sbapt 1275286196Sbapt if (name == NULL) 1276286196Sbapt errx(EX_DATAERR, "login name required"); 1277286196Sbapt 1278286196Sbapt pwd = &fakeuser; 1279286196Sbapt pwd->pw_name = name; 1280286196Sbapt pwd->pw_class = cmdcnf->default_class ? cmdcnf->default_class : ""; 1281286196Sbapt pwd->pw_uid = pw_uidpolicy(cmdcnf, id); 1282286196Sbapt pwd->pw_gid = pw_gidpolicy(cnf, grname, pwd->pw_name, 1283286196Sbapt (gid_t) pwd->pw_uid, dryrun); 1284286196Sbapt pwd->pw_change = cmdcnf->password_days; 1285286196Sbapt pwd->pw_expire = cmdcnf->expire_days; 1286286196Sbapt pwd->pw_dir = pw_homepolicy(cmdcnf, homedir, pwd->pw_name); 1287286196Sbapt pwd->pw_shell = pw_shellpolicy(cmdcnf); 1288286196Sbapt lc = login_getpwclass(pwd); 1289286196Sbapt if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 1290286196Sbapt warn("setting crypt(3) format"); 1291286196Sbapt login_close(lc); 1292286196Sbapt pwd->pw_passwd = pw_password(cmdcnf, pwd->pw_name, dryrun); 1293286196Sbapt if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 1294286196Sbapt warnx("WARNING: new account `%s' has a uid of 0 " 1295286196Sbapt "(superuser access!)", pwd->pw_name); 1296286196Sbapt if (gecos) 1297286196Sbapt pwd->pw_gecos = gecos; 1298286196Sbapt 1299286196Sbapt if (fd != -1) 1300286196Sbapt pw_set_passwd(pwd, fd, precrypted, false); 1301286196Sbapt 1302286196Sbapt if (dryrun) 1303286196Sbapt return (print_user(pwd, pretty, false)); 1304286196Sbapt 1305286196Sbapt if ((rc = addpwent(pwd)) != 0) { 1306286196Sbapt if (rc == -1) 1307286196Sbapt errx(EX_IOERR, "user '%s' already exists", 1308286196Sbapt pwd->pw_name); 1309286196Sbapt else if (rc != 0) 1310286196Sbapt err(EX_IOERR, "passwd file update"); 1311109961Sgad } 1312286196Sbapt if (nis && cmdcnf->nispasswd && *cmdcnf->nispasswd == '/') { 1313286196Sbapt printf("%s\n", cmdcnf->nispasswd); 1314286196Sbapt rc = addnispwent(cmdcnf->nispasswd, pwd); 1315286196Sbapt if (rc == -1) 1316286196Sbapt warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 1317286196Sbapt else if (rc != 0) 1318286196Sbapt warn("NIS passwd update"); 1319286196Sbapt /* NOTE: we treat NIS-only update errors as non-fatal */ 1320286196Sbapt } 1321284110Sbapt 1322286196Sbapt if (cmdcnf->groups != NULL) { 1323286196Sbapt for (i = 0; i < cmdcnf->groups->sl_cur; i++) { 1324286196Sbapt grp = GETGRNAM(cmdcnf->groups->sl_str[i]); 1325286196Sbapt grp = gr_add(grp, pwd->pw_name); 1326286196Sbapt /* 1327286196Sbapt * grp can only be NULL in 2 cases: 1328286196Sbapt * - the new member is already a member 1329286196Sbapt * - a problem with memory occurs 1330286196Sbapt * in both cases we want to skip now. 1331286196Sbapt */ 1332286196Sbapt if (grp == NULL) 1333286196Sbapt continue; 1334286196Sbapt chggrent(grp->gr_name, grp); 1335286196Sbapt free(grp); 1336286196Sbapt } 1337286196Sbapt } 133820253Sjoerg 1339286196Sbapt pwd = GETPWNAM(name); 1340286196Sbapt if (pwd == NULL) 1341286196Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 134220253Sjoerg 1343286196Sbapt grp = GETGRGID(pwd->pw_gid); 1344286196Sbapt pw_log(cnf, M_ADD, W_USER, "%s(%ju):%s(%ju):%s:%s:%s", 1345286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 1346286196Sbapt grp ? grp->gr_name : "unknown", (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1), 1347286196Sbapt pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 134820253Sjoerg 1349286196Sbapt /* 1350286196Sbapt * let's touch and chown the user's mail file. This is not 1351286196Sbapt * strictly necessary under BSD with a 0755 maildir but it also 1352286196Sbapt * doesn't hurt anything to create the empty mailfile 1353286196Sbapt */ 1354286196Sbapt if (PWALTDIR() != PWF_ALT) { 1355286196Sbapt snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, 1356286196Sbapt pwd->pw_name); 1357286196Sbapt /* Preserve contents & mtime */ 1358286196Sbapt close(openat(conf.rootfd, path +1, O_RDWR | O_CREAT, 0600)); 1359286196Sbapt fchownat(conf.rootfd, path + 1, pwd->pw_uid, pwd->pw_gid, 1360286196Sbapt AT_SYMLINK_NOFOLLOW); 1361286196Sbapt } 136220253Sjoerg 1363286196Sbapt /* 1364286196Sbapt * Let's create and populate the user's home directory. Note 1365286196Sbapt * that this also `works' for editing users if -m is used, but 1366286196Sbapt * existing files will *not* be overwritten. 1367286196Sbapt */ 1368286196Sbapt if (PWALTDIR() != PWF_ALT && createhome && pwd->pw_dir && 1369286196Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) 1370286196Sbapt create_and_populate_homedir(cmdcnf, pwd, cmdcnf->dotdir, 1371286196Sbapt cmdcnf->homemode, false); 137220253Sjoerg 1373286196Sbapt if (!PWALTDIR() && cmdcnf->newmail && *cmdcnf->newmail && 1374286196Sbapt (fp = fopen(cnf->newmail, "r")) != NULL) { 1375286196Sbapt if ((pfp = popen(_PATH_SENDMAIL " -t", "w")) == NULL) 1376286196Sbapt warn("sendmail"); 1377286196Sbapt else { 1378286196Sbapt fprintf(pfp, "From: root\n" "To: %s\n" 1379286196Sbapt "Subject: Welcome!\n\n", pwd->pw_name); 1380286196Sbapt while (fgets(line, sizeof(line), fp) != NULL) { 1381286196Sbapt /* Do substitutions? */ 1382286196Sbapt fputs(line, pfp); 138320253Sjoerg } 1384286196Sbapt pclose(pfp); 1385286196Sbapt pw_log(cnf, M_ADD, W_USER, "%s(%ju) new user mail sent", 1386286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid); 138720253Sjoerg } 1388286196Sbapt fclose(fp); 138920253Sjoerg } 1390286196Sbapt 1391286196Sbapt if (nis && nis_update() == 0) 1392286196Sbapt pw_log(cnf, M_ADD, W_USER, "NIS maps updated"); 1393286196Sbapt 1394286196Sbapt return (EXIT_SUCCESS); 139520253Sjoerg} 139620747Sdavidn 1397286196Sbaptint 1398286196Sbaptpw_user_mod(int argc, char **argv, char *arg1) 139920747Sdavidn{ 1400286196Sbapt struct userconf *cnf; 1401286196Sbapt struct passwd *pwd; 1402286196Sbapt struct group *grp; 1403286196Sbapt StringList *groups = NULL; 1404286196Sbapt char args[] = "C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:NPYy:"; 1405286196Sbapt const char *cfg; 1406286196Sbapt char *gecos, *homedir, *grname, *name, *newname, *walk, *skel, *shell; 1407286196Sbapt char *passwd, *class, *nispasswd; 1408286196Sbapt login_cap_t *lc; 1409286196Sbapt struct stat st; 1410286196Sbapt intmax_t id = -1; 1411286196Sbapt int ch, fd = -1; 1412286196Sbapt size_t i, j; 1413286196Sbapt bool quiet, createhome, pretty, dryrun, nis, edited, docreatehome; 1414286196Sbapt mode_t homemode = 0; 1415286196Sbapt time_t expire_days, password_days, now, precrypted; 141620747Sdavidn 1417286196Sbapt expire_days = password_days = -1; 1418286196Sbapt gecos = homedir = grname = name = newname = skel = shell =NULL; 1419286196Sbapt passwd = NULL; 1420286196Sbapt class = nispasswd = NULL; 1421286196Sbapt quiet = createhome = pretty = dryrun = nis = precrypted = false; 1422286196Sbapt edited = docreatehome = false; 142320747Sdavidn 1424286196Sbapt if (arg1 != NULL) { 1425286196Sbapt if (strspn(arg1, "0123456789") == strlen(arg1)) 1426286196Sbapt id = pw_checkid(arg1, UID_MAX); 1427286196Sbapt else 1428286196Sbapt name = arg1; 1429286196Sbapt } 1430286196Sbapt 1431286196Sbapt while ((ch = getopt(argc, argv, args)) != -1) { 1432286196Sbapt switch (ch) { 1433286196Sbapt case 'C': 1434286196Sbapt cfg = optarg; 1435285433Sbapt break; 1436286196Sbapt case 'q': 1437286196Sbapt quiet = true; 1438286196Sbapt break; 1439286196Sbapt case 'n': 1440286196Sbapt name = optarg; 1441286196Sbapt break; 1442286196Sbapt case 'u': 1443286196Sbapt id = pw_checkid(optarg, UID_MAX); 1444286196Sbapt break; 1445286196Sbapt case 'c': 1446286196Sbapt gecos = pw_checkname(optarg, 1); 1447286196Sbapt break; 1448286196Sbapt case 'd': 1449286196Sbapt homedir = optarg; 1450286196Sbapt break; 1451286196Sbapt case 'e': 1452286196Sbapt now = time(NULL); 1453286196Sbapt expire_days = parse_date(now, optarg); 1454286196Sbapt break; 1455286196Sbapt case 'p': 1456286196Sbapt now = time(NULL); 1457286196Sbapt password_days = parse_date(now, optarg); 1458286196Sbapt break; 1459286196Sbapt case 'g': 1460286196Sbapt group_from_name_or_id(optarg); 1461286196Sbapt grname = optarg; 1462286196Sbapt break; 1463286196Sbapt case 'G': 1464286196Sbapt split_groups(&groups, optarg); 1465286196Sbapt break; 1466286196Sbapt case 'm': 1467286196Sbapt createhome = true; 1468286196Sbapt break; 1469286196Sbapt case 'M': 1470286196Sbapt homemode = validate_mode(optarg); 1471286196Sbapt break; 1472286196Sbapt case 'l': 1473286196Sbapt newname = optarg; 1474286196Sbapt break; 1475286196Sbapt case 'k': 1476286196Sbapt walk = skel = optarg; 1477286196Sbapt if (*walk == '/') 1478286196Sbapt walk++; 1479286196Sbapt if (fstatat(conf.rootfd, walk, &st, 0) == -1) 1480286196Sbapt errx(EX_OSFILE, "skeleton `%s' does not " 1481286196Sbapt "exists", skel); 1482286196Sbapt if (!S_ISDIR(st.st_mode)) 1483286196Sbapt errx(EX_OSFILE, "skeleton `%s' is not a " 1484286196Sbapt "directory", skel); 1485286196Sbapt break; 1486286196Sbapt case 's': 1487286196Sbapt shell = optarg; 1488286196Sbapt break; 1489286196Sbapt case 'w': 1490286196Sbapt passwd = optarg; 1491286196Sbapt break; 1492286196Sbapt case 'L': 1493286196Sbapt class = pw_checkname(optarg, 0); 1494286196Sbapt break; 1495286196Sbapt case 'H': 1496286196Sbapt if (fd != -1) 1497286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1498286196Sbapt "exclusive options"); 1499286196Sbapt fd = pw_checkfd(optarg); 1500286196Sbapt precrypted = true; 1501286196Sbapt if (fd == '-') 1502286196Sbapt errx(EX_USAGE, "-H expects a file descriptor"); 1503286196Sbapt break; 1504286196Sbapt case 'h': 1505286196Sbapt if (fd != -1) 1506286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1507286196Sbapt "exclusive options"); 1508286196Sbapt fd = pw_checkfd(optarg); 1509286196Sbapt break; 1510286196Sbapt case 'N': 1511286196Sbapt dryrun = true; 1512286196Sbapt break; 1513286196Sbapt case 'P': 1514286196Sbapt pretty = true; 1515286196Sbapt break; 1516286196Sbapt case 'y': 1517286196Sbapt nispasswd = optarg; 1518286196Sbapt break; 1519286196Sbapt case 'Y': 1520286196Sbapt nis = true; 1521286196Sbapt break; 152220747Sdavidn } 152320747Sdavidn } 1524286196Sbapt 1525286196Sbapt if (geteuid() != 0 && ! dryrun) 1526286196Sbapt errx(EX_NOPERM, "you must be root"); 1527286196Sbapt 1528286196Sbapt if (quiet) 1529286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 1530286196Sbapt 1531286196Sbapt cnf = get_userconfig(cfg); 1532286196Sbapt 1533286196Sbapt if (id < 0 && name == NULL) 1534286196Sbapt errx(EX_DATAERR, "username or id required"); 1535286196Sbapt 1536286196Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 1537286196Sbapt if (pwd == NULL) { 1538286196Sbapt if (name == NULL) 1539286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", 1540286196Sbapt (uintmax_t) id); 1541286196Sbapt errx(EX_NOUSER, "no such user `%s'", name); 1542286196Sbapt } 1543286196Sbapt 1544286196Sbapt if (name == NULL) 1545286196Sbapt name = pwd->pw_name; 1546286196Sbapt 1547286196Sbapt if (nis && nispasswd == NULL) 1548286196Sbapt nispasswd = cnf->nispasswd; 1549286196Sbapt 1550286196Sbapt if (PWF._altdir == PWF_REGULAR && 1551286196Sbapt ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) { 1552286196Sbapt if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 1553286196Sbapt if (!nis && nispasswd && *nispasswd != '/') 1554286196Sbapt errx(EX_NOUSER, "Cannot modify NIS user `%s'", 1555286196Sbapt name); 1556286196Sbapt } else { 1557286196Sbapt errx(EX_NOUSER, "Cannot modify non local user `%s'", 1558286196Sbapt name); 1559286196Sbapt } 1560286196Sbapt } 1561286196Sbapt 1562286196Sbapt if (newname) { 1563286196Sbapt if (strcmp(pwd->pw_name, "root") == 0) 1564286196Sbapt errx(EX_DATAERR, "can't rename `root' account"); 1565286196Sbapt if (strcmp(pwd->pw_name, newname) != 0) { 1566286196Sbapt pwd->pw_name = pw_checkname(newname, 0); 1567286196Sbapt edited = true; 1568286196Sbapt } 1569286196Sbapt } 1570286196Sbapt 1571286196Sbapt if (id > 0 && pwd->pw_uid != id) { 1572286196Sbapt pwd->pw_uid = id; 1573286196Sbapt edited = true; 1574286196Sbapt if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 1575286196Sbapt errx(EX_DATAERR, "can't change uid of `root' account"); 1576286196Sbapt if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 1577286196Sbapt warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 1578286196Sbapt } 1579286196Sbapt 1580286196Sbapt if (grname && pwd->pw_uid != 0) { 1581286196Sbapt grp = GETGRNAM(grname); 1582286196Sbapt if (grp == NULL) 1583286196Sbapt grp = GETGRGID(pw_checkid(grname, GID_MAX)); 1584286196Sbapt if (grp->gr_gid != pwd->pw_gid) { 1585286196Sbapt pwd->pw_gid = grp->gr_gid; 1586286196Sbapt edited = true; 1587286196Sbapt } 1588286196Sbapt } 1589286196Sbapt 1590286196Sbapt if (password_days >= 0 && pwd->pw_change != password_days) { 1591286196Sbapt pwd->pw_change = password_days; 1592286196Sbapt edited = true; 1593286196Sbapt } 1594286196Sbapt 1595286196Sbapt if (expire_days >= 0 && pwd->pw_expire != expire_days) { 1596286196Sbapt pwd->pw_expire = expire_days; 1597286196Sbapt edited = true; 1598286196Sbapt } 1599286196Sbapt 1600286196Sbapt if (shell) { 1601286196Sbapt shell = shell_path(cnf->shelldir, cnf->shells, shell); 1602286196Sbapt if (shell == NULL) 1603286196Sbapt shell = ""; 1604286196Sbapt if (strcmp(shell, pwd->pw_shell) != 0) { 1605286196Sbapt pwd->pw_shell = shell; 1606286196Sbapt edited = true; 1607286196Sbapt } 1608286196Sbapt } 1609286196Sbapt 1610286196Sbapt if (class && strcmp(pwd->pw_class, class) != 0) { 1611286196Sbapt pwd->pw_class = class; 1612286196Sbapt edited = true; 1613286196Sbapt } 1614286196Sbapt 1615286196Sbapt if (homedir && strcmp(pwd->pw_dir, homedir) != 0) { 1616286196Sbapt pwd->pw_dir = homedir; 1617286196Sbapt if (fstatat(conf.rootfd, pwd->pw_dir, &st, 0) == -1) { 1618286196Sbapt if (!createhome) 1619286196Sbapt warnx("WARNING: home `%s' does not exist", 1620286196Sbapt pwd->pw_dir); 1621286196Sbapt else 1622286196Sbapt docreatehome = true; 1623286196Sbapt } else if (!S_ISDIR(st.st_mode)) { 1624286196Sbapt warnx("WARNING: home `%s' is not a directory", 1625286196Sbapt pwd->pw_dir); 1626286196Sbapt } 1627286196Sbapt } 1628286196Sbapt 1629286196Sbapt if (passwd && conf.fd == -1) { 1630286196Sbapt lc = login_getpwclass(pwd); 1631286196Sbapt if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 1632286196Sbapt warn("setting crypt(3) format"); 1633286196Sbapt login_close(lc); 1634286196Sbapt pwd->pw_passwd = pw_password(cnf, pwd->pw_name, dryrun); 1635286196Sbapt edited = true; 1636286196Sbapt } 1637286196Sbapt 1638286196Sbapt if (gecos && strcmp(pwd->pw_gecos, gecos) != 0) { 1639286196Sbapt pwd->pw_gecos = gecos; 1640286196Sbapt edited = true; 1641286196Sbapt } 1642286196Sbapt 1643286196Sbapt if (fd != -1) 1644286196Sbapt edited = pw_set_passwd(pwd, fd, precrypted, true); 1645286196Sbapt 1646286196Sbapt if (dryrun) 1647286196Sbapt return (print_user(pwd, pretty, false)); 1648286196Sbapt 1649286196Sbapt if (edited) /* Only updated this if required */ 1650286196Sbapt perform_chgpwent(name, pwd, nis ? nispasswd : NULL); 1651286196Sbapt /* Now perform the needed changes concern groups */ 1652286196Sbapt if (groups != NULL) { 1653286196Sbapt /* Delete User from groups using old name */ 1654286196Sbapt SETGRENT(); 1655286196Sbapt while ((grp = GETGRENT()) != NULL) { 1656286196Sbapt if (grp->gr_mem == NULL) 1657286196Sbapt continue; 1658286196Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1659286196Sbapt if (strcmp(grp->gr_mem[i] , name) != 0) 1660286196Sbapt continue; 1661286196Sbapt for (j = i; grp->gr_mem[j] != NULL ; j++) 1662286196Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 1663286196Sbapt chggrent(grp->gr_name, grp); 1664286196Sbapt break; 1665286196Sbapt } 1666286196Sbapt } 1667286196Sbapt ENDGRENT(); 1668286196Sbapt /* Add the user to the needed groups */ 1669286196Sbapt for (i = 0; i < groups->sl_cur; i++) { 1670286196Sbapt grp = GETGRNAM(groups->sl_str[i]); 1671286196Sbapt grp = gr_add(grp, pwd->pw_name); 1672286196Sbapt if (grp == NULL) 1673286196Sbapt continue; 1674286196Sbapt chggrent(grp->gr_name, grp); 1675286196Sbapt free(grp); 1676286196Sbapt } 1677286196Sbapt } 1678286196Sbapt /* In case of rename we need to walk over the different groups */ 1679286196Sbapt if (newname) { 1680286196Sbapt SETGRENT(); 1681286196Sbapt while ((grp = GETGRENT()) != NULL) { 1682286196Sbapt if (grp->gr_mem == NULL) 1683286196Sbapt continue; 1684286196Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1685286196Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 1686286196Sbapt continue; 1687286196Sbapt grp->gr_mem[i] = newname; 1688286196Sbapt chggrent(grp->gr_name, grp); 1689286196Sbapt break; 1690286196Sbapt } 1691286196Sbapt } 1692286196Sbapt } 1693286196Sbapt 1694286196Sbapt /* go get a current version of pwd */ 1695286196Sbapt if (newname) 1696286196Sbapt name = newname; 1697286196Sbapt pwd = GETPWNAM(name); 1698286196Sbapt if (pwd == NULL) 1699286196Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 1700286196Sbapt grp = GETGRGID(pwd->pw_gid); 1701286196Sbapt pw_log(cnf, M_UPDATE, W_USER, "%s(%ju):%s(%ju):%s:%s:%s", 1702286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 1703286196Sbapt grp ? grp->gr_name : "unknown", 1704286196Sbapt (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1), 1705286196Sbapt pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 1706286196Sbapt 1707285433Sbapt /* 1708286196Sbapt * Let's create and populate the user's home directory. Note 1709286196Sbapt * that this also `works' for editing users if -m is used, but 1710286196Sbapt * existing files will *not* be overwritten. 1711285433Sbapt */ 1712286196Sbapt if (PWALTDIR() != PWF_ALT && docreatehome && pwd->pw_dir && 1713286196Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 1714286196Sbapt if (!skel) 1715286196Sbapt skel = cnf->dotdir; 1716286196Sbapt if (homemode == 0) 1717286196Sbapt homemode = cnf->homemode; 1718286196Sbapt create_and_populate_homedir(cnf, pwd, skel, homemode, true); 1719286196Sbapt } 1720286196Sbapt 1721286196Sbapt if (nis && nis_update() == 0) 1722286196Sbapt pw_log(cnf, M_UPDATE, W_USER, "NIS maps updated"); 1723286196Sbapt 1724286196Sbapt return (EXIT_SUCCESS); 172520747Sdavidn} 1726