]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Add InspIRCd::IsHost for checking the validity of hostnames.
authorPeter Powell <petpow@saberuk.com>
Fri, 29 Jun 2018 10:28:19 +0000 (11:28 +0100)
committerPeter Powell <petpow@saberuk.com>
Tue, 10 Jul 2018 20:14:56 +0000 (21:14 +0100)
include/inspircd.h
src/helperfuncs.cpp

index a7f19f483171123635956686847ece1f1b70363d..447fb844138edec56df5ed899d6e54f8cf1f4e06 100644 (file)
@@ -381,6 +381,12 @@ class CoreExport InspIRCd
        */
        static bool DefaultIsChannel(const std::string& channel);
 
+       /** Determines whether a hostname is valid according to RFC 5891 rules.
+        * @param host The hostname to validate.
+        * @return True if the hostname is valid; otherwise, false.
+        */
+       static bool IsHost(const std::string& host);
+
        /** Return true if str looks like a server ID
         * @param sid string to check against
         */
index 111e1363f4dda3e686077c4dd56207b94882df30..8ced78ae8ac720d880f14d34cdba42da5144ddc7 100644 (file)
@@ -267,6 +267,65 @@ bool InspIRCd::DefaultIsIdent(const std::string& n)
        return true;
 }
 
+bool InspIRCd::IsHost(const std::string& host)
+{
+       // Hostnames must be non-empty and shorter than the maximum hostname length.
+       if (host.empty() || host.length() > ServerInstance->Config->Limits.MaxHost)
+               return false;
+
+       unsigned int numdashes = 0;
+       unsigned int numdots = 0;
+       bool seendot = false;
+       const std::string::const_iterator hostend = host.end() - 1;
+       for (std::string::const_iterator iter = host.begin(); iter != host.end(); ++iter)
+       {
+               unsigned char chr = static_cast<unsigned char>(*iter);
+
+               // If the current character is a label separator.
+               if (chr == '.')
+               {
+                       numdots++;
+
+                       // Consecutive separators are not allowed and dashes can not exist at the start or end
+                       // of labels and separators must only exist between labels.
+                       if (seendot || numdashes || iter == host.begin() || iter == hostend)
+                               return false;
+
+                       seendot = true;
+                       continue;
+               }
+
+               // If this point is reached then the character is not a dot.
+               seendot = false;
+
+               // If the current character is a dash.
+               if (chr == '-')
+               {
+                       // Consecutive separators are not allowed and dashes can not exist at the start or end
+                       // of labels and separators must only exist between labels.
+                       if (seendot || numdashes >= 2 || iter == host.begin() || iter == hostend)
+                               return false;
+
+                       numdashes += 1;
+                       continue;
+               }
+
+               // If this point is reached then the character is not a dash.
+               numdashes = 0;
+
+               // Alphanumeric characters are allowed at any position.
+               if ((chr >= '0' && chr <= '9') || (chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z'))
+                       continue;
+
+               return false;
+       }
+
+       // Whilst simple hostnames (e.g. localhost) are valid we do not allow the server to use
+       // them to prevent issues with clients that differentiate between short client and server
+       // prefixes by checking whether the nickname contains a dot.
+       return numdots;
+}
+
 bool InspIRCd::IsSID(const std::string &str)
 {
        /* Returns true if the string given is exactly 3 characters long,