Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <svl/restrictedpaths.hxx>
21 :
22 : #include <algorithm>
23 : #include <osl/process.h>
24 : #include <tools/urlobj.hxx>
25 : #include <unotools/localfilehelper.hxx>
26 : #include <unotools/syslocale.hxx>
27 :
28 : namespace svt
29 : {
30 : namespace
31 : {
32 : // ----------------------------------------------------------------
33 : /** retrieves the value of an environment variable
34 : @return <TRUE/> if and only if the retrieved string value is not empty
35 : */
36 0 : bool lcl_getEnvironmentValue( const sal_Char* _pAsciiEnvName, OUString& _rValue )
37 : {
38 0 : _rValue = OUString();
39 0 : OUString sEnvName = OUString::createFromAscii( _pAsciiEnvName );
40 0 : osl_getEnvironment( sEnvName.pData, &_rValue.pData );
41 0 : return !_rValue.isEmpty();
42 : }
43 :
44 : //-----------------------------------------------------------------
45 0 : void lcl_convertStringListToUrls( const OUString& _rColonSeparatedList, ::std::vector< OUString >& _rTokens )
46 : {
47 : const sal_Unicode cSeparator =
48 : #if defined(WNT)
49 : ';'
50 : #else
51 0 : ':'
52 : #endif
53 : ;
54 0 : sal_Int32 nIndex = 0;
55 0 : do
56 : {
57 : // the current token in the list
58 0 : OUString sCurrentToken = _rColonSeparatedList.getToken( 0, cSeparator, nIndex );
59 0 : if ( !sCurrentToken.isEmpty() )
60 : {
61 0 : INetURLObject aCurrentURL;
62 :
63 0 : OUString sURL;
64 0 : if ( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( sCurrentToken, sURL ) )
65 0 : aCurrentURL = INetURLObject( sURL );
66 : else
67 : {
68 : // smart URL parsing, assuming FILE protocol
69 0 : aCurrentURL = INetURLObject( sCurrentToken, INET_PROT_FILE );
70 : }
71 :
72 0 : aCurrentURL.setFinalSlash( );
73 0 : _rTokens.push_back( aCurrentURL.GetMainURL( INetURLObject::NO_DECODE ) );
74 0 : }
75 : }
76 : while ( nIndex >= 0 );
77 0 : }
78 :
79 : }
80 :
81 : //=====================================================================
82 : //= CheckURLAllowed
83 : //=====================================================================
84 0 : struct CheckURLAllowed
85 : {
86 : protected:
87 : #ifdef WNT
88 : SvtSysLocale m_aSysLocale;
89 : #endif
90 : OUString m_sCheckURL; // the URL to check
91 : bool m_bAllowParent;
92 : public:
93 0 : inline CheckURLAllowed( const OUString& _rCheckURL, bool bAllowParent = true )
94 : : m_sCheckURL( _rCheckURL )
95 0 : , m_bAllowParent( bAllowParent )
96 : {
97 : #ifdef WNT
98 : // on windows, assume that the relevant file systems are case insensitive,
99 : // thus normalize the URL
100 : m_sCheckURL = m_aSysLocale.GetCharClass().lowercase( m_sCheckURL, 0, m_sCheckURL.getLength() );
101 : #endif
102 0 : }
103 :
104 0 : bool operator()( const OUString& _rApprovedURL )
105 : {
106 : #ifdef WNT
107 : // on windows, assume that the relevant file systems are case insensitive,
108 : // thus normalize the URL
109 : OUString sApprovedURL( m_aSysLocale.GetCharClass().lowercase( _rApprovedURL, 0, _rApprovedURL.getLength() ) );
110 : #else
111 0 : OUString sApprovedURL( _rApprovedURL );
112 : #endif
113 :
114 0 : sal_Int32 nLenApproved = sApprovedURL.getLength();
115 0 : sal_Int32 nLenChecked = m_sCheckURL.getLength();
116 :
117 0 : if ( nLenApproved > nLenChecked )
118 : {
119 0 : if ( m_bAllowParent )
120 : {
121 0 : if ( sApprovedURL.indexOf( m_sCheckURL ) == 0 )
122 : {
123 0 : if ( ( m_sCheckURL[ nLenChecked - 1 ] == '/' )
124 0 : || ( sApprovedURL[ nLenChecked ] == '/' ) )
125 0 : return true;
126 : }
127 : }
128 : else
129 : {
130 : // just a difference in final slash?
131 0 : if ( ( nLenApproved == ( nLenChecked + 1 ) ) &&
132 0 : ( sApprovedURL[ nLenApproved - 1 ] == '/' ) )
133 0 : return true;
134 : }
135 0 : return false;
136 : }
137 0 : else if ( nLenApproved < nLenChecked )
138 : {
139 0 : if ( m_sCheckURL.indexOf( sApprovedURL ) == 0 )
140 : {
141 0 : if ( ( sApprovedURL[ nLenApproved - 1 ] == '/' )
142 0 : || ( m_sCheckURL[ nLenApproved ] == '/' ) )
143 0 : return true;
144 : }
145 0 : return false;
146 : }
147 : else
148 : {
149 : // strings have equal length
150 0 : return ( sApprovedURL == m_sCheckURL );
151 0 : }
152 : }
153 : };
154 :
155 : //=====================================================================
156 : //= RestrictedPaths
157 : //=====================================================================
158 : //---------------------------------------------------------------------
159 0 : RestrictedPaths::RestrictedPaths()
160 0 : :m_bFilterIsEnabled( true )
161 : {
162 0 : OUString sRestrictedPathList;
163 0 : if ( lcl_getEnvironmentValue( "RestrictedPath", sRestrictedPathList ) )
164 : // append a final slash. This ensures that when we later on check
165 : // for unrestricted paths, we don't allow paths like "/home/user35" just because
166 : // "/home/user3" is allowed - with the final slash, we make it "/home/user3/".
167 0 : lcl_convertStringListToUrls( sRestrictedPathList, m_aUnrestrictedURLs );
168 0 : }
169 :
170 0 : RestrictedPaths::~RestrictedPaths() {}
171 :
172 : // --------------------------------------------------------------------
173 0 : bool RestrictedPaths::isUrlAllowed( const OUString& _rURL ) const
174 : {
175 0 : if ( m_aUnrestrictedURLs.empty() || !m_bFilterIsEnabled )
176 0 : return true;
177 :
178 : ::std::vector< OUString >::const_iterator aApprovedURL = ::std::find_if(
179 : m_aUnrestrictedURLs.begin(),
180 : m_aUnrestrictedURLs.end(),
181 : CheckURLAllowed( _rURL, true )
182 0 : );
183 :
184 0 : return ( aApprovedURL != m_aUnrestrictedURLs.end() );
185 : }
186 :
187 : // --------------------------------------------------------------------
188 0 : bool RestrictedPaths::isUrlAllowed( const OUString& _rURL, bool allowParents ) const
189 : {
190 0 : if ( m_aUnrestrictedURLs.empty() || !m_bFilterIsEnabled )
191 0 : return true;
192 :
193 : ::std::vector< OUString >::const_iterator aApprovedURL = ::std::find_if(
194 : m_aUnrestrictedURLs.begin(),
195 : m_aUnrestrictedURLs.end(),
196 : CheckURLAllowed( _rURL, allowParents )
197 0 : );
198 :
199 0 : return ( aApprovedURL != m_aUnrestrictedURLs.end() );
200 : }
201 :
202 : } // namespace svt
203 :
204 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|