1/* 2 Linux DNS client library implementation 3 4 Copyright (C) 2006 Krishna Ganugapati <krishnag@centeris.com> 5 Copyright (C) 2006 Gerald Carter <jerry@samba.org> 6 7 ** NOTE! The following LGPL license applies to the libaddns 8 ** library. This does NOT imply that all of Samba is released 9 ** under the LGPL 10 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. 15 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. 20 21 You should have received a copy of the GNU Lesser General Public 22 License along with this library; if not, write to the Free Software 23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 24 02110-1301 USA 25*/ 26 27#ifndef _DNS_H 28#define _DNS_H 29 30#include "config.h" 31 32#include <stdio.h> 33#include <stdlib.h> 34#include <fcntl.h> 35#include <time.h> 36#include <string.h> 37#include <errno.h> 38#include <netdb.h> 39#include <sys/types.h> 40#include <sys/socket.h> 41#include <netinet/in.h> 42#include <arpa/inet.h> 43#include <stdarg.h> 44 45#ifdef HAVE_UUID_UUID_H 46#include <uuid/uuid.h> 47#endif 48 49#ifdef HAVE_KRB5_H 50#include <krb5.h> 51#endif 52 53#ifdef HAVE_INTTYPES_H 54#include <inttypes.h> 55 56#ifndef int16 57#define int16 int16_t 58#endif 59 60#ifndef uint16 61#define uint16 uint16_t 62#endif 63 64#ifndef int32 65#define int32 int32_t 66#endif 67 68#ifndef uint32 69#define uint32 uint32_t 70#endif 71#endif 72 73#ifdef HAVE_KRB5_H 74#include <krb5.h> 75#endif 76 77#if HAVE_GSSAPI_H 78#include <gssapi.h> 79#elif HAVE_GSSAPI_GSSAPI_H 80#include <gssapi/gssapi.h> 81#elif HAVE_GSSAPI_GSSAPI_GENERIC_H 82#include <gssapi/gssapi_generic.h> 83#endif 84 85#if defined(HAVE_GSSAPI_H) || defined(HAVE_GSSAPI_GSSAPI_H) || defined(HAVE_GSSAPI_GSSAPI_GENERIC_H) 86#define HAVE_GSSAPI_SUPPORT 1 87#endif 88 89#include <talloc.h> 90 91void *_talloc_zero_zeronull(const void *ctx, size_t size, const char *name); 92void *_talloc_memdup_zeronull(const void *t, const void *p, size_t size, const char *name); 93void *_talloc_array_zeronull(const void *ctx, size_t el_size, unsigned count, const char *name); 94void *_talloc_zero_array_zeronull(const void *ctx, size_t el_size, unsigned count, const char *name); 95void *talloc_zeronull(const void *context, size_t size, const char *name); 96 97#define TALLOC(ctx, size) talloc_zeronull(ctx, size, __location__) 98#define TALLOC_P(ctx, type) (type *)talloc_zeronull(ctx, sizeof(type), #type) 99#define TALLOC_ARRAY(ctx, type, count) (type *)_talloc_array_zeronull(ctx, sizeof(type), count, #type) 100#define TALLOC_MEMDUP(ctx, ptr, size) _talloc_memdup_zeronull(ctx, ptr, size, __location__) 101#define TALLOC_ZERO(ctx, size) _talloc_zero_zeronull(ctx, size, __location__) 102#define TALLOC_ZERO_P(ctx, type) (type *)_talloc_zero_zeronull(ctx, sizeof(type), #type) 103#define TALLOC_ZERO_ARRAY(ctx, type, count) (type *)_talloc_zero_array_zeronull(ctx, sizeof(type), count, #type) 104#define TALLOC_REALLOC(ctx, ptr, count) _talloc_realloc(ctx, ptr, count, __location__) 105#define TALLOC_REALLOC_ARRAY(ctx, ptr, type, count) (type *)_talloc_realloc_array(ctx, ptr, sizeof(type), count, #type) 106#define talloc_destroy(ctx) talloc_free(ctx) 107#define TALLOC_FREE(ctx) do { if ((ctx) != NULL) {talloc_free(ctx); ctx=NULL;} } while(0) 108#define TALLOC_SIZE(ctx, size) talloc_zeronull(ctx, size, __location__) 109#define TALLOC_ZERO_SIZE(ctx, size) _talloc_zero_zeronull(ctx, size, __location__) 110 111/******************************************************************* 112 Type definitions for int16, int32, uint16 and uint32. Needed 113 for Samba coding style 114*******************************************************************/ 115 116#ifndef uint8 117# define uint8 unsigned char 118#endif 119 120#if !defined(int16) && !defined(HAVE_INT16_FROM_RPC_RPC_H) 121# if (SIZEOF_SHORT == 4) 122# define int16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16; 123# else /* SIZEOF_SHORT != 4 */ 124# define int16 short 125# endif /* SIZEOF_SHORT != 4 */ 126 /* needed to work around compile issue on HP-UX 11.x */ 127# define _INT16 1 128#endif 129 130/* 131 * Note we duplicate the size tests in the unsigned 132 * case as int16 may be a typedef from rpc/rpc.h 133 */ 134 135#if !defined(uint16) && !defined(HAVE_UINT16_FROM_RPC_RPC_H) 136# if (SIZEOF_SHORT == 4) 137# define uint16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16; 138# else /* SIZEOF_SHORT != 4 */ 139# define uint16 unsigned short 140# endif /* SIZEOF_SHORT != 4 */ 141#endif 142 143#if !defined(int32) && !defined(HAVE_INT32_FROM_RPC_RPC_H) 144# if (SIZEOF_INT == 4) 145# define int32 int 146# elif (SIZEOF_LONG == 4) 147# define int32 long 148# elif (SIZEOF_SHORT == 4) 149# define int32 short 150# else 151 /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */ 152# define int32 int 153# endif 154 /* needed to work around compile issue on HP-UX 11.x */ 155# define _INT32 1 156#endif 157 158/* 159 * Note we duplicate the size tests in the unsigned 160 * case as int32 may be a typedef from rpc/rpc.h 161 */ 162 163#if !defined(uint32) && !defined(HAVE_UINT32_FROM_RPC_RPC_H) 164# if (SIZEOF_INT == 4) 165# define uint32 unsigned int 166# elif (SIZEOF_LONG == 4) 167# define uint32 unsigned long 168# elif (SIZEOF_SHORT == 4) 169# define uint32 unsigned short 170# else 171 /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */ 172# define uint32 unsigned 173# endif 174#endif 175 176/* 177 * check for 8 byte long long 178 */ 179 180#if !defined(uint64) 181# if (SIZEOF_LONG == 8) 182# define uint64 unsigned long 183# elif (SIZEOF_LONG_LONG == 8) 184# define uint64 unsigned long long 185# endif /* don't lie. If we don't have it, then don't use it */ 186#endif 187 188/* needed on Sun boxes */ 189#ifndef INADDR_NONE 190#define INADDR_NONE 0xFFFFFFFF 191#endif 192 193#include "dnserr.h" 194 195 196#define DNS_TCP 1 197#define DNS_UDP 2 198 199#define DNS_OPCODE_UPDATE 1 200 201/* DNS Class Types */ 202 203#define DNS_CLASS_IN 1 204#define DNS_CLASS_ANY 255 205#define DNS_CLASS_NONE 254 206 207/* DNS RR Types */ 208 209#define DNS_RR_A 1 210 211#define DNS_TCP_PORT 53 212#define DNS_UDP_PORT 53 213 214#define QTYPE_A 1 215#define QTYPE_NS 2 216#define QTYPE_MD 3 217#define QTYPE_CNAME 5 218#define QTYPE_SOA 6 219#define QTYPE_ANY 255 220#define QTYPE_TKEY 249 221#define QTYPE_TSIG 250 222 223/* 224MF 4 a mail forwarder (Obsolete - use MX) 225CNAME 5 the canonical name for an alias 226SOA 6 marks the start of a zone of authority 227MB 7 a mailbox domain name (EXPERIMENTAL) 228MG 8 a mail group member (EXPERIMENTAL) 229MR 9 a mail rename domain name (EXPERIMENTAL) 230NULL 10 a null RR (EXPERIMENTAL) 231WKS 11 a well known service description 232PTR 12 a domain name pointer 233HINFO 13 host information 234MINFO 14 mailbox or mail list information 235MX 15 mail exchange 236TXT 16 text strings 237*/ 238 239#define QR_QUERY 0x0000 240#define QR_RESPONSE 0x0001 241 242#define OPCODE_QUERY 0x00 243#define OPCODE_IQUERY 0x01 244#define OPCODE_STATUS 0x02 245 246#define AA 1 247 248#define RECURSION_DESIRED 0x01 249 250#define RCODE_NOERROR 0 251#define RCODE_FORMATERROR 1 252#define RCODE_SERVER_FAILURE 2 253#define RCODE_NAME_ERROR 3 254#define RCODE_NOTIMPLEMENTED 4 255#define RCODE_REFUSED 5 256 257#define SENDBUFFER_SIZE 65536 258#define RECVBUFFER_SIZE 65536 259 260/* 261 * TKEY Modes from rfc2930 262 */ 263 264#define DNS_TKEY_MODE_SERVER 1 265#define DNS_TKEY_MODE_DH 2 266#define DNS_TKEY_MODE_GSSAPI 3 267#define DNS_TKEY_MODE_RESOLVER 4 268#define DNS_TKEY_MODE_DELETE 5 269 270 271#define DNS_ONE_DAY_IN_SECS 86400 272#define DNS_TEN_HOURS_IN_SECS 36000 273 274#define SOCKET_ERROR -1 275#define INVALID_SOCKET -1 276 277#define DNS_NO_ERROR 0 278#define DNS_FORMAT_ERROR 1 279#define DNS_SERVER_FAILURE 2 280#define DNS_NAME_ERROR 3 281#define DNS_NOT_IMPLEMENTED 4 282#define DNS_REFUSED 5 283 284typedef long HANDLE; 285 286#ifndef _UPPER_BOOL 287typedef int BOOL; 288#define _UPPER_BOOL 289#endif 290 291 292enum dns_ServerType { DNS_SRV_ANY, DNS_SRV_WIN2000, DNS_SRV_WIN2003 }; 293 294struct dns_domain_label { 295 struct dns_domain_label *next; 296 char *label; 297 size_t len; 298}; 299 300struct dns_domain_name { 301 struct dns_domain_label *pLabelList; 302}; 303 304struct dns_question { 305 struct dns_domain_name *name; 306 uint16 q_type; 307 uint16 q_class; 308}; 309 310/* 311 * Before changing the definition of dns_zone, look 312 * dns_marshall_update_request(), we rely on this being the same as 313 * dns_question right now. 314 */ 315 316struct dns_zone { 317 struct dns_domain_name *name; 318 uint16 z_type; 319 uint16 z_class; 320}; 321 322struct dns_rrec { 323 struct dns_domain_name *name; 324 uint16 type; 325 uint16 r_class; 326 uint32 ttl; 327 uint16 data_length; 328 uint8 *data; 329}; 330 331struct dns_tkey_record { 332 struct dns_domain_name *algorithm; 333 time_t inception; 334 time_t expiration; 335 uint16 mode; 336 uint16 error; 337 uint16 key_length; 338 uint8 *key; 339}; 340 341struct dns_request { 342 uint16 id; 343 uint16 flags; 344 uint16 num_questions; 345 uint16 num_answers; 346 uint16 num_auths; 347 uint16 num_additionals; 348 struct dns_question **questions; 349 struct dns_rrec **answers; 350 struct dns_rrec **auths; 351 struct dns_rrec **additionals; 352}; 353 354/* 355 * Before changing the definition of dns_update_request, look 356 * dns_marshall_update_request(), we rely on this being the same as 357 * dns_request right now. 358 */ 359 360struct dns_update_request { 361 uint16 id; 362 uint16 flags; 363 uint16 num_zones; 364 uint16 num_preqs; 365 uint16 num_updates; 366 uint16 num_additionals; 367 struct dns_zone **zones; 368 struct dns_rrec **preqs; 369 struct dns_rrec **updates; 370 struct dns_rrec **additionals; 371}; 372 373struct dns_connection { 374 int32 hType; 375 int s; 376 struct sockaddr RecvAddr; 377}; 378 379struct dns_buffer { 380 uint8 *data; 381 size_t size; 382 size_t offset; 383 DNS_ERROR error; 384}; 385 386/* from dnsutils.c */ 387 388DNS_ERROR dns_domain_name_from_string( TALLOC_CTX *mem_ctx, 389 const char *pszDomainName, 390 struct dns_domain_name **presult ); 391char *dns_generate_keyname( TALLOC_CTX *mem_ctx ); 392 393/* from dnsrecord.c */ 394 395DNS_ERROR dns_create_query( TALLOC_CTX *mem_ctx, const char *name, 396 uint16 q_type, uint16 q_class, 397 struct dns_request **preq ); 398DNS_ERROR dns_create_update( TALLOC_CTX *mem_ctx, const char *name, 399 struct dns_update_request **preq ); 400DNS_ERROR dns_create_probe(TALLOC_CTX *mem_ctx, const char *zone, 401 const char *host, int num_ips, 402 const struct in_addr *iplist, 403 struct dns_update_request **preq); 404DNS_ERROR dns_create_rrec(TALLOC_CTX *mem_ctx, const char *name, 405 uint16 type, uint16 r_class, uint32 ttl, 406 uint16 data_length, uint8 *data, 407 struct dns_rrec **prec); 408DNS_ERROR dns_add_rrec(TALLOC_CTX *mem_ctx, struct dns_rrec *rec, 409 uint16 *num_records, struct dns_rrec ***records); 410DNS_ERROR dns_create_tkey_record(TALLOC_CTX *mem_ctx, const char *keyname, 411 const char *algorithm_name, time_t inception, 412 time_t expiration, uint16 mode, uint16 error, 413 uint16 key_length, const uint8 *key, 414 struct dns_rrec **prec); 415DNS_ERROR dns_create_name_in_use_record(TALLOC_CTX *mem_ctx, 416 const char *name, 417 const struct in_addr *ip, 418 struct dns_rrec **prec); 419DNS_ERROR dns_create_delete_record(TALLOC_CTX *mem_ctx, const char *name, 420 uint16 type, uint16 r_class, 421 struct dns_rrec **prec); 422DNS_ERROR dns_create_name_not_in_use_record(TALLOC_CTX *mem_ctx, 423 const char *name, uint32 type, 424 struct dns_rrec **prec); 425DNS_ERROR dns_create_a_record(TALLOC_CTX *mem_ctx, const char *host, 426 uint32 ttl, struct in_addr ip, 427 struct dns_rrec **prec); 428DNS_ERROR dns_unmarshall_tkey_record(TALLOC_CTX *mem_ctx, struct dns_rrec *rec, 429 struct dns_tkey_record **ptkey); 430DNS_ERROR dns_create_tsig_record(TALLOC_CTX *mem_ctx, const char *keyname, 431 const char *algorithm_name, 432 time_t time_signed, uint16 fudge, 433 uint16 mac_length, const uint8 *mac, 434 uint16 original_id, uint16 error, 435 struct dns_rrec **prec); 436DNS_ERROR dns_add_rrec(TALLOC_CTX *mem_ctx, struct dns_rrec *rec, 437 uint16 *num_records, struct dns_rrec ***records); 438 439/* from dnssock.c */ 440 441DNS_ERROR dns_open_connection( const char *nameserver, int32 dwType, 442 TALLOC_CTX *mem_ctx, 443 struct dns_connection **conn ); 444DNS_ERROR dns_send(struct dns_connection *conn, const struct dns_buffer *buf); 445DNS_ERROR dns_receive(TALLOC_CTX *mem_ctx, struct dns_connection *conn, 446 struct dns_buffer **presult); 447DNS_ERROR dns_transaction(TALLOC_CTX *mem_ctx, struct dns_connection *conn, 448 const struct dns_request *req, 449 struct dns_request **resp); 450DNS_ERROR dns_update_transaction(TALLOC_CTX *mem_ctx, 451 struct dns_connection *conn, 452 struct dns_update_request *up_req, 453 struct dns_update_request **up_resp); 454 455/* from dnsmarshall.c */ 456 457struct dns_buffer *dns_create_buffer(TALLOC_CTX *mem_ctx); 458void dns_marshall_buffer(struct dns_buffer *buf, const uint8 *data, 459 size_t len); 460void dns_marshall_uint16(struct dns_buffer *buf, uint16 val); 461void dns_marshall_uint32(struct dns_buffer *buf, uint32 val); 462void dns_unmarshall_buffer(struct dns_buffer *buf, uint8 *data, 463 size_t len); 464void dns_unmarshall_uint16(struct dns_buffer *buf, uint16 *val); 465void dns_unmarshall_uint32(struct dns_buffer *buf, uint32 *val); 466void dns_unmarshall_domain_name(TALLOC_CTX *mem_ctx, 467 struct dns_buffer *buf, 468 struct dns_domain_name **pname); 469void dns_marshall_domain_name(struct dns_buffer *buf, 470 const struct dns_domain_name *name); 471void dns_unmarshall_domain_name(TALLOC_CTX *mem_ctx, 472 struct dns_buffer *buf, 473 struct dns_domain_name **pname); 474DNS_ERROR dns_marshall_request(TALLOC_CTX *mem_ctx, 475 const struct dns_request *req, 476 struct dns_buffer **pbuf); 477DNS_ERROR dns_unmarshall_request(TALLOC_CTX *mem_ctx, 478 struct dns_buffer *buf, 479 struct dns_request **preq); 480DNS_ERROR dns_marshall_update_request(TALLOC_CTX *mem_ctx, 481 struct dns_update_request *update, 482 struct dns_buffer **pbuf); 483DNS_ERROR dns_unmarshall_update_request(TALLOC_CTX *mem_ctx, 484 struct dns_buffer *buf, 485 struct dns_update_request **pupreq); 486struct dns_request *dns_update2request(struct dns_update_request *update); 487struct dns_update_request *dns_request2update(struct dns_request *request); 488uint16 dns_response_code(uint16 flags); 489 490/* from dnsgss.c */ 491 492#ifdef HAVE_GSSAPI_SUPPORT 493 494void display_status( const char *msg, OM_uint32 maj_stat, OM_uint32 min_stat ); 495DNS_ERROR dns_negotiate_sec_ctx( const char *target_realm, 496 const char *servername, 497 const char *keyname, 498 gss_ctx_id_t *gss_ctx, 499 enum dns_ServerType srv_type ); 500DNS_ERROR dns_sign_update(struct dns_update_request *req, 501 gss_ctx_id_t gss_ctx, 502 const char *keyname, 503 const char *algorithmname, 504 time_t time_signed, uint16 fudge); 505DNS_ERROR dns_create_update_request(TALLOC_CTX *mem_ctx, 506 const char *domainname, 507 const char *hostname, 508 const struct in_addr *ip_addr, 509 size_t num_adds, 510 struct dns_update_request **preq); 511 512#endif /* HAVE_GSSAPI_SUPPORT */ 513 514#endif /* _DNS_H */ 515