s4-dns: return the correct TTL
[samba.git] / source4 / dns_server / dlz_bind9.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    bind9 dlz driver for Samba
5
6    Copyright (C) 2010 Andrew Tridgell
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "talloc.h"
24 #include "param/param.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "dsdb/common/util.h"
27 #include "auth/session.h"
28 #include "gen_ndr/ndr_dnsp.h"
29 #include "lib/cmdline/popt_common.h"
30 #include "dlz_bind9.h"
31
32 struct dlz_bind9_data {
33         struct ldb_context *samdb;
34         struct tevent_context *ev_ctx;
35         struct loadparm_context *lp;
36
37         /* helper functions from the dlz_dlopen driver */
38         void (*log)(int level, const char *fmt, ...);
39         isc_result_t (*putrr)(dns_sdlzlookup_t *handle, const char *type,
40                               dns_ttl_t ttl, const char *data);
41         isc_result_t (*putnamedrr)(dns_sdlzlookup_t *handle, const char *name,
42                                    const char *type, dns_ttl_t ttl, const char *data);
43 };
44
45 /*
46   return the version of the API
47  */
48 _PUBLIC_ int dlz_version(unsigned int *flags)
49 {
50         return DLZ_DLOPEN_VERSION;
51 }
52
53 /*
54    remember a helper function from the bind9 dlz_dlopen driver
55  */
56 static void b9_add_helper(struct dlz_bind9_data *state, const char *helper_name, void *ptr)
57 {
58         if (strcmp(helper_name, "log") == 0) {
59                 state->log = ptr;
60         }
61         if (strcmp(helper_name, "putrr") == 0) {
62                 state->putrr = ptr;
63         }
64         if (strcmp(helper_name, "putnamedrr") == 0) {
65                 state->putnamedrr = ptr;
66         }
67 }
68
69 /*
70   format a record for bind9
71  */
72 static bool b9_format(struct dlz_bind9_data *state,
73                       TALLOC_CTX *mem_ctx,
74                       struct dnsp_DnssrvRpcRecord *rec,
75                       const char **type, const char **data)
76 {
77         switch (rec->wType) {
78         case DNS_TYPE_A:
79                 *type = "a";
80                 *data = rec->data.ipv4;
81                 break;
82
83         case DNS_TYPE_AAAA:
84                 *type = "aaaa";
85                 *data = rec->data.ipv6;
86                 break;
87
88         case DNS_TYPE_CNAME:
89                 *type = "cname";
90                 *data = rec->data.cname;
91                 break;
92
93         case DNS_TYPE_TXT:
94                 *type = "txt";
95                 *data = rec->data.txt;
96                 break;
97
98         case DNS_TYPE_PTR:
99                 *type = "ptr";
100                 *data = rec->data.ptr;
101                 break;
102
103         case DNS_TYPE_SRV:
104                 *type = "srv";
105                 *data = talloc_asprintf(mem_ctx, "%u %u %u %s",
106                                         rec->data.srv.wPriority,
107                                         rec->data.srv.wWeight,
108                                         rec->data.srv.wPort,
109                                         rec->data.srv.nameTarget);
110                 break;
111
112         case DNS_TYPE_MX:
113                 *type = "mx";
114                 *data = talloc_asprintf(mem_ctx, "%u %s",
115                                         rec->data.srv.wPriority,
116                                         rec->data.srv.nameTarget);
117                 break;
118
119         case DNS_TYPE_HINFO:
120                 *type = "hinfo";
121                 *data = talloc_asprintf(mem_ctx, "%s %s",
122                                         rec->data.hinfo.cpu,
123                                         rec->data.hinfo.os);
124                 break;
125
126         case DNS_TYPE_NS:
127                 *type = "ns";
128                 *data = rec->data.ns;
129                 break;
130
131         case DNS_TYPE_SOA:
132                 *type = "soa";
133                 *data = talloc_asprintf(mem_ctx, "%s %s %u %u %u %u %u",
134                                         rec->data.soa.mname,
135                                         rec->data.soa.rname,
136                                         rec->data.soa.serial,
137                                         rec->data.soa.refresh,
138                                         rec->data.soa.retry,
139                                         rec->data.soa.expire,
140                                         rec->data.soa.minimum);
141                 break;
142
143         default:
144                 state->log(ISC_LOG_ERROR, "samba b9_putrr: unhandled record type %u",
145                            rec->wType);
146                 return false;
147         }
148
149         return true;
150 }
151
152 /*
153   send a resource recond to bind9
154  */
155 static isc_result_t b9_putrr(struct dlz_bind9_data *state,
156                              void *handle, struct dnsp_DnssrvRpcRecord *rec,
157                              const char **types)
158 {
159         isc_result_t result;
160         const char *type, *data;
161         TALLOC_CTX *tmp_ctx = talloc_new(state);
162
163         if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
164                 return ISC_R_FAILURE;
165         }
166
167         if (data == NULL) {
168                 talloc_free(tmp_ctx);
169                 return ISC_R_NOMEMORY;
170         }
171
172         if (types) {
173                 int i;
174                 for (i=0; types[i]; i++) {
175                         if (strcmp(types[i], type) == 0) break;
176                 }
177                 if (types[i] == NULL) {
178                         /* skip it */
179                         return ISC_R_SUCCESS;
180                 }
181         }
182
183         result = state->putrr(handle, type, rec->dwTtlSeconds, data);
184         if (result != ISC_R_SUCCESS) {
185                 state->log(ISC_LOG_ERROR, "Failed to put rr");
186         }
187         talloc_free(tmp_ctx);
188         return result;
189 }
190
191
192 /*
193   send a named resource recond to bind9
194  */
195 static isc_result_t b9_putnamedrr(struct dlz_bind9_data *state,
196                                   void *handle, const char *name,
197                                   struct dnsp_DnssrvRpcRecord *rec)
198 {
199         isc_result_t result;
200         const char *type, *data;
201         TALLOC_CTX *tmp_ctx = talloc_new(state);
202
203         if (!b9_format(state, tmp_ctx, rec, &type, &data)) {
204                 return ISC_R_FAILURE;
205         }
206
207         if (data == NULL) {
208                 talloc_free(tmp_ctx);
209                 return ISC_R_NOMEMORY;
210         }
211
212         result = state->putnamedrr(handle, name, type, rec->dwTtlSeconds, data);
213         if (result != ISC_R_SUCCESS) {
214                 state->log(ISC_LOG_ERROR, "Failed to put named rr '%s'", name);
215         }
216         talloc_free(tmp_ctx);
217         return result;
218 }
219
220
221 /*
222    parse options
223  */
224 static isc_result_t parse_options(struct dlz_bind9_data *state,
225                                   unsigned int argc, char *argv[])
226 {
227         int opt;
228         poptContext pc;
229         struct poptOption long_options[] = {
230                 POPT_COMMON_SAMBA
231                 { NULL }
232         };
233
234         pc = poptGetContext("dlz_bind9", argc, (const char **)argv, long_options,
235                             POPT_CONTEXT_KEEP_FIRST);
236
237         while ((opt = poptGetNextOpt(pc)) != -1) {
238                 switch (opt) {
239                 default:
240                         state->log(ISC_LOG_ERROR, "Invalid option %s: %s",
241                                    poptBadOption(pc, 0), poptStrerror(opt));
242                         return ISC_R_FAILURE;
243                 }
244         }
245
246         return ISC_R_SUCCESS;
247 }
248
249
250 /*
251   called to initialise the driver
252  */
253 _PUBLIC_ isc_result_t dlz_create(const char *dlzname,
254                                  unsigned int argc, char *argv[],
255                                  void *driverarg, void **dbdata, ...)
256 {
257         struct dlz_bind9_data *state;
258         const char *helper_name;
259         va_list ap;
260         isc_result_t result;
261         const char *url;
262         TALLOC_CTX *tmp_ctx;
263         int ret;
264         struct ldb_dn *dn;
265
266         state = talloc_zero(NULL, struct dlz_bind9_data);
267         if (state == NULL) {
268                 return ISC_R_NOMEMORY;
269         }
270
271         tmp_ctx = talloc_new(state);
272
273         /* fill in the helper functions */
274         va_start(ap, dbdata);
275         while ((helper_name = va_arg(ap, const char *)) != NULL) {
276                 b9_add_helper(state, helper_name, va_arg(ap, void*));
277         }
278         va_end(ap);
279
280         result = parse_options(state, argc, argv);
281         if (result != ISC_R_SUCCESS) {
282                 goto failed;
283         }
284
285         state->lp = loadparm_init_global(true);
286         if (state->lp == NULL) {
287                 result = ISC_R_NOMEMORY;
288                 goto failed;
289         }
290
291         state->ev_ctx = tevent_context_init(state);
292         if (state->ev_ctx == NULL) {
293                 result = ISC_R_NOMEMORY;
294                 goto failed;
295         }
296
297         state->samdb = ldb_init(state, state->ev_ctx);
298         if (state->samdb == NULL) {
299                 state->log(ISC_LOG_ERROR, "samba dlz_bind9: Failed to create ldb");
300                 result = ISC_R_FAILURE;
301                 goto failed;
302         }
303
304         url = talloc_asprintf(tmp_ctx, "ldapi://%s",
305                               private_path(tmp_ctx, state->lp, "ldap_priv/ldapi"));
306         if (url == NULL) {
307                 result = ISC_R_NOMEMORY;
308                 goto failed;
309         }
310
311         ret = ldb_connect(state->samdb, url, 0, NULL);
312         if (ret == -1) {
313                 state->log(ISC_LOG_ERROR, "samba dlz_bind9: Failed to connect to %s - %s",
314                            url, ldb_errstring(state->samdb));
315                 result = ISC_R_FAILURE;
316                 goto failed;
317         }
318
319         dn = ldb_get_default_basedn(state->samdb);
320         if (dn == NULL) {
321                 state->log(ISC_LOG_ERROR, "samba dlz_bind9: Unable to get basedn for %s - %s",
322                            url, ldb_errstring(state->samdb));
323                 result = ISC_R_FAILURE;
324                 goto failed;
325         }
326
327         state->log(ISC_LOG_INFO, "samba dlz_bind9: started for DN %s",
328                    ldb_dn_get_linearized(dn));
329
330         *dbdata = state;
331
332         talloc_free(tmp_ctx);
333         return ISC_R_SUCCESS;
334
335 failed:
336         talloc_free(state);
337         return result;
338 }
339
340 /*
341   shutdown the backend
342  */
343 _PUBLIC_ void dlz_destroy(void *driverarg, void *dbdata)
344 {
345         struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
346         state->log(ISC_LOG_INFO, "samba dlz_bind9: shutting down");
347         talloc_free(state);
348 }
349
350
351 /*
352   see if we handle a given zone
353  */
354 _PUBLIC_ isc_result_t dlz_findzonedb(void *driverarg, void *dbdata, const char *name)
355 {
356         struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
357         if (strcasecmp(lpcfg_dnsdomain(state->lp), name) == 0) {
358                 return ISC_R_SUCCESS;
359         }
360         return ISC_R_NOTFOUND;
361 }
362
363
364 /*
365   lookup one record
366  */
367 _PUBLIC_ isc_result_t dlz_lookup_types(struct dlz_bind9_data *state,
368                                        const char *zone, const char *name,
369                                        void *driverarg, dns_sdlzlookup_t *lookup,
370                                        const char **types)
371 {
372         struct ldb_dn *dn;
373         TALLOC_CTX *tmp_ctx = talloc_new(state);
374         const char *attrs[] = { "dnsRecord", NULL };
375         int ret, i;
376         struct ldb_result *res;
377         struct ldb_message_element *el;
378
379         dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
380         if (dn == NULL) {
381                 talloc_free(tmp_ctx);
382                 return ISC_R_NOMEMORY;
383         }
384
385         if (!ldb_dn_add_child_fmt(dn, "DC=%s,DC=%s,CN=MicrosoftDNS,DC=DomainDnsZones",
386                                   name, zone)) {
387                 talloc_free(tmp_ctx);
388                 return ISC_R_NOMEMORY;
389         }
390
391         ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
392                          attrs, "objectClass=dnsNode");
393         if (ret != LDB_SUCCESS) {
394                 talloc_free(tmp_ctx);
395                 return ISC_R_NOTFOUND;
396         }
397
398         el = ldb_msg_find_element(res->msgs[0], "dnsRecord");
399         if (el == NULL || el->num_values == 0) {
400                 state->log(ISC_LOG_INFO, "failed to find %s",
401                            ldb_dn_get_linearized(dn));
402                 talloc_free(tmp_ctx);
403                 return ISC_R_NOTFOUND;
404         }
405
406         for (i=0; i<el->num_values; i++) {
407                 struct dnsp_DnssrvRpcRecord rec;
408                 enum ndr_err_code ndr_err;
409                 isc_result_t result;
410
411                 ndr_err = ndr_pull_struct_blob(&el->values[i], tmp_ctx, &rec,
412                                                (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
413                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
414                         state->log(ISC_LOG_ERROR, "samba dlz_bind9: failed to parse dnsRecord for %s",
415                                    ldb_dn_get_linearized(dn));
416                         talloc_free(tmp_ctx);
417                         return ISC_R_FAILURE;
418                 }
419
420                 result = b9_putrr(state, lookup, &rec, types);
421                 if (result != ISC_R_SUCCESS) {
422                         talloc_free(tmp_ctx);
423                         return result;
424                 }
425         }
426
427         talloc_free(tmp_ctx);
428         return ISC_R_SUCCESS;
429 }
430
431 /*
432   lookup one record
433  */
434 _PUBLIC_ isc_result_t dlz_lookup(const char *zone, const char *name, void *driverarg,
435                                  void *dbdata, dns_sdlzlookup_t *lookup)
436 {
437         struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
438         return dlz_lookup_types(state, zone, name, driverarg, lookup, NULL);
439 }
440
441
442 /*
443   see if a zone transfer is allowed
444  */
445 _PUBLIC_ isc_result_t dlz_allowzonexfr(void *driverarg, void *dbdata, const char *name,
446                                        const char *client)
447 {
448         struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
449
450         if (strcasecmp(lpcfg_dnsdomain(state->lp), name) == 0) {
451                 /* TODO: check an ACL here? client is the IP of the requester */
452                 state->log(ISC_LOG_INFO, "samba dlz_bind9: allowing zone transfer for '%s' by '%s'",
453                            name, client);
454                 return ISC_R_SUCCESS;
455         }
456         return ISC_R_NOTFOUND;
457 }
458
459 /*
460   perform a zone transfer
461  */
462 _PUBLIC_ isc_result_t dlz_allnodes(const char *zone, void *driverarg, void *dbdata,
463                                    dns_sdlzallnodes_t *allnodes)
464 {
465         struct dlz_bind9_data *state = talloc_get_type_abort(dbdata, struct dlz_bind9_data);
466         const char *attrs[] = { "dnsRecord", NULL };
467         int ret, i, j;
468         struct ldb_dn *dn;
469         struct ldb_result *res;
470         TALLOC_CTX *tmp_ctx = talloc_new(state);
471
472
473         dn = ldb_dn_copy(tmp_ctx, ldb_get_default_basedn(state->samdb));
474         if (dn == NULL) {
475                 talloc_free(tmp_ctx);
476                 return ISC_R_NOMEMORY;
477         }
478
479         if (!ldb_dn_add_child_fmt(dn, "DC=%s,CN=MicrosoftDNS,DC=DomainDnsZones", zone)) {
480                 talloc_free(tmp_ctx);
481                 return ISC_R_NOMEMORY;
482         }
483
484         ret = ldb_search(state->samdb, tmp_ctx, &res, dn, LDB_SCOPE_SUBTREE,
485                          attrs, "objectClass=dnsNode");
486         if (ret != LDB_SUCCESS) {
487                 talloc_free(tmp_ctx);
488                 return ISC_R_NOTFOUND;
489         }
490
491         for (i=0; i<res->count; i++) {
492                 struct ldb_message_element *el;
493                 TALLOC_CTX *el_ctx = talloc_new(tmp_ctx);
494                 const char *rdn, *name;
495                 const struct ldb_val *v;
496
497                 el = ldb_msg_find_element(res->msgs[i], "dnsRecord");
498                 if (el == NULL || el->num_values == 0) {
499                         state->log(ISC_LOG_INFO, "failed to find dnsRecord for %s",
500                                    ldb_dn_get_linearized(dn));
501                         talloc_free(el_ctx);
502                         continue;
503                 }
504
505                 v = ldb_dn_get_rdn_val(res->msgs[i]->dn);
506                 if (v == NULL) {
507                         state->log(ISC_LOG_INFO, "failed to find RDN for %s",
508                                    ldb_dn_get_linearized(dn));
509                         talloc_free(el_ctx);
510                         continue;
511                 }
512
513                 rdn = talloc_strndup(el_ctx, (char *)v->data, v->length);
514                 if (rdn == NULL) {
515                         talloc_free(tmp_ctx);
516                         return ISC_R_NOMEMORY;
517                 }
518
519                 if (strcmp(rdn, "@") == 0) {
520                         name = zone;
521                 } else {
522                         name = talloc_asprintf(el_ctx, "%s.%s", rdn, zone);
523                 }
524                 if (name == NULL) {
525                         talloc_free(tmp_ctx);
526                         return ISC_R_NOMEMORY;
527                 }
528
529                 for (j=0; j<el->num_values; j++) {
530                         struct dnsp_DnssrvRpcRecord rec;
531                         enum ndr_err_code ndr_err;
532                         isc_result_t result;
533
534                         ndr_err = ndr_pull_struct_blob(&el->values[j], el_ctx, &rec,
535                                                        (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
536                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
537                                 state->log(ISC_LOG_ERROR, "samba dlz_bind9: failed to parse dnsRecord for %s",
538                                            ldb_dn_get_linearized(dn));
539                                 talloc_free(el_ctx);
540                                 continue;
541                         }
542
543                         result = b9_putnamedrr(state, allnodes, name, &rec);
544                         if (result != ISC_R_SUCCESS) {
545                                 talloc_free(el_ctx);
546                                 continue;
547                         }
548                 }
549         }
550
551         talloc_free(tmp_ctx);
552
553         return ISC_R_SUCCESS;
554 }