samdb: Add remote address to connect
[amitay/samba.git] / source4 / torture / dns / dlz_bind9.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Bartlett 2012
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "torture/smbtorture.h"
22 #include "dns_server/dlz_minimal.h"
23 #include <talloc.h>
24 #include <ldb.h>
25 #include "lib/param/param.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "dsdb/common/util.h"
28 #include "auth/session.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/credentials/credentials.h"
31 #include "lib/cmdline/popt_common.h"
32
33 struct torture_context *tctx_static;
34
35 static void dlz_bind9_log_wrapper(int level, const char *fmt, ...)
36                                   PRINTF_ATTRIBUTE(2,3);
37
38 static void dlz_bind9_log_wrapper(int level, const char *fmt, ...)
39 {
40         va_list ap;
41         char *msg;
42         va_start(ap, fmt);
43         msg = talloc_vasprintf(NULL, fmt, ap);
44         torture_comment(tctx_static, "%s\n", msg);
45         TALLOC_FREE(msg);
46         va_end(ap);
47 }
48
49 static bool test_dlz_bind9_version(struct torture_context *tctx)
50 {
51         unsigned int flags = 0;
52         torture_assert_int_equal(tctx, dlz_version(&flags),
53                                  DLZ_DLOPEN_VERSION, "got wrong DLZ version");
54         return true;
55 }
56
57 static char *test_dlz_bind9_binddns_dir(struct torture_context *tctx,
58                                         const char *file)
59 {
60         return talloc_asprintf(tctx,
61                                "ldb://%s/%s",
62                                lpcfg_binddns_dir(tctx->lp_ctx),
63                                file);
64 }
65
66 static bool test_dlz_bind9_create(struct torture_context *tctx)
67 {
68         void *dbdata;
69         const char *argv[] = {
70                 "samba_dlz",
71                 "-H",
72                 test_dlz_bind9_binddns_dir(tctx, "dns/sam.ldb"),
73                 NULL
74         };
75         tctx_static = tctx;
76         torture_assert_int_equal(tctx, dlz_create("samba_dlz", 3, argv, &dbdata,
77                                                   "log", dlz_bind9_log_wrapper, NULL), ISC_R_SUCCESS,
78                 "Failed to create samba_dlz");
79
80         dlz_destroy(dbdata);
81
82         return true;
83 }
84
85 static isc_result_t dlz_bind9_writeable_zone_hook(dns_view_t *view,
86                                            const char *zone_name)
87 {
88         struct torture_context *tctx = talloc_get_type((void *)view, struct torture_context);
89         struct ldb_context *samdb = NULL;
90         char *errstring = NULL;
91         int ret = samdb_connect_url(
92                         tctx,
93                         NULL,
94                         tctx->lp_ctx,
95                         system_session(tctx->lp_ctx),
96                         0,
97                         test_dlz_bind9_binddns_dir(tctx, "dns/sam.ldb"),
98                         NULL,
99                         &samdb,
100                         &errstring);
101         struct ldb_message *msg;
102         const char *attrs[] = {
103                 NULL
104         };
105         if (ret != LDB_SUCCESS) {
106                 torture_fail(tctx, "Failed to connect to samdb");
107                 return ISC_R_FAILURE;
108         }
109
110         ret = dsdb_search_one(samdb, tctx, &msg, NULL,
111                               LDB_SCOPE_SUBTREE, attrs, DSDB_SEARCH_SEARCH_ALL_PARTITIONS,
112                               "(&(objectClass=dnsZone)(name=%s))", zone_name);
113         if (ret != LDB_SUCCESS) {
114                 torture_fail(tctx, talloc_asprintf(tctx, "Failed to search for %s: %s", zone_name, ldb_errstring(samdb)));
115                 return ISC_R_FAILURE;
116         }
117         talloc_free(msg);
118
119         return ISC_R_SUCCESS;
120 }
121
122 static bool test_dlz_bind9_configure(struct torture_context *tctx)
123 {
124         void *dbdata;
125         const char *argv[] = {
126                 "samba_dlz",
127                 "-H",
128                 test_dlz_bind9_binddns_dir(tctx, "dns/sam.ldb"),
129                 NULL
130         };
131         tctx_static = tctx;
132         torture_assert_int_equal(tctx, dlz_create("samba_dlz", 3, argv, &dbdata,
133                                                   "log", dlz_bind9_log_wrapper,
134                                                   "writeable_zone", dlz_bind9_writeable_zone_hook, NULL),
135                                  ISC_R_SUCCESS,
136                                  "Failed to create samba_dlz");
137
138         torture_assert_int_equal(tctx, dlz_configure((void*)tctx, dbdata),
139                                                      ISC_R_SUCCESS,
140                                  "Failed to configure samba_dlz");
141
142         dlz_destroy(dbdata);
143
144         return true;
145 }
146
147 /*
148  * Test that a ticket obtained for the DNS service will be accepted on the Samba DLZ side
149  *
150  */
151 static bool test_dlz_bind9_gensec(struct torture_context *tctx, const char *mech)
152 {
153         NTSTATUS status;
154
155         struct gensec_security *gensec_client_context;
156
157         DATA_BLOB client_to_server, server_to_client;
158
159         void *dbdata;
160         const char *argv[] = {
161                 "samba_dlz",
162                 "-H",
163                 test_dlz_bind9_binddns_dir(tctx, "dns/sam.ldb"),
164                 NULL
165         };
166         tctx_static = tctx;
167         torture_assert_int_equal(tctx, dlz_create("samba_dlz", 3, argv, &dbdata,
168                                                   "log", dlz_bind9_log_wrapper,
169                                                   "writeable_zone", dlz_bind9_writeable_zone_hook, NULL),
170                                  ISC_R_SUCCESS,
171                                  "Failed to create samba_dlz");
172
173         torture_assert_int_equal(tctx, dlz_configure((void*)tctx, dbdata),
174                                                      ISC_R_SUCCESS,
175                                  "Failed to configure samba_dlz");
176
177         status = gensec_client_start(tctx, &gensec_client_context,
178                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx));
179         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
180
181         /*
182          * dlz_bind9 use the special dns/host.domain account
183          */
184         status = gensec_set_target_hostname(gensec_client_context,
185                                             talloc_asprintf(tctx,
186                                 "%s.%s",
187                                 torture_setting_string(tctx, "host", NULL),
188                                 lpcfg_dnsdomain(tctx->lp_ctx)));
189         torture_assert_ntstatus_ok(tctx, status, "gensec_set_target_hostname (client) failed");
190
191         status = gensec_set_target_service(gensec_client_context, "dns");
192         torture_assert_ntstatus_ok(tctx, status, "gensec_set_target_service failed");
193
194         status = gensec_set_credentials(gensec_client_context,
195                         popt_get_cmdline_credentials());
196         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
197
198         status = gensec_start_mech_by_sasl_name(gensec_client_context, mech);
199         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
200
201         server_to_client = data_blob(NULL, 0);
202
203         /* Do one step of the client-server update dance */
204         status = gensec_update(gensec_client_context, tctx, server_to_client, &client_to_server);
205         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
206                 torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
207         }
208
209         torture_assert_int_equal(tctx, dlz_ssumatch(
210                                         cli_credentials_get_username(
211                                                 popt_get_cmdline_credentials()),
212                                         lpcfg_dnsdomain(tctx->lp_ctx),
213                                         "127.0.0.1", "type", "key",
214                                         client_to_server.length,
215                                         client_to_server.data,
216                                         dbdata),
217                                         ISC_TRUE,
218                          "Failed to check key for update rights samba_dlz");
219
220         dlz_destroy(dbdata);
221
222         return true;
223 }
224
225 static bool test_dlz_bind9_gssapi(struct torture_context *tctx)
226 {
227         return test_dlz_bind9_gensec(tctx, "GSSAPI");
228 }
229
230 static bool test_dlz_bind9_spnego(struct torture_context *tctx)
231 {
232         return test_dlz_bind9_gensec(tctx, "GSS-SPNEGO");
233 }
234
235 struct test_expected_record {
236         const char *name;
237         const char *type;
238         const char *data;
239         int ttl;
240         bool printed;
241 };
242
243 struct test_expected_rr {
244         struct torture_context *tctx;
245         const char *query_name;
246         size_t num_records;
247         struct test_expected_record *records;
248         size_t num_rr;
249 };
250
251 static bool dlz_bind9_putnamedrr_torture_hook(struct test_expected_rr *expected,
252                                               const char *name,
253                                               const char *type,
254                                               dns_ttl_t ttl,
255                                               const char *data)
256 {
257         size_t i;
258
259         torture_assert(expected->tctx, name != NULL,
260                        talloc_asprintf(expected->tctx,
261                        "Got unnamed record type[%s] data[%s]\n",
262                        type, data));
263
264         expected->num_rr++;
265         torture_comment(expected->tctx, "%u: name[%s] type[%s] ttl[%u] data[%s]\n",
266                         (unsigned)expected->num_rr, name, type, (unsigned)ttl, data);
267
268         for (i = 0; i < expected->num_records; i++) {
269                 if (expected->records[i].name != NULL) {
270                         if (strcmp(name, expected->records[i].name) != 0) {
271                                 continue;
272                         }
273                 }
274
275                 if (strcmp(type, expected->records[i].type) != 0) {
276                         continue;
277                 }
278
279                 if (expected->records[i].data != NULL) {
280                         if (strcmp(data, expected->records[i].data) != 0) {
281                                 continue;
282                         }
283                 }
284
285                 torture_assert_int_equal(expected->tctx, ttl,
286                                          expected->records[i].ttl,
287                                          talloc_asprintf(expected->tctx,
288                                          "TTL did not match expectations for type %s",
289                                          type));
290
291                 expected->records[i].printed = true;
292         }
293
294         return true;
295 }
296
297 static isc_result_t dlz_bind9_putrr_hook(dns_sdlzlookup_t *lookup,
298                                          const char *type,
299                                          dns_ttl_t ttl,
300                                          const char *data)
301 {
302         struct test_expected_rr *expected =
303                 talloc_get_type_abort(lookup, struct test_expected_rr);
304         bool ok;
305
306         ok = dlz_bind9_putnamedrr_torture_hook(expected, expected->query_name,
307                                                type, ttl, data);
308         if (!ok) {
309                 return ISC_R_FAILURE;
310         }
311
312         return ISC_R_SUCCESS;
313 }
314
315 static isc_result_t dlz_bind9_putnamedrr_hook(dns_sdlzallnodes_t *allnodes,
316                                               const char *name,
317                                               const char *type,
318                                               dns_ttl_t ttl,
319                                               const char *data)
320 {
321         struct test_expected_rr *expected =
322                 talloc_get_type_abort(allnodes, struct test_expected_rr);
323         bool ok;
324
325         ok = dlz_bind9_putnamedrr_torture_hook(expected, name, type, ttl, data);
326         if (!ok) {
327                 return ISC_R_FAILURE;
328         }
329
330         return ISC_R_SUCCESS;
331 }
332
333 /*
334  * Tests some lookups
335  */
336 static bool test_dlz_bind9_lookup(struct torture_context *tctx)
337 {
338         size_t i;
339         void *dbdata;
340         const char *argv[] = {
341                 "samba_dlz",
342                 "-H",
343                 test_dlz_bind9_binddns_dir(tctx, "dns/sam.ldb"),
344                 NULL
345         };
346         struct test_expected_rr *expected1 = NULL;
347         struct test_expected_rr *expected2 = NULL;
348
349         tctx_static = tctx;
350         torture_assert_int_equal(tctx, dlz_create("samba_dlz", 3, argv, &dbdata,
351                                                   "log", dlz_bind9_log_wrapper,
352                                                   "writeable_zone", dlz_bind9_writeable_zone_hook,
353                                                   "putrr", dlz_bind9_putrr_hook,
354                                                   "putnamedrr", dlz_bind9_putnamedrr_hook,
355                                                   NULL),
356                                  ISC_R_SUCCESS,
357                                  "Failed to create samba_dlz");
358
359         torture_assert_int_equal(tctx, dlz_configure((void*)tctx, dbdata),
360                                                      ISC_R_SUCCESS,
361                                  "Failed to configure samba_dlz");
362
363         expected1 = talloc_zero(tctx, struct test_expected_rr);
364         torture_assert(tctx, expected1 != NULL, "talloc failed");
365         expected1->tctx = tctx;
366
367         expected1->query_name = "@";
368
369         expected1->num_records = 4;
370         expected1->records = talloc_zero_array(expected1,
371                                                struct test_expected_record,
372                                                expected1->num_records);
373         torture_assert(tctx, expected1->records != NULL, "talloc failed");
374
375         expected1->records[0].name = expected1->query_name;
376         expected1->records[0].type = "soa";
377         expected1->records[0].ttl = 3600;
378         expected1->records[0].data = talloc_asprintf(expected1->records,
379                                 "%s.%s. hostmaster.%s. 1 900 600 86400 3600",
380                                 torture_setting_string(tctx, "host", NULL),
381                                 lpcfg_dnsdomain(tctx->lp_ctx),
382                                 lpcfg_dnsdomain(tctx->lp_ctx));
383         torture_assert(tctx, expected1->records[0].data != NULL, "talloc failed");
384
385         expected1->records[1].name = expected1->query_name;
386         expected1->records[1].type = "ns";
387         expected1->records[1].ttl = 900;
388         expected1->records[1].data = talloc_asprintf(expected1->records, "%s.%s.",
389                                 torture_setting_string(tctx, "host", NULL),
390                                 lpcfg_dnsdomain(tctx->lp_ctx));
391         torture_assert(tctx, expected1->records[1].data != NULL, "talloc failed");
392
393         expected1->records[2].name = expected1->query_name;
394         expected1->records[2].type = "aaaa";
395         expected1->records[2].ttl = 900;
396
397         expected1->records[3].name = expected1->query_name;
398         expected1->records[3].type = "a";
399         expected1->records[3].ttl = 900;
400
401         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
402                                                   expected1->query_name, dbdata,
403                                                   (dns_sdlzlookup_t *)expected1),
404                                  ISC_R_SUCCESS,
405                                  "Failed to lookup @");
406         for (i = 0; i < expected1->num_records; i++) {
407                 torture_assert(tctx, expected1->records[i].printed,
408                                talloc_asprintf(tctx,
409                                "Failed to have putrr callback run for type %s",
410                                expected1->records[i].type));
411         }
412         torture_assert_int_equal(tctx, expected1->num_rr,
413                                  expected1->num_records,
414                                  "Got too much data");
415
416         expected2 = talloc_zero(tctx, struct test_expected_rr);
417         torture_assert(tctx, expected2 != NULL, "talloc failed");
418         expected2->tctx = tctx;
419
420         expected2->query_name = torture_setting_string(tctx, "host", NULL);
421         torture_assert(tctx, expected2->query_name != NULL, "unknown host");
422
423         expected2->num_records = 2;
424         expected2->records = talloc_zero_array(expected2,
425                                                struct test_expected_record,
426                                                expected2->num_records);
427         torture_assert(tctx, expected2->records != NULL, "talloc failed");
428
429         expected2->records[0].name = expected2->query_name;
430         expected2->records[0].type = "aaaa";
431         expected2->records[0].ttl = 900;
432
433         expected2->records[1].name = expected2->query_name;
434         expected2->records[1].type = "a";
435         expected2->records[1].ttl = 900;
436
437         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
438                                                   expected2->query_name, dbdata,
439                                                   (dns_sdlzlookup_t *)expected2),
440                                  ISC_R_SUCCESS,
441                                  "Failed to lookup hostname");
442         for (i = 0; i < expected2->num_records; i++) {
443                 torture_assert(tctx, expected2->records[i].printed,
444                                talloc_asprintf(tctx,
445                                "Failed to have putrr callback run name[%s] for type %s",
446                                expected2->records[i].name,
447                                expected2->records[i].type));
448         }
449         torture_assert_int_equal(tctx, expected2->num_rr,
450                                  expected2->num_records,
451                                  "Got too much data");
452
453         dlz_destroy(dbdata);
454
455         return true;
456 }
457
458 /*
459  * Test some zone dumps
460  */
461 static bool test_dlz_bind9_zonedump(struct torture_context *tctx)
462 {
463         size_t i;
464         void *dbdata;
465         const char *argv[] = {
466                 "samba_dlz",
467                 "-H",
468                 test_dlz_bind9_binddns_dir(tctx, "dns/sam.ldb"),
469                 NULL
470         };
471         struct test_expected_rr *expected1 = NULL;
472
473         tctx_static = tctx;
474         torture_assert_int_equal(tctx, dlz_create("samba_dlz", 3, argv, &dbdata,
475                                                   "log", dlz_bind9_log_wrapper,
476                                                   "writeable_zone", dlz_bind9_writeable_zone_hook,
477                                                   "putrr", dlz_bind9_putrr_hook,
478                                                   "putnamedrr", dlz_bind9_putnamedrr_hook,
479                                                   NULL),
480                                  ISC_R_SUCCESS,
481                                  "Failed to create samba_dlz");
482
483         torture_assert_int_equal(tctx, dlz_configure((void*)tctx, dbdata),
484                                                      ISC_R_SUCCESS,
485                                  "Failed to configure samba_dlz");
486
487         expected1 = talloc_zero(tctx, struct test_expected_rr);
488         torture_assert(tctx, expected1 != NULL, "talloc failed");
489         expected1->tctx = tctx;
490
491         expected1->num_records = 7;
492         expected1->records = talloc_zero_array(expected1,
493                                                struct test_expected_record,
494                                                expected1->num_records);
495         torture_assert(tctx, expected1->records != NULL, "talloc failed");
496
497         expected1->records[0].name = talloc_asprintf(expected1->records,
498                                 "%s.", lpcfg_dnsdomain(tctx->lp_ctx));
499         expected1->records[0].type = "soa";
500         expected1->records[0].ttl = 3600;
501         expected1->records[0].data = talloc_asprintf(expected1->records,
502                                 "%s.%s. hostmaster.%s. 1 900 600 86400 3600",
503                                 torture_setting_string(tctx, "host", NULL),
504                                 lpcfg_dnsdomain(tctx->lp_ctx),
505                                 lpcfg_dnsdomain(tctx->lp_ctx));
506         torture_assert(tctx, expected1->records[0].data != NULL, "talloc failed");
507
508         expected1->records[1].name = talloc_asprintf(expected1->records,
509                                 "%s.", lpcfg_dnsdomain(tctx->lp_ctx));
510         expected1->records[1].type = "ns";
511         expected1->records[1].ttl = 900;
512         expected1->records[1].data = talloc_asprintf(expected1->records, "%s.%s.",
513                                 torture_setting_string(tctx, "host", NULL),
514                                 lpcfg_dnsdomain(tctx->lp_ctx));
515         torture_assert(tctx, expected1->records[1].data != NULL, "talloc failed");
516
517         expected1->records[2].name = talloc_asprintf(expected1->records,
518                                 "%s.", lpcfg_dnsdomain(tctx->lp_ctx));
519         expected1->records[2].type = "aaaa";
520         expected1->records[2].ttl = 900;
521
522         expected1->records[3].name = talloc_asprintf(expected1->records,
523                                 "%s.", lpcfg_dnsdomain(tctx->lp_ctx));
524         expected1->records[3].type = "a";
525         expected1->records[3].ttl = 900;
526
527         expected1->records[4].name = talloc_asprintf(expected1->records, "%s.%s.",
528                                 torture_setting_string(tctx, "host", NULL),
529                                 lpcfg_dnsdomain(tctx->lp_ctx));
530         torture_assert(tctx, expected1->records[4].name != NULL, "unknown host");
531         expected1->records[4].type = "aaaa";
532         expected1->records[4].ttl = 900;
533
534         expected1->records[5].name = talloc_asprintf(expected1->records, "%s.%s.",
535                                 torture_setting_string(tctx, "host", NULL),
536                                 lpcfg_dnsdomain(tctx->lp_ctx));
537         torture_assert(tctx, expected1->records[5].name != NULL, "unknown host");
538         expected1->records[5].type = "a";
539         expected1->records[5].ttl = 900;
540
541         /*
542          * We expect multiple srv records
543          */
544         expected1->records[6].name = NULL;
545         expected1->records[6].type = "srv";
546         expected1->records[6].ttl = 900;
547
548         torture_assert_int_equal(tctx, dlz_allnodes(lpcfg_dnsdomain(tctx->lp_ctx),
549                                                     dbdata, (dns_sdlzallnodes_t *)expected1),
550                                  ISC_R_SUCCESS,
551                                  "Failed to configure samba_dlz");
552         for (i = 0; i < expected1->num_records; i++) {
553                 torture_assert(tctx, expected1->records[i].printed,
554                                talloc_asprintf(tctx,
555                                "Failed to have putrr callback run name[%s] for type %s",
556                                expected1->records[i].name,
557                                expected1->records[i].type));
558         }
559         torture_assert_int_equal(tctx, expected1->num_rr, 24,
560                                  "Got wrong record count");
561
562         dlz_destroy(dbdata);
563
564         return true;
565 }
566
567 /*
568  * Test some updates
569  */
570 static bool test_dlz_bind9_update01(struct torture_context *tctx)
571 {
572         NTSTATUS status;
573         struct gensec_security *gensec_client_context;
574         DATA_BLOB client_to_server, server_to_client;
575         void *dbdata;
576         void *version = NULL;
577         const char *argv[] = {
578                 "samba_dlz",
579                 "-H",
580                 test_dlz_bind9_binddns_dir(tctx, "dns/sam.ldb"),
581                 NULL
582         };
583         struct test_expected_rr *expected1 = NULL;
584         char *name = NULL;
585         char *data0 = NULL;
586         char *data1 = NULL;
587         char *data2 = NULL;
588         bool ret = false;
589
590         tctx_static = tctx;
591         torture_assert_int_equal(tctx, dlz_create("samba_dlz", 3, argv, &dbdata,
592                                                   "log", dlz_bind9_log_wrapper,
593                                                   "writeable_zone", dlz_bind9_writeable_zone_hook,
594                                                   "putrr", dlz_bind9_putrr_hook,
595                                                   "putnamedrr", dlz_bind9_putnamedrr_hook,
596                                                   NULL),
597                                  ISC_R_SUCCESS,
598                                  "Failed to create samba_dlz");
599
600         torture_assert_int_equal(tctx, dlz_configure((void*)tctx, dbdata),
601                                                      ISC_R_SUCCESS,
602                                  "Failed to configure samba_dlz");
603
604         expected1 = talloc_zero(tctx, struct test_expected_rr);
605         torture_assert(tctx, expected1 != NULL, "talloc failed");
606         expected1->tctx = tctx;
607
608         expected1->query_name = __func__;
609
610         name = talloc_asprintf(expected1, "%s.%s",
611                                 expected1->query_name,
612                                 lpcfg_dnsdomain(tctx->lp_ctx));
613         torture_assert(tctx, name != NULL, "talloc failed");
614
615         expected1->num_records = 2;
616         expected1->records = talloc_zero_array(expected1,
617                                                struct test_expected_record,
618                                                expected1->num_records);
619         torture_assert(tctx, expected1->records != NULL, "talloc failed");
620
621         expected1->records[0].name = expected1->query_name;
622         expected1->records[0].type = "a";
623         expected1->records[0].ttl = 3600;
624         expected1->records[0].data = "127.1.2.3";
625         expected1->records[0].printed = false;
626
627         data0 = talloc_asprintf(expected1,
628                                 "%s.\t" "%u\t" "%s\t" "%s\t" "%s",
629                                 name,
630                                 (unsigned)expected1->records[0].ttl,
631                                 "in",
632                                 expected1->records[0].type,
633                                 expected1->records[0].data);
634         torture_assert(tctx, data0 != NULL, "talloc failed");
635
636         expected1->records[1].name = expected1->query_name;
637         expected1->records[1].type = "a";
638         expected1->records[1].ttl = 3600;
639         expected1->records[1].data = "127.3.2.1";
640         expected1->records[1].printed = false;
641
642         data1 = talloc_asprintf(expected1,
643                                 "%s.\t" "%u\t" "%s\t" "%s\t" "%s",
644                                 name,
645                                 (unsigned)expected1->records[1].ttl,
646                                 "in",
647                                 expected1->records[1].type,
648                                 expected1->records[1].data);
649         torture_assert(tctx, data1 != NULL, "talloc failed");
650
651         data2 = talloc_asprintf(expected1,
652                                 "%s.\t" "0\t" "in\t" "a\t" "127.3.3.3",
653                                 name);
654         torture_assert(tctx, data2 != NULL, "talloc failed");
655
656         /*
657          * Prepare session info
658          */
659         status = gensec_client_start(tctx, &gensec_client_context,
660                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx));
661         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
662
663         /*
664          * dlz_bind9 use the special dns/host.domain account
665          */
666         status = gensec_set_target_hostname(gensec_client_context,
667                                             talloc_asprintf(tctx,
668                                 "%s.%s",
669                                 torture_setting_string(tctx, "host", NULL),
670                                 lpcfg_dnsdomain(tctx->lp_ctx)));
671         torture_assert_ntstatus_ok(tctx, status, "gensec_set_target_hostname (client) failed");
672
673         status = gensec_set_target_service(gensec_client_context, "dns");
674         torture_assert_ntstatus_ok(tctx, status, "gensec_set_target_service failed");
675
676         status = gensec_set_credentials(gensec_client_context,
677                         popt_get_cmdline_credentials());
678         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
679
680         status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSS-SPNEGO");
681         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
682
683         server_to_client = data_blob(NULL, 0);
684
685         /* Do one step of the client-server update dance */
686         status = gensec_update(gensec_client_context, tctx, server_to_client, &client_to_server);
687         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
688                 torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
689         }
690
691         torture_assert_int_equal(tctx, dlz_ssumatch(
692                                 cli_credentials_get_username(
693                                         popt_get_cmdline_credentials()),
694                                 name,
695                                 "127.0.0.1",
696                                 expected1->records[0].type,
697                                 "key",
698                                 client_to_server.length,
699                                 client_to_server.data,
700                                 dbdata),
701                                 ISC_TRUE,
702                          "Failed to check key for update rights samba_dlz");
703
704         /*
705          * We test the following:
706          *
707          *  1. lookup the records => NOT_FOUND
708          *  2. delete all records => NOT_FOUND
709          *  3. delete 1st record => NOT_FOUND
710          *  4. create 1st record => SUCCESS
711          *  5. lookup the records => found 1st
712          *  6. create 2nd record => SUCCESS
713          *  7. lookup the records => found 1st and 2nd
714          *  8. delete unknown record => NOT_FOUND
715          *  9. lookup the records => found 1st and 2nd
716          * 10. delete 1st record => SUCCESS
717          * 11. lookup the records => found 2nd
718          * 12. delete 2nd record => SUCCESS
719          * 13. lookup the records => NOT_FOUND
720          * 14. create 1st record => SUCCESS
721          * 15. lookup the records => found 1st
722          * 16. create 2nd record => SUCCESS
723          * 17. lookup the records => found 1st and 2nd
724          * 18. update 1st record => SUCCESS
725          * 19. lookup the records => found 1st and 2nd
726          * 20. delete all unknown type records => NOT_FOUND
727          * 21. lookup the records => found 1st and 2nd
728          * 22. delete all records => SUCCESS
729          * 23. lookup the records => NOT_FOUND
730          */
731
732         /* Step 1. */
733         expected1->num_rr = 0;
734         expected1->records[0].printed = false;
735         expected1->records[1].printed = false;
736         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
737                                                   expected1->query_name, dbdata,
738                                                   (dns_sdlzlookup_t *)expected1),
739                                  ISC_R_NOTFOUND,
740                                  "Found hostname");
741         torture_assert_int_equal(tctx, expected1->num_rr, 0,
742                                  "Got wrong record count");
743
744         /* Step 2. */
745         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
746                                                       dbdata, &version),
747                                  ISC_R_SUCCESS,
748                                  "Failed to start transaction");
749         torture_assert_int_equal_goto(tctx,
750                         dlz_delrdataset(name,
751                                         expected1->records[0].type,
752                                         dbdata, version),
753                         ISC_R_NOTFOUND, ret, cancel_version,
754                         talloc_asprintf(tctx, "Deleted name[%s] type[%s]\n",
755                         name, expected1->records[0].type));
756         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), false, dbdata, &version);
757
758         /* Step 3. */
759         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
760                                                       dbdata, &version),
761                                  ISC_R_SUCCESS,
762                                  "Failed to start transaction");
763         torture_assert_int_equal_goto(tctx,
764                         dlz_subrdataset(name, data0, dbdata, version),
765                         ISC_R_NOTFOUND, ret, cancel_version,
766                         talloc_asprintf(tctx, "Deleted name[%s] data[%s]\n",
767                         name, data0));
768         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), false, dbdata, &version);
769
770         /* Step 4. */
771         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
772                                                       dbdata, &version),
773                                  ISC_R_SUCCESS,
774                                  "Failed to start transaction");
775         torture_assert_int_equal_goto(tctx,
776                         dlz_addrdataset(name, data0, dbdata, version),
777                         ISC_R_SUCCESS, ret, cancel_version,
778                         talloc_asprintf(tctx, "Failed to add name[%s] data[%s]\n",
779                         name, data0));
780         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), true, dbdata, &version);
781
782         /* Step 5. */
783         expected1->num_rr = 0;
784         expected1->records[0].printed = false;
785         expected1->records[1].printed = false;
786         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
787                                                   expected1->query_name, dbdata,
788                                                   (dns_sdlzlookup_t *)expected1),
789                                  ISC_R_SUCCESS,
790                                  "Not found hostname");
791         torture_assert(tctx, expected1->records[0].printed,
792                        talloc_asprintf(tctx,
793                        "Failed to have putrr callback run name[%s] for type %s",
794                        expected1->records[0].name,
795                        expected1->records[0].type));
796         torture_assert_int_equal(tctx, expected1->num_rr, 1,
797                                  "Got wrong record count");
798
799         /* Step 6. */
800         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
801                                                       dbdata, &version),
802                                  ISC_R_SUCCESS,
803                                  "Failed to start transaction");
804         torture_assert_int_equal_goto(tctx,
805                         dlz_addrdataset(name, data1, dbdata, version),
806                         ISC_R_SUCCESS, ret, cancel_version,
807                         talloc_asprintf(tctx, "Failed to add name[%s] data[%s]\n",
808                         name, data1));
809         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), true, dbdata, &version);
810
811         /* Step 7. */
812         expected1->num_rr = 0;
813         expected1->records[0].printed = false;
814         expected1->records[1].printed = false;
815         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
816                                                   expected1->query_name, dbdata,
817                                                   (dns_sdlzlookup_t *)expected1),
818                                  ISC_R_SUCCESS,
819                                  "Not found hostname");
820         torture_assert(tctx, expected1->records[0].printed,
821                        talloc_asprintf(tctx,
822                        "Failed to have putrr callback run name[%s] for type %s",
823                        expected1->records[0].name,
824                        expected1->records[0].type));
825         torture_assert(tctx, expected1->records[1].printed,
826                        talloc_asprintf(tctx,
827                        "Failed to have putrr callback run name[%s] for type %s",
828                        expected1->records[1].name,
829                        expected1->records[1].type));
830         torture_assert_int_equal(tctx, expected1->num_rr, 2,
831                                  "Got wrong record count");
832
833         /* Step 8. */
834         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
835                                                       dbdata, &version),
836                                  ISC_R_SUCCESS,
837                                  "Failed to start transaction");
838         torture_assert_int_equal_goto(tctx,
839                         dlz_subrdataset(name, data2, dbdata, version),
840                         ISC_R_NOTFOUND, ret, cancel_version,
841                         talloc_asprintf(tctx, "Deleted name[%s] data[%s]\n",
842                         name, data2));
843         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), true, dbdata, &version);
844
845         /* Step 9. */
846         expected1->num_rr = 0;
847         expected1->records[0].printed = false;
848         expected1->records[1].printed = false;
849         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
850                                                   expected1->query_name, dbdata,
851                                                   (dns_sdlzlookup_t *)expected1),
852                                  ISC_R_SUCCESS,
853                                  "Not found hostname");
854         torture_assert(tctx, expected1->records[0].printed,
855                        talloc_asprintf(tctx,
856                        "Failed to have putrr callback run name[%s] for type %s",
857                        expected1->records[0].name,
858                        expected1->records[0].type));
859         torture_assert(tctx, expected1->records[1].printed,
860                        talloc_asprintf(tctx,
861                        "Failed to have putrr callback run name[%s] for type %s",
862                        expected1->records[1].name,
863                        expected1->records[1].type));
864         torture_assert_int_equal(tctx, expected1->num_rr, 2,
865                                  "Got wrong record count");
866
867         /* Step 10. */
868         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
869                                                       dbdata, &version),
870                                  ISC_R_SUCCESS,
871                                  "Failed to start transaction");
872         torture_assert_int_equal_goto(tctx,
873                         dlz_subrdataset(name, data0, dbdata, version),
874                         ISC_R_SUCCESS, ret, cancel_version,
875                         talloc_asprintf(tctx, "Failed to delete name[%s] data[%s]\n",
876                         name, data0));
877         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), true, dbdata, &version);
878
879         /* Step 11. */
880         expected1->num_rr = 0;
881         expected1->records[0].printed = false;
882         expected1->records[1].printed = false;
883         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
884                                                   expected1->query_name, dbdata,
885                                                   (dns_sdlzlookup_t *)expected1),
886                                  ISC_R_SUCCESS,
887                                  "Not found hostname");
888         torture_assert(tctx, expected1->records[1].printed,
889                        talloc_asprintf(tctx,
890                        "Failed to have putrr callback run name[%s] for type %s",
891                        expected1->records[1].name,
892                        expected1->records[1].type));
893         torture_assert_int_equal(tctx, expected1->num_rr, 1,
894                                  "Got wrong record count");
895
896         /* Step 12. */
897         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
898                                                       dbdata, &version),
899                                  ISC_R_SUCCESS,
900                                  "Failed to start transaction");
901         torture_assert_int_equal_goto(tctx,
902                         dlz_subrdataset(name, data1, dbdata, version),
903                         ISC_R_SUCCESS, ret, cancel_version,
904                         talloc_asprintf(tctx, "Failed to delete name[%s] data[%s]\n",
905                         name, data1));
906         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), true, dbdata, &version);
907
908         /* Step 13. */
909         expected1->num_rr = 0;
910         expected1->records[0].printed = false;
911         expected1->records[1].printed = false;
912         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
913                                                   expected1->query_name, dbdata,
914                                                   (dns_sdlzlookup_t *)expected1),
915                                  ISC_R_NOTFOUND,
916                                  "Found hostname");
917         torture_assert_int_equal(tctx, expected1->num_rr, 0,
918                                  "Got wrong record count");
919
920         /* Step 14. */
921         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
922                                                       dbdata, &version),
923                                  ISC_R_SUCCESS,
924                                  "Failed to start transaction");
925         torture_assert_int_equal_goto(tctx,
926                         dlz_addrdataset(name, data0, dbdata, version),
927                         ISC_R_SUCCESS, ret, cancel_version,
928                         talloc_asprintf(tctx, "Failed to add name[%s] data[%s]\n",
929                         name, data0));
930         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), true, dbdata, &version);
931
932         /* Step 15. */
933         expected1->num_rr = 0;
934         expected1->records[0].printed = false;
935         expected1->records[1].printed = false;
936         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
937                                                   expected1->query_name, dbdata,
938                                                   (dns_sdlzlookup_t *)expected1),
939                                  ISC_R_SUCCESS,
940                                  "Not found hostname");
941         torture_assert(tctx, expected1->records[0].printed,
942                        talloc_asprintf(tctx,
943                        "Failed to have putrr callback run name[%s] for type %s",
944                        expected1->records[0].name,
945                        expected1->records[0].type));
946         torture_assert_int_equal(tctx, expected1->num_rr, 1,
947                                  "Got wrong record count");
948
949         /* Step 16. */
950         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
951                                                       dbdata, &version),
952                                  ISC_R_SUCCESS,
953                                  "Failed to start transaction");
954         torture_assert_int_equal_goto(tctx,
955                         dlz_addrdataset(name, data1, dbdata, version),
956                         ISC_R_SUCCESS, ret, cancel_version,
957                         talloc_asprintf(tctx, "Failed to add name[%s] data[%s]\n",
958                         name, data1));
959         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), true, dbdata, &version);
960
961         /* Step 17. */
962         expected1->num_rr = 0;
963         expected1->records[0].printed = false;
964         expected1->records[1].printed = false;
965         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
966                                                   expected1->query_name, dbdata,
967                                                   (dns_sdlzlookup_t *)expected1),
968                                  ISC_R_SUCCESS,
969                                  "Not found hostname");
970         torture_assert(tctx, expected1->records[0].printed,
971                        talloc_asprintf(tctx,
972                        "Failed to have putrr callback run name[%s] for type %s",
973                        expected1->records[0].name,
974                        expected1->records[0].type));
975         torture_assert(tctx, expected1->records[1].printed,
976                        talloc_asprintf(tctx,
977                        "Failed to have putrr callback run name[%s] for type %s",
978                        expected1->records[1].name,
979                        expected1->records[1].type));
980         torture_assert_int_equal(tctx, expected1->num_rr, 2,
981                                  "Got wrong record count");
982
983         /* Step 18. */
984         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
985                                                       dbdata, &version),
986                                  ISC_R_SUCCESS,
987                                  "Failed to start transaction");
988         torture_assert_int_equal_goto(tctx,
989                         dlz_addrdataset(name, data0, dbdata, version),
990                         ISC_R_SUCCESS, ret, cancel_version,
991                         talloc_asprintf(tctx, "Failed to update name[%s] data[%s]\n",
992                         name, data0));
993         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), true, dbdata, &version);
994
995         /* Step 19. */
996         expected1->num_rr = 0;
997         expected1->records[0].printed = false;
998         expected1->records[1].printed = false;
999         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
1000                                                   expected1->query_name, dbdata,
1001                                                   (dns_sdlzlookup_t *)expected1),
1002                                  ISC_R_SUCCESS,
1003                                  "Not found hostname");
1004         torture_assert(tctx, expected1->records[0].printed,
1005                        talloc_asprintf(tctx,
1006                        "Failed to have putrr callback run name[%s] for type %s",
1007                        expected1->records[0].name,
1008                        expected1->records[0].type));
1009         torture_assert(tctx, expected1->records[1].printed,
1010                        talloc_asprintf(tctx,
1011                        "Failed to have putrr callback run name[%s] for type %s",
1012                        expected1->records[1].name,
1013                        expected1->records[1].type));
1014         torture_assert_int_equal(tctx, expected1->num_rr, 2,
1015                                  "Got wrong record count");
1016
1017         /* Step 20. */
1018         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
1019                                                       dbdata, &version),
1020                                  ISC_R_SUCCESS,
1021                                  "Failed to start transaction");
1022         torture_assert_int_equal_goto(tctx,
1023                         dlz_delrdataset(name, "txt", dbdata, version),
1024                         ISC_R_FAILURE, ret, cancel_version,
1025                         talloc_asprintf(tctx, "Deleted name[%s] type[%s]\n",
1026                         name, "txt"));
1027         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), false, dbdata, &version);
1028
1029         /* Step 21. */
1030         expected1->num_rr = 0;
1031         expected1->records[0].printed = false;
1032         expected1->records[1].printed = false;
1033         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
1034                                                   expected1->query_name, dbdata,
1035                                                   (dns_sdlzlookup_t *)expected1),
1036                                  ISC_R_SUCCESS,
1037                                  "Not found hostname");
1038         torture_assert(tctx, expected1->records[0].printed,
1039                        talloc_asprintf(tctx,
1040                        "Failed to have putrr callback run name[%s] for type %s",
1041                        expected1->records[0].name,
1042                        expected1->records[0].type));
1043         torture_assert(tctx, expected1->records[1].printed,
1044                        talloc_asprintf(tctx,
1045                        "Failed to have putrr callback run name[%s] for type %s",
1046                        expected1->records[1].name,
1047                        expected1->records[1].type));
1048         torture_assert_int_equal(tctx, expected1->num_rr, 2,
1049                                  "Got wrong record count");
1050
1051         /* Step 22. */
1052         torture_assert_int_equal(tctx, dlz_newversion(lpcfg_dnsdomain(tctx->lp_ctx),
1053                                                       dbdata, &version),
1054                                  ISC_R_SUCCESS,
1055                                  "Failed to start transaction");
1056         torture_assert_int_equal_goto(tctx,
1057                         dlz_delrdataset(name,
1058                                         expected1->records[0].type,
1059                                         dbdata, version),
1060                         ISC_R_SUCCESS, ret, cancel_version,
1061                         talloc_asprintf(tctx, "Failed to delete name[%s] type[%s]\n",
1062                         name, expected1->records[0].type));
1063         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), true, dbdata, &version);
1064
1065         /* Step 23. */
1066         expected1->num_rr = 0;
1067         expected1->records[0].printed = false;
1068         expected1->records[1].printed = false;
1069         torture_assert_int_equal(tctx, dlz_lookup(lpcfg_dnsdomain(tctx->lp_ctx),
1070                                                   expected1->query_name, dbdata,
1071                                                   (dns_sdlzlookup_t *)expected1),
1072                                  ISC_R_NOTFOUND,
1073                                  "Found hostname");
1074         torture_assert_int_equal(tctx, expected1->num_rr, 0,
1075                                  "Got wrong record count");
1076
1077         dlz_destroy(dbdata);
1078
1079         return true;
1080
1081 cancel_version:
1082         dlz_closeversion(lpcfg_dnsdomain(tctx->lp_ctx), false, dbdata, &version);
1083         return ret;
1084 }
1085
1086 static struct torture_suite *dlz_bind9_suite(TALLOC_CTX *ctx)
1087 {
1088         struct torture_suite *suite = torture_suite_create(ctx, "dlz_bind9");
1089
1090         suite->description = talloc_strdup(suite,
1091                                            "Tests for the BIND 9 DLZ module");
1092         torture_suite_add_simple_test(suite, "version", test_dlz_bind9_version);
1093         torture_suite_add_simple_test(suite, "create", test_dlz_bind9_create);
1094         torture_suite_add_simple_test(suite, "configure", test_dlz_bind9_configure);
1095         torture_suite_add_simple_test(suite, "gssapi", test_dlz_bind9_gssapi);
1096         torture_suite_add_simple_test(suite, "spnego", test_dlz_bind9_spnego);
1097         torture_suite_add_simple_test(suite, "lookup", test_dlz_bind9_lookup);
1098         torture_suite_add_simple_test(suite, "zonedump", test_dlz_bind9_zonedump);
1099         torture_suite_add_simple_test(suite, "update01", test_dlz_bind9_update01);
1100         return suite;
1101 }
1102
1103 /**
1104  * DNS torture module initialization
1105  */
1106 NTSTATUS torture_bind_dns_init(TALLOC_CTX *);
1107 NTSTATUS torture_bind_dns_init(TALLOC_CTX *ctx)
1108 {
1109         struct torture_suite *suite;
1110
1111         /* register DNS related test cases */
1112         suite = dlz_bind9_suite(ctx);
1113         if (!suite) return NT_STATUS_NO_MEMORY;
1114         torture_register_suite(ctx, suite);
1115
1116         return NT_STATUS_OK;
1117 }