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$";
3030259Scharnier#endif /* not lint */
3130259Scharnier
3220253Sjoerg#include <stdio.h>
3320253Sjoerg#include <stdlib.h>
3420253Sjoerg#include <string.h>
3520253Sjoerg#include <unistd.h>
3620253Sjoerg#include <stdarg.h>
37242349Sbapt#include <pwd.h>
38242349Sbapt#include <libutil.h>
3920253Sjoerg#include <errno.h>
40242349Sbapt#include <err.h>
4120253Sjoerg#include <sys/types.h>
4220253Sjoerg#include <sys/stat.h>
4344229Sdavidn#include <sys/param.h>
4420253Sjoerg#include <sys/wait.h>
4520253Sjoerg
4620253Sjoerg#include "pwupd.h"
4720253Sjoerg
4820267Sjoerg#define HAVE_PWDB_C	1
4961758Sdavidn#define	HAVE_PWDB_U	1
5020267Sjoerg
5144229Sdavidnstatic char pathpwd[] = _PATH_PWD;
5244229Sdavidnstatic char * pwpath = pathpwd;
5344229Sdavidn
5444229Sdavidnint
5544229Sdavidnsetpwdir(const char * dir)
5644229Sdavidn{
5744229Sdavidn	if (dir == NULL)
5844229Sdavidn		return -1;
59243896Seadler	else
60243896Seadler		pwpath = strdup(dir);
61243896Seadler	if (pwpath == NULL)
62243896Seadler		return -1;
6344229Sdavidn	return 0;
6444229Sdavidn}
6544229Sdavidn
6644229Sdavidnchar *
6744229Sdavidngetpwpath(char const * file)
6844229Sdavidn{
6944229Sdavidn	static char pathbuf[MAXPATHLEN];
7044229Sdavidn
7144229Sdavidn	snprintf(pathbuf, sizeof pathbuf, "%s/%s", pwpath, file);
7244229Sdavidn	return pathbuf;
7344229Sdavidn}
7444229Sdavidn
75242349Sbaptstatic int
7620253Sjoergpwdb(char *arg,...)
7720253Sjoerg{
7820253Sjoerg	int             i = 0;
7920253Sjoerg	pid_t           pid;
8020253Sjoerg	va_list         ap;
8144229Sdavidn	char           *args[10];
8220253Sjoerg
8320253Sjoerg	args[i++] = _PATH_PWD_MKDB;
8420253Sjoerg	va_start(ap, arg);
8520253Sjoerg	while (i < 6 && arg != NULL) {
8620253Sjoerg		args[i++] = arg;
8720253Sjoerg		arg = va_arg(ap, char *);
8820253Sjoerg	}
8944229Sdavidn	if (pwpath != pathpwd) {
9044229Sdavidn		args[i++] = "-d";
9144229Sdavidn		args[i++] = pwpath;
9244229Sdavidn	}
9344229Sdavidn	args[i++] = getpwpath(_MASTERPASSWD);
9420253Sjoerg	args[i] = NULL;
9520253Sjoerg
9620253Sjoerg	if ((pid = fork()) == -1)	/* Error (errno set) */
9752502Sdavidn		i = errno;
9820253Sjoerg	else if (pid == 0) {	/* Child */
9920253Sjoerg		execv(args[0], args);
10020253Sjoerg		_exit(1);
10120253Sjoerg	} else {		/* Parent */
10220253Sjoerg		waitpid(pid, &i, 0);
10352502Sdavidn		if (WEXITSTATUS(i))
10452502Sdavidn			i = EIO;
10520253Sjoerg	}
106241021Skevlo	va_end(ap);
10720253Sjoerg	return i;
10820253Sjoerg}
10920253Sjoerg
11020253Sjoergstatic int
111242349Sbaptpw_update(struct passwd * pwd, char const * user)
11220253Sjoerg{
11320253Sjoerg	int             rc = 0;
11420253Sjoerg
11520253Sjoerg	/*
11620253Sjoerg	 * First, let's check the see if the database is alright
11733259Swosch	 * Note: -C is only available in FreeBSD 2.2 and above
11820253Sjoerg	 */
11920267Sjoerg#ifdef HAVE_PWDB_C
120180565Sjhb	rc = pwdb("-C", (char *)NULL);	/* Check only */
121180565Sjhb	if (rc == 0) {
12220267Sjoerg#else
12333259Swosch	{				/* No -C */
12420267Sjoerg#endif
125242349Sbapt		int pfd, tfd;
126242349Sbapt		struct passwd *pw = NULL;
127242349Sbapt		struct passwd *old_pw = NULL;
12820253Sjoerg
129242349Sbapt	       	if (pwd != NULL)
130242349Sbapt		       pw = pw_dup(pwd);
13120253Sjoerg
132242349Sbapt		if (user != NULL)
133242349Sbapt			old_pw = GETPWNAM(user);
13452502Sdavidn
135242349Sbapt		if (pw_init(pwpath, NULL))
136242349Sbapt			err(1, "pw_init()");
137242349Sbapt		if ((pfd = pw_lock()) == -1) {
138242349Sbapt			pw_fini();
139242349Sbapt			err(1, "pw_lock()");
14020253Sjoerg		}
141242349Sbapt		if ((tfd = pw_tmp(-1)) == -1) {
142242349Sbapt			pw_fini();
143242349Sbapt			err(1, "pw_tmp()");
144242349Sbapt		}
145242349Sbapt		if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
146242349Sbapt			pw_fini();
147242349Sbapt			err(1, "pw_copy()");
148242349Sbapt		}
149244711Sbapt		/*
150244711Sbapt		 * in case of deletion of a user, the whole database
151244711Sbapt		 * needs to be regenerated
152244711Sbapt		 */
153244711Sbapt		if (pw_mkdb(pw != NULL ? user : NULL) == -1) {
154242349Sbapt			pw_fini();
155242349Sbapt			err(1, "pw_mkdb()");
156242349Sbapt		}
157242349Sbapt		free(pw);
158242349Sbapt		pw_fini();
15920253Sjoerg	}
160242349Sbapt	return 0;
16120253Sjoerg}
16220253Sjoerg
16320253Sjoergint
16420253Sjoergaddpwent(struct passwd * pwd)
16520253Sjoerg{
166242349Sbapt	return pw_update(pwd, NULL);
16720253Sjoerg}
16820253Sjoerg
16920253Sjoergint
17020253Sjoergchgpwent(char const * login, struct passwd * pwd)
17120253Sjoerg{
172242349Sbapt	return pw_update(pwd, login);
17320253Sjoerg}
17420253Sjoerg
17520253Sjoergint
17620253Sjoergdelpwent(struct passwd * pwd)
17720253Sjoerg{
178242349Sbapt	char login[MAXLOGNAME];
179242349Sbapt
180242349Sbapt	strlcpy(login, pwd->pw_name, MAXLOGNAME);
181242349Sbapt	return pw_update(NULL, login);
18220253Sjoerg}
183