secur32: Implement CompleteAuthToken for NTLM.
[wine] / dlls / secur32 / ntlm.c
1 /*
2  * Copyright 2005 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 "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "rpc.h"
27 #include "sspi.h"
28 #include "lm.h"
29 #include "secur32_priv.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
33
34 #define NTLM_MAX_BUF 1904
35
36
37 /***********************************************************************
38  *              QueryCredentialsAttributesA
39  */
40 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
41         PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
42 {
43     SECURITY_STATUS ret;
44
45     TRACE("(%p, %ld, %p)\n", phCredential, ulAttribute, pBuffer);
46
47     if(ulAttribute == SECPKG_ATTR_NAMES)
48     {
49         FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
50         ret = SEC_E_UNSUPPORTED_FUNCTION;
51     }
52     else
53         ret = SEC_E_UNSUPPORTED_FUNCTION;
54     
55     return ret;
56 }
57
58 /***********************************************************************
59  *              QueryCredentialsAttributesW
60  */
61 static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
62         PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
63 {
64     SECURITY_STATUS ret;
65
66     TRACE("(%p, %ld, %p)\n", phCredential, ulAttribute, pBuffer);
67
68     if(ulAttribute == SECPKG_ATTR_NAMES)
69     {
70         FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
71         ret = SEC_E_UNSUPPORTED_FUNCTION;
72     }
73     else
74         ret = SEC_E_UNSUPPORTED_FUNCTION;
75     
76     return ret;
77 }
78
79 /***********************************************************************
80  *              AcquireCredentialsHandleW
81  */
82 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
83  SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
84  PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
85  PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
86 {
87     SECURITY_STATUS ret;
88     PNegoHelper helper = NULL;
89     static CHAR ntlm_auth[] = "ntlm_auth",
90                 helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
91
92     SEC_CHAR *client_user_arg = NULL;
93     SEC_CHAR *client_domain_arg = NULL;
94     SEC_WCHAR *username = NULL, *domain = NULL;
95
96     SEC_CHAR *client_argv[5];
97     SEC_CHAR *server_argv[] = { ntlm_auth,
98         helper_protocol,
99         NULL };
100
101     TRACE("(%s, %s, 0x%08lx, %p, %p, %p, %p, %p, %p)\n",
102      debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
103      pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
104
105
106     switch(fCredentialUse)
107     {
108         case SECPKG_CRED_INBOUND:
109             if( (ret = fork_helper(&helper, "ntlm_auth", server_argv)) !=
110                     SEC_E_OK)
111             {
112                 phCredential = NULL;
113                 break;
114             }
115             else
116             {
117                 helper->mode = NTLM_SERVER;
118                 phCredential->dwUpper = fCredentialUse;
119                 phCredential->dwLower = (DWORD)helper;
120             }
121             ret = SEC_E_OK;
122             break;
123         case SECPKG_CRED_OUTBOUND:
124             {
125                 static const char username_arg[] = "--username=";
126                 static const char domain_arg[] = "--domain=";
127                 static char ntlm_auth[] = "ntlm_auth";
128                 static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
129                 int unixcp_size;
130
131                 if(pAuthData == NULL)
132                 {
133                     LPWKSTA_USER_INFO_1 ui = NULL;
134                     NET_API_STATUS status;
135
136                     status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
137                     if (status != NERR_Success || ui == NULL)
138                     {
139                         ret = SEC_E_NO_CREDENTIALS;
140                         phCredential = NULL;
141                         break;
142                     }
143                     
144                     username = HeapAlloc(GetProcessHeap(), 0, 
145                             (lstrlenW(ui->wkui1_username)+1) * 
146                             sizeof(SEC_WCHAR));
147                     lstrcpyW(username, ui->wkui1_username);
148                         
149                     /* same for the domain */
150                     domain = HeapAlloc(GetProcessHeap(), 0, 
151                             (lstrlenW(ui->wkui1_logon_domain)+1) * 
152                             sizeof(SEC_WCHAR));
153                     lstrcpyW(domain, ui->wkui1_logon_domain);
154                     NetApiBufferFree(ui);
155                 }
156                 else
157                 {
158                     PSEC_WINNT_AUTH_IDENTITY_W auth_data = 
159                         (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
160
161                     if (!auth_data->UserLength || !auth_data->DomainLength)
162                     {
163                         ret = SEC_E_NO_CREDENTIALS;
164                         phCredential = NULL;
165                         break;
166                     }
167                     /* Get username and domain from pAuthData */
168                     username = HeapAlloc(GetProcessHeap(), 0, 
169                             (auth_data->UserLength + 1) * sizeof(SEC_WCHAR));
170                     lstrcpyW(username, auth_data->User);
171
172                     domain = HeapAlloc(GetProcessHeap(), 0,
173                             (auth_data->DomainLength + 1) * sizeof(SEC_WCHAR));
174                     lstrcpyW(domain, auth_data->Domain);
175                 }
176                 TRACE("Username is %s\n", debugstr_w(username));
177                 unixcp_size =  WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
178                         username, -1, NULL, 0, NULL, NULL) + sizeof(username_arg);
179                 client_user_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
180                 lstrcpyA(client_user_arg, username_arg);
181                 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, username, -1,
182                         client_user_arg + sizeof(username_arg) - 1, 
183                         unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
184
185                 TRACE("Domain name is %s\n", debugstr_w(domain));
186                 unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
187                         domain, -1, NULL, 0,  NULL, NULL) + sizeof(domain_arg);
188                 client_domain_arg = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
189                 lstrcpyA(client_domain_arg, domain_arg);
190                 WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domain,
191                         -1, client_domain_arg + sizeof(domain_arg) - 1, 
192                         unixcp_size - sizeof(domain) + 1, NULL, NULL);
193
194                 client_argv[0] = ntlm_auth;
195                 client_argv[1] = helper_protocol;
196                 client_argv[2] = client_user_arg;
197                 client_argv[3] = client_domain_arg;
198                 client_argv[4] = NULL;
199
200                 if((ret = fork_helper(&helper, "ntlm_auth", client_argv)) != 
201                         SEC_E_OK)
202                 {
203                     phCredential = NULL;
204                     break;
205                 }
206                 else
207                 {
208                     helper->mode = NTLM_CLIENT;
209
210                     if(pAuthData != NULL)
211                     {
212                         PSEC_WINNT_AUTH_IDENTITY_W auth_data = 
213                            (PSEC_WINNT_AUTH_IDENTITY_W)pAuthData;
214
215                         if(auth_data->PasswordLength != 0)
216                         {
217                             helper->pwlen = WideCharToMultiByte(CP_UNIXCP, 
218                                 WC_NO_BEST_FIT_CHARS, auth_data->Password, 
219                                 auth_data->PasswordLength+1, NULL, 0, NULL, NULL);
220                         
221                             helper->password = HeapAlloc(GetProcessHeap(), 0, 
222                                     helper->pwlen);
223
224                             WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
225                                 auth_data->Password, auth_data->PasswordLength+1,
226                                 helper->password, helper->pwlen, NULL, NULL);
227                         }
228                     }
229            
230                     phCredential->dwUpper = fCredentialUse;
231                     phCredential->dwLower = (DWORD)helper;
232                     TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
233                             phCredential->dwUpper, phCredential->dwLower);
234                 }
235                 ret = SEC_E_OK;
236                 break;
237             }
238         case SECPKG_CRED_BOTH:
239             FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
240             ret = SEC_E_UNSUPPORTED_FUNCTION;
241             phCredential = NULL;
242             break;
243         default:
244             phCredential = NULL;
245             ret = SEC_E_UNKNOWN_CREDENTIALS;
246     }
247     
248
249     HeapFree(GetProcessHeap(), 0, client_user_arg);
250     HeapFree(GetProcessHeap(), 0, client_domain_arg);
251     HeapFree(GetProcessHeap(), 0, username);
252     HeapFree(GetProcessHeap(), 0, domain);
253
254     return ret;
255 }
256
257 /***********************************************************************
258  *              AcquireCredentialsHandleA
259  */
260 static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
261  SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
262  PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
263  PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
264 {
265     SECURITY_STATUS ret;
266     int user_sizeW, domain_sizeW, passwd_sizeW;
267     
268     SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
269     
270     PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
271     PSEC_WINNT_AUTH_IDENTITY_A identity  = NULL;
272
273     TRACE("(%s, %s, 0x%08lx, %p, %p, %p, %p, %p, %p)\n",
274      debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
275      pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
276     
277     if(pszPackage != NULL)
278     {
279         int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
280                 NULL, 0);
281
282         package = HeapAlloc(GetProcessHeap(), 0, package_sizeW * 
283                 sizeof(SEC_WCHAR));
284         MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
285     }
286
287     
288     if(pAuthData != NULL)
289     {
290         identity = (PSEC_WINNT_AUTH_IDENTITY_A)pAuthData;
291         
292         if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
293         {
294             pAuthDataW = HeapAlloc(GetProcessHeap(), 0, 
295                     sizeof(SEC_WINNT_AUTH_IDENTITY_W));
296
297             if(identity->UserLength != 0)
298             {
299                 user_sizeW = MultiByteToWideChar(CP_ACP, 0, 
300                     (LPCSTR)identity->User, identity->UserLength+1, NULL, 0);
301                 user = HeapAlloc(GetProcessHeap(), 0, user_sizeW * 
302                         sizeof(SEC_WCHAR));
303                 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User, 
304                     identity->UserLength+1, user, user_sizeW);
305             }
306             else
307             {
308                 user_sizeW = 0;
309             }
310              
311             if(identity->DomainLength != 0)
312             {
313                 domain_sizeW = MultiByteToWideChar(CP_ACP, 0, 
314                     (LPCSTR)identity->Domain, identity->DomainLength+1, NULL, 0);
315                 domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW 
316                     * sizeof(SEC_WCHAR));
317                 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain, 
318                     identity->DomainLength+1, domain, domain_sizeW);
319             }
320             else
321             {
322                 domain_sizeW = 0;
323             }
324
325             if(identity->PasswordLength != 0)
326             {
327                 passwd_sizeW = MultiByteToWideChar(CP_ACP, 0, 
328                     (LPCSTR)identity->Password, identity->PasswordLength+1, 
329                     NULL, 0);
330                 passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
331                     * sizeof(SEC_WCHAR));
332                 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
333                     identity->PasswordLength+1, passwd, passwd_sizeW);
334             }
335             else
336             {
337                 passwd_sizeW = 0;
338             }
339             
340             pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
341             pAuthDataW->User = user;
342             pAuthDataW->UserLength = user_sizeW;
343             pAuthDataW->Domain = domain;
344             pAuthDataW->DomainLength = domain_sizeW;
345             pAuthDataW->Password = passwd;
346             pAuthDataW->PasswordLength = passwd_sizeW;
347         }
348         else
349         {
350             pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
351         }
352     }       
353     
354     ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse, 
355             pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
356             ptsExpiry);
357     
358     HeapFree(GetProcessHeap(), 0, package);
359     HeapFree(GetProcessHeap(), 0, user);
360     HeapFree(GetProcessHeap(), 0, domain);
361     HeapFree(GetProcessHeap(), 0, passwd);
362     if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
363         HeapFree(GetProcessHeap(), 0, pAuthDataW);
364     
365     return ret;
366 }
367
368 /***********************************************************************
369  *              InitializeSecurityContextW
370  */
371 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
372  PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName, 
373  ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep, 
374  PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext, 
375  PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
376 {
377     SECURITY_STATUS ret;
378     PNegoHelper helper;
379     ULONG ctxt_attr = 0;
380     char* buffer;
381     PBYTE bin;
382     int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
383
384     TRACE("%p %p %s %ld %ld %ld %p %ld %p %p %p %p\n", phCredential, phContext,
385      debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
386      Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
387
388     if(!phCredential)
389         return SEC_E_INVALID_HANDLE;
390
391     /* As the server side of sspi never calls this, make sure that
392      * the handler is a client handler.
393      */
394     helper = (PNegoHelper)phCredential->dwLower;
395     if(helper->mode != NTLM_CLIENT)
396     {
397         TRACE("Helper mode = %d\n", helper->mode);
398         return SEC_E_INVALID_HANDLE;
399     }
400
401     /****************************************
402      * When communicating with the client, there can be the
403      * following reply packets:
404      * YR <base64 blob>         should be sent to the server
405      * PW                       should be sent back to helper with
406      *                          base64 encoded password
407      * AF <base64 blob>         client is done, blob should be
408      *                          sent to server with KK prefixed
409      * BH <char reason>         something broke
410      */
411     /* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
412
413     if (pszTargetName)
414     {
415         TRACE("According to a MS whitepaper pszTargetName is ignored.\n");
416     }
417     /* Handle all the flags */
418     if(fContextReq & ISC_REQ_CONFIDENTIALITY)
419     {
420         FIXME("InitializeSecurityContext(): ISC_REQ_CONFIDENTIALITY stub\n");
421     }
422     if(fContextReq & ISC_REQ_CONNECTION)
423     {
424         /* This is default, so we'll enable it */
425         ctxt_attr |= ISC_RET_CONNECTION;
426     }
427     if(fContextReq & ISC_REQ_EXTENDED_ERROR)
428         FIXME("ISC_REQ_EXTENDED_ERROR\n");
429     if(fContextReq & ISC_REQ_INTEGRITY)
430         FIXME("ISC_REQ_INTEGRITY\n");
431     if(fContextReq & ISC_REQ_MUTUAL_AUTH)
432         FIXME("ISC_REQ_MUTUAL_AUTH\n");
433     if(fContextReq & ISC_REQ_REPLAY_DETECT)
434         FIXME("ISC_REQ_REPLAY_DETECT\n");
435     if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
436         FIXME("ISC_REQ_SEQUENCE_DETECT\n");
437     if(fContextReq & ISC_REQ_STREAM)
438         FIXME("ISC_REQ_STREAM\n");
439
440     /* Done with the flags */
441     if(TargetDataRep == SECURITY_NETWORK_DREP){
442         TRACE("Setting SECURITY_NETWORK_DREP\n");
443     }
444
445     buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
446     bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
447
448     if((phContext == NULL) && (pInput == NULL))
449     {
450         TRACE("First time in ISC()\n");
451         /* Request a challenge request from ntlm_auth */
452         if(helper->password == NULL)
453         {
454             FIXME("Using empty password for now.\n");
455             lstrcpynA(buffer, "PW AA==", max_len-1);
456         }
457         else
458         {
459             lstrcpynA(buffer, "PW ", max_len-1);
460             if((ret = encodeBase64((unsigned char*)helper->password,
461                         helper->pwlen-2, buffer+3,
462                         max_len-3, &buffer_len)) != SEC_E_OK)
463             {
464                 TRACE("Deleting password!\n");
465                 memset(helper->password, 0, helper->pwlen-2);
466                 HeapFree(GetProcessHeap(), 0, helper->password);
467                 goto end;
468             }
469
470         }
471
472         TRACE("Sending to helper: %s\n", debugstr_a(buffer));
473         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
474             goto end;
475
476         TRACE("Helper returned %s\n", debugstr_a(buffer));
477         lstrcpynA(buffer, "YR", max_len-1);
478
479         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
480             goto end;
481
482         TRACE("%s\n", buffer);
483
484         if(strncmp(buffer, "YR ", 3) != 0)
485         {
486             /* Something borked */
487             TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
488             ret = SEC_E_INTERNAL_ERROR;
489             goto end;
490         }
491         if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
492                         max_len-1, &bin_len)) != SEC_E_OK)
493             goto end;
494
495         /* put the decoded client blob into the out buffer */
496
497         ret = SEC_I_CONTINUE_NEEDED;
498     }
499     else
500     {
501         /* handle second call here */
502         /* encode server data to base64 */
503         if (!pInput || !pInput->cBuffers)
504         {
505             ret = SEC_E_INCOMPLETE_MESSAGE;
506             goto end;
507         }
508
509         if (!pInput->pBuffers[0].pvBuffer)
510         {
511             ret = SEC_E_INTERNAL_ERROR;
512             goto end;
513         }
514
515         if(pInput->pBuffers[0].cbBuffer > max_len)
516         {
517             TRACE("pInput->pBuffers[0].cbBuffer is: %ld\n",
518                     pInput->pBuffers[0].cbBuffer);
519             ret = SEC_E_INVALID_TOKEN;
520             goto end;
521         }
522         else
523             bin_len = pInput->pBuffers[0].cbBuffer;
524
525         memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
526
527         lstrcpynA(buffer, "TT ", max_len-1);
528
529         if((ret = encodeBase64(bin, bin_len, buffer+3,
530                         max_len-3, &buffer_len)) != SEC_E_OK)
531             goto end;
532
533         TRACE("Server sent: %s\n", debugstr_a(buffer));
534
535         /* send TT base64 blob to ntlm_auth */
536         if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
537             goto end;
538
539         TRACE("Helper replied: %s\n", debugstr_a(buffer));
540
541         if( (strncmp(buffer, "KK ", 3) != 0) &&
542                 (strncmp(buffer, "AF ", 3) !=0))
543         {
544             TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
545             HeapFree(GetProcessHeap(), 0, buffer);
546             HeapFree(GetProcessHeap(), 0, bin);
547             return SEC_E_INVALID_TOKEN;
548         }
549
550         /* decode the blob and send it to server */
551         if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
552                         &bin_len)) != SEC_E_OK)
553         {
554             HeapFree(GetProcessHeap(), 0, buffer);
555             HeapFree(GetProcessHeap(), 0, bin);
556             return ret;
557         }
558
559         phNewContext->dwUpper = ctxt_attr;
560         phNewContext->dwLower = ret;
561
562         ret = SEC_E_OK;
563     }
564
565     /* put the decoded client blob into the out buffer */
566
567     if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
568     {
569         if (pOutput)
570         {
571             pOutput->cBuffers = 1;
572             pOutput->pBuffers[0].pvBuffer = SECUR32_ALLOC(bin_len);
573             pOutput->pBuffers[0].cbBuffer = bin_len;
574         }
575     }
576
577     if (!pOutput || !pOutput->cBuffers || pOutput->pBuffers[0].cbBuffer < bin_len)
578     {
579         TRACE("out buffer is NULL or has not enough space\n");
580         ret = SEC_E_BUFFER_TOO_SMALL;
581         goto end;
582     }
583
584     if (!pOutput->pBuffers[0].pvBuffer)
585     {
586         TRACE("out buffer is NULL\n");
587         ret = SEC_E_INTERNAL_ERROR;
588         goto end;
589     }
590
591     pOutput->pBuffers[0].cbBuffer = bin_len;
592     pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
593     memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
594
595     if(ret != SEC_I_CONTINUE_NEEDED)
596     {
597         TRACE("Deleting password!\n");
598         if(helper->password)
599             memset(helper->password, 0, helper->pwlen-2);
600         HeapFree(GetProcessHeap(), 0, helper->password);
601     }
602 end:
603     HeapFree(GetProcessHeap(), 0, buffer);
604     HeapFree(GetProcessHeap(), 0, bin);
605     return ret;
606 }
607
608 /***********************************************************************
609  *              InitializeSecurityContextA
610  */
611 static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
612  PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
613  ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep, 
614  PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext, 
615  PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
616 {
617     SECURITY_STATUS ret;
618
619     TRACE("%p %p %s %ld %ld %ld %p %ld %p %p %p %p\n", phCredential, phContext,
620      debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
621      Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
622     
623     if (phCredential)
624     {
625         SEC_WCHAR *target = NULL;
626         if(pszTargetName != NULL)
627         {
628             int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName, 
629                 strlen(pszTargetName)+1, NULL, 0);
630             target = HeapAlloc(GetProcessHeap(), 0, target_size * 
631                     sizeof(SEC_WCHAR));
632             MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
633                 target, target_size);
634         }
635         
636         ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target, 
637                 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
638                 phNewContext, pOutput, pfContextAttr, ptsExpiry);
639         
640         HeapFree(GetProcessHeap(), 0, target);
641     }
642     else
643     {
644         ret = SEC_E_INVALID_HANDLE;
645     }
646     return ret;
647 }
648
649 /***********************************************************************
650  *              AcceptSecurityContext
651  */
652 static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
653  PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
654  ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext, 
655  PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
656 {
657     SECURITY_STATUS ret;
658
659     TRACE("%p %p %p %ld %ld %p %p %p %p\n", phCredential, phContext, pInput,
660      fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
661      ptsExpiry);
662     if (phCredential)
663     {
664         PNegoHelper helper = (PNegoHelper)phCredential->dwLower;
665         /* Max size of input data is 2010 byte, as that's the maximum size 
666          * ntlm_auth will handle*/
667         char *buffer = HeapAlloc(GetProcessHeap(), 0, 
668                 sizeof(char) * NTLM_MAX_BUF);
669         PBYTE bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
670         int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
671         ULONG ctxt_attr = 0;
672
673         if(helper->mode != NTLM_SERVER)
674         {
675             HeapFree(GetProcessHeap(), 0, buffer);
676             HeapFree(GetProcessHeap(), 0, bin);
677             return SEC_E_INVALID_HANDLE;
678         }
679
680         /* Handle all the flags */
681         if(fContextReq & ISC_REQ_ALLOCATE_MEMORY)
682         {
683             FIXME("AcceptSecurityContext(): ISC_REQ_ALLOCATE_MEMORY stub\n");
684         }
685         if(fContextReq & ISC_REQ_CONFIDENTIALITY)
686         {
687             FIXME("AcceptSecurityContext(): ISC_REQ_CONFIDENTIALITY stub\n");
688         }
689         if(fContextReq & ISC_REQ_CONNECTION)
690         {
691             /* This is default, so we'll enable it */
692             ctxt_attr |= ISC_RET_CONNECTION;
693         }
694         if(fContextReq & ISC_REQ_EXTENDED_ERROR)
695         {
696             FIXME("AcceptSecurityContext(): ISC_REQ_EXTENDED_ERROR stub\n");
697         }
698         if(fContextReq & ISC_REQ_INTEGRITY)
699         {
700             FIXME("AcceptSecurityContext(): ISC_REQ_INTEGRITY stub\n");
701         }
702         if(fContextReq & ISC_REQ_MUTUAL_AUTH)
703         {
704             FIXME("AcceptSecurityContext(): ISC_REQ_MUTUAL_AUTH stub\n");
705         }
706         if(fContextReq & ISC_REQ_REPLAY_DETECT)
707         {
708             FIXME("AcceptSecurityContext(): ISC_REQ_REPLAY_DETECT stub\n");
709         }
710         if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
711         {
712             FIXME("AcceptSecurityContext(): ISC_REQ_SEQUENCE_DETECT stub\n");
713         }
714         if(fContextReq & ISC_REQ_STREAM)
715         {
716             FIXME("AcceptSecurityContext(): ISC_REQ_STREAM stub\n");
717         }
718         /* Done with the flags */
719         if(TargetDataRep == SECURITY_NETWORK_DREP){
720             TRACE("Using SECURITY_NETWORK_DREP\n");
721         }
722
723         
724         if(phContext == NULL)
725         {
726             /* This is the first call to AcceptSecurityHandle */
727             if(pInput == NULL)
728             {
729                 HeapFree(GetProcessHeap(), 0, buffer);
730                 HeapFree(GetProcessHeap(), 0, bin);
731                 return SEC_E_INCOMPLETE_MESSAGE;
732             }
733             
734             if(pInput->cBuffers < 1)
735             {
736                 HeapFree(GetProcessHeap(), 0, buffer);
737                 HeapFree(GetProcessHeap(), 0, bin);
738                 return SEC_E_INCOMPLETE_MESSAGE;
739             }
740
741             if(pInput->pBuffers[0].cbBuffer > max_len)
742             {
743                 HeapFree(GetProcessHeap(), 0, buffer);
744                 HeapFree(GetProcessHeap(), 0, bin);
745                 return SEC_E_INVALID_TOKEN;
746             }
747             else
748                 bin_len = pInput->pBuffers[0].cbBuffer;
749
750             /* This is the YR request from the client, encode to base64 */
751             
752             memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
753
754             lstrcpynA(buffer, "YR ", max_len-1);
755
756             if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
757                         &buffer_len)) != SEC_E_OK)
758             {
759                 HeapFree(GetProcessHeap(), 0, buffer);
760                 HeapFree(GetProcessHeap(), 0, bin);
761                 return ret;
762             }
763             
764             TRACE("Client sent: %s\n", debugstr_a(buffer));
765             
766             if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
767                         SEC_E_OK)
768             {
769                 HeapFree(GetProcessHeap(), 0, buffer);
770                 HeapFree(GetProcessHeap(), 0, bin);
771                 return ret;
772             }
773
774             TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
775             /* The expected answer is TT <base64 blob> */
776
777             if(strncmp(buffer, "TT ", 3) != 0)
778             {
779                 HeapFree(GetProcessHeap(), 0, buffer);
780                 HeapFree(GetProcessHeap(), 0, bin);
781                 return SEC_E_INVALID_TOKEN;
782             }
783
784             if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
785                             &bin_len)) != SEC_E_OK)
786             {
787                 HeapFree(GetProcessHeap(), 0, buffer);
788                 HeapFree(GetProcessHeap(), 0, bin);
789                 return ret;
790             }
791             
792             /* send this to the client */
793             if(pOutput == NULL)
794             {
795                 HeapFree(GetProcessHeap(), 0, buffer);
796                 HeapFree(GetProcessHeap(), 0, bin);
797                 return SEC_E_INSUFFICIENT_MEMORY;
798             }
799
800             if(pOutput->cBuffers < 1)
801             {
802                 HeapFree(GetProcessHeap(), 0, buffer);
803                 HeapFree(GetProcessHeap(), 0, bin);
804                 return SEC_E_INSUFFICIENT_MEMORY;
805             }
806
807             pOutput->pBuffers[0].cbBuffer = bin_len;
808             pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
809             memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
810             ret = SEC_I_CONTINUE_NEEDED;
811             
812         }
813         else
814         {
815             /* we expect a KK request from client */
816             if(pInput == NULL)
817             {
818                 HeapFree(GetProcessHeap(), 0, buffer);
819                 HeapFree(GetProcessHeap(), 0, bin);
820                 return SEC_E_INCOMPLETE_MESSAGE;
821             }
822             
823             if(pInput->cBuffers < 1)
824             {
825                 HeapFree(GetProcessHeap(), 0, buffer);
826                 HeapFree(GetProcessHeap(), 0, bin);
827                 return SEC_E_INCOMPLETE_MESSAGE;
828             }
829
830             if(pInput->pBuffers[0].cbBuffer > max_len)
831             {
832                 HeapFree(GetProcessHeap(), 0, buffer);
833                 HeapFree(GetProcessHeap(), 0, bin);
834                 return SEC_E_INVALID_TOKEN;
835             }
836             else
837                 bin_len = pInput->pBuffers[0].cbBuffer;
838
839             memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
840
841             lstrcpynA(buffer, "KK ", max_len-1);
842
843             if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
844                         &buffer_len)) != SEC_E_OK)
845             {
846                 HeapFree(GetProcessHeap(), 0, buffer);
847                 HeapFree(GetProcessHeap(), 0, bin);
848                 return ret;
849             }
850             
851             TRACE("Client sent: %s\n", debugstr_a(buffer));
852             
853             if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
854                         SEC_E_OK)
855             {
856                 HeapFree(GetProcessHeap(), 0, buffer);
857                 HeapFree(GetProcessHeap(), 0, bin);
858                 return ret;
859             }
860
861             TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
862             
863             if(strncmp(buffer, "AF ", 3) != 0)
864             {
865                 if(strncmp(buffer, "NA ", 3) == 0)
866                 {
867                     HeapFree(GetProcessHeap(), 0, buffer);
868                     HeapFree(GetProcessHeap(), 0, bin);
869                     return SEC_E_LOGON_DENIED;
870                 }
871                 else
872                 {
873                     HeapFree(GetProcessHeap(), 0, buffer);
874                     HeapFree(GetProcessHeap(), 0, bin);
875                     return SEC_E_INVALID_TOKEN;
876                 }
877             }
878             
879             ret = SEC_E_OK;
880         }
881         
882         phNewContext->dwUpper = ctxt_attr;
883         phNewContext->dwLower = ret;
884         HeapFree(GetProcessHeap(), 0, buffer);
885         HeapFree(GetProcessHeap(), 0, bin);
886
887     }
888     else
889     {
890         ret = SEC_E_INVALID_HANDLE;
891     }
892     return ret;
893 }
894
895 /***********************************************************************
896  *              CompleteAuthToken
897  */
898 static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
899  PSecBufferDesc pToken)
900 {
901     /* We never need to call CompleteAuthToken anyway */
902     TRACE("%p %p\n", phContext, pToken);
903     if (!phContext)
904         return SEC_E_INVALID_HANDLE;
905     
906     return SEC_E_OK;
907 }
908
909 /***********************************************************************
910  *              DeleteSecurityContext
911  */
912 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
913 {
914     SECURITY_STATUS ret;
915
916     TRACE("%p\n", phContext);
917     if (phContext)
918     {
919         phContext->dwUpper = 0;
920         phContext->dwLower = 0;
921         ret = SEC_E_OK;
922     }
923     else
924     {
925         ret = SEC_E_INVALID_HANDLE;
926     }
927     return ret;
928 }
929
930 /***********************************************************************
931  *              QueryContextAttributesW
932  */
933 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
934  unsigned long ulAttribute, void *pBuffer)
935 {
936     TRACE("%p %ld %p\n", phContext, ulAttribute, pBuffer);
937     if (!phContext)
938         return SEC_E_INVALID_HANDLE;
939
940     switch(ulAttribute)
941     {
942 #define _x(x) case (x) : FIXME(#x" stub\n"); break
943         _x(SECPKG_ATTR_ACCESS_TOKEN);
944         _x(SECPKG_ATTR_AUTHORITY);
945         _x(SECPKG_ATTR_DCE_INFO);
946         _x(SECPKG_ATTR_FLAGS);
947         _x(SECPKG_ATTR_KEY_INFO);
948         _x(SECPKG_ATTR_LIFESPAN);
949         _x(SECPKG_ATTR_NAMES);
950         _x(SECPKG_ATTR_NATIVE_NAMES);
951         _x(SECPKG_ATTR_NEGOTIATION_INFO);
952         _x(SECPKG_ATTR_PACKAGE_INFO);
953         _x(SECPKG_ATTR_PASSWORD_EXPIRY);
954         _x(SECPKG_ATTR_SESSION_KEY);
955         case SECPKG_ATTR_SIZES:
956             {
957                 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
958                 spcs->cbMaxToken = NTLM_MAX_BUF;
959                 spcs->cbMaxSignature = 16;
960                 spcs->cbBlockSize = 0;
961                 spcs->cbSecurityTrailer = 16;
962                 return SEC_E_OK;
963             }
964         _x(SECPKG_ATTR_STREAM_SIZES);
965         _x(SECPKG_ATTR_TARGET_INFORMATION);
966 #undef _x
967         default:
968             TRACE("Unknown value %ld passed for ulAttribute\n", ulAttribute);
969     }
970
971     return SEC_E_UNSUPPORTED_FUNCTION;
972 }
973
974 /***********************************************************************
975  *              QueryContextAttributesA
976  */
977 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
978  unsigned long ulAttribute, void *pBuffer)
979 {
980     return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
981 }
982
983 /***********************************************************************
984  *              ImpersonateSecurityContext
985  */
986 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
987 {
988     SECURITY_STATUS ret;
989
990     TRACE("%p\n", phContext);
991     if (phContext)
992     {
993         ret = SEC_E_UNSUPPORTED_FUNCTION;
994     }
995     else
996     {
997         ret = SEC_E_INVALID_HANDLE;
998     }
999     return ret;
1000 }
1001
1002 /***********************************************************************
1003  *              RevertSecurityContext
1004  */
1005 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1006 {
1007     SECURITY_STATUS ret;
1008
1009     TRACE("%p\n", phContext);
1010     if (phContext)
1011     {
1012         ret = SEC_E_UNSUPPORTED_FUNCTION;
1013     }
1014     else
1015     {
1016         ret = SEC_E_INVALID_HANDLE;
1017     }
1018     return ret;
1019 }
1020
1021 /***********************************************************************
1022  *              MakeSignature
1023  */
1024 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1025  PSecBufferDesc pMessage, ULONG MessageSeqNo)
1026 {
1027     SECURITY_STATUS ret;
1028
1029     TRACE("%p %ld %p %ld\n", phContext, fQOP, pMessage, MessageSeqNo);
1030     if (phContext)
1031     {
1032         ret = SEC_E_UNSUPPORTED_FUNCTION;
1033     }
1034     else
1035     {
1036         ret = SEC_E_INVALID_HANDLE;
1037     }
1038     return ret;
1039 }
1040
1041 /***********************************************************************
1042  *              VerifySignature
1043  */
1044 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1045  PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1046 {
1047     SECURITY_STATUS ret;
1048
1049     TRACE("%p %p %ld %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1050     if (phContext)
1051     {
1052         ret = SEC_E_UNSUPPORTED_FUNCTION;
1053     }
1054     else
1055     {
1056         ret = SEC_E_INVALID_HANDLE;
1057     }
1058     return ret;
1059 }
1060
1061 /***********************************************************************
1062  *             FreeCredentialsHandle
1063  */
1064 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1065         PCredHandle phCredential)
1066 {
1067     SECURITY_STATUS ret;
1068
1069     if(phCredential){
1070         PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1071         phCredential->dwUpper = 0;
1072         phCredential->dwLower = 0;
1073         cleanup_helper(helper);
1074         ret = SEC_E_OK;
1075     }
1076     else
1077         ret = SEC_E_OK;
1078     
1079     return ret;
1080 }
1081
1082 /***********************************************************************
1083  *             EncryptMessage
1084  */
1085 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1086         ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1087 {
1088     TRACE("%p %ld %p %ld stub\n", phContext, fQOP, pMessage, MessageSeqNo);
1089
1090     if(!phContext)
1091         return SEC_E_INVALID_HANDLE;
1092
1093     return SEC_E_UNSUPPORTED_FUNCTION;
1094 }
1095
1096 /***********************************************************************
1097  *             DecryptMessage
1098  */
1099 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1100         PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1101 {
1102     TRACE("%p %p %ld %p stub\n", phContext, pMessage, MessageSeqNo, pfQOP);
1103
1104     if(!phContext)
1105         return SEC_E_INVALID_HANDLE;
1106
1107     return SEC_E_UNSUPPORTED_FUNCTION;
1108 }
1109
1110 static SecurityFunctionTableA ntlmTableA = {
1111     1,
1112     NULL,   /* EnumerateSecurityPackagesA */
1113     ntlm_QueryCredentialsAttributesA,   /* QueryCredentialsAttributesA */
1114     ntlm_AcquireCredentialsHandleA,     /* AcquireCredentialsHandleA */
1115     ntlm_FreeCredentialsHandle,         /* FreeCredentialsHandle */
1116     NULL,   /* Reserved2 */
1117     ntlm_InitializeSecurityContextA,    /* InitializeSecurityContextA */
1118     ntlm_AcceptSecurityContext,         /* AcceptSecurityContext */
1119     ntlm_CompleteAuthToken,             /* CompleteAuthToken */
1120     ntlm_DeleteSecurityContext,         /* DeleteSecurityContext */
1121     NULL,  /* ApplyControlToken */
1122     ntlm_QueryContextAttributesA,       /* QueryContextAttributesA */
1123     ntlm_ImpersonateSecurityContext,    /* ImpersonateSecurityContext */
1124     ntlm_RevertSecurityContext,         /* RevertSecurityContext */
1125     ntlm_MakeSignature,                 /* MakeSignature */
1126     ntlm_VerifySignature,               /* VerifySignature */
1127     FreeContextBuffer,                  /* FreeContextBuffer */
1128     NULL,   /* QuerySecurityPackageInfoA */
1129     NULL,   /* Reserved3 */
1130     NULL,   /* Reserved4 */
1131     NULL,   /* ExportSecurityContext */
1132     NULL,   /* ImportSecurityContextA */
1133     NULL,   /* AddCredentialsA */
1134     NULL,   /* Reserved8 */
1135     NULL,   /* QuerySecurityContextToken */
1136     ntlm_EncryptMessage,                /* EncryptMessage */
1137     ntlm_DecryptMessage,                /* DecryptMessage */
1138     NULL,   /* SetContextAttributesA */
1139 };
1140
1141 static SecurityFunctionTableW ntlmTableW = {
1142     1,
1143     NULL,   /* EnumerateSecurityPackagesW */
1144     ntlm_QueryCredentialsAttributesW,   /* QueryCredentialsAttributesW */
1145     ntlm_AcquireCredentialsHandleW,     /* AcquireCredentialsHandleW */
1146     ntlm_FreeCredentialsHandle,         /* FreeCredentialsHandle */
1147     NULL,   /* Reserved2 */
1148     ntlm_InitializeSecurityContextW,    /* InitializeSecurityContextW */
1149     ntlm_AcceptSecurityContext,         /* AcceptSecurityContext */
1150     ntlm_CompleteAuthToken,             /* CompleteAuthToken */
1151     ntlm_DeleteSecurityContext,         /* DeleteSecurityContext */
1152     NULL,  /* ApplyControlToken */
1153     ntlm_QueryContextAttributesW,       /* QueryContextAttributesW */
1154     ntlm_ImpersonateSecurityContext,    /* ImpersonateSecurityContext */
1155     ntlm_RevertSecurityContext,         /* RevertSecurityContext */
1156     ntlm_MakeSignature,                 /* MakeSignature */
1157     ntlm_VerifySignature,               /* VerifySignature */
1158     FreeContextBuffer,                  /* FreeContextBuffer */
1159     NULL,   /* QuerySecurityPackageInfoW */
1160     NULL,   /* Reserved3 */
1161     NULL,   /* Reserved4 */
1162     NULL,   /* ExportSecurityContext */
1163     NULL,   /* ImportSecurityContextW */
1164     NULL,   /* AddCredentialsW */
1165     NULL,   /* Reserved8 */
1166     NULL,   /* QuerySecurityContextToken */
1167     ntlm_EncryptMessage,                /* EncryptMessage */
1168     ntlm_DecryptMessage,                /* DecryptMessage */
1169     NULL,   /* SetContextAttributesW */
1170 };
1171
1172 #define NTLM_COMMENT \
1173    { 'N', 'T', 'L', 'M', ' ', \
1174      'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1175      'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1176
1177 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1178 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1179
1180 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1181
1182 static char ntlm_name_A[] = NTLM_NAME;
1183 static WCHAR ntlm_name_W[] = NTLM_NAME;
1184
1185 /* According to Windows, NTLM has the following capabilities.  */
1186 #define CAPS ( \
1187         SECPKG_FLAG_INTEGRITY | \
1188         SECPKG_FLAG_PRIVACY | \
1189         SECPKG_FLAG_TOKEN_ONLY | \
1190         SECPKG_FLAG_CONNECTION | \
1191         SECPKG_FLAG_MULTI_REQUIRED | \
1192         SECPKG_FLAG_IMPERSONATION | \
1193         SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1194         SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1195
1196 static const SecPkgInfoW infoW = {
1197     CAPS,
1198     1,
1199     RPC_C_AUTHN_WINNT,
1200     NTLM_MAX_BUF,
1201     ntlm_name_W,
1202     ntlm_comment_W
1203 };
1204
1205 static const SecPkgInfoA infoA = {
1206     CAPS,
1207     1,
1208     RPC_C_AUTHN_WINNT,
1209     NTLM_MAX_BUF,
1210     ntlm_name_A,
1211     ntlm_comment_A
1212 };
1213
1214 void SECUR32_initNTLMSP(void)
1215 {
1216     SECURITY_STATUS ret;
1217     PNegoHelper helper;
1218     static CHAR ntlm_auth[] = "ntlm_auth",
1219                 version[]   = "--version";
1220
1221     SEC_CHAR *args[] = {
1222         ntlm_auth,
1223         version,
1224         NULL };
1225
1226     if((ret = fork_helper(&helper, "ntlm_auth", args)) != SEC_E_OK)
1227     {
1228         /* Cheat and allocate a helper anyway, so cleanup later will work. */
1229         helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1230         helper->version = -1;
1231     }
1232     else
1233         check_version(helper);
1234
1235     if(helper->version > 2)
1236     {
1237         SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1238         SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1239     }
1240     cleanup_helper(helper);
1241 }