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