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 "lib/audit_logging/audit_logging.h"
63 * Determine the type of the password supplied for the
64 * authorisation attempt.
67 static const char* get_password_type(const struct auth_usersupplied_info
*ui
);
72 #include "system/time.h"
75 * Write the json object to the debug logs.
78 static void log_json(struct imessaging_context
*msg_ctx
,
79 struct loadparm_context
*lp_ctx
,
80 struct json_object
*object
,
85 audit_log_json(type
, object
, debug_class
, debug_level
);
86 if (msg_ctx
&& lp_ctx
&& lpcfg_auth_event_notification(lp_ctx
)) {
87 audit_message_send(msg_ctx
,
95 * Write a machine parsable json formatted authentication log entry.
97 * IF removing or changing the format/meaning of a field please update the
98 * major version number AUTH_MAJOR
100 * IF adding a new field please update the minor version number AUTH_MINOR
102 * To process the resulting log lines from the commend line use jq to
105 * grep "JSON Authentication" log file |
107 * jq -rc '"\(.timestamp)\t\(.Authentication.status)\t
108 * \(.Authentication.clientDomain)\t
109 * \(.Authentication.clientAccount)
110 * \t\(.Authentication.workstation)
111 * \t\(.Authentication.remoteAddress)
112 * \t\(.Authentication.localAddress)"'
114 static void log_authentication_event_json(
115 struct imessaging_context
*msg_ctx
,
116 struct loadparm_context
*lp_ctx
,
117 const struct timeval
*start_time
,
118 const struct auth_usersupplied_info
*ui
,
120 const char *domain_name
,
121 const char *account_name
,
122 const char *unix_username
,
126 struct json_object wrapper
= json_empty_object
;
127 struct json_object authentication
= json_empty_object
;
128 char negotiate_flags
[11];
131 authentication
= json_new_object();
132 if (json_is_invalid(&authentication
)) {
135 rc
= json_add_version(&authentication
, AUTH_MAJOR
, AUTH_MINOR
);
139 rc
= json_add_string(&authentication
, "status", nt_errstr(status
));
143 rc
= json_add_address(&authentication
, "localAddress", ui
->local_host
);
148 json_add_address(&authentication
, "remoteAddress", ui
->remote_host
);
152 rc
= json_add_string(
153 &authentication
, "serviceDescription", ui
->service_description
);
157 rc
= json_add_string(
158 &authentication
, "authDescription", ui
->auth_description
);
162 rc
= json_add_string(
163 &authentication
, "clientDomain", ui
->client
.domain_name
);
167 rc
= json_add_string(
168 &authentication
, "clientAccount", ui
->client
.account_name
);
172 rc
= json_add_string(
173 &authentication
, "workstation", ui
->workstation_name
);
177 rc
= json_add_string(&authentication
, "becameAccount", account_name
);
181 rc
= json_add_string(&authentication
, "becameDomain", domain_name
);
185 rc
= json_add_sid(&authentication
, "becameSid", sid
);
189 rc
= json_add_string(
190 &authentication
, "mappedAccount", ui
->mapped
.account_name
);
194 rc
= json_add_string(
195 &authentication
, "mappedDomain", ui
->mapped
.domain_name
);
199 rc
= json_add_string(&authentication
,
201 ui
->netlogon_trust_account
.computer_name
);
205 rc
= json_add_string(&authentication
,
206 "netlogonTrustAccount",
207 ui
->netlogon_trust_account
.account_name
);
211 snprintf(negotiate_flags
,
212 sizeof( negotiate_flags
),
214 ui
->netlogon_trust_account
.negotiate_flags
);
215 rc
= json_add_string(
216 &authentication
, "netlogonNegotiateFlags", negotiate_flags
);
220 rc
= json_add_int(&authentication
,
221 "netlogonSecureChannelType",
222 ui
->netlogon_trust_account
.secure_channel_type
);
226 rc
= json_add_sid(&authentication
,
227 "netlogonTrustAccountSid",
228 ui
->netlogon_trust_account
.sid
);
232 rc
= json_add_string(
233 &authentication
, "passwordType", get_password_type(ui
));
238 wrapper
= json_new_object();
239 if (json_is_invalid(&wrapper
)) {
242 rc
= json_add_timestamp(&wrapper
);
246 rc
= json_add_string(&wrapper
, "type", AUTH_JSON_TYPE
);
250 rc
= json_add_object(&wrapper
, AUTH_JSON_TYPE
, &authentication
);
256 * While not a general-purpose profiling solution this will
257 * assist some to determine how long NTLM and KDC
258 * authentication takes once this process can handle it. This
259 * covers transactions elsewhere but not (eg) the delay while
260 * this is waiting unread on the input socket.
262 if (start_time
!= NULL
) {
263 struct timeval current_time
= timeval_current();
264 uint64_t duration
= usec_time_diff(¤t_time
,
266 rc
= json_add_int(&authentication
, "duration", duration
);
282 * On a failure authentication will not have been added to wrapper so it
283 * needs to be freed to avoid a leak.
286 json_free(&authentication
);
288 DBG_ERR("Failed to write authentication event JSON log message\n");
292 * Log details of a successful authorization to a service,
293 * in a machine parsable json format
295 * IF removing or changing the format/meaning of a field please update the
296 * major version number AUTHZ_MAJOR
298 * IF adding a new field please update the minor version number AUTHZ_MINOR
300 * To process the resulting log lines from the commend line use jq to
303 * grep "JSON Authentication" log_file |\
304 * sed "s;^[^{]*;;" |\
305 * jq -rc '"\(.timestamp)\t
306 * \(.Authorization.domain)\t
307 * \(.Authorization.account)\t
308 * \(.Authorization.remoteAddress)"'
311 static void log_successful_authz_event_json(
312 struct imessaging_context
*msg_ctx
,
313 struct loadparm_context
*lp_ctx
,
314 const struct tsocket_address
*remote
,
315 const struct tsocket_address
*local
,
316 const char *service_description
,
317 const char *auth_type
,
318 const char *transport_protection
,
319 struct auth_session_info
*session_info
,
322 struct json_object wrapper
= json_empty_object
;
323 struct json_object authorization
= json_empty_object
;
324 char account_flags
[11];
327 authorization
= json_new_object();
328 if (json_is_invalid(&authorization
)) {
331 rc
= json_add_version(&authorization
, AUTHZ_MAJOR
, AUTHZ_MINOR
);
335 rc
= json_add_address(&authorization
, "localAddress", local
);
339 rc
= json_add_address(&authorization
, "remoteAddress", remote
);
343 rc
= json_add_string(
344 &authorization
, "serviceDescription", service_description
);
348 rc
= json_add_string(&authorization
, "authType", auth_type
);
352 rc
= json_add_string(
353 &authorization
, "domain", session_info
->info
->domain_name
);
357 rc
= json_add_string(
358 &authorization
, "account", session_info
->info
->account_name
);
363 &authorization
, "sid", &session_info
->security_token
->sids
[0]);
368 &authorization
, "sessionId", &session_info
->unique_session_token
);
372 rc
= json_add_string(
373 &authorization
, "logonServer", session_info
->info
->logon_server
);
377 rc
= json_add_string(
378 &authorization
, "transportProtection", transport_protection
);
383 snprintf(account_flags
,
384 sizeof(account_flags
),
386 session_info
->info
->acct_flags
);
387 rc
= json_add_string(&authorization
, "accountFlags", account_flags
);
392 wrapper
= json_new_object();
393 if (json_is_invalid(&wrapper
)) {
396 rc
= json_add_timestamp(&wrapper
);
400 rc
= json_add_string(&wrapper
, "type", AUTHZ_JSON_TYPE
);
404 rc
= json_add_object(&wrapper
, AUTHZ_JSON_TYPE
, &authorization
);
419 * On a failure authorization will not have been added to wrapper so it
420 * needs to be freed to avoid a leak.
423 json_free(&authorization
);
425 DBG_ERR("Unable to log Authentication event JSON audit message\n");
430 static void log_no_json(struct imessaging_context
*msg_ctx
,
431 struct loadparm_context
*lp_ctx
)
433 if (msg_ctx
&& lp_ctx
&& lpcfg_auth_event_notification(lp_ctx
)) {
434 static bool auth_event_logged
= false;
435 if (auth_event_logged
== false) {
436 auth_event_logged
= true;
437 DBG_ERR("auth event notification = true but Samba was "
438 "not compiled with jansson\n");
441 static bool json_logged
= false;
442 if (json_logged
== false) {
444 DBG_NOTICE("JSON auth logs not available unless "
445 "compiled with jansson\n");
452 static void log_authentication_event_json(
453 struct imessaging_context
*msg_ctx
,
454 struct loadparm_context
*lp_ctx
,
455 const struct timeval
*start_time
,
456 const struct auth_usersupplied_info
*ui
,
458 const char *domain_name
,
459 const char *account_name
,
460 const char *unix_username
,
464 log_no_json(msg_ctx
, lp_ctx
);
468 static void log_successful_authz_event_json(
469 struct imessaging_context
*msg_ctx
,
470 struct loadparm_context
*lp_ctx
,
471 const struct tsocket_address
*remote
,
472 const struct tsocket_address
*local
,
473 const char *service_description
,
474 const char *auth_type
,
475 const char *transport_protection
,
476 struct auth_session_info
*session_info
,
479 log_no_json(msg_ctx
, lp_ctx
);
486 * Determine the type of the password supplied for the
487 * authorisation attempt.
490 static const char* get_password_type(const struct auth_usersupplied_info
*ui
)
493 const char *password_type
= NULL
;
495 if (ui
->password_type
!= NULL
) {
496 password_type
= ui
->password_type
;
497 } else if (ui
->auth_description
!= NULL
&&
498 strncmp("ServerAuthenticate", ui
->auth_description
, 18) == 0)
500 if (ui
->netlogon_trust_account
.negotiate_flags
501 & NETLOGON_NEG_SUPPORTS_AES
) {
502 password_type
= "HMAC-SHA256";
503 } else if (ui
->netlogon_trust_account
.negotiate_flags
504 & NETLOGON_NEG_STRONG_KEYS
) {
505 password_type
= "HMAC-MD5";
507 password_type
= "DES";
509 } else if (ui
->password_state
== AUTH_PASSWORD_RESPONSE
&&
510 (ui
->logon_parameters
& MSV1_0_ALLOW_MSVCHAPV2
) &&
511 ui
->password
.response
.nt
.length
== 24) {
512 password_type
= "MSCHAPv2";
513 } else if ((ui
->logon_parameters
& MSV1_0_CLEARTEXT_PASSWORD_SUPPLIED
)
514 || (ui
->password_state
== AUTH_PASSWORD_PLAIN
)) {
515 password_type
= "Plaintext";
516 } else if (ui
->password_state
== AUTH_PASSWORD_HASH
) {
517 password_type
= "Supplied-NT-Hash";
518 } else if (ui
->password_state
== AUTH_PASSWORD_RESPONSE
519 && ui
->password
.response
.nt
.length
> 24) {
520 password_type
= "NTLMv2";
521 } else if (ui
->password_state
== AUTH_PASSWORD_RESPONSE
522 && ui
->password
.response
.nt
.length
== 24) {
523 password_type
= "NTLMv1";
524 } else if (ui
->password_state
== AUTH_PASSWORD_RESPONSE
525 && ui
->password
.response
.lanman
.length
== 24) {
526 password_type
= "LANMan";
527 } else if (ui
->password_state
== AUTH_PASSWORD_RESPONSE
528 && ui
->password
.response
.nt
.length
== 0
529 && ui
->password
.response
.lanman
.length
== 0) {
530 password_type
= "No-Password";
532 return password_type
;
536 * Write a human readable authentication log entry.
539 static void log_authentication_event_human_readable(
540 const struct auth_usersupplied_info
*ui
,
542 const char *domain_name
,
543 const char *account_name
,
544 const char *unix_username
,
548 TALLOC_CTX
*frame
= NULL
;
550 const char *ts
= NULL
; /* formatted current time */
551 char *remote
= NULL
; /* formatted remote host */
552 char *local
= NULL
; /* formatted local host */
553 char *nl
= NULL
; /* NETLOGON details if present */
554 char *trust_computer_name
= NULL
;
555 char *trust_account_name
= NULL
;
556 char *logon_line
= NULL
;
557 const char *password_type
= NULL
;
559 frame
= talloc_stackframe();
561 password_type
= get_password_type(ui
);
562 /* Get the current time */
563 ts
= audit_get_timestamp(frame
);
565 /* Only log the NETLOGON details if they are present */
566 if (ui
->netlogon_trust_account
.computer_name
||
567 ui
->netlogon_trust_account
.account_name
) {
568 trust_computer_name
= log_escape(frame
,
569 ui
->netlogon_trust_account
.computer_name
);
570 trust_account_name
= log_escape(frame
,
571 ui
->netlogon_trust_account
.account_name
);
572 nl
= talloc_asprintf(frame
,
573 " NETLOGON computer [%s] trust account [%s]",
574 trust_computer_name
, trust_account_name
);
577 remote
= tsocket_address_string(ui
->remote_host
, frame
);
578 local
= tsocket_address_string(ui
->local_host
, frame
);
580 if (NT_STATUS_IS_OK(status
)) {
581 char sid_buf
[DOM_SID_STR_BUFLEN
];
583 dom_sid_string_buf(sid
, sid_buf
, sizeof(sid_buf
));
584 logon_line
= talloc_asprintf(frame
,
585 " became [%s]\\[%s] [%s].",
586 log_escape(frame
, domain_name
),
587 log_escape(frame
, account_name
),
590 logon_line
= talloc_asprintf(
592 " mapped to [%s]\\[%s].",
593 log_escape(frame
, ui
->mapped
.domain_name
),
594 log_escape(frame
, ui
->mapped
.account_name
));
597 DEBUGC(DBGC_AUTH_AUDIT
, debug_level
,
598 ("Auth: [%s,%s] user [%s]\\[%s]"
599 " at [%s] with [%s] status [%s]"
600 " workstation [%s] remote host [%s]"
603 ui
->service_description
,
604 ui
->auth_description
,
605 log_escape(frame
, ui
->client
.domain_name
),
606 log_escape(frame
, ui
->client
.account_name
),
610 log_escape(frame
, ui
->workstation_name
),
621 * Log details of an authentication attempt.
622 * Successful and unsuccessful attempts are logged.
624 * NOTE: msg_ctx and lp_ctx is optional, but when supplied allows streaming the
625 * authentication events over the message bus.
627 void log_authentication_event(
628 struct imessaging_context
*msg_ctx
,
629 struct loadparm_context
*lp_ctx
,
630 const struct timeval
*start_time
,
631 const struct auth_usersupplied_info
*ui
,
633 const char *domain_name
,
634 const char *account_name
,
635 const char *unix_username
,
638 /* set the log level */
639 int debug_level
= AUTH_FAILURE_LEVEL
;
641 if (NT_STATUS_IS_OK(status
)) {
642 debug_level
= AUTH_SUCCESS_LEVEL
;
643 if (dom_sid_equal(sid
, &global_sid_Anonymous
)) {
644 debug_level
= AUTH_ANONYMOUS_LEVEL
;
648 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT
, debug_level
)) {
649 log_authentication_event_human_readable(ui
,
657 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT_JSON
, debug_level
) ||
658 (msg_ctx
&& lp_ctx
&& lpcfg_auth_event_notification(lp_ctx
))) {
659 log_authentication_event_json(msg_ctx
,
675 * Log details of a successful authorization to a service,
676 * in a human readable format.
679 static void log_successful_authz_event_human_readable(
680 const struct tsocket_address
*remote
,
681 const struct tsocket_address
*local
,
682 const char *service_description
,
683 const char *auth_type
,
684 const char *transport_protection
,
685 struct auth_session_info
*session_info
,
688 TALLOC_CTX
*frame
= NULL
;
690 const char *ts
= NULL
; /* formatted current time */
691 char *remote_str
= NULL
; /* formatted remote host */
692 char *local_str
= NULL
; /* formatted local host */
693 char sid_buf
[DOM_SID_STR_BUFLEN
];
695 frame
= talloc_stackframe();
697 /* Get the current time */
698 ts
= audit_get_timestamp(frame
);
700 remote_str
= tsocket_address_string(remote
, frame
);
701 local_str
= tsocket_address_string(local
, frame
);
703 dom_sid_string_buf(&session_info
->security_token
->sids
[0],
707 DEBUGC(DBGC_AUTH_AUDIT
, debug_level
,
708 ("Successful AuthZ: [%s,%s] user [%s]\\[%s] [%s]"
711 " local host [%s]\n",
714 log_escape(frame
, session_info
->info
->domain_name
),
715 log_escape(frame
, session_info
->info
->account_name
),
725 * Log details of a successful authorization to a service.
727 * Only successful authorizations are logged. For clarity:
728 * - NTLM bad passwords will be recorded by log_authentication_event
729 * - Kerberos decrypt failures need to be logged in gensec_gssapi et al
731 * The service may later refuse authorization due to an ACL.
733 * NOTE: msg_ctx and lp_ctx is optional, but when supplied allows streaming the
734 * authentication events over the message bus.
736 void log_successful_authz_event(
737 struct imessaging_context
*msg_ctx
,
738 struct loadparm_context
*lp_ctx
,
739 const struct tsocket_address
*remote
,
740 const struct tsocket_address
*local
,
741 const char *service_description
,
742 const char *auth_type
,
743 const char *transport_protection
,
744 struct auth_session_info
*session_info
)
746 int debug_level
= AUTHZ_SUCCESS_LEVEL
;
748 /* set the log level */
749 if (security_token_is_anonymous(session_info
->security_token
)) {
750 debug_level
= AUTH_ANONYMOUS_LEVEL
;
753 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT
, debug_level
)) {
754 log_successful_authz_event_human_readable(remote
,
758 transport_protection
,
762 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT_JSON
, debug_level
) ||
763 (msg_ctx
&& lp_ctx
&& lpcfg_auth_event_notification(lp_ctx
))) {
764 log_successful_authz_event_json(msg_ctx
, lp_ctx
,
769 transport_protection
,