Пример #1
0
        public virtual void UpdateSoaRecord(string zoneName, string host, string primaryNsServer,
                                            string primaryPerson)
        {
            // get all zone records
            List <DnsRecord> records = GetZoneRecordsArrayList(zoneName);

            // delete SOA record
            DeleteRecord(zoneName, records, DnsRecordType.SOA, null, null);

            // add new TXT record
            DnsSOARecord soa = new DnsSOARecord();

            soa.RecordType      = DnsRecordType.SOA;
            soa.RecordName      = "";
            soa.PrimaryNsServer = primaryNsServer;
            soa.PrimaryPerson   = primaryPerson;
            records.Add(soa);

            // update primary person contact
            //if (soa.PrimaryPerson.ToLower().EndsWith(zoneName.ToLower()))
            //    soa.PrimaryPerson = soa.PrimaryPerson.Substring(0, (soa.PrimaryPerson.Length - zoneName.Length) - 1);

            // update zone
            UpdateZone(zoneName, records);
        }
Пример #2
0
        public virtual void AddPrimaryZone(string zoneName, string[] secondaryServers)
        {
            // create zone record
            StringBuilder sb = new StringBuilder();

            sb.Append("\r\nzone \"").Append(zoneName).Append("\" in {\r\n");
            sb.Append("\ttype master;\r\n");
            sb.Append("\tfile \"").Append(GetZoneFileName(zoneName)).Append("\";\r\n");
            sb.Append("\tallow-transfer {");
            if (secondaryServers == null || secondaryServers.Length == 0)
            {
                sb.Append(" none;");
            }
            else
            {
                foreach (string ip in secondaryServers)
                {
                    sb.Append(" ").Append(ip).Append(";");
                }
            }

            sb.Append(" };\r\n");
            sb.Append("\tallow-update { none; };\r\n");
            sb.Append("};\r\n");

            // append to config file
            File.AppendAllText(BindConfigPath, sb.ToString());

            // create zone file and fill it with initial info
            List <DnsRecord> records = new List <DnsRecord>();

            // add SOA record
            DnsSOARecord soa = new DnsSOARecord();

            soa.RecordType      = DnsRecordType.SOA;
            soa.RecordName      = "";
            soa.PrimaryNsServer = System.Net.Dns.GetHostEntry("LocalHost").HostName;
            soa.PrimaryPerson   = "hostmaster";          //"hostmaster." + zoneName;
            records.Add(soa);
            ReloadBIND("reconfig", "");
            // add DNS zone
            UpdateZone(zoneName, records);
        }
Пример #3
0
        public virtual void AddPrimaryZone(string zoneName, string[] secondaryServers)
        {
            // CREATE PRIMARY DNS ZONE
            List <DnsRecord> records = new List <DnsRecord>();

            // add "Zone transfers" record
            if (secondaryServers != null && secondaryServers.Length != 0)
            {
                DnsRecord zt = new DnsRecord();
                zt.RecordType = DnsRecordType.Other;
                zt.RecordName = "";
                if (secondaryServers.Length == 1 &&
                    secondaryServers[0] == "*")
                {
                    zt.RecordText = ";$AllowZT *";
                }
                else
                {
                    zt.RecordText = ";$AllowZT " + String.Join(" ", secondaryServers);
                }

                records.Add(zt);
            }

            // add SOA record
            DnsSOARecord soa = new DnsSOARecord();

            soa.RecordType      = DnsRecordType.SOA;
            soa.RecordName      = "";
            soa.PrimaryNsServer = System.Net.Dns.GetHostEntry("LocalHost").HostName;
            soa.PrimaryPerson   = "hostmaster";          //"hostmaster." + zoneName;
            records.Add(soa);

            // add DNS zone
            UpdateZone(zoneName, records);
        }
