Query() public method

Queries server with specified query.
public Query ( string queryText, QTYPE queryType ) : DnsServerResponse
queryText string Query text. It depends on queryType.
queryType QTYPE Query type.
return DnsServerResponse
Exemplo n.º 1
0
//-------------------------------------------------------------------------------------------
    private string LookupA(string recordname, string sectiontext)
    {
        Dns_Client        dc  = new LumiSoft.Net.Dns.Client.Dns_Client();
        DnsServerResponse dsr = null;

        try
        {
            dsr = dc.Query(recordname, LumiSoft.Net.Dns.Client.QTYPE.A);
        }
        catch (Exception ex)
        {
            throw new Exception("DNS A record query for " + recordname + " failed.", ex);
        }
        DNS_rr_A[] records = dsr.GetARecords();

        string response = sectiontext;

        if (records.Length < 1)
        {
            fail = true;
        }
        for (int i = 0; i < records.Length; i++)
        {
            response += (i + 1) + ".&nbsp;&nbsp;&nbsp;";
            response += records[i].IP + " with a TTL of " + records[i].TTL + "<br>";
            response += CheckPort(records[i].IP.ToString(), 5222);
            response += CheckPort(records[i].IP.ToString(), 5223);
            response += CheckPort(records[i].IP.ToString(), 5269);
            response += CheckPort(records[i].IP.ToString(), 5270);
        }
        return(response);
    }
        /// <summary>
        /// Filters sender.
        /// </summary>
        /// <param name="from">Sender.</param>
        /// <param name="api">Reference to server API.</param>
        /// <param name="session">Reference to SMTP session.</param>
        /// <param name="errorText">Filtering error text what is returned to client. ASCII text, 100 chars maximum.</param>
        /// <returns>Returns true if sender is ok or false if rejected.</returns>
        public bool Filter(string from,IMailServerApi api,SMTP_Session session,out string errorText)
        {
            errorText = "";
            string ip = session.RemoteEndPoint.Address.ToString();

            Dns_Client dns = new Dns_Client();

            bool ok = false;

            // Don't check PTR for authenticated session and LAN IP ranges
            if(session.Authenticated || ip.StartsWith("127.0.0.1") || ip.StartsWith("10.") || ip.StartsWith("192.168")){
                return true;
            }

            DnsServerResponse reponse = dns.Query(ip,QTYPE.PTR);
            if(reponse.ResponseCode == RCODE.NO_ERROR){
                foreach(PTR_Record rec in reponse.GetPTRRecords()){
                    if(rec.DomainName.ToLower() == session.EhloName.ToLower()){
                        ok = true;
                        break;
                    }
                }
            }

            if(!ok){
                errorText = "Bad EHLO/HELO name, you must have valid DNS PTR record for your EHLO name and IP.";
            }

            return ok;
        }
Exemplo n.º 3
0
//-------------------------------------------------------------------------------------------
    private string LookupSRV(string recordname, string domain, string sectiontext)
    {
        recordname += domain;
        Dns_Client        dc  = new LumiSoft.Net.Dns.Client.Dns_Client();
        DnsServerResponse dsr = null;

        try
        {
            dsr = dc.Query(recordname, LumiSoft.Net.Dns.Client.QTYPE.SRV);
        }
        catch (Exception ex)
        {
            return("<span style='color:red'>DNS SRV record query for " + recordname + domain + " timed out.</span>");
        }
        DNS_rr_SRV[] records = dsr.GetSRVRecords();

        string response = sectiontext;

        if (records.Length < 1)
        {
            fail = true;
        }
        for (int i = 0; i < records.Length; i++)
        {
            DnsServerResponse dsr2;
            try
            {
                dsr2 = dc.Query(records[i].Target, QTYPE.A);
            }
            catch (Exception ex)
            {
                throw new Exception("DNS sub A record query for " + domain + " failed.", ex);
            }
            DNS_rr_A[] aRecs = dsr2.GetARecords();

            response += (i + 1) + ".&nbsp;&nbsp;&nbsp;";
            response += records[i].Target + " on port " + records[i].Port + " with a TTL of " + records[i].TTL + " and a priority of " + records[i].Priority + "<br />";

            for (int x = 0; x < aRecs.Length; x++)
            {
                response += "&nbsp;&nbsp;---->&nbsp;Resolves to " + aRecs[x].IP + "<br />";
                response += CheckPort(aRecs[x].IP, records[i].Port);
                response += CheckPort(aRecs[x].IP, 5223);
            }
        }
        return(response);
    }
