120253Sjoerg/*- 2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3330449Seadler * 420302Sjoerg * Copyright (C) 1996 520302Sjoerg * David L. Nugent. All rights reserved. 620253Sjoerg * 720253Sjoerg * Redistribution and use in source and binary forms, with or without 820253Sjoerg * modification, are permitted provided that the following conditions 920253Sjoerg * are met: 1020253Sjoerg * 1. Redistributions of source code must retain the above copyright 1120302Sjoerg * notice, this list of conditions and the following disclaimer. 1220253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1320253Sjoerg * notice, this list of conditions and the following disclaimer in the 1420253Sjoerg * documentation and/or other materials provided with the distribution. 1520253Sjoerg * 1620302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 1720253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1820253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1920302Sjoerg * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 2020253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2120253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2220253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2320253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2420253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2520253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2620253Sjoerg * SUCH DAMAGE. 2744229Sdavidn * 2820253Sjoerg */ 2920253Sjoerg 3030259Scharnier#ifndef lint 3130259Scharnierstatic const char rcsid[] = 3250479Speter "$FreeBSD: stable/11/usr.sbin/pw/pw_user.c 360326 2020-04-25 22:23:34Z dim $"; 3330259Scharnier#endif /* not lint */ 3430259Scharnier 35286201Sbapt#include <sys/param.h> 36286201Sbapt#include <sys/types.h> 37286201Sbapt 38319217Sasomers#include <assert.h> 3930259Scharnier#include <ctype.h> 40286201Sbapt#include <dirent.h> 4130259Scharnier#include <err.h> 42286982Sbapt#include <errno.h> 4320253Sjoerg#include <fcntl.h> 44286201Sbapt#include <grp.h> 45286201Sbapt#include <pwd.h> 46286201Sbapt#include <libutil.h> 47286201Sbapt#include <login_cap.h> 4830259Scharnier#include <paths.h> 49286201Sbapt#include <string.h> 50286201Sbapt#include <sysexits.h> 5120253Sjoerg#include <termios.h> 52286201Sbapt#include <unistd.h> 53286201Sbapt 5420253Sjoerg#include "pw.h" 5520253Sjoerg#include "bitmap.h" 56286201Sbapt#include "psdate.h" 5720253Sjoerg 5823318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 5922394Sdavidn 6052512Sdavidnstatic char locked_str[] = "*LOCKED*"; 6124214Sache 62286196Sbaptstatic struct passwd fakeuser = { 63286196Sbapt "nouser", 64286196Sbapt "*", 65286196Sbapt -1, 66286196Sbapt -1, 67286196Sbapt 0, 68286196Sbapt "", 69286196Sbapt "User &", 70286196Sbapt "/nonexistent", 71286196Sbapt "/bin/sh", 72286196Sbapt 0, 73286196Sbapt 0 74286196Sbapt}; 7520253Sjoerg 76286196Sbaptstatic int print_user(struct passwd *pwd, bool pretty, bool v7); 77286196Sbaptstatic uid_t pw_uidpolicy(struct userconf *cnf, intmax_t id); 78286196Sbaptstatic uid_t pw_gidpolicy(struct userconf *cnf, char *grname, char *nam, 79286196Sbapt gid_t prefer, bool dryrun); 80286196Sbaptstatic char *pw_homepolicy(struct userconf * cnf, char *homedir, 81286196Sbapt const char *user); 82286196Sbaptstatic char *pw_shellpolicy(struct userconf * cnf); 83286196Sbaptstatic char *pw_password(struct userconf * cnf, char const * user, 84286196Sbapt bool dryrun); 85286196Sbaptstatic char *shell_path(char const * path, char *shells[], char *sh); 86286196Sbaptstatic void rmat(uid_t uid); 87286196Sbaptstatic void rmopie(char const * name); 88286196Sbapt 89283961Sbaptstatic void 90286982Sbaptmkdir_home_parents(int dfd, const char *dir) 91286982Sbapt{ 92286982Sbapt struct stat st; 93286982Sbapt char *dirs, *tmp; 94286982Sbapt 95286982Sbapt if (*dir != '/') 96286982Sbapt errx(EX_DATAERR, "invalid base directory for home '%s'", dir); 97286982Sbapt 98286982Sbapt dir++; 99286982Sbapt 100286982Sbapt if (fstatat(dfd, dir, &st, 0) != -1) { 101286982Sbapt if (S_ISDIR(st.st_mode)) 102286982Sbapt return; 103286982Sbapt errx(EX_OSFILE, "root home `/%s' is not a directory", dir); 104286982Sbapt } 105286982Sbapt 106286982Sbapt dirs = strdup(dir); 107286982Sbapt if (dirs == NULL) 108286982Sbapt errx(EX_UNAVAILABLE, "out of memory"); 109286982Sbapt 110286982Sbapt tmp = strrchr(dirs, '/'); 111290153Sbdrewery if (tmp == NULL) { 112290153Sbdrewery free(dirs); 113286982Sbapt return; 114290153Sbdrewery } 115286982Sbapt tmp[0] = '\0'; 116286982Sbapt 117286982Sbapt /* 118286982Sbapt * This is a kludge especially for Joerg :) 119286982Sbapt * If the home directory would be created in the root partition, then 120286982Sbapt * we really create it under /usr which is likely to have more space. 121286982Sbapt * But we create a symlink from cnf->home -> "/usr" -> cnf->home 122286982Sbapt */ 123286982Sbapt if (strchr(dirs, '/') == NULL) { 124286982Sbapt asprintf(&tmp, "usr/%s", dirs); 125286982Sbapt if (tmp == NULL) 126286982Sbapt errx(EX_UNAVAILABLE, "out of memory"); 127286982Sbapt if (mkdirat(dfd, tmp, _DEF_DIRMODE) != -1 || errno == EEXIST) { 128286982Sbapt fchownat(dfd, tmp, 0, 0, 0); 129286986Sbapt symlinkat(tmp, dfd, dirs); 130286982Sbapt } 131286982Sbapt free(tmp); 132286982Sbapt } 133286982Sbapt tmp = dirs; 134286982Sbapt if (fstatat(dfd, dirs, &st, 0) == -1) { 135286982Sbapt while ((tmp = strchr(tmp + 1, '/')) != NULL) { 136286982Sbapt *tmp = '\0'; 137286982Sbapt if (fstatat(dfd, dirs, &st, 0) == -1) { 138286982Sbapt if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1) 139286982Sbapt err(EX_OSFILE, "'%s' (root home parent) is not a directory", dirs); 140286982Sbapt } 141286982Sbapt *tmp = '/'; 142286982Sbapt } 143286982Sbapt } 144286982Sbapt if (fstatat(dfd, dirs, &st, 0) == -1) { 145286982Sbapt if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1) 146286982Sbapt err(EX_OSFILE, "'%s' (root home parent) is not a directory", dirs); 147286982Sbapt fchownat(dfd, dirs, 0, 0, 0); 148286982Sbapt } 149286982Sbapt 150286982Sbapt free(dirs); 151286982Sbapt} 152286982Sbapt 153286982Sbaptstatic void 154286196Sbaptcreate_and_populate_homedir(struct userconf *cnf, struct passwd *pwd, 155286196Sbapt const char *skeldir, mode_t homemode, bool update) 156283961Sbapt{ 157285430Sbapt int skelfd = -1; 158283961Sbapt 159286982Sbapt /* Create home parents directories */ 160286982Sbapt mkdir_home_parents(conf.rootfd, pwd->pw_dir); 161286982Sbapt 162285430Sbapt if (skeldir != NULL && *skeldir != '\0') { 163285434Sbapt if (*skeldir == '/') 164285434Sbapt skeldir++; 165285434Sbapt skelfd = openat(conf.rootfd, skeldir, O_DIRECTORY|O_CLOEXEC); 166283961Sbapt } 167283961Sbapt 168286196Sbapt copymkdir(conf.rootfd, pwd->pw_dir, skelfd, homemode, pwd->pw_uid, 169285430Sbapt pwd->pw_gid, 0); 170286196Sbapt pw_log(cnf, update ? M_UPDATE : M_ADD, W_USER, "%s(%ju) home %s made", 171286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, pwd->pw_dir); 172283961Sbapt} 173283961Sbapt 174285133Sbaptstatic int 175286196Sbaptpw_set_passwd(struct passwd *pwd, int fd, bool precrypted, bool update) 176285133Sbapt{ 177285133Sbapt int b, istty; 178285133Sbapt struct termios t, n; 179285133Sbapt login_cap_t *lc; 180285133Sbapt char line[_PASSWORD_LEN+1]; 181285133Sbapt char *p; 182285133Sbapt 183286196Sbapt if (fd == '-') { 184285133Sbapt if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 185285133Sbapt pwd->pw_passwd = "*"; /* No access */ 186285133Sbapt return (1); 187285133Sbapt } 188285133Sbapt return (0); 189285133Sbapt } 190285133Sbapt 191286196Sbapt if ((istty = isatty(fd))) { 192286196Sbapt if (tcgetattr(fd, &t) == -1) 193285133Sbapt istty = 0; 194285133Sbapt else { 195285137Sbapt n = t; 196285133Sbapt n.c_lflag &= ~(ECHO); 197286196Sbapt tcsetattr(fd, TCSANOW, &n); 198285133Sbapt printf("%s%spassword for user %s:", 199285133Sbapt update ? "new " : "", 200286196Sbapt precrypted ? "encrypted " : "", 201285133Sbapt pwd->pw_name); 202285133Sbapt fflush(stdout); 203285133Sbapt } 204285133Sbapt } 205286196Sbapt b = read(fd, line, sizeof(line) - 1); 206285133Sbapt if (istty) { /* Restore state */ 207286196Sbapt tcsetattr(fd, TCSANOW, &t); 208285133Sbapt fputc('\n', stdout); 209285133Sbapt fflush(stdout); 210285133Sbapt } 211285133Sbapt 212285133Sbapt if (b < 0) 213285133Sbapt err(EX_IOERR, "-%c file descriptor", 214286196Sbapt precrypted ? 'H' : 'h'); 215285133Sbapt line[b] = '\0'; 216285133Sbapt if ((p = strpbrk(line, "\r\n")) != NULL) 217285133Sbapt *p = '\0'; 218285133Sbapt if (!*line) 219285133Sbapt errx(EX_DATAERR, "empty password read on file descriptor %d", 220286196Sbapt fd); 221286196Sbapt if (precrypted) { 222285133Sbapt if (strchr(line, ':') != NULL) 223285137Sbapt errx(EX_DATAERR, "bad encrypted password"); 224286196Sbapt pwd->pw_passwd = strdup(line); 225285133Sbapt } else { 226285133Sbapt lc = login_getpwclass(pwd); 227285133Sbapt if (lc == NULL || 228285133Sbapt login_setcryptfmt(lc, "sha512", NULL) == NULL) 229285133Sbapt warn("setting crypt(3) format"); 230285133Sbapt login_close(lc); 231285133Sbapt pwd->pw_passwd = pw_pwcrypt(line); 232285133Sbapt } 233285133Sbapt return (1); 234285133Sbapt} 235285133Sbapt 236285405Sbaptstatic void 237286196Sbaptperform_chgpwent(const char *name, struct passwd *pwd, char *nispasswd) 238285405Sbapt{ 239285405Sbapt int rc; 240286196Sbapt struct passwd *nispwd; 241285405Sbapt 242286196Sbapt /* duplicate for nis so that chgpwent is not modifying before NIS */ 243286196Sbapt if (nispasswd && *nispasswd == '/') 244286196Sbapt nispwd = pw_dup(pwd); 245286196Sbapt 246285405Sbapt rc = chgpwent(name, pwd); 247285405Sbapt if (rc == -1) 248285405Sbapt errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name); 249285405Sbapt else if (rc != 0) 250285405Sbapt err(EX_IOERR, "passwd file update"); 251285405Sbapt 252286196Sbapt if (nispasswd && *nispasswd == '/') { 253286196Sbapt rc = chgnispwent(nispasswd, name, nispwd); 254285405Sbapt if (rc == -1) 255285405Sbapt warn("User '%s' not found in NIS passwd", pwd->pw_name); 256285984Sbapt else if (rc != 0) 257285405Sbapt warn("NIS passwd update"); 258285405Sbapt /* NOTE: NIS-only update errors are not fatal */ 259285405Sbapt } 260285405Sbapt} 261285405Sbapt 262285405Sbapt/* 263285405Sbapt * The M_LOCK and M_UNLOCK functions simply add or remove 264285405Sbapt * a "*LOCKED*" prefix from in front of the password to 265285405Sbapt * prevent it decoding correctly, and therefore prevents 266285405Sbapt * access. Of course, this only prevents access via 267285405Sbapt * password authentication (not ssh, kerberos or any 268285405Sbapt * other method that does not use the UNIX password) but 269285405Sbapt * that is a known limitation. 270285405Sbapt */ 271285405Sbaptstatic int 272286196Sbaptpw_userlock(char *arg1, int mode) 273285405Sbapt{ 274285405Sbapt struct passwd *pwd = NULL; 275285405Sbapt char *passtmp = NULL; 276286196Sbapt char *name; 277285405Sbapt bool locked = false; 278291657Sbapt uid_t id = (uid_t)-1; 279285405Sbapt 280286196Sbapt if (geteuid() != 0) 281286196Sbapt errx(EX_NOPERM, "you must be root"); 282286196Sbapt 283286196Sbapt if (arg1 == NULL) 284285405Sbapt errx(EX_DATAERR, "username or id required"); 285285405Sbapt 286291657Sbapt name = arg1; 287291657Sbapt if (arg1[strspn(name, "0123456789")] == '\0') 288291657Sbapt id = pw_checkid(name, UID_MAX); 289286196Sbapt 290291657Sbapt pwd = GETPWNAM(pw_checkname(name, 0)); 291291657Sbapt if (pwd == NULL && id != (uid_t)-1) { 292291657Sbapt pwd = GETPWUID(id); 293291657Sbapt if (pwd != NULL) 294291657Sbapt name = pwd->pw_name; 295291657Sbapt } 296285405Sbapt if (pwd == NULL) { 297291657Sbapt if (id == (uid_t)-1) 298291657Sbapt errx(EX_NOUSER, "no such name or uid `%ju'", (uintmax_t) id); 299285405Sbapt errx(EX_NOUSER, "no such user `%s'", name); 300285405Sbapt } 301285405Sbapt 302285405Sbapt if (name == NULL) 303285405Sbapt name = pwd->pw_name; 304285405Sbapt 305285405Sbapt if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str) -1) == 0) 306285405Sbapt locked = true; 307285405Sbapt if (mode == M_LOCK && locked) 308285405Sbapt errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 309285405Sbapt if (mode == M_UNLOCK && !locked) 310285405Sbapt errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 311285405Sbapt 312285405Sbapt if (mode == M_LOCK) { 313285405Sbapt asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 314285405Sbapt if (passtmp == NULL) /* disaster */ 315285405Sbapt errx(EX_UNAVAILABLE, "out of memory"); 316285405Sbapt pwd->pw_passwd = passtmp; 317285405Sbapt } else { 318285405Sbapt pwd->pw_passwd += sizeof(locked_str)-1; 319285405Sbapt } 320285405Sbapt 321286196Sbapt perform_chgpwent(name, pwd, NULL); 322285405Sbapt free(passtmp); 323285405Sbapt 324285405Sbapt return (EXIT_SUCCESS); 325285405Sbapt} 326285405Sbapt 327286196Sbaptstatic uid_t 328286196Sbaptpw_uidpolicy(struct userconf * cnf, intmax_t id) 32920253Sjoerg{ 330286196Sbapt struct passwd *pwd; 331286196Sbapt struct bitmap bm; 332286196Sbapt uid_t uid = (uid_t) - 1; 33320253Sjoerg 33420267Sjoerg /* 335286196Sbapt * Check the given uid, if any 33620253Sjoerg */ 337286196Sbapt if (id >= 0) { 338286196Sbapt uid = (uid_t) id; 33920253Sjoerg 340286196Sbapt if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate) 341286196Sbapt errx(EX_DATAERR, "uid `%ju' has already been allocated", 342286196Sbapt (uintmax_t)pwd->pw_uid); 343286196Sbapt return (uid); 34420253Sjoerg } 34521052Sdavidn /* 346286196Sbapt * We need to allocate the next available uid under one of 347286196Sbapt * two policies a) Grab the first unused uid b) Grab the 348286196Sbapt * highest possible unused uid 34921052Sdavidn */ 350286196Sbapt if (cnf->min_uid >= cnf->max_uid) { /* Sanity 351286196Sbapt * claus^H^H^H^Hheck */ 352286196Sbapt cnf->min_uid = 1000; 353286196Sbapt cnf->max_uid = 32000; 35421052Sdavidn } 355286196Sbapt bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 35621052Sdavidn 35720253Sjoerg /* 358286196Sbapt * Now, let's fill the bitmap from the password file 35920253Sjoerg */ 360286196Sbapt SETPWENT(); 361286196Sbapt while ((pwd = GETPWENT()) != NULL) 362286196Sbapt if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 363286196Sbapt bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 364286196Sbapt ENDPWENT(); 36552527Sdavidn 36620253Sjoerg /* 367286196Sbapt * Then apply the policy, with fallback to reuse if necessary 36820253Sjoerg */ 369286196Sbapt if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 370286196Sbapt uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 37120253Sjoerg 37220267Sjoerg /* 373286196Sbapt * Another sanity check 37420267Sjoerg */ 375286196Sbapt if (uid < cnf->min_uid || uid > cnf->max_uid) 376286196Sbapt errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 377286196Sbapt bm_dealloc(&bm); 378286196Sbapt return (uid); 37920253Sjoerg} 38020253Sjoerg 381286196Sbaptstatic uid_t 382286196Sbaptpw_gidpolicy(struct userconf *cnf, char *grname, char *nam, gid_t prefer, bool dryrun) 38320253Sjoerg{ 38420253Sjoerg struct group *grp; 38520253Sjoerg gid_t gid = (uid_t) - 1; 38620253Sjoerg 38720253Sjoerg /* 38820253Sjoerg * Check the given gid, if any 38920253Sjoerg */ 39044229Sdavidn SETGRENT(); 391286196Sbapt if (grname) { 392286196Sbapt if ((grp = GETGRNAM(grname)) == NULL) { 393286196Sbapt gid = pw_checkid(grname, GID_MAX); 394286196Sbapt grp = GETGRGID(gid); 39520253Sjoerg } 39620253Sjoerg gid = grp->gr_gid; 397262865Sjulian } else if ((grp = GETGRNAM(nam)) != NULL && 398262865Sjulian (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 39920267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 40020253Sjoerg } else { 401286196Sbapt intmax_t grid = -1; 40220253Sjoerg 40320253Sjoerg /* 40420253Sjoerg * We need to auto-create a group with the user's name. We 40520253Sjoerg * can send all the appropriate output to our sister routine 40620253Sjoerg * bit first see if we can create a group with gid==uid so we 40720253Sjoerg * can keep the user and group ids in sync. We purposely do 40820253Sjoerg * NOT check the gid range if we can force the sync. If the 40920253Sjoerg * user's name dups an existing group, then the group add 41020253Sjoerg * function will happily handle that case for us and exit. 41120253Sjoerg */ 412285414Sbapt if (GETGRGID(prefer) == NULL) 413285414Sbapt grid = prefer; 414286196Sbapt if (dryrun) { 415285395Sbapt gid = pw_groupnext(cnf, true); 416285395Sbapt } else { 417286196Sbapt if (grid == -1) 418286196Sbapt grid = pw_groupnext(cnf, true); 419286196Sbapt groupadd(cnf, nam, grid, NULL, -1, false, false, false); 42044229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 42120267Sjoerg gid = grp->gr_gid; 42220267Sjoerg } 42320253Sjoerg } 42444229Sdavidn ENDGRENT(); 425286196Sbapt return (gid); 42620253Sjoerg} 42720253Sjoerg 428286196Sbaptstatic char * 429286196Sbaptpw_homepolicy(struct userconf * cnf, char *homedir, const char *user) 43020253Sjoerg{ 431282699Sbapt static char home[128]; 43220253Sjoerg 433286196Sbapt if (homedir) 434286196Sbapt return (homedir); 43520253Sjoerg 436282699Sbapt if (cnf->home == NULL || *cnf->home == '\0') 437282699Sbapt errx(EX_CONFIG, "no base home directory set"); 438282699Sbapt snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 439282699Sbapt 440282699Sbapt return (home); 44120253Sjoerg} 44220253Sjoerg 443286196Sbaptstatic char * 44420253Sjoergshell_path(char const * path, char *shells[], char *sh) 44520253Sjoerg{ 44620253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 44720253Sjoerg return sh; /* specified full path or forced none */ 44820253Sjoerg else { 44920253Sjoerg char *p; 45020253Sjoerg char paths[_UC_MAXLINE]; 45120253Sjoerg 45220253Sjoerg /* 45320253Sjoerg * We need to search paths 45420253Sjoerg */ 455130633Srobert strlcpy(paths, path, sizeof(paths)); 45620253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 45720253Sjoerg int i; 45820253Sjoerg static char shellpath[256]; 45920253Sjoerg 46020253Sjoerg if (sh != NULL) { 461282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); 46220253Sjoerg if (access(shellpath, X_OK) == 0) 46320253Sjoerg return shellpath; 46420253Sjoerg } else 46520253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 466282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); 46720253Sjoerg if (access(shellpath, X_OK) == 0) 46820253Sjoerg return shellpath; 46920253Sjoerg } 47020253Sjoerg } 47120253Sjoerg if (sh == NULL) 47230259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 47330259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 47420253Sjoerg return NULL; 47520253Sjoerg } 47620253Sjoerg} 47720253Sjoerg 478286196Sbaptstatic char * 479286196Sbaptpw_shellpolicy(struct userconf * cnf) 48020253Sjoerg{ 48120253Sjoerg 482286196Sbapt return shell_path(cnf->shelldir, cnf->shells, cnf->shell_default); 48320253Sjoerg} 48420253Sjoerg 485179365Santoine#define SALTSIZE 32 48620253Sjoerg 487179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 488179365Santoine 489286196Sbaptchar * 49020253Sjoergpw_pwcrypt(char *password) 49120253Sjoerg{ 49220253Sjoerg int i; 493179365Santoine char salt[SALTSIZE + 1]; 494231994Skevlo char *cryptpw; 49520253Sjoerg static char buf[256]; 496319217Sasomers size_t pwlen; 49720253Sjoerg 49820253Sjoerg /* 49920253Sjoerg * Calculate a salt value 50020253Sjoerg */ 501179365Santoine for (i = 0; i < SALTSIZE; i++) 502181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 503179365Santoine salt[SALTSIZE] = '\0'; 50420253Sjoerg 505231994Skevlo cryptpw = crypt(password, salt); 506231994Skevlo if (cryptpw == NULL) 507231994Skevlo errx(EX_CONFIG, "crypt(3) failure"); 508319217Sasomers pwlen = strlcpy(buf, cryptpw, sizeof(buf)); 509319217Sasomers assert(pwlen < sizeof(buf)); 510319217Sasomers return (buf); 51120253Sjoerg} 51220253Sjoerg 513286196Sbaptstatic char * 514286196Sbaptpw_password(struct userconf * cnf, char const * user, bool dryrun) 51520253Sjoerg{ 51620253Sjoerg int i, l; 51720253Sjoerg char pwbuf[32]; 51820253Sjoerg 51920253Sjoerg switch (cnf->default_password) { 520326848Seugen case P_NONE: /* No password at all! */ 521326848Seugen return ""; 522326848Seugen case P_RANDOM: /* Random password */ 52373563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 52420253Sjoerg for (i = 0; i < l; i++) 525181785Sache pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 52620253Sjoerg pwbuf[i] = '\0'; 52720253Sjoerg 52820253Sjoerg /* 52920253Sjoerg * We give this information back to the user 53020253Sjoerg */ 531286196Sbapt if (conf.fd == -1 && !dryrun) { 53261957Sache if (isatty(STDOUT_FILENO)) 53320712Sdavidn printf("Password for '%s' is: ", user); 53420253Sjoerg printf("%s\n", pwbuf); 53520253Sjoerg fflush(stdout); 53620253Sjoerg } 53720253Sjoerg break; 538326848Seugen case P_YES: /* user's name */ 539326848Seugen strlcpy(pwbuf, user, sizeof(pwbuf)); 540326848Seugen break; 541326848Seugen case P_NO: /* No login - default */ 542326848Seugen /* FALLTHROUGH */ 54320253Sjoerg default: 54420253Sjoerg return "*"; 54520253Sjoerg } 54620253Sjoerg return pw_pwcrypt(pwbuf); 54720253Sjoerg} 54820253Sjoerg 549284111Sbaptstatic int 550286196Sbaptprint_user(struct passwd * pwd, bool pretty, bool v7) 551284111Sbapt{ 552286196Sbapt int j; 553286196Sbapt char *p; 554286196Sbapt struct group *grp = GETGRGID(pwd->pw_gid); 555286196Sbapt char uname[60] = "User &", office[60] = "[None]", 556286196Sbapt wphone[60] = "[None]", hphone[60] = "[None]"; 557286196Sbapt char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 558286196Sbapt struct tm * tptr; 559286196Sbapt 560286196Sbapt if (!pretty) { 561286196Sbapt p = v7 ? pw_make_v7(pwd) : pw_make(pwd); 562286196Sbapt printf("%s\n", p); 563286196Sbapt free(p); 564286196Sbapt return (EXIT_SUCCESS); 565286196Sbapt } 566286196Sbapt 567286196Sbapt if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 568286196Sbapt strlcpy(uname, p, sizeof(uname)); 569286196Sbapt if ((p = strtok(NULL, ",")) != NULL) { 570286196Sbapt strlcpy(office, p, sizeof(office)); 571286196Sbapt if ((p = strtok(NULL, ",")) != NULL) { 572286196Sbapt strlcpy(wphone, p, sizeof(wphone)); 573286196Sbapt if ((p = strtok(NULL, "")) != NULL) { 574286196Sbapt strlcpy(hphone, p, sizeof(hphone)); 575286196Sbapt } 576286196Sbapt } 577286196Sbapt } 578286196Sbapt } 579286196Sbapt /* 580286196Sbapt * Handle '&' in gecos field 581286196Sbapt */ 582286196Sbapt if ((p = strchr(uname, '&')) != NULL) { 583286196Sbapt int l = strlen(pwd->pw_name); 584286196Sbapt int m = strlen(p); 585286196Sbapt 586286196Sbapt memmove(p + l, p + 1, m); 587286196Sbapt memmove(p, pwd->pw_name, l); 588286196Sbapt *p = (char) toupper((unsigned char)*p); 589286196Sbapt } 590286196Sbapt if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 591286196Sbapt strftime(acexpire, sizeof acexpire, "%c", tptr); 592360326Sdim if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 593286196Sbapt strftime(pwexpire, sizeof pwexpire, "%c", tptr); 594286196Sbapt printf("Login Name: %-15s #%-12ju Group: %-15s #%ju\n" 595286196Sbapt " Full Name: %s\n" 596286196Sbapt " Home: %-26.26s Class: %s\n" 597286196Sbapt " Shell: %-26.26s Office: %s\n" 598286196Sbapt "Work Phone: %-26.26s Home Phone: %s\n" 599286196Sbapt "Acc Expire: %-26.26s Pwd Expire: %s\n", 600286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 601286196Sbapt grp ? grp->gr_name : "(invalid)", (uintmax_t)pwd->pw_gid, 602286196Sbapt uname, pwd->pw_dir, pwd->pw_class, 603286196Sbapt pwd->pw_shell, office, wphone, hphone, 604286196Sbapt acexpire, pwexpire); 605286196Sbapt SETGRENT(); 606286196Sbapt j = 0; 607286196Sbapt while ((grp=GETGRENT()) != NULL) { 608286196Sbapt int i = 0; 609286196Sbapt if (grp->gr_mem != NULL) { 610286196Sbapt while (grp->gr_mem[i] != NULL) { 611286196Sbapt if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) { 612286196Sbapt printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 613286196Sbapt break; 614286196Sbapt } 615286196Sbapt ++i; 616286196Sbapt } 617286196Sbapt } 618286196Sbapt } 619286196Sbapt ENDGRENT(); 620286196Sbapt printf("%s", j ? "\n" : ""); 621286196Sbapt return (EXIT_SUCCESS); 622286196Sbapt} 623286196Sbapt 624286196Sbaptchar * 625286196Sbaptpw_checkname(char *name, int gecos) 626286196Sbapt{ 627286196Sbapt char showch[8]; 628286196Sbapt const char *badchars, *ch, *showtype; 629286196Sbapt int reject; 630286196Sbapt 631286196Sbapt ch = name; 632286196Sbapt reject = 0; 633286196Sbapt if (gecos) { 634286196Sbapt /* See if the name is valid as a gecos (comment) field. */ 635330694Sdab badchars = ":"; 636286196Sbapt showtype = "gecos field"; 637286196Sbapt } else { 638286196Sbapt /* See if the name is valid as a userid or group. */ 639286196Sbapt badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 640286196Sbapt showtype = "userid/group name"; 641286196Sbapt /* Userids and groups can not have a leading '-'. */ 642286196Sbapt if (*ch == '-') 643286196Sbapt reject = 1; 644286196Sbapt } 645286196Sbapt if (!reject) { 646286196Sbapt while (*ch) { 647291658Sbapt if (strchr(badchars, *ch) != NULL || 648291658Sbapt (!gecos && *ch < ' ') || 649286196Sbapt *ch == 127) { 650286196Sbapt reject = 1; 651286196Sbapt break; 652286196Sbapt } 653286196Sbapt /* 8-bit characters are only allowed in GECOS fields */ 654286196Sbapt if (!gecos && (*ch & 0x80)) { 655286196Sbapt reject = 1; 656286196Sbapt break; 657286196Sbapt } 658286196Sbapt ch++; 659286196Sbapt } 660286196Sbapt } 661286196Sbapt /* 662286196Sbapt * A `$' is allowed as the final character for userids and groups, 663286196Sbapt * mainly for the benefit of samba. 664286196Sbapt */ 665286196Sbapt if (reject && !gecos) { 666286196Sbapt if (*ch == '$' && *(ch + 1) == '\0') { 667286196Sbapt reject = 0; 668286196Sbapt ch++; 669286196Sbapt } 670286196Sbapt } 671286196Sbapt if (reject) { 672286196Sbapt snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 673286196Sbapt ? "`%c'" : "0x%02x", *ch); 674286196Sbapt errx(EX_DATAERR, "invalid character %s at position %td in %s", 675286196Sbapt showch, (ch - name), showtype); 676286196Sbapt } 677286196Sbapt if (!gecos && (ch - name) > LOGNAMESIZE) 678286196Sbapt errx(EX_USAGE, "name too long `%s' (max is %d)", name, 679286196Sbapt LOGNAMESIZE); 680286196Sbapt 681286196Sbapt return (name); 682286196Sbapt} 683286196Sbapt 684286196Sbaptstatic void 685286196Sbaptrmat(uid_t uid) 686286196Sbapt{ 687286196Sbapt DIR *d = opendir("/var/at/jobs"); 688286196Sbapt 689286196Sbapt if (d != NULL) { 690286196Sbapt struct dirent *e; 691286196Sbapt 692286196Sbapt while ((e = readdir(d)) != NULL) { 693286196Sbapt struct stat st; 694286196Sbapt 695286196Sbapt if (strncmp(e->d_name, ".lock", 5) != 0 && 696286196Sbapt stat(e->d_name, &st) == 0 && 697286196Sbapt !S_ISDIR(st.st_mode) && 698286196Sbapt st.st_uid == uid) { 699286196Sbapt char tmp[MAXPATHLEN]; 700286196Sbapt 701286202Sbapt snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", 702286202Sbapt e->d_name); 703286196Sbapt system(tmp); 704286196Sbapt } 705286196Sbapt } 706286196Sbapt closedir(d); 707286196Sbapt } 708286196Sbapt} 709286196Sbapt 710286196Sbaptstatic void 711286196Sbaptrmopie(char const * name) 712286196Sbapt{ 713286196Sbapt char tmp[1014]; 714286196Sbapt FILE *fp; 715286196Sbapt int fd; 716286196Sbapt size_t len; 717286196Sbapt off_t atofs = 0; 718286196Sbapt 719286196Sbapt if ((fd = openat(conf.rootfd, "etc/opiekeys", O_RDWR)) == -1) 720286196Sbapt return; 721286196Sbapt 722286196Sbapt fp = fdopen(fd, "r+"); 723286196Sbapt len = strlen(name); 724286196Sbapt 725286196Sbapt while (fgets(tmp, sizeof(tmp), fp) != NULL) { 726286196Sbapt if (strncmp(name, tmp, len) == 0 && tmp[len]==' ') { 727286196Sbapt /* Comment username out */ 728286196Sbapt if (fseek(fp, atofs, SEEK_SET) == 0) 729286196Sbapt fwrite("#", 1, 1, fp); 730286196Sbapt break; 731286196Sbapt } 732286196Sbapt atofs = ftell(fp); 733286196Sbapt } 734286196Sbapt /* 735286196Sbapt * If we got an error of any sort, don't update! 736286196Sbapt */ 737286196Sbapt fclose(fp); 738286196Sbapt} 739286196Sbapt 740286196Sbaptint 741286196Sbaptpw_user_next(int argc, char **argv, char *name __unused) 742286196Sbapt{ 743286196Sbapt struct userconf *cnf = NULL; 744286196Sbapt const char *cfg = NULL; 745286196Sbapt int ch; 746286196Sbapt bool quiet = false; 747286196Sbapt uid_t next; 748286196Sbapt 749301367Sbapt while ((ch = getopt(argc, argv, "C:q")) != -1) { 750286196Sbapt switch (ch) { 751286196Sbapt case 'C': 752286196Sbapt cfg = optarg; 753286196Sbapt break; 754286196Sbapt case 'q': 755286217Sadrian quiet = true; 756286196Sbapt break; 757286196Sbapt } 758286196Sbapt } 759286196Sbapt 760286196Sbapt if (quiet) 761286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 762286196Sbapt 763286196Sbapt cnf = get_userconfig(cfg); 764286196Sbapt 765286196Sbapt next = pw_uidpolicy(cnf, -1); 766286196Sbapt 767286196Sbapt printf("%ju:", (uintmax_t)next); 768286196Sbapt pw_groupnext(cnf, quiet); 769286196Sbapt 770286196Sbapt return (EXIT_SUCCESS); 771286196Sbapt} 772286196Sbapt 773286196Sbaptint 774286196Sbaptpw_user_show(int argc, char **argv, char *arg1) 775286196Sbapt{ 776285401Sbapt struct passwd *pwd = NULL; 777286196Sbapt char *name = NULL; 778286218Sbapt intmax_t id = -1; 779286196Sbapt int ch; 780286196Sbapt bool all = false; 781286196Sbapt bool pretty = false; 782286196Sbapt bool force = false; 783286196Sbapt bool v7 = false; 784286196Sbapt bool quiet = false; 78520253Sjoerg 786286196Sbapt if (arg1 != NULL) { 787286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 788286196Sbapt id = pw_checkid(arg1, UID_MAX); 789286196Sbapt else 790286196Sbapt name = arg1; 791286196Sbapt } 792286196Sbapt 793286196Sbapt while ((ch = getopt(argc, argv, "C:qn:u:FPa7")) != -1) { 794286196Sbapt switch (ch) { 795286196Sbapt case 'C': 796286196Sbapt /* ignore compatibility */ 797286196Sbapt break; 798286196Sbapt case 'q': 799286196Sbapt quiet = true; 800286196Sbapt break; 801286196Sbapt case 'n': 802286196Sbapt name = optarg; 803286196Sbapt break; 804286196Sbapt case 'u': 805286196Sbapt id = pw_checkid(optarg, UID_MAX); 806286196Sbapt break; 807286196Sbapt case 'F': 808286196Sbapt force = true; 809286196Sbapt break; 810286196Sbapt case 'P': 811286196Sbapt pretty = true; 812286196Sbapt break; 813286196Sbapt case 'a': 814286196Sbapt all = true; 815286196Sbapt break; 816287799Sbapt case '7': 817286196Sbapt v7 = true; 818286196Sbapt break; 819286196Sbapt } 820286196Sbapt } 821286196Sbapt 822286196Sbapt if (quiet) 823286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 824286196Sbapt 825286196Sbapt if (all) { 826286196Sbapt SETPWENT(); 827286196Sbapt while ((pwd = GETPWENT()) != NULL) 828286196Sbapt print_user(pwd, pretty, v7); 829286196Sbapt ENDPWENT(); 830286196Sbapt return (EXIT_SUCCESS); 831286196Sbapt } 832286196Sbapt 833285401Sbapt if (id < 0 && name == NULL) 834285401Sbapt errx(EX_DATAERR, "username or id required"); 835285401Sbapt 836285401Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 837285401Sbapt if (pwd == NULL) { 838286196Sbapt if (force) { 839286196Sbapt pwd = &fakeuser; 840286196Sbapt } else { 841286196Sbapt if (name == NULL) 842286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", 843286196Sbapt (uintmax_t) id); 844286196Sbapt errx(EX_NOUSER, "no such user `%s'", name); 845286196Sbapt } 846286196Sbapt } 847286196Sbapt 848286196Sbapt return (print_user(pwd, pretty, v7)); 849286196Sbapt} 850286196Sbapt 851286196Sbaptint 852286196Sbaptpw_user_del(int argc, char **argv, char *arg1) 853286196Sbapt{ 854286196Sbapt struct userconf *cnf = NULL; 855286196Sbapt struct passwd *pwd = NULL; 856286196Sbapt struct group *gr, *grp; 857286196Sbapt char *name = NULL; 858286196Sbapt char grname[MAXLOGNAME]; 859286196Sbapt char *nispasswd = NULL; 860286196Sbapt char file[MAXPATHLEN]; 861286196Sbapt char home[MAXPATHLEN]; 862286196Sbapt const char *cfg = NULL; 863286196Sbapt struct stat st; 864286218Sbapt intmax_t id = -1; 865286196Sbapt int ch, rc; 866286196Sbapt bool nis = false; 867286196Sbapt bool deletehome = false; 868286196Sbapt bool quiet = false; 869286196Sbapt 870286196Sbapt if (arg1 != NULL) { 871286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 872286196Sbapt id = pw_checkid(arg1, UID_MAX); 873286196Sbapt else 874286196Sbapt name = arg1; 875286196Sbapt } 876286196Sbapt 877286196Sbapt while ((ch = getopt(argc, argv, "C:qn:u:rYy:")) != -1) { 878286196Sbapt switch (ch) { 879286196Sbapt case 'C': 880286196Sbapt cfg = optarg; 881286196Sbapt break; 882286196Sbapt case 'q': 883286196Sbapt quiet = true; 884286196Sbapt break; 885286196Sbapt case 'n': 886286196Sbapt name = optarg; 887286196Sbapt break; 888286196Sbapt case 'u': 889286196Sbapt id = pw_checkid(optarg, UID_MAX); 890286196Sbapt break; 891286196Sbapt case 'r': 892286196Sbapt deletehome = true; 893286196Sbapt break; 894286196Sbapt case 'y': 895286196Sbapt nispasswd = optarg; 896286196Sbapt break; 897286196Sbapt case 'Y': 898286196Sbapt nis = true; 899286196Sbapt break; 900286196Sbapt } 901286196Sbapt } 902286196Sbapt 903286196Sbapt if (quiet) 904286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 905286196Sbapt 906286196Sbapt if (id < 0 && name == NULL) 907286196Sbapt errx(EX_DATAERR, "username or id required"); 908286196Sbapt 909286196Sbapt cnf = get_userconfig(cfg); 910286196Sbapt 911286196Sbapt if (nispasswd == NULL) 912286196Sbapt nispasswd = cnf->nispasswd; 913286196Sbapt 914286196Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 915286196Sbapt if (pwd == NULL) { 916285401Sbapt if (name == NULL) 917286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", (uintmax_t) id); 918285401Sbapt errx(EX_NOUSER, "no such user `%s'", name); 919285401Sbapt } 920285989Sbapt 921285989Sbapt if (PWF._altdir == PWF_REGULAR && 922286196Sbapt ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) { 923286196Sbapt if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 924286196Sbapt if (!nis && nispasswd && *nispasswd != '/') 925286196Sbapt errx(EX_NOUSER, "Cannot remove NIS user `%s'", 926286196Sbapt name); 927286196Sbapt } else { 928286196Sbapt errx(EX_NOUSER, "Cannot remove non local user `%s'", 929286196Sbapt name); 930286196Sbapt } 931286196Sbapt } 932285989Sbapt 933286196Sbapt id = pwd->pw_uid; 934285401Sbapt if (name == NULL) 935285401Sbapt name = pwd->pw_name; 936285401Sbapt 937284111Sbapt if (strcmp(pwd->pw_name, "root") == 0) 938284111Sbapt errx(EX_DATAERR, "cannot remove user 'root'"); 939284111Sbapt 940286196Sbapt /* Remove opie record from /etc/opiekeys */ 941285433Sbapt if (PWALTDIR() != PWF_ALT) 942284111Sbapt rmopie(pwd->pw_name); 943284111Sbapt 944285433Sbapt if (!PWALTDIR()) { 945285433Sbapt /* Remove crontabs */ 946284111Sbapt snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 947284111Sbapt if (access(file, F_OK) == 0) { 948286202Sbapt snprintf(file, sizeof(file), "crontab -u %s -r", 949286202Sbapt pwd->pw_name); 950284111Sbapt system(file); 951284111Sbapt } 952284111Sbapt } 953286196Sbapt 954284111Sbapt /* 955284111Sbapt * Save these for later, since contents of pwd may be 956284111Sbapt * invalidated by deletion 957284111Sbapt */ 958284111Sbapt snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 959284111Sbapt strlcpy(home, pwd->pw_dir, sizeof(home)); 960284111Sbapt gr = GETGRGID(pwd->pw_gid); 961284111Sbapt if (gr != NULL) 962284111Sbapt strlcpy(grname, gr->gr_name, LOGNAMESIZE); 963284111Sbapt else 964284111Sbapt grname[0] = '\0'; 965284111Sbapt 966284111Sbapt rc = delpwent(pwd); 967284111Sbapt if (rc == -1) 968284111Sbapt err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 969284111Sbapt else if (rc != 0) 970284111Sbapt err(EX_IOERR, "passwd update"); 971284111Sbapt 972286196Sbapt if (nis && nispasswd && *nispasswd=='/') { 973286196Sbapt rc = delnispwent(nispasswd, name); 974284111Sbapt if (rc == -1) 975285401Sbapt warnx("WARNING: user '%s' does not exist in NIS passwd", 976285401Sbapt pwd->pw_name); 977284111Sbapt else if (rc != 0) 978284111Sbapt warn("WARNING: NIS passwd update"); 979284111Sbapt } 980284111Sbapt 981284128Sbapt grp = GETGRNAM(name); 982284111Sbapt if (grp != NULL && 983284111Sbapt (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 984284128Sbapt strcmp(name, grname) == 0) 985284128Sbapt delgrent(GETGRNAM(name)); 986284111Sbapt SETGRENT(); 987284111Sbapt while ((grp = GETGRENT()) != NULL) { 988284111Sbapt int i, j; 989284111Sbapt char group[MAXLOGNAME]; 990284113Sbapt if (grp->gr_mem == NULL) 991284113Sbapt continue; 992284113Sbapt 993284113Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 994284128Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 995284113Sbapt continue; 996284113Sbapt 997284113Sbapt for (j = i; grp->gr_mem[j] != NULL; j++) 998284113Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 999284113Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 1000284113Sbapt chggrent(group, grp); 1001284111Sbapt } 1002284111Sbapt } 1003284111Sbapt ENDGRENT(); 1004284111Sbapt 1005286196Sbapt pw_log(cnf, M_DELETE, W_USER, "%s(%ju) account removed", name, 1006286196Sbapt (uintmax_t)id); 1007284111Sbapt 1008285433Sbapt /* Remove mail file */ 1009285433Sbapt if (PWALTDIR() != PWF_ALT) 1010285433Sbapt unlinkat(conf.rootfd, file + 1, 0); 1011284111Sbapt 1012286196Sbapt /* Remove at jobs */ 1013286196Sbapt if (!PWALTDIR() && getpwuid(id) == NULL) 1014286196Sbapt rmat(id); 1015284111Sbapt 1016285433Sbapt /* Remove home directory and contents */ 1017286196Sbapt if (PWALTDIR() != PWF_ALT && deletehome && *home == '/' && 1018286196Sbapt GETPWUID(id) == NULL && 1019285433Sbapt fstatat(conf.rootfd, home + 1, &st, 0) != -1) { 1020286196Sbapt rm_r(conf.rootfd, home, id); 1021286196Sbapt pw_log(cnf, M_DELETE, W_USER, "%s(%ju) home '%s' %s" 1022286196Sbapt "removed", name, (uintmax_t)id, home, 1023285433Sbapt fstatat(conf.rootfd, home + 1, &st, 0) == -1 ? "" : "not " 1024285433Sbapt "completely "); 1025284111Sbapt } 1026284111Sbapt 1027284111Sbapt return (EXIT_SUCCESS); 1028284111Sbapt} 1029284111Sbapt 1030286196Sbaptint 1031286196Sbaptpw_user_lock(int argc, char **argv, char *arg1) 103220253Sjoerg{ 1033286196Sbapt int ch; 103420253Sjoerg 1035286196Sbapt while ((ch = getopt(argc, argv, "Cq")) != -1) { 1036286196Sbapt switch (ch) { 1037286196Sbapt case 'C': 1038286196Sbapt case 'q': 1039286196Sbapt /* compatibility */ 1040286196Sbapt break; 104120253Sjoerg } 1042286196Sbapt } 104320253Sjoerg 1044286196Sbapt return (pw_userlock(arg1, M_LOCK)); 1045286196Sbapt} 1046286196Sbapt 1047286196Sbaptint 1048286196Sbaptpw_user_unlock(int argc, char **argv, char *arg1) 1049286196Sbapt{ 1050286196Sbapt int ch; 1051286196Sbapt 1052286196Sbapt while ((ch = getopt(argc, argv, "Cq")) != -1) { 1053286196Sbapt switch (ch) { 1054286196Sbapt case 'C': 1055286196Sbapt case 'q': 1056286196Sbapt /* compatibility */ 1057286196Sbapt break; 105820253Sjoerg } 105920253Sjoerg } 1060286196Sbapt 1061286196Sbapt return (pw_userlock(arg1, M_UNLOCK)); 106220253Sjoerg} 106320253Sjoerg 1064286196Sbaptstatic struct group * 1065286196Sbaptgroup_from_name_or_id(char *name) 106620253Sjoerg{ 1067286196Sbapt const char *errstr = NULL; 1068286196Sbapt struct group *grp; 1069286196Sbapt uintmax_t id; 107020253Sjoerg 1071286196Sbapt if ((grp = GETGRNAM(name)) == NULL) { 1072286196Sbapt id = strtounum(name, 0, GID_MAX, &errstr); 1073286196Sbapt if (errstr) 1074286196Sbapt errx(EX_NOUSER, "group `%s' does not exist", name); 1075286196Sbapt grp = GETGRGID(id); 1076286196Sbapt if (grp == NULL) 1077286196Sbapt errx(EX_NOUSER, "group `%s' does not exist", name); 107820253Sjoerg } 1079286196Sbapt 1080286196Sbapt return (grp); 1081286196Sbapt} 1082286196Sbapt 1083286196Sbaptstatic void 1084286196Sbaptsplit_groups(StringList **groups, char *groupsstr) 1085286196Sbapt{ 1086286196Sbapt struct group *grp; 1087286196Sbapt char *p; 1088286196Sbapt char tok[] = ", \t"; 1089286196Sbapt 1090322919Semaste if (*groups == NULL) 1091322919Semaste *groups = sl_init(); 1092286196Sbapt for (p = strtok(groupsstr, tok); p != NULL; p = strtok(NULL, tok)) { 1093286196Sbapt grp = group_from_name_or_id(p); 1094286196Sbapt sl_add(*groups, newstr(grp->gr_name)); 1095286196Sbapt } 1096286196Sbapt} 1097286196Sbapt 1098286196Sbaptstatic void 1099286196Sbaptvalidate_grname(struct userconf *cnf, char *group) 1100286196Sbapt{ 1101286196Sbapt struct group *grp; 1102286196Sbapt 1103286196Sbapt if (group == NULL || *group == '\0') { 1104286196Sbapt cnf->default_group = ""; 1105286196Sbapt return; 1106286196Sbapt } 1107286196Sbapt grp = group_from_name_or_id(group); 1108286196Sbapt cnf->default_group = newstr(grp->gr_name); 1109286196Sbapt} 1110286196Sbapt 1111286196Sbaptstatic mode_t 1112286196Sbaptvalidate_mode(char *mode) 1113286196Sbapt{ 1114286196Sbapt mode_t m; 1115286196Sbapt void *set; 1116286196Sbapt 1117286196Sbapt if ((set = setmode(mode)) == NULL) 1118286196Sbapt errx(EX_DATAERR, "invalid directory creation mode '%s'", mode); 1119286196Sbapt 1120286196Sbapt m = getmode(set, _DEF_DIRMODE); 1121286196Sbapt free(set); 1122286196Sbapt return (m); 1123286196Sbapt} 1124286196Sbapt 1125326848Seugenstatic long 1126326848Seugenvalidate_expire(char *str, int opt) 1127326848Seugen{ 1128326848Seugen if (!numerics(str)) 1129326848Seugen errx(EX_DATAERR, "-%c argument must be numeric " 1130326848Seugen "when setting defaults: %s", (char)opt, str); 1131326848Seugen return strtol(str, NULL, 0); 1132326848Seugen} 1133326848Seugen 1134286196Sbaptstatic void 1135286196Sbaptmix_config(struct userconf *cmdcnf, struct userconf *cfg) 1136286196Sbapt{ 1137286196Sbapt 1138326848Seugen if (cmdcnf->default_password < 0) 1139286196Sbapt cmdcnf->default_password = cfg->default_password; 1140286196Sbapt if (cmdcnf->reuse_uids == 0) 1141286196Sbapt cmdcnf->reuse_uids = cfg->reuse_uids; 1142286196Sbapt if (cmdcnf->reuse_gids == 0) 1143286196Sbapt cmdcnf->reuse_gids = cfg->reuse_gids; 1144286196Sbapt if (cmdcnf->nispasswd == NULL) 1145286196Sbapt cmdcnf->nispasswd = cfg->nispasswd; 1146286196Sbapt if (cmdcnf->dotdir == NULL) 1147286196Sbapt cmdcnf->dotdir = cfg->dotdir; 1148286196Sbapt if (cmdcnf->newmail == NULL) 1149286196Sbapt cmdcnf->newmail = cfg->newmail; 1150286196Sbapt if (cmdcnf->logfile == NULL) 1151286196Sbapt cmdcnf->logfile = cfg->logfile; 1152286196Sbapt if (cmdcnf->home == NULL) 1153286196Sbapt cmdcnf->home = cfg->home; 1154286196Sbapt if (cmdcnf->homemode == 0) 1155286196Sbapt cmdcnf->homemode = cfg->homemode; 1156286196Sbapt if (cmdcnf->shelldir == NULL) 1157286196Sbapt cmdcnf->shelldir = cfg->shelldir; 1158286196Sbapt if (cmdcnf->shells == NULL) 1159286196Sbapt cmdcnf->shells = cfg->shells; 1160286196Sbapt if (cmdcnf->shell_default == NULL) 1161286196Sbapt cmdcnf->shell_default = cfg->shell_default; 1162286196Sbapt if (cmdcnf->default_group == NULL) 1163286196Sbapt cmdcnf->default_group = cfg->default_group; 1164286196Sbapt if (cmdcnf->groups == NULL) 1165286196Sbapt cmdcnf->groups = cfg->groups; 1166286196Sbapt if (cmdcnf->default_class == NULL) 1167286196Sbapt cmdcnf->default_class = cfg->default_class; 1168286196Sbapt if (cmdcnf->min_uid == 0) 1169286196Sbapt cmdcnf->min_uid = cfg->min_uid; 1170286196Sbapt if (cmdcnf->max_uid == 0) 1171286196Sbapt cmdcnf->max_uid = cfg->max_uid; 1172286196Sbapt if (cmdcnf->min_gid == 0) 1173286196Sbapt cmdcnf->min_gid = cfg->min_gid; 1174286196Sbapt if (cmdcnf->max_gid == 0) 1175286196Sbapt cmdcnf->max_gid = cfg->max_gid; 1176326848Seugen if (cmdcnf->expire_days < 0) 1177286196Sbapt cmdcnf->expire_days = cfg->expire_days; 1178326848Seugen if (cmdcnf->password_days < 0) 1179286196Sbapt cmdcnf->password_days = cfg->password_days; 1180286196Sbapt} 1181286196Sbapt 1182286196Sbaptint 1183286196Sbaptpw_user_add(int argc, char **argv, char *arg1) 1184286196Sbapt{ 1185286196Sbapt struct userconf *cnf, *cmdcnf; 1186286196Sbapt struct passwd *pwd; 1187286196Sbapt struct group *grp; 1188286196Sbapt struct stat st; 1189286196Sbapt char args[] = "C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y"; 1190286196Sbapt char line[_PASSWORD_LEN+1], path[MAXPATHLEN]; 1191286196Sbapt char *gecos, *homedir, *skel, *walk, *userid, *groupid, *grname; 1192286196Sbapt char *default_passwd, *name, *p; 1193316347Sbapt const char *cfg = NULL; 1194286196Sbapt login_cap_t *lc; 1195286196Sbapt FILE *pfp, *fp; 1196286196Sbapt intmax_t id = -1; 1197286196Sbapt time_t now; 1198286196Sbapt int rc, ch, fd = -1; 1199286196Sbapt size_t i; 1200286196Sbapt bool dryrun, nis, pretty, quiet, createhome, precrypted, genconf; 1201286196Sbapt 1202286196Sbapt dryrun = nis = pretty = quiet = createhome = precrypted = false; 1203286196Sbapt genconf = false; 1204286196Sbapt gecos = homedir = skel = userid = groupid = default_passwd = NULL; 1205286196Sbapt grname = name = NULL; 1206286196Sbapt 1207286196Sbapt if ((cmdcnf = calloc(1, sizeof(struct userconf))) == NULL) 1208286196Sbapt err(EXIT_FAILURE, "calloc()"); 1209286196Sbapt 1210326848Seugen cmdcnf->default_password = cmdcnf->expire_days = cmdcnf->password_days = -1; 1211326848Seugen now = time(NULL); 1212326848Seugen 1213286196Sbapt if (arg1 != NULL) { 1214286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 1215286196Sbapt id = pw_checkid(arg1, UID_MAX); 1216286196Sbapt else 1217323009Semaste name = pw_checkname(arg1, 0); 1218286196Sbapt } 1219286196Sbapt 1220286196Sbapt while ((ch = getopt(argc, argv, args)) != -1) { 1221286196Sbapt switch (ch) { 1222286196Sbapt case 'C': 1223286196Sbapt cfg = optarg; 1224286196Sbapt break; 1225286196Sbapt case 'q': 1226286196Sbapt quiet = true; 1227286196Sbapt break; 1228286196Sbapt case 'n': 1229323009Semaste name = pw_checkname(optarg, 0); 1230286196Sbapt break; 1231286196Sbapt case 'u': 1232286196Sbapt userid = optarg; 1233286196Sbapt break; 1234286196Sbapt case 'c': 1235286196Sbapt gecos = pw_checkname(optarg, 1); 1236286196Sbapt break; 1237286196Sbapt case 'd': 1238286196Sbapt homedir = optarg; 1239286196Sbapt break; 1240286196Sbapt case 'e': 1241326848Seugen if (genconf) 1242326848Seugen cmdcnf->expire_days = validate_expire(optarg, ch); 1243326848Seugen else 1244326848Seugen cmdcnf->expire_days = parse_date(now, optarg); 1245286196Sbapt break; 1246286196Sbapt case 'p': 1247326848Seugen if (genconf) 1248326848Seugen cmdcnf->password_days = validate_expire(optarg, ch); 1249326848Seugen else 1250326848Seugen cmdcnf->password_days = parse_date(now, optarg); 1251286196Sbapt break; 1252286196Sbapt case 'g': 1253286196Sbapt validate_grname(cmdcnf, optarg); 1254286196Sbapt grname = optarg; 1255286196Sbapt break; 1256286196Sbapt case 'G': 1257286196Sbapt split_groups(&cmdcnf->groups, optarg); 1258286196Sbapt break; 1259286196Sbapt case 'm': 1260286196Sbapt createhome = true; 1261286196Sbapt break; 1262286196Sbapt case 'M': 1263286196Sbapt cmdcnf->homemode = validate_mode(optarg); 1264286196Sbapt break; 1265286196Sbapt case 'k': 1266286196Sbapt walk = skel = optarg; 1267286196Sbapt if (*walk == '/') 1268286196Sbapt walk++; 1269286196Sbapt if (fstatat(conf.rootfd, walk, &st, 0) == -1) 1270286196Sbapt errx(EX_OSFILE, "skeleton `%s' does not " 1271286196Sbapt "exists", skel); 1272286196Sbapt if (!S_ISDIR(st.st_mode)) 1273286196Sbapt errx(EX_OSFILE, "skeleton `%s' is not a " 1274286196Sbapt "directory", skel); 1275286196Sbapt cmdcnf->dotdir = skel; 1276286196Sbapt break; 1277286196Sbapt case 's': 1278286196Sbapt cmdcnf->shell_default = optarg; 1279286196Sbapt break; 1280286196Sbapt case 'o': 1281286196Sbapt conf.checkduplicate = false; 1282286196Sbapt break; 1283286196Sbapt case 'L': 1284286196Sbapt cmdcnf->default_class = pw_checkname(optarg, 0); 1285286196Sbapt break; 1286286196Sbapt case 'i': 1287286196Sbapt groupid = optarg; 1288286196Sbapt break; 1289286196Sbapt case 'w': 1290286196Sbapt default_passwd = optarg; 1291286196Sbapt break; 1292286196Sbapt case 'H': 1293286196Sbapt if (fd != -1) 1294286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1295286196Sbapt "exclusive options"); 1296286196Sbapt fd = pw_checkfd(optarg); 1297286196Sbapt precrypted = true; 1298286196Sbapt if (fd == '-') 1299286196Sbapt errx(EX_USAGE, "-H expects a file descriptor"); 1300286196Sbapt break; 1301286196Sbapt case 'h': 1302286196Sbapt if (fd != -1) 1303286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1304286196Sbapt "exclusive options"); 1305286196Sbapt fd = pw_checkfd(optarg); 1306286196Sbapt break; 1307286196Sbapt case 'D': 1308286196Sbapt genconf = true; 1309286196Sbapt break; 1310286196Sbapt case 'b': 1311286196Sbapt cmdcnf->home = optarg; 1312286196Sbapt break; 1313286196Sbapt case 'N': 1314286196Sbapt dryrun = true; 1315286196Sbapt break; 1316286196Sbapt case 'P': 1317286196Sbapt pretty = true; 1318286196Sbapt break; 1319286196Sbapt case 'y': 1320286196Sbapt cmdcnf->nispasswd = optarg; 1321286196Sbapt break; 1322286196Sbapt case 'Y': 1323286196Sbapt nis = true; 1324286196Sbapt break; 1325109961Sgad } 1326109961Sgad } 1327286196Sbapt 1328286196Sbapt if (geteuid() != 0 && ! dryrun) 1329286196Sbapt errx(EX_NOPERM, "you must be root"); 1330286196Sbapt 1331286196Sbapt if (quiet) 1332286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 1333286196Sbapt 1334286196Sbapt cnf = get_userconfig(cfg); 1335286196Sbapt 1336286196Sbapt mix_config(cmdcnf, cnf); 1337286196Sbapt if (default_passwd) 1338305741Sasomers cmdcnf->default_password = passwd_val(default_passwd, 1339286196Sbapt cnf->default_password); 1340286196Sbapt if (genconf) { 1341286196Sbapt if (name != NULL) 1342286196Sbapt errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 1343286196Sbapt if (userid != NULL) { 1344286196Sbapt if ((p = strtok(userid, ", \t")) != NULL) 1345286196Sbapt cmdcnf->min_uid = pw_checkid(p, UID_MAX); 1346286196Sbapt if (cmdcnf->min_uid == 0) 1347286196Sbapt cmdcnf->min_uid = 1000; 1348286196Sbapt if ((p = strtok(NULL, " ,\t")) != NULL) 1349286196Sbapt cmdcnf->max_uid = pw_checkid(p, UID_MAX); 1350286196Sbapt if (cmdcnf->max_uid == 0) 1351286196Sbapt cmdcnf->max_uid = 32000; 1352109961Sgad } 1353286196Sbapt if (groupid != NULL) { 1354286196Sbapt if ((p = strtok(groupid, ", \t")) != NULL) 1355286196Sbapt cmdcnf->min_gid = pw_checkid(p, GID_MAX); 1356286196Sbapt if (cmdcnf->min_gid == 0) 1357286196Sbapt cmdcnf->min_gid = 1000; 1358286196Sbapt if ((p = strtok(NULL, " ,\t")) != NULL) 1359286196Sbapt cmdcnf->max_gid = pw_checkid(p, GID_MAX); 1360286196Sbapt if (cmdcnf->max_gid == 0) 1361286196Sbapt cmdcnf->max_gid = 32000; 1362286196Sbapt } 1363286196Sbapt if (write_userconfig(cmdcnf, cfg)) 1364286196Sbapt return (EXIT_SUCCESS); 1365286196Sbapt err(EX_IOERR, "config update"); 1366109961Sgad } 1367286196Sbapt 1368286196Sbapt if (userid) 1369286196Sbapt id = pw_checkid(userid, UID_MAX); 1370286196Sbapt if (id < 0 && name == NULL) 1371286196Sbapt errx(EX_DATAERR, "user name or id required"); 1372286196Sbapt 1373286196Sbapt if (name == NULL) 1374286196Sbapt errx(EX_DATAERR, "login name required"); 1375286196Sbapt 1376286198Sbapt if (GETPWNAM(name) != NULL) 1377286198Sbapt errx(EX_DATAERR, "login name `%s' already exists", name); 1378286198Sbapt 1379316347Sbapt if (!grname) 1380316347Sbapt grname = cmdcnf->default_group; 1381316347Sbapt 1382286196Sbapt pwd = &fakeuser; 1383286196Sbapt pwd->pw_name = name; 1384286196Sbapt pwd->pw_class = cmdcnf->default_class ? cmdcnf->default_class : ""; 1385286196Sbapt pwd->pw_uid = pw_uidpolicy(cmdcnf, id); 1386286196Sbapt pwd->pw_gid = pw_gidpolicy(cnf, grname, pwd->pw_name, 1387286196Sbapt (gid_t) pwd->pw_uid, dryrun); 1388327090Seugen 1389327090Seugen /* cmdcnf->password_days and cmdcnf->expire_days hold unixtime here */ 1390326848Seugen if (cmdcnf->password_days > 0) 1391327090Seugen pwd->pw_change = cmdcnf->password_days; 1392326848Seugen if (cmdcnf->expire_days > 0) 1393327090Seugen pwd->pw_expire = cmdcnf->expire_days; 1394326848Seugen 1395286196Sbapt pwd->pw_dir = pw_homepolicy(cmdcnf, homedir, pwd->pw_name); 1396286196Sbapt pwd->pw_shell = pw_shellpolicy(cmdcnf); 1397286196Sbapt lc = login_getpwclass(pwd); 1398286196Sbapt if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 1399286196Sbapt warn("setting crypt(3) format"); 1400286196Sbapt login_close(lc); 1401286196Sbapt pwd->pw_passwd = pw_password(cmdcnf, pwd->pw_name, dryrun); 1402286196Sbapt if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 1403286196Sbapt warnx("WARNING: new account `%s' has a uid of 0 " 1404286196Sbapt "(superuser access!)", pwd->pw_name); 1405286196Sbapt if (gecos) 1406286196Sbapt pwd->pw_gecos = gecos; 1407286196Sbapt 1408286196Sbapt if (fd != -1) 1409286196Sbapt pw_set_passwd(pwd, fd, precrypted, false); 1410286196Sbapt 1411286196Sbapt if (dryrun) 1412286196Sbapt return (print_user(pwd, pretty, false)); 1413286196Sbapt 1414286196Sbapt if ((rc = addpwent(pwd)) != 0) { 1415286196Sbapt if (rc == -1) 1416286196Sbapt errx(EX_IOERR, "user '%s' already exists", 1417286196Sbapt pwd->pw_name); 1418286196Sbapt else if (rc != 0) 1419286196Sbapt err(EX_IOERR, "passwd file update"); 1420109961Sgad } 1421286196Sbapt if (nis && cmdcnf->nispasswd && *cmdcnf->nispasswd == '/') { 1422286196Sbapt printf("%s\n", cmdcnf->nispasswd); 1423286196Sbapt rc = addnispwent(cmdcnf->nispasswd, pwd); 1424286196Sbapt if (rc == -1) 1425286202Sbapt warnx("User '%s' already exists in NIS passwd", 1426286202Sbapt pwd->pw_name); 1427286196Sbapt else if (rc != 0) 1428286196Sbapt warn("NIS passwd update"); 1429286196Sbapt /* NOTE: we treat NIS-only update errors as non-fatal */ 1430286196Sbapt } 1431284110Sbapt 1432286196Sbapt if (cmdcnf->groups != NULL) { 1433286196Sbapt for (i = 0; i < cmdcnf->groups->sl_cur; i++) { 1434286196Sbapt grp = GETGRNAM(cmdcnf->groups->sl_str[i]); 1435286196Sbapt grp = gr_add(grp, pwd->pw_name); 1436286196Sbapt /* 1437286196Sbapt * grp can only be NULL in 2 cases: 1438286196Sbapt * - the new member is already a member 1439286196Sbapt * - a problem with memory occurs 1440286196Sbapt * in both cases we want to skip now. 1441286196Sbapt */ 1442286196Sbapt if (grp == NULL) 1443286196Sbapt continue; 1444286196Sbapt chggrent(grp->gr_name, grp); 1445286196Sbapt free(grp); 1446286196Sbapt } 1447286196Sbapt } 144820253Sjoerg 1449286196Sbapt pwd = GETPWNAM(name); 1450286196Sbapt if (pwd == NULL) 1451286196Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 145220253Sjoerg 1453286196Sbapt grp = GETGRGID(pwd->pw_gid); 1454286196Sbapt pw_log(cnf, M_ADD, W_USER, "%s(%ju):%s(%ju):%s:%s:%s", 1455286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 1456286202Sbapt grp ? grp->gr_name : "unknown", 1457286202Sbapt (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1), 1458286196Sbapt pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 145920253Sjoerg 1460286196Sbapt /* 1461286196Sbapt * let's touch and chown the user's mail file. This is not 1462286196Sbapt * strictly necessary under BSD with a 0755 maildir but it also 1463286196Sbapt * doesn't hurt anything to create the empty mailfile 1464286196Sbapt */ 1465286196Sbapt if (PWALTDIR() != PWF_ALT) { 1466286196Sbapt snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, 1467286196Sbapt pwd->pw_name); 1468286196Sbapt /* Preserve contents & mtime */ 1469286196Sbapt close(openat(conf.rootfd, path +1, O_RDWR | O_CREAT, 0600)); 1470286196Sbapt fchownat(conf.rootfd, path + 1, pwd->pw_uid, pwd->pw_gid, 1471286196Sbapt AT_SYMLINK_NOFOLLOW); 1472286196Sbapt } 147320253Sjoerg 1474286196Sbapt /* 1475286196Sbapt * Let's create and populate the user's home directory. Note 1476286196Sbapt * that this also `works' for editing users if -m is used, but 1477286196Sbapt * existing files will *not* be overwritten. 1478286196Sbapt */ 1479286196Sbapt if (PWALTDIR() != PWF_ALT && createhome && pwd->pw_dir && 1480286196Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) 1481286196Sbapt create_and_populate_homedir(cmdcnf, pwd, cmdcnf->dotdir, 1482286196Sbapt cmdcnf->homemode, false); 148320253Sjoerg 1484286196Sbapt if (!PWALTDIR() && cmdcnf->newmail && *cmdcnf->newmail && 1485286196Sbapt (fp = fopen(cnf->newmail, "r")) != NULL) { 1486286196Sbapt if ((pfp = popen(_PATH_SENDMAIL " -t", "w")) == NULL) 1487286196Sbapt warn("sendmail"); 1488286196Sbapt else { 1489286196Sbapt fprintf(pfp, "From: root\n" "To: %s\n" 1490286196Sbapt "Subject: Welcome!\n\n", pwd->pw_name); 1491286196Sbapt while (fgets(line, sizeof(line), fp) != NULL) { 1492286196Sbapt /* Do substitutions? */ 1493286196Sbapt fputs(line, pfp); 149420253Sjoerg } 1495286196Sbapt pclose(pfp); 1496286196Sbapt pw_log(cnf, M_ADD, W_USER, "%s(%ju) new user mail sent", 1497286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid); 149820253Sjoerg } 1499286196Sbapt fclose(fp); 150020253Sjoerg } 1501286196Sbapt 1502286196Sbapt if (nis && nis_update() == 0) 1503286196Sbapt pw_log(cnf, M_ADD, W_USER, "NIS maps updated"); 1504286196Sbapt 1505286196Sbapt return (EXIT_SUCCESS); 150620253Sjoerg} 150720747Sdavidn 1508286196Sbaptint 1509286196Sbaptpw_user_mod(int argc, char **argv, char *arg1) 151020747Sdavidn{ 1511286196Sbapt struct userconf *cnf; 1512286196Sbapt struct passwd *pwd; 1513286196Sbapt struct group *grp; 1514286196Sbapt StringList *groups = NULL; 1515286196Sbapt char args[] = "C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:NPYy:"; 1516316347Sbapt const char *cfg = NULL; 1517286196Sbapt char *gecos, *homedir, *grname, *name, *newname, *walk, *skel, *shell; 1518286196Sbapt char *passwd, *class, *nispasswd; 1519286196Sbapt login_cap_t *lc; 1520286196Sbapt struct stat st; 1521286196Sbapt intmax_t id = -1; 1522286196Sbapt int ch, fd = -1; 1523286196Sbapt size_t i, j; 1524312931Sbapt bool quiet, createhome, pretty, dryrun, nis, edited; 1525286218Sbapt bool precrypted; 1526286196Sbapt mode_t homemode = 0; 1527327090Seugen time_t expire_time, password_time, now; 152820747Sdavidn 1529327090Seugen expire_time = password_time = -1; 1530286196Sbapt gecos = homedir = grname = name = newname = skel = shell =NULL; 1531286196Sbapt passwd = NULL; 1532286196Sbapt class = nispasswd = NULL; 1533286196Sbapt quiet = createhome = pretty = dryrun = nis = precrypted = false; 1534312931Sbapt edited = false; 1535326848Seugen now = time(NULL); 153620747Sdavidn 1537286196Sbapt if (arg1 != NULL) { 1538286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 1539286196Sbapt id = pw_checkid(arg1, UID_MAX); 1540286196Sbapt else 1541286196Sbapt name = arg1; 1542286196Sbapt } 1543286196Sbapt 1544286196Sbapt while ((ch = getopt(argc, argv, args)) != -1) { 1545286196Sbapt switch (ch) { 1546286196Sbapt case 'C': 1547286196Sbapt cfg = optarg; 1548285433Sbapt break; 1549286196Sbapt case 'q': 1550286196Sbapt quiet = true; 1551286196Sbapt break; 1552286196Sbapt case 'n': 1553286196Sbapt name = optarg; 1554286196Sbapt break; 1555286196Sbapt case 'u': 1556286196Sbapt id = pw_checkid(optarg, UID_MAX); 1557286196Sbapt break; 1558286196Sbapt case 'c': 1559286196Sbapt gecos = pw_checkname(optarg, 1); 1560286196Sbapt break; 1561286196Sbapt case 'd': 1562286196Sbapt homedir = optarg; 1563286196Sbapt break; 1564286196Sbapt case 'e': 1565327090Seugen expire_time = parse_date(now, optarg); 1566286196Sbapt break; 1567286196Sbapt case 'p': 1568327090Seugen password_time = parse_date(now, optarg); 1569286196Sbapt break; 1570286196Sbapt case 'g': 1571286196Sbapt group_from_name_or_id(optarg); 1572286196Sbapt grname = optarg; 1573286196Sbapt break; 1574286196Sbapt case 'G': 1575286196Sbapt split_groups(&groups, optarg); 1576286196Sbapt break; 1577286196Sbapt case 'm': 1578286196Sbapt createhome = true; 1579286196Sbapt break; 1580286196Sbapt case 'M': 1581286196Sbapt homemode = validate_mode(optarg); 1582286196Sbapt break; 1583286196Sbapt case 'l': 1584286196Sbapt newname = optarg; 1585286196Sbapt break; 1586286196Sbapt case 'k': 1587286196Sbapt walk = skel = optarg; 1588286196Sbapt if (*walk == '/') 1589286196Sbapt walk++; 1590286196Sbapt if (fstatat(conf.rootfd, walk, &st, 0) == -1) 1591286196Sbapt errx(EX_OSFILE, "skeleton `%s' does not " 1592286196Sbapt "exists", skel); 1593286196Sbapt if (!S_ISDIR(st.st_mode)) 1594286196Sbapt errx(EX_OSFILE, "skeleton `%s' is not a " 1595286196Sbapt "directory", skel); 1596286196Sbapt break; 1597286196Sbapt case 's': 1598286196Sbapt shell = optarg; 1599286196Sbapt break; 1600286196Sbapt case 'w': 1601286196Sbapt passwd = optarg; 1602286196Sbapt break; 1603286196Sbapt case 'L': 1604286196Sbapt class = pw_checkname(optarg, 0); 1605286196Sbapt break; 1606286196Sbapt case 'H': 1607286196Sbapt if (fd != -1) 1608286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1609286196Sbapt "exclusive options"); 1610286196Sbapt fd = pw_checkfd(optarg); 1611286196Sbapt precrypted = true; 1612286196Sbapt if (fd == '-') 1613286196Sbapt errx(EX_USAGE, "-H expects a file descriptor"); 1614286196Sbapt break; 1615286196Sbapt case 'h': 1616286196Sbapt if (fd != -1) 1617286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1618286196Sbapt "exclusive options"); 1619286196Sbapt fd = pw_checkfd(optarg); 1620286196Sbapt break; 1621286196Sbapt case 'N': 1622286196Sbapt dryrun = true; 1623286196Sbapt break; 1624286196Sbapt case 'P': 1625286196Sbapt pretty = true; 1626286196Sbapt break; 1627286196Sbapt case 'y': 1628286196Sbapt nispasswd = optarg; 1629286196Sbapt break; 1630286196Sbapt case 'Y': 1631286196Sbapt nis = true; 1632286196Sbapt break; 163320747Sdavidn } 163420747Sdavidn } 1635286196Sbapt 1636286196Sbapt if (geteuid() != 0 && ! dryrun) 1637286196Sbapt errx(EX_NOPERM, "you must be root"); 1638286196Sbapt 1639286196Sbapt if (quiet) 1640286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 1641286196Sbapt 1642286196Sbapt cnf = get_userconfig(cfg); 1643286196Sbapt 1644286196Sbapt if (id < 0 && name == NULL) 1645286196Sbapt errx(EX_DATAERR, "username or id required"); 1646286196Sbapt 1647286196Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 1648286196Sbapt if (pwd == NULL) { 1649286196Sbapt if (name == NULL) 1650286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", 1651286196Sbapt (uintmax_t) id); 1652286196Sbapt errx(EX_NOUSER, "no such user `%s'", name); 1653286196Sbapt } 1654286196Sbapt 1655286196Sbapt if (name == NULL) 1656286196Sbapt name = pwd->pw_name; 1657286196Sbapt 1658286196Sbapt if (nis && nispasswd == NULL) 1659286196Sbapt nispasswd = cnf->nispasswd; 1660286196Sbapt 1661286196Sbapt if (PWF._altdir == PWF_REGULAR && 1662286196Sbapt ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) { 1663286196Sbapt if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 1664286196Sbapt if (!nis && nispasswd && *nispasswd != '/') 1665286196Sbapt errx(EX_NOUSER, "Cannot modify NIS user `%s'", 1666286196Sbapt name); 1667286196Sbapt } else { 1668286196Sbapt errx(EX_NOUSER, "Cannot modify non local user `%s'", 1669286196Sbapt name); 1670286196Sbapt } 1671286196Sbapt } 1672286196Sbapt 1673286196Sbapt if (newname) { 1674286196Sbapt if (strcmp(pwd->pw_name, "root") == 0) 1675286196Sbapt errx(EX_DATAERR, "can't rename `root' account"); 1676286196Sbapt if (strcmp(pwd->pw_name, newname) != 0) { 1677286196Sbapt pwd->pw_name = pw_checkname(newname, 0); 1678286196Sbapt edited = true; 1679286196Sbapt } 1680286196Sbapt } 1681286196Sbapt 1682296300Smarkj if (id >= 0 && pwd->pw_uid != id) { 1683286196Sbapt pwd->pw_uid = id; 1684286196Sbapt edited = true; 1685286196Sbapt if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 1686286196Sbapt errx(EX_DATAERR, "can't change uid of `root' account"); 1687286196Sbapt if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 1688286202Sbapt warnx("WARNING: account `%s' will have a uid of 0 " 1689286202Sbapt "(superuser access!)", pwd->pw_name); 1690286196Sbapt } 1691286196Sbapt 1692286196Sbapt if (grname && pwd->pw_uid != 0) { 1693286196Sbapt grp = GETGRNAM(grname); 1694286196Sbapt if (grp == NULL) 1695286196Sbapt grp = GETGRGID(pw_checkid(grname, GID_MAX)); 1696286196Sbapt if (grp->gr_gid != pwd->pw_gid) { 1697286196Sbapt pwd->pw_gid = grp->gr_gid; 1698286196Sbapt edited = true; 1699286196Sbapt } 1700286196Sbapt } 1701286196Sbapt 1702326848Seugen 1703327090Seugen if (password_time >= 0 && pwd->pw_change != password_time) { 1704327090Seugen pwd->pw_change = password_time; 1705286196Sbapt edited = true; 1706286196Sbapt } 1707286196Sbapt 1708327090Seugen if (expire_time >= 0 && pwd->pw_expire != expire_time) { 1709327090Seugen pwd->pw_expire = expire_time; 1710286196Sbapt edited = true; 1711286196Sbapt } 1712286196Sbapt 1713286196Sbapt if (shell) { 1714286196Sbapt shell = shell_path(cnf->shelldir, cnf->shells, shell); 1715286196Sbapt if (shell == NULL) 1716286196Sbapt shell = ""; 1717286196Sbapt if (strcmp(shell, pwd->pw_shell) != 0) { 1718286196Sbapt pwd->pw_shell = shell; 1719286196Sbapt edited = true; 1720286196Sbapt } 1721286196Sbapt } 1722286196Sbapt 1723286196Sbapt if (class && strcmp(pwd->pw_class, class) != 0) { 1724286196Sbapt pwd->pw_class = class; 1725286196Sbapt edited = true; 1726286196Sbapt } 1727286196Sbapt 1728286196Sbapt if (homedir && strcmp(pwd->pw_dir, homedir) != 0) { 1729286196Sbapt pwd->pw_dir = homedir; 1730287701Sbapt edited = true; 1731286196Sbapt if (fstatat(conf.rootfd, pwd->pw_dir, &st, 0) == -1) { 1732286196Sbapt if (!createhome) 1733286196Sbapt warnx("WARNING: home `%s' does not exist", 1734286196Sbapt pwd->pw_dir); 1735286196Sbapt } else if (!S_ISDIR(st.st_mode)) { 1736286196Sbapt warnx("WARNING: home `%s' is not a directory", 1737286196Sbapt pwd->pw_dir); 1738286196Sbapt } 1739286196Sbapt } 1740286196Sbapt 1741286196Sbapt if (passwd && conf.fd == -1) { 1742286196Sbapt lc = login_getpwclass(pwd); 1743286196Sbapt if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 1744286196Sbapt warn("setting crypt(3) format"); 1745286196Sbapt login_close(lc); 1746305741Sasomers cnf->default_password = passwd_val(passwd, 1747286775Sbapt cnf->default_password); 1748286196Sbapt pwd->pw_passwd = pw_password(cnf, pwd->pw_name, dryrun); 1749286196Sbapt edited = true; 1750286196Sbapt } 1751286196Sbapt 1752286196Sbapt if (gecos && strcmp(pwd->pw_gecos, gecos) != 0) { 1753286196Sbapt pwd->pw_gecos = gecos; 1754286196Sbapt edited = true; 1755286196Sbapt } 1756286196Sbapt 1757286196Sbapt if (fd != -1) 1758286196Sbapt edited = pw_set_passwd(pwd, fd, precrypted, true); 1759286196Sbapt 1760286196Sbapt if (dryrun) 1761286196Sbapt return (print_user(pwd, pretty, false)); 1762286196Sbapt 1763286196Sbapt if (edited) /* Only updated this if required */ 1764286196Sbapt perform_chgpwent(name, pwd, nis ? nispasswd : NULL); 1765286196Sbapt /* Now perform the needed changes concern groups */ 1766286196Sbapt if (groups != NULL) { 1767286196Sbapt /* Delete User from groups using old name */ 1768286196Sbapt SETGRENT(); 1769286196Sbapt while ((grp = GETGRENT()) != NULL) { 1770286196Sbapt if (grp->gr_mem == NULL) 1771286196Sbapt continue; 1772286196Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1773286196Sbapt if (strcmp(grp->gr_mem[i] , name) != 0) 1774286196Sbapt continue; 1775286196Sbapt for (j = i; grp->gr_mem[j] != NULL ; j++) 1776286196Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 1777286196Sbapt chggrent(grp->gr_name, grp); 1778286196Sbapt break; 1779286196Sbapt } 1780286196Sbapt } 1781286196Sbapt ENDGRENT(); 1782286196Sbapt /* Add the user to the needed groups */ 1783286196Sbapt for (i = 0; i < groups->sl_cur; i++) { 1784286196Sbapt grp = GETGRNAM(groups->sl_str[i]); 1785286196Sbapt grp = gr_add(grp, pwd->pw_name); 1786286196Sbapt if (grp == NULL) 1787286196Sbapt continue; 1788286196Sbapt chggrent(grp->gr_name, grp); 1789286196Sbapt free(grp); 1790286196Sbapt } 1791286196Sbapt } 1792286196Sbapt /* In case of rename we need to walk over the different groups */ 1793286196Sbapt if (newname) { 1794286196Sbapt SETGRENT(); 1795286196Sbapt while ((grp = GETGRENT()) != NULL) { 1796286196Sbapt if (grp->gr_mem == NULL) 1797286196Sbapt continue; 1798286196Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1799286196Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 1800286196Sbapt continue; 1801286196Sbapt grp->gr_mem[i] = newname; 1802286196Sbapt chggrent(grp->gr_name, grp); 1803286196Sbapt break; 1804286196Sbapt } 1805286196Sbapt } 1806286196Sbapt } 1807286196Sbapt 1808286196Sbapt /* go get a current version of pwd */ 1809286196Sbapt if (newname) 1810286196Sbapt name = newname; 1811286196Sbapt pwd = GETPWNAM(name); 1812286196Sbapt if (pwd == NULL) 1813286196Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 1814286196Sbapt grp = GETGRGID(pwd->pw_gid); 1815286196Sbapt pw_log(cnf, M_UPDATE, W_USER, "%s(%ju):%s(%ju):%s:%s:%s", 1816286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 1817286196Sbapt grp ? grp->gr_name : "unknown", 1818286196Sbapt (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1), 1819286196Sbapt pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 1820286196Sbapt 1821285433Sbapt /* 1822286196Sbapt * Let's create and populate the user's home directory. Note 1823286196Sbapt * that this also `works' for editing users if -m is used, but 1824286196Sbapt * existing files will *not* be overwritten. 1825285433Sbapt */ 1826312931Sbapt if (PWALTDIR() != PWF_ALT && createhome && pwd->pw_dir && 1827286196Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 1828286196Sbapt if (!skel) 1829286196Sbapt skel = cnf->dotdir; 1830286196Sbapt if (homemode == 0) 1831286196Sbapt homemode = cnf->homemode; 1832286196Sbapt create_and_populate_homedir(cnf, pwd, skel, homemode, true); 1833286196Sbapt } 1834286196Sbapt 1835286196Sbapt if (nis && nis_update() == 0) 1836286196Sbapt pw_log(cnf, M_UPDATE, W_USER, "NIS maps updated"); 1837286196Sbapt 1838286196Sbapt return (EXIT_SUCCESS); 183920747Sdavidn} 1840