Пример #4
0
        private void UpdateZone(string zoneName, List <DnsRecord> records, string[] masterServers)
        {
            // build zone file
            StringBuilder sb = new StringBuilder();

            // add SolidCP comment
            sb.Append("; Updated with SolidCP DNS API ").Append(DateTime.Now).Append("\r\n\r\n");

            // TTL
            sb.Append("$TTL ").Append(MinimumTTL).Append("\r\n\r\n");

            // render SOA record
            foreach (DnsRecord rr in records)
            {
                string host = "";
                string type = "";
                string data = "";

                if (rr is DnsSOARecord)
                {
                    type = "IN SOA";
                    DnsSOARecord soa = (DnsSOARecord)rr;
                    host = soa.RecordName;
                    data = String.Format("{0} {1} {2} {3} {4} {5} {6}",
                                         CorrectSOARecord(zoneName, soa.PrimaryNsServer),
                                         CorrectSOARecord(zoneName, soa.PrimaryPerson),
                                         UpdateSerialNumber(soa.SerialNumber),
                                         RefreshInterval,
                                         RetryDelay,
                                         ExpireLimit,
                                         MinimumTTL);

                    // add line to the zone file
                    sb.Append(BuildRecordName(zoneName, host)).Append("\t");
                    sb.Append("\t");
                    sb.Append(type).Append("\t");
                    sb.Append(data);

                    // add line break
                    sb.Append("\r\n");
                }
            }

            // render all other records
            foreach (DnsRecord rr in records)
            {
                string host = "";
                string type = "";
                string data = "";

                if (rr.RecordType == DnsRecordType.A)
                {
                    type = "A";
                    host = rr.RecordName;
                    data = rr.RecordData;
                }
                else if (rr.RecordType == DnsRecordType.AAAA)
                {
                    type = "AAAA";
                    host = rr.RecordName;
                    data = rr.RecordData;
                }
                else if (rr.RecordType == DnsRecordType.NS)
                {
                    type = "NS";
                    host = rr.RecordName;
                    data = BuildRecordData(zoneName, rr.RecordData);
                }
                else if (rr.RecordType == DnsRecordType.CNAME)
                {
                    type = "CNAME";
                    host = rr.RecordName;
                    data = BuildRecordData(zoneName, rr.RecordData);
                }
                else if (rr.RecordType == DnsRecordType.MX)
                {
                    type = "MX";
                    host = rr.RecordName;
                    data = String.Format("{0} {1}",
                                         rr.MxPriority,
                                         BuildRecordData(zoneName, rr.RecordData));
                }
                else if (rr.RecordType == DnsRecordType.TXT)
                {
                    type = "TXT";
                    host = rr.RecordName;
                    data = "\"" + rr.RecordData + "\"";
                }
                else if (rr.RecordType == DnsRecordType.SRV)
                {
                    type = "SRV";
                    host = rr.RecordName;
                    data = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0} {1} {2} {3}",
                                         new object[] { rr.SrvPriority,
                                                        rr.SrvWeight,
                                                        rr.SrvPort,
                                                        BuildRecordData(zoneName, rr.RecordData) });
                }

                // add line to the zone file
                if (type != "")
                {
                    sb.Append(BuildRecordName(zoneName, host)).Append("\t");
                    if (type == "NS")
                    {
                        sb.Append(MinimumTTL);
                    }
                    sb.Append("\t");
                    sb.Append(type).Append("\t");
                    sb.Append(data);

                    // add line break
                    sb.Append("\r\n");
                }
            }

            // update zone file
            UpdateZoneFile(zoneName, sb.ToString());
            ReloadBIND("reload", zoneName);
        }