Exemplo n.º 4
0
        /// <summary>
        /// Resolves host name to IP addresses.
        /// </summary>
        /// <param name="host">Host name or IP address.</param>
        /// <returns>Return specified host IP addresses.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>host</b> is null.</exception>
        public static IPAddress[] Resolve(string host)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }

            // If hostName_IP is IP
            try{
                return(new IPAddress[] { IPAddress.Parse(host) });
            }
            catch {
            }

            // This is probably NetBios name
            if (host.IndexOf(".") == -1)
            {
                return(System.Net.Dns.GetHostEntry(host).AddressList);
            }
            else
            {
                // hostName_IP must be host name, try to resolve it's IP
                Dns_Client        dns  = new Dns_Client();
                DnsServerResponse resp = dns.Query(host, QTYPE.A);
                if (resp.ResponseCode == RCODE.NO_ERROR)
                {
                    DNS_rr_A[]  records = resp.GetARecords();
                    IPAddress[] retVal  = new IPAddress[records.Length];
                    for (int i = 0; i < records.Length; i++)
                    {
                        retVal[i] = records[i].IP;
                    }

                    return(retVal);
                }
                else
                {
                    throw new Exception(resp.ResponseCode.ToString());
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Resolves host name to IP addresses.
        /// </summary>
        /// <param name="host">Host name or IP address.</param>
        /// <returns>Return specified host IP addresses.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>host</b> is null.</exception>
        public static IPAddress[] Resolve(string host)
        {
            if(host == null){
                throw new ArgumentNullException("host");
            }

            // If hostName_IP is IP
            try{
                return new IPAddress[]{IPAddress.Parse(host)};
            }
            catch{
            }

            // This is probably NetBios name
            if(host.IndexOf(".") == -1){
                return System.Net.Dns.GetHostEntry(host).AddressList;
            }
            else{
                // hostName_IP must be host name, try to resolve it's IP
                Dns_Client dns = new Dns_Client();
                DnsServerResponse resp = dns.Query(host,QTYPE.A);
                if(resp.ResponseCode == RCODE.NO_ERROR){
                    DNS_rr_A[] records = resp.GetARecords();
                    IPAddress[] retVal = new IPAddress[records.Length];
                    for(int i=0;i<records.Length;i++){
                        retVal[i] = records[i].IP;
                    }

                    return retVal;
                }
                else{
                    throw new Exception(resp.ResponseCode.ToString());
                }
            }
        }
        /// <summary>
        /// Filters sender.
        /// </summary>
        /// <param name="from">Sender.</param>
        /// <param name="api">Reference to server API.</param>
        /// <param name="session">Reference to SMTP session.</param>
        /// <param name="errorText">Filtering error text what is returned to client. ASCII text, 100 chars maximum.</param>
        /// <returns>Returns true if sender is ok or false if rejected.</returns>
        public bool Filter(string from,IMailServerApi api,SMTP_Session session,out string errorText)
        {
            errorText = null;
            bool ok = true;

            // Don't check authenticated users or LAN IP
            if(session.Authenticated || IsPrivateIP(session.RemoteEndPoint.Address)){
                return true;
            }

            try{
                //--- Load data -----------------------
                DataSet ds = new DataSet();
                ds.Tables.Add("General");
                ds.Tables["General"].Columns.Add("CheckHelo");
                ds.Tables.Add("BlackListSettings");
                ds.Tables["BlackListSettings"].Columns.Add("ErrorText");
                ds.Tables.Add("BlackList");
                ds.Tables["BlackList"].Columns.Add("IP");
                ds.Tables.Add("Servers");
                ds.Tables["Servers"].Columns.Add("Cost");
                ds.Tables["Servers"].Columns.Add("Server");
                ds.Tables["Servers"].Columns.Add("DefaultRejectionText");

                ds.ReadXml(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\lsDNSBL_Filter_db.xml");

                Dns_Client dns = new Dns_Client();

                #region General

                if(ds.Tables["General"].Rows.Count == 1){
                    if(Convert.ToBoolean(ds.Tables["General"].Rows[0]["CheckHelo"])){
                        DnsServerResponse response = dns.Query(session.EhloName,QTYPE.A);
                        // If dns server connection errors, don't block.
                        if(response.ConnectionOk && response.ResponseCode != RCODE.SERVER_FAILURE){
                            bool found = false;
                            foreach(A_Record a in response.GetARecords()){
                                if(session.RemoteEndPoint.Address.ToString() == a.IP){
                                    found = true;
                                    break;
                                }
                            }
                            if(!found){
                                errorText = "Not valid DNS EHLO/HELO name '" + session.EhloName + "' !";
                                return false;
                            }
                        }
                    }
                }

                #endregion

                #region Balck List

                foreach (DataRow dr in ds.Tables["BlackList"].Rows){
                    if(IsAstericMatch(dr["IP"].ToString(),session.RemoteEndPoint.Address.ToString())){
                        errorText = ds.Tables["BlackListSettings"].Rows[0]["ErrorText"].ToString();
                        return false;
                    }
                }

                #endregion

                #region DNSBL

                foreach(DataRow dr in ds.Tables["Servers"].Rows){
                    DnsServerResponse dnsResponse =  dns.Query(ReverseIP(session.RemoteEndPoint.Address) + "." + dr["Server"].ToString(),QTYPE.ANY);
                    A_Record[] recs = dnsResponse.GetARecords();
                    if(recs.Length > 0){
                        // Logging blocking
                        WriteFilterLog("Sender:" + from + " IP:" + session.RemoteEndPoint.Address.ToString() + " blocked\r\n");

                        errorText = dr["DefaultRejectionText"].ToString();
                        // Server provided return text, use it
                        if(dnsResponse.GetTXTRecords().Length > 0){
                            errorText = dnsResponse.GetTXTRecords()[0].Text;
                        }
                        if(errorText == ""){
                            errorText = "You are in '" + dr["Server"].ToString() + "' rejection list !";
                        }
                        return false;
                    }
                }

                #endregion
            }
            catch{
            }

            return ok;
        }
        private void m_pTestDns_Click(object sender, EventArgs e)
        {
            Dns_Client.DnsServers  = new string[]{m_pDns1.Text};
            Dns_Client.UseDnsCache = false;
            Dns_Client dns = new Dns_Client();

            DnsServerResponse response = dns.Query("lumisoft.ee",QTYPE.MX);
            if(!response.ConnectionOk || response.ResponseCode != RCODE.NO_ERROR){
                MessageBox.Show(this,"Invalid dns server(" + m_pDns1.Text + "), can't resolve lumisoft.ee","Info",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }

            Dns_Client.DnsServers  = new string[]{m_pDns2.Text};
            Dns_Client.UseDnsCache = false;
            Dns_Client dns2 = new Dns_Client();

            response = dns2.Query("lumisoft.ee",QTYPE.MX);
            if(!response.ConnectionOk || response.ResponseCode != RCODE.NO_ERROR){
                MessageBox.Show(this,"Invalid dns server(" + m_pDns2.Text + "), can't resolve lumisoft.ee","Info",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }

            MessageBox.Show(this,"Ok.","Info",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets specified email domain SMTP hosts. Values are in descending priority order.
        /// </summary>
        /// <param name="domain">Domain name. This value can be email address too, then domain parsed automatically.</param>
        /// <returns>Returns specified email domain SMTP hosts.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>domain</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        /// <exception cref="DNS_ClientException">Is raised when DNS query failure.</exception>
        public static string[] GetDomainHosts(string domain)
        {
            if(domain == null){
                throw new ArgumentNullException("domain");
            }
            if(string.IsNullOrEmpty(domain)){
                throw new ArgumentException("Invalid argument 'domain' value, you need to specify domain value.");
            }

            // We have email address, parse domain.
            if(domain.IndexOf("@") > -1){
                domain = domain.Substring(domain.IndexOf('@') + 1);
            }

            List<string> retVal = new List<string>();

            // Get MX records.
            Dns_Client dns = new Dns_Client();
            DnsServerResponse response = dns.Query(domain,QTYPE.MX);
            if(response.ResponseCode == RCODE.NO_ERROR){
                foreach(DNS_rr_MX mx in response.GetMXRecords()){
                    // Block invalid MX records.
                    if(!string.IsNullOrEmpty(mx.Host)){
                        retVal.Add(mx.Host);
                    }
                }
            }
            else{
                throw new DNS_ClientException(response.ResponseCode);
            }

            /* RFC 2821 5.
                If no MX records are found, but an A RR is found, the A RR is treated as if it
                was associated with an implicit MX RR, with a preference of 0, pointing to that host.
            */
            if(retVal.Count == 0){
                retVal.Add(domain);
            }

            return retVal.ToArray();
        }
Exemplo n.º 9
0
    //-------------------------------------------------------------------------------------------
    private string LookupSRV(string recordname, string domain, string sectiontext)
    {
        recordname                 += domain;
          Dns_Client          dc      = new LumiSoft.Net.Dns.Client.Dns_Client();
          DnsServerResponse   dsr     = null;
          try
          {
               dsr = dc.Query(recordname, LumiSoft.Net.Dns.Client.QTYPE.SRV);
          }
          catch (Exception ex)
          {
               return "<span style='color:red'>DNS SRV record query for " + recordname + domain + " timed out.</span>";
          }
          DNS_rr_SRV[]        records = dsr.GetSRVRecords();

          string response = sectiontext;
          if (records.Length < 1)
               fail = true;
          for (int i = 0; i < records.Length; i++)
          {
               DnsServerResponse dsr2;
               try
               {
                    dsr2 = dc.Query(records[i].Target, QTYPE.A);
               }
               catch (Exception ex)
               {
                    throw new Exception("DNS sub A record query for " + domain + " failed.", ex);
               }
               DNS_rr_A[] aRecs = dsr2.GetARecords();

               response += (i + 1) + ".&nbsp;&nbsp;&nbsp;";
               response += records[i].Target + " on port " + records[i].Port + " with a TTL of " + records[i].TTL + " and a priority of " + records[i].Priority + "<br />";

               for (int x = 0; x < aRecs.Length; x++)
               {
                    response += "&nbsp;&nbsp;---->&nbsp;Resolves to " + aRecs[x].IP + "<br />";
                    response += CheckPort(aRecs[x].IP, records[i].Port);
                    response += CheckPort(aRecs[x].IP, 5223);
               }

          }
          return response;
    }
Exemplo n.º 10
0
    //-------------------------------------------------------------------------------------------
    private string LookupA(string recordname, string sectiontext)
    {
        Dns_Client          dc      = new LumiSoft.Net.Dns.Client.Dns_Client();
          DnsServerResponse   dsr     = null;
          try
          {
          dsr = dc.Query(recordname, LumiSoft.Net.Dns.Client.QTYPE.A);
          }
          catch (Exception ex)
          {
               throw new Exception("DNS A record query for " + recordname + " failed.", ex);
          }
          DNS_rr_A[]        records = dsr.GetARecords();

          string response = sectiontext;
          if (records.Length < 1)
               fail = true;
          for (int i = 0; i < records.Length; i++)
          {
               response += (i + 1) + ".&nbsp;&nbsp;&nbsp;";
               response += records[i].IP + " with a TTL of " + records[i].TTL + "<br>";
               response += CheckPort(records[i].IP.ToString(), 5222);
               response += CheckPort(records[i].IP.ToString(), 5223);
               response += CheckPort(records[i].IP.ToString(), 5269);
               response += CheckPort(records[i].IP.ToString(), 5270);
          }
          return response;
    }