pw_user.c revision 291658
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 291658 2015-12-02 22:35:25Z bapt $"; 3130259Scharnier#endif /* not lint */ 3230259Scharnier 33286201Sbapt#include <sys/param.h> 34286201Sbapt#include <sys/resource.h> 35286201Sbapt#include <sys/time.h> 36286201Sbapt#include <sys/types.h> 37286201Sbapt 3830259Scharnier#include <ctype.h> 39286201Sbapt#include <dirent.h> 4030259Scharnier#include <err.h> 41286982Sbapt#include <errno.h> 4220253Sjoerg#include <fcntl.h> 43286201Sbapt#include <grp.h> 44286201Sbapt#include <pwd.h> 45286201Sbapt#include <libutil.h> 46286201Sbapt#include <login_cap.h> 4730259Scharnier#include <paths.h> 48286201Sbapt#include <string.h> 49286201Sbapt#include <sysexits.h> 5020253Sjoerg#include <termios.h> 51286201Sbapt#include <unistd.h> 52286201Sbapt 5320253Sjoerg#include "pw.h" 5420253Sjoerg#include "bitmap.h" 55286201Sbapt#include "psdate.h" 5620253Sjoerg 5723318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 5822394Sdavidn 5952512Sdavidnstatic char locked_str[] = "*LOCKED*"; 6024214Sache 61286196Sbaptstatic struct passwd fakeuser = { 62286196Sbapt "nouser", 63286196Sbapt "*", 64286196Sbapt -1, 65286196Sbapt -1, 66286196Sbapt 0, 67286196Sbapt "", 68286196Sbapt "User &", 69286196Sbapt "/nonexistent", 70286196Sbapt "/bin/sh", 71286196Sbapt 0, 72286196Sbapt 0 73286196Sbapt}; 7420253Sjoerg 75286196Sbaptstatic int print_user(struct passwd *pwd, bool pretty, bool v7); 76286196Sbaptstatic uid_t pw_uidpolicy(struct userconf *cnf, intmax_t id); 77286196Sbaptstatic uid_t pw_gidpolicy(struct userconf *cnf, char *grname, char *nam, 78286196Sbapt gid_t prefer, bool dryrun); 79286196Sbaptstatic char *pw_homepolicy(struct userconf * cnf, char *homedir, 80286196Sbapt const char *user); 81286196Sbaptstatic char *pw_shellpolicy(struct userconf * cnf); 82286196Sbaptstatic char *pw_password(struct userconf * cnf, char const * user, 83286196Sbapt bool dryrun); 84286196Sbaptstatic char *shell_path(char const * path, char *shells[], char *sh); 85286196Sbaptstatic void rmat(uid_t uid); 86286196Sbaptstatic void rmopie(char const * name); 87286196Sbapt 88283961Sbaptstatic void 89286982Sbaptmkdir_home_parents(int dfd, const char *dir) 90286982Sbapt{ 91286982Sbapt struct stat st; 92286982Sbapt char *dirs, *tmp; 93286982Sbapt 94286982Sbapt if (*dir != '/') 95286982Sbapt errx(EX_DATAERR, "invalid base directory for home '%s'", dir); 96286982Sbapt 97286982Sbapt dir++; 98286982Sbapt 99286982Sbapt if (fstatat(dfd, dir, &st, 0) != -1) { 100286982Sbapt if (S_ISDIR(st.st_mode)) 101286982Sbapt return; 102286982Sbapt errx(EX_OSFILE, "root home `/%s' is not a directory", dir); 103286982Sbapt } 104286982Sbapt 105286982Sbapt dirs = strdup(dir); 106286982Sbapt if (dirs == NULL) 107286982Sbapt errx(EX_UNAVAILABLE, "out of memory"); 108286982Sbapt 109286982Sbapt tmp = strrchr(dirs, '/'); 110290153Sbdrewery if (tmp == NULL) { 111290153Sbdrewery free(dirs); 112286982Sbapt return; 113290153Sbdrewery } 114286982Sbapt tmp[0] = '\0'; 115286982Sbapt 116286982Sbapt /* 117286982Sbapt * This is a kludge especially for Joerg :) 118286982Sbapt * If the home directory would be created in the root partition, then 119286982Sbapt * we really create it under /usr which is likely to have more space. 120286982Sbapt * But we create a symlink from cnf->home -> "/usr" -> cnf->home 121286982Sbapt */ 122286982Sbapt if (strchr(dirs, '/') == NULL) { 123286982Sbapt asprintf(&tmp, "usr/%s", dirs); 124286982Sbapt if (tmp == NULL) 125286982Sbapt errx(EX_UNAVAILABLE, "out of memory"); 126286982Sbapt if (mkdirat(dfd, tmp, _DEF_DIRMODE) != -1 || errno == EEXIST) { 127286982Sbapt fchownat(dfd, tmp, 0, 0, 0); 128286986Sbapt symlinkat(tmp, dfd, dirs); 129286982Sbapt } 130286982Sbapt free(tmp); 131286982Sbapt } 132286982Sbapt tmp = dirs; 133286982Sbapt if (fstatat(dfd, dirs, &st, 0) == -1) { 134286982Sbapt while ((tmp = strchr(tmp + 1, '/')) != NULL) { 135286982Sbapt *tmp = '\0'; 136286982Sbapt if (fstatat(dfd, dirs, &st, 0) == -1) { 137286982Sbapt if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1) 138286982Sbapt err(EX_OSFILE, "'%s' (root home parent) is not a directory", dirs); 139286982Sbapt } 140286982Sbapt *tmp = '/'; 141286982Sbapt } 142286982Sbapt } 143286982Sbapt if (fstatat(dfd, dirs, &st, 0) == -1) { 144286982Sbapt if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1) 145286982Sbapt err(EX_OSFILE, "'%s' (root home parent) is not a directory", dirs); 146286982Sbapt fchownat(dfd, dirs, 0, 0, 0); 147286982Sbapt } 148286982Sbapt 149286982Sbapt free(dirs); 150286982Sbapt} 151286982Sbapt 152286982Sbaptstatic void 153286196Sbaptcreate_and_populate_homedir(struct userconf *cnf, struct passwd *pwd, 154286196Sbapt const char *skeldir, mode_t homemode, bool update) 155283961Sbapt{ 156285430Sbapt int skelfd = -1; 157283961Sbapt 158286982Sbapt /* Create home parents directories */ 159286982Sbapt mkdir_home_parents(conf.rootfd, pwd->pw_dir); 160286982Sbapt 161285430Sbapt if (skeldir != NULL && *skeldir != '\0') { 162285434Sbapt if (*skeldir == '/') 163285434Sbapt skeldir++; 164285434Sbapt skelfd = openat(conf.rootfd, skeldir, O_DIRECTORY|O_CLOEXEC); 165283961Sbapt } 166283961Sbapt 167286196Sbapt copymkdir(conf.rootfd, pwd->pw_dir, skelfd, homemode, pwd->pw_uid, 168285430Sbapt pwd->pw_gid, 0); 169286196Sbapt pw_log(cnf, update ? M_UPDATE : M_ADD, W_USER, "%s(%ju) home %s made", 170286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, pwd->pw_dir); 171283961Sbapt} 172283961Sbapt 173285133Sbaptstatic int 174286196Sbaptpw_set_passwd(struct passwd *pwd, int fd, bool precrypted, bool update) 175285133Sbapt{ 176285133Sbapt int b, istty; 177285133Sbapt struct termios t, n; 178285133Sbapt login_cap_t *lc; 179285133Sbapt char line[_PASSWORD_LEN+1]; 180285133Sbapt char *p; 181285133Sbapt 182286196Sbapt if (fd == '-') { 183285133Sbapt if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 184285133Sbapt pwd->pw_passwd = "*"; /* No access */ 185285133Sbapt return (1); 186285133Sbapt } 187285133Sbapt return (0); 188285133Sbapt } 189285133Sbapt 190286196Sbapt if ((istty = isatty(fd))) { 191286196Sbapt if (tcgetattr(fd, &t) == -1) 192285133Sbapt istty = 0; 193285133Sbapt else { 194285137Sbapt n = t; 195285133Sbapt n.c_lflag &= ~(ECHO); 196286196Sbapt tcsetattr(fd, TCSANOW, &n); 197285133Sbapt printf("%s%spassword for user %s:", 198285133Sbapt update ? "new " : "", 199286196Sbapt precrypted ? "encrypted " : "", 200285133Sbapt pwd->pw_name); 201285133Sbapt fflush(stdout); 202285133Sbapt } 203285133Sbapt } 204286196Sbapt b = read(fd, line, sizeof(line) - 1); 205285133Sbapt if (istty) { /* Restore state */ 206286196Sbapt tcsetattr(fd, TCSANOW, &t); 207285133Sbapt fputc('\n', stdout); 208285133Sbapt fflush(stdout); 209285133Sbapt } 210285133Sbapt 211285133Sbapt if (b < 0) 212285133Sbapt err(EX_IOERR, "-%c file descriptor", 213286196Sbapt precrypted ? 'H' : 'h'); 214285133Sbapt line[b] = '\0'; 215285133Sbapt if ((p = strpbrk(line, "\r\n")) != NULL) 216285133Sbapt *p = '\0'; 217285133Sbapt if (!*line) 218285133Sbapt errx(EX_DATAERR, "empty password read on file descriptor %d", 219286196Sbapt fd); 220286196Sbapt if (precrypted) { 221285133Sbapt if (strchr(line, ':') != NULL) 222285137Sbapt errx(EX_DATAERR, "bad encrypted password"); 223286196Sbapt pwd->pw_passwd = strdup(line); 224285133Sbapt } else { 225285133Sbapt lc = login_getpwclass(pwd); 226285133Sbapt if (lc == NULL || 227285133Sbapt login_setcryptfmt(lc, "sha512", NULL) == NULL) 228285133Sbapt warn("setting crypt(3) format"); 229285133Sbapt login_close(lc); 230285133Sbapt pwd->pw_passwd = pw_pwcrypt(line); 231285133Sbapt } 232285133Sbapt return (1); 233285133Sbapt} 234285133Sbapt 235285405Sbaptstatic void 236286196Sbaptperform_chgpwent(const char *name, struct passwd *pwd, char *nispasswd) 237285405Sbapt{ 238285405Sbapt int rc; 239286196Sbapt struct passwd *nispwd; 240285405Sbapt 241286196Sbapt /* duplicate for nis so that chgpwent is not modifying before NIS */ 242286196Sbapt if (nispasswd && *nispasswd == '/') 243286196Sbapt nispwd = pw_dup(pwd); 244286196Sbapt 245285405Sbapt rc = chgpwent(name, pwd); 246285405Sbapt if (rc == -1) 247285405Sbapt errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name); 248285405Sbapt else if (rc != 0) 249285405Sbapt err(EX_IOERR, "passwd file update"); 250285405Sbapt 251286196Sbapt if (nispasswd && *nispasswd == '/') { 252286196Sbapt rc = chgnispwent(nispasswd, name, nispwd); 253285405Sbapt if (rc == -1) 254285405Sbapt warn("User '%s' not found in NIS passwd", pwd->pw_name); 255285984Sbapt else if (rc != 0) 256285405Sbapt warn("NIS passwd update"); 257285405Sbapt /* NOTE: NIS-only update errors are not fatal */ 258285405Sbapt } 259285405Sbapt} 260285405Sbapt 261285405Sbapt/* 262285405Sbapt * The M_LOCK and M_UNLOCK functions simply add or remove 263285405Sbapt * a "*LOCKED*" prefix from in front of the password to 264285405Sbapt * prevent it decoding correctly, and therefore prevents 265285405Sbapt * access. Of course, this only prevents access via 266285405Sbapt * password authentication (not ssh, kerberos or any 267285405Sbapt * other method that does not use the UNIX password) but 268285405Sbapt * that is a known limitation. 269285405Sbapt */ 270285405Sbaptstatic int 271286196Sbaptpw_userlock(char *arg1, int mode) 272285405Sbapt{ 273285405Sbapt struct passwd *pwd = NULL; 274285405Sbapt char *passtmp = NULL; 275286196Sbapt char *name; 276285405Sbapt bool locked = false; 277291657Sbapt uid_t id = (uid_t)-1; 278285405Sbapt 279286196Sbapt if (geteuid() != 0) 280286196Sbapt errx(EX_NOPERM, "you must be root"); 281286196Sbapt 282286196Sbapt if (arg1 == NULL) 283285405Sbapt errx(EX_DATAERR, "username or id required"); 284285405Sbapt 285291657Sbapt name = arg1; 286291657Sbapt if (arg1[strspn(name, "0123456789")] == '\0') 287291657Sbapt id = pw_checkid(name, UID_MAX); 288286196Sbapt 289291657Sbapt pwd = GETPWNAM(pw_checkname(name, 0)); 290291657Sbapt if (pwd == NULL && id != (uid_t)-1) { 291291657Sbapt pwd = GETPWUID(id); 292291657Sbapt if (pwd != NULL) 293291657Sbapt name = pwd->pw_name; 294291657Sbapt } 295285405Sbapt if (pwd == NULL) { 296291657Sbapt if (id == (uid_t)-1) 297291657Sbapt errx(EX_NOUSER, "no such name or uid `%ju'", (uintmax_t) id); 298285405Sbapt errx(EX_NOUSER, "no such user `%s'", name); 299285405Sbapt } 300285405Sbapt 301285405Sbapt if (name == NULL) 302285405Sbapt name = pwd->pw_name; 303285405Sbapt 304285405Sbapt if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str) -1) == 0) 305285405Sbapt locked = true; 306285405Sbapt if (mode == M_LOCK && locked) 307285405Sbapt errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 308285405Sbapt if (mode == M_UNLOCK && !locked) 309285405Sbapt errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 310285405Sbapt 311285405Sbapt if (mode == M_LOCK) { 312285405Sbapt asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 313285405Sbapt if (passtmp == NULL) /* disaster */ 314285405Sbapt errx(EX_UNAVAILABLE, "out of memory"); 315285405Sbapt pwd->pw_passwd = passtmp; 316285405Sbapt } else { 317285405Sbapt pwd->pw_passwd += sizeof(locked_str)-1; 318285405Sbapt } 319285405Sbapt 320286196Sbapt perform_chgpwent(name, pwd, NULL); 321285405Sbapt free(passtmp); 322285405Sbapt 323285405Sbapt return (EXIT_SUCCESS); 324285405Sbapt} 325285405Sbapt 326286196Sbaptstatic uid_t 327286196Sbaptpw_uidpolicy(struct userconf * cnf, intmax_t id) 32820253Sjoerg{ 329286196Sbapt struct passwd *pwd; 330286196Sbapt struct bitmap bm; 331286196Sbapt uid_t uid = (uid_t) - 1; 33220253Sjoerg 33320267Sjoerg /* 334286196Sbapt * Check the given uid, if any 33520253Sjoerg */ 336286196Sbapt if (id >= 0) { 337286196Sbapt uid = (uid_t) id; 33820253Sjoerg 339286196Sbapt if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate) 340286196Sbapt errx(EX_DATAERR, "uid `%ju' has already been allocated", 341286196Sbapt (uintmax_t)pwd->pw_uid); 342286196Sbapt return (uid); 34320253Sjoerg } 34421052Sdavidn /* 345286196Sbapt * We need to allocate the next available uid under one of 346286196Sbapt * two policies a) Grab the first unused uid b) Grab the 347286196Sbapt * highest possible unused uid 34821052Sdavidn */ 349286196Sbapt if (cnf->min_uid >= cnf->max_uid) { /* Sanity 350286196Sbapt * claus^H^H^H^Hheck */ 351286196Sbapt cnf->min_uid = 1000; 352286196Sbapt cnf->max_uid = 32000; 35321052Sdavidn } 354286196Sbapt bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 35521052Sdavidn 35620253Sjoerg /* 357286196Sbapt * Now, let's fill the bitmap from the password file 35820253Sjoerg */ 359286196Sbapt SETPWENT(); 360286196Sbapt while ((pwd = GETPWENT()) != NULL) 361286196Sbapt if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 362286196Sbapt bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 363286196Sbapt ENDPWENT(); 36452527Sdavidn 36520253Sjoerg /* 366286196Sbapt * Then apply the policy, with fallback to reuse if necessary 36720253Sjoerg */ 368286196Sbapt if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 369286196Sbapt uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 37020253Sjoerg 37120267Sjoerg /* 372286196Sbapt * Another sanity check 37320267Sjoerg */ 374286196Sbapt if (uid < cnf->min_uid || uid > cnf->max_uid) 375286196Sbapt errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 376286196Sbapt bm_dealloc(&bm); 377286196Sbapt return (uid); 37820253Sjoerg} 37920253Sjoerg 380286196Sbaptstatic uid_t 381286196Sbaptpw_gidpolicy(struct userconf *cnf, char *grname, char *nam, gid_t prefer, bool dryrun) 38220253Sjoerg{ 38320253Sjoerg struct group *grp; 38420253Sjoerg gid_t gid = (uid_t) - 1; 38520253Sjoerg 38620253Sjoerg /* 38720253Sjoerg * Check the given gid, if any 38820253Sjoerg */ 38944229Sdavidn SETGRENT(); 390286196Sbapt if (grname) { 391286196Sbapt if ((grp = GETGRNAM(grname)) == NULL) { 392286196Sbapt gid = pw_checkid(grname, GID_MAX); 393286196Sbapt grp = GETGRGID(gid); 39420253Sjoerg } 39520253Sjoerg gid = grp->gr_gid; 396262865Sjulian } else if ((grp = GETGRNAM(nam)) != NULL && 397262865Sjulian (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 39820267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 39920253Sjoerg } else { 400286196Sbapt intmax_t grid = -1; 40120253Sjoerg 40220253Sjoerg /* 40320253Sjoerg * We need to auto-create a group with the user's name. We 40420253Sjoerg * can send all the appropriate output to our sister routine 40520253Sjoerg * bit first see if we can create a group with gid==uid so we 40620253Sjoerg * can keep the user and group ids in sync. We purposely do 40720253Sjoerg * NOT check the gid range if we can force the sync. If the 40820253Sjoerg * user's name dups an existing group, then the group add 40920253Sjoerg * function will happily handle that case for us and exit. 41020253Sjoerg */ 411285414Sbapt if (GETGRGID(prefer) == NULL) 412285414Sbapt grid = prefer; 413286196Sbapt if (dryrun) { 414285395Sbapt gid = pw_groupnext(cnf, true); 415285395Sbapt } else { 416286196Sbapt if (grid == -1) 417286196Sbapt grid = pw_groupnext(cnf, true); 418286196Sbapt groupadd(cnf, nam, grid, NULL, -1, false, false, false); 41944229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 42020267Sjoerg gid = grp->gr_gid; 42120267Sjoerg } 42220253Sjoerg } 42344229Sdavidn ENDGRENT(); 424286196Sbapt return (gid); 42520253Sjoerg} 42620253Sjoerg 427286196Sbaptstatic char * 428286196Sbaptpw_homepolicy(struct userconf * cnf, char *homedir, const char *user) 42920253Sjoerg{ 430282699Sbapt static char home[128]; 43120253Sjoerg 432286196Sbapt if (homedir) 433286196Sbapt return (homedir); 43420253Sjoerg 435282699Sbapt if (cnf->home == NULL || *cnf->home == '\0') 436282699Sbapt errx(EX_CONFIG, "no base home directory set"); 437282699Sbapt snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 438282699Sbapt 439282699Sbapt return (home); 44020253Sjoerg} 44120253Sjoerg 442286196Sbaptstatic char * 44320253Sjoergshell_path(char const * path, char *shells[], char *sh) 44420253Sjoerg{ 44520253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 44620253Sjoerg return sh; /* specified full path or forced none */ 44720253Sjoerg else { 44820253Sjoerg char *p; 44920253Sjoerg char paths[_UC_MAXLINE]; 45020253Sjoerg 45120253Sjoerg /* 45220253Sjoerg * We need to search paths 45320253Sjoerg */ 454130633Srobert strlcpy(paths, path, sizeof(paths)); 45520253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 45620253Sjoerg int i; 45720253Sjoerg static char shellpath[256]; 45820253Sjoerg 45920253Sjoerg if (sh != NULL) { 460282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); 46120253Sjoerg if (access(shellpath, X_OK) == 0) 46220253Sjoerg return shellpath; 46320253Sjoerg } else 46420253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 465282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); 46620253Sjoerg if (access(shellpath, X_OK) == 0) 46720253Sjoerg return shellpath; 46820253Sjoerg } 46920253Sjoerg } 47020253Sjoerg if (sh == NULL) 47130259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 47230259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 47320253Sjoerg return NULL; 47420253Sjoerg } 47520253Sjoerg} 47620253Sjoerg 477286196Sbaptstatic char * 478286196Sbaptpw_shellpolicy(struct userconf * cnf) 47920253Sjoerg{ 48020253Sjoerg 481286196Sbapt return shell_path(cnf->shelldir, cnf->shells, cnf->shell_default); 48220253Sjoerg} 48320253Sjoerg 484179365Santoine#define SALTSIZE 32 48520253Sjoerg 486179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 487179365Santoine 488286196Sbaptchar * 48920253Sjoergpw_pwcrypt(char *password) 49020253Sjoerg{ 49120253Sjoerg int i; 492179365Santoine char salt[SALTSIZE + 1]; 493231994Skevlo char *cryptpw; 49420253Sjoerg static char buf[256]; 49520253Sjoerg 49620253Sjoerg /* 49720253Sjoerg * Calculate a salt value 49820253Sjoerg */ 499179365Santoine for (i = 0; i < SALTSIZE; i++) 500181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 501179365Santoine salt[SALTSIZE] = '\0'; 50220253Sjoerg 503231994Skevlo cryptpw = crypt(password, salt); 504231994Skevlo if (cryptpw == NULL) 505231994Skevlo errx(EX_CONFIG, "crypt(3) failure"); 506231994Skevlo return strcpy(buf, cryptpw); 50720253Sjoerg} 50820253Sjoerg 509286196Sbaptstatic char * 510286196Sbaptpw_password(struct userconf * cnf, char const * user, bool dryrun) 51120253Sjoerg{ 51220253Sjoerg int i, l; 51320253Sjoerg char pwbuf[32]; 51420253Sjoerg 51520253Sjoerg switch (cnf->default_password) { 51620253Sjoerg case -1: /* Random password */ 51773563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 51820253Sjoerg for (i = 0; i < l; i++) 519181785Sache pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 52020253Sjoerg pwbuf[i] = '\0'; 52120253Sjoerg 52220253Sjoerg /* 52320253Sjoerg * We give this information back to the user 52420253Sjoerg */ 525286196Sbapt if (conf.fd == -1 && !dryrun) { 52661957Sache if (isatty(STDOUT_FILENO)) 52720712Sdavidn printf("Password for '%s' is: ", user); 52820253Sjoerg printf("%s\n", pwbuf); 52920253Sjoerg fflush(stdout); 53020253Sjoerg } 53120253Sjoerg break; 53220253Sjoerg 53320253Sjoerg case -2: /* No password at all! */ 53420253Sjoerg return ""; 53520253Sjoerg 53620253Sjoerg case 0: /* No login - default */ 53720253Sjoerg default: 53820253Sjoerg return "*"; 53920253Sjoerg 54020253Sjoerg case 1: /* user's name */ 541130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 54220253Sjoerg break; 54320253Sjoerg } 54420253Sjoerg return pw_pwcrypt(pwbuf); 54520253Sjoerg} 54620253Sjoerg 547284111Sbaptstatic int 548286196Sbaptprint_user(struct passwd * pwd, bool pretty, bool v7) 549284111Sbapt{ 550286196Sbapt int j; 551286196Sbapt char *p; 552286196Sbapt struct group *grp = GETGRGID(pwd->pw_gid); 553286196Sbapt char uname[60] = "User &", office[60] = "[None]", 554286196Sbapt wphone[60] = "[None]", hphone[60] = "[None]"; 555286196Sbapt char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 556286196Sbapt struct tm * tptr; 557286196Sbapt 558286196Sbapt if (!pretty) { 559286196Sbapt p = v7 ? pw_make_v7(pwd) : pw_make(pwd); 560286196Sbapt printf("%s\n", p); 561286196Sbapt free(p); 562286196Sbapt return (EXIT_SUCCESS); 563286196Sbapt } 564286196Sbapt 565286196Sbapt if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 566286196Sbapt strlcpy(uname, p, sizeof(uname)); 567286196Sbapt if ((p = strtok(NULL, ",")) != NULL) { 568286196Sbapt strlcpy(office, p, sizeof(office)); 569286196Sbapt if ((p = strtok(NULL, ",")) != NULL) { 570286196Sbapt strlcpy(wphone, p, sizeof(wphone)); 571286196Sbapt if ((p = strtok(NULL, "")) != NULL) { 572286196Sbapt strlcpy(hphone, p, sizeof(hphone)); 573286196Sbapt } 574286196Sbapt } 575286196Sbapt } 576286196Sbapt } 577286196Sbapt /* 578286196Sbapt * Handle '&' in gecos field 579286196Sbapt */ 580286196Sbapt if ((p = strchr(uname, '&')) != NULL) { 581286196Sbapt int l = strlen(pwd->pw_name); 582286196Sbapt int m = strlen(p); 583286196Sbapt 584286196Sbapt memmove(p + l, p + 1, m); 585286196Sbapt memmove(p, pwd->pw_name, l); 586286196Sbapt *p = (char) toupper((unsigned char)*p); 587286196Sbapt } 588286196Sbapt if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 589286196Sbapt strftime(acexpire, sizeof acexpire, "%c", tptr); 590286196Sbapt if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 591286196Sbapt strftime(pwexpire, sizeof pwexpire, "%c", tptr); 592286196Sbapt printf("Login Name: %-15s #%-12ju Group: %-15s #%ju\n" 593286196Sbapt " Full Name: %s\n" 594286196Sbapt " Home: %-26.26s Class: %s\n" 595286196Sbapt " Shell: %-26.26s Office: %s\n" 596286196Sbapt "Work Phone: %-26.26s Home Phone: %s\n" 597286196Sbapt "Acc Expire: %-26.26s Pwd Expire: %s\n", 598286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 599286196Sbapt grp ? grp->gr_name : "(invalid)", (uintmax_t)pwd->pw_gid, 600286196Sbapt uname, pwd->pw_dir, pwd->pw_class, 601286196Sbapt pwd->pw_shell, office, wphone, hphone, 602286196Sbapt acexpire, pwexpire); 603286196Sbapt SETGRENT(); 604286196Sbapt j = 0; 605286196Sbapt while ((grp=GETGRENT()) != NULL) { 606286196Sbapt int i = 0; 607286196Sbapt if (grp->gr_mem != NULL) { 608286196Sbapt while (grp->gr_mem[i] != NULL) { 609286196Sbapt if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) { 610286196Sbapt printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 611286196Sbapt break; 612286196Sbapt } 613286196Sbapt ++i; 614286196Sbapt } 615286196Sbapt } 616286196Sbapt } 617286196Sbapt ENDGRENT(); 618286196Sbapt printf("%s", j ? "\n" : ""); 619286196Sbapt return (EXIT_SUCCESS); 620286196Sbapt} 621286196Sbapt 622286196Sbaptchar * 623286196Sbaptpw_checkname(char *name, int gecos) 624286196Sbapt{ 625286196Sbapt char showch[8]; 626286196Sbapt const char *badchars, *ch, *showtype; 627286196Sbapt int reject; 628286196Sbapt 629286196Sbapt ch = name; 630286196Sbapt reject = 0; 631286196Sbapt if (gecos) { 632286196Sbapt /* See if the name is valid as a gecos (comment) field. */ 633286196Sbapt badchars = ":!@"; 634286196Sbapt showtype = "gecos field"; 635286196Sbapt } else { 636286196Sbapt /* See if the name is valid as a userid or group. */ 637286196Sbapt badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 638286196Sbapt showtype = "userid/group name"; 639286196Sbapt /* Userids and groups can not have a leading '-'. */ 640286196Sbapt if (*ch == '-') 641286196Sbapt reject = 1; 642286196Sbapt } 643286196Sbapt if (!reject) { 644286196Sbapt while (*ch) { 645291658Sbapt if (strchr(badchars, *ch) != NULL || 646291658Sbapt (!gecos && *ch < ' ') || 647286196Sbapt *ch == 127) { 648286196Sbapt reject = 1; 649286196Sbapt break; 650286196Sbapt } 651286196Sbapt /* 8-bit characters are only allowed in GECOS fields */ 652286196Sbapt if (!gecos && (*ch & 0x80)) { 653286196Sbapt reject = 1; 654286196Sbapt break; 655286196Sbapt } 656286196Sbapt ch++; 657286196Sbapt } 658286196Sbapt } 659286196Sbapt /* 660286196Sbapt * A `$' is allowed as the final character for userids and groups, 661286196Sbapt * mainly for the benefit of samba. 662286196Sbapt */ 663286196Sbapt if (reject && !gecos) { 664286196Sbapt if (*ch == '$' && *(ch + 1) == '\0') { 665286196Sbapt reject = 0; 666286196Sbapt ch++; 667286196Sbapt } 668286196Sbapt } 669286196Sbapt if (reject) { 670286196Sbapt snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 671286196Sbapt ? "`%c'" : "0x%02x", *ch); 672286196Sbapt errx(EX_DATAERR, "invalid character %s at position %td in %s", 673286196Sbapt showch, (ch - name), showtype); 674286196Sbapt } 675286196Sbapt if (!gecos && (ch - name) > LOGNAMESIZE) 676286196Sbapt errx(EX_USAGE, "name too long `%s' (max is %d)", name, 677286196Sbapt LOGNAMESIZE); 678286196Sbapt 679286196Sbapt return (name); 680286196Sbapt} 681286196Sbapt 682286196Sbaptstatic void 683286196Sbaptrmat(uid_t uid) 684286196Sbapt{ 685286196Sbapt DIR *d = opendir("/var/at/jobs"); 686286196Sbapt 687286196Sbapt if (d != NULL) { 688286196Sbapt struct dirent *e; 689286196Sbapt 690286196Sbapt while ((e = readdir(d)) != NULL) { 691286196Sbapt struct stat st; 692286196Sbapt 693286196Sbapt if (strncmp(e->d_name, ".lock", 5) != 0 && 694286196Sbapt stat(e->d_name, &st) == 0 && 695286196Sbapt !S_ISDIR(st.st_mode) && 696286196Sbapt st.st_uid == uid) { 697286196Sbapt char tmp[MAXPATHLEN]; 698286196Sbapt 699286202Sbapt snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", 700286202Sbapt e->d_name); 701286196Sbapt system(tmp); 702286196Sbapt } 703286196Sbapt } 704286196Sbapt closedir(d); 705286196Sbapt } 706286196Sbapt} 707286196Sbapt 708286196Sbaptstatic void 709286196Sbaptrmopie(char const * name) 710286196Sbapt{ 711286196Sbapt char tmp[1014]; 712286196Sbapt FILE *fp; 713286196Sbapt int fd; 714286196Sbapt size_t len; 715286196Sbapt off_t atofs = 0; 716286196Sbapt 717286196Sbapt if ((fd = openat(conf.rootfd, "etc/opiekeys", O_RDWR)) == -1) 718286196Sbapt return; 719286196Sbapt 720286196Sbapt fp = fdopen(fd, "r+"); 721286196Sbapt len = strlen(name); 722286196Sbapt 723286196Sbapt while (fgets(tmp, sizeof(tmp), fp) != NULL) { 724286196Sbapt if (strncmp(name, tmp, len) == 0 && tmp[len]==' ') { 725286196Sbapt /* Comment username out */ 726286196Sbapt if (fseek(fp, atofs, SEEK_SET) == 0) 727286196Sbapt fwrite("#", 1, 1, fp); 728286196Sbapt break; 729286196Sbapt } 730286196Sbapt atofs = ftell(fp); 731286196Sbapt } 732286196Sbapt /* 733286196Sbapt * If we got an error of any sort, don't update! 734286196Sbapt */ 735286196Sbapt fclose(fp); 736286196Sbapt} 737286196Sbapt 738286196Sbaptint 739286196Sbaptpw_user_next(int argc, char **argv, char *name __unused) 740286196Sbapt{ 741286196Sbapt struct userconf *cnf = NULL; 742286196Sbapt const char *cfg = NULL; 743286196Sbapt int ch; 744286196Sbapt bool quiet = false; 745286196Sbapt uid_t next; 746286196Sbapt 747286196Sbapt while ((ch = getopt(argc, argv, "Cq")) != -1) { 748286196Sbapt switch (ch) { 749286196Sbapt case 'C': 750286196Sbapt cfg = optarg; 751286196Sbapt break; 752286196Sbapt case 'q': 753286217Sadrian quiet = true; 754286196Sbapt break; 755286196Sbapt } 756286196Sbapt } 757286196Sbapt 758286196Sbapt if (quiet) 759286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 760286196Sbapt 761286196Sbapt cnf = get_userconfig(cfg); 762286196Sbapt 763286196Sbapt next = pw_uidpolicy(cnf, -1); 764286196Sbapt 765286196Sbapt printf("%ju:", (uintmax_t)next); 766286196Sbapt pw_groupnext(cnf, quiet); 767286196Sbapt 768286196Sbapt return (EXIT_SUCCESS); 769286196Sbapt} 770286196Sbapt 771286196Sbaptint 772286196Sbaptpw_user_show(int argc, char **argv, char *arg1) 773286196Sbapt{ 774285401Sbapt struct passwd *pwd = NULL; 775286196Sbapt char *name = NULL; 776286218Sbapt intmax_t id = -1; 777286196Sbapt int ch; 778286196Sbapt bool all = false; 779286196Sbapt bool pretty = false; 780286196Sbapt bool force = false; 781286196Sbapt bool v7 = false; 782286196Sbapt bool quiet = false; 78320253Sjoerg 784286196Sbapt if (arg1 != NULL) { 785286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 786286196Sbapt id = pw_checkid(arg1, UID_MAX); 787286196Sbapt else 788286196Sbapt name = arg1; 789286196Sbapt } 790286196Sbapt 791286196Sbapt while ((ch = getopt(argc, argv, "C:qn:u:FPa7")) != -1) { 792286196Sbapt switch (ch) { 793286196Sbapt case 'C': 794286196Sbapt /* ignore compatibility */ 795286196Sbapt break; 796286196Sbapt case 'q': 797286196Sbapt quiet = true; 798286196Sbapt break; 799286196Sbapt case 'n': 800286196Sbapt name = optarg; 801286196Sbapt break; 802286196Sbapt case 'u': 803286196Sbapt id = pw_checkid(optarg, UID_MAX); 804286196Sbapt break; 805286196Sbapt case 'F': 806286196Sbapt force = true; 807286196Sbapt break; 808286196Sbapt case 'P': 809286196Sbapt pretty = true; 810286196Sbapt break; 811286196Sbapt case 'a': 812286196Sbapt all = true; 813286196Sbapt break; 814287799Sbapt case '7': 815286196Sbapt v7 = true; 816286196Sbapt break; 817286196Sbapt } 818286196Sbapt } 819286196Sbapt 820286196Sbapt if (quiet) 821286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 822286196Sbapt 823286196Sbapt if (all) { 824286196Sbapt SETPWENT(); 825286196Sbapt while ((pwd = GETPWENT()) != NULL) 826286196Sbapt print_user(pwd, pretty, v7); 827286196Sbapt ENDPWENT(); 828286196Sbapt return (EXIT_SUCCESS); 829286196Sbapt } 830286196Sbapt 831285401Sbapt if (id < 0 && name == NULL) 832285401Sbapt errx(EX_DATAERR, "username or id required"); 833285401Sbapt 834285401Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 835285401Sbapt if (pwd == NULL) { 836286196Sbapt if (force) { 837286196Sbapt pwd = &fakeuser; 838286196Sbapt } else { 839286196Sbapt if (name == NULL) 840286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", 841286196Sbapt (uintmax_t) id); 842286196Sbapt errx(EX_NOUSER, "no such user `%s'", name); 843286196Sbapt } 844286196Sbapt } 845286196Sbapt 846286196Sbapt return (print_user(pwd, pretty, v7)); 847286196Sbapt} 848286196Sbapt 849286196Sbaptint 850286196Sbaptpw_user_del(int argc, char **argv, char *arg1) 851286196Sbapt{ 852286196Sbapt struct userconf *cnf = NULL; 853286196Sbapt struct passwd *pwd = NULL; 854286196Sbapt struct group *gr, *grp; 855286196Sbapt char *name = NULL; 856286196Sbapt char grname[MAXLOGNAME]; 857286196Sbapt char *nispasswd = NULL; 858286196Sbapt char file[MAXPATHLEN]; 859286196Sbapt char home[MAXPATHLEN]; 860286196Sbapt const char *cfg = NULL; 861286196Sbapt struct stat st; 862286218Sbapt intmax_t id = -1; 863286196Sbapt int ch, rc; 864286196Sbapt bool nis = false; 865286196Sbapt bool deletehome = false; 866286196Sbapt bool quiet = false; 867286196Sbapt 868286196Sbapt if (arg1 != NULL) { 869286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 870286196Sbapt id = pw_checkid(arg1, UID_MAX); 871286196Sbapt else 872286196Sbapt name = arg1; 873286196Sbapt } 874286196Sbapt 875286196Sbapt while ((ch = getopt(argc, argv, "C:qn:u:rYy:")) != -1) { 876286196Sbapt switch (ch) { 877286196Sbapt case 'C': 878286196Sbapt cfg = optarg; 879286196Sbapt break; 880286196Sbapt case 'q': 881286196Sbapt quiet = true; 882286196Sbapt break; 883286196Sbapt case 'n': 884286196Sbapt name = optarg; 885286196Sbapt break; 886286196Sbapt case 'u': 887286196Sbapt id = pw_checkid(optarg, UID_MAX); 888286196Sbapt break; 889286196Sbapt case 'r': 890286196Sbapt deletehome = true; 891286196Sbapt break; 892286196Sbapt case 'y': 893286196Sbapt nispasswd = optarg; 894286196Sbapt break; 895286196Sbapt case 'Y': 896286196Sbapt nis = true; 897286196Sbapt break; 898286196Sbapt } 899286196Sbapt } 900286196Sbapt 901286196Sbapt if (quiet) 902286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 903286196Sbapt 904286196Sbapt if (id < 0 && name == NULL) 905286196Sbapt errx(EX_DATAERR, "username or id required"); 906286196Sbapt 907286196Sbapt cnf = get_userconfig(cfg); 908286196Sbapt 909286196Sbapt if (nispasswd == NULL) 910286196Sbapt nispasswd = cnf->nispasswd; 911286196Sbapt 912286196Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 913286196Sbapt if (pwd == NULL) { 914285401Sbapt if (name == NULL) 915286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", (uintmax_t) id); 916285401Sbapt errx(EX_NOUSER, "no such user `%s'", name); 917285401Sbapt } 918285989Sbapt 919285989Sbapt if (PWF._altdir == PWF_REGULAR && 920286196Sbapt ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) { 921286196Sbapt if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 922286196Sbapt if (!nis && nispasswd && *nispasswd != '/') 923286196Sbapt errx(EX_NOUSER, "Cannot remove NIS user `%s'", 924286196Sbapt name); 925286196Sbapt } else { 926286196Sbapt errx(EX_NOUSER, "Cannot remove non local user `%s'", 927286196Sbapt name); 928286196Sbapt } 929286196Sbapt } 930285989Sbapt 931286196Sbapt id = pwd->pw_uid; 932285401Sbapt if (name == NULL) 933285401Sbapt name = pwd->pw_name; 934285401Sbapt 935284111Sbapt if (strcmp(pwd->pw_name, "root") == 0) 936284111Sbapt errx(EX_DATAERR, "cannot remove user 'root'"); 937284111Sbapt 938286196Sbapt /* Remove opie record from /etc/opiekeys */ 939285433Sbapt if (PWALTDIR() != PWF_ALT) 940284111Sbapt rmopie(pwd->pw_name); 941284111Sbapt 942285433Sbapt if (!PWALTDIR()) { 943285433Sbapt /* Remove crontabs */ 944284111Sbapt snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 945284111Sbapt if (access(file, F_OK) == 0) { 946286202Sbapt snprintf(file, sizeof(file), "crontab -u %s -r", 947286202Sbapt pwd->pw_name); 948284111Sbapt system(file); 949284111Sbapt } 950284111Sbapt } 951286196Sbapt 952284111Sbapt /* 953284111Sbapt * Save these for later, since contents of pwd may be 954284111Sbapt * invalidated by deletion 955284111Sbapt */ 956284111Sbapt snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 957284111Sbapt strlcpy(home, pwd->pw_dir, sizeof(home)); 958284111Sbapt gr = GETGRGID(pwd->pw_gid); 959284111Sbapt if (gr != NULL) 960284111Sbapt strlcpy(grname, gr->gr_name, LOGNAMESIZE); 961284111Sbapt else 962284111Sbapt grname[0] = '\0'; 963284111Sbapt 964284111Sbapt rc = delpwent(pwd); 965284111Sbapt if (rc == -1) 966284111Sbapt err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 967284111Sbapt else if (rc != 0) 968284111Sbapt err(EX_IOERR, "passwd update"); 969284111Sbapt 970286196Sbapt if (nis && nispasswd && *nispasswd=='/') { 971286196Sbapt rc = delnispwent(nispasswd, name); 972284111Sbapt if (rc == -1) 973285401Sbapt warnx("WARNING: user '%s' does not exist in NIS passwd", 974285401Sbapt pwd->pw_name); 975284111Sbapt else if (rc != 0) 976284111Sbapt warn("WARNING: NIS passwd update"); 977284111Sbapt } 978284111Sbapt 979284128Sbapt grp = GETGRNAM(name); 980284111Sbapt if (grp != NULL && 981284111Sbapt (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 982284128Sbapt strcmp(name, grname) == 0) 983284128Sbapt delgrent(GETGRNAM(name)); 984284111Sbapt SETGRENT(); 985284111Sbapt while ((grp = GETGRENT()) != NULL) { 986284111Sbapt int i, j; 987284111Sbapt char group[MAXLOGNAME]; 988284113Sbapt if (grp->gr_mem == NULL) 989284113Sbapt continue; 990284113Sbapt 991284113Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 992284128Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 993284113Sbapt continue; 994284113Sbapt 995284113Sbapt for (j = i; grp->gr_mem[j] != NULL; j++) 996284113Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 997284113Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 998284113Sbapt chggrent(group, grp); 999284111Sbapt } 1000284111Sbapt } 1001284111Sbapt ENDGRENT(); 1002284111Sbapt 1003286196Sbapt pw_log(cnf, M_DELETE, W_USER, "%s(%ju) account removed", name, 1004286196Sbapt (uintmax_t)id); 1005284111Sbapt 1006285433Sbapt /* Remove mail file */ 1007285433Sbapt if (PWALTDIR() != PWF_ALT) 1008285433Sbapt unlinkat(conf.rootfd, file + 1, 0); 1009284111Sbapt 1010286196Sbapt /* Remove at jobs */ 1011286196Sbapt if (!PWALTDIR() && getpwuid(id) == NULL) 1012286196Sbapt rmat(id); 1013284111Sbapt 1014285433Sbapt /* Remove home directory and contents */ 1015286196Sbapt if (PWALTDIR() != PWF_ALT && deletehome && *home == '/' && 1016286196Sbapt GETPWUID(id) == NULL && 1017285433Sbapt fstatat(conf.rootfd, home + 1, &st, 0) != -1) { 1018286196Sbapt rm_r(conf.rootfd, home, id); 1019286196Sbapt pw_log(cnf, M_DELETE, W_USER, "%s(%ju) home '%s' %s" 1020286196Sbapt "removed", name, (uintmax_t)id, home, 1021285433Sbapt fstatat(conf.rootfd, home + 1, &st, 0) == -1 ? "" : "not " 1022285433Sbapt "completely "); 1023284111Sbapt } 1024284111Sbapt 1025284111Sbapt return (EXIT_SUCCESS); 1026284111Sbapt} 1027284111Sbapt 1028286196Sbaptint 1029286196Sbaptpw_user_lock(int argc, char **argv, char *arg1) 103020253Sjoerg{ 1031286196Sbapt int ch; 103220253Sjoerg 1033286196Sbapt while ((ch = getopt(argc, argv, "Cq")) != -1) { 1034286196Sbapt switch (ch) { 1035286196Sbapt case 'C': 1036286196Sbapt case 'q': 1037286196Sbapt /* compatibility */ 1038286196Sbapt break; 103920253Sjoerg } 1040286196Sbapt } 104120253Sjoerg 1042286196Sbapt return (pw_userlock(arg1, M_LOCK)); 1043286196Sbapt} 1044286196Sbapt 1045286196Sbaptint 1046286196Sbaptpw_user_unlock(int argc, char **argv, char *arg1) 1047286196Sbapt{ 1048286196Sbapt int ch; 1049286196Sbapt 1050286196Sbapt while ((ch = getopt(argc, argv, "Cq")) != -1) { 1051286196Sbapt switch (ch) { 1052286196Sbapt case 'C': 1053286196Sbapt case 'q': 1054286196Sbapt /* compatibility */ 1055286196Sbapt break; 105620253Sjoerg } 105720253Sjoerg } 1058286196Sbapt 1059286196Sbapt return (pw_userlock(arg1, M_UNLOCK)); 106020253Sjoerg} 106120253Sjoerg 1062286196Sbaptstatic struct group * 1063286196Sbaptgroup_from_name_or_id(char *name) 106420253Sjoerg{ 1065286196Sbapt const char *errstr = NULL; 1066286196Sbapt struct group *grp; 1067286196Sbapt uintmax_t id; 106820253Sjoerg 1069286196Sbapt if ((grp = GETGRNAM(name)) == NULL) { 1070286196Sbapt id = strtounum(name, 0, GID_MAX, &errstr); 1071286196Sbapt if (errstr) 1072286196Sbapt errx(EX_NOUSER, "group `%s' does not exist", name); 1073286196Sbapt grp = GETGRGID(id); 1074286196Sbapt if (grp == NULL) 1075286196Sbapt errx(EX_NOUSER, "group `%s' does not exist", name); 107620253Sjoerg } 1077286196Sbapt 1078286196Sbapt return (grp); 1079286196Sbapt} 1080286196Sbapt 1081286196Sbaptstatic void 1082286196Sbaptsplit_groups(StringList **groups, char *groupsstr) 1083286196Sbapt{ 1084286196Sbapt struct group *grp; 1085286196Sbapt char *p; 1086286196Sbapt char tok[] = ", \t"; 1087286196Sbapt 1088286196Sbapt for (p = strtok(groupsstr, tok); p != NULL; p = strtok(NULL, tok)) { 1089286196Sbapt grp = group_from_name_or_id(p); 1090286196Sbapt if (*groups == NULL) 1091286196Sbapt *groups = sl_init(); 1092286196Sbapt sl_add(*groups, newstr(grp->gr_name)); 1093286196Sbapt } 1094286196Sbapt} 1095286196Sbapt 1096286196Sbaptstatic void 1097286196Sbaptvalidate_grname(struct userconf *cnf, char *group) 1098286196Sbapt{ 1099286196Sbapt struct group *grp; 1100286196Sbapt 1101286196Sbapt if (group == NULL || *group == '\0') { 1102286196Sbapt cnf->default_group = ""; 1103286196Sbapt return; 1104286196Sbapt } 1105286196Sbapt grp = group_from_name_or_id(group); 1106286196Sbapt cnf->default_group = newstr(grp->gr_name); 1107286196Sbapt} 1108286196Sbapt 1109286196Sbaptstatic mode_t 1110286196Sbaptvalidate_mode(char *mode) 1111286196Sbapt{ 1112286196Sbapt mode_t m; 1113286196Sbapt void *set; 1114286196Sbapt 1115286196Sbapt if ((set = setmode(mode)) == NULL) 1116286196Sbapt errx(EX_DATAERR, "invalid directory creation mode '%s'", mode); 1117286196Sbapt 1118286196Sbapt m = getmode(set, _DEF_DIRMODE); 1119286196Sbapt free(set); 1120286196Sbapt return (m); 1121286196Sbapt} 1122286196Sbapt 1123286196Sbaptstatic void 1124286196Sbaptmix_config(struct userconf *cmdcnf, struct userconf *cfg) 1125286196Sbapt{ 1126286196Sbapt 1127286196Sbapt if (cmdcnf->default_password == 0) 1128286196Sbapt cmdcnf->default_password = cfg->default_password; 1129286196Sbapt if (cmdcnf->reuse_uids == 0) 1130286196Sbapt cmdcnf->reuse_uids = cfg->reuse_uids; 1131286196Sbapt if (cmdcnf->reuse_gids == 0) 1132286196Sbapt cmdcnf->reuse_gids = cfg->reuse_gids; 1133286196Sbapt if (cmdcnf->nispasswd == NULL) 1134286196Sbapt cmdcnf->nispasswd = cfg->nispasswd; 1135286196Sbapt if (cmdcnf->dotdir == NULL) 1136286196Sbapt cmdcnf->dotdir = cfg->dotdir; 1137286196Sbapt if (cmdcnf->newmail == NULL) 1138286196Sbapt cmdcnf->newmail = cfg->newmail; 1139286196Sbapt if (cmdcnf->logfile == NULL) 1140286196Sbapt cmdcnf->logfile = cfg->logfile; 1141286196Sbapt if (cmdcnf->home == NULL) 1142286196Sbapt cmdcnf->home = cfg->home; 1143286196Sbapt if (cmdcnf->homemode == 0) 1144286196Sbapt cmdcnf->homemode = cfg->homemode; 1145286196Sbapt if (cmdcnf->shelldir == NULL) 1146286196Sbapt cmdcnf->shelldir = cfg->shelldir; 1147286196Sbapt if (cmdcnf->shells == NULL) 1148286196Sbapt cmdcnf->shells = cfg->shells; 1149286196Sbapt if (cmdcnf->shell_default == NULL) 1150286196Sbapt cmdcnf->shell_default = cfg->shell_default; 1151286196Sbapt if (cmdcnf->default_group == NULL) 1152286196Sbapt cmdcnf->default_group = cfg->default_group; 1153286196Sbapt if (cmdcnf->groups == NULL) 1154286196Sbapt cmdcnf->groups = cfg->groups; 1155286196Sbapt if (cmdcnf->default_class == NULL) 1156286196Sbapt cmdcnf->default_class = cfg->default_class; 1157286196Sbapt if (cmdcnf->min_uid == 0) 1158286196Sbapt cmdcnf->min_uid = cfg->min_uid; 1159286196Sbapt if (cmdcnf->max_uid == 0) 1160286196Sbapt cmdcnf->max_uid = cfg->max_uid; 1161286196Sbapt if (cmdcnf->min_gid == 0) 1162286196Sbapt cmdcnf->min_gid = cfg->min_gid; 1163286196Sbapt if (cmdcnf->max_gid == 0) 1164286196Sbapt cmdcnf->max_gid = cfg->max_gid; 1165286196Sbapt if (cmdcnf->expire_days == 0) 1166286196Sbapt cmdcnf->expire_days = cfg->expire_days; 1167286196Sbapt if (cmdcnf->password_days == 0) 1168286196Sbapt cmdcnf->password_days = cfg->password_days; 1169286196Sbapt} 1170286196Sbapt 1171286196Sbaptint 1172286196Sbaptpw_user_add(int argc, char **argv, char *arg1) 1173286196Sbapt{ 1174286196Sbapt struct userconf *cnf, *cmdcnf; 1175286196Sbapt struct passwd *pwd; 1176286196Sbapt struct group *grp; 1177286196Sbapt struct stat st; 1178286196Sbapt char args[] = "C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y"; 1179286196Sbapt char line[_PASSWORD_LEN+1], path[MAXPATHLEN]; 1180286196Sbapt char *gecos, *homedir, *skel, *walk, *userid, *groupid, *grname; 1181286196Sbapt char *default_passwd, *name, *p; 1182286196Sbapt const char *cfg; 1183286196Sbapt login_cap_t *lc; 1184286196Sbapt FILE *pfp, *fp; 1185286196Sbapt intmax_t id = -1; 1186286196Sbapt time_t now; 1187286196Sbapt int rc, ch, fd = -1; 1188286196Sbapt size_t i; 1189286196Sbapt bool dryrun, nis, pretty, quiet, createhome, precrypted, genconf; 1190286196Sbapt 1191286196Sbapt dryrun = nis = pretty = quiet = createhome = precrypted = false; 1192286196Sbapt genconf = false; 1193286196Sbapt gecos = homedir = skel = userid = groupid = default_passwd = NULL; 1194286196Sbapt grname = name = NULL; 1195286196Sbapt 1196286196Sbapt if ((cmdcnf = calloc(1, sizeof(struct userconf))) == NULL) 1197286196Sbapt err(EXIT_FAILURE, "calloc()"); 1198286196Sbapt 1199286196Sbapt if (arg1 != NULL) { 1200286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 1201286196Sbapt id = pw_checkid(arg1, UID_MAX); 1202286196Sbapt else 1203286196Sbapt name = arg1; 1204286196Sbapt } 1205286196Sbapt 1206286196Sbapt while ((ch = getopt(argc, argv, args)) != -1) { 1207286196Sbapt switch (ch) { 1208286196Sbapt case 'C': 1209286196Sbapt cfg = optarg; 1210286196Sbapt break; 1211286196Sbapt case 'q': 1212286196Sbapt quiet = true; 1213286196Sbapt break; 1214286196Sbapt case 'n': 1215286196Sbapt name = optarg; 1216286196Sbapt break; 1217286196Sbapt case 'u': 1218286196Sbapt userid = optarg; 1219286196Sbapt break; 1220286196Sbapt case 'c': 1221286196Sbapt gecos = pw_checkname(optarg, 1); 1222286196Sbapt break; 1223286196Sbapt case 'd': 1224286196Sbapt homedir = optarg; 1225286196Sbapt break; 1226286196Sbapt case 'e': 1227286196Sbapt now = time(NULL); 1228286196Sbapt cmdcnf->expire_days = parse_date(now, optarg); 1229286196Sbapt break; 1230286196Sbapt case 'p': 1231286196Sbapt now = time(NULL); 1232286196Sbapt cmdcnf->password_days = parse_date(now, optarg); 1233286196Sbapt break; 1234286196Sbapt case 'g': 1235286196Sbapt validate_grname(cmdcnf, optarg); 1236286196Sbapt grname = optarg; 1237286196Sbapt break; 1238286196Sbapt case 'G': 1239286196Sbapt split_groups(&cmdcnf->groups, optarg); 1240286196Sbapt break; 1241286196Sbapt case 'm': 1242286196Sbapt createhome = true; 1243286196Sbapt break; 1244286196Sbapt case 'M': 1245286196Sbapt cmdcnf->homemode = validate_mode(optarg); 1246286196Sbapt break; 1247286196Sbapt case 'k': 1248286196Sbapt walk = skel = optarg; 1249286196Sbapt if (*walk == '/') 1250286196Sbapt walk++; 1251286196Sbapt if (fstatat(conf.rootfd, walk, &st, 0) == -1) 1252286196Sbapt errx(EX_OSFILE, "skeleton `%s' does not " 1253286196Sbapt "exists", skel); 1254286196Sbapt if (!S_ISDIR(st.st_mode)) 1255286196Sbapt errx(EX_OSFILE, "skeleton `%s' is not a " 1256286196Sbapt "directory", skel); 1257286196Sbapt cmdcnf->dotdir = skel; 1258286196Sbapt break; 1259286196Sbapt case 's': 1260286196Sbapt cmdcnf->shell_default = optarg; 1261286196Sbapt break; 1262286196Sbapt case 'o': 1263286196Sbapt conf.checkduplicate = false; 1264286196Sbapt break; 1265286196Sbapt case 'L': 1266286196Sbapt cmdcnf->default_class = pw_checkname(optarg, 0); 1267286196Sbapt break; 1268286196Sbapt case 'i': 1269286196Sbapt groupid = optarg; 1270286196Sbapt break; 1271286196Sbapt case 'w': 1272286196Sbapt default_passwd = optarg; 1273286196Sbapt break; 1274286196Sbapt case 'H': 1275286196Sbapt if (fd != -1) 1276286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1277286196Sbapt "exclusive options"); 1278286196Sbapt fd = pw_checkfd(optarg); 1279286196Sbapt precrypted = true; 1280286196Sbapt if (fd == '-') 1281286196Sbapt errx(EX_USAGE, "-H expects a file descriptor"); 1282286196Sbapt break; 1283286196Sbapt case 'h': 1284286196Sbapt if (fd != -1) 1285286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1286286196Sbapt "exclusive options"); 1287286196Sbapt fd = pw_checkfd(optarg); 1288286196Sbapt break; 1289286196Sbapt case 'D': 1290286196Sbapt genconf = true; 1291286196Sbapt break; 1292286196Sbapt case 'b': 1293286196Sbapt cmdcnf->home = optarg; 1294286196Sbapt break; 1295286196Sbapt case 'N': 1296286196Sbapt dryrun = true; 1297286196Sbapt break; 1298286196Sbapt case 'P': 1299286196Sbapt pretty = true; 1300286196Sbapt break; 1301286196Sbapt case 'y': 1302286196Sbapt cmdcnf->nispasswd = optarg; 1303286196Sbapt break; 1304286196Sbapt case 'Y': 1305286196Sbapt nis = true; 1306286196Sbapt break; 1307109961Sgad } 1308109961Sgad } 1309286196Sbapt 1310286196Sbapt if (geteuid() != 0 && ! dryrun) 1311286196Sbapt errx(EX_NOPERM, "you must be root"); 1312286196Sbapt 1313286196Sbapt if (quiet) 1314286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 1315286196Sbapt 1316286196Sbapt cnf = get_userconfig(cfg); 1317286196Sbapt 1318286196Sbapt mix_config(cmdcnf, cnf); 1319286196Sbapt if (default_passwd) 1320286196Sbapt cmdcnf->default_password = boolean_val(default_passwd, 1321286196Sbapt cnf->default_password); 1322286196Sbapt if (genconf) { 1323286196Sbapt if (name != NULL) 1324286196Sbapt errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 1325286196Sbapt if (userid != NULL) { 1326286196Sbapt if ((p = strtok(userid, ", \t")) != NULL) 1327286196Sbapt cmdcnf->min_uid = pw_checkid(p, UID_MAX); 1328286196Sbapt if (cmdcnf->min_uid == 0) 1329286196Sbapt cmdcnf->min_uid = 1000; 1330286196Sbapt if ((p = strtok(NULL, " ,\t")) != NULL) 1331286196Sbapt cmdcnf->max_uid = pw_checkid(p, UID_MAX); 1332286196Sbapt if (cmdcnf->max_uid == 0) 1333286196Sbapt cmdcnf->max_uid = 32000; 1334109961Sgad } 1335286196Sbapt if (groupid != NULL) { 1336286196Sbapt if ((p = strtok(groupid, ", \t")) != NULL) 1337286196Sbapt cmdcnf->min_gid = pw_checkid(p, GID_MAX); 1338286196Sbapt if (cmdcnf->min_gid == 0) 1339286196Sbapt cmdcnf->min_gid = 1000; 1340286196Sbapt if ((p = strtok(NULL, " ,\t")) != NULL) 1341286196Sbapt cmdcnf->max_gid = pw_checkid(p, GID_MAX); 1342286196Sbapt if (cmdcnf->max_gid == 0) 1343286196Sbapt cmdcnf->max_gid = 32000; 1344286196Sbapt } 1345286196Sbapt if (write_userconfig(cmdcnf, cfg)) 1346286196Sbapt return (EXIT_SUCCESS); 1347286196Sbapt err(EX_IOERR, "config update"); 1348109961Sgad } 1349286196Sbapt 1350286196Sbapt if (userid) 1351286196Sbapt id = pw_checkid(userid, UID_MAX); 1352286196Sbapt if (id < 0 && name == NULL) 1353286196Sbapt errx(EX_DATAERR, "user name or id required"); 1354286196Sbapt 1355286196Sbapt if (name == NULL) 1356286196Sbapt errx(EX_DATAERR, "login name required"); 1357286196Sbapt 1358286198Sbapt if (GETPWNAM(name) != NULL) 1359286198Sbapt errx(EX_DATAERR, "login name `%s' already exists", name); 1360286198Sbapt 1361286196Sbapt pwd = &fakeuser; 1362286196Sbapt pwd->pw_name = name; 1363286196Sbapt pwd->pw_class = cmdcnf->default_class ? cmdcnf->default_class : ""; 1364286196Sbapt pwd->pw_uid = pw_uidpolicy(cmdcnf, id); 1365286196Sbapt pwd->pw_gid = pw_gidpolicy(cnf, grname, pwd->pw_name, 1366286196Sbapt (gid_t) pwd->pw_uid, dryrun); 1367286196Sbapt pwd->pw_change = cmdcnf->password_days; 1368286196Sbapt pwd->pw_expire = cmdcnf->expire_days; 1369286196Sbapt pwd->pw_dir = pw_homepolicy(cmdcnf, homedir, pwd->pw_name); 1370286196Sbapt pwd->pw_shell = pw_shellpolicy(cmdcnf); 1371286196Sbapt lc = login_getpwclass(pwd); 1372286196Sbapt if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 1373286196Sbapt warn("setting crypt(3) format"); 1374286196Sbapt login_close(lc); 1375286196Sbapt pwd->pw_passwd = pw_password(cmdcnf, pwd->pw_name, dryrun); 1376286196Sbapt if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 1377286196Sbapt warnx("WARNING: new account `%s' has a uid of 0 " 1378286196Sbapt "(superuser access!)", pwd->pw_name); 1379286196Sbapt if (gecos) 1380286196Sbapt pwd->pw_gecos = gecos; 1381286196Sbapt 1382286196Sbapt if (fd != -1) 1383286196Sbapt pw_set_passwd(pwd, fd, precrypted, false); 1384286196Sbapt 1385286196Sbapt if (dryrun) 1386286196Sbapt return (print_user(pwd, pretty, false)); 1387286196Sbapt 1388286196Sbapt if ((rc = addpwent(pwd)) != 0) { 1389286196Sbapt if (rc == -1) 1390286196Sbapt errx(EX_IOERR, "user '%s' already exists", 1391286196Sbapt pwd->pw_name); 1392286196Sbapt else if (rc != 0) 1393286196Sbapt err(EX_IOERR, "passwd file update"); 1394109961Sgad } 1395286196Sbapt if (nis && cmdcnf->nispasswd && *cmdcnf->nispasswd == '/') { 1396286196Sbapt printf("%s\n", cmdcnf->nispasswd); 1397286196Sbapt rc = addnispwent(cmdcnf->nispasswd, pwd); 1398286196Sbapt if (rc == -1) 1399286202Sbapt warnx("User '%s' already exists in NIS passwd", 1400286202Sbapt pwd->pw_name); 1401286196Sbapt else if (rc != 0) 1402286196Sbapt warn("NIS passwd update"); 1403286196Sbapt /* NOTE: we treat NIS-only update errors as non-fatal */ 1404286196Sbapt } 1405284110Sbapt 1406286196Sbapt if (cmdcnf->groups != NULL) { 1407286196Sbapt for (i = 0; i < cmdcnf->groups->sl_cur; i++) { 1408286196Sbapt grp = GETGRNAM(cmdcnf->groups->sl_str[i]); 1409286196Sbapt grp = gr_add(grp, pwd->pw_name); 1410286196Sbapt /* 1411286196Sbapt * grp can only be NULL in 2 cases: 1412286196Sbapt * - the new member is already a member 1413286196Sbapt * - a problem with memory occurs 1414286196Sbapt * in both cases we want to skip now. 1415286196Sbapt */ 1416286196Sbapt if (grp == NULL) 1417286196Sbapt continue; 1418286196Sbapt chggrent(grp->gr_name, grp); 1419286196Sbapt free(grp); 1420286196Sbapt } 1421286196Sbapt } 142220253Sjoerg 1423286196Sbapt pwd = GETPWNAM(name); 1424286196Sbapt if (pwd == NULL) 1425286196Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 142620253Sjoerg 1427286196Sbapt grp = GETGRGID(pwd->pw_gid); 1428286196Sbapt pw_log(cnf, M_ADD, W_USER, "%s(%ju):%s(%ju):%s:%s:%s", 1429286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 1430286202Sbapt grp ? grp->gr_name : "unknown", 1431286202Sbapt (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1), 1432286196Sbapt pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 143320253Sjoerg 1434286196Sbapt /* 1435286196Sbapt * let's touch and chown the user's mail file. This is not 1436286196Sbapt * strictly necessary under BSD with a 0755 maildir but it also 1437286196Sbapt * doesn't hurt anything to create the empty mailfile 1438286196Sbapt */ 1439286196Sbapt if (PWALTDIR() != PWF_ALT) { 1440286196Sbapt snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, 1441286196Sbapt pwd->pw_name); 1442286196Sbapt /* Preserve contents & mtime */ 1443286196Sbapt close(openat(conf.rootfd, path +1, O_RDWR | O_CREAT, 0600)); 1444286196Sbapt fchownat(conf.rootfd, path + 1, pwd->pw_uid, pwd->pw_gid, 1445286196Sbapt AT_SYMLINK_NOFOLLOW); 1446286196Sbapt } 144720253Sjoerg 1448286196Sbapt /* 1449286196Sbapt * Let's create and populate the user's home directory. Note 1450286196Sbapt * that this also `works' for editing users if -m is used, but 1451286196Sbapt * existing files will *not* be overwritten. 1452286196Sbapt */ 1453286196Sbapt if (PWALTDIR() != PWF_ALT && createhome && pwd->pw_dir && 1454286196Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) 1455286196Sbapt create_and_populate_homedir(cmdcnf, pwd, cmdcnf->dotdir, 1456286196Sbapt cmdcnf->homemode, false); 145720253Sjoerg 1458286196Sbapt if (!PWALTDIR() && cmdcnf->newmail && *cmdcnf->newmail && 1459286196Sbapt (fp = fopen(cnf->newmail, "r")) != NULL) { 1460286196Sbapt if ((pfp = popen(_PATH_SENDMAIL " -t", "w")) == NULL) 1461286196Sbapt warn("sendmail"); 1462286196Sbapt else { 1463286196Sbapt fprintf(pfp, "From: root\n" "To: %s\n" 1464286196Sbapt "Subject: Welcome!\n\n", pwd->pw_name); 1465286196Sbapt while (fgets(line, sizeof(line), fp) != NULL) { 1466286196Sbapt /* Do substitutions? */ 1467286196Sbapt fputs(line, pfp); 146820253Sjoerg } 1469286196Sbapt pclose(pfp); 1470286196Sbapt pw_log(cnf, M_ADD, W_USER, "%s(%ju) new user mail sent", 1471286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid); 147220253Sjoerg } 1473286196Sbapt fclose(fp); 147420253Sjoerg } 1475286196Sbapt 1476286196Sbapt if (nis && nis_update() == 0) 1477286196Sbapt pw_log(cnf, M_ADD, W_USER, "NIS maps updated"); 1478286196Sbapt 1479286196Sbapt return (EXIT_SUCCESS); 148020253Sjoerg} 148120747Sdavidn 1482286196Sbaptint 1483286196Sbaptpw_user_mod(int argc, char **argv, char *arg1) 148420747Sdavidn{ 1485286196Sbapt struct userconf *cnf; 1486286196Sbapt struct passwd *pwd; 1487286196Sbapt struct group *grp; 1488286196Sbapt StringList *groups = NULL; 1489286196Sbapt char args[] = "C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:NPYy:"; 1490286196Sbapt const char *cfg; 1491286196Sbapt char *gecos, *homedir, *grname, *name, *newname, *walk, *skel, *shell; 1492286196Sbapt char *passwd, *class, *nispasswd; 1493286196Sbapt login_cap_t *lc; 1494286196Sbapt struct stat st; 1495286196Sbapt intmax_t id = -1; 1496286196Sbapt int ch, fd = -1; 1497286196Sbapt size_t i, j; 1498286196Sbapt bool quiet, createhome, pretty, dryrun, nis, edited, docreatehome; 1499286218Sbapt bool precrypted; 1500286196Sbapt mode_t homemode = 0; 1501286218Sbapt time_t expire_days, password_days, now; 150220747Sdavidn 1503286196Sbapt expire_days = password_days = -1; 1504286196Sbapt gecos = homedir = grname = name = newname = skel = shell =NULL; 1505286196Sbapt passwd = NULL; 1506286196Sbapt class = nispasswd = NULL; 1507286196Sbapt quiet = createhome = pretty = dryrun = nis = precrypted = false; 1508286196Sbapt edited = docreatehome = false; 150920747Sdavidn 1510286196Sbapt if (arg1 != NULL) { 1511286259Sed if (arg1[strspn(arg1, "0123456789")] == '\0') 1512286196Sbapt id = pw_checkid(arg1, UID_MAX); 1513286196Sbapt else 1514286196Sbapt name = arg1; 1515286196Sbapt } 1516286196Sbapt 1517286196Sbapt while ((ch = getopt(argc, argv, args)) != -1) { 1518286196Sbapt switch (ch) { 1519286196Sbapt case 'C': 1520286196Sbapt cfg = optarg; 1521285433Sbapt break; 1522286196Sbapt case 'q': 1523286196Sbapt quiet = true; 1524286196Sbapt break; 1525286196Sbapt case 'n': 1526286196Sbapt name = optarg; 1527286196Sbapt break; 1528286196Sbapt case 'u': 1529286196Sbapt id = pw_checkid(optarg, UID_MAX); 1530286196Sbapt break; 1531286196Sbapt case 'c': 1532286196Sbapt gecos = pw_checkname(optarg, 1); 1533286196Sbapt break; 1534286196Sbapt case 'd': 1535286196Sbapt homedir = optarg; 1536286196Sbapt break; 1537286196Sbapt case 'e': 1538286196Sbapt now = time(NULL); 1539286196Sbapt expire_days = parse_date(now, optarg); 1540286196Sbapt break; 1541286196Sbapt case 'p': 1542286196Sbapt now = time(NULL); 1543286196Sbapt password_days = parse_date(now, optarg); 1544286196Sbapt break; 1545286196Sbapt case 'g': 1546286196Sbapt group_from_name_or_id(optarg); 1547286196Sbapt grname = optarg; 1548286196Sbapt break; 1549286196Sbapt case 'G': 1550286196Sbapt split_groups(&groups, optarg); 1551286196Sbapt break; 1552286196Sbapt case 'm': 1553286196Sbapt createhome = true; 1554286196Sbapt break; 1555286196Sbapt case 'M': 1556286196Sbapt homemode = validate_mode(optarg); 1557286196Sbapt break; 1558286196Sbapt case 'l': 1559286196Sbapt newname = optarg; 1560286196Sbapt break; 1561286196Sbapt case 'k': 1562286196Sbapt walk = skel = optarg; 1563286196Sbapt if (*walk == '/') 1564286196Sbapt walk++; 1565286196Sbapt if (fstatat(conf.rootfd, walk, &st, 0) == -1) 1566286196Sbapt errx(EX_OSFILE, "skeleton `%s' does not " 1567286196Sbapt "exists", skel); 1568286196Sbapt if (!S_ISDIR(st.st_mode)) 1569286196Sbapt errx(EX_OSFILE, "skeleton `%s' is not a " 1570286196Sbapt "directory", skel); 1571286196Sbapt break; 1572286196Sbapt case 's': 1573286196Sbapt shell = optarg; 1574286196Sbapt break; 1575286196Sbapt case 'w': 1576286196Sbapt passwd = optarg; 1577286196Sbapt break; 1578286196Sbapt case 'L': 1579286196Sbapt class = pw_checkname(optarg, 0); 1580286196Sbapt break; 1581286196Sbapt case 'H': 1582286196Sbapt if (fd != -1) 1583286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1584286196Sbapt "exclusive options"); 1585286196Sbapt fd = pw_checkfd(optarg); 1586286196Sbapt precrypted = true; 1587286196Sbapt if (fd == '-') 1588286196Sbapt errx(EX_USAGE, "-H expects a file descriptor"); 1589286196Sbapt break; 1590286196Sbapt case 'h': 1591286196Sbapt if (fd != -1) 1592286196Sbapt errx(EX_USAGE, "'-h' and '-H' are mutually " 1593286196Sbapt "exclusive options"); 1594286196Sbapt fd = pw_checkfd(optarg); 1595286196Sbapt break; 1596286196Sbapt case 'N': 1597286196Sbapt dryrun = true; 1598286196Sbapt break; 1599286196Sbapt case 'P': 1600286196Sbapt pretty = true; 1601286196Sbapt break; 1602286196Sbapt case 'y': 1603286196Sbapt nispasswd = optarg; 1604286196Sbapt break; 1605286196Sbapt case 'Y': 1606286196Sbapt nis = true; 1607286196Sbapt break; 160820747Sdavidn } 160920747Sdavidn } 1610286196Sbapt 1611286196Sbapt if (geteuid() != 0 && ! dryrun) 1612286196Sbapt errx(EX_NOPERM, "you must be root"); 1613286196Sbapt 1614286196Sbapt if (quiet) 1615286196Sbapt freopen(_PATH_DEVNULL, "w", stderr); 1616286196Sbapt 1617286196Sbapt cnf = get_userconfig(cfg); 1618286196Sbapt 1619286196Sbapt if (id < 0 && name == NULL) 1620286196Sbapt errx(EX_DATAERR, "username or id required"); 1621286196Sbapt 1622286196Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 1623286196Sbapt if (pwd == NULL) { 1624286196Sbapt if (name == NULL) 1625286196Sbapt errx(EX_NOUSER, "no such uid `%ju'", 1626286196Sbapt (uintmax_t) id); 1627286196Sbapt errx(EX_NOUSER, "no such user `%s'", name); 1628286196Sbapt } 1629286196Sbapt 1630286196Sbapt if (name == NULL) 1631286196Sbapt name = pwd->pw_name; 1632286196Sbapt 1633286196Sbapt if (nis && nispasswd == NULL) 1634286196Sbapt nispasswd = cnf->nispasswd; 1635286196Sbapt 1636286196Sbapt if (PWF._altdir == PWF_REGULAR && 1637286196Sbapt ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) { 1638286196Sbapt if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 1639286196Sbapt if (!nis && nispasswd && *nispasswd != '/') 1640286196Sbapt errx(EX_NOUSER, "Cannot modify NIS user `%s'", 1641286196Sbapt name); 1642286196Sbapt } else { 1643286196Sbapt errx(EX_NOUSER, "Cannot modify non local user `%s'", 1644286196Sbapt name); 1645286196Sbapt } 1646286196Sbapt } 1647286196Sbapt 1648286196Sbapt if (newname) { 1649286196Sbapt if (strcmp(pwd->pw_name, "root") == 0) 1650286196Sbapt errx(EX_DATAERR, "can't rename `root' account"); 1651286196Sbapt if (strcmp(pwd->pw_name, newname) != 0) { 1652286196Sbapt pwd->pw_name = pw_checkname(newname, 0); 1653286196Sbapt edited = true; 1654286196Sbapt } 1655286196Sbapt } 1656286196Sbapt 1657286196Sbapt if (id > 0 && pwd->pw_uid != id) { 1658286196Sbapt pwd->pw_uid = id; 1659286196Sbapt edited = true; 1660286196Sbapt if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 1661286196Sbapt errx(EX_DATAERR, "can't change uid of `root' account"); 1662286196Sbapt if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 1663286202Sbapt warnx("WARNING: account `%s' will have a uid of 0 " 1664286202Sbapt "(superuser access!)", pwd->pw_name); 1665286196Sbapt } 1666286196Sbapt 1667286196Sbapt if (grname && pwd->pw_uid != 0) { 1668286196Sbapt grp = GETGRNAM(grname); 1669286196Sbapt if (grp == NULL) 1670286196Sbapt grp = GETGRGID(pw_checkid(grname, GID_MAX)); 1671286196Sbapt if (grp->gr_gid != pwd->pw_gid) { 1672286196Sbapt pwd->pw_gid = grp->gr_gid; 1673286196Sbapt edited = true; 1674286196Sbapt } 1675286196Sbapt } 1676286196Sbapt 1677286196Sbapt if (password_days >= 0 && pwd->pw_change != password_days) { 1678286196Sbapt pwd->pw_change = password_days; 1679286196Sbapt edited = true; 1680286196Sbapt } 1681286196Sbapt 1682286196Sbapt if (expire_days >= 0 && pwd->pw_expire != expire_days) { 1683286196Sbapt pwd->pw_expire = expire_days; 1684286196Sbapt edited = true; 1685286196Sbapt } 1686286196Sbapt 1687286196Sbapt if (shell) { 1688286196Sbapt shell = shell_path(cnf->shelldir, cnf->shells, shell); 1689286196Sbapt if (shell == NULL) 1690286196Sbapt shell = ""; 1691286196Sbapt if (strcmp(shell, pwd->pw_shell) != 0) { 1692286196Sbapt pwd->pw_shell = shell; 1693286196Sbapt edited = true; 1694286196Sbapt } 1695286196Sbapt } 1696286196Sbapt 1697286196Sbapt if (class && strcmp(pwd->pw_class, class) != 0) { 1698286196Sbapt pwd->pw_class = class; 1699286196Sbapt edited = true; 1700286196Sbapt } 1701286196Sbapt 1702286196Sbapt if (homedir && strcmp(pwd->pw_dir, homedir) != 0) { 1703286196Sbapt pwd->pw_dir = homedir; 1704287701Sbapt edited = true; 1705286196Sbapt if (fstatat(conf.rootfd, pwd->pw_dir, &st, 0) == -1) { 1706286196Sbapt if (!createhome) 1707286196Sbapt warnx("WARNING: home `%s' does not exist", 1708286196Sbapt pwd->pw_dir); 1709286196Sbapt else 1710286196Sbapt docreatehome = true; 1711286196Sbapt } else if (!S_ISDIR(st.st_mode)) { 1712286196Sbapt warnx("WARNING: home `%s' is not a directory", 1713286196Sbapt pwd->pw_dir); 1714286196Sbapt } 1715286196Sbapt } 1716286196Sbapt 1717286196Sbapt if (passwd && conf.fd == -1) { 1718286196Sbapt lc = login_getpwclass(pwd); 1719286196Sbapt if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 1720286196Sbapt warn("setting crypt(3) format"); 1721286196Sbapt login_close(lc); 1722286775Sbapt cnf->default_password = boolean_val(passwd, 1723286775Sbapt cnf->default_password); 1724286196Sbapt pwd->pw_passwd = pw_password(cnf, pwd->pw_name, dryrun); 1725286196Sbapt edited = true; 1726286196Sbapt } 1727286196Sbapt 1728286196Sbapt if (gecos && strcmp(pwd->pw_gecos, gecos) != 0) { 1729286196Sbapt pwd->pw_gecos = gecos; 1730286196Sbapt edited = true; 1731286196Sbapt } 1732286196Sbapt 1733286196Sbapt if (fd != -1) 1734286196Sbapt edited = pw_set_passwd(pwd, fd, precrypted, true); 1735286196Sbapt 1736286196Sbapt if (dryrun) 1737286196Sbapt return (print_user(pwd, pretty, false)); 1738286196Sbapt 1739286196Sbapt if (edited) /* Only updated this if required */ 1740286196Sbapt perform_chgpwent(name, pwd, nis ? nispasswd : NULL); 1741286196Sbapt /* Now perform the needed changes concern groups */ 1742286196Sbapt if (groups != NULL) { 1743286196Sbapt /* Delete User from groups using old name */ 1744286196Sbapt SETGRENT(); 1745286196Sbapt while ((grp = GETGRENT()) != NULL) { 1746286196Sbapt if (grp->gr_mem == NULL) 1747286196Sbapt continue; 1748286196Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1749286196Sbapt if (strcmp(grp->gr_mem[i] , name) != 0) 1750286196Sbapt continue; 1751286196Sbapt for (j = i; grp->gr_mem[j] != NULL ; j++) 1752286196Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 1753286196Sbapt chggrent(grp->gr_name, grp); 1754286196Sbapt break; 1755286196Sbapt } 1756286196Sbapt } 1757286196Sbapt ENDGRENT(); 1758286196Sbapt /* Add the user to the needed groups */ 1759286196Sbapt for (i = 0; i < groups->sl_cur; i++) { 1760286196Sbapt grp = GETGRNAM(groups->sl_str[i]); 1761286196Sbapt grp = gr_add(grp, pwd->pw_name); 1762286196Sbapt if (grp == NULL) 1763286196Sbapt continue; 1764286196Sbapt chggrent(grp->gr_name, grp); 1765286196Sbapt free(grp); 1766286196Sbapt } 1767286196Sbapt } 1768286196Sbapt /* In case of rename we need to walk over the different groups */ 1769286196Sbapt if (newname) { 1770286196Sbapt SETGRENT(); 1771286196Sbapt while ((grp = GETGRENT()) != NULL) { 1772286196Sbapt if (grp->gr_mem == NULL) 1773286196Sbapt continue; 1774286196Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1775286196Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 1776286196Sbapt continue; 1777286196Sbapt grp->gr_mem[i] = newname; 1778286196Sbapt chggrent(grp->gr_name, grp); 1779286196Sbapt break; 1780286196Sbapt } 1781286196Sbapt } 1782286196Sbapt } 1783286196Sbapt 1784286196Sbapt /* go get a current version of pwd */ 1785286196Sbapt if (newname) 1786286196Sbapt name = newname; 1787286196Sbapt pwd = GETPWNAM(name); 1788286196Sbapt if (pwd == NULL) 1789286196Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 1790286196Sbapt grp = GETGRGID(pwd->pw_gid); 1791286196Sbapt pw_log(cnf, M_UPDATE, W_USER, "%s(%ju):%s(%ju):%s:%s:%s", 1792286196Sbapt pwd->pw_name, (uintmax_t)pwd->pw_uid, 1793286196Sbapt grp ? grp->gr_name : "unknown", 1794286196Sbapt (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1), 1795286196Sbapt pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 1796286196Sbapt 1797285433Sbapt /* 1798286196Sbapt * Let's create and populate the user's home directory. Note 1799286196Sbapt * that this also `works' for editing users if -m is used, but 1800286196Sbapt * existing files will *not* be overwritten. 1801285433Sbapt */ 1802286196Sbapt if (PWALTDIR() != PWF_ALT && docreatehome && pwd->pw_dir && 1803286196Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 1804286196Sbapt if (!skel) 1805286196Sbapt skel = cnf->dotdir; 1806286196Sbapt if (homemode == 0) 1807286196Sbapt homemode = cnf->homemode; 1808286196Sbapt create_and_populate_homedir(cnf, pwd, skel, homemode, true); 1809286196Sbapt } 1810286196Sbapt 1811286196Sbapt if (nis && nis_update() == 0) 1812286196Sbapt pw_log(cnf, M_UPDATE, W_USER, "NIS maps updated"); 1813286196Sbapt 1814286196Sbapt return (EXIT_SUCCESS); 181520747Sdavidn} 1816