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 #include <talloc.h>
46 
47 #include "dnserr.h"
48 
49 
50 #define DNS_TCP			1
51 #define DNS_UDP			2
52 
53 #define DNS_OPCODE_UPDATE	1
54 
55 /* DNS Class Types */
56 
57 #define DNS_CLASS_IN		1
58 #define DNS_CLASS_ANY		255
59 #define DNS_CLASS_NONE		254
60 
61 /* DNS RR Types */
62 
63 #define DNS_RR_A		1
64 
65 #define DNS_TCP_PORT		53
66 #define DNS_UDP_PORT		53
67 
68 #define QTYPE_A         1
69 #define QTYPE_NS        2
70 #define QTYPE_MD        3
71 #define QTYPE_CNAME	5
72 #define QTYPE_SOA	6
73 #define QTYPE_AAAA	28
74 #define QTYPE_ANY	255
75 #define	QTYPE_TKEY	249
76 #define QTYPE_TSIG	250
77 
78 /*
79 MF              4 a mail forwarder (Obsolete - use MX)
80 CNAME           5 the canonical name for an alias
81 SOA             6 marks the start of a zone of authority
82 MB              7 a mailbox domain name (EXPERIMENTAL)
83 MG              8 a mail group member (EXPERIMENTAL)
84 MR              9 a mail rename domain name (EXPERIMENTAL)
85 NULL            10 a null RR (EXPERIMENTAL)
86 WKS             11 a well known service description
87 PTR             12 a domain name pointer
88 HINFO           13 host information
89 MINFO           14 mailbox or mail list information
90 MX              15 mail exchange
91 TXT             16 text strings
92 */
93 
94 #define QR_QUERY	 0x0000
95 #define QR_RESPONSE	 0x0001
96 
97 #define OPCODE_QUERY 0x00
98 #define OPCODE_IQUERY	0x01
99 #define OPCODE_STATUS	0x02
100 
101 #define AA			1
102 
103 #define RECURSION_DESIRED	0x01
104 
105 #define RCODE_NOERROR          0
106 #define RCODE_FORMATERROR      1
107 #define RCODE_SERVER_FAILURE   2
108 #define RCODE_NAME_ERROR       3
109 #define RCODE_NOTIMPLEMENTED   4
110 #define RCODE_REFUSED          5
111 
112 #define SENDBUFFER_SIZE		65536
113 #define RECVBUFFER_SIZE		65536
114 
115 /*
116  * TKEY Modes from rfc2930
117  */
118 
119 #define DNS_TKEY_MODE_SERVER   1
120 #define DNS_TKEY_MODE_DH       2
121 #define DNS_TKEY_MODE_GSSAPI   3
122 #define DNS_TKEY_MODE_RESOLVER 4
123 #define DNS_TKEY_MODE_DELETE   5
124 
125 
126 #define DNS_ONE_DAY_IN_SECS	86400
127 #define DNS_TEN_HOURS_IN_SECS	36000
128 
129 #define SOCKET_ERROR 		-1
130 #define INVALID_SOCKET		-1
131 
132 #define  DNS_NO_ERROR		0
133 #define  DNS_FORMAT_ERROR	1
134 #define  DNS_SERVER_FAILURE	2
135 #define  DNS_NAME_ERROR		3
136 #define  DNS_NOT_IMPLEMENTED	4
137 #define  DNS_REFUSED		5
138 
139 typedef long HANDLE;
140 
141 enum dns_ServerType { DNS_SRV_ANY, DNS_SRV_WIN2000, DNS_SRV_WIN2003 };
142 
143 struct dns_domain_label {
144 	struct dns_domain_label *next;
145 	char *label;
146 	size_t len;
147 };
148 
149 struct dns_domain_name {
150 	struct dns_domain_label *pLabelList;
151 };
152 
153 struct dns_question {
154 	struct dns_domain_name *name;
155 	uint16_t q_type;
156 	uint16_t q_class;
157 };
158 
159 /*
160  * Before changing the definition of dns_zone, look
161  * dns_marshall_update_request(), we rely on this being the same as
162  * dns_question right now.
163  */
164 
165 struct dns_zone {
166 	struct dns_domain_name *name;
167 	uint16_t z_type;
168 	uint16_t z_class;
169 };
170 
171 struct dns_rrec {
172 	struct dns_domain_name *name;
173 	uint16_t type;
174 	uint16_t r_class;
175 	uint32_t ttl;
176 	uint16_t data_length;
177 	uint8_t *data;
178 };
179 
180 struct dns_tkey_record {
181 	struct dns_domain_name *algorithm;
182 	time_t inception;
183 	time_t expiration;
184 	uint16_t mode;
185 	uint16_t error;
186 	uint16_t key_length;
187 	uint8_t *key;
188 };
189 
190 struct dns_request {
191 	uint16_t id;
192 	uint16_t flags;
193 	uint16_t num_questions;
194 	uint16_t num_answers;
195 	uint16_t num_auths;
196 	uint16_t num_additionals;
197 	struct dns_question **questions;
198 	struct dns_rrec **answers;
199 	struct dns_rrec **auths;
200 	struct dns_rrec **additionals;
201 };
202 
203 /*
204  * Before changing the definition of dns_update_request, look
205  * dns_marshall_update_request(), we rely on this being the same as
206  * dns_request right now.
207  */
208 
209 struct dns_update_request {
210 	uint16_t id;
211 	uint16_t flags;
212 	uint16_t num_zones;
213 	uint16_t num_preqs;
214 	uint16_t num_updates;
215 	uint16_t num_additionals;
216 	struct dns_zone **zones;
217 	struct dns_rrec **preqs;
218 	struct dns_rrec **updates;
219 	struct dns_rrec **additionals;
220 };
221 
222 struct dns_connection {
223 	int32_t hType;
224 	int s;
225 	struct sockaddr_storage RecvAddr;
226 };
227 
228 struct dns_buffer {
229 	uint8_t *data;
230 	size_t size;
231 	size_t offset;
232 	DNS_ERROR error;
233 };
234 
235 /* from dnsutils.c */
236 
237 DNS_ERROR dns_domain_name_from_string( TALLOC_CTX *mem_ctx,
238 				       const char *pszDomainName,
239 				       struct dns_domain_name **presult );
240 char *dns_generate_keyname( TALLOC_CTX *mem_ctx );
241 
242 /* from dnsrecord.c */
243 
244 DNS_ERROR dns_create_query( TALLOC_CTX *mem_ctx, const char *name,
245 			    uint16_t q_type, uint16_t q_class,
246 			    struct dns_request **preq );
247 DNS_ERROR dns_create_update( TALLOC_CTX *mem_ctx, const char *name,
248 			     struct dns_update_request **preq );
249 DNS_ERROR dns_create_probe(TALLOC_CTX *mem_ctx, const char *zone,
250 			   const char *host, int num_ips,
251 			   const struct sockaddr_storage *sslist,
252 			   struct dns_update_request **preq);
253 DNS_ERROR dns_create_rrec(TALLOC_CTX *mem_ctx, const char *name,
254 			  uint16_t type, uint16_t r_class, uint32_t ttl,
255 			  uint16_t data_length, uint8_t *data,
256 			  struct dns_rrec **prec);
257 DNS_ERROR dns_add_rrec(TALLOC_CTX *mem_ctx, struct dns_rrec *rec,
258 		       uint16_t *num_records, struct dns_rrec ***records);
259 DNS_ERROR dns_create_tkey_record(TALLOC_CTX *mem_ctx, const char *keyname,
260 				 const char *algorithm_name, time_t inception,
261 				 time_t expiration, uint16_t mode, uint16_t error,
262 				 uint16_t key_length, const uint8_t *key,
263 				 struct dns_rrec **prec);
264 DNS_ERROR dns_create_name_in_use_record(TALLOC_CTX *mem_ctx,
265 					const char *name,
266 					const struct sockaddr_storage *ip,
267 					struct dns_rrec **prec);
268 DNS_ERROR dns_create_delete_record(TALLOC_CTX *mem_ctx, const char *name,
269 				   uint16_t type, uint16_t r_class,
270 				   struct dns_rrec **prec);
271 DNS_ERROR dns_create_name_not_in_use_record(TALLOC_CTX *mem_ctx,
272 					    const char *name, uint32_t type,
273 					    struct dns_rrec **prec);
274 DNS_ERROR dns_create_a_record(TALLOC_CTX *mem_ctx, const char *host,
275 			      uint32_t ttl, const struct sockaddr_storage *pss,
276 			      struct dns_rrec **prec);
277 DNS_ERROR dns_create_aaaa_record(TALLOC_CTX *mem_ctx, const char *host,
278 				 uint32_t ttl, const struct sockaddr_storage *pss,
279 				 struct dns_rrec **prec);
280 DNS_ERROR dns_unmarshall_tkey_record(TALLOC_CTX *mem_ctx, struct dns_rrec *rec,
281 				     struct dns_tkey_record **ptkey);
282 DNS_ERROR dns_create_tsig_record(TALLOC_CTX *mem_ctx, const char *keyname,
283 				 const char *algorithm_name,
284 				 time_t time_signed, uint16_t fudge,
285 				 uint16_t mac_length, const uint8_t *mac,
286 				 uint16_t original_id, uint16_t error,
287 				 struct dns_rrec **prec);
288 DNS_ERROR dns_add_rrec(TALLOC_CTX *mem_ctx, struct dns_rrec *rec,
289 		       uint16_t *num_records, struct dns_rrec ***records);
290 DNS_ERROR dns_create_update_request(TALLOC_CTX *mem_ctx,
291 				    const char *domainname,
292 				    const char *hostname,
293 				    const struct sockaddr_storage *ip_addr,
294 				    size_t num_adds,
295 				    struct dns_update_request **preq);
296 
297 /* from dnssock.c */
298 
299 DNS_ERROR dns_open_connection( const char *nameserver, int32_t dwType,
300 		    TALLOC_CTX *mem_ctx,
301 		    struct dns_connection **conn );
302 DNS_ERROR dns_send(struct dns_connection *conn, const struct dns_buffer *buf);
303 DNS_ERROR dns_receive(TALLOC_CTX *mem_ctx, struct dns_connection *conn,
304 		      struct dns_buffer **presult);
305 DNS_ERROR dns_transaction(TALLOC_CTX *mem_ctx, struct dns_connection *conn,
306 			  const struct dns_request *req,
307 			  struct dns_request **resp);
308 DNS_ERROR dns_update_transaction(TALLOC_CTX *mem_ctx,
309 				 struct dns_connection *conn,
310 				 struct dns_update_request *up_req,
311 				 struct dns_update_request **up_resp);
312 
313 /* from dnsmarshall.c */
314 
315 struct dns_buffer *dns_create_buffer(TALLOC_CTX *mem_ctx);
316 void dns_marshall_buffer(struct dns_buffer *buf, const uint8_t *data,
317 			 size_t len);
318 void dns_marshall_uint16(struct dns_buffer *buf, uint16_t val);
319 void dns_marshall_uint32(struct dns_buffer *buf, uint32_t val);
320 void dns_unmarshall_buffer(struct dns_buffer *buf, uint8_t *data,
321 			   size_t len);
322 void dns_unmarshall_uint16(struct dns_buffer *buf, uint16_t *val);
323 void dns_unmarshall_uint32(struct dns_buffer *buf, uint32_t *val);
324 void dns_unmarshall_domain_name(TALLOC_CTX *mem_ctx,
325 				struct dns_buffer *buf,
326 				struct dns_domain_name **pname);
327 void dns_marshall_domain_name(struct dns_buffer *buf,
328 			      const struct dns_domain_name *name);
329 void dns_unmarshall_domain_name(TALLOC_CTX *mem_ctx,
330 				struct dns_buffer *buf,
331 				struct dns_domain_name **pname);
332 DNS_ERROR dns_marshall_request(TALLOC_CTX *mem_ctx,
333 			       const struct dns_request *req,
334 			       struct dns_buffer **pbuf);
335 DNS_ERROR dns_unmarshall_request(TALLOC_CTX *mem_ctx,
336 				 struct dns_buffer *buf,
337 				 struct dns_request **preq);
338 DNS_ERROR dns_marshall_update_request(TALLOC_CTX *mem_ctx,
339 				      struct dns_update_request *update,
340 				      struct dns_buffer **pbuf);
341 DNS_ERROR dns_unmarshall_update_request(TALLOC_CTX *mem_ctx,
342 					struct dns_buffer *buf,
343 					struct dns_update_request **pupreq);
344 struct dns_request *dns_update2request(struct dns_update_request *update);
345 struct dns_update_request *dns_request2update(struct dns_request *request);
346 uint16_t dns_response_code(uint16_t flags);
347 const char *dns_errstr(DNS_ERROR err);
348 
349 /* from dnsgss.c */
350 
351 #ifdef HAVE_GSSAPI
352 
353 void display_status( const char *msg, OM_uint32 maj_stat, OM_uint32 min_stat );
354 DNS_ERROR dns_negotiate_sec_ctx( const char *target_realm,
355 				 const char *servername,
356 				 const char *keyname,
357 				 gss_ctx_id_t *gss_ctx,
358 				 enum dns_ServerType srv_type );
359 DNS_ERROR dns_sign_update(struct dns_update_request *req,
360 			  gss_ctx_id_t gss_ctx,
361 			  const char *keyname,
362 			  const char *algorithmname,
363 			  time_t time_signed, uint16_t fudge);
364 
365 #endif	/* HAVE_GSSAPI */
366 
367 #endif	/* _DNS_H */
368