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