summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Sutton <josephsutton@catalyst.net.nz>2023-05-16 09:57:12 +1200
committerAndrew Bartlett <abartlet@samba.org>2023-05-18 01:03:37 +0000
commitd7b68236ecf8692f276d63d29e475c3b1ddb290d (patch)
tree609055ffe2030fac1e6edf6428b1037b00b2c653
parent0080148483c2972393d33bf1f2c7dbb248bbb9c0 (diff)
downloadsamba-d7b68236ecf8692f276d63d29e475c3b1ddb290d.tar.gz
lib:audit_logging: Add function to add a formatted time value to a JSON message
json_add_timestamp() is limited to adding a ‘timestamp’ field with the current time. The new function can add an arbitrary timestamp with an arbitrary field name. Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
-rw-r--r--lib/audit_logging/audit_logging.c60
-rw-r--r--lib/audit_logging/audit_logging.h1
2 files changed, 44 insertions, 17 deletions
diff --git a/lib/audit_logging/audit_logging.c b/lib/audit_logging/audit_logging.c
index a7e56b7b6be..25f93d02799 100644
--- a/lib/audit_logging/audit_logging.c
+++ b/lib/audit_logging/audit_logging.c
@@ -730,37 +730,28 @@ int json_add_version(struct json_object *object, int major, int minor)
/*
* @brief add an ISO 8601 timestamp to the object.
*
- * Add the current date and time as a timestamp in ISO 8601 format
- * to a JSON object
+ * Add a date and time as a timestamp in ISO 8601 format to a JSON object
*
- * "timestamp":"2017-03-06T17:18:04.455081+1300"
+ * "time":"2017-03-06T17:18:04.455081+1300"
*
*
* @param object the JSON object to be updated.
+ * @param name the name.
+ * @param time the value to set.
*
* @return 0 the operation was successful
* -1 the operation failed
*/
-int json_add_timestamp(struct json_object *object)
+int json_add_time(struct json_object *object, const char *name, const struct timeval tv)
{
char buffer[40]; /* formatted time less usec and timezone */
char timestamp[65]; /* the formatted ISO 8601 time stamp */
char tz[10]; /* formatted time zone */
struct tm* tm_info; /* current local time */
- struct timeval tv; /* current system time */
- int r; /* response code from gettimeofday */
int ret; /* return code from json operations */
if (json_is_invalid(object)) {
- DBG_ERR("Unable to add time stamp, target object is invalid\n");
- return JSON_ERROR;
- }
-
- r = gettimeofday(&tv, NULL);
- if (r) {
- DBG_ERR("Unable to get time of day: (%d) %s\n",
- errno,
- strerror(errno));
+ DBG_ERR("Unable to add time, target object is invalid\n");
return JSON_ERROR;
}
@@ -779,14 +770,49 @@ int json_add_timestamp(struct json_object *object)
buffer,
tv.tv_usec,
tz);
- ret = json_add_string(object, "timestamp", timestamp);
+ ret = json_add_string(object, name, timestamp);
if (ret != 0) {
- DBG_ERR("Unable to add time stamp to JSON object\n");
+ DBG_ERR("Unable to add time to JSON object\n");
}
return ret;
}
/*
+ * @brief add an ISO 8601 timestamp to the object.
+ *
+ * Add the current date and time as a timestamp in ISO 8601 format
+ * to a JSON object
+ *
+ * "timestamp":"2017-03-06T17:18:04.455081+1300"
+ *
+ *
+ * @param object the JSON object to be updated.
+ *
+ * @return 0 the operation was successful
+ * -1 the operation failed
+ */
+int json_add_timestamp(struct json_object *object)
+{
+ struct timeval tv; /* current system time */
+ int r; /* response code from gettimeofday */
+
+ if (json_is_invalid(object)) {
+ DBG_ERR("Unable to add time stamp, target object is invalid\n");
+ return JSON_ERROR;
+ }
+
+ r = gettimeofday(&tv, NULL);
+ if (r) {
+ DBG_ERR("Unable to get time of day: (%d) %s\n",
+ errno,
+ strerror(errno));
+ return JSON_ERROR;
+ }
+
+ return json_add_time(object, "timestamp", tv);
+}
+
+/*
*@brief Add a tsocket_address to a JSON object
*
* Add the string representation of a Samba tsocket_address to the object.
diff --git a/lib/audit_logging/audit_logging.h b/lib/audit_logging/audit_logging.h
index 2f0935f4c5b..594931ec8ae 100644
--- a/lib/audit_logging/audit_logging.h
+++ b/lib/audit_logging/audit_logging.h
@@ -78,6 +78,7 @@ _WARN_UNUSED_RESULT_ int json_add_stringn(struct json_object *object,
_WARN_UNUSED_RESULT_ int json_add_version(struct json_object *object,
int major,
int minor);
+_WARN_UNUSED_RESULT_ int json_add_time(struct json_object *object, const char *name, struct timeval tv);
_WARN_UNUSED_RESULT_ int json_add_timestamp(struct json_object *object);
_WARN_UNUSED_RESULT_ int json_add_address(
struct json_object *object,