gen_pw.c revision 1.1.1.1.2.2 1 1.1.1.1.2.2 jym /* $NetBSD: gen_pw.c,v 1.1.1.1.2.2 2009/05/13 18:52:10 jym Exp $ */
2 1.1.1.1.2.2 jym
3 1.1.1.1.2.2 jym /*
4 1.1.1.1.2.2 jym * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1.1.1.2.2 jym * Copyright (c) 1996,1999 by Internet Software Consortium.
6 1.1.1.1.2.2 jym *
7 1.1.1.1.2.2 jym * Permission to use, copy, modify, and distribute this software for any
8 1.1.1.1.2.2 jym * purpose with or without fee is hereby granted, provided that the above
9 1.1.1.1.2.2 jym * copyright notice and this permission notice appear in all copies.
10 1.1.1.1.2.2 jym *
11 1.1.1.1.2.2 jym * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.1.1.1.2.2 jym * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1.1.1.2.2 jym * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1.1.1.2.2 jym * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1.1.1.2.2 jym * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1.1.1.2.2 jym * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1.1.1.2.2 jym * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1.1.1.2.2 jym */
19 1.1.1.1.2.2 jym
20 1.1.1.1.2.2 jym #if !defined(LINT) && !defined(CODECENTER)
21 1.1.1.1.2.2 jym static const char rcsid[] = "Id: gen_pw.c,v 1.3 2005/04/27 04:56:24 sra Exp";
22 1.1.1.1.2.2 jym #endif
23 1.1.1.1.2.2 jym
24 1.1.1.1.2.2 jym /* Imports */
25 1.1.1.1.2.2 jym
26 1.1.1.1.2.2 jym #include "port_before.h"
27 1.1.1.1.2.2 jym
28 1.1.1.1.2.2 jym #ifndef WANT_IRS_PW
29 1.1.1.1.2.2 jym static int __bind_irs_pw_unneeded;
30 1.1.1.1.2.2 jym #else
31 1.1.1.1.2.2 jym
32 1.1.1.1.2.2 jym #include <sys/types.h>
33 1.1.1.1.2.2 jym #include <netinet/in.h>
34 1.1.1.1.2.2 jym #include <arpa/nameser.h>
35 1.1.1.1.2.2 jym #include <resolv.h>
36 1.1.1.1.2.2 jym
37 1.1.1.1.2.2 jym #include <errno.h>
38 1.1.1.1.2.2 jym #include <pwd.h>
39 1.1.1.1.2.2 jym #include <stdlib.h>
40 1.1.1.1.2.2 jym #include <string.h>
41 1.1.1.1.2.2 jym
42 1.1.1.1.2.2 jym #include <isc/memcluster.h>
43 1.1.1.1.2.2 jym #include <irs.h>
44 1.1.1.1.2.2 jym
45 1.1.1.1.2.2 jym #include "port_after.h"
46 1.1.1.1.2.2 jym
47 1.1.1.1.2.2 jym #include "irs_p.h"
48 1.1.1.1.2.2 jym #include "gen_p.h"
49 1.1.1.1.2.2 jym
50 1.1.1.1.2.2 jym /* Types */
51 1.1.1.1.2.2 jym
52 1.1.1.1.2.2 jym struct pvt {
53 1.1.1.1.2.2 jym struct irs_rule * rules;
54 1.1.1.1.2.2 jym struct irs_rule * rule;
55 1.1.1.1.2.2 jym struct __res_state * res;
56 1.1.1.1.2.2 jym void (*free_res)(void *);
57 1.1.1.1.2.2 jym };
58 1.1.1.1.2.2 jym
59 1.1.1.1.2.2 jym /* Forward */
60 1.1.1.1.2.2 jym
61 1.1.1.1.2.2 jym static void pw_close(struct irs_pw *);
62 1.1.1.1.2.2 jym static struct passwd * pw_next(struct irs_pw *);
63 1.1.1.1.2.2 jym static struct passwd * pw_byname(struct irs_pw *, const char *);
64 1.1.1.1.2.2 jym static struct passwd * pw_byuid(struct irs_pw *, uid_t);
65 1.1.1.1.2.2 jym static void pw_rewind(struct irs_pw *);
66 1.1.1.1.2.2 jym static void pw_minimize(struct irs_pw *);
67 1.1.1.1.2.2 jym static struct __res_state * pw_res_get(struct irs_pw *);
68 1.1.1.1.2.2 jym static void pw_res_set(struct irs_pw *,
69 1.1.1.1.2.2 jym struct __res_state *,
70 1.1.1.1.2.2 jym void (*)(void *));
71 1.1.1.1.2.2 jym
72 1.1.1.1.2.2 jym /* Public */
73 1.1.1.1.2.2 jym
74 1.1.1.1.2.2 jym struct irs_pw *
75 1.1.1.1.2.2 jym irs_gen_pw(struct irs_acc *this) {
76 1.1.1.1.2.2 jym struct gen_p *accpvt = (struct gen_p *)this->private;
77 1.1.1.1.2.2 jym struct irs_pw *pw;
78 1.1.1.1.2.2 jym struct pvt *pvt;
79 1.1.1.1.2.2 jym
80 1.1.1.1.2.2 jym if (!(pw = memget(sizeof *pw))) {
81 1.1.1.1.2.2 jym errno = ENOMEM;
82 1.1.1.1.2.2 jym return (NULL);
83 1.1.1.1.2.2 jym }
84 1.1.1.1.2.2 jym memset(pw, 0x5e, sizeof *pw);
85 1.1.1.1.2.2 jym if (!(pvt = memget(sizeof *pvt))) {
86 1.1.1.1.2.2 jym memput(pw, sizeof *pvt);
87 1.1.1.1.2.2 jym errno = ENOMEM;
88 1.1.1.1.2.2 jym return (NULL);
89 1.1.1.1.2.2 jym }
90 1.1.1.1.2.2 jym memset(pvt, 0, sizeof *pvt);
91 1.1.1.1.2.2 jym pvt->rules = accpvt->map_rules[irs_pw];
92 1.1.1.1.2.2 jym pvt->rule = pvt->rules;
93 1.1.1.1.2.2 jym pw->private = pvt;
94 1.1.1.1.2.2 jym pw->close = pw_close;
95 1.1.1.1.2.2 jym pw->next = pw_next;
96 1.1.1.1.2.2 jym pw->byname = pw_byname;
97 1.1.1.1.2.2 jym pw->byuid = pw_byuid;
98 1.1.1.1.2.2 jym pw->rewind = pw_rewind;
99 1.1.1.1.2.2 jym pw->minimize = pw_minimize;
100 1.1.1.1.2.2 jym pw->res_get = pw_res_get;
101 1.1.1.1.2.2 jym pw->res_set = pw_res_set;
102 1.1.1.1.2.2 jym return (pw);
103 1.1.1.1.2.2 jym }
104 1.1.1.1.2.2 jym
105 1.1.1.1.2.2 jym /* Methods */
106 1.1.1.1.2.2 jym
107 1.1.1.1.2.2 jym static void
108 1.1.1.1.2.2 jym pw_close(struct irs_pw *this) {
109 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
110 1.1.1.1.2.2 jym
111 1.1.1.1.2.2 jym memput(pvt, sizeof *pvt);
112 1.1.1.1.2.2 jym memput(this, sizeof *this);
113 1.1.1.1.2.2 jym }
114 1.1.1.1.2.2 jym
115 1.1.1.1.2.2 jym static struct passwd *
116 1.1.1.1.2.2 jym pw_next(struct irs_pw *this) {
117 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
118 1.1.1.1.2.2 jym struct passwd *rval;
119 1.1.1.1.2.2 jym struct irs_pw *pw;
120 1.1.1.1.2.2 jym
121 1.1.1.1.2.2 jym while (pvt->rule) {
122 1.1.1.1.2.2 jym pw = pvt->rule->inst->pw;
123 1.1.1.1.2.2 jym rval = (*pw->next)(pw);
124 1.1.1.1.2.2 jym if (rval)
125 1.1.1.1.2.2 jym return (rval);
126 1.1.1.1.2.2 jym if (!(pvt->rule->flags & IRS_CONTINUE))
127 1.1.1.1.2.2 jym break;
128 1.1.1.1.2.2 jym pvt->rule = pvt->rule->next;
129 1.1.1.1.2.2 jym if (pvt->rule) {
130 1.1.1.1.2.2 jym pw = pvt->rule->inst->pw;
131 1.1.1.1.2.2 jym (*pw->rewind)(pw);
132 1.1.1.1.2.2 jym }
133 1.1.1.1.2.2 jym }
134 1.1.1.1.2.2 jym return (NULL);
135 1.1.1.1.2.2 jym }
136 1.1.1.1.2.2 jym
137 1.1.1.1.2.2 jym static void
138 1.1.1.1.2.2 jym pw_rewind(struct irs_pw *this) {
139 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
140 1.1.1.1.2.2 jym struct irs_pw *pw;
141 1.1.1.1.2.2 jym
142 1.1.1.1.2.2 jym pvt->rule = pvt->rules;
143 1.1.1.1.2.2 jym if (pvt->rule) {
144 1.1.1.1.2.2 jym pw = pvt->rule->inst->pw;
145 1.1.1.1.2.2 jym (*pw->rewind)(pw);
146 1.1.1.1.2.2 jym }
147 1.1.1.1.2.2 jym }
148 1.1.1.1.2.2 jym
149 1.1.1.1.2.2 jym static struct passwd *
150 1.1.1.1.2.2 jym pw_byname(struct irs_pw *this, const char *name) {
151 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
152 1.1.1.1.2.2 jym struct irs_rule *rule;
153 1.1.1.1.2.2 jym struct passwd *rval;
154 1.1.1.1.2.2 jym struct irs_pw *pw;
155 1.1.1.1.2.2 jym
156 1.1.1.1.2.2 jym rval = NULL;
157 1.1.1.1.2.2 jym for (rule = pvt->rules; rule; rule = rule->next) {
158 1.1.1.1.2.2 jym pw = rule->inst->pw;
159 1.1.1.1.2.2 jym rval = (*pw->byname)(pw, name);
160 1.1.1.1.2.2 jym if (rval || !(rule->flags & IRS_CONTINUE))
161 1.1.1.1.2.2 jym break;
162 1.1.1.1.2.2 jym }
163 1.1.1.1.2.2 jym return (rval);
164 1.1.1.1.2.2 jym }
165 1.1.1.1.2.2 jym
166 1.1.1.1.2.2 jym static struct passwd *
167 1.1.1.1.2.2 jym pw_byuid(struct irs_pw *this, uid_t uid) {
168 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
169 1.1.1.1.2.2 jym struct irs_rule *rule;
170 1.1.1.1.2.2 jym struct passwd *rval;
171 1.1.1.1.2.2 jym struct irs_pw *pw;
172 1.1.1.1.2.2 jym
173 1.1.1.1.2.2 jym rval = NULL;
174 1.1.1.1.2.2 jym for (rule = pvt->rules; rule; rule = rule->next) {
175 1.1.1.1.2.2 jym pw = rule->inst->pw;
176 1.1.1.1.2.2 jym rval = (*pw->byuid)(pw, uid);
177 1.1.1.1.2.2 jym if (rval || !(rule->flags & IRS_CONTINUE))
178 1.1.1.1.2.2 jym break;
179 1.1.1.1.2.2 jym }
180 1.1.1.1.2.2 jym return (rval);
181 1.1.1.1.2.2 jym }
182 1.1.1.1.2.2 jym
183 1.1.1.1.2.2 jym static void
184 1.1.1.1.2.2 jym pw_minimize(struct irs_pw *this) {
185 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
186 1.1.1.1.2.2 jym struct irs_rule *rule;
187 1.1.1.1.2.2 jym
188 1.1.1.1.2.2 jym for (rule = pvt->rules; rule != NULL; rule = rule->next) {
189 1.1.1.1.2.2 jym struct irs_pw *pw = rule->inst->pw;
190 1.1.1.1.2.2 jym
191 1.1.1.1.2.2 jym (*pw->minimize)(pw);
192 1.1.1.1.2.2 jym }
193 1.1.1.1.2.2 jym }
194 1.1.1.1.2.2 jym
195 1.1.1.1.2.2 jym static struct __res_state *
196 1.1.1.1.2.2 jym pw_res_get(struct irs_pw *this) {
197 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
198 1.1.1.1.2.2 jym
199 1.1.1.1.2.2 jym if (!pvt->res) {
200 1.1.1.1.2.2 jym struct __res_state *res;
201 1.1.1.1.2.2 jym res = (struct __res_state *)malloc(sizeof *res);
202 1.1.1.1.2.2 jym if (!res) {
203 1.1.1.1.2.2 jym errno = ENOMEM;
204 1.1.1.1.2.2 jym return (NULL);
205 1.1.1.1.2.2 jym }
206 1.1.1.1.2.2 jym memset(res, 0, sizeof *res);
207 1.1.1.1.2.2 jym pw_res_set(this, res, free);
208 1.1.1.1.2.2 jym }
209 1.1.1.1.2.2 jym
210 1.1.1.1.2.2 jym return (pvt->res);
211 1.1.1.1.2.2 jym }
212 1.1.1.1.2.2 jym
213 1.1.1.1.2.2 jym static void
214 1.1.1.1.2.2 jym pw_res_set(struct irs_pw *this, struct __res_state *res,
215 1.1.1.1.2.2 jym void (*free_res)(void *)) {
216 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
217 1.1.1.1.2.2 jym struct irs_rule *rule;
218 1.1.1.1.2.2 jym
219 1.1.1.1.2.2 jym if (pvt->res && pvt->free_res) {
220 1.1.1.1.2.2 jym res_nclose(pvt->res);
221 1.1.1.1.2.2 jym (*pvt->free_res)(pvt->res);
222 1.1.1.1.2.2 jym }
223 1.1.1.1.2.2 jym
224 1.1.1.1.2.2 jym pvt->res = res;
225 1.1.1.1.2.2 jym pvt->free_res = free_res;
226 1.1.1.1.2.2 jym
227 1.1.1.1.2.2 jym for (rule = pvt->rules; rule != NULL; rule = rule->next) {
228 1.1.1.1.2.2 jym struct irs_pw *pw = rule->inst->pw;
229 1.1.1.1.2.2 jym
230 1.1.1.1.2.2 jym if (pw->res_set)
231 1.1.1.1.2.2 jym (*pw->res_set)(pw, pvt->res, NULL);
232 1.1.1.1.2.2 jym }
233 1.1.1.1.2.2 jym }
234 1.1.1.1.2.2 jym
235 1.1.1.1.2.2 jym #endif /* WANT_IRS_PW */
236 1.1.1.1.2.2 jym /*! \file */
237