dinput: joydev_enum_deviceA now returns the correct value.
[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     SECURITY_STATUS ret;
902
903     TRACE("%p %p\n", phContext, pToken);
904     if (phContext)
905     {
906         ret = SEC_E_UNSUPPORTED_FUNCTION;
907     }
908     else
909     {
910         ret = SEC_E_INVALID_HANDLE;
911     }
912     return ret;
913 }
914
915 /***********************************************************************
916  *              DeleteSecurityContext
917  */
918 static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
919 {
920     SECURITY_STATUS ret;
921
922     TRACE("%p\n", phContext);
923     if (phContext)
924     {
925         phContext->dwUpper = 0;
926         phContext->dwLower = 0;
927         ret = SEC_E_OK;
928     }
929     else
930     {
931         ret = SEC_E_INVALID_HANDLE;
932     }
933     return ret;
934 }
935
936 /***********************************************************************
937  *              QueryContextAttributesW
938  */
939 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
940  unsigned long ulAttribute, void *pBuffer)
941 {
942     TRACE("%p %ld %p\n", phContext, ulAttribute, pBuffer);
943     if (!phContext)
944         return SEC_E_INVALID_HANDLE;
945
946     switch(ulAttribute)
947     {
948 #define _x(x) case (x) : FIXME(#x" stub\n"); break
949         _x(SECPKG_ATTR_ACCESS_TOKEN);
950         _x(SECPKG_ATTR_AUTHORITY);
951         _x(SECPKG_ATTR_DCE_INFO);
952         _x(SECPKG_ATTR_FLAGS);
953         _x(SECPKG_ATTR_KEY_INFO);
954         _x(SECPKG_ATTR_LIFESPAN);
955         _x(SECPKG_ATTR_NAMES);
956         _x(SECPKG_ATTR_NATIVE_NAMES);
957         _x(SECPKG_ATTR_NEGOTIATION_INFO);
958         _x(SECPKG_ATTR_PACKAGE_INFO);
959         _x(SECPKG_ATTR_PASSWORD_EXPIRY);
960         _x(SECPKG_ATTR_SESSION_KEY);
961         case SECPKG_ATTR_SIZES:
962             {
963                 PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
964                 spcs->cbMaxToken = NTLM_MAX_BUF;
965                 spcs->cbMaxSignature = 16;
966                 spcs->cbBlockSize = 1;
967                 spcs->cbSecurityTrailer = 16;
968                 return SEC_E_OK;
969             }
970         _x(SECPKG_ATTR_STREAM_SIZES);
971         _x(SECPKG_ATTR_TARGET_INFORMATION);
972 #undef _x
973         default:
974             TRACE("Unknown value %ld passed for ulAttribute\n", ulAttribute);
975     }
976
977     return SEC_E_UNSUPPORTED_FUNCTION;
978 }
979
980 /***********************************************************************
981  *              QueryContextAttributesA
982  */
983 static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
984  unsigned long ulAttribute, void *pBuffer)
985 {
986     return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
987 }
988
989 /***********************************************************************
990  *              ImpersonateSecurityContext
991  */
992 static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
993 {
994     SECURITY_STATUS ret;
995
996     TRACE("%p\n", phContext);
997     if (phContext)
998     {
999         ret = SEC_E_UNSUPPORTED_FUNCTION;
1000     }
1001     else
1002     {
1003         ret = SEC_E_INVALID_HANDLE;
1004     }
1005     return ret;
1006 }
1007
1008 /***********************************************************************
1009  *              RevertSecurityContext
1010  */
1011 static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
1012 {
1013     SECURITY_STATUS ret;
1014
1015     TRACE("%p\n", phContext);
1016     if (phContext)
1017     {
1018         ret = SEC_E_UNSUPPORTED_FUNCTION;
1019     }
1020     else
1021     {
1022         ret = SEC_E_INVALID_HANDLE;
1023     }
1024     return ret;
1025 }
1026
1027 /***********************************************************************
1028  *              MakeSignature
1029  */
1030 static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1031  PSecBufferDesc pMessage, ULONG MessageSeqNo)
1032 {
1033     SECURITY_STATUS ret;
1034
1035     TRACE("%p %ld %p %ld\n", phContext, fQOP, pMessage, MessageSeqNo);
1036     if (phContext)
1037     {
1038         ret = SEC_E_UNSUPPORTED_FUNCTION;
1039     }
1040     else
1041     {
1042         ret = SEC_E_INVALID_HANDLE;
1043     }
1044     return ret;
1045 }
1046
1047 /***********************************************************************
1048  *              VerifySignature
1049  */
1050 static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
1051  PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1052 {
1053     SECURITY_STATUS ret;
1054
1055     TRACE("%p %p %ld %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1056     if (phContext)
1057     {
1058         ret = SEC_E_UNSUPPORTED_FUNCTION;
1059     }
1060     else
1061     {
1062         ret = SEC_E_INVALID_HANDLE;
1063     }
1064     return ret;
1065 }
1066
1067 /***********************************************************************
1068  *             FreeCredentialsHandle
1069  */
1070 static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(
1071         PCredHandle phCredential)
1072 {
1073     SECURITY_STATUS ret;
1074
1075     if(phCredential){
1076         PNegoHelper helper = (PNegoHelper) phCredential->dwLower;
1077         phCredential->dwUpper = 0;
1078         phCredential->dwLower = 0;
1079         cleanup_helper(helper);
1080         ret = SEC_E_OK;
1081     }
1082     else
1083         ret = SEC_E_OK;
1084     
1085     return ret;
1086 }
1087
1088 /***********************************************************************
1089  *             EncryptMessage
1090  */
1091 static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
1092         ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
1093 {
1094     TRACE("%p %ld %p %ld stub\n", phContext, fQOP, pMessage, MessageSeqNo);
1095
1096     if(!phContext)
1097         return SEC_E_INVALID_HANDLE;
1098
1099     return SEC_E_UNSUPPORTED_FUNCTION;
1100 }
1101
1102 /***********************************************************************
1103  *             DecryptMessage
1104  */
1105 static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
1106         PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1107 {
1108     TRACE("%p %p %ld %p stub\n", phContext, pMessage, MessageSeqNo, pfQOP);
1109
1110     if(!phContext)
1111         return SEC_E_INVALID_HANDLE;
1112
1113     return SEC_E_UNSUPPORTED_FUNCTION;
1114 }
1115
1116 static SecurityFunctionTableA ntlmTableA = {
1117     1,
1118     NULL,   /* EnumerateSecurityPackagesA */
1119     ntlm_QueryCredentialsAttributesA,   /* QueryCredentialsAttributesA */
1120     ntlm_AcquireCredentialsHandleA,     /* AcquireCredentialsHandleA */
1121     ntlm_FreeCredentialsHandle,         /* FreeCredentialsHandle */
1122     NULL,   /* Reserved2 */
1123     ntlm_InitializeSecurityContextA,    /* InitializeSecurityContextA */
1124     ntlm_AcceptSecurityContext,         /* AcceptSecurityContext */
1125     ntlm_CompleteAuthToken,             /* CompleteAuthToken */
1126     ntlm_DeleteSecurityContext,         /* DeleteSecurityContext */
1127     NULL,  /* ApplyControlToken */
1128     ntlm_QueryContextAttributesA,       /* QueryContextAttributesA */
1129     ntlm_ImpersonateSecurityContext,    /* ImpersonateSecurityContext */
1130     ntlm_RevertSecurityContext,         /* RevertSecurityContext */
1131     ntlm_MakeSignature,                 /* MakeSignature */
1132     ntlm_VerifySignature,               /* VerifySignature */
1133     FreeContextBuffer,                  /* FreeContextBuffer */
1134     NULL,   /* QuerySecurityPackageInfoA */
1135     NULL,   /* Reserved3 */
1136     NULL,   /* Reserved4 */
1137     NULL,   /* ExportSecurityContext */
1138     NULL,   /* ImportSecurityContextA */
1139     NULL,   /* AddCredentialsA */
1140     NULL,   /* Reserved8 */
1141     NULL,   /* QuerySecurityContextToken */
1142     ntlm_EncryptMessage,                /* EncryptMessage */
1143     ntlm_DecryptMessage,                /* DecryptMessage */
1144     NULL,   /* SetContextAttributesA */
1145 };
1146
1147 static SecurityFunctionTableW ntlmTableW = {
1148     1,
1149     NULL,   /* EnumerateSecurityPackagesW */
1150     ntlm_QueryCredentialsAttributesW,   /* QueryCredentialsAttributesW */
1151     ntlm_AcquireCredentialsHandleW,     /* AcquireCredentialsHandleW */
1152     ntlm_FreeCredentialsHandle,         /* FreeCredentialsHandle */
1153     NULL,   /* Reserved2 */
1154     ntlm_InitializeSecurityContextW,    /* InitializeSecurityContextW */
1155     ntlm_AcceptSecurityContext,         /* AcceptSecurityContext */
1156     ntlm_CompleteAuthToken,             /* CompleteAuthToken */
1157     ntlm_DeleteSecurityContext,         /* DeleteSecurityContext */
1158     NULL,  /* ApplyControlToken */
1159     ntlm_QueryContextAttributesW,       /* QueryContextAttributesW */
1160     ntlm_ImpersonateSecurityContext,    /* ImpersonateSecurityContext */
1161     ntlm_RevertSecurityContext,         /* RevertSecurityContext */
1162     ntlm_MakeSignature,                 /* MakeSignature */
1163     ntlm_VerifySignature,               /* VerifySignature */
1164     FreeContextBuffer,                  /* FreeContextBuffer */
1165     NULL,   /* QuerySecurityPackageInfoW */
1166     NULL,   /* Reserved3 */
1167     NULL,   /* Reserved4 */
1168     NULL,   /* ExportSecurityContext */
1169     NULL,   /* ImportSecurityContextW */
1170     NULL,   /* AddCredentialsW */
1171     NULL,   /* Reserved8 */
1172     NULL,   /* QuerySecurityContextToken */
1173     ntlm_EncryptMessage,                /* EncryptMessage */
1174     ntlm_DecryptMessage,                /* DecryptMessage */
1175     NULL,   /* SetContextAttributesW */
1176 };
1177
1178 #define NTLM_COMMENT \
1179    { 'N', 'T', 'L', 'M', ' ', \
1180      'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
1181      'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
1182
1183 static CHAR ntlm_comment_A[] = NTLM_COMMENT;
1184 static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
1185
1186 #define NTLM_NAME {'N', 'T', 'L', 'M', 0}
1187
1188 static char ntlm_name_A[] = NTLM_NAME;
1189 static WCHAR ntlm_name_W[] = NTLM_NAME;
1190
1191 /* According to Windows, NTLM has the following capabilities.  */
1192 #define CAPS ( \
1193         SECPKG_FLAG_INTEGRITY | \
1194         SECPKG_FLAG_PRIVACY | \
1195         SECPKG_FLAG_TOKEN_ONLY | \
1196         SECPKG_FLAG_CONNECTION | \
1197         SECPKG_FLAG_MULTI_REQUIRED | \
1198         SECPKG_FLAG_IMPERSONATION | \
1199         SECPKG_FLAG_ACCEPT_WIN32_NAME | \
1200         SECPKG_FLAG_READONLY_WITH_CHECKSUM)
1201
1202 static const SecPkgInfoW infoW = {
1203     CAPS,
1204     1,
1205     RPC_C_AUTHN_WINNT,
1206     NTLM_MAX_BUF,
1207     ntlm_name_W,
1208     ntlm_comment_W
1209 };
1210
1211 static const SecPkgInfoA infoA = {
1212     CAPS,
1213     1,
1214     RPC_C_AUTHN_WINNT,
1215     NTLM_MAX_BUF,
1216     ntlm_name_A,
1217     ntlm_comment_A
1218 };
1219
1220 void SECUR32_initNTLMSP(void)
1221 {
1222     SECURITY_STATUS ret;
1223     PNegoHelper helper;
1224     static CHAR ntlm_auth[] = "ntlm_auth",
1225                 version[]   = "--version";
1226
1227     SEC_CHAR *args[] = {
1228         ntlm_auth,
1229         version,
1230         NULL };
1231
1232     if((ret = fork_helper(&helper, "ntlm_auth", args)) != SEC_E_OK)
1233     {
1234         /* Cheat and allocate a helper anyway, so cleanup later will work. */
1235         helper = HeapAlloc(GetProcessHeap(),0, sizeof(PNegoHelper));
1236         helper->version = -1;
1237     }
1238     else
1239         check_version(helper);
1240
1241     if(helper->version > 2)
1242     {
1243         SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
1244         SECUR32_addPackages(provider, 1L, &infoA, &infoW);
1245     }
1246     cleanup_helper(helper);
1247 }