#!/usr/bin/env python import os import shutil import xmlrpclib as x from jinja2 import Environment from jinja2.loaders import FileSystemLoader class FreeRadiusManager: def __init__(self): self.radius_path="/etc/freeradius/" self.templates_path="/usr/share/n4d/templates/n4d-freeradius/" self.variable_list=["LDAP_BASE_DN","INTERNAL_NETWORK","INTERNAL_MASK"] #def init def render_templates(self,server,radius_secret,ldap_user,ldap_pwd): ''' # TESTING c=x.ServerProxy("https://172.20.9.136:9779") vars=c.get_variable_list("","VariablesManager",self.variable_list) # /TESTING ''' vars=objects["VariablesManager"].get_variable_list(self.variable_list) vars["RADIUS_SECRET"]=radius_secret vars["LDAP_USER"]=ldap_user vars["LDAP_PASSWORD"]=ldap_pwd vars["SERVER"]=server env = Environment(loader=FileSystemLoader(self.templates_path)) template=env.get_template("clients.conf") str_template=template.render(vars).encode("utf-8") clients_str=str_template f=open(self.templates_path+"modules/ldap") lines=f.readlines() f.close() str_template="" for line in lines: if "%%LDAP_USER%%" in line: line=line.replace("%%LDAP_USER%%",vars["LDAP_USER"]) if "%%LDAP_PASSWORD%%" in line: line=line.replace("%%LDAP_PASSWORD%%",vars["LDAP_PASSWORD"]) if "%%LDAP_BASE_DN%%" in line: line=line.replace("%%LDAP_BASE_DN%%",vars["LDAP_BASE_DN"]) if "%%SERVER%%" in line: line=line.replace("%%SERVER%%",vars["SERVER"]) str_template+=line ldap_str=str_template return (clients_str,ldap_str) #def render_template def fix_perms(self,f): os.system("chown root:freerad %s"%f) os.system("chmod 640 %s"%f) #def def install_conf_files(self,server,radius_secret,ldap_user,ldap_pwd): try: clients_str,ldap_str=self.render_templates(server,radius_secret,ldap_user,ldap_pwd) if not os.path.exists(self.radius_path): os.makedirs(self.radius_path) #os.sysmte() # clients.conf f=open(self.radius_path+"clients.conf","w") f.write(clients_str) f.close() self.fix_perms(self.radius_path+"clients.conf") # modules/ldap if not os.path.exists(self.radius_path+"modules"): os.makedirs(self.radius_path+"modules") f=open(self.radius_path+"modules/ldap","w") f.write(ldap_str) f.close() # default if not os.path.exists(self.radius_path+"sites-available"): os.makedirs(self.radius_path+"sites-available") shutil.copy(self.templates_path+"sites-available/default",self.radius_path+"sites-available/") # inner-tunnel shutil.copy(self.templates_path+"sites-available/inner-tunnel",self.radius_path+"sites-available/") # radiusd.conf shutil.copy(self.templates_path+"radiusd.conf",self.radius_path) self.fix_perms(self.radius_path+"radiusd.conf") # eap.conf shutil.copy(self.templates_path+"eap.conf",self.radius_path) self.fix_perms(self.radius_path+"eap.conf") # modules/mschap shutil.copy(self.templates_path+"modules/mschap",self.radius_path+"modules/") os.system("service freeradius restart") #os.system("zero-center set-configured lliurex-freeradius") return {"status":True,"msg":str(True)} except Exception as e: return {"status":False,"msg":str(e)} #def install_conf_files #class RadiusManager if __name__=="__main__": r=RadiusManager() r.install_conf_files("server","myradius1","cn=roadmin...","2")