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, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
45 #ifdef HAVE_UUID_UUID_H
46 #include <uuid/uuid.h>
55 #elif HAVE_GSSAPI_GSSAPI_H
56 #include <gssapi/gssapi.h>
57 #elif HAVE_GSSAPI_GSSAPI_GENERIC_H
58 #include <gssapi/gssapi_generic.h>
61 #if defined(HAVE_GSSAPI_H) || defined(HAVE_GSSAPI_GSSAPI_H) || defined(HAVE_GSSAPI_GSSAPI_GENERIC_H)
62 #define HAVE_GSSAPI_SUPPORT 1
67 #define TALLOC(ctx, size) talloc_named_const(ctx, size, __location__)
68 #define TALLOC_P(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
69 #define TALLOC_ARRAY(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
70 #define TALLOC_MEMDUP(ctx, ptr, size) _talloc_memdup(ctx, ptr, size, __location__)
71 #define TALLOC_ZERO(ctx, size) _talloc_zero(ctx, size, __location__)
72 #define TALLOC_ZERO_P(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
73 #define TALLOC_ZERO_ARRAY(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
74 #define TALLOC_REALLOC(ctx, ptr, count) _talloc_realloc(ctx, ptr, count, __location__)
75 #define TALLOC_REALLOC_ARRAY(ctx, ptr, type, count) (type *)_talloc_realloc_array(ctx, ptr, sizeof(type), count, #type)
76 #define TALLOC_FREE(ctx) do { if ((ctx) != NULL) {talloc_free(ctx); ctx=NULL;} } while(0)
78 /*******************************************************************
79 Type definitions for int16, int32, uint16 and uint32. Needed
80 for Samba coding style
81 *******************************************************************/
84 # define uint8 unsigned char
87 #if !defined(int16) && !defined(HAVE_INT16_FROM_RPC_RPC_H)
88 # if (SIZEOF_SHORT == 4)
89 # define int16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16;
90 # else /* SIZEOF_SHORT != 4 */
92 # endif /* SIZEOF_SHORT != 4 */
93 /* needed to work around compile issue on HP-UX 11.x */
98 * Note we duplicate the size tests in the unsigned
99 * case as int16 may be a typedef from rpc/rpc.h
102 #if !defined(uint16) && !defined(HAVE_UINT16_FROM_RPC_RPC_H)
103 # if (SIZEOF_SHORT == 4)
104 # define uint16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16;
105 # else /* SIZEOF_SHORT != 4 */
106 # define uint16 unsigned short
107 # endif /* SIZEOF_SHORT != 4 */
110 #if !defined(int32) && !defined(HAVE_INT32_FROM_RPC_RPC_H)
111 # if (SIZEOF_INT == 4)
113 # elif (SIZEOF_LONG == 4)
115 # elif (SIZEOF_SHORT == 4)
118 /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */
121 /* needed to work around compile issue on HP-UX 11.x */
126 * Note we duplicate the size tests in the unsigned
127 * case as int32 may be a typedef from rpc/rpc.h
130 #if !defined(uint32) && !defined(HAVE_UINT32_FROM_RPC_RPC_H)
131 # if (SIZEOF_INT == 4)
132 # define uint32 unsigned int
133 # elif (SIZEOF_LONG == 4)
134 # define uint32 unsigned long
135 # elif (SIZEOF_SHORT == 4)
136 # define uint32 unsigned short
138 /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */
139 # define uint32 unsigned
144 * check for 8 byte long long
148 # if (SIZEOF_LONG == 8)
149 # define uint64 unsigned long
150 # elif (SIZEOF_LONG_LONG == 8)
151 # define uint64 unsigned long long
152 # endif /* don't lie. If we don't have it, then don't use it */
155 /* needed on Sun boxes */
157 #define INADDR_NONE 0xFFFFFFFF
166 #define DNS_OPCODE_UPDATE 1
168 #define BAIL_ON_ERROR(x) \
173 #define BAIL_ON_DNS_ERROR(x) \
174 if ( !ERR_DNS_IS_OK((x)) ) { \
178 #define BAIL_ON_SEC_ERROR(dwMajorStatus) \
179 if ((dwMajorStatus!= GSS_S_COMPLETE)\
180 && (dwMajorStatus != GSS_S_CONTINUE_NEEDED)) {\
184 /* DNS Class Types */
186 #define DNS_CLASS_IN 1
187 #define DNS_CLASS_ANY 255
188 #define DNS_CLASS_NONE 254
194 #define DNS_TCP_PORT 53
195 #define DNS_UDP_PORT 53
200 #define QTYPE_CNAME 5
202 #define QTYPE_ANY 255
203 #define QTYPE_TKEY 249
204 #define QTYPE_TSIG 250
207 MF 4 a mail forwarder (Obsolete - use MX)
208 CNAME 5 the canonical name for an alias
209 SOA 6 marks the start of a zone of authority
210 MB 7 a mailbox domain name (EXPERIMENTAL)
211 MG 8 a mail group member (EXPERIMENTAL)
212 MR 9 a mail rename domain name (EXPERIMENTAL)
213 NULL 10 a null RR (EXPERIMENTAL)
214 WKS 11 a well known service description
215 PTR 12 a domain name pointer
216 HINFO 13 host information
217 MINFO 14 mailbox or mail list information
222 #define QR_QUERY 0x0000
223 #define QR_RESPONSE 0x0001
225 #define OPCODE_QUERY 0x00
226 #define OPCODE_IQUERY 0x01
227 #define OPCODE_STATUS 0x02
231 #define RECURSION_DESIRED 0x01
233 #define RCODE_NOERROR 0
234 #define RCODE_FORMATERROR 1
235 #define RCODE_SERVER_FAILURE 2
236 #define RCODE_NAME_ERROR 3
237 #define RCODE_NOTIMPLEMENTED 4
238 #define RCODE_REFUSED 5
240 #define SENDBUFFER_SIZE 65536
241 #define RECVBUFFER_SIZE 65536
243 #define DNS_ONE_DAY_IN_SECS 86400
244 #define DNS_TEN_HOURS_IN_SECS 36000
246 #define SOCKET_ERROR -1
247 #define INVALID_SOCKET -1
249 #define DNS_NO_ERROR 0
250 #define DNS_FORMAT_ERROR 1
251 #define DNS_SERVER_FAILURE 2
252 #define DNS_NAME_ERROR 3
253 #define DNS_NOT_IMPLEMENTED 4
254 #define DNS_REFUSED 5
261 #define _BOOL /* So we don't typedef BOOL again */
265 typedef struct dns_domain_label
{
266 struct dns_domain_label
*pNext
;
272 DNS_DOMAIN_LABEL
*pLabelList
;
276 DNS_DOMAIN_NAME
*pDomainName
;
279 } DNS_QUESTION_RECORD
;
283 DNS_DOMAIN_NAME
*pDomainName
;
290 DNS_DOMAIN_NAME
*pDomainName
;
304 int16 wAlgorithmOffset
;
305 int16 wInceptionOffset
;
306 int16 wExpirationOffset
;
309 int16 wKeySizeOffset
;
310 int16 wKeyDataOffset
;
311 int16 wOtherSizeOffset
;
312 int16 wOtherDataOffset
;
316 int16 wAlgorithmOffset
;
317 int16 wTimeSignedOffset
;
319 int16 wMacSizeOffset
;
320 int16 wMacDataOffset
;
321 int16 wOriginalMessageIdOffset
;
323 int16 wOtherSizeOffset
;
324 int16 wOtherDataOffset
;
329 DNS_RR_HEADER RRHeader
;
331 DNS_TKEY_OFFSETS TKey
;
332 DNS_TSIG_OFFSETS TSig
;
339 int16 wIdentification
;
345 DNS_QUESTION_RECORD
**ppQuestionRRSet
;
346 DNS_RR_RECORD
**ppAnswerRRSet
;
347 DNS_RR_RECORD
**ppAuthorityRRSet
;
348 DNS_RR_RECORD
**ppAdditionalRRSet
;
353 int16 wIdentification
;
359 DNS_ZONE_RECORD
**ppZoneRRSet
;
360 DNS_RR_RECORD
**ppPRRRSet
;
361 DNS_RR_RECORD
**ppUpdateRRSet
;
362 DNS_RR_RECORD
**ppAdditionalRRSet
;
363 } DNS_UPDATE_REQUEST
;
367 int16 wIdentification
;
373 DNS_QUESTION_RECORD
**ppQuestionRRSet
;
374 DNS_RR_RECORD
**ppAnswerRRSet
;
375 DNS_RR_RECORD
**ppAuthorityRRSet
;
376 DNS_RR_RECORD
**ppAdditionalRRSet
;
377 uint8
*pDNSOutBuffer
;
382 int16 wIdentification
;
388 DNS_ZONE_RECORD
**ppZoneRRSet
;
389 DNS_RR_RECORD
**ppPRRRSet
;
390 DNS_RR_RECORD
**ppUpdateRRSet
;
391 DNS_RR_RECORD
**ppAdditionalRRSet
;
392 uint8
*pDNSOutBuffer
;
394 } DNS_UPDATE_RESPONSE
;
399 struct sockaddr RecvAddr
;
400 } DNS_CONNECTION_CONTEXT
;
405 int32 dwBytesWritten
;
406 int32 dwBufferOffset
;
407 } DNS_SENDBUFFER_CONTEXT
;
414 } DNS_RECEIVEBUFFER_CONTEXT
;
416 /* from dnsutils.c */
418 int32
DNSGenerateIdentifier( int16
* pwIdentifer
);
419 int32
DNSGetDomainNameLength( DNS_DOMAIN_NAME
* pDomainName
, int32
* pdwLength
);
420 int32
DNSCopyDomainName( uint8
* pBuffer
, DNS_DOMAIN_NAME
* pDomainName
, int32
* pdwCopied
);
421 int32
DNSAllocateString( char *pszInputString
, char **ppszOutputString
);
422 int32
DNSGenerateKeyName( char **pszKeyName
);
423 int32
DNSMakeRRHeader( DNS_RR_HEADER
* pDNSRR
, char *szOwnerName
, int16 wType
, int32 dwTTL
);
424 int32
DNSDomainNameFromString( char *pszDomainName
, DNS_DOMAIN_NAME
** ppDomainName
);
425 int32
DNSAppendLabel( DNS_DOMAIN_LABEL
* pLabelList
, DNS_DOMAIN_LABEL
* pLabel
, DNS_DOMAIN_LABEL
** ppNewLabelList
);
426 int32
DNSGenerateKeyName( char **ppszKeyName
);
427 void DNSRecordGenerateOffsets( DNS_RR_RECORD
* pDNSRecord
);
428 int32
MapDNSResponseCodes( int16 wResponseCode
);
429 int32
GetLastError( void );
430 int32
WSAGetLastError( void );
431 int32
DNSAllocateMemory(int32 dwSize
, void * * ppMemory
);
432 int32
DNSReallocMemory(void * pMemory
, void * * ppNewMemory
, int32 dwSize
);
433 void DNSFreeMemory( void * pMemory
);
434 int32
DNSAllocateString(char *pszInputString
, char **ppszOutputString
);
435 void DNSFreeString(char * pszString
);
436 void DNSFreeDomainName(DNS_DOMAIN_NAME
*pDomainName
);
438 /* from dnsrecord.c */
440 int32
DNSCreateDeleteRecord( char *szHost
, int16 wClass
, int16 wType
, DNS_RR_RECORD
** ppDNSRecord
);
441 int32
DNSCreateARecord( char *szHost
, int16 wClass
, int16 wType
, int32 dwIP
, DNS_RR_RECORD
** ppDNSRecord
);
442 int32
DNSCreateTKeyRecord( char *szKeyName
, uint8
* pKeyData
, int16 dwKeyLen
, DNS_RR_RECORD
** ppDNSRecord
);
443 int32
DNSCreateTSIGRecord( char *szKeyName
, int32 dwTimeSigned
, int16 wFudge
, int16 wOriginalID
, uint8
* pMac
, int16 wMacSize
, DNS_RR_RECORD
** ppDNSRecord
);
444 int32
DNSCreateQuestionRecord( char *pszQName
, int16 wQType
, int16 wQClass
, DNS_QUESTION_RECORD
** ppDNSQuestionRecord
);
445 int32
DNSAddQuestionSection( DNS_REQUEST
* pDNSRequest
, DNS_QUESTION_RECORD
* pDNSQuestion
);
446 int32
DNSAddAdditionalSection( DNS_REQUEST
* pDNSRequest
, DNS_RR_RECORD
* pDNSRecord
);
447 int32
DNSAddAnswerSection( DNS_REQUEST
* pDNSRequest
, DNS_RR_RECORD
* pDNSRecord
);
448 int32
DNSAddAuthoritySection( DNS_REQUEST
* pDNSRequest
, DNS_RR_RECORD
* pDNSRecord
);
449 int32
DNSCreateZoneRecord( char *pszZName
, DNS_ZONE_RECORD
** ppDNSZoneRecord
);
450 int32
DNSFreeZoneRecord( DNS_ZONE_RECORD
* pDNSZoneRecord
);
451 int32
DNSCreateNameInUseRecord( char *pszName
, int32 qtype
, struct in_addr
*addr
, DNS_RR_RECORD
** ppDNSRRRecord
);
452 int32
DNSCreateNameNotInUseRecord( char *pszName
, int32 qtype
, DNS_RR_RECORD
** ppDNSRRRecord
);
454 /* from dnsresponse.c */
456 int32
DNSStdReceiveStdResponse( HANDLE hDNSHandle
, DNS_RESPONSE
** ppDNSResponse
);
457 int32
DNSUnmarshallDomainName( HANDLE hRecvBuffer
, DNS_DOMAIN_NAME
** ppDomainName
);
458 int32
DNSUnmarshallRRHeader( HANDLE hRecvBuffer
, DNS_RR_HEADER
* pRRHeader
);
459 int32
DNSUnmarshallRData( HANDLE hRecvBuffer
, int32 dwSize
, uint8
** ppRData
, int32
* pdwRead
);
460 int32
DNSUpdateGetResponseCode( DNS_UPDATE_RESPONSE
* pDNSUpdateResponse
, int32
* pdwResponseCode
);
462 /* from dnsrequest.c */
464 int32
DNSStdSendMarshallSection( HANDLE hSendBuffer
, DNS_RR_RECORD
** ppDNSAnswerRRRecords
, int16 wAnswers
);
465 int32
DNSMarshallDomainName( HANDLE hSendBuffer
, DNS_DOMAIN_NAME
* pDomainName
);
466 int32
DNSMarshallRRHeader( HANDLE hSendBuffer
, DNS_RR_RECORD
* pDNSRecord
);
467 int32
DNSMarshallRData( HANDLE hSendBuffer
, DNS_RR_RECORD
* pDNSRecord
);
468 int32
DNSWriteDomainName( HANDLE hDNSHandle
, DNS_DOMAIN_NAME
* pDomainName
);
469 void DNSFreeRequest( DNS_REQUEST
* pDNSRequest
);
470 int32
DNSStdAddQuestionSection( DNS_REQUEST
* pDNSRequest
, DNS_QUESTION_RECORD
* pDNSQuestion
);
471 int32
DNSStdAddAdditionalSection( DNS_REQUEST
* pDNSRequest
, DNS_RR_RECORD
* pDNSRecord
);
472 int32
DNSStdCreateStdRequest( DNS_REQUEST
** ppDNSRequest
);
473 int32
DNSStdSendStdRequest2( HANDLE hDNSServer
, DNS_REQUEST
* pDNSRequest
);
475 /* from dnsuprequest.c */
477 int32
DNSUpdateSendUpdateRequest2( HANDLE hSendBuffer
, DNS_UPDATE_REQUEST
* pDNSRequest
);
478 int32
DNSUpdateBuildRequestMessage( DNS_UPDATE_REQUEST
* pDNSRequest
, HANDLE
* phSendBuffer
);
479 void DNSUpdateFreeRequest( DNS_UPDATE_REQUEST
* pDNSRequest
);
480 int32
DNSWriteDomainName( HANDLE hDNSHandle
, DNS_DOMAIN_NAME
* pDomainName
);
481 void DNSUpdateFreeRequest( DNS_UPDATE_REQUEST
* pDNSRequest
);
482 int32
DNSUpdateAddZoneSection( DNS_UPDATE_REQUEST
* pDNSRequest
, DNS_ZONE_RECORD
* pDNSZone
);
483 int32
DNSUpdateAddAdditionalSection( DNS_UPDATE_REQUEST
* pDNSRequest
, DNS_RR_RECORD
* pDNSRecord
);
484 int32
DNSUpdateAddPRSection( DNS_UPDATE_REQUEST
* pDNSRequest
, DNS_RR_RECORD
* pDNSRecord
);
485 int32
DNSUpdateAddUpdateSection( DNS_UPDATE_REQUEST
* pDNSRequest
, DNS_RR_RECORD
* pDNSRecord
);
486 int32
DNSUpdateCreateUpdateRequest( DNS_UPDATE_REQUEST
** ppDNSRequest
);
490 DNS_ERROR
DNSOpen( char *nameserver
, int32 dwType
, HANDLE
* phDNSServer
);
491 int32
DNSReceiveBufferContext( HANDLE hDNSHandle
, HANDLE hDNSRecvBuffer
, int32
* pdwBytesRead
);
492 int32
DNSCreateSendBuffer( HANDLE
* phDNSSendBuffer
);
493 int32
DNSMarshallBuffer( HANDLE hDNSSendBuffer
, uint8
* pDNSSendBuffer
, int32 dwBufferSize
, int32
* pdwBytesWritten
);;
494 int32
DNSSendBufferContext( HANDLE hDNSServer
, HANDLE hSendBuffer
, int32
* pdwBytesSent
);
495 int32
DNSCreateReceiveBuffer( HANDLE
* phDNSRecvBuffer
);
496 int32
DNSUnmarshallBuffer( HANDLE hDNSRecvBuffer
, uint8
* pDNSRecvBuffer
, int32 dwBufferSize
, int32
* pdwBytesRead
);
497 int32
DNSUnmarshallDomainNameAtOffset( HANDLE hRecvBuffer
, int16 wOffset
, DNS_DOMAIN_NAME
** ppDomainName
);
498 int32
DNSReceiveBufferMoveBackIndex( HANDLE hRecvBuffer
, int16 wOffset
);
499 void DNSFreeSendBufferContext( HANDLE hSendBuffer
);
500 int32
DNSGetSendBufferContextSize( HANDLE hSendBuffer
);
501 uint8
*DNSGetSendBufferContextBuffer( HANDLE hSendBuffer
);
506 #ifdef HAVE_GSSAPI_SUPPORT
508 int32
DNSVerifyResponseMessage_GSSSuccess( gss_ctx_id_t
* pGSSContext
, DNS_RR_RECORD
* pClientTKeyRecord
, DNS_RESPONSE
* pDNSResponse
);
509 int32
DNSVerifyResponseMessage_GSSContinue( gss_ctx_id_t
* pGSSContext
, DNS_RR_RECORD
* pClientTKeyRecord
, DNS_RESPONSE
* pDNSResponse
, uint8
** ppServerKeyData
, int16
* pwServerKeyDataSize
);
510 int32
DNSResponseGetRCode( DNS_RESPONSE
* pDNSResponse
, int16
* pwRCode
);
511 int32
DNSResponseGetTSIGRecord( DNS_RESPONSE
* pDNSResponse
, DNS_RR_RECORD
** ppTSIGRecord
);
512 int32
DNSCompareTKeyRecord( DNS_RR_RECORD
* pClientTKeyRecord
, DNS_RR_RECORD
* pTKeyRecord
);
513 int32
DNSBuildTKeyQueryRequest( char *szKeyName
, uint8
* pKeyData
, int32 dwKeyLen
, DNS_REQUEST
** ppDNSRequest
);
514 int32
DNSResponseGetTKeyRecord( DNS_RESPONSE
* pDNSResponse
, DNS_RR_RECORD
** ppTKeyRecord
);
515 int32
DNSGetTKeyData( DNS_RR_RECORD
* pTKeyRecord
, uint8
** ppKeyData
, int16
* pwKeyDataSize
);
516 int32
DNSNegotiateSecureContext( HANDLE hDNSServer
, char *szDomain
, char *szServerName
, char *szKeyName
, gss_ctx_id_t
* pGSSContext
);
517 void display_status( const char *msg
, OM_uint32 maj_stat
, OM_uint32 min_stat
);
518 int32
DNSNegotiateContextAndSecureUpdate( HANDLE hDNSServer
, char *szServiceName
, char *szDomainName
, char *szHost
, int32 dwIPAddress
);
520 #endif /* HAVE_GSSAPI_SUPPORT */
522 /* from dnsupdate.c */
524 int32
DNSSendUpdate( HANDLE hDNSServer
, char *szDomainName
, char *szHost
, struct in_addr
*iplist
, int num_addrs
, DNS_UPDATE_RESPONSE
** ppDNSUpdateResponse
);
525 int32
DNSBuildSignatureBuffer( int32 dwMaxSignatureSize
, uint8
** ppSignature
);
526 int32
DNSBuildMessageBuffer( DNS_UPDATE_REQUEST
* pDNSUpdateRequest
, char *szKeyName
, int32
* pdwTimeSigned
, int16
* pwFudge
, uint8
** ppMessageBuffer
, int32
* pdwMessageSize
);
527 int32
DNSClose( HANDLE hDNSUpdate
);
529 #ifdef HAVE_GSSAPI_SUPPORT
530 int32
DNSSendSecureUpdate( HANDLE hDNSServer
, gss_ctx_id_t
* pGSSContext
, char *pszKeyName
, char *szDomainName
, char *szHost
, int32 dwIP
, DNS_UPDATE_RESPONSE
** ppDNSUpdateResponse
);
531 int32
DNSUpdateGenerateSignature( gss_ctx_id_t
* pGSSContext
, DNS_UPDATE_REQUEST
* pDNSUpdateRequest
, char *pszKeyName
);
532 #endif /* HAVE_GSSAPI_SUPPORT */
534 /* from dnsupresp.c */
536 int32
DNSUpdateReceiveUpdateResponse( HANDLE hDNSHandle
, DNS_UPDATE_RESPONSE
** ppDNSResponse
);
540 #ifdef HAVE_GSSAPI_SUPPORT
541 int32
DNSGenerateHash( gss_ctx_id_t
* gss_context
, uint8
* pRequestBuffer
, uint8
** ppMAC
, int32
* pdwMacLen
);
542 int32
BuildHashInputBuffer( DNS_REQUEST
* pDNSRequest
, int32 dwLength
, uint8
** ppHashInputBuffer
, int32
* pdwHashInputBufferLen
);
543 int32
DNSStdValidateAndGetTSIGRecord( gss_ctx_id_t
* gss_context
, DNS_RESPONSE
* pDNSResponse
, DNS_RR_RECORD
** ppDNSTSIGRecord
);
544 #endif /* HAVE_GSSAPI_SUPPORT */