1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-25 17:57:42 +03:00

lib:audit_logging: Add function to add flags to a JSON message

This replaces a couple of calls to snprintf() in
log_authentication_event_json() and log_successful_authz_event_json()
respectively.

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
Joseph Sutton 2023-05-16 09:53:02 +12:00 committed by Andrew Bartlett
parent 89d30cdfe1
commit 4440f1db54
3 changed files with 59 additions and 14 deletions

View File

@ -149,7 +149,6 @@ static void log_authentication_event_json(
{
struct json_object wrapper = json_empty_object;
struct json_object authentication = json_empty_object;
char negotiate_flags[11];
char logon_id[19];
int rc = 0;
const char *clientDomain = ui->orig_client.domain_name ?
@ -257,12 +256,9 @@ static void log_authentication_event_json(
if (rc != 0) {
goto failure;
}
snprintf(negotiate_flags,
sizeof( negotiate_flags),
"0x%08X",
ui->netlogon_trust_account.negotiate_flags);
rc = json_add_string(
&authentication, "netlogonNegotiateFlags", negotiate_flags);
rc = json_add_flags32(
&authentication, "netlogonNegotiateFlags",
ui->netlogon_trust_account.negotiate_flags);
if (rc != 0) {
goto failure;
}
@ -368,7 +364,6 @@ static void log_successful_authz_event_json(
{
struct json_object wrapper = json_empty_object;
struct json_object authorization = json_empty_object;
char account_flags[11];
int rc = 0;
authorization = json_new_object();
@ -426,12 +421,7 @@ static void log_successful_authz_event_json(
if (rc != 0) {
goto failure;
}
snprintf(account_flags,
sizeof(account_flags),
"0x%08X",
session_info->info->acct_flags);
rc = json_add_string(&authorization, "accountFlags", account_flags);
rc = json_add_flags32(&authorization, "accountFlags", session_info->info->acct_flags);
if (rc != 0) {
goto failure;
}

View File

@ -907,6 +907,57 @@ int json_add_guid(struct json_object *object,
return ret;
}
/*
* @brief Add a hex-formatted string representation of a 32-bit integer to a
* json object.
*
* Add a hex-formatted string representation of a 32-bit flags integer to the
* object.
*
* "accountFlags":"0x12345678"
*
*
* @param object the JSON object to be updated.
* @param name the name.
* @param flags the flags.
*
* @return 0 the operation was successful
* -1 the operation failed
*
*
*/
int json_add_flags32(struct json_object *object,
const char *name,
const uint32_t flags)
{
int ret = 0;
char buf[sizeof("0x12345678")];
if (json_is_invalid(object)) {
DBG_ERR("Unable to add flags [%s], "
"target object is invalid\n",
name);
return JSON_ERROR;
}
ret = snprintf(buf, sizeof (buf), "0x%08X", flags);
if (ret != sizeof (buf) - 1) {
DBG_ERR("Unable to format flags [%s] value [0x%08X]\n",
name,
flags);
return JSON_ERROR;
}
ret = json_add_string(object, name, buf);
if (ret != 0) {
DBG_ERR("Unable to add flags [%s] value [%s]\n",
name,
buf);
}
return ret;
}
/*
* @brief Replaces the object for a given key with a given json object.
*

View File

@ -87,6 +87,10 @@ _WARN_UNUSED_RESULT_ int json_add_guid(struct json_object *object,
const char *name,
const struct GUID *guid);
_WARN_UNUSED_RESULT_ int json_add_flags32(struct json_object *object,
const char *name,
uint32_t flags);
_WARN_UNUSED_RESULT_ int json_update_object(struct json_object *object,
const char *key,
struct json_object *new_obj);