Bläddra i källkod

modified config file; create and remove wildcard

develop
Robin Thoni 8 år sedan
förälder
incheckning
38edc5d526
2 ändrade filer med 29 tillägg och 11 borttagningar
  1. 12
    5
      config.example.json
  2. 17
    6
      localhostgen.py

+ 12
- 5
config.example.json Visa fil

6
     "username": "powerdns",
6
     "username": "powerdns",
7
     "password": "password"
7
     "password": "password"
8
   },
8
   },
9
-  "domain": "local.example.com",
10
-  "reverseDomain": "1.168.192.in-addr.arpa",
11
-  "dhcpdFilePath": "/etc/dhcp/dhcpd.conf",
12
-  "dhcpdReloadCommand": "systemctl restart isc-dhcp-server",
13
-  "recordTTL": 600
9
+
10
+  "dns": {
11
+    "domain": "local.example.com",
12
+    "reverseDomain": "1.168.192.in-addr.arpa",
13
+    "recordTTL": 600,
14
+    "wildcard": true
15
+  },
16
+
17
+  "dhcpd": {
18
+    "filePath": "/etc/dhcp/dhcpd.conf",
19
+    "reloadCommand": "systemctl restart isc-dhcp-server"
20
+  }
14
 }
21
 }

+ 17
- 6
localhostgen.py Visa fil

2
 
2
 
3
 import argparse
3
 import argparse
4
 import json
4
 import json
5
+import os
5
 from time import time
6
 from time import time
6
 import psycopg2
7
 import psycopg2
7
 import sys
8
 import sys
82
     def delete_host(self, record):
83
     def delete_host(self, record):
83
         cur = self.db_instance.cursor()
84
         cur = self.db_instance.cursor()
84
         cur.execute("DELETE FROM records WHERE name='%s' RETURNING id" % record)
85
         cur.execute("DELETE FROM records WHERE name='%s' RETURNING id" % record)
85
-        record_id = cur.fetchone()[0]
86
+        record_id = cur.fetchone()
86
         cur.close()
87
         cur.close()
87
         self.db_instance.commit()
88
         self.db_instance.commit()
88
-        return record_id
89
+        if record_id is not None:
90
+            return record_id[0]
91
+        return None
89
 
92
 
90
     def remove_host_dhcp(self, host, dhcpd_file_content):
93
     def remove_host_dhcp(self, host, dhcpd_file_content):
91
         return re.sub(" *host +%s +\{[^}]+} *\n" % host, "", dhcpd_file_content)
94
         return re.sub(" *host +%s +\{[^}]+} *\n" % host, "", dhcpd_file_content)
92
 
95
 
93
-    def create_host(self, domain, reverse_domain, host, ip, mac, ttl, dhcpd_file_path):
96
+    def create_host(self, domain, reverse_domain, host, ip, mac, ttl, dhcpd_file_path, wildcard):
94
         record_full = "%s.%s" % (host, domain)
97
         record_full = "%s.%s" % (host, domain)
98
+        host_wildcard = "*.%s" % host
95
         reverse_domain_record = self.get_reverse_domain_record(reverse_domain, ip)
99
         reverse_domain_record = self.get_reverse_domain_record(reverse_domain, ip)
96
 
100
 
97
         self.insert_host(domain, host, ip, ttl, 'A')
101
         self.insert_host(domain, host, ip, ttl, 'A')
102
+        if wildcard:
103
+            self.insert_host(domain, host_wildcard, record_full, ttl, 'CNAME')
98
         self.insert_host(reverse_domain, reverse_domain_record, record_full, ttl, 'PTR')
104
         self.insert_host(reverse_domain, reverse_domain_record, record_full, ttl, 'PTR')
99
 
105
 
100
         with open(dhcpd_file_path, "r") as f:
106
         with open(dhcpd_file_path, "r") as f:
114
 
120
 
115
     def remove_host(self, domain, reverse_domain, host, dhcpd_file_path):
121
     def remove_host(self, domain, reverse_domain, host, dhcpd_file_path):
116
         record_full = "%s.%s" % (host, domain)
122
         record_full = "%s.%s" % (host, domain)
123
+        record_full_wildcard = "*.%s" % record_full
117
         ip = self.get_record_content(record_full)
124
         ip = self.get_record_content(record_full)
118
         if ip is not None:
125
         if ip is not None:
119
             reverse_domain_record = self.get_reverse_domain_record(reverse_domain, ip)
126
             reverse_domain_record = self.get_reverse_domain_record(reverse_domain, ip)
120
             reverse_domain_record_full = "%s.%s" %(reverse_domain_record, reverse_domain)
127
             reverse_domain_record_full = "%s.%s" %(reverse_domain_record, reverse_domain)
128
+            self.delete_host(record_full_wildcard)
121
             self.delete_host(record_full)
129
             self.delete_host(record_full)
122
             self.delete_host(reverse_domain_record_full)
130
             self.delete_host(reverse_domain_record_full)
123
 
131
 
154
     host = args.host
162
     host = args.host
155
     ip = args.ip
163
     ip = args.ip
156
     mac = args.mac
164
     mac = args.mac
157
-    ttl = config['recordTTL']
158
 
165
 
159
     local_host_gen = LocalHostGen(config['database'])
166
     local_host_gen = LocalHostGen(config['database'])
160
     local_host_gen.connect()
167
     local_host_gen.connect()
163
         if ip is None or mac is None:
170
         if ip is None or mac is None:
164
             eprint("IP and MAC are required to create a host")
171
             eprint("IP and MAC are required to create a host")
165
             exit(1)
172
             exit(1)
166
-        local_host_gen.create_host(config['domain'], config['reverseDomain'], host, ip, mac, ttl, config['dhcpdFilePath'])
173
+        local_host_gen.create_host(config['dns']['domain'], config['dns']['reverseDomain'], host, ip, mac,
174
+                                   config['dns']['recordTTL'], config['dhcpd']['filePath'], config['dns']['wildcard'])
167
     elif args.remove:
175
     elif args.remove:
168
-        local_host_gen.remove_host(config['domain'], config['reverseDomain'], host, config['dhcpdFilePath'])
176
+        local_host_gen.remove_host(config['dns']['domain'], config['dns']['reverseDomain'], host,
177
+                                   config['dhcpd']['filePath'])
169
 
178
 
170
     local_host_gen.disconnect()
179
     local_host_gen.disconnect()
171
 
180
 
181
+    os.system(config['dhcpd']['reloadCommand'])
182
+
172
 
183
 
173
 main()
184
 main()

Laddar…
Avbryt
Spara