예제 #1
0
 public int UpdatePackageSettings(PackageSettings settings)
 {
     return(PackageController.UpdatePackageSettings(settings));
 }
예제 #2
0
        public static int AddZone(int packageId, int serviceId, string zoneName, bool addPackageItem, bool ignoreGlobalDNSRecords)
        {
            // get DNS provider
            DNSServer dns = GetDNSServer(serviceId);

            // Ensure zoneName is in ascii before saving to database
            zoneName = GetAsciiZoneName(zoneName);

            // check if zone already exists
            if (dns.ZoneExists(zoneName))
            {
                return(BusinessErrorCodes.ERROR_DNS_ZONE_EXISTS);
            }

            //
            TaskManager.StartTask("DNS_ZONE", "ADD", zoneName);
            //
            int zoneItemId = default(int);

            //
            try
            {
                // get secondary DNS services
                StringDictionary primSettings       = ServerController.GetServiceSettings(serviceId);
                string[]         primaryIPAddresses = GetExternalIPAddressesFromString(primSettings["ListeningIPAddresses"]);

                List <string> secondaryIPAddresses = new List <string>();
                List <int>    secondaryServiceIds  = new List <int>();
                string        strSecondaryServices = primSettings["SecondaryDNSServices"];
                if (!String.IsNullOrEmpty(strSecondaryServices))
                {
                    string[] secondaryServices = strSecondaryServices.Split(',');
                    foreach (string strSecondaryId in secondaryServices)
                    {
                        int secondaryId = Utils.ParseInt(strSecondaryId, 0);
                        if (secondaryId == 0)
                        {
                            continue;
                        }

                        secondaryServiceIds.Add(secondaryId);
                        StringDictionary secondarySettings = ServerController.GetServiceSettings(secondaryId);

                        // add secondary IPs to the master array
                        secondaryIPAddresses.AddRange(
                            GetExternalIPAddressesFromString(secondarySettings["ListeningIPAddresses"]));
                    }
                }

                // add "Allow zone transfers"
                string allowTransfers = primSettings["AllowZoneTransfers"];
                if (!String.IsNullOrEmpty(allowTransfers))
                {
                    string[] ips = Utils.ParseDelimitedString(allowTransfers, '\n', ' ', ',', ';');
                    foreach (string ip in ips)
                    {
                        if (!secondaryIPAddresses.Contains(ip))
                        {
                            secondaryIPAddresses.Add(ip);
                        }
                    }
                }

                // add primary zone
                dns.AddPrimaryZone(zoneName, secondaryIPAddresses.ToArray());

                // get DNS zone records
                List <GlobalDnsRecord> records = ServerController.GetDnsRecordsTotal(packageId);

                // get name servers
                PackageSettings packageSettings = PackageController.GetPackageSettings(packageId, PackageSettings.NAME_SERVERS);
                string[]        nameServers     = new string[] { };
                if (!String.IsNullOrEmpty(packageSettings["NameServers"]))
                {
                    nameServers = packageSettings["NameServers"].Split(';');
                }

                // build records list
                List <DnsRecord> zoneRecords = new List <DnsRecord>();

                string primaryNameServer = "ns." + zoneName;

                if (nameServers.Length > 0)
                {
                    primaryNameServer = nameServers[0];
                }

                // update SOA record

                string hostmaster = primSettings["ResponsiblePerson"];
                if (String.IsNullOrEmpty(hostmaster))
                {
                    hostmaster = "hostmaster." + zoneName;
                }
                else
                {
                    hostmaster = Utils.ReplaceStringVariable(hostmaster, "domain_name", zoneName);
                }

                dns.UpdateSoaRecord(zoneName, "", primaryNameServer, hostmaster);

                // add name servers
                foreach (string nameServer in nameServers)
                {
                    DnsRecord ns = new DnsRecord();
                    ns.RecordType = DnsRecordType.NS;
                    ns.RecordName = "";
                    ns.RecordData = nameServer;

                    zoneRecords.Add(ns);
                }

                if (!ignoreGlobalDNSRecords)
                {
                    // add all other records
                    zoneRecords.AddRange(BuildDnsResourceRecords(records, "", zoneName, ""));
                }

                // add zone records
                dns.AddZoneRecords(zoneName, zoneRecords.ToArray());



                // add secondary zones
                foreach (int secondaryId in secondaryServiceIds)
                {
                    try
                    {
                        // add secondary zone
                        DNSServer secDns = GetDNSServer(secondaryId);
                        secDns.AddSecondaryZone(zoneName, primaryIPAddresses);
                        RegisterZoneItems(packageId, secondaryId, zoneName, false);
                    }
                    catch (Exception ex)
                    {
                        TaskManager.WriteError(ex, "Error adding secondary zone (service ID = " + secondaryId + ")");
                    }
                }

                if (!addPackageItem)
                {
                    return(0);
                }
                // add service item
                zoneItemId = RegisterZoneItems(packageId, serviceId, zoneName, true);
                //
                TaskManager.ItemId = zoneItemId;
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
            //
            return(zoneItemId);
        }