pwupd.c revision 20267
1/*-
2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer as
10 *    the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by David L. Nugent.
17 * 4. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	$Id$
33 */
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39#include <stdarg.h>
40#include <errno.h>
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <sys/wait.h>
44#include <stdarg.h>
45
46#include "pwupd.h"
47
48#define HAVE_PWDB_C	1
49
50static int
51pwdb(char *arg,...)
52{
53	int             i = 0;
54	pid_t           pid;
55	va_list         ap;
56	char           *args[8];
57
58	args[i++] = _PATH_PWD_MKDB;
59	va_start(ap, arg);
60	while (i < 6 && arg != NULL) {
61		args[i++] = arg;
62		arg = va_arg(ap, char *);
63	}
64	args[i++] = _PATH_MASTERPASSWD;
65	args[i] = NULL;
66
67	if ((pid = fork()) == -1)	/* Error (errno set) */
68		i = -1;
69	else if (pid == 0) {	/* Child */
70		execv(args[0], args);
71		_exit(1);
72	} else {		/* Parent */
73		waitpid(pid, &i, 0);
74		if ((i = WEXITSTATUS(i)) != 0)
75			errno = EIO;	/* set SOMETHING */
76	}
77	return i;
78}
79
80int
81fmtpwentry(char *buf, struct passwd * pwd, int type)
82{
83	int             l;
84	char           *pw;
85
86	pw = (pwd->pw_passwd == NULL || !*pwd->pw_passwd) ? "" : (type == PWF_MASTER) ? pwd->pw_passwd : "*";
87
88	if (type == PWF_PASSWD)
89		l = sprintf(buf, "%s:*:%ld:%ld:%s:%s:%s\n",
90		       pwd->pw_name, (long) pwd->pw_uid, (long) pwd->pw_gid,
91			    pwd->pw_gecos ? pwd->pw_gecos : "User &",
92			    pwd->pw_dir, pwd->pw_shell);
93	else
94		l = sprintf(buf, "%s:%s:%ld:%ld:%s:%lu:%lu:%s:%s:%s\n",
95		   pwd->pw_name, pw, (long) pwd->pw_uid, (long) pwd->pw_gid,
96			    pwd->pw_class ? pwd->pw_class : "",
97			    (unsigned long) pwd->pw_change,
98			    (unsigned long) pwd->pw_expire,
99			    pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
100	return l;
101}
102
103
104int
105fmtpwent(char *buf, struct passwd * pwd)
106{
107	return fmtpwentry(buf, pwd, PWF_STANDARD);
108}
109
110static int
111pw_update(struct passwd * pwd, char const * user, int mode)
112{
113	int             rc = 0;
114
115	endpwent();
116
117	/*
118	 * First, let's check the see if the database is alright
119	 * Note: -c is only available in FreeBSD 2.2 and above
120	 */
121#ifdef HAVE_PWDB_C
122	if (pwdb("-c", NULL) == 0) {	/* Check only */
123#else
124	{				/* No -c */
125#endif
126		char            pfx[32];
127		char            pwbuf[MAXPWLINE];
128		int             l = sprintf(pfx, "%s:", user);
129
130		/*
131		 * Update the passwd file first
132		 */
133		if (pwd == NULL)
134			*pwbuf = '\0';
135		else
136			fmtpwentry(pwbuf, pwd, PWF_PASSWD);
137		if ((rc = fileupdate(_PATH_PASSWD, 0644, pwbuf, pfx, l, mode)) != 0) {
138
139			/*
140			 * Then the master.passwd file
141			 */
142			if (pwd != NULL)
143				fmtpwentry(pwbuf, pwd, PWF_MASTER);
144			if ((rc = fileupdate(_PATH_MASTERPASSWD, 0644, pwbuf, pfx, l, mode)) != 0)
145				rc = pwdb(NULL) == 0;
146		}
147	}
148	return rc;
149}
150
151int
152addpwent(struct passwd * pwd)
153{
154	return pw_update(pwd, pwd->pw_name, UPD_CREATE);
155}
156
157int
158chgpwent(char const * login, struct passwd * pwd)
159{
160	return pw_update(pwd, login, UPD_REPLACE);
161}
162
163int
164delpwent(struct passwd * pwd)
165{
166	return pw_update(NULL, pwd->pw_name, UPD_DELETE);
167}
168