c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen * NTLM and NTLMv2 authentication mechanism.
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen * Copyright (c) 2004 Andrey Panin <pazke@donpac.ru>
e074ffeaee1ce283bd42f167c6810e3d013f8218Timo Sirainen * This software is released under the MIT license.
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen /* requested: */
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen /* received: */
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainenstatic bool lm_verify_credentials(struct ntlm_auth_request *request,
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen const unsigned char *credentials, size_t size)
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen auth_request_log_error(&request->auth_request, AUTH_SUBSYS_MECH,
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen "invalid LM credentials length");
09ea3aa6bc03544a9e712d263f07976255aaaaf0Timo Sirainen ntlmssp_buffer_length(request->response, lm_response);
09ea3aa6bc03544a9e712d263f07976255aaaaf0Timo Sirainen client_response = ntlmssp_buffer_data(request->response, lm_response);
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen auth_request_log_error(&request->auth_request, AUTH_SUBSYS_MECH,
78c471cb3d6467b7bb889330a86912cb8493e0fdTimo Sirainen "LM response length is too small");
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen ntlmssp_v1_response(credentials, request->challenge, lm_response);
ace06232cfa0e99ecca1040e8553b3216d025768Timo Sirainen return mem_equals_timing_safe(lm_response, client_response, LM_RESPONSE_SIZE);
517d1e7142d57299c733b30423e35e7e1f8d01d6Timo Sirainenlm_credentials_callback(enum passdb_result result,
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen const unsigned char *credentials, size_t size,
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen if (lm_verify_credentials(request, credentials, size))
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainenntlm_verify_credentials(struct ntlm_auth_request *request,
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen const unsigned char *credentials, size_t size)
517d1e7142d57299c733b30423e35e7e1f8d01d6Timo Sirainen struct auth_request *auth_request = &request->auth_request;
09ea3aa6bc03544a9e712d263f07976255aaaaf0Timo Sirainen ntlmssp_buffer_length(request->response, ntlm_response);
09ea3aa6bc03544a9e712d263f07976255aaaaf0Timo Sirainen client_response = ntlmssp_buffer_data(request->response, ntlm_response);
517d1e7142d57299c733b30423e35e7e1f8d01d6Timo Sirainen /* try LM authentication unless NTLM2 was negotiated */
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen auth_request_log_error(&request->auth_request, AUTH_SUBSYS_MECH,
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen "invalid NTLM credentials length");
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen if (response_length > NTLMSSP_RESPONSE_SIZE) {
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen unsigned char ntlm_v2_response[NTLMSSP_V2_RESPONSE_SIZE];
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen * Authentication target == NULL because we are acting
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen * as a standalone server, not as NT domain member.
ace06232cfa0e99ecca1040e8553b3216d025768Timo Sirainen return mem_equals_timing_safe(ntlm_v2_response, client_response,
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen unsigned char ntlm_response[NTLMSSP_RESPONSE_SIZE];
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen ntlmssp_buffer_data(request->response, lm_response);
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen ntlmssp2_response(credentials, request->challenge,
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen ntlmssp_v1_response(credentials, request->challenge,
ace06232cfa0e99ecca1040e8553b3216d025768Timo Sirainen return mem_equals_timing_safe(ntlm_response, client_response,
517d1e7142d57299c733b30423e35e7e1f8d01d6Timo Sirainenntlm_credentials_callback(enum passdb_result result,
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen const unsigned char *credentials, size_t size,
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen ret = ntlm_verify_credentials(request, credentials, size);
517d1e7142d57299c733b30423e35e7e1f8d01d6Timo Sirainen /* NTLM credentials not found or didn't want to use them,
517d1e7142d57299c733b30423e35e7e1f8d01d6Timo Sirainen try with LM credentials */
a8e132559a7ebe54c8269d79ce29fa3338c76199Timo Sirainen auth_request_lookup_credentials(auth_request, "LANMAN",
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenmech_ntlm_auth_continue(struct auth_request *auth_request,
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen if (!ntlmssp_check_request(ntlm_request, data_size, &error)) {
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen auth_request_log_info(auth_request, AUTH_SUBSYS_MECH,
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen message = ntlmssp_create_challenge(request->pool, ntlm_request,
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen request->ntlm2_negotiated = (flags & NTLMSSP_NEGOTIATE_NTLM2) != 0;
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen request->unicode_negotiated = (flags & NTLMSSP_NEGOTIATE_UNICODE) != 0;
50782de8a9d5ebe11ee61496b4e695a1d3875230Timo Sirainen auth_request_handler_reply_continue(auth_request, message,
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen if (!ntlmssp_check_response(response, data_size, &error)) {
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen auth_request_log_info(auth_request, AUTH_SUBSYS_MECH,
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen request->response = p_malloc(request->pool, data_size);
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen memcpy(request->response, response, data_size);
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen username = ntlmssp_t_str(request->response, user,
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen if (!auth_request_set_username(auth_request, username, &error)) {
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen auth_request_log_info(auth_request, AUTH_SUBSYS_MECH,
a8e132559a7ebe54c8269d79ce29fa3338c76199Timo Sirainen auth_request_lookup_credentials(auth_request, "NTLM",
4ac5448461b63de9637de839fbc611a3d503287cTimo Sirainenstatic struct auth_request *mech_ntlm_auth_new(void)
1b81b28b2e7856748cffd7d01052a944b6c80b23Timo Sirainen pool = pool_alloconly_create(MEMPOOL_GROWING"ntlm_auth_request", 2048);
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen request = p_new(pool, struct ntlm_auth_request, 1);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen .flags = MECH_SEC_DICTIONARY | MECH_SEC_ACTIVE,