xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/irp_ng.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * 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(LINT) && !defined(CODECENTER)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: irp_ng.c,v 1.4 2006/12/07 04:46:27 marka Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /* Imports */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "port_before.h"
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <errno.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <syslog.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <irs.h>
340Sstevel@tonic-gate #include <irp.h>
350Sstevel@tonic-gate #include <isc/memcluster.h>
360Sstevel@tonic-gate #include <isc/irpmarshall.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include "irs_p.h"
390Sstevel@tonic-gate #include "irp_p.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "port_after.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /* Definitions */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate struct pvt {
460Sstevel@tonic-gate 	struct irp_p	       *girpdata;
470Sstevel@tonic-gate 	int			warned;
480Sstevel@tonic-gate };
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /* Forward */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static void		ng_rewind(struct irs_ng *, const char*);
540Sstevel@tonic-gate static void		ng_close(struct irs_ng *);
550Sstevel@tonic-gate static int		ng_next(struct irs_ng *, const char **, const char **,
560Sstevel@tonic-gate 				const char **);
570Sstevel@tonic-gate static int		ng_test(struct irs_ng *, const char *,
580Sstevel@tonic-gate 				const char *, const char *,
590Sstevel@tonic-gate 				const char *);
600Sstevel@tonic-gate static void		ng_minimize(struct irs_ng *);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /* Public */
640Sstevel@tonic-gate 
65*11038SRao.Shoaib@Sun.COM /*%
660Sstevel@tonic-gate  *	Intialize the irp netgroup module.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate struct irs_ng *
irs_irp_ng(struct irs_acc * this)710Sstevel@tonic-gate irs_irp_ng(struct irs_acc *this) {
720Sstevel@tonic-gate 	struct irs_ng *ng;
730Sstevel@tonic-gate 	struct pvt *pvt;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (!(ng = memget(sizeof *ng))) {
760Sstevel@tonic-gate 		errno = ENOMEM;
770Sstevel@tonic-gate 		return (NULL);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 	memset(ng, 0x5e, sizeof *ng);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
820Sstevel@tonic-gate 		memput(ng, sizeof *ng);
830Sstevel@tonic-gate 		errno = ENOMEM;
840Sstevel@tonic-gate 		return (NULL);
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
870Sstevel@tonic-gate 	pvt->girpdata = this->private;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	ng->private = pvt;
900Sstevel@tonic-gate 	ng->close = ng_close;
910Sstevel@tonic-gate 	ng->next = ng_next;
920Sstevel@tonic-gate 	ng->test = ng_test;
930Sstevel@tonic-gate 	ng->rewind = ng_rewind;
940Sstevel@tonic-gate 	ng->minimize = ng_minimize;
950Sstevel@tonic-gate 	return (ng);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /* Methods */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * void ng_close(struct irs_ng *this)
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static void
ng_close(struct irs_ng * this)1080Sstevel@tonic-gate ng_close(struct irs_ng *this) {
1090Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	ng_minimize(this);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1140Sstevel@tonic-gate 	memput(this, sizeof *this);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * void ng_rewind(struct irs_ng *this, const char *group)
1220Sstevel@tonic-gate  *
1230Sstevel@tonic-gate  *
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate static void
ng_rewind(struct irs_ng * this,const char * group)1270Sstevel@tonic-gate ng_rewind(struct irs_ng *this, const char *group) {
1280Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1290Sstevel@tonic-gate 	char text[256];
1300Sstevel@tonic-gate 	int code;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1330Sstevel@tonic-gate 		return;
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata,
1370Sstevel@tonic-gate 				 "setnetgrent %s", group) != 0) {
1380Sstevel@tonic-gate 		return;
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
1420Sstevel@tonic-gate 	if (code != IRPD_GETNETGR_SETOK) {
1430Sstevel@tonic-gate 		if (irp_log_errors) {
1440Sstevel@tonic-gate 			syslog(LOG_WARNING, "setnetgrent(%s) failed: %s",
1450Sstevel@tonic-gate 			       group, text);
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	return;
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  *	Get the next netgroup item from the cache.
1540Sstevel@tonic-gate  *
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate static int
ng_next(struct irs_ng * this,const char ** host,const char ** user,const char ** domain)1580Sstevel@tonic-gate ng_next(struct irs_ng *this, const char **host, const char **user,
1590Sstevel@tonic-gate         const char **domain)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1620Sstevel@tonic-gate 	int code;
1630Sstevel@tonic-gate 	char *body = NULL;
1640Sstevel@tonic-gate 	size_t bodylen;
1650Sstevel@tonic-gate 	int rval = 0;
1660Sstevel@tonic-gate 	char text[256];
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1690Sstevel@tonic-gate 		return (0);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "getnetgrent") != 0)
1730Sstevel@tonic-gate 		return (0);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (irs_irp_get_full_response(pvt->girpdata, &code,
1760Sstevel@tonic-gate 				      text, sizeof text,
1770Sstevel@tonic-gate 				      &body, &bodylen) != 0) {
1780Sstevel@tonic-gate 		return (0);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (code == IRPD_GETNETGR_OK) {
1820Sstevel@tonic-gate 		if (irp_unmarshall_ng(host, user, domain, body) == 0) {
1830Sstevel@tonic-gate 			rval = 1;
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (body != NULL) {
1880Sstevel@tonic-gate 		memput(body, bodylen);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	return (rval);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  *	Search for a match in a netgroup.
1960Sstevel@tonic-gate  *
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate static int
ng_test(struct irs_ng * this,const char * name,const char * host,const char * user,const char * domain)2000Sstevel@tonic-gate ng_test(struct irs_ng *this, const char *name,
2010Sstevel@tonic-gate 	const char *host, const char *user, const char *domain)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2040Sstevel@tonic-gate 	char *body = NULL;
2050Sstevel@tonic-gate 	size_t bodylen = 0;
2060Sstevel@tonic-gate 	int code;
2070Sstevel@tonic-gate 	char text[256];
2080Sstevel@tonic-gate 	int rval = 0;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	UNUSED(name);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2130Sstevel@tonic-gate 		return (0);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (irp_marshall_ng(host, user, domain, &body, &bodylen) != 0) {
2170Sstevel@tonic-gate 		return (0);
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "innetgr %s", body) == 0) {
2210Sstevel@tonic-gate 		code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
2220Sstevel@tonic-gate 		if (code == IRPD_GETNETGR_MATCHES) {
2230Sstevel@tonic-gate 			rval = 1;
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
227*11038SRao.Shoaib@Sun.COM 	memput(body, bodylen);
228*11038SRao.Shoaib@Sun.COM 
2290Sstevel@tonic-gate 	return (rval);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate  * void ng_minimize(struct irs_ng *this)
2370Sstevel@tonic-gate  *
2380Sstevel@tonic-gate  */
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate static void
ng_minimize(struct irs_ng * this)2410Sstevel@tonic-gate ng_minimize(struct irs_ng *this) {
2420Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	irs_irp_disconnect(pvt->girpdata);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /* Private */
2510Sstevel@tonic-gate 
252*11038SRao.Shoaib@Sun.COM 
253*11038SRao.Shoaib@Sun.COM /*! \file */
254