Merge from vendor branch LESS:
[dragonfly.git] / contrib / bind-9.3 / lib / bind / irs / nis_nw.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(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: nis_nw.c,v 1.2.206.1 2004/03/09 08:33:38 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
21
22 /* Imports */
23
24 #include "port_before.h"
25
26 #ifndef WANT_IRS_NIS
27 static int __bind_irs_nis_unneeded;
28 #else
29
30 #include <sys/types.h>
31 #include <sys/socket.h>
32
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <arpa/nameser.h>
36 #ifdef T_NULL
37 #undef T_NULL           /* Silence re-definition warning of T_NULL. */
38 #endif
39 #include <rpc/rpc.h>
40 #include <rpc/xdr.h>
41 #include <rpcsvc/yp_prot.h>
42 #include <rpcsvc/ypclnt.h>
43
44 #include <errno.h>
45 #include <resolv.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include <isc/memcluster.h>
51 #include <irs.h>
52
53 #include "port_after.h"
54
55 #include "irs_p.h"
56 #include "nis_p.h"
57
58 /* Definitions */
59
60 #define MAXALIASES 35
61 #define MAXADDRSIZE 4
62
63 struct pvt {
64         int             needrewind;
65         char *          nis_domain;
66         char *          curkey_data;
67         int             curkey_len;
68         char *          curval_data;
69         int             curval_len;
70
71         struct nwent    nwent;
72         char *          nwbuf;
73
74         char *          aliases[MAXALIASES + 1];
75         u_char          addr[MAXADDRSIZE];
76
77         struct __res_state *  res;
78         void            (*free_res)(void *);
79 };
80
81 enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
82
83 static /*const*/ char networks_byname[] = "networks.byname";
84 static /*const*/ char networks_byaddr[] = "networks.byaddr";
85
86 /* Forward */
87
88 static void             nw_close(struct irs_nw *);
89 static struct nwent *   nw_byname(struct irs_nw *, const char *, int);
90 static struct nwent *   nw_byaddr(struct irs_nw *, void *, int, int);
91 static struct nwent *   nw_next(struct irs_nw *);
92 static void             nw_rewind(struct irs_nw *);
93 static void             nw_minimize(struct irs_nw *);
94 static struct __res_state * nw_res_get(struct irs_nw *this);
95 static void             nw_res_set(struct irs_nw *this,
96                                    struct __res_state *res,
97                                    void (*free_res)(void *));
98
99 static struct nwent *   makenwent(struct irs_nw *this);
100 static void             nisfree(struct pvt *, enum do_what);
101 static int              init(struct irs_nw *this);
102
103 /* Public */
104
105 struct irs_nw *
106 irs_nis_nw(struct irs_acc *this) {
107         struct irs_nw *nw;
108         struct pvt *pvt;
109
110         if (!(pvt = memget(sizeof *pvt))) {
111                 errno = ENOMEM;
112                 return (NULL);
113         }
114         memset(pvt, 0, sizeof *pvt);
115         if (!(nw = memget(sizeof *nw))) {
116                 memput(pvt, sizeof *pvt);
117                 errno = ENOMEM;
118                 return (NULL);
119         }
120         memset(nw, 0x5e, sizeof *nw);
121         pvt->needrewind = 1;
122         pvt->nis_domain = ((struct nis_p *)this->private)->domain;
123         nw->private = pvt;
124         nw->close = nw_close;
125         nw->byname = nw_byname;
126         nw->byaddr = nw_byaddr;
127         nw->next = nw_next;
128         nw->rewind = nw_rewind;
129         nw->minimize = nw_minimize;
130         nw->res_get = nw_res_get;
131         nw->res_set = nw_res_set;
132         return (nw);
133 }
134
135 /* Methods */
136
137 static void
138 nw_close(struct irs_nw *this) {
139         struct pvt *pvt = (struct pvt *)this->private;  
140
141         nw_minimize(this);
142         if (pvt->res && pvt->free_res)
143                 (*pvt->free_res)(pvt->res);
144         if (pvt->nwbuf)
145                 free(pvt->nwbuf);
146         memput(pvt, sizeof *pvt);
147         memput(this, sizeof *this);
148 }
149
150 static struct nwent *
151 nw_byaddr(struct irs_nw *this, void *net, int length, int af) {
152         struct pvt *pvt = (struct pvt *)this->private;
153         char tmp[sizeof "255.255.255.255/32"], *t;
154         int r;
155
156         if (init(this) == -1)
157                 return (NULL);
158
159         if (af != AF_INET) {
160                 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
161                 errno = EAFNOSUPPORT;
162                 return (NULL);
163         }
164         nisfree(pvt, do_val);
165         /* Try it with /CIDR first. */
166         if (inet_net_ntop(AF_INET, net, length, tmp, sizeof tmp) == NULL) {
167                 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
168                 return (NULL);
169         }
170         r = yp_match(pvt->nis_domain, networks_byaddr, tmp, strlen(tmp),
171                      &pvt->curval_data, &pvt->curval_len);
172         if (r != 0) {
173                 /* Give it a shot without the /CIDR. */
174                 if ((t = strchr(tmp, '/')) != NULL) {
175                         *t = '\0';
176                         r = yp_match(pvt->nis_domain, networks_byaddr,
177                                      tmp, strlen(tmp),
178                                      &pvt->curval_data, &pvt->curval_len);
179                 }
180                 if (r != 0) {
181                         RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND);
182                         return (NULL);
183                 }
184         }
185         return (makenwent(this));
186 }
187
188 static struct nwent *
189 nw_byname(struct irs_nw *this, const char *name, int af) {
190         struct pvt *pvt = (struct pvt *)this->private;
191         int r;
192         char *tmp;
193         
194         if (init(this) == -1)
195                 return (NULL);
196
197         if (af != AF_INET) {
198                 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
199                 errno = EAFNOSUPPORT;
200                 return (NULL);
201         }
202         nisfree(pvt, do_val);
203         DE_CONST(name, tmp);
204         r = yp_match(pvt->nis_domain, networks_byname, tmp,
205                      strlen(tmp), &pvt->curval_data, &pvt->curval_len);
206         if (r != 0) {
207                 RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND);
208                 return (NULL);
209         }
210         return (makenwent(this));
211 }
212
213 static void
214 nw_rewind(struct irs_nw *this) {
215         struct pvt *pvt = (struct pvt *)this->private;
216
217         pvt->needrewind = 1;
218 }
219
220 static struct nwent *
221 nw_next(struct irs_nw *this) {
222         struct pvt *pvt = (struct pvt *)this->private;
223         struct nwent *rval;
224         int r;
225
226         if (init(this) == -1)
227                 return (NULL);
228
229         do {
230                 if (pvt->needrewind) {
231                         nisfree(pvt, do_all);
232                         r = yp_first(pvt->nis_domain, networks_byaddr,
233                                      &pvt->curkey_data, &pvt->curkey_len,
234                                      &pvt->curval_data, &pvt->curval_len);
235                         pvt->needrewind = 0;
236                 } else {
237                         char *newkey_data;
238                         int newkey_len;
239
240                         nisfree(pvt, do_val);
241                         r = yp_next(pvt->nis_domain, networks_byaddr,
242                                     pvt->curkey_data, pvt->curkey_len,
243                                     &newkey_data, &newkey_len,
244                                     &pvt->curval_data, &pvt->curval_len);
245                         nisfree(pvt, do_key);
246                         pvt->curkey_data = newkey_data;
247                         pvt->curkey_len = newkey_len;
248                 }
249                 if (r != 0) {
250                         RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND);
251                         return (NULL);
252                 }
253                 rval = makenwent(this);
254         } while (rval == NULL);
255         return (rval);
256 }
257
258 static void
259 nw_minimize(struct irs_nw *this) {
260         struct pvt *pvt = (struct pvt *)this->private;
261
262         if (pvt->res)
263                 res_nclose(pvt->res);
264 }
265
266 static struct __res_state *
267 nw_res_get(struct irs_nw *this) {
268         struct pvt *pvt = (struct pvt *)this->private;
269
270         if (!pvt->res) {
271                 struct __res_state *res;
272                 res = (struct __res_state *)malloc(sizeof *res);
273                 if (!res) {
274                         errno = ENOMEM;
275                         return (NULL);
276                 }
277                 memset(res, 0, sizeof *res);
278                 nw_res_set(this, res, free);
279         }
280
281         return (pvt->res);
282 }
283
284 static void
285 nw_res_set(struct irs_nw *this, struct __res_state *res,
286                 void (*free_res)(void *)) {
287         struct pvt *pvt = (struct pvt *)this->private;
288
289         if (pvt->res && pvt->free_res) {
290                 res_nclose(pvt->res);
291                 (*pvt->free_res)(pvt->res);
292         }
293
294         pvt->res = res;
295         pvt->free_res = free_res;
296 }
297
298 /* Private */
299
300 static struct nwent *
301 makenwent(struct irs_nw *this) {
302         struct pvt *pvt = (struct pvt *)this->private;
303         static const char spaces[] = " \t";
304         char *t, *cp, **ap;
305
306         if (pvt->nwbuf)
307                 free(pvt->nwbuf);
308         pvt->nwbuf = pvt->curval_data;
309         pvt->curval_data = NULL;
310
311         if ((cp = strpbrk(pvt->nwbuf, "#\n")) != NULL)
312                 *cp = '\0';
313         cp = pvt->nwbuf;
314
315         /* Name */
316         pvt->nwent.n_name = cp;
317         cp += strcspn(cp, spaces);
318         if (!*cp)
319                 goto cleanup;
320         *cp++ = '\0';
321         cp += strspn(cp, spaces);
322
323         /* Network */
324         pvt->nwent.n_addrtype = AF_INET;
325         t = cp + strcspn(cp, spaces);
326         if (*t)
327                 *t++ = '\0';
328         pvt->nwent.n_length = inet_net_pton(AF_INET, cp,
329                                             pvt->addr, sizeof pvt->addr);
330         if (pvt->nwent.n_length < 0)
331                 goto cleanup;
332         pvt->nwent.n_addr = pvt->addr;
333         cp = t;
334
335         /* Aliases */
336         ap = pvt->nwent.n_aliases = pvt->aliases;
337         while (*cp) {
338                 if (ap >= &pvt->aliases[MAXALIASES])
339                         break;
340                 *ap++ = cp;
341                 cp += strcspn(cp, spaces);
342                 if (!*cp)
343                         break;
344                 *cp++ = '\0';
345                 cp += strspn(cp, spaces);
346         }
347         *ap = NULL;
348
349         return (&pvt->nwent);
350
351  cleanup:
352         if (pvt->nwbuf) {
353                 free(pvt->nwbuf);
354                 pvt->nwbuf = NULL;
355         }
356         return (NULL);
357 }
358
359 static void
360 nisfree(struct pvt *pvt, enum do_what do_what) {
361         if ((do_what & do_key) && pvt->curkey_data) {
362                 free(pvt->curkey_data);
363                 pvt->curkey_data = NULL;
364         }
365         if ((do_what & do_val) && pvt->curval_data) {
366                 free(pvt->curval_data);
367                 pvt->curval_data = NULL;
368         }
369 }
370
371 static int
372 init(struct irs_nw *this) {
373         struct pvt *pvt = (struct pvt *)this->private;
374         
375         if (!pvt->res && !nw_res_get(this))
376                 return (-1);
377         if (((pvt->res->options & RES_INIT) == 0) &&
378             res_ninit(pvt->res) == -1)
379                 return (-1);
380         return (0);
381 }
382
383 #endif /*WANT_IRS_NIS*/