17c478bd9Sstevel@tonic-gate /*
29525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1996,1999 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 <sys/types.h>
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include <netinet/in.h>
257c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
267c478bd9Sstevel@tonic-gate #include <resolv.h>
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <errno.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
337c478bd9Sstevel@tonic-gate #include <irs.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "port_after.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "irs_p.h"
387c478bd9Sstevel@tonic-gate #include "gen_p.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /* Types */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate struct pvt {
437c478bd9Sstevel@tonic-gate 	struct irs_rule *	rules;
447c478bd9Sstevel@tonic-gate 	struct irs_rule *	rule;
457c478bd9Sstevel@tonic-gate 	char *			curgroup;
467c478bd9Sstevel@tonic-gate };
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* Forward */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static void		ng_close(struct irs_ng *);
517c478bd9Sstevel@tonic-gate static int		ng_next(struct irs_ng *, const char **,
527c478bd9Sstevel@tonic-gate 				const char **, const char **);
537c478bd9Sstevel@tonic-gate static int 		ng_test(struct irs_ng *, const char *,
547c478bd9Sstevel@tonic-gate 				const char *, const char *,
557c478bd9Sstevel@tonic-gate 				const char *);
567c478bd9Sstevel@tonic-gate static void 		ng_rewind(struct irs_ng *, const char *);
577c478bd9Sstevel@tonic-gate static void		ng_minimize(struct irs_ng *);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* Public */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate struct irs_ng *
irs_gen_ng(struct irs_acc * this)627c478bd9Sstevel@tonic-gate irs_gen_ng(struct irs_acc *this) {
637c478bd9Sstevel@tonic-gate 	struct gen_p *accpvt = (struct gen_p *)this->private;
647c478bd9Sstevel@tonic-gate 	struct irs_ng *ng;
657c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
66*55fea89dSDan Cross 
677c478bd9Sstevel@tonic-gate 	if (!(ng = memget(sizeof *ng))) {
687c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
697c478bd9Sstevel@tonic-gate 		return (NULL);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 	memset(ng, 0x5e, sizeof *ng);
727c478bd9Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
737c478bd9Sstevel@tonic-gate 		memput(ng, sizeof *ng);
747c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
757c478bd9Sstevel@tonic-gate 		return (NULL);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
787c478bd9Sstevel@tonic-gate 	pvt->rules = accpvt->map_rules[irs_ng];
797c478bd9Sstevel@tonic-gate 	pvt->rule = pvt->rules;
807c478bd9Sstevel@tonic-gate 	ng->private = pvt;
817c478bd9Sstevel@tonic-gate 	ng->close = ng_close;
827c478bd9Sstevel@tonic-gate 	ng->next = ng_next;
837c478bd9Sstevel@tonic-gate 	ng->test = ng_test;
847c478bd9Sstevel@tonic-gate 	ng->rewind = ng_rewind;
857c478bd9Sstevel@tonic-gate 	ng->minimize = ng_minimize;
867c478bd9Sstevel@tonic-gate 	return (ng);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /* Methods */
907c478bd9Sstevel@tonic-gate 
91*55fea89dSDan Cross static void
ng_close(struct irs_ng * this)927c478bd9Sstevel@tonic-gate ng_close(struct irs_ng *this) {
937c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
94*55fea89dSDan Cross 
957c478bd9Sstevel@tonic-gate 	ng_minimize(this);
967c478bd9Sstevel@tonic-gate 	if (pvt->curgroup)
977c478bd9Sstevel@tonic-gate 		free(pvt->curgroup);
987c478bd9Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
997c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static int
ng_next(struct irs_ng * this,const char ** host,const char ** user,const char ** domain)1037c478bd9Sstevel@tonic-gate ng_next(struct irs_ng *this, const char **host, const char **user,
1047c478bd9Sstevel@tonic-gate 	const char **domain)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1077c478bd9Sstevel@tonic-gate 	struct irs_ng *ng;
108*55fea89dSDan Cross 
1097c478bd9Sstevel@tonic-gate 	while (pvt->rule) {
1107c478bd9Sstevel@tonic-gate 		ng = pvt->rule->inst->ng;
1117c478bd9Sstevel@tonic-gate 		if ((*ng->next)(ng, host, user, domain) == 1)
1127c478bd9Sstevel@tonic-gate 			return (1);
1137c478bd9Sstevel@tonic-gate 		if (!(pvt->rule->flags & IRS_CONTINUE))
1147c478bd9Sstevel@tonic-gate 			break;
1157c478bd9Sstevel@tonic-gate 		pvt->rule = pvt->rule->next;
1167c478bd9Sstevel@tonic-gate 		if (pvt->rule) {
1177c478bd9Sstevel@tonic-gate 			ng = pvt->rule->inst->ng;
1187c478bd9Sstevel@tonic-gate 			(*ng->rewind)(ng, pvt->curgroup);
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	return (0);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static int
ng_test(struct irs_ng * this,const char * name,const char * user,const char * host,const char * domain)1257c478bd9Sstevel@tonic-gate ng_test(struct irs_ng *this, const char *name,
1267c478bd9Sstevel@tonic-gate 	const char *user, const char *host, const char *domain)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1297c478bd9Sstevel@tonic-gate 	struct irs_rule *rule;
1307c478bd9Sstevel@tonic-gate 	struct irs_ng *ng;
1317c478bd9Sstevel@tonic-gate 	int rval;
132*55fea89dSDan Cross 
1337c478bd9Sstevel@tonic-gate 	rval = 0;
1347c478bd9Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
1357c478bd9Sstevel@tonic-gate 		ng = rule->inst->ng;
1367c478bd9Sstevel@tonic-gate 		rval = (*ng->test)(ng, name, user, host, domain);
1377c478bd9Sstevel@tonic-gate 		if (rval || !(rule->flags & IRS_CONTINUE))
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	return (rval);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static void
ng_rewind(struct irs_ng * this,const char * group)1447c478bd9Sstevel@tonic-gate ng_rewind(struct irs_ng *this, const char *group) {
1457c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1467c478bd9Sstevel@tonic-gate 	struct irs_ng *ng;
147*55fea89dSDan Cross 
1487c478bd9Sstevel@tonic-gate 	pvt->rule = pvt->rules;
1497c478bd9Sstevel@tonic-gate 	if (pvt->rule) {
1507c478bd9Sstevel@tonic-gate 		if (pvt->curgroup)
1517c478bd9Sstevel@tonic-gate 			free(pvt->curgroup);
1527c478bd9Sstevel@tonic-gate 		pvt->curgroup = strdup(group);
1537c478bd9Sstevel@tonic-gate 		ng = pvt->rule->inst->ng;
1547c478bd9Sstevel@tonic-gate 		(*ng->rewind)(ng, pvt->curgroup);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static void
ng_minimize(struct irs_ng * this)1597c478bd9Sstevel@tonic-gate ng_minimize(struct irs_ng *this) {
1607c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1617c478bd9Sstevel@tonic-gate 	struct irs_rule *rule;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
1647c478bd9Sstevel@tonic-gate 		struct irs_ng *ng = rule->inst->ng;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		(*ng->minimize)(ng);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate }
1699525b14bSRao Shoaib 
1709525b14bSRao Shoaib /*! \file */
171