1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: restrictedpaths.cxx,v $
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>
36 #include <osl/process.h>
37 #include <tools/urlobj.hxx>
38 #include <unotools/localfilehelper.hxx>
39 #include <svtools/syslocale.hxx>
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
=
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() )
76 INetURLObject aCurrentURL
;
79 if ( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( sCurrentToken
, sURL
) )
80 aCurrentURL
= INetURLObject( sURL
);
83 // smart URL parsing, assuming FILE protocol
84 aCurrentURL
= INetURLObject( sCurrentToken
, INET_PROT_FILE
);
88 aCurrentURL
.setFinalSlash( );
90 aCurrentURL
.removeFinalSlash( );
91 _rTokens
.push_back( aCurrentURL
.GetMainURL( INetURLObject::NO_DECODE
) );
97 //=====================================================================
99 //=====================================================================
100 struct CheckURLAllowed
104 SvtSysLocale m_aSysLocale
;
106 String m_sCheckURL
; // the URL to check
109 inline CheckURLAllowed( const String
& _rCheckURL
, bool bAllowParent
= true )
110 :m_sCheckURL( _rCheckURL
), m_bAllowParent( bAllowParent
)
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() );
119 bool operator()( const String
& _rApprovedURL
)
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() ) );
126 String
sApprovedURL( _rApprovedURL
);
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
) == '/' ) )
145 // just a difference in final slash?
146 if ( ( nLenApproved
== ( nLenChecked
+ 1 ) ) &&
147 ( sApprovedURL
.GetChar( nLenApproved
- 1 ) == '/' ) )
152 else if ( nLenApproved
< nLenChecked
)
154 if ( m_sCheckURL
.Search( sApprovedURL
) == 0 )
156 if ( ( sApprovedURL
.GetChar( nLenApproved
- 1 ) == '/' )
157 || ( m_sCheckURL
.GetChar( nLenApproved
) == '/' ) )
164 // strings have equal length
165 return ( sApprovedURL
== m_sCheckURL
);
170 //=====================================================================
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
)
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
)
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() );