4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if !defined(LINT) && !defined(CODECENTER)
21 static const char rcsid
[] = "Id: gen_pw.c,v 1.3 2005/04/27 04:56:24 sra Exp";
26 #include "port_before.h"
29 static int __bind_irs_pw_unneeded
;
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
42 #include <isc/memcluster.h>
45 #include "port_after.h"
53 struct irs_rule
* rules
;
54 struct irs_rule
* rule
;
55 struct __res_state
* res
;
56 void (*free_res
)(void *);
61 static void pw_close(struct irs_pw
*);
62 static struct passwd
* pw_next(struct irs_pw
*);
63 static struct passwd
* pw_byname(struct irs_pw
*, const char *);
64 static struct passwd
* pw_byuid(struct irs_pw
*, uid_t
);
65 static void pw_rewind(struct irs_pw
*);
66 static void pw_minimize(struct irs_pw
*);
67 static struct __res_state
* pw_res_get(struct irs_pw
*);
68 static void pw_res_set(struct irs_pw
*,
75 irs_gen_pw(struct irs_acc
*this) {
76 struct gen_p
*accpvt
= (struct gen_p
*)this->private;
80 if (!(pw
= memget(sizeof *pw
))) {
84 memset(pw
, 0x5e, sizeof *pw
);
85 if (!(pvt
= memget(sizeof *pvt
))) {
86 memput(pw
, sizeof *pvt
);
90 memset(pvt
, 0, sizeof *pvt
);
91 pvt
->rules
= accpvt
->map_rules
[irs_pw
];
92 pvt
->rule
= pvt
->rules
;
96 pw
->byname
= pw_byname
;
98 pw
->rewind
= pw_rewind
;
99 pw
->minimize
= pw_minimize
;
100 pw
->res_get
= pw_res_get
;
101 pw
->res_set
= pw_res_set
;
108 pw_close(struct irs_pw
*this) {
109 struct pvt
*pvt
= (struct pvt
*)this->private;
111 memput(pvt
, sizeof *pvt
);
112 memput(this, sizeof *this);
115 static struct passwd
*
116 pw_next(struct irs_pw
*this) {
117 struct pvt
*pvt
= (struct pvt
*)this->private;
122 pw
= pvt
->rule
->inst
->pw
;
123 rval
= (*pw
->next
)(pw
);
126 if (!(pvt
->rule
->flags
& IRS_CONTINUE
))
128 pvt
->rule
= pvt
->rule
->next
;
130 pw
= pvt
->rule
->inst
->pw
;
138 pw_rewind(struct irs_pw
*this) {
139 struct pvt
*pvt
= (struct pvt
*)this->private;
142 pvt
->rule
= pvt
->rules
;
144 pw
= pvt
->rule
->inst
->pw
;
149 static struct passwd
*
150 pw_byname(struct irs_pw
*this, const char *name
) {
151 struct pvt
*pvt
= (struct pvt
*)this->private;
152 struct irs_rule
*rule
;
157 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
159 rval
= (*pw
->byname
)(pw
, name
);
160 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
166 static struct passwd
*
167 pw_byuid(struct irs_pw
*this, uid_t uid
) {
168 struct pvt
*pvt
= (struct pvt
*)this->private;
169 struct irs_rule
*rule
;
174 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
176 rval
= (*pw
->byuid
)(pw
, uid
);
177 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
184 pw_minimize(struct irs_pw
*this) {
185 struct pvt
*pvt
= (struct pvt
*)this->private;
186 struct irs_rule
*rule
;
188 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
189 struct irs_pw
*pw
= rule
->inst
->pw
;
195 static struct __res_state
*
196 pw_res_get(struct irs_pw
*this) {
197 struct pvt
*pvt
= (struct pvt
*)this->private;
200 struct __res_state
*res
;
201 res
= (struct __res_state
*)malloc(sizeof *res
);
206 memset(res
, 0, sizeof *res
);
207 pw_res_set(this, res
, free
);
214 pw_res_set(struct irs_pw
*this, struct __res_state
*res
,
215 void (*free_res
)(void *)) {
216 struct pvt
*pvt
= (struct pvt
*)this->private;
217 struct irs_rule
*rule
;
219 if (pvt
->res
&& pvt
->free_res
) {
220 res_nclose(pvt
->res
);
221 (*pvt
->free_res
)(pvt
->res
);
225 pvt
->free_res
= free_res
;
227 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
228 struct irs_pw
*pw
= rule
->inst
->pw
;
231 (*pw
->res_set
)(pw
, pvt
->res
, NULL
);
235 #endif /* WANT_IRS_PW */