services.c revision 158115
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/services.c 158115 2006-04-28 12:03:38Z ume $");
30
31#include <sys/param.h>
32#include <sys/types.h>
33#include <assert.h>
34#include <nsswitch.h>
35#include <netdb.h>
36#include <string.h>
37#include <stdlib.h>
38#include "../debug.h"
39#include "services.h"
40
41static int services_marshal_func(struct servent *, char *, size_t *);
42static int services_lookup_func(const char *, size_t, char **, size_t *);
43static void *services_mp_init_func();
44static int services_mp_lookup_func(char **, size_t *, void *);
45static void services_mp_destroy_func(void *);
46
47static int
48services_marshal_func(struct servent *serv, char *buffer, size_t *buffer_size)
49{
50	struct servent	new_serv;
51	size_t	desired_size;
52	char	**alias;
53	char	*p;
54	size_t	size;
55	size_t	aliases_size;
56
57	TRACE_IN(services_marshal_func);
58	desired_size = ALIGNBYTES + sizeof(struct servent) + sizeof(char *);
59	if (serv->s_name != NULL)
60		desired_size += strlen(serv->s_name) + 1;
61	if (serv->s_proto != NULL)
62		desired_size += strlen(serv->s_proto) + 1;
63
64	aliases_size = 0;
65	if (serv->s_aliases != NULL) {
66		for (alias = serv->s_aliases; *alias; ++alias) {
67			desired_size += strlen(*alias) + 1;
68			++aliases_size;
69		}
70
71		desired_size += ALIGNBYTES + sizeof(char *) *
72		    (aliases_size + 1);
73	}
74
75	if ((*buffer_size < desired_size) || (buffer == NULL)) {
76		*buffer_size = desired_size;
77		TRACE_OUT(services_marshal_func);
78		return (NS_RETURN);
79	}
80
81	memcpy(&new_serv, serv, sizeof(struct servent));
82	memset(buffer, 0, desired_size);
83
84	*buffer_size = desired_size;
85	p = buffer + sizeof(struct servent) + sizeof(char *);
86	memcpy(buffer + sizeof(struct servent), &p, sizeof(char *));
87	p = (char *)ALIGN(p);
88
89	if (new_serv.s_name != NULL) {
90		size = strlen(new_serv.s_name);
91		memcpy(p, new_serv.s_name, size);
92		new_serv.s_name = p;
93		p += size + 1;
94	}
95
96	if (new_serv.s_proto != NULL) {
97		size = strlen(new_serv.s_proto);
98		memcpy(p, new_serv.s_proto, size);
99		new_serv.s_proto = p;
100		p += size + 1;
101	}
102
103	if (new_serv.s_aliases != NULL) {
104		p = (char *)ALIGN(p);
105		memcpy(p, new_serv.s_aliases, sizeof(char *) * aliases_size);
106		new_serv.s_aliases = (char **)p;
107		p += sizeof(char *) * (aliases_size + 1);
108
109		for (alias = new_serv.s_aliases; *alias; ++alias) {
110			size = strlen(*alias);
111			memcpy(p, *alias, size);
112			*alias = p;
113			p += size + 1;
114		}
115	}
116
117	memcpy(buffer, &new_serv, sizeof(struct servent));
118	TRACE_OUT(services_marshal_func);
119	return (NS_SUCCESS);
120}
121
122static int
123services_lookup_func(const char *key, size_t key_size, char **buffer,
124	size_t *buffer_size)
125{
126	enum nss_lookup_type lookup_type;
127	char	*name = NULL;
128	char	*proto = NULL;
129	size_t	size, size2;
130	int	port;
131
132	struct servent *result;
133
134	TRACE_IN(services_lookup_func);
135
136	assert(buffer != NULL);
137	assert(buffer_size != NULL);
138
139	if (key_size < sizeof(enum nss_lookup_type)) {
140		TRACE_OUT(passwd_lookup_func);
141		return (NS_UNAVAIL);
142	}
143	memcpy(&lookup_type, key, sizeof(enum nss_lookup_type));
144
145	switch (lookup_type) {
146	case nss_lt_name:
147		size = key_size - sizeof(enum nss_lookup_type)	+ 1;
148		name = (char *)malloc(size);
149		assert(name != NULL);
150		memset(name, 0, size);
151		memcpy(name, key + sizeof(enum nss_lookup_type), size - 1);
152
153		size2 = strlen(name) + 1;
154		if (size2 < size) {
155			proto = strchr(name, '\0');
156			if (strrchr(name, '\0') > proto)
157				++proto ;
158			else
159				proto = NULL;
160		}
161		break;
162	case nss_lt_id:
163		if (key_size < sizeof(enum nss_lookup_type) +
164			sizeof(int)) {
165			TRACE_OUT(passwd_lookup_func);
166			return (NS_UNAVAIL);
167		}
168
169		memcpy(&port, key + sizeof(enum nss_lookup_type),
170			sizeof(int));
171
172		size = key_size - sizeof(enum nss_lookup_type) + sizeof(int);
173		if (size > 0) {
174			proto = (char *)malloc(size + 1);
175			assert(proto != NULL);
176			memset(proto, size + 1, 0);
177			memcpy(proto, key + sizeof(enum nss_lookup_type) +
178				sizeof(int), size);
179		}
180		break;
181	default:
182		TRACE_OUT(passwd_lookup_func);
183		return (NS_UNAVAIL);
184	}
185
186	switch (lookup_type) {
187	case nss_lt_name:
188		result = getservbyname(name, proto);
189		free(name);
190		break;
191	case nss_lt_id:
192		result = getservbyport(port, proto);
193		free(proto);
194		break;
195	default:
196		/* SHOULD NOT BE REACHED */
197		break;
198	}
199
200	if (result != NULL) {
201		services_marshal_func(result, NULL, buffer_size);
202		*buffer = (char *)malloc(*buffer_size);
203		assert(*buffer != NULL);
204		services_marshal_func(result, *buffer, buffer_size);
205	}
206
207	TRACE_OUT(services_lookup_func);
208	return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
209}
210
211static void *
212services_mp_init_func()
213{
214	TRACE_IN(services_mp_init_func);
215	setservent(0);
216	TRACE_OUT(services_mp_init_func);
217
218	return (NULL);
219}
220
221static int
222services_mp_lookup_func(char **buffer, size_t *buffer_size, void *mdata)
223{
224	struct servent *result;
225
226	TRACE_IN(services_mp_lookup_func);
227	result = getservent();
228	if (result != NULL) {
229		services_marshal_func(result, NULL, buffer_size);
230		*buffer = (char *)malloc(*buffer_size);
231		assert(*buffer != NULL);
232		services_marshal_func(result, *buffer, buffer_size);
233	}
234
235	TRACE_OUT(services_mp_lookup_func);
236	return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
237}
238
239static void
240services_mp_destroy_func(void *mdata)
241{
242	TRACE_IN(services_mp_destroy_func);
243	TRACE_OUT(services_mp_destroy_func);
244}
245
246struct agent *
247init_services_agent()
248{
249	struct common_agent	*retval;
250	TRACE_IN(init_services_agent);
251
252	retval = (struct common_agent *)malloc(sizeof(struct common_agent));
253	assert(retval != NULL);
254	memset(retval, 0, sizeof(struct common_agent));
255
256	retval->parent.name = strdup("services");
257	assert(retval->parent.name != NULL);
258
259	retval->parent.type = COMMON_AGENT;
260	retval->lookup_func = services_lookup_func;
261
262	TRACE_OUT(init_services_agent);
263	return ((struct agent *)retval);
264}
265
266struct agent *
267init_services_mp_agent()
268{
269	struct multipart_agent	*retval;
270
271	TRACE_IN(init_services_mp_agent);
272	retval = (struct multipart_agent *)malloc(
273		sizeof(struct multipart_agent));
274	assert(retval != NULL);
275	memset(retval, 0, sizeof(struct multipart_agent));
276
277	retval->parent.name = strdup("services");
278	retval->parent.type = MULTIPART_AGENT;
279	retval->mp_init_func = services_mp_init_func;
280	retval->mp_lookup_func = services_mp_lookup_func;
281	retval->mp_destroy_func = services_mp_destroy_func;
282	assert(retval->parent.name != NULL);
283
284	TRACE_OUT(init_services_mp_agent);
285	return ((struct agent *)retval);
286}
287