1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_desktop.hxx"
26 
27 #include "dp_misc.h"
28 #include "dp_backend.h"
29 #include "dp_ucb.h"
30 #include "dp_interact.h"
31 #include "rtl/string.hxx"
32 #include "osl/file.hxx"
33 #include "ucbhelper/content.hxx"
34 #include "comphelper/servicedecl.hxx"
35 #include "svl/inettype.hxx"
36 #include "cppuhelper/implbase1.hxx"
37 #include "dp_executablebackenddb.hxx"
38 
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::ucb;
42 using namespace dp_misc;
43 using ::rtl::OUString;
44 
45 namespace dp_registry {
46 namespace backend {
47 namespace executable {
48 namespace {
49 
50 class BackendImpl : public ::dp_registry::backend::PackageRegistryBackend
51 {
52     class ExecutablePackageImpl : public ::dp_registry::backend::Package
53     {
54         BackendImpl * getMyBackend() const;
55 
56         // Package
57         virtual beans::Optional< beans::Ambiguous<sal_Bool> > isRegistered_(
58             ::osl::ResettableMutexGuard & guard,
59             ::rtl::Reference<dp_misc::AbortChannel> const & abortChannel,
60             Reference<XCommandEnvironment> const & xCmdEnv );
61         virtual void processPackage_(
62             ::osl::ResettableMutexGuard & guard,
63             bool registerPackage,
64             bool startup,
65             ::rtl::Reference<dp_misc::AbortChannel> const & abortChannel,
66             Reference<XCommandEnvironment> const & xCmdEnv );
67 
68         bool getFileAttributes(sal_uInt64& out_Attributes);
69         bool isUrlTargetInExtension();
70 
71     public:
ExecutablePackageImpl(::rtl::Reference<PackageRegistryBackend> const & myBackend,OUString const & url,OUString const & name,Reference<deployment::XPackageTypeInfo> const & xPackageType,bool bRemoved,OUString const & identifier)72         inline ExecutablePackageImpl(
73             ::rtl::Reference<PackageRegistryBackend> const & myBackend,
74             OUString const & url, OUString const & name,
75             Reference<deployment::XPackageTypeInfo> const & xPackageType,
76             bool bRemoved, OUString const & identifier)
77             : Package( myBackend, url, name, name /* display-name */,
78                        xPackageType, bRemoved, identifier)
79             {}
80     };
81     friend class ExecutablePackageImpl;
82 
83     typedef ::std::hash_map< OUString, Reference<XInterface>,
84                              ::rtl::OUStringHash > t_string2object;
85 
86     // PackageRegistryBackend
87     virtual Reference<deployment::XPackage> bindPackage_(
88         OUString const & url, OUString const & mediaType, sal_Bool bRemoved,
89         OUString const & identifier, Reference<XCommandEnvironment> const & xCmdEnv );
90 
91     void addDataToDb(OUString const & url);
92     bool hasActiveEntry(OUString const & url);
93     void revokeEntryFromDb(OUString const & url);
94 
95     Reference<deployment::XPackageTypeInfo> m_xExecutableTypeInfo;
96     std::auto_ptr<ExecutableBackendDb> m_backendDb;
97 public:
98     BackendImpl( Sequence<Any> const & args,
99                  Reference<XComponentContext> const & xComponentContext );
100 
101     // XPackageRegistry
102     virtual Sequence< Reference<deployment::XPackageTypeInfo> > SAL_CALL
103     getSupportedPackageTypes() throw (RuntimeException);
104     virtual void SAL_CALL packageRemoved(OUString const & url, OUString const & mediaType)
105         throw (deployment::DeploymentException,
106                uno::RuntimeException);
107 
108     using PackageRegistryBackend::disposing;
109 };
110 
111 
BackendImpl(Sequence<Any> const & args,Reference<XComponentContext> const & xComponentContext)112 BackendImpl::BackendImpl(
113     Sequence<Any> const & args,
114     Reference<XComponentContext> const & xComponentContext )
115     : PackageRegistryBackend( args, xComponentContext ),
116       m_xExecutableTypeInfo(new Package::TypeInfo(
117                                 OUSTR("application/vnd.sun.star.executable"),
118                                 OUSTR(""),
119                                 OUSTR("Executable"),
120                                 RID_IMG_COMPONENT,
121                                 RID_IMG_COMPONENT_HC ) )
122 {
123     if (!transientMode())
124     {
125         OUString dbFile = makeURL(getCachePath(), OUSTR("backenddb.xml"));
126         m_backendDb.reset(
127             new ExecutableBackendDb(getComponentContext(), dbFile));
128    }
129 }
130 
addDataToDb(OUString const & url)131 void BackendImpl::addDataToDb(OUString const & url)
132 {
133     if (m_backendDb.get())
134         m_backendDb->addEntry(url);
135 }
136 
revokeEntryFromDb(OUString const & url)137 void BackendImpl::revokeEntryFromDb(OUString const & url)
138 {
139     if (m_backendDb.get())
140         m_backendDb->revokeEntry(url);
141 }
142 
hasActiveEntry(OUString const & url)143 bool BackendImpl::hasActiveEntry(OUString const & url)
144 {
145     if (m_backendDb.get())
146         return m_backendDb->hasActiveEntry(url);
147     return false;
148 }
149 
150 
151 // XPackageRegistry
152 Sequence< Reference<deployment::XPackageTypeInfo> >
getSupportedPackageTypes()153 BackendImpl::getSupportedPackageTypes() throw (RuntimeException)
154 {
155     return Sequence<Reference<deployment::XPackageTypeInfo> >(
156         & m_xExecutableTypeInfo, 1);
157 }
158 
packageRemoved(OUString const & url,OUString const &)159 void BackendImpl::packageRemoved(OUString const & url, OUString const & /*mediaType*/)
160         throw (deployment::DeploymentException,
161                uno::RuntimeException)
162 {
163     if (m_backendDb.get())
164         m_backendDb->removeEntry(url);
165 }
166 
167 // PackageRegistryBackend
bindPackage_(OUString const & url,OUString const & mediaType,sal_Bool bRemoved,OUString const & identifier,Reference<XCommandEnvironment> const & xCmdEnv)168 Reference<deployment::XPackage> BackendImpl::bindPackage_(
169     OUString const & url, OUString const & mediaType, sal_Bool bRemoved,
170     OUString const & identifier, Reference<XCommandEnvironment> const & xCmdEnv )
171 {
172     if (mediaType.getLength() == 0)
173     {
174         throw lang::IllegalArgumentException(
175             StrCannotDetectMediaType::get() + url,
176             static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
177     }
178 
179     String type, subType;
180     INetContentTypeParameterList params;
181     if (INetContentTypes::parse( mediaType, type, subType, &params ))
182     {
183         if (type.EqualsIgnoreCaseAscii("application"))
184         {
185             OUString name;
186             if (!bRemoved)
187             {
188                 ::ucbhelper::Content ucbContent( url, xCmdEnv );
189                 name = ucbContent.getPropertyValue(
190                     dp_misc::StrTitle::get() ).get<OUString>();
191             }
192             if (subType.EqualsIgnoreCaseAscii("vnd.sun.star.executable"))
193             {
194                 return new BackendImpl::ExecutablePackageImpl(
195                     this, url, name,  m_xExecutableTypeInfo, bRemoved,
196                     identifier);
197             }
198         }
199     }
200     return Reference<deployment::XPackage>();
201 }
202 
203 //##############################################################################
204 
205 
206 // Package
getMyBackend() const207 BackendImpl * BackendImpl::ExecutablePackageImpl::getMyBackend() const
208 {
209     BackendImpl * pBackend = static_cast<BackendImpl *>(m_myBackend.get());
210     if (NULL == pBackend)
211     {
212         //May throw a DisposedException
213         check();
214         //We should never get here...
215         throw RuntimeException(
216             OUSTR("Failed to get the BackendImpl"),
217             static_cast<OWeakObject*>(const_cast<ExecutablePackageImpl *>(this)));
218     }
219     return pBackend;
220 }
221 
222 beans::Optional< beans::Ambiguous<sal_Bool> >
isRegistered_(::osl::ResettableMutexGuard &,::rtl::Reference<dp_misc::AbortChannel> const &,Reference<XCommandEnvironment> const &)223 BackendImpl::ExecutablePackageImpl::isRegistered_(
224     ::osl::ResettableMutexGuard &,
225     ::rtl::Reference<dp_misc::AbortChannel> const &,
226     Reference<XCommandEnvironment> const & )
227 {
228     bool registered = getMyBackend()->hasActiveEntry(getURL());
229     return beans::Optional< beans::Ambiguous<sal_Bool> >(
230             sal_True /* IsPresent */,
231                 beans::Ambiguous<sal_Bool>(
232                     registered, sal_False /* IsAmbiguous */ ) );
233 }
234 
processPackage_(::osl::ResettableMutexGuard &,bool doRegisterPackage,bool,::rtl::Reference<dp_misc::AbortChannel> const & abortChannel,Reference<XCommandEnvironment> const &)235 void BackendImpl::ExecutablePackageImpl::processPackage_(
236     ::osl::ResettableMutexGuard &,
237     bool doRegisterPackage,
238     bool /*startup*/,
239     ::rtl::Reference<dp_misc::AbortChannel> const & abortChannel,
240     Reference<XCommandEnvironment> const & /*xCmdEnv*/ )
241 {
242     checkAborted(abortChannel);
243     if (doRegisterPackage)
244     {
245         if (!isUrlTargetInExtension())
246         {
247             OSL_ASSERT(0);
248             return;
249         }
250         sal_uInt64 attributes = 0;
251         //Setting the executable attribut does not affect executables on Windows
252         if (getFileAttributes(attributes))
253         {
254             if(getMyBackend()->m_context.equals(OUSTR("user")))
255                 attributes |= osl_File_Attribute_OwnExe;
256             else if (getMyBackend()->m_context.equals(OUSTR("shared")))
257                 attributes |= (osl_File_Attribute_OwnExe | osl_File_Attribute_GrpExe
258                                | osl_File_Attribute_OthExe);
259             else if (!getMyBackend()->m_context.equals(OUSTR("bundled"))
260                 && !getMyBackend()->m_context.equals(OUSTR("bundled_prereg")))
261                 //Bundled extension are required to be in the properly
262                 //installed. That is an executable must have the right flags
263                 OSL_ASSERT(0);
264 
265             //This won't have affect on Windows
266             osl::File::setAttributes(
267                     dp_misc::expandUnoRcUrl(m_url), attributes);
268         }
269         getMyBackend()->addDataToDb(getURL());
270     }
271     else
272     {
273         getMyBackend()->revokeEntryFromDb(getURL());
274     }
275 }
276 
277 //We currently cannot check if this XPackage represents a content of a particular extension
278 //But we can check if we are within $UNO_USER_PACKAGES_CACHE etc.
279 //Done for security reasons. For example an extension manifest could contain a path to
280 //an executable outside the extension.
isUrlTargetInExtension()281 bool BackendImpl::ExecutablePackageImpl::isUrlTargetInExtension()
282 {
283     bool bSuccess = false;
284     OUString sExtensionDir;
285     if(getMyBackend()->m_context.equals(OUSTR("user")))
286         sExtensionDir = dp_misc::expandUnoRcTerm(OUSTR("$UNO_USER_PACKAGES_CACHE"));
287     else if (getMyBackend()->m_context.equals(OUSTR("shared")))
288         sExtensionDir = dp_misc::expandUnoRcTerm(OUSTR("$UNO_SHARED_PACKAGES_CACHE"));
289     else if (getMyBackend()->m_context.equals(OUSTR("bundled"))
290         || getMyBackend()->m_context.equals(OUSTR("bundled_prereg")))
291         sExtensionDir = dp_misc::expandUnoRcTerm(OUSTR("$BUNDLED_EXTENSIONS"));
292     else
293         OSL_ASSERT(0);
294     //remove file ellipses
295     if (osl::File::E_None == osl::File::getAbsoluteFileURL(OUString(), sExtensionDir, sExtensionDir))
296     {
297         OUString sFile;
298         if (osl::File::E_None == osl::File::getAbsoluteFileURL(
299             OUString(), dp_misc::expandUnoRcUrl(m_url), sFile))
300         {
301             if (sal_True == sFile.match(sExtensionDir, 0))
302                 bSuccess = true;
303         }
304     }
305     return bSuccess;
306 }
307 
getFileAttributes(sal_uInt64 & out_Attributes)308 bool BackendImpl::ExecutablePackageImpl::getFileAttributes(sal_uInt64& out_Attributes)
309 {
310     bool bSuccess = false;
311     const OUString url(dp_misc::expandUnoRcUrl(m_url));
312     osl::DirectoryItem item;
313     if (osl::FileBase::E_None == osl::DirectoryItem::get(url, item))
314     {
315         osl::FileStatus aStatus(osl_FileStatus_Mask_Attributes);
316         if( osl::FileBase::E_None == item.getFileStatus(aStatus))
317         {
318             out_Attributes = aStatus.getAttributes();
319             bSuccess = true;
320         }
321     }
322     return bSuccess;
323 }
324 
325 //##############################################################################
326 
327 
328 } // anon namespace
329 
330 namespace sdecl = comphelper::service_decl;
331 sdecl::class_<BackendImpl, sdecl::with_args<true> > serviceBI;
332 extern sdecl::ServiceDecl const serviceDecl(
333     serviceBI,
334     "com.sun.star.comp.deployment.executable.PackageRegistryBackend",
335     BACKEND_SERVICE_NAME );
336 
337 } // namespace component
338 } // namespace backend
339 } // namespace dp_registry
340 
341 
342