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