OpenVAS Scanner 23.43.1
nasl_krb5.c File Reference
#include "nasl_krb5.h"
#include "../misc/openvas-krb5.h"
#include "nasl_debug.h"
#include "nasl_func.h"
#include "nasl_global_ctxt.h"
#include "nasl_lex_ctxt.h"
#include "nasl_tree.h"
#include "nasl_var.h"
#include <gvm/base/networking.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
Include dependency graph for nasl_krb5.c:

Go to the source code of this file.

Macros

#define NASL_PRINT_KRB_ERROR(lexic, credential, result)
#define SET_SLICE_FROM_LEX_OR_ENV(lexic, slice, name, env_name)
#define PERROR_SET_SLICE_FROM_LEX_OR_ENV(lexic, slice, name, env_name)

Functions

static OKrb5Credential build_krb5_credential (lex_ctxt *lexic)
tree_cellnasl_okrb5_find_kdc (lex_ctxt *lexic)
 Returns the defined KDC of a given Realm.
tree_cellnasl_okrb5_add_realm (lex_ctxt *lexic)
 Adds the given KDC to the given Realm.
tree_cellnasl_okrb5_is_success (lex_ctxt *lexic)
 Returns 1 if the krb5 function was successful 0 otherwise.
tree_cellnasl_okrb5_is_failure (lex_ctxt *lexic)
 Returns 0 if the krb5 function was successful and 1 if it failed.
tree_cellnasl_okrb5_gss_init (lex_ctxt *lexic)
tree_cellnasl_okrb5_gss_prepare_context (lex_ctxt *lexic)
tree_cellnasl_okrb5_gss_update_context (lex_ctxt *lexic)
void nasl_okrb5_clean (void)
tree_cellnasl_okrb5_gss_update_context_needs_more (lex_ctxt *lexic)
static tree_cellokrb5_slice_to_tree_cell (struct OKrb5Slice *slice)
tree_cellnasl_okrb5_gss_update_context_out (lex_ctxt *lexic)
tree_cellnasl_okrb5_gss_session_key_context (lex_ctxt *lexic)
tree_cellnasl_okrb5_error_code_to_string (lex_ctxt *lexic)

Variables

static OKrb5ErrorCode last_okrb5_result
static struct OKrb5GSSContextcached_gss_context = NULL
static struct OKrb5Sliceto_application = NULL
static bool gss_update_context_more = false
static char * generated_config_path = NULL

Macro Definition Documentation

◆ NASL_PRINT_KRB_ERROR

#define NASL_PRINT_KRB_ERROR ( lexic,
credential,
result )
Value:
do \
{ \
char *error_str = okrb5_error_code_to_string (result); \
nasl_perror ( \
lexic, "%s[config_path: '%s' realm: '%s' user: '%s'] => %s (%d)", \
__func__, credential.config_path.data, credential.realm.data, \
credential.user.user.data, error_str, result); \
free (error_str); \
} \
while (0)
char * okrb5_error_code_to_string(const OKrb5ErrorCode code)

Definition at line 21 of file nasl_krb5.c.

21#define NASL_PRINT_KRB_ERROR(lexic, credential, result) \
22 do \
23 { \
24 char *error_str = okrb5_error_code_to_string (result); \
25 nasl_perror ( \
26 lexic, "%s[config_path: '%s' realm: '%s' user: '%s'] => %s (%d)", \
27 __func__, credential.config_path.data, credential.realm.data, \
28 credential.user.user.data, error_str, result); \
29 free (error_str); \
30 } \
31 while (0)

Referenced by build_krb5_credential(), nasl_okrb5_add_realm(), and nasl_okrb5_find_kdc().

◆ PERROR_SET_SLICE_FROM_LEX_OR_ENV

#define PERROR_SET_SLICE_FROM_LEX_OR_ENV ( lexic,
slice,
name,
env_name )
Value:
do \
{ \
SET_SLICE_FROM_LEX_OR_ENV (lexic, slice, name, env_name); \
if (slice.len == 0) \
{ \
nasl_perror (lexic, "Expected %s or env variable %s", name, \
env_name); \
} \
} \
while (0)
const char * name
Definition nasl_init.c:440

