pwupd.c revision 136087
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 136087 2004-10-03 14:40:27Z stefanf $";
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
114109749Sfjoe	pw = (type == PWF_MASTER) ?
115109749Sfjoe	    ((pwd->pw_passwd == NULL) ? "" : pwd->pw_passwd) : "*";
11620253Sjoerg
11720253Sjoerg	if (type == PWF_PASSWD)
11820253Sjoerg		l = sprintf(buf, "%s:*:%ld:%ld:%s:%s:%s\n",
11920253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, (long) pwd->pw_gid,
12020253Sjoerg			    pwd->pw_gecos ? pwd->pw_gecos : "User &",
12120253Sjoerg			    pwd->pw_dir, pwd->pw_shell);
12220253Sjoerg	else
12320253Sjoerg		l = sprintf(buf, "%s:%s:%ld:%ld:%s:%lu:%lu:%s:%s:%s\n",
12420253Sjoerg		   pwd->pw_name, pw, (long) pwd->pw_uid, (long) pwd->pw_gid,
12520253Sjoerg			    pwd->pw_class ? pwd->pw_class : "",
12620253Sjoerg			    (unsigned long) pwd->pw_change,
12720253Sjoerg			    (unsigned long) pwd->pw_expire,
12820253Sjoerg			    pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
12920253Sjoerg	return l;
13020253Sjoerg}
13120253Sjoerg
13220253Sjoerg
13320253Sjoergint
13420253Sjoergfmtpwent(char *buf, struct passwd * pwd)
13520253Sjoerg{
13620253Sjoerg	return fmtpwentry(buf, pwd, PWF_STANDARD);
13720253Sjoerg}
13820253Sjoerg
13920253Sjoergstatic int
14020253Sjoergpw_update(struct passwd * pwd, char const * user, int mode)
14120253Sjoerg{
14220253Sjoerg	int             rc = 0;
14320253Sjoerg
14444229Sdavidn	ENDPWENT();
14520253Sjoerg
14620253Sjoerg	/*
14720253Sjoerg	 * First, let's check the see if the database is alright
14833259Swosch	 * Note: -C is only available in FreeBSD 2.2 and above
14920253Sjoerg	 */
15020267Sjoerg#ifdef HAVE_PWDB_C
151136087Sstefanf	if (pwdb("-C", (char *)NULL) == 0) {	/* Check only */
15220267Sjoerg#else
15333259Swosch	{				/* No -C */
15420267Sjoerg#endif
15561758Sdavidn		char            pfx[PWBUFSZ];
15620747Sdavidn		char            pwbuf[PWBUFSZ];
15761758Sdavidn		int             l = snprintf(pfx, PWBUFSZ, "%s:", user);
15861758Sdavidn#ifdef HAVE_PWDB_U
15962097Sdavidn		int		isrename = pwd!=NULL && strcmp(user, pwd->pw_name);
16061758Sdavidn#endif
16120253Sjoerg
16220253Sjoerg		/*
16320253Sjoerg		 * Update the passwd file first
16420253Sjoerg		 */
16520253Sjoerg		if (pwd == NULL)
16620253Sjoerg			*pwbuf = '\0';
16720253Sjoerg		else
16820253Sjoerg			fmtpwentry(pwbuf, pwd, PWF_PASSWD);
16920253Sjoerg
17081982Sbrian		if (l < 0)
17181977Sbrian			l = 0;
17252502Sdavidn		rc = fileupdate(getpwpath(_PASSWD), 0644, pwbuf, pfx, l, mode);
17352502Sdavidn		if (rc == 0) {
17452502Sdavidn
17520253Sjoerg			/*
17620253Sjoerg			 * Then the master.passwd file
17720253Sjoerg			 */
17820253Sjoerg			if (pwd != NULL)
17920253Sjoerg				fmtpwentry(pwbuf, pwd, PWF_MASTER);
18088280Snectar			rc = fileupdate(getpwpath(_MASTERPASSWD), 0600, pwbuf, pfx, l, mode);
18152511Sdavidn			if (rc == 0) {
18261758Sdavidn#ifdef HAVE_PWDB_U
18361758Sdavidn				if (mode == UPD_DELETE || isrename)
18461758Sdavidn#endif
18552502Sdavidn					rc = pwdb(NULL);
18661758Sdavidn#ifdef HAVE_PWDB_U
18750653Ssheldonh				else
188136087Sstefanf					rc = pwdb("-u", user, (char *)NULL);
18961758Sdavidn#endif
19050653Ssheldonh			}
19120253Sjoerg		}
19220253Sjoerg	}
19320253Sjoerg	return rc;
19420253Sjoerg}
19520253Sjoerg
19620253Sjoergint
19720253Sjoergaddpwent(struct passwd * pwd)
19820253Sjoerg{
19920253Sjoerg	return pw_update(pwd, pwd->pw_name, UPD_CREATE);
20020253Sjoerg}
20120253Sjoerg
20220253Sjoergint
20320253Sjoergchgpwent(char const * login, struct passwd * pwd)
20420253Sjoerg{
20520253Sjoerg	return pw_update(pwd, login, UPD_REPLACE);
20620253Sjoerg}
20720253Sjoerg
20820253Sjoergint
20920253Sjoergdelpwent(struct passwd * pwd)
21020253Sjoerg{
21120253Sjoerg	return pw_update(NULL, pwd->pw_name, UPD_DELETE);
21220253Sjoerg}
213