b1cb9672e8b4ed986a8592a27bdc2ef2cf88c9cd
[samba.git] / source4 / torture / dns / internal_dns.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Kai Blin 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 <talloc.h>
23 #include "lib/addns/dns.h"
24
25 static struct dns_connection *setup_connection(struct torture_context *tctx)
26 {
27         DNS_ERROR err;
28         struct dns_connection *conn;
29
30         err = dns_open_connection(getenv("DC_SERVER_IP"), DNS_TCP, tctx, &conn);
31         if (!ERR_DNS_IS_OK(err)) {
32                 printf("Failed to open connection to DNS server\n");
33                 return NULL;
34         }
35
36         return conn;
37 }
38
39 static char *get_dns_domain(struct torture_context *tctx)
40 {
41         return strlower_talloc(tctx, getenv("REALM"));
42 }
43
44 static struct sockaddr_storage *str_to_sockaddr(TALLOC_CTX *mem_ctx, const char *ip_string)
45 {
46         struct sockaddr_storage *ss = talloc_zero(mem_ctx, struct sockaddr_storage);
47         int ret;
48
49         if (ss == NULL) {
50                 return NULL;
51         }
52
53         ss->ss_family = AF_INET;
54
55         ret = inet_pton(AF_INET, ip_string, &(((struct sockaddr_in *)ss)->sin_addr));
56         if (ret != 1) {
57                 return NULL;
58         }
59
60         return ss;
61 }
62
63 static bool test_internal_dns_query_self(struct torture_context *tctx)
64 {
65         struct dns_connection *conn;
66         struct dns_request *req, *resp;
67         char *host;
68         DNS_ERROR err;
69
70         conn = setup_connection(tctx);
71         if (conn == NULL) {
72                 return false;
73         }
74
75         host = talloc_asprintf(tctx, "%s.%s", getenv("DC_SERVER"), get_dns_domain(tctx));
76         if (host == NULL) {
77                 return false;
78         }
79
80         err = dns_create_query(conn, host, QTYPE_A, DNS_CLASS_IN, &req);
81         if (!ERR_DNS_IS_OK(err)) {
82                 printf("Failed to create A record query\n");
83                 return false;
84         }
85
86         err = dns_transaction(conn, conn, req, &resp);
87         if (!ERR_DNS_IS_OK(err)) {
88                 printf("Failed to query DNS server\n");
89                 return false;
90         }
91
92         if (dns_response_code(resp->flags) != DNS_NO_ERROR) {
93                 printf("Query returned %u\n", dns_response_code(resp->flags));
94                 return false;
95         }
96
97         /* FIXME: is there _any_ way to unmarshal the response to check this? */
98
99         return true;
100 }
101
102 static bool test_internal_dns_update_self(struct torture_context *tctx)
103 {
104         struct dns_connection *conn;
105         struct dns_update_request *req, *resp;
106         struct dns_rrec *rec = NULL;
107         char *host;
108         DNS_ERROR err;
109         struct sockaddr_storage *ss;
110
111         conn = setup_connection(tctx);
112         if (conn == NULL) {
113                 return false;
114         }
115
116         host = talloc_asprintf(tctx, "%s.%s", getenv("DC_SERVER"), get_dns_domain(tctx));
117         if (host == NULL) {
118                 return false;
119         }
120
121         err = dns_create_update(conn, get_dns_domain(tctx), &req);
122         if (!ERR_DNS_IS_OK(err)) {
123                 printf("Failed to update packet\n");
124                 return false;
125         }
126
127         ss = str_to_sockaddr(conn, getenv("DC_SERVER_IP"));
128         if (ss == NULL) {
129                 printf("Converting '%s' to sockaddr_storage failed\n", getenv("DC_SERVER_IP"));
130                 return false;
131         }
132
133         err = dns_create_a_record(req, host, 300, ss, &rec);
134         if (!ERR_DNS_IS_OK(err)) {
135                 printf("Failed to create A update record\n");
136                 return false;
137         }
138
139         err = dns_add_rrec(req, rec, &req->num_updates, &req->updates);
140         if (!ERR_DNS_IS_OK(err)) {
141                 printf("Failed to add A update record to update packet\n");
142                 return false;
143         }
144
145         err = dns_update_transaction(conn, conn, req, &resp);
146         if (!ERR_DNS_IS_OK(err)) {
147                 printf("Failed to send update\n");
148                 return false;
149         }
150
151         if (dns_response_code(resp->flags) != DNS_REFUSED) {
152                 printf("Update returned %u\n", dns_response_code(resp->flags));
153                 return false;
154         }
155
156         /* FIXME: is there _any_ way to unmarshal the response to check this? */
157
158         return true;
159 }
160
161 static struct torture_suite *internal_dns_suite(TALLOC_CTX *ctx)
162 {
163         struct torture_suite *suite = torture_suite_create(ctx, "dns_internal");
164
165         suite->description = talloc_strdup(suite,
166                                            "Tests for the internal DNS server");
167         torture_suite_add_simple_test(suite, "queryself", test_internal_dns_query_self);
168         torture_suite_add_simple_test(suite, "updateself", test_internal_dns_update_self);
169         return suite;
170 }
171
172
173 /* Silence silly compiler warning */
174 NTSTATUS torture_internal_dns_init(void);
175
176 /**
177  * DNS torture module initialization
178  */
179 NTSTATUS torture_internal_dns_init(void)
180 {
181         struct torture_suite *suite;
182         TALLOC_CTX *mem_ctx = talloc_autofree_context();
183
184         /* register internal DNS torture test cases */
185         suite = internal_dns_suite(mem_ctx);
186         if (!suite) return NT_STATUS_NO_MEMORY;
187         torture_register_suite(suite);
188
189         return NT_STATUS_OK;
190 }