10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate *
9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate */
170Sstevel@tonic-gate
180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: irp_ho.c,v 1.3 2005/04/27 04:56:28 sra Exp $";
200Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
210Sstevel@tonic-gate
220Sstevel@tonic-gate /* Imports. */
230Sstevel@tonic-gate
240Sstevel@tonic-gate #include "port_before.h"
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <syslog.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/socket.h>
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <netinet/in.h>
320Sstevel@tonic-gate #include <arpa/inet.h>
330Sstevel@tonic-gate #include <arpa/nameser.h>
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <fcntl.h>
380Sstevel@tonic-gate #include <netdb.h>
390Sstevel@tonic-gate #include <resolv.h>
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <string.h>
430Sstevel@tonic-gate #include <syslog.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate #include <irs.h>
460Sstevel@tonic-gate #include <irp.h>
470Sstevel@tonic-gate #include <isc/irpmarshall.h>
480Sstevel@tonic-gate #include <isc/memcluster.h>
490Sstevel@tonic-gate
500Sstevel@tonic-gate #include "irs_p.h"
510Sstevel@tonic-gate #include "dns_p.h"
520Sstevel@tonic-gate #include "irp_p.h"
530Sstevel@tonic-gate
540Sstevel@tonic-gate #include "port_after.h"
550Sstevel@tonic-gate
560Sstevel@tonic-gate /* Definitions. */
570Sstevel@tonic-gate
580Sstevel@tonic-gate #define MAXALIASES 35
590Sstevel@tonic-gate #define MAXADDRS 35
600Sstevel@tonic-gate #define Max(a,b) ((a) > (b) ? (a) : (b))
610Sstevel@tonic-gate
620Sstevel@tonic-gate
630Sstevel@tonic-gate struct pvt {
640Sstevel@tonic-gate struct irp_p *girpdata;
650Sstevel@tonic-gate int warned;
660Sstevel@tonic-gate struct hostent host;
670Sstevel@tonic-gate };
680Sstevel@tonic-gate
690Sstevel@tonic-gate /* Forward. */
700Sstevel@tonic-gate
710Sstevel@tonic-gate static void ho_close(struct irs_ho *this);
720Sstevel@tonic-gate static struct hostent * ho_byname(struct irs_ho *this, const char *name);
730Sstevel@tonic-gate static struct hostent * ho_byname2(struct irs_ho *this, const char *name,
740Sstevel@tonic-gate int af);
750Sstevel@tonic-gate static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
760Sstevel@tonic-gate int len, int af);
770Sstevel@tonic-gate static struct hostent * ho_next(struct irs_ho *this);
780Sstevel@tonic-gate static void ho_rewind(struct irs_ho *this);
790Sstevel@tonic-gate static void ho_minimize(struct irs_ho *this);
800Sstevel@tonic-gate
810Sstevel@tonic-gate static void free_host(struct hostent *ho);
820Sstevel@tonic-gate static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
830Sstevel@tonic-gate const struct addrinfo *pai);
840Sstevel@tonic-gate
850Sstevel@tonic-gate /* Public. */
860Sstevel@tonic-gate
87*11038SRao.Shoaib@Sun.COM /*%
880Sstevel@tonic-gate * struct irs_ho * irs_irp_ho(struct irs_acc *this)
890Sstevel@tonic-gate *
900Sstevel@tonic-gate * Notes:
910Sstevel@tonic-gate *
920Sstevel@tonic-gate * Initializes the irp_ho module.
930Sstevel@tonic-gate *
940Sstevel@tonic-gate */
950Sstevel@tonic-gate
960Sstevel@tonic-gate struct irs_ho *
irs_irp_ho(struct irs_acc * this)970Sstevel@tonic-gate irs_irp_ho(struct irs_acc *this) {
980Sstevel@tonic-gate struct irs_ho *ho;
990Sstevel@tonic-gate struct pvt *pvt;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate if (!(ho = memget(sizeof *ho))) {
1020Sstevel@tonic-gate errno = ENOMEM;
1030Sstevel@tonic-gate return (NULL);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate memset(ho, 0x0, sizeof *ho);
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) {
1080Sstevel@tonic-gate memput(ho, sizeof *ho);
1090Sstevel@tonic-gate errno = ENOMEM;
1100Sstevel@tonic-gate return (NULL);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt);
1130Sstevel@tonic-gate pvt->girpdata = this->private;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate ho->private = pvt;
1160Sstevel@tonic-gate ho->close = ho_close;
1170Sstevel@tonic-gate ho->byname = ho_byname;
1180Sstevel@tonic-gate ho->byname2 = ho_byname2;
1190Sstevel@tonic-gate ho->byaddr = ho_byaddr;
1200Sstevel@tonic-gate ho->next = ho_next;
1210Sstevel@tonic-gate ho->rewind = ho_rewind;
1220Sstevel@tonic-gate ho->minimize = ho_minimize;
1230Sstevel@tonic-gate ho->addrinfo = ho_addrinfo;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate return (ho);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /* Methods. */
1290Sstevel@tonic-gate
130*11038SRao.Shoaib@Sun.COM /*%
1310Sstevel@tonic-gate * Closes down the module.
1320Sstevel@tonic-gate *
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate static void
ho_close(struct irs_ho * this)1360Sstevel@tonic-gate ho_close(struct irs_ho *this) {
1370Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate ho_minimize(this);
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate free_host(&pvt->host);
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1440Sstevel@tonic-gate memput(this, sizeof *this);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * struct hostent * ho_byname(struct irs_ho *this, const char *name)
1510Sstevel@tonic-gate *
1520Sstevel@tonic-gate */
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate static struct hostent *
ho_byname(struct irs_ho * this,const char * name)1550Sstevel@tonic-gate ho_byname(struct irs_ho *this, const char *name) {
1560Sstevel@tonic-gate return (ho_byname2(this, name, AF_INET));
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate * struct hostent * ho_byname2(struct irs_ho *this, const char *name, int af)
1650Sstevel@tonic-gate *
1660Sstevel@tonic-gate */
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate static struct hostent *
ho_byname2(struct irs_ho * this,const char * name,int af)1690Sstevel@tonic-gate ho_byname2(struct irs_ho *this, const char *name, int af) {
1700Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1710Sstevel@tonic-gate struct hostent *ho = &pvt->host;
1720Sstevel@tonic-gate char *body = NULL;
1730Sstevel@tonic-gate size_t bodylen;
1740Sstevel@tonic-gate int code;
1750Sstevel@tonic-gate char text[256];
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (ho->h_name != NULL &&
1780Sstevel@tonic-gate strcmp(name, ho->h_name) == 0 &&
1790Sstevel@tonic-gate af == ho->h_addrtype) {
1800Sstevel@tonic-gate return (ho);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1840Sstevel@tonic-gate return (NULL);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "gethostbyname2 %s %s",
1880Sstevel@tonic-gate name, ADDR_T_STR(af)) != 0)
1890Sstevel@tonic-gate return (NULL);
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code,
1920Sstevel@tonic-gate text, sizeof text,
1930Sstevel@tonic-gate &body, &bodylen) != 0) {
1940Sstevel@tonic-gate return (NULL);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate if (code == IRPD_GETHOST_OK) {
1980Sstevel@tonic-gate free_host(ho);
1990Sstevel@tonic-gate if (irp_unmarshall_ho(ho, body) != 0) {
2000Sstevel@tonic-gate ho = NULL;
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate } else {
2030Sstevel@tonic-gate ho = NULL;
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate if (body != NULL) {
2070Sstevel@tonic-gate memput(body, bodylen);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate return (ho);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate * struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
2170Sstevel@tonic-gate * int len, int af)
2180Sstevel@tonic-gate *
2190Sstevel@tonic-gate */
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate static struct hostent *
ho_byaddr(struct irs_ho * this,const void * addr,int len,int af)2220Sstevel@tonic-gate ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) {
2230Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2240Sstevel@tonic-gate struct hostent *ho = &pvt->host;
2250Sstevel@tonic-gate char *body = NULL;
2260Sstevel@tonic-gate size_t bodylen;
2270Sstevel@tonic-gate int code;
2280Sstevel@tonic-gate char **p;
2290Sstevel@tonic-gate char paddr[MAXPADDRSIZE];
2300Sstevel@tonic-gate char text[256];
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate if (ho->h_name != NULL &&
2330Sstevel@tonic-gate af == ho->h_addrtype &&
2340Sstevel@tonic-gate len == ho->h_length) {
2350Sstevel@tonic-gate for (p = ho->h_addr_list ; *p != NULL ; p++) {
2360Sstevel@tonic-gate if (memcmp(*p, addr, len) == 0)
2370Sstevel@tonic-gate return (ho);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2420Sstevel@tonic-gate return (NULL);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if (inet_ntop(af, addr, paddr, sizeof paddr) == NULL) {
2460Sstevel@tonic-gate return (NULL);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "gethostbyaddr %s %s",
2500Sstevel@tonic-gate paddr, ADDR_T_STR(af)) != 0) {
2510Sstevel@tonic-gate return (NULL);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code,
2550Sstevel@tonic-gate text, sizeof text,
2560Sstevel@tonic-gate &body, &bodylen) != 0) {
2570Sstevel@tonic-gate return (NULL);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate if (code == IRPD_GETHOST_OK) {
2610Sstevel@tonic-gate free_host(ho);
2620Sstevel@tonic-gate if (irp_unmarshall_ho(ho, body) != 0) {
2630Sstevel@tonic-gate ho = NULL;
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate } else {
2660Sstevel@tonic-gate ho = NULL;
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate if (body != NULL) {
2700Sstevel@tonic-gate memput(body, bodylen);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate return (ho);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate
276*11038SRao.Shoaib@Sun.COM /*%
2770Sstevel@tonic-gate * The implementation for gethostent(3). The first time it's
2780Sstevel@tonic-gate * called all the data is pulled from the remote(i.e. what
2790Sstevel@tonic-gate * the maximum number of gethostent(3) calls would return)
2800Sstevel@tonic-gate * and that data is cached.
2810Sstevel@tonic-gate *
2820Sstevel@tonic-gate */
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate static struct hostent *
ho_next(struct irs_ho * this)2850Sstevel@tonic-gate ho_next(struct irs_ho *this) {
2860Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2870Sstevel@tonic-gate struct hostent *ho = &pvt->host;
2880Sstevel@tonic-gate char *body;
2890Sstevel@tonic-gate size_t bodylen;
2900Sstevel@tonic-gate int code;
2910Sstevel@tonic-gate char text[256];
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2940Sstevel@tonic-gate return (NULL);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "gethostent") != 0) {
2980Sstevel@tonic-gate return (NULL);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code,
3020Sstevel@tonic-gate text, sizeof text,
3030Sstevel@tonic-gate &body, &bodylen) != 0) {
3040Sstevel@tonic-gate return (NULL);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate if (code == IRPD_GETHOST_OK) {
3080Sstevel@tonic-gate free_host(ho);
3090Sstevel@tonic-gate if (irp_unmarshall_ho(ho, body) != 0) {
3100Sstevel@tonic-gate ho = NULL;
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate } else {
3130Sstevel@tonic-gate ho = NULL;
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate if (body != NULL) {
3170Sstevel@tonic-gate memput(body, bodylen);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate return (ho);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate
323*11038SRao.Shoaib@Sun.COM /*%
3240Sstevel@tonic-gate * void ho_rewind(struct irs_ho *this)
3250Sstevel@tonic-gate *
3260Sstevel@tonic-gate */
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate static void
ho_rewind(struct irs_ho * this)3290Sstevel@tonic-gate ho_rewind(struct irs_ho *this) {
3300Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3310Sstevel@tonic-gate char text[256];
3320Sstevel@tonic-gate int code;
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
3350Sstevel@tonic-gate return;
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "sethostent") != 0) {
3390Sstevel@tonic-gate return;
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
3430Sstevel@tonic-gate if (code != IRPD_GETHOST_SETOK) {
3440Sstevel@tonic-gate if (irp_log_errors) {
3450Sstevel@tonic-gate syslog(LOG_WARNING, "sethostent failed: %s", text);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate return;
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
352*11038SRao.Shoaib@Sun.COM /*%
3530Sstevel@tonic-gate * void ho_minimize(struct irs_ho *this)
3540Sstevel@tonic-gate *
3550Sstevel@tonic-gate */
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate static void
ho_minimize(struct irs_ho * this)3580Sstevel@tonic-gate ho_minimize(struct irs_ho *this) {
3590Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate free_host(&pvt->host);
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate irs_irp_disconnect(pvt->girpdata);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate
366*11038SRao.Shoaib@Sun.COM /*%
3670Sstevel@tonic-gate * void free_host(struct hostent *ho)
3680Sstevel@tonic-gate *
3690Sstevel@tonic-gate */
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate static void
free_host(struct hostent * ho)3720Sstevel@tonic-gate free_host(struct hostent *ho) {
3730Sstevel@tonic-gate char **p;
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate if (ho == NULL) {
3760Sstevel@tonic-gate return;
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate if (ho->h_name != NULL)
3800Sstevel@tonic-gate free(ho->h_name);
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate if (ho->h_aliases != NULL) {
3830Sstevel@tonic-gate for (p = ho->h_aliases ; *p != NULL ; p++)
3840Sstevel@tonic-gate free(*p);
3850Sstevel@tonic-gate free(ho->h_aliases);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate if (ho->h_addr_list != NULL) {
3890Sstevel@tonic-gate for (p = ho->h_addr_list ; *p != NULL ; p++)
3900Sstevel@tonic-gate free(*p);
3910Sstevel@tonic-gate free(ho->h_addr_list);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate /* dummy */
3960Sstevel@tonic-gate static struct addrinfo *
ho_addrinfo(struct irs_ho * this,const char * name,const struct addrinfo * pai)3970Sstevel@tonic-gate ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate UNUSED(this);
4000Sstevel@tonic-gate UNUSED(name);
4010Sstevel@tonic-gate UNUSED(pai);
4020Sstevel@tonic-gate return(NULL);
4030Sstevel@tonic-gate }
404*11038SRao.Shoaib@Sun.COM
405*11038SRao.Shoaib@Sun.COM /*! \file */
406