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/>.
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
43 #ifdef HAVE_UUID_UUID_H
44 #include <uuid/uuid.h>
51 #ifdef HAVE_INTTYPES_H
59 #define uint16 uint16_t
67 #define uint32 uint32_t
77 #elif HAVE_GSSAPI_GSSAPI_H
78 #include <gssapi/gssapi.h>
79 #elif HAVE_GSSAPI_GSSAPI_GENERIC_H
80 #include <gssapi/gssapi_generic.h>
83 #if defined(HAVE_GSSAPI_H) || defined(HAVE_GSSAPI_GSSAPI_H) || defined(HAVE_GSSAPI_GSSAPI_GENERIC_H)
84 #define HAVE_GSSAPI_SUPPORT 1
91 Disable these now we have checked all code paths and ensured
92 NULL returns on zero request. JRA.
94 void *_talloc_zero_zeronull(const void *ctx, size_t size, const char *name);
95 void *_talloc_memdup_zeronull(const void *t, const void *p, size_t size, const char *name);
96 void *_talloc_array_zeronull(const void *ctx, size_t el_size, unsigned count, const char *name);
97 void *_talloc_zero_array_zeronull(const void *ctx, size_t el_size, unsigned count, const char *name);
98 void *talloc_zeronull(const void *context, size_t size, const char *name);
100 #define TALLOC(ctx, size) talloc_zeronull(ctx, size, __location__)
101 #define TALLOC_P(ctx, type) (type *)talloc_zeronull(ctx, sizeof(type), #type)
102 #define TALLOC_ARRAY(ctx, type, count) (type *)_talloc_array_zeronull(ctx, sizeof(type), count, #type)
103 #define TALLOC_MEMDUP(ctx, ptr, size) _talloc_memdup_zeronull(ctx, ptr, size, __location__)
104 #define TALLOC_ZERO(ctx, size) _talloc_zero_zeronull(ctx, size, __location__)
105 #define TALLOC_ZERO_P(ctx, type) (type *)_talloc_zero_zeronull(ctx, sizeof(type), #type)
106 #define TALLOC_ZERO_ARRAY(ctx, type, count) (type *)_talloc_zero_array_zeronull(ctx, sizeof(type), count, #type)
107 #define TALLOC_SIZE(ctx, size) talloc_zeronull(ctx, size, __location__)
108 #define TALLOC_ZERO_SIZE(ctx, size) _talloc_zero_zeronull(ctx, size, __location__)
112 #define TALLOC(ctx, size) talloc_named_const(ctx, size, __location__)
113 #define TALLOC_P(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
114 #define TALLOC_ARRAY(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
115 #define TALLOC_MEMDUP(ctx, ptr, size) _talloc_memdup(ctx, ptr, size, __location__)
116 #define TALLOC_ZERO(ctx, size) _talloc_zero(ctx, size, __location__)
117 #define TALLOC_ZERO_P(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
118 #define TALLOC_ZERO_ARRAY(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
119 #define TALLOC_SIZE(ctx, size) talloc_named_const(ctx, size, __location__)
120 #define TALLOC_ZERO_SIZE(ctx, size) _talloc_zero(ctx, size, __location__)
124 #define TALLOC_REALLOC(ctx, ptr, count) _talloc_realloc(ctx, ptr, count, __location__)
125 #define TALLOC_REALLOC_ARRAY(ctx, ptr, type, count) (type *)_talloc_realloc_array(ctx, ptr, sizeof(type), count, #type)
126 #define talloc_destroy(ctx) talloc_free(ctx)
127 #define TALLOC_FREE(ctx) do { if ((ctx) != NULL) {talloc_free(ctx); ctx=NULL;} } while(0)
129 /*******************************************************************
130 Type definitions for int16, int32, uint16 and uint32. Needed
131 for Samba coding style
132 *******************************************************************/
135 # define uint8 unsigned char
138 #if !defined(int16) && !defined(HAVE_INT16_FROM_RPC_RPC_H)
139 # if (SIZEOF_SHORT == 4)
140 # define int16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16;
141 # else /* SIZEOF_SHORT != 4 */
143 # endif /* SIZEOF_SHORT != 4 */
144 /* needed to work around compile issue on HP-UX 11.x */
149 * Note we duplicate the size tests in the unsigned
150 * case as int16 may be a typedef from rpc/rpc.h
153 #if !defined(uint16) && !defined(HAVE_UINT16_FROM_RPC_RPC_H)
154 # if (SIZEOF_SHORT == 4)
155 # define uint16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16;
156 # else /* SIZEOF_SHORT != 4 */
157 # define uint16 unsigned short
158 # endif /* SIZEOF_SHORT != 4 */
161 #if !defined(int32) && !defined(HAVE_INT32_FROM_RPC_RPC_H)
162 # if (SIZEOF_INT == 4)
164 # elif (SIZEOF_LONG == 4)
166 # elif (SIZEOF_SHORT == 4)
169 /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */
172 /* needed to work around compile issue on HP-UX 11.x */
177 * Note we duplicate the size tests in the unsigned
178 * case as int32 may be a typedef from rpc/rpc.h
181 #if !defined(uint32) && !defined(HAVE_UINT32_FROM_RPC_RPC_H)
182 # if (SIZEOF_INT == 4)
183 # define uint32 unsigned int
184 # elif (SIZEOF_LONG == 4)
185 # define uint32 unsigned long
186 # elif (SIZEOF_SHORT == 4)
187 # define uint32 unsigned short
189 /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */
190 # define uint32 unsigned
195 * check for 8 byte long long
199 # if (SIZEOF_LONG == 8)
200 # define uint64 unsigned long
201 # elif (SIZEOF_LONG_LONG == 8)
202 # define uint64 unsigned long long
203 # endif /* don't lie. If we don't have it, then don't use it */
206 /* needed on Sun boxes */
208 #define INADDR_NONE 0xFFFFFFFF
217 #define DNS_OPCODE_UPDATE 1
219 /* DNS Class Types */
221 #define DNS_CLASS_IN 1
222 #define DNS_CLASS_ANY 255
223 #define DNS_CLASS_NONE 254
229 #define DNS_TCP_PORT 53
230 #define DNS_UDP_PORT 53
235 #define QTYPE_CNAME 5
237 #define QTYPE_ANY 255
238 #define QTYPE_TKEY 249
239 #define QTYPE_TSIG 250
242 MF 4 a mail forwarder (Obsolete - use MX)
243 CNAME 5 the canonical name for an alias
244 SOA 6 marks the start of a zone of authority
245 MB 7 a mailbox domain name (EXPERIMENTAL)
246 MG 8 a mail group member (EXPERIMENTAL)
247 MR 9 a mail rename domain name (EXPERIMENTAL)
248 NULL 10 a null RR (EXPERIMENTAL)
249 WKS 11 a well known service description
250 PTR 12 a domain name pointer
251 HINFO 13 host information
252 MINFO 14 mailbox or mail list information
257 #define QR_QUERY 0x0000
258 #define QR_RESPONSE 0x0001
260 #define OPCODE_QUERY 0x00
261 #define OPCODE_IQUERY 0x01
262 #define OPCODE_STATUS 0x02
266 #define RECURSION_DESIRED 0x01
268 #define RCODE_NOERROR 0
269 #define RCODE_FORMATERROR 1
270 #define RCODE_SERVER_FAILURE 2
271 #define RCODE_NAME_ERROR 3
272 #define RCODE_NOTIMPLEMENTED 4
273 #define RCODE_REFUSED 5
275 #define SENDBUFFER_SIZE 65536
276 #define RECVBUFFER_SIZE 65536
279 * TKEY Modes from rfc2930
282 #define DNS_TKEY_MODE_SERVER 1
283 #define DNS_TKEY_MODE_DH 2
284 #define DNS_TKEY_MODE_GSSAPI 3
285 #define DNS_TKEY_MODE_RESOLVER 4
286 #define DNS_TKEY_MODE_DELETE 5
289 #define DNS_ONE_DAY_IN_SECS 86400
290 #define DNS_TEN_HOURS_IN_SECS 36000
292 #define SOCKET_ERROR -1
293 #define INVALID_SOCKET -1
295 #define DNS_NO_ERROR 0
296 #define DNS_FORMAT_ERROR 1
297 #define DNS_SERVER_FAILURE 2
298 #define DNS_NAME_ERROR 3
299 #define DNS_NOT_IMPLEMENTED 4
300 #define DNS_REFUSED 5
304 enum dns_ServerType { DNS_SRV_ANY, DNS_SRV_WIN2000, DNS_SRV_WIN2003 };
306 struct dns_domain_label {
307 struct dns_domain_label *next;
312 struct dns_domain_name {
313 struct dns_domain_label *pLabelList;
316 struct dns_question {
317 struct dns_domain_name *name;
323 * Before changing the definition of dns_zone, look
324 * dns_marshall_update_request(), we rely on this being the same as
325 * dns_question right now.
329 struct dns_domain_name *name;
335 struct dns_domain_name *name;
343 struct dns_tkey_record {
344 struct dns_domain_name *algorithm;
356 uint16 num_questions;
359 uint16 num_additionals;
360 struct dns_question **questions;
361 struct dns_rrec **answers;
362 struct dns_rrec **auths;
363 struct dns_rrec **additionals;
367 * Before changing the definition of dns_update_request, look
368 * dns_marshall_update_request(), we rely on this being the same as
369 * dns_request right now.
372 struct dns_update_request {
378 uint16 num_additionals;
379 struct dns_zone **zones;
380 struct dns_rrec **preqs;
381 struct dns_rrec **updates;
382 struct dns_rrec **additionals;
385 struct dns_connection {
388 struct sockaddr RecvAddr;
398 /* from dnsutils.c */
400 DNS_ERROR dns_domain_name_from_string( TALLOC_CTX *mem_ctx,
401 const char *pszDomainName,
402 struct dns_domain_name **presult );
403 char *dns_generate_keyname( TALLOC_CTX *mem_ctx );
405 /* from dnsrecord.c */
407 DNS_ERROR dns_create_query( TALLOC_CTX *mem_ctx, const char *name,
408 uint16 q_type, uint16 q_class,
409 struct dns_request **preq );
410 DNS_ERROR dns_create_update( TALLOC_CTX *mem_ctx, const char *name,
411 struct dns_update_request **preq );
412 DNS_ERROR dns_create_probe(TALLOC_CTX *mem_ctx, const char *zone,
413 const char *host, int num_ips,
414 const struct in_addr *iplist,
415 struct dns_update_request **preq);
416 DNS_ERROR dns_create_rrec(TALLOC_CTX *mem_ctx, const char *name,
417 uint16 type, uint16 r_class, uint32 ttl,
418 uint16 data_length, uint8 *data,
419 struct dns_rrec **prec);
420 DNS_ERROR dns_add_rrec(TALLOC_CTX *mem_ctx, struct dns_rrec *rec,
421 uint16 *num_records, struct dns_rrec ***records);
422 DNS_ERROR dns_create_tkey_record(TALLOC_CTX *mem_ctx, const char *keyname,
423 const char *algorithm_name, time_t inception,
424 time_t expiration, uint16 mode, uint16 error,
425 uint16 key_length, const uint8 *key,
426 struct dns_rrec **prec);
427 DNS_ERROR dns_create_name_in_use_record(TALLOC_CTX *mem_ctx,
429 const struct in_addr *ip,
430 struct dns_rrec **prec);
431 DNS_ERROR dns_create_delete_record(TALLOC_CTX *mem_ctx, const char *name,
432 uint16 type, uint16 r_class,
433 struct dns_rrec **prec);
434 DNS_ERROR dns_create_name_not_in_use_record(TALLOC_CTX *mem_ctx,
435 const char *name, uint32 type,
436 struct dns_rrec **prec);
437 DNS_ERROR dns_create_a_record(TALLOC_CTX *mem_ctx, const char *host,
438 uint32 ttl, struct in_addr ip,
439 struct dns_rrec **prec);
440 DNS_ERROR dns_unmarshall_tkey_record(TALLOC_CTX *mem_ctx, struct dns_rrec *rec,
441 struct dns_tkey_record **ptkey);
442 DNS_ERROR dns_create_tsig_record(TALLOC_CTX *mem_ctx, const char *keyname,
443 const char *algorithm_name,
444 time_t time_signed, uint16 fudge,
445 uint16 mac_length, const uint8 *mac,
446 uint16 original_id, uint16 error,
447 struct dns_rrec **prec);
448 DNS_ERROR dns_add_rrec(TALLOC_CTX *mem_ctx, struct dns_rrec *rec,
449 uint16 *num_records, struct dns_rrec ***records);
453 DNS_ERROR dns_open_connection( const char *nameserver, int32 dwType,
455 struct dns_connection **conn );
456 DNS_ERROR dns_send(struct dns_connection *conn, const struct dns_buffer *buf);
457 DNS_ERROR dns_receive(TALLOC_CTX *mem_ctx, struct dns_connection *conn,
458 struct dns_buffer **presult);
459 DNS_ERROR dns_transaction(TALLOC_CTX *mem_ctx, struct dns_connection *conn,
460 const struct dns_request *req,
461 struct dns_request **resp);
462 DNS_ERROR dns_update_transaction(TALLOC_CTX *mem_ctx,
463 struct dns_connection *conn,
464 struct dns_update_request *up_req,
465 struct dns_update_request **up_resp);
467 /* from dnsmarshall.c */
469 struct dns_buffer *dns_create_buffer(TALLOC_CTX *mem_ctx);
470 void dns_marshall_buffer(struct dns_buffer *buf, const uint8 *data,
472 void dns_marshall_uint16(struct dns_buffer *buf, uint16 val);
473 void dns_marshall_uint32(struct dns_buffer *buf, uint32 val);
474 void dns_unmarshall_buffer(struct dns_buffer *buf, uint8 *data,
476 void dns_unmarshall_uint16(struct dns_buffer *buf, uint16 *val);
477 void dns_unmarshall_uint32(struct dns_buffer *buf, uint32 *val);
478 void dns_unmarshall_domain_name(TALLOC_CTX *mem_ctx,
479 struct dns_buffer *buf,
480 struct dns_domain_name **pname);
481 void dns_marshall_domain_name(struct dns_buffer *buf,
482 const struct dns_domain_name *name);
483 void dns_unmarshall_domain_name(TALLOC_CTX *mem_ctx,
484 struct dns_buffer *buf,
485 struct dns_domain_name **pname);
486 DNS_ERROR dns_marshall_request(TALLOC_CTX *mem_ctx,
487 const struct dns_request *req,
488 struct dns_buffer **pbuf);
489 DNS_ERROR dns_unmarshall_request(TALLOC_CTX *mem_ctx,
490 struct dns_buffer *buf,
491 struct dns_request **preq);
492 DNS_ERROR dns_marshall_update_request(TALLOC_CTX *mem_ctx,
493 struct dns_update_request *update,
494 struct dns_buffer **pbuf);
495 DNS_ERROR dns_unmarshall_update_request(TALLOC_CTX *mem_ctx,
496 struct dns_buffer *buf,
497 struct dns_update_request **pupreq);
498 struct dns_request *dns_update2request(struct dns_update_request *update);
499 struct dns_update_request *dns_request2update(struct dns_request *request);
500 uint16 dns_response_code(uint16 flags);
504 #ifdef HAVE_GSSAPI_SUPPORT
506 void display_status( const char *msg, OM_uint32 maj_stat, OM_uint32 min_stat );
507 DNS_ERROR dns_negotiate_sec_ctx( const char *target_realm,
508 const char *servername,
510 gss_ctx_id_t *gss_ctx,
511 enum dns_ServerType srv_type );
512 DNS_ERROR dns_sign_update(struct dns_update_request *req,
513 gss_ctx_id_t gss_ctx,
515 const char *algorithmname,
516 time_t time_signed, uint16 fudge);
517 DNS_ERROR dns_create_update_request(TALLOC_CTX *mem_ctx,
518 const char *domainname,
519 const char *hostname,
520 const struct in_addr *ip_addr,
522 struct dns_update_request **preq);
524 #endif /* HAVE_GSSAPI_SUPPORT */