3 Authentication and authorization logging
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2017
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * Debug log levels for authentication logging (these both map to
23 * LOG_NOTICE in syslog)
25 #define AUTH_FAILURE_LEVEL 2
26 #define AUTH_SUCCESS_LEVEL 3
27 #define AUTHZ_SUCCESS_LEVEL 4
29 /* 5 is used for both authentication and authorization */
30 #define AUTH_ANONYMOUS_LEVEL 5
31 #define AUTHZ_ANONYMOUS_LEVEL 5
33 #define AUTHZ_JSON_TYPE "Authorization"
34 #define AUTH_JSON_TYPE "Authentication"
37 * JSON message version numbers
39 * If adding a field increment the minor version
40 * If removing or changing the format/meaning of a field
41 * increment the major version.
49 #include "../lib/tsocket/tsocket.h"
50 #include "common_auth.h"
51 #include "lib/util/util_str_escape.h"
52 #include "libcli/security/dom_sid.h"
53 #include "libcli/security/security_token.h"
54 #include "librpc/gen_ndr/server_id.h"
55 #include "source4/lib/messaging/messaging.h"
56 #include "source4/lib/messaging/irpc.h"
57 #include "lib/util/server_id_db.h"
58 #include "lib/param/param.h"
59 #include "librpc/ndr/libndr.h"
60 #include "librpc/gen_ndr/windows_event_ids.h"
61 #include "lib/audit_logging/audit_logging.h"
64 * Determine the type of the password supplied for the
65 * authorisation attempt.
68 static const char* get_password_type(const struct auth_usersupplied_info *ui);
73 #include "system/time.h"
76 * Write the json object to the debug logs.
79 static void log_json(struct imessaging_context *msg_ctx,
80 struct loadparm_context *lp_ctx,
81 struct json_object *object,
85 audit_log_json(object, debug_class, debug_level);
86 if (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx)) {
87 audit_message_send(msg_ctx,
95 * Determine the Windows logon type for the current authorisation attempt.
97 * Currently Samba only supports
99 * 2 Interactive A user logged on to this computer.
100 * 3 Network A user or computer logged on to this computer from
102 * 8 NetworkCleartext A user logged on to this computer from the network.
103 * The user's password was passed to the authentication
104 * package in its unhashed form.
107 static enum event_logon_type get_logon_type(
108 const struct auth_usersupplied_info *ui)
110 if ((ui->logon_parameters & MSV1_0_CLEARTEXT_PASSWORD_SUPPLIED)
111 || (ui->password_state == AUTH_PASSWORD_PLAIN)) {
112 return EVT_LOGON_NETWORK_CLEAR_TEXT;
113 } else if (ui->flags & USER_INFO_INTERACTIVE_LOGON) {
114 return EVT_LOGON_INTERACTIVE;
116 return EVT_LOGON_NETWORK;
120 * Write a machine parsable json formatted authentication log entry.
122 * IF removing or changing the format/meaning of a field please update the
123 * major version number AUTH_MAJOR
125 * IF adding a new field please update the minor version number AUTH_MINOR
127 * To process the resulting log lines from the commend line use jq to
130 * grep "^ {" log file |
131 * jq -rc '"\(.timestamp)\t\(.Authentication.status)\t
132 * \(.Authentication.clientDomain)\t
133 * \(.Authentication.clientAccount)
134 * \t\(.Authentication.workstation)
135 * \t\(.Authentication.remoteAddress)
136 * \t\(.Authentication.localAddress)"'
138 static void log_authentication_event_json(
139 struct imessaging_context *msg_ctx,
140 struct loadparm_context *lp_ctx,
141 const struct timeval *start_time,
142 const struct auth_usersupplied_info *ui,
144 const char *domain_name,
145 const char *account_name,
147 enum event_id_type event_id,
150 struct json_object wrapper = json_empty_object;
151 struct json_object authentication = json_empty_object;
152 char negotiate_flags[11];
156 authentication = json_new_object();
157 if (json_is_invalid(&authentication)) {
160 rc = json_add_version(&authentication, AUTH_MAJOR, AUTH_MINOR);
164 rc = json_add_int(&authentication,
174 rc = json_add_string(&authentication, "logonId", logon_id);
178 rc = json_add_int(&authentication, "logonType", get_logon_type(ui));
182 rc = json_add_string(&authentication, "status", nt_errstr(status));
186 rc = json_add_address(&authentication, "localAddress", ui->local_host);
191 json_add_address(&authentication, "remoteAddress", ui->remote_host);
195 rc = json_add_string(
196 &authentication, "serviceDescription", ui->service_description);
200 rc = json_add_string(
201 &authentication, "authDescription", ui->auth_description);
205 rc = json_add_string(
206 &authentication, "clientDomain", ui->client.domain_name);
210 rc = json_add_string(
211 &authentication, "clientAccount", ui->client.account_name);
215 rc = json_add_string(
216 &authentication, "workstation", ui->workstation_name);
220 rc = json_add_string(&authentication, "becameAccount", account_name);
224 rc = json_add_string(&authentication, "becameDomain", domain_name);
228 rc = json_add_sid(&authentication, "becameSid", sid);
232 rc = json_add_string(
233 &authentication, "mappedAccount", ui->mapped.account_name);
237 rc = json_add_string(
238 &authentication, "mappedDomain", ui->mapped.domain_name);
242 rc = json_add_string(&authentication,
244 ui->netlogon_trust_account.computer_name);
248 rc = json_add_string(&authentication,
249 "netlogonTrustAccount",
250 ui->netlogon_trust_account.account_name);
254 snprintf(negotiate_flags,
255 sizeof( negotiate_flags),
257 ui->netlogon_trust_account.negotiate_flags);
258 rc = json_add_string(
259 &authentication, "netlogonNegotiateFlags", negotiate_flags);
263 rc = json_add_int(&authentication,
264 "netlogonSecureChannelType",
265 ui->netlogon_trust_account.secure_channel_type);
269 rc = json_add_sid(&authentication,
270 "netlogonTrustAccountSid",
271 ui->netlogon_trust_account.sid);
275 rc = json_add_string(
276 &authentication, "passwordType", get_password_type(ui));
281 wrapper = json_new_object();
282 if (json_is_invalid(&wrapper)) {
285 rc = json_add_timestamp(&wrapper);
289 rc = json_add_string(&wrapper, "type", AUTH_JSON_TYPE);
293 rc = json_add_object(&wrapper, AUTH_JSON_TYPE, &authentication);
299 * While not a general-purpose profiling solution this will
300 * assist some to determine how long NTLM and KDC
301 * authentication takes once this process can handle it. This
302 * covers transactions elsewhere but not (eg) the delay while
303 * this is waiting unread on the input socket.
305 if (start_time != NULL) {
306 struct timeval current_time = timeval_current();
307 uint64_t duration = usec_time_diff(¤t_time,
309 rc = json_add_int(&authentication, "duration", duration);
318 DBGC_AUTH_AUDIT_JSON,
324 * On a failure authentication will not have been added to wrapper so it
325 * needs to be freed to avoid a leak.
328 json_free(&authentication);
330 DBG_ERR("Failed to write authentication event JSON log message\n");
334 * Log details of a successful authorization to a service,
335 * in a machine parsable json format
337 * IF removing or changing the format/meaning of a field please update the
338 * major version number AUTHZ_MAJOR
340 * IF adding a new field please update the minor version number AUTHZ_MINOR
342 * To process the resulting log lines from the commend line use jq to
345 * grep "^ {" log_file |\
346 * jq -rc '"\(.timestamp)\t
347 * \(.Authorization.domain)\t
348 * \(.Authorization.account)\t
349 * \(.Authorization.remoteAddress)"'
352 static void log_successful_authz_event_json(
353 struct imessaging_context *msg_ctx,
354 struct loadparm_context *lp_ctx,
355 const struct tsocket_address *remote,
356 const struct tsocket_address *local,
357 const char *service_description,
358 const char *auth_type,
359 const char *transport_protection,
360 struct auth_session_info *session_info,
363 struct json_object wrapper = json_empty_object;
364 struct json_object authorization = json_empty_object;
365 char account_flags[11];
368 authorization = json_new_object();
369 if (json_is_invalid(&authorization)) {
372 rc = json_add_version(&authorization, AUTHZ_MAJOR, AUTHZ_MINOR);
376 rc = json_add_address(&authorization, "localAddress", local);
380 rc = json_add_address(&authorization, "remoteAddress", remote);
384 rc = json_add_string(
385 &authorization, "serviceDescription", service_description);
389 rc = json_add_string(&authorization, "authType", auth_type);
393 rc = json_add_string(
394 &authorization, "domain", session_info->info->domain_name);
398 rc = json_add_string(
399 &authorization, "account", session_info->info->account_name);
404 &authorization, "sid", &session_info->security_token->sids[0]);
409 &authorization, "sessionId", &session_info->unique_session_token);
413 rc = json_add_string(
414 &authorization, "logonServer", session_info->info->logon_server);
418 rc = json_add_string(
419 &authorization, "transportProtection", transport_protection);
424 snprintf(account_flags,
425 sizeof(account_flags),
427 session_info->info->acct_flags);
428 rc = json_add_string(&authorization, "accountFlags", account_flags);
433 wrapper = json_new_object();
434 if (json_is_invalid(&wrapper)) {
437 rc = json_add_timestamp(&wrapper);
441 rc = json_add_string(&wrapper, "type", AUTHZ_JSON_TYPE);
445 rc = json_add_object(&wrapper, AUTHZ_JSON_TYPE, &authorization);
453 DBGC_AUTH_AUDIT_JSON,
459 * On a failure authorization will not have been added to wrapper so it
460 * needs to be freed to avoid a leak.
463 json_free(&authorization);
465 DBG_ERR("Unable to log Authentication event JSON audit message\n");
470 static void log_no_json(struct imessaging_context *msg_ctx,
471 struct loadparm_context *lp_ctx)
473 if (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx)) {
474 static bool auth_event_logged = false;
475 if (auth_event_logged == false) {
476 auth_event_logged = true;
477 DBG_ERR("auth event notification = true but Samba was "
478 "not compiled with jansson\n");
481 static bool json_logged = false;
482 if (json_logged == false) {
484 DBG_NOTICE("JSON auth logs not available unless "
485 "compiled with jansson\n");
492 static void log_authentication_event_json(
493 struct imessaging_context *msg_ctx,
494 struct loadparm_context *lp_ctx,
495 const struct timeval *start_time,
496 const struct auth_usersupplied_info *ui,
498 const char *domain_name,
499 const char *account_name,
501 enum event_id_type event_id,
504 log_no_json(msg_ctx, lp_ctx);
508 static void log_successful_authz_event_json(
509 struct imessaging_context *msg_ctx,
510 struct loadparm_context *lp_ctx,
511 const struct tsocket_address *remote,
512 const struct tsocket_address *local,
513 const char *service_description,
514 const char *auth_type,
515 const char *transport_protection,
516 struct auth_session_info *session_info,
519 log_no_json(msg_ctx, lp_ctx);
526 * Determine the type of the password supplied for the
527 * authorisation attempt.
530 static const char* get_password_type(const struct auth_usersupplied_info *ui)
533 const char *password_type = NULL;
535 if (ui->password_type != NULL) {
536 password_type = ui->password_type;
537 } else if (ui->auth_description != NULL &&
538 strncmp("ServerAuthenticate", ui->auth_description, 18) == 0)
540 if (ui->netlogon_trust_account.negotiate_flags
541 & NETLOGON_NEG_SUPPORTS_AES) {
542 password_type = "HMAC-SHA256";
543 } else if (ui->netlogon_trust_account.negotiate_flags
544 & NETLOGON_NEG_STRONG_KEYS) {
545 password_type = "HMAC-MD5";
547 password_type = "DES";
549 } else if (ui->password_state == AUTH_PASSWORD_RESPONSE &&
550 (ui->logon_parameters & MSV1_0_ALLOW_MSVCHAPV2) &&
551 ui->password.response.nt.length == 24) {
552 password_type = "MSCHAPv2";
553 } else if ((ui->logon_parameters & MSV1_0_CLEARTEXT_PASSWORD_SUPPLIED)
554 || (ui->password_state == AUTH_PASSWORD_PLAIN)) {
555 password_type = "Plaintext";
556 } else if (ui->password_state == AUTH_PASSWORD_HASH) {
557 password_type = "Supplied-NT-Hash";
558 } else if (ui->password_state == AUTH_PASSWORD_RESPONSE
559 && ui->password.response.nt.length > 24) {
560 password_type = "NTLMv2";
561 } else if (ui->password_state == AUTH_PASSWORD_RESPONSE
562 && ui->password.response.nt.length == 24) {
563 password_type = "NTLMv1";
564 } else if (ui->password_state == AUTH_PASSWORD_RESPONSE
565 && ui->password.response.lanman.length == 24) {
566 password_type = "LANMan";
567 } else if (ui->password_state == AUTH_PASSWORD_RESPONSE
568 && ui->password.response.nt.length == 0
569 && ui->password.response.lanman.length == 0) {
570 password_type = "No-Password";
572 return password_type;
576 * Write a human readable authentication log entry.
579 static void log_authentication_event_human_readable(
580 const struct auth_usersupplied_info *ui,
582 const char *domain_name,
583 const char *account_name,
587 TALLOC_CTX *frame = NULL;
589 const char *ts = NULL; /* formatted current time */
590 char *remote = NULL; /* formatted remote host */
591 char *local = NULL; /* formatted local host */
592 char *nl = NULL; /* NETLOGON details if present */
593 char *trust_computer_name = NULL;
594 char *trust_account_name = NULL;
595 char *logon_line = NULL;
596 const char *password_type = NULL;
598 frame = talloc_stackframe();
600 password_type = get_password_type(ui);
601 /* Get the current time */
602 ts = audit_get_timestamp(frame);
604 /* Only log the NETLOGON details if they are present */
605 if (ui->netlogon_trust_account.computer_name ||
606 ui->netlogon_trust_account.account_name) {
607 trust_computer_name = log_escape(frame,
608 ui->netlogon_trust_account.computer_name);
609 trust_account_name = log_escape(frame,
610 ui->netlogon_trust_account.account_name);
611 nl = talloc_asprintf(frame,
612 " NETLOGON computer [%s] trust account [%s]",
613 trust_computer_name, trust_account_name);
616 remote = tsocket_address_string(ui->remote_host, frame);
617 local = tsocket_address_string(ui->local_host, frame);
619 if (NT_STATUS_IS_OK(status)) {
620 struct dom_sid_buf sid_buf;
622 logon_line = talloc_asprintf(frame,
623 " became [%s]\\[%s] [%s].",
624 log_escape(frame, domain_name),
625 log_escape(frame, account_name),
626 dom_sid_str_buf(sid, &sid_buf));
628 logon_line = talloc_asprintf(
630 " mapped to [%s]\\[%s].",
631 log_escape(frame, ui->mapped.domain_name),
632 log_escape(frame, ui->mapped.account_name));
635 DEBUGC(DBGC_AUTH_AUDIT, debug_level,
636 ("Auth: [%s,%s] user [%s]\\[%s]"
637 " at [%s] with [%s] status [%s]"
638 " workstation [%s] remote host [%s]"
641 ui->service_description,
642 ui->auth_description,
643 log_escape(frame, ui->client.domain_name),
644 log_escape(frame, ui->client.account_name),
648 log_escape(frame, ui->workstation_name),
659 * Log details of an authentication attempt.
660 * Successful and unsuccessful attempts are logged.
662 * NOTE: msg_ctx and lp_ctx is optional, but when supplied allows streaming the
663 * authentication events over the message bus.
665 void log_authentication_event(
666 struct imessaging_context *msg_ctx,
667 struct loadparm_context *lp_ctx,
668 const struct timeval *start_time,
669 const struct auth_usersupplied_info *ui,
671 const char *domain_name,
672 const char *account_name,
675 /* set the log level */
676 int debug_level = AUTH_FAILURE_LEVEL;
677 enum event_id_type event_id = EVT_ID_UNSUCCESSFUL_LOGON;
679 if (NT_STATUS_IS_OK(status)) {
680 debug_level = AUTH_SUCCESS_LEVEL;
681 event_id = EVT_ID_SUCCESSFUL_LOGON;
682 if (dom_sid_equal(sid, &global_sid_Anonymous)) {
683 debug_level = AUTH_ANONYMOUS_LEVEL;
687 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT, debug_level)) {
688 log_authentication_event_human_readable(ui,
695 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT_JSON, debug_level) ||
696 (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx))) {
697 log_authentication_event_json(msg_ctx,
713 * Log details of a successful authorization to a service,
714 * in a human readable format.
717 static void log_successful_authz_event_human_readable(
718 const struct tsocket_address *remote,
719 const struct tsocket_address *local,
720 const char *service_description,
721 const char *auth_type,
722 struct auth_session_info *session_info,
725 TALLOC_CTX *frame = NULL;
727 const char *ts = NULL; /* formatted current time */
728 char *remote_str = NULL; /* formatted remote host */
729 char *local_str = NULL; /* formatted local host */
730 struct dom_sid_buf sid_buf;
732 frame = talloc_stackframe();
734 /* Get the current time */
735 ts = audit_get_timestamp(frame);
737 remote_str = tsocket_address_string(remote, frame);
738 local_str = tsocket_address_string(local, frame);
740 DEBUGC(DBGC_AUTH_AUDIT, debug_level,
741 ("Successful AuthZ: [%s,%s] user [%s]\\[%s] [%s]"
744 " local host [%s]\n",
747 log_escape(frame, session_info->info->domain_name),
748 log_escape(frame, session_info->info->account_name),
749 dom_sid_str_buf(&session_info->security_token->sids[0],
759 * Log details of a successful authorization to a service.
761 * Only successful authorizations are logged. For clarity:
762 * - NTLM bad passwords will be recorded by log_authentication_event
763 * - Kerberos decrypt failures need to be logged in gensec_gssapi et al
765 * The service may later refuse authorization due to an ACL.
767 * NOTE: msg_ctx and lp_ctx is optional, but when supplied allows streaming the
768 * authentication events over the message bus.
770 void log_successful_authz_event(
771 struct imessaging_context *msg_ctx,
772 struct loadparm_context *lp_ctx,
773 const struct tsocket_address *remote,
774 const struct tsocket_address *local,
775 const char *service_description,
776 const char *auth_type,
777 const char *transport_protection,
778 struct auth_session_info *session_info)
780 int debug_level = AUTHZ_SUCCESS_LEVEL;
782 /* set the log level */
783 if (security_token_is_anonymous(session_info->security_token)) {
784 debug_level = AUTH_ANONYMOUS_LEVEL;
787 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT, debug_level)) {
788 log_successful_authz_event_human_readable(remote,
795 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT_JSON, debug_level) ||
796 (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx))) {
797 log_successful_authz_event_json(msg_ctx, lp_ctx,
802 transport_protection,