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 *context,
91 json = json_dumps(context->root, 0);
93 DBG_ERR("Unable to convert JSON object to string\n");
94 context->error = true;
98 DEBUGC(debug_class, debug_level, ("JSON %s: %s\n", type, json));
99 if (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx)) {
100 audit_message_send(msg_ctx,
113 * Write a machine parsable json formatted authentication log entry.
115 * IF removing or changing the format/meaning of a field please update the
116 * major version number AUTH_MAJOR
118 * IF adding a new field please update the minor version number AUTH_MINOR
120 * To process the resulting log lines from the commend line use jq to
123 * grep "JSON Authentication" log file |
125 * jq -rc '"\(.timestamp)\t\(.Authentication.status)\t
126 * \(.Authentication.clientDomain)\t
127 * \(.Authentication.clientAccount)
128 * \t\(.Authentication.workstation)
129 * \t\(.Authentication.remoteAddress)
130 * \t\(.Authentication.localAddress)"'
132 static void log_authentication_event_json(
133 struct imessaging_context *msg_ctx,
134 struct loadparm_context *lp_ctx,
135 const struct auth_usersupplied_info *ui,
137 const char *domain_name,
138 const char *account_name,
139 const char *unix_username,
143 struct json_object context = json_new_object();
144 struct json_object authentication;
145 char negotiate_flags[11];
147 json_add_timestamp(&context);
148 json_add_string(&context, "type", AUTH_JSON_TYPE);
150 authentication = json_new_object();
151 json_add_version(&authentication, AUTH_MAJOR, AUTH_MINOR);
152 json_add_string(&authentication, "status", nt_errstr(status));
153 json_add_address(&authentication, "localAddress", ui->local_host);
154 json_add_address(&authentication, "remoteAddress", ui->remote_host);
155 json_add_string(&authentication,
156 "serviceDescription",
157 ui->service_description);
158 json_add_string(&authentication,
160 ui->auth_description);
161 json_add_string(&authentication,
163 ui->client.domain_name);
164 json_add_string(&authentication,
166 ui->client.account_name);
167 json_add_string(&authentication,
169 ui->workstation_name);
170 json_add_string(&authentication, "becameAccount", account_name);
171 json_add_string(&authentication, "becameDomain", domain_name);
172 json_add_sid(&authentication, "becameSid", sid);
173 json_add_string(&authentication,
175 ui->mapped.account_name);
176 json_add_string(&authentication,
178 ui->mapped.domain_name);
179 json_add_string(&authentication,
181 ui->netlogon_trust_account.computer_name);
182 json_add_string(&authentication,
183 "netlogonTrustAccount",
184 ui->netlogon_trust_account.account_name);
185 snprintf(negotiate_flags,
186 sizeof( negotiate_flags),
188 ui->netlogon_trust_account.negotiate_flags);
189 json_add_string(&authentication,
190 "netlogonNegotiateFlags",
192 json_add_int(&authentication,
193 "netlogonSecureChannelType",
194 ui->netlogon_trust_account.secure_channel_type);
195 json_add_sid(&authentication,
196 "netlogonTrustAccountSid",
197 ui->netlogon_trust_account.sid);
198 json_add_string(&authentication, "passwordType", get_password_type(ui));
199 json_add_object(&context,AUTH_JSON_TYPE, &authentication);
211 * Log details of a successful authorization to a service,
212 * in a machine parsable json format
214 * IF removing or changing the format/meaning of a field please update the
215 * major version number AUTHZ_MAJOR
217 * IF adding a new field please update the minor version number AUTHZ_MINOR
219 * To process the resulting log lines from the commend line use jq to
222 * grep "JSON Authentication" log_file |\
223 * sed "s;^[^{]*;;" |\
224 * jq -rc '"\(.timestamp)\t
225 * \(.Authorization.domain)\t
226 * \(.Authorization.account)\t
227 * \(.Authorization.remoteAddress)"'
230 static void log_successful_authz_event_json(
231 struct imessaging_context *msg_ctx,
232 struct loadparm_context *lp_ctx,
233 const struct tsocket_address *remote,
234 const struct tsocket_address *local,
235 const char *service_description,
236 const char *auth_type,
237 const char *transport_protection,
238 struct auth_session_info *session_info,
241 struct json_object context = json_new_object();
242 struct json_object authorization;
243 char account_flags[11];
245 json_add_timestamp(&context);
246 json_add_string(&context, "type", AUTHZ_JSON_TYPE);
247 authorization = json_new_object();
248 json_add_version(&authorization, AUTHZ_MAJOR, AUTHZ_MINOR);
249 json_add_address(&authorization, "localAddress", local);
250 json_add_address(&authorization, "remoteAddress", remote);
251 json_add_string(&authorization,
252 "serviceDescription",
253 service_description);
254 json_add_string(&authorization, "authType", auth_type);
255 json_add_string(&authorization,
257 session_info->info->domain_name);
258 json_add_string(&authorization,
260 session_info->info->account_name);
261 json_add_sid(&authorization,
263 &session_info->security_token->sids[0]);
264 json_add_guid(&authorization,
266 &session_info->unique_session_token);
267 json_add_string(&authorization,
269 session_info->info->logon_server);
270 json_add_string(&authorization,
271 "transportProtection",
272 transport_protection);
274 snprintf(account_flags,
275 sizeof(account_flags),
277 session_info->info->acct_flags);
278 json_add_string(&authorization, "accountFlags", account_flags);
279 json_add_object(&context, AUTHZ_JSON_TYPE, &authorization);
292 static void log_no_json(struct imessaging_context *msg_ctx,
293 struct loadparm_context *lp_ctx)
295 if (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx)) {
296 static bool auth_event_logged = false;
297 if (auth_event_logged == false) {
298 auth_event_logged = true;
299 DBG_ERR("auth event notification = true but Samba was "
300 "not compiled with jansson\n");
303 static bool json_logged = false;
304 if (json_logged == false) {
306 DBG_NOTICE("JSON auth logs not available unless "
307 "compiled with jansson\n");
314 static void log_authentication_event_json(
315 struct imessaging_context *msg_ctx,
316 struct loadparm_context *lp_ctx,
317 const struct auth_usersupplied_info *ui,
319 const char *domain_name,
320 const char *account_name,
321 const char *unix_username,
325 log_no_json(msg_ctx, lp_ctx);
329 static void log_successful_authz_event_json(
330 struct imessaging_context *msg_ctx,
331 struct loadparm_context *lp_ctx,
332 const struct tsocket_address *remote,
333 const struct tsocket_address *local,
334 const char *service_description,
335 const char *auth_type,
336 const char *transport_protection,
337 struct auth_session_info *session_info,
340 log_no_json(msg_ctx, lp_ctx);
347 * Determine the type of the password supplied for the
348 * authorisation attempt.
351 static const char* get_password_type(const struct auth_usersupplied_info *ui)
354 const char *password_type = NULL;
356 if (ui->password_type != NULL) {
357 password_type = ui->password_type;
358 } else if (ui->auth_description != NULL &&
359 strncmp("ServerAuthenticate", ui->auth_description, 18) == 0)
361 if (ui->netlogon_trust_account.negotiate_flags
362 & NETLOGON_NEG_SUPPORTS_AES) {
363 password_type = "HMAC-SHA256";
364 } else if (ui->netlogon_trust_account.negotiate_flags
365 & NETLOGON_NEG_STRONG_KEYS) {
366 password_type = "HMAC-MD5";
368 password_type = "DES";
370 } else if (ui->password_state == AUTH_PASSWORD_RESPONSE &&
371 (ui->logon_parameters & MSV1_0_ALLOW_MSVCHAPV2) &&
372 ui->password.response.nt.length == 24) {
373 password_type = "MSCHAPv2";
374 } else if ((ui->logon_parameters & MSV1_0_CLEARTEXT_PASSWORD_SUPPLIED)
375 || (ui->password_state == AUTH_PASSWORD_PLAIN)) {
376 password_type = "Plaintext";
377 } else if (ui->password_state == AUTH_PASSWORD_HASH) {
378 password_type = "Supplied-NT-Hash";
379 } else if (ui->password_state == AUTH_PASSWORD_RESPONSE
380 && ui->password.response.nt.length > 24) {
381 password_type = "NTLMv2";
382 } else if (ui->password_state == AUTH_PASSWORD_RESPONSE
383 && ui->password.response.nt.length == 24) {
384 password_type = "NTLMv1";
385 } else if (ui->password_state == AUTH_PASSWORD_RESPONSE
386 && ui->password.response.lanman.length == 24) {
387 password_type = "LANMan";
388 } else if (ui->password_state == AUTH_PASSWORD_RESPONSE
389 && ui->password.response.nt.length == 0
390 && ui->password.response.lanman.length == 0) {
391 password_type = "No-Password";
393 return password_type;
397 * Write a human readable authentication log entry.
400 static void log_authentication_event_human_readable(
401 const struct auth_usersupplied_info *ui,
403 const char *domain_name,
404 const char *account_name,
405 const char *unix_username,
409 TALLOC_CTX *frame = NULL;
411 const char *ts = NULL; /* formatted current time */
412 char *remote = NULL; /* formatted remote host */
413 char *local = NULL; /* formatted local host */
414 char *nl = NULL; /* NETLOGON details if present */
415 char *trust_computer_name = NULL;
416 char *trust_account_name = NULL;
417 char *logon_line = NULL;
418 const char *password_type = NULL;
420 frame = talloc_stackframe();
422 password_type = get_password_type(ui);
423 /* Get the current time */
424 ts = audit_get_timestamp(frame);
426 /* Only log the NETLOGON details if they are present */
427 if (ui->netlogon_trust_account.computer_name ||
428 ui->netlogon_trust_account.account_name) {
429 trust_computer_name = log_escape(frame,
430 ui->netlogon_trust_account.computer_name);
431 trust_account_name = log_escape(frame,
432 ui->netlogon_trust_account.account_name);
433 nl = talloc_asprintf(frame,
434 " NETLOGON computer [%s] trust account [%s]",
435 trust_computer_name, trust_account_name);
438 remote = tsocket_address_string(ui->remote_host, frame);
439 local = tsocket_address_string(ui->local_host, frame);
441 if (NT_STATUS_IS_OK(status)) {
442 char sid_buf[DOM_SID_STR_BUFLEN];
444 dom_sid_string_buf(sid, sid_buf, sizeof(sid_buf));
445 logon_line = talloc_asprintf(frame,
446 " became [%s]\\[%s] [%s].",
447 log_escape(frame, domain_name),
448 log_escape(frame, account_name),
451 logon_line = talloc_asprintf(
453 " mapped to [%s]\\[%s].",
454 log_escape(frame, ui->mapped.domain_name),
455 log_escape(frame, ui->mapped.account_name));
458 DEBUGC(DBGC_AUTH_AUDIT, debug_level,
459 ("Auth: [%s,%s] user [%s]\\[%s]"
460 " at [%s] with [%s] status [%s]"
461 " workstation [%s] remote host [%s]"
464 ui->service_description,
465 ui->auth_description,
466 log_escape(frame, ui->client.domain_name),
467 log_escape(frame, ui->client.account_name),
471 log_escape(frame, ui->workstation_name),
482 * Log details of an authentication attempt.
483 * Successful and unsuccessful attempts are logged.
485 * NOTE: msg_ctx and lp_ctx is optional, but when supplied allows streaming the
486 * authentication events over the message bus.
488 void log_authentication_event(
489 struct imessaging_context *msg_ctx,
490 struct loadparm_context *lp_ctx,
491 const struct auth_usersupplied_info *ui,
493 const char *domain_name,
494 const char *account_name,
495 const char *unix_username,
498 /* set the log level */
499 int debug_level = AUTH_FAILURE_LEVEL;
501 if (NT_STATUS_IS_OK(status)) {
502 debug_level = AUTH_SUCCESS_LEVEL;
503 if (dom_sid_equal(sid, &global_sid_Anonymous)) {
504 debug_level = AUTH_ANONYMOUS_LEVEL;
508 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT, debug_level)) {
509 log_authentication_event_human_readable(ui,
517 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT_JSON, debug_level) ||
518 (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx))) {
519 log_authentication_event_json(msg_ctx, lp_ctx,
533 * Log details of a successful authorization to a service,
534 * in a human readable format.
537 static void log_successful_authz_event_human_readable(
538 const struct tsocket_address *remote,
539 const struct tsocket_address *local,
540 const char *service_description,
541 const char *auth_type,
542 const char *transport_protection,
543 struct auth_session_info *session_info,
546 TALLOC_CTX *frame = NULL;
548 const char *ts = NULL; /* formatted current time */
549 char *remote_str = NULL; /* formatted remote host */
550 char *local_str = NULL; /* formatted local host */
551 char sid_buf[DOM_SID_STR_BUFLEN];
553 frame = talloc_stackframe();
555 /* Get the current time */
556 ts = audit_get_timestamp(frame);
558 remote_str = tsocket_address_string(remote, frame);
559 local_str = tsocket_address_string(local, frame);
561 dom_sid_string_buf(&session_info->security_token->sids[0],
565 DEBUGC(DBGC_AUTH_AUDIT, debug_level,
566 ("Successful AuthZ: [%s,%s] user [%s]\\[%s] [%s]"
569 " local host [%s]\n",
572 log_escape(frame, session_info->info->domain_name),
573 log_escape(frame, session_info->info->account_name),
583 * Log details of a successful authorization to a service.
585 * Only successful authorizations are logged. For clarity:
586 * - NTLM bad passwords will be recorded by log_authentication_event
587 * - Kerberos decrypt failures need to be logged in gensec_gssapi et al
589 * The service may later refuse authorization due to an ACL.
591 * NOTE: msg_ctx and lp_ctx is optional, but when supplied allows streaming the
592 * authentication events over the message bus.
594 void log_successful_authz_event(
595 struct imessaging_context *msg_ctx,
596 struct loadparm_context *lp_ctx,
597 const struct tsocket_address *remote,
598 const struct tsocket_address *local,
599 const char *service_description,
600 const char *auth_type,
601 const char *transport_protection,
602 struct auth_session_info *session_info)
604 int debug_level = AUTHZ_SUCCESS_LEVEL;
606 /* set the log level */
607 if (security_token_is_anonymous(session_info->security_token)) {
608 debug_level = AUTH_ANONYMOUS_LEVEL;
611 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT, debug_level)) {
612 log_successful_authz_event_human_readable(remote,
616 transport_protection,
620 if (CHECK_DEBUGLVLC(DBGC_AUTH_AUDIT_JSON, debug_level) ||
621 (msg_ctx && lp_ctx && lpcfg_auth_event_notification(lp_ctx))) {
622 log_successful_authz_event_json(msg_ctx, lp_ctx,
627 transport_protection,