push e80a6a424f22991f7e391a74203fa6344d2429d0
[wine/hacks.git] / dlls / secur32 / ntlm.c
blob4ae00a4952f510b2c52120de3a2714e4c33e1c87
1 /*
2 * Copyright 2005, 2006 Kai Blin
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 * This file implements the NTLM security provider.
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "rpc.h"
28 #include "sspi.h"
29 #include "lm.h"
30 #include "secur32_priv.h"
31 #include "hmac_md5.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(ntlm);
36 #define NTLM_MAX_BUF 1904
37 #define MIN_NTLM_AUTH_MAJOR_VERSION 3
38 #define MIN_NTLM_AUTH_MINOR_VERSION 0
39 #define MIN_NTLM_AUTH_MICRO_VERSION 25
41 static CHAR ntlm_auth[] = "ntlm_auth";
43 typedef struct _NtlmCredentials
45 HelperMode mode;
47 /* these are all in the Unix codepage */
48 char *username_arg;
49 char *domain_arg;
50 char *password; /* not nul-terminated */
51 int pwlen;
52 } NtlmCredentials, *PNtlmCredentials;
54 /***********************************************************************
55 * QueryCredentialsAttributesA
57 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
58 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
60 SECURITY_STATUS ret;
62 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
64 if(ulAttribute == SECPKG_ATTR_NAMES)
66 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
67 ret = SEC_E_UNSUPPORTED_FUNCTION;
69 else
70 ret = SEC_E_UNSUPPORTED_FUNCTION;
72 return ret;
75 /***********************************************************************
76 * QueryCredentialsAttributesW
78 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
79 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
81 SECURITY_STATUS ret;
83 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
85 if(ulAttribute == SECPKG_ATTR_NAMES)
87 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
88 ret = SEC_E_UNSUPPORTED_FUNCTION;
90 else
91 ret = SEC_E_UNSUPPORTED_FUNCTION;
93 return ret;
96 /***********************************************************************
97 * AcquireCredentialsHandleW
99 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
100 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
101 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
102 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
104 SECURITY_STATUS ret;
105 PNtlmCredentials ntlm_cred = NULL;
106 SEC_WCHAR *username = NULL, *domain = NULL;
108 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
109 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
110 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
112 switch(fCredentialUse)
114 case SECPKG_CRED_INBOUND:
115 ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
116 if (!ntlm_cred)
117 ret = SEC_E_INSUFFICIENT_MEMORY;
118 else
120 ntlm_cred->mode = NTLM_SERVER;
121 ntlm_cred->username_arg = NULL;
122 ntlm_cred->domain_arg = NULL;
123 ntlm_cred->password = NULL;
124 ntlm_cred->pwlen = 0;
125 phCredential->dwUpper = fCredentialUse;
126 phCredential->dwLower = (ULONG_PTR)ntlm_cred;
127 ret = SEC_E_OK;
129 break;
130 case SECPKG_CRED_OUTBOUND:
132 static const char username_arg[] = "--username=";
133 static const char domain_arg[] = "--domain=";
134 int unixcp_size;
136 if(pAuthData == NULL)
138 LPWKSTA_USER_INFO_1 ui = NULL;
139 NET_API_STATUS status;
141 status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
142 if (status != NERR_Success || ui == NULL)
144 ret = SEC_E_NO_CREDENTIALS;
145 phCredential = NULL;
146 break;
149 username = HeapAlloc(GetProcessHeap(), 0,
150 (lstrlenW(ui->wkui1_username)+1) *
151 sizeof(SEC_WCHAR));
152 lstrcpyW(username, ui->wkui1_username);
154 /* same for the domain */
155 domain = HeapAlloc(GetProcessHeap(), 0,
156 (lstrlenW(ui->wkui1_logon_domain)+1) *
157 sizeof(SEC_WCHAR));
158 lstrcpyW(domain, ui->wkui1_logon_domain);
159 NetApiBufferFree(ui);
161 else
163 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
164 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
166 /* Get username and domain from pAuthData */
167 username = HeapAlloc(GetProcessHeap(), 0,
168 (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
169 memcpy(username, auth_data->User,
170 auth_data->UserLength * sizeof(SEC_WCHAR));
171 username[auth_data->UserLength] = '\0';
173 domain = HeapAlloc(GetProcessHeap(), 0,
174 (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
175 memcpy(domain, auth_data->Domain,
176 auth_data->DomainLength * sizeof(SEC_WCHAR));
177 domain[auth_data->DomainLength] = '\0';
180 ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
181 if (!ntlm_cred)
183 ret = SEC_E_INSUFFICIENT_MEMORY;
184 break;
186 ntlm_cred->mode = NTLM_CLIENT;
187 ntlm_cred->password = NULL;
188 ntlm_cred->pwlen = 0;
190 TRACE("Username is %s\n", debugstr_w(username));
191 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
192 username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
193 ntlm_cred->username_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
194 memcpy(ntlm_cred->username_arg, username_arg, sizeof(username_arg) - 1);
195 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
196 ntlm_cred->username_arg + sizeof(username_arg) - 1,
197 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
199 TRACE("Domain name is %s\n", debugstr_w(domain));
200 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
201 domain, -1, NULL, 0, NULL, NULL) + sizeof(domain_arg);
202 ntlm_cred->domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
203 memcpy(ntlm_cred->domain_arg, domain_arg, sizeof(domain_arg) - 1);
204 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
205 -1, ntlm_cred->domain_arg + sizeof(domain_arg) - 1,
206 unixcp_size - sizeof(domain) + 1, NULL, NULL);
208 if(pAuthData != NULL)
210 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
211 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
213 if(auth_data->PasswordLength != 0)
215 ntlm_cred->pwlen = WideCharToMultiByte(CP_UNIXCP,
216 WC_NO_BEST_FIT_CHARS, auth_data->Password,
217 auth_data->PasswordLength, NULL, 0, NULL,
218 NULL);
220 ntlm_cred->password = HeapAlloc(GetProcessHeap(), 0,
221 ntlm_cred->pwlen);
223 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
224 auth_data->Password, auth_data->PasswordLength,
225 ntlm_cred->password, ntlm_cred->pwlen, NULL, NULL);
229 phCredential->dwUpper = fCredentialUse;
230 phCredential->dwLower = (ULONG_PTR)ntlm_cred;
231 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
232 phCredential->dwUpper, phCredential->dwLower);
233 ret = SEC_E_OK;
234 break;
236 case SECPKG_CRED_BOTH:
237 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
238 ret = SEC_E_UNSUPPORTED_FUNCTION;
239 phCredential = NULL;
240 break;
241 default:
242 phCredential = NULL;
243 ret = SEC_E_UNKNOWN_CREDENTIALS;
246 HeapFree(GetProcessHeap(), 0, username);
247 HeapFree(GetProcessHeap(), 0, domain);
249 return ret;
252 /***********************************************************************
253 * AcquireCredentialsHandleA
255 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
256 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
257 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
258 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
260 SECURITY_STATUS ret;
261 int user_sizeW, domain_sizeW, passwd_sizeW;
263 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
265 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
266 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
268 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
269 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
270 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
272 if(pszPackage != NULL)
274 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
275 NULL, 0);
277 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
278 sizeof(SEC_WCHAR));
279 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
283 if(pAuthData != NULL)
285 identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
287 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
289 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
290 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
292 if(identity->UserLength != 0)
294 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
295 (LPCSTR)identity->User, identity->UserLength, NULL, 0);
296 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
297 sizeof(SEC_WCHAR));
298 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
299 identity->UserLength, user, user_sizeW);
301 else
303 user_sizeW = 0;
306 if(identity->DomainLength != 0)
308 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
309 (LPCSTR)identity->Domain, identity->DomainLength, NULL, 0);
310 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
311 * sizeof(SEC_WCHAR));
312 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
313 identity->DomainLength, domain, domain_sizeW);
315 else
317 domain_sizeW = 0;
320 if(identity->PasswordLength != 0)
322 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
323 (LPCSTR)identity->Password, identity->PasswordLength,
324 NULL, 0);
325 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
326 * sizeof(SEC_WCHAR));
327 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
328 identity->PasswordLength, passwd, passwd_sizeW);
330 else
332 passwd_sizeW = 0;
335 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
336 pAuthDataW->User = user;
337 pAuthDataW->UserLength = user_sizeW;
338 pAuthDataW->Domain = domain;
339 pAuthDataW->DomainLength = domain_sizeW;
340 pAuthDataW->Password = passwd;
341 pAuthDataW->PasswordLength = passwd_sizeW;
343 else
345 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
349 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
350 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
351 ptsExpiry);
353 HeapFree(GetProcessHeap(), 0, package);
354 HeapFree(GetProcessHeap(), 0, user);
355 HeapFree(GetProcessHeap(), 0, domain);
356 HeapFree(GetProcessHeap(), 0, passwd);
357 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
358 HeapFree(GetProcessHeap(), 0, pAuthDataW);
360 return ret;
363 /*************************************************************************
364 * ntlm_GetTokenBufferIndex
365 * Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
366 * Returns index if found or -1 if not found.
368 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
370 UINT i;
372 TRACE("%p\n", pMessage);
374 for( i = 0; i < pMessage->cBuffers; ++i )
376 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
377 return i;
380 return -1;
383 /*************************************************************************
384 * ntlm_GetDataBufferIndex
385 * Calculates the index of the first secbuffer with BufferType == SECBUFFER_DATA
386 * Returns index if found or -1 if not found.
388 static int ntlm_GetDataBufferIndex(PSecBufferDesc pMessage)
390 UINT i;
392 TRACE("%p\n", pMessage);
394 for( i = 0; i < pMessage->cBuffers; ++i )
396 if(pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
397 return i;
400 return -1;
403 /***********************************************************************
404 * InitializeSecurityContextW
406 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
407 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
408 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
409 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
410 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
412 SECURITY_STATUS ret;
413 PNtlmCredentials ntlm_cred = NULL;
414 PNegoHelper helper = NULL;
415 ULONG ctxt_attr = 0;
416 char* buffer, *want_flags = NULL;
417 PBYTE bin;
418 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
419 int token_idx;
421 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
422 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
423 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
425 /****************************************
426 * When communicating with the client, there can be the
427 * following reply packets:
428 * YR <base64 blob> should be sent to the server
429 * PW should be sent back to helper with
430 * base64 encoded password
431 * AF <base64 blob> client is done, blob should be
432 * sent to server with KK prefixed
433 * GF <string list> A string list of negotiated flags
434 * GK <base64 blob> base64 encoded session key
435 * BH <char reason> something broke
437 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
439 if (pszTargetName)
441 TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
444 if(TargetDataRep == SECURITY_NETWORK_DREP){
445 TRACE("Setting SECURITY_NETWORK_DREP\n");
448 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
449 bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
451 if((phContext == NULL) && (pInput == NULL))
453 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
454 static CHAR credentials_argv[] = "--use-cached-creds";
455 SEC_CHAR *client_argv[6];
457 TRACE("First time in ISC()\n");
459 if(!phCredential)
461 ret = SEC_E_INVALID_HANDLE;
462 goto isc_end;
465 /* As the server side of sspi never calls this, make sure that
466 * the handler is a client handler.
468 ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
469 if(ntlm_cred->mode != NTLM_CLIENT)
471 TRACE("Cred mode = %d\n", ntlm_cred->mode);
472 ret = SEC_E_INVALID_HANDLE;
473 goto isc_end;
476 client_argv[0] = ntlm_auth;
477 client_argv[1] = helper_protocol;
478 client_argv[2] = ntlm_cred->username_arg;
479 client_argv[3] = ntlm_cred->domain_arg;
480 client_argv[4] = credentials_argv;
481 client_argv[5] = NULL;
483 if((ret = fork_helper(&helper, ntlm_auth, client_argv)) != SEC_E_OK)
484 goto isc_end;
486 helper->mode = NTLM_CLIENT;
487 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
488 if (!helper->session_key)
490 cleanup_helper(helper);
491 ret = SEC_E_INSUFFICIENT_MEMORY;
492 goto isc_end;
495 /* Generate the dummy session key = MD4(MD4(password))*/
496 if(ntlm_cred->password)
498 SEC_WCHAR *unicode_password;
499 int passwd_lenW;
501 TRACE("Converting password to unicode.\n");
502 passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
503 (LPCSTR)ntlm_cred->password, ntlm_cred->pwlen,
504 NULL, 0);
505 unicode_password = HeapAlloc(GetProcessHeap(), 0,
506 passwd_lenW * sizeof(SEC_WCHAR));
507 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)ntlm_cred->password,
508 ntlm_cred->pwlen, unicode_password, passwd_lenW);
510 SECUR32_CreateNTLMv1SessionKey((PBYTE)unicode_password,
511 passwd_lenW * sizeof(SEC_WCHAR), helper->session_key);
513 HeapFree(GetProcessHeap(), 0, unicode_password);
515 else
516 memset(helper->session_key, 0, 16);
518 /* Allocate space for a maximal string of
519 * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
520 * NTLMSSP_FEATURE_SESSION_KEY"
522 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
523 if(want_flags == NULL)
525 cleanup_helper(helper);
526 ret = SEC_E_INSUFFICIENT_MEMORY;
527 goto isc_end;
529 lstrcpyA(want_flags, "SF");
530 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
532 char *ptr;
533 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SEAL")) == NULL)
534 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
536 if(fContextReq & ISC_REQ_CONNECTION)
537 ctxt_attr |= ISC_RET_CONNECTION;
538 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
539 ctxt_attr |= ISC_RET_EXTENDED_ERROR;
540 if(fContextReq & ISC_REQ_INTEGRITY)
542 char *ptr;
543 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
544 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
546 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
547 ctxt_attr |= ISC_RET_MUTUAL_AUTH;
548 if(fContextReq & ISC_REQ_REPLAY_DETECT)
550 char *ptr;
551 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
552 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
554 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
556 char *ptr;
557 if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
558 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
560 if(fContextReq & ISC_REQ_STREAM)
561 FIXME("ISC_REQ_STREAM\n");
562 if(fContextReq & ISC_REQ_USE_DCE_STYLE)
563 ctxt_attr |= ISC_RET_USED_DCE_STYLE;
564 if(fContextReq & ISC_REQ_DELEGATE)
565 ctxt_attr |= ISC_RET_DELEGATE;
567 /* If no password is given, try to use cached credentials. Fall back to an empty
568 * password if this failed. */
569 if(ntlm_cred->password == NULL)
571 lstrcpynA(buffer, "OK", max_len-1);
572 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
574 cleanup_helper(helper);
575 goto isc_end;
577 /* If the helper replied with "PW", using cached credentials failed */
578 if(!strncmp(buffer, "PW", 2))
580 TRACE("Using cached credentials failed. Using empty password.\n");
581 lstrcpynA(buffer, "PW AA==", max_len-1);
583 else /* Just do a noop on the next run */
584 lstrcpynA(buffer, "OK", max_len-1);
586 else
588 lstrcpynA(buffer, "PW ", max_len-1);
589 if((ret = encodeBase64((unsigned char*)ntlm_cred->password,
590 ntlm_cred->pwlen, buffer+3,
591 max_len-3, &buffer_len)) != SEC_E_OK)
593 cleanup_helper(helper);
594 goto isc_end;
599 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
600 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
602 cleanup_helper(helper);
603 goto isc_end;
606 TRACE("Helper returned %s\n", debugstr_a(buffer));
608 if(lstrlenA(want_flags) > 2)
610 TRACE("Want flags are %s\n", debugstr_a(want_flags));
611 lstrcpynA(buffer, want_flags, max_len-1);
612 if((ret = run_helper(helper, buffer, max_len, &buffer_len))
613 != SEC_E_OK)
614 goto isc_end;
615 if(!strncmp(buffer, "BH", 2))
616 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
619 lstrcpynA(buffer, "YR", max_len-1);
621 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
623 cleanup_helper(helper);
624 goto isc_end;
627 TRACE("%s\n", buffer);
629 if(strncmp(buffer, "YR ", 3) != 0)
631 /* Something borked */
632 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
633 ret = SEC_E_INTERNAL_ERROR;
634 cleanup_helper(helper);
635 goto isc_end;
637 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
638 max_len-1, &bin_len)) != SEC_E_OK)
640 cleanup_helper(helper);
641 goto isc_end;
644 /* put the decoded client blob into the out buffer */
646 phNewContext->dwUpper = ctxt_attr;
647 phNewContext->dwLower = (ULONG_PTR)helper;
649 ret = SEC_I_CONTINUE_NEEDED;
651 else
653 int input_token_idx;
655 /* handle second call here */
656 /* encode server data to base64 */
657 if (!pInput || ((input_token_idx = ntlm_GetTokenBufferIndex(pInput)) == -1))
659 ret = SEC_E_INVALID_TOKEN;
660 goto isc_end;
663 if(!phContext)
665 ret = SEC_E_INVALID_HANDLE;
666 goto isc_end;
669 /* As the server side of sspi never calls this, make sure that
670 * the handler is a client handler.
672 helper = (PNegoHelper)phContext->dwLower;
673 if(helper->mode != NTLM_CLIENT)
675 TRACE("Helper mode = %d\n", helper->mode);
676 ret = SEC_E_INVALID_HANDLE;
677 goto isc_end;
680 if (!pInput->pBuffers[input_token_idx].pvBuffer)
682 ret = SEC_E_INTERNAL_ERROR;
683 goto isc_end;
686 if(pInput->pBuffers[input_token_idx].cbBuffer > max_len)
688 TRACE("pInput->pBuffers[%d].cbBuffer is: %ld\n",
689 input_token_idx,
690 pInput->pBuffers[input_token_idx].cbBuffer);
691 ret = SEC_E_INVALID_TOKEN;
692 goto isc_end;
694 else
695 bin_len = pInput->pBuffers[input_token_idx].cbBuffer;
697 memcpy(bin, pInput->pBuffers[input_token_idx].pvBuffer, bin_len);
699 lstrcpynA(buffer, "TT ", max_len-1);
701 if((ret = encodeBase64(bin, bin_len, buffer+3,
702 max_len-3, &buffer_len)) != SEC_E_OK)
703 goto isc_end;
705 TRACE("Server sent: %s\n", debugstr_a(buffer));
707 /* send TT base64 blob to ntlm_auth */
708 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
709 goto isc_end;
711 TRACE("Helper replied: %s\n", debugstr_a(buffer));
713 if( (strncmp(buffer, "KK ", 3) != 0) &&
714 (strncmp(buffer, "AF ", 3) !=0))
716 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
717 ret = SEC_E_INVALID_TOKEN;
718 goto isc_end;
721 /* decode the blob and send it to server */
722 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
723 &bin_len)) != SEC_E_OK)
725 goto isc_end;
728 phNewContext->dwUpper = ctxt_attr;
729 phNewContext->dwLower = (ULONG_PTR)helper;
731 ret = SEC_E_OK;
734 /* put the decoded client blob into the out buffer */
736 if (!pOutput || ((token_idx = ntlm_GetTokenBufferIndex(pOutput)) == -1))
738 TRACE("no SECBUFFER_TOKEN buffer could be found\n");
739 ret = SEC_E_BUFFER_TOO_SMALL;
740 if ((phContext == NULL) && (pInput == NULL))
742 cleanup_helper(helper);
743 phNewContext->dwUpper = 0;
744 phNewContext->dwLower = 0;
746 goto isc_end;
749 if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
751 pOutput->pBuffers[token_idx].pvBuffer = SECUR32_ALLOC(bin_len);
752 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
754 else if (pOutput->pBuffers[token_idx].cbBuffer < bin_len)
756 TRACE("out buffer is NULL or has not enough space\n");
757 ret = SEC_E_BUFFER_TOO_SMALL;
758 if ((phContext == NULL) && (pInput == NULL))
760 cleanup_helper(helper);
761 phNewContext->dwUpper = 0;
762 phNewContext->dwLower = 0;
764 goto isc_end;
767 if (!pOutput->pBuffers[token_idx].pvBuffer)
769 TRACE("out buffer is NULL\n");
770 ret = SEC_E_INTERNAL_ERROR;
771 if ((phContext == NULL) && (pInput == NULL))
773 cleanup_helper(helper);
774 phNewContext->dwUpper = 0;
775 phNewContext->dwLower = 0;
777 goto isc_end;
780 pOutput->pBuffers[token_idx].cbBuffer = bin_len;
781 memcpy(pOutput->pBuffers[token_idx].pvBuffer, bin, bin_len);
783 if(ret == SEC_E_OK)
785 TRACE("Getting negotiated flags\n");
786 lstrcpynA(buffer, "GF", max_len - 1);
787 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
788 goto isc_end;
790 if(buffer_len < 3)
792 TRACE("No flags negotiated.\n");
793 helper->neg_flags = 0l;
795 else
797 TRACE("Negotiated %s\n", debugstr_a(buffer));
798 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
799 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
802 TRACE("Getting session key\n");
803 lstrcpynA(buffer, "GK", max_len - 1);
804 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
805 goto isc_end;
807 if(strncmp(buffer, "BH", 2) == 0)
808 TRACE("No key negotiated.\n");
809 else if(strncmp(buffer, "GK ", 3) == 0)
811 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
812 &bin_len)) != SEC_E_OK)
814 TRACE("Failed to decode session key\n");
816 TRACE("Session key is %s\n", debugstr_a(buffer+3));
817 HeapFree(GetProcessHeap(), 0, helper->session_key);
818 helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
819 if(!helper->session_key)
821 TRACE("Failed to allocate memory for session key\n");
822 ret = SEC_E_INTERNAL_ERROR;
823 goto isc_end;
825 memcpy(helper->session_key, bin, bin_len);
828 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
829 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
830 helper->crypt.ntlm.seq_num = 0l;
831 SECUR32_CreateNTLMv2SubKeys(helper);
832 helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
833 helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
834 SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
835 (BYTE *)helper->crypt.ntlm2.send_seal_key, 16);
836 SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
837 (BYTE *)helper->crypt.ntlm2.recv_seal_key, 16);
838 helper->crypt.ntlm2.send_seq_no = 0l;
839 helper->crypt.ntlm2.recv_seq_no = 0l;
842 isc_end:
843 HeapFree(GetProcessHeap(), 0, want_flags);
844 HeapFree(GetProcessHeap(), 0, buffer);
845 HeapFree(GetProcessHeap(), 0, bin);
846 return ret;
849 /***********************************************************************
850 * InitializeSecurityContextA
852 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
853 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
854 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
855 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
856 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
858 SECURITY_STATUS ret;
859 SEC_WCHAR *target = NULL;
861 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
862 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
863 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
865 if(pszTargetName != NULL)
867 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
868 strlen(pszTargetName)+1, NULL, 0);
869 target = HeapAlloc(GetProcessHeap(), 0, target_size *
870 sizeof(SEC_WCHAR));
871 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
872 target, target_size);
875 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
876 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
877 phNewContext, pOutput, pfContextAttr, ptsExpiry);
879 HeapFree(GetProcessHeap(), 0, target);
880 return ret;
883 /***********************************************************************
884 * AcceptSecurityContext
886 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
887 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
888 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
889 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
891 SECURITY_STATUS ret;
892 char *buffer, *want_flags = NULL;
893 PBYTE bin;
894 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
895 ULONG ctxt_attr = 0;
896 PNegoHelper helper;
897 PNtlmCredentials ntlm_cred;
899 TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
900 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
901 ptsExpiry);
903 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
904 bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
906 if(TargetDataRep == SECURITY_NETWORK_DREP){
907 TRACE("Using SECURITY_NETWORK_DREP\n");
910 if(phContext == NULL)
912 static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
913 SEC_CHAR *server_argv[] = { ntlm_auth,
914 server_helper_protocol,
915 NULL };
917 if (!phCredential)
919 ret = SEC_E_INVALID_HANDLE;
920 goto asc_end;
923 ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
925 if(ntlm_cred->mode != NTLM_SERVER)
927 ret = SEC_E_INVALID_HANDLE;
928 goto asc_end;
931 /* This is the first call to AcceptSecurityHandle */
932 if(pInput == NULL)
934 ret = SEC_E_INCOMPLETE_MESSAGE;
935 goto asc_end;
938 if(pInput->cBuffers < 1)
940 ret = SEC_E_INCOMPLETE_MESSAGE;
941 goto asc_end;
944 if(pInput->pBuffers[0].cbBuffer > max_len)
946 ret = SEC_E_INVALID_TOKEN;
947 goto asc_end;
949 else
950 bin_len = pInput->pBuffers[0].cbBuffer;
952 if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
953 SEC_E_OK)
955 ret = SEC_E_INTERNAL_ERROR;
956 goto asc_end;
958 helper->mode = NTLM_SERVER;
960 /* Handle all the flags */
961 want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
962 if(want_flags == NULL)
964 TRACE("Failed to allocate memory for the want_flags!\n");
965 ret = SEC_E_INSUFFICIENT_MEMORY;
966 cleanup_helper(helper);
967 goto asc_end;
969 lstrcpyA(want_flags, "SF");
970 if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
972 FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
974 if(fContextReq & ASC_REQ_CONFIDENTIALITY)
976 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
978 if(fContextReq & ASC_REQ_CONNECTION)
980 /* This is default, so we'll enable it */
981 lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
982 ctxt_attr |= ASC_RET_CONNECTION;
984 if(fContextReq & ASC_REQ_EXTENDED_ERROR)
986 FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
988 if(fContextReq & ASC_REQ_INTEGRITY)
990 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
992 if(fContextReq & ASC_REQ_MUTUAL_AUTH)
994 FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
996 if(fContextReq & ASC_REQ_REPLAY_DETECT)
998 FIXME("ASC_REQ_REPLAY_DETECT stub\n");
1000 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
1002 FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
1004 if(fContextReq & ISC_REQ_STREAM)
1006 FIXME("ASC_REQ_STREAM stub\n");
1008 /* Done with the flags */
1010 if(lstrlenA(want_flags) > 3)
1012 TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
1013 lstrcpynA(buffer, want_flags, max_len - 1);
1014 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1015 SEC_E_OK)
1017 cleanup_helper(helper);
1018 goto asc_end;
1020 if(!strncmp(buffer, "BH", 2))
1021 TRACE("Helper doesn't understand new command set\n");
1024 /* This is the YR request from the client, encode to base64 */
1026 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1028 lstrcpynA(buffer, "YR ", max_len-1);
1030 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1031 &buffer_len)) != SEC_E_OK)
1033 cleanup_helper(helper);
1034 goto asc_end;
1037 TRACE("Client sent: %s\n", debugstr_a(buffer));
1039 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1040 SEC_E_OK)
1042 cleanup_helper(helper);
1043 goto asc_end;
1046 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1047 /* The expected answer is TT <base64 blob> */
1049 if(strncmp(buffer, "TT ", 3) != 0)
1051 ret = SEC_E_INTERNAL_ERROR;
1052 cleanup_helper(helper);
1053 goto asc_end;
1056 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1057 &bin_len)) != SEC_E_OK)
1059 cleanup_helper(helper);
1060 goto asc_end;
1063 /* send this to the client */
1064 if(pOutput == NULL)
1066 ret = SEC_E_INSUFFICIENT_MEMORY;
1067 cleanup_helper(helper);
1068 goto asc_end;
1071 if(pOutput->cBuffers < 1)
1073 ret = SEC_E_INSUFFICIENT_MEMORY;
1074 cleanup_helper(helper);
1075 goto asc_end;
1078 pOutput->pBuffers[0].cbBuffer = bin_len;
1079 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
1080 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
1081 ret = SEC_I_CONTINUE_NEEDED;
1084 else
1086 /* we expect a KK request from client */
1087 if(pInput == NULL)
1089 ret = SEC_E_INCOMPLETE_MESSAGE;
1090 goto asc_end;
1093 if(pInput->cBuffers < 1)
1095 ret = SEC_E_INCOMPLETE_MESSAGE;
1096 goto asc_end;
1099 if(!phContext)
1101 ret = SEC_E_INVALID_HANDLE;
1102 goto asc_end;
1105 helper = (PNegoHelper)phContext->dwLower;
1107 if(helper->mode != NTLM_SERVER)
1109 ret = SEC_E_INVALID_HANDLE;
1110 goto asc_end;
1113 if(pInput->pBuffers[0].cbBuffer > max_len)
1115 ret = SEC_E_INVALID_TOKEN;
1116 goto asc_end;
1118 else
1119 bin_len = pInput->pBuffers[0].cbBuffer;
1121 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1123 lstrcpynA(buffer, "KK ", max_len-1);
1125 if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1126 &buffer_len)) != SEC_E_OK)
1128 goto asc_end;
1131 TRACE("Client sent: %s\n", debugstr_a(buffer));
1133 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1134 SEC_E_OK)
1136 goto asc_end;
1139 TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1141 if(strncmp(buffer, "AF ", 3) != 0)
1143 if(strncmp(buffer, "NA ", 3) == 0)
1145 ret = SEC_E_LOGON_DENIED;
1146 goto asc_end;
1148 else
1150 ret = SEC_E_INTERNAL_ERROR;
1151 goto asc_end;
1154 pOutput->pBuffers[0].cbBuffer = 0;
1155 ret = SEC_E_OK;
1157 TRACE("Getting negotiated flags\n");
1158 lstrcpynA(buffer, "GF", max_len - 1);
1159 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1160 goto asc_end;
1162 if(buffer_len < 3)
1164 TRACE("No flags negotiated, or helper does not support GF command\n");
1166 else
1168 TRACE("Negotiated %s\n", debugstr_a(buffer));
1169 sscanf(buffer + 3, "%lx", &(helper->neg_flags));
1170 TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
1173 TRACE("Getting session key\n");
1174 lstrcpynA(buffer, "GK", max_len - 1);
1175 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1176 goto asc_end;
1178 if(buffer_len < 3)
1179 TRACE("Helper does not support GK command\n");
1180 else
1182 if(strncmp(buffer, "BH ", 3) == 0)
1184 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1185 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1186 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1187 memset(helper->session_key, 0 , 16);
1189 else if(strncmp(buffer, "GK ", 3) == 0)
1191 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1192 &bin_len)) != SEC_E_OK)
1194 TRACE("Failed to decode session key\n");
1196 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1197 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1198 if(!helper->session_key)
1200 TRACE("Failed to allocate memory for session key\n");
1201 ret = SEC_E_INTERNAL_ERROR;
1202 goto asc_end;
1204 memcpy(helper->session_key, bin, 16);
1207 helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1208 SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1209 helper->crypt.ntlm.seq_num = 0l;
1212 phNewContext->dwUpper = ctxt_attr;
1213 phNewContext->dwLower = (ULONG_PTR)helper;
1215 asc_end:
1216 HeapFree(GetProcessHeap(), 0, want_flags);
1217 HeapFree(GetProcessHeap(), 0, buffer);
1218 HeapFree(GetProcessHeap(), 0, bin);
1219 return ret;
1222 /***********************************************************************
1223 * CompleteAuthToken
1225 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1226 PSecBufferDesc pToken)
1228 /* We never need to call CompleteAuthToken anyway */
1229 TRACE("%p %p\n", phContext, pToken);
1230 if (!phContext)
1231 return SEC_E_INVALID_HANDLE;
1233 return SEC_E_OK;
1236 /***********************************************************************
1237 * DeleteSecurityContext
1239 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1241 PNegoHelper helper;
1243 TRACE("%p\n", phContext);
1244 if (!phContext)
1245 return SEC_E_INVALID_HANDLE;
1247 helper = (PNegoHelper)phContext->dwLower;
1249 phContext->dwUpper = 0;
1250 phContext->dwLower = 0;
1252 SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1253 HeapFree(GetProcessHeap(), 0, helper->session_key);
1254 SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1255 SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1256 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1257 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1258 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1259 HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1261 cleanup_helper(helper);
1263 return SEC_E_OK;
1266 /***********************************************************************
1267 * QueryContextAttributesW
1269 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1270 ULONG ulAttribute, void *pBuffer)
1272 TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1273 if (!phContext)
1274 return SEC_E_INVALID_HANDLE;
1276 switch(ulAttribute)
1278 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1279 _x(SECPKG_ATTR_ACCESS_TOKEN);
1280 _x(SECPKG_ATTR_AUTHORITY);
1281 _x(SECPKG_ATTR_DCE_INFO);
1282 case SECPKG_ATTR_FLAGS:
1284 PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1285 PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1287 spcf->Flags = 0;
1288 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1289 spcf->Flags |= ISC_RET_INTEGRITY;
1290 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1291 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1292 return SEC_E_OK;
1294 _x(SECPKG_ATTR_KEY_INFO);
1295 _x(SECPKG_ATTR_LIFESPAN);
1296 _x(SECPKG_ATTR_NAMES);
1297 _x(SECPKG_ATTR_NATIVE_NAMES);
1298 _x(SECPKG_ATTR_NEGOTIATION_INFO);
1299 _x(SECPKG_ATTR_PACKAGE_INFO);
1300 _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1301 _x(SECPKG_ATTR_SESSION_KEY);
1302 case SECPKG_ATTR_SIZES:
1304 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1305 spcs->cbMaxToken = NTLM_MAX_BUF;
1306 spcs->cbMaxSignature = 16;
1307 spcs->cbBlockSize = 0;
1308 spcs->cbSecurityTrailer = 16;
1309 return SEC_E_OK;
1311 _x(SECPKG_ATTR_STREAM_SIZES);
1312 _x(SECPKG_ATTR_TARGET_INFORMATION);
1313 #undef _x
1314 default:
1315 TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1318 return SEC_E_UNSUPPORTED_FUNCTION;
1321 /***********************************************************************
1322 * QueryContextAttributesA
1324 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1325 ULONG ulAttribute, void *pBuffer)
1327 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1330 /***********************************************************************
1331 * ImpersonateSecurityContext
1333 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1335 SECURITY_STATUS ret;
1337 TRACE("%p\n", phContext);
1338 if (phContext)
1340 ret = SEC_E_UNSUPPORTED_FUNCTION;
1342 else
1344 ret = SEC_E_INVALID_HANDLE;
1346 return ret;
1349 /***********************************************************************
1350 * RevertSecurityContext
1352 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1354 SECURITY_STATUS ret;
1356 TRACE("%p\n", phContext);
1357 if (phContext)
1359 ret = SEC_E_UNSUPPORTED_FUNCTION;
1361 else
1363 ret = SEC_E_INVALID_HANDLE;
1365 return ret;
1368 /***********************************************************************
1369 * ntlm_CreateSignature
1370 * As both MakeSignature and VerifySignature need this, but different keys
1371 * are needed for NTLMv2, the logic goes into a helper function.
1372 * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1373 * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1374 * the signature is encrypted after the message was encrypted, so
1375 * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1376 * false.
1378 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1379 int token_idx, SignDirection direction, BOOL encrypt_sig)
1381 ULONG sign_version = 1;
1382 UINT i;
1383 PBYTE sig;
1384 TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1385 encrypt_sig);
1387 sig = pMessage->pBuffers[token_idx].pvBuffer;
1389 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1390 helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1392 BYTE digest[16];
1393 BYTE seq_no[4];
1394 HMAC_MD5_CTX hmac_md5_ctx;
1396 TRACE("Signing NTLM2 style\n");
1398 if(direction == NTLM_SEND)
1400 seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
1401 seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
1402 seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1403 seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1405 ++(helper->crypt.ntlm2.send_seq_no);
1407 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1409 else
1411 seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
1412 seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
1413 seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1414 seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1416 ++(helper->crypt.ntlm2.recv_seq_no);
1418 HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1421 HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1422 for( i = 0; i < pMessage->cBuffers; ++i )
1424 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1425 HMACMD5Update(&hmac_md5_ctx, (BYTE *)pMessage->pBuffers[i].pvBuffer,
1426 pMessage->pBuffers[i].cbBuffer);
1429 HMACMD5Final(&hmac_md5_ctx, digest);
1431 if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1433 if(direction == NTLM_SEND)
1434 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1435 else
1436 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1439 /* The NTLM2 signature is the sign version */
1440 sig[ 0] = (sign_version >> 0) & 0xff;
1441 sig[ 1] = (sign_version >> 8) & 0xff;
1442 sig[ 2] = (sign_version >> 16) & 0xff;
1443 sig[ 3] = (sign_version >> 24) & 0xff;
1444 /* The first 8 bytes of the digest */
1445 memcpy(sig+4, digest, 8);
1446 /* And the sequence number */
1447 memcpy(sig+12, seq_no, 4);
1449 pMessage->pBuffers[token_idx].cbBuffer = 16;
1451 return SEC_E_OK;
1453 if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1455 ULONG crc = 0U;
1456 TRACE("Signing NTLM1 style\n");
1458 for(i=0; i < pMessage->cBuffers; ++i)
1460 if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1462 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1463 pMessage->pBuffers[i].cbBuffer, crc);
1467 sig[ 0] = (sign_version >> 0) & 0xff;
1468 sig[ 1] = (sign_version >> 8) & 0xff;
1469 sig[ 2] = (sign_version >> 16) & 0xff;
1470 sig[ 3] = (sign_version >> 24) & 0xff;
1471 memset(sig+4, 0, 4);
1472 sig[ 8] = (crc >> 0) & 0xff;
1473 sig[ 9] = (crc >> 8) & 0xff;
1474 sig[10] = (crc >> 16) & 0xff;
1475 sig[11] = (crc >> 24) & 0xff;
1476 sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
1477 sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
1478 sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1479 sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1481 ++(helper->crypt.ntlm.seq_num);
1483 if(encrypt_sig)
1484 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1485 return SEC_E_OK;
1488 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1490 TRACE("Creating a dummy signature.\n");
1491 /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1492 memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1493 memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1494 pMessage->pBuffers[token_idx].cbBuffer = 16;
1495 return SEC_E_OK;
1498 return SEC_E_UNSUPPORTED_FUNCTION;
1501 /***********************************************************************
1502 * MakeSignature
1504 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1505 PSecBufferDesc pMessage, ULONG MessageSeqNo)
1507 PNegoHelper helper;
1508 int token_idx;
1510 TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1511 if (!phContext)
1512 return SEC_E_INVALID_HANDLE;
1514 if(fQOP)
1515 FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1517 if(MessageSeqNo)
1518 FIXME("Ignoring MessageSeqNo\n");
1520 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1521 return SEC_E_INVALID_TOKEN;
1523 /* If we didn't find a SECBUFFER_TOKEN type buffer */
1524 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1525 return SEC_E_INVALID_TOKEN;
1527 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1528 return SEC_E_BUFFER_TOO_SMALL;
1530 helper = (PNegoHelper)phContext->dwLower;
1531 TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1533 return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1536 /***********************************************************************
1537 * VerifySignature
1539 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1540 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1542 PNegoHelper helper;
1543 ULONG fQOP = 0;
1544 UINT i;
1545 int token_idx;
1546 SECURITY_STATUS ret;
1547 SecBufferDesc local_desc;
1548 PSecBuffer local_buff;
1549 BYTE local_sig[16];
1551 TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1552 if(!phContext)
1553 return SEC_E_INVALID_HANDLE;
1555 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1556 return SEC_E_INVALID_TOKEN;
1558 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1559 return SEC_E_INVALID_TOKEN;
1561 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1562 return SEC_E_BUFFER_TOO_SMALL;
1564 if(MessageSeqNo)
1565 FIXME("Ignoring MessageSeqNo\n");
1567 helper = (PNegoHelper)phContext->dwLower;
1568 TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1570 local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1572 local_desc.ulVersion = SECBUFFER_VERSION;
1573 local_desc.cBuffers = pMessage->cBuffers;
1574 local_desc.pBuffers = local_buff;
1576 for(i=0; i < pMessage->cBuffers; ++i)
1578 if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1580 local_buff[i].BufferType = SECBUFFER_TOKEN;
1581 local_buff[i].cbBuffer = 16;
1582 local_buff[i].pvBuffer = local_sig;
1584 else
1586 local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1587 local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1588 local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1592 if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1593 return ret;
1595 if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1596 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1597 ret = SEC_E_MESSAGE_ALTERED;
1598 else
1599 ret = SEC_E_OK;
1601 HeapFree(GetProcessHeap(), 0, local_buff);
1602 pfQOP = &fQOP;
1604 return ret;
1608 /***********************************************************************
1609 * FreeCredentialsHandle
1611 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1612 PCredHandle phCredential)
1614 SECURITY_STATUS ret;
1616 if(phCredential){
1617 PNtlmCredentials ntlm_cred = (PNtlmCredentials) phCredential->dwLower;
1618 phCredential->dwUpper = 0;
1619 phCredential->dwLower = 0;
1620 if (ntlm_cred->password)
1621 memset(ntlm_cred->password, 0, ntlm_cred->pwlen);
1622 HeapFree(GetProcessHeap(), 0, ntlm_cred->password);
1623 HeapFree(GetProcessHeap(), 0, ntlm_cred->username_arg);
1624 HeapFree(GetProcessHeap(), 0, ntlm_cred->domain_arg);
1625 ret = SEC_E_OK;
1627 else
1628 ret = SEC_E_OK;
1630 return ret;
1633 /***********************************************************************
1634 * EncryptMessage
1636 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1637 ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1639 PNegoHelper helper;
1640 int token_idx, data_idx;
1642 TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1644 if(!phContext)
1645 return SEC_E_INVALID_HANDLE;
1647 if(fQOP)
1648 FIXME("Ignoring fQOP\n");
1650 if(MessageSeqNo)
1651 FIXME("Ignoring MessageSeqNo\n");
1653 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1654 return SEC_E_INVALID_TOKEN;
1656 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1657 return SEC_E_INVALID_TOKEN;
1659 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1 )
1660 return SEC_E_INVALID_TOKEN;
1662 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1663 return SEC_E_BUFFER_TOO_SMALL;
1665 helper = (PNegoHelper) phContext->dwLower;
1667 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1668 helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1670 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1671 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1672 (BYTE *)pMessage->pBuffers[data_idx].pvBuffer,
1673 pMessage->pBuffers[data_idx].cbBuffer);
1675 if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1676 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1677 ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1680 return SEC_E_OK;
1682 else
1684 PBYTE sig;
1685 ULONG save_flags;
1687 /* EncryptMessage always produces real signatures, so make sure
1688 * NTLMSSP_NEGOTIATE_SIGN is set*/
1689 save_flags = helper->neg_flags;
1690 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1691 ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1692 helper->neg_flags = save_flags;
1694 sig = pMessage->pBuffers[token_idx].pvBuffer;
1696 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1697 pMessage->pBuffers[data_idx].pvBuffer,
1698 pMessage->pBuffers[data_idx].cbBuffer);
1699 SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1701 if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1702 memset(sig+4, 0, 4);
1706 return SEC_E_OK;
1709 /***********************************************************************
1710 * DecryptMessage
1712 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1713 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1715 SECURITY_STATUS ret;
1716 ULONG ntlmssp_flags_save;
1717 PNegoHelper helper;
1718 int token_idx, data_idx;
1719 TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1721 if(!phContext)
1722 return SEC_E_INVALID_HANDLE;
1724 if(MessageSeqNo)
1725 FIXME("Ignoring MessageSeqNo\n");
1727 if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1728 return SEC_E_INVALID_TOKEN;
1730 if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1731 return SEC_E_INVALID_TOKEN;
1733 if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1)
1734 return SEC_E_INVALID_TOKEN;
1736 if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1737 return SEC_E_BUFFER_TOO_SMALL;
1739 helper = (PNegoHelper) phContext->dwLower;
1741 if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1743 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1744 pMessage->pBuffers[data_idx].pvBuffer,
1745 pMessage->pBuffers[data_idx].cbBuffer);
1747 else
1749 SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1750 pMessage->pBuffers[data_idx].pvBuffer,
1751 pMessage->pBuffers[data_idx].cbBuffer);
1754 /* Make sure we use a session key for the signature check, EncryptMessage
1755 * always does that, even in the dummy case */
1756 ntlmssp_flags_save = helper->neg_flags;
1758 helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1759 ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1761 helper->neg_flags = ntlmssp_flags_save;
1763 return ret;
1766 static const SecurityFunctionTableA ntlmTableA = {
1768 NULL, /* EnumerateSecurityPackagesA */
1769 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
1770 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
1771 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1772 NULL, /* Reserved2 */
1773 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
1774 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1775 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1776 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1777 NULL, /* ApplyControlToken */
1778 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
1779 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1780 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1781 ntlm_MakeSignature, /* MakeSignature */
1782 ntlm_VerifySignature, /* VerifySignature */
1783 FreeContextBuffer, /* FreeContextBuffer */
1784 NULL, /* QuerySecurityPackageInfoA */
1785 NULL, /* Reserved3 */
1786 NULL, /* Reserved4 */
1787 NULL, /* ExportSecurityContext */
1788 NULL, /* ImportSecurityContextA */
1789 NULL, /* AddCredentialsA */
1790 NULL, /* Reserved8 */
1791 NULL, /* QuerySecurityContextToken */
1792 ntlm_EncryptMessage, /* EncryptMessage */
1793 ntlm_DecryptMessage, /* DecryptMessage */
1794 NULL, /* SetContextAttributesA */
1797 static const SecurityFunctionTableW ntlmTableW = {
1799 NULL, /* EnumerateSecurityPackagesW */
1800 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
1801 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
1802 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
1803 NULL, /* Reserved2 */
1804 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
1805 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
1806 ntlm_CompleteAuthToken, /* CompleteAuthToken */
1807 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
1808 NULL, /* ApplyControlToken */
1809 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
1810 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
1811 ntlm_RevertSecurityContext, /* RevertSecurityContext */
1812 ntlm_MakeSignature, /* MakeSignature */
1813 ntlm_VerifySignature, /* VerifySignature */
1814 FreeContextBuffer, /* FreeContextBuffer */
1815 NULL, /* QuerySecurityPackageInfoW */
1816 NULL, /* Reserved3 */
1817 NULL, /* Reserved4 */
1818 NULL, /* ExportSecurityContext */
1819 NULL, /* ImportSecurityContextW */
1820 NULL, /* AddCredentialsW */
1821 NULL, /* Reserved8 */
1822 NULL, /* QuerySecurityContextToken */
1823 ntlm_EncryptMessage, /* EncryptMessage */
1824 ntlm_DecryptMessage, /* DecryptMessage */
1825 NULL, /* SetContextAttributesW */
1828 #define NTLM_COMMENT \
1829 { 'N', 'T', 'L', 'M', ' ', \
1830 'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1831 'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1833 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1834 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1836 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1838 static char ntlm_name_A[] = NTLM_NAME;
1839 static WCHAR ntlm_name_W[] = NTLM_NAME;
1841 /* According to Windows, NTLM has the following capabilities. */
1842 #define CAPS ( \
1843 SECPKG_FLAG_INTEGRITY | \
1844 SECPKG_FLAG_PRIVACY | \
1845 SECPKG_FLAG_TOKEN_ONLY | \
1846 SECPKG_FLAG_CONNECTION | \
1847 SECPKG_FLAG_MULTI_REQUIRED | \
1848 SECPKG_FLAG_IMPERSONATION | \
1849 SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1850 SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1852 static const SecPkgInfoW infoW = {
1853 CAPS,
1855 RPC_C_AUTHN_WINNT,
1856 NTLM_MAX_BUF,
1857 ntlm_name_W,
1858 ntlm_comment_W
1861 static const SecPkgInfoA infoA = {
1862 CAPS,
1864 RPC_C_AUTHN_WINNT,
1865 NTLM_MAX_BUF,
1866 ntlm_name_A,
1867 ntlm_comment_A
1870 void SECUR32_initNTLMSP(void)
1872 SECURITY_STATUS ret;
1873 PNegoHelper helper;
1874 static CHAR version[] = "--version";
1876 SEC_CHAR *args[] = {
1877 ntlm_auth,
1878 version,
1879 NULL };
1881 if((ret = fork_helper(&helper, ntlm_auth, args)) != SEC_E_OK)
1883 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1884 helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1885 helper->major = helper->minor = helper->micro = -1;
1887 else
1888 check_version(helper);
1890 if( (helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
1891 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1892 helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
1893 (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
1894 helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
1895 helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION) )
1897 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1898 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1900 else
1902 ERR("%s was not found or is outdated. "
1903 "Make sure that ntlm_auth >= %d.%d.%d is in your path.\n",
1904 ntlm_auth,
1905 MIN_NTLM_AUTH_MAJOR_VERSION,
1906 MIN_NTLM_AUTH_MINOR_VERSION,
1907 MIN_NTLM_AUTH_MICRO_VERSION);
1908 ERR("Usually, you can find it in the winbind package of your "
1909 "distribution.\n");
1912 cleanup_helper(helper);