66from jinja2 import FileSystemLoader ,BaseLoader
77import backend_utils as u
88import jinja2
9-
9+ __PRIVATE_KEY__ = '../.ssh/id_ed25519'
10+ __TEMPLATES_PS1__ = "../ps1_templates/"
1011__DEBUG__ = 0
12+ def set_private_key (keyfile ):
13+ global __PRIVATE_KEY__
14+ __PRIVATE_KEY__ = keyfile
15+
16+ def set_template_ps1_dir (dir ):
17+ global __TEMPLATES_PS1__
18+ __TEMPLATES_PS1__ = dir
1119def set_config (config ):
1220 u .__CONFIG__ = config
13-
21+ def set_debug ():
22+ global __DEBUG__
23+ __DEBUG__ = 1
1424def open_ssh_conn ():
1525 """Opening a ssh client connection with parameter in ../etc/config.conf"""
16- pkey = paramiko .Ed25519Key .from_private_key_file ('../.ssh/id_ed25519' )
26+ pkey = paramiko .Ed25519Key .from_private_key_file (__PRIVATE_KEY__ )
1727 client = paramiko .SSHClient ()
1828 policy = paramiko .AutoAddPolicy ()
1929 client .set_missing_host_key_policy (policy )
@@ -51,11 +61,17 @@ def compose_dn(entity):
5161 data ['rdnValue' ]= rdnValue
5262 if branchAttr != '' :
5363 branchValue = u .find_key (entity ,branchAttr )
54- key_branch = 'branchFor' + branchValue
55- if branchValue != '' :
64+ if type (branchValue ) is list :
65+ key_branch = 'branchFor' + branchValue [0 ]
66+ else :
67+ key_branch = 'branchFor' + branchValue
68+ if key_branch != '' :
5669 branch = u .config (key_branch ,'' )
5770 data ['branch' ]= branch
58- template_string = 'cn={{ rdnValue}},{{ branch }},{{ config.base }}'
71+ if branch == "" :
72+ template_string = 'cn={{ rdnValue}},{{ config.base }}'
73+ else :
74+ template_string = 'cn={{ rdnValue}},{{ branch }},{{ config.base }}'
5975 else :
6076 template_string = 'cn={{ rdnValue}},{{ config.base }}'
6177 else :
@@ -71,7 +87,7 @@ def dn_superior(dn):
7187
7288
7389def test_conn ():
74- environment = jinja2 .Environment (loader = FileSystemLoader ("../ps1_templates/" ))
90+ environment = jinja2 .Environment (loader = FileSystemLoader (__TEMPLATES_PS1__ ))
7591 template = environment .get_template ('ping.template' )
7692 content = template .render ({})
7793 scriptName = 'ping.ps1'
@@ -85,12 +101,8 @@ def test_conn():
85101 exitCode = chan .recv_exit_status ()
86102 content = chan .recv (4096 ).decode ('utf-8' )
87103 del client
88- if exitCode == 0 :
89- print (u .returncode (0 , content .rstrip ("\n " )))
90- exit (0 )
91- else :
92- print (u .returncode (1 , content .rstrip ("\n " )))
93- exit (1 )
104+ return exitCode
105+
94106
95107def gen_script_from_template (entity ,template ):
96108 dataStatus = 0
@@ -108,7 +120,7 @@ def gen_script_from_template(entity,template):
108120 'dataStatus' : dataStatus
109121 }
110122
111- environment = jinja2 .Environment (loader = FileSystemLoader ("../ps1_templates/" ))
123+ environment = jinja2 .Environment (loader = FileSystemLoader (__TEMPLATES_PS1__ ))
112124 template = environment .get_template (template )
113125 content = template .render (data )
114126 return content
@@ -145,10 +157,52 @@ def ad_exec_script(entity,template,params=""):
145157 del client
146158 if exitCode == 0 :
147159 print (u .returncode (0 ,content .rstrip ("\n " )))
148- exit (0 )
160+ return (0 )
149161 else :
150162 print (u .returncode (1 ,content .rstrip ("\n " )))
151- exit (1 )
163+ return (1 )
152164 else :
153165 print (u .returncode (0 , "Backend in debug mode" ))
166+ return (0 )
154167
168+ def ad_exec_script_content (entity ,template ,params = "" ):
169+ if u .config ('debug' ,0 ) == "1" :
170+ __DEBUG__ = 1
171+ else :
172+ __DEBUG__ = 0
173+ content = gen_script_from_template (entity ,template )
174+ client = open_ssh_conn ()
175+ sshfile = client .open_sftp ()
176+ pid = os .getpid ()
177+ if __DEBUG__ == 0 :
178+ scriptName = 'sesame_script.' + str (pid ) + '.ps1'
179+ else :
180+ scriptName = os .path .splitext (os .path .basename (sys .argv [0 ]))[0 ] + ".ps1"
181+ with sshfile .open (scriptName , mode = "w" ) as message :
182+ message .write (content )
183+ ##execution du script
184+ chan = client .get_transport ().open_session ()
185+ if params == '' :
186+ cmd = scriptName
187+ else :
188+ cmd = scriptName + " " + params
189+ if __DEBUG__ == 0 :
190+ chan .exec_command ('powershell -ExecutionPolicy Bypass -NonInteractive -File ' + cmd )
191+ exitCode = chan .recv_exit_status ()
192+ content = chan .recv (4096 ).decode ()
193+ error = chan .recv_stderr (4096 ).decode ()
194+ chan = client .get_transport ().open_session ()
195+ ##supression du script
196+ chan .exec_command ('del ' + scriptName )
197+ del client
198+ return (content .rstrip ("\n " ))
199+ else :
200+ return ("" )
201+ def reset_password (entity ):
202+ x = ad_exec_script (entity , 'resetpassword.template' ,"-user " + entity ['payload' ]['uid' ] + " -newp " + '"' + entity ['payload' ]['newPassword' ] + '"' )
203+ return x
204+ def change_password (entity ):
205+ r = ad_exec_script (entity , 'changepassword.template' ,
206+ "-user " + entity ['payload' ]['uid' ] + ' -oldp "' + entity ['payload' ]['oldPassword' ] + '" -newp "' +
207+ entity ['payload' ]['newPassword' ] + '"' )
208+ return (r )
0 commit comments