Пример #5
0
        private List <DnsRecord> ParseZoneFileToArrayList(string zoneName, string zf)
        {
            List <DnsRecord> records = new List <DnsRecord>();
            StringReader     reader  = new StringReader(zf);
            string           zfLine  = null;

            DnsSOARecord soa = null;

            while ((zfLine = reader.ReadLine()) != null)
            {
                //string line = Regex.Replace(zfLine, "\\s+", " ").Trim();

                string[] columns = zfLine.Split('\t');

                string recordName  = "";
                string recordTTL   = "";
                string recordType  = "";
                string recordData  = "";
                string recordData2 = "";

                recordName = columns[0];
                if (columns.Length > 1)
                {
                    recordTTL = columns[1];
                }
                if (columns.Length > 2)
                {
                    recordType = columns[2];
                }
                if (columns.Length > 3)
                {
                    recordData = columns[3];
                }
                if (columns.Length > 4)
                {
                    recordData2 = columns[4].Trim();
                }

                if (recordType == "IN SOA")
                {
                    string[] dataColumns = recordData.Split(' ');

                    // parse SOA record
                    soa                 = new DnsSOARecord();
                    soa.RecordType      = DnsRecordType.SOA;
                    soa.RecordName      = "";
                    soa.PrimaryNsServer = RemoveTrailingDot(dataColumns[0]);
                    soa.PrimaryPerson   = RemoveTrailingDot(dataColumns[1]);
                    soa.RecordText      = zfLine;
                    if (dataColumns[2] != "(")
                    {
                        soa.SerialNumber = dataColumns[2];
                    }

                    // add to the collection
                    records.Add(soa);
                }
                else if (recordData2.IndexOf("; Serial number") != -1)
                {
                    string[] dataColumns = recordData2.Split(' ');

                    // append soa serial number
                    soa.SerialNumber = dataColumns[0];
                }
                else if (recordType == "NS")                 // NS record with empty host
                {
                    DnsRecord r = new DnsRecord();
                    r.RecordType = DnsRecordType.NS;
                    r.RecordName = CorrectRecordName(zoneName, recordName);
                    r.RecordData = CorrectRecordData(zoneName, recordData);
                    r.RecordText = zfLine;
                    records.Add(r);
                }
                else if (recordType == "A")                 // A record
                {
                    DnsRecord r = new DnsRecord();
                    r.RecordType = DnsRecordType.A;
                    r.RecordName = CorrectRecordName(zoneName, recordName);
                    r.RecordData = recordData;
                    r.RecordText = zfLine;
                    records.Add(r);
                }
                else if (recordType == "AAAA")                 // A record
                {
                    DnsRecord r = new DnsRecord();
                    r.RecordType = DnsRecordType.AAAA;
                    r.RecordName = CorrectRecordName(zoneName, recordName);
                    r.RecordData = recordData;
                    r.RecordText = zfLine;
                    records.Add(r);
                }
                else if (recordType == "CNAME")                 // CNAME record
                {
                    DnsRecord r = new DnsRecord();
                    r.RecordType = DnsRecordType.CNAME;
                    r.RecordName = CorrectRecordName(zoneName, recordName);
                    r.RecordData = CorrectRecordData(zoneName, recordData);
                    r.RecordText = zfLine;
                    records.Add(r);
                }
                else if (recordType == "MX")                 // MX record
                {
                    string[] dataColumns = recordData.Split(' ');

                    DnsRecord r = new DnsRecord();
                    r.RecordType = DnsRecordType.MX;
                    r.RecordName = CorrectRecordName(zoneName, recordName);
                    r.MxPriority = Int32.Parse(dataColumns[0]);
                    r.RecordData = CorrectRecordData(zoneName, dataColumns[1]);
                    r.RecordText = zfLine;
                    records.Add(r);
                }
                else if (recordType == "TXT")                 // TXT record
                {
                    DnsRecord r = new DnsRecord();
                    r.RecordType = DnsRecordType.TXT;
                    r.RecordName = CorrectRecordName(zoneName, recordName);
                    r.RecordData = recordData.Substring(1, recordData.Length - 2);
                    r.RecordText = zfLine;
                    records.Add(r);
                }
                else if (recordType == "SRV")                 // SRV record
                {
                    string[] dataColumns = recordData.Split(' ');

                    DnsRecord r = new DnsRecord();
                    r.RecordType  = DnsRecordType.SRV;
                    r.RecordName  = CorrectRecordName(zoneName, recordName);
                    r.SrvPriority = Int32.Parse(dataColumns[0]);
                    r.SrvWeight   = Int32.Parse(dataColumns[1]);
                    r.SrvPort     = Int32.Parse(dataColumns[2]);
                    r.RecordData  = CorrectRecordData(zoneName, dataColumns[3]);
                    r.RecordText  = zfLine;
                    records.Add(r);
                }

                //Debug.WriteLine(zfLine);
            }
            return(records);
        }
