python: Allow forced signing via smb.SMB()
[garming/samba-autobuild/.git] / auth / auth_log.c
index fec77077bc94c7ff44a75ebfc90bd35c7c1eac7d..67d23c12a1b852cc7670c326f9386c48a23be9af 100644 (file)
  * Debug log levels for authentication logging (these both map to
  * LOG_NOTICE in syslog)
  */
-#define AUTH_SUCCESS_LEVEL 4
-#define AUTHZ_SUCCESS_LEVEL 5
 #define AUTH_FAILURE_LEVEL 2
+#define AUTH_SUCCESS_LEVEL 3
+#define AUTHZ_SUCCESS_LEVEL 4
+
+/* 5 is used for both authentication and authorization */
+#define AUTH_ANONYMOUS_LEVEL 5
+#define AUTHZ_ANONYMOUS_LEVEL 5
+
+#define AUTHZ_JSON_TYPE "Authorization"
+#define AUTH_JSON_TYPE  "Authentication"
+
+/*
+ * JSON message version numbers
+ *
+ * If adding a field increment the minor version
+ * If removing or changing the format/meaning of a field
+ * increment the major version.
+ */
+#define AUTH_MAJOR 1
+#define AUTH_MINOR 0
+#define AUTHZ_MAJOR 1
+#define AUTHZ_MINOR 1
 
 #include "includes.h"
 #include "../lib/tsocket/tsocket.h"
 #include "common_auth.h"
 #include "lib/util/util_str_escape.h"
 #include "libcli/security/dom_sid.h"
+#include "libcli/security/security_token.h"
+#include "librpc/gen_ndr/server_id.h"
+#include "source4/lib/messaging/messaging.h"
+#include "source4/lib/messaging/irpc.h"
+#include "lib/util/server_id_db.h"
+#include "lib/param/param.h"
+#include "librpc/ndr/libndr.h"
+#include "lib/audit_logging/audit_logging.h"
 
 /*
- * Get a human readable timestamp.
- *
- * Returns the current time formatted as
- *  "Tue, 14 Mar 2017 08:38:42.209028 NZDT"
+ * Determine the type of the password supplied for the
+ * authorisation attempt.
  *
- * The returned string is allocated by talloc in the supplied context.
- * It is the callers responsibility to free it.
+ */
+static const char* get_password_type(const struct auth_usersupplied_info *ui);
+
+#ifdef HAVE_JANSSON
+
+#include <jansson.h>
+#include "system/time.h"
+
+/*
+ * Write the json object to the debug logs.
  *
  */
-static const char* get_timestamp( TALLOC_CTX *frame )
+static void log_json(struct imessaging_context *msg_ctx,
+                    struct loadparm_context *lp_ctx,
+                    struct json_object *object,
+                    const char *type,
+                    int debug_class,
+                    int debug_level)
 {
-       char buffer[40];        /* formatted time less usec and timezone */
-       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       */
-       const char * ts;        /* formatted time stamp                  */
-
-       r = gettimeofday(&tv, NULL);
-       if (r) {
-               DBG_ERR("Unable to get time of day: (%d) %s\n",
-                       errno,
-                       strerror( errno));
-               return NULL;
+       audit_log_json(type, object, debug_class, debug_level);
+       if (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx)) {
+               audit_message_send(msg_ctx,
+                                  AUTH_EVENT_NAME,
+                                  MSG_AUTH_LOG,
+                                  object);
        }
+}
 
