1*5bbd2a12Schristos /* $NetBSD: irp_ho.c,v 1.1.1.2 2012/09/09 16:07:58 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_ho.c,v 1.3 2005/04/27 04:56:28 sra Exp ";
22b5677b36Schristos #endif /* LIBC_SCCS and not lint */
23b5677b36Schristos
24b5677b36Schristos /* Imports. */
25b5677b36Schristos
26b5677b36Schristos #include "port_before.h"
27b5677b36Schristos
28b5677b36Schristos #include <syslog.h>
29b5677b36Schristos #include <sys/types.h>
30b5677b36Schristos #include <sys/param.h>
31b5677b36Schristos #include <sys/socket.h>
32b5677b36Schristos
33b5677b36Schristos #include <netinet/in.h>
34b5677b36Schristos #include <arpa/inet.h>
35b5677b36Schristos #include <arpa/nameser.h>
36b5677b36Schristos
37b5677b36Schristos #include <ctype.h>
38b5677b36Schristos #include <errno.h>
39b5677b36Schristos #include <fcntl.h>
40b5677b36Schristos #include <netdb.h>
41b5677b36Schristos #include <resolv.h>
42b5677b36Schristos #include <stdio.h>
43b5677b36Schristos #include <stdlib.h>
44b5677b36Schristos #include <string.h>
45b5677b36Schristos #include <syslog.h>
46b5677b36Schristos
47b5677b36Schristos #include <irs.h>
48b5677b36Schristos #include <irp.h>
49b5677b36Schristos #include <isc/irpmarshall.h>
50b5677b36Schristos #include <isc/memcluster.h>
51b5677b36Schristos
52b5677b36Schristos #include "irs_p.h"
53b5677b36Schristos #include "dns_p.h"
54b5677b36Schristos #include "irp_p.h"
55b5677b36Schristos
56b5677b36Schristos #include "port_after.h"
57b5677b36Schristos
58b5677b36Schristos /* Definitions. */
59b5677b36Schristos
60b5677b36Schristos #define MAXALIASES 35
61b5677b36Schristos #define MAXADDRS 35
62b5677b36Schristos #define Max(a,b) ((a) > (b) ? (a) : (b))
63b5677b36Schristos
64b5677b36Schristos
65b5677b36Schristos struct pvt {
66b5677b36Schristos struct irp_p *girpdata;
67b5677b36Schristos int warned;
68b5677b36Schristos struct hostent host;
69b5677b36Schristos };
70b5677b36Schristos
71b5677b36Schristos /* Forward. */
72b5677b36Schristos
73b5677b36Schristos static void ho_close(struct irs_ho *this);
74b5677b36Schristos static struct hostent * ho_byname(struct irs_ho *this, const char *name);
75b5677b36Schristos static struct hostent * ho_byname2(struct irs_ho *this, const char *name,
76b5677b36Schristos int af);
77b5677b36Schristos static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
78b5677b36Schristos int len, int af);
79b5677b36Schristos static struct hostent * ho_next(struct irs_ho *this);
80b5677b36Schristos static void ho_rewind(struct irs_ho *this);
81b5677b36Schristos static void ho_minimize(struct irs_ho *this);
82b5677b36Schristos
83b5677b36Schristos static void free_host(struct hostent *ho);
84b5677b36Schristos static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
85b5677b36Schristos const struct addrinfo *pai);
86b5677b36Schristos
87b5677b36Schristos /* Public. */
88b5677b36Schristos
89b5677b36Schristos /*%
90b5677b36Schristos * struct irs_ho * irs_irp_ho(struct irs_acc *this)
91b5677b36Schristos *
92b5677b36Schristos * Notes:
93b5677b36Schristos *
94b5677b36Schristos * Initializes the irp_ho module.
95b5677b36Schristos *
96b5677b36Schristos */
97b5677b36Schristos
98b5677b36Schristos struct irs_ho *
irs_irp_ho(struct irs_acc * this)99b5677b36Schristos irs_irp_ho(struct irs_acc *this) {
100b5677b36Schristos struct irs_ho *ho;
101b5677b36Schristos struct pvt *pvt;
102b5677b36Schristos
103b5677b36Schristos if (!(ho = memget(sizeof *ho))) {
104b5677b36Schristos errno = ENOMEM;
105b5677b36Schristos return (NULL);
106b5677b36Schristos }
107b5677b36Schristos memset(ho, 0x0, sizeof *ho);
108b5677b36Schristos
109b5677b36Schristos if (!(pvt = memget(sizeof *pvt))) {
110b5677b36Schristos memput(ho, sizeof *ho);
111b5677b36Schristos errno = ENOMEM;
112b5677b36Schristos return (NULL);
113b5677b36Schristos }
114b5677b36Schristos memset(pvt, 0, sizeof *pvt);
115b5677b36Schristos pvt->girpdata = this->private;
116b5677b36Schristos
117b5677b36Schristos ho->private = pvt;
118b5677b36Schristos ho->close = ho_close;
119b5677b36Schristos ho->byname = ho_byname;
120b5677b36Schristos ho->byname2 = ho_byname2;
121b5677b36Schristos ho->byaddr = ho_byaddr;
122b5677b36Schristos ho->next = ho_next;
123b5677b36Schristos ho->rewind = ho_rewind;
124b5677b36Schristos ho->minimize = ho_minimize;
125b5677b36Schristos ho->addrinfo = ho_addrinfo;
126b5677b36Schristos
127b5677b36Schristos return (ho);
128b5677b36Schristos }
129b5677b36Schristos
130b5677b36Schristos /* Methods. */
131b5677b36Schristos
132b5677b36Schristos /*%
133b5677b36Schristos * Closes down the module.
134b5677b36Schristos *
135b5677b36Schristos */
136b5677b36Schristos
137b5677b36Schristos static void
ho_close(struct irs_ho * this)138b5677b36Schristos ho_close(struct irs_ho *this) {
139b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
140b5677b36Schristos
141b5677b36Schristos ho_minimize(this);
142b5677b36Schristos
143b5677b36Schristos free_host(&pvt->host);
144b5677b36Schristos
145b5677b36Schristos memput(pvt, sizeof *pvt);
146b5677b36Schristos memput(this, sizeof *this);
147b5677b36Schristos }
148b5677b36Schristos
149b5677b36Schristos
150b5677b36Schristos
151b5677b36Schristos /*
152b5677b36Schristos * struct hostent * ho_byname(struct irs_ho *this, const char *name)
153b5677b36Schristos *
154b5677b36Schristos */
155b5677b36Schristos
156b5677b36Schristos static struct hostent *
ho_byname(struct irs_ho * this,const char * name)157b5677b36Schristos ho_byname(struct irs_ho *this, const char *name) {
158b5677b36Schristos return (ho_byname2(this, name, AF_INET));
159b5677b36Schristos }
160b5677b36Schristos
161b5677b36Schristos
162b5677b36Schristos
163b5677b36Schristos
164b5677b36Schristos
165b5677b36Schristos /*
166b5677b36Schristos * struct hostent * ho_byname2(struct irs_ho *this, const char *name, int af)
167b5677b36Schristos *
168b5677b36Schristos */
169b5677b36Schristos
170b5677b36Schristos static struct hostent *
ho_byname2(struct irs_ho * this,const char * name,int af)171b5677b36Schristos ho_byname2(struct irs_ho *this, const char *name, int af) {
172b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
173b5677b36Schristos struct hostent *ho = &pvt->host;
174b5677b36Schristos char *body = NULL;
175b5677b36Schristos size_t bodylen;
176b5677b36Schristos int code;
177b5677b36Schristos char text[256];
178b5677b36Schristos
179b5677b36Schristos if (ho->h_name != NULL &&
180b5677b36Schristos strcmp(name, ho->h_name) == 0 &&
181b5677b36Schristos af == ho->h_addrtype) {
182b5677b36Schristos return (ho);
183b5677b36Schristos }
184b5677b36Schristos
185b5677b36Schristos if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
186b5677b36Schristos return (NULL);
187b5677b36Schristos }
188b5677b36Schristos
189b5677b36Schristos if (irs_irp_send_command(pvt->girpdata, "gethostbyname2 %s %s",
190b5677b36Schristos name, ADDR_T_STR(af)) != 0)
191b5677b36Schristos return (NULL);
192b5677b36Schristos
193b5677b36Schristos if (irs_irp_get_full_response(pvt->girpdata, &code,
194b5677b36Schristos text, sizeof text,
195b5677b36Schristos &body, &bodylen) != 0) {
196b5677b36Schristos return (NULL);
197b5677b36Schristos }
198b5677b36Schristos
199b5677b36Schristos if (code == IRPD_GETHOST_OK) {
200b5677b36Schristos free_host(ho);
201b5677b36Schristos if (irp_unmarshall_ho(ho, body) != 0) {
202b5677b36Schristos ho = NULL;
203b5677b36Schristos }
204b5677b36Schristos } else {
205b5677b36Schristos ho = NULL;
206b5677b36Schristos }
207b5677b36Schristos
208b5677b36Schristos if (body != NULL) {
209b5677b36Schristos memput(body, bodylen);
210b5677b36Schristos }
211b5677b36Schristos
212b5677b36Schristos return (ho);
213b5677b36Schristos }
214b5677b36Schristos
215b5677b36Schristos
216b5677b36Schristos
217b5677b36Schristos /*
218b5677b36Schristos * struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
219b5677b36Schristos * int len, int af)
220b5677b36Schristos *
221b5677b36Schristos */
222b5677b36Schristos
223b5677b36Schristos static struct hostent *
ho_byaddr(struct irs_ho * this,const void * addr,int len,int af)224b5677b36Schristos ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) {
225b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
226b5677b36Schristos struct hostent *ho = &pvt->host;
227b5677b36Schristos char *body = NULL;
228b5677b36Schristos size_t bodylen;
229b5677b36Schristos int code;
230b5677b36Schristos char **p;
231b5677b36Schristos char paddr[MAXPADDRSIZE];
232b5677b36Schristos char text[256];
233b5677b36Schristos
234b5677b36Schristos if (ho->h_name != NULL &&
235b5677b36Schristos af == ho->h_addrtype &&
236b5677b36Schristos len == ho->h_length) {
237b5677b36Schristos for (p = ho->h_addr_list ; *p != NULL ; p++) {
238b5677b36Schristos if (memcmp(*p, addr, len) == 0)
239b5677b36Schristos return (ho);
240b5677b36Schristos }
241b5677b36Schristos }
242b5677b36Schristos
243b5677b36Schristos if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
244b5677b36Schristos return (NULL);
245b5677b36Schristos }
246b5677b36Schristos
247b5677b36Schristos if (inet_ntop(af, addr, paddr, sizeof paddr) == NULL) {
248b5677b36Schristos return (NULL);
249b5677b36Schristos }
250b5677b36Schristos
251b5677b36Schristos if (irs_irp_send_command(pvt->girpdata, "gethostbyaddr %s %s",
252b5677b36Schristos paddr, ADDR_T_STR(af)) != 0) {
253b5677b36Schristos return (NULL);
254b5677b36Schristos }
255b5677b36Schristos
256b5677b36Schristos if (irs_irp_get_full_response(pvt->girpdata, &code,
257b5677b36Schristos text, sizeof text,
258b5677b36Schristos &body, &bodylen) != 0) {
259b5677b36Schristos return (NULL);
260b5677b36Schristos }
261b5677b36Schristos
262b5677b36Schristos if (code == IRPD_GETHOST_OK) {
263b5677b36Schristos free_host(ho);
264b5677b36Schristos if (irp_unmarshall_ho(ho, body) != 0) {
265b5677b36Schristos ho = NULL;
266b5677b36Schristos }
267b5677b36Schristos } else {
268b5677b36Schristos ho = NULL;
269b5677b36Schristos }
270b5677b36Schristos
271b5677b36Schristos if (body != NULL) {
272b5677b36Schristos memput(body, bodylen);
273b5677b36Schristos }
274b5677b36Schristos
275b5677b36Schristos return (ho);
276b5677b36Schristos }
277b5677b36Schristos
278b5677b36Schristos /*%
279b5677b36Schristos * The implementation for gethostent(3). The first time it's
280b5677b36Schristos * called all the data is pulled from the remote(i.e. what
281b5677b36Schristos * the maximum number of gethostent(3) calls would return)
282b5677b36Schristos * and that data is cached.
283b5677b36Schristos *
284b5677b36Schristos */
285b5677b36Schristos
286b5677b36Schristos static struct hostent *
ho_next(struct irs_ho * this)287b5677b36Schristos ho_next(struct irs_ho *this) {
288b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
289b5677b36Schristos struct hostent *ho = &pvt->host;
290b5677b36Schristos char *body;
291b5677b36Schristos size_t bodylen;
292b5677b36Schristos int code;
293b5677b36Schristos char text[256];
294b5677b36Schristos
295b5677b36Schristos if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
296b5677b36Schristos return (NULL);
297b5677b36Schristos }
298b5677b36Schristos
299b5677b36Schristos if (irs_irp_send_command(pvt->girpdata, "gethostent") != 0) {
300b5677b36Schristos return (NULL);
301b5677b36Schristos }
302b5677b36Schristos
303b5677b36Schristos if (irs_irp_get_full_response(pvt->girpdata, &code,
304b5677b36Schristos text, sizeof text,
305b5677b36Schristos &body, &bodylen) != 0) {
306b5677b36Schristos return (NULL);
307b5677b36Schristos }
308b5677b36Schristos
309b5677b36Schristos if (code == IRPD_GETHOST_OK) {
310b5677b36Schristos free_host(ho);
311b5677b36Schristos if (irp_unmarshall_ho(ho, body) != 0) {
312b5677b36Schristos ho = NULL;
313b5677b36Schristos }
314b5677b36Schristos } else {
315b5677b36Schristos ho = NULL;
316b5677b36Schristos }
317b5677b36Schristos
318b5677b36Schristos if (body != NULL) {
319b5677b36Schristos memput(body, bodylen);
320b5677b36Schristos }
321b5677b36Schristos
322b5677b36Schristos return (ho);
323b5677b36Schristos }
324b5677b36Schristos
325b5677b36Schristos /*%
326b5677b36Schristos * void ho_rewind(struct irs_ho *this)
327b5677b36Schristos *
328b5677b36Schristos */
329b5677b36Schristos
330b5677b36Schristos static void
ho_rewind(struct irs_ho * this)331b5677b36Schristos ho_rewind(struct irs_ho *this) {
332b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
333b5677b36Schristos char text[256];
334b5677b36Schristos int code;
335b5677b36Schristos
336b5677b36Schristos if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
337b5677b36Schristos return;
338b5677b36Schristos }
339b5677b36Schristos
340b5677b36Schristos if (irs_irp_send_command(pvt->girpdata, "sethostent") != 0) {
341b5677b36Schristos return;
342b5677b36Schristos }
343b5677b36Schristos
344b5677b36Schristos code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
345b5677b36Schristos if (code != IRPD_GETHOST_SETOK) {
346b5677b36Schristos if (irp_log_errors) {
347b5677b36Schristos syslog(LOG_WARNING, "sethostent failed: %s", text);
348b5677b36Schristos }
349b5677b36Schristos }
350b5677b36Schristos
351b5677b36Schristos return;
352b5677b36Schristos }
353b5677b36Schristos
354b5677b36Schristos /*%
355b5677b36Schristos * void ho_minimize(struct irs_ho *this)
356b5677b36Schristos *
357b5677b36Schristos */
358b5677b36Schristos
359b5677b36Schristos static void
ho_minimize(struct irs_ho * this)360b5677b36Schristos ho_minimize(struct irs_ho *this) {
361b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
362b5677b36Schristos
363b5677b36Schristos free_host(&pvt->host);
364b5677b36Schristos
365b5677b36Schristos irs_irp_disconnect(pvt->girpdata);
366b5677b36Schristos }
367b5677b36Schristos
368b5677b36Schristos /*%
369b5677b36Schristos * void free_host(struct hostent *ho)
370b5677b36Schristos *
371b5677b36Schristos */
372b5677b36Schristos
373b5677b36Schristos static void
free_host(struct hostent * ho)374b5677b36Schristos free_host(struct hostent *ho) {
375b5677b36Schristos char **p;
376b5677b36Schristos
377b5677b36Schristos if (ho == NULL) {
378b5677b36Schristos return;
379b5677b36Schristos }
380b5677b36Schristos
381b5677b36Schristos if (ho->h_name != NULL)
382b5677b36Schristos free(ho->h_name);
383b5677b36Schristos
384b5677b36Schristos if (ho->h_aliases != NULL) {
385b5677b36Schristos for (p = ho->h_aliases ; *p != NULL ; p++)
386b5677b36Schristos free(*p);
387b5677b36Schristos free(ho->h_aliases);
388b5677b36Schristos }
389b5677b36Schristos
390b5677b36Schristos if (ho->h_addr_list != NULL) {
391b5677b36Schristos for (p = ho->h_addr_list ; *p != NULL ; p++)
392b5677b36Schristos free(*p);
393b5677b36Schristos free(ho->h_addr_list);
394b5677b36Schristos }
395b5677b36Schristos }
396b5677b36Schristos
397b5677b36Schristos /* dummy */
398b5677b36Schristos static struct addrinfo *
ho_addrinfo(struct irs_ho * this,const char * name,const struct addrinfo * pai)399b5677b36Schristos ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
400b5677b36Schristos {
401b5677b36Schristos UNUSED(this);
402b5677b36Schristos UNUSED(name);
403b5677b36Schristos UNUSED(pai);
404b5677b36Schristos return(NULL);
405b5677b36Schristos }
406b5677b36Schristos
407b5677b36Schristos /*! \file */
408