xref: /aoo41x/main/sal/osl/w32/security.c (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include "system.h"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <osl/security.h>
32*cdf0e10cSrcweir #include <osl/diagnose.h>
33*cdf0e10cSrcweir #include <osl/thread.h>
34*cdf0e10cSrcweir #include <osl/file.h>
35*cdf0e10cSrcweir #include <systools/win32/uwinapi.h>
36*cdf0e10cSrcweir #include "secimpl.h"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir /*****************************************************************************/
39*cdf0e10cSrcweir /* Data Type Definition */
40*cdf0e10cSrcweir /*****************************************************************************/
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir /* Data for use in (un)LoadProfile Functions */
44*cdf0e10cSrcweir /* Declarations based on USERENV.H for Windows 2000 Beta 2 */
45*cdf0e10cSrcweir #define PI_NOUI         0x00000001   // Prevents displaying of messages
46*cdf0e10cSrcweir #define PI_APPLYPOLICY  0x00000002   // Apply NT4 style policy
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir typedef struct _PROFILEINFOW {
49*cdf0e10cSrcweir   DWORD    dwSize;          // Must be set to sizeof(PROFILEINFO)
50*cdf0e10cSrcweir   DWORD    dwFlags;         // See flags above
51*cdf0e10cSrcweir   LPWSTR   lpUserName;      // User name (required)
52*cdf0e10cSrcweir   LPWSTR   lpProfilePath;   // Roaming profile path
53*cdf0e10cSrcweir   LPWSTR   lpDefaultPath;   // Default user profile path
54*cdf0e10cSrcweir   LPWSTR   lpServerName;    // Validating DC name in netbios format
55*cdf0e10cSrcweir   LPWSTR   lpPolicyPath;    // Path to the NT4 style policy file
56*cdf0e10cSrcweir   HANDLE   hProfile;        // Registry key handle - filled by function
57*cdf0e10cSrcweir } PROFILEINFOW, FAR * LPPROFILEINFOW;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir /* Typedefs for function pointers in USERENV.DLL */
60*cdf0e10cSrcweir typedef BOOL (STDMETHODCALLTYPE FAR * LPFNLOADUSERPROFILE) (
61*cdf0e10cSrcweir   HANDLE hToken,
62*cdf0e10cSrcweir   LPPROFILEINFOW lpProfileInfo
63*cdf0e10cSrcweir );
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir typedef BOOL (STDMETHODCALLTYPE FAR * LPFNUNLOADUSERPROFILE) (
66*cdf0e10cSrcweir   HANDLE hToken,
67*cdf0e10cSrcweir   HANDLE hProfile
68*cdf0e10cSrcweir );
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETUSERPROFILEDIR) (
71*cdf0e10cSrcweir   HANDLE hToken,
72*cdf0e10cSrcweir   LPTSTR lpProfileDir,
73*cdf0e10cSrcweir   LPDWORD lpcchSize
74*cdf0e10cSrcweir );
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir /* To get an impersonation token we need to create an impersonation
77*cdf0e10cSrcweir    duplicate so every access token has to be created with duplicate
78*cdf0e10cSrcweir    access rights */
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir #define TOKEN_DUP_QUERY	(TOKEN_QUERY|TOKEN_DUPLICATE)
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir /*****************************************************************************/
83*cdf0e10cSrcweir /* Static Module Function Declarations */
84*cdf0e10cSrcweir /*****************************************************************************/
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir static sal_Bool isWNT(void);
87*cdf0e10cSrcweir static sal_Bool GetSpecialFolder(rtl_uString **strPath,int nFolder);
88*cdf0e10cSrcweir static BOOL Privilege(LPTSTR pszPrivilege, BOOL bEnable);
89*cdf0e10cSrcweir static sal_Bool SAL_CALL getUserNameImpl(oslSecurity Security, rtl_uString **strName, sal_Bool bIncludeDomain);
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir /*****************************************************************************/
92*cdf0e10cSrcweir /* Exported Module Functions */
93*cdf0e10cSrcweir /*****************************************************************************/
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir oslSecurity SAL_CALL osl_getCurrentSecurity(void)
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir     oslSecurityImpl* pSecImpl = malloc(sizeof(oslSecurityImpl));
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 	pSecImpl->m_pNetResource = NULL;
100*cdf0e10cSrcweir 	pSecImpl->m_User[0] = '\0';
101*cdf0e10cSrcweir 	pSecImpl->m_hToken = NULL;
102*cdf0e10cSrcweir 	pSecImpl->m_hProfile = NULL;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	return ((oslSecurity)pSecImpl);
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir oslSecurityError SAL_CALL osl_loginUser( rtl_uString *strUserName, rtl_uString *strPasswd, oslSecurity *pSecurity )
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir 	oslSecurityError ret;
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 	if (!isWNT())
112*cdf0e10cSrcweir 	{
113*cdf0e10cSrcweir 		*pSecurity = osl_getCurrentSecurity();
114*cdf0e10cSrcweir 		ret = osl_Security_E_None;
115*cdf0e10cSrcweir 	}
116*cdf0e10cSrcweir 	else
117*cdf0e10cSrcweir 	{
118*cdf0e10cSrcweir 		sal_Unicode*	strUser;
119*cdf0e10cSrcweir 		sal_Unicode*	strDomain = _wcsdup(rtl_uString_getStr(strUserName));
120*cdf0e10cSrcweir 		HANDLE	hUserToken;
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 0
123*cdf0e10cSrcweir 		    LUID luid;
124*cdf0e10cSrcweir 		#endif
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 		if (NULL != (strUser = wcschr(strDomain, L'/')))
127*cdf0e10cSrcweir 			*strUser++ = L'\0';
128*cdf0e10cSrcweir 		else
129*cdf0e10cSrcweir 		{
130*cdf0e10cSrcweir 			strUser   = strDomain;
131*cdf0e10cSrcweir 			strDomain = NULL;
132*cdf0e10cSrcweir 		}
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 		// this process must have the right: 'act as a part of operatingsystem'
135*cdf0e10cSrcweir 		OSL_ASSERT(LookupPrivilegeValue(NULL, SE_TCB_NAME, &luid));
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 		if (LogonUserW(strUser, strDomain ? strDomain : L"", rtl_uString_getStr(strPasswd),
138*cdf0e10cSrcweir 					  LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
139*cdf0e10cSrcweir 		 			  &hUserToken))
140*cdf0e10cSrcweir 		{
141*cdf0e10cSrcweir 			oslSecurityImpl* pSecImpl = malloc(sizeof(oslSecurityImpl));
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 			pSecImpl->m_pNetResource = NULL;
144*cdf0e10cSrcweir 			pSecImpl->m_hToken = hUserToken;
145*cdf0e10cSrcweir 			pSecImpl->m_hProfile = NULL;
146*cdf0e10cSrcweir 			wcscpy(pSecImpl->m_User, strUser);
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 			*pSecurity = (oslSecurity)pSecImpl;
149*cdf0e10cSrcweir 			ret = osl_Security_E_None;
150*cdf0e10cSrcweir 		}
151*cdf0e10cSrcweir 		else
152*cdf0e10cSrcweir 			ret = osl_Security_E_UserUnknown;
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 		if (strDomain)
155*cdf0e10cSrcweir 			free(strDomain);
156*cdf0e10cSrcweir 		else
157*cdf0e10cSrcweir 			free(strUser);
158*cdf0e10cSrcweir 	}
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	return ret;
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir oslSecurityError SAL_CALL osl_loginUserOnFileServer(rtl_uString *strUserName,
164*cdf0e10cSrcweir 													rtl_uString *strPasswd,
165*cdf0e10cSrcweir 													rtl_uString *strFileServer,
166*cdf0e10cSrcweir 													oslSecurity *pSecurity)
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir 	oslSecurityError 	ret;
169*cdf0e10cSrcweir 	DWORD 				err;
170*cdf0e10cSrcweir 	NETRESOURCEW 		netResource;
171*cdf0e10cSrcweir 	sal_Unicode* 				remoteName;
172*cdf0e10cSrcweir 	sal_Unicode*				userName;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 	remoteName 	= malloc(rtl_uString_getLength(strFileServer) + rtl_uString_getLength(strUserName) + 4);
175*cdf0e10cSrcweir 	userName 	= malloc(rtl_uString_getLength(strFileServer) + rtl_uString_getLength(strUserName) + 2);
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 	wcscpy(remoteName, L"\\\\");
178*cdf0e10cSrcweir 	wcscat(remoteName, rtl_uString_getStr(strFileServer));
179*cdf0e10cSrcweir 	wcscat(remoteName, L"\\");
180*cdf0e10cSrcweir 	wcscat(remoteName, rtl_uString_getStr(strUserName));
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 	wcscpy(userName, rtl_uString_getStr(strFileServer));
183*cdf0e10cSrcweir 	wcscat(userName, L"\\");
184*cdf0e10cSrcweir 	wcscat(userName, rtl_uString_getStr(strUserName));
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 	netResource.dwScope 		= RESOURCE_GLOBALNET;
187*cdf0e10cSrcweir 	netResource.dwType			= RESOURCETYPE_DISK;
188*cdf0e10cSrcweir 	netResource.dwDisplayType 	= RESOURCEDISPLAYTYPE_SHARE;
189*cdf0e10cSrcweir 	netResource.dwUsage 		= RESOURCEUSAGE_CONNECTABLE;
190*cdf0e10cSrcweir 	netResource.lpLocalName 	= NULL;
191*cdf0e10cSrcweir 	netResource.lpRemoteName 	= remoteName;
192*cdf0e10cSrcweir 	netResource.lpComment 		= NULL;
193*cdf0e10cSrcweir 	netResource.lpProvider 		= NULL;
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 	err = WNetAddConnection2W(&netResource, rtl_uString_getStr(strPasswd), userName, 0);
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 	if ((err == NO_ERROR) || (err == ERROR_ALREADY_ASSIGNED))
198*cdf0e10cSrcweir 	{
199*cdf0e10cSrcweir 	    oslSecurityImpl* pSecImpl = malloc(sizeof(oslSecurityImpl));
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 		pSecImpl->m_pNetResource = malloc(sizeof(NETRESOURCE));
202*cdf0e10cSrcweir 		*pSecImpl->m_pNetResource = netResource;
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 		pSecImpl->m_hToken = NULL;
205*cdf0e10cSrcweir 		pSecImpl->m_hProfile = NULL;
206*cdf0e10cSrcweir 		wcscpy(pSecImpl->m_User, rtl_uString_getStr(strUserName));
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 		*pSecurity = (oslSecurity)pSecImpl;
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 		ret = osl_Security_E_None;
211*cdf0e10cSrcweir 	}
212*cdf0e10cSrcweir 	else
213*cdf0e10cSrcweir 		ret = osl_Security_E_UserUnknown;
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 	free(remoteName);
216*cdf0e10cSrcweir 	free(userName);
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 	return ret;
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir static BOOL	WINAPI CheckTokenMembership_Stub( HANDLE TokenHandle, PSID SidToCheck, PBOOL IsMember )
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir 	typedef BOOL (WINAPI *CheckTokenMembership_PROC)( HANDLE, PSID, PBOOL );
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 	static HMODULE	hModule = NULL;
227*cdf0e10cSrcweir 	static CheckTokenMembership_PROC	pCheckTokenMembership = NULL;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 	if ( !hModule )
230*cdf0e10cSrcweir 	{
231*cdf0e10cSrcweir 		/* SAL is always linked against ADVAPI32 so we can rely on that it is already mapped */
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 		hModule = GetModuleHandleA( "ADVAPI32.DLL" );
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 		pCheckTokenMembership = (CheckTokenMembership_PROC)GetProcAddress( hModule, "CheckTokenMembership" );
236*cdf0e10cSrcweir 	}
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 	if ( pCheckTokenMembership )
239*cdf0e10cSrcweir 		return pCheckTokenMembership( TokenHandle, SidToCheck, IsMember );
240*cdf0e10cSrcweir 	else
241*cdf0e10cSrcweir 	{
242*cdf0e10cSrcweir 		SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
243*cdf0e10cSrcweir 		return FALSE;
244*cdf0e10cSrcweir 	}
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir sal_Bool SAL_CALL osl_isAdministrator(oslSecurity Security)
250*cdf0e10cSrcweir {
251*cdf0e10cSrcweir     if (Security != NULL)
252*cdf0e10cSrcweir 	{
253*cdf0e10cSrcweir 		/* ts: on Window 95 systems any user seems to be an adminstrator */
254*cdf0e10cSrcweir 		if (!isWNT())
255*cdf0e10cSrcweir 		{
256*cdf0e10cSrcweir 			return(sal_True);
257*cdf0e10cSrcweir 		}
258*cdf0e10cSrcweir 		else
259*cdf0e10cSrcweir 		{
260*cdf0e10cSrcweir 			HANDLE						hImpersonationToken = NULL;
261*cdf0e10cSrcweir 			PSID 						psidAdministrators;
262*cdf0e10cSrcweir 			SID_IDENTIFIER_AUTHORITY 	siaNtAuthority = SECURITY_NT_AUTHORITY;
263*cdf0e10cSrcweir 			sal_Bool 					bSuccess = sal_False;
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 			/* If Security contains an access token we need to duplicate it to an impersonation
267*cdf0e10cSrcweir 			   access token. NULL works with CheckTokenMembership() as the current effective
268*cdf0e10cSrcweir 			   impersonation token
269*cdf0e10cSrcweir 			 */
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir 			if ( ((oslSecurityImpl*)Security)->m_hToken )
272*cdf0e10cSrcweir 			{
273*cdf0e10cSrcweir 				if ( !DuplicateToken (((oslSecurityImpl*)Security)->m_hToken, SecurityImpersonation, &hImpersonationToken) )
274*cdf0e10cSrcweir 					return sal_False;
275*cdf0e10cSrcweir 			}
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir 			/* CheckTokenMembership() can be used on W2K and higher (NT4 no longer supported by OOo)
278*cdf0e10cSrcweir 			   and also works on Vista to retrieve the effective user rights. Just checking for
279*cdf0e10cSrcweir 			   membership of Administrators group is not enough on Vista this would require additional
280*cdf0e10cSrcweir 			   complicated checks as described in KB arcticle Q118626: http://support.microsoft.com/kb/118626/en-us
281*cdf0e10cSrcweir 			*/
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 			if (AllocateAndInitializeSid(&siaNtAuthority,
284*cdf0e10cSrcweir 										 2,
285*cdf0e10cSrcweir 			 							 SECURITY_BUILTIN_DOMAIN_RID,
286*cdf0e10cSrcweir 			 							 DOMAIN_ALIAS_RID_ADMINS,
287*cdf0e10cSrcweir 			 							 0, 0, 0, 0, 0, 0,
288*cdf0e10cSrcweir 			 							 &psidAdministrators))
289*cdf0e10cSrcweir 			{
290*cdf0e10cSrcweir 				BOOL	fSuccess = FALSE;
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir 				if ( CheckTokenMembership_Stub( hImpersonationToken, psidAdministrators, &fSuccess ) && fSuccess )
293*cdf0e10cSrcweir 					bSuccess = sal_True;
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 				FreeSid(psidAdministrators);
296*cdf0e10cSrcweir 			}
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir 			if ( hImpersonationToken )
299*cdf0e10cSrcweir 				CloseHandle( hImpersonationToken );
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir 			return (bSuccess);
302*cdf0e10cSrcweir 		}
303*cdf0e10cSrcweir 	}
304*cdf0e10cSrcweir 	else
305*cdf0e10cSrcweir 		return (sal_False);
306*cdf0e10cSrcweir }
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir void SAL_CALL osl_freeSecurityHandle(oslSecurity Security)
310*cdf0e10cSrcweir {
311*cdf0e10cSrcweir 	if (Security)
312*cdf0e10cSrcweir 	{
313*cdf0e10cSrcweir 		oslSecurityImpl *pSecImpl = (oslSecurityImpl*)Security;
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir 		if (pSecImpl->m_pNetResource != NULL)
316*cdf0e10cSrcweir 		{
317*cdf0e10cSrcweir 			WNetCancelConnection2W(pSecImpl->m_pNetResource->lpRemoteName, 0, sal_True);
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir 			free(pSecImpl->m_pNetResource->lpRemoteName);
320*cdf0e10cSrcweir 			free(pSecImpl->m_pNetResource);
321*cdf0e10cSrcweir 		}
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 		if (pSecImpl->m_hToken)
324*cdf0e10cSrcweir 			CloseHandle(pSecImpl->m_hToken);
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir 		if ( pSecImpl->m_hProfile )
327*cdf0e10cSrcweir 			CloseHandle(pSecImpl->m_hProfile);
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir 		free (pSecImpl);
330*cdf0e10cSrcweir 	}
331*cdf0e10cSrcweir }
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **strIdent)
335*cdf0e10cSrcweir {
336*cdf0e10cSrcweir 	if (Security != NULL)
337*cdf0e10cSrcweir 	{
338*cdf0e10cSrcweir 		oslSecurityImpl *pSecImpl = (oslSecurityImpl*)Security;
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir 		HANDLE hAccessToken = pSecImpl->m_hToken;
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir 		if (hAccessToken == NULL)
343*cdf0e10cSrcweir 			OpenProcessToken(GetCurrentProcess(), TOKEN_DUP_QUERY, &hAccessToken);
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir 		if (hAccessToken)
346*cdf0e10cSrcweir 		{
347*cdf0e10cSrcweir 			sal_Char		*Ident;
348*cdf0e10cSrcweir 			DWORD  nInfoBuffer = 512;
349*cdf0e10cSrcweir 			UCHAR* pInfoBuffer = malloc(nInfoBuffer);
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir 			while (!GetTokenInformation(hAccessToken, TokenUser,
353*cdf0e10cSrcweir            							    pInfoBuffer, nInfoBuffer, &nInfoBuffer))
354*cdf0e10cSrcweir 			{
355*cdf0e10cSrcweir 				if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
356*cdf0e10cSrcweir 					pInfoBuffer = realloc(pInfoBuffer, nInfoBuffer);
357*cdf0e10cSrcweir 				else
358*cdf0e10cSrcweir 				{
359*cdf0e10cSrcweir 					free(pInfoBuffer);
360*cdf0e10cSrcweir 					pInfoBuffer = NULL;
361*cdf0e10cSrcweir 					break;
362*cdf0e10cSrcweir 				}
363*cdf0e10cSrcweir 			}
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir 			if (pSecImpl->m_hToken == NULL)
366*cdf0e10cSrcweir 				CloseHandle(hAccessToken);
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir 			if (pInfoBuffer)
369*cdf0e10cSrcweir 			{
370*cdf0e10cSrcweir 				PSID pSid = ((PTOKEN_USER)pInfoBuffer)->User.Sid;
371*cdf0e10cSrcweir 			    PSID_IDENTIFIER_AUTHORITY psia;
372*cdf0e10cSrcweir 			    DWORD dwSubAuthorities;
373*cdf0e10cSrcweir 			    DWORD dwSidRev=SID_REVISION;
374*cdf0e10cSrcweir 			    DWORD dwCounter;
375*cdf0e10cSrcweir 			    DWORD dwSidSize;
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir 			    /* obtain SidIdentifierAuthority */
378*cdf0e10cSrcweir 			    psia=GetSidIdentifierAuthority(pSid);
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir 			    /* obtain sidsubauthority count */
381*cdf0e10cSrcweir 			    dwSubAuthorities=min(*GetSidSubAuthorityCount(pSid), 5);
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir 				/* buffer length: S-SID_REVISION- + identifierauthority- + subauthorities- + NULL */
384*cdf0e10cSrcweir 				Ident=malloc(88*sizeof(sal_Char));
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir 			    /* prepare S-SID_REVISION- */
387*cdf0e10cSrcweir 			    dwSidSize=wsprintf(Ident, TEXT("S-%lu-"), dwSidRev);
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir 			    /* prepare SidIdentifierAuthority */
390*cdf0e10cSrcweir 			    if ((psia->Value[0] != 0) || (psia->Value[1] != 0))
391*cdf0e10cSrcweir 			    {
392*cdf0e10cSrcweir 			        dwSidSize+=wsprintf(Ident + strlen(Ident),
393*cdf0e10cSrcweir 			                    TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
394*cdf0e10cSrcweir 			                    (USHORT)psia->Value[0],
395*cdf0e10cSrcweir 			                    (USHORT)psia->Value[1],
396*cdf0e10cSrcweir 			                    (USHORT)psia->Value[2],
397*cdf0e10cSrcweir 			                    (USHORT)psia->Value[3],
398*cdf0e10cSrcweir 			                    (USHORT)psia->Value[4],
399*cdf0e10cSrcweir 			                    (USHORT)psia->Value[5]);
400*cdf0e10cSrcweir 			    }
401*cdf0e10cSrcweir 			    else
402*cdf0e10cSrcweir 			    {
403*cdf0e10cSrcweir 			        dwSidSize+=wsprintf(Ident + strlen(Ident),
404*cdf0e10cSrcweir 			                    TEXT("%lu"),
405*cdf0e10cSrcweir 			                    (ULONG)(psia->Value[5]      )   +
406*cdf0e10cSrcweir 			                    (ULONG)(psia->Value[4] <<  8)   +
407*cdf0e10cSrcweir 			                    (ULONG)(psia->Value[3] << 16)   +
408*cdf0e10cSrcweir 			                    (ULONG)(psia->Value[2] << 24)   );
409*cdf0e10cSrcweir 			    }
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir 			    /* loop through SidSubAuthorities */
412*cdf0e10cSrcweir 			    for (dwCounter=0; dwCounter < dwSubAuthorities; dwCounter++)
413*cdf0e10cSrcweir 			    {
414*cdf0e10cSrcweir 			        dwSidSize+=wsprintf(Ident + dwSidSize, TEXT("-%lu"),
415*cdf0e10cSrcweir 			                    *GetSidSubAuthority(pSid, dwCounter) );
416*cdf0e10cSrcweir 			    }
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir 				rtl_uString_newFromAscii( strIdent, Ident );
419*cdf0e10cSrcweir 
420*cdf0e10cSrcweir 				free(pInfoBuffer);
421*cdf0e10cSrcweir 				free(Ident);
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir 				return (sal_True);
424*cdf0e10cSrcweir 			}
425*cdf0e10cSrcweir 		}
426*cdf0e10cSrcweir 		else
427*cdf0e10cSrcweir 		{
428*cdf0e10cSrcweir 			DWORD needed=0;
429*cdf0e10cSrcweir 			sal_Unicode		*Ident;
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir 			WNetGetUserA(NULL, NULL, &needed);
432*cdf0e10cSrcweir 			needed = max( 16 , needed );
433*cdf0e10cSrcweir 			Ident=malloc(needed*sizeof(sal_Unicode));
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir 			if (WNetGetUserW(NULL, Ident, &needed) != NO_ERROR)
436*cdf0e10cSrcweir 			{
437*cdf0e10cSrcweir 				wcscpy(Ident, L"unknown");
438*cdf0e10cSrcweir 				Ident[7] = L'\0';
439*cdf0e10cSrcweir 			}
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir 			rtl_uString_newFromStr( strIdent, Ident);
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir 			free(Ident);
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir 			return sal_True;
446*cdf0e10cSrcweir 		}
447*cdf0e10cSrcweir 	}
448*cdf0e10cSrcweir 
449*cdf0e10cSrcweir 	return sal_False;
450*cdf0e10cSrcweir }
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir 
454*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getUserName(oslSecurity Security, rtl_uString **strName)
455*cdf0e10cSrcweir {
456*cdf0e10cSrcweir 	return getUserNameImpl(Security, strName, sal_True);
457*cdf0e10cSrcweir }
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory)
461*cdf0e10cSrcweir {
462*cdf0e10cSrcweir 	rtl_uString	*ustrSysDir = NULL;
463*cdf0e10cSrcweir 	sal_Bool	bSuccess = sal_False;
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir     if (Security != NULL)
466*cdf0e10cSrcweir 	{
467*cdf0e10cSrcweir 		oslSecurityImpl *pSecImpl = (oslSecurityImpl*)Security;
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir 		if (pSecImpl->m_pNetResource != NULL)
470*cdf0e10cSrcweir 		{
471*cdf0e10cSrcweir 			rtl_uString_newFromStr( &ustrSysDir, pSecImpl->m_pNetResource->lpRemoteName);
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir 			bSuccess = (sal_Bool)(osl_File_E_None == osl_getFileURLFromSystemPath( ustrSysDir, pustrDirectory ));
474*cdf0e10cSrcweir 		}
475*cdf0e10cSrcweir 		else
476*cdf0e10cSrcweir 		{
477*cdf0e10cSrcweir #if 0
478*cdf0e10cSrcweir 			if (pSecImpl->m_hToken)
479*cdf0e10cSrcweir 			{
480*cdf0e10cSrcweir 				DWORD  nInfoBuffer = 512;
481*cdf0e10cSrcweir 				UCHAR* pInfoBuffer = malloc(nInfoBuffer);
482*cdf0e10cSrcweir 
483*cdf0e10cSrcweir 				while (!GetTokenInformation(pSecImpl->m_hToken, TokenUser,
484*cdf0e10cSrcweir 	           							    pInfoBuffer, nInfoBuffer, &nInfoBuffer))
485*cdf0e10cSrcweir 				{
486*cdf0e10cSrcweir 					if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
487*cdf0e10cSrcweir 						pInfoBuffer = realloc(pInfoBuffer, nInfoBuffer);
488*cdf0e10cSrcweir 					else
489*cdf0e10cSrcweir 					{
490*cdf0e10cSrcweir 						free(pInfoBuffer);
491*cdf0e10cSrcweir 						pInfoBuffer = NULL;
492*cdf0e10cSrcweir 						break;
493*cdf0e10cSrcweir 					}
494*cdf0e10cSrcweir 				}
495*cdf0e10cSrcweir 
496*cdf0e10cSrcweir 				/* not implemented */
497*cdf0e10cSrcweir 				OSL_ASSERT(sal_False);
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir 				if (pInfoBuffer)
500*cdf0e10cSrcweir 				{
501*cdf0e10cSrcweir 					/* if (EqualSid() ... */
502*cdf0e10cSrcweir 
503*cdf0e10cSrcweir 				}
504*cdf0e10cSrcweir 			}
505*cdf0e10cSrcweir 			else
506*cdf0e10cSrcweir #endif
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir 				bSuccess = (sal_Bool)(GetSpecialFolder(&ustrSysDir, CSIDL_PERSONAL) &&
509*cdf0e10cSrcweir 				                     (osl_File_E_None == osl_getFileURLFromSystemPath(ustrSysDir, pustrDirectory)));
510*cdf0e10cSrcweir 		}
511*cdf0e10cSrcweir 	}
512*cdf0e10cSrcweir 
513*cdf0e10cSrcweir 	if ( ustrSysDir )
514*cdf0e10cSrcweir 		rtl_uString_release( ustrSysDir );
515*cdf0e10cSrcweir 
516*cdf0e10cSrcweir 	return bSuccess;
517*cdf0e10cSrcweir }
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDirectory)
520*cdf0e10cSrcweir {
521*cdf0e10cSrcweir 	sal_Bool	bSuccess = sal_False;
522*cdf0e10cSrcweir 
523*cdf0e10cSrcweir     if (Security != NULL)
524*cdf0e10cSrcweir 	{
525*cdf0e10cSrcweir 		oslSecurityImpl *pSecImpl = (oslSecurityImpl*)Security;
526*cdf0e10cSrcweir 
527*cdf0e10cSrcweir 		if (pSecImpl->m_pNetResource != NULL)
528*cdf0e10cSrcweir 		{
529*cdf0e10cSrcweir 			rtl_uString *ustrSysDir = NULL;
530*cdf0e10cSrcweir 
531*cdf0e10cSrcweir 			rtl_uString_newFromStr( &ustrSysDir, pSecImpl->m_pNetResource->lpRemoteName);
532*cdf0e10cSrcweir 			bSuccess = (sal_Bool)(osl_File_E_None == osl_getFileURLFromSystemPath( ustrSysDir, pustrDirectory));
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir 			if ( ustrSysDir )
535*cdf0e10cSrcweir 				rtl_uString_release( ustrSysDir );
536*cdf0e10cSrcweir 		}
537*cdf0e10cSrcweir 		else
538*cdf0e10cSrcweir 		{
539*cdf0e10cSrcweir 			if (pSecImpl->m_hToken)
540*cdf0e10cSrcweir 			{
541*cdf0e10cSrcweir 				/* not implemented */
542*cdf0e10cSrcweir 				OSL_ASSERT(sal_False);
543*cdf0e10cSrcweir 			}
544*cdf0e10cSrcweir 			else
545*cdf0e10cSrcweir 			{
546*cdf0e10cSrcweir                 rtl_uString *ustrFile = NULL;
547*cdf0e10cSrcweir 				sal_Unicode	sFile[_MAX_PATH];
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir 				if ( !GetSpecialFolder( &ustrFile, CSIDL_APPDATA) )
550*cdf0e10cSrcweir 				{
551*cdf0e10cSrcweir 					OSL_VERIFY(GetWindowsDirectoryW(sFile, _MAX_DIR) > 0);
552*cdf0e10cSrcweir 
553*cdf0e10cSrcweir 					rtl_uString_newFromStr( &ustrFile, sFile);
554*cdf0e10cSrcweir 				}
555*cdf0e10cSrcweir 
556*cdf0e10cSrcweir 				bSuccess = (sal_Bool)(osl_File_E_None == osl_getFileURLFromSystemPath(ustrFile, pustrDirectory));
557*cdf0e10cSrcweir 
558*cdf0e10cSrcweir 				if ( ustrFile )
559*cdf0e10cSrcweir 					rtl_uString_release( ustrFile );
560*cdf0e10cSrcweir 			}
561*cdf0e10cSrcweir 		}
562*cdf0e10cSrcweir 	}
563*cdf0e10cSrcweir 
564*cdf0e10cSrcweir 	return bSuccess;
565*cdf0e10cSrcweir }
566*cdf0e10cSrcweir 
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security)
569*cdf0e10cSrcweir {
570*cdf0e10cSrcweir 	/* 	CreateProcessAsUser does not load the specified user's profile
571*cdf0e10cSrcweir 		into the HKEY_USERS registry key. This means that access to information
572*cdf0e10cSrcweir 		in the HKEY_CURRENT_USER registry key may not produce results consistent
573*cdf0e10cSrcweir 		with a normal interactive logon.
574*cdf0e10cSrcweir 		It is your responsibility to load the user's registry hive into HKEY_USERS
575*cdf0e10cSrcweir 		with the LoadUserProfile function before calling CreateProcessAsUser.
576*cdf0e10cSrcweir 	*/
577*cdf0e10cSrcweir 	BOOL bOk = FALSE;
578*cdf0e10cSrcweir 
579*cdf0e10cSrcweir 	RegCloseKey(HKEY_CURRENT_USER);
580*cdf0e10cSrcweir 
581*cdf0e10cSrcweir 	if (Privilege(SE_RESTORE_NAME, TRUE))
582*cdf0e10cSrcweir 	{
583*cdf0e10cSrcweir 		HMODULE                 hUserEnvLib			= NULL;
584*cdf0e10cSrcweir 		LPFNLOADUSERPROFILE     fLoadUserProfile	= NULL;
585*cdf0e10cSrcweir 		LPFNUNLOADUSERPROFILE   fUnloadUserProfile	= NULL;
586*cdf0e10cSrcweir 		HANDLE 					hAccessToken 		= ((oslSecurityImpl*)Security)->m_hToken;
587*cdf0e10cSrcweir 		DWORD					nError				= 0;
588*cdf0e10cSrcweir 
589*cdf0e10cSrcweir 		/* try to create user profile */
590*cdf0e10cSrcweir 		if ( !hAccessToken )
591*cdf0e10cSrcweir 		{
592*cdf0e10cSrcweir 			/* retrieve security handle if not done before e.g. osl_getCurrentSecurity()
593*cdf0e10cSrcweir 			*/
594*cdf0e10cSrcweir 			HANDLE hProcess = GetCurrentProcess();
595*cdf0e10cSrcweir 
596*cdf0e10cSrcweir 			if (hProcess != NULL)
597*cdf0e10cSrcweir 			{
598*cdf0e10cSrcweir 				OpenProcessToken(hProcess, TOKEN_IMPERSONATE, &hAccessToken);
599*cdf0e10cSrcweir 				CloseHandle(hProcess);
600*cdf0e10cSrcweir 			}
601*cdf0e10cSrcweir 		}
602*cdf0e10cSrcweir 
603*cdf0e10cSrcweir 	    hUserEnvLib = LoadLibraryA("userenv.dll");
604*cdf0e10cSrcweir 
605*cdf0e10cSrcweir 		if (hUserEnvLib)
606*cdf0e10cSrcweir 		{
607*cdf0e10cSrcweir 			fLoadUserProfile = (LPFNLOADUSERPROFILE)GetProcAddress(hUserEnvLib, "LoadUserProfileW");
608*cdf0e10cSrcweir 			fUnloadUserProfile = (LPFNUNLOADUSERPROFILE)GetProcAddress(hUserEnvLib, "UnloadUserProfile");
609*cdf0e10cSrcweir 
610*cdf0e10cSrcweir 			if (fLoadUserProfile && fUnloadUserProfile)
611*cdf0e10cSrcweir 			{
612*cdf0e10cSrcweir 				rtl_uString  	*buffer = 0;
613*cdf0e10cSrcweir 				PROFILEINFOW	pi;
614*cdf0e10cSrcweir 
615*cdf0e10cSrcweir 				getUserNameImpl(Security, &buffer, sal_False);
616*cdf0e10cSrcweir 
617*cdf0e10cSrcweir 				ZeroMemory( &pi, sizeof(pi) );
618*cdf0e10cSrcweir 	  			pi.dwSize = sizeof(pi);
619*cdf0e10cSrcweir 				pi.lpUserName = rtl_uString_getStr(buffer);
620*cdf0e10cSrcweir 				pi.dwFlags = PI_NOUI;
621*cdf0e10cSrcweir 
622*cdf0e10cSrcweir 				if (fLoadUserProfile(hAccessToken, &pi))
623*cdf0e10cSrcweir 				{
624*cdf0e10cSrcweir 					fUnloadUserProfile(hAccessToken, pi.hProfile);
625*cdf0e10cSrcweir 
626*cdf0e10cSrcweir 					bOk = TRUE;
627*cdf0e10cSrcweir 				}
628*cdf0e10cSrcweir 				else
629*cdf0e10cSrcweir 					nError = GetLastError();
630*cdf0e10cSrcweir 
631*cdf0e10cSrcweir 				rtl_uString_release(buffer);
632*cdf0e10cSrcweir 			}
633*cdf0e10cSrcweir 
634*cdf0e10cSrcweir 			FreeLibrary(hUserEnvLib);
635*cdf0e10cSrcweir 		}
636*cdf0e10cSrcweir 
637*cdf0e10cSrcweir 		if (hAccessToken && (hAccessToken != ((oslSecurityImpl*)Security)->m_hToken))
638*cdf0e10cSrcweir 			CloseHandle(hAccessToken);
639*cdf0e10cSrcweir 	}
640*cdf0e10cSrcweir 
641*cdf0e10cSrcweir 	return (sal_Bool)bOk;
642*cdf0e10cSrcweir }
643*cdf0e10cSrcweir 
644*cdf0e10cSrcweir 
645*cdf0e10cSrcweir void SAL_CALL osl_unloadUserProfile(oslSecurity Security)
646*cdf0e10cSrcweir {
647*cdf0e10cSrcweir 	if ( ((oslSecurityImpl*)Security)->m_hProfile != NULL )
648*cdf0e10cSrcweir 	{
649*cdf0e10cSrcweir 		HMODULE                 hUserEnvLib			= NULL;
650*cdf0e10cSrcweir 		LPFNLOADUSERPROFILE     fLoadUserProfile	= NULL;
651*cdf0e10cSrcweir 		LPFNUNLOADUSERPROFILE   fUnloadUserProfile	= NULL;
652*cdf0e10cSrcweir 		BOOL 					bOk 				= FALSE;
653*cdf0e10cSrcweir 		HANDLE 					hAccessToken 		= ((oslSecurityImpl*)Security)->m_hToken;
654*cdf0e10cSrcweir 
655*cdf0e10cSrcweir 		if ( !hAccessToken )
656*cdf0e10cSrcweir 		{
657*cdf0e10cSrcweir 			/* retrieve security handle if not done before e.g. osl_getCurrentSecurity()
658*cdf0e10cSrcweir 			*/
659*cdf0e10cSrcweir 			HANDLE hProcess = GetCurrentProcess();
660*cdf0e10cSrcweir 
661*cdf0e10cSrcweir 			if (hProcess != NULL)
662*cdf0e10cSrcweir 			{
663*cdf0e10cSrcweir 				OpenProcessToken(hProcess, TOKEN_IMPERSONATE, &hAccessToken);
664*cdf0e10cSrcweir 				CloseHandle(hProcess);
665*cdf0e10cSrcweir 			}
666*cdf0e10cSrcweir 		}
667*cdf0e10cSrcweir 
668*cdf0e10cSrcweir 	    hUserEnvLib = LoadLibrary("userenv.dll");
669*cdf0e10cSrcweir 
670*cdf0e10cSrcweir 		if (hUserEnvLib)
671*cdf0e10cSrcweir 		{
672*cdf0e10cSrcweir 			fLoadUserProfile = (LPFNLOADUSERPROFILE)GetProcAddress(hUserEnvLib, "LoadUserProfileA");
673*cdf0e10cSrcweir 			fUnloadUserProfile = (LPFNUNLOADUSERPROFILE)GetProcAddress(hUserEnvLib, "UnloadUserProfile");
674*cdf0e10cSrcweir 
675*cdf0e10cSrcweir 			if (fLoadUserProfile && fUnloadUserProfile)
676*cdf0e10cSrcweir 			{
677*cdf0e10cSrcweir 				/* unloading the user profile */
678*cdf0e10cSrcweir 				if (fLoadUserProfile && fUnloadUserProfile)
679*cdf0e10cSrcweir 					bOk = fUnloadUserProfile(hAccessToken, ((oslSecurityImpl*)Security)->m_hProfile);
680*cdf0e10cSrcweir 
681*cdf0e10cSrcweir 				if (hUserEnvLib)
682*cdf0e10cSrcweir 					FreeLibrary(hUserEnvLib);
683*cdf0e10cSrcweir 			}
684*cdf0e10cSrcweir 		}
685*cdf0e10cSrcweir 
686*cdf0e10cSrcweir 		((oslSecurityImpl*)Security)->m_hProfile;
687*cdf0e10cSrcweir 
688*cdf0e10cSrcweir 		if (hAccessToken && (hAccessToken != ((oslSecurityImpl*)Security)->m_hToken))
689*cdf0e10cSrcweir 		{
690*cdf0e10cSrcweir 			CloseHandle(hAccessToken);
691*cdf0e10cSrcweir 		}
692*cdf0e10cSrcweir 	}
693*cdf0e10cSrcweir }
694*cdf0e10cSrcweir 
695*cdf0e10cSrcweir /*****************************************************************************/
696*cdf0e10cSrcweir /* Static Module Functions */
697*cdf0e10cSrcweir /*****************************************************************************/
698*cdf0e10cSrcweir 
699*cdf0e10cSrcweir 
700*cdf0e10cSrcweir static sal_Bool GetSpecialFolder(rtl_uString **strPath, int nFolder)
701*cdf0e10cSrcweir {
702*cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
703*cdf0e10cSrcweir 	HINSTANCE hLibrary;
704*cdf0e10cSrcweir 	sal_Char PathA[_MAX_PATH];
705*cdf0e10cSrcweir 	sal_Unicode	PathW[_MAX_PATH];
706*cdf0e10cSrcweir 
707*cdf0e10cSrcweir 	if ((hLibrary = LoadLibrary("shell32.dll")) != NULL)
708*cdf0e10cSrcweir 	{
709*cdf0e10cSrcweir 		BOOL (WINAPI *pSHGetSpecialFolderPathA)(HWND, LPSTR, int, BOOL);
710*cdf0e10cSrcweir 		BOOL (WINAPI *pSHGetSpecialFolderPathW)(HWND, LPWSTR, int, BOOL);
711*cdf0e10cSrcweir 
712*cdf0e10cSrcweir         pSHGetSpecialFolderPathA = (BOOL (WINAPI *)(HWND, LPSTR, int, BOOL))GetProcAddress(hLibrary, "SHGetSpecialFolderPathA");
713*cdf0e10cSrcweir         pSHGetSpecialFolderPathW = (BOOL (WINAPI *)(HWND, LPWSTR, int, BOOL))GetProcAddress(hLibrary, "SHGetSpecialFolderPathW");
714*cdf0e10cSrcweir 
715*cdf0e10cSrcweir 		if (pSHGetSpecialFolderPathA)
716*cdf0e10cSrcweir 		{
717*cdf0e10cSrcweir 			if (pSHGetSpecialFolderPathA(GetActiveWindow(), PathA, nFolder, TRUE))
718*cdf0e10cSrcweir 			{
719*cdf0e10cSrcweir 				rtl_string2UString( strPath, PathA, strlen(PathA), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS);
720*cdf0e10cSrcweir                 OSL_ASSERT(*strPath != NULL);
721*cdf0e10cSrcweir 				bRet = sal_True;
722*cdf0e10cSrcweir 			}
723*cdf0e10cSrcweir 		}
724*cdf0e10cSrcweir 		else if (pSHGetSpecialFolderPathW)
725*cdf0e10cSrcweir 		{
726*cdf0e10cSrcweir 			if (pSHGetSpecialFolderPathW(GetActiveWindow(), PathW, nFolder, TRUE))
727*cdf0e10cSrcweir 			{
728*cdf0e10cSrcweir 				rtl_uString_newFromStr( strPath, PathW);
729*cdf0e10cSrcweir 				bRet = sal_True;
730*cdf0e10cSrcweir 			}
731*cdf0e10cSrcweir 		}
732*cdf0e10cSrcweir 		else
733*cdf0e10cSrcweir 		{
734*cdf0e10cSrcweir 			HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *) = (HRESULT (WINAPI *)(HWND, int, LPITEMIDLIST *))GetProcAddress(hLibrary, "SHGetSpecialFolderLocation");
735*cdf0e10cSrcweir 			BOOL (WINAPI *pSHGetPathFromIDListA)(LPCITEMIDLIST, LPSTR) = (BOOL (WINAPI *)(LPCITEMIDLIST, LPSTR))GetProcAddress(hLibrary, "SHGetPathFromIDListA");
736*cdf0e10cSrcweir 			BOOL (WINAPI *pSHGetPathFromIDListW)(LPCITEMIDLIST, LPWSTR) = (BOOL (WINAPI *)(LPCITEMIDLIST, LPWSTR))GetProcAddress(hLibrary, "SHGetPathFromIDListW");
737*cdf0e10cSrcweir  			HRESULT (WINAPI *pSHGetMalloc)(LPMALLOC *) = (HRESULT (WINAPI *)(LPMALLOC *))GetProcAddress(hLibrary, "SHGetMalloc");
738*cdf0e10cSrcweir 
739*cdf0e10cSrcweir 
740*cdf0e10cSrcweir 			if (pSHGetSpecialFolderLocation && (pSHGetPathFromIDListA || pSHGetPathFromIDListW ) && pSHGetMalloc )
741*cdf0e10cSrcweir 			{
742*cdf0e10cSrcweir 			   	LPITEMIDLIST pidl;
743*cdf0e10cSrcweir 				LPMALLOC pMalloc;
744*cdf0e10cSrcweir 			   	HRESULT  hr;
745*cdf0e10cSrcweir 
746*cdf0e10cSrcweir 			   	hr = pSHGetSpecialFolderLocation(GetActiveWindow(), nFolder, &pidl);
747*cdf0e10cSrcweir 
748*cdf0e10cSrcweir 				/* Get SHGetSpecialFolderLocation fails if directory does not exists. */
749*cdf0e10cSrcweir 				/* If it fails we try to create the directory and redo the call */
750*cdf0e10cSrcweir 				if (! SUCCEEDED(hr))
751*cdf0e10cSrcweir 				{
752*cdf0e10cSrcweir 					HKEY hRegKey;
753*cdf0e10cSrcweir 
754*cdf0e10cSrcweir 					if (RegOpenKey(HKEY_CURRENT_USER,
755*cdf0e10cSrcweir 								   "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
756*cdf0e10cSrcweir 								   &hRegKey) == ERROR_SUCCESS)
757*cdf0e10cSrcweir 					{
758*cdf0e10cSrcweir 						LONG lRet;
759*cdf0e10cSrcweir 						DWORD lSize = elementsof(PathA);
760*cdf0e10cSrcweir 						DWORD Type = REG_SZ;
761*cdf0e10cSrcweir 
762*cdf0e10cSrcweir 						switch (nFolder)
763*cdf0e10cSrcweir 						{
764*cdf0e10cSrcweir 							case CSIDL_APPDATA:
765*cdf0e10cSrcweir 								lRet = RegQueryValueEx(hRegKey, "AppData", NULL, &Type, (LPBYTE)PathA, &lSize);
766*cdf0e10cSrcweir 	  							break;
767*cdf0e10cSrcweir 
768*cdf0e10cSrcweir 							case CSIDL_PERSONAL:
769*cdf0e10cSrcweir 								lRet = RegQueryValueEx(hRegKey, "Personal", NULL, &Type, (LPBYTE)PathA, &lSize);
770*cdf0e10cSrcweir 								break;
771*cdf0e10cSrcweir 
772*cdf0e10cSrcweir 							default:
773*cdf0e10cSrcweir 								lRet = -1l;
774*cdf0e10cSrcweir 						}
775*cdf0e10cSrcweir 
776*cdf0e10cSrcweir 						if ((lRet == ERROR_SUCCESS) && (Type == REG_SZ))
777*cdf0e10cSrcweir 						{
778*cdf0e10cSrcweir 							if (_access(PathA, 0) < 0)
779*cdf0e10cSrcweir 								CreateDirectory(PathA, NULL);
780*cdf0e10cSrcweir 
781*cdf0e10cSrcweir 						   	hr = pSHGetSpecialFolderLocation(GetActiveWindow(), nFolder, &pidl);
782*cdf0e10cSrcweir 						}
783*cdf0e10cSrcweir 
784*cdf0e10cSrcweir 						RegCloseKey(hRegKey);
785*cdf0e10cSrcweir 					}
786*cdf0e10cSrcweir 				}
787*cdf0e10cSrcweir 
788*cdf0e10cSrcweir 				if (SUCCEEDED(hr))
789*cdf0e10cSrcweir 				{
790*cdf0e10cSrcweir 					if (pSHGetPathFromIDListW && pSHGetPathFromIDListW(pidl, PathW))
791*cdf0e10cSrcweir 			   		{
792*cdf0e10cSrcweir 						/* if directory does not exist, create it */
793*cdf0e10cSrcweir 						if (_waccess(PathW, 0) < 0)
794*cdf0e10cSrcweir 							CreateDirectoryW(PathW, NULL);
795*cdf0e10cSrcweir 
796*cdf0e10cSrcweir 						rtl_uString_newFromStr( strPath, PathW);
797*cdf0e10cSrcweir 						bRet = sal_True;
798*cdf0e10cSrcweir 				   	}
799*cdf0e10cSrcweir 					else if (pSHGetPathFromIDListA && pSHGetPathFromIDListA(pidl, PathA))
800*cdf0e10cSrcweir 					{
801*cdf0e10cSrcweir 						/* if directory does not exist, create it */
802*cdf0e10cSrcweir 						if (_access(PathA, 0) < 0)
803*cdf0e10cSrcweir 							CreateDirectoryA(PathA, NULL);
804*cdf0e10cSrcweir 
805*cdf0e10cSrcweir 						rtl_string2UString( strPath, PathA, strlen(PathA), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS);
806*cdf0e10cSrcweir                         OSL_ASSERT(*strPath != NULL);
807*cdf0e10cSrcweir 						bRet = sal_True;
808*cdf0e10cSrcweir 					}
809*cdf0e10cSrcweir 			   	}
810*cdf0e10cSrcweir 
811*cdf0e10cSrcweir 			   	if (SUCCEEDED(pSHGetMalloc(&pMalloc)))
812*cdf0e10cSrcweir 				{
813*cdf0e10cSrcweir 				   	pMalloc->lpVtbl->Free(pMalloc, pidl);
814*cdf0e10cSrcweir 					pMalloc->lpVtbl->Release(pMalloc);
815*cdf0e10cSrcweir 				}
816*cdf0e10cSrcweir 			}
817*cdf0e10cSrcweir 		}
818*cdf0e10cSrcweir 	}
819*cdf0e10cSrcweir 
820*cdf0e10cSrcweir 	FreeLibrary(hLibrary);
821*cdf0e10cSrcweir 
822*cdf0e10cSrcweir 	return (bRet);
823*cdf0e10cSrcweir }
824*cdf0e10cSrcweir 
825*cdf0e10cSrcweir 
826*cdf0e10cSrcweir static sal_Bool isWNT(void)
827*cdf0e10cSrcweir {
828*cdf0e10cSrcweir 	static sal_Bool isInit = sal_False;
829*cdf0e10cSrcweir 	static sal_Bool isWNT = sal_False;
830*cdf0e10cSrcweir 
831*cdf0e10cSrcweir 	if (!isInit)
832*cdf0e10cSrcweir 	{
833*cdf0e10cSrcweir 		OSVERSIONINFO VersionInformation =
834*cdf0e10cSrcweir 
835*cdf0e10cSrcweir 		{
836*cdf0e10cSrcweir 			sizeof(OSVERSIONINFO),
837*cdf0e10cSrcweir 			0,
838*cdf0e10cSrcweir 	    	0,
839*cdf0e10cSrcweir 	    	0,
840*cdf0e10cSrcweir 	    	0,
841*cdf0e10cSrcweir 	    	"",
842*cdf0e10cSrcweir 		};
843*cdf0e10cSrcweir 
844*cdf0e10cSrcweir 		if (
845*cdf0e10cSrcweir 			GetVersionEx(&VersionInformation) &&
846*cdf0e10cSrcweir 			(VersionInformation.dwPlatformId == VER_PLATFORM_WIN32_NT)
847*cdf0e10cSrcweir 		   )
848*cdf0e10cSrcweir 		{
849*cdf0e10cSrcweir 			isWNT = sal_True;
850*cdf0e10cSrcweir 		}
851*cdf0e10cSrcweir 
852*cdf0e10cSrcweir 		isInit = sal_True;
853*cdf0e10cSrcweir 	}
854*cdf0e10cSrcweir 
855*cdf0e10cSrcweir 	return(isWNT);
856*cdf0e10cSrcweir }
857*cdf0e10cSrcweir 
858*cdf0e10cSrcweir static BOOL Privilege(LPTSTR strPrivilege, BOOL bEnable)
859*cdf0e10cSrcweir {
860*cdf0e10cSrcweir 	HANDLE           hToken;
861*cdf0e10cSrcweir 	TOKEN_PRIVILEGES tp;
862*cdf0e10cSrcweir 
863*cdf0e10cSrcweir 	/*
864*cdf0e10cSrcweir 		obtain the processes token
865*cdf0e10cSrcweir 	*/
866*cdf0e10cSrcweir 	if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_DUP_QUERY, &hToken))
867*cdf0e10cSrcweir 		return FALSE;
868*cdf0e10cSrcweir 
869*cdf0e10cSrcweir 	/*
870*cdf0e10cSrcweir 		get the luid
871*cdf0e10cSrcweir 	*/
872*cdf0e10cSrcweir 	if (!LookupPrivilegeValue(NULL, strPrivilege, &tp.Privileges[0].Luid))
873*cdf0e10cSrcweir 		return FALSE;
874*cdf0e10cSrcweir 
875*cdf0e10cSrcweir 	tp.PrivilegeCount = 1;
876*cdf0e10cSrcweir 
877*cdf0e10cSrcweir 	if (bEnable)
878*cdf0e10cSrcweir 		tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
879*cdf0e10cSrcweir 	else
880*cdf0e10cSrcweir 		tp.Privileges[0].Attributes = 0;
881*cdf0e10cSrcweir 
882*cdf0e10cSrcweir 	/*
883*cdf0e10cSrcweir 		enable or disable the privilege
884*cdf0e10cSrcweir 	*/
885*cdf0e10cSrcweir 	if (!AdjustTokenPrivileges(hToken, FALSE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, 0))
886*cdf0e10cSrcweir 		return FALSE;
887*cdf0e10cSrcweir 
888*cdf0e10cSrcweir 	if (!CloseHandle(hToken))
889*cdf0e10cSrcweir 		return FALSE;
890*cdf0e10cSrcweir 
891*cdf0e10cSrcweir 	return TRUE;
892*cdf0e10cSrcweir }
893*cdf0e10cSrcweir 
894*cdf0e10cSrcweir static sal_Bool SAL_CALL getUserNameImpl(oslSecurity Security, rtl_uString **strName,  sal_Bool bIncludeDomain)
895*cdf0e10cSrcweir {
896*cdf0e10cSrcweir     if (Security != NULL)
897*cdf0e10cSrcweir 	{
898*cdf0e10cSrcweir 		oslSecurityImpl *pSecImpl = (oslSecurityImpl*)Security;
899*cdf0e10cSrcweir 
900*cdf0e10cSrcweir 		HANDLE hAccessToken = pSecImpl->m_hToken;
901*cdf0e10cSrcweir 
902*cdf0e10cSrcweir 		if (hAccessToken == NULL)
903*cdf0e10cSrcweir 			OpenProcessToken(GetCurrentProcess(), TOKEN_DUP_QUERY, &hAccessToken);
904*cdf0e10cSrcweir 
905*cdf0e10cSrcweir 		if (hAccessToken)
906*cdf0e10cSrcweir 		{
907*cdf0e10cSrcweir 			DWORD  nInfoBuffer = 512;
908*cdf0e10cSrcweir 			UCHAR* pInfoBuffer = malloc(nInfoBuffer);
909*cdf0e10cSrcweir 
910*cdf0e10cSrcweir 			while (!GetTokenInformation(hAccessToken, TokenUser,
911*cdf0e10cSrcweir            							    pInfoBuffer, nInfoBuffer, &nInfoBuffer))
912*cdf0e10cSrcweir 			{
913*cdf0e10cSrcweir 				if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
914*cdf0e10cSrcweir 					pInfoBuffer = realloc(pInfoBuffer, nInfoBuffer);
915*cdf0e10cSrcweir 				else
916*cdf0e10cSrcweir 				{
917*cdf0e10cSrcweir 					free(pInfoBuffer);
918*cdf0e10cSrcweir 					pInfoBuffer = NULL;
919*cdf0e10cSrcweir 					break;
920*cdf0e10cSrcweir 				}
921*cdf0e10cSrcweir 			}
922*cdf0e10cSrcweir 
923*cdf0e10cSrcweir 			if (pSecImpl->m_hToken == NULL)
924*cdf0e10cSrcweir 				CloseHandle(hAccessToken);
925*cdf0e10cSrcweir 
926*cdf0e10cSrcweir 			if (pInfoBuffer)
927*cdf0e10cSrcweir 			{
928*cdf0e10cSrcweir 				sal_Unicode  UserName[128];
929*cdf0e10cSrcweir 				sal_Unicode  DomainName[128];
930*cdf0e10cSrcweir 				sal_Unicode	 Name[257];
931*cdf0e10cSrcweir 				DWORD nUserName   = sizeof(UserName);
932*cdf0e10cSrcweir 				DWORD nDomainName = sizeof(DomainName);
933*cdf0e10cSrcweir 				SID_NAME_USE sUse;
934*cdf0e10cSrcweir 
935*cdf0e10cSrcweir 				if (LookupAccountSidW(NULL, ((PTOKEN_USER)pInfoBuffer)->User.Sid,
936*cdf0e10cSrcweir            						     UserName, &nUserName,
937*cdf0e10cSrcweir            						     DomainName, &nDomainName, &sUse))
938*cdf0e10cSrcweir 				{
939*cdf0e10cSrcweir 					if (bIncludeDomain)
940*cdf0e10cSrcweir 					{
941*cdf0e10cSrcweir 						wcscpy(Name, DomainName);
942*cdf0e10cSrcweir 						wcscat(Name, L"/");
943*cdf0e10cSrcweir 						wcscat(Name, UserName);
944*cdf0e10cSrcweir 					}
945*cdf0e10cSrcweir 					else
946*cdf0e10cSrcweir 					{
947*cdf0e10cSrcweir 						wcscpy(Name, UserName);
948*cdf0e10cSrcweir 					}
949*cdf0e10cSrcweir            		}
950*cdf0e10cSrcweir 				rtl_uString_newFromStr( strName, Name);
951*cdf0e10cSrcweir 
952*cdf0e10cSrcweir 				free(pInfoBuffer);
953*cdf0e10cSrcweir 
954*cdf0e10cSrcweir 				return (sal_True);
955*cdf0e10cSrcweir 			}
956*cdf0e10cSrcweir 		}
957*cdf0e10cSrcweir 		else
958*cdf0e10cSrcweir 		{
959*cdf0e10cSrcweir 			DWORD needed=0;
960*cdf0e10cSrcweir 			sal_Unicode			*pNameW=NULL;
961*cdf0e10cSrcweir 
962*cdf0e10cSrcweir 			WNetGetUserW(NULL, NULL, &needed);
963*cdf0e10cSrcweir 			pNameW = malloc (needed*sizeof(sal_Unicode));
964*cdf0e10cSrcweir 
965*cdf0e10cSrcweir 			if (WNetGetUserW(NULL, pNameW, &needed) == NO_ERROR)
966*cdf0e10cSrcweir 			{
967*cdf0e10cSrcweir 				rtl_uString_newFromStr( strName, pNameW);
968*cdf0e10cSrcweir 
969*cdf0e10cSrcweir 				if (pNameW)
970*cdf0e10cSrcweir 					free(pNameW);
971*cdf0e10cSrcweir 				return (sal_True);
972*cdf0e10cSrcweir 			}
973*cdf0e10cSrcweir 			else
974*cdf0e10cSrcweir 				if (wcslen(pSecImpl->m_User) > 0)
975*cdf0e10cSrcweir 				{
976*cdf0e10cSrcweir 					rtl_uString_newFromStr( strName, pSecImpl->m_pNetResource->lpRemoteName);
977*cdf0e10cSrcweir 
978*cdf0e10cSrcweir 					if (pNameW)
979*cdf0e10cSrcweir 						free(pNameW);
980*cdf0e10cSrcweir 
981*cdf0e10cSrcweir 					return (sal_True);
982*cdf0e10cSrcweir 				}
983*cdf0e10cSrcweir 
984*cdf0e10cSrcweir 			if (pNameW)
985*cdf0e10cSrcweir 				free(pNameW);
986*cdf0e10cSrcweir 		}
987*cdf0e10cSrcweir 	}
988*cdf0e10cSrcweir 
989*cdf0e10cSrcweir     return sal_False;
990*cdf0e10cSrcweir }
991*cdf0e10cSrcweir 
992