Parcourir la source

dhcp override

develop
Robin Thoni il y a 9 ans
Parent
révision
170f64b7bc

+ 29
- 2
src/Business/PdnsSlave.cpp Voir le fichier

@@ -7,6 +7,14 @@
7 7
 #include "DataAccess/HostsConfig.h"
8 8
 #include "PdnsSlave.h"
9 9
 
10
+bool replace(std::string& str, const std::string& from, const std::string& to) {
11
+    size_t start_pos = str.find(from);
12
+    if(start_pos == std::string::npos)
13
+        return false;
14
+    str.replace(start_pos, from.length(), to);
15
+    return true;
16
+}
17
+
10 18
 PdnsSlave::PdnsSlave(const std::string &filePath)
11 19
     : _filePath(filePath)
12 20
 {
@@ -50,7 +58,26 @@ Result<Actions> PdnsSlave::readHosts()
50 58
     if (!res)
51 59
         return res;
52 60
     _actions = res.getData();
53
-    for (auto action : _actions)
54
-        std::cout << action->getSqlQuery() << std::endl << std::endl;
55 61
     return res;
56 62
 }
63
+
64
+BResult PdnsSlave::overridePdns()
65
+{
66
+    return true;
67
+}
68
+
69
+BResult PdnsSlave::overrideDhcp()
70
+{
71
+    std::string hosts;
72
+    for (auto a : _actions)
73
+        hosts += a->getDhcpConf();
74
+
75
+    auto dhcp = _dhcpdTemplateContent;
76
+    replace(dhcp, "%%HOSTS%%", hosts);
77
+    std::ofstream file(_dhcpdFilePath);
78
+    if (!file)
79
+        return BResult().error("Could not open file");
80
+    file << dhcp;
81
+    file.close();
82
+    return true;
83
+}

+ 4
- 0
src/Business/PdnsSlave.h Voir le fichier

@@ -21,6 +21,10 @@ public:
21 21
 
22 22
     Result<Actions> readHosts();
23 23
 
24
+    BResult overridePdns();
25
+
26
+    BResult overrideDhcp();
27
+
24 28
 private:
25 29
     std::string _filePath;
26 30
 

+ 10
- 0
src/DBO/Actions/Action.cpp Voir le fichier

@@ -26,3 +26,13 @@ const std::string Action::getSqlQuery() const
26 26
 {
27 27
     return "SET @domain_id=(SELECT get_domain_id(\"" + _domain + "\"));\n" + getSql();
28 28
 }
29
+
30
+const std::string Action::getDhcpConf() const
31
+{
32
+    return getDhcp();
33
+}
34
+
35
+const std::string Action::getDhcp() const
36
+{
37
+    return "";
38
+}

+ 4
- 0
src/DBO/Actions/Action.h Voir le fichier

@@ -15,6 +15,8 @@ public:
15 15
     Action();
16 16
     virtual ~Action();
17 17
 
18
+    const std::string getDhcpConf() const;
19
+
18 20
     const std::string getSqlQuery() const;
19 21
 
20 22
     const std::string &getDomain() const;
@@ -24,6 +26,8 @@ public:
24 26
 protected:
25 27
     virtual const std::string getSql() const = 0;
26 28
 
29
+    virtual const std::string getDhcp() const;
30
+
27 31
     std::string _domain;
28 32
 };
29 33
 

+ 13
- 0
src/DBO/Actions/ActionAddHost.cpp Voir le fichier

@@ -122,3 +122,16 @@ const std::string ActionAddHost::getReversedValue() const
122 122
         reversedValue += std::string(splitValue[i]) + (i == (int) splitValue.size() - index ? "" : ".");
123 123
     return reversedValue;
124 124
 }
125
+
126
+const std::string ActionAddHost::getDhcp() const
127
+{
128
+    if (!_dhcpMac.empty() && _recordType == "A")
129
+    {
130
+        return "\thost " + _host + " {\n"
131
+                "\t\thardware ethernet " + _dhcpMac + ";\n"
132
+                "\t\tfixed-address " + _host + "." + _domain + ";\n"
133
+                "\t}\n";
134
+    }
135
+    else
136
+        return "";
137
+}

+ 2
- 0
src/DBO/Actions/ActionAddHost.h Voir le fichier

@@ -39,6 +39,8 @@ public:
39 39
 protected:
40 40
     const std::string getSql() const override;
41 41
 
42
+    const std::string getDhcp() const override;
43
+
42 44
 private:
43 45
     const std::string getReversedValue() const;
44 46
 

+ 23
- 0
src/Interface/MainClass.cpp Voir le fichier

@@ -19,10 +19,12 @@ int MainClass::main()
19 19
 {
20 20
     CommandLineOption configFile("config", 'c', "Path to the configuration file",
21 21
                                  "FILE", "/etc/pdns-slave/pdns-slave.json");
22
+    CommandLineOption check("check", 'C', "Only check configurations", "", "");
22 23
     CommandLineOption help("help", 'h', "Displays this help", "", "");
23 24
     CommandLineParser parser(_argc, _argv);
24 25
     parser.addOption(&help);
25 26
     parser.addOption(&configFile);
27
+    parser.addOption(&check);
26 28
     if (!parser.parse())
27 29
         return parser.showHelp(EX_USAGE);
28 30
     if (help.isSet())
@@ -62,5 +64,26 @@ int MainClass::main()
62 64
     }
63 65
     res2.print();
64 66
 
67
+    if (!check.isSet())
68
+    {
69
+        std::cout << "Overriding dns" << std::endl;
70
+        if (!(res = pdnsSlave.overridePdns()))
71
+        {
72
+            std::cerr << "Failed to override dns" << std::endl;
73
+            res.print();
74
+            return 4;
75
+        }
76
+        res.print();
77
+
78
+        std::cout << "Overriding dhcp" << std::endl;
79
+        if (!(res = pdnsSlave.overrideDhcp()))
80
+        {
81
+            std::cerr << "Failed to override dhcp" << std::endl;
82
+            res.print();
83
+            return 5;
84
+        }
85
+        res.print();
86
+    }
87
+
65 88
     return 0;
66 89
 }

Chargement…
Annuler
Enregistrer