2 Linux DNS client library implementation
4 Copyright (C) 2006 Krishna Ganugapati <krishnag@centeris.com>
5 Copyright (C) 2006 Gerald Carter <jerry@samba.org>
7 ** NOTE! The following LGPL license applies to the libaddns
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "../replace/replace.h"
29 #include "system/network.h"
31 /* make sure we have included the correct config.h */
32 #ifndef NO_CONFIG_H /* for some tests */
33 #ifndef CONFIG_H_IS_FROM_SAMBA
34 #error "make sure you have removed all config.h files from standalone builds!"
35 #error "the included config.h isn't from samba!"
37 #endif /* NO_CONFIG_H */
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
52 #ifdef HAVE_UUID_UUID_H
53 #include <uuid/uuid.h>
60 #ifdef HAVE_INTTYPES_H
68 #define uint16 uint16_t
76 #define uint32 uint32_t
84 #if HAVE_GSSAPI_GSSAPI_H
85 #include <gssapi/gssapi.h>
86 #elif HAVE_GSSAPI_GSSAPI_GENERIC_H
87 #include <gssapi/gssapi_generic.h>
92 #if defined(HAVE_GSSAPI_H) || defined(HAVE_GSSAPI_GSSAPI_H) || defined(HAVE_GSSAPI_GSSAPI_GENERIC_H)
93 #define HAVE_GSSAPI_SUPPORT 1
98 /*******************************************************************
99 Type definitions for int16, int32, uint16 and uint32. Needed
100 for Samba coding style
101 *******************************************************************/
104 # define uint8 unsigned char
107 #if !defined(int16) && !defined(HAVE_INT16_FROM_RPC_RPC_H)
108 # if (SIZEOF_SHORT == 4)
109 # define int16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16;
110 # else /* SIZEOF_SHORT != 4 */
112 # endif /* SIZEOF_SHORT != 4 */
113 /* needed to work around compile issue on HP-UX 11.x */
118 * Note we duplicate the size tests in the unsigned
119 * case as int16 may be a typedef from rpc/rpc.h
122 #if !defined(uint16) && !defined(HAVE_UINT16_FROM_RPC_RPC_H)
123 # if (SIZEOF_SHORT == 4)
124 # define uint16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16;
125 # else /* SIZEOF_SHORT != 4 */
126 # define uint16 unsigned short
127 # endif /* SIZEOF_SHORT != 4 */
130 #if !defined(int32) && !defined(HAVE_INT32_FROM_RPC_RPC_H)
131 # if (SIZEOF_INT == 4)
133 # elif (SIZEOF_LONG == 4)
135 # elif (SIZEOF_SHORT == 4)
138 /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */
142 /* needed to work around compile issue on HP-UX 11.x */
148 * Note we duplicate the size tests in the unsigned
149 * case as int32 may be a typedef from rpc/rpc.h
152 #if !defined(uint32) && !defined(HAVE_UINT32_FROM_RPC_RPC_H)
153 # if (SIZEOF_INT == 4)
154 # define uint32 unsigned int
155 # elif (SIZEOF_LONG == 4)
156 # define uint32 unsigned long
157 # elif (SIZEOF_SHORT == 4)
158 # define uint32 unsigned short
160 /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */
161 # define uint32 unsigned
166 * check for 8 byte long long
170 # if (SIZEOF_LONG == 8)
171 # define uint64 unsigned long
172 # elif (SIZEOF_LONG_LONG == 8)
173 # define uint64 unsigned long long
174 # endif /* don't lie. If we don't have it, then don't use it */
177 /* needed on Sun boxes */
179 #define INADDR_NONE 0xFFFFFFFF
188 #define DNS_OPCODE_UPDATE 1
190 /* DNS Class Types */
192 #define DNS_CLASS_IN 1
193 #define DNS_CLASS_ANY 255
194 #define DNS_CLASS_NONE 254
200 #define DNS_TCP_PORT 53
201 #define DNS_UDP_PORT 53
206 #define QTYPE_CNAME 5
208 #define QTYPE_AAAA 28
209 #define QTYPE_ANY 255
210 #define QTYPE_TKEY 249
211 #define QTYPE_TSIG 250
214 MF 4 a mail forwarder (Obsolete - use MX)
215 CNAME 5 the canonical name for an alias
216 SOA 6 marks the start of a zone of authority
217 MB 7 a mailbox domain name (EXPERIMENTAL)
218 MG 8 a mail group member (EXPERIMENTAL)
219 MR 9 a mail rename domain name (EXPERIMENTAL)
220 NULL 10 a null RR (EXPERIMENTAL)
221 WKS 11 a well known service description
222 PTR 12 a domain name pointer
223 HINFO 13 host information
224 MINFO 14 mailbox or mail list information
229 #define QR_QUERY 0x0000
230 #define QR_RESPONSE 0x0001
232 #define OPCODE_QUERY 0x00
233 #define OPCODE_IQUERY 0x01
234 #define OPCODE_STATUS 0x02
238 #define RECURSION_DESIRED 0x01
240 #define RCODE_NOERROR 0
241 #define RCODE_FORMATERROR 1
242 #define RCODE_SERVER_FAILURE 2
243 #define RCODE_NAME_ERROR 3
244 #define RCODE_NOTIMPLEMENTED 4
245 #define RCODE_REFUSED 5
247 #define SENDBUFFER_SIZE 65536
248 #define RECVBUFFER_SIZE 65536
251 * TKEY Modes from rfc2930
254 #define DNS_TKEY_MODE_SERVER 1
255 #define DNS_TKEY_MODE_DH 2
256 #define DNS_TKEY_MODE_GSSAPI 3
257 #define DNS_TKEY_MODE_RESOLVER 4
258 #define DNS_TKEY_MODE_DELETE 5
261 #define DNS_ONE_DAY_IN_SECS 86400
262 #define DNS_TEN_HOURS_IN_SECS 36000
264 #define SOCKET_ERROR -1
265 #define INVALID_SOCKET -1
267 #define DNS_NO_ERROR 0
268 #define DNS_FORMAT_ERROR 1
269 #define DNS_SERVER_FAILURE 2
270 #define DNS_NAME_ERROR 3
271 #define DNS_NOT_IMPLEMENTED 4
272 #define DNS_REFUSED 5
276 enum dns_ServerType
{ DNS_SRV_ANY
, DNS_SRV_WIN2000
, DNS_SRV_WIN2003
};
278 struct dns_domain_label
{
279 struct dns_domain_label
*next
;
284 struct dns_domain_name
{
285 struct dns_domain_label
*pLabelList
;
288 struct dns_question
{
289 struct dns_domain_name
*name
;
295 * Before changing the definition of dns_zone, look
296 * dns_marshall_update_request(), we rely on this being the same as
297 * dns_question right now.
301 struct dns_domain_name
*name
;
307 struct dns_domain_name
*name
;
315 struct dns_tkey_record
{
316 struct dns_domain_name
*algorithm
;
328 uint16 num_questions
;
331 uint16 num_additionals
;
332 struct dns_question
**questions
;
333 struct dns_rrec
**answers
;
334 struct dns_rrec
**auths
;
335 struct dns_rrec
**additionals
;
339 * Before changing the definition of dns_update_request, look
340 * dns_marshall_update_request(), we rely on this being the same as
341 * dns_request right now.
344 struct dns_update_request
{
350 uint16 num_additionals
;
351 struct dns_zone
**zones
;
352 struct dns_rrec
**preqs
;
353 struct dns_rrec
**updates
;
354 struct dns_rrec
**additionals
;
357 struct dns_connection
{
360 struct sockaddr RecvAddr
;
370 /* from dnsutils.c */
372 DNS_ERROR
dns_domain_name_from_string( TALLOC_CTX
*mem_ctx
,
373 const char *pszDomainName
,
374 struct dns_domain_name
**presult
);
375 char *dns_generate_keyname( TALLOC_CTX
*mem_ctx
);
377 /* from dnsrecord.c */
379 DNS_ERROR
dns_create_query( TALLOC_CTX
*mem_ctx
, const char *name
,
380 uint16 q_type
, uint16 q_class
,
381 struct dns_request
**preq
);
382 DNS_ERROR
dns_create_update( TALLOC_CTX
*mem_ctx
, const char *name
,
383 struct dns_update_request
**preq
);
384 DNS_ERROR
dns_create_probe(TALLOC_CTX
*mem_ctx
, const char *zone
,
385 const char *host
, int num_ips
,
386 const struct sockaddr_storage
*sslist
,
387 struct dns_update_request
**preq
);
388 DNS_ERROR
dns_create_rrec(TALLOC_CTX
*mem_ctx
, const char *name
,
389 uint16 type
, uint16 r_class
, uint32 ttl
,
390 uint16 data_length
, uint8
*data
,
391 struct dns_rrec
**prec
);
392 DNS_ERROR
dns_add_rrec(TALLOC_CTX
*mem_ctx
, struct dns_rrec
*rec
,
393 uint16
*num_records
, struct dns_rrec
***records
);
394 DNS_ERROR
dns_create_tkey_record(TALLOC_CTX
*mem_ctx
, const char *keyname
,
395 const char *algorithm_name
, time_t inception
,
396 time_t expiration
, uint16 mode
, uint16 error
,
397 uint16 key_length
, const uint8
*key
,
398 struct dns_rrec
**prec
);
399 DNS_ERROR
dns_create_name_in_use_record(TALLOC_CTX
*mem_ctx
,
401 const struct sockaddr_storage
*ip
,
402 struct dns_rrec
**prec
);
403 DNS_ERROR
dns_create_delete_record(TALLOC_CTX
*mem_ctx
, const char *name
,
404 uint16 type
, uint16 r_class
,
405 struct dns_rrec
**prec
);
406 DNS_ERROR
dns_create_name_not_in_use_record(TALLOC_CTX
*mem_ctx
,
407 const char *name
, uint32 type
,
408 struct dns_rrec
**prec
);
409 DNS_ERROR
dns_create_a_record(TALLOC_CTX
*mem_ctx
, const char *host
,
410 uint32 ttl
, const struct sockaddr_storage
*pss
,
411 struct dns_rrec
**prec
);
412 DNS_ERROR
dns_create_aaaa_record(TALLOC_CTX
*mem_ctx
, const char *host
,
413 uint32 ttl
, const struct sockaddr_storage
*pss
,
414 struct dns_rrec
**prec
);
415 DNS_ERROR
dns_unmarshall_tkey_record(TALLOC_CTX
*mem_ctx
, struct dns_rrec
*rec
,
416 struct dns_tkey_record
**ptkey
);
417 DNS_ERROR
dns_create_tsig_record(TALLOC_CTX
*mem_ctx
, const char *keyname
,
418 const char *algorithm_name
,
419 time_t time_signed
, uint16 fudge
,
420 uint16 mac_length
, const uint8
*mac
,
421 uint16 original_id
, uint16 error
,
422 struct dns_rrec
**prec
);
423 DNS_ERROR
dns_add_rrec(TALLOC_CTX
*mem_ctx
, struct dns_rrec
*rec
,
424 uint16
*num_records
, struct dns_rrec
***records
);
425 DNS_ERROR
dns_create_update_request(TALLOC_CTX
*mem_ctx
,
426 const char *domainname
,
427 const char *hostname
,
428 const struct sockaddr_storage
*ip_addr
,
430 struct dns_update_request
**preq
);
434 DNS_ERROR
dns_open_connection( const char *nameserver
, int32 dwType
,
436 struct dns_connection
**conn
);
437 DNS_ERROR
dns_send(struct dns_connection
*conn
, const struct dns_buffer
*buf
);
438 DNS_ERROR
dns_receive(TALLOC_CTX
*mem_ctx
, struct dns_connection
*conn
,
439 struct dns_buffer
**presult
);
440 DNS_ERROR
dns_transaction(TALLOC_CTX
*mem_ctx
, struct dns_connection
*conn
,
441 const struct dns_request
*req
,
442 struct dns_request
**resp
);
443 DNS_ERROR
dns_update_transaction(TALLOC_CTX
*mem_ctx
,
444 struct dns_connection
*conn
,
445 struct dns_update_request
*up_req
,
446 struct dns_update_request
**up_resp
);
448 /* from dnsmarshall.c */
450 struct dns_buffer
*dns_create_buffer(TALLOC_CTX
*mem_ctx
);
451 void dns_marshall_buffer(struct dns_buffer
*buf
, const uint8
*data
,
453 void dns_marshall_uint16(struct dns_buffer
*buf
, uint16 val
);
454 void dns_marshall_uint32(struct dns_buffer
*buf
, uint32 val
);
455 void dns_unmarshall_buffer(struct dns_buffer
*buf
, uint8
*data
,
457 void dns_unmarshall_uint16(struct dns_buffer
*buf
, uint16
*val
);
458 void dns_unmarshall_uint32(struct dns_buffer
*buf
, uint32
*val
);
459 void dns_unmarshall_domain_name(TALLOC_CTX
*mem_ctx
,
460 struct dns_buffer
*buf
,
461 struct dns_domain_name
**pname
);
462 void dns_marshall_domain_name(struct dns_buffer
*buf
,
463 const struct dns_domain_name
*name
);
464 void dns_unmarshall_domain_name(TALLOC_CTX
*mem_ctx
,
465 struct dns_buffer
*buf
,
466 struct dns_domain_name
**pname
);
467 DNS_ERROR
dns_marshall_request(TALLOC_CTX
*mem_ctx
,
468 const struct dns_request
*req
,
469 struct dns_buffer
**pbuf
);
470 DNS_ERROR
dns_unmarshall_request(TALLOC_CTX
*mem_ctx
,
471 struct dns_buffer
*buf
,
472 struct dns_request
**preq
);
473 DNS_ERROR
dns_marshall_update_request(TALLOC_CTX
*mem_ctx
,
474 struct dns_update_request
*update
,
475 struct dns_buffer
**pbuf
);
476 DNS_ERROR
dns_unmarshall_update_request(TALLOC_CTX
*mem_ctx
,
477 struct dns_buffer
*buf
,
478 struct dns_update_request
**pupreq
);
479 struct dns_request
*dns_update2request(struct dns_update_request
*update
);
480 struct dns_update_request
*dns_request2update(struct dns_request
*request
);
481 uint16
dns_response_code(uint16 flags
);
482 const char *dns_errstr(DNS_ERROR err
);
486 #ifdef HAVE_GSSAPI_SUPPORT
488 void display_status( const char *msg
, OM_uint32 maj_stat
, OM_uint32 min_stat
);
489 DNS_ERROR
dns_negotiate_sec_ctx( const char *target_realm
,
490 const char *servername
,
492 gss_ctx_id_t
*gss_ctx
,
493 enum dns_ServerType srv_type
);
494 DNS_ERROR
dns_sign_update(struct dns_update_request
*req
,
495 gss_ctx_id_t gss_ctx
,
497 const char *algorithmname
,
498 time_t time_signed
, uint16 fudge
);
500 #endif /* HAVE_GSSAPI_SUPPORT */