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