@@ -31,13 +31,16 @@ function password_hook_command($hook, $login, $newpassword, $oldpassword = null,
3131 return $ command ;
3232}
3333
34- /* @function string hook_command(string $hook, string $login )
35- Creates hook command line passing login as parameter
34+ /* @function string hook_command(string $hook, string arg1, string arg2,... )
35+ Creates hook command line passing multiple arguments
3636 @param $hook string script/command to execute for procesing hook data
37- @param $login string username
37+ @param $argN string Nth argument
3838 */
39- function hook_command ($ hook , $ login ) {
40- $ command = escapeshellcmd ($ hook ).' ' .escapeshellarg ($ login );
39+ function hook_command ($ hook , ...$ args ) {
40+ $ command = escapeshellcmd ($ hook );
41+ foreach ($ args as $ arg ) {
42+ $ command .= ' ' .escapeshellarg ($ arg );
43+ }
4144 return $ command ;
4245}
4346
@@ -65,84 +68,133 @@ function get_hook_login($dn, $ldapInstance, $login_attribute)
6568 return $ login_value ;
6669}
6770
71+ function call_external_command ($ hookConfig , $ entrypoint , $ login_value , $ params )
72+ {
73+ $ returnCode = 0 ;
74+ $ returnMessage = "" ;
75+ $ returnedEntry = isset ($ params ['entry ' ]) ? $ params ['entry ' ] : null ;
76+
77+ switch ($ entrypoint ) {
78+
79+ case "passwordReset " :
80+ $ password = $ params ['password ' ];
81+ $ command = password_hook_command ($ hookConfig [$ entrypoint ]['externalScript ' ],
82+ $ login_value ,
83+ $ password ,
84+ null ,
85+ $ hookConfig [$ entrypoint ]['encodebase64 ' ]);
86+ exec ($ command , $ output , $ returnCode );
87+ $ returnMessage = $ output [0 ];
88+ break ;
89+
90+ case "updateValidityDates " :
91+ $ start_date = $ params ['start_date ' ];
92+ $ end_date = $ params ['end_date ' ];
93+ $ command = validity_hook_command ($ hookConfig [$ entrypoint ]['externalScript ' ],
94+ $ login_value ,
95+ $ start_date ,
96+ $ end_date );
97+ exec ($ command , $ output , $ returnCode );
98+ $ returnMessage = $ output [0 ];
99+ break ;
100+
101+ case "passwordLock " :
102+ case "passwordUnlock " :
103+ case "accountEnable " :
104+ case "accountDisable " :
105+ case "deleteAccount " :
106+ $ command = hook_command ($ hookConfig [$ entrypoint ]['externalScript ' ], $ login_value );
107+ exec ($ command , $ output , $ returnCode );
108+ $ returnMessage = $ output [0 ];
109+ break ;
110+
111+ case "createAccount " :
112+ case "updateAccount " :
113+ $ dn = $ params ['dn ' ];
114+ $ command = hook_command ($ hookConfig [$ entrypoint ]['externalScript ' ], $ dn , json_encode ($ returnedEntry ));
115+ exec ($ command , $ output , $ returnCode );
116+ $ returnMessage = $ output [0 ];
117+ if (count ($ output ) > 1 ) {
118+ $ returnedEntry = json_decode (implode ('' , array_slice ($ output , 1 )), true );
119+ }
120+ break ;
121+
122+ }
123+ return array ($ returnCode , $ returnMessage , $ returnedEntry );
124+ }
125+
126+ function call_external_function ($ hookConfig , $ entrypoint , $ login_value , $ params )
127+ {
128+ $ returnCode = 0 ;
129+ $ returnMessage = "" ;
130+ $ returnedEntry = isset ($ params ['entry ' ]) ? $ params ['entry ' ] : null ;
131+
132+ switch ($ entrypoint ) {
133+
134+ case "passwordReset " :
135+ $ password = $ params ['password ' ];
136+ if ( isset ($ hookConfig [$ entrypoint ]['encodebase64 ' ]) &&
137+ $ hookConfig [$ entrypoint ]['encodebase64 ' ] )
138+ {
139+ $ password = base64_encode ($ params ['password ' ]);
140+ }
141+ $ params = [$ login_value , $ password ];
142+ list ($ returnCode , $ returnMessage ) =
143+ $ hookConfig [$ entrypoint ]['function ' ](...$ params );
144+ break ;
145+
146+ case "updateValidityDates " :
147+ $ start_date = $ params ['start_date ' ];
148+ $ end_date = $ params ['end_date ' ];
149+ $ params = [$ login_value , $ start_date , $ end_date ];
150+ list ($ returnCode , $ returnMessage ) =
151+ $ hookConfig [$ entrypoint ]['function ' ](...$ params );
152+ break ;
153+
154+ case "passwordLock " :
155+ case "passwordUnlock " :
156+ case "accountEnable " :
157+ case "accountDisable " :
158+ case "deleteAccount " :
159+ $ params = [$ login_value ];
160+ list ($ returnCode , $ returnMessage ) =
161+ $ hookConfig [$ entrypoint ]['function ' ](...$ params );
162+ break ;
163+
164+ case "createAccount " :
165+ case "updateAccount " :
166+ $ dn = $ params ['dn ' ];
167+ $ params = [$ dn , $ returnedEntry ];
168+ list ($ returnCode , $ returnMessage , $ returnedEntry ) =
169+ $ hookConfig [$ entrypoint ]['function ' ](...$ params );
170+ break ;
171+
172+ }
173+ return array ($ returnCode , $ returnMessage , $ returnedEntry );
174+ }
175+
68176function hook ($ hookConfig , $ entrypoint , $ login_value , $ params ) {
69177
70178 $ returnCode = 0 ; # success return code by default
71179 $ returnMessage = "" ;
180+ $ returnedEntry = isset ($ params ['entry ' ]) ? $ params ['entry ' ] : null ;
72181
73182 if ( isset ($ hookConfig [$ entrypoint ]['externalScript ' ]) ||
74183 isset ($ hookConfig [$ entrypoint ]['function ' ]) ) {
75184 if ( isset ($ login_value ) ) {
76185
77- # Compute external command
78- if (isset ($ hookConfig [$ entrypoint ]['externalScript ' ]))
79- {
80- switch ($ entrypoint ) {
81- case "passwordReset " :
82- $ password = $ params ['password ' ];
83- $ command = password_hook_command ($ hookConfig [$ entrypoint ]['externalScript ' ],
84- $ login_value ,
85- $ password ,
86- null ,
87- $ hookConfig [$ entrypoint ]['encodebase64 ' ]);
88- break ;
89- case "updateValidityDates " :
90- $ start_date = $ params ['start_date ' ];
91- $ end_date = $ params ['end_date ' ];
92- $ command = validity_hook_command ($ hookConfig [$ entrypoint ]['externalScript ' ],
93- $ login_value ,
94- $ start_date ,
95- $ end_date );
96- break ;
97- case "passwordLock " :
98- case "passwordUnlock " :
99- case "accountEnable " :
100- case "accountDisable " :
101- case "deleteAccount " :
102- $ command = hook_command ($ hookConfig [$ entrypoint ]['externalScript ' ], $ login_value );
103- break ;
104- }
105- }
106-
107- # Run external command
186+ # Compute and run external command
108187 if (isset ($ hookConfig [$ entrypoint ]['externalScript ' ]))
109188 {
110- exec ( $ command , $ output , $ returnCode );
111- $ returnMessage = $ output [ 0 ] ;
189+ list ( $ returnCode , $ returnMessage , $ returnedEntry ) =
190+ call_external_command ( $ hookConfig , $ entrypoint , $ login_value , $ params ) ;
112191 }
113192
114- # Prepare arguments and run function
193+ # Compute arguments and run external function
115194 if (isset ($ hookConfig [$ entrypoint ]['function ' ]))
116195 {
117- switch ($ entrypoint ) {
118- case "passwordReset " :
119- $ password = $ params ['password ' ];
120- if ( isset ($ hookConfig [$ entrypoint ]['encodebase64 ' ]) &&
121- $ hookConfig [$ entrypoint ]['encodebase64 ' ] )
122- {
123- $ password = base64_encode ($ params ['password ' ]);
124- }
125- $ params = [$ login_value , $ password ];
126- list ($ returnCode , $ returnMessage ) =
127- $ hookConfig [$ entrypoint ]['function ' ](...$ params );
128- break ;
129- case "updateValidityDates " :
130- $ start_date = $ params ['start_date ' ];
131- $ end_date = $ params ['end_date ' ];
132- $ params = [$ login_value , $ start_date , $ end_date ];
133- list ($ returnCode , $ returnMessage ) =
134- $ hookConfig [$ entrypoint ]['function ' ](...$ params );
135- break ;
136- case "passwordLock " :
137- case "passwordUnlock " :
138- case "accountEnable " :
139- case "accountDisable " :
140- case "deleteAccount " :
141- $ params = [$ login_value ];
142- list ($ returnCode , $ returnMessage ) =
143- $ hookConfig [$ entrypoint ]['function ' ](...$ params );
144- break ;
145- }
196+ list ($ returnCode , $ returnMessage , $ returnedEntry ) =
197+ call_external_function ($ hookConfig , $ entrypoint , $ login_value , $ params );
146198 }
147199
148200 }
@@ -153,7 +205,7 @@ function hook($hookConfig, $entrypoint, $login_value, $params) {
153205 }
154206 }
155207
156- return array ($ returnCode , $ returnMessage );
208+ return array ($ returnCode , $ returnMessage, $ returnedEntry );
157209}
158210
159211?>
0 commit comments