From: Joseph Sutton Date: Mon, 15 May 2023 21:57:12 +0000 (+1200) Subject: lib:audit_logging: Add function to add a formatted time value to a JSON message X-Git-Tag: talloc-2.4.1~641 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d7b68236ecf8692f276d63d29e475c3b1ddb290d;p=thirdparty%2Fsamba.git 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 Reviewed-by: Andrew Bartlett --- 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,13 +770,48 @@ 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 * 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,