Definition at line 73 of file nasl_krb5.c.

73#define PERROR_SET_SLICE_FROM_LEX_OR_ENV(lexic, slice, name, env_name) \
74 do \
75 { \
76 SET_SLICE_FROM_LEX_OR_ENV (lexic, slice, name, env_name); \
77 if (slice.len == 0) \
78 { \
79 nasl_perror (lexic, "Expected %s or env variable %s", name, \
80 env_name); \
81 } \
82 } \
83 while (0)

Referenced by build_krb5_credential().

◆ SET_SLICE_FROM_LEX_OR_ENV

#define SET_SLICE_FROM_LEX_OR_ENV ( lexic,
slice,
name,
env_name )
Value:
do \
{ \
okrb5_set_slice_from_str (slice, get_str_var_by_name (lexic, name)); \
if (slice.len == 0) \
{ \
okrb5_set_slice_from_str (slice, getenv (env_name)); \
} \
else \
{ \
setenv (env_name, get_str_var_by_name (lexic, name), 1); \
} \
} \
while (0)
char * get_str_var_by_name(lex_ctxt *, const char *)
Definition nasl_var.c:1118

Definition at line 58 of file nasl_krb5.c.

58#define SET_SLICE_FROM_LEX_OR_ENV(lexic, slice, name, env_name) \
59 do \
60 { \
61 okrb5_set_slice_from_str (slice, get_str_var_by_name (lexic, name)); \
62 if (slice.len == 0) \
63 { \
64 okrb5_set_slice_from_str (slice, getenv (env_name)); \
65 } \
66 else \
67 { \
68 setenv (env_name, get_str_var_by_name (lexic, name), 1); \
69 } \
70 } \
71 while (0)

Referenced by build_krb5_credential().

Function Documentation

◆ build_krb5_credential()

OKrb5Credential build_krb5_credential ( lex_ctxt * lexic)
static

Definition at line 86 of file nasl_krb5.c.

87{
88 OKrb5Credential credential = {0};
90
91 char *kdc = NULL;
92
93 SET_SLICE_FROM_LEX_OR_ENV (lexic, credential.config_path, "config_path",
94 "KRB5_CONFIG");
95 if (credential.config_path.len == 0)
96 {
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);
110 }
111
112 // Store path for cleanup
113 if (generated_config_path != NULL)
116 strndup (credential.config_path.data, credential.config_path.len);
117
118 PERROR_SET_SLICE_FROM_LEX_OR_ENV (lexic, credential.realm, "realm",
119 "KRB5_REALM");
120 PERROR_SET_SLICE_FROM_LEX_OR_ENV (lexic, credential.kdc, "kdc", "KRB5_KDC");
121 PERROR_SET_SLICE_FROM_LEX_OR_ENV (lexic, credential.user.user, "user",
122 "KRB5_USER");
123 PERROR_SET_SLICE_FROM_LEX_OR_ENV (lexic, credential.user.password, "password",
124 "KRB5_PASSWORD");
125 PERROR_SET_SLICE_FROM_LEX_OR_ENV (lexic, credential.target.host_name, "host",
126 "KRB5_TARGET_HOST");
127 // SET_SLICE_FROM_LEX_OR_ENV (lexic, credential.target.service, "service",
128 // "KRB5_TARGET_SERVICE");
129
130 if ((code = o_krb5_find_kdc (&credential, &kdc)))
131 {
133 {
134 NASL_PRINT_KRB_ERROR (lexic, credential, code);
135 }
136 else
137 {
138 if ((code = o_krb5_add_realm (&credential, credential.kdc.data)))
139 {
140 NASL_PRINT_KRB_ERROR (lexic, credential, code);
141 }
142 }
143 }
144 else
145 {
146 free (kdc);
147 }
148 if (credential.target.service.len == 0)
149 {
150 okrb5_set_slice_from_str (credential.target.service, "cifs");
151 }
152 SET_SLICE_FROM_LEX_OR_ENV (lexic, credential.kdc, "kdc", "KRB5_KDC");
153
154 memset (&credential.target.domain, 0, sizeof (struct OKrb5Slice));
155
156 return credential;
157}
void free(void *)
static char * generated_config_path
Definition nasl_krb5.c:56
#define PERROR_SET_SLICE_FROM_LEX_OR_ENV(lexic, slice, name, env_name)
Definition nasl_krb5.c:73
#define SET_SLICE_FROM_LEX_OR_ENV(lexic, slice, name, env_name)
Definition nasl_krb5.c:58
#define NASL_PRINT_KRB_ERROR(lexic, credential, result)
Definition nasl_krb5.c:21
#define code
OKrb5ErrorCode o_krb5_add_realm(const OKrb5Credential *creds, const char *kdc)
OKrb5ErrorCode o_krb5_find_kdc(const OKrb5Credential *creds, char **kdc)
OKrb5ErrorCode
@ O_KRB5_REALM_NOT_FOUND
@ O_KRB5_CONF_NOT_FOUND
#define okrb5_set_slice_from_str(slice, str)
struct OKrb5Slice kdc
struct OKrb5Slice realm
struct OKrb5Target target
struct OKrb5Slice config_path
struct OKrb5User user
void * data
struct OKrb5Slice service
struct OKrb5Slice domain
struct OKrb5Slice host_name
struct OKrb5Slice password
struct OKrb5Slice user
struct in6_addr * ip
Definition scanneraux.h:37
struct script_infos * script_infos

