1d6f907dcSJoerg Wunsch /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
4ad7cf975SJoerg Wunsch * Copyright (C) 1996
5ad7cf975SJoerg Wunsch * David L. Nugent. All rights reserved.
6d6f907dcSJoerg Wunsch *
7d6f907dcSJoerg Wunsch * Redistribution and use in source and binary forms, with or without
8d6f907dcSJoerg Wunsch * modification, are permitted provided that the following conditions
9d6f907dcSJoerg Wunsch * are met:
10d6f907dcSJoerg Wunsch * 1. Redistributions of source code must retain the above copyright
11ad7cf975SJoerg Wunsch * notice, this list of conditions and the following disclaimer.
12d6f907dcSJoerg Wunsch * 2. Redistributions in binary form must reproduce the above copyright
13d6f907dcSJoerg Wunsch * notice, this list of conditions and the following disclaimer in the
14d6f907dcSJoerg Wunsch * documentation and/or other materials provided with the distribution.
15d6f907dcSJoerg Wunsch *
16ad7cf975SJoerg Wunsch * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17d6f907dcSJoerg Wunsch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18d6f907dcSJoerg Wunsch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ad7cf975SJoerg Wunsch * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20d6f907dcSJoerg Wunsch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21d6f907dcSJoerg Wunsch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22d6f907dcSJoerg Wunsch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23d6f907dcSJoerg Wunsch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24d6f907dcSJoerg Wunsch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25d6f907dcSJoerg Wunsch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26d6f907dcSJoerg Wunsch * SUCH DAMAGE.
275f12594aSDavid Nugent *
28d6f907dcSJoerg Wunsch */
29d6f907dcSJoerg Wunsch
30bcbdb01eSBaptiste Daroussin #include <sys/param.h>
31*d2f1f71eSMike Karels #include <sys/wait.h>
32bcbdb01eSBaptiste Daroussin
33daccabe9SAlan Somers #include <assert.h>
34d6f907dcSJoerg Wunsch #include <ctype.h>
35bcbdb01eSBaptiste Daroussin #include <dirent.h>
361dcc6ec7SPhilippe Charnier #include <err.h>
3795c64defSBaptiste Daroussin #include <errno.h>
381dcc6ec7SPhilippe Charnier #include <fcntl.h>
39fb2db031SBaptiste Daroussin #include <grp.h>
40bcbdb01eSBaptiste Daroussin #include <pwd.h>
41fb2db031SBaptiste Daroussin #include <libutil.h>
42bcbdb01eSBaptiste Daroussin #include <login_cap.h>
43bcbdb01eSBaptiste Daroussin #include <paths.h>
44bcbdb01eSBaptiste Daroussin #include <string.h>
45bcbdb01eSBaptiste Daroussin #include <sysexits.h>
46bcbdb01eSBaptiste Daroussin #include <termios.h>
47bcbdb01eSBaptiste Daroussin #include <unistd.h>
48ef7d0eb9SBaptiste Daroussin #include <spawn.h>
49bcbdb01eSBaptiste Daroussin
50d6f907dcSJoerg Wunsch #include "pw.h"
51d6f907dcSJoerg Wunsch #include "bitmap.h"
52bcbdb01eSBaptiste Daroussin #include "psdate.h"
53d6f907dcSJoerg Wunsch
5408a77c42SAndrey A. Chernov #define LOGNAMESIZE (MAXLOGNAME-1)
55e57b5686SDavid Nugent
56ef7d0eb9SBaptiste Daroussin extern char **environ;
572399cd14SDavid Nugent static char locked_str[] = "*LOCKED*";
58a200d18aSAndrey A. Chernov
59d2d022b9SBaptiste Daroussin static struct passwd fakeuser = {
60d2d022b9SBaptiste Daroussin "nouser",
61d2d022b9SBaptiste Daroussin "*",
62d2d022b9SBaptiste Daroussin -1,
63d2d022b9SBaptiste Daroussin -1,
64d2d022b9SBaptiste Daroussin 0,
65d2d022b9SBaptiste Daroussin "",
66d2d022b9SBaptiste Daroussin "User &",
67d2d022b9SBaptiste Daroussin "/nonexistent",
68d2d022b9SBaptiste Daroussin "/bin/sh",
69d2d022b9SBaptiste Daroussin 0,
70d2d022b9SBaptiste Daroussin 0
71d2d022b9SBaptiste Daroussin };
72d2d022b9SBaptiste Daroussin
73d2d022b9SBaptiste Daroussin static int print_user(struct passwd *pwd, bool pretty, bool v7);
74d2d022b9SBaptiste Daroussin static uid_t pw_uidpolicy(struct userconf *cnf, intmax_t id);
75d2d022b9SBaptiste Daroussin static uid_t pw_gidpolicy(struct userconf *cnf, char *grname, char *nam,
76d2d022b9SBaptiste Daroussin gid_t prefer, bool dryrun);
77d2d022b9SBaptiste Daroussin static char *pw_homepolicy(struct userconf * cnf, char *homedir,
78d2d022b9SBaptiste Daroussin const char *user);
79d2d022b9SBaptiste Daroussin static char *pw_shellpolicy(struct userconf * cnf);
801cbe5012SEugene Grosbein static char *pw_password(struct userconf * cnf, char const * user);
81d6f907dcSJoerg Wunsch static char *shell_path(char const * path, char *shells[], char *sh);
82d6f907dcSJoerg Wunsch static void rmat(uid_t uid);
83d6f907dcSJoerg Wunsch
84ac72be28SBaptiste Daroussin static void
mkdir_home_parents(int dfd,const char * dir)8595c64defSBaptiste Daroussin mkdir_home_parents(int dfd, const char *dir)
8695c64defSBaptiste Daroussin {
8795c64defSBaptiste Daroussin struct stat st;
8895c64defSBaptiste Daroussin char *dirs, *tmp;
8995c64defSBaptiste Daroussin
9095c64defSBaptiste Daroussin if (*dir != '/')
9195c64defSBaptiste Daroussin errx(EX_DATAERR, "invalid base directory for home '%s'", dir);
9295c64defSBaptiste Daroussin
9395c64defSBaptiste Daroussin dir++;
9495c64defSBaptiste Daroussin
9595c64defSBaptiste Daroussin if (fstatat(dfd, dir, &st, 0) != -1) {
9695c64defSBaptiste Daroussin if (S_ISDIR(st.st_mode))
9795c64defSBaptiste Daroussin return;
9895c64defSBaptiste Daroussin errx(EX_OSFILE, "root home `/%s' is not a directory", dir);
9995c64defSBaptiste Daroussin }
10095c64defSBaptiste Daroussin
10195c64defSBaptiste Daroussin dirs = strdup(dir);
10295c64defSBaptiste Daroussin if (dirs == NULL)
10395c64defSBaptiste Daroussin errx(EX_UNAVAILABLE, "out of memory");
10495c64defSBaptiste Daroussin
10595c64defSBaptiste Daroussin tmp = strrchr(dirs, '/');
106b1585b5aSBryan Drewery if (tmp == NULL) {
107b1585b5aSBryan Drewery free(dirs);
10895c64defSBaptiste Daroussin return;
109b1585b5aSBryan Drewery }
11095c64defSBaptiste Daroussin tmp[0] = '\0';
11195c64defSBaptiste Daroussin
11295c64defSBaptiste Daroussin tmp = dirs;
11395c64defSBaptiste Daroussin if (fstatat(dfd, dirs, &st, 0) == -1) {
11495c64defSBaptiste Daroussin while ((tmp = strchr(tmp + 1, '/')) != NULL) {
11595c64defSBaptiste Daroussin *tmp = '\0';
11695c64defSBaptiste Daroussin if (fstatat(dfd, dirs, &st, 0) == -1) {
11795c64defSBaptiste Daroussin if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1)
118bbb2d2ceSMike Karels err(EX_OSFILE, "'%s' (home parent) is not a directory", dirs);
11995c64defSBaptiste Daroussin }
12095c64defSBaptiste Daroussin *tmp = '/';
12195c64defSBaptiste Daroussin }
12295c64defSBaptiste Daroussin }
12395c64defSBaptiste Daroussin if (fstatat(dfd, dirs, &st, 0) == -1) {
12495c64defSBaptiste Daroussin if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1)
125bbb2d2ceSMike Karels err(EX_OSFILE, "'%s' (home parent) is not a directory", dirs);
12695c64defSBaptiste Daroussin fchownat(dfd, dirs, 0, 0, 0);
12795c64defSBaptiste Daroussin }
12895c64defSBaptiste Daroussin
12995c64defSBaptiste Daroussin free(dirs);
13095c64defSBaptiste Daroussin }
13195c64defSBaptiste Daroussin
13295c64defSBaptiste Daroussin static void
create_and_populate_homedir(struct userconf * cnf,struct passwd * pwd,const char * skeldir,mode_t homemode,bool update)133d2d022b9SBaptiste Daroussin create_and_populate_homedir(struct userconf *cnf, struct passwd *pwd,
134d2d022b9SBaptiste Daroussin const char *skeldir, mode_t homemode, bool update)
135ac72be28SBaptiste Daroussin {
13665730d93SBaptiste Daroussin int skelfd = -1;
137ac72be28SBaptiste Daroussin
13895c64defSBaptiste Daroussin /* Create home parents directories */
13995c64defSBaptiste Daroussin mkdir_home_parents(conf.rootfd, pwd->pw_dir);
14095c64defSBaptiste Daroussin
14165730d93SBaptiste Daroussin if (skeldir != NULL && *skeldir != '\0') {
14228b2f129SBaptiste Daroussin if (*skeldir == '/')
14328b2f129SBaptiste Daroussin skeldir++;
14428b2f129SBaptiste Daroussin skelfd = openat(conf.rootfd, skeldir, O_DIRECTORY|O_CLOEXEC);
145ac72be28SBaptiste Daroussin }
146ac72be28SBaptiste Daroussin
147d2d022b9SBaptiste Daroussin copymkdir(conf.rootfd, pwd->pw_dir, skelfd, homemode, pwd->pw_uid,
14865730d93SBaptiste Daroussin pwd->pw_gid, 0);
149a9ea647cSDag-Erling Smørgrav pw_log(cnf, update ? M_MODIFY : M_ADD, W_USER, "%s(%ju) home %s made",
150d2d022b9SBaptiste Daroussin pwd->pw_name, (uintmax_t)pwd->pw_uid, pwd->pw_dir);
151ac72be28SBaptiste Daroussin }
152ac72be28SBaptiste Daroussin
15311621f96SBaptiste Daroussin static int
pw_set_passwd(struct passwd * pwd,int fd,bool precrypted,bool update)154d2d022b9SBaptiste Daroussin pw_set_passwd(struct passwd *pwd, int fd, bool precrypted, bool update)
15511621f96SBaptiste Daroussin {
15611621f96SBaptiste Daroussin int b, istty;
15711621f96SBaptiste Daroussin struct termios t, n;
15811621f96SBaptiste Daroussin login_cap_t *lc;
15911621f96SBaptiste Daroussin char line[_PASSWORD_LEN+1];
16011621f96SBaptiste Daroussin char *p;
16111621f96SBaptiste Daroussin
162d2d022b9SBaptiste Daroussin if (fd == '-') {
16311621f96SBaptiste Daroussin if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
16411621f96SBaptiste Daroussin pwd->pw_passwd = "*"; /* No access */
16511621f96SBaptiste Daroussin return (1);
16611621f96SBaptiste Daroussin }
16711621f96SBaptiste Daroussin return (0);
16811621f96SBaptiste Daroussin }
16911621f96SBaptiste Daroussin
170d2d022b9SBaptiste Daroussin if ((istty = isatty(fd))) {
171d2d022b9SBaptiste Daroussin if (tcgetattr(fd, &t) == -1)
17211621f96SBaptiste Daroussin istty = 0;
17311621f96SBaptiste Daroussin else {
1741f8d9092SBaptiste Daroussin n = t;
17511621f96SBaptiste Daroussin n.c_lflag &= ~(ECHO);
176d2d022b9SBaptiste Daroussin tcsetattr(fd, TCSANOW, &n);
17711621f96SBaptiste Daroussin printf("%s%spassword for user %s:",
17811621f96SBaptiste Daroussin update ? "new " : "",
179d2d022b9SBaptiste Daroussin precrypted ? "encrypted " : "",
18011621f96SBaptiste Daroussin pwd->pw_name);
18111621f96SBaptiste Daroussin fflush(stdout);
18211621f96SBaptiste Daroussin }
18311621f96SBaptiste Daroussin }
184d2d022b9SBaptiste Daroussin b = read(fd, line, sizeof(line) - 1);
18511621f96SBaptiste Daroussin if (istty) { /* Restore state */
186d2d022b9SBaptiste Daroussin tcsetattr(fd, TCSANOW, &t);
18711621f96SBaptiste Daroussin fputc('\n', stdout);
18811621f96SBaptiste Daroussin fflush(stdout);
18911621f96SBaptiste Daroussin }
19011621f96SBaptiste Daroussin
19111621f96SBaptiste Daroussin if (b < 0)
19211621f96SBaptiste Daroussin err(EX_IOERR, "-%c file descriptor",
193d2d022b9SBaptiste Daroussin precrypted ? 'H' : 'h');
19411621f96SBaptiste Daroussin line[b] = '\0';
19511621f96SBaptiste Daroussin if ((p = strpbrk(line, "\r\n")) != NULL)
19611621f96SBaptiste Daroussin *p = '\0';
19711621f96SBaptiste Daroussin if (!*line)
19811621f96SBaptiste Daroussin errx(EX_DATAERR, "empty password read on file descriptor %d",
199d2d022b9SBaptiste Daroussin fd);
200d2d022b9SBaptiste Daroussin if (precrypted) {
20111621f96SBaptiste Daroussin if (strchr(line, ':') != NULL)
2021f8d9092SBaptiste Daroussin errx(EX_DATAERR, "bad encrypted password");
203d2d022b9SBaptiste Daroussin pwd->pw_passwd = strdup(line);
20411621f96SBaptiste Daroussin } else {
20511621f96SBaptiste Daroussin lc = login_getpwclass(pwd);
20611621f96SBaptiste Daroussin if (lc == NULL ||
20711621f96SBaptiste Daroussin login_setcryptfmt(lc, "sha512", NULL) == NULL)
20811621f96SBaptiste Daroussin warn("setting crypt(3) format");
20911621f96SBaptiste Daroussin login_close(lc);
21011621f96SBaptiste Daroussin pwd->pw_passwd = pw_pwcrypt(line);
21111621f96SBaptiste Daroussin }
21211621f96SBaptiste Daroussin return (1);
21311621f96SBaptiste Daroussin }
21411621f96SBaptiste Daroussin
21504c25b67SBaptiste Daroussin static void
perform_chgpwent(const char * name,struct passwd * pwd,char * nispasswd)216d2d022b9SBaptiste Daroussin perform_chgpwent(const char *name, struct passwd *pwd, char *nispasswd)
21704c25b67SBaptiste Daroussin {
21804c25b67SBaptiste Daroussin int rc;
219d2d022b9SBaptiste Daroussin struct passwd *nispwd;
220d2d022b9SBaptiste Daroussin
221d2d022b9SBaptiste Daroussin /* duplicate for nis so that chgpwent is not modifying before NIS */
222d2d022b9SBaptiste Daroussin if (nispasswd && *nispasswd == '/')
223d2d022b9SBaptiste Daroussin nispwd = pw_dup(pwd);
22404c25b67SBaptiste Daroussin
22504c25b67SBaptiste Daroussin rc = chgpwent(name, pwd);
22604c25b67SBaptiste Daroussin if (rc == -1)
22704c25b67SBaptiste Daroussin errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name);
22804c25b67SBaptiste Daroussin else if (rc != 0)
22904c25b67SBaptiste Daroussin err(EX_IOERR, "passwd file update");
23004c25b67SBaptiste Daroussin
231d2d022b9SBaptiste Daroussin if (nispasswd && *nispasswd == '/') {
232d2d022b9SBaptiste Daroussin rc = chgnispwent(nispasswd, name, nispwd);
23304c25b67SBaptiste Daroussin if (rc == -1)
23404c25b67SBaptiste Daroussin warn("User '%s' not found in NIS passwd", pwd->pw_name);
23581e2ba84SBaptiste Daroussin else if (rc != 0)
23604c25b67SBaptiste Daroussin warn("NIS passwd update");
23704c25b67SBaptiste Daroussin /* NOTE: NIS-only update errors are not fatal */
23804c25b67SBaptiste Daroussin }
23904c25b67SBaptiste Daroussin }
24004c25b67SBaptiste Daroussin
24104c25b67SBaptiste Daroussin /*
24204c25b67SBaptiste Daroussin * The M_LOCK and M_UNLOCK functions simply add or remove
24304c25b67SBaptiste Daroussin * a "*LOCKED*" prefix from in front of the password to
24404c25b67SBaptiste Daroussin * prevent it decoding correctly, and therefore prevents
24504c25b67SBaptiste Daroussin * access. Of course, this only prevents access via
24604c25b67SBaptiste Daroussin * password authentication (not ssh, kerberos or any
24704c25b67SBaptiste Daroussin * other method that does not use the UNIX password) but
24804c25b67SBaptiste Daroussin * that is a known limitation.
24904c25b67SBaptiste Daroussin */
25004c25b67SBaptiste Daroussin static int
pw_userlock(char * arg1,int mode)251d2d022b9SBaptiste Daroussin pw_userlock(char *arg1, int mode)
25204c25b67SBaptiste Daroussin {
25304c25b67SBaptiste Daroussin struct passwd *pwd = NULL;
25404c25b67SBaptiste Daroussin char *passtmp = NULL;
255d2d022b9SBaptiste Daroussin char *name;
25604c25b67SBaptiste Daroussin bool locked = false;
257c514e5a5SBaptiste Daroussin uid_t id = (uid_t)-1;
25804c25b67SBaptiste Daroussin
259d2d022b9SBaptiste Daroussin if (geteuid() != 0)
260d2d022b9SBaptiste Daroussin errx(EX_NOPERM, "you must be root");
261d2d022b9SBaptiste Daroussin
262d2d022b9SBaptiste Daroussin if (arg1 == NULL)
26304c25b67SBaptiste Daroussin errx(EX_DATAERR, "username or id required");
26404c25b67SBaptiste Daroussin
265d2d022b9SBaptiste Daroussin name = arg1;
266c514e5a5SBaptiste Daroussin if (arg1[strspn(name, "0123456789")] == '\0')
267c514e5a5SBaptiste Daroussin id = pw_checkid(name, UID_MAX);
268d2d022b9SBaptiste Daroussin
269c514e5a5SBaptiste Daroussin pwd = GETPWNAM(pw_checkname(name, 0));
270c514e5a5SBaptiste Daroussin if (pwd == NULL && id != (uid_t)-1) {
271c514e5a5SBaptiste Daroussin pwd = GETPWUID(id);
272c514e5a5SBaptiste Daroussin if (pwd != NULL)
273c514e5a5SBaptiste Daroussin name = pwd->pw_name;
274c514e5a5SBaptiste Daroussin }
27504c25b67SBaptiste Daroussin if (pwd == NULL) {
276c514e5a5SBaptiste Daroussin if (id == (uid_t)-1)
277c514e5a5SBaptiste Daroussin errx(EX_NOUSER, "no such name or uid `%ju'", (uintmax_t) id);
27804c25b67SBaptiste Daroussin errx(EX_NOUSER, "no such user `%s'", name);
27904c25b67SBaptiste Daroussin }
28004c25b67SBaptiste Daroussin
28104c25b67SBaptiste Daroussin if (name == NULL)
28204c25b67SBaptiste Daroussin name = pwd->pw_name;
28304c25b67SBaptiste Daroussin
28404c25b67SBaptiste Daroussin if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str) -1) == 0)
28504c25b67SBaptiste Daroussin locked = true;
28604c25b67SBaptiste Daroussin if (mode == M_LOCK && locked)
28704c25b67SBaptiste Daroussin errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
28804c25b67SBaptiste Daroussin if (mode == M_UNLOCK && !locked)
28904c25b67SBaptiste Daroussin errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
29004c25b67SBaptiste Daroussin
29104c25b67SBaptiste Daroussin if (mode == M_LOCK) {
29204c25b67SBaptiste Daroussin asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd);
29304c25b67SBaptiste Daroussin if (passtmp == NULL) /* disaster */
29404c25b67SBaptiste Daroussin errx(EX_UNAVAILABLE, "out of memory");
29504c25b67SBaptiste Daroussin pwd->pw_passwd = passtmp;
29604c25b67SBaptiste Daroussin } else {
29704c25b67SBaptiste Daroussin pwd->pw_passwd += sizeof(locked_str)-1;
29804c25b67SBaptiste Daroussin }
29904c25b67SBaptiste Daroussin
300d2d022b9SBaptiste Daroussin perform_chgpwent(name, pwd, NULL);
30104c25b67SBaptiste Daroussin free(passtmp);
30204c25b67SBaptiste Daroussin
30304c25b67SBaptiste Daroussin return (EXIT_SUCCESS);
30404c25b67SBaptiste Daroussin }
30504c25b67SBaptiste Daroussin
306d6f907dcSJoerg Wunsch static uid_t
pw_uidpolicy(struct userconf * cnf,intmax_t id)307d2d022b9SBaptiste Daroussin pw_uidpolicy(struct userconf * cnf, intmax_t id)
308d6f907dcSJoerg Wunsch {
309d6f907dcSJoerg Wunsch struct passwd *pwd;
310d2d022b9SBaptiste Daroussin struct bitmap bm;
311d6f907dcSJoerg Wunsch uid_t uid = (uid_t) - 1;
312d6f907dcSJoerg Wunsch
313d6f907dcSJoerg Wunsch /*
314d6f907dcSJoerg Wunsch * Check the given uid, if any
315d6f907dcSJoerg Wunsch */
316d7747c3cSBaptiste Daroussin if (id >= 0) {
31791860967SBaptiste Daroussin uid = (uid_t) id;
318d6f907dcSJoerg Wunsch
31991860967SBaptiste Daroussin if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate)
32050698e67SBaptiste Daroussin errx(EX_DATAERR, "uid `%ju' has already been allocated",
32150698e67SBaptiste Daroussin (uintmax_t)pwd->pw_uid);
322d2d022b9SBaptiste Daroussin return (uid);
323d2d022b9SBaptiste Daroussin }
324d6f907dcSJoerg Wunsch /*
325d6f907dcSJoerg Wunsch * We need to allocate the next available uid under one of
326d6f907dcSJoerg Wunsch * two policies a) Grab the first unused uid b) Grab the
327d6f907dcSJoerg Wunsch * highest possible unused uid
328d6f907dcSJoerg Wunsch */
329d6f907dcSJoerg Wunsch if (cnf->min_uid >= cnf->max_uid) { /* Sanity
330d6f907dcSJoerg Wunsch * claus^H^H^H^Hheck */
331d6f907dcSJoerg Wunsch cnf->min_uid = 1000;
332d6f907dcSJoerg Wunsch cnf->max_uid = 32000;
333d6f907dcSJoerg Wunsch }
334d6f907dcSJoerg Wunsch bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
335d6f907dcSJoerg Wunsch
336d6f907dcSJoerg Wunsch /*
337d6f907dcSJoerg Wunsch * Now, let's fill the bitmap from the password file
338d6f907dcSJoerg Wunsch */
3395f12594aSDavid Nugent SETPWENT();
3405f12594aSDavid Nugent while ((pwd = GETPWENT()) != NULL)
3415f12594aSDavid Nugent if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
342d6f907dcSJoerg Wunsch bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
3435f12594aSDavid Nugent ENDPWENT();
344d6f907dcSJoerg Wunsch
345d6f907dcSJoerg Wunsch /*
346d6f907dcSJoerg Wunsch * Then apply the policy, with fallback to reuse if necessary
347d6f907dcSJoerg Wunsch */
348d6f907dcSJoerg Wunsch if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
349d6f907dcSJoerg Wunsch uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
350d6f907dcSJoerg Wunsch
351d6f907dcSJoerg Wunsch /*
352d6f907dcSJoerg Wunsch * Another sanity check
353d6f907dcSJoerg Wunsch */
354d6f907dcSJoerg Wunsch if (uid < cnf->min_uid || uid > cnf->max_uid)
3551dcc6ec7SPhilippe Charnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
356d6f907dcSJoerg Wunsch bm_dealloc(&bm);
357d2d022b9SBaptiste Daroussin return (uid);
358d6f907dcSJoerg Wunsch }
359d6f907dcSJoerg Wunsch
360d6f907dcSJoerg Wunsch static uid_t
pw_gidpolicy(struct userconf * cnf,char * grname,char * nam,gid_t prefer,bool dryrun)361d2d022b9SBaptiste Daroussin pw_gidpolicy(struct userconf *cnf, char *grname, char *nam, gid_t prefer, bool dryrun)
362d6f907dcSJoerg Wunsch {
363d6f907dcSJoerg Wunsch struct group *grp;
364d6f907dcSJoerg Wunsch gid_t gid = (uid_t) - 1;
365d6f907dcSJoerg Wunsch
366d6f907dcSJoerg Wunsch /*
367d6f907dcSJoerg Wunsch * Check the given gid, if any
368d6f907dcSJoerg Wunsch */
3695f12594aSDavid Nugent SETGRENT();
370d2d022b9SBaptiste Daroussin if (grname) {
371d2d022b9SBaptiste Daroussin if ((grp = GETGRNAM(grname)) == NULL) {
372d2d022b9SBaptiste Daroussin gid = pw_checkid(grname, GID_MAX);
373d2d022b9SBaptiste Daroussin grp = GETGRGID(gid);
374d6f907dcSJoerg Wunsch }
375d6f907dcSJoerg Wunsch gid = grp->gr_gid;
3761a8d37b8SNaman Sood } else if ((grp = GETGRNAM(nam)) != NULL) {
37748aee7f3SJoerg Wunsch gid = grp->gr_gid; /* Already created? Use it anyway... */
378d6f907dcSJoerg Wunsch } else {
379d2d022b9SBaptiste Daroussin intmax_t grid = -1;
380d6f907dcSJoerg Wunsch
381d6f907dcSJoerg Wunsch /*
382d6f907dcSJoerg Wunsch * We need to auto-create a group with the user's name. We
383d6f907dcSJoerg Wunsch * can send all the appropriate output to our sister routine
384d6f907dcSJoerg Wunsch * bit first see if we can create a group with gid==uid so we
385d6f907dcSJoerg Wunsch * can keep the user and group ids in sync. We purposely do
386d6f907dcSJoerg Wunsch * NOT check the gid range if we can force the sync. If the
387d6f907dcSJoerg Wunsch * user's name dups an existing group, then the group add
388d6f907dcSJoerg Wunsch * function will happily handle that case for us and exit.
389d6f907dcSJoerg Wunsch */
39052f92d8cSBaptiste Daroussin if (GETGRGID(prefer) == NULL)
39152f92d8cSBaptiste Daroussin grid = prefer;
392d2d022b9SBaptiste Daroussin if (dryrun) {
393f936ccb5SBaptiste Daroussin gid = pw_groupnext(cnf, true);
394f936ccb5SBaptiste Daroussin } else {
395d2d022b9SBaptiste Daroussin if (grid == -1)
396d2d022b9SBaptiste Daroussin grid = pw_groupnext(cnf, true);
397d2d022b9SBaptiste Daroussin groupadd(cnf, nam, grid, NULL, -1, false, false, false);
3985f12594aSDavid Nugent if ((grp = GETGRNAM(nam)) != NULL)
399d6f907dcSJoerg Wunsch gid = grp->gr_gid;
400d6f907dcSJoerg Wunsch }
40148aee7f3SJoerg Wunsch }
4025f12594aSDavid Nugent ENDGRENT();
403d2d022b9SBaptiste Daroussin return (gid);
404d6f907dcSJoerg Wunsch }
405d6f907dcSJoerg Wunsch
406d6f907dcSJoerg Wunsch static char *
pw_homepolicy(struct userconf * cnf,char * homedir,const char * user)407d2d022b9SBaptiste Daroussin pw_homepolicy(struct userconf * cnf, char *homedir, const char *user)
408d6f907dcSJoerg Wunsch {
4095d8ece9aSBaptiste Daroussin static char home[128];
410d6f907dcSJoerg Wunsch
411d2d022b9SBaptiste Daroussin if (homedir)
412d2d022b9SBaptiste Daroussin return (homedir);
413d6f907dcSJoerg Wunsch
414d6f907dcSJoerg Wunsch if (cnf->home == NULL || *cnf->home == '\0')
4151dcc6ec7SPhilippe Charnier errx(EX_CONFIG, "no base home directory set");
4165d8ece9aSBaptiste Daroussin snprintf(home, sizeof(home), "%s/%s", cnf->home, user);
4175d8ece9aSBaptiste Daroussin
4185d8ece9aSBaptiste Daroussin return (home);
419d6f907dcSJoerg Wunsch }
420d6f907dcSJoerg Wunsch
421d6f907dcSJoerg Wunsch static char *
shell_path(char const * path,char * shells[],char * sh)422d6f907dcSJoerg Wunsch shell_path(char const * path, char *shells[], char *sh)
423d6f907dcSJoerg Wunsch {
424d6f907dcSJoerg Wunsch if (sh != NULL && (*sh == '/' || *sh == '\0'))
425d6f907dcSJoerg Wunsch return sh; /* specified full path or forced none */
426d6f907dcSJoerg Wunsch else {
427d6f907dcSJoerg Wunsch char *p;
428d6f907dcSJoerg Wunsch char paths[_UC_MAXLINE];
429d6f907dcSJoerg Wunsch
430d6f907dcSJoerg Wunsch /*
431d6f907dcSJoerg Wunsch * We need to search paths
432d6f907dcSJoerg Wunsch */
433b8938b66SRobert Drehmel strlcpy(paths, path, sizeof(paths));
434d6f907dcSJoerg Wunsch for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
435d6f907dcSJoerg Wunsch int i;
436d6f907dcSJoerg Wunsch static char shellpath[256];
437d6f907dcSJoerg Wunsch
438d6f907dcSJoerg Wunsch if (sh != NULL) {
4392518db8bSBaptiste Daroussin snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh);
440d6f907dcSJoerg Wunsch if (access(shellpath, X_OK) == 0)
441d6f907dcSJoerg Wunsch return shellpath;
442d6f907dcSJoerg Wunsch } else
443d6f907dcSJoerg Wunsch for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
4442518db8bSBaptiste Daroussin snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]);
445d6f907dcSJoerg Wunsch if (access(shellpath, X_OK) == 0)
446d6f907dcSJoerg Wunsch return shellpath;
447d6f907dcSJoerg Wunsch }
448d6f907dcSJoerg Wunsch }
449d6f907dcSJoerg Wunsch if (sh == NULL)
4501dcc6ec7SPhilippe Charnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
4511dcc6ec7SPhilippe Charnier errx(EX_CONFIG, "no default shell available or defined");
452d6f907dcSJoerg Wunsch return NULL;
453d6f907dcSJoerg Wunsch }
454d6f907dcSJoerg Wunsch }
455d6f907dcSJoerg Wunsch
456d6f907dcSJoerg Wunsch static char *
pw_shellpolicy(struct userconf * cnf)457d2d022b9SBaptiste Daroussin pw_shellpolicy(struct userconf * cnf)
458d6f907dcSJoerg Wunsch {
459d6f907dcSJoerg Wunsch
460d2d022b9SBaptiste Daroussin return shell_path(cnf->shelldir, cnf->shells, cnf->shell_default);
461d6f907dcSJoerg Wunsch }
462d6f907dcSJoerg Wunsch
463e8e8c216SAntoine Brodin #define SALTSIZE 32
464e8e8c216SAntoine Brodin
465e8e8c216SAntoine Brodin static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
466d6f907dcSJoerg Wunsch
467d6f907dcSJoerg Wunsch char *
pw_pwcrypt(char * password)468d6f907dcSJoerg Wunsch pw_pwcrypt(char *password)
469d6f907dcSJoerg Wunsch {
470d6f907dcSJoerg Wunsch int i;
471e8e8c216SAntoine Brodin char salt[SALTSIZE + 1];
47229dcf726SKevin Lo char *cryptpw;
473d6f907dcSJoerg Wunsch static char buf[256];
4740ce59aa8SAlan Somers size_t pwlen;
475d6f907dcSJoerg Wunsch
476d6f907dcSJoerg Wunsch /*
477d6f907dcSJoerg Wunsch * Calculate a salt value
478d6f907dcSJoerg Wunsch */
479e8e8c216SAntoine Brodin for (i = 0; i < SALTSIZE; i++)
480f70f5c20SAndrey A. Chernov salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
481e8e8c216SAntoine Brodin salt[SALTSIZE] = '\0';
482d6f907dcSJoerg Wunsch
48329dcf726SKevin Lo cryptpw = crypt(password, salt);
48429dcf726SKevin Lo if (cryptpw == NULL)
48529dcf726SKevin Lo errx(EX_CONFIG, "crypt(3) failure");
4860ce59aa8SAlan Somers pwlen = strlcpy(buf, cryptpw, sizeof(buf));
4870ce59aa8SAlan Somers assert(pwlen < sizeof(buf));
488daccabe9SAlan Somers return (buf);
489d6f907dcSJoerg Wunsch }
490d6f907dcSJoerg Wunsch
491d6f907dcSJoerg Wunsch static char *
pw_password(struct userconf * cnf,char const * user)4921cbe5012SEugene Grosbein pw_password(struct userconf * cnf, char const * user)
493d6f907dcSJoerg Wunsch {
494d6f907dcSJoerg Wunsch int i, l;
495d6f907dcSJoerg Wunsch char pwbuf[32];
496d6f907dcSJoerg Wunsch
497d6f907dcSJoerg Wunsch switch (cnf->default_password) {
498d30f122fSEugene Grosbein case P_NONE: /* No password at all! */
499d30f122fSEugene Grosbein return "";
500d30f122fSEugene Grosbein case P_RANDOM: /* Random password */
50127540537SKris Kennaway l = (arc4random() % 8 + 8); /* 8 - 16 chars */
502d6f907dcSJoerg Wunsch for (i = 0; i < l; i++)
503f70f5c20SAndrey A. Chernov pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
504d6f907dcSJoerg Wunsch pwbuf[i] = '\0';
505d6f907dcSJoerg Wunsch
506d6f907dcSJoerg Wunsch /*
507d6f907dcSJoerg Wunsch * We give this information back to the user
508d6f907dcSJoerg Wunsch */
5091cbe5012SEugene Grosbein if (conf.fd == -1) {
510e7161f36SAndrey A. Chernov if (isatty(STDOUT_FILENO))
5115e75134dSDavid Nugent printf("Password for '%s' is: ", user);
512d6f907dcSJoerg Wunsch printf("%s\n", pwbuf);
513d6f907dcSJoerg Wunsch fflush(stdout);
514d6f907dcSJoerg Wunsch }
515d6f907dcSJoerg Wunsch break;
516d30f122fSEugene Grosbein case P_YES: /* user's name */
517b8938b66SRobert Drehmel strlcpy(pwbuf, user, sizeof(pwbuf));
518d6f907dcSJoerg Wunsch break;
519d30f122fSEugene Grosbein case P_NO: /* No login - default */
520d30f122fSEugene Grosbein /* FALLTHROUGH */
521d30f122fSEugene Grosbein default:
522d30f122fSEugene Grosbein return "*";
523d6f907dcSJoerg Wunsch }
524d6f907dcSJoerg Wunsch return pw_pwcrypt(pwbuf);
525d6f907dcSJoerg Wunsch }
526d6f907dcSJoerg Wunsch
527581a66eaSBaptiste Daroussin static int
print_user(struct passwd * pwd,bool pretty,bool v7)528d2d022b9SBaptiste Daroussin print_user(struct passwd * pwd, bool pretty, bool v7)
529581a66eaSBaptiste Daroussin {
53048aee7f3SJoerg Wunsch int j;
531d6f907dcSJoerg Wunsch char *p;
5325f12594aSDavid Nugent struct group *grp = GETGRGID(pwd->pw_gid);
533d6f907dcSJoerg Wunsch char uname[60] = "User &", office[60] = "[None]",
534d6f907dcSJoerg Wunsch wphone[60] = "[None]", hphone[60] = "[None]";
5352e3db6fbSDavid Nugent char acexpire[32] = "[None]", pwexpire[32] = "[None]";
5362e3db6fbSDavid Nugent struct tm * tptr;
537d6f907dcSJoerg Wunsch
538d2d022b9SBaptiste Daroussin if (!pretty) {
539d2d022b9SBaptiste Daroussin p = v7 ? pw_make_v7(pwd) : pw_make(pwd);
540d2d022b9SBaptiste Daroussin printf("%s\n", p);
541d2d022b9SBaptiste Daroussin free(p);
542d2d022b9SBaptiste Daroussin return (EXIT_SUCCESS);
543d2d022b9SBaptiste Daroussin }
544d2d022b9SBaptiste Daroussin
545d6f907dcSJoerg Wunsch if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
546b8938b66SRobert Drehmel strlcpy(uname, p, sizeof(uname));
547d6f907dcSJoerg Wunsch if ((p = strtok(NULL, ",")) != NULL) {
548b8938b66SRobert Drehmel strlcpy(office, p, sizeof(office));
549d6f907dcSJoerg Wunsch if ((p = strtok(NULL, ",")) != NULL) {
550b8938b66SRobert Drehmel strlcpy(wphone, p, sizeof(wphone));
551d6f907dcSJoerg Wunsch if ((p = strtok(NULL, "")) != NULL) {
552d2d022b9SBaptiste Daroussin strlcpy(hphone, p, sizeof(hphone));
553d6f907dcSJoerg Wunsch }
554d6f907dcSJoerg Wunsch }
555d6f907dcSJoerg Wunsch }
556d6f907dcSJoerg Wunsch }
557d6f907dcSJoerg Wunsch /*
558d6f907dcSJoerg Wunsch * Handle '&' in gecos field
559d6f907dcSJoerg Wunsch */
560d6f907dcSJoerg Wunsch if ((p = strchr(uname, '&')) != NULL) {
561d6f907dcSJoerg Wunsch int l = strlen(pwd->pw_name);
562d6f907dcSJoerg Wunsch int m = strlen(p);
563d6f907dcSJoerg Wunsch
564d6f907dcSJoerg Wunsch memmove(p + l, p + 1, m);
565d6f907dcSJoerg Wunsch memmove(p, pwd->pw_name, l);
566e7161f36SAndrey A. Chernov *p = (char) toupper((unsigned char)*p);
567d6f907dcSJoerg Wunsch }
5682e3db6fbSDavid Nugent if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
569b01e74b7SAndrey A. Chernov strftime(acexpire, sizeof acexpire, "%c", tptr);
570e7161f36SAndrey A. Chernov if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
571b01e74b7SAndrey A. Chernov strftime(pwexpire, sizeof pwexpire, "%c", tptr);
57250698e67SBaptiste Daroussin printf("Login Name: %-15s #%-12ju Group: %-15s #%ju\n"
573d6f907dcSJoerg Wunsch " Full Name: %s\n"
5740970881fSDavid Nugent " Home: %-26.26s Class: %s\n"
5750970881fSDavid Nugent " Shell: %-26.26s Office: %s\n"
5760970881fSDavid Nugent "Work Phone: %-26.26s Home Phone: %s\n"
5770970881fSDavid Nugent "Acc Expire: %-26.26s Pwd Expire: %s\n",
57850698e67SBaptiste Daroussin pwd->pw_name, (uintmax_t)pwd->pw_uid,
57950698e67SBaptiste Daroussin grp ? grp->gr_name : "(invalid)", (uintmax_t)pwd->pw_gid,
580d6f907dcSJoerg Wunsch uname, pwd->pw_dir, pwd->pw_class,
5812e3db6fbSDavid Nugent pwd->pw_shell, office, wphone, hphone,
5822e3db6fbSDavid Nugent acexpire, pwexpire);
5835f12594aSDavid Nugent SETGRENT();
58448aee7f3SJoerg Wunsch j = 0;
585d2d022b9SBaptiste Daroussin while ((grp=GETGRENT()) != NULL) {
58648aee7f3SJoerg Wunsch int i = 0;
587bbd054cbSJulian Elischer if (grp->gr_mem != NULL) {
588d2d022b9SBaptiste Daroussin while (grp->gr_mem[i] != NULL) {
589d2d022b9SBaptiste Daroussin if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) {
59048aee7f3SJoerg Wunsch printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name);
59148aee7f3SJoerg Wunsch break;
592d6f907dcSJoerg Wunsch }
59348aee7f3SJoerg Wunsch ++i;
59448aee7f3SJoerg Wunsch }
59548aee7f3SJoerg Wunsch }
596bbd054cbSJulian Elischer }
5975f12594aSDavid Nugent ENDGRENT();
598e7161f36SAndrey A. Chernov printf("%s", j ? "\n" : "");
599d2d022b9SBaptiste Daroussin return (EXIT_SUCCESS);
600d6f907dcSJoerg Wunsch }
601d6f907dcSJoerg Wunsch
602d9b711b7SDavid Nugent char *
pw_checkname(char * name,int gecos)6031238a919SBaptiste Daroussin pw_checkname(char *name, int gecos)
604d6f907dcSJoerg Wunsch {
60584a0950aSGarance A Drosehn char showch[8];
6061238a919SBaptiste Daroussin const char *badchars, *ch, *showtype;
60784a0950aSGarance A Drosehn int reject;
608d6f907dcSJoerg Wunsch
60984a0950aSGarance A Drosehn ch = name;
61084a0950aSGarance A Drosehn reject = 0;
61184a0950aSGarance A Drosehn if (gecos) {
61284a0950aSGarance A Drosehn /* See if the name is valid as a gecos (comment) field. */
61332bd1c2bSDavid Bright badchars = ":";
61484a0950aSGarance A Drosehn showtype = "gecos field";
61584a0950aSGarance A Drosehn } else {
61684a0950aSGarance A Drosehn /* See if the name is valid as a userid or group. */
617e0155c69Spbuff badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\";";
61884a0950aSGarance A Drosehn showtype = "userid/group name";
61984a0950aSGarance A Drosehn /* Userids and groups can not have a leading '-'. */
62084a0950aSGarance A Drosehn if (*ch == '-')
62184a0950aSGarance A Drosehn reject = 1;
622d6f907dcSJoerg Wunsch }
62384a0950aSGarance A Drosehn if (!reject) {
62484a0950aSGarance A Drosehn while (*ch) {
6258ab28c4eSBaptiste Daroussin if (strchr(badchars, *ch) != NULL ||
6268ab28c4eSBaptiste Daroussin (!gecos && *ch < ' ') ||
62784a0950aSGarance A Drosehn *ch == 127) {
62884a0950aSGarance A Drosehn reject = 1;
62984a0950aSGarance A Drosehn break;
63084a0950aSGarance A Drosehn }
63184a0950aSGarance A Drosehn /* 8-bit characters are only allowed in GECOS fields */
63284a0950aSGarance A Drosehn if (!gecos && (*ch & 0x80)) {
63384a0950aSGarance A Drosehn reject = 1;
63484a0950aSGarance A Drosehn break;
63584a0950aSGarance A Drosehn }
63684a0950aSGarance A Drosehn ch++;
63784a0950aSGarance A Drosehn }
63884a0950aSGarance A Drosehn }
63984a0950aSGarance A Drosehn /*
64084a0950aSGarance A Drosehn * A `$' is allowed as the final character for userids and groups,
64184a0950aSGarance A Drosehn * mainly for the benefit of samba.
64284a0950aSGarance A Drosehn */
64384a0950aSGarance A Drosehn if (reject && !gecos) {
64484a0950aSGarance A Drosehn if (*ch == '$' && *(ch + 1) == '\0') {
64584a0950aSGarance A Drosehn reject = 0;
64684a0950aSGarance A Drosehn ch++;
64784a0950aSGarance A Drosehn }
64884a0950aSGarance A Drosehn }
64984a0950aSGarance A Drosehn if (reject) {
65084a0950aSGarance A Drosehn snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
65184a0950aSGarance A Drosehn ? "`%c'" : "0x%02x", *ch);
6525893527dSDimitry Andric errx(EX_DATAERR, "invalid character %s at position %td in %s",
65384a0950aSGarance A Drosehn showch, (ch - name), showtype);
65484a0950aSGarance A Drosehn }
65584a0950aSGarance A Drosehn if (!gecos && (ch - name) > LOGNAMESIZE)
656d2d022b9SBaptiste Daroussin errx(EX_USAGE, "name too long `%s' (max is %d)", name,
65784a0950aSGarance A Drosehn LOGNAMESIZE);
6581238a919SBaptiste Daroussin
6591238a919SBaptiste Daroussin return (name);
660d6f907dcSJoerg Wunsch }
661d6f907dcSJoerg Wunsch
662d6f907dcSJoerg Wunsch static void
rmat(uid_t uid)663d6f907dcSJoerg Wunsch rmat(uid_t uid)
664d6f907dcSJoerg Wunsch {
665d6f907dcSJoerg Wunsch DIR *d = opendir("/var/at/jobs");
666d6f907dcSJoerg Wunsch
667d6f907dcSJoerg Wunsch if (d != NULL) {
668d6f907dcSJoerg Wunsch struct dirent *e;
669d6f907dcSJoerg Wunsch
670d6f907dcSJoerg Wunsch while ((e = readdir(d)) != NULL) {
671d6f907dcSJoerg Wunsch struct stat st;
672*d2f1f71eSMike Karels pid_t pid;
673d6f907dcSJoerg Wunsch
674d6f907dcSJoerg Wunsch if (strncmp(e->d_name, ".lock", 5) != 0 &&
675d6f907dcSJoerg Wunsch stat(e->d_name, &st) == 0 &&
676d6f907dcSJoerg Wunsch !S_ISDIR(st.st_mode) &&
677d6f907dcSJoerg Wunsch st.st_uid == uid) {
678ef7d0eb9SBaptiste Daroussin const char *argv[] = {
679ef7d0eb9SBaptiste Daroussin "/usr/sbin/atrm",
680ef7d0eb9SBaptiste Daroussin e->d_name,
681ef7d0eb9SBaptiste Daroussin NULL
682ef7d0eb9SBaptiste Daroussin };
683*d2f1f71eSMike Karels if (posix_spawn(&pid, argv[0], NULL, NULL,
684ef7d0eb9SBaptiste Daroussin (char *const *) argv, environ)) {
685ef7d0eb9SBaptiste Daroussin warn("Failed to execute '%s %s'",
686ef7d0eb9SBaptiste Daroussin argv[0], argv[1]);
687*d2f1f71eSMike Karels } else
688*d2f1f71eSMike Karels (void) waitpid(pid, NULL, 0);
689d6f907dcSJoerg Wunsch }
690d6f907dcSJoerg Wunsch }
691d6f907dcSJoerg Wunsch closedir(d);
692d6f907dcSJoerg Wunsch }
693d6f907dcSJoerg Wunsch }
6940970881fSDavid Nugent
695d2d022b9SBaptiste Daroussin int
pw_user_next(int argc,char ** argv,char * name __unused)696d2d022b9SBaptiste Daroussin pw_user_next(int argc, char **argv, char *name __unused)
697d2d022b9SBaptiste Daroussin {
698d2d022b9SBaptiste Daroussin struct userconf *cnf = NULL;
699d2d022b9SBaptiste Daroussin const char *cfg = NULL;
700d2d022b9SBaptiste Daroussin int ch;
701d2d022b9SBaptiste Daroussin bool quiet = false;
702d2d022b9SBaptiste Daroussin uid_t next;
703d2d022b9SBaptiste Daroussin
704e1fc61c1SBaptiste Daroussin while ((ch = getopt(argc, argv, "C:q")) != -1) {
705d2d022b9SBaptiste Daroussin switch (ch) {
706d2d022b9SBaptiste Daroussin case 'C':
707d2d022b9SBaptiste Daroussin cfg = optarg;
708d2d022b9SBaptiste Daroussin break;
709d2d022b9SBaptiste Daroussin case 'q':
710bbec1b5aSAdrian Chadd quiet = true;
711d2d022b9SBaptiste Daroussin break;
712c0358afdSBaptiste Daroussin default:
713c8611932SDag-Erling Smørgrav usage();
714d2d022b9SBaptiste Daroussin }
715d2d022b9SBaptiste Daroussin }
716c8611932SDag-Erling Smørgrav argc -= optind;
717c8611932SDag-Erling Smørgrav argv += optind;
718c8611932SDag-Erling Smørgrav if (argc > 0)
719c8611932SDag-Erling Smørgrav usage();
720d2d022b9SBaptiste Daroussin
721d2d022b9SBaptiste Daroussin if (quiet)
722d2d022b9SBaptiste Daroussin freopen(_PATH_DEVNULL, "w", stderr);
723d2d022b9SBaptiste Daroussin
724d2d022b9SBaptiste Daroussin cnf = get_userconfig(cfg);
725d2d022b9SBaptiste Daroussin
726d2d022b9SBaptiste Daroussin next = pw_uidpolicy(cnf, -1);
727d2d022b9SBaptiste Daroussin
728d2d022b9SBaptiste Daroussin printf("%ju:", (uintmax_t)next);
729d2d022b9SBaptiste Daroussin pw_groupnext(cnf, quiet);
730d2d022b9SBaptiste Daroussin
731d2d022b9SBaptiste Daroussin return (EXIT_SUCCESS);
732d2d022b9SBaptiste Daroussin }
733d2d022b9SBaptiste Daroussin
734d2d022b9SBaptiste Daroussin int
pw_user_show(int argc,char ** argv,char * arg1)735d2d022b9SBaptiste Daroussin pw_user_show(int argc, char **argv, char *arg1)
736d2d022b9SBaptiste Daroussin {
737d2d022b9SBaptiste Daroussin struct passwd *pwd = NULL;
738d2d022b9SBaptiste Daroussin char *name = NULL;
7395e52b667SBaptiste Daroussin intmax_t id = -1;
740d2d022b9SBaptiste Daroussin int ch;
741d2d022b9SBaptiste Daroussin bool all = false;
742d2d022b9SBaptiste Daroussin bool pretty = false;
743d2d022b9SBaptiste Daroussin bool force = false;
744d2d022b9SBaptiste Daroussin bool v7 = false;
745d2d022b9SBaptiste Daroussin bool quiet = false;
746d2d022b9SBaptiste Daroussin
747d2d022b9SBaptiste Daroussin if (arg1 != NULL) {
748fc6ab770SEd Schouten if (arg1[strspn(arg1, "0123456789")] == '\0')
749d2d022b9SBaptiste Daroussin id = pw_checkid(arg1, UID_MAX);
750d2d022b9SBaptiste Daroussin else
751d2d022b9SBaptiste Daroussin name = arg1;
752d2d022b9SBaptiste Daroussin }
753d2d022b9SBaptiste Daroussin
754d2d022b9SBaptiste Daroussin while ((ch = getopt(argc, argv, "C:qn:u:FPa7")) != -1) {
755d2d022b9SBaptiste Daroussin switch (ch) {
756d2d022b9SBaptiste Daroussin case 'C':
757d2d022b9SBaptiste Daroussin /* ignore compatibility */
758d2d022b9SBaptiste Daroussin break;
759d2d022b9SBaptiste Daroussin case 'q':
760d2d022b9SBaptiste Daroussin quiet = true;
761d2d022b9SBaptiste Daroussin break;
762d2d022b9SBaptiste Daroussin case 'n':
763d2d022b9SBaptiste Daroussin name = optarg;
764d2d022b9SBaptiste Daroussin break;
765d2d022b9SBaptiste Daroussin case 'u':
766d2d022b9SBaptiste Daroussin id = pw_checkid(optarg, UID_MAX);
767d2d022b9SBaptiste Daroussin break;
768d2d022b9SBaptiste Daroussin case 'F':
769d2d022b9SBaptiste Daroussin force = true;
770d2d022b9SBaptiste Daroussin break;
771d2d022b9SBaptiste Daroussin case 'P':
772d2d022b9SBaptiste Daroussin pretty = true;
773d2d022b9SBaptiste Daroussin break;
774d2d022b9SBaptiste Daroussin case 'a':
775d2d022b9SBaptiste Daroussin all = true;
776d2d022b9SBaptiste Daroussin break;
7773d85e406SBaptiste Daroussin case '7':
778d2d022b9SBaptiste Daroussin v7 = true;
779d2d022b9SBaptiste Daroussin break;
780c0358afdSBaptiste Daroussin default:
781c8611932SDag-Erling Smørgrav usage();
782d2d022b9SBaptiste Daroussin }
783d2d022b9SBaptiste Daroussin }
784c8611932SDag-Erling Smørgrav argc -= optind;
785c8611932SDag-Erling Smørgrav argv += optind;
786c8611932SDag-Erling Smørgrav if (argc > 0)
787c8611932SDag-Erling Smørgrav usage();
788d2d022b9SBaptiste Daroussin
789d2d022b9SBaptiste Daroussin if (quiet)
790d2d022b9SBaptiste Daroussin freopen(_PATH_DEVNULL, "w", stderr);
791d2d022b9SBaptiste Daroussin
792d2d022b9SBaptiste Daroussin if (all) {
793d2d022b9SBaptiste Daroussin SETPWENT();
794d2d022b9SBaptiste Daroussin while ((pwd = GETPWENT()) != NULL)
795d2d022b9SBaptiste Daroussin print_user(pwd, pretty, v7);
796d2d022b9SBaptiste Daroussin ENDPWENT();
797d2d022b9SBaptiste Daroussin return (EXIT_SUCCESS);
798d2d022b9SBaptiste Daroussin }
799d2d022b9SBaptiste Daroussin
800d2d022b9SBaptiste Daroussin if (id < 0 && name == NULL)
801d2d022b9SBaptiste Daroussin errx(EX_DATAERR, "username or id required");
802d2d022b9SBaptiste Daroussin
803d2d022b9SBaptiste Daroussin pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
804d2d022b9SBaptiste Daroussin if (pwd == NULL) {
805d2d022b9SBaptiste Daroussin if (force) {
806d2d022b9SBaptiste Daroussin pwd = &fakeuser;
807d2d022b9SBaptiste Daroussin } else {
808d2d022b9SBaptiste Daroussin if (name == NULL)
809d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "no such uid `%ju'",
810d2d022b9SBaptiste Daroussin (uintmax_t) id);
811d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "no such user `%s'", name);
812d2d022b9SBaptiste Daroussin }
813d2d022b9SBaptiste Daroussin }
814d2d022b9SBaptiste Daroussin
815d2d022b9SBaptiste Daroussin return (print_user(pwd, pretty, v7));
816d2d022b9SBaptiste Daroussin }
817d2d022b9SBaptiste Daroussin
818d2d022b9SBaptiste Daroussin int
pw_user_del(int argc,char ** argv,char * arg1)819d2d022b9SBaptiste Daroussin pw_user_del(int argc, char **argv, char *arg1)
820d2d022b9SBaptiste Daroussin {
821d2d022b9SBaptiste Daroussin struct userconf *cnf = NULL;
822d2d022b9SBaptiste Daroussin struct passwd *pwd = NULL;
823d2d022b9SBaptiste Daroussin struct group *gr, *grp;
824d2d022b9SBaptiste Daroussin char *name = NULL;
825d2d022b9SBaptiste Daroussin char grname[MAXLOGNAME];
826d2d022b9SBaptiste Daroussin char *nispasswd = NULL;
827d2d022b9SBaptiste Daroussin char file[MAXPATHLEN];
828d2d022b9SBaptiste Daroussin char home[MAXPATHLEN];
829d2d022b9SBaptiste Daroussin const char *cfg = NULL;
830d2d022b9SBaptiste Daroussin struct stat st;
8315e52b667SBaptiste Daroussin intmax_t id = -1;
832d2d022b9SBaptiste Daroussin int ch, rc;
833d2d022b9SBaptiste Daroussin bool nis = false;
834d2d022b9SBaptiste Daroussin bool deletehome = false;
835d2d022b9SBaptiste Daroussin bool quiet = false;
836d2d022b9SBaptiste Daroussin
837d2d022b9SBaptiste Daroussin if (arg1 != NULL) {
838fc6ab770SEd Schouten if (arg1[strspn(arg1, "0123456789")] == '\0')
839d2d022b9SBaptiste Daroussin id = pw_checkid(arg1, UID_MAX);
840d2d022b9SBaptiste Daroussin else
841d2d022b9SBaptiste Daroussin name = arg1;
842d2d022b9SBaptiste Daroussin }
843d2d022b9SBaptiste Daroussin
844d2d022b9SBaptiste Daroussin while ((ch = getopt(argc, argv, "C:qn:u:rYy:")) != -1) {
845d2d022b9SBaptiste Daroussin switch (ch) {
846d2d022b9SBaptiste Daroussin case 'C':
847d2d022b9SBaptiste Daroussin cfg = optarg;
848d2d022b9SBaptiste Daroussin break;
849d2d022b9SBaptiste Daroussin case 'q':
850d2d022b9SBaptiste Daroussin quiet = true;
851d2d022b9SBaptiste Daroussin break;
852d2d022b9SBaptiste Daroussin case 'n':
853d2d022b9SBaptiste Daroussin name = optarg;
854d2d022b9SBaptiste Daroussin break;
855d2d022b9SBaptiste Daroussin case 'u':
856d2d022b9SBaptiste Daroussin id = pw_checkid(optarg, UID_MAX);
857d2d022b9SBaptiste Daroussin break;
858d2d022b9SBaptiste Daroussin case 'r':
859d2d022b9SBaptiste Daroussin deletehome = true;
860d2d022b9SBaptiste Daroussin break;
861d2d022b9SBaptiste Daroussin case 'y':
862d2d022b9SBaptiste Daroussin nispasswd = optarg;
863d2d022b9SBaptiste Daroussin break;
864d2d022b9SBaptiste Daroussin case 'Y':
865d2d022b9SBaptiste Daroussin nis = true;
866d2d022b9SBaptiste Daroussin break;
867c0358afdSBaptiste Daroussin default:
868c8611932SDag-Erling Smørgrav usage();
869d2d022b9SBaptiste Daroussin }
870d2d022b9SBaptiste Daroussin }
871c8611932SDag-Erling Smørgrav argc -= optind;
872c8611932SDag-Erling Smørgrav argv += optind;
873c8611932SDag-Erling Smørgrav if (argc > 0)
874c8611932SDag-Erling Smørgrav usage();
875d2d022b9SBaptiste Daroussin
876d2d022b9SBaptiste Daroussin if (quiet)
877d2d022b9SBaptiste Daroussin freopen(_PATH_DEVNULL, "w", stderr);
878d2d022b9SBaptiste Daroussin
879d2d022b9SBaptiste Daroussin if (id < 0 && name == NULL)
880d2d022b9SBaptiste Daroussin errx(EX_DATAERR, "username or id required");
881d2d022b9SBaptiste Daroussin
882d2d022b9SBaptiste Daroussin cnf = get_userconfig(cfg);
883d2d022b9SBaptiste Daroussin
884d2d022b9SBaptiste Daroussin if (nispasswd == NULL)
885d2d022b9SBaptiste Daroussin nispasswd = cnf->nispasswd;
886d2d022b9SBaptiste Daroussin
887d2d022b9SBaptiste Daroussin pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
888d2d022b9SBaptiste Daroussin if (pwd == NULL) {
889d2d022b9SBaptiste Daroussin if (name == NULL)
890d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "no such uid `%ju'", (uintmax_t) id);
891d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "no such user `%s'", name);
892d2d022b9SBaptiste Daroussin }
893d2d022b9SBaptiste Daroussin
894d2d022b9SBaptiste Daroussin if (PWF._altdir == PWF_REGULAR &&
895d2d022b9SBaptiste Daroussin ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) {
896d2d022b9SBaptiste Daroussin if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
897d2d022b9SBaptiste Daroussin if (!nis && nispasswd && *nispasswd != '/')
898d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "Cannot remove NIS user `%s'",
899d2d022b9SBaptiste Daroussin name);
900d2d022b9SBaptiste Daroussin } else {
901d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "Cannot remove non local user `%s'",
902d2d022b9SBaptiste Daroussin name);
903d2d022b9SBaptiste Daroussin }
904d2d022b9SBaptiste Daroussin }
905d2d022b9SBaptiste Daroussin
906d2d022b9SBaptiste Daroussin id = pwd->pw_uid;
907d2d022b9SBaptiste Daroussin if (name == NULL)
908d2d022b9SBaptiste Daroussin name = pwd->pw_name;
909d2d022b9SBaptiste Daroussin
910d2d022b9SBaptiste Daroussin if (strcmp(pwd->pw_name, "root") == 0)
911d2d022b9SBaptiste Daroussin errx(EX_DATAERR, "cannot remove user 'root'");
912d2d022b9SBaptiste Daroussin
913d2d022b9SBaptiste Daroussin if (!PWALTDIR()) {
914d2d022b9SBaptiste Daroussin /* Remove crontabs */
915d2d022b9SBaptiste Daroussin snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
916d2d022b9SBaptiste Daroussin if (access(file, F_OK) == 0) {
917ef7d0eb9SBaptiste Daroussin const char *argv[] = {
918ef7d0eb9SBaptiste Daroussin "crontab",
919ef7d0eb9SBaptiste Daroussin "-u",
920ef7d0eb9SBaptiste Daroussin pwd->pw_name,
921ef7d0eb9SBaptiste Daroussin "-r",
922ef7d0eb9SBaptiste Daroussin NULL
923ef7d0eb9SBaptiste Daroussin };
924*d2f1f71eSMike Karels pid_t pid;
925*d2f1f71eSMike Karels
926*d2f1f71eSMike Karels if (posix_spawnp(&pid, argv[0], NULL, NULL,
927ef7d0eb9SBaptiste Daroussin (char *const *) argv, environ)) {
928ef7d0eb9SBaptiste Daroussin warn("Failed to execute '%s %s'",
929ef7d0eb9SBaptiste Daroussin argv[0], argv[1]);
930*d2f1f71eSMike Karels } else
931*d2f1f71eSMike Karels (void) waitpid(pid, NULL, 0);
932d2d022b9SBaptiste Daroussin }
933d2d022b9SBaptiste Daroussin }
934d2d022b9SBaptiste Daroussin
935d2d022b9SBaptiste Daroussin /*
936d2d022b9SBaptiste Daroussin * Save these for later, since contents of pwd may be
937d2d022b9SBaptiste Daroussin * invalidated by deletion
938d2d022b9SBaptiste Daroussin */
939d2d022b9SBaptiste Daroussin snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
940d2d022b9SBaptiste Daroussin strlcpy(home, pwd->pw_dir, sizeof(home));
941d2d022b9SBaptiste Daroussin gr = GETGRGID(pwd->pw_gid);
942d2d022b9SBaptiste Daroussin if (gr != NULL)
943d2d022b9SBaptiste Daroussin strlcpy(grname, gr->gr_name, LOGNAMESIZE);
944d2d022b9SBaptiste Daroussin else
945d2d022b9SBaptiste Daroussin grname[0] = '\0';
946d2d022b9SBaptiste Daroussin
947d2d022b9SBaptiste Daroussin rc = delpwent(pwd);
948d2d022b9SBaptiste Daroussin if (rc == -1)
949d2d022b9SBaptiste Daroussin err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
950d2d022b9SBaptiste Daroussin else if (rc != 0)
951d2d022b9SBaptiste Daroussin err(EX_IOERR, "passwd update");
952d2d022b9SBaptiste Daroussin
953d2d022b9SBaptiste Daroussin if (nis && nispasswd && *nispasswd=='/') {
954d2d022b9SBaptiste Daroussin rc = delnispwent(nispasswd, name);
955d2d022b9SBaptiste Daroussin if (rc == -1)
956d2d022b9SBaptiste Daroussin warnx("WARNING: user '%s' does not exist in NIS passwd",
957d2d022b9SBaptiste Daroussin pwd->pw_name);
958d2d022b9SBaptiste Daroussin else if (rc != 0)
959d2d022b9SBaptiste Daroussin warn("WARNING: NIS passwd update");
960d2d022b9SBaptiste Daroussin }
961d2d022b9SBaptiste Daroussin
962d2d022b9SBaptiste Daroussin grp = GETGRNAM(name);
963d2d022b9SBaptiste Daroussin if (grp != NULL &&
964d2d022b9SBaptiste Daroussin (grp->gr_mem == NULL || *grp->gr_mem == NULL) &&
965d2d022b9SBaptiste Daroussin strcmp(name, grname) == 0)
966d2d022b9SBaptiste Daroussin delgrent(GETGRNAM(name));
967d2d022b9SBaptiste Daroussin SETGRENT();
968d2d022b9SBaptiste Daroussin while ((grp = GETGRENT()) != NULL) {
969d2d022b9SBaptiste Daroussin int i, j;
970d2d022b9SBaptiste Daroussin char group[MAXLOGNAME];
971d2d022b9SBaptiste Daroussin if (grp->gr_mem == NULL)
972d2d022b9SBaptiste Daroussin continue;
973d2d022b9SBaptiste Daroussin
974d2d022b9SBaptiste Daroussin for (i = 0; grp->gr_mem[i] != NULL; i++) {
975d2d022b9SBaptiste Daroussin if (strcmp(grp->gr_mem[i], name) != 0)
976d2d022b9SBaptiste Daroussin continue;
977d2d022b9SBaptiste Daroussin
978d2d022b9SBaptiste Daroussin for (j = i; grp->gr_mem[j] != NULL; j++)
979d2d022b9SBaptiste Daroussin grp->gr_mem[j] = grp->gr_mem[j+1];
980d2d022b9SBaptiste Daroussin strlcpy(group, grp->gr_name, MAXLOGNAME);
981d2d022b9SBaptiste Daroussin chggrent(group, grp);
982d2d022b9SBaptiste Daroussin }
983d2d022b9SBaptiste Daroussin }
984d2d022b9SBaptiste Daroussin ENDGRENT();
985d2d022b9SBaptiste Daroussin
986d2d022b9SBaptiste Daroussin pw_log(cnf, M_DELETE, W_USER, "%s(%ju) account removed", name,
987d2d022b9SBaptiste Daroussin (uintmax_t)id);
988d2d022b9SBaptiste Daroussin
989d2d022b9SBaptiste Daroussin /* Remove mail file */
990d2d022b9SBaptiste Daroussin if (PWALTDIR() != PWF_ALT)
991d2d022b9SBaptiste Daroussin unlinkat(conf.rootfd, file + 1, 0);
992d2d022b9SBaptiste Daroussin
993d2d022b9SBaptiste Daroussin /* Remove at jobs */
994d2d022b9SBaptiste Daroussin if (!PWALTDIR() && getpwuid(id) == NULL)
995d2d022b9SBaptiste Daroussin rmat(id);
996d2d022b9SBaptiste Daroussin
997d2d022b9SBaptiste Daroussin /* Remove home directory and contents */
998d2d022b9SBaptiste Daroussin if (PWALTDIR() != PWF_ALT && deletehome && *home == '/' &&
999d2d022b9SBaptiste Daroussin GETPWUID(id) == NULL &&
1000d2d022b9SBaptiste Daroussin fstatat(conf.rootfd, home + 1, &st, 0) != -1) {
1001d2d022b9SBaptiste Daroussin rm_r(conf.rootfd, home, id);
1002d2d022b9SBaptiste Daroussin pw_log(cnf, M_DELETE, W_USER, "%s(%ju) home '%s' %s"
1003d2d022b9SBaptiste Daroussin "removed", name, (uintmax_t)id, home,
1004d2d022b9SBaptiste Daroussin fstatat(conf.rootfd, home + 1, &st, 0) == -1 ? "" : "not "
1005d2d022b9SBaptiste Daroussin "completely ");
1006d2d022b9SBaptiste Daroussin }
1007d2d022b9SBaptiste Daroussin
1008d2d022b9SBaptiste Daroussin return (EXIT_SUCCESS);
1009d2d022b9SBaptiste Daroussin }
1010d2d022b9SBaptiste Daroussin
1011d2d022b9SBaptiste Daroussin int
pw_user_lock(int argc,char ** argv,char * arg1)1012d2d022b9SBaptiste Daroussin pw_user_lock(int argc, char **argv, char *arg1)
1013d2d022b9SBaptiste Daroussin {
1014d2d022b9SBaptiste Daroussin int ch;
1015d2d022b9SBaptiste Daroussin
1016d2d022b9SBaptiste Daroussin while ((ch = getopt(argc, argv, "Cq")) != -1) {
1017d2d022b9SBaptiste Daroussin switch (ch) {
1018d2d022b9SBaptiste Daroussin case 'C':
1019d2d022b9SBaptiste Daroussin case 'q':
1020d2d022b9SBaptiste Daroussin /* compatibility */
1021d2d022b9SBaptiste Daroussin break;
1022c0358afdSBaptiste Daroussin default:
1023c8611932SDag-Erling Smørgrav usage();
1024d2d022b9SBaptiste Daroussin }
1025d2d022b9SBaptiste Daroussin }
1026c8611932SDag-Erling Smørgrav argc -= optind;
1027c8611932SDag-Erling Smørgrav argv += optind;
1028c8611932SDag-Erling Smørgrav if (argc > 0)
1029c8611932SDag-Erling Smørgrav usage();
1030d2d022b9SBaptiste Daroussin
1031d2d022b9SBaptiste Daroussin return (pw_userlock(arg1, M_LOCK));
1032d2d022b9SBaptiste Daroussin }
1033d2d022b9SBaptiste Daroussin
1034d2d022b9SBaptiste Daroussin int
pw_user_unlock(int argc,char ** argv,char * arg1)1035d2d022b9SBaptiste Daroussin pw_user_unlock(int argc, char **argv, char *arg1)
1036d2d022b9SBaptiste Daroussin {
1037d2d022b9SBaptiste Daroussin int ch;
1038d2d022b9SBaptiste Daroussin
1039d2d022b9SBaptiste Daroussin while ((ch = getopt(argc, argv, "Cq")) != -1) {
1040d2d022b9SBaptiste Daroussin switch (ch) {
1041d2d022b9SBaptiste Daroussin case 'C':
1042d2d022b9SBaptiste Daroussin case 'q':
1043d2d022b9SBaptiste Daroussin /* compatibility */
1044d2d022b9SBaptiste Daroussin break;
1045c0358afdSBaptiste Daroussin default:
1046c8611932SDag-Erling Smørgrav usage();
1047d2d022b9SBaptiste Daroussin }
1048d2d022b9SBaptiste Daroussin }
1049c8611932SDag-Erling Smørgrav argc -= optind;
1050c8611932SDag-Erling Smørgrav argv += optind;
1051c8611932SDag-Erling Smørgrav if (argc > 0)
1052c8611932SDag-Erling Smørgrav usage();
1053d2d022b9SBaptiste Daroussin
1054d2d022b9SBaptiste Daroussin return (pw_userlock(arg1, M_UNLOCK));
1055d2d022b9SBaptiste Daroussin }
1056d2d022b9SBaptiste Daroussin
1057d2d022b9SBaptiste Daroussin static struct group *
group_from_name_or_id(char * name)1058d2d022b9SBaptiste Daroussin group_from_name_or_id(char *name)
1059d2d022b9SBaptiste Daroussin {
1060d2d022b9SBaptiste Daroussin const char *errstr = NULL;
1061d2d022b9SBaptiste Daroussin struct group *grp;
1062d2d022b9SBaptiste Daroussin uintmax_t id;
1063d2d022b9SBaptiste Daroussin
1064d2d022b9SBaptiste Daroussin if ((grp = GETGRNAM(name)) == NULL) {
1065d2d022b9SBaptiste Daroussin id = strtounum(name, 0, GID_MAX, &errstr);
1066d2d022b9SBaptiste Daroussin if (errstr)
1067d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "group `%s' does not exist", name);
1068d2d022b9SBaptiste Daroussin grp = GETGRGID(id);
1069d2d022b9SBaptiste Daroussin if (grp == NULL)
1070d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "group `%s' does not exist", name);
1071d2d022b9SBaptiste Daroussin }
1072d2d022b9SBaptiste Daroussin
1073d2d022b9SBaptiste Daroussin return (grp);
1074d2d022b9SBaptiste Daroussin }
1075d2d022b9SBaptiste Daroussin
1076d2d022b9SBaptiste Daroussin static void
split_groups(StringList ** groups,char * groupsstr)1077d2d022b9SBaptiste Daroussin split_groups(StringList **groups, char *groupsstr)
1078d2d022b9SBaptiste Daroussin {
1079d2d022b9SBaptiste Daroussin struct group *grp;
1080d2d022b9SBaptiste Daroussin char *p;
1081d2d022b9SBaptiste Daroussin char tok[] = ", \t";
1082d2d022b9SBaptiste Daroussin
1083d2d022b9SBaptiste Daroussin if (*groups == NULL)
1084d2d022b9SBaptiste Daroussin *groups = sl_init();
10853dc869c2SEd Maste for (p = strtok(groupsstr, tok); p != NULL; p = strtok(NULL, tok)) {
10863dc869c2SEd Maste grp = group_from_name_or_id(p);
1087d2d022b9SBaptiste Daroussin sl_add(*groups, newstr(grp->gr_name));
1088d2d022b9SBaptiste Daroussin }
1089d2d022b9SBaptiste Daroussin }
1090d2d022b9SBaptiste Daroussin
1091d2d022b9SBaptiste Daroussin static void
validate_grname(struct userconf * cnf,char * group)1092d2d022b9SBaptiste Daroussin validate_grname(struct userconf *cnf, char *group)
1093d2d022b9SBaptiste Daroussin {
1094d2d022b9SBaptiste Daroussin struct group *grp;
1095d2d022b9SBaptiste Daroussin
1096d2d022b9SBaptiste Daroussin if (group == NULL || *group == '\0') {
1097d2d022b9SBaptiste Daroussin cnf->default_group = "";
1098d2d022b9SBaptiste Daroussin return;
1099d2d022b9SBaptiste Daroussin }
1100d2d022b9SBaptiste Daroussin grp = group_from_name_or_id(group);
1101d2d022b9SBaptiste Daroussin cnf->default_group = newstr(grp->gr_name);
1102d2d022b9SBaptiste Daroussin }
1103d2d022b9SBaptiste Daroussin
1104d2d022b9SBaptiste Daroussin static mode_t
validate_mode(char * mode)1105d2d022b9SBaptiste Daroussin validate_mode(char *mode)
1106d2d022b9SBaptiste Daroussin {
1107d2d022b9SBaptiste Daroussin mode_t m;
1108d2d022b9SBaptiste Daroussin void *set;
1109d2d022b9SBaptiste Daroussin
1110d2d022b9SBaptiste Daroussin if ((set = setmode(mode)) == NULL)
1111d2d022b9SBaptiste Daroussin errx(EX_DATAERR, "invalid directory creation mode '%s'", mode);
1112d2d022b9SBaptiste Daroussin
1113d2d022b9SBaptiste Daroussin m = getmode(set, _DEF_DIRMODE);
1114d2d022b9SBaptiste Daroussin free(set);
1115d2d022b9SBaptiste Daroussin return (m);
1116d2d022b9SBaptiste Daroussin }
1117d2d022b9SBaptiste Daroussin
1118d30f122fSEugene Grosbein static long
validate_expire(char * str,int opt)1119d30f122fSEugene Grosbein validate_expire(char *str, int opt)
1120d30f122fSEugene Grosbein {
1121d30f122fSEugene Grosbein if (!numerics(str))
1122d30f122fSEugene Grosbein errx(EX_DATAERR, "-%c argument must be numeric "
1123d30f122fSEugene Grosbein "when setting defaults: %s", (char)opt, str);
1124d30f122fSEugene Grosbein return strtol(str, NULL, 0);
1125d30f122fSEugene Grosbein }
1126d30f122fSEugene Grosbein
1127d2d022b9SBaptiste Daroussin static void
mix_config(struct userconf * cmdcnf,struct userconf * cfg)1128d2d022b9SBaptiste Daroussin mix_config(struct userconf *cmdcnf, struct userconf *cfg)
1129d2d022b9SBaptiste Daroussin {
1130d2d022b9SBaptiste Daroussin
1131d30f122fSEugene Grosbein if (cmdcnf->default_password < 0)
1132d2d022b9SBaptiste Daroussin cmdcnf->default_password = cfg->default_password;
1133d2d022b9SBaptiste Daroussin if (cmdcnf->reuse_uids == 0)
1134d2d022b9SBaptiste Daroussin cmdcnf->reuse_uids = cfg->reuse_uids;
1135d2d022b9SBaptiste Daroussin if (cmdcnf->reuse_gids == 0)
1136d2d022b9SBaptiste Daroussin cmdcnf->reuse_gids = cfg->reuse_gids;
1137d2d022b9SBaptiste Daroussin if (cmdcnf->nispasswd == NULL)
1138d2d022b9SBaptiste Daroussin cmdcnf->nispasswd = cfg->nispasswd;
1139d2d022b9SBaptiste Daroussin if (cmdcnf->dotdir == NULL)
1140d2d022b9SBaptiste Daroussin cmdcnf->dotdir = cfg->dotdir;
1141d2d022b9SBaptiste Daroussin if (cmdcnf->newmail == NULL)
1142d2d022b9SBaptiste Daroussin cmdcnf->newmail = cfg->newmail;
1143d2d022b9SBaptiste Daroussin if (cmdcnf->logfile == NULL)
1144d2d022b9SBaptiste Daroussin cmdcnf->logfile = cfg->logfile;
1145d2d022b9SBaptiste Daroussin if (cmdcnf->home == NULL)
1146d2d022b9SBaptiste Daroussin cmdcnf->home = cfg->home;
1147d2d022b9SBaptiste Daroussin if (cmdcnf->homemode == 0)
1148d2d022b9SBaptiste Daroussin cmdcnf->homemode = cfg->homemode;
1149d2d022b9SBaptiste Daroussin if (cmdcnf->shelldir == NULL)
1150d2d022b9SBaptiste Daroussin cmdcnf->shelldir = cfg->shelldir;
1151d2d022b9SBaptiste Daroussin if (cmdcnf->shells == NULL)
1152d2d022b9SBaptiste Daroussin cmdcnf->shells = cfg->shells;
1153d2d022b9SBaptiste Daroussin if (cmdcnf->shell_default == NULL)
1154d2d022b9SBaptiste Daroussin cmdcnf->shell_default = cfg->shell_default;
1155d2d022b9SBaptiste Daroussin if (cmdcnf->default_group == NULL)
1156d2d022b9SBaptiste Daroussin cmdcnf->default_group = cfg->default_group;
1157d2d022b9SBaptiste Daroussin if (cmdcnf->groups == NULL)
1158d2d022b9SBaptiste Daroussin cmdcnf->groups = cfg->groups;
1159d2d022b9SBaptiste Daroussin if (cmdcnf->default_class == NULL)
1160d2d022b9SBaptiste Daroussin cmdcnf->default_class = cfg->default_class;
1161d2d022b9SBaptiste Daroussin if (cmdcnf->min_uid == 0)
1162d2d022b9SBaptiste Daroussin cmdcnf->min_uid = cfg->min_uid;
1163d2d022b9SBaptiste Daroussin if (cmdcnf->max_uid == 0)
1164d2d022b9SBaptiste Daroussin cmdcnf->max_uid = cfg->max_uid;
1165d2d022b9SBaptiste Daroussin if (cmdcnf->min_gid == 0)
1166d2d022b9SBaptiste Daroussin cmdcnf->min_gid = cfg->min_gid;
1167d2d022b9SBaptiste Daroussin if (cmdcnf->max_gid == 0)
1168d2d022b9SBaptiste Daroussin cmdcnf->max_gid = cfg->max_gid;
1169d30f122fSEugene Grosbein if (cmdcnf->expire_days < 0)
1170d2d022b9SBaptiste Daroussin cmdcnf->expire_days = cfg->expire_days;
1171d30f122fSEugene Grosbein if (cmdcnf->password_days < 0)
1172d2d022b9SBaptiste Daroussin cmdcnf->password_days = cfg->password_days;
1173d2d022b9SBaptiste Daroussin }
1174d2d022b9SBaptiste Daroussin
1175d2d022b9SBaptiste Daroussin int
pw_user_add(int argc,char ** argv,char * arg1)1176d2d022b9SBaptiste Daroussin pw_user_add(int argc, char **argv, char *arg1)
1177d2d022b9SBaptiste Daroussin {
1178d2d022b9SBaptiste Daroussin struct userconf *cnf, *cmdcnf;
1179d2d022b9SBaptiste Daroussin struct passwd *pwd;
1180d2d022b9SBaptiste Daroussin struct group *grp;
1181d2d022b9SBaptiste Daroussin struct stat st;
1182d2d022b9SBaptiste Daroussin char args[] = "C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y";
1183d2d022b9SBaptiste Daroussin char line[_PASSWORD_LEN+1], path[MAXPATHLEN];
1184d2d022b9SBaptiste Daroussin char *gecos, *homedir, *skel, *walk, *userid, *groupid, *grname;
1185d2d022b9SBaptiste Daroussin char *default_passwd, *name, *p;
118692bb425eSEugene Grosbein const char *cfg = NULL;
1187d2d022b9SBaptiste Daroussin login_cap_t *lc;
1188d2d022b9SBaptiste Daroussin FILE *pfp, *fp;
1189d2d022b9SBaptiste Daroussin intmax_t id = -1;
1190d2d022b9SBaptiste Daroussin time_t now;
1191d2d022b9SBaptiste Daroussin int rc, ch, fd = -1;
1192d2d022b9SBaptiste Daroussin size_t i;
1193d2d022b9SBaptiste Daroussin bool dryrun, nis, pretty, quiet, createhome, precrypted, genconf;
1194d2d022b9SBaptiste Daroussin
1195d2d022b9SBaptiste Daroussin dryrun = nis = pretty = quiet = createhome = precrypted = false;
1196d2d022b9SBaptiste Daroussin genconf = false;
1197d2d022b9SBaptiste Daroussin gecos = homedir = skel = userid = groupid = default_passwd = NULL;
1198d2d022b9SBaptiste Daroussin grname = name = NULL;
1199d2d022b9SBaptiste Daroussin
1200d2d022b9SBaptiste Daroussin if ((cmdcnf = calloc(1, sizeof(struct userconf))) == NULL)
1201d2d022b9SBaptiste Daroussin err(EXIT_FAILURE, "calloc()");
1202d2d022b9SBaptiste Daroussin
1203d30f122fSEugene Grosbein cmdcnf->default_password = cmdcnf->expire_days = cmdcnf->password_days = -1;
1204d30f122fSEugene Grosbein now = time(NULL);
1205d30f122fSEugene Grosbein
1206d2d022b9SBaptiste Daroussin if (arg1 != NULL) {
1207fc6ab770SEd Schouten if (arg1[strspn(arg1, "0123456789")] == '\0')
1208d2d022b9SBaptiste Daroussin id = pw_checkid(arg1, UID_MAX);
1209d2d022b9SBaptiste Daroussin else
12108d2dbd28SEd Maste name = pw_checkname(arg1, 0);
1211d2d022b9SBaptiste Daroussin }
1212d2d022b9SBaptiste Daroussin
1213d2d022b9SBaptiste Daroussin while ((ch = getopt(argc, argv, args)) != -1) {
1214d2d022b9SBaptiste Daroussin switch (ch) {
1215d2d022b9SBaptiste Daroussin case 'C':
1216d2d022b9SBaptiste Daroussin cfg = optarg;
1217d2d022b9SBaptiste Daroussin break;
1218d2d022b9SBaptiste Daroussin case 'q':
1219d2d022b9SBaptiste Daroussin quiet = true;
1220d2d022b9SBaptiste Daroussin break;
1221d2d022b9SBaptiste Daroussin case 'n':
12228d2dbd28SEd Maste name = pw_checkname(optarg, 0);
1223d2d022b9SBaptiste Daroussin break;
1224d2d022b9SBaptiste Daroussin case 'u':
1225d2d022b9SBaptiste Daroussin userid = optarg;
1226d2d022b9SBaptiste Daroussin break;
1227d2d022b9SBaptiste Daroussin case 'c':
1228d2d022b9SBaptiste Daroussin gecos = pw_checkname(optarg, 1);
1229d2d022b9SBaptiste Daroussin break;
1230d2d022b9SBaptiste Daroussin case 'd':
1231d2d022b9SBaptiste Daroussin homedir = optarg;
1232d2d022b9SBaptiste Daroussin break;
1233d2d022b9SBaptiste Daroussin case 'e':
1234d30f122fSEugene Grosbein if (genconf)
1235d30f122fSEugene Grosbein cmdcnf->expire_days = validate_expire(optarg, ch);
1236d30f122fSEugene Grosbein else
1237d2d022b9SBaptiste Daroussin cmdcnf->expire_days = parse_date(now, optarg);
1238d2d022b9SBaptiste Daroussin break;
1239d2d022b9SBaptiste Daroussin case 'p':
1240d30f122fSEugene Grosbein if (genconf)
1241d30f122fSEugene Grosbein cmdcnf->password_days = validate_expire(optarg, ch);
1242d30f122fSEugene Grosbein else
1243d2d022b9SBaptiste Daroussin cmdcnf->password_days = parse_date(now, optarg);
1244d2d022b9SBaptiste Daroussin break;
1245d2d022b9SBaptiste Daroussin case 'g':
1246d2d022b9SBaptiste Daroussin validate_grname(cmdcnf, optarg);
1247d2d022b9SBaptiste Daroussin grname = optarg;
1248d2d022b9SBaptiste Daroussin break;
1249d2d022b9SBaptiste Daroussin case 'G':
1250d2d022b9SBaptiste Daroussin split_groups(&cmdcnf->groups, optarg);
1251d2d022b9SBaptiste Daroussin break;
1252d2d022b9SBaptiste Daroussin case 'm':
1253d2d022b9SBaptiste Daroussin createhome = true;
1254d2d022b9SBaptiste Daroussin break;
1255d2d022b9SBaptiste Daroussin case 'M':
1256d2d022b9SBaptiste Daroussin cmdcnf->homemode = validate_mode(optarg);
1257d2d022b9SBaptiste Daroussin break;
1258d2d022b9SBaptiste Daroussin case 'k':
1259d2d022b9SBaptiste Daroussin walk = skel = optarg;
1260d2d022b9SBaptiste Daroussin if (*walk == '/')
1261d2d022b9SBaptiste Daroussin walk++;
1262d2d022b9SBaptiste Daroussin if (fstatat(conf.rootfd, walk, &st, 0) == -1)
1263d2d022b9SBaptiste Daroussin errx(EX_OSFILE, "skeleton `%s' does not "
1264d2d022b9SBaptiste Daroussin "exists", skel);
1265d2d022b9SBaptiste Daroussin if (!S_ISDIR(st.st_mode))
1266d2d022b9SBaptiste Daroussin errx(EX_OSFILE, "skeleton `%s' is not a "
1267d2d022b9SBaptiste Daroussin "directory", skel);
1268d2d022b9SBaptiste Daroussin cmdcnf->dotdir = skel;
1269d2d022b9SBaptiste Daroussin break;
1270d2d022b9SBaptiste Daroussin case 's':
1271d2d022b9SBaptiste Daroussin cmdcnf->shell_default = optarg;
1272d2d022b9SBaptiste Daroussin break;
1273d2d022b9SBaptiste Daroussin case 'o':
1274d2d022b9SBaptiste Daroussin conf.checkduplicate = false;
1275d2d022b9SBaptiste Daroussin break;
1276d2d022b9SBaptiste Daroussin case 'L':
1277d2d022b9SBaptiste Daroussin cmdcnf->default_class = pw_checkname(optarg, 0);
1278d2d022b9SBaptiste Daroussin break;
1279d2d022b9SBaptiste Daroussin case 'i':
1280d2d022b9SBaptiste Daroussin groupid = optarg;
1281d2d022b9SBaptiste Daroussin break;
1282d2d022b9SBaptiste Daroussin case 'w':
1283d2d022b9SBaptiste Daroussin default_passwd = optarg;
1284d2d022b9SBaptiste Daroussin break;
1285d2d022b9SBaptiste Daroussin case 'H':
1286d2d022b9SBaptiste Daroussin if (fd != -1)
1287d2d022b9SBaptiste Daroussin errx(EX_USAGE, "'-h' and '-H' are mutually "
1288d2d022b9SBaptiste Daroussin "exclusive options");
1289d2d022b9SBaptiste Daroussin fd = pw_checkfd(optarg);
1290d2d022b9SBaptiste Daroussin precrypted = true;
1291d2d022b9SBaptiste Daroussin if (fd == '-')
1292d2d022b9SBaptiste Daroussin errx(EX_USAGE, "-H expects a file descriptor");
1293d2d022b9SBaptiste Daroussin break;
1294d2d022b9SBaptiste Daroussin case 'h':
1295d2d022b9SBaptiste Daroussin if (fd != -1)
1296d2d022b9SBaptiste Daroussin errx(EX_USAGE, "'-h' and '-H' are mutually "
1297d2d022b9SBaptiste Daroussin "exclusive options");
1298d2d022b9SBaptiste Daroussin fd = pw_checkfd(optarg);
1299d2d022b9SBaptiste Daroussin break;
1300d2d022b9SBaptiste Daroussin case 'D':
1301d2d022b9SBaptiste Daroussin genconf = true;
1302d2d022b9SBaptiste Daroussin break;
1303d2d022b9SBaptiste Daroussin case 'b':
1304d2d022b9SBaptiste Daroussin cmdcnf->home = optarg;
1305d2d022b9SBaptiste Daroussin break;
1306d2d022b9SBaptiste Daroussin case 'N':
1307d2d022b9SBaptiste Daroussin dryrun = true;
1308d2d022b9SBaptiste Daroussin break;
1309d2d022b9SBaptiste Daroussin case 'P':
1310d2d022b9SBaptiste Daroussin pretty = true;
1311d2d022b9SBaptiste Daroussin break;
1312d2d022b9SBaptiste Daroussin case 'y':
1313d2d022b9SBaptiste Daroussin cmdcnf->nispasswd = optarg;
1314d2d022b9SBaptiste Daroussin break;
1315d2d022b9SBaptiste Daroussin case 'Y':
1316d2d022b9SBaptiste Daroussin nis = true;
1317d2d022b9SBaptiste Daroussin break;
1318c0358afdSBaptiste Daroussin default:
1319c8611932SDag-Erling Smørgrav usage();
1320d2d022b9SBaptiste Daroussin }
1321d2d022b9SBaptiste Daroussin }
1322c8611932SDag-Erling Smørgrav argc -= optind;
1323c8611932SDag-Erling Smørgrav argv += optind;
1324c8611932SDag-Erling Smørgrav if (argc > 0)
1325c8611932SDag-Erling Smørgrav usage();
1326d2d022b9SBaptiste Daroussin
1327d2d022b9SBaptiste Daroussin if (geteuid() != 0 && ! dryrun)
1328d2d022b9SBaptiste Daroussin errx(EX_NOPERM, "you must be root");
1329d2d022b9SBaptiste Daroussin
1330d2d022b9SBaptiste Daroussin if (quiet)
1331d2d022b9SBaptiste Daroussin freopen(_PATH_DEVNULL, "w", stderr);
1332d2d022b9SBaptiste Daroussin
1333d2d022b9SBaptiste Daroussin cnf = get_userconfig(cfg);
1334d2d022b9SBaptiste Daroussin
1335d2d022b9SBaptiste Daroussin mix_config(cmdcnf, cnf);
1336d2d022b9SBaptiste Daroussin if (default_passwd)
13377c46e6efSAlan Somers cmdcnf->default_password = passwd_val(default_passwd,
1338d2d022b9SBaptiste Daroussin cnf->default_password);
1339d2d022b9SBaptiste Daroussin if (genconf) {
1340d2d022b9SBaptiste Daroussin if (name != NULL)
1341d2d022b9SBaptiste Daroussin errx(EX_DATAERR, "can't combine `-D' with `-n name'");
1342d2d022b9SBaptiste Daroussin if (userid != NULL) {
1343d2d022b9SBaptiste Daroussin if ((p = strtok(userid, ", \t")) != NULL)
1344d2d022b9SBaptiste Daroussin cmdcnf->min_uid = pw_checkid(p, UID_MAX);
1345d2d022b9SBaptiste Daroussin if (cmdcnf->min_uid == 0)
1346d2d022b9SBaptiste Daroussin cmdcnf->min_uid = 1000;
1347d2d022b9SBaptiste Daroussin if ((p = strtok(NULL, " ,\t")) != NULL)
1348d2d022b9SBaptiste Daroussin cmdcnf->max_uid = pw_checkid(p, UID_MAX);
1349d2d022b9SBaptiste Daroussin if (cmdcnf->max_uid == 0)
1350d2d022b9SBaptiste Daroussin cmdcnf->max_uid = 32000;
1351d2d022b9SBaptiste Daroussin }
1352d2d022b9SBaptiste Daroussin if (groupid != NULL) {
1353d2d022b9SBaptiste Daroussin if ((p = strtok(groupid, ", \t")) != NULL)
1354d2d022b9SBaptiste Daroussin cmdcnf->min_gid = pw_checkid(p, GID_MAX);
1355d2d022b9SBaptiste Daroussin if (cmdcnf->min_gid == 0)
1356d2d022b9SBaptiste Daroussin cmdcnf->min_gid = 1000;
1357d2d022b9SBaptiste Daroussin if ((p = strtok(NULL, " ,\t")) != NULL)
1358d2d022b9SBaptiste Daroussin cmdcnf->max_gid = pw_checkid(p, GID_MAX);
1359d2d022b9SBaptiste Daroussin if (cmdcnf->max_gid == 0)
1360d2d022b9SBaptiste Daroussin cmdcnf->max_gid = 32000;
1361d2d022b9SBaptiste Daroussin }
1362d2d022b9SBaptiste Daroussin if (write_userconfig(cmdcnf, cfg))
1363d2d022b9SBaptiste Daroussin return (EXIT_SUCCESS);
1364d2d022b9SBaptiste Daroussin err(EX_IOERR, "config update");
1365d2d022b9SBaptiste Daroussin }
1366d2d022b9SBaptiste Daroussin
1367d2d022b9SBaptiste Daroussin if (userid)
1368d2d022b9SBaptiste Daroussin id = pw_checkid(userid, UID_MAX);
1369d2d022b9SBaptiste Daroussin if (id < 0 && name == NULL)
1370d2d022b9SBaptiste Daroussin errx(EX_DATAERR, "user name or id required");
1371d2d022b9SBaptiste Daroussin
1372d2d022b9SBaptiste Daroussin if (name == NULL)
1373d2d022b9SBaptiste Daroussin errx(EX_DATAERR, "login name required");
1374d2d022b9SBaptiste Daroussin
1375a64b90fbSBaptiste Daroussin if (GETPWNAM(name) != NULL)
1376a64b90fbSBaptiste Daroussin errx(EX_DATAERR, "login name `%s' already exists", name);
1377a64b90fbSBaptiste Daroussin
137892bb425eSEugene Grosbein if (!grname)
137992bb425eSEugene Grosbein grname = cmdcnf->default_group;
138092bb425eSEugene Grosbein
1381d2d022b9SBaptiste Daroussin pwd = &fakeuser;
1382d2d022b9SBaptiste Daroussin pwd->pw_name = name;
1383d2d022b9SBaptiste Daroussin pwd->pw_class = cmdcnf->default_class ? cmdcnf->default_class : "";
1384d2d022b9SBaptiste Daroussin pwd->pw_uid = pw_uidpolicy(cmdcnf, id);
1385d2d022b9SBaptiste Daroussin pwd->pw_gid = pw_gidpolicy(cnf, grname, pwd->pw_name,
1386d2d022b9SBaptiste Daroussin (gid_t) pwd->pw_uid, dryrun);
1387d30f122fSEugene Grosbein
1388a2c08ce7SEugene Grosbein /* cmdcnf->password_days and cmdcnf->expire_days hold unixtime here */
1389d30f122fSEugene Grosbein if (cmdcnf->password_days > 0)
1390a2c08ce7SEugene Grosbein pwd->pw_change = cmdcnf->password_days;
1391d30f122fSEugene Grosbein if (cmdcnf->expire_days > 0)
1392a2c08ce7SEugene Grosbein pwd->pw_expire = cmdcnf->expire_days;
1393d30f122fSEugene Grosbein
1394d2d022b9SBaptiste Daroussin pwd->pw_dir = pw_homepolicy(cmdcnf, homedir, pwd->pw_name);
1395d2d022b9SBaptiste Daroussin pwd->pw_shell = pw_shellpolicy(cmdcnf);
1396d2d022b9SBaptiste Daroussin lc = login_getpwclass(pwd);
1397d2d022b9SBaptiste Daroussin if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
1398d2d022b9SBaptiste Daroussin warn("setting crypt(3) format");
1399d2d022b9SBaptiste Daroussin login_close(lc);
14001cbe5012SEugene Grosbein pwd->pw_passwd = pw_password(cmdcnf, pwd->pw_name);
1401d2d022b9SBaptiste Daroussin if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
1402d2d022b9SBaptiste Daroussin warnx("WARNING: new account `%s' has a uid of 0 "
1403d2d022b9SBaptiste Daroussin "(superuser access!)", pwd->pw_name);
1404d2d022b9SBaptiste Daroussin if (gecos)
1405d2d022b9SBaptiste Daroussin pwd->pw_gecos = gecos;
1406d2d022b9SBaptiste Daroussin
1407d2d022b9SBaptiste Daroussin if (fd != -1)
1408d2d022b9SBaptiste Daroussin pw_set_passwd(pwd, fd, precrypted, false);
1409d2d022b9SBaptiste Daroussin
1410d2d022b9SBaptiste Daroussin if (dryrun)
1411d2d022b9SBaptiste Daroussin return (print_user(pwd, pretty, false));
1412d2d022b9SBaptiste Daroussin
1413d2d022b9SBaptiste Daroussin if ((rc = addpwent(pwd)) != 0) {
1414d2d022b9SBaptiste Daroussin if (rc == -1)
1415d2d022b9SBaptiste Daroussin errx(EX_IOERR, "user '%s' already exists",
1416d2d022b9SBaptiste Daroussin pwd->pw_name);
1417d2d022b9SBaptiste Daroussin else if (rc != 0)
1418d2d022b9SBaptiste Daroussin err(EX_IOERR, "passwd file update");
1419d2d022b9SBaptiste Daroussin }
1420d2d022b9SBaptiste Daroussin if (nis && cmdcnf->nispasswd && *cmdcnf->nispasswd == '/') {
1421d2d022b9SBaptiste Daroussin printf("%s\n", cmdcnf->nispasswd);
1422d2d022b9SBaptiste Daroussin rc = addnispwent(cmdcnf->nispasswd, pwd);
1423d2d022b9SBaptiste Daroussin if (rc == -1)
14249261982dSBaptiste Daroussin warnx("User '%s' already exists in NIS passwd",
14259261982dSBaptiste Daroussin pwd->pw_name);
1426d2d022b9SBaptiste Daroussin else if (rc != 0)
1427d2d022b9SBaptiste Daroussin warn("NIS passwd update");
1428d2d022b9SBaptiste Daroussin /* NOTE: we treat NIS-only update errors as non-fatal */
1429d2d022b9SBaptiste Daroussin }
1430d2d022b9SBaptiste Daroussin
1431d2d022b9SBaptiste Daroussin if (cmdcnf->groups != NULL) {
1432d2d022b9SBaptiste Daroussin for (i = 0; i < cmdcnf->groups->sl_cur; i++) {
1433d2d022b9SBaptiste Daroussin grp = GETGRNAM(cmdcnf->groups->sl_str[i]);
143417839f45SNaman Sood /* gr_add doesn't check if new member is already in group */
143517839f45SNaman Sood if (grp_has_member(grp, pwd->pw_name))
143617839f45SNaman Sood continue;
1437d2d022b9SBaptiste Daroussin grp = gr_add(grp, pwd->pw_name);
1438d2d022b9SBaptiste Daroussin /*
1439d2d022b9SBaptiste Daroussin * grp can only be NULL in 2 cases:
1440d2d022b9SBaptiste Daroussin * - the new member is already a member
1441d2d022b9SBaptiste Daroussin * - a problem with memory occurs
1442d2d022b9SBaptiste Daroussin * in both cases we want to skip now.
1443d2d022b9SBaptiste Daroussin */
1444d2d022b9SBaptiste Daroussin if (grp == NULL)
1445d2d022b9SBaptiste Daroussin continue;
1446d2d022b9SBaptiste Daroussin chggrent(grp->gr_name, grp);
1447d2d022b9SBaptiste Daroussin free(grp);
1448d2d022b9SBaptiste Daroussin }
1449d2d022b9SBaptiste Daroussin }
1450d2d022b9SBaptiste Daroussin
1451d2d022b9SBaptiste Daroussin pwd = GETPWNAM(name);
1452d2d022b9SBaptiste Daroussin if (pwd == NULL)
1453d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "user '%s' disappeared during update", name);
1454d2d022b9SBaptiste Daroussin
1455d2d022b9SBaptiste Daroussin grp = GETGRGID(pwd->pw_gid);
1456d2d022b9SBaptiste Daroussin pw_log(cnf, M_ADD, W_USER, "%s(%ju):%s(%ju):%s:%s:%s",
1457d2d022b9SBaptiste Daroussin pwd->pw_name, (uintmax_t)pwd->pw_uid,
14589261982dSBaptiste Daroussin grp ? grp->gr_name : "unknown",
14599261982dSBaptiste Daroussin (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1),
1460d2d022b9SBaptiste Daroussin pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
1461d2d022b9SBaptiste Daroussin
1462d2d022b9SBaptiste Daroussin /*
1463d2d022b9SBaptiste Daroussin * let's touch and chown the user's mail file. This is not
1464d2d022b9SBaptiste Daroussin * strictly necessary under BSD with a 0755 maildir but it also
1465d2d022b9SBaptiste Daroussin * doesn't hurt anything to create the empty mailfile
1466d2d022b9SBaptiste Daroussin */
1467d2d022b9SBaptiste Daroussin if (PWALTDIR() != PWF_ALT) {
1468d2d022b9SBaptiste Daroussin snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR,
1469d2d022b9SBaptiste Daroussin pwd->pw_name);
1470d2d022b9SBaptiste Daroussin /* Preserve contents & mtime */
1471d2d022b9SBaptiste Daroussin close(openat(conf.rootfd, path +1, O_RDWR | O_CREAT, 0600));
1472d2d022b9SBaptiste Daroussin fchownat(conf.rootfd, path + 1, pwd->pw_uid, pwd->pw_gid,
1473d2d022b9SBaptiste Daroussin AT_SYMLINK_NOFOLLOW);
1474d2d022b9SBaptiste Daroussin }
1475d2d022b9SBaptiste Daroussin
1476d2d022b9SBaptiste Daroussin /*
1477d2d022b9SBaptiste Daroussin * Let's create and populate the user's home directory. Note
1478d2d022b9SBaptiste Daroussin * that this also `works' for editing users if -m is used, but
1479d2d022b9SBaptiste Daroussin * existing files will *not* be overwritten.
1480d2d022b9SBaptiste Daroussin */
1481d2d022b9SBaptiste Daroussin if (PWALTDIR() != PWF_ALT && createhome && pwd->pw_dir &&
1482d2d022b9SBaptiste Daroussin *pwd->pw_dir == '/' && pwd->pw_dir[1])
1483d2d022b9SBaptiste Daroussin create_and_populate_homedir(cmdcnf, pwd, cmdcnf->dotdir,
1484d2d022b9SBaptiste Daroussin cmdcnf->homemode, false);
1485d2d022b9SBaptiste Daroussin
1486d2d022b9SBaptiste Daroussin if (!PWALTDIR() && cmdcnf->newmail && *cmdcnf->newmail &&
1487d2d022b9SBaptiste Daroussin (fp = fopen(cnf->newmail, "r")) != NULL) {
1488d2d022b9SBaptiste Daroussin if ((pfp = popen(_PATH_SENDMAIL " -t", "w")) == NULL)
1489d2d022b9SBaptiste Daroussin warn("sendmail");
1490d2d022b9SBaptiste Daroussin else {
1491d2d022b9SBaptiste Daroussin fprintf(pfp, "From: root\n" "To: %s\n"
1492d2d022b9SBaptiste Daroussin "Subject: Welcome!\n\n", pwd->pw_name);
1493d2d022b9SBaptiste Daroussin while (fgets(line, sizeof(line), fp) != NULL) {
1494d2d022b9SBaptiste Daroussin /* Do substitutions? */
1495d2d022b9SBaptiste Daroussin fputs(line, pfp);
1496d2d022b9SBaptiste Daroussin }
1497d2d022b9SBaptiste Daroussin pclose(pfp);
1498d2d022b9SBaptiste Daroussin pw_log(cnf, M_ADD, W_USER, "%s(%ju) new user mail sent",
1499d2d022b9SBaptiste Daroussin pwd->pw_name, (uintmax_t)pwd->pw_uid);
1500d2d022b9SBaptiste Daroussin }
1501d2d022b9SBaptiste Daroussin fclose(fp);
1502d2d022b9SBaptiste Daroussin }
1503d2d022b9SBaptiste Daroussin
1504d2d022b9SBaptiste Daroussin if (nis && nis_update() == 0)
1505d2d022b9SBaptiste Daroussin pw_log(cnf, M_ADD, W_USER, "NIS maps updated");
1506d2d022b9SBaptiste Daroussin
1507d2d022b9SBaptiste Daroussin return (EXIT_SUCCESS);
1508d2d022b9SBaptiste Daroussin }
1509d2d022b9SBaptiste Daroussin
1510d2d022b9SBaptiste Daroussin int
pw_user_mod(int argc,char ** argv,char * arg1)1511d2d022b9SBaptiste Daroussin pw_user_mod(int argc, char **argv, char *arg1)
1512d2d022b9SBaptiste Daroussin {
1513d2d022b9SBaptiste Daroussin struct userconf *cnf;
1514d2d022b9SBaptiste Daroussin struct passwd *pwd;
1515d2d022b9SBaptiste Daroussin struct group *grp;
1516d2d022b9SBaptiste Daroussin StringList *groups = NULL;
1517d2d022b9SBaptiste Daroussin char args[] = "C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:NPYy:";
151892bb425eSEugene Grosbein const char *cfg = NULL;
1519d2d022b9SBaptiste Daroussin char *gecos, *homedir, *grname, *name, *newname, *walk, *skel, *shell;
1520d2d022b9SBaptiste Daroussin char *passwd, *class, *nispasswd;
1521d2d022b9SBaptiste Daroussin login_cap_t *lc;
1522d2d022b9SBaptiste Daroussin struct stat st;
1523d2d022b9SBaptiste Daroussin intmax_t id = -1;
1524d2d022b9SBaptiste Daroussin int ch, fd = -1;
1525d2d022b9SBaptiste Daroussin size_t i, j;
1526b11244e7SBaptiste Daroussin bool quiet, createhome, pretty, dryrun, nis, edited;
15275e52b667SBaptiste Daroussin bool precrypted;
1528d2d022b9SBaptiste Daroussin mode_t homemode = 0;
1529a2c08ce7SEugene Grosbein time_t expire_time, password_time, now;
1530d2d022b9SBaptiste Daroussin
1531a2c08ce7SEugene Grosbein expire_time = password_time = -1;
1532d2d022b9SBaptiste Daroussin gecos = homedir = grname = name = newname = skel = shell =NULL;
1533d2d022b9SBaptiste Daroussin passwd = NULL;
1534d2d022b9SBaptiste Daroussin class = nispasswd = NULL;
1535d2d022b9SBaptiste Daroussin quiet = createhome = pretty = dryrun = nis = precrypted = false;
1536b11244e7SBaptiste Daroussin edited = false;
1537d30f122fSEugene Grosbein now = time(NULL);
1538d2d022b9SBaptiste Daroussin
1539d2d022b9SBaptiste Daroussin if (arg1 != NULL) {
1540fc6ab770SEd Schouten if (arg1[strspn(arg1, "0123456789")] == '\0')
1541d2d022b9SBaptiste Daroussin id = pw_checkid(arg1, UID_MAX);
1542d2d022b9SBaptiste Daroussin else
1543d2d022b9SBaptiste Daroussin name = arg1;
1544d2d022b9SBaptiste Daroussin }
1545d2d022b9SBaptiste Daroussin
1546d2d022b9SBaptiste Daroussin while ((ch = getopt(argc, argv, args)) != -1) {
1547d2d022b9SBaptiste Daroussin switch (ch) {
1548d2d022b9SBaptiste Daroussin case 'C':
1549d2d022b9SBaptiste Daroussin cfg = optarg;
1550d2d022b9SBaptiste Daroussin break;
1551d2d022b9SBaptiste Daroussin case 'q':
1552d2d022b9SBaptiste Daroussin quiet = true;
1553d2d022b9SBaptiste Daroussin break;
1554d2d022b9SBaptiste Daroussin case 'n':
1555d2d022b9SBaptiste Daroussin name = optarg;
1556d2d022b9SBaptiste Daroussin break;
1557d2d022b9SBaptiste Daroussin case 'u':
1558d2d022b9SBaptiste Daroussin id = pw_checkid(optarg, UID_MAX);
1559d2d022b9SBaptiste Daroussin break;
1560d2d022b9SBaptiste Daroussin case 'c':
1561d2d022b9SBaptiste Daroussin gecos = pw_checkname(optarg, 1);
1562d2d022b9SBaptiste Daroussin break;
1563d2d022b9SBaptiste Daroussin case 'd':
1564d2d022b9SBaptiste Daroussin homedir = optarg;
1565d2d022b9SBaptiste Daroussin break;
1566d2d022b9SBaptiste Daroussin case 'e':
1567a2c08ce7SEugene Grosbein expire_time = parse_date(now, optarg);
1568d2d022b9SBaptiste Daroussin break;
1569d2d022b9SBaptiste Daroussin case 'p':
1570a2c08ce7SEugene Grosbein password_time = parse_date(now, optarg);
1571d2d022b9SBaptiste Daroussin break;
1572d2d022b9SBaptiste Daroussin case 'g':
1573d2d022b9SBaptiste Daroussin group_from_name_or_id(optarg);
1574d2d022b9SBaptiste Daroussin grname = optarg;
1575d2d022b9SBaptiste Daroussin break;
1576d2d022b9SBaptiste Daroussin case 'G':
1577d2d022b9SBaptiste Daroussin split_groups(&groups, optarg);
1578d2d022b9SBaptiste Daroussin break;
1579d2d022b9SBaptiste Daroussin case 'm':
1580d2d022b9SBaptiste Daroussin createhome = true;
1581d2d022b9SBaptiste Daroussin break;
1582d2d022b9SBaptiste Daroussin case 'M':
1583d2d022b9SBaptiste Daroussin homemode = validate_mode(optarg);
1584d2d022b9SBaptiste Daroussin break;
1585d2d022b9SBaptiste Daroussin case 'l':
1586d2d022b9SBaptiste Daroussin newname = optarg;
1587d2d022b9SBaptiste Daroussin break;
1588d2d022b9SBaptiste Daroussin case 'k':
1589d2d022b9SBaptiste Daroussin walk = skel = optarg;
1590d2d022b9SBaptiste Daroussin if (*walk == '/')
1591d2d022b9SBaptiste Daroussin walk++;
1592d2d022b9SBaptiste Daroussin if (fstatat(conf.rootfd, walk, &st, 0) == -1)
1593d2d022b9SBaptiste Daroussin errx(EX_OSFILE, "skeleton `%s' does not "
1594d2d022b9SBaptiste Daroussin "exists", skel);
1595d2d022b9SBaptiste Daroussin if (!S_ISDIR(st.st_mode))
1596d2d022b9SBaptiste Daroussin errx(EX_OSFILE, "skeleton `%s' is not a "
1597d2d022b9SBaptiste Daroussin "directory", skel);
1598d2d022b9SBaptiste Daroussin break;
1599d2d022b9SBaptiste Daroussin case 's':
1600d2d022b9SBaptiste Daroussin shell = optarg;
1601d2d022b9SBaptiste Daroussin break;
1602d2d022b9SBaptiste Daroussin case 'w':
1603d2d022b9SBaptiste Daroussin passwd = optarg;
1604d2d022b9SBaptiste Daroussin break;
1605d2d022b9SBaptiste Daroussin case 'L':
1606d2d022b9SBaptiste Daroussin class = pw_checkname(optarg, 0);
1607d2d022b9SBaptiste Daroussin break;
1608d2d022b9SBaptiste Daroussin case 'H':
1609d2d022b9SBaptiste Daroussin if (fd != -1)
1610d2d022b9SBaptiste Daroussin errx(EX_USAGE, "'-h' and '-H' are mutually "
1611d2d022b9SBaptiste Daroussin "exclusive options");
1612d2d022b9SBaptiste Daroussin fd = pw_checkfd(optarg);
1613d2d022b9SBaptiste Daroussin precrypted = true;
1614d2d022b9SBaptiste Daroussin if (fd == '-')
1615d2d022b9SBaptiste Daroussin errx(EX_USAGE, "-H expects a file descriptor");
1616d2d022b9SBaptiste Daroussin break;
1617d2d022b9SBaptiste Daroussin case 'h':
1618d2d022b9SBaptiste Daroussin if (fd != -1)
1619d2d022b9SBaptiste Daroussin errx(EX_USAGE, "'-h' and '-H' are mutually "
1620d2d022b9SBaptiste Daroussin "exclusive options");
1621d2d022b9SBaptiste Daroussin fd = pw_checkfd(optarg);
1622d2d022b9SBaptiste Daroussin break;
1623d2d022b9SBaptiste Daroussin case 'N':
1624d2d022b9SBaptiste Daroussin dryrun = true;
1625d2d022b9SBaptiste Daroussin break;
1626d2d022b9SBaptiste Daroussin case 'P':
1627d2d022b9SBaptiste Daroussin pretty = true;
1628d2d022b9SBaptiste Daroussin break;
1629d2d022b9SBaptiste Daroussin case 'y':
1630d2d022b9SBaptiste Daroussin nispasswd = optarg;
1631d2d022b9SBaptiste Daroussin break;
1632d2d022b9SBaptiste Daroussin case 'Y':
1633d2d022b9SBaptiste Daroussin nis = true;
1634d2d022b9SBaptiste Daroussin break;
1635c0358afdSBaptiste Daroussin default:
1636c8611932SDag-Erling Smørgrav usage();
1637d2d022b9SBaptiste Daroussin }
1638d2d022b9SBaptiste Daroussin }
1639c8611932SDag-Erling Smørgrav argc -= optind;
1640c8611932SDag-Erling Smørgrav argv += optind;
1641c8611932SDag-Erling Smørgrav if (argc > 0)
1642c8611932SDag-Erling Smørgrav usage();
1643d2d022b9SBaptiste Daroussin
1644d2d022b9SBaptiste Daroussin if (geteuid() != 0 && ! dryrun)
1645d2d022b9SBaptiste Daroussin errx(EX_NOPERM, "you must be root");
1646d2d022b9SBaptiste Daroussin
1647d2d022b9SBaptiste Daroussin if (quiet)
1648d2d022b9SBaptiste Daroussin freopen(_PATH_DEVNULL, "w", stderr);
1649d2d022b9SBaptiste Daroussin
1650d2d022b9SBaptiste Daroussin cnf = get_userconfig(cfg);
1651d2d022b9SBaptiste Daroussin
1652d2d022b9SBaptiste Daroussin if (id < 0 && name == NULL)
1653d2d022b9SBaptiste Daroussin errx(EX_DATAERR, "username or id required");
1654d2d022b9SBaptiste Daroussin
1655d2d022b9SBaptiste Daroussin pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
1656d2d022b9SBaptiste Daroussin if (pwd == NULL) {
1657d2d022b9SBaptiste Daroussin if (name == NULL)
1658d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "no such uid `%ju'",
1659d2d022b9SBaptiste Daroussin (uintmax_t) id);
1660d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "no such user `%s'", name);
1661d2d022b9SBaptiste Daroussin }
1662d2d022b9SBaptiste Daroussin
1663d2d022b9SBaptiste Daroussin if (name == NULL)
1664d2d022b9SBaptiste Daroussin name = pwd->pw_name;
1665d2d022b9SBaptiste Daroussin
1666d2d022b9SBaptiste Daroussin if (nis && nispasswd == NULL)
1667d2d022b9SBaptiste Daroussin nispasswd = cnf->nispasswd;
1668d2d022b9SBaptiste Daroussin
1669d2d022b9SBaptiste Daroussin if (PWF._altdir == PWF_REGULAR &&
1670d2d022b9SBaptiste Daroussin ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) {
1671d2d022b9SBaptiste Daroussin if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
1672d2d022b9SBaptiste Daroussin if (!nis && nispasswd && *nispasswd != '/')
1673d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "Cannot modify NIS user `%s'",
1674d2d022b9SBaptiste Daroussin name);
1675d2d022b9SBaptiste Daroussin } else {
1676d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "Cannot modify non local user `%s'",
1677d2d022b9SBaptiste Daroussin name);
1678d2d022b9SBaptiste Daroussin }
1679d2d022b9SBaptiste Daroussin }
1680d2d022b9SBaptiste Daroussin
1681d2d022b9SBaptiste Daroussin if (newname) {
1682d2d022b9SBaptiste Daroussin if (strcmp(pwd->pw_name, "root") == 0)
1683d2d022b9SBaptiste Daroussin errx(EX_DATAERR, "can't rename `root' account");
1684d2d022b9SBaptiste Daroussin if (strcmp(pwd->pw_name, newname) != 0) {
1685d2d022b9SBaptiste Daroussin pwd->pw_name = pw_checkname(newname, 0);
1686d2d022b9SBaptiste Daroussin edited = true;
1687d2d022b9SBaptiste Daroussin }
1688d2d022b9SBaptiste Daroussin }
1689d2d022b9SBaptiste Daroussin
1690429008a9SMark Johnston if (id >= 0 && pwd->pw_uid != id) {
1691d2d022b9SBaptiste Daroussin pwd->pw_uid = id;
1692d2d022b9SBaptiste Daroussin edited = true;
1693d2d022b9SBaptiste Daroussin if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
1694d2d022b9SBaptiste Daroussin errx(EX_DATAERR, "can't change uid of `root' account");
1695d2d022b9SBaptiste Daroussin if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
16969261982dSBaptiste Daroussin warnx("WARNING: account `%s' will have a uid of 0 "
16979261982dSBaptiste Daroussin "(superuser access!)", pwd->pw_name);
1698d2d022b9SBaptiste Daroussin }
1699d2d022b9SBaptiste Daroussin
1700d2d022b9SBaptiste Daroussin if (grname && pwd->pw_uid != 0) {
1701d2d022b9SBaptiste Daroussin grp = GETGRNAM(grname);
1702d2d022b9SBaptiste Daroussin if (grp == NULL)
1703d2d022b9SBaptiste Daroussin grp = GETGRGID(pw_checkid(grname, GID_MAX));
1704d2d022b9SBaptiste Daroussin if (grp->gr_gid != pwd->pw_gid) {
1705d2d022b9SBaptiste Daroussin pwd->pw_gid = grp->gr_gid;
1706d2d022b9SBaptiste Daroussin edited = true;
1707d2d022b9SBaptiste Daroussin }
1708d2d022b9SBaptiste Daroussin }
1709d2d022b9SBaptiste Daroussin
1710d30f122fSEugene Grosbein
1711a2c08ce7SEugene Grosbein if (password_time >= 0 && pwd->pw_change != password_time) {
1712a2c08ce7SEugene Grosbein pwd->pw_change = password_time;
1713d2d022b9SBaptiste Daroussin edited = true;
1714d2d022b9SBaptiste Daroussin }
1715d2d022b9SBaptiste Daroussin
1716a2c08ce7SEugene Grosbein if (expire_time >= 0 && pwd->pw_expire != expire_time) {
1717a2c08ce7SEugene Grosbein pwd->pw_expire = expire_time;
1718d2d022b9SBaptiste Daroussin edited = true;
1719d2d022b9SBaptiste Daroussin }
1720d2d022b9SBaptiste Daroussin
1721d2d022b9SBaptiste Daroussin if (shell) {
1722d2d022b9SBaptiste Daroussin shell = shell_path(cnf->shelldir, cnf->shells, shell);
1723d2d022b9SBaptiste Daroussin if (shell == NULL)
1724d2d022b9SBaptiste Daroussin shell = "";
1725d2d022b9SBaptiste Daroussin if (strcmp(shell, pwd->pw_shell) != 0) {
1726d2d022b9SBaptiste Daroussin pwd->pw_shell = shell;
1727d2d022b9SBaptiste Daroussin edited = true;
1728d2d022b9SBaptiste Daroussin }
1729d2d022b9SBaptiste Daroussin }
1730d2d022b9SBaptiste Daroussin
1731d2d022b9SBaptiste Daroussin if (class && strcmp(pwd->pw_class, class) != 0) {
1732d2d022b9SBaptiste Daroussin pwd->pw_class = class;
1733d2d022b9SBaptiste Daroussin edited = true;
1734d2d022b9SBaptiste Daroussin }
1735d2d022b9SBaptiste Daroussin
1736d2d022b9SBaptiste Daroussin if (homedir && strcmp(pwd->pw_dir, homedir) != 0) {
1737d2d022b9SBaptiste Daroussin pwd->pw_dir = homedir;
1738ec4f7bd9SBaptiste Daroussin edited = true;
1739d2d022b9SBaptiste Daroussin if (fstatat(conf.rootfd, pwd->pw_dir, &st, 0) == -1) {
1740d2d022b9SBaptiste Daroussin if (!createhome)
1741d2d022b9SBaptiste Daroussin warnx("WARNING: home `%s' does not exist",
1742d2d022b9SBaptiste Daroussin pwd->pw_dir);
1743d2d022b9SBaptiste Daroussin } else if (!S_ISDIR(st.st_mode)) {
1744d2d022b9SBaptiste Daroussin warnx("WARNING: home `%s' is not a directory",
1745d2d022b9SBaptiste Daroussin pwd->pw_dir);
1746d2d022b9SBaptiste Daroussin }
1747d2d022b9SBaptiste Daroussin }
1748d2d022b9SBaptiste Daroussin
1749d2d022b9SBaptiste Daroussin if (passwd && conf.fd == -1) {
1750d2d022b9SBaptiste Daroussin lc = login_getpwclass(pwd);
1751d2d022b9SBaptiste Daroussin if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
1752d2d022b9SBaptiste Daroussin warn("setting crypt(3) format");
1753d2d022b9SBaptiste Daroussin login_close(lc);
17547c46e6efSAlan Somers cnf->default_password = passwd_val(passwd,
17555770ce6fSBaptiste Daroussin cnf->default_password);
17561cbe5012SEugene Grosbein pwd->pw_passwd = pw_password(cnf, pwd->pw_name);
1757d2d022b9SBaptiste Daroussin edited = true;
1758d2d022b9SBaptiste Daroussin }
1759d2d022b9SBaptiste Daroussin
1760d2d022b9SBaptiste Daroussin if (gecos && strcmp(pwd->pw_gecos, gecos) != 0) {
1761d2d022b9SBaptiste Daroussin pwd->pw_gecos = gecos;
1762d2d022b9SBaptiste Daroussin edited = true;
1763d2d022b9SBaptiste Daroussin }
1764d2d022b9SBaptiste Daroussin
1765d2d022b9SBaptiste Daroussin if (fd != -1)
1766d2d022b9SBaptiste Daroussin edited = pw_set_passwd(pwd, fd, precrypted, true);
1767d2d022b9SBaptiste Daroussin
1768d2d022b9SBaptiste Daroussin if (dryrun)
1769d2d022b9SBaptiste Daroussin return (print_user(pwd, pretty, false));
1770d2d022b9SBaptiste Daroussin
1771d2d022b9SBaptiste Daroussin if (edited) /* Only updated this if required */
1772d2d022b9SBaptiste Daroussin perform_chgpwent(name, pwd, nis ? nispasswd : NULL);
1773d2d022b9SBaptiste Daroussin /* Now perform the needed changes concern groups */
1774d2d022b9SBaptiste Daroussin if (groups != NULL) {
1775d2d022b9SBaptiste Daroussin /* Delete User from groups using old name */
1776d2d022b9SBaptiste Daroussin SETGRENT();
1777d2d022b9SBaptiste Daroussin while ((grp = GETGRENT()) != NULL) {
1778d2d022b9SBaptiste Daroussin if (grp->gr_mem == NULL)
1779d2d022b9SBaptiste Daroussin continue;
1780d2d022b9SBaptiste Daroussin for (i = 0; grp->gr_mem[i] != NULL; i++) {
1781d2d022b9SBaptiste Daroussin if (strcmp(grp->gr_mem[i] , name) != 0)
1782d2d022b9SBaptiste Daroussin continue;
1783d2d022b9SBaptiste Daroussin for (j = i; grp->gr_mem[j] != NULL ; j++)
1784d2d022b9SBaptiste Daroussin grp->gr_mem[j] = grp->gr_mem[j+1];
1785d2d022b9SBaptiste Daroussin chggrent(grp->gr_name, grp);
1786d2d022b9SBaptiste Daroussin break;
1787d2d022b9SBaptiste Daroussin }
1788d2d022b9SBaptiste Daroussin }
1789d2d022b9SBaptiste Daroussin ENDGRENT();
1790d2d022b9SBaptiste Daroussin /* Add the user to the needed groups */
1791d2d022b9SBaptiste Daroussin for (i = 0; i < groups->sl_cur; i++) {
1792d2d022b9SBaptiste Daroussin grp = GETGRNAM(groups->sl_str[i]);
1793d2d022b9SBaptiste Daroussin grp = gr_add(grp, pwd->pw_name);
1794d2d022b9SBaptiste Daroussin if (grp == NULL)
1795d2d022b9SBaptiste Daroussin continue;
1796d2d022b9SBaptiste Daroussin chggrent(grp->gr_name, grp);
1797d2d022b9SBaptiste Daroussin free(grp);
1798d2d022b9SBaptiste Daroussin }
1799d2d022b9SBaptiste Daroussin }
1800d2d022b9SBaptiste Daroussin /* In case of rename we need to walk over the different groups */
1801d2d022b9SBaptiste Daroussin if (newname) {
1802d2d022b9SBaptiste Daroussin SETGRENT();
1803d2d022b9SBaptiste Daroussin while ((grp = GETGRENT()) != NULL) {
1804d2d022b9SBaptiste Daroussin if (grp->gr_mem == NULL)
1805d2d022b9SBaptiste Daroussin continue;
1806d2d022b9SBaptiste Daroussin for (i = 0; grp->gr_mem[i] != NULL; i++) {
1807d2d022b9SBaptiste Daroussin if (strcmp(grp->gr_mem[i], name) != 0)
1808d2d022b9SBaptiste Daroussin continue;
1809d2d022b9SBaptiste Daroussin grp->gr_mem[i] = newname;
1810d2d022b9SBaptiste Daroussin chggrent(grp->gr_name, grp);
1811d2d022b9SBaptiste Daroussin break;
1812d2d022b9SBaptiste Daroussin }
1813d2d022b9SBaptiste Daroussin }
1814d2d022b9SBaptiste Daroussin }
1815d2d022b9SBaptiste Daroussin
1816d2d022b9SBaptiste Daroussin /* go get a current version of pwd */
1817d2d022b9SBaptiste Daroussin if (newname)
1818d2d022b9SBaptiste Daroussin name = newname;
1819d2d022b9SBaptiste Daroussin pwd = GETPWNAM(name);
1820d2d022b9SBaptiste Daroussin if (pwd == NULL)
1821d2d022b9SBaptiste Daroussin errx(EX_NOUSER, "user '%s' disappeared during update", name);
1822d2d022b9SBaptiste Daroussin grp = GETGRGID(pwd->pw_gid);
1823a9ea647cSDag-Erling Smørgrav pw_log(cnf, M_MODIFY, W_USER, "%s(%ju):%s(%ju):%s:%s:%s",
1824d2d022b9SBaptiste Daroussin pwd->pw_name, (uintmax_t)pwd->pw_uid,
1825d2d022b9SBaptiste Daroussin grp ? grp->gr_name : "unknown",
1826d2d022b9SBaptiste Daroussin (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1),
1827d2d022b9SBaptiste Daroussin pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
1828d2d022b9SBaptiste Daroussin
1829d2d022b9SBaptiste Daroussin /*
1830d2d022b9SBaptiste Daroussin * Let's create and populate the user's home directory. Note
1831d2d022b9SBaptiste Daroussin * that this also `works' for editing users if -m is used, but
1832d2d022b9SBaptiste Daroussin * existing files will *not* be overwritten.
1833d2d022b9SBaptiste Daroussin */
1834b11244e7SBaptiste Daroussin if (PWALTDIR() != PWF_ALT && createhome && pwd->pw_dir &&
1835d2d022b9SBaptiste Daroussin *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
1836d2d022b9SBaptiste Daroussin if (!skel)
1837d2d022b9SBaptiste Daroussin skel = cnf->dotdir;
1838d2d022b9SBaptiste Daroussin if (homemode == 0)
1839d2d022b9SBaptiste Daroussin homemode = cnf->homemode;
1840d2d022b9SBaptiste Daroussin create_and_populate_homedir(cnf, pwd, skel, homemode, true);
1841d2d022b9SBaptiste Daroussin }
1842d2d022b9SBaptiste Daroussin
1843d2d022b9SBaptiste Daroussin if (nis && nis_update() == 0)
1844a9ea647cSDag-Erling Smørgrav pw_log(cnf, M_MODIFY, W_USER, "NIS maps updated");
1845d2d022b9SBaptiste Daroussin
1846d2d022b9SBaptiste Daroussin return (EXIT_SUCCESS);
1847d2d022b9SBaptiste Daroussin }
1848