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
,
146 const char *unix_username
,
148 enum event_id_type event_id
,
151 struct json_object wrapper
= json_empty_object
;
152 struct json_object authentication
= json_empty_object
;
153 char negotiate_flags
[11];
157 authentication
= json_new_object();
158 if (json_is_invalid(&authentication
)) {
161 rc
= json_add_version(&authentication
, AUTH_MAJOR
, AUTH_MINOR
);
165 rc
= json_add_int(&authentication
,
175 rc
= json_add_string(&authentication
, "logonId", logon_id
);
179 rc
= json_add_int(&authentication
, "logonType", get_logon_type(ui
));
183 rc
= json_add_string(&authentication
, "status", nt_errstr(status
));
187 rc
= json_add_address(&authentication
, "localAddress", ui
->local_host
);
192 json_add_address(&authentication
, "remoteAddress", ui
->remote_host
);
196 rc
= json_add_string(
197 &authentication
, "serviceDescription", ui
->service_description
);
201 rc
= json_add_string(
202 &authentication
, "authDescription", ui
->auth_description
);
206 rc
= json_add_string(
207 &authentication
, "clientDomain", ui
->client
.domain_name
);
211 rc
= json_add_string(
212 &authentication
, "clientAccount", ui
->client
.account_name
);
216 rc
= json_add_string(
217 &authentication
, "workstation", ui
->workstation_name
);
221 rc
= json_add_string(&authentication
, "becameAccount", account_name
);
225 rc
= json_add_string(&authentication
, "becameDomain", domain_name
);
229 rc
= json_add_sid(&authentication
, "becameSid", sid
);
233 rc
= json_add_string(
234 &authentication
, "mappedAccount", ui
->mapped
.account_name
);
238 rc
= json_add_string(
239 &authentication
, "mappedDomain", ui
->mapped
.domain_name
);
243 rc
= json_add_string(&authentication
,
245 ui
->netlogon_trust_account
.computer_name
);
249 rc
= json_add_string(&authentication
,
250 "netlogonTrustAccount",
251 ui
->netlogon_trust_account
.account_name
);
255 snprintf(negotiate_flags
,
256 sizeof( negotiate_flags
),
258 ui
->netlogon_trust_account
.negotiate_flags
);
259 rc
= json_add_string(
260 &authentication
, "netlogonNegotiateFlags", negotiate_flags
);
264 rc
= json_add_int(&authentication
,
265 "netlogonSecureChannelType",
266 ui
->netlogon_trust_account
.secure_channel_type
);
270 rc
= json_add_sid(&authentication
,
271 "netlogonTrustAccountSid",
272 ui
->netlogon_trust_account
.sid
);
276 rc
= json_add_string(
277 &authentication
, "passwordType", get_password_type(ui
));
282 wrapper
= json_new_object();
283 if (json_is_invalid(&wrapper
)) {
286 rc
= json_add_timestamp(&wrapper
);
290 rc
= json_add_string(&wrapper
, "type", AUTH_JSON_TYPE
);
294 rc
= json_add_object(&wrapper
, AUTH_JSON_TYPE
, &authentication
);
300 * While not a general-purpose profiling solution this will
301 * assist some to determine how long NTLM and KDC
302 * authentication takes once this process can handle it. This
303 * covers transactions elsewhere but not (eg) the delay while
304 * this is waiting unread on the input socket.
306 if (start_time
!= NULL
) {
307 struct timeval current_time
= timeval_current();
308 uint64_t duration
= usec_time_diff(¤t_time
,
310 rc
= json_add_int(&authentication
, "duration", duration
);
319 DBGC_AUTH_AUDIT_JSON
,
325 * On a failure authentication will not have been added to wrapper so it
326 * needs to be freed to avoid a leak.
329 json_free(&authentication
);
331 DBG_ERR("Failed to write authentication event JSON log message\n");
335 * Log details of a successful authorization to a service,
336 * in a machine parsable json format
338 * IF removing or changing the format/meaning of a field please update the
339 * major version number AUTHZ_MAJOR
341 * IF adding a new field please update the minor version number AUTHZ_MINOR
343 * To process the resulting log lines from the commend line use jq to
346 * grep "^ {" log_file |\
347 * jq -rc '"\(.timestamp)\t
348 * \(.Authorization.domain)\t
349 * \(.Authorization.account)\t
350 * \(.Authorization.remoteAddress)"'
353 static void log_successful_authz_event_json(
354 struct imessaging_context
*msg_ctx
,
355 struct loadparm_context
*lp_ctx
,
356 const struct tsocket_address
*remote
,
357 const struct tsocket_address
*local
,
358 const char *service_description
,
359 const char *auth_type
,
360 const char *transport_protection
,
361 struct auth_session_info
*session_info
,
364 struct json_object wrapper
= json_empty_object
;
365 struct json_object authorization
= json_empty_object
;
366 char account_flags
[11];
369 authorization
= json_new_object();
370 if (json_is_invalid(&authorization
)) {
373 rc
= json_add_version(&authorization
, AUTHZ_MAJOR
, AUTHZ_MINOR
);
377 rc
= json_add_address(&authorization
, "localAddress", local
);
381 rc
= json_add_address(&authorization
, "remoteAddress", remote
);
385 rc
= json_add_string(
386 &authorization
, "serviceDescription", service_description
);
390 rc
= json_add_string(&authorization
, "authType", auth_type
);
394 rc
= json_add_string(
395 &authorization
, "domain", session_info
->info
->domain_name
);
399 rc
= json_add_string(
400 &authorization
, "account", session_info
->info
->account_name
);
405 &authorization
, "sid", &session_info
->security_token
->sids
[0]);
410 &authorization
, "sessionId", &session_info
->unique_session_token
);
414 rc
= json_add_string(
415 &authorization
, "logonServer", session_info
->info
->logon_server
);
419 rc
= json_add_string(
420 &authorization
, "transportProtection", transport_protection
);
425 snprintf(account_flags
,
426 sizeof(account_flags
),
428 session_info
->info
->acct_flags
);
429 rc
= json_add_string(&authorization
, "accountFlags", account_flags
);
434 wrapper
= json_new_object();
435 if (json_is_invalid(&wrapper
)) {
438 rc
= json_add_timestamp(&wrapper
);
442 rc
= json_add_string(&wrapper
, "type", AUTHZ_JSON_TYPE
);
446 rc
= json_add_object(&wrapper
, AUTHZ_JSON_TYPE
, &authorization
);
454 DBGC_AUTH_AUDIT_JSON
,
460 * On a failure authorization will not have been added to wrapper so it
461 * needs to be freed to avoid a leak.
464 json_free(&authorization
);
466 DBG_ERR("Unable to log Authentication event JSON audit message\n");
471 static void log_no_json(struct imessaging_context
*msg_ctx
,
472 struct loadparm_context
*lp_ctx
)
474 if (msg_ctx
&& lp_ctx
&& lpcfg_auth_event_notification(lp_ctx
)) {
475 static bool auth_event_logged
= false;
476 if (auth_event_logged
== false) {
477 auth_event_logged
= true;
478 DBG_ERR("auth event notification = true but Samba was "
479 "not compiled with jansson\n");
482 static bool json_logged
= false;
483 if (json_logged
== false) {
485 DBG_NOTICE("JSON auth logs not available unless "
486 "compiled with jansson\n");
493 static void log_authentication_event_json(
494 struct imessaging_context
*msg_ctx
,
495 struct loadparm_context
*lp_ctx
,
496 const struct timeval
*start_time
,
497 const struct auth_usersupplied_info
*ui
,
499 const char *domain_name
,
500 const char *account_name
,
501 const char *unix_username
,
503 enum event_id_type event_id
,
506 log_no_json(msg_ctx
, lp_ctx
);
510 static void log_successful_authz_event_json(
511 struct imessaging_context
*msg_ctx
,
512 struct loadparm_context
*lp_ctx
,
513 const struct tsocket_address
*remote
,
514 const struct tsocket_address
*local
,
515 const char *service_description
,
516 const char *auth_type
,
517 const char *transport_protection
,
518 struct auth_session_info
*session_info
,
521 log_no_json(msg_ctx
, lp_ctx
);
528 * Determine the type of the password supplied for the
529 * authorisation attempt.
532 static const char* get_password_type(const struct auth_usersupplied_info
*ui
)
535 const char *password_type
= NULL
;
537 if (ui
->password_type
!= NULL
) {
538 password_type
= ui
->password_type
;
539 } else if (ui
->auth_description
!= NULL
&&
540 strncmp("ServerAuthenticate", ui
->auth_description
, 18) == 0)
542 if (ui
->netlogon_trust_account
.negotiate_flags
543 & NETLOGON_NEG_SUPPORTS_AES
) {
544 password_type
= "HMAC-SHA256";
545 } else if (ui
->netlogon_trust_account
.negotiate_flags
546 & NETLOGON_NEG_STRONG_KEYS
) {
547 password_type
= "HMAC-MD5";
549 password_type
= "DES";
551 } else if (ui
->password_state
== AUTH_PASSWORD_RESPONSE
&&
552 (ui
->logon_parameters
& MSV1_0_ALLOW_MSVCHAPV2
) &&
553 ui
->password
.response
.nt
.length
== 24) {
554 password_type
= "MSCHAPv2";
555 } else if ((ui
->logon_parameters
& MSV1_0_CLEARTEXT_PASSWORD_SUPPLIED
)
556 || (ui
->password_state
== AUTH_PASSWORD_PLAIN
)) {
557 password_type
= "Plaintext";
558 } else if (ui
->password_state
== AUTH_PASSWORD_HASH
) {
559 password_type
= "Supplied-NT-Hash";
560 } else if (ui
->password_state
== AUTH_PASSWORD_RESPONSE
561 && ui
->password
.response
.nt
.length
> 24) {
562 password_type
= "NTLMv2";
563 } else if (ui
->password_state
== AUTH_PASSWORD_RESPONSE
564 && ui
->password
.response
.nt
.length
== 24) {
565 password_type
= "NTLMv1";
566 } else if (ui
->password_state
== AUTH_PASSWORD_RESPONSE
567 && ui
->password
.response
.lanman
.length
== 24) {
568 password_type
= "LANMan";
569 } else if (ui
->password_state
== AUTH_PASSWORD_RESPONSE
570 && ui
->password
.response
.nt
.length
== 0
571 && ui
->password
.response
.lanman
.length
== 0) {
572 password_type
= "No-Password";
574 return password_type
;
578 * Write a human readable authentication log entry.
581 static void log_authentication_event_human_readable(
582 const struct auth_usersupplied_info
*ui
,
584 const char *domain_name
,
585 const char *account_name
,
586 const char *unix_username
,
590 TALLOC_CTX
*frame
= NULL
;
592 const char *ts
= NULL
; /* formatted current time */
593 char *remote
= NULL
; /* formatted remote host */
594 char *local
= NULL
; /* formatted local host */
595 char *nl
= NULL
; /* NETLOGON details if present */
596 char *trust_computer_name
= NULL
;
597 char *trust_account_name
= NULL
;
598 char *logon_line
= NULL
;
599 const char *password_type
= NULL
;
601 frame
= talloc_stackframe();
603 password_type
= get_password_type(ui
);
604 /* Get the current time */
605 ts
= audit_get_timestamp(frame
);
607 /* Only log the NETLOGON details if they are present */
608 if (ui
->netlogon_trust_account
.computer_name
||
609 ui
->netlogon_trust_account
.account_name
) {
610 trust_computer_name
= log_escape(frame
,
611 ui
->netlogon_trust_account
.computer_name
);
612 trust_account_name
= log_escape(frame
,
613 ui
->netlogon_trust_account
.account_name
);
614 nl
= talloc_asprintf(frame
,
615 " NETLOGON computer [%s] trust account [%s]",
616 trust_computer_name
, trust_account_name
);
619 remote
= tsocket_address_string(ui
->remote_host
, frame
);
620 local
= tsocket_address_string(ui
->local_host
, frame
);
622 if (NT_STATUS_IS_OK(status
)) {
623 struct dom_sid_buf sid_buf
;
625 logon_line
= talloc_asprintf(frame
,
626 " became [%s]\\[%s] [%s].",
627 log_escape(frame
, domain_name
),
628 log_escape(frame
, account_name
),
629 dom_sid_str_buf(sid
, &sid_buf
));
631 logon_line
= talloc_asprintf(
633 " mapped to [%s]\\[%s].",
634 log_escape(frame
, ui
->mapped
.domain_name
),
635 log_escape(frame
, ui
->mapped
.account_name
));
638 DEBUGC(DBGC_AUTH_AUDIT
, debug_level
,
639 ("Auth: [%s,%s] user [%s]\\[%s]"
640 " at [%s] with [%s] status [%s]"
641 " workstation [%s] remote host [%s]"
644 ui
->service_description
,
645 ui
->auth_description
,
646 log_escape(frame
, ui
->client
.domain_name
),
647 log_escape(frame
, ui
->client
.account_name
),
651 log_escape(frame
, ui
->workstation_name
),
662 * Log details of an authentication attempt.
663 * Successful and unsuccessful attempts are logged.
665 * NOTE: msg_ctx and lp_ctx is optional, but when supplied allows streaming the
666 * authentication events over the message bus.
668 void log_authentication_event(
669 struct imessaging_context
*msg_ctx
,
670 struct loadparm_context
*lp_ctx
,
671 const struct timeval
*start_time
,
672 const struct auth_usersupplied_info
*ui
,
674 const char *domain_name
,
675 const char *account_name
,
676 const char *unix_username
,
679 /* set the log level */
680 int debug_level
= AUTH_FAILURE_LEVEL
;
681 enum event_id_type event_id
= EVT_ID_UNSUCCESSFUL_LOGON
;
683 if (NT_STATUS_IS_OK(status
)) {
684 debug_level
= AUTH_SUCCESS_LEVEL
;
685 event_id
= EVT_ID_SUCCESSFUL_LOGON
;
686 if (dom_sid_equal(sid
, &global_sid_Anonymous
)) {
687 debug_level
= AUTH_ANONYMOUS_LEVEL
;
691 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT
, debug_level
)) {
692 log_authentication_event_human_readable(ui
,
700 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT_JSON
, debug_level
) ||
701 (msg_ctx
&& lp_ctx
&& lpcfg_auth_event_notification(lp_ctx
))) {
702 log_authentication_event_json(msg_ctx
,
719 * Log details of a successful authorization to a service,
720 * in a human readable format.
723 static void log_successful_authz_event_human_readable(
724 const struct tsocket_address
*remote
,
725 const struct tsocket_address
*local
,
726 const char *service_description
,
727 const char *auth_type
,
728 const char *transport_protection
,
729 struct auth_session_info
*session_info
,
732 TALLOC_CTX
*frame
= NULL
;
734 const char *ts
= NULL
; /* formatted current time */
735 char *remote_str
= NULL
; /* formatted remote host */
736 char *local_str
= NULL
; /* formatted local host */
737 struct dom_sid_buf sid_buf
;
739 frame
= talloc_stackframe();
741 /* Get the current time */
742 ts
= audit_get_timestamp(frame
);
744 remote_str
= tsocket_address_string(remote
, frame
);
745 local_str
= tsocket_address_string(local
, frame
);
747 DEBUGC(DBGC_AUTH_AUDIT
, debug_level
,
748 ("Successful AuthZ: [%s,%s] user [%s]\\[%s] [%s]"
751 " local host [%s]\n",
754 log_escape(frame
, session_info
->info
->domain_name
),
755 log_escape(frame
, session_info
->info
->account_name
),
756 dom_sid_str_buf(&session_info
->security_token
->sids
[0],
766 * Log details of a successful authorization to a service.
768 * Only successful authorizations are logged. For clarity:
769 * - NTLM bad passwords will be recorded by log_authentication_event
770 * - Kerberos decrypt failures need to be logged in gensec_gssapi et al
772 * The service may later refuse authorization due to an ACL.
774 * NOTE: msg_ctx and lp_ctx is optional, but when supplied allows streaming the
775 * authentication events over the message bus.
777 void log_successful_authz_event(
778 struct imessaging_context
*msg_ctx
,
779 struct loadparm_context
*lp_ctx
,
780 const struct tsocket_address
*remote
,
781 const struct tsocket_address
*local
,
782 const char *service_description
,
783 const char *auth_type
,
784 const char *transport_protection
,
785 struct auth_session_info
*session_info
)
787 int debug_level
= AUTHZ_SUCCESS_LEVEL
;
789 /* set the log level */
790 if (security_token_is_anonymous(session_info
->security_token
)) {
791 debug_level
= AUTH_ANONYMOUS_LEVEL
;
794 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT
, debug_level
)) {
795 log_successful_authz_event_human_readable(remote
,
799 transport_protection
,
803 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT_JSON
, debug_level
) ||
804 (msg_ctx
&& lp_ctx
&& lpcfg_auth_event_notification(lp_ctx
))) {
805 log_successful_authz_event_json(msg_ctx
, lp_ctx
,
810 transport_protection
,