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