import os import shutil import subprocess import json from pprint import pprint class N4dGuard: def __init__(self): pass #def __init def set_list_file(self, filename, filelist): try: path = '/etc/lliurex-guard-standalone/config/lists/' filename=path+str(filename) print filename print str(filelist) f=open(filename, 'w') f.write(filelist); except Exception as e: return False return True # def set_list_file def remove_list_file(self, filename): try: path='/etc/lliurex-guard-standalone/config/lists/' filename=path+str(filename) os.remove(filename) except Exception as e: print str(e) return False return True def remove_template(self, template): try: path='/etc/lliurex-guard-standalone/config/' filename=path+str(template)+".config" os.remove(filename) except Exception as e: print str(e) return False return True def save_template(self, template): try: path='/etc/lliurex-guard-standalone/config/' filename=path+str(template['template'])+".config" f=open(filename, 'w') f.write(json.dumps(template)) except Exception as e: print str(e) return False return True def apply_template(self, template): try: print "[N4dGuard] Applying template "+str(template) path='/etc/lliurex-guard-standalone/config/' json_data=open(path+template+".config") data = json.load(json_data) pprint(data) json_data.close() if(data["default_policy"]=="deny"): readlist=data["whitelist"] includelist="http_access allow domain_list\n" defaultpolicy="http_access deny all\n" else: readlist=data["blacklist"] includelist="http_access deny domain_list\n" defaultpolicy="http_access allow all\n" # Prepare to write list dstfile=open("/etc/squid3/lliurex/llx-guard-dst-domains.conf", "w") for listname in readlist: if (os.path.isfile(path+"lists/"+listname)): f=open(path+"lists/"+listname) for line in f: #print line.replace("\n", "") dstfile.write(line.replace("\n", "")+"\n") f.close() dstfile.close() f=open(path+"running_template", "w"); f.write('{"running_template":"'+str(template)+'"}'); f.close() # Copy squid.conf with appropiate templates fin=open("/etc/lliurex-guard-standalone/templates/squid.conf") fout=open("/etc/squid3/squid.conf","w") for line in fin: if (line=="#{LliureXGuardIncludeList}\n"): line=includelist if (line=="#{LliureXGuardDefaultPolicy}\n"): line=defaultpolicy fout.write(line) fout.close() fin.close() # Now restart squid command = ['invoke-rc.d', 'squid3', 'restart'] subprocess.check_output(command, shell=False) # And iptables command = ['/etc/lliurex-guard-standalone/iptables.conf'] subprocess.check_output(command, shell=True) except Exception as e: print str(e) return False return True