@@ -11,7 +11,8 @@ use nasl_c_lib::krb5::{
1111 OKrb5ErrorCode_O_KRB5_EXPECTED_NOT_NULL , OKrb5ErrorCode_O_KRB5_REALM_NOT_FOUND ,
1212 OKrb5ErrorCode_O_KRB5_SUCCESS , OKrb5GSSContext , OKrb5Slice , OKrb5Target , OKrb5User ,
1313 o_krb5_add_realm, o_krb5_find_kdc, o_krb5_gss_prepare_context, o_krb5_gss_session_key_context,
14- o_krb5_gss_update_context, okrb5_error_code_to_string, okrb5_gss_init_context,
14+ o_krb5_gss_update_context, okrb5_error_code_to_string, okrb5_gss_free_context,
15+ okrb5_gss_init_context,
1516} ;
1617use nasl_function_proc_macro:: nasl_function;
1718use std:: os;
@@ -20,6 +21,7 @@ use std::sync::Mutex;
2021use std:: { ffi:: CStr , sync:: Arc } ;
2122use thiserror:: Error ;
2223
24+ use crate :: nasl:: ScanCtx ;
2325use crate :: {
2426 function_set,
2527 nasl:: { FnError , utils:: function:: StringOrData } ,
@@ -176,6 +178,7 @@ pub struct Krb5 {
176178 cached_gss_context : Arc < Mutex < * mut OKrb5GSSContext > > ,
177179 to_application : Arc < Mutex < * mut OKrb5Slice > > ,
178180 gss_context_needs_more : bool ,
181+ config_path : Option < String > ,
179182}
180183
181184impl Drop for Krb5 {
@@ -191,13 +194,15 @@ impl Drop for Krb5 {
191194 }
192195 }
193196
194- // TODO: This block leads to munmap_chunk(): invalid pointer and Aborted (core dumped)
195- // let cached_gss_context = *self.cached_gss_context.lock().unwrap();
196- // if !cached_gss_context.is_null() {
197- // unsafe {
198- // okrb5_gss_free_context(cached_gss_context);
199- // }
200- // }
197+ let cached_gss_context = * self . cached_gss_context . lock ( ) . unwrap ( ) ;
198+ if !cached_gss_context. is_null ( ) {
199+ unsafe {
200+ okrb5_gss_free_context ( cached_gss_context) ;
201+ }
202+ }
203+ if let Some ( config_path) = & self . config_path {
204+ let _ = std:: fs:: remove_file ( config_path) ;
205+ }
201206 }
202207}
203208
@@ -229,6 +234,7 @@ impl Krb5 {
229234 #[ allow( clippy:: too_many_arguments) ]
230235 fn build_krb5_credential (
231236 & mut self ,
237+ context : & ScanCtx < ' _ > ,
232238 config_path : Option < & str > ,
233239 realm : Option < & str > ,
234240 kdc : Option < & str > ,
@@ -237,10 +243,21 @@ impl Krb5 {
237243 host : Option < & str > ,
238244 service : Option < & str > ,
239245 ) -> Result < Krb5Credentials , Krb5Error > {
240- let config_path = config_path
241- . map ( |x| x. to_string ( ) )
242- . or ( std:: env:: var ( "KRB5_CONFIG" ) . ok ( ) )
243- . unwrap_or ( "/etc/krb5.conf" . to_string ( ) ) ;
246+ let config_path = if let Some ( path) = config_path {
247+ unsafe { std:: env:: set_var ( "KRB5_CONFIG" , path) } ;
248+ path. to_string ( )
249+ } else if let Ok ( env_path) = std:: env:: var ( "KRB5_CONFIG" ) {
250+ env_path
251+ } else {
252+ let path = format ! (
253+ "/tmp/krb5_{}.conf" ,
254+ context. target( ) . ip_addr( ) . to_string( ) . replace( "." , "_" )
255+ ) ;
256+ unsafe { std:: env:: set_var ( "KRB5_CONFIG" , & path) } ;
257+ path
258+ } ;
259+
260+ self . config_path = Some ( config_path. clone ( ) ) ;
244261
245262 let realm = get_var_or_env ! ( realm, "KRB5_REALM" ) ?;
246263 let kdc = get_var_or_env ! ( kdc, "KRB5_KDC" ) ?;
@@ -309,15 +326,24 @@ impl Krb5 {
309326 #[ nasl_function( named( config_path, realm, kdc, user, password, host) ) ]
310327 fn krb5_find_kdc (
311328 & mut self ,
329+ context : & ScanCtx < ' _ > ,
312330 config_path : Option < & str > ,
313331 realm : Option < & str > ,
314332 kdc : Option < & str > ,
315333 user : Option < & str > ,
316334 password : Option < & str > ,
317335 host : Option < & str > ,
318336 ) -> Result < String , FnError > {
319- let credential =
320- self . build_krb5_credential ( config_path, realm, kdc, user, password, host, None ) ?;
337+ let credential = self . build_krb5_credential (
338+ context,
339+ config_path,
340+ realm,
341+ kdc,
342+ user,
343+ password,
344+ host,
345+ None ,
346+ ) ?;
321347 let mut kdc_ptr: * mut c_char = std:: ptr:: null_mut ( ) ;
322348
323349 self . last_okrb5_result =
@@ -364,6 +390,7 @@ impl Krb5 {
364390 #[ nasl_function( named( config_path, realm, kdc, user, password, host, service) ) ]
365391 fn krb5_gss_prepare_context (
366392 & mut self ,
393+ context : & ScanCtx < ' _ > ,
367394 config_path : Option < & str > ,
368395 realm : Option < & str > ,
369396 kdc : Option < & str > ,
@@ -372,8 +399,16 @@ impl Krb5 {
372399 host : Option < & str > ,
373400 service : Option < & str > ,
374401 ) -> Result < u32 , FnError > {
375- let credential =
376- self . build_krb5_credential ( config_path, realm, kdc, user, password, host, service) ?;
402+ let credential = self . build_krb5_credential (
403+ context,
404+ config_path,
405+ realm,
406+ kdc,
407+ user,
408+ password,
409+ host,
410+ service,
411+ ) ?;
377412 let mut cached_gss_context = self . cached_gss_context . lock ( ) . unwrap ( ) ;
378413 if cached_gss_context. is_null ( ) {
379414 * cached_gss_context = unsafe { okrb5_gss_init_context ( ) } ;
0 commit comments