2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #if !defined(LINT) && !defined(CODECENTER)
19 static const char rcsid[] = "$Id: gen_pw.c,v 1.2.18.1 2005/04/27 05:00:57 sra Exp $";
24 #include "port_before.h"
27 static int __bind_irs_pw_unneeded;
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
40 #include <isc/memcluster.h>
43 #include "port_after.h"
51 struct irs_rule * rules;
52 struct irs_rule * rule;
53 struct __res_state * res;
54 void (*free_res)(void *);
59 static void pw_close(struct irs_pw *);
60 static struct passwd * pw_next(struct irs_pw *);
61 static struct passwd * pw_byname(struct irs_pw *, const char *);
62 static struct passwd * pw_byuid(struct irs_pw *, uid_t);
63 static void pw_rewind(struct irs_pw *);
64 static void pw_minimize(struct irs_pw *);
65 static struct __res_state * pw_res_get(struct irs_pw *);
66 static void pw_res_set(struct irs_pw *,
73 irs_gen_pw(struct irs_acc *this) {
74 struct gen_p *accpvt = (struct gen_p *)this->private;
78 if (!(pw = memget(sizeof *pw))) {
82 memset(pw, 0x5e, sizeof *pw);
83 if (!(pvt = memget(sizeof *pvt))) {
84 memput(pw, sizeof *pvt);
88 memset(pvt, 0, sizeof *pvt);
89 pvt->rules = accpvt->map_rules[irs_pw];
90 pvt->rule = pvt->rules;
94 pw->byname = pw_byname;
96 pw->rewind = pw_rewind;
97 pw->minimize = pw_minimize;
98 pw->res_get = pw_res_get;
99 pw->res_set = pw_res_set;
106 pw_close(struct irs_pw *this) {
107 struct pvt *pvt = (struct pvt *)this->private;
109 memput(pvt, sizeof *pvt);
110 memput(this, sizeof *this);
113 static struct passwd *
114 pw_next(struct irs_pw *this) {
115 struct pvt *pvt = (struct pvt *)this->private;
120 pw = pvt->rule->inst->pw;
121 rval = (*pw->next)(pw);
124 if (!(pvt->rule->flags & IRS_CONTINUE))
126 pvt->rule = pvt->rule->next;
128 pw = pvt->rule->inst->pw;
136 pw_rewind(struct irs_pw *this) {
137 struct pvt *pvt = (struct pvt *)this->private;
140 pvt->rule = pvt->rules;
142 pw = pvt->rule->inst->pw;
147 static struct passwd *
148 pw_byname(struct irs_pw *this, const char *name) {
149 struct pvt *pvt = (struct pvt *)this->private;
150 struct irs_rule *rule;
155 for (rule = pvt->rules; rule; rule = rule->next) {
157 rval = (*pw->byname)(pw, name);
158 if (rval || !(rule->flags & IRS_CONTINUE))
164 static struct passwd *
165 pw_byuid(struct irs_pw *this, uid_t uid) {
166 struct pvt *pvt = (struct pvt *)this->private;
167 struct irs_rule *rule;
172 for (rule = pvt->rules; rule; rule = rule->next) {
174 rval = (*pw->byuid)(pw, uid);
175 if (rval || !(rule->flags & IRS_CONTINUE))
182 pw_minimize(struct irs_pw *this) {
183 struct pvt *pvt = (struct pvt *)this->private;
184 struct irs_rule *rule;
186 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
187 struct irs_pw *pw = rule->inst->pw;
193 static struct __res_state *
194 pw_res_get(struct irs_pw *this) {
195 struct pvt *pvt = (struct pvt *)this->private;
198 struct __res_state *res;
199 res = (struct __res_state *)malloc(sizeof *res);
204 memset(res, 0, sizeof *res);
205 pw_res_set(this, res, free);
212 pw_res_set(struct irs_pw *this, struct __res_state *res,
213 void (*free_res)(void *)) {
214 struct pvt *pvt = (struct pvt *)this->private;
215 struct irs_rule *rule;
217 if (pvt->res && pvt->free_res) {
218 res_nclose(pvt->res);
219 (*pvt->free_res)(pvt->res);
223 pvt->free_res = free_res;
225 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
226 struct irs_pw *pw = rule->inst->pw;
229 (*pw->res_set)(pw, pvt->res, NULL);
233 #endif /* WANT_IRS_PW */