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 { \
@@ -56,6 +63,10 @@ static bool gss_update_context_more = false;
5663 { \
5764 okrb5_set_slice_from_str (slice, getenv (env_name)); \
5865 } \
66+ else \
67+ { \
68+ setenv (env_name, get_str_var_by_name (lexic, name), 1); \
69+ } \
5970 } \
6071 while (0)
6172
@@ -71,7 +82,6 @@ static bool gss_update_context_more = false;
7182 } \
7283 while (0)
7384
74-
7585static OKrb5Credential
7686build_krb5_credential (lex_ctxt * lexic )
7787{
@@ -84,9 +94,27 @@ build_krb5_credential (lex_ctxt *lexic)
8494 "KRB5_CONFIG" );
8595 if (credential .config_path .len == 0 )
8696 {
87- okrb5_set_slice_from_str (credential .config_path , "/etc/krb5.conf" );
97+ char * ip_str = addr6_as_str (lexic -> script_infos -> ip );
98+ for (int i = 0 ; ip_str [i ] != '\0' ; i ++ )
99+ {
100+ if (ip_str [i ] == '.' || ip_str [i ] == ':' )
101+ {
102+ ip_str [i ] = '_' ;
103+ }
104+ }
105+ char default_config_path [256 ];
106+ snprintf (default_config_path , sizeof (default_config_path ),
107+ "/tmp/krb5_%s.conf" , ip_str );
108+ setenv ("KRB5_CONFIG" , default_config_path , 1 );
109+ okrb5_set_slice_from_str (credential .config_path , default_config_path );
88110 }
89111
112+ // Store path for cleanup
113+ if (generated_config_path != NULL )
114+ free (generated_config_path );
115+ generated_config_path =
116+ strndup (credential .config_path .data , credential .config_path .len );
117+
90118 PERROR_SET_SLICE_FROM_LEX_OR_ENV (lexic , credential .realm , "realm" ,
91119 "KRB5_REALM" );
92120 PERROR_SET_SLICE_FROM_LEX_OR_ENV (lexic , credential .kdc , "kdc" , "KRB5_KDC" );
@@ -240,7 +268,6 @@ nasl_okrb5_is_failure (lex_ctxt *lexic)
240268 return retc ;
241269}
242270
243-
244271tree_cell *
245272nasl_okrb5_gss_init (lex_ctxt * lexic )
246273{
@@ -277,7 +304,6 @@ nasl_okrb5_gss_prepare_context (lex_ctxt *lexic)
277304 return retc ;
278305}
279306
280-
281307tree_cell *
282308nasl_okrb5_gss_update_context (lex_ctxt * lexic )
283309{
@@ -322,6 +348,13 @@ nasl_okrb5_clean (void)
322348 if (cached_gss_context != NULL )
323349 {
324350 okrb5_gss_free_context (cached_gss_context );
351+ cached_gss_context = NULL ;
352+ }
353+ if (generated_config_path != NULL )
354+ {
355+ unlink (generated_config_path );
356+ free (generated_config_path );
357+ generated_config_path = NULL ;
325358 }
326359}
327360
0 commit comments