getnetnamadr.c revision 1.30 1 /* $NetBSD: getnetnamadr.c,v 1.30 2004/05/21 02:30:03 christos Exp $ */
2
3 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
4 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 */
10 /*
11 * Copyright (c) 1983, 1993
12 * The Regents of the University of California. All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93";
43 static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03";
44 static char rcsid[] = "Id: getnetnamadr.c,v 8.8 1997/06/01 20:34:37 vixie Exp ";
45 #else
46 __RCSID("$NetBSD: getnetnamadr.c,v 1.30 2004/05/21 02:30:03 christos Exp $");
47 #endif
48 #endif /* LIBC_SCCS and not lint */
49
50 #include "namespace.h"
51 #include <sys/types.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <arpa/nameser.h>
57
58 #include <assert.h>
59 #include <ctype.h>
60 #include <errno.h>
61 #include <netdb.h>
62 #include <nsswitch.h>
63 #include <resolv.h>
64 #include <stdarg.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68
69 #ifdef YP
70 #include <rpc/rpc.h>
71 #include <rpcsvc/yp_prot.h>
72 #include <rpcsvc/ypclnt.h>
73 #endif
74
75 #ifdef __weak_alias
76 __weak_alias(getnetbyaddr,_getnetbyaddr)
77 __weak_alias(getnetbyname,_getnetbyname)
78 #endif
79
80 extern int _net_stayopen;
81
82 #define BYADDR 0
83 #define BYNAME 1
84 #define MAXALIASES 35
85
86 #define MAXPACKET (64*1024)
87
88 typedef union {
89 HEADER hdr;
90 u_char buf[MAXPACKET];
91 } querybuf;
92
93 typedef union {
94 long al;
95 char ac;
96 } align;
97
98 #ifdef YP
99 static char *__ypdomain;
100 static char *__ypcurrent;
101 static int __ypcurrentlen;
102 #endif
103
104 static struct netent net_entry;
105 static char *net_aliases[MAXALIASES];
106
107 static struct netent *getnetanswer(querybuf *, int, int);
108 int _files_getnetbyaddr(void *, void *, va_list);
109 int _files_getnetbyname(void *, void *, va_list);
110 int _dns_getnetbyaddr(void *, void *, va_list);
111 int _dns_getnetbyname(void *, void *, va_list);
112 #ifdef YP
113 int _yp_getnetbyaddr(void *, void *, va_list);
114 int _yp_getnetbyname(void *, void *, va_list);
115 struct netent *_ypnetent(char *);
116 #endif
117
118 static struct netent *
119 getnetanswer(querybuf *answer, int anslen, int net_i)
120 {
121 HEADER *hp;
122 u_char *cp;
123 int n;
124 u_char *eom;
125 int type, class, ancount, qdcount, haveanswer, i, nchar;
126 char aux1[MAXDNAME], aux2[MAXDNAME], ans[MAXDNAME];
127 char *in, *st, *pauxt, *bp, **ap;
128 char *paux1 = &aux1[0], *paux2 = &aux2[0], *ep;
129 static char netbuf[PACKETSZ];
130
131 _DIAGASSERT(answer != NULL);
132
133 /*
134 * find first satisfactory answer
135 *
136 * answer --> +------------+ ( MESSAGE )
137 * | Header |
138 * +------------+
139 * | Question | the question for the name server
140 * +------------+
141 * | Answer | RRs answering the question
142 * +------------+
143 * | Authority | RRs pointing toward an authority
144 * | Additional | RRs holding additional information
145 * +------------+
146 */
147 eom = answer->buf + anslen;
148 hp = &answer->hdr;
149 ancount = ntohs(hp->ancount); /* #/records in the answer section */
150 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
151 bp = netbuf;
152 ep = netbuf + sizeof(netbuf);
153 cp = answer->buf + HFIXEDSZ;
154 if (!qdcount) {
155 if (hp->aa)
156 h_errno = HOST_NOT_FOUND;
157 else
158 h_errno = TRY_AGAIN;
159 return NULL;
160 }
161 while (qdcount-- > 0) {
162 n = __dn_skipname(cp, eom);
163 if (n < 0 || (cp + n + QFIXEDSZ) > eom) {
164 h_errno = NO_RECOVERY;
165 return(NULL);
166 }
167 cp += n + QFIXEDSZ;
168 }
169 ap = net_aliases;
170 *ap = NULL;
171 net_entry.n_aliases = net_aliases;
172 haveanswer = 0;
173 while (--ancount >= 0 && cp < eom) {
174 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
175 if ((n < 0) || !res_dnok(bp))
176 break;
177 cp += n;
178 ans[0] = '\0';
179 (void)strlcpy(ans, bp, sizeof(ans));
180 GETSHORT(type, cp);
181 GETSHORT(class, cp);
182 cp += INT32SZ; /* TTL */
183 GETSHORT(n, cp);
184 if (class == C_IN && type == T_PTR) {
185 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
186 if ((n < 0) || !res_hnok(bp)) {
187 cp += n;
188 return NULL;
189 }
190 cp += n;
191 *ap++ = bp;
192 bp += strlen(bp) + 1;
193 net_entry.n_addrtype =
194 (class == C_IN) ? AF_INET : AF_UNSPEC;
195 haveanswer++;
196 }
197 }
198 if (haveanswer) {
199 *ap = NULL;
200 switch (net_i) {
201 case BYADDR:
202 net_entry.n_name = *net_entry.n_aliases;
203 net_entry.n_net = 0L;
204 break;
205 case BYNAME:
206 ap = net_entry.n_aliases;
207 next_alias:
208 in = *ap++;
209 if (in == NULL) {
210 h_errno = HOST_NOT_FOUND;
211 return NULL;
212 }
213 net_entry.n_name = ans;
214 aux2[0] = '\0';
215 for (i = 0; i < 4; i++) {
216 for (st = in, nchar = 0;
217 isdigit((unsigned char)*st);
218 st++, nchar++)
219 ;
220 if (*st != '.' || nchar == 0 || nchar > 3)
221 goto next_alias;
222 if (i != 0)
223 nchar++;
224 (void)strlcpy(paux1, in, (size_t)nchar);
225 paux1[nchar] = '\0';
226 pauxt = paux2;
227 paux2 = strcat(paux1, paux2);
228 paux1 = pauxt;
229 in = ++st;
230 }
231 net_entry.n_net = inet_network(paux2);
232 break;
233 }
234 if (strcasecmp(in, "IN-ADDR.ARPA") != 0)
235 goto next_alias;
236 net_entry.n_aliases++;
237 #if (defined(__sparc__) && defined(_LP64)) || \
238 defined(__alpha__) || \
239 (defined(__i386__) && defined(_LP64)) || \
240 (defined(__sh__) && defined(_LP64))
241 net_entry.__n_pad0 = 0;
242 #endif
243 return &net_entry;
244 }
245 h_errno = TRY_AGAIN;
246 return NULL;
247 }
248
249 /*ARGSUSED*/
250 int
251 _files_getnetbyaddr(void *rv, void *cb_data, va_list ap)
252 {
253 struct netent *p;
254 unsigned long net;
255 int type;
256
257 _DIAGASSERT(rv != NULL);
258
259 net = va_arg(ap, unsigned long);
260 type = va_arg(ap, int);
261
262 setnetent(_net_stayopen);
263 while ((p = getnetent()) != NULL)
264 if (p->n_addrtype == type && p->n_net == net)
265 break;
266 if (!_net_stayopen)
267 endnetent();
268 *((struct netent **)rv) = p;
269 if (p==NULL) {
270 h_errno = HOST_NOT_FOUND;
271 return NS_NOTFOUND;
272 }
273 return NS_SUCCESS;
274 }
275
276 /*ARGSUSED*/
277 int
278 _dns_getnetbyaddr(void *rv, void *cb_data, va_list ap)
279 {
280 unsigned int netbr[4];
281 int nn, anslen;
282 querybuf *buf;
283 char qbuf[MAXDNAME];
284 unsigned long net2;
285 struct netent *np;
286 unsigned long net;
287 int type;
288 res_state res;
289
290 _DIAGASSERT(rv != NULL);
291
292 net = va_arg(ap, unsigned long);
293 type = va_arg(ap, int);
294
295 if (type != AF_INET)
296 return NS_UNAVAIL;
297
298 for (nn = 4, net2 = net; net2; net2 >>= 8)
299 netbr[--nn] = (unsigned int)(net2 & 0xff);
300 switch (nn) {
301 default:
302 return NS_UNAVAIL;
303 case 3: /* Class A */
304 snprintf(qbuf, sizeof(qbuf), "0.0.0.%u.in-addr.arpa", netbr[3]);
305 break;
306 case 2: /* Class B */
307 snprintf(qbuf, sizeof(qbuf), "0.0.%u.%u.in-addr.arpa",
308 netbr[3], netbr[2]);
309 break;
310 case 1: /* Class C */
311 snprintf(qbuf, sizeof(qbuf), "0.%u.%u.%u.in-addr.arpa",
312 netbr[3], netbr[2], netbr[1]);
313 break;
314 case 0: /* Class D - E */
315 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
316 netbr[3], netbr[2], netbr[1], netbr[0]);
317 break;
318 }
319 buf = malloc(sizeof(*buf));
320 if (buf == NULL) {
321 h_errno = NETDB_INTERNAL;
322 return NS_NOTFOUND;
323 }
324 res = __res_get_state();
325 if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
326 h_errno = NETDB_INTERNAL;
327 __res_put_state(res);
328 return NS_NOTFOUND;
329 }
330 anslen = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
331 if (anslen < 0) {
332 free(buf);
333 #ifdef DEBUG
334 if (res->options & RES_DEBUG)
335 printf("res_query failed\n");
336 #endif
337 __res_put_state(res);
338 return NS_NOTFOUND;
339 }
340 __res_put_state(res);
341 np = getnetanswer(buf, anslen, BYADDR);
342 free(buf);
343 if (np) {
344 /* maybe net should be unsigned? */
345 unsigned long u_net = net;
346
347 /* Strip trailing zeros */
348 while ((u_net & 0xff) == 0 && u_net != 0)
349 u_net >>= 8;
350 np->n_net = u_net;
351 }
352 *((struct netent **)rv) = np;
353 if (np == NULL) {
354 h_errno = HOST_NOT_FOUND;
355 return NS_NOTFOUND;
356 }
357 return NS_SUCCESS;
358 }
359
360
361 struct netent *
362 getnetbyaddr(uint32_t net, int net_type)
363 {
364 struct netent *np;
365 static const ns_dtab dtab[] = {
366 NS_FILES_CB(_files_getnetbyaddr, NULL)
367 { NSSRC_DNS, _dns_getnetbyaddr, NULL }, /* force -DHESIOD */
368 NS_NIS_CB(_yp_getnetbyaddr, NULL)
369 { 0 }
370 };
371
372 np = NULL;
373 h_errno = NETDB_INTERNAL;
374 if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyaddr", __nsdefaultsrc,
375 net, net_type) != NS_SUCCESS)
376 return NULL;
377 h_errno = NETDB_SUCCESS;
378 return np;
379 }
380
381 /*ARGSUSED*/
382 int
383 _files_getnetbyname(void *rv, void *cb_data, va_list ap)
384 {
385 struct netent *p;
386 char **cp;
387 const char *name;
388
389 _DIAGASSERT(rv != NULL);
390
391 name = va_arg(ap, const char *);
392 setnetent(_net_stayopen);
393 while ((p = getnetent()) != NULL) {
394 if (strcasecmp(p->n_name, name) == 0)
395 break;
396 for (cp = p->n_aliases; *cp != 0; cp++)
397 if (strcasecmp(*cp, name) == 0)
398 goto found;
399 }
400 found:
401 if (!_net_stayopen)
402 endnetent();
403 *((struct netent **)rv) = p;
404 if (p==NULL) {
405 h_errno = HOST_NOT_FOUND;
406 return NS_NOTFOUND;
407 }
408 return NS_SUCCESS;
409 }
410
411 /*ARGSUSED*/
412 int
413 _dns_getnetbyname(void *rv, void *cb_data, va_list ap)
414 {
415 int anslen;
416 querybuf *buf;
417 char qbuf[MAXDNAME];
418 struct netent *np;
419 const char *net;
420 res_state res;
421
422 _DIAGASSERT(rv != NULL);
423
424 net = va_arg(ap, const char *);
425 strlcpy(&qbuf[0], net, sizeof(qbuf));
426 buf = malloc(sizeof(*buf));
427 if (buf == NULL) {
428 h_errno = NETDB_INTERNAL;
429 return NS_NOTFOUND;
430 }
431 res = __res_get_state();
432 if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
433 h_errno = NETDB_INTERNAL;
434 __res_put_state(res);
435 return NS_NOTFOUND;
436 }
437
438 anslen = res_nsearch(res, qbuf, C_IN, T_PTR, buf->buf,
439 sizeof(buf->buf));
440 if (anslen < 0) {
441 free(buf);
442 #ifdef DEBUG
443 if (res->options & RES_DEBUG)
444 printf("res_search failed\n");
445 #endif
446 __res_put_state(res);
447 return NS_NOTFOUND;
448 }
449 __res_put_state(res);
450 np = getnetanswer(buf, anslen, BYNAME);
451 free(buf);
452 *((struct netent **)rv) = np;
453 if (np == NULL) {
454 h_errno = HOST_NOT_FOUND;
455 return NS_NOTFOUND;
456 }
457 return NS_SUCCESS;
458 }
459
460 struct netent *
461 getnetbyname(const char *net)
462 {
463 struct netent *np;
464 static const ns_dtab dtab[] = {
465 NS_FILES_CB(_files_getnetbyname, NULL)
466 { NSSRC_DNS, _dns_getnetbyname, NULL }, /* force -DHESIOD */
467 NS_NIS_CB(_yp_getnetbyname, NULL)
468 { 0 }
469 };
470
471 _DIAGASSERT(net != NULL);
472
473 np = NULL;
474 h_errno = NETDB_INTERNAL;
475 if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyname", __nsdefaultsrc,
476 net) != NS_SUCCESS)
477 return NULL;
478 h_errno = NETDB_SUCCESS;
479 return np;
480 }
481
482 #ifdef YP
483 /*ARGSUSED*/
484 int
485 _yp_getnetbyaddr(void *rv, void *cb_data, va_list ap)
486 {
487 struct netent *np;
488 char qbuf[MAXDNAME];
489 unsigned int netbr[4];
490 unsigned long net, net2;
491 int type, r;
492
493 _DIAGASSERT(rv != NULL);
494
495 net = va_arg(ap, unsigned long);
496 type = va_arg(ap, int);
497
498 if (type != AF_INET)
499 return NS_UNAVAIL;
500
501 if (!__ypdomain) {
502 if (_yp_check(&__ypdomain) == 0)
503 return NS_UNAVAIL;
504 }
505 np = NULL;
506 if (__ypcurrent)
507 free(__ypcurrent);
508 __ypcurrent = NULL;
509 for (r = 4, net2 = net; net2; net2 >>= 8)
510 netbr[--r] = (unsigned int)(net2 & 0xff);
511 switch (r) {
512 default:
513 return NS_UNAVAIL;
514 case 3: /* Class A */
515 snprintf(qbuf, sizeof(qbuf), "%u", netbr[3]);
516 break;
517 case 2: /* Class B */
518 snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[2], netbr[3]);
519 break;
520 case 1: /* Class C */
521 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[1], netbr[2],
522 netbr[3]);
523 break;
524 case 0: /* Class D - E */
525 snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1],
526 netbr[2], netbr[3]);
527 break;
528 }
529 r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf),
530 &__ypcurrent, &__ypcurrentlen);
531 if (r == 0)
532 np = _ypnetent(__ypcurrent);
533
534 *((struct netent **)rv) = np;
535 if (np == NULL) {
536 h_errno = HOST_NOT_FOUND;
537 return NS_NOTFOUND;
538 }
539 return NS_SUCCESS;
540
541 }
542
543 int
544 /*ARGSUSED*/
545 _yp_getnetbyname(void *rv, void *cb_data, va_list ap)
546 {
547 struct netent *np;
548 const char *name;
549 int r;
550
551 _DIAGASSERT(rv != NULL);
552
553 name = va_arg(ap, const char *);
554
555 if (!__ypdomain) {
556 if (_yp_check(&__ypdomain) == 0)
557 return NS_UNAVAIL;
558 }
559 np = NULL;
560 if (__ypcurrent)
561 free(__ypcurrent);
562 __ypcurrent = NULL;
563 r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name),
564 &__ypcurrent, &__ypcurrentlen);
565 if (r == 0)
566 np = _ypnetent(__ypcurrent);
567
568 *((struct netent **)rv) = np;
569 if (np == NULL) {
570 h_errno = HOST_NOT_FOUND;
571 return NS_NOTFOUND;
572 }
573 return NS_SUCCESS;
574 }
575
576 struct netent *
577 _ypnetent(char *line)
578 {
579 char *cp, *p, **q;
580
581 _DIAGASSERT(line != NULL);
582
583 net_entry.n_name = line;
584 cp = strpbrk(line, " \t");
585 if (cp == NULL)
586 return NULL;
587 *cp++ = '\0';
588 while (*cp == ' ' || *cp == '\t')
589 cp++;
590 p = strpbrk(cp, " \t");
591 if (p != NULL)
592 *p++ = '\0';
593 net_entry.n_net = inet_network(cp);
594 #if (defined(__sparc__) && defined(_LP64)) || \
595 defined(__alpha__) || \
596 (defined(__i386__) && defined(_LP64)) || \
597 (defined(__sh__) && defined(_LP64))
598 net_entry.__n_pad0 = 0;
599 #endif
600 net_entry.n_addrtype = AF_INET;
601 q = net_entry.n_aliases = net_aliases;
602 if (p != NULL) {
603 cp = p;
604 while (cp && *cp) {
605 if (*cp == ' ' || *cp == '\t') {
606 cp++;
607 continue;
608 }
609 if (q < &net_aliases[MAXALIASES - 1])
610 *q++ = cp;
611 cp = strpbrk(cp, " \t");
612 if (cp != NULL)
613 *cp++ = '\0';
614 }
615 }
616 *q = NULL;
617
618 return &net_entry;
619 }
620 #endif
621