1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
53 typedef WeakComponentImplHelper
< security::XPolicy
, lang::XServiceInfo
> t_helper
;
60 Reference
< XComponentContext
> m_xComponentContext
;
63 Sequence
< Any
> m_defaultPermissions
;
64 typedef std::unordered_map
< OUString
, Sequence
< Any
> > t_permissions
;
65 t_permissions m_userPermissions
;
69 virtual void SAL_CALL
disposing() override
;
72 explicit FilePolicy( Reference
< XComponentContext
> const & xComponentContext
);
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
;
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
)
88 , m_xComponentContext( xComponentContext
)
89 , m_ac( xComponentContext
)
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
)
110 MutexGuard
guard( m_mutex
);
111 t_permissions::iterator
iFind( m_userPermissions
.find( userId
) );
112 if (m_userPermissions
.end() == iFind
)
114 return Sequence
< Any
>();
118 return iFind
->second
;
122 Sequence
< Any
> FilePolicy::getDefaultPermissions()
130 MutexGuard
guard( m_mutex
);
131 return m_defaultPermissions
;
138 oslFileHandle m_file
;
141 rtl::ByteSequence m_line
;
146 void back( sal_Unicode 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
); }
157 PolicyReader( OUString
const & file
, AccessControl
& ac
);
160 void error( OUString
const & msg
);
163 OUString
assureToken();
164 OUString
getQuotedToken();
165 OUString
assureQuotedToken();
166 void assureToken( sal_Unicode token
);
169 void PolicyReader::assureToken( sal_Unicode token
)
172 sal_Unicode c
= get();
175 OUString msg
= "expected >" + OUStringChar(c
) + "<!";
179 OUString
PolicyReader::assureQuotedToken()
181 OUString
token( getQuotedToken() );
183 error( "unexpected end of file!" );
187 OUString
PolicyReader::getQuotedToken()
190 OUStringBuffer
buf( 32 );
191 sal_Unicode c
= get();
193 error( "expected quoting >\"< character!" );
195 while ('\0' != c
&& '\"' != c
)
200 return buf
.makeStringAndClear();
203 OUString
PolicyReader::assureToken()
205 OUString
token( getToken() );
206 if ( token
.isEmpty())
207 error( "unexpected end of file!" );
211 OUString
PolicyReader::getToken()
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
))
224 return buf
.makeStringAndClear();
227 void PolicyReader::skipWhiteSpace()
234 while (isWhiteSpace( c
)); // seeking next non-whitespace char
236 if ('/' == c
) // C/C++ like comment
239 if ('/' == c
) // C++ like comment
245 while ('\n' != c
&& '\0' != c
); // seek eol/eof
246 skipWhiteSpace(); // cont skip on next line
248 else if ('*' == c
) // C like comment
257 fini
= ('/' == c
|| '\0' == c
);
265 skipWhiteSpace(); // cont skip on next line
269 error( "expected C/C++ like comment!" );
272 else if ('#' == c
) // script like comment
278 while ('\n' != c
&& '\0' != c
); // seek eol/eof
279 skipWhiteSpace(); // cont skip on next line
282 else // is token char
288 sal_Unicode
PolicyReader::get()
290 if ('\0' != m_back
) // one char push back possible
292 sal_Unicode c
= m_back
;
296 else if (m_pos
== m_line
.getLength()) // provide newline as whitespace
301 else if (m_pos
> m_line
.getLength()) // read new line
304 oslFileError rc
= ::osl_isEndOfFile( m_file
, &eof
);
305 if (osl_File_E_None
!= rc
)
306 error( "checking eof failed!" );
310 rc
= ::osl_readLine( m_file
, reinterpret_cast< sal_Sequence
** >( &m_line
) );
311 if (osl_File_E_None
!= rc
)
312 error( "read line failed!" );
314 if (! m_line
.getLength()) // empty line read
316 m_pos
= 1; // read new line next time
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
) +
333 PolicyReader::PolicyReader( OUString
const & fileName
, AccessControl
& ac
)
334 : m_fileName( fileName
)
336 , m_pos( 1 ) // force readline
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
) {
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):
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
);
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!" );
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();
403 while ( token
!= s_closingBrace
)
405 if ( token
!= s_permission
)
406 reader
.error( "expected >permission< or closing brace >}<!" );
408 token
= reader
.assureToken(); // permission type
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();
435 reader
.error( "expected permission type!" );
438 reader
.assureToken( ';' );
441 if (!userId
.isEmpty())
443 Sequence
< Any
> perms( userPermissions
[ userId
] );
444 sal_Int32 len
= perms
.getLength();
445 perms
.realloc( len
+1 );
447 userPermissions
[ userId
] = perms
;
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
464 MutexGuard
guard( m_mutex
);
465 m_defaultPermissions
= defaultPermissions
;
466 m_userPermissions
= userPermissions
;
470 OUString
FilePolicy::getImplementationName()
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" };
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: */