Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : DNS Server
5 :
6 : Copyright (C) Amitay Isaacs 2011
7 :
8 : This program is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3 of the License, or
11 : (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program. If not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include "includes.h"
23 : #include "dnsserver.h"
24 : #include "rpc_server/common/common.h"
25 : #include "dns_server/dnsserver_common.h"
26 : #include "dsdb/samdb/samdb.h"
27 : #include "lib/socket/netif.h"
28 : #include "lib/util/util_net.h"
29 : #include "dnsserver_common.h"
30 :
31 : #undef strcasecmp
32 :
33 1393 : static struct DNS_ADDR_ARRAY *fill_dns_addr_array(TALLOC_CTX *mem_ctx,
34 : struct loadparm_context *lp_ctx,
35 : bool listen_only)
36 : {
37 : struct interface *ifaces;
38 : int num_interfaces, i;
39 : struct DNS_ADDR_ARRAY *dns_addr_array;
40 : const char *ipstr;
41 : bool have_ipv4, have_ipv6;
42 : uint16_t family;
43 :
44 1393 : have_ipv4 = have_ipv6 = false;
45 :
46 1393 : if (!listen_only) {
47 : /*
48 : Return all interfaces from kernel
49 : Not implemented!
50 : */
51 0 : return NULL;
52 : }
53 :
54 : /* Only the used interfaces */
55 1393 : load_interface_list(mem_ctx, lp_ctx, &ifaces);
56 1393 : num_interfaces = iface_list_count(ifaces);
57 :
58 1393 : dns_addr_array = talloc_zero(mem_ctx, struct DNS_ADDR_ARRAY);
59 1393 : if (dns_addr_array == NULL) {
60 0 : goto nomem;
61 : }
62 1393 : dns_addr_array->MaxCount = num_interfaces;
63 1393 : dns_addr_array->AddrCount = num_interfaces;
64 1393 : if (num_interfaces == 0) {
65 0 : goto nomem;
66 : }
67 :
68 1393 : dns_addr_array->AddrArray = talloc_zero_array(mem_ctx, struct DNS_ADDR,
69 : num_interfaces);
70 1393 : if (!dns_addr_array->AddrArray) {
71 0 : TALLOC_FREE(dns_addr_array);
72 0 : goto nomem;
73 : }
74 :
75 4179 : for (i = 0; i < num_interfaces; i++) {
76 : int ret;
77 2786 : ipstr = iface_list_n_ip(ifaces, i);
78 2786 : if (is_ipaddress_v4(ipstr)) {
79 1393 : have_ipv4 = true;
80 1393 : dns_addr_array->AddrArray[i].MaxSa[0] = 0x02;
81 1393 : ret = inet_pton(AF_INET, ipstr,
82 1393 : &dns_addr_array->AddrArray[i].MaxSa[4]);
83 : } else {
84 1393 : have_ipv6 = true;
85 1393 : dns_addr_array->AddrArray[i].MaxSa[0] = 0x17;
86 1393 : ret = inet_pton(AF_INET6, ipstr,
87 1393 : &dns_addr_array->AddrArray[i].MaxSa[8]);
88 : }
89 2786 : if (ret != 1) { /*yep, 1 means success for inet_pton */
90 0 : DBG_ERR("Interface %d address (%s) is invalid\n",
91 : i, ipstr);
92 0 : goto nomem;
93 : }
94 : }
95 :
96 1393 : if (have_ipv4 && have_ipv6) {
97 1393 : family = 0; /* mixed: MS-DNSP */
98 0 : } else if (have_ipv4 && !have_ipv6) {
99 0 : family = AF_INET;
100 : } else {
101 0 : family = AF_INET6;
102 : }
103 1393 : dns_addr_array->Family = family;
104 :
105 1393 : nomem:
106 1393 : talloc_free(ifaces);
107 1393 : return dns_addr_array;
108 : }
109 :
110 1393 : struct dnsserver_serverinfo *dnsserver_init_serverinfo(TALLOC_CTX *mem_ctx,
111 : struct loadparm_context *lp_ctx,
112 : struct ldb_context *samdb)
113 : {
114 : struct dnsserver_serverinfo *serverinfo;
115 : struct dcerpc_server_info *dinfo;
116 : struct ldb_dn *domain_dn, *forest_dn;
117 :
118 1393 : serverinfo = talloc_zero(mem_ctx, struct dnsserver_serverinfo);
119 1393 : if (serverinfo == NULL) {
120 0 : return NULL;
121 : }
122 :
123 1393 : dinfo = lpcfg_dcerpc_server_info(mem_ctx, lp_ctx);
124 1393 : if (dinfo) {
125 3863 : serverinfo->dwVersion = (dinfo->version_build & 0x0000FFFF) << 16 |
126 2628 : (dinfo->version_minor & 0x000000FF) << 8 |
127 1393 : (dinfo->version_major & 0x000000FF);
128 1393 : talloc_free(dinfo);
129 : } else {
130 0 : serverinfo->dwVersion = 0x0ECE0205; /* build, os_minor, os_major */;
131 : }
132 :
133 1393 : serverinfo->fBootMethod = DNS_BOOT_METHOD_DIRECTORY;
134 1393 : serverinfo->fAdminConfigured = 0;
135 1393 : serverinfo->fAllowUpdate = 1;
136 1393 : serverinfo->fDsAvailable = 1;
137 :
138 1393 : serverinfo->pszServerName = talloc_asprintf(mem_ctx, "%s.%s",
139 : lpcfg_netbios_name(lp_ctx),
140 : lpcfg_dnsdomain(lp_ctx));
141 :
142 1393 : domain_dn = ldb_get_default_basedn(samdb);
143 1393 : forest_dn = ldb_get_root_basedn(samdb);
144 :
145 1393 : serverinfo->pszDsContainer = talloc_asprintf(mem_ctx,
146 : "CN=MicrosoftDNS,DC=DomainDnsZones,%s",
147 : ldb_dn_get_linearized(domain_dn));
148 :
149 1393 : serverinfo->dwDsForestVersion = dsdb_forest_functional_level(samdb);
150 1393 : serverinfo->dwDsDomainVersion = dsdb_functional_level(samdb);
151 1393 : serverinfo->dwDsDsaVersion = dsdb_dc_functional_level(samdb);
152 :
153 1393 : serverinfo->pszDomainName = samdb_dn_to_dns_domain(mem_ctx, domain_dn);
154 1393 : serverinfo->pszForestName = samdb_dn_to_dns_domain(mem_ctx, forest_dn);
155 :
156 1393 : serverinfo->pszDomainDirectoryPartition = talloc_asprintf(mem_ctx,
157 : "DC=DomainDnsZones,%s",
158 : ldb_dn_get_linearized(domain_dn));
159 1393 : serverinfo->pszForestDirectoryPartition = talloc_asprintf(mem_ctx,
160 : "DC=ForestDnsZones,%s",
161 : ldb_dn_get_linearized(forest_dn));
162 : /* IP addresses on which the DNS server listens for DNS requests */
163 1393 : serverinfo->aipListenAddrs = fill_dns_addr_array(mem_ctx, lp_ctx, true);
164 :
165 : /* All IP addresses available on the server
166 : * Not implemented!
167 : * Use same as listen addresses
168 : */
169 1393 : serverinfo->aipServerAddrs = serverinfo->aipListenAddrs;
170 :
171 1393 : serverinfo->aipForwarders = NULL;
172 :
173 1393 : serverinfo->aipLogFilter = NULL;
174 1393 : serverinfo->pwszLogFilePath = NULL;
175 :
176 1393 : serverinfo->dwLogLevel = 0;
177 1393 : serverinfo->dwDebugLevel = 0;
178 1393 : serverinfo->dwEventLogLevel = DNS_EVENT_LOG_INFORMATION_TYPE;
179 1393 : serverinfo->dwLogFileMaxSize = 0;
180 :
181 1393 : serverinfo->dwForwardTimeout = 3; /* seconds (default) */
182 1393 : serverinfo->dwRpcProtocol = 5;
183 1393 : serverinfo->dwNameCheckFlag = DNS_ALLOW_MULTIBYTE_NAMES;
184 1393 : serverinfo->cAddressAnswerLimit = 0;
185 1393 : serverinfo->dwRecursionRetry = 3; /* seconds (default) */
186 1393 : serverinfo->dwRecursionTimeout = 8; /* seconds (default) */
187 1393 : serverinfo->dwMaxCacheTtl = 0x00015180; /* 1 day (default) */
188 1393 : serverinfo->dwDsPollingInterval = 0xB4; /* 3 minutes (default) */
189 1393 : serverinfo->dwLocalNetPriorityNetMask = 0x000000FF;
190 :
191 1393 : serverinfo->dwScavengingInterval = lpcfg_parm_int(
192 : lp_ctx, NULL, "dnsserver", "ScavengingInterval", 24 * 7);
193 1393 : serverinfo->dwDefaultRefreshInterval = lpcfg_parm_int(
194 : lp_ctx, NULL, "dnsserver", "DefaultRefreshInterval", 24 * 3);
195 1393 : serverinfo->dwDefaultNoRefreshInterval = lpcfg_parm_int(
196 : lp_ctx, NULL, "dnsserver", "DefaultNoRefreshInterval", 24 * 3);
197 :
198 1393 : serverinfo->dwLastScavengeTime = 0;
199 :
200 1393 : serverinfo->fAutoReverseZones = 0;
201 1393 : serverinfo->fAutoCacheUpdate = 0;
202 :
203 1393 : serverinfo->fRecurseAfterForwarding = 0;
204 1393 : serverinfo->fForwardDelegations = 1;
205 1393 : serverinfo->fNoRecursion = 0;
206 1393 : serverinfo->fSecureResponses = 0;
207 :
208 1393 : serverinfo->fRoundRobin = 1;
209 1393 : serverinfo->fLocalNetPriority = 0;
210 :
211 1393 : serverinfo->fBindSecondaries = 0;
212 1393 : serverinfo->fWriteAuthorityNs = 0;
213 :
214 1393 : serverinfo->fStrictFileParsing = 0;
215 1393 : serverinfo->fLooseWildcarding = 0 ;
216 1393 : serverinfo->fDefaultAgingState = 0;
217 :
218 1393 : return serverinfo;
219 : }
220 :
221 5332 : struct dnsserver_zoneinfo *dnsserver_init_zoneinfo(struct dnsserver_zone *zone,
222 : struct dnsserver_serverinfo *serverinfo)
223 : {
224 : struct dnsserver_zoneinfo *zoneinfo;
225 : uint32_t fReverse;
226 5332 : const char *revzone = "in-addr.arpa";
227 5332 : const char *revzone6 = "ip6.arpa";
228 : int len1, len2;
229 5332 : unsigned int i = 0;
230 :
231 5332 : zoneinfo = talloc_zero(zone, struct dnsserver_zoneinfo);
232 5332 : if (zoneinfo == NULL) {
233 0 : return NULL;
234 : }
235 :
236 : /* If the zone name ends with in-addr.arpa, it's reverse zone */
237 : /* If the zone name ends with ip6.arpa, it's reverse zone (IPv6) */
238 5332 : fReverse = 0;
239 5332 : len1 = strlen(zone->name);
240 5332 : len2 = strlen(revzone);
241 5332 : if (len1 > len2 && strcasecmp(&zone->name[len1-len2], revzone) == 0) {
242 1 : fReverse = 1;
243 : } else {
244 5331 : len2 = strlen(revzone6);
245 5331 : if (len1 > len2 && strcasecmp(&zone->name[len1-len2], revzone6) == 0) {
246 0 : fReverse = 1;
247 : }
248 : }
249 :
250 5332 : zoneinfo->Version = 0x32;
251 5332 : zoneinfo->Flags = DNS_RPC_ZONE_DSINTEGRATED;
252 :
253 5332 : if (strcmp(zone->name, ".") == 0) {
254 1393 : zoneinfo->dwZoneType = DNS_ZONE_TYPE_CACHE;
255 1393 : zoneinfo->fAllowUpdate = DNS_ZONE_UPDATE_OFF;
256 1393 : zoneinfo->fSecureSecondaries = DNS_ZONE_SECSECURE_NO_SECURITY;
257 1393 : zoneinfo->fNotifyLevel = DNS_ZONE_NOTIFY_OFF;
258 1393 : zoneinfo->dwNoRefreshInterval = 0;
259 1393 : zoneinfo->dwRefreshInterval = 0;
260 : } else {
261 3939 : zoneinfo->Flags |= DNS_RPC_ZONE_UPDATE_SECURE;
262 3939 : zoneinfo->dwZoneType = DNS_ZONE_TYPE_PRIMARY;
263 3939 : zoneinfo->fAllowUpdate = DNS_ZONE_UPDATE_SECURE;
264 3939 : zoneinfo->fSecureSecondaries = DNS_ZONE_SECSECURE_NO_XFER;
265 3939 : zoneinfo->fNotifyLevel = DNS_ZONE_NOTIFY_LIST_ONLY;
266 3939 : zoneinfo->dwNoRefreshInterval = serverinfo->dwDefaultNoRefreshInterval;
267 3939 : zoneinfo->dwRefreshInterval = serverinfo->dwDefaultRefreshInterval;
268 : }
269 :
270 5332 : zoneinfo->fReverse = fReverse;
271 5332 : zoneinfo->fPaused = 0;
272 5332 : zoneinfo->fShutdown = 0;
273 5332 : zoneinfo->fAutoCreated = 0;
274 5332 : zoneinfo->fUseDatabase = 1;
275 5332 : zoneinfo->pszDataFile = NULL;
276 5332 : zoneinfo->aipMasters = NULL;
277 5332 : zoneinfo->aipSecondaries = NULL;
278 5332 : zoneinfo->aipNotify = NULL;
279 5332 : zoneinfo->fUseWins = 0;
280 5332 : zoneinfo->fUseNbstat = 0;
281 5332 : zoneinfo->fAging = 0;
282 5332 : zoneinfo->dwAvailForScavengeTime = 0;
283 5332 : zoneinfo->aipScavengeServers = NULL;
284 5332 : zoneinfo->dwForwarderTimeout = 0;
285 5332 : zoneinfo->fForwarderSlave = 0;
286 5332 : zoneinfo->aipLocalMasters = NULL;
287 5332 : zoneinfo->pwszZoneDn = discard_const_p(char, ldb_dn_get_linearized(zone->zone_dn));
288 5332 : zoneinfo->dwLastSuccessfulSoaCheck = 0;
289 5332 : zoneinfo->dwLastSuccessfulXfr = 0;
290 5332 : zoneinfo->fQueuedForBackgroundLoad = 0;
291 5332 : zoneinfo->fBackgroundLoadInProgress = 0;
292 5332 : zoneinfo->fReadOnlyZone = 0;
293 5332 : zoneinfo->dwLastXfrAttempt = 0;
294 5332 : zoneinfo->dwLastXfrResult = 0;
295 :
296 32905 : for(i=0; i<zone->num_props; i++){
297 : bool valid_property;
298 27573 : valid_property = dns_zoneinfo_load_zone_property(
299 27573 : zoneinfo, &zone->tmp_props[i]);
300 27573 : if (!valid_property) {
301 0 : TALLOC_FREE(zoneinfo);
302 0 : return NULL;
303 : }
304 : }
305 :
306 5332 : return zoneinfo;
307 : }
308 :
309 11625 : struct dnsserver_zone *dnsserver_find_zone(struct dnsserver_zone *zones, const char *zone_name)
310 : {
311 11625 : struct dnsserver_zone *z = NULL;
312 :
313 24254 : for (z = zones; z; z = z->next) {
314 18531 : if (samba_dns_name_equal(zone_name, z->name)) {
315 5902 : break;
316 : }
317 : }
318 :
319 11625 : return z;
320 : }
321 :
322 577 : struct ldb_dn *dnsserver_name_to_dn(TALLOC_CTX *mem_ctx, struct dnsserver_zone *z, const char *name)
323 : {
324 : struct ldb_dn *dn;
325 : bool ret;
326 557 : struct ldb_val name_val =
327 20 : data_blob_string_const(name);
328 :
329 577 : dn = ldb_dn_copy(mem_ctx, z->zone_dn);
330 577 : if (dn == NULL) {
331 0 : return NULL;
332 : }
333 577 : if (strcasecmp(name, z->name) == 0) {
334 0 : ret = ldb_dn_add_child_fmt(dn, "DC=@");
335 0 : if (!ret) {
336 0 : talloc_free(dn);
337 0 : return NULL;
338 : }
339 0 : return dn;
340 : }
341 :
342 577 : ret = ldb_dn_add_child_val(dn,
343 : "DC",
344 : name_val);
345 :
346 577 : if (!ret) {
347 0 : talloc_free(dn);
348 0 : return NULL;
349 : }
350 :
351 577 : return dn;
352 : }
353 :
354 1160 : uint32_t dnsserver_zone_to_request_filter(const char *zone_name)
355 : {
356 1160 : uint32_t request_filter = 0;
357 :
358 1160 : if (strcmp(zone_name, "..AllZones") == 0) {
359 0 : request_filter = DNS_ZONE_REQUEST_PRIMARY
360 : | DNS_ZONE_REQUEST_SECONDARY
361 : | DNS_ZONE_REQUEST_AUTO
362 : | DNS_ZONE_REQUEST_FORWARD
363 : | DNS_ZONE_REQUEST_REVERSE
364 : | DNS_ZONE_REQUEST_FORWARDER
365 : | DNS_ZONE_REQUEST_STUB
366 : | DNS_ZONE_REQUEST_DS
367 : | DNS_ZONE_REQUEST_NON_DS
368 : | DNS_ZONE_REQUEST_DOMAIN_DP
369 : | DNS_ZONE_REQUEST_FOREST_DP
370 : | DNS_ZONE_REQUEST_CUSTOM_DP
371 : | DNS_ZONE_REQUEST_LEGACY_DP;
372 1160 : } else if (strcmp(zone_name, "..AllZonesAndCache") == 0) {
373 0 : request_filter = DNS_ZONE_REQUEST_PRIMARY
374 : | DNS_ZONE_REQUEST_SECONDARY
375 : | DNS_ZONE_REQUEST_CACHE
376 : | DNS_ZONE_REQUEST_AUTO
377 : | DNS_ZONE_REQUEST_FORWARD
378 : | DNS_ZONE_REQUEST_REVERSE
379 : | DNS_ZONE_REQUEST_FORWARDER
380 : | DNS_ZONE_REQUEST_STUB
381 : | DNS_ZONE_REQUEST_DS
382 : | DNS_ZONE_REQUEST_NON_DS
383 : | DNS_ZONE_REQUEST_DOMAIN_DP
384 : | DNS_ZONE_REQUEST_FOREST_DP
385 : | DNS_ZONE_REQUEST_CUSTOM_DP
386 : | DNS_ZONE_REQUEST_LEGACY_DP;
387 1160 : } else if (strcmp(zone_name, "..AllPrimaryZones") == 0) {
388 0 : request_filter = DNS_ZONE_REQUEST_PRIMARY;
389 1160 : } else if (strcmp(zone_name, "..AllSecondaryZones") == 0) {
390 0 : request_filter = DNS_ZONE_REQUEST_SECONDARY;
391 1160 : } else if (strcmp(zone_name, "..AllForwardZones") == 0) {
392 0 : request_filter = DNS_ZONE_REQUEST_FORWARD;
393 1160 : } else if (strcmp(zone_name, "..AllReverseZones") == 0) {
394 0 : request_filter = DNS_ZONE_REQUEST_REVERSE;
395 1160 : } else if (strcmp(zone_name, "..AllDsZones") == 0) {
396 0 : request_filter = DNS_ZONE_REQUEST_DS;
397 1160 : } else if (strcmp(zone_name, "..AllNonDsZones") == 0) {
398 0 : request_filter = DNS_ZONE_REQUEST_NON_DS;
399 1160 : } else if (strcmp(zone_name, "..AllPrimaryReverseZones") == 0) {
400 0 : request_filter = DNS_ZONE_REQUEST_PRIMARY
401 : | DNS_ZONE_REQUEST_REVERSE;
402 1160 : } else if (strcmp(zone_name, "..AllPrimaryForwardZones") == 0) {
403 0 : request_filter = DNS_ZONE_REQUEST_PRIMARY
404 : | DNS_ZONE_REQUEST_FORWARD;
405 1160 : } else if (strcmp(zone_name, "..AllSecondaryReverseZones") == 0) {
406 0 : request_filter = DNS_ZONE_REQUEST_SECONDARY
407 : | DNS_ZONE_REQUEST_REVERSE;
408 1160 : } else if (strcmp(zone_name, "..AllSecondaryForwardZones") == 0) {
409 0 : request_filter = DNS_ZONE_REQUEST_SECONDARY
410 : | DNS_ZONE_REQUEST_REVERSE;
411 : }
412 :
413 1160 : return request_filter;
414 : }
|