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
26 * $FreeBSD: src/usr.sbin/nscd/agents/services.c,v 1.4 2008/10/23 00:15:00 delphij Exp $
29 #include <sys/param.h>
30 #include <sys/types.h>
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 *);
46 services_marshal_func(struct servent
*serv
, char *buffer
, size_t *buffer_size
)
48 struct servent new_serv
;
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;
63 if (serv
->s_aliases
!= NULL
) {
64 for (alias
= serv
->s_aliases
; *alias
; ++alias
) {
65 desired_size
+= strlen(*alias
) + 1;
69 desired_size
+= ALIGNBYTES
+ sizeof(char *) *
73 if ((*buffer_size
< desired_size
) || (buffer
== NULL
)) {
74 *buffer_size
= desired_size
;
75 TRACE_OUT(services_marshal_func
);
79 memcpy(&new_serv
, serv
, sizeof(struct servent
));
80 memset(buffer
, 0, desired_size
);
82 *buffer_size
= desired_size
;
83 p
= buffer
+ sizeof(struct servent
) + sizeof(char *);
84 memcpy(buffer
+ sizeof(struct servent
), &p
, sizeof(char *));
87 if (new_serv
.s_name
!= NULL
) {
88 size
= strlen(new_serv
.s_name
);
89 memcpy(p
, new_serv
.s_name
, size
);
94 if (new_serv
.s_proto
!= NULL
) {
95 size
= strlen(new_serv
.s_proto
);
96 memcpy(p
, new_serv
.s_proto
, size
);
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);
107 for (alias
= new_serv
.s_aliases
; *alias
; ++alias
) {
108 size
= strlen(*alias
);
109 memcpy(p
, *alias
, size
);
115 memcpy(buffer
, &new_serv
, sizeof(struct servent
));
116 TRACE_OUT(services_marshal_func
);
121 services_lookup_func(const char *key
, size_t key_size
, char **buffer
,
124 enum nss_lookup_type lookup_type
;
130 struct servent
*result
;
132 TRACE_IN(services_lookup_func
);
134 assert(buffer
!= NULL
);
135 assert(buffer_size
!= NULL
);
137 if (key_size
< sizeof(enum nss_lookup_type
)) {
138 TRACE_OUT(passwd_lookup_func
);
141 memcpy(&lookup_type
, key
, sizeof(enum nss_lookup_type
));
143 switch (lookup_type
) {
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
);
150 size2
= strlen(name
) + 1;
153 proto
= name
+ size2
;
158 if (key_size
< sizeof(enum nss_lookup_type
) +
160 TRACE_OUT(passwd_lookup_func
);
164 memcpy(&port
, key
+ sizeof(enum nss_lookup_type
),
167 size
= key_size
- sizeof(enum nss_lookup_type
) - sizeof(int);
169 proto
= (char *)calloc(1, size
+ 1);
170 assert(proto
!= NULL
);
171 memcpy(proto
, key
+ sizeof(enum nss_lookup_type
) +
176 TRACE_OUT(passwd_lookup_func
);
180 switch (lookup_type
) {
182 result
= getservbyname(name
, proto
);
186 result
= getservbyport(port
, proto
);
190 /* SHOULD NOT BE REACHED */
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
);
201 TRACE_OUT(services_lookup_func
);
202 return (result
== NULL
? NS_NOTFOUND
: NS_SUCCESS
);
206 services_mp_init_func(void)
208 TRACE_IN(services_mp_init_func
);
210 TRACE_OUT(services_mp_init_func
);
216 services_mp_lookup_func(char **buffer
, size_t *buffer_size
, void *mdata
)
218 struct servent
*result
;
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
);
229 TRACE_OUT(services_mp_lookup_func
);
230 return (result
== NULL
? NS_NOTFOUND
: NS_SUCCESS
);
234 services_mp_destroy_func(void *mdata
)
236 TRACE_IN(services_mp_destroy_func
);
237 TRACE_OUT(services_mp_destroy_func
);
241 init_services_agent(void)
243 struct common_agent
*retval
;
244 TRACE_IN(init_services_agent
);
246 retval
= (struct common_agent
*)calloc(1, sizeof(struct common_agent
));
247 assert(retval
!= NULL
);
249 retval
->parent
.name
= strdup("services");
250 assert(retval
->parent
.name
!= NULL
);
252 retval
->parent
.type
= COMMON_AGENT
;
253 retval
->lookup_func
= services_lookup_func
;
255 TRACE_OUT(init_services_agent
);
256 return ((struct agent
*)retval
);
260 init_services_mp_agent(void)
262 struct multipart_agent
*retval
;
264 TRACE_IN(init_services_mp_agent
);
265 retval
= (struct multipart_agent
*)calloc(1,
266 sizeof(struct multipart_agent
));
267 assert(retval
!= NULL
);
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
);
276 TRACE_OUT(init_services_mp_agent
);
277 return ((struct agent
*)retval
);