1 | # npackage example https://svn.lliurex.net/pandora/n4d-ldap/trunk |
---|
2 | # jinja2 http://jinja.pocoo.org/docs/templates |
---|
3 | |
---|
4 | from jinja2 import Environment |
---|
5 | from jinja2.loaders import FileSystemLoader |
---|
6 | from jinja2 import Template |
---|
7 | import tempfile |
---|
8 | import shutil |
---|
9 | import os |
---|
10 | import subprocess |
---|
11 | |
---|
12 | class Dnsmasq: |
---|
13 | |
---|
14 | def __init__(self): |
---|
15 | #Load template file |
---|
16 | self.tpl_env = Environment(loader=FileSystemLoader('/usr/share/n4d/templates/dnsmasq')) |
---|
17 | self.dnsfile = "/var/lib/dnsmasq/hosts/reg" # Format file =====> HOSTIP {HOST_PREFIX}NUMBER_PC_REGISTERED{INTERNAL_DOMAIN} |
---|
18 | self.pathfile = "/var/lib/dnsmasq/macs/all-hosts" # Format file ======> dhcp-host=MAC_PC,HOSTIP,{HOST_PREFIX}NUMBER_PC_REGISTERED{INTERNAL_DOMAIN} |
---|
19 | self.locktocken = "/tmp/lockmacregister" |
---|
20 | self.leases = "/var/lib/misc/dnsmasq.leases" |
---|
21 | self.backup_files=["/etc/dnsmasq.conf"] |
---|
22 | self.backup_dirs=["/var/lib/dnsmasq/","/etc/dnsmasq.d/"] |
---|
23 | |
---|
24 | #def init |
---|
25 | |
---|
26 | def startup(self,options): |
---|
27 | # executed when launching n4d |
---|
28 | pass |
---|
29 | |
---|
30 | #def startup |
---|
31 | |
---|
32 | def apt(self): |
---|
33 | # executed after apt operations |
---|
34 | pass |
---|
35 | |
---|
36 | #def apt |
---|
37 | |
---|
38 | # service test and backup functions # |
---|
39 | |
---|
40 | def test(self): |
---|
41 | |
---|
42 | pass |
---|
43 | |
---|
44 | #def test |
---|
45 | def makedir(self,dir=None): |
---|
46 | |
---|
47 | if not os.path.isdir(dir): |
---|
48 | os.makedirs(dir) |
---|
49 | |
---|
50 | return [True] |
---|
51 | |
---|
52 | #def get_time |
---|
53 | def get_time(self): |
---|
54 | |
---|
55 | return get_backup_name("Dnsmasq") |
---|
56 | |
---|
57 | #def backup |
---|
58 | def backup(self,dir="/backup"): |
---|
59 | |
---|
60 | try: |
---|
61 | self.makedir(dir) |
---|
62 | file_path=dir+"/"+self.get_time() |
---|
63 | tar=tarfile.open(file_path,"w:gz") |
---|
64 | |
---|
65 | for f in self.backup_files: |
---|
66 | if os.path.exists(f): |
---|
67 | tar.add(f) |
---|
68 | |
---|
69 | for d in self.backup_dirs: |
---|
70 | if os.path.exists(d): |
---|
71 | tar.add(d) |
---|
72 | tar.close() |
---|
73 | |
---|
74 | return [True,file_path] |
---|
75 | |
---|
76 | except Exception as e: |
---|
77 | return [False,str(e)] |
---|
78 | |
---|
79 | |
---|
80 | #def restore |
---|
81 | def restore(self,file_path=None): |
---|
82 | |
---|
83 | try: |
---|
84 | if file_path==None: |
---|
85 | dir="/backup" |
---|
86 | for f in sorted(os.listdir(dir),reverse=True): |
---|
87 | if "Dnsmasq" in f: |
---|
88 | file_path=dir+"/"+f |
---|
89 | break |
---|
90 | |
---|
91 | if os.path.exists(file_path): |
---|
92 | tmp_dir=tempfile.mkdtemp() |
---|
93 | tar=tarfile.open(file_path) |
---|
94 | tar.extractall(tmp_dir) |
---|
95 | tar.close() |
---|
96 | |
---|
97 | for f in self.backup_files: |
---|
98 | tmp_path=tmp_dir+f |
---|
99 | if os.path.exists(tmp_path): |
---|
100 | shutil.copy(tmp_path,f) |
---|
101 | |
---|
102 | for d in self.backup_dirs: |
---|
103 | tmp_path=tmp_dir+d |
---|
104 | if os.path.exists(tmp_path): |
---|
105 | self.makedir(d) |
---|
106 | cmd="cp -r " + tmp_path +"/* " + d |
---|
107 | os.system(cmd) |
---|
108 | |
---|
109 | os.system("service dnsmasq restart") |
---|
110 | |
---|
111 | return [True,""] |
---|
112 | |
---|
113 | else: |
---|
114 | return [False,"Backup file not found"] |
---|
115 | |
---|
116 | except Exception as e: |
---|
117 | |
---|
118 | return [False,str(e)] |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | def has_name(self,mac): |
---|
123 | pass |
---|
124 | #def has_name |
---|
125 | |
---|
126 | def set_dns_external(self,dnsexternal): |
---|
127 | list_variables = {} |
---|
128 | #Get template |
---|
129 | template_extradns = self.tpl_env.get_template("extra-dns") |
---|
130 | #Inicialize DNS_EXTERNAL |
---|
131 | list_variables['DNS_EXTERNAL'] = objects['VariablesManager'].get_variable('DNS_EXTERNAL') |
---|
132 | #If variable DNS_EXTERNAL is not defined calculate it with args values |
---|
133 | status,list_variables['DNS_EXTERNAL'] = objects['VariablesManager'].init_variable('DNS_EXTERNAL',{'DNS':dnsexternal}) |
---|
134 | #Encode vars to UTF-8 |
---|
135 | string_template = template_extradns.render(list_variables).encode('UTF-8') |
---|
136 | #Open template file |
---|
137 | fd, tmpfilepath = tempfile.mkstemp() |
---|
138 | new_export_file = open(tmpfilepath,'w') |
---|
139 | new_export_file.write(string_template) |
---|
140 | new_export_file.close() |
---|
141 | os.close(fd) |
---|
142 | #Write template values |
---|
143 | #shutil.move(tmpfilepath,'/etc/dnsmasq.conf') |
---|
144 | n4d_mv(tmpfilepath,'/var/lib/dnsmasq/config/extra-dns',True,'root','root','0644',False ) |
---|
145 | return {'status':True,'msg':'Set dns external succesfully'} |
---|
146 | #def set_dns_external |
---|
147 | |
---|
148 | def configure_service(self,domain): |
---|
149 | list_variables = {} |
---|
150 | #status,list_variables['SRV_NAME'] = objects['VariablesManager'].init_variable('SRV_NAME',{'NAME':srv_name}) |
---|
151 | status,list_variables['INTERNAL_DOMAIN'] = objects['VariablesManager'].init_variable('INTERNAL_DOMAIN',{'DOMAIN':domain}) |
---|
152 | #self.set_srv_name(srv_name) |
---|
153 | self.set_internal_domain(domain) |
---|
154 | self.load_exports() |
---|
155 | return {'status':True,'msg':'SUCCESS'} |
---|
156 | #def config_service |
---|
157 | |
---|
158 | def add_alias(self,alias): |
---|
159 | template_cname = self.tpl_env.get_template("cname-server") |
---|
160 | list_variables = {} |
---|
161 | #get INTERNAL_DOMAIN |
---|
162 | list_variables['INTERNAL_DOMAIN'] = objects['VariablesManager'].get_variable('INTERNAL_DOMAIN') |
---|
163 | #If INT_DOMAIN is not defined return an error |
---|
164 | if list_variables['INTERNAL_DOMAIN'] == None: |
---|
165 | return {'status':False,'msg':'Variable INTERNAL_DOMAIN not defined'} |
---|
166 | #get HOSTNAME |
---|
167 | list_variables['HOSTNAME'] = objects['VariablesManager'].get_variable('HOSTNAME') |
---|
168 | #If INT_DOMAIN is not defined return an error |
---|
169 | if list_variables['HOSTNAME'] == None: |
---|
170 | return {'status':False,'msg':'Variable HOSTNAME not defined'} |
---|
171 | |
---|
172 | #Add alias to SRV_ALIAS |
---|
173 | #Obtains actual SRV_ALIAS variable |
---|
174 | list_variables['SRV_ALIAS'] = objects['VariablesManager'].get_variable('SRV_ALIAS') |
---|
175 | #Add new alias |
---|
176 | if list_variables['SRV_ALIAS'] ==None: |
---|
177 | list_variables['SRV_ALIAS']=[] |
---|
178 | list_variables['SRV_ALIAS'].append(alias) |
---|
179 | #Save new values |
---|
180 | status,list_variables['SRV_ALIAS'] = objects['VariablesManager'].init_variable('SRV_ALIAS',{'ALIAS':list_variables['SRV_ALIAS']}) |
---|
181 | #return {'status':True,'msg':'Set server name succesfully'} |
---|
182 | |
---|
183 | #Encode vars to UTF-8 |
---|
184 | string_template = template_cname.render(list_variables).encode('UTF-8') |
---|
185 | #Open template file |
---|
186 | fd, tmpfilepath = tempfile.mkstemp() |
---|
187 | new_export_file = open(tmpfilepath,'w') |
---|
188 | new_export_file.write(string_template) |
---|
189 | new_export_file.close() |
---|
190 | os.close(fd) |
---|
191 | #Write template values |
---|
192 | #shutil.move(tmpfilepath,'/etc/dnsmasq.conf') |
---|
193 | n4d_mv(tmpfilepath,'/var/lib/dnsmasq/config/cname-server',True,'root','root','0644',False ) |
---|
194 | return {'status':True,'msg':'SRV_ALIAS changed'} |
---|
195 | self.reboot_dhcpd() |
---|
196 | #def add_alias |
---|
197 | |
---|
198 | def remove_alias(self,alias): |
---|
199 | template_cname = self.tpl_env.get_template("cname-server") |
---|
200 | list_variables = {} |
---|
201 | #get INTERNAL_DOMAIN |
---|
202 | list_variables['INTERNAL_DOMAIN'] = objects['VariablesManager'].get_variable('INTERNAL_DOMAIN') |
---|
203 | #If INT_DOMAIN is not defined return an error |
---|
204 | if list_variables['INTERNAL_DOMAIN'] == None: |
---|
205 | return {'status':False,'msg':'Variable INTERNAL_DOMAIN not defined'} |
---|
206 | #get HOSTNAME |
---|
207 | list_variables['HOSTNAME'] = objects['VariablesManager'].get_variable('HOSTNAME') |
---|
208 | #If INT_DOMAIN is not defined return an error |
---|
209 | if list_variables['HOSTNAME'] == None: |
---|
210 | return {'status':False,'msg':'Variable HOSTNAME not defined'} |
---|
211 | |
---|
212 | #Add alias to SRV_ALIAS |
---|
213 | #Obtains actual SRV_ALIAS variable |
---|
214 | list_variables['SRV_ALIAS'] = objects['VariablesManager'].get_variable('SRV_ALIAS') |
---|
215 | #Add new alias |
---|
216 | if list_variables['SRV_ALIAS'] == None: |
---|
217 | return {'status':True,'msg':'SRV_ALIAS is empty'} |
---|
218 | if alias in list_variables['SRV_ALIAS']: |
---|
219 | list_variables['SRV_ALIAS'].remove(alias) |
---|
220 | else: |
---|
221 | return {'status':False,'msg': 'alias not found'} |
---|
222 | #Save new values |
---|
223 | status,list_variables['SRV_ALIAS'] = objects['VariablesManager'].init_variable('SRV_ALIAS',{'ALIAS':list_variables['SRV_ALIAS']}) |
---|
224 | #return {'status':True,'msg':'Set server name succesfully'} |
---|
225 | |
---|
226 | #Encode vars to UTF-8 |
---|
227 | string_template = template_cname.render(list_variables).encode('UTF-8') |
---|
228 | #Open template file |
---|
229 | fd, tmpfilepath = tempfile.mkstemp() |
---|
230 | new_export_file = open(tmpfilepath,'w') |
---|
231 | new_export_file.write(string_template) |
---|
232 | new_export_file.close() |
---|
233 | os.close(fd) |
---|
234 | #Write template values |
---|
235 | #shutil.move(tmpfilepath,'/etc/dnsmasq.conf') |
---|
236 | n4d_mv(tmpfilepath,'/var/lib/dnsmasq/config/cname-server',True,'root','root','0644',False ) |
---|
237 | return {'status':True,'msg':'SRV_ALIAS changed'} |
---|
238 | self.reboot_dhcpd() |
---|
239 | #def config_service |
---|
240 | |
---|
241 | ''' |
---|
242 | def set_srv_name(self,name): |
---|
243 | template_cname = self.tpl_env.get_template("cname-server") |
---|
244 | list_variables = {} |
---|
245 | #get INTERNAL_DOMAIN |
---|
246 | list_variables['INTERNAL_DOMAIN'] = objects['VariablesManager'].get_variable('INTERNAL_DOMAIN') |
---|
247 | #If INT_DOMAIN is not defined return an error |
---|
248 | if list_variables['INTERNAL_DOMAIN'] == None: |
---|
249 | return {'status':False,'msg':'Variable INTERNAL_DOMAIN not defined'} |
---|
250 | |
---|
251 | #Inicialize SRV_NAME |
---|
252 | status,list_variables['SRV_NAME'] = objects['VariablesManager'].init_variable('SRV_NAME',{'NAME':name}) |
---|
253 | #return {'status':True,'msg':'Set server name succesfully'} |
---|
254 | |
---|
255 | #Encode vars to UTF-8 |
---|
256 | string_template = template_cname.render(list_variables).encode('UTF-8') |
---|
257 | #Open template file |
---|
258 | fd, tmpfilepath = tempfile.mkstemp() |
---|
259 | new_export_file = open(tmpfilepath,'w') |
---|
260 | new_export_file.write(string_template) |
---|
261 | new_export_file.close() |
---|
262 | os.close(fd) |
---|
263 | #Write template values |
---|
264 | #shutil.move(tmpfilepath,'/etc/dnsmasq.conf') |
---|
265 | n4d_mv(tmpfilepath,'/var/lib/dnsmasq/config/cname-server',True,'root','root','0644',False ) |
---|
266 | return {'status':True,'msg':'SRV_NAME changed'} |
---|
267 | ''' |
---|
268 | |
---|
269 | def set_internal_domain(self,domain): |
---|
270 | list_variables = {} |
---|
271 | #Get HOSTNAME |
---|
272 | list_variables['HOSTNAME'] = objects['VariablesManager'].get_variable('HOSTNAME') |
---|
273 | #If variable INTERNAL_DOMAIN is not defined calculate it with args values |
---|
274 | if list_variables['HOSTNAME'] == None: |
---|
275 | return {'status':False,'msg':'Variable HOSTNAME is not defined'} |
---|
276 | #Set INTERNAL_DOMAIN with args values |
---|
277 | status,list_variables['INTERNAL_DOMAIN'] = objects['VariablesManager'].init_variable('INTERNAL_DOMAIN',{'DOMAIN':domain}) |
---|
278 | return {'status':True,'msg':'Set internal domain succesfully'} |
---|
279 | |
---|
280 | def load_exports(self): |
---|
281 | #Get template |
---|
282 | template = self.tpl_env.get_template("dnsmasq.conf") |
---|
283 | template_cname = self.tpl_env.get_template("cname-server") |
---|
284 | template_server = self.tpl_env.get_template("server") |
---|
285 | template_dhclientconf = self.tpl_env.get_template("dhclient.conf") |
---|
286 | |
---|
287 | list_variables = {} |
---|
288 | |
---|
289 | ########################### |
---|
290 | #Getting VARS |
---|
291 | ########################### |
---|
292 | |
---|
293 | #Obtains INTERNAL_INTERFACE |
---|
294 | list_variables['INTERNAL_INTERFACE'] = objects['VariablesManager'].get_variable('INTERNAL_INTERFACE') |
---|
295 | #If INTERNAL_INTERFACE is not defined returns an error |
---|
296 | if list_variables['INTERNAL_INTERFACE'] == None: |
---|
297 | return {'status':False,'msg':'Variable INTERNAL_INTERFACE not defined'} |
---|
298 | |
---|
299 | #Obtains INTERNAL_NETWORK |
---|
300 | list_variables['INTERNAL_NETWORK'] = objects['VariablesManager'].get_variable('INTERNAL_NETWORK') |
---|
301 | #If INTERNAL_NETWORK is not defined returns an error |
---|
302 | if list_variables['INTERNAL_NETWORK'] == None: |
---|
303 | return {'status':False,'msg':'Variable INTERNAL_NETWORK not defined'} |
---|
304 | |
---|
305 | #Obtains INTERNAL_MASK |
---|
306 | list_variables['INTERNAL_MASK'] = objects['VariablesManager'].get_variable('INTERNAL_MASK') |
---|
307 | #If INTERNAL_NETWORK is not defined returns an error |
---|
308 | if list_variables['INTERNAL_MASK'] == None: |
---|
309 | return {'status':False,'msg':'Variable INTERNAL_MASK not defined'} |
---|
310 | |
---|
311 | #Inicialize INTERNAL_DOMAIN |
---|
312 | list_variables['INTERNAL_DOMAIN'] = objects['VariablesManager'].get_variable('INTERNAL_DOMAIN') |
---|
313 | #If INT_DOMAIN is not defined calculate it with args values |
---|
314 | if list_variables['INTERNAL_DOMAIN'] == None: |
---|
315 | return {'status':False,'msg':'Variable INTERNAL_DOMAIN not defined'} |
---|
316 | |
---|
317 | #Obtains SRV_IP |
---|
318 | list_variables['SRV_IP'] = objects['VariablesManager'].get_variable('SRV_IP') |
---|
319 | #If variable SRV_IP is not defined returns an error |
---|
320 | if list_variables['SRV_IP'] == None: |
---|
321 | return {'status':False,'msg':'Variable SRV_IP not defined'} |
---|
322 | ''' |
---|
323 | #Obtains SRV_NAME |
---|
324 | list_variables['SRV_NAME'] = objects['VariablesManager'].get_variable('SRV_NAME') |
---|
325 | #If variable SRV_IP is not defined returns an error |
---|
326 | if list_variables['SRV_NAME'] == None: |
---|
327 | return {'status':False,'msg':'Variable SRV_NAME not defined'} |
---|
328 | ''' |
---|
329 | #Obtains HOSTNAME |
---|
330 | list_variables['HOSTNAME'] = objects['VariablesManager'].get_variable('HOSTNAME') |
---|
331 | #If variable SRV_IP is not defined returns an error |
---|
332 | if list_variables['HOSTNAME'] == None: |
---|
333 | return {'status':False,'msg':'Variable HOSTNAME not defined'} |
---|
334 | |
---|
335 | ############################ |
---|
336 | #Setting VARS |
---|
337 | ############################ |
---|
338 | |
---|
339 | #Inicialize DHCP_ENABLE |
---|
340 | #list_variables['DHCP_ENABLE'] = objects['VariablesManager'].get_variable('DHCP_ENABLE') |
---|
341 | #If variable DHCP_ENABLE is not defined calculate it with args values |
---|
342 | #if list_variables['DHCP_ENABLE'] == None: |
---|
343 | status,list_variables['DHCP_ENABLE'] = objects['VariablesManager'].init_variable('DHCP_ENABLE',{'ENABLE':'True'}) |
---|
344 | |
---|
345 | #Inicialize DHCP_LEASE_TIME |
---|
346 | #list_variables['DHCP_LEASE_TIME'] = objects['VariablesManager'].get_variable('DHCP_LEASE_TIME') |
---|
347 | #If variable DHCP_LEASE_TIME is not defined calculate it with args values |
---|
348 | #if list_variables['DHCP_LEASE_TIME'] == None: |
---|
349 | status,list_variables['DHCP_LEASE_TIME'] = objects['VariablesManager'].init_variable('DHCP_LEASE_TIME',{'LEASE_TIME':12}) |
---|
350 | |
---|
351 | #Inicialize DHCP_DENY_UNKNOWN_CLIENTS |
---|
352 | #list_variables['DHCP_DENY_UNKNOWN_CLIENTS'] = objects['VariablesManager'].get_variable('DHCP_DENY_UNKNOWN_CLIENTS') |
---|
353 | #If variable DHCP_DENY_UNKNOWN_CLIENTS is not defined calculate it with args values |
---|
354 | #if list_variables['DHCP_DENY_UNKNOWN_CLIENTS'] == None: |
---|
355 | status,list_variables['DHCP_DENY_UNKNOWN_CLIENTS'] = objects['VariablesManager'].init_variable('DHCP_DENY_UNKNOWN_CLIENTS',{'DENY_UNKNOWN':'no'}) |
---|
356 | |
---|
357 | #Inicialize DHCP_HOST_MAX |
---|
358 | #list_variables['DHCP_HOST_MAX'] = objects['VariablesManager'].get_variable('DHCP_HOST_MAX') |
---|
359 | #If variable DHCP_HOST_MAX is not defined calculate it with args values |
---|
360 | #if list_variables['DHCP_HOST_MAX'] == None: |
---|
361 | status,list_variables['DHCP_HOST_MAX'] = objects['VariablesManager'].init_variable('DHCP_HOST_MAX',{'HOST_MAX':80}) |
---|
362 | |
---|
363 | #Inicialize DHCP_FIRST_IP |
---|
364 | #list_variables['DHCP_FIRST_IP'] = objects['VariablesManager'].get_variable('DHCP_FIRST_IP') |
---|
365 | #If variable DHCP_FIRST_IP is not defined calculate it with args values |
---|
366 | #if list_variables['DHCP_FIRST_IP'] == None: |
---|
367 | status,list_variables['DHCP_FIRST_IP'] = objects['VariablesManager'].init_variable('DHCP_FIRST_IP',{'NETWORK':list_variables['INTERNAL_NETWORK'],'MASK':list_variables['INTERNAL_MASK']}) |
---|
368 | |
---|
369 | #Inicialize DHCP_LAST_IP |
---|
370 | #list_variables['DHCP_LAST_IP'] = objects['VariablesManager'].get_variable('DHCP_LAST_IP') |
---|
371 | #If variable DHCP_LAST_IP is not defined calculate it with args values |
---|
372 | #if list_variables['DHCP_LAST_IP'] == None: |
---|
373 | status,list_variables['DHCP_LAST_IP'] = objects['VariablesManager'].init_variable('DHCP_LAST_IP',{'NETWORK':list_variables['INTERNAL_NETWORK'],'MASK':list_variables['INTERNAL_MASK']}) |
---|
374 | |
---|
375 | #Inicialize DNS_HOSTNAME_PREFIX |
---|
376 | #list_variables['DNS_HOSTNAME_PREFIX'] = objects['VariablesManager'].get_variable('DNS_HOSTNAME_PREFIX') |
---|
377 | #If variable DNS_HOSTNAME_PREFIX is not defined calculate it with args values |
---|
378 | #if list_variables['DNS_HOSTNAME_PREFIX'] == None: |
---|
379 | status,list_variables['DNS_HOSTNAME_PREFIX'] = objects['VariablesManager'].init_variable('DNS_HOSTNAME_PREFIX',{'PREFIX':'llx-'}) |
---|
380 | |
---|
381 | #Inicialize DNS_UNREG_HOSTNAME_PREFIX |
---|
382 | #list_variables['DNS_UNREG_HOSTNAME_PREFIX'] = objects['VariablesManager'].get_variable('DNS_UNREG_HOSTNAME_PREFIX') |
---|
383 | #If variable DNS_UNREG_HOSTNAME_PREFIX is not defined calculate it with args values |
---|
384 | #if list_variables['DNS_UNREG_HOSTNAME_PREFIX'] == None: |
---|
385 | status,list_variables['DNS_UNREG_HOSTNAME_PREFIX'] = objects['VariablesManager'].init_variable('DNS_UNREG_HOSTNAME_PREFIX',{'PREFIX':'host-'}) |
---|
386 | |
---|
387 | #Inicialize SRV_ALIAS |
---|
388 | #list_variables['SRV_ALIAS'] = objects['VariablesManager'].get_variable('SRV_ALIAS') |
---|
389 | #If variable SRV_ALIAS is not defined calculate it with args values |
---|
390 | #if list_variables['SRV_ALIAS'] == None: |
---|
391 | status,list_variables['SRV_ALIAS'] = objects['VariablesManager'].init_variable('SRV_ALIAS') |
---|
392 | |
---|
393 | |
---|
394 | #Encode vars to UTF-8 |
---|
395 | string_template = template.render(list_variables).encode('UTF-8') |
---|
396 | #Open template file |
---|
397 | fd, tmpfilepath = tempfile.mkstemp() |
---|
398 | new_export_file = open(tmpfilepath,'w') |
---|
399 | new_export_file.write(string_template) |
---|
400 | new_export_file.close() |
---|
401 | os.close(fd) |
---|
402 | #Write template values |
---|
403 | #shutil.move(tmpfilepath,'/etc/dnsmasq.conf') |
---|
404 | n4d_mv(tmpfilepath,'/etc/dnsmasq.conf',True,'root','root','0644',False ) |
---|
405 | |
---|
406 | #Encode vars to UTF-8 |
---|
407 | string_template = template_cname.render(list_variables).encode('UTF-8') |
---|
408 | #Open template file |
---|
409 | fd, tmpfilepath = tempfile.mkstemp() |
---|
410 | new_export_file = open(tmpfilepath,'w') |
---|
411 | new_export_file.write(string_template) |
---|
412 | new_export_file.close() |
---|
413 | os.close(fd) |
---|
414 | #Write template values |
---|
415 | n4d_mv(tmpfilepath,'/var/lib/dnsmasq/config/cname-server',True,'root','root','0644',False ) |
---|
416 | |
---|
417 | #Encode vars to UTF-8 |
---|
418 | string_template = template_server.render(list_variables).encode('UTF-8') |
---|
419 | #Open template file |
---|
420 | fd, tmpfilepath = tempfile.mkstemp() |
---|
421 | new_export_file = open(tmpfilepath,'w') |
---|
422 | new_export_file.write(string_template) |
---|
423 | new_export_file.close() |
---|
424 | os.close(fd) |
---|
425 | #Write template values |
---|
426 | n4d_mv(tmpfilepath,'/var/lib/dnsmasq/hosts/server',True,'root','root','0644',False ) |
---|
427 | |
---|
428 | #Encode vars to UTF-8 |
---|
429 | string_template = template_dhclientconf.render(list_variables).encode('UTF-8') |
---|
430 | #Open template file |
---|
431 | fd, tmpfilepath = tempfile.mkstemp() |
---|
432 | new_export_file = open(tmpfilepath,'w') |
---|
433 | new_export_file.write(string_template) |
---|
434 | new_export_file.close() |
---|
435 | os.close(fd) |
---|
436 | #Write template values |
---|
437 | n4d_mv(tmpfilepath,'/etc/dhcp/dhclient.conf',True,'root','root','0644',False ) |
---|
438 | |
---|
439 | return {'status':True,'msg':'Service configured'} |
---|
440 | #def load_exports |
---|
441 | |
---|
442 | def reboot_dhcpd(self): |
---|
443 | #Restart dhcpd service |
---|
444 | subprocess.Popen(['service','dnsmasq','restart'],stdout=subprocess.PIPE).communicate() |
---|
445 | return {'status':True,'msg':'DNSMASQ rebooted'} |
---|
446 | #def reboot_dhcpd |
---|
447 | |
---|
448 | |
---|
449 | def get_available_id_list(self): |
---|
450 | new_hostname_prefix = objects['VariablesManager'].get_variable('DNS_HOSTNAME_PREFIX') |
---|
451 | new_hostname_sufix = objects['VariablesManager'].get_variable('INTERNAL_DOMAIN') |
---|
452 | lavailable = range(1,self._subtraction_ip(objects['VariablesManager'].get_variable('DHCP_FIRST_IP'),objects['VariablesManager'].get_variable('DHCP_LAST_IP'))) |
---|
453 | default = self._get_first_id_available(new_hostname_prefix,new_hostname_sufix) |
---|
454 | if int(default) > int(lavailable[-1]): |
---|
455 | default = lavailable[-1] |
---|
456 | return {'status':True,'result':lavailable,'default':default} |
---|
457 | #def get_available_id_list |
---|
458 | |
---|
459 | def remove_register(self,mac): |
---|
460 | if not os.path.exists(self.locktocken): |
---|
461 | open(self.locktocken,'a') |
---|
462 | else: |
---|
463 | return {'status':False,'msg':'Server is locked now' } |
---|
464 | registerfile = open(self.pathfile,'r') |
---|
465 | content = registerfile.readlines() |
---|
466 | registerfile.close() |
---|
467 | found = False |
---|
468 | found_str = "" |
---|
469 | new_content = [] |
---|
470 | #dhcp-host=00:0F:FE:C3:D9:EC,10.0.2.1,llx-pc01 |
---|
471 | for line in content: |
---|
472 | try: |
---|
473 | auxline = line.strip('\n') |
---|
474 | auxline = auxline.split('=')[1] |
---|
475 | lmac,lip,lhostname = auxline.split(',') |
---|
476 | except: |
---|
477 | continue |
---|
478 | if lmac == mac : |
---|
479 | found = True |
---|
480 | found_str = lip + " " + lhostname |
---|
481 | else: |
---|
482 | new_content.append(line) |
---|
483 | if not found : |
---|
484 | os.remove(self.locktocken) |
---|
485 | return {'status':False,'result':'MAC ' + mac + ' is not registred' } |
---|
486 | else: |
---|
487 | #mac founded |
---|
488 | registerfile = open(self.pathfile,'w') |
---|
489 | for line in new_content: |
---|
490 | registerfile.write(line) |
---|
491 | registerfile.close() |
---|
492 | |
---|
493 | new_content = [] |
---|
494 | dnsfile = open(self.dnsfile,'r') |
---|
495 | content = dnsfile.readlines() |
---|
496 | dnsfile.close() |
---|
497 | for line in content: |
---|
498 | line_striped = line.strip() |
---|
499 | if (line_striped != found_str): |
---|
500 | new_content.append(line) |
---|
501 | |
---|
502 | dnsfile = open(self.dnsfile,'w') |
---|
503 | for line in new_content: |
---|
504 | dnsfile.write(line) |
---|
505 | dnsfile.close() |
---|
506 | |
---|
507 | os.remove(self.locktocken) |
---|
508 | return {'status':True,'result':'MAC '+mac+' has been removed' } |
---|
509 | #def register_machine |
---|
510 | |
---|
511 | def register_machine(self,id,mac,isteacher): |
---|
512 | #Check if this process is running |
---|
513 | if not os.path.exists(self.locktocken): |
---|
514 | open(self.locktocken,'a') |
---|
515 | else: |
---|
516 | return {'status':False,'result':'Server is locked now' } |
---|
517 | |
---|
518 | new_hostname_prefix = objects['VariablesManager'].get_variable('DNS_HOSTNAME_PREFIX') |
---|
519 | new_hostname_sufix = objects['VariablesManager'].get_variable('INTERNAL_DOMAIN') |
---|
520 | if int(id) < 10 : |
---|
521 | new_hostname = new_hostname_prefix + "0" + id + new_hostname_sufix |
---|
522 | else: |
---|
523 | new_hostname = new_hostname_prefix + id + new_hostname_sufix |
---|
524 | ip_base = objects['VariablesManager'].get_variable('INTERNAL_NETWORK') |
---|
525 | new_ip = self._increment_ip(ip_base,id) |
---|
526 | if new_ip == None: |
---|
527 | os.remove(self.locktocken) |
---|
528 | return {'status':False,'result':'Not found ip for ' + new_hostname} |
---|
529 | # dhcp-host=MAC_PC,HOSTIP,{HOST_PREFIX}NUMBER_PC_REGISTERED{INTERNAL_DOMAIN} |
---|
530 | new_content = [] |
---|
531 | registerfile = open(self.pathfile,'r') |
---|
532 | content = registerfile.readlines() |
---|
533 | for line in content: |
---|
534 | try: |
---|
535 | auxline = line.split('=')[1] |
---|
536 | lmac,lip,lhostname = auxline.split(',') |
---|
537 | lhostname = lhostname.strip() |
---|
538 | except: |
---|
539 | continue |
---|
540 | if new_hostname != lhostname and lmac != mac: |
---|
541 | new_content.append(line) |
---|
542 | registerfile.close() |
---|
543 | |
---|
544 | new_content.append("dhcp-host=" + mac + "," + new_ip + ","+ new_hostname +"\n") |
---|
545 | registerfile = open(self.pathfile,'w') |
---|
546 | for line in new_content: |
---|
547 | registerfile.write(line) |
---|
548 | registerfile.close() |
---|
549 | |
---|
550 | # HOSTIP {HOST_PREFIX}NUMBER_PC_REGISTERED{INTERNAL_DOMAIN} |
---|
551 | new_content = [] |
---|
552 | dnsfile = open(self.dnsfile,'r') |
---|
553 | content = dnsfile.readlines() |
---|
554 | for line in content: |
---|
555 | try: |
---|
556 | lip,lhostname = line.split(' ') |
---|
557 | lhostname = lhostname.strip() |
---|
558 | except: |
---|
559 | continue |
---|
560 | if new_hostname != lhostname and lip != new_ip: |
---|
561 | new_content.append(line) |
---|
562 | dnsfile.close() |
---|
563 | |
---|
564 | new_content.append(new_ip + " " + new_hostname + "\n") |
---|
565 | dnsfile = open(self.dnsfile,'w') |
---|
566 | for line in new_content: |
---|
567 | dnsfile.write(line) |
---|
568 | dnsfile.close() |
---|
569 | |
---|
570 | os.remove(self.locktocken) |
---|
571 | return {'status':True,'result':'MAC '+ mac + ' has been registered with id ' + id } |
---|
572 | #def register_machine |
---|
573 | |
---|
574 | ''' |
---|
575 | Internal method |
---|
576 | ''' |
---|
577 | |
---|
578 | def _subtraction_ip(self,a,b): |
---|
579 | list_a = a.split('.') |
---|
580 | list_a.reverse() |
---|
581 | list_b = b.split('.') |
---|
582 | list_b.reverse() |
---|
583 | total = 0 |
---|
584 | for i in range(len(list_a)): |
---|
585 | if i > 0 : |
---|
586 | total += (int(list_b[i]) - int(list_a[i])) * (256 ** i) |
---|
587 | else: |
---|
588 | total += int(list_b[i]) - int(list_a[i]) + 1 |
---|
589 | return total |
---|
590 | #def _subtraction_ip |
---|
591 | |
---|
592 | def _get_ip_by_mac(self, mac): |
---|
593 | f = open(self.leases,'r') |
---|
594 | all_lines = f.readlines() |
---|
595 | ip = "" |
---|
596 | for line in all_lines: |
---|
597 | try: |
---|
598 | list_params = line.split(' ') |
---|
599 | if (list_params[1] == mac ): |
---|
600 | ip = list_params[2] |
---|
601 | break |
---|
602 | except Exception as e: |
---|
603 | pass |
---|
604 | f.close() |
---|
605 | if (ip == ""): |
---|
606 | return None |
---|
607 | else: |
---|
608 | return ip |
---|
609 | #def _get_ip_by_mac |
---|
610 | |
---|
611 | def _get_first_id_available(self,head,foot): |
---|
612 | f = open(self.dnsfile,'r') |
---|
613 | lines = f.readlines() |
---|
614 | final = [] |
---|
615 | for x in lines: |
---|
616 | s = re.search(".*"+head+"(\d*)"+foot+".*",x) |
---|
617 | if s != None: |
---|
618 | final.append(s.group(1)) |
---|
619 | if (len(final) == 0) or (int(final[0]) > 1): |
---|
620 | return 0 |
---|
621 | z = 0 |
---|
622 | found = False |
---|
623 | for x in final: |
---|
624 | z += 1 |
---|
625 | if z != int(x): |
---|
626 | found = True |
---|
627 | break |
---|
628 | if not found: |
---|
629 | z += 1 |
---|
630 | return z |
---|
631 | #def _get_first_id_available |
---|
632 | |
---|
633 | def _increment_ip(self,base,num): |
---|
634 | try: |
---|
635 | ip = base.split('.') |
---|
636 | last_octect = ip[-1] |
---|
637 | x = int(last_octect) + int(num) |
---|
638 | t_octect = x / 256 |
---|
639 | l_octect = x % 256 |
---|
640 | calculed_ip = ip[0]+"."+ip[1]+"."+str(int(ip[2])+t_octect)+ "." + str(l_octect) |
---|
641 | return calculed_ip |
---|
642 | except: |
---|
643 | return None |
---|
644 | |
---|
645 | |
---|
646 | |
---|
647 | |
---|
648 | #class Dnsmasq |
---|