Add nsswitch support.
[dragonfly.git] / lib / libc / net / getnetbydns.c
1 /*-
2  * Copyright (c) 1985, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  * -
29  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
30  *
31  * Permission to use, copy, modify, and distribute this software for any
32  * purpose with or without fee is hereby granted, provided that the above
33  * copyright notice and this permission notice appear in all copies, and that
34  * the name of Digital Equipment Corporation not be used in advertising or
35  * publicity pertaining to distribution of the document or software without
36  * specific, written prior permission.
37  *
38  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
39  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
41  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
42  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
43  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
44  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45  * SOFTWARE.
46  * -
47  * --Copyright--
48  *
49  * @(#)gethostnamadr.c  8.1 (Berkeley) 6/4/93
50  * $FreeBSD: src/lib/libc/net/getnetbydns.c,v 1.13.2.4 2002/10/11 11:07:13 ume Exp $
51  * $DragonFly: src/lib/libc/net/getnetbydns.c,v 1.5 2005/11/13 02:04:47 swildner Exp $
52  */
53 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
54  *      Dep. Matematica Universidade de Coimbra, Portugal, Europe
55  *
56  * Permission to use, copy, modify, and distribute this software for any
57  * purpose with or without fee is hereby granted, provided that the above
58  * copyright notice and this permission notice appear in all copies.
59  */
60
61 #include <sys/param.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
65 #include <arpa/nameser.h>
66
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <netdb.h>
70 #include <resolv.h>
71 #include <ctype.h>
72 #include <string.h>
73 #include <unistd.h>
74 #include <syslog.h>
75 #include <stdarg.h>
76 #include <nsswitch.h>
77
78 #include "res_config.h"
79
80 extern int h_errno;
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 static struct netent *
99 getnetanswer(querybuf *answer, int anslen, int net_i)
100 {
101
102         HEADER *hp;
103         u_char *cp;
104         int n;
105         u_char *eom;
106         int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
107         char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN];
108         char *in, *st, *pauxt, *bp, **ap;
109         char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
110 static  struct netent net_entry;
111 static  char *net_aliases[MAXALIASES], netbuf[PACKETSZ];
112
113         /*
114          * find first satisfactory answer
115          *
116          *      answer --> +------------+  ( MESSAGE )
117          *                 |   Header   |
118          *                 +------------+
119          *                 |  Question  | the question for the name server
120          *                 +------------+
121          *                 |   Answer   | RRs answering the question
122          *                 +------------+
123          *                 | Authority  | RRs pointing toward an authority
124          *                 | Additional | RRs holding additional information
125          *                 +------------+
126          */
127         eom = answer->buf + anslen;
128         hp = &answer->hdr;
129         ancount = ntohs(hp->ancount); /* #/records in the answer section */
130         qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
131         bp = netbuf;
132         buflen = sizeof(netbuf);
133         cp = answer->buf + HFIXEDSZ;
134         if (!qdcount) {
135                 if (hp->aa)
136                         h_errno = HOST_NOT_FOUND;
137                 else
138                         h_errno = TRY_AGAIN;
139                 return (NULL);
140         }
141         while (qdcount-- > 0)
142                 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
143         ap = net_aliases;
144         *ap = NULL;
145         net_entry.n_aliases = net_aliases;
146         haveanswer = 0;
147         while (--ancount >= 0 && cp < eom) {
148                 n = dn_expand(answer->buf, eom, cp, bp, buflen);
149                 if ((n < 0) || !res_dnok(bp))
150                         break;
151                 cp += n;
152                 ans[0] = '\0';
153                 strncpy(&ans[0], bp, sizeof(ans) - 1);
154                 ans[sizeof(ans) - 1] = '\0';
155                 GETSHORT(type, cp);
156                 GETSHORT(class, cp);
157                 cp += INT32SZ;          /* TTL */
158                 GETSHORT(n, cp);
159                 if (class == C_IN && type == T_PTR) {
160                         n = dn_expand(answer->buf, eom, cp, bp, buflen);
161                         if ((n < 0) || !res_hnok(bp)) {
162                                 cp += n;
163                                 return (NULL);
164                         }
165                         cp += n; 
166                         *ap++ = bp;
167                         n = strlen(bp) + 1;
168                         bp += n;
169                         buflen -= n;
170                         net_entry.n_addrtype =
171                                 (class == C_IN) ? AF_INET : AF_UNSPEC;
172                         haveanswer++;
173                 }
174         }
175         if (haveanswer) {
176                 *ap = NULL;
177                 switch (net_i) {
178                 case BYADDR:
179                         net_entry.n_name = *net_entry.n_aliases;
180                         net_entry.n_net = 0L;
181                         break;
182                 case BYNAME:
183                         in = *net_entry.n_aliases;
184                         net_entry.n_name = &ans[0];
185                         aux2[0] = '\0';
186                         for (i = 0; i < 4; i++) {
187                                 for (st = in, nchar = 0;
188                                      *st != '.';
189                                      st++, nchar++)
190                                         ;
191                                 if (nchar != 1 || *in != '0' || flag) {
192                                         flag = 1;
193                                         strncpy(paux1, (i==0) ? in : in-1,
194                                                 (i==0) ? nchar : nchar+1);
195                                         paux1[(i==0) ? nchar : nchar+1] = '\0';
196                                         pauxt = paux2;
197                                         paux2 = strcat(paux1, paux2);
198                                         paux1 = pauxt;
199                                 }
200                                 in = ++st;
201                         }                 
202                         net_entry.n_net = inet_network(paux2);
203                         break;
204                 }
205                 net_entry.n_aliases++;
206                 return (&net_entry);
207         }
208         h_errno = TRY_AGAIN;
209         return (NULL);
210 }
211
212 int
213 _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap)
214 {
215         unsigned int netbr[4];
216         int nn, anslen, net_type;
217         querybuf *buf;
218         char qbuf[MAXDNAME];
219         unsigned long net, net2;
220         struct netent *net_entry;
221
222         net = va_arg(ap, unsigned long);
223         net_type = va_arg(ap, int);
224
225         *(struct netent **)rval = NULL;
226
227         if (net_type != AF_INET)
228                 return NS_UNAVAIL;
229
230         for (nn = 4, net2 = net; net2; net2 >>= 8)
231                 netbr[--nn] = net2 & 0xff;
232         switch (nn) {
233         case 3:         /* Class A */
234                 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
235                 break;
236         case 2:         /* Class B */
237                 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
238                 break;
239         case 1:         /* Class C */
240                 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
241                     netbr[1]);
242                 break;
243         case 0:         /* Class D - E */
244                 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
245                     netbr[1], netbr[0]);
246                 break;
247         }
248         if ((buf = malloc(sizeof(*buf))) == NULL) {
249                 h_errno = NETDB_INTERNAL;
250                 return NS_NOTFOUND;
251         }
252         anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf));
253         if (anslen < 0) {
254                 free(buf);
255 #ifdef DEBUG
256                 if (_res.options & RES_DEBUG)
257                         printf("res_search failed\n");
258 #endif
259                 return NS_UNAVAIL;
260         } else if (anslen > sizeof(*buf)) {
261                 free(buf);
262 #ifdef DEBUG
263                 if (_res.options & RES_DEBUG)
264                         printf("res_search static buffer too small\n");
265 #endif
266                 return NS_UNAVAIL;
267         }
268         net_entry = getnetanswer(buf, anslen, BYADDR);
269         free(buf);
270         if (net_entry) {
271                 unsigned u_net = net;   /* maybe net should be unsigned ? */
272
273                 /* Strip trailing zeros */
274                 while ((u_net & 0xff) == 0 && u_net != 0)
275                         u_net >>= 8;
276                 net_entry->n_net = u_net;
277                 *(struct netent **)rval = net_entry;
278                 return NS_SUCCESS;
279         }
280         return NS_NOTFOUND;
281 }
282
283 int
284 _dns_getnetbyname(void *rval, void *cb_data, va_list ap)
285 {
286         const char *net;
287         int anslen;
288         querybuf *buf;
289         char qbuf[MAXDNAME];
290         struct netent *net_entry;
291
292         net = va_arg(ap, const char *);
293
294         *(struct netent**)rval = NULL;
295
296         if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
297                 h_errno = NETDB_INTERNAL;
298                 return NS_UNAVAIL;
299         }
300         if ((buf = malloc(sizeof(*buf))) == NULL) {
301                 h_errno = NETDB_INTERNAL;
302                 return NS_NOTFOUND;
303         }
304         strncpy(qbuf, net, sizeof(qbuf) - 1);
305         qbuf[sizeof(qbuf) - 1] = '\0';
306         anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf));
307         if (anslen < 0) {
308                 free(buf);
309 #ifdef DEBUG
310                 if (_res.options & RES_DEBUG)
311                         printf("res_search failed\n");
312 #endif
313                 return NS_UNAVAIL;
314         } else if (anslen > sizeof(*buf)) {
315                 free(buf);
316 #ifdef DEBUG
317                 if (_res.options & RES_DEBUG)
318                         printf("res_search static buffer too small\n");
319 #endif
320                 return NS_UNAVAIL;
321         }
322         *(struct netent**)rval = getnetanswer(buf, anslen, BYNAME);
323         free(buf);
324         return (*(struct netent**)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
325 }
326
327 void
328 _setnetdnsent(int stayopen)
329 {
330         if (stayopen)
331                 _res.options |= RES_STAYOPEN | RES_USEVC;
332 }
333
334 void
335 _endnetdnsent(void)
336 {
337         _res.options &= ~(RES_STAYOPEN | RES_USEVC);
338         res_close();
339 }