RIP BOOL. Convert BOOL -> bool. I found a few interesting
[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 "config.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <time.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <netdb.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <stdarg.h>
42
43 #ifdef HAVE_UUID_UUID_H
44 #include <uuid/uuid.h>
45 #endif
46
47 #ifdef HAVE_KRB5_H
48 #include <krb5.h>
49 #endif
50
51 #ifdef HAVE_INTTYPES_H
52 #include <inttypes.h>
53
54 #ifndef int16
55 #define int16 int16_t
56 #endif
57
58 #ifndef uint16
59 #define uint16 uint16_t
60 #endif
61
62 #ifndef int32
63 #define int32 int32_t
64 #endif
65
66 #ifndef uint32
67 #define uint32 uint32_t
68 #endif
69 #endif
70
71 #ifdef HAVE_KRB5_H
72 #include <krb5.h>
73 #endif
74
75 #if HAVE_GSSAPI_H
76 #include <gssapi.h>
77 #elif HAVE_GSSAPI_GSSAPI_H
78 #include <gssapi/gssapi.h>
79 #elif HAVE_GSSAPI_GSSAPI_GENERIC_H
80 #include <gssapi/gssapi_generic.h>
81 #endif
82
83 #if defined(HAVE_GSSAPI_H) || defined(HAVE_GSSAPI_GSSAPI_H) || defined(HAVE_GSSAPI_GSSAPI_GENERIC_H)
84 #define HAVE_GSSAPI_SUPPORT    1
85 #endif
86
87 #include <talloc.h>
88
89 #if 0
90
91 Disable these now we have checked all code paths and ensured
92 NULL returns on zero request. JRA.
93
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);
99
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__)
109
110 #else
111
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__)
121
122 #endif
123
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)
128
129 /*******************************************************************
130    Type definitions for int16, int32, uint16 and uint32.  Needed
131    for Samba coding style
132 *******************************************************************/
133
134 #ifndef uint8
135 #  define uint8 unsigned char
136 #endif
137
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 */
142 #    define int16 short
143 #  endif /* SIZEOF_SHORT != 4 */
144    /* needed to work around compile issue on HP-UX 11.x */
145 #  define _INT16        1
146 #endif
147
148 /*
149  * Note we duplicate the size tests in the unsigned
150  * case as int16 may be a typedef from rpc/rpc.h
151  */
152
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 */
159 #endif
160
161 #if !defined(int32) && !defined(HAVE_INT32_FROM_RPC_RPC_H)
162 #  if (SIZEOF_INT == 4)
163 #    define int32 int
164 #  elif (SIZEOF_LONG == 4)
165 #    define int32 long
166 #  elif (SIZEOF_SHORT == 4)
167 #    define int32 short
168 #  else
169      /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */
170 #    define int32 int
171 #  endif
172    /* needed to work around compile issue on HP-UX 11.x */
173 #  define _INT32        1
174 #endif
175
176 /*
177  * Note we duplicate the size tests in the unsigned
178  * case as int32 may be a typedef from rpc/rpc.h
179  */
180
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
188 #  else
189       /* uggh - no 32 bit type?? probably a CRAY. just hope this works ... */
190 #    define uint32 unsigned
191 #  endif
192 #endif
193
194 /*
195  * check for 8 byte long long
196  */
197
198 #if !defined(uint64)
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 */
204 #endif
205
206 /* needed on Sun boxes */
207 #ifndef INADDR_NONE
208 #define INADDR_NONE          0xFFFFFFFF
209 #endif
210
211 #include "dnserr.h"
212
213
214 #define DNS_TCP                 1
215 #define DNS_UDP                 2
216
217 #define DNS_OPCODE_UPDATE       1
218
219 /* DNS Class Types */
220
221 #define DNS_CLASS_IN            1
222 #define DNS_CLASS_ANY           255
223 #define DNS_CLASS_NONE          254
224
225 /* DNS RR Types */
226
227 #define DNS_RR_A                1
228
229 #define DNS_TCP_PORT            53
230 #define DNS_UDP_PORT            53
231
232 #define QTYPE_A         1
233 #define QTYPE_NS        2
234 #define QTYPE_MD        3
235 #define QTYPE_CNAME     5
236 #define QTYPE_SOA       6
237 #define QTYPE_ANY       255
238 #define QTYPE_TKEY      249
239 #define QTYPE_TSIG      250
240
241 /*
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
253 MX              15 mail exchange
254 TXT             16 text strings
255 */
256
257 #define QR_QUERY         0x0000
258 #define QR_RESPONSE      0x0001
259
260 #define OPCODE_QUERY 0x00
261 #define OPCODE_IQUERY   0x01
262 #define OPCODE_STATUS   0x02
263
264 #define AA                      1
265
266 #define RECURSION_DESIRED       0x01
267
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
274
275 #define SENDBUFFER_SIZE         65536
276 #define RECVBUFFER_SIZE         65536
277
278 /*
279  * TKEY Modes from rfc2930
280  */
281
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
287
288
289 #define DNS_ONE_DAY_IN_SECS     86400
290 #define DNS_TEN_HOURS_IN_SECS   36000
291
292 #define SOCKET_ERROR            -1
293 #define INVALID_SOCKET          -1
294
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
301
302 typedef long HANDLE;
303
304 enum dns_ServerType { DNS_SRV_ANY, DNS_SRV_WIN2000, DNS_SRV_WIN2003 };
305
306 struct dns_domain_label {
307         struct dns_domain_label *next;
308         char *label;
309         size_t len;
310 };
311
312 struct dns_domain_name {
313         struct dns_domain_label *pLabelList;
314 };
315
316 struct dns_question {
317         struct dns_domain_name *name;
318         uint16 q_type;
319         uint16 q_class;
320 };
321
322 /*
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.
326  */
327
328 struct dns_zone {
329         struct dns_domain_name *name;
330         uint16 z_type;
331         uint16 z_class;
332 };
333
334 struct dns_rrec {
335         struct dns_domain_name *name;
336         uint16 type;
337         uint16 r_class;
338         uint32 ttl;
339         uint16 data_length;
340         uint8 *data;
341 };
342
343 struct dns_tkey_record {
344         struct dns_domain_name *algorithm;
345         time_t inception;
346         time_t expiration;
347         uint16 mode;
348         uint16 error;
349         uint16 key_length;
350         uint8 *key;
351 };
352
353 struct dns_request {
354         uint16 id;
355         uint16 flags;
356         uint16 num_questions;
357         uint16 num_answers;
358         uint16 num_auths;
359         uint16 num_additionals;
360         struct dns_question **questions;
361         struct dns_rrec **answers;
362         struct dns_rrec **auths;
363         struct dns_rrec **additionals;
364 };
365
366 /*
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.
370  */
371
372 struct dns_update_request {
373         uint16 id;
374         uint16 flags;
375         uint16 num_zones;
376         uint16 num_preqs;
377         uint16 num_updates;
378         uint16 num_additionals;
379         struct dns_zone **zones;
380         struct dns_rrec **preqs;
381         struct dns_rrec **updates;
382         struct dns_rrec **additionals;
383 };
384
385 struct dns_connection {
386         int32 hType;
387         int s;
388         struct sockaddr RecvAddr;
389 };
390
391 struct dns_buffer {
392         uint8 *data;
393         size_t size;
394         size_t offset;
395         DNS_ERROR error;
396 };
397
398 /* from dnsutils.c */
399
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 );
404
405 /* from dnsrecord.c */
406
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,
428                                         const char *name,
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);
450
451 /* from dnssock.c */
452
453 DNS_ERROR dns_open_connection( const char *nameserver, int32 dwType,
454                     TALLOC_CTX *mem_ctx,
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);
466
467 /* from dnsmarshall.c */
468
469 struct dns_buffer *dns_create_buffer(TALLOC_CTX *mem_ctx);
470 void dns_marshall_buffer(struct dns_buffer *buf, const uint8 *data,
471                          size_t len);
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,
475                            size_t len);
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);
501
502 /* from dnsgss.c */
503
504 #ifdef HAVE_GSSAPI_SUPPORT
505
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,
509                                  const char *keyname,
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,
514                           const char *keyname,
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,
521                                     size_t num_adds,
522                                     struct dns_update_request **preq);
523
524 #endif  /* HAVE_GSSAPI_SUPPORT */
525
526 #endif  /* _DNS_H */