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