-       tm_info = localtime(&tv.tv_sec);
-       if (tm_info == NULL) {
-               DBG_ERR("Unable to determine local time\n");
-               return NULL;
+/*
+ * Write a machine parsable json formatted authentication log entry.
+ *
+ * IF removing or changing the format/meaning of a field please update the
+ *    major version number AUTH_MAJOR
+ *
+ * IF adding a new field please update the minor version number AUTH_MINOR
+ *
+ *  To process the resulting log lines from the commend line use jq to
+ *  parse the json.
+ *
+ *  grep "JSON Authentication" log file |
+ *  sed 's;^[^{]*;;' |
+ * jq -rc  '"\(.timestamp)\t\(.Authentication.status)\t
+ *           \(.Authentication.clientDomain)\t
+ *           \(.Authentication.clientAccount)
+ *           \t\(.Authentication.workstation)
+ *           \t\(.Authentication.remoteAddress)
+ *           \t\(.Authentication.localAddress)"'
+ */
+static void log_authentication_event_json(
+       struct imessaging_context *msg_ctx,
+       struct loadparm_context *lp_ctx,
+       const struct timeval *start_time,
+       const struct auth_usersupplied_info *ui,
+       NTSTATUS status,
+       const char *domain_name,
+       const char *account_name,
+       const char *unix_username,
+       struct dom_sid *sid,
+       int debug_level)
+{
+       struct json_object wrapper = json_new_object();
+       struct json_object authentication;
+       char negotiate_flags[11];
+
+       json_add_timestamp(&wrapper);
+       json_add_string(&wrapper, "type", AUTH_JSON_TYPE);
+
+       authentication = json_new_object();
+       json_add_version(&authentication, AUTH_MAJOR, AUTH_MINOR);
+       json_add_string(&authentication, "status", nt_errstr(status));
+       json_add_address(&authentication, "localAddress", ui->local_host);
+       json_add_address(&authentication, "remoteAddress", ui->remote_host);
+       json_add_string(&authentication,
+                       "serviceDescription",
+                       ui->service_description);
+       json_add_string(&authentication,
+                       "authDescription",
+                       ui->auth_description);
+       json_add_string(&authentication,
+                       "clientDomain",
+                       ui->client.domain_name);
+       json_add_string(&authentication,
+                       "clientAccount",
+                       ui->client.account_name);
+       json_add_string(&authentication,
+                       "workstation",
+                       ui->workstation_name);
+       json_add_string(&authentication, "becameAccount", account_name);
+       json_add_string(&authentication, "becameDomain", domain_name);
+       json_add_sid(&authentication, "becameSid", sid);
+       json_add_string(&authentication,
+                       "mappedAccount",
+                       ui->mapped.account_name);
+       json_add_string(&authentication,
+                       "mappedDomain",
+                       ui->mapped.domain_name);
+       json_add_string(&authentication,
+                       "netlogonComputer",
+                       ui->netlogon_trust_account.computer_name);
+       json_add_string(&authentication,
+                       "netlogonTrustAccount",
+                       ui->netlogon_trust_account.account_name);
+       snprintf(negotiate_flags,
+                sizeof( negotiate_flags),
+                "0x%08X",
+                ui->netlogon_trust_account.negotiate_flags);
+       json_add_string(&authentication,
+                       "netlogonNegotiateFlags",
+                       negotiate_flags);
+       json_add_int(&authentication,
+                    "netlogonSecureChannelType",
+                    ui->netlogon_trust_account.secure_channel_type);
+       json_add_sid(&authentication,
+                    "netlogonTrustAccountSid",
+                    ui->netlogon_trust_account.sid);
+       json_add_string(&authentication, "passwordType", get_password_type(ui));
+       json_add_object(&wrapper, AUTH_JSON_TYPE, &authentication);
+
+       /*
+        * While not a general-purpose profiling solution this will
+        * assist some to determine how long NTLM and KDC
+        * authentication takes once this process can handle it.  This
+        * covers transactions elsewhere but not (eg) the delay while
+        * this is waiting unread on the input socket.
+        */
+       if (start_time != NULL) {
+               struct timeval current_time = timeval_current();
+               uint64_t duration =  usec_time_diff(&current_time,
+                                                   start_time);
+               json_add_int(&authentication,
+                            "duration",
+                            duration);
        }
 
-       strftime(buffer, sizeof(buffer)-1, "%a, %d %b %Y %H:%M:%S", tm_info);
-       strftime(tz, sizeof(tz)-1, "%Z", tm_info);
-       ts = talloc_asprintf(frame, "%s.%06ld %s", buffer, tv.tv_usec, tz);
-       if (ts == NULL) {
-               DBG_ERR("Out of memory formatting time stamp\n");
+       log_json(msg_ctx,
+                lp_ctx,
+                &wrapper,
+                AUTH_JSON_TYPE,
+                DBGC_AUTH_AUDIT,
+                debug_level);
+       json_free(&wrapper);
+}
+
+/*
+ * Log details of a successful authorization to a service,
+ * in a machine parsable json format
+ *
+ * IF removing or changing the format/meaning of a field please update the
+ *    major version number AUTHZ_MAJOR
+ *
+ * IF adding a new field please update the minor version number AUTHZ_MINOR
+ *
+ *  To process the resulting log lines from the commend line use jq to
+ *  parse the json.
+ *
+ *  grep "JSON Authentication" log_file |\
+ *  sed "s;^[^{]*;;" |\
+ *  jq -rc '"\(.timestamp)\t
+ *           \(.Authorization.domain)\t
+ *           \(.Authorization.account)\t
+ *           \(.Authorization.remoteAddress)"'
+ *
+ */
+static void log_successful_authz_event_json(
+       struct imessaging_context *msg_ctx,
+       struct loadparm_context *lp_ctx,
+       const struct tsocket_address *remote,
+       const struct tsocket_address *local,
+       const char *service_description,
+       const char *auth_type,
+       const char *transport_protection,
+       struct auth_session_info *session_info,
+       int debug_level)
+{
+       struct json_object wrapper = json_new_object();
+       struct json_object authorization;
+       char account_flags[11];
+
+       json_add_timestamp(&wrapper);
+       json_add_string(&wrapper, "type", AUTHZ_JSON_TYPE);
+       authorization = json_new_object();
+       json_add_version(&authorization, AUTHZ_MAJOR, AUTHZ_MINOR);
+       json_add_address(&authorization, "localAddress", local);
+       json_add_address(&authorization, "remoteAddress", remote);
+       json_add_string(&authorization,
+                       "serviceDescription",
+                       service_description);
+       json_add_string(&authorization, "authType", auth_type);
+       json_add_string(&authorization,
+                       "domain",
+                       session_info->info->domain_name);
+       json_add_string(&authorization,
+                       "account",
+                       session_info->info->account_name);
+       json_add_sid(&authorization,
+                    "sid",
+                    &session_info->security_token->sids[0]);
+       json_add_guid(&authorization,
+                     "sessionId",
+                     &session_info->unique_session_token);
+       json_add_string(&authorization,
+                       "logonServer",
+                       session_info->info->logon_server);
+       json_add_string(&authorization,
+                       "transportProtection",
+                       transport_protection);
+
+       snprintf(account_flags,
+                sizeof(account_flags),
+                "0x%08X",
+                session_info->info->acct_flags);
+       json_add_string(&authorization, "accountFlags", account_flags);
+       json_add_object(&wrapper, AUTHZ_JSON_TYPE, &authorization);
+
+       log_json(msg_ctx,
+                lp_ctx,
+                &wrapper,
+                AUTHZ_JSON_TYPE,
+                DBGC_AUTH_AUDIT,
+                debug_level);
+       json_free(&wrapper);
+}
+
+#else
+
+static void log_no_json(struct imessaging_context *msg_ctx,
+                        struct loadparm_context *lp_ctx)
+{
+       if (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx)) {
+               static bool auth_event_logged = false;
+               if (auth_event_logged == false) {
+                       auth_event_logged = true;
+                       DBG_ERR("auth event notification = true but Samba was "
+                               "not compiled with jansson\n");
+               }
+       } else {
+               static bool json_logged = false;
+               if (json_logged == false) {
+                       json_logged = true;
+                       DBG_NOTICE("JSON auth logs not available unless "
+                                  "compiled with jansson\n");
+               }
        }
-       return ts;
+
+       return;
+}
+
+static void log_authentication_event_json(
+       struct imessaging_context *msg_ctx,
+       struct loadparm_context *lp_ctx,
+       const struct timeval *start_time,
+       const struct auth_usersupplied_info *ui,
+       NTSTATUS status,
+       const char *domain_name,
+       const char *account_name,
+       const char *unix_username,
+       struct dom_sid *sid,
+       int debug_level)
+{
+       log_no_json(msg_ctx, lp_ctx);
+       return;
+}
+
+static void log_successful_authz_event_json(
+       struct imessaging_context *msg_ctx,
+       struct loadparm_context *lp_ctx,
+       const struct tsocket_address *remote,
+       const struct tsocket_address *local,
+       const char *service_description,
+       const char *auth_type,
+       const char *transport_protection,
+       struct auth_session_info *session_info,
+       int debug_level)
+{
+       log_no_json(msg_ctx, lp_ctx);
+       return;
 }
 
+#endif
+
 /*
  * Determine the type of the password supplied for the
  * authorisation attempt.
@@ -84,9 +353,23 @@ static const char* get_password_type(const struct auth_usersupplied_info *ui)
 
        const char *password_type = NULL;
 
-       if (ui->password_state == AUTH_PASSWORD_RESPONSE &&
-           (ui->logon_parameters & MSV1_0_ALLOW_MSVCHAPV2) &&
-           ui->password.response.nt.length == 24) {
+       if (ui->password_type != NULL) {
+               password_type = ui->password_type;
+       } else if (ui->auth_description != NULL &&
+                  strncmp("ServerAuthenticate", ui->auth_description, 18) == 0)
+       {
+               if (ui->netlogon_trust_account.negotiate_flags
+                   & NETLOGON_NEG_SUPPORTS_AES) {
+                       password_type = "HMAC-SHA256";
+               } else if (ui->netlogon_trust_account.negotiate_flags
+                          & NETLOGON_NEG_STRONG_KEYS) {
+                       password_type = "HMAC-MD5";
+               } else {
+                       password_type = "DES";
+               }
+       } else if (ui->password_state == AUTH_PASSWORD_RESPONSE &&
+                  (ui->logon_parameters & MSV1_0_ALLOW_MSVCHAPV2) &&
+                  ui->password.response.nt.length == 24) {
                password_type = "MSCHAPv2";
        } else if ((ui->logon_parameters & MSV1_0_CLEARTEXT_PASSWORD_SUPPLIED)
                   || (ui->password_state == AUTH_PASSWORD_PLAIN)) {
@@ -111,16 +394,17 @@ static const char* get_password_type(const struct auth_usersupplied_info *ui)
 }
 
 /*
- * Log details of an authentication attempt.
- * Successful and unsuccessful attempts are logged.
+ * Write a human readable authentication log entry.
  *
  */
-void log_authentication_event(const struct auth_usersupplied_info *ui,
-                             NTSTATUS status,
-                             const char *domain_name,
-                             const char *account_name,
-                             const char *unix_username,
-                             struct dom_sid *sid)
+static void log_authentication_event_human_readable(
+       const struct auth_usersupplied_info *ui,
+       NTSTATUS status,
+       const char *domain_name,
+       const char *account_name,
+       const char *unix_username,
+       struct dom_sid *sid,
+       int debug_level)
 {
        TALLOC_CTX *frame = NULL;
 
@@ -133,17 +417,11 @@ void log_authentication_event(const struct auth_usersupplied_info *ui,
        char *logon_line = NULL;
        const char *password_type = NULL;
 
-       /* set the log level */
-       int  level = NT_STATUS_IS_OK(status) ? AUTH_FAILURE_LEVEL : AUTH_SUCCESS_LEVEL;
-       if (!CHECK_DEBUGLVLC( DBGC_AUTH_AUDIT, level)) {
-               return;
-       }
-
        frame = talloc_stackframe();
 
-       password_type = get_password_type( ui);
+       password_type = get_password_type(ui);
        /* Get the current time */
-        ts = get_timestamp(frame);
+        ts = audit_get_timestamp(frame);
 
        /* Only log the NETLOGON details if they are present */
        if (ui->netlogon_trust_account.computer_name ||
@@ -158,7 +436,7 @@ void log_authentication_event(const struct auth_usersupplied_info *ui,
        }
 
        remote = tsocket_address_string(ui->remote_host, frame);
-       local  = tsocket_address_string(ui->local_host, frame);
+       local = tsocket_address_string(ui->local_host, frame);
 
        if (NT_STATUS_IS_OK(status)) {
                char sid_buf[DOM_SID_STR_BUFLEN];
@@ -170,14 +448,15 @@ void log_authentication_event(const struct auth_usersupplied_info *ui,
                                             log_escape(frame, account_name),
                                             sid_buf);
        } else {
-               logon_line = talloc_asprintf(frame,
-                                            " mapped to [%s]\\[%s].",
-                                            log_escape(frame, ui->mapped.domain_name),
-                                            log_escape(frame, ui->mapped.account_name));
+               logon_line = talloc_asprintf(
+                               frame,
+                               " mapped to [%s]\\[%s].",
+                               log_escape(frame, ui->mapped.domain_name),
+                               log_escape(frame, ui->mapped.account_name));
        }
 
-       DEBUGC( DBGC_AUTH_AUDIT, level, (
-               "Auth: [%s,%s] user [%s]\\[%s]"
+       DEBUGC(DBGC_AUTH_AUDIT, debug_level,
+              ("Auth: [%s,%s] user [%s]\\[%s]"
                " at [%s] with [%s] status [%s]"
                " workstation [%s] remote host [%s]"
                "%s local host [%s]"
@@ -188,32 +467,84 @@ void log_authentication_event(const struct auth_usersupplied_info *ui,
                log_escape(frame, ui->client.account_name),
                ts,
                password_type,
-               nt_errstr( status),
+               nt_errstr(status),
                log_escape(frame, ui->workstation_name),
                remote,
                logon_line,
                local,
                nl ? nl : ""
-               ));
+       ));
 
        talloc_free(frame);
 }
 
-
 /*
- * Log details of a successful authorization to a service.
- *
- * Only successful authorizations are logged.  For clarity:
- * - NTLM bad passwords will be recorded by the above
- * - Kerberos decrypt failures need to be logged in gensec_gssapi et al
+ * Log details of an authentication attempt.
+ * Successful and unsuccessful attempts are logged.
  *
- * The service may later refuse authorization due to an ACL.
+ * NOTE: msg_ctx and lp_ctx is optional, but when supplied allows streaming the
+ * authentication events over the message bus.
+ */
+void log_authentication_event(
+       struct imessaging_context *msg_ctx,
+       struct loadparm_context *lp_ctx,
+       const struct timeval *start_time,
+       const struct auth_usersupplied_info *ui,
+       NTSTATUS status,
+       const char *domain_name,
+       const char *account_name,
+       const char *unix_username,
+       struct dom_sid *sid)
+{
+       /* set the log level */
+       int debug_level = AUTH_FAILURE_LEVEL;
+
+       if (NT_STATUS_IS_OK(status)) {
+               debug_level = AUTH_SUCCESS_LEVEL;
+               if (dom_sid_equal(sid, &global_sid_Anonymous)) {
+                       debug_level = AUTH_ANONYMOUS_LEVEL;
+               }
+       }
+
+       if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT, debug_level)) {
+               log_authentication_event_human_readable(ui,
+                                                       status,
+                                                       domain_name,
+                                                       account_name,
+                                                       unix_username,
+                                                       sid,
+                                                       debug_level);
+       }
+       if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT_JSON, debug_level) ||
+           (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx))) {
+               log_authentication_event_json(msg_ctx,
+                                             lp_ctx,
+                                             start_time,
+                                             ui,
+                                             status,
+                                             domain_name,
+                                             account_name,
+                                             unix_username,
+                                             sid,
+                                             debug_level);
+       }
+}
+
+
+
+/*
+ * Log details of a successful authorization to a service,
+ * in a human readable format.
  *
  */
