Bump version to 21.06.18.1
[LibreOffice.git] / stoc / source / security / file_policy.cxx
blob7b70602f982c21102dea4d82cd18c7a66fd2ffea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <osl/diagnose.h>
22 #include <osl/file.h>
23 #include <rtl/byteseq.hxx>
24 #include <rtl/ustrbuf.hxx>
26 #include <cppuhelper/access_control.hxx>
27 #include <cppuhelper/compbase.hxx>
28 #include <cppuhelper/supportsservice.hxx>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/security/XPolicy.hpp>
32 #include <com/sun/star/security/AllPermission.hpp>
33 #include <com/sun/star/security/RuntimePermission.hpp>
34 #include <com/sun/star/io/FilePermission.hpp>
35 #include <com/sun/star/connection/SocketPermission.hpp>
36 #include <com/sun/star/uno/XComponentContext.hpp>
38 #include <unordered_map>
40 #define IMPL_NAME "com.sun.star.security.comp.stoc.FilePolicy"
42 using namespace ::osl;
43 using namespace ::cppu;
44 using namespace ::com::sun::star;
45 using namespace css::uno;
47 namespace {
49 struct MutexHolder
51 Mutex m_mutex;
53 typedef WeakComponentImplHelper< security::XPolicy, lang::XServiceInfo > t_helper;
56 class FilePolicy
57 : public MutexHolder
58 , public t_helper
60 Reference< XComponentContext > m_xComponentContext;
61 AccessControl m_ac;
63 Sequence< Any > m_defaultPermissions;
64 typedef std::unordered_map< OUString, Sequence< Any > > t_permissions;
65 t_permissions m_userPermissions;
66 bool m_init;
68 protected:
69 virtual void SAL_CALL disposing() override;
71 public:
72 explicit FilePolicy( Reference< XComponentContext > const & xComponentContext );
74 // XPolicy impl
75 virtual Sequence< Any > SAL_CALL getPermissions(
76 OUString const & userId ) override;
77 virtual Sequence< Any > SAL_CALL getDefaultPermissions() override;
78 virtual void SAL_CALL refresh() override;
80 // XServiceInfo impl
81 virtual OUString SAL_CALL getImplementationName() override;
82 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) override;
83 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
86 FilePolicy::FilePolicy( Reference< XComponentContext > const & xComponentContext )
87 : t_helper( m_mutex )
88 , m_xComponentContext( xComponentContext )
89 , m_ac( xComponentContext )
90 , m_init( false )
93 void FilePolicy::disposing()
95 m_userPermissions.clear();
96 m_defaultPermissions = Sequence< Any >();
97 m_xComponentContext.clear();
101 Sequence< Any > FilePolicy::getPermissions(
102 OUString const & userId )
104 if (! m_init)
106 refresh();
107 m_init = true;
110 MutexGuard guard( m_mutex );
111 t_permissions::iterator iFind( m_userPermissions.find( userId ) );
112 if (m_userPermissions.end() == iFind)
114 return Sequence< Any >();
116 else
118 return iFind->second;
122 Sequence< Any > FilePolicy::getDefaultPermissions()
124 if (! m_init)
126 refresh();
127 m_init = true;
130 MutexGuard guard( m_mutex );
131 return m_defaultPermissions;
135 class PolicyReader
137 OUString m_fileName;
138 oslFileHandle m_file;
140 sal_Int32 m_linepos;
141 rtl::ByteSequence m_line;
142 sal_Int32 m_pos;
143 sal_Unicode m_back;
145 sal_Unicode get();
146 void back( sal_Unicode c )
147 { m_back = c; }
149 static bool isWhiteSpace( sal_Unicode c )
150 { return (' ' == c || '\t' == c || '\n' == c || '\r' == c); }
151 void skipWhiteSpace();
153 static bool isCharToken( sal_Unicode c )
154 { return (';' == c || ',' == c || '{' == c || '}' == c); }
156 public:
157 PolicyReader( OUString const & file, AccessControl & ac );
158 ~PolicyReader();
160 void error( OUString const & msg );
162 OUString getToken();
163 OUString assureToken();
164 OUString getQuotedToken();
165 OUString assureQuotedToken();
166 void assureToken( sal_Unicode token );
169 void PolicyReader::assureToken( sal_Unicode token )
171 skipWhiteSpace();
172 sal_Unicode c = get();
173 if (c == token)
174 return;
175 OUString msg = "expected >" + OUStringChar(c) + "<!";
176 error( msg );
179 OUString PolicyReader::assureQuotedToken()
181 OUString token( getQuotedToken() );
182 if (token.isEmpty())
183 error( "unexpected end of file!" );
184 return token;
187 OUString PolicyReader::getQuotedToken()
189 skipWhiteSpace();
190 OUStringBuffer buf( 32 );
191 sal_Unicode c = get();
192 if ('\"' != c)
193 error( "expected quoting >\"< character!" );
194 c = get();
195 while ('\0' != c && '\"' != c)
197 buf.append( c );
198 c = get();
200 return buf.makeStringAndClear();
203 OUString PolicyReader::assureToken()
205 OUString token( getToken() );
206 if ( token.isEmpty())
207 error( "unexpected end of file!" );
208 return token;
211 OUString PolicyReader::getToken()
213 skipWhiteSpace();
214 sal_Unicode c = get();
215 if (isCharToken( c ))
216 return OUString( &c, 1 );
217 OUStringBuffer buf( 32 );
218 while ('\0' != c && !isCharToken( c ) && !isWhiteSpace( c ))
220 buf.append( c );
221 c = get();
223 back( c );
224 return buf.makeStringAndClear();
227 void PolicyReader::skipWhiteSpace()
229 sal_Unicode c;
232 c = get();
234 while (isWhiteSpace( c )); // seeking next non-whitespace char
236 if ('/' == c) // C/C++ like comment
238 c = get();
239 if ('/' == c) // C++ like comment
243 c = get();
245 while ('\n' != c && '\0' != c); // seek eol/eof
246 skipWhiteSpace(); // cont skip on next line
248 else if ('*' == c) // C like comment
250 bool fini = true;
253 c = get();
254 if ('*' == c)
256 c = get();
257 fini = ('/' == c || '\0' == c);
259 else
261 fini = ('\0' == c);
264 while (! fini);
265 skipWhiteSpace(); // cont skip on next line
267 else
269 error( "expected C/C++ like comment!" );
272 else if ('#' == c) // script like comment
276 c = get();
278 while ('\n' != c && '\0' != c); // seek eol/eof
279 skipWhiteSpace(); // cont skip on next line
282 else // is token char
284 back( c );
288 sal_Unicode PolicyReader::get()
290 if ('\0' != m_back) // one char push back possible
292 sal_Unicode c = m_back;
293 m_back = '\0';
294 return c;
296 else if (m_pos == m_line.getLength()) // provide newline as whitespace
298 ++m_pos;
299 return '\n';
301 else if (m_pos > m_line.getLength()) // read new line
303 sal_Bool eof;
304 oslFileError rc = ::osl_isEndOfFile( m_file, &eof );
305 if (osl_File_E_None != rc)
306 error( "checking eof failed!" );
307 if (eof)
308 return '\0';
310 rc = ::osl_readLine( m_file, reinterpret_cast< sal_Sequence ** >( &m_line ) );
311 if (osl_File_E_None != rc)
312 error( "read line failed!" );
313 ++m_linepos;
314 if (! m_line.getLength()) // empty line read
316 m_pos = 1; // read new line next time
317 return '\n';
319 m_pos = 0;
321 return (m_line.getConstArray()[ m_pos++ ]);
324 void PolicyReader::error( OUString const & msg )
326 throw RuntimeException(
327 "error processing file \"" + m_fileName +
328 "\" [line " + OUString::number(m_linepos) +
329 ", column " + OUString::number(m_pos) +
330 "] " + msg);
333 PolicyReader::PolicyReader( OUString const & fileName, AccessControl & ac )
334 : m_fileName( fileName )
335 , m_linepos( 0 )
336 , m_pos( 1 ) // force readline
337 , m_back( '\0' )
339 ac.checkFilePermission( m_fileName, "read" );
340 if (osl_File_E_None != ::osl_openFile( m_fileName.pData, &m_file, osl_File_OpenFlag_Read ))
342 throw RuntimeException( "cannot open file \"" + m_fileName + "\"!" );
346 PolicyReader::~PolicyReader()
348 if ( ::osl_closeFile( m_file ) != osl_File_E_None ) {
349 OSL_ASSERT( false );
353 #define s_grant "grant"
354 #define s_user "user"
355 #define s_permission "permission"
356 #define s_openBrace "{"
357 #define s_closingBrace "}"
359 #define s_filePermission "com.sun.star.io.FilePermission"
360 #define s_socketPermission "com.sun.star.connection.SocketPermission"
361 #define s_runtimePermission "com.sun.star.security.RuntimePermission"
362 #define s_allPermission "com.sun.star.security.AllPermission"
365 void FilePolicy::refresh()
367 // read out file (the .../file-name value had originally been set in
368 // cppu::add_access_control_entries (cppuhelper/source/servicefactory.cxx)
369 // depending on various UNO_AC* bootstrap variables that are no longer
370 // supported, so this is effectively dead code):
371 OUString fileName;
372 m_xComponentContext->getValueByName(
373 "/implementations/" IMPL_NAME "/file-name" ) >>= fileName;
374 if ( fileName.isEmpty() )
376 throw RuntimeException(
377 "name of policy file unknown!",
378 static_cast<OWeakObject *>(this) );
381 PolicyReader reader( fileName, m_ac );
383 // fill these two
384 Sequence< Any > defaultPermissions;
385 t_permissions userPermissions;
387 OUString token( reader.getToken() );
388 while (!token.isEmpty())
390 if ( token != s_grant )
391 reader.error( "expected >grant< token!" );
392 OUString userId;
393 token = reader.assureToken();
394 if ( token == s_user ) // next token is user-id
396 userId = reader.assureQuotedToken();
397 token = reader.assureToken();
399 if ( token != s_openBrace )
400 reader.error( "expected opening brace >{<!" );
401 token = reader.assureToken();
402 // permissions list
403 while ( token != s_closingBrace )
405 if ( token != s_permission )
406 reader.error( "expected >permission< or closing brace >}<!" );
408 token = reader.assureToken(); // permission type
409 Any perm;
410 if ( token == s_filePermission ) // FilePermission
412 OUString url( reader.assureQuotedToken() );
413 reader.assureToken( ',' );
414 OUString actions( reader.assureQuotedToken() );
415 perm <<= io::FilePermission( url, actions );
417 else if ( token == s_socketPermission ) // SocketPermission
419 OUString host( reader.assureQuotedToken() );
420 reader.assureToken( ',' );
421 OUString actions( reader.assureQuotedToken() );
422 perm <<= connection::SocketPermission( host, actions );
424 else if ( token == s_runtimePermission ) // RuntimePermission
426 OUString name( reader.assureQuotedToken() );
427 perm <<= security::RuntimePermission( name );
429 else if ( token == s_allPermission ) // AllPermission
431 perm <<= security::AllPermission();
433 else
435 reader.error( "expected permission type!" );
438 reader.assureToken( ';' );
440 // insert
441 if (!userId.isEmpty())
443 Sequence< Any > perms( userPermissions[ userId ] );
444 sal_Int32 len = perms.getLength();
445 perms.realloc( len +1 );
446 perms[ len ] = perm;
447 userPermissions[ userId ] = perms;
449 else
451 sal_Int32 len = defaultPermissions.getLength();
452 defaultPermissions.realloc( len +1 );
453 defaultPermissions[ len ] = perm;
456 token = reader.assureToken(); // next permissions token
459 reader.assureToken( ';' ); // semi
460 token = reader.getToken(); // next grant token
463 // assign new ones
464 MutexGuard guard( m_mutex );
465 m_defaultPermissions = defaultPermissions;
466 m_userPermissions = userPermissions;
470 OUString FilePolicy::getImplementationName()
472 return IMPL_NAME;
475 sal_Bool FilePolicy::supportsService( OUString const & serviceName )
477 return cppu::supportsService(this, serviceName);
480 Sequence< OUString > FilePolicy::getSupportedServiceNames()
482 return { "com.sun.star.security.Policy" };
485 } // namespace
487 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
488 com_sun_star_security_comp_stoc_FilePolicy_get_implementation(
489 css::uno::XComponentContext *context,
490 css::uno::Sequence<css::uno::Any> const &)
492 return cppu::acquire(new FilePolicy(context));
495 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */