From 4440f1db54b7ad54b7a4920ac67236d1d8605353 Mon Sep 17 00:00:00 2001 From: Joseph Sutton Date: Tue, 16 May 2023 09:53:02 +1200 Subject: 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 Reviewed-by: Andrew Bartlett --- auth/auth_log.c | 18 +++----------- lib/audit_logging/audit_logging.c | 51 +++++++++++++++++++++++++++++++++++++++ lib/audit_logging/audit_logging.h | 4 +++ 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/auth/auth_log.c b/auth/auth_log.c index 730e5b6080e..c0506823008 100644 --- a/auth/auth_log.c +++ b/auth/auth_log.c @@ -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; } diff --git a/lib/audit_logging/audit_logging.c b/lib/audit_logging/audit_logging.c index 671653fdcef..65d6f3915e9 100644 --- a/lib/audit_logging/audit_logging.c +++ b/lib/audit_logging/audit_logging.c @@ -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. * diff --git a/lib/audit_logging/audit_logging.h b/lib/audit_logging/audit_logging.h index eb7c103944d..d3eca06b883 100644 --- a/lib/audit_logging/audit_logging.h +++ b/lib/audit_logging/audit_logging.h @@ -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); -- cgit v1.2.1