References code, OKrb5Credential::config_path, OKrb5Slice::data, OKrb5Target::domain, free(), generated_config_path, OKrb5Target::host_name, script_infos::ip, OKrb5Credential::kdc, OKrb5Slice::len, NASL_PRINT_KRB_ERROR, o_krb5_add_realm(), O_KRB5_CONF_NOT_FOUND, o_krb5_find_kdc(), O_KRB5_REALM_NOT_FOUND, okrb5_set_slice_from_str, OKrb5User::password, PERROR_SET_SLICE_FROM_LEX_OR_ENV, OKrb5Credential::realm, struct_lex_ctxt::script_infos, OKrb5Target::service, SET_SLICE_FROM_LEX_OR_ENV, OKrb5Credential::target, OKrb5Credential::user, and OKrb5User::user.

Referenced by nasl_okrb5_add_realm(), nasl_okrb5_find_kdc(), and nasl_okrb5_gss_prepare_context().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_okrb5_add_realm()

tree_cell * nasl_okrb5_add_realm ( lex_ctxt * lexic)

Adds the given KDC to the given Realm.

This function returns 0 on success. To retrieve a human readable error message, the function okrb5_result can be used.

The nasl function has three optional parameter:

  • realm: The realm for which the KDC should be returned. If the realm is not defined, then the env parameter KRB5_REALM is used.
  • kdc: The realm for which the KDC should be returned. If the realm is not defined, then the env parameter KRB5_KDC is used.
  • config_path: The path to the krb5.conf file. If the path is not defined, then the env parameter KRB5_CONFIG is used.

This function should only be used for debug purposes.

Parameters
[in]lexicNASL lexer.
Returns
lex cell containing a number indicating success or failure.

Definition at line 200 of file nasl_krb5.c.

