LCOV - code coverage report
Current view: top level - oox/source/core - filterdetect.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 130 182 71.4 %
Date: 2014-04-11 Functions: 25 38 65.8 %
Legend: Lines: hit not hit

          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 "oox/core/filterdetect.hxx"
      21             : 
      22             : #include <com/sun/star/io/TempFile.hpp>
      23             : #include <com/sun/star/io/XStream.hpp>
      24             : #include <comphelper/docpasswordhelper.hxx>
      25             : #include <unotools/mediadescriptor.hxx>
      26             : #include <cppuhelper/supportsservice.hxx>
      27             : 
      28             : #include "oox/core/fastparser.hxx"
      29             : #include "oox/helper/attributelist.hxx"
      30             : #include "oox/helper/zipstorage.hxx"
      31             : #include "oox/ole/olestorage.hxx"
      32             : 
      33             : #include "oox/crypto/DocumentDecryption.hxx"
      34             : 
      35             : #include <com/sun/star/uri/UriReferenceFactory.hpp>
      36             : 
      37             : namespace oox {
      38             : namespace core {
      39             : 
      40             : using namespace ::com::sun::star::beans;
      41             : using namespace ::com::sun::star::io;
      42             : using namespace ::com::sun::star::lang;
      43             : using namespace ::com::sun::star::uno;
      44             : using namespace ::com::sun::star::xml::sax;
      45             : using namespace ::com::sun::star::uri;
      46             : 
      47             : using utl::MediaDescriptor;
      48             : using comphelper::SequenceAsHashMap;
      49             : using comphelper::IDocPasswordVerifier;
      50             : using comphelper::DocPasswordVerifierResult;
      51             : 
      52        1014 : FilterDetectDocHandler::FilterDetectDocHandler( const  Reference< XComponentContext >& rxContext, OUString& rFilterName ) :
      53             :     mrFilterName( rFilterName ),
      54        1014 :     mxContext( rxContext )
      55             : {
      56        1014 :     maContextStack.reserve( 2 );
      57        1014 : }
      58             : 
      59        2028 : FilterDetectDocHandler::~FilterDetectDocHandler()
      60             : {
      61        2028 : }
      62             : 
      63        1824 : void SAL_CALL FilterDetectDocHandler::startDocument()
      64             :     throw (SAXException, RuntimeException, std::exception)
      65             : {
      66        1824 : }
      67             : 
      68        1824 : void SAL_CALL FilterDetectDocHandler::endDocument()
      69             :     throw (SAXException, RuntimeException, std::exception)
      70             : {
      71        1824 : }
      72             : 
      73        1824 : void SAL_CALL FilterDetectDocHandler::setDocumentLocator( const Reference<XLocator>& /*xLocator*/ )
      74             :     throw (SAXException, RuntimeException, std::exception)
      75             : {
      76        1824 : }
      77             : 
      78       18462 : void SAL_CALL FilterDetectDocHandler::startFastElement(
      79             :         sal_Int32 nElement, const Reference< XFastAttributeList >& rAttribs )
      80             :     throw (SAXException,RuntimeException, std::exception)
      81             : {
      82       18462 :     AttributeList aAttribs( rAttribs );
      83       18462 :     switch ( nElement )
      84             :     {
      85             :         // cases for _rels/.rels
      86             :         case PR_TOKEN( Relationships ):
      87         912 :         break;
      88             :         case PR_TOKEN( Relationship ):
      89        2782 :             if( !maContextStack.empty() && (maContextStack.back() == PR_TOKEN( Relationships )) )
      90        2782 :                 parseRelationship( aAttribs );
      91        2782 :         break;
      92             : 
      93             :         // cases for [Content_Types].xml
      94             :         case PC_TOKEN( Types ):
      95         912 :         break;
      96             :         case PC_TOKEN( Default ):
      97        1317 :             if( !maContextStack.empty() && (maContextStack.back() == PC_TOKEN( Types )) )
      98        1317 :                 parseContentTypesDefault( aAttribs );
      99        1317 :         break;
     100             :         case PC_TOKEN( Override ):
     101       12539 :             if( !maContextStack.empty() && (maContextStack.back() == PC_TOKEN( Types )) )
     102       12539 :                 parseContentTypesOverride( aAttribs );
     103       12539 :         break;
     104             :     }
     105       18462 :     maContextStack.push_back( nElement );
     106       18462 : }
     107             : 
     108           0 : void SAL_CALL FilterDetectDocHandler::startUnknownElement(
     109             :     const OUString& /*Namespace*/, const OUString& /*Name*/, const Reference<XFastAttributeList>& /*Attribs*/ )
     110             :     throw (SAXException, RuntimeException, std::exception)
     111             : {
     112           0 : }
     113             : 
     114       18462 : void SAL_CALL FilterDetectDocHandler::endFastElement( sal_Int32 /*nElement*/ )
     115             :     throw (SAXException, RuntimeException, std::exception)
     116             : {
     117       18462 :     maContextStack.pop_back();
     118       18462 : }
     119             : 
     120           0 : void SAL_CALL FilterDetectDocHandler::endUnknownElement(
     121             :     const OUString& /*Namespace*/, const OUString& /*Name*/ ) throw (SAXException, RuntimeException, std::exception)
     122             : {
     123           0 : }
     124             : 
     125       18462 : Reference<XFastContextHandler> SAL_CALL FilterDetectDocHandler::createFastChildContext(
     126             :     sal_Int32 /*Element*/, const Reference<XFastAttributeList>& /*Attribs*/ )
     127             :     throw (SAXException, RuntimeException, std::exception)
     128             : {
     129       18462 :     return this;
     130             : }
     131             : 
     132           0 : Reference<XFastContextHandler> SAL_CALL FilterDetectDocHandler::createUnknownChildContext(
     133             :     const OUString& /*Namespace*/, const OUString& /*Name*/, const Reference<XFastAttributeList>& /*Attribs*/)
     134             :     throw (SAXException, RuntimeException, std::exception)
     135             : {
     136           0 :     return this;
     137             : }
     138             : 
     139         799 : void SAL_CALL FilterDetectDocHandler::characters( const OUString& /*aChars*/ )
     140             :     throw (SAXException, RuntimeException, std::exception)
     141             : {
     142         799 : }
     143             : 
     144           0 : void SAL_CALL FilterDetectDocHandler::ignorableWhitespace( const OUString& /*aWhitespaces*/ )
     145             :     throw (SAXException, RuntimeException)
     146             : {
     147           0 : }
     148             : 
     149           0 : void SAL_CALL FilterDetectDocHandler::processingInstruction(
     150             :     const OUString& /*aTarget*/, const OUString& /*aData*/ )
     151             :     throw (SAXException, RuntimeException)
     152             : {
     153           0 : }
     154             : 
     155        2782 : void FilterDetectDocHandler::parseRelationship( const AttributeList& rAttribs )
     156             : {
     157        2782 :     OUString aType = rAttribs.getString( XML_Type, OUString() );
     158        5564 :     if ( aType == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" // OOXML Transitional
     159        2782 :             || aType == "http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument" ) //OOXML strict
     160             :     {
     161         912 :         Reference<XUriReferenceFactory> xFactory = UriReferenceFactory::create( mxContext );
     162             :         try
     163             :         {
     164             :              // use '/' to representent the root of the zip package ( and provide a 'file' scheme to
     165             :              // keep the XUriReference implementation happy )
     166         912 :              Reference< XUriReference > xBase = xFactory->parse( OUString("file:///") );
     167             : 
     168        1824 :              Reference< XUriReference > xPart = xFactory->parse(  rAttribs.getString( XML_Target, OUString() ) );
     169        1824 :              Reference< XUriReference > xAbs = xFactory->makeAbsolute(  xBase, xPart, sal_True, RelativeUriExcessParentSegments_RETAIN );
     170             : 
     171         912 :              if ( xAbs.is() )
     172        1824 :                  maTargetPath = xAbs->getPath();
     173             :         }
     174           0 :         catch( const Exception& )
     175             :         {
     176         912 :         }
     177        2782 :     }
     178        2782 : }
     179             : 
     180        1472 : OUString FilterDetectDocHandler::getFilterNameFromContentType( const OUString& rContentType ) const
     181             : {
     182        2043 :     if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" ) ||
     183         571 :         rContentType.equalsAscii("application/vnd.ms-word.document.macroEnabled.main+xml" ) )
     184         901 :         return OUString( "writer_MS_Word_2007" );
     185             : 
     186        1141 :     if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml") ||
     187         570 :         rContentType.equalsAscii("application/vnd.ms-word.template.macroEnabledTemplate.main+xml") )
     188           1 :         return OUString( "writer_MS_Word_2007_Template" );
     189             : 
     190        1132 :     if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml") ||
     191         562 :         rContentType.equalsAscii("application/vnd.ms-excel.sheet.macroEnabled.main+xml") )
     192           8 :         return OUString( "MS Excel 2007 XML" );
     193             : 
     194        1124 :     if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml") ||
     195         562 :         rContentType.equalsAscii("application/vnd.ms-excel.template.macroEnabled.main+xml") )
     196           0 :         return OUString( "MS Excel 2007 XML Template" );
     197             : 
     198         562 :     if ( rContentType == "application/vnd.ms-excel.sheet.binary.macroEnabled.main" )
     199           0 :         return OUString( "MS Excel 2007 Binary" );
     200             : 
     201        1122 :     if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml") ||
     202         560 :         rContentType.equalsAscii("application/vnd.ms-powerpoint.presentation.macroEnabled.main+xml") )
     203           2 :         return OUString( "MS PowerPoint 2007 XML" );
     204             : 
     205        1120 :     if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.presentationml.slideshow.main+xml") ||
     206         560 :         rContentType.equalsAscii("application/vnd.ms-powerpoint.slideshow.macroEnabled.main+xml") )
     207           0 :         return OUString( "MS PowerPoint 2007 XML AutoPlay" );
     208             : 
     209        1120 :     if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.presentationml.template.main+xml") ||
     210         560 :         rContentType.equalsAscii("application/vnd.ms-powerpoint.template.macroEnabled.main+xml") )
     211           0 :         return OUString( "MS PowerPoint 2007 XML Template" );
     212             : 
     213         560 :     return OUString();
     214             : }
     215             : 
     216        1317 : void FilterDetectDocHandler::parseContentTypesDefault( const AttributeList& rAttribs )
     217             : {
     218             :     // only if no overridden part name found
     219        1317 :     if( mrFilterName.isEmpty() )
     220             :     {
     221             :         // check if target path ends with extension
     222        1294 :         OUString aExtension = rAttribs.getString( XML_Extension, OUString() );
     223        1294 :         sal_Int32 nExtPos = maTargetPath.getLength() - aExtension.getLength();
     224        1294 :         if( (nExtPos > 0) && (maTargetPath[ nExtPos - 1 ] == '.') && maTargetPath.match( aExtension, nExtPos ) )
     225         560 :             mrFilterName = getFilterNameFromContentType( rAttribs.getString( XML_ContentType, OUString() ) );
     226             :     }
     227        1317 : }
     228             : 
     229       12539 : void FilterDetectDocHandler::parseContentTypesOverride( const AttributeList& rAttribs )
     230             : {
     231       12539 :     if( rAttribs.getString( XML_PartName, OUString() ).equals( maTargetPath ) )
     232         912 :         mrFilterName = getFilterNameFromContentType( rAttribs.getString( XML_ContentType, OUString() ) );
     233       12539 : }
     234             : 
     235             : /* Helper for XServiceInfo */
     236          12 : Sequence< OUString > FilterDetect_getSupportedServiceNames()
     237             : {
     238          12 :     Sequence< OUString > aServiceNames( 1 );
     239          12 :     aServiceNames[ 0 ] = "com.sun.star.frame.ExtendedTypeDetection";
     240          12 :     return aServiceNames;
     241             : }
     242             : 
     243             : /* Helper for XServiceInfo */
     244          45 : OUString FilterDetect_getImplementationName()
     245             : {
     246          45 :     return OUString( "com.sun.star.comp.oox.FormatDetector" );
     247             : }
     248             : 
     249             : /* Helper for registry */
     250        1074 : Reference< XInterface > SAL_CALL FilterDetect_createInstance( const Reference< XComponentContext >& rxContext ) throw( Exception )
     251             : {
     252        1074 :     return static_cast< ::cppu::OWeakObject* >( new FilterDetect( rxContext ) );
     253             : }
     254             : 
     255        3179 : FilterDetect::FilterDetect( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
     256        3179 :     mxContext( rxContext, UNO_SET_THROW )
     257             : {
     258        3179 : }
     259             : 
     260        4253 : FilterDetect::~FilterDetect()
     261             : {
     262        4253 : }
     263             : 
     264             : namespace
     265             : {
     266             : 
     267        3179 : bool lclIsZipPackage( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm )
     268             : {
     269        3179 :     ZipStorage aZipStorage( rxContext, rxInStrm );
     270        3179 :     return aZipStorage.isStorage();
     271             : }
     272             : 
     273           0 : class PasswordVerifier : public IDocPasswordVerifier
     274             : {
     275             : public:
     276             :     explicit PasswordVerifier( DocumentDecryption& aDecryptor );
     277             : 
     278             :     virtual DocPasswordVerifierResult verifyPassword( const OUString& rPassword, Sequence<NamedValue>& rEncryptionData ) SAL_OVERRIDE;
     279             : 
     280             :     virtual DocPasswordVerifierResult verifyEncryptionData( const Sequence<NamedValue>& rEncryptionData ) SAL_OVERRIDE;
     281             : private:
     282             :     DocumentDecryption& mDecryptor;
     283             : };
     284             : 
     285           0 : PasswordVerifier::PasswordVerifier( DocumentDecryption& aDecryptor ) :
     286           0 :     mDecryptor(aDecryptor)
     287           0 : {}
     288             : 
     289           0 : comphelper::DocPasswordVerifierResult PasswordVerifier::verifyPassword( const OUString& rPassword, Sequence<NamedValue>& rEncryptionData )
     290             : {
     291           0 :     if(mDecryptor.generateEncryptionKey(rPassword))
     292           0 :         rEncryptionData = mDecryptor.createEncryptionData(rPassword);
     293             : 
     294           0 :     return rEncryptionData.hasElements() ? comphelper::DocPasswordVerifierResult_OK : comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
     295             : }
     296             : 
     297           0 : comphelper::DocPasswordVerifierResult PasswordVerifier::verifyEncryptionData( const Sequence<NamedValue>& rEncryptionData )
     298             : {
     299           0 :     comphelper::DocPasswordVerifierResult aResult = comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
     300           0 :     if (DocumentDecryption::checkEncryptionData(rEncryptionData))
     301           0 :         aResult = comphelper::DocPasswordVerifierResult_OK;
     302           0 :     return aResult;
     303             : }
     304             : 
     305             : } // namespace
     306             : 
     307             : 
     308        3179 : Reference< XInputStream > FilterDetect::extractUnencryptedPackage( MediaDescriptor& rMediaDescriptor ) const
     309             : {
     310             :     // try the plain input stream
     311        3179 :     Reference<XInputStream> xInputStream( rMediaDescriptor[ MediaDescriptor::PROP_INPUTSTREAM() ], UNO_QUERY );
     312        3179 :     if( !xInputStream.is() || lclIsZipPackage( mxContext, xInputStream ) )
     313        3119 :         return xInputStream;
     314             : 
     315             :     // check if a temporary file is passed in the 'ComponentData' property
     316         120 :     Reference<XStream> xDecrypted( rMediaDescriptor.getComponentDataEntry( "DecryptedPackage" ), UNO_QUERY );
     317          60 :     if( xDecrypted.is() )
     318             :     {
     319           0 :         Reference<XInputStream> xDecryptedInputStream = xDecrypted->getInputStream();
     320           0 :         if( lclIsZipPackage( mxContext, xDecryptedInputStream ) )
     321           0 :             return xDecryptedInputStream;
     322             :     }
     323             : 
     324             :     // try to decrypt an encrypted OLE package
     325         120 :     oox::ole::OleStorage aOleStorage( mxContext, xInputStream, false );
     326          60 :     if( aOleStorage.isStorage() )
     327             :     {
     328             :         try
     329             :         {
     330          15 :             DocumentDecryption aDecryptor(aOleStorage, mxContext);
     331             : 
     332          15 :             if( aDecryptor.readEncryptionInfo() )
     333             :             {
     334             :                 /*  "VelvetSweatshop" is the built-in default encryption
     335             :                     password used by MS Excel for the "workbook protection"
     336             :                     feature with password. Try this first before prompting the
     337             :                     user for a password. */
     338           0 :                 std::vector<OUString> aDefaultPasswords;
     339           0 :                 aDefaultPasswords.push_back("VelvetSweatshop");
     340             : 
     341             :                 /*  Use the comphelper password helper to request a password.
     342             :                     This helper returns either with the correct password
     343             :                     (according to the verifier), or with an empty string if
     344             :                     user has cancelled the password input dialog. */
     345           0 :                 PasswordVerifier aVerifier( aDecryptor );
     346           0 :                 Sequence<NamedValue> aEncryptionData;
     347           0 :                 aEncryptionData = rMediaDescriptor.requestAndVerifyDocPassword(
     348             :                                                 aVerifier,
     349             :                                                 comphelper::DocPasswordRequestType_MS,
     350           0 :                                                 &aDefaultPasswords );
     351             : 
     352           0 :                 if( aEncryptionData.getLength() == 0 )
     353             :                 {
     354           0 :                     rMediaDescriptor[ MediaDescriptor::PROP_ABORTED() ] <<= true;
     355             :                 }
     356             :                 else
     357             :                 {
     358             :                     // create temporary file for unencrypted package
     359           0 :                     Reference<XStream> xTempFile( TempFile::create(mxContext), UNO_QUERY_THROW );
     360           0 :                     aDecryptor.decrypt( xTempFile );
     361             : 
     362             :                     // store temp file in media descriptor to keep it alive
     363           0 :                     rMediaDescriptor.setComponentDataEntry( "DecryptedPackage", Any( xTempFile ) );
     364             : 
     365           0 :                     Reference<XInputStream> xDecryptedInputStream = xTempFile->getInputStream();
     366           0 :                     if( lclIsZipPackage( mxContext, xDecryptedInputStream ) )
     367           0 :                         return xDecryptedInputStream;
     368           0 :                 }
     369          15 :             }
     370             :         }
     371          15 :         catch( const Exception& )
     372             :         {
     373             :         }
     374             :     }
     375        3239 :     return Reference<XInputStream>();
     376             : }
     377             : 
     378             : // com.sun.star.lang.XServiceInfo interface -----------------------------------
     379             : 
     380           0 : OUString SAL_CALL FilterDetect::getImplementationName() throw( RuntimeException, std::exception )
     381             : {
     382           0 :     return FilterDetect_getImplementationName();
     383             : }
     384             : 
     385           0 : sal_Bool SAL_CALL FilterDetect::supportsService( const OUString& rServiceName ) throw( RuntimeException, std::exception )
     386             : {
     387           0 :     return cppu::supportsService(this, rServiceName);
     388             : }
     389             : 
     390           0 : Sequence< OUString > SAL_CALL FilterDetect::getSupportedServiceNames() throw( RuntimeException, std::exception )
     391             : {
     392           0 :     return FilterDetect_getSupportedServiceNames();
     393             : }
     394             : 
     395             : // com.sun.star.document.XExtendedFilterDetection interface -------------------
     396             : 
     397        1074 : OUString SAL_CALL FilterDetect::detect( Sequence< PropertyValue >& rMediaDescSeq ) throw( RuntimeException, std::exception )
     398             : {
     399        1074 :     OUString aFilterName;
     400        2148 :     MediaDescriptor aMediaDescriptor( rMediaDescSeq );
     401             : 
     402             :     /*  Check that the user has not choosen to abort detection, e.g. by hitting
     403             :         'Cancel' in the password input dialog. This may happen because this
     404             :         filter detection is used by different filters. */
     405        1074 :     bool bAborted = aMediaDescriptor.getUnpackedValueOrDefault( MediaDescriptor::PROP_ABORTED(), false );
     406        1074 :     if( !bAborted ) try
     407             :     {
     408        1074 :         aMediaDescriptor.addInputStream();
     409             : 
     410             :         /*  Get the unencrypted input stream. This may include creation of a
     411             :             temporary file that contains the decrypted package. This temporary
     412             :             file will be stored in the 'ComponentData' property of the media
     413             :             descriptor. */
     414        1134 :         Reference< XInputStream > xInputStream( extractUnencryptedPackage( aMediaDescriptor ), UNO_SET_THROW );
     415             : 
     416             :         // stream must be a ZIP package
     417        2028 :         ZipStorage aZipStorage( mxContext, xInputStream );
     418        1014 :         if( aZipStorage.isStorage() )
     419             :         {
     420             :             // create the fast parser, register the XML namespaces, set document handler
     421        1014 :             FastParser aParser( mxContext );
     422        1014 :             aParser.registerNamespace( NMSP_packageRel );
     423        1014 :             aParser.registerNamespace( NMSP_officeRel );
     424        1014 :             aParser.registerNamespace( NMSP_packageContentTypes );
     425        1014 :             aParser.setDocumentHandler( new FilterDetectDocHandler( mxContext, aFilterName ) );
     426             : 
     427             :             /*  Parse '_rels/.rels' to get the target path and '[Content_Types].xml'
     428             :                 to determine the content type of the part at the target path. */
     429        1116 :             aParser.parseStream( aZipStorage, "_rels/.rels" );
     430         912 :             aParser.parseStream( aZipStorage, "[Content_Types].xml" );
     431        1014 :         }
     432             :     }
     433         162 :     catch( const Exception& )
     434             :     {
     435             :     }
     436             : 
     437             :     // write back changed media descriptor members
     438        1074 :     aMediaDescriptor >> rMediaDescSeq;
     439        2148 :     return aFilterName;
     440             : }
     441             : 
     442             : 
     443             : 
     444             : } // namespace core
     445         177 : } // namespace oox
     446             : 
     447             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10