merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / misc / restrictedpaths.cxx
blob80581bb43f205dc8a9b340c635df30e12e3aa2dc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: restrictedpaths.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #include <svtools/restrictedpaths.hxx>
35 #include <algorithm>
36 #include <osl/process.h>
37 #include <tools/urlobj.hxx>
38 #include <unotools/localfilehelper.hxx>
39 #include <svtools/syslocale.hxx>
41 namespace svt
43 namespace
45 // ----------------------------------------------------------------
46 /** retrieves the value of an environment variable
47 @return <TRUE/> if and only if the retrieved string value is not empty
49 bool lcl_getEnvironmentValue( const sal_Char* _pAsciiEnvName, ::rtl::OUString& _rValue )
51 _rValue = ::rtl::OUString();
52 ::rtl::OUString sEnvName = ::rtl::OUString::createFromAscii( _pAsciiEnvName );
53 osl_getEnvironment( sEnvName.pData, &_rValue.pData );
54 return _rValue.getLength() != 0;
57 //-----------------------------------------------------------------
58 void lcl_convertStringListToUrls( const String& _rColonSeparatedList, ::std::vector< String >& _rTokens, bool _bFinalSlash )
60 const sal_Unicode s_cSeparator =
61 #if defined(WNT)
62 ';'
63 #else
64 ':'
65 #endif
67 xub_StrLen nTokens = _rColonSeparatedList.GetTokenCount( s_cSeparator );
68 _rTokens.resize( 0 ); _rTokens.reserve( nTokens );
69 for ( xub_StrLen i=0; i<nTokens; ++i )
71 // the current token in the list
72 String sCurrentToken = _rColonSeparatedList.GetToken( i, s_cSeparator );
73 if ( !sCurrentToken.Len() )
74 continue;
76 INetURLObject aCurrentURL;
78 String sURL;
79 if ( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( sCurrentToken, sURL ) )
80 aCurrentURL = INetURLObject( sURL );
81 else
83 // smart URL parsing, assuming FILE protocol
84 aCurrentURL = INetURLObject( sCurrentToken, INET_PROT_FILE );
87 if ( _bFinalSlash )
88 aCurrentURL.setFinalSlash( );
89 else
90 aCurrentURL.removeFinalSlash( );
91 _rTokens.push_back( aCurrentURL.GetMainURL( INetURLObject::NO_DECODE ) );
97 //=====================================================================
98 //= CheckURLAllowed
99 //=====================================================================
100 struct CheckURLAllowed
102 protected:
103 #ifdef WNT
104 SvtSysLocale m_aSysLocale;
105 #endif
106 String m_sCheckURL; // the URL to check
107 bool m_bAllowParent;
108 public:
109 inline CheckURLAllowed( const String& _rCheckURL, bool bAllowParent = true )
110 :m_sCheckURL( _rCheckURL ), m_bAllowParent( bAllowParent )
112 #ifdef WNT
113 // on windows, assume that the relevant file systems are case insensitive,
114 // thus normalize the URL
115 m_sCheckURL = m_aSysLocale.GetCharClass().toLower( m_sCheckURL, 0, m_sCheckURL.Len() );
116 #endif
119 bool operator()( const String& _rApprovedURL )
121 #ifdef WNT
122 // on windows, assume that the relevant file systems are case insensitive,
123 // thus normalize the URL
124 String sApprovedURL( m_aSysLocale.GetCharClass().toLower( _rApprovedURL, 0, _rApprovedURL.Len() ) );
125 #else
126 String sApprovedURL( _rApprovedURL );
127 #endif
129 xub_StrLen nLenApproved = sApprovedURL.Len();
130 xub_StrLen nLenChecked = m_sCheckURL.Len();
132 if ( nLenApproved > nLenChecked )
134 if ( m_bAllowParent )
136 if ( sApprovedURL.Search( m_sCheckURL ) == 0 )
138 if ( ( m_sCheckURL.GetChar( nLenChecked - 1 ) == '/' )
139 || ( sApprovedURL.GetChar( nLenChecked ) == '/' ) )
140 return true;
143 else
145 // just a difference in final slash?
146 if ( ( nLenApproved == ( nLenChecked + 1 ) ) &&
147 ( sApprovedURL.GetChar( nLenApproved - 1 ) == '/' ) )
148 return true;
150 return false;
152 else if ( nLenApproved < nLenChecked )
154 if ( m_sCheckURL.Search( sApprovedURL ) == 0 )
156 if ( ( sApprovedURL.GetChar( nLenApproved - 1 ) == '/' )
157 || ( m_sCheckURL.GetChar( nLenApproved ) == '/' ) )
158 return true;
160 return false;
162 else
164 // strings have equal length
165 return ( sApprovedURL == m_sCheckURL );
170 //=====================================================================
171 //= RestrictedPaths
172 //=====================================================================
173 //---------------------------------------------------------------------
174 RestrictedPaths::RestrictedPaths()
175 :m_bFilterIsEnabled( true )
177 ::rtl::OUString sRestrictedPathList;
178 if ( lcl_getEnvironmentValue( "RestrictedPath", sRestrictedPathList ) )
179 // append a final slash. This ensures that when we later on check
180 // for unrestricted paths, we don't allow paths like "/home/user35" just because
181 // "/home/user3" is allowed - with the final slash, we make it "/home/user3/".
182 lcl_convertStringListToUrls( sRestrictedPathList, m_aUnrestrictedURLs, true );
185 RestrictedPaths::~RestrictedPaths() {}
187 // --------------------------------------------------------------------
188 bool RestrictedPaths::isUrlAllowed( const String& _rURL ) const
190 if ( m_aUnrestrictedURLs.empty() || !m_bFilterIsEnabled )
191 return true;
193 ::std::vector< String >::const_iterator aApprovedURL = ::std::find_if(
194 m_aUnrestrictedURLs.begin(),
195 m_aUnrestrictedURLs.end(),
196 CheckURLAllowed( _rURL, true )
199 return ( aApprovedURL != m_aUnrestrictedURLs.end() );
202 // --------------------------------------------------------------------
203 bool RestrictedPaths::isUrlAllowed( const String& _rURL, bool allowParents ) const
205 if ( m_aUnrestrictedURLs.empty() || !m_bFilterIsEnabled )
206 return true;
208 ::std::vector< String >::const_iterator aApprovedURL = ::std::find_if(
209 m_aUnrestrictedURLs.begin(),
210 m_aUnrestrictedURLs.end(),
211 CheckURLAllowed( _rURL, allowParents )
214 return ( aApprovedURL != m_aUnrestrictedURLs.end() );
217 } // namespace svt