201{
202 tree_cell *retc;
203 OKrb5Credential credential;
204 char *kdc = get_str_var_by_name (lexic, "kdc");
205 if (kdc == NULL)
206 {
207 kdc = getenv ("KRB5_KDC");
208 if (kdc == NULL)
209 {
211 NASL_PRINT_KRB_ERROR (lexic, credential, last_okrb5_result);
212 goto exit;
213 }
214 }
215
216 credential = build_krb5_credential (lexic);
217
218 if ((last_okrb5_result = o_krb5_add_realm (&credential, kdc)))
219 {
220 NASL_PRINT_KRB_ERROR (lexic, credential, last_okrb5_result);
221 }
222
223exit:
225 retc->x.i_val = last_okrb5_result;
226 return retc;
227}
static OKrb5ErrorCode last_okrb5_result
Definition nasl_krb5.c:37
static OKrb5Credential build_krb5_credential(lex_ctxt *lexic)
Definition nasl_krb5.c:86
tree_cell * alloc_typed_cell(int typ)
Definition nasl_tree.c:25
@ CONST_INT
Definition nasl_tree.h:79
struct TC tree_cell
@ O_KRB5_EXPECTED_NOT_NULL
long int i_val
Definition nasl_tree.h:104
union TC::@332262321161220155002104006201360276211317150140 x

References alloc_typed_cell(), build_krb5_credential(), CONST_INT, get_str_var_by_name(), TC::i_val, last_okrb5_result, NASL_PRINT_KRB_ERROR, o_krb5_add_realm(), O_KRB5_EXPECTED_NOT_NULL, and TC::x.

Here is the call graph for this function:

◆ nasl_okrb5_clean()

void nasl_okrb5_clean ( void )

Definition at line 340 of file nasl_krb5.c.

341{
342 if (to_application != NULL)
343 {
344 free (to_application->data);
346 to_application = NULL;
347 }
348 if (cached_gss_context != NULL)
349 {
351 cached_gss_context = NULL;
352 }
353 if (generated_config_path != NULL)
354 {
355 unlink (generated_config_path);
358 }
359}
static struct OKrb5Slice * to_application
Definition nasl_krb5.c:49
static struct OKrb5GSSContext * cached_gss_context
Definition nasl_krb5.c:45
void okrb5_gss_free_context(struct OKrb5GSSContext *context)

References cached_gss_context, free(), generated_config_path, okrb5_gss_free_context(), and to_application.

Referenced by exec_nasl_script().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_okrb5_error_code_to_string()

tree_cell * nasl_okrb5_error_code_to_string ( lex_ctxt * lexic)

Definition at line 414 of file nasl_krb5.c.

415{
416 (void) lexic;
419 retc->size = strlen (retc->x.str_val);
420 return retc;
421}
@ CONST_STR
Definition nasl_tree.h:80
long int size
Definition nasl_tree.h:99
char * str_val
Definition nasl_tree.h:103

References alloc_typed_cell(), CONST_STR, last_okrb5_result, okrb5_error_code_to_string(), TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_okrb5_find_kdc()

tree_cell * nasl_okrb5_find_kdc ( lex_ctxt * lexic)

Returns the defined KDC of a given Realm.

This function returns the KDC of a given Realm. The Realm is defined in the krb5.conf file. If there is no KDC for the given Realm, the function returns NULL within the tree_cell to the script.

The nasl function has two optional parameter:

  • realm: The realm for which the KDC should be returned. If the realm is not defined, then the env parameter KRB5_REALM is used.
  • config_path: The path to the krb5.conf file. If the path is not defined, then the env parameter KRB5_CONFIG is used.

This function should only be used for debug purposes.

Parameters
[in]lexicNASL lexer.
Returns
lex cell containing the KDC as a string.

Definition at line 179 of file nasl_krb5.c.

180{
181 tree_cell *retc;
182 char *kdc = NULL;
183 OKrb5Credential credential;
184
185 credential = build_krb5_credential (lexic);
186
187 if ((last_okrb5_result = o_krb5_find_kdc (&credential, &kdc)))
188 {
189 NASL_PRINT_KRB_ERROR (lexic, credential, last_okrb5_result);
190 return FAKE_CELL;
191 }
192
194 retc->x.str_val = kdc;
195 retc->size = strlen (kdc);
196 return retc;
197}
@ CONST_DATA
Definition nasl_tree.h:82
#define FAKE_CELL
Definition nasl_tree.h:110

References alloc_typed_cell(), build_krb5_credential(), CONST_DATA, FAKE_CELL, last_okrb5_result, NASL_PRINT_KRB_ERROR, o_krb5_find_kdc(), TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_okrb5_gss_init()

