public int RestoreItem(string tempFolder, XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group) { if (itemType != typeof(DnsZone)) { return(0); } // DNS provider DNSServer dns = GetDNSServer(serviceId); // check service item if (!dns.ZoneExists(itemName)) { // create primary and secondary zones AddZone(packageId, serviceId, itemName, false, false); // restore records XmlSerializer serializer = new XmlSerializer(typeof(DnsRecord)); List <DnsRecord> records = new List <DnsRecord>(); foreach (XmlNode childNode in itemNode.ChildNodes) { if (childNode.Name == "DnsRecord") { records.Add((DnsRecord)serializer.Deserialize(new XmlNodeReader(childNode))); } } dns.AddZoneRecords(itemName, records.ToArray()); } // check if meta-item exists int zoneId = 0; DnsZone item = (DnsZone)PackageController.GetPackageItemByName(packageId, itemName, typeof(DnsZone)); if (item == null) { // restore meta-item item = new DnsZone(); item.Name = itemName; item.PackageId = packageId; item.ServiceId = serviceId; zoneId = PackageController.AddPackageItem(item); } else { zoneId = item.Id; } // restore domains RestoreDomainByZone(itemName, packageId, zoneId); return(0); }
/// <summary> /// Adds SharePoint site collection. /// </summary> /// <param name="item">Site collection description.</param> /// <returns>Created site collection id within metabase.</returns> public static int AddSiteCollection(SharePointEnterpriseSiteCollection item) { // Check account. int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive); if (accountCheck < 0) { return(accountCheck); } // Check package. int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive); if (packageCheck < 0) { return(packageCheck); } // Check quota. OrganizationStatistics orgStats = OrganizationController.GetOrganizationStatistics(item.OrganizationId); //QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.HOSTED_SHAREPOINT_SITES); if (orgStats.AllocatedSharePointEnterpriseSiteCollections > -1 && orgStats.CreatedSharePointEnterpriseSiteCollections >= orgStats.AllocatedSharePointEnterpriseSiteCollections) { return(BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_QUOTA_LIMIT); } // Check if stats resource is available int serviceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.SharepointEnterpriseServer); if (serviceId == 0) { return(BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_UNAVAILABLE); } StringDictionary hostedSharePointSettings = ServerController.GetServiceSettings(serviceId); QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.HOSTED_SHAREPOINT_ENTERPRISE_USESHAREDSSL); Uri rootWebApplicationUri = new Uri(hostedSharePointSettings["RootWebApplicationUri"]); Organization org = OrganizationController.GetOrganization(item.OrganizationId); string siteName = item.Name; if (quota.QuotaAllocatedValue == 1) { string sslRoot = hostedSharePointSettings["SharedSSLRoot"]; string defaultDomain = org.DefaultDomain; string hostNameBase = string.Empty; string[] tmp = defaultDomain.Split('.'); if (tmp.Length == 2) { hostNameBase = tmp[0]; } else { if (tmp.Length > 2) { hostNameBase = tmp[0] + tmp[1]; } } int counter = 0; item.Name = String.Format("{0}://{1}", rootWebApplicationUri.Scheme, hostNameBase + "-" + counter.ToString() + "." + sslRoot); siteName = String.Format("{0}", hostNameBase + "-" + counter.ToString() + "." + sslRoot); while (DataProvider.CheckServiceItemExists(serviceId, item.Name, "WebsitePanel.Providers.SharePoint.SharePointEnterpriseSiteCollection, WebsitePanel.Providers.Base")) { counter++; item.Name = String.Format("{0}://{1}", rootWebApplicationUri.Scheme, hostNameBase + "-" + counter.ToString() + "." + sslRoot); siteName = String.Format("{0}", hostNameBase + "-" + counter.ToString() + "." + sslRoot); } } else { item.Name = String.Format("{0}://{1}", rootWebApplicationUri.Scheme, item.Name); } if (rootWebApplicationUri.Port > 0 && rootWebApplicationUri.Port != 80 && rootWebApplicationUri.Port != 443) { item.PhysicalAddress = String.Format("{0}:{1}", item.Name, rootWebApplicationUri.Port); } else { item.PhysicalAddress = item.Name; } if (Utils.ParseBool(hostedSharePointSettings["LocalHostFile"], false)) { item.RootWebApplicationInteralIpAddress = hostedSharePointSettings["RootWebApplicationInteralIpAddress"]; item.RootWebApplicationFQDN = item.Name.Replace(rootWebApplicationUri.Scheme + "://", ""); } item.MaxSiteStorage = RecalculateMaxSize(org.MaxSharePointEnterpriseStorage, (int)item.MaxSiteStorage); item.WarningStorage = item.MaxSiteStorage == -1 ? -1 : Math.Min((int)item.WarningStorage, item.MaxSiteStorage); // Check package item with given name already exists. if (PackageController.GetPackageItemByName(item.PackageId, item.Name, typeof(SharePointEnterpriseSiteCollection)) != null) { return(BusinessErrorCodes.ERROR_SHAREPOINT_PACKAGE_ITEM_EXISTS); } // Log operation. TaskManager.StartTask("HOSTED_SHAREPOINT_ENTERPRISE", "ADD_SITE_COLLECTION", item.Name); try { // Create site collection on server. HostedSharePointServerEnt hostedSharePointServer = GetHostedSharePointServer(serviceId); hostedSharePointServer.Enterprise_CreateSiteCollection(item); // Make record in metabase. item.ServiceId = serviceId; int itemId = PackageController.AddPackageItem(item); hostedSharePointServer.Enterprise_SetPeoplePickerOu(item.Name, org.DistinguishedName); int dnsServiceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.Dns); if (dnsServiceId > 0) { string[] tmpStr = siteName.Split('.'); string hostName = tmpStr[0]; string domainName = siteName.Substring(hostName.Length + 1, siteName.Length - (hostName.Length + 1)); List <GlobalDnsRecord> dnsRecords = ServerController.GetDnsRecordsByService(serviceId); List <DnsRecord> resourceRecords = DnsServerController.BuildDnsResourceRecords(dnsRecords, hostName, domainName, ""); DNSServer dns = new DNSServer(); ServiceProviderProxy.Init(dns, dnsServiceId); // add new resource records dns.AddZoneRecords(domainName, resourceRecords.ToArray()); } TaskManager.ItemId = itemId; return(itemId); } catch (Exception ex) { throw TaskManager.WriteError(ex); } finally { TaskManager.CompleteTask(); } }
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); }
public static void AddServiceDNSRecords(int packageId, string groupName, DomainInfo domain, string serviceIP, bool wildcardOnly) { int serviceId = PackageController.GetPackageServiceId(packageId, groupName); if (serviceId > 0) { List<DnsRecord> tmpZoneRecords = new List<DnsRecord>(); List<GlobalDnsRecord> dnsRecords = ServerController.GetDnsRecordsByService(serviceId); if (wildcardOnly) { List<GlobalDnsRecord> temp = new List<GlobalDnsRecord>(); foreach (GlobalDnsRecord d in dnsRecords) { if ((d.RecordName == "*") || (d.RecordName == "@")) temp.Add(d); } dnsRecords = temp; } DnsZone zone = (DnsZone)PackageController.GetPackageItem(domain.ZoneItemId); tmpZoneRecords.AddRange(DnsServerController.BuildDnsResourceRecords(dnsRecords, "", domain.ZoneName, serviceIP)); try { DNSServer dns = new DNSServer(); ServiceProviderProxy.Init(dns, zone.ServiceId); DnsRecord[] domainRecords = dns.GetZoneRecords(domain.DomainName); List<DnsRecord> zoneRecords = new List<DnsRecord>(); foreach (DnsRecord t in tmpZoneRecords) { if (!RecordDoesExist(t, domainRecords)) zoneRecords.Add(t); } // add new resource records dns.AddZoneRecords(zone.Name, zoneRecords.ToArray()); } catch (Exception ex1) { TaskManager.WriteError(ex1, "Error updating DNS records"); } } }