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(LIBC_SCCS) && !defined(lint)
21 static const char rcsid
[] = "Id: nis_nw.c,v 1.4 2005/04/27 04:56:33 sra Exp";
22 #endif /* LIBC_SCCS and not lint */
26 #include "port_before.h"
29 static int __bind_irs_nis_unneeded
;
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <arpa/nameser.h>
39 #undef T_NULL /* Silence re-definition warning of T_NULL. */
43 #include <rpcsvc/yp_prot.h>
44 #include <rpcsvc/ypclnt.h>
52 #include <isc/memcluster.h>
55 #include "port_after.h"
76 char * aliases
[MAXALIASES
+ 1];
77 u_char addr
[MAXADDRSIZE
];
79 struct __res_state
* res
;
80 void (*free_res
)(void *);
83 enum do_what
{ do_none
= 0x0, do_key
= 0x1, do_val
= 0x2, do_all
= 0x3 };
85 static /*const*/ char networks_byname
[] = "networks.byname";
86 static /*const*/ char networks_byaddr
[] = "networks.byaddr";
90 static void nw_close(struct irs_nw
*);
91 static struct nwent
* nw_byname(struct irs_nw
*, const char *, int);
92 static struct nwent
* nw_byaddr(struct irs_nw
*, void *, int, int);
93 static struct nwent
* nw_next(struct irs_nw
*);
94 static void nw_rewind(struct irs_nw
*);
95 static void nw_minimize(struct irs_nw
*);
96 static struct __res_state
* nw_res_get(struct irs_nw
*this);
97 static void nw_res_set(struct irs_nw
*this,
98 struct __res_state
*res
,
99 void (*free_res
)(void *));
101 static struct nwent
* makenwent(struct irs_nw
*this);
102 static void nisfree(struct pvt
*, enum do_what
);
103 static int init(struct irs_nw
*this);
108 irs_nis_nw(struct irs_acc
*this) {
112 if (!(pvt
= memget(sizeof *pvt
))) {
116 memset(pvt
, 0, sizeof *pvt
);
117 if (!(nw
= memget(sizeof *nw
))) {
118 memput(pvt
, sizeof *pvt
);
122 memset(nw
, 0x5e, sizeof *nw
);
124 pvt
->nis_domain
= ((struct nis_p
*)this->private)->domain
;
126 nw
->close
= nw_close
;
127 nw
->byname
= nw_byname
;
128 nw
->byaddr
= nw_byaddr
;
130 nw
->rewind
= nw_rewind
;
131 nw
->minimize
= nw_minimize
;
132 nw
->res_get
= nw_res_get
;
133 nw
->res_set
= nw_res_set
;
140 nw_close(struct irs_nw
*this) {
141 struct pvt
*pvt
= (struct pvt
*)this->private;
144 if (pvt
->res
&& pvt
->free_res
)
145 (*pvt
->free_res
)(pvt
->res
);
148 memput(pvt
, sizeof *pvt
);
149 memput(this, sizeof *this);
152 static struct nwent
*
153 nw_byaddr(struct irs_nw
*this, void *net
, int length
, int af
) {
154 struct pvt
*pvt
= (struct pvt
*)this->private;
155 char tmp
[sizeof "255.255.255.255/32"], *t
;
158 if (init(this) == -1)
162 RES_SET_H_ERRNO(pvt
->res
, NETDB_INTERNAL
);
163 errno
= EAFNOSUPPORT
;
166 nisfree(pvt
, do_val
);
167 /* Try it with /CIDR first. */
168 if (inet_net_ntop(AF_INET
, net
, length
, tmp
, sizeof tmp
) == NULL
) {
169 RES_SET_H_ERRNO(pvt
->res
, NETDB_INTERNAL
);
172 r
= yp_match(pvt
->nis_domain
, networks_byaddr
, tmp
, strlen(tmp
),
173 &pvt
->curval_data
, &pvt
->curval_len
);
175 /* Give it a shot without the /CIDR. */
176 if ((t
= strchr(tmp
, '/')) != NULL
) {
178 r
= yp_match(pvt
->nis_domain
, networks_byaddr
,
180 &pvt
->curval_data
, &pvt
->curval_len
);
183 RES_SET_H_ERRNO(pvt
->res
, HOST_NOT_FOUND
);
187 return (makenwent(this));
190 static struct nwent
*
191 nw_byname(struct irs_nw
*this, const char *name
, int af
) {
192 struct pvt
*pvt
= (struct pvt
*)this->private;
196 if (init(this) == -1)
200 RES_SET_H_ERRNO(pvt
->res
, NETDB_INTERNAL
);
201 errno
= EAFNOSUPPORT
;
204 nisfree(pvt
, do_val
);
206 r
= yp_match(pvt
->nis_domain
, networks_byname
, tmp
,
207 strlen(tmp
), &pvt
->curval_data
, &pvt
->curval_len
);
209 RES_SET_H_ERRNO(pvt
->res
, HOST_NOT_FOUND
);
212 return (makenwent(this));
216 nw_rewind(struct irs_nw
*this) {
217 struct pvt
*pvt
= (struct pvt
*)this->private;
222 static struct nwent
*
223 nw_next(struct irs_nw
*this) {
224 struct pvt
*pvt
= (struct pvt
*)this->private;
228 if (init(this) == -1)
232 if (pvt
->needrewind
) {
233 nisfree(pvt
, do_all
);
234 r
= yp_first(pvt
->nis_domain
, networks_byaddr
,
235 &pvt
->curkey_data
, &pvt
->curkey_len
,
236 &pvt
->curval_data
, &pvt
->curval_len
);
242 nisfree(pvt
, do_val
);
243 r
= yp_next(pvt
->nis_domain
, networks_byaddr
,
244 pvt
->curkey_data
, pvt
->curkey_len
,
245 &newkey_data
, &newkey_len
,
246 &pvt
->curval_data
, &pvt
->curval_len
);
247 nisfree(pvt
, do_key
);
248 pvt
->curkey_data
= newkey_data
;
249 pvt
->curkey_len
= newkey_len
;
252 RES_SET_H_ERRNO(pvt
->res
, HOST_NOT_FOUND
);
255 rval
= makenwent(this);
256 } while (rval
== NULL
);
261 nw_minimize(struct irs_nw
*this) {
262 struct pvt
*pvt
= (struct pvt
*)this->private;
265 res_nclose(pvt
->res
);
268 static struct __res_state
*
269 nw_res_get(struct irs_nw
*this) {
270 struct pvt
*pvt
= (struct pvt
*)this->private;
273 struct __res_state
*res
;
274 res
= (struct __res_state
*)malloc(sizeof *res
);
279 memset(res
, 0, sizeof *res
);
280 nw_res_set(this, res
, free
);
287 nw_res_set(struct irs_nw
*this, struct __res_state
*res
,
288 void (*free_res
)(void *)) {
289 struct pvt
*pvt
= (struct pvt
*)this->private;
291 if (pvt
->res
&& pvt
->free_res
) {
292 res_nclose(pvt
->res
);
293 (*pvt
->free_res
)(pvt
->res
);
297 pvt
->free_res
= free_res
;
302 static struct nwent
*
303 makenwent(struct irs_nw
*this) {
304 struct pvt
*pvt
= (struct pvt
*)this->private;
305 static const char spaces
[] = " \t";
310 pvt
->nwbuf
= pvt
->curval_data
;
311 pvt
->curval_data
= NULL
;
313 if ((cp
= strpbrk(pvt
->nwbuf
, "#\n")) != NULL
)
318 pvt
->nwent
.n_name
= cp
;
319 cp
+= strcspn(cp
, spaces
);
323 cp
+= strspn(cp
, spaces
);
326 pvt
->nwent
.n_addrtype
= AF_INET
;
327 t
= cp
+ strcspn(cp
, spaces
);
330 pvt
->nwent
.n_length
= inet_net_pton(AF_INET
, cp
,
331 pvt
->addr
, sizeof pvt
->addr
);
332 if (pvt
->nwent
.n_length
< 0)
334 pvt
->nwent
.n_addr
= pvt
->addr
;
338 ap
= pvt
->nwent
.n_aliases
= pvt
->aliases
;
340 if (ap
>= &pvt
->aliases
[MAXALIASES
])
343 cp
+= strcspn(cp
, spaces
);
347 cp
+= strspn(cp
, spaces
);
351 return (&pvt
->nwent
);
362 nisfree(struct pvt
*pvt
, enum do_what do_what
) {
363 if ((do_what
& do_key
) && pvt
->curkey_data
) {
364 free(pvt
->curkey_data
);
365 pvt
->curkey_data
= NULL
;
367 if ((do_what
& do_val
) && pvt
->curval_data
) {
368 free(pvt
->curval_data
);
369 pvt
->curval_data
= NULL
;
374 init(struct irs_nw
*this) {
375 struct pvt
*pvt
= (struct pvt
*)this->private;
377 if (!pvt
->res
&& !nw_res_get(this))
379 if (((pvt
->res
->options
& RES_INIT
) == 0) &&
380 res_ninit(pvt
->res
) == -1)
385 #endif /*WANT_IRS_NIS*/