From c9ac06657ecf79c5c7bada112a6fc67f87fbcba1 Mon Sep 17 00:00:00 2001
From: "A. Wilcox" <AWilcox@Wilcox-Tech.com>
Date: Sat, 12 Oct 2019 02:04:08 -0500
Subject: hscript: Validate 'hostname' keys and add another test

---
 hscript/meta.cc | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

(limited to 'hscript')

diff --git a/hscript/meta.cc b/hscript/meta.cc
index 84e026e..5897c8b 100644
--- a/hscript/meta.cc
+++ b/hscript/meta.cc
@@ -31,7 +31,36 @@ Key *Hostname::parseFromData(const std::string data, int lineno, int *errors,
 
 bool Hostname::validate(ScriptOptions) const {
     /* Validate that the name is a valid machine or DNS name */
-    return false;
+    bool any_failure = false;
+    std::string::size_type last_dot = 0, next_dot = 0;
+
+    if(!isalpha(this->_value[0])) {
+        any_failure = true;
+        output_error("installfile:" + std::to_string(this->lineno()),
+                     "hostname: must start with alphabetical character");
+    }
+
+    if(this->_value.size() > 320) {
+        any_failure = true;
+        output_error("installfile:" + std::to_string(this->lineno()),
+                     "hostname: value too long",
+                     "valid host names must be less than 320 characters");
+    }
+
+    do {
+        next_dot = this->_value.find_first_of('.', next_dot + 1);
+        if(next_dot == std::string::npos) {
+            next_dot = this->_value.size();
+        }
+        if(next_dot - last_dot > 64) {
+            any_failure = true;
+            output_error("installfile:" + std::to_string(this->lineno()),
+                         "hostname: component too long",
+                         "each component must be less than 64 characters");
+        }
+    } while(next_dot != this->_value.size());
+
+    return any_failure;
 }
 
 bool Hostname::execute(ScriptOptions) const {
-- 
cgit v1.2.3-70-g09d2