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_ANY 255
209 #define QTYPE_TKEY 249
210 #define QTYPE_TSIG 250
213 MF 4 a mail forwarder (Obsolete - use MX)
214 CNAME 5 the canonical name for an alias
215 SOA 6 marks the start of a zone of authority
216 MB 7 a mailbox domain name (EXPERIMENTAL)
217 MG 8 a mail group member (EXPERIMENTAL)
218 MR 9 a mail rename domain name (EXPERIMENTAL)
219 NULL 10 a null RR (EXPERIMENTAL)
220 WKS 11 a well known service description
221 PTR 12 a domain name pointer
222 HINFO 13 host information
223 MINFO 14 mailbox or mail list information
228 #define QR_QUERY 0x0000
229 #define QR_RESPONSE 0x0001
231 #define OPCODE_QUERY 0x00
232 #define OPCODE_IQUERY 0x01
233 #define OPCODE_STATUS 0x02
237 #define RECURSION_DESIRED 0x01
239 #define RCODE_NOERROR 0
240 #define RCODE_FORMATERROR 1
241 #define RCODE_SERVER_FAILURE 2
242 #define RCODE_NAME_ERROR 3
243 #define RCODE_NOTIMPLEMENTED 4
244 #define RCODE_REFUSED 5
246 #define SENDBUFFER_SIZE 65536
247 #define RECVBUFFER_SIZE 65536
250 * TKEY Modes from rfc2930
253 #define DNS_TKEY_MODE_SERVER 1
254 #define DNS_TKEY_MODE_DH 2
255 #define DNS_TKEY_MODE_GSSAPI 3
256 #define DNS_TKEY_MODE_RESOLVER 4
257 #define DNS_TKEY_MODE_DELETE 5
260 #define DNS_ONE_DAY_IN_SECS 86400
261 #define DNS_TEN_HOURS_IN_SECS 36000
263 #define SOCKET_ERROR -1
264 #define INVALID_SOCKET -1
266 #define DNS_NO_ERROR 0
267 #define DNS_FORMAT_ERROR 1
268 #define DNS_SERVER_FAILURE 2
269 #define DNS_NAME_ERROR 3
270 #define DNS_NOT_IMPLEMENTED 4
271 #define DNS_REFUSED 5
275 enum dns_ServerType
{ DNS_SRV_ANY
, DNS_SRV_WIN2000
, DNS_SRV_WIN2003
};
277 struct dns_domain_label
{
278 struct dns_domain_label
*next
;
283 struct dns_domain_name
{
284 struct dns_domain_label
*pLabelList
;
287 struct dns_question
{
288 struct dns_domain_name
*name
;
294 * Before changing the definition of dns_zone, look
295 * dns_marshall_update_request(), we rely on this being the same as
296 * dns_question right now.
300 struct dns_domain_name
*name
;
306 struct dns_domain_name
*name
;
314 struct dns_tkey_record
{
315 struct dns_domain_name
*algorithm
;
327 uint16 num_questions
;
330 uint16 num_additionals
;
331 struct dns_question
**questions
;
332 struct dns_rrec
**answers
;
333 struct dns_rrec
**auths
;
334 struct dns_rrec
**additionals
;
338 * Before changing the definition of dns_update_request, look
339 * dns_marshall_update_request(), we rely on this being the same as
340 * dns_request right now.
343 struct dns_update_request
{
349 uint16 num_additionals
;
350 struct dns_zone
**zones
;
351 struct dns_rrec
**preqs
;
352 struct dns_rrec
**updates
;
353 struct dns_rrec
**additionals
;
356 struct dns_connection
{
359 struct sockaddr RecvAddr
;
369 /* from dnsutils.c */
371 DNS_ERROR
dns_domain_name_from_string( TALLOC_CTX
*mem_ctx
,
372 const char *pszDomainName
,
373 struct dns_domain_name
**presult
);
374 char *dns_generate_keyname( TALLOC_CTX
*mem_ctx
);
376 /* from dnsrecord.c */
378 DNS_ERROR
dns_create_query( TALLOC_CTX
*mem_ctx
, const char *name
,
379 uint16 q_type
, uint16 q_class
,
380 struct dns_request
**preq
);
381 DNS_ERROR
dns_create_update( TALLOC_CTX
*mem_ctx
, const char *name
,
382 struct dns_update_request
**preq
);
383 DNS_ERROR
dns_create_probe(TALLOC_CTX
*mem_ctx
, const char *zone
,
384 const char *host
, int num_ips
,
385 const struct sockaddr_storage
*sslist
,
386 struct dns_update_request
**preq
);
387 DNS_ERROR
dns_create_rrec(TALLOC_CTX
*mem_ctx
, const char *name
,
388 uint16 type
, uint16 r_class
, uint32 ttl
,
389 uint16 data_length
, uint8
*data
,
390 struct dns_rrec
**prec
);
391 DNS_ERROR
dns_add_rrec(TALLOC_CTX
*mem_ctx
, struct dns_rrec
*rec
,
392 uint16
*num_records
, struct dns_rrec
***records
);
393 DNS_ERROR
dns_create_tkey_record(TALLOC_CTX
*mem_ctx
, const char *keyname
,
394 const char *algorithm_name
, time_t inception
,
395 time_t expiration
, uint16 mode
, uint16 error
,
396 uint16 key_length
, const uint8
*key
,
397 struct dns_rrec
**prec
);
398 DNS_ERROR
dns_create_name_in_use_record(TALLOC_CTX
*mem_ctx
,
400 const struct sockaddr_storage
*ip
,
401 struct dns_rrec
**prec
);
402 DNS_ERROR
dns_create_delete_record(TALLOC_CTX
*mem_ctx
, const char *name
,
403 uint16 type
, uint16 r_class
,
404 struct dns_rrec
**prec
);
405 DNS_ERROR
dns_create_name_not_in_use_record(TALLOC_CTX
*mem_ctx
,
406 const char *name
, uint32 type
,
407 struct dns_rrec
**prec
);
408 DNS_ERROR
dns_create_a_record(TALLOC_CTX
*mem_ctx
, const char *host
,
409 uint32 ttl
, const struct sockaddr_storage
*pss
,
410 struct dns_rrec
**prec
);
411 DNS_ERROR
dns_unmarshall_tkey_record(TALLOC_CTX
*mem_ctx
, struct dns_rrec
*rec
,
412 struct dns_tkey_record
**ptkey
);
413 DNS_ERROR
dns_create_tsig_record(TALLOC_CTX
*mem_ctx
, const char *keyname
,
414 const char *algorithm_name
,
415 time_t time_signed
, uint16 fudge
,
416 uint16 mac_length
, const uint8
*mac
,
417 uint16 original_id
, uint16 error
,
418 struct dns_rrec
**prec
);
419 DNS_ERROR
dns_add_rrec(TALLOC_CTX
*mem_ctx
, struct dns_rrec
*rec
,
420 uint16
*num_records
, struct dns_rrec
***records
);
421 DNS_ERROR
dns_create_update_request(TALLOC_CTX
*mem_ctx
,
422 const char *domainname
,
423 const char *hostname
,
424 const struct sockaddr_storage
*ip_addr
,
426 struct dns_update_request
**preq
);
430 DNS_ERROR
dns_open_connection( const char *nameserver
, int32 dwType
,
432 struct dns_connection
**conn
);
433 DNS_ERROR
dns_send(struct dns_connection
*conn
, const struct dns_buffer
*buf
);
434 DNS_ERROR
dns_receive(TALLOC_CTX
*mem_ctx
, struct dns_connection
*conn
,
435 struct dns_buffer
**presult
);
436 DNS_ERROR
dns_transaction(TALLOC_CTX
*mem_ctx
, struct dns_connection
*conn
,
437 const struct dns_request
*req
,
438 struct dns_request
**resp
);
439 DNS_ERROR
dns_update_transaction(TALLOC_CTX
*mem_ctx
,
440 struct dns_connection
*conn
,
441 struct dns_update_request
*up_req
,
442 struct dns_update_request
**up_resp
);
444 /* from dnsmarshall.c */
446 struct dns_buffer
*dns_create_buffer(TALLOC_CTX
*mem_ctx
);
447 void dns_marshall_buffer(struct dns_buffer
*buf
, const uint8
*data
,
449 void dns_marshall_uint16(struct dns_buffer
*buf
, uint16 val
);
450 void dns_marshall_uint32(struct dns_buffer
*buf
, uint32 val
);
451 void dns_unmarshall_buffer(struct dns_buffer
*buf
, uint8
*data
,
453 void dns_unmarshall_uint16(struct dns_buffer
*buf
, uint16
*val
);
454 void dns_unmarshall_uint32(struct dns_buffer
*buf
, uint32
*val
);
455 void dns_unmarshall_domain_name(TALLOC_CTX
*mem_ctx
,
456 struct dns_buffer
*buf
,
457 struct dns_domain_name
**pname
);
458 void dns_marshall_domain_name(struct dns_buffer
*buf
,
459 const struct dns_domain_name
*name
);
460 void dns_unmarshall_domain_name(TALLOC_CTX
*mem_ctx
,
461 struct dns_buffer
*buf
,
462 struct dns_domain_name
**pname
);
463 DNS_ERROR
dns_marshall_request(TALLOC_CTX
*mem_ctx
,
464 const struct dns_request
*req
,
465 struct dns_buffer
**pbuf
);
466 DNS_ERROR
dns_unmarshall_request(TALLOC_CTX
*mem_ctx
,
467 struct dns_buffer
*buf
,
468 struct dns_request
**preq
);
469 DNS_ERROR
dns_marshall_update_request(TALLOC_CTX
*mem_ctx
,
470 struct dns_update_request
*update
,
471 struct dns_buffer
**pbuf
);
472 DNS_ERROR
dns_unmarshall_update_request(TALLOC_CTX
*mem_ctx
,
473 struct dns_buffer
*buf
,
474 struct dns_update_request
**pupreq
);
475 struct dns_request
*dns_update2request(struct dns_update_request
*update
);
476 struct dns_update_request
*dns_request2update(struct dns_request
*request
);
477 uint16
dns_response_code(uint16 flags
);
478 const char *dns_errstr(DNS_ERROR err
);
482 #ifdef HAVE_GSSAPI_SUPPORT
484 void display_status( const char *msg
, OM_uint32 maj_stat
, OM_uint32 min_stat
);
485 DNS_ERROR
dns_negotiate_sec_ctx( const char *target_realm
,
486 const char *servername
,
488 gss_ctx_id_t
*gss_ctx
,
489 enum dns_ServerType srv_type
);
490 DNS_ERROR
dns_sign_update(struct dns_update_request
*req
,
491 gss_ctx_id_t gss_ctx
,
493 const char *algorithmname
,
494 time_t time_signed
, uint16 fudge
);
496 #endif /* HAVE_GSSAPI_SUPPORT */