summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Sutton <josephsutton@catalyst.net.nz>2023-05-16 09:53:02 +1200
committerAndrew Bartlett <abartlet@samba.org>2023-05-18 01:03:37 +0000
commit4440f1db54b7ad54b7a4920ac67236d1d8605353 (patch)
tree6895220fdde9d7af80f8466780d4754fdf21e655
parent89d30cdfe164e441d0b565e02ea9422eaae789fb (diff)
downloadsamba-4440f1db54b7ad54b7a4920ac67236d1d8605353.tar.gz
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>
-rw-r--r--auth/auth_log.c18
-rw-r--r--lib/audit_logging/audit_logging.c51
-rw-r--r--lib/audit_logging/audit_logging.h4
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
@@ -908,6 +908,57 @@ int json_add_guid(struct json_object *object,
}
/*
+ * @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.
*
* If key already exists, the value will be replaced. Otherwise the given
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);