#!/usr/bin/env python from jinja2 import Environment from jinja2.loaders import FileSystemLoader import ConfigParser import tarfile import datetime import os, sys, shutil import os.path import tempfile import xmlrpclib class PamnssPlugin: # Templates variables TEMPLATES_PATH="/usr/share/n4d/templates/pamnss/" LDAP_ENVIRONMENT_CLIENT_TEMPLATE="etc.ldap.ldap.conf" LDAP_TEMPLATE="etc.ldap.conf" NSSWITCH_TEMPLATE="etc.nsswitch.conf" # Destination variables LDAP_ENVIRONMENT_CLIENT_DESTINATION="/etc/ldap/ldap.conf" LDAP_DESTINATION="/etc/ldap.conf" NSSWITCH_DESTINATION="/etc/nsswitch.conf" LIST_OF_FILES=["etc.ldap.conf","etc.ldap.ldap.conf","etc.nsswitch.conf"] # Backups BACKUP_DEFAULT_PATH="/backup/" BACKUP_DEFAULT_TAR="" def uchmod(self,file,mode): prevmask = os.umask(0) os.chmod(file,mode) os.umask(prevmask) #def uchmod def mkdir(path): try: os.makedirs(path) except: pass #def mkdir(path): def n4d_cron(self,minutes): if minutes%3==0: if self.is_client(): print "CLIENT - hola" #def n4d_cron def is_client(self): if "REMOTE_VARIABLES_SERVER" in objects["VariablesManager"].variables: return True else: return False #def is_client def startup(self,options): if options["controlled"]: self.configure_ldap_environment_client() self.configure_ldap() self.configure_nsswitch() if os.path.exists("/usr/sbin/nscd") and options['boot']: os.system("nscd -i passwd") os.system("nscd -i group") os.system("nscd -i netgroup") os.system("nscd -i services") os.system("nscd -i hosts") os.system("service nscd restart") return [True,True] #def startup(self): def configure_ldap_environment_client(self): # XMLRPC Debug # server=xmlrpclib.ServerProxy("https://localhost:9779") # XMLRPC Debug # if True: if objects.has_key("VariablesManager"): ldap_environment_variables=objects["VariablesManager"].get_variable_list(["LDAP_BASE_DN","CLIENT_LDAP_URI"]) # Temporal file creation path_to_work=tempfile.mkdtemp() filename=path_to_work+"ldap.conf" # Create temporal environment for jinja env = Environment(loader=FileSystemLoader(PamnssPlugin.TEMPLATES_PATH)) tmpl = env.get_template(PamnssPlugin.LDAP_ENVIRONMENT_CLIENT_TEMPLATE) # Render the template with diferent values textrendered=tmpl.render(ldap_environment_variables) # Create a temporal for nsswitch tmp,filename=tempfile.mkstemp() f = open(filename,'w') f.writelines(textrendered) f.close() # Using the ultimate chmod self.uchmod(filename,0644) # Copy unitaria shutil.copy(filename,PamnssPlugin.LDAP_ENVIRONMENT_CLIENT_DESTINATION) return [True,True] # def configure_ldap_environment_client(self): def configure_ldap(self): # Temporal file creation path_to_work=tempfile.mkdtemp() filename=path_to_work+"ldap.conf" # XMLRPC Debug # if True: if objects.has_key("VariablesManager"): ldap_variables=objects["VariablesManager"].get_variable_list(["LDAP_BASE_DN","CLIENT_LDAP_URI_NOSSL"]) # Create temporal environment for jinja env = Environment(loader=FileSystemLoader(PamnssPlugin.TEMPLATES_PATH)) tmpl = env.get_template(PamnssPlugin.LDAP_TEMPLATE) # Render the template with diferent values textrendered = tmpl.render(ldap_variables) # Create a temporal for nsswitch tmp,filename=tempfile.mkstemp() f = open(filename,'w') f.writelines(textrendered) f.close() # Using the ultimate chmod self.uchmod(filename,0644) # Move to the final destination shutil.copy(filename,PamnssPlugin.LDAP_DESTINATION) return [True,True] #def configure_ldap(self): def configure_nsswitch(self): # XMLRPC Debug # server=xmlrpclib.ServerProxy("https://localhost:9779") # Get the template from templates library env = Environment(loader=FileSystemLoader(PamnssPlugin.TEMPLATES_PATH)) tmpl = env.get_template('etc.nsswitch.conf') enable_nss_ldap={} enable_nss_ldap["ENABLE_NSS_LDAP"]="ENABLED" # Render the template textrendered=tmpl.render(enable_nss_ldap) # Create a temporal for nsswitch tmp,filename=tempfile.mkstemp() f = open(filename,'w') f.writelines(textrendered) f.close() self.uchmod(filename,0644) # Copy unitaria shutil.copy(filename,PamnssPlugin.NSSWITCH_DESTINATION) os.system("initctl emit --no-wait nsswitch-configured") if os.path.exists("/usr/sbin/nscd"): os.system("nscd -i passwd") os.system("nscd -i group") os.system("nscd -i netgroup") os.system("nscd -i services") os.system("nscd -i hosts") os.system("service nscd restart") return [True,True] # def configure_nsswitch def backup(self,dir=BACKUP_DEFAULT_PATH): file_path=dir+"/"+get_backup_name("PamnssPlugin") self.backup_output=self.backup_configuration(file_path) return self.backup_output #def backup(self): def test(self): return [True, True] #def test(self): def restore(self,file_path=None): if file_path==None: for f in sorted(os.listdir("/backup"),reverse=True): if "PamnssPlugin" in f: file_path="/backup/"+f break self.restore_output=self.restore_configuration(file_path) return self.restore_output #def restore(self): def restore_configuration(self, path): # Exists the file? if not os.path.exists(path): return [False, "[N4D] PamnssPlugin -Restore- Tar File is not present"] # Extract to temporal directory path_to_work=tempfile.mkdtemp() tar=tarfile.open(path) tar.extractall(path_to_work) tar.close() path_to_work=path_to_work+"/"+self.__class__.__name__+"/" print path_to_work # First read the configuration from n4d-config-ini if not os.path.exists(path_to_work+"n4d-config.ini"): return [False, "[N4D] - PamnssPlugin -Restore- Configuration File is not present"] Config = ConfigParser.ConfigParser() Config.read(path_to_work+"n4d-config.ini") for section in Config.sections(): # Read the config.ini back_file=Config.get(section,"file") dest_path= Config.get(section,"path") permissions=Config.get(section,"permissions") # Create path in destination if not exists if not os.path.isdir(os.path.dirname(dest_path)): os.makedirs(os.path.dirname(dest_path),0755) # Copy the file to destination shutil.copy(path_to_work+back_file,dest_path) print (path_to_work+back_file,dest_path) return [True, True] #def restore_configuration(self, path=BACKUP_DEFAULT_PATH): def backup_configuration(self,file_path): # Temporal directory to work path_to_work=tempfile.mkdtemp()+"/" # Create config to backup the files Config = ConfigParser.ConfigParser() backupCfg= open(path_to_work+"n4d-config.ini","w") # Copy the configuration files to the path try : # Copy LDAP files shutil.copy(PamnssPlugin.LDAP_DESTINATION,path_to_work+PamnssPlugin.LDAP_TEMPLATE) # Add to ini file backup info Config.add_section("LDAP") Config.set("LDAP", "FILE", PamnssPlugin.LDAP_TEMPLATE) Config.set("LDAP","PATH",PamnssPlugin.LDAP_DESTINATION) Config.set("LDAP","PERMISSIONS","0644") # Copy LDAP CLIENT shutil.copy(PamnssPlugin.LDAP_ENVIRONMENT_CLIENT_DESTINATION,path_to_work+PamnssPlugin.LDAP_ENVIRONMENT_CLIENT_TEMPLATE) # Add to ini file backup info Config.add_section("LDAPENVIRONMENT") Config.set("LDAPENVIRONMENT", "FILE", PamnssPlugin.LDAP_ENVIRONMENT_CLIENT_TEMPLATE) Config.set("LDAPENVIRONMENT","PATH",PamnssPlugin.LDAP_ENVIRONMENT_CLIENT_DESTINATION) Config.set("LDAPENVIRONMENT","PERMISSIONS","0644") # Copy nsswitch shutil.copy(PamnssPlugin.NSSWITCH_DESTINATION,path_to_work+PamnssPlugin.NSSWITCH_TEMPLATE) # Addd to ini file backup Infoe Config.add_section("NSSWITCH") Config.set("NSSWITCH","FILE",PamnssPlugin.NSSWITCH_TEMPLATE) Config.set("NSSWITCH","PATH",PamnssPlugin.NSSWITCH_DESTINATION) Config.set("NSSWITCH","PERMISSIONS","0644") # Close the ini.file Config.write(backupCfg) backupCfg.close() # Create tar.gz date_tar_gz=datetime.date.today().__str__() path_to_backup=file_path # Resolve if the file is present if os.path.exists(path_to_backup): os.remove(path_to_backup) tar = tarfile.open(path_to_backup,"w:gz") tar.add(path_to_work,arcname=self.__class__.__name__) tar.close() return [True, path_to_backup] except IOError as e: # something is going wrong return [False, "[N4D] I/O error({0}): {1}".format(e.errno, e.strerror)] #def backup_configuration(self,path=BACKUP_DEFAULT_PATH): def check_network_authentication(self): if self.nsswitch_enable and self.ldap_enable : return [True,True] else: return [True,False] #def check_network_authentication(self): #class PamnssPlugin if __name__=="__main__": print ("[N4D] Debug Pamnss") pp=PamnssPlugin() #print pp.backup() #print pp.configure_ldap() #print name_backup #print pp.restore_configuration("/backup/n4d/PamnssPlugin-2012-10-04.tar.gz")