120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  All rights reserved.
420253Sjoerg *
520253Sjoerg * Redistribution and use in source and binary forms, with or without
620253Sjoerg * modification, are permitted provided that the following conditions
720253Sjoerg * are met:
820253Sjoerg * 1. Redistributions of source code must retain the above copyright
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2520253Sjoerg */
2620253Sjoerg
2730259Scharnier#ifndef lint
2830259Scharnierstatic const char rcsid[] =
2950479Speter  "$FreeBSD: stable/10/usr.sbin/pw/pwupd.c 310173 2016-12-16 20:10:55Z asomers $";
3030259Scharnier#endif /* not lint */
3130259Scharnier
32287084Sbapt#include <sys/wait.h>
33287084Sbapt
34287084Sbapt#include <err.h>
35287084Sbapt#include <errno.h>
36287084Sbapt#include <pwd.h>
37287084Sbapt#include <libutil.h>
3820253Sjoerg#include <stdio.h>
3920253Sjoerg#include <stdlib.h>
4020253Sjoerg#include <string.h>
4120253Sjoerg#include <unistd.h>
4220253Sjoerg
4320253Sjoerg#include "pwupd.h"
4420253Sjoerg
4544229Sdavidnchar *
4644229Sdavidngetpwpath(char const * file)
4744229Sdavidn{
4844229Sdavidn	static char pathbuf[MAXPATHLEN];
4944229Sdavidn
50285092Sbapt	snprintf(pathbuf, sizeof pathbuf, "%s/%s", conf.etcpath, file);
51285092Sbapt
52285092Sbapt	return (pathbuf);
5344229Sdavidn}
5444229Sdavidn
55242349Sbaptstatic int
56285092Sbaptpwdb_check(void)
5720253Sjoerg{
5820253Sjoerg	int             i = 0;
5920253Sjoerg	pid_t           pid;
6044229Sdavidn	char           *args[10];
6120253Sjoerg
6220253Sjoerg	args[i++] = _PATH_PWD_MKDB;
63285092Sbapt	args[i++] = "-C";
64285092Sbapt
65285092Sbapt	if (strcmp(conf.etcpath, _PATH_PWD) != 0) {
6644229Sdavidn		args[i++] = "-d";
67285092Sbapt		args[i++] = conf.etcpath;
6844229Sdavidn	}
6944229Sdavidn	args[i++] = getpwpath(_MASTERPASSWD);
7020253Sjoerg	args[i] = NULL;
7120253Sjoerg
7220253Sjoerg	if ((pid = fork()) == -1)	/* Error (errno set) */
7352502Sdavidn		i = errno;
7420253Sjoerg	else if (pid == 0) {	/* Child */
7520253Sjoerg		execv(args[0], args);
7620253Sjoerg		_exit(1);
7720253Sjoerg	} else {		/* Parent */
7820253Sjoerg		waitpid(pid, &i, 0);
7952502Sdavidn		if (WEXITSTATUS(i))
8052502Sdavidn			i = EIO;
8120253Sjoerg	}
82285092Sbapt
83285092Sbapt	return (i);
8420253Sjoerg}
8520253Sjoerg
8620253Sjoergstatic int
87242349Sbaptpw_update(struct passwd * pwd, char const * user)
8820253Sjoerg{
89285092Sbapt	struct passwd	*pw = NULL;
90285092Sbapt	struct passwd	*old_pw = NULL;
91285092Sbapt	int		 rc, pfd, tfd;
9220253Sjoerg
93285092Sbapt	if ((rc = pwdb_check()) != 0)
94285092Sbapt		return (rc);
9520253Sjoerg
96285092Sbapt	if (pwd != NULL)
97285092Sbapt		pw = pw_dup(pwd);
9820253Sjoerg
99285092Sbapt	if (user != NULL)
100285092Sbapt		old_pw = GETPWNAM(user);
10152502Sdavidn
102285092Sbapt	if (pw_init(conf.etcpath, NULL))
103285092Sbapt		err(1, "pw_init()");
104285092Sbapt	if ((pfd = pw_lock()) == -1) {
105242349Sbapt		pw_fini();
106285092Sbapt		err(1, "pw_lock()");
10720253Sjoerg	}
108285092Sbapt	if ((tfd = pw_tmp(-1)) == -1) {
109285092Sbapt		pw_fini();
110285092Sbapt		err(1, "pw_tmp()");
111285092Sbapt	}
112285092Sbapt	if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
113285092Sbapt		pw_fini();
114308814Sasomers		close(tfd);
115285092Sbapt		err(1, "pw_copy()");
116285092Sbapt	}
117310173Sasomers	fsync(tfd);
118308814Sasomers	close(tfd);
119285092Sbapt	/*
120285092Sbapt	 * in case of deletion of a user, the whole database
121285092Sbapt	 * needs to be regenerated
122285092Sbapt	 */
123285092Sbapt	if (pw_mkdb(pw != NULL ? pw->pw_name : NULL) == -1) {
124285092Sbapt		pw_fini();
125285092Sbapt		err(1, "pw_mkdb()");
126285092Sbapt	}
127285092Sbapt	free(pw);
128285092Sbapt	pw_fini();
129285092Sbapt
130285092Sbapt	return (0);
13120253Sjoerg}
13220253Sjoerg
13320253Sjoergint
13420253Sjoergaddpwent(struct passwd * pwd)
13520253Sjoerg{
136285092Sbapt
137285092Sbapt	return (pw_update(pwd, NULL));
13820253Sjoerg}
13920253Sjoerg
14020253Sjoergint
14120253Sjoergchgpwent(char const * login, struct passwd * pwd)
14220253Sjoerg{
143285092Sbapt
144285092Sbapt	return (pw_update(pwd, login));
14520253Sjoerg}
14620253Sjoerg
14720253Sjoergint
14820253Sjoergdelpwent(struct passwd * pwd)
14920253Sjoerg{
150285092Sbapt
151285092Sbapt	return (pw_update(NULL, pwd->pw_name));
15220253Sjoerg}
153