1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

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>
This commit is contained in:
Joseph Sutton 2023-05-16 09:57:12 +12:00 committed by Andrew Bartlett
parent 0080148483
commit d7b68236ec
2 changed files with 44 additions and 17 deletions

View File

@ -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
*

View File

@ -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,