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