Skip to content

Commit 96a5cf1

Browse files
committed
Add: Create krb5 config per IP
The default for the krb5 config was /etc/krb5.conf which. This now changes and for each IP a separate config is created in /tmp/krb5_<ip>.conf. These are also cleaned up.
1 parent a27df09 commit 96a5cf1

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

nasl/exec.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "nasl_func.h"
1616
#include "nasl_global_ctxt.h"
1717
#include "nasl_init.h"
18+
#include "nasl_krb5.h" /* for nasl_okrb5_clean */
1819
#include "nasl_lex_ctxt.h"
1920
#include "nasl_tree.h"
2021
#include "nasl_var.h"
@@ -1745,6 +1746,7 @@ exec_nasl_script (struct script_infos *script_infos, int mode)
17451746
}
17461747
g_free (old_dir);
17471748

1749+
nasl_okrb5_clean ();
17481750
nasl_clean_ctx (&ctx);
17491751
free_lex_ctxt (lexic);
17501752
return err;

nasl/nasl_krb5.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
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.
4145
static 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`.
4549
static 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.
4953
static 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-
7581
static OKrb5Credential
7682
build_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-
244266
tree_cell *
245267
nasl_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-
281302
tree_cell *
282303
nasl_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

src/openvas.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "../misc/plugutils.h" /* nvticache_free */
2727
#include "../misc/scan_id.h" /* to manage global scan_id */
2828
#include "../misc/vendorversion.h" /* for vendor_version_set */
29-
#include "../nasl/nasl_krb5.h" /* for nasl_okrb5_clean */
3029
#include "attack.h" /* for attack_network */
3130
#include "debug_utils.h" /* for init_sentry */
3231
#include "pluginlaunch.h" /* for init_loading_shm */
@@ -641,7 +640,6 @@ openvas (int argc, char *argv[], char *env[])
641640

642641
gvm_close_sentry ();
643642
destroy_scan_globals (globals);
644-
nasl_okrb5_clean ();
645643
#ifdef LOG_REFERENCES_AVAILABLE
646644
free_log_reference ();
647645
#endif // LOG_REFERENCES_AVAILABLE

0 commit comments

Comments
 (0)