17c478bd9Sstevel@tonic-gate /*
29525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
99525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
109525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
129525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
159525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate /* Imports */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include "port_before.h"
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <syslog.h>
237c478bd9Sstevel@tonic-gate #include <sys/types.h>
247c478bd9Sstevel@tonic-gate #include <sys/socket.h>
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <netinet/in.h>
277c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
287c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <resolv.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <syslog.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <irs.h>
397c478bd9Sstevel@tonic-gate #include <irp.h>
407c478bd9Sstevel@tonic-gate #include <isc/irpmarshall.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
437c478bd9Sstevel@tonic-gate #include <isc/misc.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "irs_p.h"
467c478bd9Sstevel@tonic-gate #include "lcl_p.h"
477c478bd9Sstevel@tonic-gate #include "irp_p.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include "port_after.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define MAXALIASES 35
527c478bd9Sstevel@tonic-gate #define MAXADDRSIZE 4
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate struct pvt {
557c478bd9Sstevel@tonic-gate 	struct irp_p	       *girpdata;
567c478bd9Sstevel@tonic-gate 	int			warned;
577c478bd9Sstevel@tonic-gate 	struct nwent		net;
587c478bd9Sstevel@tonic-gate };
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* Forward */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static void		nw_close(struct irs_nw *);
637c478bd9Sstevel@tonic-gate static struct nwent *	nw_byname(struct irs_nw *, const char *, int);
647c478bd9Sstevel@tonic-gate static struct nwent *	nw_byaddr(struct irs_nw *, void *, int, int);
657c478bd9Sstevel@tonic-gate static struct nwent *	nw_next(struct irs_nw *);
667c478bd9Sstevel@tonic-gate static void		nw_rewind(struct irs_nw *);
677c478bd9Sstevel@tonic-gate static void		nw_minimize(struct irs_nw *);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static void		free_nw(struct nwent *nw);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /* Public */
737c478bd9Sstevel@tonic-gate 
749525b14bSRao Shoaib /*%
75*55fea89dSDan Cross  * struct irs_nw * irs_irp_nw(struct irs_acc *this)
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate struct irs_nw *
irs_irp_nw(struct irs_acc * this)807c478bd9Sstevel@tonic-gate irs_irp_nw(struct irs_acc *this) {
817c478bd9Sstevel@tonic-gate 	struct irs_nw *nw;
827c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
857c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
867c478bd9Sstevel@tonic-gate 		return (NULL);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (!(nw = memget(sizeof *nw))) {
917c478bd9Sstevel@tonic-gate 		memput(pvt, sizeof *pvt);
927c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
937c478bd9Sstevel@tonic-gate 		return (NULL);
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 	memset(nw, 0x0, sizeof *nw);
967c478bd9Sstevel@tonic-gate 	pvt->girpdata = this->private;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	nw->private = pvt;
997c478bd9Sstevel@tonic-gate 	nw->close = nw_close;
1007c478bd9Sstevel@tonic-gate 	nw->byname = nw_byname;
1017c478bd9Sstevel@tonic-gate 	nw->byaddr = nw_byaddr;
1027c478bd9Sstevel@tonic-gate 	nw->next = nw_next;
1037c478bd9Sstevel@tonic-gate 	nw->rewind = nw_rewind;
1047c478bd9Sstevel@tonic-gate 	nw->minimize = nw_minimize;
1057c478bd9Sstevel@tonic-gate 	return (nw);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /* Methods */
1097c478bd9Sstevel@tonic-gate 
1109525b14bSRao Shoaib /*%
111*55fea89dSDan Cross  * void nw_close(struct irs_nw *this)
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static void
nw_close(struct irs_nw * this)1167c478bd9Sstevel@tonic-gate nw_close(struct irs_nw *this) {
1177c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	nw_minimize(this);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	free_nw(&pvt->net);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1247c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1279525b14bSRao Shoaib /*%
128*55fea89dSDan Cross  * struct nwent * nw_byaddr(struct irs_nw *this, void *net,
129*55fea89dSDan Cross  * 				int length, int type)
1307c478bd9Sstevel@tonic-gate  *
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static struct nwent *
nw_byaddr(struct irs_nw * this,void * net,int length,int type)1347c478bd9Sstevel@tonic-gate nw_byaddr(struct irs_nw *this, void *net, int length, int type) {
1357c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1367c478bd9Sstevel@tonic-gate 	struct nwent *nw = &pvt->net;
1377c478bd9Sstevel@tonic-gate 	char *body = NULL;
1387c478bd9Sstevel@tonic-gate 	size_t bodylen;
1397c478bd9Sstevel@tonic-gate 	int code;
1409525b14bSRao Shoaib 	char paddr[24];			/*%< bigenough for ip4 w/ cidr spec. */
1417c478bd9Sstevel@tonic-gate 	char text[256];
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (inet_net_ntop(type, net, length, paddr, sizeof paddr) == NULL) {
1447c478bd9Sstevel@tonic-gate 		return (NULL);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1487c478bd9Sstevel@tonic-gate 		return (NULL);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "getnetbyaddr %s %s",
1527c478bd9Sstevel@tonic-gate 				 paddr, ADDR_T_STR(type)) != 0)
1537c478bd9Sstevel@tonic-gate 		return (NULL);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (irs_irp_get_full_response(pvt->girpdata, &code,
1567c478bd9Sstevel@tonic-gate 				      text, sizeof text,
1577c478bd9Sstevel@tonic-gate 				      &body, &bodylen) != 0) {
1587c478bd9Sstevel@tonic-gate 		return (NULL);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (code == IRPD_GETNET_OK) {
1627c478bd9Sstevel@tonic-gate 		free_nw(nw);
1637c478bd9Sstevel@tonic-gate 		if (irp_unmarshall_nw(nw, body) != 0) {
1647c478bd9Sstevel@tonic-gate 			nw = NULL;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 	} else {
1677c478bd9Sstevel@tonic-gate 		nw = NULL;
1687c478bd9Sstevel@tonic-gate 	}
169*55fea89dSDan Cross 
1707c478bd9Sstevel@tonic-gate 	if (body != NULL) {
1717c478bd9Sstevel@tonic-gate 		memput(body, bodylen);
1727c478bd9Sstevel@tonic-gate 	}
173*55fea89dSDan Cross 
1747c478bd9Sstevel@tonic-gate 	return (nw);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1779525b14bSRao Shoaib /*%
178*55fea89dSDan Cross  * struct nwent * nw_byname(struct irs_nw *this, const char *name, int type)
1797c478bd9Sstevel@tonic-gate  *
1807c478bd9Sstevel@tonic-gate  */
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate static struct nwent *
nw_byname(struct irs_nw * this,const char * name,int type)1837c478bd9Sstevel@tonic-gate nw_byname(struct irs_nw *this, const char *name, int type) {
1847c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1857c478bd9Sstevel@tonic-gate 	struct nwent *nw = &pvt->net;
1867c478bd9Sstevel@tonic-gate 	char *body = NULL;
1877c478bd9Sstevel@tonic-gate 	size_t bodylen;
1887c478bd9Sstevel@tonic-gate 	int code;
1897c478bd9Sstevel@tonic-gate 	char text[256];
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (nw->n_name != NULL &&
1927c478bd9Sstevel@tonic-gate 	    strcmp(name, nw->n_name) == 0 &&
1937c478bd9Sstevel@tonic-gate 	    nw->n_addrtype == type) {
1947c478bd9Sstevel@tonic-gate 		return (nw);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1987c478bd9Sstevel@tonic-gate 		return (NULL);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "getnetbyname %s", name) != 0)
2027c478bd9Sstevel@tonic-gate 		return (NULL);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (irs_irp_get_full_response(pvt->girpdata, &code,
2057c478bd9Sstevel@tonic-gate 				      text, sizeof text,
2067c478bd9Sstevel@tonic-gate 				      &body, &bodylen) != 0) {
2077c478bd9Sstevel@tonic-gate 		return (NULL);
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (code == IRPD_GETNET_OK) {
2117c478bd9Sstevel@tonic-gate 		free_nw(nw);
2127c478bd9Sstevel@tonic-gate 		if (irp_unmarshall_nw(nw, body) != 0) {
2137c478bd9Sstevel@tonic-gate 			nw = NULL;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 	} else {
2167c478bd9Sstevel@tonic-gate 		nw = NULL;
2177c478bd9Sstevel@tonic-gate 	}
218*55fea89dSDan Cross 
2197c478bd9Sstevel@tonic-gate 	if (body != NULL) {
2207c478bd9Sstevel@tonic-gate 		memput(body, bodylen);
2217c478bd9Sstevel@tonic-gate 	}
222*55fea89dSDan Cross 
2237c478bd9Sstevel@tonic-gate 	return (nw);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2269525b14bSRao Shoaib /*%
227*55fea89dSDan Cross  * void nw_rewind(struct irs_nw *this)
2287c478bd9Sstevel@tonic-gate  *
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static void
nw_rewind(struct irs_nw * this)2327c478bd9Sstevel@tonic-gate nw_rewind(struct irs_nw *this) {
2337c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2347c478bd9Sstevel@tonic-gate 	char text[256];
2357c478bd9Sstevel@tonic-gate 	int code;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2387c478bd9Sstevel@tonic-gate 		return;
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "setnetent") != 0) {
2427c478bd9Sstevel@tonic-gate 		return;
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
2467c478bd9Sstevel@tonic-gate 	if (code != IRPD_GETNET_SETOK) {
2477c478bd9Sstevel@tonic-gate 		if (irp_log_errors) {
2487c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING, "setnetent failed: %s", text);
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 	}
251*55fea89dSDan Cross 
2527c478bd9Sstevel@tonic-gate 	return;
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2559525b14bSRao Shoaib /*%
256*55fea89dSDan Cross  * 	Prepares the cache if necessary and returns the first, or
2577c478bd9Sstevel@tonic-gate  * 	next item from it.
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static struct nwent *
nw_next(struct irs_nw * this)2617c478bd9Sstevel@tonic-gate nw_next(struct irs_nw *this) {
2627c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2637c478bd9Sstevel@tonic-gate 	struct nwent *nw = &pvt->net;
2647c478bd9Sstevel@tonic-gate 	char *body;
2657c478bd9Sstevel@tonic-gate 	size_t bodylen;
2667c478bd9Sstevel@tonic-gate 	int code;
2677c478bd9Sstevel@tonic-gate 	char text[256];
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2707c478bd9Sstevel@tonic-gate 		return (NULL);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "getnetent") != 0) {
2747c478bd9Sstevel@tonic-gate 		return (NULL);
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (irs_irp_get_full_response(pvt->girpdata, &code,
2787c478bd9Sstevel@tonic-gate 				      text, sizeof text,
2797c478bd9Sstevel@tonic-gate 				      &body, &bodylen) != 0) {
2807c478bd9Sstevel@tonic-gate 		return (NULL);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (code == IRPD_GETNET_OK) {
2847c478bd9Sstevel@tonic-gate 		free_nw(nw);
2857c478bd9Sstevel@tonic-gate 		if (irp_unmarshall_nw(nw, body) != 0) {
2867c478bd9Sstevel@tonic-gate 			nw = NULL;
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 	} else {
2897c478bd9Sstevel@tonic-gate 		nw = NULL;
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2929525b14bSRao Shoaib 	if (body != NULL)
2939525b14bSRao Shoaib 		memput(body, bodylen);
2947c478bd9Sstevel@tonic-gate 	return (nw);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2979525b14bSRao Shoaib /*%
298*55fea89dSDan Cross  * void nw_minimize(struct irs_nw *this)
2997c478bd9Sstevel@tonic-gate  *
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate static void
nw_minimize(struct irs_nw * this)3037c478bd9Sstevel@tonic-gate nw_minimize(struct irs_nw *this) {
3047c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	irs_irp_disconnect(pvt->girpdata);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /* private. */
3137c478bd9Sstevel@tonic-gate 
3149525b14bSRao Shoaib /*%
3157c478bd9Sstevel@tonic-gate  *	deallocate all the memory irp_unmarshall_pw allocated.
3167c478bd9Sstevel@tonic-gate  *
3177c478bd9Sstevel@tonic-gate  */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate static void
free_nw(struct nwent * nw)3207c478bd9Sstevel@tonic-gate free_nw(struct nwent *nw) {
3217c478bd9Sstevel@tonic-gate 	char **p;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (nw == NULL)
3247c478bd9Sstevel@tonic-gate 		return;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (nw->n_name != NULL)
3277c478bd9Sstevel@tonic-gate 		free(nw->n_name);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (nw->n_aliases != NULL) {
3307c478bd9Sstevel@tonic-gate 		for (p = nw->n_aliases ; *p != NULL ; p++) {
3317c478bd9Sstevel@tonic-gate 			free(*p);
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		free(nw->n_aliases);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (nw->n_addr != NULL)
3377c478bd9Sstevel@tonic-gate 		free(nw->n_addr);
3387c478bd9Sstevel@tonic-gate }
3399525b14bSRao Shoaib 
3409525b14bSRao Shoaib /*! \file */
341