2 * Copyright 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * This file implements the NTLM security provider.
29 #include "secur32_priv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
34 #define NTLM_MAX_BUF 2010
36 static char ntlm_name_A[] = "NTLM";
37 static WCHAR ntlm_name_W[] = {'N', 'T', 'L', 'M', 0};
40 /***********************************************************************
41 * QueryCredentialsAttributesA
43 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
44 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
48 TRACE("(%p, %ld, %p)\n", phCredential, ulAttribute, pBuffer);
50 if(ulAttribute == SECPKG_ATTR_NAMES)
52 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
53 ret = SEC_E_UNSUPPORTED_FUNCTION;
56 ret = SEC_E_UNSUPPORTED_FUNCTION;
61 /***********************************************************************
62 * QueryCredentialsAttributesW
64 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
65 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
69 TRACE("(%p, %ld, %p)\n", phCredential, ulAttribute, pBuffer);
71 if(ulAttribute == SECPKG_ATTR_NAMES)
73 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
74 ret = SEC_E_UNSUPPORTED_FUNCTION;
77 ret = SEC_E_UNSUPPORTED_FUNCTION;
82 /***********************************************************************
83 * AcquireCredentialsHandleW
85 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
86 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
87 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
88 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
91 PNegoHelper helper = NULL;
93 SEC_CHAR *client_user_arg = NULL;
94 SEC_CHAR *client_domain_arg = NULL;
95 SEC_WCHAR *username = NULL, *domain = NULL;
97 SEC_CHAR *client_argv[5];
98 SEC_CHAR *server_argv[] = { "ntlm_auth",
99 "--helper-protocol=squid-2.5-ntlmssp",
102 TRACE("(%s, %s, 0x%08lx, %p, %p, %p, %p, %p, %p)\n",
103 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
104 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
107 switch(fCredentialUse)
109 case SECPKG_CRED_INBOUND:
110 if( (ret = fork_helper(&helper, "ntlm_auth", server_argv)) !=
118 helper->mode = NTLM_SERVER;
119 phCredential->dwUpper = fCredentialUse;
120 phCredential->dwLower = (DWORD)helper;
124 case SECPKG_CRED_OUTBOUND:
126 static const char username_arg[] = "--username=";
127 static const char domain_arg[] = "--domain=";
130 if(pAuthData == NULL)
132 LPWKSTA_USER_INFO_1 ui = NULL;
133 NET_API_STATUS status;
135 if((status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui)) !=
138 ret = SEC_E_NO_CREDENTIALS;
145 username = HeapAlloc(GetProcessHeap(), 0,
146 (lstrlenW(ui->wkui1_username)+1) *
148 lstrcpyW(username, ui->wkui1_username);
150 /* same for the domain */
151 domain = HeapAlloc(GetProcessHeap(), 0,
152 (lstrlenW(ui->wkui1_logon_domain)+1) *
154 lstrcpyW(domain, ui->wkui1_logon_domain);
155 NetApiBufferFree(ui);
159 ret = SEC_E_NO_CREDENTIALS;
168 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
169 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
171 if(auth_data->UserLength != 0)
173 /* Get username and domain from pAuthData */
174 username = HeapAlloc(GetProcessHeap(), 0,
175 (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
176 lstrcpyW(username, auth_data->User);
180 ret = SEC_E_NO_CREDENTIALS;
184 if(auth_data->DomainLength != 0)
186 domain = HeapAlloc(GetProcessHeap(), 0,
187 (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
188 lstrcpyW(domain, auth_data->Domain);
193 ret = SEC_E_NO_CREDENTIALS;
199 TRACE("Username is %s\n", debugstr_w(username));
200 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
201 username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
202 client_user_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
203 lstrcpyA(client_user_arg, username_arg);
204 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
205 client_user_arg + sizeof(username_arg) - 1,
206 unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
208 TRACE("Domain name is %s\n", debugstr_w(domain));
209 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
210 domain, -1, NULL, 0, NULL, NULL) + sizeof(domain_arg);
211 client_domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
212 lstrcpyA(client_domain_arg, domain_arg);
213 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
214 -1, client_domain_arg + sizeof(domain_arg) - 1,
215 unixcp_size - sizeof(domain) + 1, NULL, NULL);
217 client_argv[0] = "ntlm_auth";
218 client_argv[1] = "--helper-protocol=ntlmssp-client-1";
219 client_argv[2] = client_user_arg;
220 client_argv[3] = client_domain_arg;
221 client_argv[4] = NULL;
223 if((ret = fork_helper(&helper, "ntlm_auth", client_argv)) !=
231 helper->mode = NTLM_CLIENT;
233 if(pAuthData != NULL)
235 PSEC_WINNT_AUTH_IDENTITY_W auth_data =
236 (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
238 if(auth_data->PasswordLength != 0)
240 helper->pwlen = WideCharToMultiByte(CP_UNIXCP,
241 WC_NO_BEST_FIT_CHARS, auth_data->Password,
242 auth_data->PasswordLength+1, NULL, 0, NULL, NULL);
244 helper->password = HeapAlloc(GetProcessHeap(), 0,
247 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
248 auth_data->Password, auth_data->PasswordLength+1,
249 helper->password, helper->pwlen, NULL, NULL);
253 phCredential->dwUpper = fCredentialUse;
254 phCredential->dwLower = (DWORD)helper;
255 TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
256 phCredential->dwUpper, phCredential->dwLower);
261 case SECPKG_CRED_BOTH:
262 FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
263 ret = SEC_E_UNSUPPORTED_FUNCTION;
268 ret = SEC_E_UNKNOWN_CREDENTIALS;
272 HeapFree(GetProcessHeap(), 0, client_user_arg);
273 HeapFree(GetProcessHeap(), 0, client_domain_arg);
274 HeapFree(GetProcessHeap(), 0, username);
275 HeapFree(GetProcessHeap(), 0, domain);
280 /***********************************************************************
281 * AcquireCredentialsHandleA
283 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
284 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
285 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
286 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
289 int user_sizeW, domain_sizeW, passwd_sizeW;
291 SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
293 PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
294 PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
296 TRACE("(%s, %s, 0x%08lx, %p, %p, %p, %p, %p, %p)\n",
297 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
298 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
300 if(pszPackage != NULL)
302 int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
305 package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
307 MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
311 if(pAuthData != NULL)
313 identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
315 if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
317 pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
318 sizeof(SEC_WINNT_AUTH_IDENTITY_W));
320 if(identity->UserLength != 0)
322 user_sizeW = MultiByteToWideChar(CP_ACP, 0,
323 (LPCSTR)identity->User, identity->UserLength+1, NULL, 0);
324 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
326 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
327 identity->UserLength+1, user, user_sizeW);
334 if(identity->DomainLength != 0)
336 domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
337 (LPCSTR)identity->Domain, identity->DomainLength+1, NULL, 0);
338 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
339 * sizeof(SEC_WCHAR));
340 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
341 identity->DomainLength+1, domain, domain_sizeW);
348 if(identity->PasswordLength != 0)
350 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
351 (LPCSTR)identity->Password, identity->PasswordLength+1,
353 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
354 * sizeof(SEC_WCHAR));
355 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
356 identity->PasswordLength, passwd, passwd_sizeW);
363 pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
364 pAuthDataW->User = user;
365 pAuthDataW->UserLength = user_sizeW;
366 pAuthDataW->Domain = domain;
367 pAuthDataW->DomainLength = domain_sizeW;
368 pAuthDataW->Password = passwd;
369 pAuthDataW->PasswordLength = passwd_sizeW;
373 pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
377 ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
378 pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
381 HeapFree(GetProcessHeap(), 0, package);
382 HeapFree(GetProcessHeap(), 0, user);
383 HeapFree(GetProcessHeap(), 0, domain);
384 HeapFree(GetProcessHeap(), 0, passwd);
385 if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
386 HeapFree(GetProcessHeap(), 0, pAuthDataW);
391 /***********************************************************************
392 * InitializeSecurityContextW
394 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
395 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
396 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
397 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
398 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
402 TRACE("%p %p %s %ld %ld %ld %p %ld %p %p %p %p\n", phCredential, phContext,
403 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
404 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
407 /* As the server side of sspi never calls this, make sure that
408 * the handler is a client handler.
410 PNegoHelper helper = (PNegoHelper)phCredential->dwLower;
412 if(helper->mode == NTLM_CLIENT)
414 /****************************************
415 * When communicating with the client, there can be the
416 * following reply packets:
417 * YR <base64 blob> should be sent to the server
418 * PW should be sent back to helper with
419 * base64 encoded password
420 * AF <base64 blob> client is done, blob should be
421 * sent to server with KK prefixed
422 * BH <char reason> something broke
425 /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
426 char* buffer = HeapAlloc(GetProcessHeap(), 0,
427 sizeof(char) * NTLM_MAX_BUF);
428 PBYTE bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) *
430 int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
432 if((phContext == NULL) && (pInput == NULL))
436 TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
438 /* Handle all the flags */
439 if(fContextReq & ISC_REQ_ALLOCATE_MEMORY)
441 FIXME("InitializeSecurityContext(): ISC_REQ_ALLOCATE_MEMORY stub\n");
443 if(fContextReq & ISC_REQ_CONFIDENTIALITY)
445 FIXME("InitializeSecurityContext(): ISC_REQ_CONFIDENTIALITY stub\n");
447 if(fContextReq & ISC_REQ_CONNECTION)
449 /* This is default, so we'll enable it */
450 ctxt_attr |= ISC_RET_CONNECTION;
452 if(fContextReq & ISC_REQ_EXTENDED_ERROR)
454 FIXME("InitializeSecurityContext(): ISC_REQ_EXTENDED_ERROR stub\n");
456 if(fContextReq & ISC_REQ_INTEGRITY)
458 FIXME("InitializeSecurityContext(): ISC_REQ_INTEGRITY stub\n");
460 if(fContextReq & ISC_REQ_MUTUAL_AUTH)
462 FIXME("InitializeSecurityContext(): ISC_REQ_MUTUAL_AUTH stub\n");
464 if(fContextReq & ISC_REQ_REPLAY_DETECT)
466 FIXME("InitializeSecurityContext(): ISC_REQ_REPLAY_DETECT stub\n");
468 if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
470 FIXME("InitializeSecurityContext(): ISC_REQ_SEQUENCE_DETECT stub\n");
472 if(fContextReq & ISC_REQ_STREAM)
474 FIXME("InitializeSecurityContext(): ISC_REQ_STREAM stub\n");
476 /* Done with the flags */
477 if(TargetDataRep == SECURITY_NETWORK_DREP){
478 FIXME("Don't know how to do SECURITY_NETWORK_DREP\n");
479 HeapFree(GetProcessHeap(), 0, buffer);
480 HeapFree(GetProcessHeap(), 0, bin);
481 return SEC_E_UNSUPPORTED_FUNCTION;
486 TRACE("First time in ISC()\n");
487 /* Request a challenge request from ntlm_auth */
488 if(helper->password == NULL)
490 FIXME("Using empty password for now.\n");
491 lstrcpynA(buffer, "PW AA==", max_len-1);
495 lstrcpynA(buffer, "PW ", max_len-1);
496 if((ret = encodeBase64((unsigned char*)helper->password,
497 helper->pwlen-2, buffer+3,
498 max_len-3, &buffer_len)) != SEC_E_OK)
500 TRACE("Deleting password!\n");
501 memset(helper->password, 0, helper->pwlen-2);
502 HeapFree(GetProcessHeap(), 0, helper->password);
503 HeapFree(GetProcessHeap(), 0, buffer);
504 HeapFree(GetProcessHeap(), 0, bin);
510 TRACE("Sending to helper: %s\n", debugstr_a(buffer));
511 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
514 HeapFree(GetProcessHeap(), 0, buffer);
515 HeapFree(GetProcessHeap(), 0, bin);
518 TRACE("Helper returned %s\n", debugstr_a(buffer));
519 lstrcpynA(buffer, "YR", max_len-1);
521 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
524 HeapFree(GetProcessHeap(), 0, buffer);
525 HeapFree(GetProcessHeap(), 0, bin);
529 TRACE("%s\n", buffer);
531 if(strncmp(buffer, "YR ", 3) != 0)
534 /* Something borked */
535 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
536 HeapFree(GetProcessHeap(), 0, buffer);
537 HeapFree(GetProcessHeap(), 0, bin);
538 return SEC_E_INTERNAL_ERROR;
540 if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
541 max_len-1, &bin_len)) != SEC_E_OK)
543 HeapFree(GetProcessHeap(), 0, buffer);
544 HeapFree(GetProcessHeap(), 0, bin);
548 /* put the decoded client blob into the out buffer */
550 TRACE("pOutput is NULL\n");
551 HeapFree(GetProcessHeap(), 0, buffer);
552 HeapFree(GetProcessHeap(), 0, bin);
553 return SEC_E_INSUFFICIENT_MEMORY;
556 if(pOutput->cBuffers < 1)
558 TRACE("pOutput->cBuffers is %ld\n", pOutput->cBuffers);
559 HeapFree(GetProcessHeap(), 0, buffer);
560 HeapFree(GetProcessHeap(), 0, bin);
561 return SEC_E_INSUFFICIENT_MEMORY;
563 pOutput->pBuffers[0].cbBuffer = bin_len;
564 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
565 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
567 ret = SEC_I_CONTINUE_NEEDED;
571 /* handle second call here */
572 /* encode server data to base64 */
575 HeapFree(GetProcessHeap(), 0, buffer);
576 HeapFree(GetProcessHeap(), 0, bin);
577 return SEC_E_INCOMPLETE_MESSAGE;
580 if(pInput->cBuffers < 1)
582 HeapFree(GetProcessHeap(), 0, buffer);
583 HeapFree(GetProcessHeap(), 0, bin);
584 return SEC_E_INCOMPLETE_MESSAGE;
587 if(pInput->pBuffers[0].cbBuffer > max_len)
589 TRACE("pInput->pBuffers[0].cbBuffer is: %ld\n",
590 pInput->pBuffers[0].cbBuffer);
591 HeapFree(GetProcessHeap(), 0, buffer);
592 HeapFree(GetProcessHeap(), 0, bin);
593 return SEC_E_INVALID_TOKEN;
596 bin_len = pInput->pBuffers[0].cbBuffer;
598 memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
600 lstrcpynA(buffer, "TT ", max_len-1);
602 if((ret = encodeBase64(bin, bin_len, buffer+3,
603 max_len-3, &buffer_len)) != SEC_E_OK)
605 HeapFree(GetProcessHeap(), 0, buffer);
606 HeapFree(GetProcessHeap(), 0, bin);
610 TRACE("Server sent: %s\n", debugstr_a(buffer));
612 /* send TT base64 blob to ntlm_auth */
613 if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
616 HeapFree(GetProcessHeap(), 0, buffer);
617 HeapFree(GetProcessHeap(), 0, bin);
621 TRACE("Helper replied: %s\n", debugstr_a(buffer));
623 if( (strncmp(buffer, "KK ", 3) != 0) &&
624 (strncmp(buffer, "AF ", 3) !=0))
626 TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
627 HeapFree(GetProcessHeap(), 0, buffer);
628 HeapFree(GetProcessHeap(), 0, bin);
629 return SEC_E_INVALID_TOKEN;
632 /* decode the blob and send it to server */
633 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
634 &bin_len)) != SEC_E_OK)
636 HeapFree(GetProcessHeap(), 0, buffer);
637 HeapFree(GetProcessHeap(), 0, bin);
643 HeapFree(GetProcessHeap(), 0, buffer);
644 HeapFree(GetProcessHeap(), 0, bin);
645 return SEC_E_INSUFFICIENT_MEMORY;
648 if(pOutput->cBuffers < 1)
650 HeapFree(GetProcessHeap(), 0, buffer);
651 HeapFree(GetProcessHeap(), 0, bin);
652 return SEC_E_INSUFFICIENT_MEMORY;
655 pOutput->pBuffers[0].cbBuffer = bin_len;
656 pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
657 memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
660 phNewContext->dwUpper = ctxt_attr;
661 phNewContext->dwLower = ret;
664 HeapFree(GetProcessHeap(), 0, buffer);
665 HeapFree(GetProcessHeap(), 0, bin);
666 if(ret != SEC_I_CONTINUE_NEEDED)
668 TRACE("Deleting password!\n");
670 memset(helper->password, 0, helper->pwlen-2);
671 HeapFree(GetProcessHeap(), 0, helper->password);
677 ret = SEC_E_INVALID_HANDLE;
678 TRACE("Helper mode = %d\n", helper->mode);
683 ret = SEC_E_INVALID_HANDLE;
688 /***********************************************************************
689 * InitializeSecurityContextA
691 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
692 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
693 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
694 PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
695 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
699 TRACE("%p %p %s %ld %ld %ld %p %ld %p %p %p %p\n", phCredential, phContext,
700 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
701 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
705 SEC_WCHAR *target = NULL;
706 if(pszTargetName != NULL)
708 int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
709 strlen(pszTargetName)+1, NULL, 0);
710 target = HeapAlloc(GetProcessHeap(), 0, target_size *
712 MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
713 target, target_size);
716 ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
717 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
718 phNewContext, pOutput, pfContextAttr, ptsExpiry);
720 HeapFree(GetProcessHeap(), 0, target);
724 ret = SEC_E_INVALID_HANDLE;
729 /***********************************************************************
730 * AcceptSecurityContext
732 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
733 PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
734 ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
735 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
739 TRACE("%p %p %p %ld %ld %p %p %p %p\n", phCredential, phContext, pInput,
740 fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
744 ret = SEC_E_UNSUPPORTED_FUNCTION;
748 ret = SEC_E_INVALID_HANDLE;
753 /***********************************************************************
756 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
757 PSecBufferDesc pToken)
761 TRACE("%p %p\n", phContext, pToken);
764 ret = SEC_E_UNSUPPORTED_FUNCTION;
768 ret = SEC_E_INVALID_HANDLE;
773 /***********************************************************************
774 * DeleteSecurityContext
776 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
780 TRACE("%p\n", phContext);
783 ret = SEC_E_UNSUPPORTED_FUNCTION;
787 ret = SEC_E_INVALID_HANDLE;
792 /***********************************************************************
793 * QueryContextAttributesW
795 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
796 unsigned long ulAttribute, void *pBuffer)
800 /* FIXME: From reading wrapper.h, I think the dwUpper part of a context is
801 * the SecurePackage part and the dwLower part is the actual context
802 * handle. It should be easy to extract the context attributes from that.
804 TRACE("%p %ld %p\n", phContext, ulAttribute, pBuffer);
807 ret = SEC_E_UNSUPPORTED_FUNCTION;
811 ret = SEC_E_INVALID_HANDLE;
816 /***********************************************************************
817 * QueryContextAttributesA
819 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
820 unsigned long ulAttribute, void *pBuffer)
822 return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
825 /***********************************************************************
826 * ImpersonateSecurityContext
828 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
832 TRACE("%p\n", phContext);
835 ret = SEC_E_UNSUPPORTED_FUNCTION;
839 ret = SEC_E_INVALID_HANDLE;
844 /***********************************************************************
845 * RevertSecurityContext
847 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
851 TRACE("%p\n", phContext);
854 ret = SEC_E_UNSUPPORTED_FUNCTION;
858 ret = SEC_E_INVALID_HANDLE;
863 /***********************************************************************
866 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
867 PSecBufferDesc pMessage, ULONG MessageSeqNo)
871 TRACE("%p %ld %p %ld\n", phContext, fQOP, pMessage, MessageSeqNo);
874 ret = SEC_E_UNSUPPORTED_FUNCTION;
878 ret = SEC_E_INVALID_HANDLE;
883 /***********************************************************************
886 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
887 PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
891 TRACE("%p %p %ld %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
894 ret = SEC_E_UNSUPPORTED_FUNCTION;
898 ret = SEC_E_INVALID_HANDLE;
903 /***********************************************************************
904 * FreeCredentialsHandle
906 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
907 PCredHandle phCredential)
912 PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
913 phCredential->dwUpper = 0;
914 phCredential->dwLower = 0;
915 cleanup_helper(helper);
924 static SecurityFunctionTableA ntlmTableA = {
926 NULL, /* EnumerateSecurityPackagesA */
927 ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
928 ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
929 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
930 NULL, /* Reserved2 */
931 ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
932 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
933 ntlm_CompleteAuthToken, /* CompleteAuthToken */
934 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
935 NULL, /* ApplyControlToken */
936 ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
937 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
938 ntlm_RevertSecurityContext, /* RevertSecurityContext */
939 ntlm_MakeSignature, /* MakeSignature */
940 ntlm_VerifySignature, /* VerifySignature */
941 FreeContextBuffer, /* FreeContextBuffer */
942 NULL, /* QuerySecurityPackageInfoA */
943 NULL, /* Reserved3 */
944 NULL, /* Reserved4 */
945 NULL, /* ExportSecurityContext */
946 NULL, /* ImportSecurityContextA */
947 NULL, /* AddCredentialsA */
948 NULL, /* Reserved8 */
949 NULL, /* QuerySecurityContextToken */
950 NULL, /* EncryptMessage */
951 NULL, /* DecryptMessage */
952 NULL, /* SetContextAttributesA */
955 static SecurityFunctionTableW ntlmTableW = {
957 NULL, /* EnumerateSecurityPackagesW */
958 ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
959 ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
960 ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
961 NULL, /* Reserved2 */
962 ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
963 ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
964 ntlm_CompleteAuthToken, /* CompleteAuthToken */
965 ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
966 NULL, /* ApplyControlToken */
967 ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
968 ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
969 ntlm_RevertSecurityContext, /* RevertSecurityContext */
970 ntlm_MakeSignature, /* MakeSignature */
971 ntlm_VerifySignature, /* VerifySignature */
972 FreeContextBuffer, /* FreeContextBuffer */
973 NULL, /* QuerySecurityPackageInfoW */
974 NULL, /* Reserved3 */
975 NULL, /* Reserved4 */
976 NULL, /* ExportSecurityContext */
977 NULL, /* ImportSecurityContextW */
978 NULL, /* AddCredentialsW */
979 NULL, /* Reserved8 */
980 NULL, /* QuerySecurityContextToken */
981 NULL, /* EncryptMessage */
982 NULL, /* DecryptMessage */
983 NULL, /* SetContextAttributesW */
986 static WCHAR ntlm_comment_W[] = { 'N', 'T', 'L', 'M', ' ', 'S', 'e',
987 'c', 'u', 'r', 'i', 't', 'y', ' ', 'P', 'a', 'c', 'k', 'a', 'g', 'e',0};
989 static CHAR ntlm_comment_A[] = "NTLM Security Package";
991 void SECUR32_initNTLMSP(void)
1001 if((ret = fork_helper(&helper, "ntlm_auth", args)) != SEC_E_OK)
1003 /* Cheat and allocate a helper anyway, so cleanup later will work. */
1004 helper = HeapAlloc(GetProcessHeap,0, sizeof(PNegoHelper));
1005 helper->version = -1;
1009 check_version(helper);
1012 if(helper->version > 2)
1015 SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW,
1017 /* According to Windows, NTLM has the following capabilities.
1020 static const LONG caps =
1021 SECPKG_FLAG_INTEGRITY |
1022 SECPKG_FLAG_PRIVACY |
1023 SECPKG_FLAG_TOKEN_ONLY |
1024 SECPKG_FLAG_CONNECTION |
1025 SECPKG_FLAG_MULTI_REQUIRED |
1026 SECPKG_FLAG_IMPERSONATION |
1027 SECPKG_FLAG_ACCEPT_WIN32_NAME |
1028 SECPKG_FLAG_READONLY_WITH_CHECKSUM;
1030 static const USHORT version = 1;
1031 static const USHORT rpcid = 10;
1032 /* In Windows, this is 12000, but ntlm_auth won't take more than 2010
1033 * characters, so there is no use reporting a bigger size */
1034 static const ULONG max_token = NTLM_MAX_BUF;
1035 const SecPkgInfoW infoW = { caps, version, rpcid, max_token, ntlm_name_W,
1037 const SecPkgInfoA infoA = { caps, version, rpcid, max_token, ntlm_name_A,
1040 SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1042 cleanup_helper(helper);