-void log_successful_authz_event(const struct tsocket_address *remote,
-                               const struct tsocket_address *local,
-                               const char *service_description,
-                               struct auth_session_info *session_info)
+static void log_successful_authz_event_human_readable(
+       const struct tsocket_address *remote,
+       const struct tsocket_address *local,
+       const char *service_description,
+       const char *auth_type,
+       const char *transport_protection,
+       struct auth_session_info *session_info,
+       int debug_level)
 {
        TALLOC_CTX *frame = NULL;
 
@@ -222,27 +553,25 @@ void log_successful_authz_event(const struct tsocket_address *remote,
        char *local_str = NULL;      /* formatted local host        */
        char sid_buf[DOM_SID_STR_BUFLEN];
 
-       /* set the log level */
-       if (!CHECK_DEBUGLVLC( DBGC_AUTH_AUDIT, AUTHZ_SUCCESS_LEVEL)) {
-               return;
-       }
-
        frame = talloc_stackframe();
 
        /* Get the current time */
-        ts = get_timestamp(frame);
+        ts = audit_get_timestamp(frame);
 
        remote_str = tsocket_address_string(remote, frame);
-       local_str  = tsocket_address_string(local, frame);
+       local_str = tsocket_address_string(local, frame);
 
-       dom_sid_string_buf(&session_info->security_token->sids[0], sid_buf, sizeof(sid_buf));
+       dom_sid_string_buf(&session_info->security_token->sids[0],
+                          sid_buf,
+                          sizeof(sid_buf));
 
-       DEBUGC( DBGC_AUTH_AUDIT, AUTHZ_SUCCESS_LEVEL, (
-               "Successful AuthZ: [%s] user [%s]\\[%s] [%s]"
+       DEBUGC(DBGC_AUTH_AUDIT, debug_level,
+              ("Successful AuthZ: [%s,%s] user [%s]\\[%s] [%s]"
                " at [%s]"
                " Remote host [%s]"
                " local host [%s]\n",
                service_description,
+               auth_type,
                log_escape(frame, session_info->info->domain_name),
                log_escape(frame, session_info->info->account_name),
                sid_buf,
@@ -252,3 +581,54 @@ void log_successful_authz_event(const struct tsocket_address *remote,
 
        talloc_free(frame);
 }
+
+/*
+ * Log details of a successful authorization to a service.
+ *
+ * Only successful authorizations are logged.  For clarity:
+ * - NTLM bad passwords will be recorded by log_authentication_event
+ * - Kerberos decrypt failures need to be logged in gensec_gssapi et al
+ *
+ * The service may later refuse authorization due to an ACL.
+ *
+ * NOTE: msg_ctx and lp_ctx is optional, but when supplied allows streaming the
+ * authentication events over the message bus.
+ */
+void log_successful_authz_event(
+       struct imessaging_context *msg_ctx,
+       struct loadparm_context *lp_ctx,
+       const struct tsocket_address *remote,
+       const struct tsocket_address *local,
+       const char *service_description,
+       const char *auth_type,
+       const char *transport_protection,
+       struct auth_session_info *session_info)
+{
+       int debug_level = AUTHZ_SUCCESS_LEVEL;
+
+       /* set the log level */
+       if (security_token_is_anonymous(session_info->security_token)) {
+               debug_level = AUTH_ANONYMOUS_LEVEL;
+       }
+
+       if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT, debug_level)) {
+               log_successful_authz_event_human_readable(remote,
+                                                         local,
+                                                         service_description,
+                                                         auth_type,
+                                                         transport_protection,
+                                                         session_info,
+                                                         debug_level);
+       }
+       if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT_JSON, debug_level) ||
+           (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx))) {
+               log_successful_authz_event_json(msg_ctx, lp_ctx,
+                                               remote,
+                                               local,
+                                               service_description,
+                                               auth_type,
+                                               transport_protection,
+                                               session_info,
+                                               debug_level);
+       }
+}