tree_cell * nasl_okrb5_gss_init ( lex_ctxt * lexic)

Definition at line 272 of file nasl_krb5.c.

273{
274 (void) lexic;
276 if (cached_gss_context == NULL)
277 {
279 }
280 else
281 {
283 };
285 retc->x.i_val = last_okrb5_result;
286 return retc;
287}
struct OKrb5GSSContext * okrb5_gss_init_context(void)
@ O_KRB5_SUCCESS

References alloc_typed_cell(), cached_gss_context, CONST_INT, TC::i_val, last_okrb5_result, O_KRB5_EXPECTED_NOT_NULL, O_KRB5_SUCCESS, okrb5_gss_init_context(), and TC::x.

Here is the call graph for this function:

◆ nasl_okrb5_gss_prepare_context()

tree_cell * nasl_okrb5_gss_prepare_context ( lex_ctxt * lexic)

Definition at line 289 of file nasl_krb5.c.

290{
291 (void) lexic;
292
293 OKrb5Credential credential;
294 credential = build_krb5_credential (lexic);
296 if (cached_gss_context == NULL)
297 {
299 }
300 result = o_krb5_gss_prepare_context (&credential, cached_gss_context);
302 retc->x.i_val = result;
303 last_okrb5_result = result;
304 return retc;
305}
OKrb5ErrorCode o_krb5_gss_prepare_context(const OKrb5Credential *creds, struct OKrb5GSSContext *gss_context)

References alloc_typed_cell(), build_krb5_credential(), cached_gss_context, CONST_INT, TC::i_val, last_okrb5_result, o_krb5_gss_prepare_context(), O_KRB5_SUCCESS, okrb5_gss_init_context(), and TC::x.

Here is the call graph for this function:

◆ nasl_okrb5_gss_session_key_context()

tree_cell * nasl_okrb5_gss_session_key_context ( lex_ctxt * lexic)

Definition at line 395 of file nasl_krb5.c.

