Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / gen_pw.c
blob59a486e64d7a9e3ff799ae45a9cebb05a597a084
1 /* $NetBSD$ */
3 /*
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";
22 #endif
24 /* Imports */
26 #include "port_before.h"
28 #ifndef WANT_IRS_PW
29 static int __bind_irs_pw_unneeded;
30 #else
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
35 #include <resolv.h>
37 #include <errno.h>
38 #include <pwd.h>
39 #include <stdlib.h>
40 #include <string.h>
42 #include <isc/memcluster.h>
43 #include <irs.h>
45 #include "port_after.h"
47 #include "irs_p.h"
48 #include "gen_p.h"
50 /* Types */
52 struct pvt {
53 struct irs_rule * rules;
54 struct irs_rule * rule;
55 struct __res_state * res;
56 void (*free_res)(void *);
59 /* Forward */
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 *,
69 struct __res_state *,
70 void (*)(void *));
72 /* Public */
74 struct irs_pw *
75 irs_gen_pw(struct irs_acc *this) {
76 struct gen_p *accpvt = (struct gen_p *)this->private;
77 struct irs_pw *pw;
78 struct pvt *pvt;
80 if (!(pw = memget(sizeof *pw))) {
81 errno = ENOMEM;
82 return (NULL);
84 memset(pw, 0x5e, sizeof *pw);
85 if (!(pvt = memget(sizeof *pvt))) {
86 memput(pw, sizeof *pvt);
87 errno = ENOMEM;
88 return (NULL);
90 memset(pvt, 0, sizeof *pvt);
91 pvt->rules = accpvt->map_rules[irs_pw];
92 pvt->rule = pvt->rules;
93 pw->private = pvt;
94 pw->close = pw_close;
95 pw->next = pw_next;
96 pw->byname = pw_byname;
97 pw->byuid = pw_byuid;
98 pw->rewind = pw_rewind;
99 pw->minimize = pw_minimize;
100 pw->res_get = pw_res_get;
101 pw->res_set = pw_res_set;
102 return (pw);
105 /* Methods */
107 static void
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;
118 struct passwd *rval;
119 struct irs_pw *pw;
121 while (pvt->rule) {
122 pw = pvt->rule->inst->pw;
123 rval = (*pw->next)(pw);
124 if (rval)
125 return (rval);
126 if (!(pvt->rule->flags & IRS_CONTINUE))
127 break;
128 pvt->rule = pvt->rule->next;
129 if (pvt->rule) {
130 pw = pvt->rule->inst->pw;
131 (*pw->rewind)(pw);
134 return (NULL);
137 static void
138 pw_rewind(struct irs_pw *this) {
139 struct pvt *pvt = (struct pvt *)this->private;
140 struct irs_pw *pw;
142 pvt->rule = pvt->rules;
143 if (pvt->rule) {
144 pw = pvt->rule->inst->pw;
145 (*pw->rewind)(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;
153 struct passwd *rval;
154 struct irs_pw *pw;
156 rval = NULL;
157 for (rule = pvt->rules; rule; rule = rule->next) {
158 pw = rule->inst->pw;
159 rval = (*pw->byname)(pw, name);
160 if (rval || !(rule->flags & IRS_CONTINUE))
161 break;
163 return (rval);
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;
170 struct passwd *rval;
171 struct irs_pw *pw;
173 rval = NULL;
174 for (rule = pvt->rules; rule; rule = rule->next) {
175 pw = rule->inst->pw;
176 rval = (*pw->byuid)(pw, uid);
177 if (rval || !(rule->flags & IRS_CONTINUE))
178 break;
180 return (rval);
183 static void
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;
191 (*pw->minimize)(pw);
195 static struct __res_state *
196 pw_res_get(struct irs_pw *this) {
197 struct pvt *pvt = (struct pvt *)this->private;
199 if (!pvt->res) {
200 struct __res_state *res;
201 res = (struct __res_state *)malloc(sizeof *res);
202 if (!res) {
203 errno = ENOMEM;
204 return (NULL);
206 memset(res, 0, sizeof *res);
207 pw_res_set(this, res, free);
210 return (pvt->res);
213 static void
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);
224 pvt->res = 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;
230 if (pw->res_set)
231 (*pw->res_set)(pw, pvt->res, NULL);
235 #endif /* WANT_IRS_PW */
236 /*! \file */