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 #include <sys/param.h>
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 *);
47 services_marshal_func(struct servent *serv, char *buffer, size_t *buffer_size)
49 struct servent new_serv;
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;
64 if (serv->s_aliases != NULL) {
65 for (alias = serv->s_aliases; *alias; ++alias) {
66 desired_size += strlen(*alias) + 1;
70 desired_size += ALIGNBYTES + sizeof(char *) *
74 if ((*buffer_size < desired_size) || (buffer == NULL)) {
75 *buffer_size = desired_size;
76 TRACE_OUT(services_marshal_func);
80 memcpy(&new_serv, serv, sizeof(struct servent));
81 memset(buffer, 0, desired_size);
83 *buffer_size = desired_size;
84 p = buffer + sizeof(struct servent) + sizeof(char *);
85 memcpy(buffer + sizeof(struct servent), &p, sizeof(char *));
88 if (new_serv.s_name != NULL) {
89 size = strlen(new_serv.s_name);
90 memcpy(p, new_serv.s_name, size);
95 if (new_serv.s_proto != NULL) {
96 size = strlen(new_serv.s_proto);
97 memcpy(p, new_serv.s_proto, size);
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);
108 for (alias = new_serv.s_aliases; *alias; ++alias) {
109 size = strlen(*alias);
110 memcpy(p, *alias, size);
116 memcpy(buffer, &new_serv, sizeof(struct servent));
117 TRACE_OUT(services_marshal_func);
122 services_lookup_func(const char *key, size_t key_size, char **buffer,
125 enum nss_lookup_type lookup_type;
131 struct servent *result;
133 TRACE_IN(services_lookup_func);
135 assert(buffer != NULL);
136 assert(buffer_size != NULL);
138 if (key_size < sizeof(enum nss_lookup_type)) {
139 TRACE_OUT(passwd_lookup_func);
142 memcpy(&lookup_type, key, sizeof(enum nss_lookup_type));
144 switch (lookup_type) {
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);
151 size2 = strlen(name) + 1;
154 proto = name + size2;
159 if (key_size < sizeof(enum nss_lookup_type) +
161 TRACE_OUT(passwd_lookup_func);
165 memcpy(&port, key + sizeof(enum nss_lookup_type),
168 size = key_size - sizeof(enum nss_lookup_type) - sizeof(int);
170 proto = calloc(1, size + 1);
171 assert(proto != NULL);
172 memcpy(proto, key + sizeof(enum nss_lookup_type) +
177 TRACE_OUT(passwd_lookup_func);
181 switch (lookup_type) {
183 result = getservbyname(name, proto);
187 result = getservbyport(port, proto);
191 /* SHOULD NOT BE REACHED */
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);
202 TRACE_OUT(services_lookup_func);
203 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
207 services_mp_init_func(void)
209 TRACE_IN(services_mp_init_func);
211 TRACE_OUT(services_mp_init_func);
217 services_mp_lookup_func(char **buffer, size_t *buffer_size, void *mdata)
219 struct servent *result;
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);
230 TRACE_OUT(services_mp_lookup_func);
231 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
235 services_mp_destroy_func(void *mdata)
237 TRACE_IN(services_mp_destroy_func);
238 TRACE_OUT(services_mp_destroy_func);
242 init_services_agent(void)
244 struct common_agent *retval;
245 TRACE_IN(init_services_agent);
247 retval = calloc(1, sizeof(*retval));
248 assert(retval != NULL);
250 retval->parent.name = strdup("services");
251 assert(retval->parent.name != NULL);
253 retval->parent.type = COMMON_AGENT;
254 retval->lookup_func = services_lookup_func;
256 TRACE_OUT(init_services_agent);
257 return ((struct agent *)retval);
261 init_services_mp_agent(void)
263 struct multipart_agent *retval;
265 TRACE_IN(init_services_mp_agent);
268 assert(retval != NULL);
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);
277 TRACE_OUT(init_services_mp_agent);
278 return ((struct agent *)retval);