Release 0.9.14.
[wine/multimedia.git] / dlls / secur32 / secur32.c
blob6ad2138db3f08e69a8fba3597cebb18f12a84d0c
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>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winnls.h"
24 #include "winreg.h"
25 #include "winternl.h"
26 #include "shlwapi.h"
27 #include "sspi.h"
28 #include "secur32_priv.h"
29 #include "secext.h"
30 #include "ntsecapi.h"
31 #include "thunks.h"
33 #include "wine/list.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
38 /**
39 * Type definitions
42 typedef struct _SecurePackageTable
44 DWORD numPackages;
45 DWORD numAllocated;
46 struct list table;
47 } SecurePackageTable;
49 typedef struct _SecureProviderTable
51 DWORD numProviders;
52 DWORD numAllocated;
53 struct list table;
54 } SecureProviderTable;
56 /**
57 * Prototypes
60 /* Tries to load moduleName as a provider. If successful, enumerates what
61 * packages it can and adds them to the package and provider tables. Resizes
62 * tables as necessary.
64 static void _tryLoadProvider(PWSTR moduleName);
66 /* Initialization: read securityproviders value and attempt to open each dll
67 * there. For each DLL, call _tryLoadProvider to see if it's really an SSP.
68 * Two undocumented functions, AddSecurityPackage(A/W) and
69 * DeleteSecurityPackage(A/W), seem suspiciously like they'd register or
70 * unregister a dll, but I'm not sure.
72 static void SECUR32_initializeProviders(void);
74 /* Frees all loaded packages and providers */
75 static void SECUR32_freeProviders(void);
77 /**
78 * Globals
81 static CRITICAL_SECTION cs;
82 static SecurePackageTable *packageTable = NULL;
83 static SecureProviderTable *providerTable = NULL;
85 static SecurityFunctionTableA securityFunctionTableA = {
86 SECURITY_SUPPORT_PROVIDER_INTERFACE_VERSION_2,
87 EnumerateSecurityPackagesA,
88 QueryCredentialsAttributesA,
89 AcquireCredentialsHandleA,
90 FreeCredentialsHandle,
91 NULL, /* Reserved2 */
92 InitializeSecurityContextA,
93 AcceptSecurityContext,
94 CompleteAuthToken,
95 DeleteSecurityContext,
96 ApplyControlToken,
97 QueryContextAttributesA,
98 ImpersonateSecurityContext,
99 RevertSecurityContext,
100 MakeSignature,
101 VerifySignature,
102 FreeContextBuffer,
103 QuerySecurityPackageInfoA,
104 NULL, /* Reserved3 */
105 NULL, /* Reserved4 */
106 ExportSecurityContext,
107 ImportSecurityContextA,
108 AddCredentialsA,
109 NULL, /* Reserved8 */
110 QuerySecurityContextToken,
111 EncryptMessage,
112 DecryptMessage,
113 SetContextAttributesA
116 static SecurityFunctionTableW securityFunctionTableW = {
117 SECURITY_SUPPORT_PROVIDER_INTERFACE_VERSION_2,
118 EnumerateSecurityPackagesW,
119 QueryCredentialsAttributesW,
120 AcquireCredentialsHandleW,
121 FreeCredentialsHandle,
122 NULL, /* Reserved2 */
123 InitializeSecurityContextW,
124 AcceptSecurityContext,
125 CompleteAuthToken,
126 DeleteSecurityContext,
127 ApplyControlToken,
128 QueryContextAttributesW,
129 ImpersonateSecurityContext,
130 RevertSecurityContext,
131 MakeSignature,
132 VerifySignature,
133 FreeContextBuffer,
134 QuerySecurityPackageInfoW,
135 NULL, /* Reserved3 */
136 NULL, /* Reserved4 */
137 ExportSecurityContext,
138 ImportSecurityContextW,
139 AddCredentialsW,
140 NULL, /* Reserved8 */
141 QuerySecurityContextToken,
142 EncryptMessage,
143 DecryptMessage,
144 SetContextAttributesW
147 /***********************************************************************
148 * InitSecurityInterfaceA (SECUR32.@)
150 PSecurityFunctionTableA WINAPI InitSecurityInterfaceA(void)
152 return &securityFunctionTableA;
155 /***********************************************************************
156 * InitSecurityInterfaceW (SECUR32.@)
158 PSecurityFunctionTableW WINAPI InitSecurityInterfaceW(void)
160 return &securityFunctionTableW;
163 PWSTR SECUR32_strdupW(PCWSTR str)
165 PWSTR ret;
167 if (str)
169 ret = (PWSTR)SECUR32_ALLOC((lstrlenW(str) + 1) * sizeof(WCHAR));
170 if (ret)
171 lstrcpyW(ret, str);
173 else
174 ret = NULL;
175 return ret;
178 PWSTR SECUR32_AllocWideFromMultiByte(PCSTR str)
180 PWSTR ret;
182 if (str)
184 int charsNeeded = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
186 if (charsNeeded)
188 ret = (PWSTR)SECUR32_ALLOC(charsNeeded * sizeof(WCHAR));
189 if (ret)
190 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, charsNeeded);
192 else
193 ret = NULL;
195 else
196 ret = NULL;
197 return ret;
200 PSTR SECUR32_AllocMultiByteFromWide(PCWSTR str)
202 PSTR ret;
204 if (str)
206 int charsNeeded = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0,
207 NULL, NULL);
209 if (charsNeeded)
211 ret = (PSTR)SECUR32_ALLOC(charsNeeded);
212 if (ret)
213 WideCharToMultiByte(CP_ACP, 0, str, -1, ret, charsNeeded,
214 NULL, NULL);
216 else
217 ret = NULL;
219 else
220 ret = NULL;
221 return ret;
224 static void _makeFnTableA(PSecurityFunctionTableA fnTableA,
225 const SecurityFunctionTableA *inFnTableA,
226 const SecurityFunctionTableW *inFnTableW)
228 if (fnTableA)
230 if (inFnTableA)
232 /* The size of the version 1 table is based on platform sdk's
233 * sspi.h, though the sample ssp also provided with platform sdk
234 * implies only functions through QuerySecurityPackageInfoA are
235 * implemented (yikes)
237 size_t tableSize = inFnTableA->dwVersion == 1 ?
238 (LPBYTE)&inFnTableA->SetContextAttributesA -
239 (LPBYTE)inFnTableA : sizeof(SecurityFunctionTableA);
241 memcpy(fnTableA, inFnTableA, tableSize);
242 /* override this, since we can do it internally anyway */
243 fnTableA->QuerySecurityPackageInfoA =
244 QuerySecurityPackageInfoA;
246 else if (inFnTableW)
248 /* functions with thunks */
249 if (inFnTableW->AcquireCredentialsHandleW)
250 fnTableA->AcquireCredentialsHandleA =
251 thunk_AcquireCredentialsHandleA;
252 if (inFnTableW->InitializeSecurityContextW)
253 fnTableA->InitializeSecurityContextA =
254 thunk_InitializeSecurityContextA;
255 if (inFnTableW->ImportSecurityContextW)
256 fnTableA->ImportSecurityContextA =
257 thunk_ImportSecurityContextA;
258 if (inFnTableW->AddCredentialsW)
259 fnTableA->AddCredentialsA =
260 thunk_AddCredentialsA;
261 if (inFnTableW->QueryCredentialsAttributesW)
262 fnTableA->QueryCredentialsAttributesA =
263 thunk_QueryCredentialsAttributesA;
264 if (inFnTableW->QueryContextAttributesW)
265 fnTableA->QueryContextAttributesA =
266 thunk_QueryContextAttributesA;
267 if (inFnTableW->SetContextAttributesW)
268 fnTableA->SetContextAttributesA =
269 thunk_SetContextAttributesA;
270 /* this can't be thunked, there's no extra param to know which
271 * package to forward to */
272 fnTableA->EnumerateSecurityPackagesA = NULL;
273 /* functions with no thunks needed */
274 fnTableA->AcceptSecurityContext = inFnTableW->AcceptSecurityContext;
275 fnTableA->CompleteAuthToken = inFnTableW->CompleteAuthToken;
276 fnTableA->DeleteSecurityContext = inFnTableW->DeleteSecurityContext;
277 fnTableA->ImpersonateSecurityContext =
278 inFnTableW->ImpersonateSecurityContext;
279 fnTableA->RevertSecurityContext = inFnTableW->RevertSecurityContext;
280 fnTableA->MakeSignature = inFnTableW->MakeSignature;
281 fnTableA->VerifySignature = inFnTableW->VerifySignature;
282 fnTableA->FreeContextBuffer = inFnTableW->FreeContextBuffer;
283 fnTableA->QuerySecurityPackageInfoA =
284 QuerySecurityPackageInfoA;
285 fnTableA->ExportSecurityContext =
286 inFnTableW->ExportSecurityContext;
287 fnTableA->QuerySecurityContextToken =
288 inFnTableW->QuerySecurityContextToken;
289 fnTableA->EncryptMessage = inFnTableW->EncryptMessage;
290 fnTableA->DecryptMessage = inFnTableW->DecryptMessage;
295 static void _makeFnTableW(PSecurityFunctionTableW fnTableW,
296 const SecurityFunctionTableA *inFnTableA,
297 const SecurityFunctionTableW *inFnTableW)
299 if (fnTableW)
301 if (inFnTableW)
303 /* The size of the version 1 table is based on platform sdk's
304 * sspi.h, though the sample ssp also provided with platform sdk
305 * implies only functions through QuerySecurityPackageInfoA are
306 * implemented (yikes)
308 size_t tableSize = inFnTableW->dwVersion == 1 ?
309 (LPBYTE)&inFnTableW->SetContextAttributesW -
310 (LPBYTE)inFnTableW : sizeof(SecurityFunctionTableW);
312 memcpy(fnTableW, inFnTableW, tableSize);
313 /* override this, since we can do it internally anyway */
314 fnTableW->QuerySecurityPackageInfoW =
315 QuerySecurityPackageInfoW;
317 else if (inFnTableA)
319 /* functions with thunks */
320 if (inFnTableA->AcquireCredentialsHandleA)
321 fnTableW->AcquireCredentialsHandleW =
322 thunk_AcquireCredentialsHandleW;
323 if (inFnTableA->InitializeSecurityContextA)
324 fnTableW->InitializeSecurityContextW =
325 thunk_InitializeSecurityContextW;
326 if (inFnTableA->ImportSecurityContextA)
327 fnTableW->ImportSecurityContextW =
328 thunk_ImportSecurityContextW;
329 if (inFnTableA->AddCredentialsA)
330 fnTableW->AddCredentialsW =
331 thunk_AddCredentialsW;
332 if (inFnTableA->QueryCredentialsAttributesA)
333 fnTableW->QueryCredentialsAttributesW =
334 thunk_QueryCredentialsAttributesW;
335 if (inFnTableA->QueryContextAttributesA)
336 fnTableW->QueryContextAttributesW =
337 thunk_QueryContextAttributesW;
338 if (inFnTableA->SetContextAttributesA)
339 fnTableW->SetContextAttributesW =
340 thunk_SetContextAttributesW;
341 /* this can't be thunked, there's no extra param to know which
342 * package to forward to */
343 fnTableW->EnumerateSecurityPackagesW = NULL;
344 /* functions with no thunks needed */
345 fnTableW->AcceptSecurityContext = inFnTableA->AcceptSecurityContext;
346 fnTableW->CompleteAuthToken = inFnTableA->CompleteAuthToken;
347 fnTableW->DeleteSecurityContext = inFnTableA->DeleteSecurityContext;
348 fnTableW->ImpersonateSecurityContext =
349 inFnTableA->ImpersonateSecurityContext;
350 fnTableW->RevertSecurityContext = inFnTableA->RevertSecurityContext;
351 fnTableW->MakeSignature = inFnTableA->MakeSignature;
352 fnTableW->VerifySignature = inFnTableA->VerifySignature;
353 fnTableW->FreeContextBuffer = inFnTableA->FreeContextBuffer;
354 fnTableW->QuerySecurityPackageInfoW =
355 QuerySecurityPackageInfoW;
356 fnTableW->ExportSecurityContext =
357 inFnTableA->ExportSecurityContext;
358 fnTableW->QuerySecurityContextToken =
359 inFnTableA->QuerySecurityContextToken;
360 fnTableW->EncryptMessage = inFnTableA->EncryptMessage;
361 fnTableW->DecryptMessage = inFnTableA->DecryptMessage;
366 static void _copyPackageInfo(PSecPkgInfoW info, const SecPkgInfoA *inInfoA,
367 const SecPkgInfoW *inInfoW)
369 if (info && (inInfoA || inInfoW))
371 /* odd, I know, but up until Name and Comment the structures are
372 * identical
374 memcpy(info, inInfoW ? inInfoW : (PSecPkgInfoW)inInfoA, sizeof(*info));
375 if (inInfoW)
377 info->Name = SECUR32_strdupW(inInfoW->Name);
378 info->Comment = SECUR32_strdupW(inInfoW->Comment);
380 else
382 info->Name = SECUR32_AllocWideFromMultiByte(inInfoA->Name);
383 info->Comment = SECUR32_AllocWideFromMultiByte(inInfoA->Comment);
388 SecureProvider *SECUR32_addProvider(const SecurityFunctionTableA *fnTableA,
389 const SecurityFunctionTableW *fnTableW, const PWSTR moduleName)
391 SecureProvider *ret;
393 EnterCriticalSection(&cs);
395 if (!providerTable)
397 providerTable = HeapAlloc(GetProcessHeap(), 0, sizeof(SecureProviderTable));
398 if (!providerTable)
400 LeaveCriticalSection(&cs);
401 return NULL;
404 list_init(&providerTable->table);
407 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(SecureProvider));
408 if (!ret)
410 LeaveCriticalSection(&cs);
411 return NULL;
414 list_add_tail(&providerTable->table, &ret->entry);
415 ret->lib = NULL;
417 if (fnTableA || fnTableW)
419 ret->moduleName = NULL;
420 _makeFnTableA(&ret->fnTableA, fnTableA, fnTableW);
421 _makeFnTableW(&ret->fnTableW, fnTableA, fnTableW);
422 ret->loaded = TRUE;
424 else
426 ret->moduleName = SECUR32_strdupW(moduleName);
427 ret->loaded = FALSE;
430 LeaveCriticalSection(&cs);
431 return ret;
434 void SECUR32_addPackages(SecureProvider *provider, ULONG toAdd,
435 const SecPkgInfoA *infoA, const SecPkgInfoW *infoW)
437 ULONG i;
439 assert(provider);
440 assert(infoA || infoW);
442 EnterCriticalSection(&cs);
444 if (!packageTable)
446 packageTable = HeapAlloc(GetProcessHeap(), 0, sizeof(SecurePackageTable));
447 if (!packageTable)
449 LeaveCriticalSection(&cs);
450 return;
453 packageTable->numPackages = 0;
454 list_init(&packageTable->table);
457 for (i = 0; i < toAdd; i++)
459 SecurePackage *package = HeapAlloc(GetProcessHeap(), 0, sizeof(SecurePackage));
460 if (!package)
461 continue;
463 list_add_tail(&packageTable->table, &package->entry);
465 package->provider = provider;
466 _copyPackageInfo(&package->infoW,
467 infoA ? &infoA[i] : NULL,
468 infoW ? &infoW[i] : NULL);
470 packageTable->numPackages += toAdd;
472 LeaveCriticalSection(&cs);
475 static void _tryLoadProvider(PWSTR moduleName)
477 HMODULE lib = LoadLibraryW(moduleName);
479 if (lib)
481 INIT_SECURITY_INTERFACE_W pInitSecurityInterfaceW =
482 (INIT_SECURITY_INTERFACE_W)GetProcAddress(lib,
483 SECURITY_ENTRYPOINT_ANSIW);
484 INIT_SECURITY_INTERFACE_A pInitSecurityInterfaceA =
485 (INIT_SECURITY_INTERFACE_A)GetProcAddress(lib,
486 SECURITY_ENTRYPOINT_ANSIA);
488 TRACE("loaded %s, InitSecurityInterfaceA is %p, InitSecurityInterfaceW is %p\n",
489 debugstr_w(moduleName), pInitSecurityInterfaceA,
490 pInitSecurityInterfaceW);
491 if (pInitSecurityInterfaceW || pInitSecurityInterfaceA)
493 PSecurityFunctionTableA fnTableA = NULL;
494 PSecurityFunctionTableW fnTableW = NULL;
495 ULONG toAdd = 0;
496 PSecPkgInfoA infoA = NULL;
497 PSecPkgInfoW infoW = NULL;
498 SECURITY_STATUS ret = SEC_E_OK;
500 if (pInitSecurityInterfaceA)
501 fnTableA = pInitSecurityInterfaceA();
502 if (pInitSecurityInterfaceW)
503 fnTableW = pInitSecurityInterfaceW();
504 if (fnTableW && fnTableW->EnumerateSecurityPackagesW)
505 ret = fnTableW->EnumerateSecurityPackagesW(&toAdd, &infoW);
506 else if (fnTableA && fnTableA->EnumerateSecurityPackagesA)
507 ret = fnTableA->EnumerateSecurityPackagesA(&toAdd, &infoA);
508 if (ret == SEC_E_OK && toAdd > 0 && (infoW || infoA))
510 SecureProvider *provider = SECUR32_addProvider(NULL, NULL,
511 moduleName);
513 if (provider)
514 SECUR32_addPackages(provider, toAdd, infoA, infoW);
515 if (infoW)
516 fnTableW->FreeContextBuffer(infoW);
517 else
518 fnTableA->FreeContextBuffer(infoA);
521 FreeLibrary(lib);
523 else
524 WARN("failed to load %s\n", debugstr_w(moduleName));
527 static const WCHAR securityProvidersKeyW[] = {
528 'S','Y','S','T','E','M','\\','C','u','r','r','e','n','t','C','o','n','t','r',
529 'o','l','S','e','t','\\','C','o','n','t','r','o','l','\\','S','e','c','u','r',
530 'i','t','y','P','r','o','v','i','d','e','r','s','\0'
532 static const WCHAR securityProvidersW[] = {
533 'S','e','c','u','r','i','t','y','P','r','o','v','i','d','e','r','s',0
536 static void SECUR32_initializeProviders(void)
538 HKEY key;
539 long apiRet;
541 TRACE("\n");
542 InitializeCriticalSection(&cs);
543 /* First load built-in providers */
544 SECUR32_initSchannelSP();
545 SECUR32_initNegotiateSP();
546 SECUR32_initNTLMSP();
547 /* Now load providers from registry */
548 apiRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, securityProvidersKeyW, 0,
549 KEY_READ, &key);
550 if (apiRet == ERROR_SUCCESS)
552 WCHAR securityPkgNames[MAX_PATH]; /* arbitrary len */
553 DWORD size = sizeof(securityPkgNames) / sizeof(WCHAR), type;
555 apiRet = RegQueryValueExW(key, securityProvidersW, NULL, &type,
556 (PBYTE)securityPkgNames, &size);
557 if (apiRet == ERROR_SUCCESS && type == REG_SZ)
559 WCHAR *ptr;
561 for (ptr = securityPkgNames;
562 ptr < (PWSTR)((PBYTE)securityPkgNames + size); )
564 WCHAR *comma;
566 for (comma = ptr; *comma && *comma != ','; comma++)
568 if (*comma == ',')
569 *comma = '\0';
570 for (; *ptr && isspace(*ptr) && ptr < securityPkgNames + size;
571 ptr++)
573 if (*ptr)
574 _tryLoadProvider(ptr);
575 ptr += lstrlenW(ptr) + 1;
578 RegCloseKey(key);
582 SecurePackage *SECUR32_findPackageW(PWSTR packageName)
584 SecurePackage *ret = NULL;
585 BOOL matched = FALSE;
587 if (packageTable && packageName)
589 LIST_FOR_EACH_ENTRY(ret, &packageTable->table, SecurePackage, entry)
591 matched = !lstrcmpiW(ret->infoW.Name, packageName);
592 if (matched)
593 break;
596 if (!matched)
597 return NULL;
599 if (ret->provider && !ret->provider->loaded)
601 ret->provider->lib = LoadLibraryW(ret->provider->moduleName);
602 if (ret->provider->lib)
604 INIT_SECURITY_INTERFACE_W pInitSecurityInterfaceW =
605 (INIT_SECURITY_INTERFACE_W)GetProcAddress(ret->provider->lib,
606 SECURITY_ENTRYPOINT_ANSIW);
607 INIT_SECURITY_INTERFACE_A pInitSecurityInterfaceA =
608 (INIT_SECURITY_INTERFACE_A)GetProcAddress(ret->provider->lib,
609 SECURITY_ENTRYPOINT_ANSIA);
610 PSecurityFunctionTableA fnTableA = NULL;
611 PSecurityFunctionTableW fnTableW = NULL;
613 if (pInitSecurityInterfaceA)
614 fnTableA = pInitSecurityInterfaceA();
615 if (pInitSecurityInterfaceW)
616 fnTableW = pInitSecurityInterfaceW();
617 _makeFnTableA(&ret->provider->fnTableA, fnTableA, fnTableW);
618 _makeFnTableW(&ret->provider->fnTableW, fnTableA, fnTableW);
619 ret->provider->loaded = TRUE;
621 else
622 ret = NULL;
625 return ret;
628 SecurePackage *SECUR32_findPackageA(PSTR packageName)
630 SecurePackage *ret;
632 if (packageTable && packageName)
634 UNICODE_STRING package;
636 RtlCreateUnicodeStringFromAsciiz(&package, packageName);
637 ret = SECUR32_findPackageW(package.Buffer);
638 RtlFreeUnicodeString(&package);
640 else
641 ret = NULL;
642 return ret;
645 static void SECUR32_freeProviders(void)
647 SecurePackage *package;
648 SecureProvider *provider;
650 TRACE("\n");
651 EnterCriticalSection(&cs);
653 if (packageTable)
655 LIST_FOR_EACH_ENTRY(package, &packageTable->table, SecurePackage, entry)
657 SECUR32_FREE(package->infoW.Name);
658 SECUR32_FREE(package->infoW.Comment);
661 HeapFree(GetProcessHeap(), 0, packageTable);
662 packageTable = NULL;
665 if (providerTable)
667 LIST_FOR_EACH_ENTRY(provider, &providerTable->table, SecureProvider, entry)
669 SECUR32_FREE(provider->moduleName);
670 if (provider->lib)
671 FreeLibrary(provider->lib);
674 HeapFree(GetProcessHeap(), 0, providerTable);
675 providerTable = NULL;
678 LeaveCriticalSection(&cs);
679 DeleteCriticalSection(&cs);
682 /***********************************************************************
683 * FreeContextBuffer (SECUR32.@)
685 * Doh--if pv was allocated by a crypto package, this may not be correct.
686 * The sample ssp seems to use LocalAlloc/LocalFee, but there doesn't seem to
687 * be any guarantee, nor is there an alloc function in secur32.
689 SECURITY_STATUS WINAPI FreeContextBuffer(PVOID pv)
691 SECUR32_FREE(pv);
693 return SEC_E_OK;
696 /***********************************************************************
697 * EnumerateSecurityPackagesW (SECUR32.@)
699 SECURITY_STATUS WINAPI EnumerateSecurityPackagesW(PULONG pcPackages,
700 PSecPkgInfoW *ppPackageInfo)
702 SECURITY_STATUS ret = SEC_E_OK;
704 TRACE("(%p, %p)\n", pcPackages, ppPackageInfo);
706 /* windows just crashes if pcPackages or ppPackageInfo is NULL, so will I */
707 *pcPackages = 0;
708 EnterCriticalSection(&cs);
709 if (packageTable)
711 SecurePackage *package;
712 size_t bytesNeeded;
714 bytesNeeded = packageTable->numPackages * sizeof(SecPkgInfoW);
715 LIST_FOR_EACH_ENTRY(package, &packageTable->table, SecurePackage, entry)
717 if (package->infoW.Name)
718 bytesNeeded += (lstrlenW(package->infoW.Name) + 1) * sizeof(WCHAR);
719 if (package->infoW.Comment)
720 bytesNeeded += (lstrlenW(package->infoW.Comment) + 1) * sizeof(WCHAR);
722 if (bytesNeeded)
724 *ppPackageInfo = (PSecPkgInfoW)SECUR32_ALLOC(bytesNeeded);
725 if (*ppPackageInfo)
727 ULONG i = 0;
728 PWSTR nextString;
730 *pcPackages = packageTable->numPackages;
731 nextString = (PWSTR)((PBYTE)*ppPackageInfo +
732 packageTable->numPackages * sizeof(SecPkgInfoW));
733 LIST_FOR_EACH_ENTRY(package, &packageTable->table, SecurePackage, entry)
735 PSecPkgInfoW pkgInfo = *ppPackageInfo + i++;
737 memcpy(pkgInfo, &package->infoW, sizeof(SecPkgInfoW));
738 if (package->infoW.Name)
740 TRACE("Name[%ld] = %s\n", i - 1, debugstr_w(package->infoW.Name));
741 pkgInfo->Name = nextString;
742 lstrcpyW(nextString, package->infoW.Name);
743 nextString += lstrlenW(nextString) + 1;
745 else
746 pkgInfo->Name = NULL;
747 if (package->infoW.Comment)
749 TRACE("Comment[%ld] = %s\n", i - 1, debugstr_w(package->infoW.Comment));
750 pkgInfo->Comment = nextString;
751 lstrcpyW(nextString, package->infoW.Comment);
752 nextString += lstrlenW(nextString) + 1;
754 else
755 pkgInfo->Comment = NULL;
758 else
759 ret = SEC_E_INSUFFICIENT_MEMORY;
762 LeaveCriticalSection(&cs);
763 TRACE("<-- 0x%08lx\n", ret);
764 return ret;
767 /* Converts info (which is assumed to be an array of cPackages SecPkgInfoW
768 * structures) into an array of SecPkgInfoA structures, which it returns.
770 static PSecPkgInfoA thunk_PSecPkgInfoWToA(ULONG cPackages,
771 const PSecPkgInfoW info)
773 PSecPkgInfoA ret;
775 if (info)
777 size_t bytesNeeded = cPackages * sizeof(SecPkgInfoA);
778 ULONG i;
780 for (i = 0; i < cPackages; i++)
782 if (info[i].Name)
783 bytesNeeded += WideCharToMultiByte(CP_ACP, 0, info[i].Name,
784 -1, NULL, 0, NULL, NULL);
785 if (info[i].Comment)
786 bytesNeeded += WideCharToMultiByte(CP_ACP, 0, info[i].Comment,
787 -1, NULL, 0, NULL, NULL);
789 ret = (PSecPkgInfoA)SECUR32_ALLOC(bytesNeeded);
790 if (ret)
792 PSTR nextString;
794 nextString = (PSTR)((PBYTE)ret + cPackages * sizeof(SecPkgInfoA));
795 for (i = 0; i < cPackages; i++)
797 PSecPkgInfoA pkgInfo = ret + i;
798 int bytes;
800 memcpy(pkgInfo, &info[i], sizeof(SecPkgInfoA));
801 if (info[i].Name)
803 pkgInfo->Name = nextString;
804 /* just repeat back to WideCharToMultiByte how many bytes
805 * it requires, since we asked it earlier
807 bytes = WideCharToMultiByte(CP_ACP, 0, info[i].Name, -1,
808 NULL, 0, NULL, NULL);
809 WideCharToMultiByte(CP_ACP, 0, info[i].Name, -1,
810 pkgInfo->Name, bytes, NULL, NULL);
811 nextString += lstrlenA(nextString) + 1;
813 else
814 pkgInfo->Name = NULL;
815 if (info[i].Comment)
817 pkgInfo->Comment = nextString;
818 /* just repeat back to WideCharToMultiByte how many bytes
819 * it requires, since we asked it earlier
821 bytes = WideCharToMultiByte(CP_ACP, 0, info[i].Comment, -1,
822 NULL, 0, NULL, NULL);
823 WideCharToMultiByte(CP_ACP, 0, info[i].Comment, -1,
824 pkgInfo->Comment, bytes, NULL, NULL);
825 nextString += lstrlenA(nextString) + 1;
827 else
828 pkgInfo->Comment = NULL;
832 else
833 ret = NULL;
834 return ret;
837 /***********************************************************************
838 * EnumerateSecurityPackagesA (SECUR32.@)
840 SECURITY_STATUS WINAPI EnumerateSecurityPackagesA(PULONG pcPackages,
841 PSecPkgInfoA *ppPackageInfo)
843 SECURITY_STATUS ret;
844 PSecPkgInfoW info;
846 ret = EnumerateSecurityPackagesW(pcPackages, &info);
847 if (ret == SEC_E_OK && *pcPackages && info)
849 *ppPackageInfo = thunk_PSecPkgInfoWToA(*pcPackages, info);
850 if (*pcPackages && !*ppPackageInfo)
852 *pcPackages = 0;
853 ret = SEC_E_INSUFFICIENT_MEMORY;
855 FreeContextBuffer(info);
857 return ret;
860 /***********************************************************************
861 * GetComputerObjectNameA (SECUR32.@)
863 BOOLEAN WINAPI GetComputerObjectNameA(
864 EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize)
866 FIXME("%d %p %p\n", NameFormat, lpNameBuffer, nSize);
867 return FALSE;
870 /***********************************************************************
871 * GetComputerObjectNameW (SECUR32.@)
873 BOOLEAN WINAPI GetComputerObjectNameW(
874 EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG nSize)
876 FIXME("%d %p %p\n", NameFormat, lpNameBuffer, nSize);
877 return FALSE;
880 BOOLEAN WINAPI GetUserNameExA(
881 EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize)
883 FIXME("%d %p %p\n", NameFormat, lpNameBuffer, nSize);
884 return FALSE;
887 BOOLEAN WINAPI GetUserNameExW(
888 EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG nSize)
890 FIXME("%d %p %p\n", NameFormat, lpNameBuffer, nSize);
891 return FALSE;
894 NTSTATUS WINAPI LsaCallAuthenticationPackage(
895 HANDLE LsaHandle, ULONG AuthenticationPackage, PVOID ProtocolSubmitBuffer,
896 ULONG SubmitBufferLength, PVOID* ProtocolReturnBuffer, PULONG ReturnBufferLength,
897 PNTSTATUS ProtocolStatus)
899 FIXME("%p %ld %p %ld %p %p %p\n", LsaHandle, AuthenticationPackage,
900 ProtocolSubmitBuffer, SubmitBufferLength, ProtocolReturnBuffer,
901 ReturnBufferLength, ProtocolStatus);
902 return 0;
905 NTSTATUS WINAPI LsaConnectUntrusted(PHANDLE LsaHandle)
907 FIXME("%p\n", LsaHandle);
908 return 0;
911 NTSTATUS WINAPI LsaDeregisterLogonProcess(HANDLE LsaHandle)
913 FIXME("%p\n", LsaHandle);
914 return 0;
917 BOOLEAN WINAPI TranslateNameA(
918 LPCSTR lpAccountName, EXTENDED_NAME_FORMAT AccountNameFormat,
919 EXTENDED_NAME_FORMAT DesiredNameFormat, LPSTR lpTranslatedName,
920 PULONG nSize)
922 FIXME("%p %d %d %p %p\n", lpAccountName, AccountNameFormat,
923 DesiredNameFormat, lpTranslatedName, nSize);
924 return FALSE;
927 BOOLEAN WINAPI TranslateNameW(
928 LPCWSTR lpAccountName, EXTENDED_NAME_FORMAT AccountNameFormat,
929 EXTENDED_NAME_FORMAT DesiredNameFormat, LPWSTR lpTranslatedName,
930 PULONG nSize)
932 FIXME("%p %d %d %p %p\n", lpAccountName, AccountNameFormat,
933 DesiredNameFormat, lpTranslatedName, nSize);
934 return FALSE;
937 /***********************************************************************
938 * DllMain (SECUR32.0)
940 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
942 if (fdwReason == DLL_PROCESS_ATTACH)
944 DisableThreadLibraryCalls(hinstDLL);
945 SECUR32_initializeProviders();
947 else if (fdwReason == DLL_PROCESS_DETACH)
949 SECUR32_freeProviders();
952 return TRUE;