396{
397 (void) lexic;
398 struct OKrb5Slice *session_key = NULL;
399 if (cached_gss_context == NULL)
400 {
402 return FAKE_CELL;
403 }
404 if ((last_okrb5_result =
407 {
408 return FAKE_CELL;
409 }
410 return okrb5_slice_to_tree_cell (session_key);
411}
static tree_cell * okrb5_slice_to_tree_cell(struct OKrb5Slice *slice)
Definition nasl_krb5.c:371
OKrb5ErrorCode o_krb5_gss_session_key_context(struct OKrb5GSSContext *gss_context, struct OKrb5Slice **out)

References cached_gss_context, FAKE_CELL, last_okrb5_result, O_KRB5_EXPECTED_NOT_NULL, o_krb5_gss_session_key_context(), O_KRB5_SUCCESS, and okrb5_slice_to_tree_cell().

Here is the call graph for this function:

◆ nasl_okrb5_gss_update_context()

tree_cell * nasl_okrb5_gss_update_context ( lex_ctxt * lexic)

Definition at line 308 of file nasl_krb5.c.

309{
311 tree_cell *retc;
312 struct OKrb5Slice from_application;
313
314 if (to_application != NULL)
315 {
316 free (to_application->data);
318 to_application = NULL;
319 }
320
321 from_application.data = (void *) get_str_var_by_num (lexic, 0);
322 from_application.len = get_var_size_by_num (lexic, 0);
323
324 if (cached_gss_context == NULL)
325 {
327 goto result;
328 }
329 result =
332result:
334 retc->x.i_val = result;
335 last_okrb5_result = result;
336 return retc;
337}
static bool gss_update_context_more
Definition nasl_krb5.c:53
long int get_var_size_by_num(lex_ctxt *, int)
Definition nasl_var.c:1145
char * get_str_var_by_num(lex_ctxt *, int)
Definition nasl_var.c:1108
OKrb5ErrorCode o_krb5_gss_update_context(struct OKrb5GSSContext *gss_context, const struct OKrb5Slice *in_data, struct OKrb5Slice **out_data, bool *more)

References alloc_typed_cell(), cached_gss_context, CONST_INT, OKrb5Slice::data, free(), get_str_var_by_num(), get_var_size_by_num(), gss_update_context_more, TC::i_val, last_okrb5_result, OKrb5Slice::len, O_KRB5_EXPECTED_NOT_NULL, o_krb5_gss_update_context(), O_KRB5_SUCCESS, to_application, and TC::x.

Here is the call graph for this function:

◆ nasl_okrb5_gss_update_context_needs_more()

tree_cell * nasl_okrb5_gss_update_context_needs_more ( lex_ctxt * lexic)

Definition at line 362 of file nasl_krb5.c.

363{
364 (void) lexic;
367 return retc;
368}

References alloc_typed_cell(), CONST_INT, gss_update_context_more, TC::i_val, and TC::x.

Here is the call graph for this function:

◆ nasl_okrb5_gss_update_context_out()

tree_cell * nasl_okrb5_gss_update_context_out ( lex_ctxt * lexic)

Definition at line 380 of file nasl_krb5.c.

381{
382 (void) lexic;
383 if (to_application == NULL)
384 {
385 return FAKE_CELL;
386 }
388 // we need to prevent accidental free it as it is freed when the tree_cell is
389 // cleaned up
390 to_application = NULL;
391 return out;
392}

References FAKE_CELL, okrb5_slice_to_tree_cell(), and to_application.

Here is the call graph for this function:

◆ nasl_okrb5_is_failure()

tree_cell * nasl_okrb5_is_failure ( lex_ctxt * lexic)

Returns 0 if the krb5 function was successful and 1 if it failed.

The nasl function has one optional parameter:

  • retval: the return value of the krb5 function. If the value is not defined, the return value of the last krb5 function is used.
Parameters
[in]lexicNASL lexer.
Returns
lex cell containing a number indicating success.

Definition at line 263 of file nasl_krb5.c.

264{
267 retc->x.i_val = result != O_KRB5_SUCCESS;
268 return retc;
269}
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition nasl_var.c:1094

References alloc_typed_cell(), CONST_INT, get_int_var_by_num(), TC::i_val, last_okrb5_result, O_KRB5_SUCCESS, and TC::x.

Here is the call graph for this function:

◆ nasl_okrb5_is_success()

tree_cell * nasl_okrb5_is_success ( lex_ctxt * lexic)

Returns 1 if the krb5 function was successful 0 otherwise.

The nasl function has one optional parameter:

  • retval: the return value of the krb5 function. If the value is not defined, the return value of the last krb5 function is used.
Parameters
[in]lexicNASL lexer.
Returns
lex cell containing a number indicating success.

Definition at line 242 of file nasl_krb5.c.

243{
246 retc->x.i_val = result == O_KRB5_SUCCESS;
247 return retc;
248}

References alloc_typed_cell(), CONST_INT, get_int_var_by_num(), TC::i_val, last_okrb5_result, O_KRB5_SUCCESS, and TC::x.

Here is the call graph for this function:

◆ okrb5_slice_to_tree_cell()

tree_cell * okrb5_slice_to_tree_cell ( struct OKrb5Slice * slice)
inlinestatic

Definition at line 371 of file nasl_krb5.c.

372{
374 retc->x.str_val = slice->data;
375 retc->size = slice->len;
376 return retc;
377}

References alloc_typed_cell(), CONST_DATA, OKrb5Slice::data, OKrb5Slice::len, TC::size, TC::str_val, and TC::x.

Referenced by nasl_okrb5_gss_session_key_context(), and nasl_okrb5_gss_update_context_out().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ cached_gss_context

◆ generated_config_path

char* generated_config_path = NULL
static

Definition at line 56 of file nasl_krb5.c.

Referenced by build_krb5_credential(), and nasl_okrb5_clean().

◆ gss_update_context_more

bool gss_update_context_more = false
static

◆ last_okrb5_result

◆ to_application

struct OKrb5Slice* to_application = NULL
static