Пример #6
0
        protected virtual void UpdateZone(string zoneName, List <DnsRecord> records, string[] masterServers)
        {
            // build zone file
            StringBuilder sb = new StringBuilder();

            // add SolidCP comment
            sb.Append(";$; Updated with SolidCP DNS API ").Append(DateTime.Now).Append("\r");

            // render comment/service records
            foreach (DnsRecord rr in records)
            {
                if (rr.RecordText != null && rr.RecordText.StartsWith(";") && !(rr.RecordType == DnsRecordType.TXT))
                {
                    sb.Append(rr.RecordText);

                    // add line break
                    sb.Append("\r");
                }
            }

            // render SOA record
            foreach (DnsRecord rr in records)
            {
                string host = "";
                string type = "";
                string data = "";

                if (rr is DnsSOARecord)
                {
                    type = "IN SOA";
                    DnsSOARecord soa = (DnsSOARecord)rr;
                    host = soa.RecordName;
                    data = String.Format("{0} {1} {2} {3} {4} {5} {6}",
                                         CorrectSOARecord(zoneName, soa.PrimaryNsServer),
                                         CorrectSOARecord(zoneName, soa.PrimaryPerson),
                                         UpdateSerialNumber(soa.SerialNumber),
                                         RefreshInterval,
                                         RetryDelay,
                                         ExpireLimit,
                                         MinimumTTL);

                    // add line to the zone file
                    sb.Append(BuildRecordName(zoneName, host)).Append("\t");
                    sb.Append("\t");
                    sb.Append(type).Append("\t");
                    sb.Append(data);

                    // add line break
                    sb.Append("\r");
                }
            }

            // render all other records
            foreach (DnsRecord rr in records)
            {
                string host = String.Empty;
                string type = String.Empty;
                string data = String.Empty;
                string name = String.Empty;

                if (rr.RecordType == DnsRecordType.A)
                {
                    type = "A";
                    host = rr.RecordName;
                    data = rr.RecordData;
                    name = BuildRecordName(zoneName, host);
                }
                else if (rr.RecordType == DnsRecordType.AAAA)
                {
                    type = "AAAA";
                    host = rr.RecordName;
                    data = rr.RecordData;
                    name = BuildRecordName(zoneName, host);
                }
                else if (rr.RecordType == DnsRecordType.NS)
                {
                    type = "NS";
                    host = rr.RecordName;
                    data = BuildRecordData(zoneName, rr.RecordData);
                    name = BuildRecordName(zoneName, host);
                }
                else if (rr.RecordType == DnsRecordType.CNAME)
                {
                    type = "CNAME";
                    host = rr.RecordName;
                    data = BuildRecordData(zoneName, rr.RecordData, rr.RecordData);
                    name = host;
                }
                else if (rr.RecordType == DnsRecordType.MX)
                {
                    type = "MX";
                    host = rr.RecordName;
                    data = String.Format("{0} {1}",
                                         rr.MxPriority,
                                         BuildRecordData(zoneName, rr.RecordData));
                    name = BuildRecordName(zoneName, host);
                }
                else if (rr.RecordType == DnsRecordType.TXT)
                {
                    type = "TXT";
                    host = rr.RecordName;
                    data = "\"" + rr.RecordData + "\"";
                    name = BuildRecordName(zoneName, host);
                }

                // add line to the zone file
                if (type != "")
                {
                    sb.Append(name).Append("\t");
                    if (type == "NS")
                    {
                        sb.Append(MinimumTTL);
                    }
                    sb.Append("\t");
                    sb.Append(type).Append("\t");
                    sb.Append(data);

                    // add line break
                    sb.Append("\r");
                }
            }

            string zoneFile = sb.ToString();

            // update zone file
            string queryParams = "zone=" + zoneName + "&data=" + zoneFile;

            if (masterServers != null && masterServers.Length > 0)
            {
                queryParams += "&masterip=" + masterServers[0];
            }

            // execute query
            string result = ExecuteDnsQuery("updatezone", queryParams);
        }