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