audit_logging: Remove incorrect check for NT_STATUS_OBJECT_NAME_NOT_FOUND
[nivanova/samba-autobuild/.git] / lib / audit_logging / audit_logging.c
index 7da161cd2046d8098ac17c49a1779ac6b1ae84df..3a27eb6105f8c89044b075379b39c1a7d57d7669 100644 (file)
@@ -102,9 +102,45 @@ char* audit_get_timestamp(TALLOC_CTX *frame)
        return ts;
 }
 
-#ifdef HAVE_JANSSON
+/*
+ * @brief write an audit message to the audit logs.
+ *
+ * Write a human readable text audit message to the samba logs.
+ *
+ * @param prefix Text to be printed at the start of the log line
+ * @param message The content of the log line.
+ * @param debub_class The debug class to log the message with.
+ * @param debug_level The debug level to log the message with.
+ */
+void audit_log_human_text(const char* prefix,
+                         const char* message,
+                         int debug_class,
+                         int debug_level)
+{
+       DEBUGC(debug_class, debug_level, ("%s %s\n", prefix, message));
+}
 
-#include "system/time.h"
+#ifdef HAVE_JANSSON
+/*
+ * @brief write a json object to the samba audit logs.
+ *
+ * Write the json object to the audit logs as a formatted string
+ *
+ * @param prefix Text to be printed at the start of the log line
+ * @param message The content of the log line.
+ * @param debub_class The debug class to log the message with.
+ * @param debug_level The debug level to log the message with.
+ */
+void audit_log_json(const char* prefix,
+                   struct json_object* message,
+                   int debug_class,
+                   int debug_level)
+{
+       TALLOC_CTX *ctx = talloc_new(NULL);
+       char *s = json_to_string(ctx, message);
+       DEBUGC(debug_class, debug_level, ("JSON %s: %s\n", prefix, s));
+       TALLOC_FREE(ctx);
+}
 
 /*
  * @brief get a connection to the messaging event server.
@@ -137,7 +173,7 @@ static NTSTATUS get_event_server(
        if (!NT_STATUS_IS_OK(status)) {
                DBG_NOTICE(
                        "Failed to find '%s' registered on the message bus to "
-                       "send audit events to: %s\n",
+                       "send JSON audit events to: %s\n",
                        server_name,
                        nt_errstr(status));
                TALLOC_FREE(frame);
@@ -163,7 +199,7 @@ static NTSTATUS get_event_server(
        }
        DBG_NOTICE(
                "Failed to find '%s' registered on the message bus to "
-               "send audit events to: %s\n",
+               "send JSON audit events to: %s\n",
                server_name,
                nt_errstr(status));
        TALLOC_FREE(frame);
@@ -192,14 +228,18 @@ void audit_message_send(
        struct imessaging_context *msg_ctx,
        const char *server_name,
        uint32_t message_type,
-       const char *message)
+       struct json_object *message)
 {
        struct server_id event_server;
        NTSTATUS status;
-       DATA_BLOB message_blob = data_blob_string_const(message);
+
+       const char *message_string = NULL;
+       DATA_BLOB message_blob = data_blob_null;
+       TALLOC_CTX *ctx = talloc_new(NULL);
 
        if (msg_ctx == NULL) {
                DBG_DEBUG("No messaging context\n");
+               TALLOC_FREE(ctx);
                return;
        }
 
@@ -208,14 +248,16 @@ void audit_message_send(
         * messages may get lost
         */
        status = get_event_server(msg_ctx, server_name, &event_server);
