1212#include "nasl_tree.h"
1313#include "nasl_var.h"
1414
15+ #include <gvm/base/networking.h>
16+ #include <netinet/in.h>
1517#include <stdio.h>
18+ #include <string.h>
19+ #include <unistd.h>
1620
1721#define NASL_PRINT_KRB_ERROR (lexic , credential , result ) \
1822 do \
@@ -35,19 +39,22 @@ static OKrb5ErrorCode last_okrb5_result;
3539// cached_gss_context is used on cases that require an already existing session.
3640// NASL does currently not have the concept of a pointer nor struct so we need
3741// to store it as a global variable.
38- //
42+ //
3943// We use one context per run, this means that per run (target + oid) there is
4044// only on credential allowed making it safe to be cached in that fashion.
4145static struct OKrb5GSSContext * cached_gss_context = NULL ;
4246
43- // Is used for `krb5_gss_update_context_out` and is essential a
44- // cache for the data from `krb5_gss_update_context`.
47+ // Is used for `krb5_gss_update_context_out` and is essential a
48+ // cache for the data from `krb5_gss_update_context`.
4549static struct OKrb5Slice * to_application = NULL ;
4650
4751// Is used for `krb5_gss_update_context_needs_more` which indicates to the
48- // script author that `krb5_gss_update_context` is not satisfied yet.
52+ // script author that `krb5_gss_update_context` is not satisfied yet.
4953static bool gss_update_context_more = false;
5054
55+ // Stores the path to the generated krb5 config file for cleanup.
56+ static char * generated_config_path = NULL ;
57+
5158#define SET_SLICE_FROM_LEX_OR_ENV (lexic , slice , name , env_name ) \
5259 do \
5360 { \
@@ -71,7 +78,6 @@ static bool gss_update_context_more = false;
7178 } \
7279 while (0)
7380
74-
7581static OKrb5Credential
7682build_krb5_credential (lex_ctxt * lexic )
7783{
@@ -84,9 +90,26 @@ build_krb5_credential (lex_ctxt *lexic)
8490 "KRB5_CONFIG" );
8591 if (credential .config_path .len == 0 )
8692 {
87- okrb5_set_slice_from_str (credential .config_path , "/etc/krb5.conf" );
93+ char * ip_str = addr6_as_str (lexic -> script_infos -> ip );
94+ for (int i = 0 ; ip_str [i ] != '\0' ; i ++ )
95+ {
96+ if (ip_str [i ] == '.' || ip_str [i ] == ':' )
97+ {
98+ ip_str [i ] = '_' ;
99+ }
100+ }
101+ char default_config_path [256 ];
102+ snprintf (default_config_path , sizeof (default_config_path ),
103+ "/tmp/krb5_%s.conf" , ip_str );
104+ okrb5_set_slice_from_str (credential .config_path , default_config_path );
88105 }
89106
107+ // Store path for cleanup
108+ if (generated_config_path != NULL )
109+ free (generated_config_path );
110+ generated_config_path =
111+ strndup (credential .config_path .data , credential .config_path .len );
112+
90113 PERROR_SET_SLICE_FROM_LEX_OR_ENV (lexic , credential .realm , "realm" ,
91114 "KRB5_REALM" );
92115 PERROR_SET_SLICE_FROM_LEX_OR_ENV (lexic , credential .kdc , "kdc" , "KRB5_KDC" );
@@ -240,7 +263,6 @@ nasl_okrb5_is_failure (lex_ctxt *lexic)
240263 return retc ;
241264}
242265
243-
244266tree_cell *
245267nasl_okrb5_gss_init (lex_ctxt * lexic )
246268{
@@ -277,7 +299,6 @@ nasl_okrb5_gss_prepare_context (lex_ctxt *lexic)
277299 return retc ;
278300}
279301
280-
281302tree_cell *
282303nasl_okrb5_gss_update_context (lex_ctxt * lexic )
283304{
@@ -322,6 +343,13 @@ nasl_okrb5_clean (void)
322343 if (cached_gss_context != NULL )
323344 {
324345 okrb5_gss_free_context (cached_gss_context );
346+ cached_gss_context = NULL ;
347+ }
348+ if (generated_config_path != NULL )
349+ {
350+ unlink (generated_config_path );
351+ free (generated_config_path );
352+ generated_config_path = NULL ;
325353 }
326354}
327355
0 commit comments