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