2 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/types.h>
41 static int services_marshal_func(struct servent *, char *, size_t *);
42 static int services_lookup_func(const char *, size_t, char **, size_t *);
43 static void *services_mp_init_func();
44 static int services_mp_lookup_func(char **, size_t *, void *);
45 static void services_mp_destroy_func(void *);
48 services_marshal_func(struct servent *serv, char *buffer, size_t *buffer_size)
50 struct servent new_serv;
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;
65 if (serv->s_aliases != NULL) {
66 for (alias = serv->s_aliases; *alias; ++alias) {
67 desired_size += strlen(*alias) + 1;
71 desired_size += ALIGNBYTES + sizeof(char *) *
75 if ((*buffer_size < desired_size) || (buffer == NULL)) {
76 *buffer_size = desired_size;
77 TRACE_OUT(services_marshal_func);
81 memcpy(&new_serv, serv, sizeof(struct servent));
82 memset(buffer, 0, desired_size);
84 *buffer_size = desired_size;
85 p = buffer + sizeof(struct servent) + sizeof(char *);
86 memcpy(buffer + sizeof(struct servent), &p, sizeof(char *));
89 if (new_serv.s_name != NULL) {
90 size = strlen(new_serv.s_name);
91 memcpy(p, new_serv.s_name, size);
96 if (new_serv.s_proto != NULL) {
97 size = strlen(new_serv.s_proto);
98 memcpy(p, new_serv.s_proto, size);
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);
109 for (alias = new_serv.s_aliases; *alias; ++alias) {
110 size = strlen(*alias);
111 memcpy(p, *alias, size);
117 memcpy(buffer, &new_serv, sizeof(struct servent));
118 TRACE_OUT(services_marshal_func);
123 services_lookup_func(const char *key, size_t key_size, char **buffer,
126 enum nss_lookup_type lookup_type;
132 struct servent *result;
134 TRACE_IN(services_lookup_func);
136 assert(buffer != NULL);
137 assert(buffer_size != NULL);
139 if (key_size < sizeof(enum nss_lookup_type)) {
140 TRACE_OUT(passwd_lookup_func);
143 memcpy(&lookup_type, key, sizeof(enum nss_lookup_type));
145 switch (lookup_type) {
147 size = key_size - sizeof(enum nss_lookup_type);
148 name = (char *)malloc(size + 1);
149 assert(name != NULL);
150 memset(name, 0, size + 1);
151 memcpy(name, key + sizeof(enum nss_lookup_type), size);
153 size2 = strlen(name) + 1;
156 proto = name + size2;
161 if (key_size < sizeof(enum nss_lookup_type) +
163 TRACE_OUT(passwd_lookup_func);
167 memcpy(&port, key + sizeof(enum nss_lookup_type),
170 size = key_size - sizeof(enum nss_lookup_type) - sizeof(int);
172 proto = (char *)malloc(size + 1);
173 assert(proto != NULL);
174 memset(proto, size + 1, 0);
175 memcpy(proto, key + sizeof(enum nss_lookup_type) +
180 TRACE_OUT(passwd_lookup_func);
184 switch (lookup_type) {
186 result = getservbyname(name, proto);
190 result = getservbyport(port, proto);
194 /* SHOULD NOT BE REACHED */
198 if (result != NULL) {
199 services_marshal_func(result, NULL, buffer_size);
200 *buffer = (char *)malloc(*buffer_size);
201 assert(*buffer != NULL);
202 services_marshal_func(result, *buffer, buffer_size);
205 TRACE_OUT(services_lookup_func);
206 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
210 services_mp_init_func()
212 TRACE_IN(services_mp_init_func);
214 TRACE_OUT(services_mp_init_func);
220 services_mp_lookup_func(char **buffer, size_t *buffer_size, void *mdata)
222 struct servent *result;
224 TRACE_IN(services_mp_lookup_func);
225 result = getservent();
226 if (result != NULL) {
227 services_marshal_func(result, NULL, buffer_size);
228 *buffer = (char *)malloc(*buffer_size);
229 assert(*buffer != NULL);
230 services_marshal_func(result, *buffer, buffer_size);
233 TRACE_OUT(services_mp_lookup_func);
234 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
238 services_mp_destroy_func(void *mdata)
240 TRACE_IN(services_mp_destroy_func);
241 TRACE_OUT(services_mp_destroy_func);
245 init_services_agent()
247 struct common_agent *retval;
248 TRACE_IN(init_services_agent);
250 retval = (struct common_agent *)malloc(sizeof(struct common_agent));
251 assert(retval != NULL);
252 memset(retval, 0, sizeof(struct common_agent));
254 retval->parent.name = strdup("services");
255 assert(retval->parent.name != NULL);
257 retval->parent.type = COMMON_AGENT;
258 retval->lookup_func = services_lookup_func;
260 TRACE_OUT(init_services_agent);
261 return ((struct agent *)retval);
265 init_services_mp_agent()
267 struct multipart_agent *retval;
269 TRACE_IN(init_services_mp_agent);
270 retval = (struct multipart_agent *)malloc(
271 sizeof(struct multipart_agent));
272 assert(retval != NULL);
273 memset(retval, 0, sizeof(struct multipart_agent));
275 retval->parent.name = strdup("services");
276 retval->parent.type = MULTIPART_AGENT;
277 retval->mp_init_func = services_mp_init_func;
278 retval->mp_lookup_func = services_mp_lookup_func;
279 retval->mp_destroy_func = services_mp_destroy_func;
280 assert(retval->parent.name != NULL);
282 TRACE_OUT(init_services_mp_agent);
283 return ((struct agent *)retval);