1 | /* |
---|
2 | * Copyright (c) 1995-1999 by Internet Software Consortium. |
---|
3 | * |
---|
4 | * Permission to use, copy, modify, and distribute this software for any |
---|
5 | * purpose with or without fee is hereby granted, provided that the above |
---|
6 | * copyright notice and this permission notice appear in all copies. |
---|
7 | * |
---|
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
---|
9 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
---|
10 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
---|
11 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
---|
12 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
---|
13 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
---|
14 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
---|
15 | * SOFTWARE. |
---|
16 | */ |
---|
17 | |
---|
18 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
19 | static const char rcsid[] = "$BINDId: res_data.c,v 8.17 1999/10/13 17:11:31 vixie Exp $"; |
---|
20 | #endif /* LIBC_SCCS and not lint */ |
---|
21 | |
---|
22 | #include <sys/types.h> |
---|
23 | #include <sys/param.h> |
---|
24 | #include <sys/socket.h> |
---|
25 | #include <sys/time.h> |
---|
26 | |
---|
27 | #include <netinet/in.h> |
---|
28 | #include <arpa/inet.h> |
---|
29 | #include <arpa/nameser.h> |
---|
30 | |
---|
31 | #include <ctype.h> |
---|
32 | #include <netdb.h> |
---|
33 | #include <resolv.h> |
---|
34 | #ifdef BIND_UPDATE |
---|
35 | #include <res_update.h> |
---|
36 | #endif |
---|
37 | #include <stdio.h> |
---|
38 | #include <stdlib.h> |
---|
39 | #include <string.h> |
---|
40 | #include <unistd.h> |
---|
41 | #include "libc-symbols.h" |
---|
42 | |
---|
43 | const char *_res_opcodes[] = { |
---|
44 | "QUERY", |
---|
45 | "IQUERY", |
---|
46 | "CQUERYM", |
---|
47 | "CQUERYU", /* experimental */ |
---|
48 | "NOTIFY", /* experimental */ |
---|
49 | "UPDATE", |
---|
50 | "6", |
---|
51 | "7", |
---|
52 | "8", |
---|
53 | "9", |
---|
54 | "10", |
---|
55 | "11", |
---|
56 | "12", |
---|
57 | "13", |
---|
58 | "ZONEINIT", |
---|
59 | "ZONEREF", |
---|
60 | }; |
---|
61 | libresolv_hidden_data_def (_res_opcodes) |
---|
62 | |
---|
63 | #ifdef BIND_UPDATE |
---|
64 | const char *_res_sectioncodes[] attribute_hidden = { |
---|
65 | "ZONE", |
---|
66 | "PREREQUISITES", |
---|
67 | "UPDATE", |
---|
68 | "ADDITIONAL", |
---|
69 | }; |
---|
70 | #endif |
---|
71 | |
---|
72 | #ifndef __BIND_NOSTATIC |
---|
73 | #ifdef _LIBC |
---|
74 | /* The definition has been moved to res_libc.c. */ |
---|
75 | #else |
---|
76 | #undef _res |
---|
77 | struct __res_state _res |
---|
78 | # if defined(__BIND_RES_TEXT) |
---|
79 | = { RES_TIMEOUT, } /* Motorola, et al. */ |
---|
80 | # endif |
---|
81 | ; |
---|
82 | #endif |
---|
83 | |
---|
84 | /* Proto. */ |
---|
85 | #ifndef _LIBC |
---|
86 | int res_ourserver_p(const res_state, const struct sockaddr_in *); |
---|
87 | void res_pquery(const res_state, const u_char *, int, FILE *); |
---|
88 | #endif |
---|
89 | |
---|
90 | #ifndef _LIBC |
---|
91 | /* Moved to res_libc.c since res_init() should go into libc.so but the |
---|
92 | rest of this file not. */ |
---|
93 | int |
---|
94 | res_init(void) { |
---|
95 | extern int __res_vinit(res_state, int); |
---|
96 | |
---|
97 | /* |
---|
98 | * These three fields used to be statically initialized. This made |
---|
99 | * it hard to use this code in a shared library. It is necessary, |
---|
100 | * now that we're doing dynamic initialization here, that we preserve |
---|
101 | * the old semantics: if an application modifies one of these three |
---|
102 | * fields of _res before res_init() is called, res_init() will not |
---|
103 | * alter them. Of course, if an application is setting them to |
---|
104 | * _zero_ before calling res_init(), hoping to override what used |
---|
105 | * to be the static default, we can't detect it and unexpected results |
---|
106 | * will follow. Zero for any of these fields would make no sense, |
---|
107 | * so one can safely assume that the applications were already getting |
---|
108 | * unexpected results. |
---|
109 | * |
---|
110 | * _res.options is tricky since some apps were known to diddle the bits |
---|
111 | * before res_init() was first called. We can't replicate that semantic |
---|
112 | * with dynamic initialization (they may have turned bits off that are |
---|
113 | * set in RES_DEFAULT). Our solution is to declare such applications |
---|
114 | * "broken". They could fool us by setting RES_INIT but none do (yet). |
---|
115 | */ |
---|
116 | if (!_res.retrans) |
---|
117 | _res.retrans = RES_TIMEOUT; |
---|
118 | if (!_res.retry) |
---|
119 | _res.retry = 4; |
---|
120 | if (!(_res.options & RES_INIT)) |
---|
121 | _res.options = RES_DEFAULT; |
---|
122 | |
---|
123 | /* |
---|
124 | * This one used to initialize implicitly to zero, so unless the app |
---|
125 | * has set it to something in particular, we can randomize it now. |
---|
126 | */ |
---|
127 | if (!_res.id) |
---|
128 | _res.id = res_randomid(); |
---|
129 | |
---|
130 | return (__res_vinit(&_res, 1)); |
---|
131 | } |
---|
132 | #endif |
---|
133 | |
---|
134 | void |
---|
135 | p_query(const u_char *msg) { |
---|
136 | fp_query(msg, stdout); |
---|
137 | } |
---|
138 | |
---|
139 | void |
---|
140 | fp_query(const u_char *msg, FILE *file) { |
---|
141 | fp_nquery(msg, PACKETSZ, file); |
---|
142 | } |
---|
143 | libresolv_hidden_def (fp_query) |
---|
144 | |
---|
145 | void |
---|
146 | fp_nquery(const u_char *msg, int len, FILE *file) { |
---|
147 | if (__res_maybe_init (&_res, 0) == -1) |
---|
148 | return; |
---|
149 | |
---|
150 | res_pquery(&_res, msg, len, file); |
---|
151 | } |
---|
152 | libresolv_hidden_def (fp_nquery) |
---|
153 | |
---|
154 | int |
---|
155 | res_mkquery(int op, /* opcode of query */ |
---|
156 | const char *dname, /* domain name */ |
---|
157 | int class, int type, /* class and type of query */ |
---|
158 | const u_char *data, /* resource record data */ |
---|
159 | int datalen, /* length of data */ |
---|
160 | const u_char *newrr_in, /* new rr for modify or append */ |
---|
161 | u_char *buf, /* buffer to put query */ |
---|
162 | int buflen) /* size of buffer */ |
---|
163 | { |
---|
164 | if (__res_maybe_init (&_res, 1) == -1) { |
---|
165 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
166 | return (-1); |
---|
167 | } |
---|
168 | return (res_nmkquery(&_res, op, dname, class, type, |
---|
169 | data, datalen, |
---|
170 | newrr_in, buf, buflen)); |
---|
171 | } |
---|
172 | |
---|
173 | #ifdef BIND_UPDATE |
---|
174 | int |
---|
175 | res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) { |
---|
176 | if (__res_maybe_init (&_res, 1) == -1) { |
---|
177 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
178 | return (-1); |
---|
179 | } |
---|
180 | |
---|
181 | return (res_nmkupdate(&_res, rrecp_in, buf, buflen)); |
---|
182 | } |
---|
183 | #endif |
---|
184 | |
---|
185 | int |
---|
186 | res_query(const char *name, /* domain name */ |
---|
187 | int class, int type, /* class and type of query */ |
---|
188 | u_char *answer, /* buffer to put answer */ |
---|
189 | int anslen) /* size of answer buffer */ |
---|
190 | { |
---|
191 | if (__res_maybe_init (&_res, 1) == -1) { |
---|
192 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
193 | return (-1); |
---|
194 | } |
---|
195 | return (res_nquery(&_res, name, class, type, answer, anslen)); |
---|
196 | } |
---|
197 | |
---|
198 | void |
---|
199 | res_send_setqhook(res_send_qhook hook) { |
---|
200 | _res.qhook = hook; |
---|
201 | } |
---|
202 | |
---|
203 | void |
---|
204 | res_send_setrhook(res_send_rhook hook) { |
---|
205 | _res.rhook = hook; |
---|
206 | } |
---|
207 | |
---|
208 | int |
---|
209 | res_isourserver(const struct sockaddr_in *inp) { |
---|
210 | return (res_ourserver_p(&_res, (const struct sockaddr_in6 *) inp)); |
---|
211 | } |
---|
212 | |
---|
213 | int |
---|
214 | res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) { |
---|
215 | if (__res_maybe_init (&_res, 1) == -1) { |
---|
216 | /* errno should have been set by res_init() in this case. */ |
---|
217 | return (-1); |
---|
218 | } |
---|
219 | |
---|
220 | return (res_nsend(&_res, buf, buflen, ans, anssiz)); |
---|
221 | } |
---|
222 | |
---|
223 | #ifndef _LIBC |
---|
224 | int |
---|
225 | res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key, |
---|
226 | u_char *ans, int anssiz) |
---|
227 | { |
---|
228 | if (__res_maybe_init (&_res, 1) == -1) { |
---|
229 | /* errno should have been set by res_init() in this case. */ |
---|
230 | return (-1); |
---|
231 | } |
---|
232 | |
---|
233 | return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz)); |
---|
234 | } |
---|
235 | #endif |
---|
236 | |
---|
237 | void |
---|
238 | res_close(void) { |
---|
239 | #ifdef _LIBC |
---|
240 | /* |
---|
241 | * Some stupid programs out there call res_close() before res_init(). |
---|
242 | * Since _res._vcsock isn't explicitly initialized, these means that |
---|
243 | * we could do a close(0), which might lead to some security problems. |
---|
244 | * Therefore we check if res_init() was called before by looking at |
---|
245 | * the RES_INIT bit in _res.options. If it hasn't been set we bail out |
---|
246 | * early. */ |
---|
247 | if ((_res.options & RES_INIT) == 0) |
---|
248 | return; |
---|
249 | #endif |
---|
250 | res_nclose(&_res); |
---|
251 | } |
---|
252 | |
---|
253 | #ifdef BIND_UPDATE |
---|
254 | int |
---|
255 | res_update(ns_updrec *rrecp_in) { |
---|
256 | if (__res_maybe_init (&_res, 1) == -1) { |
---|
257 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
258 | return (-1); |
---|
259 | } |
---|
260 | |
---|
261 | return (res_nupdate(&_res, rrecp_in, NULL)); |
---|
262 | } |
---|
263 | #endif |
---|
264 | |
---|
265 | int |
---|
266 | res_search(const char *name, /* domain name */ |
---|
267 | int class, int type, /* class and type of query */ |
---|
268 | u_char *answer, /* buffer to put answer */ |
---|
269 | int anslen) /* size of answer */ |
---|
270 | { |
---|
271 | if (__res_maybe_init (&_res, 1) == -1) { |
---|
272 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
273 | return (-1); |
---|
274 | } |
---|
275 | |
---|
276 | return (res_nsearch(&_res, name, class, type, answer, anslen)); |
---|
277 | } |
---|
278 | |
---|
279 | int |
---|
280 | res_querydomain(const char *name, |
---|
281 | const char *domain, |
---|
282 | int class, int type, /* class and type of query */ |
---|
283 | u_char *answer, /* buffer to put answer */ |
---|
284 | int anslen) /* size of answer */ |
---|
285 | { |
---|
286 | if (__res_maybe_init (&_res, 1) == -1) { |
---|
287 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
288 | return (-1); |
---|
289 | } |
---|
290 | |
---|
291 | return (res_nquerydomain(&_res, name, domain, |
---|
292 | class, type, |
---|
293 | answer, anslen)); |
---|
294 | } |
---|
295 | |
---|
296 | const char * |
---|
297 | hostalias(const char *name) { |
---|
298 | static char abuf[MAXDNAME]; |
---|
299 | |
---|
300 | return (res_hostalias(&_res, name, abuf, sizeof abuf)); |
---|
301 | } |
---|
302 | libresolv_hidden_def (hostalias) |
---|
303 | |
---|
304 | #ifdef ultrix |
---|
305 | int |
---|
306 | local_hostname_length(const char *hostname) { |
---|
307 | int len_host, len_domain; |
---|
308 | |
---|
309 | if (!*_res.defdname) |
---|
310 | res_init(); |
---|
311 | len_host = strlen(hostname); |
---|
312 | len_domain = strlen(_res.defdname); |
---|
313 | if (len_host > len_domain && |
---|
314 | !strcasecmp(hostname + len_host - len_domain, _res.defdname) && |
---|
315 | hostname[len_host - len_domain - 1] == '.') |
---|
316 | return (len_host - len_domain - 1); |
---|
317 | return (0); |
---|
318 | } |
---|
319 | #endif /*ultrix*/ |
---|
320 | |
---|
321 | #endif |
---|
322 | |
---|
323 | |
---|
324 | #include <shlib-compat.h> |
---|
325 | |
---|
326 | #if SHLIB_COMPAT(libresolv, GLIBC_2_0, GLIBC_2_2) |
---|
327 | # undef res_mkquery |
---|
328 | # undef res_query |
---|
329 | # undef res_querydomain |
---|
330 | # undef res_search |
---|
331 | weak_alias (__res_mkquery, res_mkquery); |
---|
332 | weak_alias (__res_query, res_query); |
---|
333 | weak_alias (__res_querydomain, res_querydomain); |
---|
334 | weak_alias (__res_search, res_search); |
---|
335 | #endif |
---|