1dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5dde7d3faSAndrew Rist  * distributed with this work for additional information
6dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10dde7d3faSAndrew Rist  *
11dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12dde7d3faSAndrew Rist  *
13dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18dde7d3faSAndrew Rist  * under the License.
19dde7d3faSAndrew Rist  *
20dde7d3faSAndrew Rist  *************************************************************/
21dde7d3faSAndrew Rist 
22dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include <osl/file.hxx>
29cdf0e10cSrcweir #include <osl/security.hxx>
30cdf0e10cSrcweir #include <osl/thread.h>
31cdf0e10cSrcweir #include <vos/process.hxx>
32cdf0e10cSrcweir #include <rtl/textenc.h>
33cdf0e10cSrcweir #include <rtl/uri.h>
34cdf0e10cSrcweir #include <rtl/uri.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace vos;
37cdf0e10cSrcweir using namespace osl;
38cdf0e10cSrcweir using namespace rtl;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #define PATH_DELEMITTER	'/'
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #define USER_REGISTRY_NAME_ENV 		"STAR_USER_REGISTRY"
43cdf0e10cSrcweir #define SYSTEM_REGISTRY_NAME_ENV 	"STAR_REGISTRY"
44cdf0e10cSrcweir #define REGISTRY_SYSTEM_NAME	"services.rdb"
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #define REGISTRY_LOCAL_NAME		"user60.rdb"
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #ifdef SAL_UNX
49cdf0e10cSrcweir #define CONFIG_PATH_PREFIX		"."
50cdf0e10cSrcweir #else
51cdf0e10cSrcweir #define CONFIG_PATH_PREFIX		""
52cdf0e10cSrcweir #endif
53cdf0e10cSrcweir 
54cdf0e10cSrcweir namespace comphelper
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 
57cdf0e10cSrcweir /**
58cdf0e10cSrcweir    @return sal_True, if the office is started in a portal
59cdf0e10cSrcweir                      environment.
60cdf0e10cSrcweir 		   sal_False, if the common office is started
61cdf0e10cSrcweir  */
retrievePortalUserDir(OUString * pDirectory)62cdf0e10cSrcweir static sal_Bool retrievePortalUserDir( OUString *pDirectory )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir 	OStartupInfo startInfo;
65cdf0e10cSrcweir 	sal_uInt32 nArgs = startInfo.getCommandArgCount();
66cdf0e10cSrcweir 	sal_Bool bIsPortalUser = sal_False;
67cdf0e10cSrcweir 	OUString sArg;
68cdf0e10cSrcweir 	while( nArgs > 0 )
69cdf0e10cSrcweir   	{
70cdf0e10cSrcweir 		if ( !startInfo.getCommandArg(--nArgs, sArg) )
71cdf0e10cSrcweir 	  	{
72cdf0e10cSrcweir 	    	if ( sArg.indexOf(OUString::createFromAscii("-userid")) == 0 )
73cdf0e10cSrcweir 	    	{
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	      		bIsPortalUser = sal_True;
76cdf0e10cSrcweir 				sal_Int32 nStart = sArg.lastIndexOf( '[' );
77cdf0e10cSrcweir 				sal_Int32 nEnd   = sArg.lastIndexOf( ']' );
78cdf0e10cSrcweir 				if( -1 == nStart || -1 == nEnd || nEnd < nStart)
79cdf0e10cSrcweir 				{
80cdf0e10cSrcweir 					*pDirectory = OUString();
81cdf0e10cSrcweir 				}
82cdf0e10cSrcweir 				else
83cdf0e10cSrcweir 				{
84cdf0e10cSrcweir 					OUString aEncHome = sArg.copy( nStart + 1 , nEnd - nStart -1 );
85cdf0e10cSrcweir 					*pDirectory = rtl::Uri::decode(aEncHome,
86cdf0e10cSrcweir 												   rtl_UriDecodeWithCharset,
87cdf0e10cSrcweir 												   RTL_TEXTENCODING_UTF8);
88cdf0e10cSrcweir 				}
89cdf0e10cSrcweir 	      		break;
90cdf0e10cSrcweir 	    	}
91cdf0e10cSrcweir 	  	}
92cdf0e10cSrcweir  	}
93cdf0e10cSrcweir 	return bIsPortalUser;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 
getDefaultLocalRegistry()97cdf0e10cSrcweir static OUString getDefaultLocalRegistry()
98cdf0e10cSrcweir {
99cdf0e10cSrcweir 	OUString uBuffer, userRegistryName;
100cdf0e10cSrcweir 	OUString portalUserDir;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	sal_Bool bIsPortalUser = retrievePortalUserDir( &portalUserDir );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	if ( bIsPortalUser )
105cdf0e10cSrcweir    	{
106*49b34792SHerbert Dürr 		if( !portalUserDir.isEmpty() )
107cdf0e10cSrcweir 		{
108cdf0e10cSrcweir 			FileBase::getFileURLFromSystemPath( portalUserDir , portalUserDir );
109cdf0e10cSrcweir 			userRegistryName = portalUserDir;
110cdf0e10cSrcweir 			userRegistryName += OUString( RTL_CONSTASCII_USTRINGPARAM(
111cdf0e10cSrcweir 				"/user/" REGISTRY_LOCAL_NAME ) );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 			// Directory creation is probably necessary for bootstrapping a new
114cdf0e10cSrcweir 			// user in the portal environment (the ucb uses this function).
115cdf0e10cSrcweir 			// This should be solved differently, as
116cdf0e10cSrcweir 			// no one expects this function to create anything ...
117cdf0e10cSrcweir 			OUString sSeparator(RTL_CONSTASCII_USTRINGPARAM("/"));
118cdf0e10cSrcweir 			OUString sPath(RTL_CONSTASCII_USTRINGPARAM("file://"));
119cdf0e10cSrcweir 			FileBase::RC retRC = FileBase::E_None;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 			sal_Int32 nIndex = 3;
122cdf0e10cSrcweir 			sPath += userRegistryName.getToken(2, '/', nIndex);
123cdf0e10cSrcweir 			while( nIndex != -1 )
124cdf0e10cSrcweir 			{
125cdf0e10cSrcweir 				sPath += sSeparator;
126cdf0e10cSrcweir 				sPath += userRegistryName.getToken(0, '/', nIndex);
127cdf0e10cSrcweir 				if( nIndex == -1 )
128cdf0e10cSrcweir 					break;
129cdf0e10cSrcweir 				Directory aDir( sPath );
130cdf0e10cSrcweir 				if( aDir.open() == FileBase::E_NOENT )
131cdf0e10cSrcweir 				{
132cdf0e10cSrcweir 					retRC = Directory::create(sPath);
133cdf0e10cSrcweir 					if ( retRC != FileBase::E_None && retRC != FileBase::E_EXIST)
134cdf0e10cSrcweir 					{
135cdf0e10cSrcweir 						return OUString();
136cdf0e10cSrcweir 					}
137cdf0e10cSrcweir 				}
138cdf0e10cSrcweir 			}
139cdf0e10cSrcweir 		}
140cdf0e10cSrcweir 	}
141cdf0e10cSrcweir 	else /* bIsPortalUser */
142cdf0e10cSrcweir 	{
143cdf0e10cSrcweir 		::osl::Security aUserSecurity;
144cdf0e10cSrcweir 		aUserSecurity.getConfigDir( userRegistryName );
145cdf0e10cSrcweir 		userRegistryName += OUString( RTL_CONSTASCII_USTRINGPARAM(
146cdf0e10cSrcweir 			"/" CONFIG_PATH_PREFIX REGISTRY_LOCAL_NAME ) );
147cdf0e10cSrcweir 	}
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 	return userRegistryName;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 
getPathToUserRegistry()153cdf0e10cSrcweir OUString getPathToUserRegistry()
154cdf0e10cSrcweir {
155cdf0e10cSrcweir 	OUString  	userRegistryName;
156cdf0e10cSrcweir 	FILE  		*f=NULL;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 	// search the environment STAR_USER_REGISTRY
159cdf0e10cSrcweir 	OString sBuffer( getenv(USER_REGISTRY_NAME_ENV) );
160*49b34792SHerbert Dürr 	if ( !sBuffer.isEmpty() )
161cdf0e10cSrcweir 	{
162cdf0e10cSrcweir 		f = fopen( sBuffer.getStr(), "r" );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 		if (f != NULL)
165cdf0e10cSrcweir 		{
166cdf0e10cSrcweir 			fclose(f);
167cdf0e10cSrcweir 			userRegistryName = OStringToOUString( sBuffer, osl_getThreadTextEncoding() );
168cdf0e10cSrcweir 		}
169cdf0e10cSrcweir 	}
170cdf0e10cSrcweir 
171*49b34792SHerbert Dürr 	if ( userRegistryName.isEmpty() )
172cdf0e10cSrcweir 	{
173cdf0e10cSrcweir 		userRegistryName = getDefaultLocalRegistry();
174cdf0e10cSrcweir 	}
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 	return userRegistryName;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
getPathToSystemRegistry()179cdf0e10cSrcweir OUString getPathToSystemRegistry()
180cdf0e10cSrcweir {
181cdf0e10cSrcweir 	OUString uBuffer;
182cdf0e10cSrcweir 	OUString registryBaseName( RTL_CONSTASCII_USTRINGPARAM(REGISTRY_SYSTEM_NAME) );
183cdf0e10cSrcweir 	OUString systemRegistryName;
184cdf0e10cSrcweir 	FILE  	 *f=NULL;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 	// search in the directory of the executable
187cdf0e10cSrcweir     OStartupInfo info;
188cdf0e10cSrcweir 	if( OStartupInfo::E_None == info.getExecutableFile(uBuffer) )
189cdf0e10cSrcweir 	{
190cdf0e10cSrcweir 		sal_uInt32 	lastIndex = uBuffer.lastIndexOf(PATH_DELEMITTER);
191cdf0e10cSrcweir 		if (lastIndex > 0)
192cdf0e10cSrcweir 		{
193cdf0e10cSrcweir 			uBuffer = uBuffer.copy(0, lastIndex + 1);
194cdf0e10cSrcweir 		}
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 		uBuffer += registryBaseName;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 		if (!FileBase::getSystemPathFromFileURL(uBuffer, systemRegistryName))
199cdf0e10cSrcweir 		{
200cdf0e10cSrcweir 			OString tmpStr( OUStringToOString(systemRegistryName, osl_getThreadTextEncoding()) );
201cdf0e10cSrcweir 			f = fopen( tmpStr.getStr(), "r" );
202cdf0e10cSrcweir 		}
203cdf0e10cSrcweir 	}
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 	if (f == NULL)
206cdf0e10cSrcweir 	{
207cdf0e10cSrcweir 		// search the environment STAR_REGISTRY
208cdf0e10cSrcweir 		OString tmpStr( getenv(SYSTEM_REGISTRY_NAME_ENV) );
209*49b34792SHerbert Dürr 		if ( !tmpStr.isEmpty() )
210cdf0e10cSrcweir 		{
211cdf0e10cSrcweir 			f = fopen(tmpStr.getStr(), "r");
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 			if (f != NULL)
214cdf0e10cSrcweir 			{
215cdf0e10cSrcweir 				fclose(f);
216cdf0e10cSrcweir 				systemRegistryName = OStringToOUString( tmpStr, osl_getThreadTextEncoding() );
217cdf0e10cSrcweir 			} else
218cdf0e10cSrcweir 			{
219cdf0e10cSrcweir 				systemRegistryName = OUString();
220cdf0e10cSrcweir 			}
221cdf0e10cSrcweir 		}
222cdf0e10cSrcweir 	} else
223cdf0e10cSrcweir 	{
224cdf0e10cSrcweir 		fclose(f);
225cdf0e10cSrcweir 	}
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 	return systemRegistryName;
228cdf0e10cSrcweir }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232