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
24 #define WIN32_NO_STATUS
37 #include "wine/list.h"
38 #include "wine/debug.h"
39 #include "secur32_priv.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(secur32
);
47 typedef struct _SecurePackageTable
54 typedef struct _SecureProviderTable
59 } SecureProviderTable
;
65 /* Tries to load moduleName as a provider. If successful, enumerates what
66 * packages it can and adds them to the package and provider tables. Resizes
67 * tables as necessary.
69 static void _tryLoadProvider(PWSTR moduleName
);
71 /* Initialization: read securityproviders value and attempt to open each dll
72 * there. For each DLL, call _tryLoadProvider to see if it's really an SSP.
73 * Two undocumented functions, AddSecurityPackage(A/W) and
74 * DeleteSecurityPackage(A/W), seem suspiciously like they'd register or
75 * unregister a dll, but I'm not sure.
77 static void SECUR32_initializeProviders(void);
79 /* Frees all loaded packages and providers */
80 static void SECUR32_freeProviders(void);
86 static CRITICAL_SECTION cs
;
87 static CRITICAL_SECTION_DEBUG cs_debug
=
90 { &cs_debug
.ProcessLocksList
, &cs_debug
.ProcessLocksList
},
91 0, 0, { (DWORD_PTR
)(__FILE__
": cs") }
93 static CRITICAL_SECTION cs
= { &cs_debug
, -1, 0, 0, 0, 0 };
94 static SecurePackageTable
*packageTable
= NULL
;
95 static SecureProviderTable
*providerTable
= NULL
;
97 static SecurityFunctionTableA securityFunctionTableA
= {
98 SECURITY_SUPPORT_PROVIDER_INTERFACE_VERSION
,
99 EnumerateSecurityPackagesA
,
100 QueryCredentialsAttributesA
,
101 AcquireCredentialsHandleA
,
102 FreeCredentialsHandle
,
103 NULL
, /* Reserved2 */
104 InitializeSecurityContextA
,
105 AcceptSecurityContext
,
107 DeleteSecurityContext
,
109 QueryContextAttributesA
,
110 ImpersonateSecurityContext
,
111 RevertSecurityContext
,
115 QuerySecurityPackageInfoA
,
116 EncryptMessage
, /* Reserved3 */
117 DecryptMessage
, /* Reserved4 */
118 ExportSecurityContext
,
119 ImportSecurityContextA
,
121 NULL
, /* Reserved8 */
122 QuerySecurityContextToken
,
125 SetContextAttributesA
128 static SecurityFunctionTableW securityFunctionTableW
= {
129 SECURITY_SUPPORT_PROVIDER_INTERFACE_VERSION
,
130 EnumerateSecurityPackagesW
,
131 QueryCredentialsAttributesW
,
132 AcquireCredentialsHandleW
,
133 FreeCredentialsHandle
,
134 NULL
, /* Reserved2 */
135 InitializeSecurityContextW
,
136 AcceptSecurityContext
,
138 DeleteSecurityContext
,
140 QueryContextAttributesW
,
141 ImpersonateSecurityContext
,
142 RevertSecurityContext
,
146 QuerySecurityPackageInfoW
,
147 EncryptMessage
, /* Reserved3 */
148 DecryptMessage
, /* Reserved4 */
149 ExportSecurityContext
,
150 ImportSecurityContextW
,
152 NULL
, /* Reserved8 */
153 QuerySecurityContextToken
,
156 SetContextAttributesW
159 /***********************************************************************
160 * InitSecurityInterfaceA (SECUR32.@)
162 PSecurityFunctionTableA WINAPI
InitSecurityInterfaceA(void)
164 return &securityFunctionTableA
;
167 /***********************************************************************
168 * InitSecurityInterfaceW (SECUR32.@)
170 PSecurityFunctionTableW WINAPI
InitSecurityInterfaceW(void)
172 return &securityFunctionTableW
;
175 static void _makeFnTableA(PSecurityFunctionTableA fnTableA
,
176 const SecurityFunctionTableA
*inFnTableA
,
177 const SecurityFunctionTableW
*inFnTableW
)
183 /* The size of the version 1 table is based on platform sdk's
184 * sspi.h, though the sample ssp also provided with platform sdk
185 * implies only functions through QuerySecurityPackageInfoA are
186 * implemented (yikes)
188 size_t tableSize
= inFnTableA
->dwVersion
== 1 ?
189 offsetof(SecurityFunctionTableA
, SetContextAttributesA
) :
190 sizeof(SecurityFunctionTableA
);
192 memcpy(fnTableA
, inFnTableA
, tableSize
);
193 /* override this, since we can do it internally anyway */
194 fnTableA
->QuerySecurityPackageInfoA
=
195 QuerySecurityPackageInfoA
;
199 /* functions with thunks */
200 if (inFnTableW
->AcquireCredentialsHandleW
)
201 fnTableA
->AcquireCredentialsHandleA
=
202 thunk_AcquireCredentialsHandleA
;
203 if (inFnTableW
->InitializeSecurityContextW
)
204 fnTableA
->InitializeSecurityContextA
=
205 thunk_InitializeSecurityContextA
;
206 if (inFnTableW
->ImportSecurityContextW
)
207 fnTableA
->ImportSecurityContextA
=
208 thunk_ImportSecurityContextA
;
209 if (inFnTableW
->AddCredentialsW
)
210 fnTableA
->AddCredentialsA
=
211 thunk_AddCredentialsA
;
212 if (inFnTableW
->QueryCredentialsAttributesW
)
213 fnTableA
->QueryCredentialsAttributesA
=
214 thunk_QueryCredentialsAttributesA
;
215 if (inFnTableW
->QueryContextAttributesW
)
216 fnTableA
->QueryContextAttributesA
=
217 thunk_QueryContextAttributesA
;
218 if (inFnTableW
->SetContextAttributesW
)
219 fnTableA
->SetContextAttributesA
=
220 thunk_SetContextAttributesA
;
221 /* this can't be thunked, there's no extra param to know which
222 * package to forward to */
223 fnTableA
->EnumerateSecurityPackagesA
= NULL
;
224 /* functions with no thunks needed */
225 fnTableA
->AcceptSecurityContext
= inFnTableW
->AcceptSecurityContext
;
226 fnTableA
->CompleteAuthToken
= inFnTableW
->CompleteAuthToken
;
227 fnTableA
->DeleteSecurityContext
= inFnTableW
->DeleteSecurityContext
;
228 fnTableA
->ImpersonateSecurityContext
=
229 inFnTableW
->ImpersonateSecurityContext
;
230 fnTableA
->RevertSecurityContext
= inFnTableW
->RevertSecurityContext
;
231 fnTableA
->MakeSignature
= inFnTableW
->MakeSignature
;
232 fnTableA
->VerifySignature
= inFnTableW
->VerifySignature
;
233 fnTableA
->FreeContextBuffer
= inFnTableW
->FreeContextBuffer
;
234 fnTableA
->QuerySecurityPackageInfoA
=
235 QuerySecurityPackageInfoA
;
236 fnTableA
->ExportSecurityContext
=
237 inFnTableW
->ExportSecurityContext
;
238 fnTableA
->QuerySecurityContextToken
=
239 inFnTableW
->QuerySecurityContextToken
;
240 fnTableA
->EncryptMessage
= inFnTableW
->EncryptMessage
;
241 fnTableA
->DecryptMessage
= inFnTableW
->DecryptMessage
;
246 static void _makeFnTableW(PSecurityFunctionTableW fnTableW
,
247 const SecurityFunctionTableA
*inFnTableA
,
248 const SecurityFunctionTableW
*inFnTableW
)
254 /* The size of the version 1 table is based on platform sdk's
255 * sspi.h, though the sample ssp also provided with platform sdk
256 * implies only functions through QuerySecurityPackageInfoA are
257 * implemented (yikes)
259 size_t tableSize
= inFnTableW
->dwVersion
== 1 ?
260 offsetof(SecurityFunctionTableW
, SetContextAttributesW
) :
261 sizeof(SecurityFunctionTableW
);
263 memcpy(fnTableW
, inFnTableW
, tableSize
);
264 /* override this, since we can do it internally anyway */
265 fnTableW
->QuerySecurityPackageInfoW
=
266 QuerySecurityPackageInfoW
;
270 /* functions with thunks */
271 if (inFnTableA
->AcquireCredentialsHandleA
)
272 fnTableW
->AcquireCredentialsHandleW
=
273 thunk_AcquireCredentialsHandleW
;
274 if (inFnTableA
->InitializeSecurityContextA
)
275 fnTableW
->InitializeSecurityContextW
=
276 thunk_InitializeSecurityContextW
;
277 if (inFnTableA
->ImportSecurityContextA
)
278 fnTableW
->ImportSecurityContextW
=
279 thunk_ImportSecurityContextW
;
280 if (inFnTableA
->AddCredentialsA
)
281 fnTableW
->AddCredentialsW
=
282 thunk_AddCredentialsW
;
283 if (inFnTableA
->QueryCredentialsAttributesA
)
284 fnTableW
->QueryCredentialsAttributesW
=
285 thunk_QueryCredentialsAttributesW
;
286 if (inFnTableA
->QueryContextAttributesA
)
287 fnTableW
->QueryContextAttributesW
=
288 thunk_QueryContextAttributesW
;
289 if (inFnTableA
->SetContextAttributesA
)
290 fnTableW
->SetContextAttributesW
=
291 thunk_SetContextAttributesW
;
292 /* this can't be thunked, there's no extra param to know which
293 * package to forward to */
294 fnTableW
->EnumerateSecurityPackagesW
= NULL
;
295 /* functions with no thunks needed */
296 fnTableW
->AcceptSecurityContext
= inFnTableA
->AcceptSecurityContext
;
297 fnTableW
->CompleteAuthToken
= inFnTableA
->CompleteAuthToken
;
298 fnTableW
->DeleteSecurityContext
= inFnTableA
->DeleteSecurityContext
;
299 fnTableW
->ImpersonateSecurityContext
=
300 inFnTableA
->ImpersonateSecurityContext
;
301 fnTableW
->RevertSecurityContext
= inFnTableA
->RevertSecurityContext
;
302 fnTableW
->MakeSignature
= inFnTableA
->MakeSignature
;
303 fnTableW
->VerifySignature
= inFnTableA
->VerifySignature
;
304 fnTableW
->FreeContextBuffer
= inFnTableA
->FreeContextBuffer
;
305 fnTableW
->QuerySecurityPackageInfoW
=
306 QuerySecurityPackageInfoW
;
307 fnTableW
->ExportSecurityContext
=
308 inFnTableA
->ExportSecurityContext
;
309 fnTableW
->QuerySecurityContextToken
=
310 inFnTableA
->QuerySecurityContextToken
;
311 fnTableW
->EncryptMessage
= inFnTableA
->EncryptMessage
;
312 fnTableW
->DecryptMessage
= inFnTableA
->DecryptMessage
;
317 static WCHAR
*strdupAW( const char *str
)
322 int len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
323 if ((ret
= malloc( len
* sizeof(WCHAR
) ))) MultiByteToWideChar( CP_ACP
, 0, str
, -1, ret
, len
);
328 static void _copyPackageInfo(PSecPkgInfoW info
, const SecPkgInfoA
*inInfoA
,
329 const SecPkgInfoW
*inInfoW
)
331 if (info
&& (inInfoA
|| inInfoW
))
333 /* odd, I know, but up until Name and Comment the structures are
336 memcpy(info
, inInfoW
? inInfoW
: (const SecPkgInfoW
*)inInfoA
, sizeof(*info
));
339 info
->Name
= wcsdup(inInfoW
->Name
);
340 info
->Comment
= wcsdup(inInfoW
->Comment
);
344 info
->Name
= strdupAW(inInfoA
->Name
);
345 info
->Comment
= strdupAW(inInfoA
->Comment
);
350 SecureProvider
*SECUR32_addProvider(const SecurityFunctionTableA
*fnTableA
,
351 const SecurityFunctionTableW
*fnTableW
, PCWSTR moduleName
)
355 EnterCriticalSection(&cs
);
359 if (!(providerTable
= malloc(sizeof(*providerTable
))))
361 LeaveCriticalSection(&cs
);
365 list_init(&providerTable
->table
);
368 if (!(ret
= malloc(sizeof(*ret
))))
370 LeaveCriticalSection(&cs
);
374 list_add_tail(&providerTable
->table
, &ret
->entry
);
377 if (fnTableA
|| fnTableW
)
379 ret
->moduleName
= wcsdup(moduleName
);
380 _makeFnTableA(&ret
->fnTableA
, fnTableA
, fnTableW
);
381 _makeFnTableW(&ret
->fnTableW
, fnTableA
, fnTableW
);
382 ret
->loaded
= !moduleName
;
386 ret
->moduleName
= wcsdup(moduleName
);
390 LeaveCriticalSection(&cs
);
394 void SECUR32_addPackages(SecureProvider
*provider
, ULONG toAdd
,
395 const SecPkgInfoA
*infoA
, const SecPkgInfoW
*infoW
)
400 assert(infoA
|| infoW
);
402 EnterCriticalSection(&cs
);
406 if (!(packageTable
= malloc(sizeof(*packageTable
))))
408 LeaveCriticalSection(&cs
);
412 packageTable
->numPackages
= 0;
413 list_init(&packageTable
->table
);
416 for (i
= 0; i
< toAdd
; i
++)
418 SecurePackage
*package
;
420 if (!(package
= malloc(sizeof(*package
)))) continue;
422 list_add_tail(&packageTable
->table
, &package
->entry
);
424 package
->provider
= provider
;
425 _copyPackageInfo(&package
->infoW
, infoA
? &infoA
[i
] : NULL
, infoW
? &infoW
[i
] : NULL
);
427 packageTable
->numPackages
+= toAdd
;
429 LeaveCriticalSection(&cs
);
432 static void _tryLoadProvider(PWSTR moduleName
)
434 HMODULE lib
= LoadLibraryW(moduleName
);
438 INIT_SECURITY_INTERFACE_W pInitSecurityInterfaceW
=
439 (INIT_SECURITY_INTERFACE_W
)GetProcAddress(lib
,
440 SECURITY_ENTRYPOINT_ANSIW
);
441 INIT_SECURITY_INTERFACE_A pInitSecurityInterfaceA
=
442 (INIT_SECURITY_INTERFACE_A
)GetProcAddress(lib
,
443 SECURITY_ENTRYPOINT_ANSIA
);
445 TRACE("loaded %s, InitSecurityInterfaceA is %p, InitSecurityInterfaceW is %p\n",
446 debugstr_w(moduleName
), pInitSecurityInterfaceA
,
447 pInitSecurityInterfaceW
);
448 if (pInitSecurityInterfaceW
|| pInitSecurityInterfaceA
)
450 PSecurityFunctionTableA fnTableA
= NULL
;
451 PSecurityFunctionTableW fnTableW
= NULL
;
453 PSecPkgInfoA infoA
= NULL
;
454 PSecPkgInfoW infoW
= NULL
;
455 SECURITY_STATUS ret
= SEC_E_OK
;
457 if (pInitSecurityInterfaceA
)
458 fnTableA
= pInitSecurityInterfaceA();
459 if (pInitSecurityInterfaceW
)
460 fnTableW
= pInitSecurityInterfaceW();
461 if (fnTableW
&& fnTableW
->EnumerateSecurityPackagesW
)
463 if (fnTableW
!= &securityFunctionTableW
)
464 ret
= fnTableW
->EnumerateSecurityPackagesW(&toAdd
, &infoW
);
466 TRACE("%s has built-in providers, skip adding\n", debugstr_w(moduleName
));
468 else if (fnTableA
&& fnTableA
->EnumerateSecurityPackagesA
)
470 if (fnTableA
!= &securityFunctionTableA
)
471 ret
= fnTableA
->EnumerateSecurityPackagesA(&toAdd
, &infoA
);
473 TRACE("%s has built-in providers, skip adding\n", debugstr_w(moduleName
));
475 if (ret
== SEC_E_OK
&& toAdd
> 0 && (infoW
|| infoA
))
477 SecureProvider
*provider
= SECUR32_addProvider(NULL
, NULL
,
481 SECUR32_addPackages(provider
, toAdd
, infoA
, infoW
);
483 fnTableW
->FreeContextBuffer(infoW
);
485 fnTableA
->FreeContextBuffer(infoA
);
491 WARN("failed to load %s\n", debugstr_w(moduleName
));
494 static void SECUR32_initializeProviders(void)
500 /* First load built-in providers */
501 SECUR32_initSchannelSP();
502 /* Load SSP/AP packages (Kerberos and others) */
503 load_auth_packages();
504 /* Load the Negotiate provider last so apps stumble over the working NTLM
505 * provider first. Attempting to fix bug #16905 while keeping the
506 * application reported on wine-users on 2006-09-12 working. */
507 SECUR32_initNegotiateSP();
508 /* Now load providers from registry */
509 apiRet
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, L
"SYSTEM\\CurrentControlSet\\Control\\SecurityProviders", 0,
511 if (apiRet
== ERROR_SUCCESS
)
513 WCHAR securityPkgNames
[MAX_PATH
]; /* arbitrary len */
514 DWORD size
= sizeof(securityPkgNames
), type
;
516 apiRet
= RegQueryValueExW(key
, L
"SecurityProviders", NULL
, &type
, (PBYTE
)securityPkgNames
, &size
);
517 if (apiRet
== ERROR_SUCCESS
&& type
== REG_SZ
)
521 size
= size
/ sizeof(WCHAR
);
522 for (ptr
= securityPkgNames
;
523 ptr
< securityPkgNames
+ size
; )
527 for (comma
= ptr
; *comma
&& *comma
!= ','; comma
++)
531 for (; *ptr
&& iswspace(*ptr
) && ptr
< securityPkgNames
+ size
;
535 _tryLoadProvider(ptr
);
536 ptr
+= lstrlenW(ptr
) + 1;
543 SecurePackage
*SECUR32_findPackageW(PCWSTR packageName
)
545 SecurePackage
*ret
= NULL
;
546 BOOL matched
= FALSE
;
548 if (packageTable
&& packageName
)
550 LIST_FOR_EACH_ENTRY(ret
, &packageTable
->table
, SecurePackage
, entry
)
552 matched
= !lstrcmpiW(ret
->infoW
.Name
, packageName
);
555 if (!matched
) return NULL
;
557 if (ret
->provider
&& !ret
->provider
->loaded
)
559 ret
->provider
->lib
= LoadLibraryW(ret
->provider
->moduleName
);
560 if (ret
->provider
->lib
)
562 INIT_SECURITY_INTERFACE_W pInitSecurityInterfaceW
=
563 (INIT_SECURITY_INTERFACE_W
)GetProcAddress(ret
->provider
->lib
,
564 SECURITY_ENTRYPOINT_ANSIW
);
565 INIT_SECURITY_INTERFACE_A pInitSecurityInterfaceA
=
566 (INIT_SECURITY_INTERFACE_A
)GetProcAddress(ret
->provider
->lib
,
567 SECURITY_ENTRYPOINT_ANSIA
);
568 PSecurityFunctionTableA fnTableA
= NULL
;
569 PSecurityFunctionTableW fnTableW
= NULL
;
571 if (pInitSecurityInterfaceA
)
572 fnTableA
= pInitSecurityInterfaceA();
573 if (pInitSecurityInterfaceW
)
574 fnTableW
= pInitSecurityInterfaceW();
575 /* don't update built-in SecurityFunctionTable */
576 if (fnTableA
!= &securityFunctionTableA
)
577 _makeFnTableA(&ret
->provider
->fnTableA
, fnTableA
, fnTableW
);
578 if (fnTableW
!= &securityFunctionTableW
)
579 _makeFnTableW(&ret
->provider
->fnTableW
, fnTableA
, fnTableW
);
580 ret
->provider
->loaded
= TRUE
;
589 SecurePackage
*SECUR32_findPackageA(PCSTR packageName
)
593 if (packageTable
&& packageName
)
595 UNICODE_STRING package
;
597 RtlCreateUnicodeStringFromAsciiz(&package
, packageName
);
598 ret
= SECUR32_findPackageW(package
.Buffer
);
599 RtlFreeUnicodeString(&package
);
606 static void SECUR32_freeProviders(void)
609 EnterCriticalSection(&cs
);
611 SECUR32_deinitSchannelSP();
615 SecurePackage
*package
, *package_next
;
616 LIST_FOR_EACH_ENTRY_SAFE(package
, package_next
, &packageTable
->table
,
617 SecurePackage
, entry
)
619 free(package
->infoW
.Name
);
620 free(package
->infoW
.Comment
);
630 SecureProvider
*provider
, *provider_next
;
631 LIST_FOR_EACH_ENTRY_SAFE(provider
, provider_next
, &providerTable
->table
, SecureProvider
, entry
)
633 free(provider
->moduleName
);
634 if (provider
->lib
) FreeLibrary(provider
->lib
);
639 providerTable
= NULL
;
642 LeaveCriticalSection(&cs
);
643 DeleteCriticalSection(&cs
);
646 /***********************************************************************
647 * FreeContextBuffer (SECUR32.@)
649 * Doh--if pv was allocated by a crypto package, this may not be correct.
650 * The sample ssp seems to use LocalAlloc/LocalFee, but there doesn't seem to
651 * be any guarantee, nor is there an alloc function in secur32.
653 SECURITY_STATUS WINAPI
FreeContextBuffer( void *pv
)
655 RtlFreeHeap( GetProcessHeap(), 0, pv
);
659 /***********************************************************************
660 * EnumerateSecurityPackagesW (SECUR32.@)
662 SECURITY_STATUS WINAPI
EnumerateSecurityPackagesW(PULONG pcPackages
,
663 PSecPkgInfoW
*ppPackageInfo
)
665 SECURITY_STATUS ret
= SEC_E_OK
;
667 TRACE("(%p, %p)\n", pcPackages
, ppPackageInfo
);
669 /* windows just crashes if pcPackages or ppPackageInfo is NULL, so will I */
671 EnterCriticalSection(&cs
);
674 SecurePackage
*package
;
677 bytesNeeded
= packageTable
->numPackages
* sizeof(SecPkgInfoW
);
678 LIST_FOR_EACH_ENTRY(package
, &packageTable
->table
, SecurePackage
, entry
)
680 if (package
->infoW
.Name
)
681 bytesNeeded
+= (lstrlenW(package
->infoW
.Name
) + 1) * sizeof(WCHAR
);
682 if (package
->infoW
.Comment
)
683 bytesNeeded
+= (lstrlenW(package
->infoW
.Comment
) + 1) * sizeof(WCHAR
);
687 /* freed with FeeContextBuffer */
688 if ((*ppPackageInfo
= RtlAllocateHeap(GetProcessHeap(), 0, bytesNeeded
)))
693 *pcPackages
= packageTable
->numPackages
;
694 nextString
= (PWSTR
)((PBYTE
)*ppPackageInfo
+
695 packageTable
->numPackages
* sizeof(SecPkgInfoW
));
696 LIST_FOR_EACH_ENTRY(package
, &packageTable
->table
, SecurePackage
, entry
)
698 PSecPkgInfoW pkgInfo
= *ppPackageInfo
+ i
++;
700 *pkgInfo
= package
->infoW
;
701 if (package
->infoW
.Name
)
703 TRACE("Name[%ld] = %s\n", i
- 1, debugstr_w(package
->infoW
.Name
));
704 pkgInfo
->Name
= nextString
;
705 lstrcpyW(nextString
, package
->infoW
.Name
);
706 nextString
+= lstrlenW(nextString
) + 1;
709 pkgInfo
->Name
= NULL
;
710 if (package
->infoW
.Comment
)
712 TRACE("Comment[%ld] = %s\n", i
- 1, debugstr_w(package
->infoW
.Comment
));
713 pkgInfo
->Comment
= nextString
;
714 lstrcpyW(nextString
, package
->infoW
.Comment
);
715 nextString
+= lstrlenW(nextString
) + 1;
718 pkgInfo
->Comment
= NULL
;
722 ret
= SEC_E_INSUFFICIENT_MEMORY
;
725 LeaveCriticalSection(&cs
);
726 TRACE("<-- 0x%08lx\n", ret
);
730 /* Converts info (which is assumed to be an array of cPackages SecPkgInfoW
731 * structures) into an array of SecPkgInfoA structures, which it returns.
733 static PSecPkgInfoA
thunk_PSecPkgInfoWToA(ULONG cPackages
,
734 const SecPkgInfoW
*info
)
740 size_t bytesNeeded
= cPackages
* sizeof(SecPkgInfoA
);
743 for (i
= 0; i
< cPackages
; i
++)
746 bytesNeeded
+= WideCharToMultiByte(CP_ACP
, 0, info
[i
].Name
,
747 -1, NULL
, 0, NULL
, NULL
);
749 bytesNeeded
+= WideCharToMultiByte(CP_ACP
, 0, info
[i
].Comment
,
750 -1, NULL
, 0, NULL
, NULL
);
752 /* freed with FreeContextBuffer */
753 if ((ret
= RtlAllocateHeap(GetProcessHeap(), 0, bytesNeeded
)))
757 nextString
= (PSTR
)((PBYTE
)ret
+ cPackages
* sizeof(SecPkgInfoA
));
758 for (i
= 0; i
< cPackages
; i
++)
760 PSecPkgInfoA pkgInfo
= ret
+ i
;
763 memcpy(pkgInfo
, &info
[i
], sizeof(SecPkgInfoA
));
766 pkgInfo
->Name
= nextString
;
767 /* just repeat back to WideCharToMultiByte how many bytes
768 * it requires, since we asked it earlier
770 bytes
= WideCharToMultiByte(CP_ACP
, 0, info
[i
].Name
, -1,
771 NULL
, 0, NULL
, NULL
);
772 WideCharToMultiByte(CP_ACP
, 0, info
[i
].Name
, -1,
773 pkgInfo
->Name
, bytes
, NULL
, NULL
);
774 nextString
+= lstrlenA(nextString
) + 1;
777 pkgInfo
->Name
= NULL
;
780 pkgInfo
->Comment
= nextString
;
781 /* just repeat back to WideCharToMultiByte how many bytes
782 * it requires, since we asked it earlier
784 bytes
= WideCharToMultiByte(CP_ACP
, 0, info
[i
].Comment
, -1,
785 NULL
, 0, NULL
, NULL
);
786 WideCharToMultiByte(CP_ACP
, 0, info
[i
].Comment
, -1,
787 pkgInfo
->Comment
, bytes
, NULL
, NULL
);
788 nextString
+= lstrlenA(nextString
) + 1;
791 pkgInfo
->Comment
= NULL
;
800 /***********************************************************************
801 * EnumerateSecurityPackagesA (SECUR32.@)
803 SECURITY_STATUS WINAPI
EnumerateSecurityPackagesA(PULONG pcPackages
,
804 PSecPkgInfoA
*ppPackageInfo
)
809 ret
= EnumerateSecurityPackagesW(pcPackages
, &info
);
810 if (ret
== SEC_E_OK
&& *pcPackages
&& info
)
812 *ppPackageInfo
= thunk_PSecPkgInfoWToA(*pcPackages
, info
);
813 if (*pcPackages
&& !*ppPackageInfo
)
816 ret
= SEC_E_INSUFFICIENT_MEMORY
;
818 FreeContextBuffer(info
);
823 /***********************************************************************
824 * GetComputerObjectNameA (SECUR32.@)
826 * Get the local computer's name using the format specified.
829 * NameFormat [I] The format for the name.
830 * lpNameBuffer [O] Pointer to buffer to receive the name.
831 * nSize [I/O] Size in characters of buffer.
834 * TRUE If the name was written to lpNameBuffer.
835 * FALSE If the name couldn't be written.
838 * If lpNameBuffer is NULL, then the size of the buffer needed to hold the
839 * name will be returned in *nSize.
841 * nSize returns the number of characters written when lpNameBuffer is not
842 * NULL or the size of the buffer needed to hold the name when the buffer
843 * is too short or lpNameBuffer is NULL.
845 * It the buffer is too short, ERROR_INSUFFICIENT_BUFFER error will be set.
847 BOOLEAN WINAPI
GetComputerObjectNameA(
848 EXTENDED_NAME_FORMAT NameFormat
, LPSTR lpNameBuffer
, PULONG nSize
)
851 LPWSTR bufferW
= NULL
;
852 ULONG sizeW
= *nSize
;
854 TRACE("(%d %p %p)\n", NameFormat
, lpNameBuffer
, nSize
);
857 if (!(bufferW
= malloc(sizeW
* sizeof(WCHAR
)))) {
858 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
862 rc
= GetComputerObjectNameW(NameFormat
, bufferW
, &sizeW
);
864 ULONG len
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
865 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, lpNameBuffer
, *nSize
, NULL
, NULL
);
874 /***********************************************************************
875 * GetComputerObjectNameW (SECUR32.@)
877 BOOLEAN WINAPI
GetComputerObjectNameW(
878 EXTENDED_NAME_FORMAT NameFormat
, LPWSTR lpNameBuffer
, PULONG nSize
)
880 LSA_HANDLE policyHandle
;
881 LSA_OBJECT_ATTRIBUTES objectAttributes
;
882 PPOLICY_DNS_DOMAIN_INFO domainInfo
;
886 TRACE("(%d %p %p)\n", NameFormat
, lpNameBuffer
, nSize
);
888 if (NameFormat
== NameUnknown
)
890 SetLastError(ERROR_INVALID_PARAMETER
);
894 ZeroMemory(&objectAttributes
, sizeof(objectAttributes
));
895 objectAttributes
.Length
= sizeof(objectAttributes
);
897 ntStatus
= LsaOpenPolicy(NULL
, &objectAttributes
,
898 POLICY_VIEW_LOCAL_INFORMATION
,
900 if (ntStatus
!= STATUS_SUCCESS
)
902 SetLastError(LsaNtStatusToWinError(ntStatus
));
903 WARN("LsaOpenPolicy failed with NT status %lu\n", GetLastError());
907 ntStatus
= LsaQueryInformationPolicy(policyHandle
,
908 PolicyDnsDomainInformation
,
909 (PVOID
*)&domainInfo
);
910 if (ntStatus
!= STATUS_SUCCESS
)
912 SetLastError(LsaNtStatusToWinError(ntStatus
));
913 WARN("LsaQueryInformationPolicy failed with NT status %lu\n",
915 LsaClose(policyHandle
);
923 case NameSamCompatible
:
925 WCHAR name
[MAX_COMPUTERNAME_LENGTH
+ 1];
926 DWORD size
= ARRAY_SIZE(name
);
927 if (GetComputerNameW(name
, &size
))
929 DWORD len
= domainInfo
->Name
.Length
+ size
+ 3;
930 if (lpNameBuffer
&& *nSize
>= len
)
932 if (domainInfo
->Name
.Buffer
)
934 lstrcpyW(lpNameBuffer
, domainInfo
->Name
.Buffer
);
935 lstrcatW(lpNameBuffer
, L
"\\");
939 lstrcatW(lpNameBuffer
, name
);
940 lstrcatW(lpNameBuffer
, L
"$");
943 else /* just requesting length required */
946 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
952 SetLastError(ERROR_INTERNAL_ERROR
);
957 case NameFullyQualifiedDN
:
959 WCHAR name
[MAX_COMPUTERNAME_LENGTH
+ 1];
963 size
= ARRAY_SIZE(name
);
964 if (!GetComputerNameW(name
, &size
))
970 len
= wcslen(L
"CN=") + size
+ 1 + wcslen(L
"CN=Computers") + 1 + wcslen(L
"DC=");
971 if (domainInfo
->DnsDomainName
.Buffer
)
973 suffix
= wcsrchr(domainInfo
->DnsDomainName
.Buffer
, '.');
977 len
+= 1 + wcslen(L
"DC=") + wcslen(suffix
);
979 len
+= wcslen(domainInfo
->DnsDomainName
.Buffer
);
984 if (lpNameBuffer
&& *nSize
> len
)
986 lstrcpyW(lpNameBuffer
, L
"CN=");
987 lstrcatW(lpNameBuffer
, name
);
988 lstrcatW(lpNameBuffer
, L
",");
989 lstrcatW(lpNameBuffer
, L
"CN=Computers");
990 if (domainInfo
->DnsDomainName
.Buffer
)
992 lstrcatW(lpNameBuffer
, L
",");
993 lstrcatW(lpNameBuffer
, L
"DC=");
994 lstrcatW(lpNameBuffer
, domainInfo
->DnsDomainName
.Buffer
);
997 lstrcatW(lpNameBuffer
, L
",");
998 lstrcatW(lpNameBuffer
, L
"DC=");
999 lstrcatW(lpNameBuffer
, suffix
);
1004 else /* just requesting length required */
1006 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1015 case NameUserPrincipal
:
1016 case NameCanonicalEx
:
1017 case NameServicePrincipal
:
1019 FIXME("NameFormat %d not implemented\n", NameFormat
);
1020 SetLastError(ERROR_CANT_ACCESS_DOMAIN_INFO
);
1024 SetLastError(ERROR_INVALID_PARAMETER
);
1030 SetLastError(ERROR_CANT_ACCESS_DOMAIN_INFO
);
1034 LsaFreeMemory(domainInfo
);
1035 LsaClose(policyHandle
);
1040 SECURITY_STATUS WINAPI
AddSecurityPackageA(LPSTR name
, SECURITY_PACKAGE_OPTIONS
*options
)
1042 FIXME("(%s %p)\n", debugstr_a(name
), options
);
1046 SECURITY_STATUS WINAPI
AddSecurityPackageW(LPWSTR name
, SECURITY_PACKAGE_OPTIONS
*options
)
1048 FIXME("(%s %p)\n", debugstr_w(name
), options
);
1052 SECURITY_STATUS WINAPI
DeleteSecurityPackageA(LPSTR name
)
1054 FIXME("(%s)\n", debugstr_a(name
));
1058 SECURITY_STATUS WINAPI
DeleteSecurityPackageW(LPWSTR name
)
1060 FIXME("(%s)\n", debugstr_w(name
));
1064 /***********************************************************************
1065 * GetUserNameExA (SECUR32.@)
1067 BOOLEAN WINAPI
GetUserNameExA(
1068 EXTENDED_NAME_FORMAT NameFormat
, LPSTR lpNameBuffer
, PULONG nSize
)
1071 LPWSTR bufferW
= NULL
;
1072 ULONG sizeW
= *nSize
;
1073 TRACE("(%d %p %p)\n", NameFormat
, lpNameBuffer
, nSize
);
1075 bufferW
= malloc(sizeW
* sizeof(WCHAR
));
1076 if (bufferW
== NULL
) {
1077 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1081 rc
= GetUserNameExW(NameFormat
, bufferW
, &sizeW
);
1083 ULONG len
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
1086 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, lpNameBuffer
, *nSize
, NULL
, NULL
);
1093 SetLastError(ERROR_MORE_DATA
);
1102 BOOLEAN WINAPI
GetUserNameExW(
1103 EXTENDED_NAME_FORMAT NameFormat
, LPWSTR lpNameBuffer
, PULONG nSize
)
1105 TRACE("(%d %p %p)\n", NameFormat
, lpNameBuffer
, nSize
);
1109 case NameSamCompatible
:
1111 WCHAR samname
[UNLEN
+ 1 + MAX_COMPUTERNAME_LENGTH
+ 1];
1115 /* This assumes the current user is always a local account */
1116 len
= MAX_COMPUTERNAME_LENGTH
+ 1;
1117 if (GetComputerNameW(samname
, &len
))
1119 out
= samname
+ lstrlenW(samname
);
1122 if (GetUserNameW(out
, &len
))
1124 if (lstrlenW(samname
) < *nSize
)
1126 lstrcpyW(lpNameBuffer
, samname
);
1127 *nSize
= lstrlenW(samname
);
1131 SetLastError(ERROR_MORE_DATA
);
1132 *nSize
= lstrlenW(samname
) + 1;
1139 case NameFullyQualifiedDN
:
1143 case NameUserPrincipal
:
1144 case NameCanonicalEx
:
1145 case NameServicePrincipal
:
1147 SetLastError(ERROR_NONE_MAPPED
);
1151 SetLastError(ERROR_INVALID_PARAMETER
);
1156 BOOLEAN WINAPI
TranslateNameA(
1157 LPCSTR lpAccountName
, EXTENDED_NAME_FORMAT AccountNameFormat
,
1158 EXTENDED_NAME_FORMAT DesiredNameFormat
, LPSTR lpTranslatedName
,
1161 FIXME("%p %d %d %p %p\n", lpAccountName
, AccountNameFormat
,
1162 DesiredNameFormat
, lpTranslatedName
, nSize
);
1166 BOOLEAN WINAPI
TranslateNameW(
1167 LPCWSTR lpAccountName
, EXTENDED_NAME_FORMAT AccountNameFormat
,
1168 EXTENDED_NAME_FORMAT DesiredNameFormat
, LPWSTR lpTranslatedName
,
1171 FIXME("%p %d %d %p %p\n", lpAccountName
, AccountNameFormat
,
1172 DesiredNameFormat
, lpTranslatedName
, nSize
);
1176 /***********************************************************************
1177 * DllMain (SECUR32.0)
1179 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD reason
, LPVOID reserved
)
1183 case DLL_PROCESS_ATTACH
:
1184 DisableThreadLibraryCalls(hinstDLL
);
1185 SECUR32_initializeProviders();
1187 case DLL_PROCESS_DETACH
:
1188 if (reserved
) break;
1189 SECUR32_freeProviders();