LCOV - code coverage report
Current view: top level - source4/rpc_server/dnsserver - dnsutils.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 179 215 83.3 %
Date: 2021-09-23 10:06:22 Functions: 6 6 100.0 %

          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        1475 : 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        1475 :         have_ipv4 = have_ipv6 = false;
      45             : 
      46        1475 :         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        1475 :         load_interface_list(mem_ctx, lp_ctx, &ifaces);
      56        1475 :         num_interfaces = iface_list_count(ifaces);
      57             : 
      58        1475 :         dns_addr_array = talloc_zero(mem_ctx, struct DNS_ADDR_ARRAY);
      59        1475 :         if (dns_addr_array == NULL) {
      60           0 :                 goto nomem;
      61             :         }
      62        1475 :         dns_addr_array->MaxCount = num_interfaces;
      63        1475 :         dns_addr_array->AddrCount = num_interfaces;
      64        1475 :         if (num_interfaces == 0) {
      65           0 :                 goto nomem;
      66             :         }
      67             : 
      68        1475 :         dns_addr_array->AddrArray = talloc_zero_array(mem_ctx, struct DNS_ADDR,
      69             :                                                       num_interfaces);
      70        1475 :         if (!dns_addr_array->AddrArray) {
      71           0 :                 TALLOC_FREE(dns_addr_array);
      72           0 :                 goto nomem;
      73             :         }
      74             : 
      75        4425 :         for (i = 0; i < num_interfaces; i++) {
      76             :                 int ret;
      77        2950 :                 ipstr = iface_list_n_ip(ifaces, i);
      78        2950 :                 if (is_ipaddress_v4(ipstr)) {
      79        1475 :                         have_ipv4 = true;
      80        1475 :                         dns_addr_array->AddrArray[i].MaxSa[0] = 0x02;
      81        1475 :                         ret = inet_pton(AF_INET, ipstr,
      82        1475 :                                         &dns_addr_array->AddrArray[i].MaxSa[4]);
      83             :                 } else {
      84        1475 :                         have_ipv6 = true;
      85        1475 :                         dns_addr_array->AddrArray[i].MaxSa[0] = 0x17;
      86        1475 :                         ret = inet_pton(AF_INET6, ipstr,
      87        1475 :                                         &dns_addr_array->AddrArray[i].MaxSa[8]);
      88             :                 }
      89        2950 :                 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        1475 :         if (have_ipv4 && have_ipv6) {
      97        1475 :                 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        1475 :         dns_addr_array->Family = family;
     104             : 
     105        1475 : nomem:
     106        1475 :         talloc_free(ifaces);
     107        1475 :         return dns_addr_array;
     108             : }
     109             : 
     110        1475 : 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        1475 :         serverinfo = talloc_zero(mem_ctx, struct dnsserver_serverinfo);
     119        1475 :         if (serverinfo == NULL) {
     120           0 :                 return NULL;
     121             :         }
     122             : 
     123        1475 :         dinfo = lpcfg_dcerpc_server_info(mem_ctx, lp_ctx);
     124        1475 :         if (dinfo) {
     125        4023 :                 serverinfo->dwVersion = (dinfo->version_build & 0x0000FFFF) << 16 |
     126        2749 :                                 (dinfo->version_minor & 0x000000FF) << 8 |
     127        1475 :                                 (dinfo->version_major & 0x000000FF);
     128        1475 :                 talloc_free(dinfo);
     129             :         } else {
     130           0 :                 serverinfo->dwVersion = 0x0ECE0205; /* build, os_minor, os_major */;
     131             :         }
     132             : 
     133        1475 :         serverinfo->fBootMethod = DNS_BOOT_METHOD_DIRECTORY;
     134        1475 :         serverinfo->fAdminConfigured = 0;
     135        1475 :         serverinfo->fAllowUpdate = 1;
     136        1475 :         serverinfo->fDsAvailable = 1;
     137             : 
     138        1475 :         serverinfo->pszServerName = talloc_asprintf(mem_ctx, "%s.%s",
     139             :                                         lpcfg_netbios_name(lp_ctx),
     140             :                                         lpcfg_dnsdomain(lp_ctx));
     141             : 
     142        1475 :         domain_dn = ldb_get_default_basedn(samdb);
     143        1475 :         forest_dn = ldb_get_root_basedn(samdb);
     144             : 
     145        1475 :         serverinfo->pszDsContainer = talloc_asprintf(mem_ctx,
     146             :                                         "CN=MicrosoftDNS,DC=DomainDnsZones,%s",
     147             :                                         ldb_dn_get_linearized(domain_dn));
     148             : 
     149        1475 :         serverinfo->dwDsForestVersion = dsdb_forest_functional_level(samdb);
     150        1475 :         serverinfo->dwDsDomainVersion = dsdb_functional_level(samdb);
     151        1475 :         serverinfo->dwDsDsaVersion = 4; /* need to do ldb search here */
     152             : 
     153        1475 :         serverinfo->pszDomainName = samdb_dn_to_dns_domain(mem_ctx, domain_dn);
     154        1475 :         serverinfo->pszForestName = samdb_dn_to_dns_domain(mem_ctx, forest_dn);
     155             : 
     156        1475 :         serverinfo->pszDomainDirectoryPartition = talloc_asprintf(mem_ctx,
     157             :                                                         "DC=DomainDnsZones,%s",
     158             :                                                         ldb_dn_get_linearized(domain_dn));
     159        1475 :         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        1475 :         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        1475 :         serverinfo->aipServerAddrs = serverinfo->aipListenAddrs;
     170             : 
     171        1475 :         serverinfo->aipForwarders = NULL;
     172             : 
     173        1475 :         serverinfo->aipLogFilter = NULL;
     174        1475 :         serverinfo->pwszLogFilePath = NULL;
     175             : 
     176        1475 :         serverinfo->dwLogLevel = 0;
     177        1475 :         serverinfo->dwDebugLevel = 0;
     178        1475 :         serverinfo->dwEventLogLevel = DNS_EVENT_LOG_INFORMATION_TYPE;
     179        1475 :         serverinfo->dwLogFileMaxSize = 0;
     180             : 
     181        1475 :         serverinfo->dwForwardTimeout = 3; /* seconds (default) */
     182        1475 :         serverinfo->dwRpcProtocol = 5;
     183        1475 :         serverinfo->dwNameCheckFlag = DNS_ALLOW_MULTIBYTE_NAMES;
     184        1475 :         serverinfo->cAddressAnswerLimit = 0;
     185        1475 :         serverinfo->dwRecursionRetry = 3;       /* seconds (default) */
     186        1475 :         serverinfo->dwRecursionTimeout = 8;     /* seconds (default) */
     187        1475 :         serverinfo->dwMaxCacheTtl = 0x00015180; /* 1 day (default) */
     188        1475 :         serverinfo->dwDsPollingInterval = 0xB4; /* 3 minutes (default) */
     189        1475 :         serverinfo->dwLocalNetPriorityNetMask = 0x000000FF;
     190             : 
     191        1475 :         serverinfo->dwScavengingInterval = lpcfg_parm_int(
     192             :             lp_ctx, NULL, "dnsserver", "ScavengingInterval", 24 * 7);
     193        1475 :         serverinfo->dwDefaultRefreshInterval = lpcfg_parm_int(
     194             :             lp_ctx, NULL, "dnsserver", "DefaultRefreshInterval", 24 * 3);
     195        1475 :         serverinfo->dwDefaultNoRefreshInterval = lpcfg_parm_int(
     196             :             lp_ctx, NULL, "dnsserver", "DefaultNoRefreshInterval", 24 * 3);
     197             : 
     198        1475 :         serverinfo->dwLastScavengeTime = 0;
     199             : 
     200        1475 :         serverinfo->fAutoReverseZones = 0;
     201        1475 :         serverinfo->fAutoCacheUpdate = 0;
     202             : 
     203        1475 :         serverinfo->fRecurseAfterForwarding = 0;
     204        1475 :         serverinfo->fForwardDelegations = 1;
     205        1475 :         serverinfo->fNoRecursion = 0;
     206        1475 :         serverinfo->fSecureResponses = 0;
     207             : 
     208        1475 :         serverinfo->fRoundRobin = 1;
     209        1475 :         serverinfo->fLocalNetPriority = 0;
     210             : 
     211        1475 :         serverinfo->fBindSecondaries = 0;
     212        1475 :         serverinfo->fWriteAuthorityNs = 0;
     213             : 
     214        1475 :         serverinfo->fStrictFileParsing = 0;
     215        1475 :         serverinfo->fLooseWildcarding = 0 ;
     216        1475 :         serverinfo->fDefaultAgingState = 0;
     217             : 
     218        1475 :         return serverinfo;
     219             : }
     220             : 
     221        5634 : 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        5634 :         const char *revzone = "in-addr.arpa";
     227        5634 :         const char *revzone6 = "ip6.arpa";
     228             :         int len1, len2;
     229        5634 :         unsigned int i = 0;
     230             : 
     231        5634 :         zoneinfo = talloc_zero(zone, struct dnsserver_zoneinfo);
     232        5634 :         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        5634 :         fReverse = 0;
     239        5634 :         len1 = strlen(zone->name);
     240        5634 :         len2 = strlen(revzone);
     241        5634 :         if (len1 > len2 && strcasecmp(&zone->name[len1-len2], revzone) == 0) {
     242           3 :                 fReverse = 1;
     243             :         } else {
     244        5631 :                 len2 = strlen(revzone6);
     245        5631 :                 if (len1 > len2 && strcasecmp(&zone->name[len1-len2], revzone6) == 0) {
     246           0 :                         fReverse = 1;
     247             :                 }
     248             :         }
     249             : 
     250        5634 :         zoneinfo->Version = 0x32;
     251        5634 :         zoneinfo->Flags = DNS_RPC_ZONE_DSINTEGRATED;
     252             : 
     253        5634 :         if (strcmp(zone->name, ".") == 0) {
     254        1475 :                 zoneinfo->dwZoneType = DNS_ZONE_TYPE_CACHE;
     255        1475 :                 zoneinfo->fAllowUpdate = DNS_ZONE_UPDATE_OFF;
     256        1475 :                 zoneinfo->fSecureSecondaries = DNS_ZONE_SECSECURE_NO_SECURITY;
     257        1475 :                 zoneinfo->fNotifyLevel = DNS_ZONE_NOTIFY_OFF;
     258        1475 :                 zoneinfo->dwNoRefreshInterval = 0;
     259        1475 :                 zoneinfo->dwRefreshInterval = 0;
     260             :         } else {
     261        4159 :                 zoneinfo->Flags |= DNS_RPC_ZONE_UPDATE_SECURE;
     262        4159 :                 zoneinfo->dwZoneType = DNS_ZONE_TYPE_PRIMARY;
     263        4159 :                 zoneinfo->fAllowUpdate = DNS_ZONE_UPDATE_SECURE;
     264        4159 :                 zoneinfo->fSecureSecondaries = DNS_ZONE_SECSECURE_NO_XFER;
     265        4159 :                 zoneinfo->fNotifyLevel = DNS_ZONE_NOTIFY_LIST_ONLY;
     266        4159 :                 zoneinfo->dwNoRefreshInterval = serverinfo->dwDefaultNoRefreshInterval;
     267        4159 :                 zoneinfo->dwRefreshInterval = serverinfo->dwDefaultRefreshInterval;
     268             :         }
     269             : 
     270        5634 :         zoneinfo->fReverse = fReverse;
     271        5634 :         zoneinfo->fPaused = 0;
     272        5634 :         zoneinfo->fShutdown = 0;
     273        5634 :         zoneinfo->fAutoCreated = 0;
     274        5634 :         zoneinfo->fUseDatabase = 1;
     275        5634 :         zoneinfo->pszDataFile = NULL;
     276        5634 :         zoneinfo->aipMasters = NULL;
     277        5634 :         zoneinfo->aipSecondaries = NULL;
     278        5634 :         zoneinfo->aipNotify = NULL;
     279        5634 :         zoneinfo->fUseWins = 0;
     280        5634 :         zoneinfo->fUseNbstat = 0;
     281        5634 :         zoneinfo->fAging = 0;
     282        5634 :         zoneinfo->dwAvailForScavengeTime = 0;
     283        5634 :         zoneinfo->aipScavengeServers = NULL;
     284        5634 :         zoneinfo->dwForwarderTimeout = 0;
     285        5634 :         zoneinfo->fForwarderSlave = 0;
     286        5634 :         zoneinfo->aipLocalMasters = NULL;
     287        5634 :         zoneinfo->pwszZoneDn = discard_const_p(char, ldb_dn_get_linearized(zone->zone_dn));
     288        5634 :         zoneinfo->dwLastSuccessfulSoaCheck = 0;
     289        5634 :         zoneinfo->dwLastSuccessfulXfr = 0;
     290        5634 :         zoneinfo->fQueuedForBackgroundLoad = 0;
     291        5634 :         zoneinfo->fBackgroundLoadInProgress = 0;
     292        5634 :         zoneinfo->fReadOnlyZone = 0;
     293        5634 :         zoneinfo->dwLastXfrAttempt = 0;
     294        5634 :         zoneinfo->dwLastXfrResult = 0;
     295             : 
     296       34723 :         for(i=0; i<zone->num_props; i++){
     297             :                 bool valid_property;
     298       29089 :                 valid_property = dns_zoneinfo_load_zone_property(
     299       29089 :                     zoneinfo, &zone->tmp_props[i]);
     300       29089 :                 if (!valid_property) {
     301           0 :                         TALLOC_FREE(zoneinfo);
     302           0 :                         return NULL;
     303             :                 }
     304             :         }
     305             : 
     306        5630 :         return zoneinfo;
     307             : }
     308             : 
     309       14889 : struct dnsserver_zone *dnsserver_find_zone(struct dnsserver_zone *zones, const char *zone_name)
     310             : {
     311       14889 :         struct dnsserver_zone *z = NULL;
     312             : 
     313       30788 :         for (z = zones; z; z = z->next) {
     314       24711 :                 if (dns_name_equal(zone_name, z->name)) {
     315        8812 :                         break;
     316             :                 }
     317             :         }
     318             : 
     319       14889 :         return z;
     320             : }
     321             : 
     322        1135 : 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         825 :         struct ldb_val name_val =
     327         310 :                 data_blob_string_const(name);
     328             : 
     329        1135 :         dn = ldb_dn_copy(mem_ctx, z->zone_dn);
     330        1135 :         if (dn == NULL) {
     331           0 :                 return NULL;
     332             :         }
     333        1135 :         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        1135 :         ret = ldb_dn_add_child_val(dn,
     343             :                                    "DC",
     344             :                                    name_val);
     345             : 
     346        1135 :         if (!ret) {
     347           0 :                 talloc_free(dn);
     348           0 :                 return NULL;
     349             :         }
     350             : 
     351        1135 :         return dn;
     352             : }
     353             : 
     354        1210 : uint32_t dnsserver_zone_to_request_filter(const char *zone_name)
     355             : {
     356        1210 :         uint32_t request_filter = 0;
     357             : 
     358        1210 :         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        1210 :         } 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        1210 :         } else if (strcmp(zone_name, "..AllPrimaryZones") == 0) {
     388           0 :                 request_filter = DNS_ZONE_REQUEST_PRIMARY;
     389        1210 :         } else if (strcmp(zone_name, "..AllSecondaryZones") == 0) {
     390           0 :                 request_filter = DNS_ZONE_REQUEST_SECONDARY;
     391        1210 :         } else if (strcmp(zone_name, "..AllForwardZones") == 0) {
     392           0 :                 request_filter = DNS_ZONE_REQUEST_FORWARD;
     393        1210 :         } else if (strcmp(zone_name, "..AllReverseZones") == 0) {
     394           0 :                 request_filter = DNS_ZONE_REQUEST_REVERSE;
     395        1210 :         } else if (strcmp(zone_name, "..AllDsZones") == 0) {
     396           0 :                 request_filter = DNS_ZONE_REQUEST_DS;
     397        1210 :         } else if (strcmp(zone_name, "..AllNonDsZones") == 0) {
     398           0 :                 request_filter = DNS_ZONE_REQUEST_NON_DS;
     399        1210 :         } else if (strcmp(zone_name, "..AllPrimaryReverseZones") == 0) {
     400           0 :                 request_filter = DNS_ZONE_REQUEST_PRIMARY
     401             :                         | DNS_ZONE_REQUEST_REVERSE;
     402        1210 :         } else if (strcmp(zone_name, "..AllPrimaryForwardZones") == 0) {
     403           0 :                 request_filter = DNS_ZONE_REQUEST_PRIMARY
     404             :                         | DNS_ZONE_REQUEST_FORWARD;
     405        1210 :         } else if (strcmp(zone_name, "..AllSecondaryReverseZones") == 0) {
     406           0 :                 request_filter = DNS_ZONE_REQUEST_SECONDARY
     407             :                         | DNS_ZONE_REQUEST_REVERSE;
     408        1210 :         } else if (strcmp(zone_name, "..AllSecondaryForwardZones") == 0) {
     409           0 :                 request_filter = DNS_ZONE_REQUEST_SECONDARY
     410             :                         | DNS_ZONE_REQUEST_REVERSE;
     411             :         }
     412             : 
     413        1210 :         return request_filter;
     414             : }

Generated by: LCOV version 1.13