-       if (!NT_STATUS_IS_OK(status) &&
-           !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
+       if (!NT_STATUS_IS_OK(status)) {
                DBG_ERR("get_event_server for %s returned (%s)\n",
                        server_name,
                        nt_errstr(status));
+               TALLOC_FREE(ctx);
                return;
        }
 
+       message_string = json_to_string(ctx, message);
+       message_blob = data_blob_string_const(message_string);
        status = imessaging_send(
                msg_ctx,
                event_server,
@@ -227,11 +269,11 @@ void audit_message_send(
         */
        if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
                status = get_event_server(msg_ctx, server_name, &event_server);
-               if (!NT_STATUS_IS_OK(status) &&
-                   !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
+               if (!NT_STATUS_IS_OK(status)) {
                        DBG_ERR("get_event_server for %s returned (%s)\n",
                                server_name,
                                nt_errstr(status));
+                       TALLOC_FREE(ctx);
                        return;
                }
                imessaging_send(
@@ -240,6 +282,7 @@ void audit_message_send(
                        message_type,
                        &message_blob);
        }
+       TALLOC_FREE(ctx);
 }
 
 /*
@@ -450,8 +493,8 @@ void json_assert_is_array(struct json_object *array) {
  *
  */
 void json_add_object(struct json_object *object,
-               const char* name,
-               struct json_object *value)
+                    const char* name,
+                    struct json_object *value)
 {
        int rc = 0;
        json_t *jv = NULL;
@@ -497,11 +540,10 @@ void json_add_object(struct json_object *object,
  * @param len the maximum number of characters to be copied.
  *
  */
-void json_add_stringn(
-       struct json_object *object,
-       const char *name,
-       const char *value,
-       const size_t len)
+void json_add_stringn(struct json_object *object,
+                     const char *name,
+                     const char *value,
+                     const size_t len)
 {
 
        int rc = 0;
@@ -569,7 +611,7 @@ void json_add_version(struct json_object *object, int major, int minor)
 void json_add_timestamp(struct json_object *object)
 {
        char buffer[40];        /* formatted time less usec and timezone */
-       char timestamp[50];     /* the formatted ISO 8601 time stamp     */
+       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                   */
@@ -622,10 +664,9 @@ void json_add_timestamp(struct json_object *object)
  * @param address the tsocket_address.
  *
  */
-void json_add_address(
-       struct json_object *object,
-       const char *name,
-       const struct tsocket_address *address)
+void json_add_address(struct json_object *object,
+                     const char *name,
+                     const struct tsocket_address *address)
 {
 
        if (object->error) {
@@ -661,10 +702,9 @@ void json_add_address(
  * @param sid the sid
  *
  */
-void json_add_sid(
-       struct json_object *object,
-       const char *name,
-       const struct dom_sid *sid)
+void json_add_sid(struct json_object *object,
+                 const char *name,
+                 const struct dom_sid *sid)
 {
 
        if (object->error) {
@@ -699,10 +739,9 @@ void json_add_sid(
  *
  *
  */
-void json_add_guid(
-       struct json_object *object,
-       const char *name,
-       const struct GUID *guid)
+void json_add_guid(struct json_object *object,
+                  const char *name,
+                  const struct GUID *guid)
 {
 
 
@@ -739,7 +778,8 @@ void json_add_guid(
  * @return A string representation of the object or NULL if the object
  *         is invalid.
  */
-char *json_to_string(TALLOC_CTX *mem_ctx, struct json_object *object)
+char *json_to_string(TALLOC_CTX *mem_ctx,
+                    struct json_object *object)
 {
        char *json = NULL;
        char *json_string = NULL;
@@ -768,4 +808,66 @@ char *json_to_string(TALLOC_CTX *mem_ctx, struct json_object *object)
 
        return json_string;
 }
+
+/*
+ * @brief get a json array named "name" from the json object.
+ *
+ * Get the array attribute named name, creating it if it does not exist.
+ *
+ * @param object the json object.
+ * @param name the name of the array attribute
+ *
+ * @return The array object, will be created if it did not exist.
+ */
+struct json_object json_get_array(struct json_object *object,
+                                 const char* name)
+{
+
+       struct json_object array = json_new_array();
+       json_t *a = NULL;
+
+       if (object->error) {
+               array.error = true;
+               return array;
+       }
+
+       a = json_object_get(object->root, name);
+       if (a == NULL) {
+               return array;
+       }
+       json_array_extend(array.root, a);
+
+       return array;
+}
+
+/*
+ * @brief get a json object named "name" from the json object.
+ *
+ * Get the object attribute named name, creating it if it does not exist.
+ *
+ * @param object the json object.
+ * @param name the name of the object attribute
+ *
+ * @return The object, will be created if it did not exist.
+ */
+struct json_object json_get_object(struct json_object *object,
+                                  const char* name)
+{
+
+       struct json_object o = json_new_object();
+       json_t *v = NULL;
+
+       if (object->error) {
+               o.error = true;
+               return o;
+       }
+
+       v = json_object_get(object->root, name);
+       if (v == NULL) {
+               return o;
+       }
+       json_object_update(o.root, v);
+
+       return o;
+}
 #endif