pwupd.c revision 44229
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[] =
2944229Sdavidn	"$Id: pwupd.c,v 1.7 1998/02/11 23:31:24 wosch Exp $";
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
4620267Sjoerg
4744229Sdavidnstatic char pathpwd[] = _PATH_PWD;
4844229Sdavidnstatic char * pwpath = pathpwd;
4944229Sdavidn
5044229Sdavidnint
5144229Sdavidnsetpwdir(const char * dir)
5244229Sdavidn{
5344229Sdavidn	if (dir == NULL)
5444229Sdavidn		return -1;
5544229Sdavidn	else {
5644229Sdavidn		char * d = malloc(strlen(dir)+1);
5744229Sdavidn		if (d == NULL)
5844229Sdavidn			return -1;
5944229Sdavidn		pwpath = strcpy(d, dir);
6044229Sdavidn	}
6144229Sdavidn	return 0;
6244229Sdavidn}
6344229Sdavidn
6444229Sdavidnchar *
6544229Sdavidngetpwpath(char const * file)
6644229Sdavidn{
6744229Sdavidn	static char pathbuf[MAXPATHLEN];
6844229Sdavidn
6944229Sdavidn	snprintf(pathbuf, sizeof pathbuf, "%s/%s", pwpath, file);
7044229Sdavidn	return pathbuf;
7144229Sdavidn}
7244229Sdavidn
7344229Sdavidnint
7420253Sjoergpwdb(char *arg,...)
7520253Sjoerg{
7620253Sjoerg	int             i = 0;
7720253Sjoerg	pid_t           pid;
7820253Sjoerg	va_list         ap;
7944229Sdavidn	char           *args[10];
8020253Sjoerg
8120253Sjoerg	args[i++] = _PATH_PWD_MKDB;
8220253Sjoerg	va_start(ap, arg);
8320253Sjoerg	while (i < 6 && arg != NULL) {
8420253Sjoerg		args[i++] = arg;
8520253Sjoerg		arg = va_arg(ap, char *);
8620253Sjoerg	}
8744229Sdavidn	if (pwpath != pathpwd) {
8844229Sdavidn		args[i++] = "-d";
8944229Sdavidn		args[i++] = pwpath;
9044229Sdavidn	}
9144229Sdavidn	args[i++] = getpwpath(_MASTERPASSWD);
9220253Sjoerg	args[i] = NULL;
9320253Sjoerg
9420253Sjoerg	if ((pid = fork()) == -1)	/* Error (errno set) */
9520253Sjoerg		i = -1;
9620253Sjoerg	else if (pid == 0) {	/* Child */
9720253Sjoerg		execv(args[0], args);
9820253Sjoerg		_exit(1);
9920253Sjoerg	} else {		/* Parent */
10020253Sjoerg		waitpid(pid, &i, 0);
10120253Sjoerg		if ((i = WEXITSTATUS(i)) != 0)
10220253Sjoerg			errno = EIO;	/* set SOMETHING */
10320253Sjoerg	}
10420253Sjoerg	return i;
10520253Sjoerg}
10620253Sjoerg
10720253Sjoergint
10820253Sjoergfmtpwentry(char *buf, struct passwd * pwd, int type)
10920253Sjoerg{
11020253Sjoerg	int             l;
11120253Sjoerg	char           *pw;
11220253Sjoerg
11320253Sjoerg	pw = (pwd->pw_passwd == NULL || !*pwd->pw_passwd) ? "" : (type == PWF_MASTER) ? pwd->pw_passwd : "*";
11420253Sjoerg
11520253Sjoerg	if (type == PWF_PASSWD)
11620253Sjoerg		l = sprintf(buf, "%s:*:%ld:%ld:%s:%s:%s\n",
11720253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, (long) pwd->pw_gid,
11820253Sjoerg			    pwd->pw_gecos ? pwd->pw_gecos : "User &",
11920253Sjoerg			    pwd->pw_dir, pwd->pw_shell);
12020253Sjoerg	else
12120253Sjoerg		l = sprintf(buf, "%s:%s:%ld:%ld:%s:%lu:%lu:%s:%s:%s\n",
12220253Sjoerg		   pwd->pw_name, pw, (long) pwd->pw_uid, (long) pwd->pw_gid,
12320253Sjoerg			    pwd->pw_class ? pwd->pw_class : "",
12420253Sjoerg			    (unsigned long) pwd->pw_change,
12520253Sjoerg			    (unsigned long) pwd->pw_expire,
12620253Sjoerg			    pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
12720253Sjoerg	return l;
12820253Sjoerg}
12920253Sjoerg
13020253Sjoerg
13120253Sjoergint
13220253Sjoergfmtpwent(char *buf, struct passwd * pwd)
13320253Sjoerg{
13420253Sjoerg	return fmtpwentry(buf, pwd, PWF_STANDARD);
13520253Sjoerg}
13620253Sjoerg
13720253Sjoergstatic int
13820253Sjoergpw_update(struct passwd * pwd, char const * user, int mode)
13920253Sjoerg{
14020253Sjoerg	int             rc = 0;
14120253Sjoerg
14244229Sdavidn	ENDPWENT();
14320253Sjoerg
14420253Sjoerg	/*
14520253Sjoerg	 * First, let's check the see if the database is alright
14633259Swosch	 * Note: -C is only available in FreeBSD 2.2 and above
14720253Sjoerg	 */
14820267Sjoerg#ifdef HAVE_PWDB_C
14933259Swosch	if (pwdb("-C", NULL) == 0) {	/* Check only */
15020267Sjoerg#else
15133259Swosch	{				/* No -C */
15220267Sjoerg#endif
15320253Sjoerg		char            pfx[32];
15420747Sdavidn		char            pwbuf[PWBUFSZ];
15520253Sjoerg		int             l = sprintf(pfx, "%s:", user);
15620253Sjoerg
15720253Sjoerg		/*
15820253Sjoerg		 * Update the passwd file first
15920253Sjoerg		 */
16020253Sjoerg		if (pwd == NULL)
16120253Sjoerg			*pwbuf = '\0';
16220253Sjoerg		else
16320253Sjoerg			fmtpwentry(pwbuf, pwd, PWF_PASSWD);
16444229Sdavidn		if ((rc = fileupdate(getpwpath(_PASSWD), 0644, pwbuf, pfx, l, mode)) != 0) {
16520253Sjoerg
16620253Sjoerg			/*
16720253Sjoerg			 * Then the master.passwd file
16820253Sjoerg			 */
16920253Sjoerg			if (pwd != NULL)
17020253Sjoerg				fmtpwentry(pwbuf, pwd, PWF_MASTER);
17144229Sdavidn			if ((rc = fileupdate(getpwpath(_MASTERPASSWD), 0644, pwbuf, pfx, l, mode)) != 0)
17220253Sjoerg				rc = pwdb(NULL) == 0;
17320253Sjoerg		}
17420253Sjoerg	}
17520253Sjoerg	return rc;
17620253Sjoerg}
17720253Sjoerg
17820253Sjoergint
17920253Sjoergaddpwent(struct passwd * pwd)
18020253Sjoerg{
18120253Sjoerg	return pw_update(pwd, pwd->pw_name, UPD_CREATE);
18220253Sjoerg}
18320253Sjoerg
18420253Sjoergint
18520253Sjoergchgpwent(char const * login, struct passwd * pwd)
18620253Sjoerg{
18720253Sjoerg	return pw_update(pwd, login, UPD_REPLACE);
18820253Sjoerg}
18920253Sjoerg
19020253Sjoergint
19120253Sjoergdelpwent(struct passwd * pwd)
19220253Sjoerg{
19320253Sjoerg	return pw_update(NULL, pwd->pw_name, UPD_DELETE);
19420253Sjoerg}
195