LCOV - code coverage report
Current view: top level - svl/source/misc - restrictedpaths.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 63 0.0 %
Date: 2012-08-25 Functions: 0 11 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 112 0.0 %

           Branch data     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, ::rtl::OUString& _rValue )
      37                 :            :         {
      38                 :          0 :             _rValue = ::rtl::OUString();
      39                 :          0 :             ::rtl::OUString sEnvName = ::rtl::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 rtl::OUString& _rColonSeparatedList, ::std::vector< String >& _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 :                 rtl::OUString sCurrentToken = _rColonSeparatedList.getToken( 0, cSeparator, nIndex );
      59         [ #  # ]:          0 :                 if ( !sCurrentToken.isEmpty() )
      60                 :            :                 {
      61         [ #  # ]:          0 :                     INetURLObject aCurrentURL;
      62                 :            : 
      63                 :          0 :                     rtl::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                 :            :         String          m_sCheckURL;    // the URL to check
      91                 :            :         bool            m_bAllowParent;
      92                 :            :     public:
      93                 :          0 :         inline CheckURLAllowed( const String& _rCheckURL, bool bAllowParent = true )
      94                 :          0 :             :m_sCheckURL( _rCheckURL ), m_bAllowParent( bAllowParent )
      95                 :            :         {
      96                 :            :     #ifdef WNT
      97                 :            :             // on windows, assume that the relevant file systems are case insensitive,
      98                 :            :             // thus normalize the URL
      99                 :            :             m_sCheckURL = m_aSysLocale.GetCharClass().lowercase( m_sCheckURL, 0, m_sCheckURL.Len() );
     100                 :            :     #endif
     101                 :          0 :         }
     102                 :            : 
     103                 :          0 :         bool operator()( const String& _rApprovedURL )
     104                 :            :         {
     105                 :            :     #ifdef WNT
     106                 :            :             // on windows, assume that the relevant file systems are case insensitive,
     107                 :            :             // thus normalize the URL
     108                 :            :             String sApprovedURL( m_aSysLocale.GetCharClass().lowercase( _rApprovedURL, 0, _rApprovedURL.Len() ) );
     109                 :            :     #else
     110         [ #  # ]:          0 :             String sApprovedURL( _rApprovedURL );
     111                 :            :     #endif
     112                 :            : 
     113                 :          0 :             xub_StrLen nLenApproved = sApprovedURL.Len();
     114                 :          0 :             xub_StrLen nLenChecked  = m_sCheckURL.Len();
     115                 :            : 
     116         [ #  # ]:          0 :             if ( nLenApproved > nLenChecked )
     117                 :            :             {
     118         [ #  # ]:          0 :                 if ( m_bAllowParent )
     119                 :            :                 {
     120 [ #  # ][ #  # ]:          0 :                     if ( sApprovedURL.Search( m_sCheckURL ) == 0 )
     121                 :            :                     {
     122   [ #  #  #  # ]:          0 :                         if ( ( m_sCheckURL.GetChar( nLenChecked - 1 ) == '/' )
                 [ #  # ]
     123                 :          0 :                             || ( sApprovedURL.GetChar( nLenChecked ) == '/' ) )
     124                 :          0 :                             return true;
     125                 :            :                     }
     126                 :            :                 }
     127                 :            :                 else
     128                 :            :                 {
     129                 :            :                     // just a difference in final slash?
     130   [ #  #  #  # ]:          0 :                     if ( ( nLenApproved == ( nLenChecked + 1 ) ) &&
                 [ #  # ]
     131                 :          0 :                         ( sApprovedURL.GetChar( nLenApproved - 1 ) == '/' ) )
     132                 :          0 :                         return true;
     133                 :            :                 }
     134                 :          0 :                 return false;
     135                 :            :             }
     136         [ #  # ]:          0 :             else if ( nLenApproved < nLenChecked )
     137                 :            :             {
     138 [ #  # ][ #  # ]:          0 :                 if ( m_sCheckURL.Search( sApprovedURL ) == 0 )
     139                 :            :                 {
     140   [ #  #  #  # ]:          0 :                     if ( ( sApprovedURL.GetChar( nLenApproved - 1 ) == '/' )
                 [ #  # ]
     141                 :          0 :                         || ( m_sCheckURL.GetChar( nLenApproved ) == '/' ) )
     142                 :          0 :                         return true;
     143                 :            :                 }
     144                 :          0 :                 return false;
     145                 :            :             }
     146                 :            :             else
     147                 :            :             {
     148                 :            :                 // strings have equal length
     149         [ #  # ]:          0 :                 return ( sApprovedURL == m_sCheckURL );
     150         [ #  # ]:          0 :             }
     151                 :            :         }
     152                 :            :     };
     153                 :            : 
     154                 :            :     //=====================================================================
     155                 :            :     //= RestrictedPaths
     156                 :            :     //=====================================================================
     157                 :            :     //---------------------------------------------------------------------
     158                 :          0 :     RestrictedPaths::RestrictedPaths()
     159         [ #  # ]:          0 :         :m_bFilterIsEnabled( true )
     160                 :            :     {
     161                 :          0 :         ::rtl::OUString sRestrictedPathList;
     162 [ #  # ][ #  # ]:          0 :         if ( lcl_getEnvironmentValue( "RestrictedPath", sRestrictedPathList ) )
     163                 :            :             // append a final slash. This ensures that when we later on check
     164                 :            :             // for unrestricted paths, we don't allow paths like "/home/user35" just because
     165                 :            :             // "/home/user3" is allowed - with the final slash, we make it "/home/user3/".
     166         [ #  # ]:          0 :             lcl_convertStringListToUrls( sRestrictedPathList, m_aUnrestrictedURLs );
     167                 :          0 :     }
     168                 :            : 
     169         [ #  # ]:          0 :     RestrictedPaths::~RestrictedPaths() {}
     170                 :            : 
     171                 :            :     // --------------------------------------------------------------------
     172                 :          0 :     bool RestrictedPaths::isUrlAllowed( const String& _rURL ) const
     173                 :            :     {
     174 [ #  # ][ #  # ]:          0 :         if ( m_aUnrestrictedURLs.empty() || !m_bFilterIsEnabled )
                 [ #  # ]
     175                 :          0 :             return true;
     176                 :            : 
     177                 :            :         ::std::vector< String >::const_iterator aApprovedURL = ::std::find_if(
     178                 :            :             m_aUnrestrictedURLs.begin(),
     179                 :            :             m_aUnrestrictedURLs.end(),
     180                 :            :             CheckURLAllowed( _rURL, true )
     181 [ #  # ][ #  # ]:          0 :         );
                 [ #  # ]
     182                 :            : 
     183         [ #  # ]:          0 :         return ( aApprovedURL != m_aUnrestrictedURLs.end() );
     184                 :            :     }
     185                 :            : 
     186                 :            :     // --------------------------------------------------------------------
     187                 :          0 :     bool RestrictedPaths::isUrlAllowed( const String& _rURL, bool allowParents ) const
     188                 :            :     {
     189 [ #  # ][ #  # ]:          0 :         if ( m_aUnrestrictedURLs.empty() || !m_bFilterIsEnabled )
                 [ #  # ]
     190                 :          0 :             return true;
     191                 :            : 
     192                 :            :         ::std::vector< String >::const_iterator aApprovedURL = ::std::find_if(
     193                 :            :             m_aUnrestrictedURLs.begin(),
     194                 :            :             m_aUnrestrictedURLs.end(),
     195                 :            :             CheckURLAllowed( _rURL, allowParents )
     196 [ #  # ][ #  # ]:          0 :         );
                 [ #  # ]
     197                 :            : 
     198         [ #  # ]:          0 :         return ( aApprovedURL != m_aUnrestrictedURLs.end() );
     199                 :            :     }
     200                 :            : 
     201                 :            : }   // namespace svt
     202                 :            : 
     203                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10