xref: /netbsd-src/external/bsd/libbind/dist/irs/irp_sv.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1*5bbd2a12Schristos /*	$NetBSD: irp_sv.c,v 1.1.1.2 2012/09/09 16:07:57 christos Exp $	*/
2b5677b36Schristos 
3b5677b36Schristos /*
4b5677b36Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos  * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
6b5677b36Schristos  *
7b5677b36Schristos  * Permission to use, copy, modify, and distribute this software for any
8b5677b36Schristos  * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos  * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos  *
11b5677b36Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12b5677b36Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13b5677b36Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14b5677b36Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15b5677b36Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16b5677b36Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17b5677b36Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos  */
19b5677b36Schristos 
20b5677b36Schristos #if defined(LIBC_SCCS) && !defined(lint)
21b5677b36Schristos static const char rcsid[] = "Id: irp_sv.c,v 1.3 2005/04/27 04:56:29 sra Exp ";
22b5677b36Schristos #endif /* LIBC_SCCS and not lint */
23b5677b36Schristos 
24b5677b36Schristos /* extern */
25b5677b36Schristos 
26b5677b36Schristos #include "port_before.h"
27b5677b36Schristos 
28b5677b36Schristos #include <syslog.h>
29b5677b36Schristos #include <sys/types.h>
30b5677b36Schristos #include <sys/socket.h>
31b5677b36Schristos 
32b5677b36Schristos #ifdef IRS_LCL_SV_DB
33b5677b36Schristos #include <db.h>
34b5677b36Schristos #endif
35b5677b36Schristos #include <errno.h>
36b5677b36Schristos #include <fcntl.h>
37b5677b36Schristos #include <limits.h>
38b5677b36Schristos #include <stdio.h>
39b5677b36Schristos #include <string.h>
40b5677b36Schristos #include <stdlib.h>
41b5677b36Schristos #include <syslog.h>
42b5677b36Schristos 
43b5677b36Schristos #include <irs.h>
44b5677b36Schristos #include <irp.h>
45b5677b36Schristos #include <isc/irpmarshall.h>
46b5677b36Schristos #include <isc/memcluster.h>
47b5677b36Schristos 
48b5677b36Schristos #include "irs_p.h"
49b5677b36Schristos #include "lcl_p.h"
50b5677b36Schristos #include "irp_p.h"
51b5677b36Schristos 
52b5677b36Schristos #include "port_after.h"
53b5677b36Schristos 
54b5677b36Schristos /* Types */
55b5677b36Schristos 
56b5677b36Schristos struct pvt {
57b5677b36Schristos 	struct irp_p	       *girpdata;
58b5677b36Schristos 	int			warned;
59b5677b36Schristos 	struct servent		service;
60b5677b36Schristos };
61b5677b36Schristos 
62b5677b36Schristos /* Forward */
63b5677b36Schristos 
64b5677b36Schristos static void			sv_close(struct irs_sv*);
65b5677b36Schristos static struct servent *		sv_next(struct irs_sv *);
66b5677b36Schristos static struct servent *		sv_byname(struct irs_sv *, const char *,
67b5677b36Schristos 					  const char *);
68b5677b36Schristos static struct servent *		sv_byport(struct irs_sv *, int, const char *);
69b5677b36Schristos static void			sv_rewind(struct irs_sv *);
70b5677b36Schristos static void			sv_minimize(struct irs_sv *);
71b5677b36Schristos 
72b5677b36Schristos static void			free_service(struct servent *sv);
73b5677b36Schristos 
74b5677b36Schristos 
75b5677b36Schristos 
76b5677b36Schristos /* Public */
77b5677b36Schristos 
78b5677b36Schristos /*%
79b5677b36Schristos  * struct irs_sv * irs_irp_sv(struct irs_acc *this)
80b5677b36Schristos  *
81b5677b36Schristos  */
82b5677b36Schristos 
83b5677b36Schristos struct irs_sv *
irs_irp_sv(struct irs_acc * this)84b5677b36Schristos irs_irp_sv(struct irs_acc *this) {
85b5677b36Schristos 	struct irs_sv *sv;
86b5677b36Schristos 	struct pvt *pvt;
87b5677b36Schristos 
88b5677b36Schristos 	if ((sv = memget(sizeof *sv)) == NULL) {
89b5677b36Schristos 		errno = ENOMEM;
90b5677b36Schristos 		return (NULL);
91b5677b36Schristos 	}
92b5677b36Schristos 	memset(sv, 0x0, sizeof *sv);
93b5677b36Schristos 
94b5677b36Schristos 	if ((pvt = memget(sizeof *pvt)) == NULL) {
95b5677b36Schristos 		memput(sv, sizeof *sv);
96b5677b36Schristos 		errno = ENOMEM;
97b5677b36Schristos 		return (NULL);
98b5677b36Schristos 	}
99b5677b36Schristos 	memset(pvt, 0, sizeof *pvt);
100b5677b36Schristos 	pvt->girpdata = this->private;
101b5677b36Schristos 
102b5677b36Schristos 	sv->private = pvt;
103b5677b36Schristos 	sv->close = sv_close;
104b5677b36Schristos 	sv->next = sv_next;
105b5677b36Schristos 	sv->byname = sv_byname;
106b5677b36Schristos 	sv->byport = sv_byport;
107b5677b36Schristos 	sv->rewind = sv_rewind;
108b5677b36Schristos 	sv->minimize = sv_minimize;
109b5677b36Schristos 
110b5677b36Schristos 	return (sv);
111b5677b36Schristos }
112b5677b36Schristos 
113b5677b36Schristos /* Methods */
114b5677b36Schristos 
115b5677b36Schristos /*%
116b5677b36Schristos  * void sv_close(struct irs_sv *this)
117b5677b36Schristos  *
118b5677b36Schristos  */
119b5677b36Schristos 
120b5677b36Schristos static void
sv_close(struct irs_sv * this)121b5677b36Schristos sv_close(struct irs_sv *this) {
122b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
123b5677b36Schristos 
124b5677b36Schristos 	sv_minimize(this);
125b5677b36Schristos 
126b5677b36Schristos 	free_service(&pvt->service);
127b5677b36Schristos 
128b5677b36Schristos 	memput(pvt, sizeof *pvt);
129b5677b36Schristos 	memput(this, sizeof *this);
130b5677b36Schristos }
131b5677b36Schristos 
132b5677b36Schristos /*%
133b5677b36Schristos  *	Fills the cache if necessary and returns the next item from it.
134b5677b36Schristos  *
135b5677b36Schristos  */
136b5677b36Schristos 
137b5677b36Schristos static struct servent *
sv_next(struct irs_sv * this)138b5677b36Schristos sv_next(struct irs_sv *this) {
139b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
140b5677b36Schristos 	struct servent *sv = &pvt->service;
141b5677b36Schristos 	char *body;
142b5677b36Schristos 	size_t bodylen;
143b5677b36Schristos 	int code;
144b5677b36Schristos 	char text[256];
145b5677b36Schristos 
146b5677b36Schristos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
147b5677b36Schristos 		return (NULL);
148b5677b36Schristos 	}
149b5677b36Schristos 
150b5677b36Schristos 	if (irs_irp_send_command(pvt->girpdata, "getservent") != 0) {
151b5677b36Schristos 		return (NULL);
152b5677b36Schristos 	}
153b5677b36Schristos 
154b5677b36Schristos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
155b5677b36Schristos 				      text, sizeof text,
156b5677b36Schristos 				      &body, &bodylen) != 0) {
157b5677b36Schristos 		return (NULL);
158b5677b36Schristos 	}
159b5677b36Schristos 
160b5677b36Schristos 	if (code == IRPD_GETSERVICE_OK) {
161b5677b36Schristos 		free_service(sv);
162b5677b36Schristos 		if (irp_unmarshall_sv(sv, body) != 0) {
163b5677b36Schristos 			sv = NULL;
164b5677b36Schristos 		}
165b5677b36Schristos 	} else {
166b5677b36Schristos 		sv = NULL;
167b5677b36Schristos 	}
168b5677b36Schristos 
169b5677b36Schristos 	if (body != NULL) {
170b5677b36Schristos 		memput(body, bodylen);
171b5677b36Schristos 	}
172b5677b36Schristos 
173b5677b36Schristos 	return (sv);
174b5677b36Schristos }
175b5677b36Schristos 
176b5677b36Schristos /*%
177b5677b36Schristos  * struct servent * sv_byname(struct irs_sv *this, const char *name,
178b5677b36Schristos  *				const char *proto)
179b5677b36Schristos  *
180b5677b36Schristos  */
181b5677b36Schristos 
182b5677b36Schristos static struct servent *
sv_byname(struct irs_sv * this,const char * name,const char * proto)183b5677b36Schristos sv_byname(struct irs_sv *this, const char *name, const char *proto) {
184b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
185b5677b36Schristos 	struct servent *sv = &pvt->service;
186b5677b36Schristos 	char *body;
187b5677b36Schristos 	char text[256];
188b5677b36Schristos 	size_t bodylen;
189b5677b36Schristos 	int code;
190b5677b36Schristos 
191b5677b36Schristos 	if (sv->s_name != NULL &&
192b5677b36Schristos 	    strcmp(name, sv->s_name) == 0 &&
193b5677b36Schristos 	    strcasecmp(proto, sv->s_proto) == 0) {
194b5677b36Schristos 		return (sv);
195b5677b36Schristos 	}
196b5677b36Schristos 
197b5677b36Schristos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
198b5677b36Schristos 		return (NULL);
199b5677b36Schristos 	}
200b5677b36Schristos 
201b5677b36Schristos 	if (irs_irp_send_command(pvt->girpdata, "getservbyname %s %s",
202b5677b36Schristos 				 name, proto) != 0)
203b5677b36Schristos 		return (NULL);
204b5677b36Schristos 
205b5677b36Schristos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
206b5677b36Schristos 				      text, sizeof text,
207b5677b36Schristos 				      &body, &bodylen) != 0) {
208b5677b36Schristos 		return (NULL);
209b5677b36Schristos 	}
210b5677b36Schristos 
211b5677b36Schristos 	if (code == IRPD_GETSERVICE_OK) {
212b5677b36Schristos 		free_service(sv);
213b5677b36Schristos 		if (irp_unmarshall_sv(sv, body) != 0) {
214b5677b36Schristos 			sv = NULL;
215b5677b36Schristos 		}
216b5677b36Schristos 	} else {
217b5677b36Schristos 		sv = NULL;
218b5677b36Schristos 	}
219b5677b36Schristos 
220b5677b36Schristos 	if (body != NULL) {
221b5677b36Schristos 		memput(body, bodylen);
222b5677b36Schristos 	}
223b5677b36Schristos 
224b5677b36Schristos 	return (sv);
225b5677b36Schristos }
226b5677b36Schristos 
227b5677b36Schristos /*%
228b5677b36Schristos  * struct servent * sv_byport(struct irs_sv *this, int port,
229b5677b36Schristos  *				const char *proto)
230b5677b36Schristos  *
231b5677b36Schristos  */
232b5677b36Schristos 
233b5677b36Schristos static struct servent *
sv_byport(struct irs_sv * this,int port,const char * proto)234b5677b36Schristos sv_byport(struct irs_sv *this, int port, const char *proto) {
235b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
236b5677b36Schristos 	struct servent *sv = &pvt->service;
237b5677b36Schristos 	char *body;
238b5677b36Schristos 	size_t bodylen;
239b5677b36Schristos 	char text[256];
240b5677b36Schristos 	int code;
241b5677b36Schristos 
242b5677b36Schristos 	if (sv->s_name != NULL &&
243b5677b36Schristos 	    port == sv->s_port &&
244b5677b36Schristos 	    strcasecmp(proto, sv->s_proto) == 0) {
245b5677b36Schristos 		return (sv);
246b5677b36Schristos 	}
247b5677b36Schristos 
248b5677b36Schristos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
249b5677b36Schristos 		return (NULL);
250b5677b36Schristos 	}
251b5677b36Schristos 
252b5677b36Schristos 	if (irs_irp_send_command(pvt->girpdata, "getservbyport %d %s",
253b5677b36Schristos 				 ntohs((short)port), proto) != 0) {
254b5677b36Schristos 		return (NULL);
255b5677b36Schristos 	}
256b5677b36Schristos 
257b5677b36Schristos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
258b5677b36Schristos 				      text, sizeof text,
259b5677b36Schristos 				      &body, &bodylen) != 0) {
260b5677b36Schristos 		return (NULL);
261b5677b36Schristos 	}
262b5677b36Schristos 
263b5677b36Schristos 	if (code == IRPD_GETSERVICE_OK) {
264b5677b36Schristos 		free_service(sv);
265b5677b36Schristos 		if (irp_unmarshall_sv(sv, body) != 0) {
266b5677b36Schristos 			sv = NULL;
267b5677b36Schristos 		}
268b5677b36Schristos 	} else {
269b5677b36Schristos 		sv = NULL;
270b5677b36Schristos 	}
271b5677b36Schristos 
272b5677b36Schristos 	if (body != NULL) {
273b5677b36Schristos 		memput(body, bodylen);
274b5677b36Schristos 	}
275b5677b36Schristos 
276b5677b36Schristos 	return (sv);
277b5677b36Schristos }
278b5677b36Schristos 
279b5677b36Schristos /*%
280b5677b36Schristos  * void sv_rewind(struct irs_sv *this)
281b5677b36Schristos  *
282b5677b36Schristos  */
283b5677b36Schristos 
284b5677b36Schristos static void
sv_rewind(struct irs_sv * this)285b5677b36Schristos sv_rewind(struct irs_sv *this) {
286b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
287b5677b36Schristos 	char text[256];
288b5677b36Schristos 	int code;
289b5677b36Schristos 
290b5677b36Schristos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
291b5677b36Schristos 		return;
292b5677b36Schristos 	}
293b5677b36Schristos 
294b5677b36Schristos 	if (irs_irp_send_command(pvt->girpdata, "setservent") != 0) {
295b5677b36Schristos 		return;
296b5677b36Schristos 	}
297b5677b36Schristos 
298b5677b36Schristos 	code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
299b5677b36Schristos 	if (code != IRPD_GETSERVICE_SETOK) {
300b5677b36Schristos 		if (irp_log_errors) {
301b5677b36Schristos 			syslog(LOG_WARNING, "setservent failed: %s", text);
302b5677b36Schristos 		}
303b5677b36Schristos 	}
304b5677b36Schristos 
305b5677b36Schristos 	return;
306b5677b36Schristos }
307b5677b36Schristos 
308b5677b36Schristos /*%
309b5677b36Schristos  * void sv_minimize(struct irs_sv *this)
310b5677b36Schristos  *
311b5677b36Schristos  */
312b5677b36Schristos 
313b5677b36Schristos static void
sv_minimize(struct irs_sv * this)314b5677b36Schristos sv_minimize(struct irs_sv *this) {
315b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
316b5677b36Schristos 
317b5677b36Schristos 	irs_irp_disconnect(pvt->girpdata);
318b5677b36Schristos }
319b5677b36Schristos 
320b5677b36Schristos 
321b5677b36Schristos 
322b5677b36Schristos 
323b5677b36Schristos 
324b5677b36Schristos 
325b5677b36Schristos static void
free_service(struct servent * sv)326b5677b36Schristos free_service(struct servent *sv) {
327b5677b36Schristos 	char **p;
328b5677b36Schristos 
329b5677b36Schristos 	if (sv == NULL) {
330b5677b36Schristos 		return;
331b5677b36Schristos 	}
332b5677b36Schristos 
333b5677b36Schristos 	if (sv->s_name != NULL) {
334b5677b36Schristos 		free(sv->s_name);
335b5677b36Schristos 	}
336b5677b36Schristos 
337b5677b36Schristos 	for (p = sv->s_aliases ; p != NULL && *p != NULL ; p++) {
338b5677b36Schristos 		free(*p);
339b5677b36Schristos 	}
340b5677b36Schristos 
341b5677b36Schristos 	if (sv->s_proto != NULL) {
342b5677b36Schristos 		free(sv->s_proto);
343b5677b36Schristos 	}
344b5677b36Schristos }
345b5677b36Schristos 
346b5677b36Schristos 
347b5677b36Schristos 
348b5677b36Schristos /*! \file */
349