Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / nis_pr.c
blob6fe55a5e835914293b48b2f7bbfa9ea5857c73cf
1 /*
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(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: nis_pr.c,v 1.2.2.1 2004/03/09 09:17:33 marka Exp $";
20 #endif
22 /* Imports */
24 #include "port_before.h"
26 #ifndef WANT_IRS_NIS
27 static int __bind_irs_nis_unneeded;
28 #else
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <resolv.h>
34 #ifdef T_NULL
35 #undef T_NULL /* Silence re-definition warning of T_NULL. */
36 #endif
37 #include <rpc/rpc.h>
38 #include <rpc/xdr.h>
39 #include <rpcsvc/yp_prot.h>
40 #include <rpcsvc/ypclnt.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <netdb.h>
45 #include <ctype.h>
46 #include <stdlib.h>
47 #include <errno.h>
49 #include <isc/memcluster.h>
50 #include <irs.h>
52 #include "port_after.h"
54 #include "irs_p.h"
55 #include "nis_p.h"
57 /* Definitions */
59 struct pvt {
60 int needrewind;
61 char * nis_domain;
62 char * curkey_data;
63 int curkey_len;
64 char * curval_data;
65 int curval_len;
66 struct protoent proto;
67 char * prbuf;
70 enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
72 static /*const*/ char protocols_byname[] = "protocols.byname";
73 static /*const*/ char protocols_bynumber[] = "protocols.bynumber";
75 /* Forward */
77 static void pr_close(struct irs_pr *);
78 static struct protoent * pr_byname(struct irs_pr *, const char *);
79 static struct protoent * pr_bynumber(struct irs_pr *, int);
80 static struct protoent * pr_next(struct irs_pr *);
81 static void pr_rewind(struct irs_pr *);
82 static void pr_minimize(struct irs_pr *);
84 static struct protoent * makeprotoent(struct irs_pr *this);
85 static void nisfree(struct pvt *, enum do_what);
87 /* Public */
89 struct irs_pr *
90 irs_nis_pr(struct irs_acc *this) {
91 struct irs_pr *pr;
92 struct pvt *pvt;
94 if (!(pr = memget(sizeof *pr))) {
95 errno = ENOMEM;
96 return (NULL);
98 memset(pr, 0x5e, sizeof *pr);
99 if (!(pvt = memget(sizeof *pvt))) {
100 memput(pr, sizeof *pr);
101 errno = ENOMEM;
102 return (NULL);
104 memset(pvt, 0, sizeof *pvt);
105 pvt->needrewind = 1;
106 pvt->nis_domain = ((struct nis_p *)this->private)->domain;
107 pr->private = pvt;
108 pr->byname = pr_byname;
109 pr->bynumber = pr_bynumber;
110 pr->next = pr_next;
111 pr->rewind = pr_rewind;
112 pr->close = pr_close;
113 pr->minimize = pr_minimize;
114 pr->res_get = NULL;
115 pr->res_set = NULL;
116 return (pr);
119 /* Methods. */
121 static void
122 pr_close(struct irs_pr *this) {
123 struct pvt *pvt = (struct pvt *)this->private;
125 nisfree(pvt, do_all);
126 if (pvt->proto.p_aliases)
127 free(pvt->proto.p_aliases);
128 if (pvt->prbuf)
129 free(pvt->prbuf);
130 memput(pvt, sizeof *pvt);
131 memput(this, sizeof *this);
134 static struct protoent *
135 pr_byname(struct irs_pr *this, const char *name) {
136 struct pvt *pvt = (struct pvt *)this->private;
137 int r;
138 char *tmp;
140 nisfree(pvt, do_val);
141 DE_CONST(name, tmp);
142 r = yp_match(pvt->nis_domain, protocols_byname, tmp,
143 strlen(tmp), &pvt->curval_data, &pvt->curval_len);
144 if (r != 0) {
145 errno = ENOENT;
146 return (NULL);
148 return (makeprotoent(this));
151 static struct protoent *
152 pr_bynumber(struct irs_pr *this, int num) {
153 struct pvt *pvt = (struct pvt *)this->private;
154 char tmp[sizeof "-4294967295"];
155 int r;
157 nisfree(pvt, do_val);
158 (void) sprintf(tmp, "%d", num);
159 r = yp_match(pvt->nis_domain, protocols_bynumber, tmp, strlen(tmp),
160 &pvt->curval_data, &pvt->curval_len);
161 if (r != 0) {
162 errno = ENOENT;
163 return (NULL);
165 return (makeprotoent(this));
168 static struct protoent *
169 pr_next(struct irs_pr *this) {
170 struct pvt *pvt = (struct pvt *)this->private;
171 struct protoent *rval;
172 int r;
174 do {
175 if (pvt->needrewind) {
176 nisfree(pvt, do_all);
177 r = yp_first(pvt->nis_domain, protocols_bynumber,
178 &pvt->curkey_data, &pvt->curkey_len,
179 &pvt->curval_data, &pvt->curval_len);
180 pvt->needrewind = 0;
181 } else {
182 char *newkey_data;
183 int newkey_len;
185 nisfree(pvt, do_val);
186 r = yp_next(pvt->nis_domain, protocols_bynumber,
187 pvt->curkey_data, pvt->curkey_len,
188 &newkey_data, &newkey_len,
189 &pvt->curval_data, &pvt->curval_len);
190 nisfree(pvt, do_key);
191 pvt->curkey_data = newkey_data;
192 pvt->curkey_len = newkey_len;
194 if (r != 0) {
195 errno = ENOENT;
196 return (NULL);
198 rval = makeprotoent(this);
199 } while (rval == NULL);
200 return (rval);
203 static void
204 pr_rewind(struct irs_pr *this) {
205 struct pvt *pvt = (struct pvt *)this->private;
207 pvt->needrewind = 1;
210 static void
211 pr_minimize(struct irs_pr *this) {
212 UNUSED(this);
213 /* NOOP */
216 /* Private */
218 static struct protoent *
219 makeprotoent(struct irs_pr *this) {
220 struct pvt *pvt = (struct pvt *)this->private;
221 char *p, **t;
222 int n, m;
224 if (pvt->prbuf)
225 free(pvt->prbuf);
226 pvt->prbuf = pvt->curval_data;
227 pvt->curval_data = NULL;
229 for (p = pvt->prbuf; *p && *p != '#';)
230 p++;
231 while (p > pvt->prbuf && isspace((unsigned char)(p[-1])))
232 p--;
233 *p = '\0';
235 p = pvt->prbuf;
236 n = m = 0;
238 pvt->proto.p_name = p;
239 while (*p && !isspace((unsigned char)*p))
240 p++;
241 if (!*p)
242 return (NULL);
243 *p++ = '\0';
245 while (*p && isspace((unsigned char)*p))
246 p++;
247 pvt->proto.p_proto = atoi(p);
248 while (*p && !isspace((unsigned char)*p))
249 p++;
250 *p++ = '\0';
252 while (*p) {
253 if ((n + 1) >= m || !pvt->proto.p_aliases) {
254 m += 10;
255 t = realloc(pvt->proto.p_aliases,
256 m * sizeof(char *));
257 if (!t) {
258 errno = ENOMEM;
259 goto cleanup;
261 pvt->proto.p_aliases = t;
263 pvt->proto.p_aliases[n++] = p;
264 while (*p && !isspace((unsigned char)*p))
265 p++;
266 if (*p)
267 *p++ = '\0';
269 if (!pvt->proto.p_aliases)
270 pvt->proto.p_aliases = malloc(sizeof(char *));
271 if (!pvt->proto.p_aliases)
272 goto cleanup;
273 pvt->proto.p_aliases[n] = NULL;
274 return (&pvt->proto);
276 cleanup:
277 if (pvt->proto.p_aliases) {
278 free(pvt->proto.p_aliases);
279 pvt->proto.p_aliases = NULL;
281 if (pvt->prbuf) {
282 free(pvt->prbuf);
283 pvt->prbuf = NULL;
285 return (NULL);
288 static void
289 nisfree(struct pvt *pvt, enum do_what do_what) {
290 if ((do_what & do_key) && pvt->curkey_data) {
291 free(pvt->curkey_data);
292 pvt->curkey_data = NULL;
294 if ((do_what & do_val) && pvt->curval_data) {
295 free(pvt->curval_data);
296 pvt->curval_data = NULL;
300 #endif /*WANT_IRS_NIS*/