21#include <osl/diagnose.h>
23#include <rtl/byteseq.hxx>
24#include <rtl/ustrbuf.hxx>
31#include <com/sun/star/lang/XServiceInfo.hpp>
32#include <com/sun/star/security/XPolicy.hpp>
33#include <com/sun/star/security/AllPermission.hpp>
34#include <com/sun/star/security/RuntimePermission.hpp>
35#include <com/sun/star/io/FilePermission.hpp>
36#include <com/sun/star/connection/SocketPermission.hpp>
37#include <com/sun/star/uno/XComponentContext.hpp>
40#include <unordered_map>
43constexpr OUStringLiteral
IMPL_NAME =
u"com.sun.star.security.comp.stoc.FilePolicy";
46using namespace ::
cppu;
48using namespace css::uno;
52typedef WeakComponentImplHelper< security::XPolicy, lang::XServiceInfo >
t_helper;
62 Sequence< Any > m_defaultPermissions;
63 typedef std::unordered_map< OUString, Sequence< Any > > t_permissions;
64 t_permissions m_userPermissions;
68 virtual void SAL_CALL disposing()
override;
71 explicit FilePolicy( Reference< XComponentContext >
const & xComponentContext );
74 virtual Sequence< Any > SAL_CALL getPermissions(
75 OUString
const & userId )
override;
76 virtual Sequence< Any > SAL_CALL getDefaultPermissions()
override;
77 virtual void SAL_CALL refresh()
override;
85FilePolicy::FilePolicy( Reference< XComponentContext >
const & xComponentContext )
88 , m_ac( xComponentContext )
92void FilePolicy::disposing()
94 m_userPermissions.clear();
95 m_defaultPermissions = Sequence< Any >();
100Sequence< Any > FilePolicy::getPermissions(
101 OUString
const & userId )
110 t_permissions::iterator iFind( m_userPermissions.find( userId ) );
111 if (m_userPermissions.end() == iFind)
113 return Sequence< Any >();
117 return iFind->second;
121Sequence< Any > FilePolicy::getDefaultPermissions()
130 return m_defaultPermissions;
137 oslFileHandle m_file;
140 rtl::ByteSequence m_line;
149 {
return (
' ' == c ||
'\t' == c ||
'\n' == c ||
'\r' == c); }
150 void skipWhiteSpace();
153 {
return (
';' == c ||
',' == c ||
'{' == c ||
'}' == c); }
156 PolicyReader( OUString file, AccessControl & ac );
159 void error( std::u16string_view msg );
162 OUString assureToken();
163 OUString getQuotedToken();
164 OUString assureQuotedToken();
174 OUString msg =
"expected >" + OUStringChar(c) +
"<!";
178OUString PolicyReader::assureQuotedToken()
180 OUString token( getQuotedToken() );
182 error(
u"unexpected end of file!" );
186OUString PolicyReader::getQuotedToken()
189 OUStringBuffer buf( 32 );
192 error(
u"expected quoting >\"< character!" );
194 while (
'\0' != c &&
'\"' != c)
199 return buf.makeStringAndClear();
202OUString PolicyReader::assureToken()
205 if ( token.isEmpty())
206 error(
u"unexpected end of file!" );
210OUString PolicyReader::getToken()
214 if (isCharToken( c ))
215 return OUString( &c, 1 );
216 OUStringBuffer buf( 32 );
217 while (
'\0' != c && !isCharToken( c ) && !isWhiteSpace( c ))
223 return buf.makeStringAndClear();
226void PolicyReader::skipWhiteSpace()
233 while (isWhiteSpace( c ));
244 while (
'\n' != c &&
'\0' != c);
256 fini = (
'/' == c ||
'\0' == c);
268 error(
u"expected C/C++ like comment!" );
277 while (
'\n' != c &&
'\0' != c);
295 else if (m_pos == m_line.getLength())
300 else if (m_pos > m_line.getLength())
303 oslFileError rc = ::osl_isEndOfFile( m_file, &eof );
304 if (osl_File_E_None != rc)
305 error(
u"checking eof failed!" );
309 rc = ::osl_readLine( m_file,
reinterpret_cast< sal_Sequence **
>( &m_line ) );
310 if (osl_File_E_None != rc)
311 error(
u"read line failed!" );
313 if (! m_line.getLength())
320 return (m_line.getConstArray()[ m_pos++ ]);
323void PolicyReader::error( std::u16string_view msg )
326 "error processing file \"" + m_fileName +
327 "\" [line " + OUString::number(m_linepos) +
328 ", column " + OUString::number(m_pos) +
332PolicyReader::PolicyReader( OUString fileName, AccessControl & ac )
333 : m_fileName(std::move( fileName ))
338 ac.checkFilePermission( m_fileName,
"read" );
339 if (osl_File_E_None != ::osl_openFile( m_fileName.pData, &m_file, osl_File_OpenFlag_Read ))
345PolicyReader::~PolicyReader()
347 if ( ::osl_closeFile( m_file ) != osl_File_E_None ) {
352constexpr OUStringLiteral s_grant =
u"grant";
353constexpr OUStringLiteral s_user =
u"user";
354constexpr OUStringLiteral s_permission =
u"permission";
355constexpr OUStringLiteral s_openBrace =
u"{";
356constexpr OUStringLiteral s_closingBrace =
u"}";
358constexpr OUStringLiteral s_filePermission =
u"com.sun.star.io.FilePermission";
359constexpr OUStringLiteral s_socketPermission =
u"com.sun.star.connection.SocketPermission";
360constexpr OUStringLiteral s_runtimePermission =
u"com.sun.star.security.RuntimePermission";
361constexpr OUStringLiteral s_allPermission =
u"com.sun.star.security.AllPermission";
364void FilePolicy::refresh()
372 "/implementations/" +
IMPL_NAME +
"/file-name" ) >>= fileName;
373 if ( fileName.isEmpty() )
376 "name of policy file unknown!",
380 PolicyReader reader( fileName, m_ac );
383 Sequence< Any > defaultPermissions;
384 t_permissions userPermissions;
386 OUString token( reader.getToken() );
387 while (!token.isEmpty())
389 if ( token != s_grant )
390 reader.error(
u"expected >grant< token!" );
392 token = reader.assureToken();
393 if ( token == s_user )
395 userId = reader.assureQuotedToken();
396 token = reader.assureToken();
398 if ( token != s_openBrace )
399 reader.error(
u"expected opening brace >{<!" );
400 token = reader.assureToken();
402 while ( token != s_closingBrace )
404 if ( token != s_permission )
405 reader.error(
u"expected >permission< or closing brace >}<!" );
407 token = reader.assureToken();
409 if ( token == s_filePermission )
411 OUString url( reader.assureQuotedToken() );
412 reader.assureToken(
',' );
413 OUString actions( reader.assureQuotedToken() );
414 perm <<= io::FilePermission( url, actions );
416 else if ( token == s_socketPermission )
418 OUString host( reader.assureQuotedToken() );
419 reader.assureToken(
',' );
420 OUString actions( reader.assureQuotedToken() );
421 perm <<= connection::SocketPermission( host, actions );
423 else if ( token == s_runtimePermission )
425 OUString
name( reader.assureQuotedToken() );
426 perm <<= security::RuntimePermission(
name );
428 else if ( token == s_allPermission )
430 perm <<= security::AllPermission();
434 reader.error(
u"expected permission type!" );
437 reader.assureToken(
';' );
440 if (!userId.isEmpty())
442 Sequence< Any > perms( userPermissions[ userId ] );
443 sal_Int32 len = perms.getLength();
444 perms.realloc( len +1 );
445 perms.getArray()[ len ] = perm;
446 userPermissions[ userId ] = perms;
450 sal_Int32 len = defaultPermissions.getLength();
451 defaultPermissions.realloc( len +1 );
452 defaultPermissions.getArray()[ len ] = perm;
455 token = reader.assureToken();
458 reader.assureToken(
';' );
459 token = reader.getToken();
464 m_defaultPermissions = defaultPermissions;
465 m_userPermissions = userPermissions;
469OUString FilePolicy::getImplementationName()
474sal_Bool FilePolicy::supportsService( OUString
const & serviceName )
479Sequence< OUString > FilePolicy::getSupportedServiceNames()
481 return {
"com.sun.star.security.Policy" };
486extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
488 css::uno::XComponentContext *context,
489 css::uno::Sequence<css::uno::Any>
const &)
491 return cppu::acquire(
new FilePolicy(context));
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_security_comp_stoc_FilePolicy_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
constexpr OUStringLiteral IMPL_NAME
css::uno::Reference< XComponentContext > m_xComponentContext
local context
sal_Int32 getToken(const Context &rContext, const char *pToken)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
::cppu::WeakImplHelper< css::script::provider::XScriptProvider, css::script::browse::XBrowseNode, css::lang::XServiceInfo, css::lang::XInitialization, css::container::XNameContainer > t_helper
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)