push 4e98c31ec75caed2ea3040ac2e710b58ba1ca0e1
[wine/hacks.git] / dlls / secur32 / secur32.c
blob27e0263c6cf6470c64c49d41e00f6db638132fe6
1 /* Copyright (C) 2004 Juan Lang
3 * This file implements loading of SSP DLLs.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <assert.h>
20 #include <stdarg.h>
22 #include "ntstatus.h"
23 #define WIN32_NO_STATUS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "winreg.h"
28 #include "winternl.h"
29 #include "shlwapi.h"
30 #include "sspi.h"
31 #include "secur32_priv.h"
32 #include "secext.h"
33 #include "ntsecapi.h"
34 #include "thunks.h"
36 #include "wine/list.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
41 /**
42 * Type definitions
45 typedef struct _SecurePackageTable
47 DWORD numPackages;
48 DWORD numAllocated;
49 struct list table;
50 } SecurePackageTable;
52 typedef struct _SecureProviderTable
54 DWORD numProviders;
55 DWORD numAllocated;
56 struct list table;
57 } SecureProviderTable;
59 /**
60 * Prototypes
63 /* Tries to load moduleName as a provider. If successful, enumerates what
64 * packages it can and adds them to the package and provider tables. Resizes
65 * tables as necessary.
67 static void _tryLoadProvider(PWSTR moduleName);
69 /* Initialization: read securityproviders value and attempt to open each dll
70 * there. For each DLL, call _tryLoadProvider to see if it's really an SSP.
71 * Two undocumented functions, AddSecurityPackage(A/W) and
72 * DeleteSecurityPackage(A/W), seem suspiciously like they'd register or
73 * unregister a dll, but I'm not sure.
75 static void SECUR32_initializeProviders(void);
77 /* Frees all loaded packages and providers */
78 static void SECUR32_freeProviders(void);
80 /**
81 * Globals
84 static CRITICAL_SECTION cs;
85 static SecurePackageTable *packageTable = NULL;
86 static SecureProviderTable *providerTable = NULL;
88 static SecurityFunctionTableA securityFunctionTableA = {
89 SECURITY_SUPPORT_PROVIDER_INTERFACE_VERSION,
90 EnumerateSecurityPackagesA,
91 QueryCredentialsAttributesA,
92 AcquireCredentialsHandleA,
93 FreeCredentialsHandle,
94 NULL, /* Reserved2 */
95 InitializeSecurityContextA,
96 AcceptSecurityContext,
97 CompleteAuthToken,
98 DeleteSecurityContext,
99 ApplyControlToken,
100 QueryContextAttributesA,
101 ImpersonateSecurityContext,
102 RevertSecurityContext,
103 MakeSignature,
104 VerifySignature,
105 FreeContextBuffer,
106 QuerySecurityPackageInfoA,
107 EncryptMessage, /* Reserved3 */
108 DecryptMessage, /* Reserved4 */
109 ExportSecurityContext,
110 ImportSecurityContextA,
111 AddCredentialsA,
112 NULL, /* Reserved8 */
113 QuerySecurityContextToken,
114 EncryptMessage,
115 DecryptMessage,
116 SetContextAttributesA
119 static SecurityFunctionTableW securityFunctionTableW = {
120 SECURITY_SUPPORT_PROVIDER_INTERFACE_VERSION,
121 EnumerateSecurityPackagesW,
122 QueryCredentialsAttributesW,
123 AcquireCredentialsHandleW,
124 FreeCredentialsHandle,
125 NULL, /* Reserved2 */
126 InitializeSecurityContextW,
127 AcceptSecurityContext,
128 CompleteAuthToken,
129 DeleteSecurityContext,
130 ApplyControlToken,
131 QueryContextAttributesW,
132 ImpersonateSecurityContext,
133 RevertSecurityContext,
134 MakeSignature,
135 VerifySignature,
136 FreeContextBuffer,
137 QuerySecurityPackageInfoW,
138 EncryptMessage, /* Reserved3 */
139 DecryptMessage, /* Reserved4 */
140 ExportSecurityContext,
141 ImportSecurityContextW,
142 AddCredentialsW,
143 NULL, /* Reserved8 */
144 QuerySecurityContextToken,
145 EncryptMessage,
146 DecryptMessage,
147 SetContextAttributesW
150 /***********************************************************************
151 * InitSecurityInterfaceA (SECUR32.@)
153 PSecurityFunctionTableA WINAPI InitSecurityInterfaceA(void)
155 return &securityFunctionTableA;
158 /***********************************************************************
159 * InitSecurityInterfaceW (SECUR32.@)
161 PSecurityFunctionTableW WINAPI InitSecurityInterfaceW(void)
163 return &securityFunctionTableW;
166 PWSTR SECUR32_strdupW(PCWSTR str)
168 PWSTR ret;
170 if (str)
172 ret = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(str) + 1) * sizeof(WCHAR));
173 if (ret)
174 lstrcpyW(ret, str);
176 else
177 ret = NULL;
178 return ret;
181 PWSTR SECUR32_AllocWideFromMultiByte(PCSTR str)
183 PWSTR ret;
185 if (str)
187 int charsNeeded = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
189 if (charsNeeded)
191 ret = HeapAlloc(GetProcessHeap(), 0, charsNeeded * sizeof(WCHAR));
192 if (ret)
193 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, charsNeeded);
195 else
196 ret = NULL;
198 else
199 ret = NULL;
200 return ret;
203 PSTR SECUR32_AllocMultiByteFromWide(PCWSTR str)
205 PSTR ret;
207 if (str)
209 int charsNeeded = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0,
210 NULL, NULL);
212 if (charsNeeded)
214 ret = HeapAlloc(GetProcessHeap(), 0, charsNeeded);
215 if (ret)
216 WideCharToMultiByte(CP_ACP, 0, str, -1, ret, charsNeeded,
217 NULL, NULL);
219 else
220 ret = NULL;
222 else
223 ret = NULL;
224 return ret;
227 static void _makeFnTableA(PSecurityFunctionTableA fnTableA,
228 const SecurityFunctionTableA *inFnTableA,
229 const SecurityFunctionTableW *inFnTableW)
231 if (fnTableA)
233 if (inFnTableA)
235 /* The size of the version 1 table is based on platform sdk's
236 * sspi.h, though the sample ssp also provided with platform sdk
237 * implies only functions through QuerySecurityPackageInfoA are
238 * implemented (yikes)
240 size_t tableSize = inFnTableA->dwVersion == 1 ?
241 (const BYTE *)&inFnTableA->SetContextAttributesA -
242 (const BYTE *)inFnTableA : sizeof(SecurityFunctionTableA);
244 memcpy(fnTableA, inFnTableA, tableSize);
245 /* override this, since we can do it internally anyway */
246 fnTableA->QuerySecurityPackageInfoA =
247 QuerySecurityPackageInfoA;
249 else if (inFnTableW)
251 /* functions with thunks */
252 if (inFnTableW->AcquireCredentialsHandleW)
253 fnTableA->AcquireCredentialsHandleA =
254 thunk_AcquireCredentialsHandleA;
255 if (inFnTableW->InitializeSecurityContextW)
256 fnTableA->InitializeSecurityContextA =
257 thunk_InitializeSecurityContextA;
258 if (inFnTableW->ImportSecurityContextW)
259 fnTableA->ImportSecurityContextA =
260 thunk_ImportSecurityContextA;
261 if (inFnTableW->AddCredentialsW)
262 fnTableA->AddCredentialsA =
263 thunk_AddCredentialsA;
264 if (inFnTableW->QueryCredentialsAttributesW)
265 fnTableA->QueryCredentialsAttributesA =
266 thunk_QueryCredentialsAttributesA;
267 if (inFnTableW->QueryContextAttributesW)
268 fnTableA->QueryContextAttributesA =
269 thunk_QueryContextAttributesA;
270 if (inFnTableW->SetContextAttributesW)
271 fnTableA->SetContextAttributesA =
272 thunk_SetContextAttributesA;
273 /* this can't be thunked, there's no extra param to know which
274 * package to forward to */
275 fnTableA->EnumerateSecurityPackagesA = NULL;
276 /* functions with no thunks needed */
277 fnTableA->AcceptSecurityContext = inFnTableW->AcceptSecurityContext;
278 fnTableA->CompleteAuthToken = inFnTableW->CompleteAuthToken;
279 fnTableA->DeleteSecurityContext = inFnTableW->DeleteSecurityContext;
280 fnTableA->ImpersonateSecurityContext =
281 inFnTableW->ImpersonateSecurityContext;
282 fnTableA->RevertSecurityContext = inFnTableW->RevertSecurityContext;
283 fnTableA->MakeSignature = inFnTableW->MakeSignature;
284 fnTableA->VerifySignature = inFnTableW->VerifySignature;
285 fnTableA->FreeContextBuffer = inFnTableW->FreeContextBuffer;
286 fnTableA->QuerySecurityPackageInfoA =
287 QuerySecurityPackageInfoA;
288 fnTableA->ExportSecurityContext =
289 inFnTableW->ExportSecurityContext;
290 fnTableA->QuerySecurityContextToken =
291 inFnTableW->QuerySecurityContextToken;
292 fnTableA->EncryptMessage = inFnTableW->EncryptMessage;
293 fnTableA->DecryptMessage = inFnTableW->DecryptMessage;
298 static void _makeFnTableW(PSecurityFunctionTableW fnTableW,
299 const SecurityFunctionTableA *inFnTableA,
300 const SecurityFunctionTableW *inFnTableW)
302 if (fnTableW)
304 if (inFnTableW)
306 /* The size of the version 1 table is based on platform sdk's
307 * sspi.h, though the sample ssp also provided with platform sdk
308 * implies only functions through QuerySecurityPackageInfoA are
309 * implemented (yikes)
311 size_t tableSize = inFnTableW->dwVersion == 1 ?
312 (const BYTE *)&inFnTableW->SetContextAttributesW -
313 (const BYTE *)inFnTableW : sizeof(SecurityFunctionTableW);
315 memcpy(fnTableW, inFnTableW, tableSize);
316 /* override this, since we can do it internally anyway */
317 fnTableW->QuerySecurityPackageInfoW =
318 QuerySecurityPackageInfoW;
320 else if (inFnTableA)
322 /* functions with thunks */
323 if (inFnTableA->AcquireCredentialsHandleA)
324 fnTableW->AcquireCredentialsHandleW =
325 thunk_AcquireCredentialsHandleW;
326 if (inFnTableA->InitializeSecurityContextA)
327 fnTableW->InitializeSecurityContextW =
328 thunk_InitializeSecurityContextW;
329 if (inFnTableA->ImportSecurityContextA)
330 fnTableW->ImportSecurityContextW =
331 thunk_ImportSecurityContextW;
332 if (inFnTableA->AddCredentialsA)
333 fnTableW->AddCredentialsW =
334 thunk_AddCredentialsW;
335 if (inFnTableA->QueryCredentialsAttributesA)
336 fnTableW->QueryCredentialsAttributesW =
337 thunk_QueryCredentialsAttributesW;
338 if (inFnTableA->QueryContextAttributesA)
339 fnTableW->QueryContextAttributesW =
340 thunk_QueryContextAttributesW;
341 if (inFnTableA->SetContextAttributesA)
342 fnTableW->SetContextAttributesW =
343 thunk_SetContextAttributesW;
344 /* this can't be thunked, there's no extra param to know which
345 * package to forward to */
346 fnTableW->EnumerateSecurityPackagesW = NULL;
347 /* functions with no thunks needed */
348 fnTableW->AcceptSecurityContext = inFnTableA->AcceptSecurityContext;
349 fnTableW->CompleteAuthToken = inFnTableA->CompleteAuthToken;
350 fnTableW->DeleteSecurityContext = inFnTableA->DeleteSecurityContext;
351 fnTableW->ImpersonateSecurityContext =
352 inFnTableA->ImpersonateSecurityContext;
353 fnTableW->RevertSecurityContext = inFnTableA->RevertSecurityContext;
354 fnTableW->MakeSignature = inFnTableA->MakeSignature;
355 fnTableW->VerifySignature = inFnTableA->VerifySignature;
356 fnTableW->FreeContextBuffer = inFnTableA->FreeContextBuffer;
357 fnTableW->QuerySecurityPackageInfoW =
358 QuerySecurityPackageInfoW;
359 fnTableW->ExportSecurityContext =
360 inFnTableA->ExportSecurityContext;
361 fnTableW->QuerySecurityContextToken =
362 inFnTableA->QuerySecurityContextToken;
363 fnTableW->EncryptMessage = inFnTableA->EncryptMessage;
364 fnTableW->DecryptMessage = inFnTableA->DecryptMessage;
369 static void _copyPackageInfo(PSecPkgInfoW info, const SecPkgInfoA *inInfoA,
370 const SecPkgInfoW *inInfoW)
372 if (info && (inInfoA || inInfoW))
374 /* odd, I know, but up until Name and Comment the structures are
375 * identical
377 memcpy(info, inInfoW ? inInfoW : (const SecPkgInfoW *)inInfoA, sizeof(*info));
378 if (inInfoW)
380 info->Name = SECUR32_strdupW(inInfoW->Name);
381 info->Comment = SECUR32_strdupW(inInfoW->Comment);
383 else
385 info->Name = SECUR32_AllocWideFromMultiByte(inInfoA->Name);
386 info->Comment = SECUR32_AllocWideFromMultiByte(inInfoA->Comment);
391 SecureProvider *SECUR32_addProvider(const SecurityFunctionTableA *fnTableA,
392 const SecurityFunctionTableW *fnTableW, PCWSTR moduleName)
394 SecureProvider *ret;
396 EnterCriticalSection(&cs);
398 if (!providerTable)
400 providerTable = HeapAlloc(GetProcessHeap(), 0, sizeof(SecureProviderTable));
401 if (!providerTable)
403 LeaveCriticalSection(&cs);
404 return NULL;
407 list_init(&providerTable->table);
410 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(SecureProvider));
411 if (!ret)
413 LeaveCriticalSection(&cs);
414 return NULL;
417 list_add_tail(&providerTable->table, &ret->entry);
418 ret->lib = NULL;
420 if (fnTableA || fnTableW)
422 ret->moduleName = moduleName ? SECUR32_strdupW(moduleName) : NULL;
423 _makeFnTableA(&ret->fnTableA, fnTableA, fnTableW);
424 _makeFnTableW(&ret->fnTableW, fnTableA, fnTableW);
425 ret->loaded = moduleName ? FALSE : TRUE;
427 else
429 ret->moduleName = SECUR32_strdupW(moduleName);
430 ret->loaded = FALSE;
433 LeaveCriticalSection(&cs);
434 return ret;
437 void SECUR32_addPackages(SecureProvider *provider, ULONG toAdd,
438 const SecPkgInfoA *infoA, const SecPkgInfoW *infoW)
440 ULONG i;
442 assert(provider);
443 assert(infoA || infoW);
445 EnterCriticalSection(&cs);
447 if (!packageTable)
449 packageTable = HeapAlloc(GetProcessHeap(), 0, sizeof(SecurePackageTable));
450 if (!packageTable)
452 LeaveCriticalSection(&cs);
453 return;
456 packageTable->numPackages = 0;
457 list_init(&packageTable->table);
460 for (i = 0; i < toAdd; i++)
462 SecurePackage *package = HeapAlloc(GetProcessHeap(), 0, sizeof(SecurePackage));
463 if (!package)
464 continue;
466 list_add_tail(&packageTable->table, &package->entry);
468 package->provider = provider;
469 _copyPackageInfo(&package->infoW,
470 infoA ? &infoA[i] : NULL,
471 infoW ? &infoW[i] : NULL);
473 packageTable->numPackages += toAdd;
475 LeaveCriticalSection(&cs);
478 static void _tryLoadProvider(PWSTR moduleName)
480 HMODULE lib = LoadLibraryW(moduleName);
482 if (lib)
484 INIT_SECURITY_INTERFACE_W pInitSecurityInterfaceW =
485 (INIT_SECURITY_INTERFACE_W)GetProcAddress(lib,
486 SECURITY_ENTRYPOINT_ANSIW);
487 INIT_SECURITY_INTERFACE_A pInitSecurityInterfaceA =
488 (INIT_SECURITY_INTERFACE_A)GetProcAddress(lib,
489 SECURITY_ENTRYPOINT_ANSIA);
491 TRACE("loaded %s, InitSecurityInterfaceA is %p, InitSecurityInterfaceW is %p\n",
492 debugstr_w(moduleName), pInitSecurityInterfaceA,
493 pInitSecurityInterfaceW);
494 if (pInitSecurityInterfaceW || pInitSecurityInterfaceA)
496 PSecurityFunctionTableA fnTableA = NULL;
497 PSecurityFunctionTableW fnTableW = NULL;
498 ULONG toAdd = 0;
499 PSecPkgInfoA infoA = NULL;
500 PSecPkgInfoW infoW = NULL;
501 SECURITY_STATUS ret = SEC_E_OK;
503 if (pInitSecurityInterfaceA)
504 fnTableA = pInitSecurityInterfaceA();
505 if (pInitSecurityInterfaceW)
506 fnTableW = pInitSecurityInterfaceW();
507 if (fnTableW && fnTableW->EnumerateSecurityPackagesW)
509 if (fnTableW != &securityFunctionTableW)
510 ret = fnTableW->EnumerateSecurityPackagesW(&toAdd, &infoW);
511 else
512 TRACE("%s has built-in providers, skip adding\n", debugstr_w(moduleName));
514 else if (fnTableA && fnTableA->EnumerateSecurityPackagesA)
516 if (fnTableA != &securityFunctionTableA)
517 ret = fnTableA->EnumerateSecurityPackagesA(&toAdd, &infoA);
518 else
519 TRACE("%s has built-in providers, skip adding\n", debugstr_w(moduleName));
521 if (ret == SEC_E_OK && toAdd > 0 && (infoW || infoA))
523 SecureProvider *provider = SECUR32_addProvider(NULL, NULL,
524 moduleName);
526 if (provider)
527 SECUR32_addPackages(provider, toAdd, infoA, infoW);
528 if (infoW)
529 fnTableW->FreeContextBuffer(infoW);
530 else
531 fnTableA->FreeContextBuffer(infoA);
534 FreeLibrary(lib);
536 else
537 WARN("failed to load %s\n", debugstr_w(moduleName));
540 static const WCHAR securityProvidersKeyW[] = {
541 'S','Y','S','T','E','M','\\','C','u','r','r','e','n','t','C','o','n','t','r',
542 'o','l','S','e','t','\\','C','o','n','t','r','o','l','\\','S','e','c','u','r',
543 'i','t','y','P','r','o','v','i','d','e','r','s','\0'
545 static const WCHAR securityProvidersW[] = {
546 'S','e','c','u','r','i','t','y','P','r','o','v','i','d','e','r','s',0
549 static void SECUR32_initializeProviders(void)
551 HKEY key;
552 long apiRet;
554 TRACE("\n");
555 InitializeCriticalSection(&cs);
556 cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": cs");
557 /* First load built-in providers */
558 SECUR32_initSchannelSP();
559 /* Do not load Negotiate yet. This breaks for some user on the wine-users
560 * mailing list as of 2006-09-12. Without Negotiate, applications should
561 * fall back to NTLM and that should work.*/
562 #if 0
563 SECUR32_initNegotiateSP();
564 #endif
565 SECUR32_initNTLMSP();
566 /* Now load providers from registry */
567 apiRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, securityProvidersKeyW, 0,
568 KEY_READ, &key);
569 if (apiRet == ERROR_SUCCESS)
571 WCHAR securityPkgNames[MAX_PATH]; /* arbitrary len */
572 DWORD size = sizeof(securityPkgNames) / sizeof(WCHAR), type;
574 apiRet = RegQueryValueExW(key, securityProvidersW, NULL, &type,
575 (PBYTE)securityPkgNames, &size);
576 if (apiRet == ERROR_SUCCESS && type == REG_SZ)
578 WCHAR *ptr;
580 size = size / sizeof(WCHAR);
581 for (ptr = securityPkgNames;
582 ptr < securityPkgNames + size; )
584 WCHAR *comma;
586 for (comma = ptr; *comma && *comma != ','; comma++)
588 if (*comma == ',')
589 *comma = '\0';
590 for (; *ptr && isspace(*ptr) && ptr < securityPkgNames + size;
591 ptr++)
593 if (*ptr)
594 _tryLoadProvider(ptr);
595 ptr += lstrlenW(ptr) + 1;
598 RegCloseKey(key);
602 SecurePackage *SECUR32_findPackageW(PCWSTR packageName)
604 SecurePackage *ret = NULL;
605 BOOL matched = FALSE;
607 if (packageTable && packageName)
609 LIST_FOR_EACH_ENTRY(ret, &packageTable->table, SecurePackage, entry)
611 matched = !lstrcmpiW(ret->infoW.Name, packageName);
612 if (matched)
613 break;
616 if (!matched)
617 return NULL;
619 if (ret->provider && !ret->provider->loaded)
621 ret->provider->lib = LoadLibraryW(ret->provider->moduleName);
622 if (ret->provider->lib)
624 INIT_SECURITY_INTERFACE_W pInitSecurityInterfaceW =
625 (INIT_SECURITY_INTERFACE_W)GetProcAddress(ret->provider->lib,
626 SECURITY_ENTRYPOINT_ANSIW);
627 INIT_SECURITY_INTERFACE_A pInitSecurityInterfaceA =
628 (INIT_SECURITY_INTERFACE_A)GetProcAddress(ret->provider->lib,
629 SECURITY_ENTRYPOINT_ANSIA);
630 PSecurityFunctionTableA fnTableA = NULL;
631 PSecurityFunctionTableW fnTableW = NULL;
633 if (pInitSecurityInterfaceA)
634 fnTableA = pInitSecurityInterfaceA();
635 if (pInitSecurityInterfaceW)
636 fnTableW = pInitSecurityInterfaceW();
637 /* don't update built-in SecurityFunctionTable */
638 if (fnTableA != &securityFunctionTableA)
639 _makeFnTableA(&ret->provider->fnTableA, fnTableA, fnTableW);
640 if (fnTableW != &securityFunctionTableW)
641 _makeFnTableW(&ret->provider->fnTableW, fnTableA, fnTableW);
642 ret->provider->loaded = TRUE;
644 else
645 ret = NULL;
648 return ret;
651 SecurePackage *SECUR32_findPackageA(PCSTR packageName)
653 SecurePackage *ret;
655 if (packageTable && packageName)
657 UNICODE_STRING package;
659 RtlCreateUnicodeStringFromAsciiz(&package, packageName);
660 ret = SECUR32_findPackageW(package.Buffer);
661 RtlFreeUnicodeString(&package);
663 else
664 ret = NULL;
665 return ret;
668 static void SECUR32_freeProviders(void)
670 SecurePackage *package;
671 SecureProvider *provider;
673 TRACE("\n");
674 EnterCriticalSection(&cs);
676 SECUR32_deinitSchannelSP();
678 if (packageTable)
680 LIST_FOR_EACH_ENTRY(package, &packageTable->table, SecurePackage, entry)
682 HeapFree(GetProcessHeap(), 0, package->infoW.Name);
683 HeapFree(GetProcessHeap(), 0, package->infoW.Comment);
686 HeapFree(GetProcessHeap(), 0, packageTable);
687 packageTable = NULL;
690 if (providerTable)
692 LIST_FOR_EACH_ENTRY(provider, &providerTable->table, SecureProvider, entry)
694 HeapFree(GetProcessHeap(), 0, provider->moduleName);
695 if (provider->lib)
696 FreeLibrary(provider->lib);
699 HeapFree(GetProcessHeap(), 0, providerTable);
700 providerTable = NULL;
703 LeaveCriticalSection(&cs);
704 cs.DebugInfo->Spare[0] = 0;
705 DeleteCriticalSection(&cs);
708 /***********************************************************************
709 * FreeContextBuffer (SECUR32.@)
711 * Doh--if pv was allocated by a crypto package, this may not be correct.
712 * The sample ssp seems to use LocalAlloc/LocalFee, but there doesn't seem to
713 * be any guarantee, nor is there an alloc function in secur32.
715 SECURITY_STATUS WINAPI FreeContextBuffer(PVOID pv)
717 HeapFree(GetProcessHeap(), 0, pv);
719 return SEC_E_OK;
722 /***********************************************************************
723 * EnumerateSecurityPackagesW (SECUR32.@)
725 SECURITY_STATUS WINAPI EnumerateSecurityPackagesW(PULONG pcPackages,
726 PSecPkgInfoW *ppPackageInfo)
728 SECURITY_STATUS ret = SEC_E_OK;
730 TRACE("(%p, %p)\n", pcPackages, ppPackageInfo);
732 /* windows just crashes if pcPackages or ppPackageInfo is NULL, so will I */
733 *pcPackages = 0;
734 EnterCriticalSection(&cs);
735 if (packageTable)
737 SecurePackage *package;
738 size_t bytesNeeded;
740 bytesNeeded = packageTable->numPackages * sizeof(SecPkgInfoW);
741 LIST_FOR_EACH_ENTRY(package, &packageTable->table, SecurePackage, entry)
743 if (package->infoW.Name)
744 bytesNeeded += (lstrlenW(package->infoW.Name) + 1) * sizeof(WCHAR);
745 if (package->infoW.Comment)
746 bytesNeeded += (lstrlenW(package->infoW.Comment) + 1) * sizeof(WCHAR);
748 if (bytesNeeded)
750 *ppPackageInfo = HeapAlloc(GetProcessHeap(), 0, bytesNeeded);
751 if (*ppPackageInfo)
753 ULONG i = 0;
754 PWSTR nextString;
756 *pcPackages = packageTable->numPackages;
757 nextString = (PWSTR)((PBYTE)*ppPackageInfo +
758 packageTable->numPackages * sizeof(SecPkgInfoW));
759 LIST_FOR_EACH_ENTRY(package, &packageTable->table, SecurePackage, entry)
761 PSecPkgInfoW pkgInfo = *ppPackageInfo + i++;
763 *pkgInfo = package->infoW;
764 if (package->infoW.Name)
766 TRACE("Name[%d] = %s\n", i - 1, debugstr_w(package->infoW.Name));
767 pkgInfo->Name = nextString;
768 lstrcpyW(nextString, package->infoW.Name);
769 nextString += lstrlenW(nextString) + 1;
771 else
772 pkgInfo->Name = NULL;
773 if (package->infoW.Comment)
775 TRACE("Comment[%d] = %s\n", i - 1, debugstr_w(package->infoW.Comment));
776 pkgInfo->Comment = nextString;
777 lstrcpyW(nextString, package->infoW.Comment);
778 nextString += lstrlenW(nextString) + 1;
780 else
781 pkgInfo->Comment = NULL;
784 else
785 ret = SEC_E_INSUFFICIENT_MEMORY;
788 LeaveCriticalSection(&cs);
789 TRACE("<-- 0x%08x\n", ret);
790 return ret;
793 /* Converts info (which is assumed to be an array of cPackages SecPkgInfoW
794 * structures) into an array of SecPkgInfoA structures, which it returns.
796 static PSecPkgInfoA thunk_PSecPkgInfoWToA(ULONG cPackages,
797 const SecPkgInfoW *info)
799 PSecPkgInfoA ret;
801 if (info)
803 size_t bytesNeeded = cPackages * sizeof(SecPkgInfoA);
804 ULONG i;
806 for (i = 0; i < cPackages; i++)
808 if (info[i].Name)
809 bytesNeeded += WideCharToMultiByte(CP_ACP, 0, info[i].Name,
810 -1, NULL, 0, NULL, NULL);
811 if (info[i].Comment)
812 bytesNeeded += WideCharToMultiByte(CP_ACP, 0, info[i].Comment,
813 -1, NULL, 0, NULL, NULL);
815 ret = HeapAlloc(GetProcessHeap(), 0, bytesNeeded);
816 if (ret)
818 PSTR nextString;
820 nextString = (PSTR)((PBYTE)ret + cPackages * sizeof(SecPkgInfoA));
821 for (i = 0; i < cPackages; i++)
823 PSecPkgInfoA pkgInfo = ret + i;
824 int bytes;
826 memcpy(pkgInfo, &info[i], sizeof(SecPkgInfoA));
827 if (info[i].Name)
829 pkgInfo->Name = nextString;
830 /* just repeat back to WideCharToMultiByte how many bytes
831 * it requires, since we asked it earlier
833 bytes = WideCharToMultiByte(CP_ACP, 0, info[i].Name, -1,
834 NULL, 0, NULL, NULL);
835 WideCharToMultiByte(CP_ACP, 0, info[i].Name, -1,
836 pkgInfo->Name, bytes, NULL, NULL);
837 nextString += lstrlenA(nextString) + 1;
839 else
840 pkgInfo->Name = NULL;
841 if (info[i].Comment)
843 pkgInfo->Comment = nextString;
844 /* just repeat back to WideCharToMultiByte how many bytes
845 * it requires, since we asked it earlier
847 bytes = WideCharToMultiByte(CP_ACP, 0, info[i].Comment, -1,
848 NULL, 0, NULL, NULL);
849 WideCharToMultiByte(CP_ACP, 0, info[i].Comment, -1,
850 pkgInfo->Comment, bytes, NULL, NULL);
851 nextString += lstrlenA(nextString) + 1;
853 else
854 pkgInfo->Comment = NULL;
858 else
859 ret = NULL;
860 return ret;
863 /***********************************************************************
864 * EnumerateSecurityPackagesA (SECUR32.@)
866 SECURITY_STATUS WINAPI EnumerateSecurityPackagesA(PULONG pcPackages,
867 PSecPkgInfoA *ppPackageInfo)
869 SECURITY_STATUS ret;
870 PSecPkgInfoW info;
872 ret = EnumerateSecurityPackagesW(pcPackages, &info);
873 if (ret == SEC_E_OK && *pcPackages && info)
875 *ppPackageInfo = thunk_PSecPkgInfoWToA(*pcPackages, info);
876 if (*pcPackages && !*ppPackageInfo)
878 *pcPackages = 0;
879 ret = SEC_E_INSUFFICIENT_MEMORY;
881 FreeContextBuffer(info);
883 return ret;
886 /***********************************************************************
887 * GetComputerObjectNameA (SECUR32.@)
889 * Get the local computer's name using the format specified.
891 * PARAMS
892 * NameFormat [I] The format for the name.
893 * lpNameBuffer [O] Pointer to buffer to receive the name.
894 * nSize [I/O] Size in characters of buffer.
896 * RETURNS
897 * TRUE If the name was written to lpNameBuffer.
898 * FALSE If the name couldn't be written.
900 * NOTES
901 * If lpNameBuffer is NULL, then the size of the buffer needed to hold the
902 * name will be returned in *nSize.
904 * nSize returns the number of characters written when lpNameBuffer is not
905 * NULL or the size of the buffer needed to hold the name when the buffer
906 * is too short or lpNameBuffer is NULL.
908 * It the buffer is too short, ERROR_INSUFFICIENT_BUFFER error will be set.
910 BOOLEAN WINAPI GetComputerObjectNameA(
911 EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize)
913 BOOLEAN rc;
914 LPWSTR bufferW = NULL;
915 ULONG sizeW = *nSize;
916 TRACE("(%d %p %p)\n", NameFormat, lpNameBuffer, nSize);
917 if (lpNameBuffer) {
918 bufferW = HeapAlloc(GetProcessHeap(), 0, sizeW * sizeof(WCHAR));
919 if (bufferW == NULL) {
920 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
921 return FALSE;
924 rc = GetComputerObjectNameW(NameFormat, bufferW, &sizeW);
925 if (rc && bufferW) {
926 ULONG len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
927 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, lpNameBuffer, *nSize, NULL, NULL);
928 *nSize = len;
930 else
931 *nSize = sizeW;
932 HeapFree(GetProcessHeap(), 0, bufferW);
933 return rc;
936 /***********************************************************************
937 * GetComputerObjectNameW (SECUR32.@)
939 BOOLEAN WINAPI GetComputerObjectNameW(
940 EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG nSize)
942 LSA_HANDLE policyHandle;
943 LSA_OBJECT_ATTRIBUTES objectAttributes;
944 PPOLICY_DNS_DOMAIN_INFO domainInfo;
945 NTSTATUS ntStatus;
946 BOOLEAN status;
947 TRACE("(%d %p %p)\n", NameFormat, lpNameBuffer, nSize);
949 if (NameFormat == NameUnknown)
951 SetLastError(ERROR_INVALID_PARAMETER);
952 return FALSE;
955 ZeroMemory(&objectAttributes, sizeof(objectAttributes));
956 objectAttributes.Length = sizeof(objectAttributes);
958 ntStatus = LsaOpenPolicy(NULL, &objectAttributes,
959 POLICY_VIEW_LOCAL_INFORMATION,
960 &policyHandle);
961 if (ntStatus != STATUS_SUCCESS)
963 SetLastError(LsaNtStatusToWinError(ntStatus));
964 WARN("LsaOpenPolicy failed with NT status %u\n", GetLastError());
965 return FALSE;
968 ntStatus = LsaQueryInformationPolicy(policyHandle,
969 PolicyDnsDomainInformation,
970 (PVOID *)&domainInfo);
971 if (ntStatus != STATUS_SUCCESS)
973 SetLastError(LsaNtStatusToWinError(ntStatus));
974 WARN("LsaQueryInformationPolicy failed with NT status %u\n",
975 GetLastError());
976 LsaClose(policyHandle);
977 return FALSE;
980 if (domainInfo->Sid)
982 switch (NameFormat)
984 case NameSamCompatible:
986 WCHAR name[MAX_COMPUTERNAME_LENGTH + 1];
987 DWORD size = sizeof(name)/sizeof(name[0]);
988 if (GetComputerNameW(name, &size))
990 int len = domainInfo->Name.Length + size + 3;
991 if (lpNameBuffer)
993 if (*nSize < len)
995 *nSize = len;
996 SetLastError(ERROR_INSUFFICIENT_BUFFER);
997 status = FALSE;
999 else
1001 WCHAR bs[] = { '\\', 0 };
1002 WCHAR ds[] = { '$', 0 };
1003 lstrcpyW(lpNameBuffer, domainInfo->Name.Buffer);
1004 lstrcatW(lpNameBuffer, bs);
1005 lstrcatW(lpNameBuffer, name);
1006 lstrcatW(lpNameBuffer, ds);
1007 status = TRUE;
1010 else /* just requesting length required */
1012 *nSize = len;
1013 status = TRUE;
1016 else
1018 SetLastError(ERROR_INTERNAL_ERROR);
1019 status = FALSE;
1022 break;
1023 case NameFullyQualifiedDN:
1024 case NameDisplay:
1025 case NameUniqueId:
1026 case NameCanonical:
1027 case NameUserPrincipal:
1028 case NameCanonicalEx:
1029 case NameServicePrincipal:
1030 case NameDnsDomain:
1031 FIXME("NameFormat %d not implemented\n", NameFormat);
1032 SetLastError(ERROR_CANT_ACCESS_DOMAIN_INFO);
1033 status = FALSE;
1034 break;
1035 default:
1036 SetLastError(ERROR_INVALID_PARAMETER);
1037 status = FALSE;
1040 else
1042 SetLastError(ERROR_CANT_ACCESS_DOMAIN_INFO);
1043 status = FALSE;
1046 LsaFreeMemory(domainInfo);
1047 LsaClose(policyHandle);
1049 return status;
1052 /***********************************************************************
1053 * GetUserNameExA (SECUR32.@)
1055 BOOLEAN WINAPI GetUserNameExA(
1056 EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize)
1058 FIXME("%d %p %p\n", NameFormat, lpNameBuffer, nSize);
1059 return FALSE;
1062 BOOLEAN WINAPI GetUserNameExW(
1063 EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG nSize)
1065 FIXME("%d %p %p\n", NameFormat, lpNameBuffer, nSize);
1066 return FALSE;
1069 BOOLEAN WINAPI TranslateNameA(
1070 LPCSTR lpAccountName, EXTENDED_NAME_FORMAT AccountNameFormat,
1071 EXTENDED_NAME_FORMAT DesiredNameFormat, LPSTR lpTranslatedName,
1072 PULONG nSize)
1074 FIXME("%p %d %d %p %p\n", lpAccountName, AccountNameFormat,
1075 DesiredNameFormat, lpTranslatedName, nSize);
1076 return FALSE;
1079 BOOLEAN WINAPI TranslateNameW(
1080 LPCWSTR lpAccountName, EXTENDED_NAME_FORMAT AccountNameFormat,
1081 EXTENDED_NAME_FORMAT DesiredNameFormat, LPWSTR lpTranslatedName,
1082 PULONG nSize)
1084 FIXME("%p %d %d %p %p\n", lpAccountName, AccountNameFormat,
1085 DesiredNameFormat, lpTranslatedName, nSize);
1086 return FALSE;
1089 /***********************************************************************
1090 * DllMain (SECUR32.0)
1092 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1094 if (fdwReason == DLL_PROCESS_ATTACH)
1096 DisableThreadLibraryCalls(hinstDLL);
1097 SECUR32_initializeProviders();
1099 else if (fdwReason == DLL_PROCESS_DETACH)
1101 SECUR32_freeProviders();
1104 return TRUE;