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