Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / gen_pw.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996,1999 by Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 #if !defined(LINT) && !defined(CODECENTER)
19 static const char rcsid[] = "$Id: gen_pw.c,v 1.1.2.1 2004/03/09 09:17:29 marka Exp $";
20 #endif
21
22 /* Imports */
23
24 #include "port_before.h"
25
26 #ifndef WANT_IRS_PW
27 static int __bind_irs_pw_unneeded;
28 #else
29
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <resolv.h>
34
35 #include <errno.h>
36 #include <pwd.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include <isc/memcluster.h>
41 #include <irs.h>
42
43 #include "port_after.h"
44
45 #include "irs_p.h"
46 #include "gen_p.h"
47
48 /* Types */
49
50 struct pvt {
51         struct irs_rule *       rules;
52         struct irs_rule *       rule;
53         struct __res_state *    res;
54         void                    (*free_res)(void *);
55 };
56
57 /* Forward */
58
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 *,
67                                            struct __res_state *,
68                                            void (*)(void *));
69
70 /* Public */
71
72 struct irs_pw *
73 irs_gen_pw(struct irs_acc *this) {
74         struct gen_p *accpvt = (struct gen_p *)this->private;
75         struct irs_pw *pw;
76         struct pvt *pvt;
77
78         if (!(pw = memget(sizeof *pw))) {
79                 errno = ENOMEM;
80                 return (NULL);
81         }
82         memset(pw, 0x5e, sizeof *pw);
83         if (!(pvt = memget(sizeof *pvt))) {
84                 memput(pw, sizeof *pvt);
85                 errno = ENOMEM;
86                 return (NULL);
87         }
88         memset(pvt, 0, sizeof *pvt);
89         pvt->rules = accpvt->map_rules[irs_pw];
90         pvt->rule = pvt->rules;
91         pw->private = pvt;
92         pw->close = pw_close;
93         pw->next = pw_next;
94         pw->byname = pw_byname;
95         pw->byuid = pw_byuid;
96         pw->rewind = pw_rewind;
97         pw->minimize = pw_minimize;
98         pw->res_get = pw_res_get;
99         pw->res_set = pw_res_set;
100         return (pw);
101 }
102
103 /* Methods */
104
105 static void
106 pw_close(struct irs_pw *this) {
107         struct pvt *pvt = (struct pvt *)this->private;
108         
109         memput(pvt, sizeof *pvt);
110         memput(this, sizeof *this);
111 }
112
113 static struct passwd *
114 pw_next(struct irs_pw *this) {
115         struct pvt *pvt = (struct pvt *)this->private;
116         struct passwd *rval;
117         struct irs_pw *pw;
118         
119         while (pvt->rule) {
120                 pw = pvt->rule->inst->pw;
121                 rval = (*pw->next)(pw);
122                 if (rval)
123                         return (rval);
124                 if (!(pvt->rule->flags & IRS_CONTINUE))
125                         break;
126                 pvt->rule = pvt->rule->next;
127                 if (pvt->rule) {
128                         pw = pvt->rule->inst->pw;
129                         (*pw->rewind)(pw);
130                 }
131         }
132         return (NULL);
133 }
134
135 static void
136 pw_rewind(struct irs_pw *this) {
137         struct pvt *pvt = (struct pvt *)this->private;
138         struct irs_pw *pw;
139         
140         pvt->rule = pvt->rules;
141         if (pvt->rule) {
142                 pw = pvt->rule->inst->pw;
143                 (*pw->rewind)(pw);
144         }
145 }
146
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;
151         struct passwd *rval;
152         struct irs_pw *pw;
153         
154         rval = NULL;
155         for (rule = pvt->rules; rule; rule = rule->next) {
156                 pw = rule->inst->pw;
157                 rval = (*pw->byname)(pw, name);
158                 if (rval || !(rule->flags & IRS_CONTINUE))
159                         break;
160         }
161         return (rval);
162 }
163
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;
168         struct passwd *rval;
169         struct irs_pw *pw;
170         
171         rval = NULL;
172         for (rule = pvt->rules; rule; rule = rule->next) {
173                 pw = rule->inst->pw;
174                 rval = (*pw->byuid)(pw, uid);
175                 if (rval || !(rule->flags & IRS_CONTINUE))
176                         break;
177         }
178         return (rval);
179 }       
180
181 static void
182 pw_minimize(struct irs_pw *this) {
183         struct pvt *pvt = (struct pvt *)this->private;
184         struct irs_rule *rule;
185
186         for (rule = pvt->rules; rule != NULL; rule = rule->next) {
187                 struct irs_pw *pw = rule->inst->pw;
188
189                 (*pw->minimize)(pw);
190         }
191 }
192
193 static struct __res_state *
194 pw_res_get(struct irs_pw *this) {
195         struct pvt *pvt = (struct pvt *)this->private;
196
197         if (!pvt->res) {
198                 struct __res_state *res;
199                 res = (struct __res_state *)malloc(sizeof *res);
200                 if (!res) {
201                         errno = ENOMEM;
202                         return (NULL);
203                 }
204                 memset(res, 0, sizeof *res);
205                 pw_res_set(this, res, free);
206         }
207
208         return (pvt->res);
209 }
210
211 static void
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;
216
217         if (pvt->res && pvt->free_res) {
218                 res_nclose(pvt->res);
219                 (*pvt->free_res)(pvt->res);
220         }
221
222         pvt->res = res;
223         pvt->free_res = free_res;
224
225         for (rule = pvt->rules; rule != NULL; rule = rule->next) {
226                 struct irs_pw *pw = rule->inst->pw;
227
228                 if (pw->res_set)
229                         (*pw->res_set)(pw, pvt->res, NULL);
230         }
231 }
232
233 #endif /* WANT_IRS_PW */