pwupd.c revision 81982
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: head/usr.sbin/pw/pwupd.c 81982 2001-08-20 15:09:34Z brian $";
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>
3720253Sjoerg#include <errno.h>
3820253Sjoerg#include <sys/types.h>
3920253Sjoerg#include <sys/stat.h>
4044229Sdavidn#include <sys/param.h>
4120253Sjoerg#include <sys/wait.h>
4220253Sjoerg
4320253Sjoerg#include "pwupd.h"
4420253Sjoerg
4520267Sjoerg#define HAVE_PWDB_C	1
4661758Sdavidn#define	HAVE_PWDB_U	1
4720267Sjoerg
4844229Sdavidnstatic char pathpwd[] = _PATH_PWD;
4944229Sdavidnstatic char * pwpath = pathpwd;
5044229Sdavidn
5144229Sdavidnint
5244229Sdavidnsetpwdir(const char * dir)
5344229Sdavidn{
5444229Sdavidn	if (dir == NULL)
5544229Sdavidn		return -1;
5644229Sdavidn	else {
5744229Sdavidn		char * d = malloc(strlen(dir)+1);
5844229Sdavidn		if (d == NULL)
5944229Sdavidn			return -1;
6044229Sdavidn		pwpath = strcpy(d, dir);
6144229Sdavidn	}
6244229Sdavidn	return 0;
6344229Sdavidn}
6444229Sdavidn
6544229Sdavidnchar *
6644229Sdavidngetpwpath(char const * file)
6744229Sdavidn{
6844229Sdavidn	static char pathbuf[MAXPATHLEN];
6944229Sdavidn
7044229Sdavidn	snprintf(pathbuf, sizeof pathbuf, "%s/%s", pwpath, file);
7144229Sdavidn	return pathbuf;
7244229Sdavidn}
7344229Sdavidn
7444229Sdavidnint
7520253Sjoergpwdb(char *arg,...)
7620253Sjoerg{
7720253Sjoerg	int             i = 0;
7820253Sjoerg	pid_t           pid;
7920253Sjoerg	va_list         ap;
8044229Sdavidn	char           *args[10];
8120253Sjoerg
8220253Sjoerg	args[i++] = _PATH_PWD_MKDB;
8320253Sjoerg	va_start(ap, arg);
8420253Sjoerg	while (i < 6 && arg != NULL) {
8520253Sjoerg		args[i++] = arg;
8620253Sjoerg		arg = va_arg(ap, char *);
8720253Sjoerg	}
8844229Sdavidn	if (pwpath != pathpwd) {
8944229Sdavidn		args[i++] = "-d";
9044229Sdavidn		args[i++] = pwpath;
9144229Sdavidn	}
9244229Sdavidn	args[i++] = getpwpath(_MASTERPASSWD);
9320253Sjoerg	args[i] = NULL;
9420253Sjoerg
9520253Sjoerg	if ((pid = fork()) == -1)	/* Error (errno set) */
9652502Sdavidn		i = errno;
9720253Sjoerg	else if (pid == 0) {	/* Child */
9820253Sjoerg		execv(args[0], args);
9920253Sjoerg		_exit(1);
10020253Sjoerg	} else {		/* Parent */
10120253Sjoerg		waitpid(pid, &i, 0);
10252502Sdavidn		if (WEXITSTATUS(i))
10352502Sdavidn			i = EIO;
10420253Sjoerg	}
10520253Sjoerg	return i;
10620253Sjoerg}
10720253Sjoerg
10820253Sjoergint
10920253Sjoergfmtpwentry(char *buf, struct passwd * pwd, int type)
11020253Sjoerg{
11120253Sjoerg	int             l;
11220253Sjoerg	char           *pw;
11320253Sjoerg
11420253Sjoerg	pw = (pwd->pw_passwd == NULL || !*pwd->pw_passwd) ? "" : (type == PWF_MASTER) ? pwd->pw_passwd : "*";
11520253Sjoerg
11620253Sjoerg	if (type == PWF_PASSWD)
11720253Sjoerg		l = sprintf(buf, "%s:*:%ld:%ld:%s:%s:%s\n",
11820253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, (long) pwd->pw_gid,
11920253Sjoerg			    pwd->pw_gecos ? pwd->pw_gecos : "User &",
12020253Sjoerg			    pwd->pw_dir, pwd->pw_shell);
12120253Sjoerg	else
12220253Sjoerg		l = sprintf(buf, "%s:%s:%ld:%ld:%s:%lu:%lu:%s:%s:%s\n",
12320253Sjoerg		   pwd->pw_name, pw, (long) pwd->pw_uid, (long) pwd->pw_gid,
12420253Sjoerg			    pwd->pw_class ? pwd->pw_class : "",
12520253Sjoerg			    (unsigned long) pwd->pw_change,
12620253Sjoerg			    (unsigned long) pwd->pw_expire,
12720253Sjoerg			    pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
12820253Sjoerg	return l;
12920253Sjoerg}
13020253Sjoerg
13120253Sjoerg
13220253Sjoergint
13320253Sjoergfmtpwent(char *buf, struct passwd * pwd)
13420253Sjoerg{
13520253Sjoerg	return fmtpwentry(buf, pwd, PWF_STANDARD);
13620253Sjoerg}
13720253Sjoerg
13820253Sjoergstatic int
13920253Sjoergpw_update(struct passwd * pwd, char const * user, int mode)
14020253Sjoerg{
14120253Sjoerg	int             rc = 0;
14220253Sjoerg
14344229Sdavidn	ENDPWENT();
14420253Sjoerg
14520253Sjoerg	/*
14620253Sjoerg	 * First, let's check the see if the database is alright
14733259Swosch	 * Note: -C is only available in FreeBSD 2.2 and above
14820253Sjoerg	 */
14920267Sjoerg#ifdef HAVE_PWDB_C
15033259Swosch	if (pwdb("-C", NULL) == 0) {	/* Check only */
15120267Sjoerg#else
15233259Swosch	{				/* No -C */
15320267Sjoerg#endif
15461758Sdavidn		char            pfx[PWBUFSZ];
15520747Sdavidn		char            pwbuf[PWBUFSZ];
15661758Sdavidn		int             l = snprintf(pfx, PWBUFSZ, "%s:", user);
15761758Sdavidn#ifdef HAVE_PWDB_U
15862097Sdavidn		int		isrename = pwd!=NULL && strcmp(user, pwd->pw_name);
15961758Sdavidn#endif
16020253Sjoerg
16120253Sjoerg		/*
16220253Sjoerg		 * Update the passwd file first
16320253Sjoerg		 */
16420253Sjoerg		if (pwd == NULL)
16520253Sjoerg			*pwbuf = '\0';
16620253Sjoerg		else
16720253Sjoerg			fmtpwentry(pwbuf, pwd, PWF_PASSWD);
16820253Sjoerg
16981982Sbrian		if (l < 0)
17081977Sbrian			l = 0;
17152502Sdavidn		rc = fileupdate(getpwpath(_PASSWD), 0644, pwbuf, pfx, l, mode);
17252502Sdavidn		if (rc == 0) {
17352502Sdavidn
17420253Sjoerg			/*
17520253Sjoerg			 * Then the master.passwd file
17620253Sjoerg			 */
17720253Sjoerg			if (pwd != NULL)
17820253Sjoerg				fmtpwentry(pwbuf, pwd, PWF_MASTER);
17952502Sdavidn			rc = fileupdate(getpwpath(_MASTERPASSWD), 0644, pwbuf, pfx, l, mode);
18052511Sdavidn			if (rc == 0) {
18161758Sdavidn#ifdef HAVE_PWDB_U
18261758Sdavidn				if (mode == UPD_DELETE || isrename)
18361758Sdavidn#endif
18452502Sdavidn					rc = pwdb(NULL);
18561758Sdavidn#ifdef HAVE_PWDB_U
18650653Ssheldonh				else
18761758Sdavidn					rc = pwdb("-u", user, NULL);
18861758Sdavidn#endif
18950653Ssheldonh			}
19020253Sjoerg		}
19120253Sjoerg	}
19220253Sjoerg	return rc;
19320253Sjoerg}
19420253Sjoerg
19520253Sjoergint
19620253Sjoergaddpwent(struct passwd * pwd)
19720253Sjoerg{
19820253Sjoerg	return pw_update(pwd, pwd->pw_name, UPD_CREATE);
19920253Sjoerg}
20020253Sjoerg
20120253Sjoergint
20220253Sjoergchgpwent(char const * login, struct passwd * pwd)
20320253Sjoerg{
20420253Sjoerg	return pw_update(pwd, login, UPD_REPLACE);
20520253Sjoerg}
20620253Sjoerg
20720253Sjoergint
20820253Sjoergdelpwent(struct passwd * pwd)
20920253Sjoerg{
21020253Sjoerg	return pw_update(NULL, pwd->pw_name, UPD_DELETE);
21120253Sjoerg}
212