diff options
author | Joseph Sutton <josephsutton@catalyst.net.nz> | 2023-05-16 09:53:02 +1200 |
---|---|---|
committer | Andrew Bartlett <abartlet@samba.org> | 2023-05-18 01:03:37 +0000 |
commit | 4440f1db54b7ad54b7a4920ac67236d1d8605353 (patch) | |
tree | 6895220fdde9d7af80f8466780d4754fdf21e655 /lib/audit_logging | |
parent | 89d30cdfe164e441d0b565e02ea9422eaae789fb (diff) | |
download | samba-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>
Diffstat (limited to 'lib/audit_logging')
-rw-r--r-- | lib/audit_logging/audit_logging.c | 51 | ||||
-rw-r--r-- | lib/audit_logging/audit_logging.h | 4 |
2 files changed, 55 insertions, 0 deletions
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); |