1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svl.hxx"
26 
27 #include <svl/folderrestriction.hxx>
28 #include "osl/process.h"
29 #include "tools/urlobj.hxx"
30 #include "unotools/localfilehelper.hxx"
31 
32 //-----------------------------------------------------------------------------
33 
convertStringListToUrls(const String & _rColonSeparatedList,::std::vector<String> & _rTokens,bool _bFinalSlash)34 static void convertStringListToUrls (
35 	const String& _rColonSeparatedList, ::std::vector< String >& _rTokens, bool _bFinalSlash )
36 {
37 	const sal_Unicode s_cSeparator =
38 #if defined(WNT)
39 		';'
40 #else
41 		':'
42 #endif
43             ;
44 	xub_StrLen nTokens = _rColonSeparatedList.GetTokenCount( s_cSeparator );
45 	_rTokens.resize( 0 ); _rTokens.reserve( nTokens );
46 	for ( xub_StrLen i=0; i<nTokens; ++i )
47 	{
48 		// the current token in the list
49 		String sCurrentToken = _rColonSeparatedList.GetToken( i, s_cSeparator );
50 		if ( !sCurrentToken.Len() )
51 			continue;
52 
53 		INetURLObject aCurrentURL;
54 
55 		String sURL;
56 		if ( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( sCurrentToken, sURL ) )
57 			aCurrentURL = INetURLObject( sURL );
58 		else
59 		{
60 			// smart URL parsing, assuming FILE protocol
61 			aCurrentURL = INetURLObject( sCurrentToken, INET_PROT_FILE );
62 		}
63 
64 		if ( _bFinalSlash )
65 			aCurrentURL.setFinalSlash( );
66 		else
67 			aCurrentURL.removeFinalSlash( );
68 		_rTokens.push_back( aCurrentURL.GetMainURL( INetURLObject::NO_DECODE ) );
69 	}
70 }
71 
72 /** retrieves the value of an environment variable
73 	@return <TRUE/> if and only if the retrieved string value is not empty
74 */
getEnvironmentValue(const sal_Char * _pAsciiEnvName,::rtl::OUString & _rValue)75 static bool getEnvironmentValue( const sal_Char* _pAsciiEnvName, ::rtl::OUString& _rValue )
76 {
77 	_rValue = ::rtl::OUString();
78 	::rtl::OUString sEnvName = ::rtl::OUString::createFromAscii( _pAsciiEnvName );
79 	osl_getEnvironment( sEnvName.pData, &_rValue.pData );
80 	return _rValue.getLength() != 0;
81 }
82 
83 //-----------------------------------------------------------------------------
84 
85 namespace svt
86 {
87 
getUnrestrictedFolders(::std::vector<String> & _rFolders)88     void getUnrestrictedFolders( ::std::vector< String >& _rFolders )
89     {
90         _rFolders.resize( 0 );
91         ::rtl::OUString sRestrictedPathList;
92         if ( getEnvironmentValue( "RestrictedPath", sRestrictedPathList ) )
93 		{
94             // append a final slash. This ensures that when we later on check
95             // for unrestricted paths, we don't allow paths like "/home/user35" just because
96             // "/home/user3" is allowed - with the final slash, we make it "/home/user3/".
97             convertStringListToUrls( sRestrictedPathList, _rFolders, true );
98 		}
99     }
100 
101 } // namespace svt
102 
103