gen.c revision ab023a65562e62b85a824509d829b6fad87e00b1
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews/*
990d0e893f5b70e735cdf990af66e9ec6e91fa78Tinderbox User * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * Copyright (c) 1996-1999 by Internet Software Consortium.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews *
4a14ce5ba00ab7bc55c99ffdcf59c7a4ab902721Automatic Updater * Permission to use, copy, modify, and distribute this software for any
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * purpose with or without fee is hereby granted, provided that the above
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * copyright notice and this permission notice appear in all copies.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews *
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews */
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#if !defined(LINT) && !defined(CODECENTER)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsstatic const char rcsid[] = "$Id: gen.c,v 1.7 2005/04/27 04:56:23 sra Exp $";
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#endif
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
e21a2904f02a03fa06b6db04d348f65fe9c67b2bMark Andrews/*! \file
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * \brief
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * this is the top level dispatcher
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater *
c3dc968140ab7f04795acc7835e4e89ccb0c0a27Tinderbox User * The dispatcher is implemented as an accessor class; it is an
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * accessor class that calls other accessor classes, as controlled by a
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * configuration file.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews *
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * A big difference between this accessor class and others is that the
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * map class initializers are NULL, and the map classes are already
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * filled in with method functions that will do the right thing.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews */
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews/* Imports */
c3dc968140ab7f04795acc7835e4e89ccb0c0a27Tinderbox User
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include "port_before.h"
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <isc/assertions.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <ctype.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <errno.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <stdio.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <stdlib.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <string.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <sys/types.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <netinet/in.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <arpa/nameser.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <resolv.h>
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <isc/memcluster.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <irs.h>
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include "port_after.h"
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include "irs_p.h"
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include "gen_p.h"
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews/* Definitions */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsstruct nameval {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews const char * name;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews int val;
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User};
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsstatic const struct nameval acc_names[irs_nacc+1] = {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews { "local", irs_lcl },
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews { "dns", irs_dns },
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews { "nis", irs_nis },
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews { "irp", irs_irp },
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews { NULL, irs_nacc }
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews};
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updatertypedef struct irs_acc *(*accinit) __P((const char *options));
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updaterstatic const accinit accs[irs_nacc+1] = {
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater irs_lcl_acc,
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater irs_dns_acc,
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater#ifdef WANT_IRS_NIS
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater irs_nis_acc,
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater#else
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater NULL,
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater#endif
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater irs_irp_acc,
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater NULL
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater};
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
be6c1c506161e6f45fcff5d0425f78801bc267c1Automatic Updaterstatic const struct nameval map_names[irs_nmap+1] = {
be6c1c506161e6f45fcff5d0425f78801bc267c1Automatic Updater { "group", irs_gr },
be6c1c506161e6f45fcff5d0425f78801bc267c1Automatic Updater { "passwd", irs_pw },
be6c1c506161e6f45fcff5d0425f78801bc267c1Automatic Updater { "services", irs_sv },
be6c1c506161e6f45fcff5d0425f78801bc267c1Automatic Updater { "protocols", irs_pr },
be6c1c506161e6f45fcff5d0425f78801bc267c1Automatic Updater { "hosts", irs_ho },
be6c1c506161e6f45fcff5d0425f78801bc267c1Automatic Updater { "networks", irs_nw },
be6c1c506161e6f45fcff5d0425f78801bc267c1Automatic Updater { "netgroup", irs_ng },
dba3c818ae00b10388d31703e86a28415db398acTinderbox User { NULL, irs_nmap }
43b94483957d3168796a816ed86cf097518817dcTinderbox User};
be6c1c506161e6f45fcff5d0425f78801bc267c1Automatic Updater
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updaterstatic const struct nameval option_names[] = {
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox User { "merge", IRS_MERGE },
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox User { "continue", IRS_CONTINUE },
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox User { NULL, 0 }
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox User};
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox User
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox User/* Forward */
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox User
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox Userstatic void gen_close(struct irs_acc *);
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox Userstatic struct __res_state * gen_res_get(struct irs_acc *);
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox Userstatic void gen_res_set(struct irs_acc *, struct __res_state *,
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox User void (*)(void *));
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox Userstatic int find_name(const char *, const struct nameval nv[]);
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox Userstatic void init_map_rules(struct gen_p *, const char *conf_file);
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox Userstatic struct irs_rule *release_rule(struct irs_rule *);
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox Userstatic int add_rule(struct gen_p *,
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater enum irs_map_id, enum irs_acc_id,
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater const char *);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater/* Public */
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updaterstruct irs_acc *
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updaterirs_gen_acc(const char *options, const char *conf_file) {
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater struct irs_acc *acc;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater struct gen_p *irs;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater if (!(acc = memget(sizeof *acc))) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews errno = ENOMEM;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return (NULL);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews }
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews memset(acc, 0x5e, sizeof *acc);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (!(irs = memget(sizeof *irs))) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews errno = ENOMEM;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews memput(acc, sizeof *acc);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return (NULL);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews }
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews memset(irs, 0x5e, sizeof *irs);
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User irs->options = strdup(options);
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User irs->res = NULL;
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User irs->free_res = NULL;
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User memset(irs->accessors, 0, sizeof irs->accessors);
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User memset(irs->map_rules, 0, sizeof irs->map_rules);
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User init_map_rules(irs, conf_file);
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User acc->private = irs;
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User#ifdef WANT_IRS_GR
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User acc->gr_map = irs_gen_gr;
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User#else
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User acc->gr_map = NULL;
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User#endif
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User#ifdef WANT_IRS_PW
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User acc->pw_map = irs_gen_pw;
0e1dece22e128f9dfa723316a35c4b3f06912381Tinderbox User#else
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews acc->pw_map = NULL;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#endif
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews acc->sv_map = irs_gen_sv;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews acc->pr_map = irs_gen_pr;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews acc->ho_map = irs_gen_ho;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews acc->nw_map = irs_gen_nw;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews acc->ng_map = irs_gen_ng;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews acc->res_get = gen_res_get;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews acc->res_set = gen_res_set;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews acc->close = gen_close;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return (acc);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews}
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews/* Methods */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsstatic struct __res_state *
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsgen_res_get(struct irs_acc *this) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews struct gen_p *irs = (struct gen_p *)this->private;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (irs->res == NULL) {
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater struct __res_state *res;
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater res = (struct __res_state *)malloc(sizeof *res);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (res == NULL)
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater return (NULL);
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater memset(res, 0, sizeof *res);
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater gen_res_set(this, res, free);
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater }
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater if (((irs->res->options & RES_INIT) == 0U) && res_ninit(irs->res) < 0)
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater return (NULL);
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater return (irs->res);
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater}
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater
60a900e83bab52c3f023be66654f3ab023172778Automatic Updaterstatic void
60a900e83bab52c3f023be66654f3ab023172778Automatic Updatergen_res_set(struct irs_acc *this, struct __res_state *res,
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater void (*free_res)(void *)) {
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater struct gen_p *irs = (struct gen_p *)this->private;
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater#if 0
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater struct irs_rule *rule;
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater struct irs_ho *ho;
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater struct irs_nw *nw;
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater#endif
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater
60a900e83bab52c3f023be66654f3ab023172778Automatic Updater if (irs->res && irs->free_res) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews res_nclose(irs->res);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews (*irs->free_res)(irs->res);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews }
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
2628293c6edaa41ed1407c42bb196083901e087bAutomatic Updater irs->res = res;
2628293c6edaa41ed1407c42bb196083901e087bAutomatic Updater irs->free_res = free_res;
2628293c6edaa41ed1407c42bb196083901e087bAutomatic Updater
2628293c6edaa41ed1407c42bb196083901e087bAutomatic Updater#if 0
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews for (rule = irs->map_rules[irs_ho]; rule; rule = rule->next) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews ho = rule->inst->ho;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews (*ho->res_set)(ho, res, NULL);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews }
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews for (rule = irs->map_rules[irs_nw]; rule; rule = rule->next) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews nw = rule->inst->nw;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews (*nw->res_set)(nw, res, NULL);
d71e2e0c61df16ff37c9934c371a4a60c08974f7Mark Andrews }
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#endif
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews}
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsstatic void
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsgen_close(struct irs_acc *this) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews struct gen_p *irs = (struct gen_p *)this->private;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews int n;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews /* Search rules. */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews for (n = 0; n < irs_nmap; n++)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews while (irs->map_rules[n] != NULL)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews irs->map_rules[n] = release_rule(irs->map_rules[n]);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews /* Access methods. */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews for (n = 0; n < irs_nacc; n++) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews /* Map objects. */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (irs->accessors[n].gr != NULL)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews (*irs->accessors[n].gr->close)(irs->accessors[n].gr);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (irs->accessors[n].pw != NULL)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews (*irs->accessors[n].pw->close)(irs->accessors[n].pw);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (irs->accessors[n].sv != NULL)
dba3c818ae00b10388d31703e86a28415db398acTinderbox User (*irs->accessors[n].sv->close)(irs->accessors[n].sv);
43b94483957d3168796a816ed86cf097518817dcTinderbox User if (irs->accessors[n].pr != NULL)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews (*irs->accessors[n].pr->close)(irs->accessors[n].pr);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (irs->accessors[n].ho != NULL)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews (*irs->accessors[n].ho->close)(irs->accessors[n].ho);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (irs->accessors[n].nw != NULL)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews (*irs->accessors[n].nw->close)(irs->accessors[n].nw);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (irs->accessors[n].ng != NULL)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews (*irs->accessors[n].ng->close)(irs->accessors[n].ng);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews /* Enclosing accessor. */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (irs->accessors[n].acc != NULL)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews (*irs->accessors[n].acc->close)(irs->accessors[n].acc);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews }
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
d71e2e0c61df16ff37c9934c371a4a60c08974f7Mark Andrews /* The options string was strdup'd. */
d71e2e0c61df16ff37c9934c371a4a60c08974f7Mark Andrews free((void*)irs->options);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (irs->res && irs->free_res)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews (*irs->free_res)(irs->res);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews /* The private data container. */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews memput(irs, sizeof *irs);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews /* The object. */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews memput(this, sizeof *this);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews}
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews/* Private */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
97e74139b19368e385a3564746d42db70879195eAutomatic Updaterstatic int
97e74139b19368e385a3564746d42db70879195eAutomatic Updaterfind_name(const char *name, const struct nameval names[]) {
43b94483957d3168796a816ed86cf097518817dcTinderbox User int n;
dba3c818ae00b10388d31703e86a28415db398acTinderbox User
dba3c818ae00b10388d31703e86a28415db398acTinderbox User for (n = 0; names[n].name != NULL; n++)
dba3c818ae00b10388d31703e86a28415db398acTinderbox User if (strcmp(name, names[n].name) == 0)
97e74139b19368e385a3564746d42db70879195eAutomatic Updater return (names[n].val);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return (-1);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews}
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsstatic struct irs_rule *
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsrelease_rule(struct irs_rule *rule) {
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews struct irs_rule *next = rule->next;
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews memput(rule, sizeof *rule);
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews return (next);
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews}
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrewsstatic int
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrewsadd_rule(struct gen_p *irs,
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews enum irs_map_id map, enum irs_acc_id acc,
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews const char *options)
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews{
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews struct irs_rule **rules, *last, *tmp, *new;
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews struct irs_inst *inst;
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews const char *cp;
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews int n;
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews#ifndef WANT_IRS_GR
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews if (map == irs_gr)
52ece689e0265f9a3e518de5b2539e749f6d35acMark Andrews return (-1);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#endif
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#ifndef WANT_IRS_PW
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (map == irs_pw)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return (-1);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#endif
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#ifndef WANT_IRS_NIS
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (acc == irs_nis)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return (-1);
dba3c818ae00b10388d31703e86a28415db398acTinderbox User#endif
dba3c818ae00b10388d31703e86a28415db398acTinderbox User new = memget(sizeof *new);
72938578c985138165e7a4b0a38f16daacbad95eAutomatic Updater if (new == NULL)
dba3c818ae00b10388d31703e86a28415db398acTinderbox User return (-1);
43b94483957d3168796a816ed86cf097518817dcTinderbox User memset(new, 0x5e, sizeof *new);
dba3c818ae00b10388d31703e86a28415db398acTinderbox User new->next = NULL;
dba3c818ae00b10388d31703e86a28415db398acTinderbox User
dba3c818ae00b10388d31703e86a28415db398acTinderbox User new->inst = &irs->accessors[acc];
dba3c818ae00b10388d31703e86a28415db398acTinderbox User
dba3c818ae00b10388d31703e86a28415db398acTinderbox User new->flags = 0;
dba3c818ae00b10388d31703e86a28415db398acTinderbox User cp = options;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews while (cp && *cp) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews char option[50], *next;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews next = strchr(cp, ',');
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (next)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews n = next++ - cp;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews else
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews n = strlen(cp);
6283056805887de88040698685b8e1936a1f7a2dAutomatic Updater if ((size_t)n > sizeof option - 1)
6283056805887de88040698685b8e1936a1f7a2dAutomatic Updater n = sizeof option - 1;
6283056805887de88040698685b8e1936a1f7a2dAutomatic Updater strncpy(option, cp, n);
6283056805887de88040698685b8e1936a1f7a2dAutomatic Updater option[n] = '\0';
6283056805887de88040698685b8e1936a1f7a2dAutomatic Updater
6283056805887de88040698685b8e1936a1f7a2dAutomatic Updater n = find_name(option, option_names);
6283056805887de88040698685b8e1936a1f7a2dAutomatic Updater if (n >= 0)
1c51f79aba598e5e20bde66aea0237e347f6d5ceAutomatic Updater new->flags |= n;
1c51f79aba598e5e20bde66aea0237e347f6d5ceAutomatic Updater
6283056805887de88040698685b8e1936a1f7a2dAutomatic Updater cp = next;
1c51f79aba598e5e20bde66aea0237e347f6d5ceAutomatic Updater }
6283056805887de88040698685b8e1936a1f7a2dAutomatic Updater
6283056805887de88040698685b8e1936a1f7a2dAutomatic Updater rules = &irs->map_rules[map];
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User for (last = NULL, tmp = *rules;
7717ec7a6a898cdd3c35cbfba66010b7304ffd9bAutomatic Updater tmp != NULL;
7717ec7a6a898cdd3c35cbfba66010b7304ffd9bAutomatic Updater last = tmp, tmp = tmp->next)
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User (void)NULL;
7717ec7a6a898cdd3c35cbfba66010b7304ffd9bAutomatic Updater if (last == NULL)
7717ec7a6a898cdd3c35cbfba66010b7304ffd9bAutomatic Updater *rules = new;
7717ec7a6a898cdd3c35cbfba66010b7304ffd9bAutomatic Updater else
7717ec7a6a898cdd3c35cbfba66010b7304ffd9bAutomatic Updater last->next = new;
7717ec7a6a898cdd3c35cbfba66010b7304ffd9bAutomatic Updater
7717ec7a6a898cdd3c35cbfba66010b7304ffd9bAutomatic Updater /* Try to instantiate map accessors for this if necessary & approp. */
7717ec7a6a898cdd3c35cbfba66010b7304ffd9bAutomatic Updater inst = &irs->accessors[acc];
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User if (inst->acc == NULL && accs[acc] != NULL)
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User inst->acc = (*accs[acc])(irs->options);
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User if (inst->acc != NULL) {
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User if (inst->gr == NULL && inst->acc->gr_map != NULL)
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User inst->gr = (*inst->acc->gr_map)(inst->acc);
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User if (inst->pw == NULL && inst->acc->pw_map != NULL)
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User inst->pw = (*inst->acc->pw_map)(inst->acc);
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User if (inst->sv == NULL && inst->acc->sv_map != NULL)
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User inst->sv = (*inst->acc->sv_map)(inst->acc);
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User if (inst->pr == NULL && inst->acc->pr_map != NULL)
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User inst->pr = (*inst->acc->pr_map)(inst->acc);
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User if (inst->ho == NULL && inst->acc->ho_map != NULL)
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User inst->ho = (*inst->acc->ho_map)(inst->acc);
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User if (inst->nw == NULL && inst->acc->nw_map != NULL)
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User inst->nw = (*inst->acc->nw_map)(inst->acc);
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User if (inst->ng == NULL && inst->acc->ng_map != NULL)
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User inst->ng = (*inst->acc->ng_map)(inst->acc);
3f9791eac42b243d9f328d604e117f91a3f67a37Tinderbox User }
7717ec7a6a898cdd3c35cbfba66010b7304ffd9bAutomatic Updater
7717ec7a6a898cdd3c35cbfba66010b7304ffd9bAutomatic Updater return (0);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews}
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsstatic void
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsdefault_map_rules(struct gen_p *irs) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews /* Install time honoured and proved BSD style rules as default. */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews add_rule(irs, irs_gr, irs_lcl, "");
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews add_rule(irs, irs_pw, irs_lcl, "");
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews add_rule(irs, irs_sv, irs_lcl, "");
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews add_rule(irs, irs_pr, irs_lcl, "");
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews add_rule(irs, irs_ho, irs_dns, "continue");
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews add_rule(irs, irs_ho, irs_lcl, "");
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews add_rule(irs, irs_nw, irs_dns, "continue");
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater add_rule(irs, irs_nw, irs_lcl, "");
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater add_rule(irs, irs_ng, irs_lcl, "");
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater}
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updaterstatic void
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updaterinit_map_rules(struct gen_p *irs, const char *conf_file) {
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater char line[1024], pattern[40], mapname[20], accname[20], options[100];
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater FILE *conf;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater if (conf_file == NULL)
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater conf_file = _PATH_IRS_CONF ;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater /* A conf file of "" means compiled in defaults. Irpd wants this */
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater if (conf_file[0] == '\0' || (conf = fopen(conf_file, "r")) == NULL) {
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater default_map_rules(irs);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater return;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater }
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater (void) sprintf(pattern, "%%%lus %%%lus %%%lus\n",
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater (unsigned long)sizeof mapname,
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater (unsigned long)sizeof accname,
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater (unsigned long)sizeof options);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater while (fgets(line, sizeof line, conf)) {
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater enum irs_map_id map;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater enum irs_acc_id acc;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater char *tmp;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater int n;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater for (tmp = line;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater isascii((unsigned char)*tmp) &&
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater isspace((unsigned char)*tmp);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater tmp++)
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater (void)NULL;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater if (*tmp == '#' || *tmp == '\n' || *tmp == '\0')
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater continue;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater n = sscanf(tmp, pattern, mapname, accname, options);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater if (n < 2)
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater continue;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater if (n < 3)
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater options[0] = '\0';
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater n = find_name(mapname, map_names);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater INSIST(n < irs_nmap);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater if (n < 0)
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater continue;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater map = (enum irs_map_id) n;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater n = find_name(accname, acc_names);
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater INSIST(n < irs_nacc);
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater if (n < 0)
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater continue;
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater acc = (enum irs_acc_id) n;
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater add_rule(irs, map, acc, options);
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater }
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater fclose(conf);
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater}
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater