pwupd.c revision 33259
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[] =
2933259Swosch	"$Id: pwupd.c,v 1.6 1997/10/10 06:23:41 charnier 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>
4020253Sjoerg#include <sys/wait.h>
4120253Sjoerg
4220253Sjoerg#include "pwupd.h"
4320253Sjoerg
4420267Sjoerg#define HAVE_PWDB_C	1
4520267Sjoerg
4620253Sjoergstatic int
4720253Sjoergpwdb(char *arg,...)
4820253Sjoerg{
4920253Sjoerg	int             i = 0;
5020253Sjoerg	pid_t           pid;
5120253Sjoerg	va_list         ap;
5220253Sjoerg	char           *args[8];
5320253Sjoerg
5420253Sjoerg	args[i++] = _PATH_PWD_MKDB;
5520253Sjoerg	va_start(ap, arg);
5620253Sjoerg	while (i < 6 && arg != NULL) {
5720253Sjoerg		args[i++] = arg;
5820253Sjoerg		arg = va_arg(ap, char *);
5920253Sjoerg	}
6020253Sjoerg	args[i++] = _PATH_MASTERPASSWD;
6120253Sjoerg	args[i] = NULL;
6220253Sjoerg
6320253Sjoerg	if ((pid = fork()) == -1)	/* Error (errno set) */
6420253Sjoerg		i = -1;
6520253Sjoerg	else if (pid == 0) {	/* Child */
6620253Sjoerg		execv(args[0], args);
6720253Sjoerg		_exit(1);
6820253Sjoerg	} else {		/* Parent */
6920253Sjoerg		waitpid(pid, &i, 0);
7020253Sjoerg		if ((i = WEXITSTATUS(i)) != 0)
7120253Sjoerg			errno = EIO;	/* set SOMETHING */
7220253Sjoerg	}
7320253Sjoerg	return i;
7420253Sjoerg}
7520253Sjoerg
7620253Sjoergint
7720253Sjoergfmtpwentry(char *buf, struct passwd * pwd, int type)
7820253Sjoerg{
7920253Sjoerg	int             l;
8020253Sjoerg	char           *pw;
8120253Sjoerg
8220253Sjoerg	pw = (pwd->pw_passwd == NULL || !*pwd->pw_passwd) ? "" : (type == PWF_MASTER) ? pwd->pw_passwd : "*";
8320253Sjoerg
8420253Sjoerg	if (type == PWF_PASSWD)
8520253Sjoerg		l = sprintf(buf, "%s:*:%ld:%ld:%s:%s:%s\n",
8620253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, (long) pwd->pw_gid,
8720253Sjoerg			    pwd->pw_gecos ? pwd->pw_gecos : "User &",
8820253Sjoerg			    pwd->pw_dir, pwd->pw_shell);
8920253Sjoerg	else
9020253Sjoerg		l = sprintf(buf, "%s:%s:%ld:%ld:%s:%lu:%lu:%s:%s:%s\n",
9120253Sjoerg		   pwd->pw_name, pw, (long) pwd->pw_uid, (long) pwd->pw_gid,
9220253Sjoerg			    pwd->pw_class ? pwd->pw_class : "",
9320253Sjoerg			    (unsigned long) pwd->pw_change,
9420253Sjoerg			    (unsigned long) pwd->pw_expire,
9520253Sjoerg			    pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
9620253Sjoerg	return l;
9720253Sjoerg}
9820253Sjoerg
9920253Sjoerg
10020253Sjoergint
10120253Sjoergfmtpwent(char *buf, struct passwd * pwd)
10220253Sjoerg{
10320253Sjoerg	return fmtpwentry(buf, pwd, PWF_STANDARD);
10420253Sjoerg}
10520253Sjoerg
10620253Sjoergstatic int
10720253Sjoergpw_update(struct passwd * pwd, char const * user, int mode)
10820253Sjoerg{
10920253Sjoerg	int             rc = 0;
11020253Sjoerg
11120253Sjoerg	endpwent();
11220253Sjoerg
11320253Sjoerg	/*
11420253Sjoerg	 * First, let's check the see if the database is alright
11533259Swosch	 * Note: -C is only available in FreeBSD 2.2 and above
11620253Sjoerg	 */
11720267Sjoerg#ifdef HAVE_PWDB_C
11833259Swosch	if (pwdb("-C", NULL) == 0) {	/* Check only */
11920267Sjoerg#else
12033259Swosch	{				/* No -C */
12120267Sjoerg#endif
12220253Sjoerg		char            pfx[32];
12320747Sdavidn		char            pwbuf[PWBUFSZ];
12420253Sjoerg		int             l = sprintf(pfx, "%s:", user);
12520253Sjoerg
12620253Sjoerg		/*
12720253Sjoerg		 * Update the passwd file first
12820253Sjoerg		 */
12920253Sjoerg		if (pwd == NULL)
13020253Sjoerg			*pwbuf = '\0';
13120253Sjoerg		else
13220253Sjoerg			fmtpwentry(pwbuf, pwd, PWF_PASSWD);
13320253Sjoerg		if ((rc = fileupdate(_PATH_PASSWD, 0644, pwbuf, pfx, l, mode)) != 0) {
13420253Sjoerg
13520253Sjoerg			/*
13620253Sjoerg			 * Then the master.passwd file
13720253Sjoerg			 */
13820253Sjoerg			if (pwd != NULL)
13920253Sjoerg				fmtpwentry(pwbuf, pwd, PWF_MASTER);
14020253Sjoerg			if ((rc = fileupdate(_PATH_MASTERPASSWD, 0644, pwbuf, pfx, l, mode)) != 0)
14120253Sjoerg				rc = pwdb(NULL) == 0;
14220253Sjoerg		}
14320253Sjoerg	}
14420253Sjoerg	return rc;
14520253Sjoerg}
14620253Sjoerg
14720253Sjoergint
14820253Sjoergaddpwent(struct passwd * pwd)
14920253Sjoerg{
15020253Sjoerg	return pw_update(pwd, pwd->pw_name, UPD_CREATE);
15120253Sjoerg}
15220253Sjoerg
15320253Sjoergint
15420253Sjoergchgpwent(char const * login, struct passwd * pwd)
15520253Sjoerg{
15620253Sjoerg	return pw_update(pwd, login, UPD_REPLACE);
15720253Sjoerg}
15820253Sjoerg
15920253Sjoergint
16020253Sjoergdelpwent(struct passwd * pwd)
16120253Sjoerg{
16220253Sjoerg	return pw_update(NULL, pwd->pw_name, UPD_DELETE);
16320253Sjoerg}
164