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