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 <sal/config.h>
21 
22 #include <chrono>
23 #include <cstring>
24 
25 #include <rtl/bootstrap.hxx>
26 #include <rtl/random.h>
27 #include <rtl/ustrbuf.hxx>
28 #include <rtl/uri.hxx>
29 #include <osl/file.hxx>
30 #include <osl/security.hxx>
31 #include <osl/thread.hxx>
32 #include <o3tl/char16_t2wchar_t.hxx>
33 #include <osl/process.h>
34 
35 #include <cppuhelper/bootstrap.hxx>
36 #include <cppuhelper/findsofficepath.h>
37 
38 #include <com/sun/star/bridge/UnoUrlResolver.hpp>
39 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
40 
41 #include "macro_expander.hxx"
42 
43 namespace com :: sun :: star :: uno { class XComponentContext; }
44 
45 using namespace ::osl;
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
48 
49 using rtl::Bootstrap;
50 
51 namespace cppu
52 {
53 
BootstrapException()54 BootstrapException::BootstrapException()
55 {
56 }
57 
BootstrapException(const OUString & rMessage)58 BootstrapException::BootstrapException( const OUString & rMessage )
59     :m_aMessage( rMessage )
60 {
61 }
62 
BootstrapException(const BootstrapException & e)63 BootstrapException::BootstrapException( const BootstrapException & e )
64 {
65     m_aMessage = e.m_aMessage;
66 }
67 
~BootstrapException()68 BootstrapException::~BootstrapException()
69 {
70 }
71 
operator =(const BootstrapException & e)72 BootstrapException & BootstrapException::operator=( const BootstrapException & e )
73 {
74     m_aMessage = e.m_aMessage;
75     return *this;
76 }
77 
getMessage() const78 const OUString & BootstrapException::getMessage() const
79 {
80     return m_aMessage;
81 }
82 
bootstrap()83 Reference< XComponentContext > SAL_CALL bootstrap()
84 {
85     Reference< XComponentContext > xRemoteContext;
86 
87     try
88     {
89         auto* p1 = cppuhelper_detail_findSofficePath();
90         if (p1 == nullptr) {
91             throw BootstrapException(
92                 "no soffice installation found!");
93         }
94         OUString p2;
95 #if defined(_WIN32)
96         p2 = o3tl::toU(p1);
97         free(p1);
98 #else
99         bool bOk = rtl_convertStringToUString(
100                 &p2.pData, p1, std::strlen(p1), osl_getThreadTextEncoding(),
101                 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
102                  RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
103                  RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR));
104         free(p1);
105         if (!bOk)
106         {
107             throw BootstrapException(
108                 "bad characters in soffice installation path!");
109         }
110 #endif
111         OUString path;
112         if (osl::FileBase::getFileURLFromSystemPath(p2, path) !=
113             osl::FileBase::E_None)
114         {
115             throw BootstrapException(
116                 "cannot convert soffice installation path to URL!");
117         }
118         if (!path.isEmpty() && !path.endsWith("/")) {
119             path += "/";
120         }
121 
122         OUString uri;
123         if (!Bootstrap::get("URE_BOOTSTRAP", uri)) {
124             Bootstrap::set(
125                 "URE_BOOTSTRAP",
126                 Bootstrap::encode(
127                     path +
128 #if defined MACOSX
129                     "../Resources/"
130 #endif
131                     SAL_CONFIGFILE("fundamental")));
132         }
133 
134         // create default local component context
135         Reference< XComponentContext > xLocalContext(
136             defaultBootstrap_InitialComponentContext() );
137         if ( !xLocalContext.is() )
138             throw BootstrapException( "no local component context!" );
139 
140         // create a random pipe name
141         rtlRandomPool hPool = rtl_random_createPool();
142         if ( hPool == nullptr )
143             throw BootstrapException( "cannot create random pool!" );
144         sal_uInt8 bytes[ 16 ];
145         if ( rtl_random_getBytes( hPool, bytes, SAL_N_ELEMENTS( bytes ) )
146             != rtl_Random_E_None )
147             throw BootstrapException( "random pool error!" );
148         rtl_random_destroyPool( hPool );
149         OUStringBuffer buf("uno");
150         for (unsigned char byte : bytes)
151             buf.append( static_cast< sal_Int32 >( byte ) );
152         OUString sPipeName( buf.makeStringAndClear() );
153 
154         // arguments
155         OUString args [] = {
156             OUString("--nologo"),
157             OUString("--nodefault"),
158             OUString("--norestore"),
159             OUString("--nolockcheck"),
160             OUString("--accept=pipe,name=" + sPipeName + ";urp;")
161         };
162         rtl_uString * ar_args [] = {
163             args[ 0 ].pData,
164             args[ 1 ].pData,
165             args[ 2 ].pData,
166             args[ 3 ].pData,
167             args[ 4 ].pData
168         };
169         ::osl::Security sec;
170 
171         // start office process
172         oslProcess hProcess = nullptr;
173         oslProcessError rc = osl_executeProcess(
174             OUString(path + "soffice").pData, ar_args, SAL_N_ELEMENTS( ar_args ),
175             osl_Process_DETACHED,
176             sec.getHandle(),
177             nullptr, // => current working dir
178             nullptr, 0, // => no env vars
179             &hProcess );
180         switch ( rc )
181         {
182             case osl_Process_E_None:
183                 osl_freeProcessHandle( hProcess );
184                 break;
185             case osl_Process_E_NotFound:
186                 throw BootstrapException( "image not found!" );
187             case osl_Process_E_TimedOut:
188                 throw BootstrapException( "timeout occurred!" );
189             case osl_Process_E_NoPermission:
190                 throw BootstrapException( "permission denied!" );
191             case osl_Process_E_Unknown:
192                 throw BootstrapException( "unknown error!" );
193             case osl_Process_E_InvalidError:
194             default:
195                 throw BootstrapException( "unmapped error!" );
196         }
197 
198         // create a URL resolver
199         Reference< bridge::XUnoUrlResolver > xUrlResolver(
200             bridge::UnoUrlResolver::create( xLocalContext ) );
201 
202         // connection string
203         OUString sConnectString( "uno:pipe,name=" + sPipeName + ";urp;StarOffice.ComponentContext" );
204 
205         // wait until office is started
206         for ( ; ; )
207         {
208             try
209             {
210                 // try to connect to office
211                 xRemoteContext.set(
212                     xUrlResolver->resolve( sConnectString ), UNO_QUERY_THROW );
213                 break;
214             }
215             catch ( connection::NoConnectException & )
216             {
217                 // wait 500 ms, then try to connect again
218                 ::osl::Thread::wait( std::chrono::milliseconds(500) );
219             }
220         }
221     }
222     catch ( Exception & e )
223     {
224         throw BootstrapException(
225             "unexpected UNO exception caught: " + e.Message );
226     }
227 
228     return xRemoteContext;
229 }
230 
bootstrap_expandUri(OUString const & uri)231 OUString bootstrap_expandUri(OUString const & uri) {
232     OUString rest;
233     return uri.startsWith("vnd.sun.star.expand:", &rest)
234         ? cppuhelper::detail::expandMacros(
235             rtl::Uri::decode(
236                 rest, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8))
237         : uri;
238 }
239 
240 } // namespace cppu
241 
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
243