xref: /titanic_44/usr/src/lib/libresolv2/common/irs/irp_ng.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * 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  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: irp_ng.c,v 1.4 2006/12/07 04:46:27 marka Exp $";
207c478bd9Sstevel@tonic-gate #endif
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /* Imports */
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include "port_before.h"
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <errno.h>
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <syslog.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <irs.h>
347c478bd9Sstevel@tonic-gate #include <irp.h>
357c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
367c478bd9Sstevel@tonic-gate #include <isc/irpmarshall.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "irs_p.h"
397c478bd9Sstevel@tonic-gate #include "irp_p.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "port_after.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /* Definitions */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate struct pvt {
467c478bd9Sstevel@tonic-gate 	struct irp_p	       *girpdata;
477c478bd9Sstevel@tonic-gate 	int			warned;
487c478bd9Sstevel@tonic-gate };
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* Forward */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static void		ng_rewind(struct irs_ng *, const char*);
547c478bd9Sstevel@tonic-gate static void		ng_close(struct irs_ng *);
557c478bd9Sstevel@tonic-gate static int		ng_next(struct irs_ng *, const char **, const char **,
567c478bd9Sstevel@tonic-gate 				const char **);
577c478bd9Sstevel@tonic-gate static int		ng_test(struct irs_ng *, const char *,
587c478bd9Sstevel@tonic-gate 				const char *, const char *,
597c478bd9Sstevel@tonic-gate 				const char *);
607c478bd9Sstevel@tonic-gate static void		ng_minimize(struct irs_ng *);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* Public */
647c478bd9Sstevel@tonic-gate 
65*9525b14bSRao Shoaib /*%
667c478bd9Sstevel@tonic-gate  *	Intialize the irp netgroup module.
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate struct irs_ng *
irs_irp_ng(struct irs_acc * this)717c478bd9Sstevel@tonic-gate irs_irp_ng(struct irs_acc *this) {
727c478bd9Sstevel@tonic-gate 	struct irs_ng *ng;
737c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (!(ng = memget(sizeof *ng))) {
767c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
777c478bd9Sstevel@tonic-gate 		return (NULL);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	memset(ng, 0x5e, sizeof *ng);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
827c478bd9Sstevel@tonic-gate 		memput(ng, sizeof *ng);
837c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
847c478bd9Sstevel@tonic-gate 		return (NULL);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
877c478bd9Sstevel@tonic-gate 	pvt->girpdata = this->private;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	ng->private = pvt;
907c478bd9Sstevel@tonic-gate 	ng->close = ng_close;
917c478bd9Sstevel@tonic-gate 	ng->next = ng_next;
927c478bd9Sstevel@tonic-gate 	ng->test = ng_test;
937c478bd9Sstevel@tonic-gate 	ng->rewind = ng_rewind;
947c478bd9Sstevel@tonic-gate 	ng->minimize = ng_minimize;
957c478bd9Sstevel@tonic-gate 	return (ng);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /* Methods */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * void ng_close(struct irs_ng *this)
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static void
ng_close(struct irs_ng * this)1087c478bd9Sstevel@tonic-gate ng_close(struct irs_ng *this) {
1097c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	ng_minimize(this);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1147c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * void ng_rewind(struct irs_ng *this, const char *group)
1227c478bd9Sstevel@tonic-gate  *
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static void
ng_rewind(struct irs_ng * this,const char * group)1277c478bd9Sstevel@tonic-gate ng_rewind(struct irs_ng *this, const char *group) {
1287c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1297c478bd9Sstevel@tonic-gate 	char text[256];
1307c478bd9Sstevel@tonic-gate 	int code;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1337c478bd9Sstevel@tonic-gate 		return;
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata,
1377c478bd9Sstevel@tonic-gate 				 "setnetgrent %s", group) != 0) {
1387c478bd9Sstevel@tonic-gate 		return;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
1427c478bd9Sstevel@tonic-gate 	if (code != IRPD_GETNETGR_SETOK) {
1437c478bd9Sstevel@tonic-gate 		if (irp_log_errors) {
1447c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING, "setnetgrent(%s) failed: %s",
1457c478bd9Sstevel@tonic-gate 			       group, text);
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	return;
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  *	Get the next netgroup item from the cache.
1547c478bd9Sstevel@tonic-gate  *
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate static int
ng_next(struct irs_ng * this,const char ** host,const char ** user,const char ** domain)1587c478bd9Sstevel@tonic-gate ng_next(struct irs_ng *this, const char **host, const char **user,
1597c478bd9Sstevel@tonic-gate         const char **domain)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1627c478bd9Sstevel@tonic-gate 	int code;
1637c478bd9Sstevel@tonic-gate 	char *body = NULL;
1647c478bd9Sstevel@tonic-gate 	size_t bodylen;
1657c478bd9Sstevel@tonic-gate 	int rval = 0;
1667c478bd9Sstevel@tonic-gate 	char text[256];
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1697c478bd9Sstevel@tonic-gate 		return (0);
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "getnetgrent") != 0)
1737c478bd9Sstevel@tonic-gate 		return (0);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (irs_irp_get_full_response(pvt->girpdata, &code,
1767c478bd9Sstevel@tonic-gate 				      text, sizeof text,
1777c478bd9Sstevel@tonic-gate 				      &body, &bodylen) != 0) {
1787c478bd9Sstevel@tonic-gate 		return (0);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (code == IRPD_GETNETGR_OK) {
1827c478bd9Sstevel@tonic-gate 		if (irp_unmarshall_ng(host, user, domain, body) == 0) {
1837c478bd9Sstevel@tonic-gate 			rval = 1;
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (body != NULL) {
1887c478bd9Sstevel@tonic-gate 		memput(body, bodylen);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	return (rval);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  *	Search for a match in a netgroup.
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static int
ng_test(struct irs_ng * this,const char * name,const char * host,const char * user,const char * domain)2007c478bd9Sstevel@tonic-gate ng_test(struct irs_ng *this, const char *name,
2017c478bd9Sstevel@tonic-gate 	const char *host, const char *user, const char *domain)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2047c478bd9Sstevel@tonic-gate 	char *body = NULL;
2057c478bd9Sstevel@tonic-gate 	size_t bodylen = 0;
2067c478bd9Sstevel@tonic-gate 	int code;
2077c478bd9Sstevel@tonic-gate 	char text[256];
2087c478bd9Sstevel@tonic-gate 	int rval = 0;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	UNUSED(name);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2137c478bd9Sstevel@tonic-gate 		return (0);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (irp_marshall_ng(host, user, domain, &body, &bodylen) != 0) {
2177c478bd9Sstevel@tonic-gate 		return (0);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "innetgr %s", body) == 0) {
2217c478bd9Sstevel@tonic-gate 		code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
2227c478bd9Sstevel@tonic-gate 		if (code == IRPD_GETNETGR_MATCHES) {
2237c478bd9Sstevel@tonic-gate 			rval = 1;
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
227*9525b14bSRao Shoaib 	memput(body, bodylen);
228*9525b14bSRao Shoaib 
2297c478bd9Sstevel@tonic-gate 	return (rval);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate  * void ng_minimize(struct irs_ng *this)
2377c478bd9Sstevel@tonic-gate  *
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate static void
ng_minimize(struct irs_ng * this)2417c478bd9Sstevel@tonic-gate ng_minimize(struct irs_ng *this) {
2427c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	irs_irp_disconnect(pvt->girpdata);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /* Private */
2517c478bd9Sstevel@tonic-gate 
252*9525b14bSRao Shoaib 
253*9525b14bSRao Shoaib /*! \file */
254