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 :
21 : #include <vector>
22 :
23 : #include "rtl/string.hxx"
24 : #include "rtl/bootstrap.hxx"
25 : #include "rtl/strbuf.hxx"
26 : #include "osl/diagnose.h"
27 : #include "osl/file.h"
28 : #include "osl/module.h"
29 : #include "osl/process.h"
30 : #include "cppuhelper/shlib.hxx"
31 : #include "cppuhelper/factory.hxx"
32 : #include "cppuhelper/component_context.hxx"
33 : #include "cppuhelper/bootstrap.hxx"
34 :
35 : #include "com/sun/star/uno/DeploymentException.hpp"
36 : #include "com/sun/star/uno/XComponentContext.hpp"
37 : #include "com/sun/star/lang/XInitialization.hpp"
38 : #include <com/sun/star/lang/XServiceInfo.hpp>
39 : #include "com/sun/star/lang/XSingleServiceFactory.hpp"
40 : #include "com/sun/star/lang/XSingleComponentFactory.hpp"
41 : #include "com/sun/star/beans/XPropertySet.hpp"
42 : #include "com/sun/star/container/XSet.hpp"
43 : #include "com/sun/star/container/XHierarchicalNameAccess.hpp"
44 : #include "com/sun/star/registry/XSimpleRegistry.hpp"
45 : #include "com/sun/star/uno/SecurityException.hpp"
46 : #if OSL_DEBUG_LEVEL > 1
47 : #include <stdio.h>
48 : #endif
49 :
50 : #include "macro_expander.hxx"
51 : #include "paths.hxx"
52 : #include "servicefactory_detail.hxx"
53 :
54 : #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
55 :
56 :
57 : using namespace ::rtl;
58 : using namespace ::osl;
59 : using namespace ::com::sun::star;
60 : using namespace ::com::sun::star::uno;
61 :
62 : namespace cppu
63 : {
64 :
65 259 : static Reference< XInterface > SAL_CALL createInstance(
66 : Reference< XInterface > const & xFactory,
67 : Reference< XComponentContext > const & xContext =
68 : Reference< XComponentContext >() )
69 : {
70 259 : Reference< lang::XSingleComponentFactory > xFac( xFactory, UNO_QUERY );
71 259 : if (xFac.is())
72 : {
73 259 : return xFac->createInstanceWithContext( xContext );
74 : }
75 : else
76 : {
77 0 : Reference< lang::XSingleServiceFactory > xFac2( xFactory, UNO_QUERY );
78 0 : if (xFac2.is())
79 : {
80 : OSL_ENSURE( !xContext.is(), "### ignoring context!" );
81 0 : return xFac2->createInstance();
82 0 : }
83 : }
84 : throw RuntimeException(
85 : OUSTR("no factory object given!"),
86 0 : Reference< XInterface >() );
87 : }
88 :
89 : /** bootstrap variables:
90 :
91 : UNO_AC=<mode> [mandatory]
92 : -- mode := { on, off, dynamic-only, single-user, single-default-user }
93 : UNO_AC_SERVICE=<service_name> [optional]
94 : -- override ac singleton service name
95 : UNO_AC_SINGLEUSER=<user-id|nothing> [optional]
96 : -- run with this user id or with default user policy (<nothing>)
97 : set UNO_AC=single-[default-]user
98 : UNO_AC_USERCACHE_SIZE=<cache_size>
99 : -- number of user permission sets to be cached
100 :
101 : UNO_AC_POLICYSERVICE=<service_name> [optional]
102 : -- override policy singleton service name
103 : UNO_AC_POLICYFILE=<file_url> [optional]
104 : -- read policy out of simple text file
105 : */
106 259 : void add_access_control_entries(
107 : ::std::vector< ContextEntry_Init > * values,
108 : Bootstrap const & bootstrap )
109 : SAL_THROW( (Exception) )
110 : {
111 259 : ContextEntry_Init entry;
112 259 : ::std::vector< ContextEntry_Init > & context_values = *values;
113 :
114 259 : OUString ac_policy;
115 259 : if (bootstrap.getFrom( OUSTR("UNO_AC_POLICYSERVICE"), ac_policy ))
116 : {
117 : // overridden service name
118 : // - policy singleton
119 0 : entry.bLateInitService = true;
120 0 : entry.name = OUSTR("/singletons/com.sun.star.security.thePolicy");
121 0 : entry.value <<= ac_policy;
122 0 : context_values.push_back( entry );
123 : }
124 259 : else if (bootstrap.getFrom( OUSTR("UNO_AC_POLICYFILE"), ac_policy ))
125 : {
126 : // check for file policy
127 : // - file policy prop: file-name
128 0 : if (0 != ac_policy.compareToAscii(
129 0 : RTL_CONSTASCII_STRINGPARAM("file:///") ))
130 : {
131 : // no file url
132 0 : OUString baseDir;
133 0 : if ( ::osl_getProcessWorkingDir( &baseDir.pData )
134 : != osl_Process_E_None )
135 : {
136 : OSL_ASSERT( false );
137 : }
138 0 : OUString fileURL;
139 0 : if ( ::osl_getAbsoluteFileURL(
140 0 : baseDir.pData, ac_policy.pData, &fileURL.pData )
141 : != osl_File_E_None )
142 : {
143 : OSL_ASSERT( false );
144 : }
145 0 : ac_policy = fileURL;
146 : }
147 :
148 0 : entry.bLateInitService = false;
149 : entry.name =
150 0 : OUSTR("/implementations/com.sun.star.security.comp.stoc.FilePolicy/"
151 0 : "file-name");
152 0 : entry.value <<= ac_policy;
153 0 : context_values.push_back( entry );
154 : // - policy singleton
155 0 : entry.bLateInitService = true;
156 0 : entry.name = OUSTR("/singletons/com.sun.star.security.thePolicy");
157 0 : entry.value <<= OUSTR("com.sun.star.security.comp.stoc.FilePolicy");
158 0 : context_values.push_back( entry );
159 : } // else policy singleton comes from storage
160 :
161 259 : OUString ac_mode;
162 259 : if (! bootstrap.getFrom( OUSTR("UNO_AC"), ac_mode ))
163 : {
164 259 : ac_mode = OUSTR("off"); // default
165 : }
166 259 : OUString ac_user;
167 259 : if (bootstrap.getFrom( OUSTR("UNO_AC_SINGLEUSER"), ac_user ))
168 : {
169 : // ac in single-user mode
170 0 : if (!ac_user.isEmpty())
171 : {
172 : // - ac prop: single-user-id
173 0 : entry.bLateInitService = false;
174 : entry.name =
175 0 : OUSTR("/services/com.sun.star.security.AccessController/"
176 0 : "single-user-id");
177 0 : entry.value <<= ac_user;
178 0 : context_values.push_back( entry );
179 0 : if ( ac_mode != "single-user" )
180 : {
181 : throw SecurityException(
182 : OUSTR("set UNO_AC=single-user "
183 : "if you set UNO_AC_SINGLEUSER=<user-id>!"),
184 0 : Reference< XInterface >() );
185 : }
186 : }
187 : else
188 : {
189 0 : if ( ac_mode != "single-default-user" )
190 : {
191 : throw SecurityException(
192 : OUSTR("set UNO_AC=single-default-user "
193 : "if you set UNO_AC_SINGLEUSER=<nothing>!"),
194 0 : Reference< XInterface >() );
195 : }
196 : }
197 : }
198 :
199 259 : OUString ac_service;
200 259 : if (! bootstrap.getFrom( OUSTR("UNO_AC_SERVICE"), ac_service ))
201 : {
202 : // override service name
203 259 : ac_service = OUSTR("com.sun.star.security.AccessController"); // default
204 : // ac = OUSTR("com.sun.star.security.comp.stoc.AccessController");
205 : }
206 :
207 : // - ac prop: user-cache-size
208 259 : OUString ac_cache;
209 259 : if (bootstrap.getFrom( OUSTR("UNO_AC_USERCACHE_SIZE"), ac_cache ))
210 : {
211 : // ac cache size
212 0 : sal_Int32 n = ac_cache.toInt32();
213 0 : if (0 < n)
214 : {
215 0 : entry.bLateInitService = false;
216 : entry.name =
217 0 : OUSTR("/services/com.sun.star.security.AccessController/"
218 0 : "user-cache-size");
219 0 : entry.value <<= n;
220 0 : context_values.push_back( entry );
221 : }
222 : }
223 :
224 : // - ac prop: mode
225 : // { "off", "on", "dynamic-only", "single-user", "single-default-user" }
226 259 : entry.bLateInitService = false;
227 259 : entry.name = OUSTR("/services/com.sun.star.security.AccessController/mode");
228 259 : entry.value <<= ac_mode;
229 259 : context_values.push_back( entry );
230 : // - ac singleton
231 259 : entry.bLateInitService = true;
232 259 : entry.name = OUSTR("/singletons/com.sun.star.security.theAccessController");
233 259 : entry.value <<= ac_service;
234 259 : context_values.push_back( entry );
235 259 : }
236 :
237 : namespace {
238 259 : void addFactories(
239 : char const * const * ppNames /* implname, ..., 0 */,
240 : OUString const & bootstrapPath,
241 : Reference< lang::XMultiComponentFactory > const & xMgr,
242 : Reference< registry::XRegistryKey > const & xKey )
243 : SAL_THROW( (Exception) )
244 : {
245 259 : Reference< container::XSet > xSet( xMgr, UNO_QUERY );
246 : OSL_ASSERT( xSet.is() );
247 259 : Reference< lang::XMultiServiceFactory > xSF( xMgr, UNO_QUERY );
248 :
249 2590 : while (*ppNames)
250 : {
251 2072 : OUString implName( OUString::createFromAscii( *ppNames++ ) );
252 :
253 : Any aFac( makeAny( loadSharedLibComponentFactory(
254 : OUSTR("bootstrap.uno" SAL_DLLEXTENSION),
255 2072 : bootstrapPath, implName, xSF, xKey ) ) );
256 2072 : xSet->insert( aFac );
257 : #if OSL_DEBUG_LEVEL > 1
258 : if (xSet->has( aFac ))
259 : {
260 : Reference< lang::XServiceInfo > xInfo;
261 : if (aFac >>= xInfo)
262 : {
263 : ::fprintf(
264 : stderr, "> implementation %s supports: ", ppNames[ -1 ] );
265 : Sequence< OUString > supported(
266 : xInfo->getSupportedServiceNames() );
267 : for ( sal_Int32 nPos = supported.getLength(); nPos--; )
268 : {
269 : OString str( OUStringToOString(
270 : supported[ nPos ], RTL_TEXTENCODING_ASCII_US ) );
271 : ::fprintf( stderr, nPos ? "%s, " : "%s\n", str.getStr() );
272 : }
273 : }
274 : else
275 : {
276 : ::fprintf(
277 : stderr,
278 : "> implementation %s provides NO lang::XServiceInfo!!!\n",
279 : ppNames[ -1 ] );
280 : }
281 : }
282 : #endif
283 : #if OSL_DEBUG_LEVEL > 0
284 : if (! xSet->has( aFac ))
285 : {
286 : OStringBuffer buf( 64 );
287 : buf.append( "### failed inserting shared lib \"" );
288 : buf.append( "bootstrap.uno" SAL_DLLEXTENSION );
289 : buf.append( "\"!!!" );
290 : OString str( buf.makeStringAndClear() );
291 : OSL_FAIL( str.getStr() );
292 : }
293 : #endif
294 2331 : }
295 259 : }
296 :
297 : } // namespace
298 :
299 259 : Reference< lang::XMultiComponentFactory > bootstrapInitialSF(
300 : OUString const & rBootstrapPath )
301 : SAL_THROW( (Exception) )
302 : {
303 : OUString const & bootstrap_path =
304 259 : rBootstrapPath.isEmpty() ? get_this_libpath() : rBootstrapPath;
305 :
306 : Reference< lang::XMultiComponentFactory > xMgr(
307 : createInstance(
308 : loadSharedLibComponentFactory(
309 : OUSTR("bootstrap.uno" SAL_DLLEXTENSION), bootstrap_path,
310 : OUSTR("com.sun.star.comp.stoc.ORegistryServiceManager"),
311 : Reference< lang::XMultiServiceFactory >(),
312 : Reference< registry::XRegistryKey >() ) ),
313 259 : UNO_QUERY );
314 :
315 : // add initial bootstrap services
316 : static char const * ar[] = {
317 : "com.sun.star.comp.stoc.OServiceManagerWrapper",
318 : "com.sun.star.comp.stoc.DLLComponentLoader",
319 : "com.sun.star.comp.stoc.SimpleRegistry",
320 : "com.sun.star.comp.stoc.NestedRegistry",
321 : "com.sun.star.comp.stoc.TypeDescriptionManager",
322 : "com.sun.star.comp.stoc.ImplementationRegistration",
323 : "com.sun.star.security.comp.stoc.AccessController",
324 : "com.sun.star.security.comp.stoc.FilePolicy",
325 : 0
326 : };
327 : addFactories(
328 : ar, bootstrap_path,
329 259 : xMgr, Reference< registry::XRegistryKey >() );
330 :
331 259 : return xMgr;
332 : }
333 :
334 : }
335 :
336 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|