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
100 Disable these now we have checked all code paths
and ensured
101 NULL returns on zero request
. JRA
.
103 void *_talloc_zero_zeronull(const void *ctx
, size_t size
, const char *name
);
104 void *_talloc_memdup_zeronull(const void *t
, const void *p
, size_t size
, const char *name
);
105 void *_talloc_array_zeronull(const void *ctx
, size_t el_size
, unsigned count
, const char *name
);
106 void *_talloc_zero_array_zeronull(const void *ctx
, size_t el_size
, unsigned count
, const char *name
);
107 void *talloc_zeronull(const void *context
, size_t size
, const char *name
);
109 #define TALLOC(ctx, size) talloc_zeronull(ctx, size, __location__)
110 #define TALLOC_P(ctx, type) (type *)talloc_zeronull(ctx, sizeof(type), #type)
111 #define TALLOC_ARRAY(ctx, type, count) (type *)_talloc_array_zeronull(ctx, sizeof(type), count, #type)
112 #define TALLOC_MEMDUP(ctx, ptr, size) _talloc_memdup_zeronull(ctx, ptr, size, __location__)
113 #define TALLOC_ZERO(ctx, size) _talloc_zero_zeronull(ctx, size, __location__)
114 #define TALLOC_ZERO_P(ctx, type) (type *)_talloc_zero_zeronull(ctx, sizeof(type), #type)
115 #define TALLOC_ZERO_ARRAY(ctx, type, count) (type *)_talloc_zero_array_zeronull(ctx, sizeof(type), count, #type)
116 #define TALLOC_SIZE(ctx, size) talloc_zeronull(ctx, size, __location__)
117 #define TALLOC_ZERO_SIZE(ctx, size) _talloc_zero_zeronull(ctx, size, __location__)
121 #define TALLOC(ctx, size) talloc_named_const(ctx, size, __location__)
122 #define TALLOC_P(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
123 #define TALLOC_ARRAY(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
124 #define TALLOC_MEMDUP(ctx, ptr, size) _talloc_memdup(ctx, ptr, size, __location__)
125 #define TALLOC_ZERO(ctx, size) _talloc_zero(ctx, size, __location__)
126 #define TALLOC_ZERO_P(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
127 #define TALLOC_ZERO_ARRAY(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
128 #define TALLOC_SIZE(ctx, size) talloc_named_const(ctx, size, __location__)
129 #define TALLOC_ZERO_SIZE(ctx, size) _talloc_zero(ctx, size, __location__)
133 #define TALLOC_REALLOC(ctx, ptr, count) _talloc_realloc(ctx, ptr, count, __location__)
134 #define TALLOC_REALLOC_ARRAY(ctx, ptr, type, count) (type *)_talloc_realloc_array(ctx, ptr, sizeof(type), count, #type)
135 #define talloc_destroy(ctx) talloc_free(ctx)
137 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
140 /*******************************************************************
141 Type definitions for int16, int32, uint16 and uint32. Needed
142 for Samba coding style
143 *******************************************************************/
146 # define uint8 unsigned char
149 #if !defined(int16) && !defined(HAVE_INT16_FROM_RPC_RPC_H)
150 # if (SIZEOF_SHORT == 4)
151 # define int16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16;
152 # else /* SIZEOF_SHORT != 4 */
154 # endif /* SIZEOF_SHORT != 4 */
155 /* needed to work around compile issue on HP-UX 11.x */
160 * Note we duplicate the size tests in the unsigned
161 * case as int16 may be a typedef from rpc/rpc.h
164 #if !defined(uint16) && !defined(HAVE_UINT16_FROM_RPC_RPC_H)
165 # if (SIZEOF_SHORT == 4)
166 # define uint16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16;
167 # else /* SIZEOF_SHORT != 4 */
168 # define uint16 unsigned short
169 # endif /* SIZEOF_SHORT != 4 */
172 #if !defined(int32) && !defined(HAVE_INT32_FROM_RPC_RPC_H)
173 # if (SIZEOF_INT == 4)
175 # elif (SIZEOF_LONG == 4)
177 # elif (SIZEOF_SHORT == 4)
180 /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */
184 /* needed to work around compile issue on HP-UX 11.x */
190 * Note we duplicate the size tests in the unsigned
191 * case as int32 may be a typedef from rpc/rpc.h
194 #if !defined(uint32) && !defined(HAVE_UINT32_FROM_RPC_RPC_H)
195 # if (SIZEOF_INT == 4)
196 # define uint32 unsigned int
197 # elif (SIZEOF_LONG == 4)
198 # define uint32 unsigned long
199 # elif (SIZEOF_SHORT == 4)
200 # define uint32 unsigned short
202 /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */
203 # define uint32 unsigned
208 * check for 8 byte long long
212 # if (SIZEOF_LONG == 8)
213 # define uint64 unsigned long
214 # elif (SIZEOF_LONG_LONG == 8)
215 # define uint64 unsigned long long
216 # endif /* don't lie. If we don't have it, then don't use it */
219 /* needed on Sun boxes */
221 #define INADDR_NONE 0xFFFFFFFF
230 #define DNS_OPCODE_UPDATE 1
232 /* DNS Class Types */
234 #define DNS_CLASS_IN 1
235 #define DNS_CLASS_ANY 255
236 #define DNS_CLASS_NONE 254
242 #define DNS_TCP_PORT 53
243 #define DNS_UDP_PORT 53
248 #define QTYPE_CNAME 5
250 #define QTYPE_ANY 255
251 #define QTYPE_TKEY 249
252 #define QTYPE_TSIG 250
255 MF 4 a mail forwarder (Obsolete - use MX)
256 CNAME 5 the canonical name for an alias
257 SOA 6 marks the start of a zone of authority
258 MB 7 a mailbox domain name (EXPERIMENTAL)
259 MG 8 a mail group member (EXPERIMENTAL)
260 MR 9 a mail rename domain name (EXPERIMENTAL)
261 NULL 10 a null RR (EXPERIMENTAL)
262 WKS 11 a well known service description
263 PTR 12 a domain name pointer
264 HINFO 13 host information
265 MINFO 14 mailbox or mail list information
270 #define QR_QUERY 0x0000
271 #define QR_RESPONSE 0x0001
273 #define OPCODE_QUERY 0x00
274 #define OPCODE_IQUERY 0x01
275 #define OPCODE_STATUS 0x02
279 #define RECURSION_DESIRED 0x01
281 #define RCODE_NOERROR 0
282 #define RCODE_FORMATERROR 1
283 #define RCODE_SERVER_FAILURE 2
284 #define RCODE_NAME_ERROR 3
285 #define RCODE_NOTIMPLEMENTED 4
286 #define RCODE_REFUSED 5
288 #define SENDBUFFER_SIZE 65536
289 #define RECVBUFFER_SIZE 65536
292 * TKEY Modes from rfc2930
295 #define DNS_TKEY_MODE_SERVER 1
296 #define DNS_TKEY_MODE_DH 2
297 #define DNS_TKEY_MODE_GSSAPI 3
298 #define DNS_TKEY_MODE_RESOLVER 4
299 #define DNS_TKEY_MODE_DELETE 5
302 #define DNS_ONE_DAY_IN_SECS 86400
303 #define DNS_TEN_HOURS_IN_SECS 36000
305 #define SOCKET_ERROR -1
306 #define INVALID_SOCKET -1
308 #define DNS_NO_ERROR 0
309 #define DNS_FORMAT_ERROR 1
310 #define DNS_SERVER_FAILURE 2
311 #define DNS_NAME_ERROR 3
312 #define DNS_NOT_IMPLEMENTED 4
313 #define DNS_REFUSED 5
317 enum dns_ServerType
{ DNS_SRV_ANY
, DNS_SRV_WIN2000
, DNS_SRV_WIN2003
};
319 struct dns_domain_label
{
320 struct dns_domain_label
*next
;
325 struct dns_domain_name
{
326 struct dns_domain_label
*pLabelList
;
329 struct dns_question
{
330 struct dns_domain_name
*name
;
336 * Before changing the definition of dns_zone, look
337 * dns_marshall_update_request(), we rely on this being the same as
338 * dns_question right now.
342 struct dns_domain_name
*name
;
348 struct dns_domain_name
*name
;
356 struct dns_tkey_record
{
357 struct dns_domain_name
*algorithm
;
369 uint16 num_questions
;
372 uint16 num_additionals
;
373 struct dns_question
**questions
;
374 struct dns_rrec
**answers
;
375 struct dns_rrec
**auths
;
376 struct dns_rrec
**additionals
;
380 * Before changing the definition of dns_update_request, look
381 * dns_marshall_update_request(), we rely on this being the same as
382 * dns_request right now.
385 struct dns_update_request
{
391 uint16 num_additionals
;
392 struct dns_zone
**zones
;
393 struct dns_rrec
**preqs
;
394 struct dns_rrec
**updates
;
395 struct dns_rrec
**additionals
;
398 struct dns_connection
{
401 struct sockaddr RecvAddr
;
411 /* from dnsutils.c */
413 DNS_ERROR
dns_domain_name_from_string( TALLOC_CTX
*mem_ctx
,
414 const char *pszDomainName
,
415 struct dns_domain_name
**presult
);
416 char *dns_generate_keyname( TALLOC_CTX
*mem_ctx
);
418 /* from dnsrecord.c */
420 DNS_ERROR
dns_create_query( TALLOC_CTX
*mem_ctx
, const char *name
,
421 uint16 q_type
, uint16 q_class
,
422 struct dns_request
**preq
);
423 DNS_ERROR
dns_create_update( TALLOC_CTX
*mem_ctx
, const char *name
,
424 struct dns_update_request
**preq
);
425 DNS_ERROR
dns_create_probe(TALLOC_CTX
*mem_ctx
, const char *zone
,
426 const char *host
, int num_ips
,
427 const struct sockaddr_storage
*sslist
,
428 struct dns_update_request
**preq
);
429 DNS_ERROR
dns_create_rrec(TALLOC_CTX
*mem_ctx
, const char *name
,
430 uint16 type
, uint16 r_class
, uint32 ttl
,
431 uint16 data_length
, uint8
*data
,
432 struct dns_rrec
**prec
);
433 DNS_ERROR
dns_add_rrec(TALLOC_CTX
*mem_ctx
, struct dns_rrec
*rec
,
434 uint16
*num_records
, struct dns_rrec
***records
);
435 DNS_ERROR
dns_create_tkey_record(TALLOC_CTX
*mem_ctx
, const char *keyname
,
436 const char *algorithm_name
, time_t inception
,
437 time_t expiration
, uint16 mode
, uint16 error
,
438 uint16 key_length
, const uint8
*key
,
439 struct dns_rrec
**prec
);
440 DNS_ERROR
dns_create_name_in_use_record(TALLOC_CTX
*mem_ctx
,
442 const struct sockaddr_storage
*ip
,
443 struct dns_rrec
**prec
);
444 DNS_ERROR
dns_create_delete_record(TALLOC_CTX
*mem_ctx
, const char *name
,
445 uint16 type
, uint16 r_class
,
446 struct dns_rrec
**prec
);
447 DNS_ERROR
dns_create_name_not_in_use_record(TALLOC_CTX
*mem_ctx
,
448 const char *name
, uint32 type
,
449 struct dns_rrec
**prec
);
450 DNS_ERROR
dns_create_a_record(TALLOC_CTX
*mem_ctx
, const char *host
,
451 uint32 ttl
, const struct sockaddr_storage
*pss
,
452 struct dns_rrec
**prec
);
453 DNS_ERROR
dns_unmarshall_tkey_record(TALLOC_CTX
*mem_ctx
, struct dns_rrec
*rec
,
454 struct dns_tkey_record
**ptkey
);
455 DNS_ERROR
dns_create_tsig_record(TALLOC_CTX
*mem_ctx
, const char *keyname
,
456 const char *algorithm_name
,
457 time_t time_signed
, uint16 fudge
,
458 uint16 mac_length
, const uint8
*mac
,
459 uint16 original_id
, uint16 error
,
460 struct dns_rrec
**prec
);
461 DNS_ERROR
dns_add_rrec(TALLOC_CTX
*mem_ctx
, struct dns_rrec
*rec
,
462 uint16
*num_records
, struct dns_rrec
***records
);
463 DNS_ERROR
dns_create_update_request(TALLOC_CTX
*mem_ctx
,
464 const char *domainname
,
465 const char *hostname
,
466 const struct sockaddr_storage
*ip_addr
,
468 struct dns_update_request
**preq
);
472 DNS_ERROR
dns_open_connection( const char *nameserver
, int32 dwType
,
474 struct dns_connection
**conn
);
475 DNS_ERROR
dns_send(struct dns_connection
*conn
, const struct dns_buffer
*buf
);
476 DNS_ERROR
dns_receive(TALLOC_CTX
*mem_ctx
, struct dns_connection
*conn
,
477 struct dns_buffer
**presult
);
478 DNS_ERROR
dns_transaction(TALLOC_CTX
*mem_ctx
, struct dns_connection
*conn
,
479 const struct dns_request
*req
,
480 struct dns_request
**resp
);
481 DNS_ERROR
dns_update_transaction(TALLOC_CTX
*mem_ctx
,
482 struct dns_connection
*conn
,
483 struct dns_update_request
*up_req
,
484 struct dns_update_request
**up_resp
);
486 /* from dnsmarshall.c */
488 struct dns_buffer
*dns_create_buffer(TALLOC_CTX
*mem_ctx
);
489 void dns_marshall_buffer(struct dns_buffer
*buf
, const uint8
*data
,
491 void dns_marshall_uint16(struct dns_buffer
*buf
, uint16 val
);
492 void dns_marshall_uint32(struct dns_buffer
*buf
, uint32 val
);
493 void dns_unmarshall_buffer(struct dns_buffer
*buf
, uint8
*data
,
495 void dns_unmarshall_uint16(struct dns_buffer
*buf
, uint16
*val
);
496 void dns_unmarshall_uint32(struct dns_buffer
*buf
, uint32
*val
);
497 void dns_unmarshall_domain_name(TALLOC_CTX
*mem_ctx
,
498 struct dns_buffer
*buf
,
499 struct dns_domain_name
**pname
);
500 void dns_marshall_domain_name(struct dns_buffer
*buf
,
501 const struct dns_domain_name
*name
);
502 void dns_unmarshall_domain_name(TALLOC_CTX
*mem_ctx
,
503 struct dns_buffer
*buf
,
504 struct dns_domain_name
**pname
);
505 DNS_ERROR
dns_marshall_request(TALLOC_CTX
*mem_ctx
,
506 const struct dns_request
*req
,
507 struct dns_buffer
**pbuf
);
508 DNS_ERROR
dns_unmarshall_request(TALLOC_CTX
*mem_ctx
,
509 struct dns_buffer
*buf
,
510 struct dns_request
**preq
);
511 DNS_ERROR
dns_marshall_update_request(TALLOC_CTX
*mem_ctx
,
512 struct dns_update_request
*update
,
513 struct dns_buffer
**pbuf
);
514 DNS_ERROR
dns_unmarshall_update_request(TALLOC_CTX
*mem_ctx
,
515 struct dns_buffer
*buf
,
516 struct dns_update_request
**pupreq
);
517 struct dns_request
*dns_update2request(struct dns_update_request
*update
);
518 struct dns_update_request
*dns_request2update(struct dns_request
*request
);
519 uint16
dns_response_code(uint16 flags
);
523 #ifdef HAVE_GSSAPI_SUPPORT
525 void display_status( const char *msg
, OM_uint32 maj_stat
, OM_uint32 min_stat
);
526 DNS_ERROR
dns_negotiate_sec_ctx( const char *target_realm
,
527 const char *servername
,
529 gss_ctx_id_t
*gss_ctx
,
530 enum dns_ServerType srv_type
);
531 DNS_ERROR
dns_sign_update(struct dns_update_request
*req
,
532 gss_ctx_id_t gss_ctx
,
534 const char *algorithmname
,
535 time_t time_signed
, uint16 fudge
);
537 #endif /* HAVE_GSSAPI_SUPPORT */