passwd.c revision 172344
1/*-
2 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
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.
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 THE AUTHOR 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 THE AUTHOR 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 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/usr.sbin/nscd/agents/passwd.c 158115 2006-04-28 12:03:38Z ume $");
30
31#include <sys/types.h>
32#include <assert.h>
33#include <nsswitch.h>
34#include <pwd.h>
35#include <string.h>
36#include <stdlib.h>
37#include "../debug.h"
38#include "passwd.h"
39
40static int passwd_marshal_func(struct passwd *, char *, size_t *);
41static int passwd_lookup_func(const char *, size_t, char **, size_t *);
42static void *passwd_mp_init_func();
43static int passwd_mp_lookup_func(char **, size_t *, void *);
44static void passwd_mp_destroy_func(void *mdata);
45
46static int
47passwd_marshal_func(struct passwd *pwd, char *buffer, size_t *buffer_size)
48{
49	char		*p;
50	struct passwd	new_pwd;
51	size_t		desired_size, size;
52
53	TRACE_IN(passwd_marshal_func);
54	desired_size = sizeof(struct passwd) + sizeof(char *) +
55		strlen(pwd->pw_name) + 1;
56	if (pwd->pw_passwd != NULL)
57		desired_size += strlen(pwd->pw_passwd) + 1;
58	if (pwd->pw_class != NULL)
59		desired_size += strlen(pwd->pw_class) + 1;
60	if (pwd->pw_gecos != NULL)
61		desired_size += strlen(pwd->pw_gecos) + 1;
62	if (pwd->pw_dir != NULL)
63		desired_size += strlen(pwd->pw_dir) + 1;
64	if (pwd->pw_shell != NULL)
65		desired_size += strlen(pwd->pw_shell) + 1;
66
67	if ((*buffer_size < desired_size) || (buffer == NULL)) {
68		*buffer_size = desired_size;
69		TRACE_OUT(passwd_marshal_func);
70		return (NS_RETURN);
71	}
72
73	memcpy(&new_pwd, pwd, sizeof(struct passwd));
74	memset(buffer, 0, desired_size);
75
76	*buffer_size = desired_size;
77	p = buffer + sizeof(struct passwd) + sizeof(char *);
78	memcpy(buffer + sizeof(struct passwd), &p, sizeof(char *));
79
80	if (new_pwd.pw_name != NULL) {
81		size = strlen(new_pwd.pw_name);
82		memcpy(p, new_pwd.pw_name, size);
83		new_pwd.pw_name = p;
84		p += size + 1;
85	}
86
87	if (new_pwd.pw_passwd != NULL) {
88		size = strlen(new_pwd.pw_passwd);
89		memcpy(p, new_pwd.pw_passwd, size);
90		new_pwd.pw_passwd = p;
91		p += size + 1;
92	}
93
94	if (new_pwd.pw_class != NULL) {
95		size = strlen(new_pwd.pw_class);
96		memcpy(p, new_pwd.pw_class, size);
97		new_pwd.pw_class = p;
98		p += size + 1;
99	}
100
101	if (new_pwd.pw_gecos != NULL) {
102		size = strlen(new_pwd.pw_gecos);
103		memcpy(p, new_pwd.pw_gecos, size);
104		new_pwd.pw_gecos = p;
105		p += size + 1;
106	}
107
108	if (new_pwd.pw_dir != NULL) {
109		size = strlen(new_pwd.pw_dir);
110		memcpy(p, new_pwd.pw_dir, size);
111		new_pwd.pw_dir = p;
112		p += size + 1;
113	}
114
115	if (new_pwd.pw_shell != NULL) {
116		size = strlen(new_pwd.pw_shell);
117		memcpy(p, new_pwd.pw_shell, size);
118		new_pwd.pw_shell = p;
119		p += size + 1;
120	}
121
122	memcpy(buffer, &new_pwd, sizeof(struct passwd));
123	TRACE_OUT(passwd_marshal_func);
124	return (NS_SUCCESS);
125}
126
127static int
128passwd_lookup_func(const char *key, size_t key_size, char **buffer,
129	size_t *buffer_size)
130{
131	enum nss_lookup_type lookup_type;
132	char	*login;
133	size_t	size;
134	uid_t	uid;
135
136	struct passwd *result;
137
138	TRACE_IN(passwd_lookup_func);
139	assert(buffer != NULL);
140	assert(buffer_size != NULL);
141
142	if (key_size < sizeof(enum nss_lookup_type)) {
143		TRACE_OUT(passwd_lookup_func);
144		return (NS_UNAVAIL);
145	}
146	memcpy(&lookup_type, key, sizeof(enum nss_lookup_type));
147
148	switch (lookup_type) {
149	case nss_lt_name:
150		size = key_size - sizeof(enum nss_lookup_type)	+ 1;
151		login = (char *)malloc(size);
152		assert(login != NULL);
153		memset(login, 0, size);
154		memcpy(login, key + sizeof(enum nss_lookup_type), size - 1);
155		break;
156	case nss_lt_id:
157		if (key_size < sizeof(enum nss_lookup_type) +
158			sizeof(uid_t)) {
159			TRACE_OUT(passwd_lookup_func);
160			return (NS_UNAVAIL);
161		}
162
163		memcpy(&uid, key + sizeof(enum nss_lookup_type), sizeof(uid_t));
164		break;
165	default:
166		TRACE_OUT(passwd_lookup_func);
167		return (NS_UNAVAIL);
168	}
169
170	switch (lookup_type) {
171	case nss_lt_name:
172		result = getpwnam(login);
173		free(login);
174		break;
175	case nss_lt_id:
176		result = getpwuid(uid);
177		break;
178	default:
179		/* SHOULD NOT BE REACHED */
180		break;
181	}
182
183	if (result != NULL) {
184		passwd_marshal_func(result, NULL, buffer_size);
185		*buffer = (char *)malloc(*buffer_size);
186		assert(*buffer != NULL);
187		passwd_marshal_func(result, *buffer, buffer_size);
188	}
189
190	TRACE_OUT(passwd_lookup_func);
191	return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
192}
193
194static void *
195passwd_mp_init_func()
196{
197	TRACE_IN(passwd_mp_init_func);
198	setpwent();
199	TRACE_OUT(passwd_mp_init_func);
200
201	return (NULL);
202}
203
204static int
205passwd_mp_lookup_func(char **buffer, size_t *buffer_size, void *mdata)
206{
207	struct passwd	*result;
208
209	TRACE_IN(passwd_mp_lookup_func);
210	result = getpwent();
211	if (result != NULL) {
212		passwd_marshal_func(result, NULL, buffer_size);
213		*buffer = (char *)malloc(*buffer_size);
214		assert(*buffer != NULL);
215		passwd_marshal_func(result, *buffer, buffer_size);
216	}
217
218	TRACE_OUT(passwd_mp_lookup_func);
219	return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
220}
221
222static void
223passwd_mp_destroy_func(void *mdata)
224{
225	TRACE_IN(passwd_mp_destroy_func);
226	TRACE_OUT(passwd_mp_destroy_func);
227}
228
229struct agent *
230init_passwd_agent()
231{
232	struct common_agent	*retval;
233
234	TRACE_IN(init_passwd_agent);
235	retval = (struct common_agent *)malloc(sizeof(struct common_agent));
236	assert(retval != NULL);
237	memset(retval, 0, sizeof(struct common_agent));
238
239	retval->parent.name = strdup("passwd");
240	assert(retval->parent.name != NULL);
241
242	retval->parent.type = COMMON_AGENT;
243	retval->lookup_func = passwd_lookup_func;
244
245	TRACE_OUT(init_passwd_agent);
246	return ((struct agent *)retval);
247}
248
249struct agent *
250init_passwd_mp_agent()
251{
252	struct multipart_agent	*retval;
253
254	TRACE_IN(init_passwd_mp_agent);
255	retval = (struct multipart_agent *)malloc(
256		sizeof(struct multipart_agent));
257	assert(retval != NULL);
258	memset(retval, 0, sizeof(struct multipart_agent));
259
260	retval->parent.name = strdup("passwd");
261	retval->parent.type = MULTIPART_AGENT;
262	retval->mp_init_func = passwd_mp_init_func;
263	retval->mp_lookup_func = passwd_mp_lookup_func;
264	retval->mp_destroy_func = passwd_mp_destroy_func;
265	assert(retval->parent.name != NULL);
266
267	TRACE_OUT(init_passwd_mp_agent);
268	return ((struct agent *)retval);
269}
270