Exemple #1
0
        /// <summary>
        /// Parses specified count of answers from query.
        /// </summary>
        /// <param name="reply">Server returned query.</param>
        /// <param name="answerCount">Number of answers to parse.</param>
        /// <param name="offset">Position from where to start parsing answers.</param>
        /// <returns></returns>
        private List <DNS_rr> ParseAnswers(byte[] reply, int answerCount, ref int offset)
        {
            /* RFC 1035 4.1.3. Resource record format
             *
             *                                                         1  1  1  1  1  1
             * 0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             |                                               |
             | /                                               /
             | /                      NAME                     /
             |                                               |
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             |                      TYPE                     |
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             |                     CLASS                     |
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             |                      TTL                      |
             |                                               |
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             |                   RDLENGTH                    |
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
             | /                     RDATA                     /
             | /                                               /
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             */

            List <DNS_rr> answers = new List <DNS_rr>();

            //---- Start parsing answers ------------------------------------------------------------------//
            for (int i = 0; i < answerCount; i++)
            {
                string name = "";
                if (!GetQName(reply, ref offset, ref name))
                {
                    break;
                }

                int type     = reply[offset++] << 8 | reply[offset++];
                int rdClass  = reply[offset++] << 8 | reply[offset++];
                int ttl      = reply[offset++] << 24 | reply[offset++] << 16 | reply[offset++] << 8 | reply[offset++];
                int rdLength = reply[offset++] << 8 | reply[offset++];

                if ((DNS_QType)type == DNS_QType.A)
                {
                    answers.Add(DNS_rr_A.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if ((DNS_QType)type == DNS_QType.NS)
                {
                    answers.Add(DNS_rr_NS.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if ((DNS_QType)type == DNS_QType.CNAME)
                {
                    answers.Add(DNS_rr_CNAME.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if ((DNS_QType)type == DNS_QType.SOA)
                {
                    answers.Add(DNS_rr_SOA.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if ((DNS_QType)type == DNS_QType.PTR)
                {
                    answers.Add(DNS_rr_PTR.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if ((DNS_QType)type == DNS_QType.HINFO)
                {
                    answers.Add(DNS_rr_HINFO.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if ((DNS_QType)type == DNS_QType.MX)
                {
                    answers.Add(DNS_rr_MX.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if ((DNS_QType)type == DNS_QType.TXT)
                {
                    answers.Add(DNS_rr_TXT.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if ((DNS_QType)type == DNS_QType.AAAA)
                {
                    answers.Add(DNS_rr_AAAA.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if ((DNS_QType)type == DNS_QType.SRV)
                {
                    answers.Add(DNS_rr_SRV.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if ((DNS_QType)type == DNS_QType.NAPTR)
                {
                    answers.Add(DNS_rr_NAPTR.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if ((DNS_QType)type == DNS_QType.SPF)
                {
                    answers.Add(DNS_rr_SPF.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else
                {
                    // Unknown record, skip it.
                    offset += rdLength;
                }
            }

            return(answers);
        }
        public bool Filter(string from, IMailServerManagementApi api, SMTP_Session session, out string errorText)
        {
            errorText = null;
            bool result = true;

            if (session.IsAuthenticated || this.IsPrivateIP(session.RemoteEndPoint.Address))
            {
                return(true);
            }
            try
            {
                DataSet dataSet = new DataSet();
                dataSet.Tables.Add("General");
                dataSet.Tables["General"].Columns.Add("CheckHelo");
                dataSet.Tables["General"].Columns.Add("LogRejections");
                dataSet.Tables.Add("BlackListSettings");
                dataSet.Tables["BlackListSettings"].Columns.Add("ErrorText");
                dataSet.Tables.Add("BlackList");
                dataSet.Tables["BlackList"].Columns.Add("IP");
                dataSet.Tables.Add("Servers");
                dataSet.Tables["Servers"].Columns.Add("Cost");
                dataSet.Tables["Servers"].Columns.Add("Server");
                dataSet.Tables["Servers"].Columns.Add("DefaultRejectionText");
                dataSet.ReadXml(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\DnsBlackList.xml");
                bool flag = false;
                if (dataSet.Tables["General"].Rows.Count == 1)
                {
                    if (Convert.ToBoolean(dataSet.Tables["General"].Rows[0]["CheckHelo"]))
                    {
                        DnsServerResponse dnsServerResponse = Dns_Client.Static.Query(session.EhloHost, DNS_QType.A);
                        if (dnsServerResponse.ConnectionOk && dnsServerResponse.ResponseCode != DNS_RCode.SERVER_FAILURE)
                        {
                            bool       flag2    = false;
                            DNS_rr_A[] aRecords = dnsServerResponse.GetARecords();
                            for (int i = 0; i < aRecords.Length; i++)
                            {
                                DNS_rr_A dNS_rr_A = aRecords[i];
                                if (session.RemoteEndPoint.Address.Equals(dNS_rr_A.IP))
                                {
                                    flag2 = true;
                                    break;
                                }
                            }
                            if (!flag2)
                            {
                                errorText = "Not valid DNS EHLO/HELO name for your IP '" + session.EhloHost + "' !";
                                bool result2 = false;
                                return(result2);
                            }
                        }
                    }
                    flag = ConvertEx.ToBoolean(dataSet.Tables["General"].Rows[0]["LogRejections"]);
                }
                foreach (DataRow dataRow in dataSet.Tables["BlackList"].Rows)
                {
                    if (this.IsAstericMatch(dataRow["IP"].ToString(), session.RemoteEndPoint.Address.ToString()))
                    {
                        errorText = dataSet.Tables["BlackListSettings"].Rows[0]["ErrorText"].ToString();
                        bool result2 = false;
                        return(result2);
                    }
                }
                foreach (DataRow dataRow2 in dataSet.Tables["Servers"].Rows)
                {
                    DnsServerResponse dnsServerResponse2 = Dns_Client.Static.Query(this.ReverseIP(session.RemoteEndPoint.Address) + "." + dataRow2["Server"].ToString(), DNS_QType.ANY);
                    DNS_rr_A[]        aRecords2          = dnsServerResponse2.GetARecords();
                    if (aRecords2.Length > 0)
                    {
                        if (flag)
                        {
                            this.WriteFilterLog(string.Concat(new string[]
                            {
                                "Sender:",
                                from,
                                " IP:",
                                session.RemoteEndPoint.Address.ToString(),
                                " blocked\r\n"
                            }));
                        }
                        errorText = dataRow2["DefaultRejectionText"].ToString();
                        if (dnsServerResponse2.GetTXTRecords().Length > 0)
                        {
                            errorText = dnsServerResponse2.GetTXTRecords()[0].Text;
                        }
                        if (errorText == "")
                        {
                            errorText = "You are in '" + dataRow2["Server"].ToString() + "' rejection list !";
                        }
                        bool result2 = false;
                        return(result2);
                    }
                }
            }
            catch
            {
            }
            return(result);
        }