dsound: Handle failing of IDsDriver_CreateSoundBuffer better.
[wine] / dlls / secur32 / ntlm.c
1 /*
2  * Copyright 2005, 2006 Kai Blin
3  *
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.
8  *
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.
13  *
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
17  *
18  * This file implements the NTLM security provider.
19  */
20
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"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(ntlm);
35
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
40
41 static CHAR ntlm_auth[] = "ntlm_auth";
42
43 typedef struct _NtlmCredentials
44 {
45     HelperMode mode;
46
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;
53
54 /***********************************************************************
55  *              QueryCredentialsAttributesA
56  */
57 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
58         PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
59 {
60     SECURITY_STATUS ret;
61
62     TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
63
64     if(ulAttribute == SECPKG_ATTR_NAMES)
65     {
66         FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
67         ret = SEC_E_UNSUPPORTED_FUNCTION;
68     }
69     else
70         ret = SEC_E_UNSUPPORTED_FUNCTION;
71     
72     return ret;
73 }
74
75 /***********************************************************************
76  *              QueryCredentialsAttributesW
77  */
78 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
79         PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
80 {
81     SECURITY_STATUS ret;
82
83     TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
84
85     if(ulAttribute == SECPKG_ATTR_NAMES)
86     {
87         FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
88         ret = SEC_E_UNSUPPORTED_FUNCTION;
89     }
90     else
91         ret = SEC_E_UNSUPPORTED_FUNCTION;
92     
93     return ret;
94 }
95
96 /***********************************************************************
97  *              AcquireCredentialsHandleW
98  */
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)
103 {
104     SECURITY_STATUS ret;
105     PNtlmCredentials ntlm_cred = NULL;
106     SEC_WCHAR *username = NULL, *domain = NULL;
107
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);
111
112     switch(fCredentialUse)
113     {
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
119             {
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;
128             }
129             break;
130         case SECPKG_CRED_OUTBOUND:
131             {
132                 static const char username_arg[] = "--username=";
133                 static const char domain_arg[] = "--domain=";
134                 int unixcp_size;
135
136                 if(pAuthData == NULL)
137                 {
138                     LPWKSTA_USER_INFO_1 ui = NULL;
139                     NET_API_STATUS status;
140
141                     status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
142                     if (status != NERR_Success || ui == NULL)
143                     {
144                         ret = SEC_E_NO_CREDENTIALS;
145                         phCredential = NULL;
146                         break;
147                     }
148                     
149                     username = HeapAlloc(GetProcessHeap(), 0, 
150                             (lstrlenW(ui->wkui1_username)+1) * 
151                             sizeof(SEC_WCHAR));
152                     lstrcpyW(username, ui->wkui1_username);
153                         
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);
160                 }
161                 else
162                 {
163                     PSEC_WINNT_AUTH_IDENTITY_W auth_data = 
164                         (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
165
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';
172
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';
178                 }
179
180                 ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
181                 if (!ntlm_cred)
182                 {
183                     ret = SEC_E_INSUFFICIENT_MEMORY;
184                     break;
185                 }
186                 ntlm_cred->mode = NTLM_CLIENT;
187                 ntlm_cred->password = NULL;
188                 ntlm_cred->pwlen = 0;
189
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);
198
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);
207
208                 if(pAuthData != NULL)
209                 {
210                     PSEC_WINNT_AUTH_IDENTITY_W auth_data =
211                     (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
212
213                     if(auth_data->PasswordLength != 0)
214                     {
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);
219
220                         ntlm_cred->password = HeapAlloc(GetProcessHeap(), 0,
221                                                         ntlm_cred->pwlen);
222
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);
226                     }
227                 }
228
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;
235             }
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;
244     }
245
246     HeapFree(GetProcessHeap(), 0, username);
247     HeapFree(GetProcessHeap(), 0, domain);
248
249     return ret;
250 }
251
252 /***********************************************************************
253  *              AcquireCredentialsHandleA
254  */
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)
259 {
260     SECURITY_STATUS ret;
261     int user_sizeW, domain_sizeW, passwd_sizeW;
262     
263     SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
264     
265     PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
266     PSEC_WINNT_AUTH_IDENTITY_A identity  = NULL;
267
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);
271     
272     if(pszPackage != NULL)
273     {
274         int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
275                 NULL, 0);
276
277         package = HeapAlloc(GetProcessHeap(), 0, package_sizeW * 
278                 sizeof(SEC_WCHAR));
279         MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
280     }
281
282     
283     if(pAuthData != NULL)
284     {
285         identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
286         
287         if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
288         {
289             pAuthDataW = HeapAlloc(GetProcessHeap(), 0, 
290                     sizeof(SEC_WINNT_AUTH_IDENTITY_W));
291
292             if(identity->UserLength != 0)
293             {
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);
300             }
301             else
302             {
303                 user_sizeW = 0;
304             }
305              
306             if(identity->DomainLength != 0)
307             {
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);
314             }
315             else
316             {
317                 domain_sizeW = 0;
318             }
319
320             if(identity->PasswordLength != 0)
321             {
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);
329             }
330             else
331             {
332                 passwd_sizeW = 0;
333             }
334             
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;
342         }
343         else
344         {
345             pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
346         }
347     }       
348     
349     ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse, 
350             pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
351             ptsExpiry);
352     
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);
359     
360     return ret;
361 }
362
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.
367  */
368 static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
369 {
370     UINT i;
371
372     TRACE("%p\n", pMessage);
373
374     for( i = 0; i < pMessage->cBuffers; ++i )
375     {
376         if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
377             return i;
378     }
379
380     return -1;
381 }
382
383 /***********************************************************************
384  *              InitializeSecurityContextW
385  */
386 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
387  PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName, 
388  ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep, 
389  PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext, 
390  PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
391 {
392     SECURITY_STATUS ret;
393     PNtlmCredentials ntlm_cred = NULL;
394     PNegoHelper helper = NULL;
395     ULONG ctxt_attr = 0;
396     char* buffer, *want_flags = NULL;
397     PBYTE bin;
398     int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
399     int token_idx;
400
401     TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
402      debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
403      Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
404
405     /****************************************
406      * When communicating with the client, there can be the
407      * following reply packets:
408      * YR <base64 blob>         should be sent to the server
409      * PW                       should be sent back to helper with
410      *                          base64 encoded password
411      * AF <base64 blob>         client is done, blob should be
412      *                          sent to server with KK prefixed
413      * GF <string list>         A string list of negotiated flags
414      * GK <base64 blob>         base64 encoded session key
415      * BH <char reason>         something broke
416      */
417     /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
418
419     if (pszTargetName)
420     {
421         TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
422     }
423
424     if(TargetDataRep == SECURITY_NETWORK_DREP){
425         TRACE("Setting SECURITY_NETWORK_DREP\n");
426     }
427
428     buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
429     bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
430
431     if((phContext == NULL) && (pInput == NULL))
432     {
433         static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
434         static CHAR credentials_argv[] = "--use-cached-creds";
435         SEC_CHAR *client_argv[6];
436
437         TRACE("First time in ISC()\n");
438
439         if(!phCredential)
440             return SEC_E_INVALID_HANDLE;
441
442         /* As the server side of sspi never calls this, make sure that
443          * the handler is a client handler.
444          */
445         ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
446         if(ntlm_cred->mode != NTLM_CLIENT)
447         {
448             TRACE("Cred mode = %d\n", ntlm_cred->mode);
449             return SEC_E_INVALID_HANDLE;
450         }
451
452         client_argv[0] = ntlm_auth;
453         client_argv[1] = helper_protocol;
454         client_argv[2] = ntlm_cred->username_arg;
455         client_argv[3] = ntlm_cred->domain_arg;
456         client_argv[4] = credentials_argv;
457         client_argv[5] = NULL;
458
459         if((ret = fork_helper(&helper, ntlm_auth, client_argv)) != SEC_E_OK)
460             goto isc_end;
461
462         helper->mode = NTLM_CLIENT;
463         helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
464         if (!helper->session_key)
465         {
466             cleanup_helper(helper);
467             ret = SEC_E_INSUFFICIENT_MEMORY;
468             goto isc_end;
469         }
470
471         /* Generate the dummy session key = MD4(MD4(password))*/
472         if(ntlm_cred->password)
473         {
474             SEC_WCHAR *unicode_password;
475             int passwd_lenW;
476
477             TRACE("Converting password to unicode.\n");
478             passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
479                                               (LPCSTR)ntlm_cred->password, ntlm_cred->pwlen,
480                                               NULL, 0);
481             unicode_password = HeapAlloc(GetProcessHeap(), 0,
482                                          passwd_lenW * sizeof(SEC_WCHAR));
483             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)ntlm_cred->password,
484                                 ntlm_cred->pwlen, unicode_password, passwd_lenW);
485
486             SECUR32_CreateNTLMv1SessionKey((PBYTE)unicode_password,
487                                            passwd_lenW * sizeof(SEC_WCHAR), helper->session_key);
488
489             HeapFree(GetProcessHeap(), 0, unicode_password);
490         }
491         else
492             memset(helper->session_key, 0, 16);
493
494         /* Allocate space for a maximal string of 
495          * "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
496          * NTLMSSP_FEATURE_SESSION_KEY"
497          */
498         want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
499         if(want_flags == NULL)
500         {
501             cleanup_helper(helper);
502             ret = SEC_E_INSUFFICIENT_MEMORY;
503             goto isc_end;
504         }
505         lstrcpyA(want_flags, "SF");
506         if(fContextReq & ISC_REQ_CONFIDENTIALITY)
507         {
508             char *ptr;
509             if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SEAL")) == NULL)
510                 lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
511         }
512         if(fContextReq & ISC_REQ_CONNECTION)
513             ctxt_attr |= ISC_RET_CONNECTION;
514         if(fContextReq & ISC_REQ_EXTENDED_ERROR)
515             ctxt_attr |= ISC_RET_EXTENDED_ERROR;
516         if(fContextReq & ISC_REQ_INTEGRITY)
517         {
518             char *ptr;
519             if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
520                 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
521         }
522         if(fContextReq & ISC_REQ_MUTUAL_AUTH)
523             ctxt_attr |= ISC_RET_MUTUAL_AUTH;
524         if(fContextReq & ISC_REQ_REPLAY_DETECT)
525         {
526             char *ptr;
527             if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
528                 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
529         }
530         if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
531         {
532             char *ptr;
533             if((ptr = strstr(want_flags, "NTLMSSP_FEATURE_SIGN")) == NULL)
534                 lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
535         }
536         if(fContextReq & ISC_REQ_STREAM)
537             FIXME("ISC_REQ_STREAM\n");
538         if(fContextReq & ISC_REQ_USE_DCE_STYLE)
539             ctxt_attr |= ISC_RET_USED_DCE_STYLE;
540         if(fContextReq & ISC_REQ_DELEGATE)
541             ctxt_attr |= ISC_RET_DELEGATE;
542
543         /* If no password is given, try to use cached credentials. Fall back to an empty
544          * password if this failed. */
545         if(ntlm_cred->password == NULL)
546         {
547             lstrcpynA(buffer, "OK", max_len-1);
548             if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
549             {
550                 cleanup_helper(helper);
551                 goto isc_end;
552             }
553             /* If the helper replied with "PW", using cached credentials failed */
554             if(!strncmp(buffer, "PW", 2))
555             {
556                 TRACE("Using cached credentials failed. Using empty password.\n");
557                 lstrcpynA(buffer, "PW AA==", max_len-1);
558             }
559             else /* Just do a noop on the next run */
560                 lstrcpynA(buffer, "OK", max_len-1);
561         }
562         else
563         {
564             lstrcpynA(buffer, "PW ", max_len-1);
565             if((ret = encodeBase64((unsigned char*)ntlm_cred->password,
566                         ntlm_cred->pwlen, buffer+3,
567                         max_len-3, &buffer_len)) != SEC_E_OK)
568             {
569                 cleanup_helper(helper);
570                 goto isc_end;
571             }
572
573         }
574
575         TRACE("Sending to helper: %s\n", debugstr_a(buffer));
576         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
577         {
578             cleanup_helper(helper);
579             goto isc_end;
580         }
581
582         TRACE("Helper returned %s\n", debugstr_a(buffer));
583
584         if(lstrlenA(want_flags) > 2)
585         {
586             TRACE("Want flags are %s\n", debugstr_a(want_flags));
587             lstrcpynA(buffer, want_flags, max_len-1);
588             if((ret = run_helper(helper, buffer, max_len, &buffer_len)) 
589                     != SEC_E_OK)
590                 goto isc_end;
591             if(!strncmp(buffer, "BH", 2))
592                 ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
593         }
594
595         lstrcpynA(buffer, "YR", max_len-1);
596
597         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
598         {
599             cleanup_helper(helper);
600             goto isc_end;
601         }
602
603         TRACE("%s\n", buffer);
604
605         if(strncmp(buffer, "YR ", 3) != 0)
606         {
607             /* Something borked */
608             TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
609             ret = SEC_E_INTERNAL_ERROR;
610             cleanup_helper(helper);
611             goto isc_end;
612         }
613         if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
614                         max_len-1, &bin_len)) != SEC_E_OK)
615         {
616             cleanup_helper(helper);
617             goto isc_end;
618         }
619
620         /* put the decoded client blob into the out buffer */
621
622         phNewContext->dwUpper = ctxt_attr;
623         phNewContext->dwLower = (ULONG_PTR)helper;
624
625         ret = SEC_I_CONTINUE_NEEDED;
626     }
627     else
628     {
629         int input_token_idx;
630
631         /* handle second call here */
632         /* encode server data to base64 */
633         if (!pInput || ((input_token_idx = ntlm_GetTokenBufferIndex(pInput)) == -1))
634         {
635             ret = SEC_E_INVALID_TOKEN;
636             goto isc_end;
637         }
638
639         if(!phContext)
640             return SEC_E_INVALID_HANDLE;
641
642         /* As the server side of sspi never calls this, make sure that
643          * the handler is a client handler.
644          */
645         helper = (PNegoHelper)phContext->dwLower;
646         if(helper->mode != NTLM_CLIENT)
647         {
648             TRACE("Helper mode = %d\n", helper->mode);
649             return SEC_E_INVALID_HANDLE;
650         }
651
652         if (!pInput->pBuffers[input_token_idx].pvBuffer)
653         {
654             ret = SEC_E_INTERNAL_ERROR;
655             goto isc_end;
656         }
657
658         if(pInput->pBuffers[input_token_idx].cbBuffer > max_len)
659         {
660             TRACE("pInput->pBuffers[%d].cbBuffer is: %ld\n",
661                     input_token_idx,
662                     pInput->pBuffers[input_token_idx].cbBuffer);
663             ret = SEC_E_INVALID_TOKEN;
664             goto isc_end;
665         }
666         else
667             bin_len = pInput->pBuffers[input_token_idx].cbBuffer;
668
669         memcpy(bin, pInput->pBuffers[input_token_idx].pvBuffer, bin_len);
670
671         lstrcpynA(buffer, "TT ", max_len-1);
672
673         if((ret = encodeBase64(bin, bin_len, buffer+3,
674                         max_len-3, &buffer_len)) != SEC_E_OK)
675             goto isc_end;
676
677         TRACE("Server sent: %s\n", debugstr_a(buffer));
678
679         /* send TT base64 blob to ntlm_auth */
680         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
681             goto isc_end;
682
683         TRACE("Helper replied: %s\n", debugstr_a(buffer));
684
685         if( (strncmp(buffer, "KK ", 3) != 0) &&
686                 (strncmp(buffer, "AF ", 3) !=0))
687         {
688             TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
689             ret = SEC_E_INVALID_TOKEN;
690             goto isc_end;
691         }
692
693         /* decode the blob and send it to server */
694         if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
695                         &bin_len)) != SEC_E_OK)
696         {
697             goto isc_end;
698         }
699
700         phNewContext->dwUpper = ctxt_attr;
701         phNewContext->dwLower = (ULONG_PTR)helper;
702
703         ret = SEC_E_OK;
704     }
705
706     /* put the decoded client blob into the out buffer */
707
708     if (!pOutput || ((token_idx = ntlm_GetTokenBufferIndex(pOutput)) == -1))
709     {
710         TRACE("no SECBUFFER_TOKEN buffer could be found\n");
711         ret = SEC_E_BUFFER_TOO_SMALL;
712         if ((phContext == NULL) && (pInput == NULL))
713         {
714             cleanup_helper(helper);
715             phNewContext->dwUpper = 0;
716             phNewContext->dwLower = 0;
717         }
718         goto isc_end;
719     }
720
721     if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
722     {
723         pOutput->pBuffers[token_idx].pvBuffer = SECUR32_ALLOC(bin_len);
724         pOutput->pBuffers[token_idx].cbBuffer = bin_len;
725     }
726     else if (pOutput->pBuffers[token_idx].cbBuffer < bin_len)
727     {
728         TRACE("out buffer is NULL or has not enough space\n");
729         ret = SEC_E_BUFFER_TOO_SMALL;
730         if ((phContext == NULL) && (pInput == NULL))
731         {
732             cleanup_helper(helper);
733             phNewContext->dwUpper = 0;
734             phNewContext->dwLower = 0;
735         }
736         goto isc_end;
737     }
738
739     if (!pOutput->pBuffers[token_idx].pvBuffer)
740     {
741         TRACE("out buffer is NULL\n");
742         ret = SEC_E_INTERNAL_ERROR;
743         if ((phContext == NULL) && (pInput == NULL))
744         {
745             cleanup_helper(helper);
746             phNewContext->dwUpper = 0;
747             phNewContext->dwLower = 0;
748         }
749         goto isc_end;
750     }
751
752     pOutput->pBuffers[token_idx].cbBuffer = bin_len;
753     memcpy(pOutput->pBuffers[token_idx].pvBuffer, bin, bin_len);
754
755     if(ret == SEC_E_OK)
756     {
757         TRACE("Getting negotiated flags\n");
758         lstrcpynA(buffer, "GF", max_len - 1);
759         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
760             goto isc_end;
761
762         if(buffer_len < 3)
763         {
764             TRACE("No flags negotiated.\n");
765             helper->neg_flags = 0l;
766         }
767         else
768         {
769             TRACE("Negotiated %s\n", debugstr_a(buffer));
770             sscanf(buffer + 3, "%lx", &(helper->neg_flags));
771             TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
772         }
773
774         TRACE("Getting session key\n");
775         lstrcpynA(buffer, "GK", max_len - 1);
776         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
777             goto isc_end;
778
779         if(strncmp(buffer, "BH", 2) == 0)
780             TRACE("No key negotiated.\n");
781         else if(strncmp(buffer, "GK ", 3) == 0)
782         {
783             if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len, 
784                             &bin_len)) != SEC_E_OK)
785             {
786                 TRACE("Failed to decode session key\n");
787             }
788             TRACE("Session key is %s\n", debugstr_a(buffer+3));
789             HeapFree(GetProcessHeap(), 0, helper->session_key);
790             helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
791             if(!helper->session_key)
792             {
793                 TRACE("Failed to allocate memory for session key\n");
794                 ret = SEC_E_INTERNAL_ERROR;
795                 goto isc_end;
796             }
797             memcpy(helper->session_key, bin, bin_len);
798         }
799
800         helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
801         SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
802         helper->crypt.ntlm.seq_num = 0l;
803         SECUR32_CreateNTLMv2SubKeys(helper);
804         helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
805         helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
806         SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
807                 (BYTE *)helper->crypt.ntlm2.send_seal_key, 16);
808         SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
809                (BYTE *)helper->crypt.ntlm2.recv_seal_key, 16);
810         helper->crypt.ntlm2.send_seq_no = 0l;
811         helper->crypt.ntlm2.recv_seq_no = 0l;
812     }
813
814 isc_end:
815     HeapFree(GetProcessHeap(), 0, want_flags);
816     HeapFree(GetProcessHeap(), 0, buffer);
817     HeapFree(GetProcessHeap(), 0, bin);
818     return ret;
819 }
820
821 /***********************************************************************
822  *              InitializeSecurityContextA
823  */
824 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
825  PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
826  ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep, 
827  PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext, 
828  PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
829 {
830     SECURITY_STATUS ret;
831     SEC_WCHAR *target = NULL;
832
833     TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
834      debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
835      Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
836
837     if(pszTargetName != NULL)
838     {
839         int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
840             strlen(pszTargetName)+1, NULL, 0);
841         target = HeapAlloc(GetProcessHeap(), 0, target_size *
842                 sizeof(SEC_WCHAR));
843         MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
844             target, target_size);
845     }
846
847     ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
848             fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
849             phNewContext, pOutput, pfContextAttr, ptsExpiry);
850
851     HeapFree(GetProcessHeap(), 0, target);
852     return ret;
853 }
854
855 /***********************************************************************
856  *              AcceptSecurityContext
857  */
858 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
859  PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
860  ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext, 
861  PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
862 {
863     SECURITY_STATUS ret;
864     char *buffer, *want_flags = NULL;
865     PBYTE bin;
866     int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
867     ULONG ctxt_attr = 0;
868     PNegoHelper helper;
869     PNtlmCredentials ntlm_cred;
870
871     TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
872      fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
873      ptsExpiry);
874
875     buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
876     bin    = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
877
878     if(TargetDataRep == SECURITY_NETWORK_DREP){
879         TRACE("Using SECURITY_NETWORK_DREP\n");
880     }
881
882     if(phContext == NULL)
883     {
884         static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
885         SEC_CHAR *server_argv[] = { ntlm_auth,
886             server_helper_protocol,
887             NULL };
888
889         if (!phCredential)
890         {
891             ret = SEC_E_INVALID_HANDLE;
892             goto asc_end;
893         }
894
895         ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
896
897         if(ntlm_cred->mode != NTLM_SERVER)
898         {
899             ret = SEC_E_INVALID_HANDLE;
900             goto asc_end;
901         }
902
903         /* This is the first call to AcceptSecurityHandle */
904         if(pInput == NULL)
905         {
906             ret = SEC_E_INCOMPLETE_MESSAGE;
907             goto asc_end;
908         }
909
910         if(pInput->cBuffers < 1)
911         {
912             ret = SEC_E_INCOMPLETE_MESSAGE;
913             goto asc_end;
914         }
915
916         if(pInput->pBuffers[0].cbBuffer > max_len)
917         {
918             ret = SEC_E_INVALID_TOKEN;
919             goto asc_end;
920         }
921         else
922             bin_len = pInput->pBuffers[0].cbBuffer;
923
924         if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
925             SEC_E_OK)
926         {
927             ret = SEC_E_INTERNAL_ERROR;
928             goto asc_end;
929         }
930         helper->mode = NTLM_SERVER;
931
932         /* Handle all the flags */
933         want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
934         if(want_flags == NULL)
935         {
936             TRACE("Failed to allocate memory for the want_flags!\n");
937             ret = SEC_E_INSUFFICIENT_MEMORY;
938             cleanup_helper(helper);
939             goto asc_end;
940         }
941         lstrcpyA(want_flags, "SF");
942         if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
943         {
944             FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
945         }
946         if(fContextReq & ASC_REQ_CONFIDENTIALITY)
947         {
948             lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
949         }
950         if(fContextReq & ASC_REQ_CONNECTION)
951         {
952             /* This is default, so we'll enable it */
953             lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
954             ctxt_attr |= ASC_RET_CONNECTION;
955         }
956         if(fContextReq & ASC_REQ_EXTENDED_ERROR)
957         {
958             FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
959         }
960         if(fContextReq & ASC_REQ_INTEGRITY)
961         {
962             lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
963         }
964         if(fContextReq & ASC_REQ_MUTUAL_AUTH)
965         {
966             FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
967         }
968         if(fContextReq & ASC_REQ_REPLAY_DETECT)
969         {
970             FIXME("ASC_REQ_REPLAY_DETECT stub\n");
971         }
972         if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
973         {
974             FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
975         }
976         if(fContextReq & ISC_REQ_STREAM)
977         {
978             FIXME("ASC_REQ_STREAM stub\n");
979         }
980         /* Done with the flags */
981
982         if(lstrlenA(want_flags) > 3)
983         {
984             TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
985             lstrcpynA(buffer, want_flags, max_len - 1);
986             if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
987                     SEC_E_OK)
988             {
989                 cleanup_helper(helper);
990                 goto asc_end;
991             }
992             if(!strncmp(buffer, "BH", 2))
993                 TRACE("Helper doesn't understand new command set\n");
994         }
995
996         /* This is the YR request from the client, encode to base64 */
997
998         memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
999
1000         lstrcpynA(buffer, "YR ", max_len-1);
1001
1002         if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1003                     &buffer_len)) != SEC_E_OK)
1004         {
1005             cleanup_helper(helper);
1006             goto asc_end;
1007         }
1008
1009         TRACE("Client sent: %s\n", debugstr_a(buffer));
1010
1011         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1012                     SEC_E_OK)
1013         {
1014             cleanup_helper(helper);
1015             goto asc_end;
1016         }
1017
1018         TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1019         /* The expected answer is TT <base64 blob> */
1020
1021         if(strncmp(buffer, "TT ", 3) != 0)
1022         {
1023             ret = SEC_E_INTERNAL_ERROR;
1024             cleanup_helper(helper);
1025             goto asc_end;
1026         }
1027
1028         if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
1029                         &bin_len)) != SEC_E_OK)
1030         {
1031             cleanup_helper(helper);
1032             goto asc_end;
1033         }
1034
1035         /* send this to the client */
1036         if(pOutput == NULL)
1037         {
1038             ret = SEC_E_INSUFFICIENT_MEMORY;
1039             cleanup_helper(helper);
1040             goto asc_end;
1041         }
1042
1043         if(pOutput->cBuffers < 1)
1044         {
1045             ret = SEC_E_INSUFFICIENT_MEMORY;
1046             cleanup_helper(helper);
1047             goto asc_end;
1048         }
1049
1050         pOutput->pBuffers[0].cbBuffer = bin_len;
1051         pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
1052         memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
1053         ret = SEC_I_CONTINUE_NEEDED;
1054
1055     }
1056     else
1057     {
1058         /* we expect a KK request from client */
1059         if(pInput == NULL)
1060         {
1061             ret = SEC_E_INCOMPLETE_MESSAGE;
1062             goto asc_end;
1063         }
1064
1065         if(pInput->cBuffers < 1)
1066         {
1067             ret = SEC_E_INCOMPLETE_MESSAGE;
1068             goto asc_end;
1069         }
1070
1071         if(!phContext)
1072         {
1073             ret = SEC_E_INVALID_HANDLE;
1074             goto asc_end;
1075         }
1076
1077         helper = (PNegoHelper)phContext->dwLower;
1078
1079         if(helper->mode != NTLM_SERVER)
1080         {
1081             ret = SEC_E_INVALID_HANDLE;
1082             goto asc_end;
1083         }
1084
1085         if(pInput->pBuffers[0].cbBuffer > max_len)
1086         {
1087             ret = SEC_E_INVALID_TOKEN;
1088             goto asc_end;
1089         }
1090         else
1091             bin_len = pInput->pBuffers[0].cbBuffer;
1092
1093         memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
1094
1095         lstrcpynA(buffer, "KK ", max_len-1);
1096
1097         if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
1098                     &buffer_len)) != SEC_E_OK)
1099         {
1100             goto asc_end;
1101         }
1102
1103         TRACE("Client sent: %s\n", debugstr_a(buffer));
1104
1105         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
1106                     SEC_E_OK)
1107         {
1108             goto asc_end;
1109         }
1110
1111         TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
1112
1113         if(strncmp(buffer, "AF ", 3) != 0)
1114         {
1115             if(strncmp(buffer, "NA ", 3) == 0)
1116             {
1117                 ret = SEC_E_LOGON_DENIED;
1118                 goto asc_end;
1119             }
1120             else
1121             {
1122                 ret = SEC_E_INTERNAL_ERROR;
1123                 goto asc_end;
1124             }
1125         }
1126         pOutput->pBuffers[0].cbBuffer = 0;
1127         ret = SEC_E_OK;
1128
1129         TRACE("Getting negotiated flags\n");
1130         lstrcpynA(buffer, "GF", max_len - 1);
1131         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1132             goto asc_end;
1133
1134         if(buffer_len < 3)
1135         {
1136             TRACE("No flags negotiated, or helper does not support GF command\n");
1137         }
1138         else
1139         {
1140             TRACE("Negotiated %s\n", debugstr_a(buffer));
1141             sscanf(buffer + 3, "%lx", &(helper->neg_flags));
1142             TRACE("Stored 0x%08lx as flags\n", helper->neg_flags);
1143         }
1144
1145         TRACE("Getting session key\n");
1146         lstrcpynA(buffer, "GK", max_len - 1);
1147         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
1148             goto asc_end;
1149
1150         if(buffer_len < 3)
1151             TRACE("Helper does not support GK command\n");
1152         else
1153         {
1154             if(strncmp(buffer, "BH ", 3) == 0)
1155             {
1156                 TRACE("Helper sent %s\n", debugstr_a(buffer+3));
1157                 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1158                 /*FIXME: Generate the dummy session key = MD4(MD4(password))*/
1159                 memset(helper->session_key, 0 , 16);
1160             }
1161             else if(strncmp(buffer, "GK ", 3) == 0)
1162             {
1163                 if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len, 
1164                                 &bin_len)) != SEC_E_OK)
1165                 {
1166                     TRACE("Failed to decode session key\n");
1167                 }
1168                 TRACE("Session key is %s\n", debugstr_a(buffer+3));
1169                 helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
1170                 if(!helper->session_key)
1171                 {
1172                     TRACE("Failed to allocate memory for session key\n");
1173                     ret = SEC_E_INTERNAL_ERROR;
1174                     goto asc_end;
1175                 }
1176                 memcpy(helper->session_key, bin, 16);
1177             }
1178         }
1179         helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
1180         SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
1181         helper->crypt.ntlm.seq_num = 0l;
1182     }
1183
1184     phNewContext->dwUpper = ctxt_attr;
1185     phNewContext->dwLower = (ULONG_PTR)helper;
1186
1187 asc_end:
1188     HeapFree(GetProcessHeap(), 0, want_flags);
1189     HeapFree(GetProcessHeap(), 0, buffer);
1190     HeapFree(GetProcessHeap(), 0, bin);
1191     return ret;
1192 }
1193
1194 /***********************************************************************
1195  *              CompleteAuthToken
1196  */
1197 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
1198  PSecBufferDesc pToken)
1199 {
1200     /* We never need to call CompleteAuthToken anyway */
1201     TRACE("%p %p\n", phContext, pToken);
1202     if (!phContext)
1203         return SEC_E_INVALID_HANDLE;
1204     
1205     return SEC_E_OK;
1206 }
1207
1208 /***********************************************************************
1209  *              DeleteSecurityContext
1210  */
1211 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
1212 {
1213     PNegoHelper helper;
1214
1215     TRACE("%p\n", phContext);
1216     if (!phContext)
1217         return SEC_E_INVALID_HANDLE;
1218
1219     helper = (PNegoHelper)phContext->dwLower;
1220
1221     phContext->dwUpper = 0;
1222     phContext->dwLower = 0;
1223
1224     SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
1225     HeapFree(GetProcessHeap(), 0, helper->session_key);
1226     SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
1227     SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
1228     HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
1229     HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
1230     HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
1231     HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
1232
1233     cleanup_helper(helper);
1234
1235     return SEC_E_OK;
1236 }
1237
1238 /***********************************************************************
1239  *              QueryContextAttributesW
1240  */
1241 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
1242  ULONG ulAttribute, void *pBuffer)
1243 {
1244     TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
1245     if (!phContext)
1246         return SEC_E_INVALID_HANDLE;
1247
1248     switch(ulAttribute)
1249     {
1250 #define _x(x) case (x) : FIXME(#x" stub\n"); break
1251         _x(SECPKG_ATTR_ACCESS_TOKEN);
1252         _x(SECPKG_ATTR_AUTHORITY);
1253         _x(SECPKG_ATTR_DCE_INFO);
1254         case SECPKG_ATTR_FLAGS:
1255         {
1256             PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
1257             PNegoHelper helper = (PNegoHelper)phContext->dwLower;
1258
1259             spcf->Flags = 0;
1260             if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1261                 spcf->Flags |= ISC_RET_INTEGRITY;
1262             if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1263                 spcf->Flags |= ISC_RET_CONFIDENTIALITY;
1264             return SEC_E_OK;
1265         }
1266         _x(SECPKG_ATTR_KEY_INFO);
1267         _x(SECPKG_ATTR_LIFESPAN);
1268         _x(SECPKG_ATTR_NAMES);
1269         _x(SECPKG_ATTR_NATIVE_NAMES);
1270         _x(SECPKG_ATTR_NEGOTIATION_INFO);
1271         _x(SECPKG_ATTR_PACKAGE_INFO);
1272         _x(SECPKG_ATTR_PASSWORD_EXPIRY);
1273         _x(SECPKG_ATTR_SESSION_KEY);
1274         case SECPKG_ATTR_SIZES:
1275         {
1276             PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
1277             spcs->cbMaxToken = NTLM_MAX_BUF;
1278             spcs->cbMaxSignature = 16;
1279             spcs->cbBlockSize = 0;
1280             spcs->cbSecurityTrailer = 16;
1281             return SEC_E_OK;
1282         }
1283         _x(SECPKG_ATTR_STREAM_SIZES);
1284         _x(SECPKG_ATTR_TARGET_INFORMATION);
1285 #undef _x
1286         default:
1287             TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
1288     }
1289
1290     return SEC_E_UNSUPPORTED_FUNCTION;
1291 }
1292
1293 /***********************************************************************
1294  *              QueryContextAttributesA
1295  */
1296 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
1297  ULONG ulAttribute, void *pBuffer)
1298 {
1299     return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
1300 }
1301
1302 /***********************************************************************
1303  *              ImpersonateSecurityContext
1304  */
1305 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
1306 {
1307     SECURITY_STATUS ret;
1308
1309     TRACE("%p\n", phContext);
1310     if (phContext)
1311     {
1312         ret = SEC_E_UNSUPPORTED_FUNCTION;
1313     }
1314     else
1315     {
1316         ret = SEC_E_INVALID_HANDLE;
1317     }
1318     return ret;
1319 }
1320
1321 /***********************************************************************
1322  *              RevertSecurityContext
1323  */
1324 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1325 {
1326     SECURITY_STATUS ret;
1327
1328     TRACE("%p\n", phContext);
1329     if (phContext)
1330     {
1331         ret = SEC_E_UNSUPPORTED_FUNCTION;
1332     }
1333     else
1334     {
1335         ret = SEC_E_INVALID_HANDLE;
1336     }
1337     return ret;
1338 }
1339
1340 /***********************************************************************
1341  *             ntlm_CreateSignature
1342  * As both MakeSignature and VerifySignature need this, but different keys
1343  * are needed for NTLMv2, the logic goes into a helper function.
1344  * To ensure maximal reusability, we can specify the direction as NTLM_SEND for
1345  * signing/encrypting and NTLM_RECV for verfying/decrypting. When encrypting,
1346  * the signature is encrypted after the message was encrypted, so
1347  * CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
1348  * false.
1349  */
1350 static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
1351         int token_idx, SignDirection direction, BOOL encrypt_sig)
1352 {
1353     ULONG sign_version = 1;
1354     UINT i;
1355     PBYTE sig;
1356     TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
1357             encrypt_sig);
1358
1359     sig = pMessage->pBuffers[token_idx].pvBuffer;
1360
1361     if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
1362             helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1363     {
1364         BYTE digest[16];
1365         BYTE seq_no[4];
1366         HMAC_MD5_CTX hmac_md5_ctx;
1367
1368         TRACE("Signing NTLM2 style\n");
1369
1370         if(direction == NTLM_SEND)
1371         {
1372             seq_no[0] = (helper->crypt.ntlm2.send_seq_no >>  0) & 0xff;
1373             seq_no[1] = (helper->crypt.ntlm2.send_seq_no >>  8) & 0xff;
1374             seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
1375             seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
1376
1377             ++(helper->crypt.ntlm2.send_seq_no);
1378
1379             HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
1380         }
1381         else
1382         {
1383             seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >>  0) & 0xff;
1384             seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >>  8) & 0xff;
1385             seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
1386             seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
1387
1388             ++(helper->crypt.ntlm2.recv_seq_no);
1389
1390             HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
1391         }
1392
1393         HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
1394         for( i = 0; i < pMessage->cBuffers; ++i )
1395         {
1396             if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1397                 HMACMD5Update(&hmac_md5_ctx, (BYTE *)pMessage->pBuffers[i].pvBuffer,
1398                         pMessage->pBuffers[i].cbBuffer);
1399         }
1400
1401         HMACMD5Final(&hmac_md5_ctx, digest);
1402
1403         if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1404         {
1405             if(direction == NTLM_SEND)
1406                 SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
1407             else
1408                 SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
1409         }
1410
1411         /* The NTLM2 signature is the sign version */
1412         sig[ 0] = (sign_version >>  0) & 0xff;
1413         sig[ 1] = (sign_version >>  8) & 0xff;
1414         sig[ 2] = (sign_version >> 16) & 0xff;
1415         sig[ 3] = (sign_version >> 24) & 0xff;
1416         /* The first 8 bytes of the digest */
1417         memcpy(sig+4, digest, 8);
1418         /* And the sequence number */
1419         memcpy(sig+12, seq_no, 4);
1420
1421         pMessage->pBuffers[token_idx].cbBuffer = 16;
1422
1423         return SEC_E_OK;
1424     }
1425     if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
1426     {
1427         ULONG crc = 0U;
1428         TRACE("Signing NTLM1 style\n");
1429
1430         for(i=0; i < pMessage->cBuffers; ++i)
1431         {
1432             if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
1433             {
1434                 crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
1435                     pMessage->pBuffers[i].cbBuffer, crc);
1436             }
1437         }
1438
1439         sig[ 0] = (sign_version >>  0) & 0xff;
1440         sig[ 1] = (sign_version >>  8) & 0xff;
1441         sig[ 2] = (sign_version >> 16) & 0xff;
1442         sig[ 3] = (sign_version >> 24) & 0xff;
1443         memset(sig+4, 0, 4);
1444         sig[ 8] = (crc >>  0) & 0xff;
1445         sig[ 9] = (crc >>  8) & 0xff;
1446         sig[10] = (crc >> 16) & 0xff;
1447         sig[11] = (crc >> 24) & 0xff;
1448         sig[12] = (helper->crypt.ntlm.seq_num >>  0) & 0xff;
1449         sig[13] = (helper->crypt.ntlm.seq_num >>  8) & 0xff;
1450         sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
1451         sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
1452
1453         ++(helper->crypt.ntlm.seq_num);
1454
1455         if(encrypt_sig)
1456             SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1457         return SEC_E_OK;
1458     }
1459
1460     if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1461     {
1462         TRACE("Creating a dummy signature.\n");
1463         /* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
1464         memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
1465         memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
1466         pMessage->pBuffers[token_idx].cbBuffer = 16;
1467         return SEC_E_OK;
1468     }
1469
1470     return SEC_E_UNSUPPORTED_FUNCTION;
1471 }
1472
1473 /***********************************************************************
1474  *              MakeSignature
1475  */
1476 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1477  PSecBufferDesc pMessage, ULONG MessageSeqNo)
1478 {
1479     PNegoHelper helper;
1480     int token_idx;
1481
1482     TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1483     if (!phContext)
1484         return SEC_E_INVALID_HANDLE;
1485
1486     if(fQOP)
1487         FIXME("Ignoring fQOP 0x%08x\n", fQOP);
1488
1489     if(MessageSeqNo)
1490         FIXME("Ignoring MessageSeqNo\n");
1491
1492     if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1493         return SEC_E_INVALID_TOKEN;
1494
1495     /* If we didn't find a SECBUFFER_TOKEN type buffer */
1496     if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1497         return SEC_E_INVALID_TOKEN;
1498
1499     if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1500         return SEC_E_BUFFER_TOO_SMALL;
1501
1502     helper = (PNegoHelper)phContext->dwLower;
1503     TRACE("Negotiated flags are: 0x%08lx\n", helper->neg_flags);
1504
1505     return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
1506 }
1507
1508 /***********************************************************************
1509  *              VerifySignature
1510  */
1511 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1512  PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1513 {
1514     PNegoHelper helper;
1515     ULONG fQOP = 0;
1516     UINT i;
1517     int token_idx;
1518     SECURITY_STATUS ret;
1519     SecBufferDesc local_desc;
1520     PSecBuffer     local_buff;
1521     BYTE          local_sig[16];
1522
1523     TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1524     if(!phContext)
1525         return SEC_E_INVALID_HANDLE;
1526
1527     if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1528         return SEC_E_INVALID_TOKEN;
1529
1530     if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1531         return SEC_E_INVALID_TOKEN;
1532
1533     if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1534         return SEC_E_BUFFER_TOO_SMALL;
1535
1536     if(MessageSeqNo)
1537         FIXME("Ignoring MessageSeqNo\n");
1538
1539     helper = (PNegoHelper)phContext->dwLower;
1540     TRACE("Negotiated flags: 0x%08lx\n", helper->neg_flags);
1541
1542     local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
1543
1544     local_desc.ulVersion = SECBUFFER_VERSION;
1545     local_desc.cBuffers = pMessage->cBuffers;
1546     local_desc.pBuffers = local_buff;
1547
1548     for(i=0; i < pMessage->cBuffers; ++i)
1549     {
1550         if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
1551         {
1552             local_buff[i].BufferType = SECBUFFER_TOKEN;
1553             local_buff[i].cbBuffer = 16;
1554             local_buff[i].pvBuffer = local_sig;
1555         }
1556         else
1557         {
1558             local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
1559             local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
1560             local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
1561         }
1562     }
1563
1564     if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
1565         return ret;
1566
1567     if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
1568                 ((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
1569         ret = SEC_E_MESSAGE_ALTERED;
1570     else
1571         ret = SEC_E_OK;
1572
1573     HeapFree(GetProcessHeap(), 0, local_buff);
1574     pfQOP = &fQOP;
1575
1576     return ret;
1577
1578 }
1579
1580 /***********************************************************************
1581  *             FreeCredentialsHandle
1582  */
1583 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1584         PCredHandle phCredential)
1585 {
1586     SECURITY_STATUS ret;
1587
1588     if(phCredential){
1589         PNtlmCredentials ntlm_cred = (PNtlmCredentials) phCredential->dwLower;
1590         phCredential->dwUpper = 0;
1591         phCredential->dwLower = 0;
1592         if (ntlm_cred->password)
1593             memset(ntlm_cred->password, 0, ntlm_cred->pwlen);
1594         HeapFree(GetProcessHeap(), 0, ntlm_cred->password);
1595         HeapFree(GetProcessHeap(), 0, ntlm_cred->username_arg);
1596         HeapFree(GetProcessHeap(), 0, ntlm_cred->domain_arg);
1597         ret = SEC_E_OK;
1598     }
1599     else
1600         ret = SEC_E_OK;
1601     
1602     return ret;
1603 }
1604
1605 /***********************************************************************
1606  *             EncryptMessage
1607  */
1608 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1609         ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1610 {
1611     PNegoHelper helper;
1612     int token_idx;
1613
1614     TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
1615
1616     if(!phContext)
1617         return SEC_E_INVALID_HANDLE;
1618
1619     if(fQOP)
1620         FIXME("Ignoring fQOP\n");
1621
1622     if(MessageSeqNo)
1623         FIXME("Ignoring MessageSeqNo\n");
1624
1625     if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1626         return SEC_E_INVALID_TOKEN;
1627
1628     if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1629         return SEC_E_INVALID_TOKEN;
1630
1631     if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1632         return SEC_E_BUFFER_TOO_SMALL;
1633
1634     helper = (PNegoHelper) phContext->dwLower;
1635
1636     if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && 
1637             helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1638     { 
1639         ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1640         SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1641                 (BYTE *)pMessage->pBuffers[1].pvBuffer,
1642                 pMessage->pBuffers[1].cbBuffer);
1643
1644         if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
1645             SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
1646                     ((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
1647
1648
1649         return SEC_E_OK;
1650     }
1651     else
1652     {
1653         PBYTE sig;
1654         ULONG save_flags;
1655
1656         /* EncryptMessage always produces real signatures, so make sure
1657          * NTLMSSP_NEGOTIATE_SIGN is set*/
1658         save_flags = helper->neg_flags;
1659         helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1660         ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
1661         helper->neg_flags = save_flags;
1662
1663         sig = pMessage->pBuffers[token_idx].pvBuffer;
1664
1665         SECUR32_arc4Process(helper->crypt.ntlm.a4i, pMessage->pBuffers[1].pvBuffer,
1666                 pMessage->pBuffers[1].cbBuffer);
1667         SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
1668
1669         if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
1670             memset(sig+4, 0, 4);
1671
1672     }
1673
1674     return SEC_E_OK;
1675 }
1676
1677 /***********************************************************************
1678  *             DecryptMessage
1679  */
1680 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1681         PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1682 {
1683     SECURITY_STATUS ret;
1684     ULONG ntlmssp_flags_save;
1685     PNegoHelper helper;
1686     int token_idx;
1687     TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
1688
1689     if(!phContext)
1690         return SEC_E_INVALID_HANDLE;
1691
1692     if(MessageSeqNo)
1693         FIXME("Ignoring MessageSeqNo\n");
1694
1695     if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
1696         return SEC_E_INVALID_TOKEN;
1697
1698     if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
1699         return SEC_E_INVALID_TOKEN;
1700
1701     if(pMessage->pBuffers[token_idx].cbBuffer < 16)
1702         return SEC_E_BUFFER_TOO_SMALL;
1703
1704     helper = (PNegoHelper) phContext->dwLower;
1705
1706     if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
1707     {
1708         SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
1709                 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1710     }
1711     else
1712     {
1713         SECUR32_arc4Process(helper->crypt.ntlm.a4i,
1714                 pMessage->pBuffers[1].pvBuffer, pMessage->pBuffers[1].cbBuffer);
1715     }
1716
1717     /* Make sure we use a session key for the signature check, EncryptMessage
1718      * always does that, even in the dummy case */
1719     ntlmssp_flags_save = helper->neg_flags;
1720
1721     helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
1722     ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
1723
1724     helper->neg_flags = ntlmssp_flags_save;
1725
1726     return ret;
1727 }
1728
1729 static const SecurityFunctionTableA ntlmTableA = {
1730     1,
1731     NULL,   /* EnumerateSecurityPackagesA */
1732     ntlm_QueryCredentialsAttributesA,   /* QueryCredentialsAttributesA */
1733     ntlm_AcquireCredentialsHandleA,     /* AcquireCredentialsHandleA */
1734     ntlm_FreeCredentialsHandle,         /* FreeCredentialsHandle */
1735     NULL,   /* Reserved2 */
1736     ntlm_InitializeSecurityContextA,    /* InitializeSecurityContextA */
1737     ntlm_AcceptSecurityContext,         /* AcceptSecurityContext */
1738     ntlm_CompleteAuthToken,             /* CompleteAuthToken */
1739     ntlm_DeleteSecurityContext,         /* DeleteSecurityContext */
1740     NULL,  /* ApplyControlToken */
1741     ntlm_QueryContextAttributesA,       /* QueryContextAttributesA */
1742     ntlm_ImpersonateSecurityContext,    /* ImpersonateSecurityContext */
1743     ntlm_RevertSecurityContext,         /* RevertSecurityContext */
1744     ntlm_MakeSignature,                 /* MakeSignature */
1745     ntlm_VerifySignature,               /* VerifySignature */
1746     FreeContextBuffer,                  /* FreeContextBuffer */
1747     NULL,   /* QuerySecurityPackageInfoA */
1748     NULL,   /* Reserved3 */
1749     NULL,   /* Reserved4 */
1750     NULL,   /* ExportSecurityContext */
1751     NULL,   /* ImportSecurityContextA */
1752     NULL,   /* AddCredentialsA */
1753     NULL,   /* Reserved8 */
1754     NULL,   /* QuerySecurityContextToken */
1755     ntlm_EncryptMessage,                /* EncryptMessage */
1756     ntlm_DecryptMessage,                /* DecryptMessage */
1757     NULL,   /* SetContextAttributesA */
1758 };
1759
1760 static const SecurityFunctionTableW ntlmTableW = {
1761     1,
1762     NULL,   /* EnumerateSecurityPackagesW */
1763     ntlm_QueryCredentialsAttributesW,   /* QueryCredentialsAttributesW */
1764     ntlm_AcquireCredentialsHandleW,     /* AcquireCredentialsHandleW */
1765     ntlm_FreeCredentialsHandle,         /* FreeCredentialsHandle */
1766     NULL,   /* Reserved2 */
1767     ntlm_InitializeSecurityContextW,    /* InitializeSecurityContextW */
1768     ntlm_AcceptSecurityContext,         /* AcceptSecurityContext */
1769     ntlm_CompleteAuthToken,             /* CompleteAuthToken */
1770     ntlm_DeleteSecurityContext,         /* DeleteSecurityContext */
1771     NULL,  /* ApplyControlToken */
1772     ntlm_QueryContextAttributesW,       /* QueryContextAttributesW */
1773     ntlm_ImpersonateSecurityContext,    /* ImpersonateSecurityContext */
1774     ntlm_RevertSecurityContext,         /* RevertSecurityContext */
1775     ntlm_MakeSignature,                 /* MakeSignature */
1776     ntlm_VerifySignature,               /* VerifySignature */
1777     FreeContextBuffer,                  /* FreeContextBuffer */
1778     NULL,   /* QuerySecurityPackageInfoW */
1779     NULL,   /* Reserved3 */
1780     NULL,   /* Reserved4 */
1781     NULL,   /* ExportSecurityContext */
1782     NULL,   /* ImportSecurityContextW */
1783     NULL,   /* AddCredentialsW */
1784     NULL,   /* Reserved8 */
1785     NULL,   /* QuerySecurityContextToken */
1786     ntlm_EncryptMessage,                /* EncryptMessage */
1787     ntlm_DecryptMessage,                /* DecryptMessage */
1788     NULL,   /* SetContextAttributesW */
1789 };
1790
1791 #define NTLM_COMMENT \
1792    { 'N', 'T', 'L', 'M', ' ', \
1793      'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1794      'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1795
1796 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1797 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1798
1799 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1800
1801 static char ntlm_name_A[] = NTLM_NAME;
1802 static WCHAR ntlm_name_W[] = NTLM_NAME;
1803
1804 /* According to Windows, NTLM has the following capabilities.  */
1805 #define CAPS ( \
1806         SECPKG_FLAG_INTEGRITY | \
1807         SECPKG_FLAG_PRIVACY | \
1808         SECPKG_FLAG_TOKEN_ONLY | \
1809         SECPKG_FLAG_CONNECTION | \
1810         SECPKG_FLAG_MULTI_REQUIRED | \
1811         SECPKG_FLAG_IMPERSONATION | \
1812         SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1813         SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1814
1815 static const SecPkgInfoW infoW = {
1816     CAPS,
1817     1,
1818     RPC_C_AUTHN_WINNT,
1819     NTLM_MAX_BUF,
1820     ntlm_name_W,
1821     ntlm_comment_W
1822 };
1823
1824 static const SecPkgInfoA infoA = {
1825     CAPS,
1826     1,
1827     RPC_C_AUTHN_WINNT,
1828     NTLM_MAX_BUF,
1829     ntlm_name_A,
1830     ntlm_comment_A
1831 };
1832
1833 void SECUR32_initNTLMSP(void)
1834 {
1835     SECURITY_STATUS ret;
1836     PNegoHelper helper;
1837     static CHAR version[] = "--version";
1838
1839     SEC_CHAR *args[] = {
1840         ntlm_auth,
1841         version,
1842         NULL };
1843
1844     if((ret = fork_helper(&helper, ntlm_auth, args)) != SEC_E_OK)
1845     {
1846         /* Cheat and allocate a helper anyway, so cleanup later will work. */
1847         helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1848         helper->major = helper->minor = helper->micro = -1;
1849     }
1850     else
1851         check_version(helper);
1852
1853     if( (helper->major >  MIN_NTLM_AUTH_MAJOR_VERSION) ||
1854         (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION  &&
1855          helper->minor >  MIN_NTLM_AUTH_MINOR_VERSION) ||
1856         (helper->major == MIN_NTLM_AUTH_MAJOR_VERSION  &&
1857          helper->minor == MIN_NTLM_AUTH_MINOR_VERSION  &&
1858          helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION) )
1859     {
1860         SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1861         SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1862     }
1863     else
1864     {
1865         ERR("%s was not found or is outdated. "
1866             "Make sure that ntlm_auth >= %d.%d.%d is in your path.\n",
1867             ntlm_auth,
1868             MIN_NTLM_AUTH_MAJOR_VERSION,
1869             MIN_NTLM_AUTH_MINOR_VERSION,
1870             MIN_NTLM_AUTH_MICRO_VERSION);
1871     }
1872     cleanup_helper(helper);
1873 }