23#include <osl/process.h>
24#include <osl/socket.hxx>
25#include <osl/mutex.hxx>
27#include <rtl/string.hxx>
28#include <rtl/ustrbuf.hxx>
32#include <com/sun/star/security/RuntimePermission.hpp>
33#include <com/sun/star/security/AllPermission.hpp>
34#include <com/sun/star/io/FilePermission.hpp>
35#include <com/sun/star/connection/SocketPermission.hpp>
36#include <com/sun/star/security/AccessControlException.hpp>
37#include <com/sun/star/uno/Sequence.hxx>
43using namespace css::uno;
50 OUString
const & items,
char const *
const * strings )
61 while (strings[
nPos ])
63 if (item.equalsAscii( strings[
nPos ] ))
65 mask |= (0x80000000 >>
nPos);
70#if OSL_DEBUG_LEVEL > 0
71 if (! strings[
nPos ])
73 SAL_WARN(
"stoc",
"ignoring unknown socket action: " << item );
82 sal_Int32 mask,
char const *
const * strings )
84 OUStringBuffer buf( 48 );
87 if (0x80000000 & mask)
89 buf.appendAscii( *strings );
96 return buf.makeStringAndClear();
101class SocketPermission :
public Permission
114 inline bool resolveHost()
const;
118 connection::SocketPermission
const & perm,
120 virtual bool implies( Permission
const & perm )
const override;
121 virtual OUString
toString()
const override;
128SocketPermission::SocketPermission(
129 connection::SocketPermission
const & perm,
131 : Permission( SOCKET, next )
144 sal_Int32 colon =
m_host.indexOf(
':' );
148 sal_Int32 minus =
m_host.indexOf(
'-', colon +1 );
153 else if (minus == (colon +1))
157 else if (minus == (
m_host.getLength() -1))
169inline bool SocketPermission::resolveHost()
const
178 SocketAddr::resolveHostname(
m_host, addr );
180 m_resolveErr = (::osl_Socket_Ok != ::osl_getDottedInetAddrOfSocketAddr(
181 addr.getHandle(), &ip.pData ));
185 MutexGuard guard( Mutex::getGlobalMutex() );
198 if (SOCKET != perm.m_type)
200 SocketPermission
const & demanded =
static_cast< SocketPermission
const &
>( perm );
203 if ((
m_actions & demanded.m_actions) != demanded.m_actions)
213 if (
m_host.equalsIgnoreAsciiCase( demanded.m_host ))
218 OUString
const & demanded_host = demanded.m_host;
219 if (demanded_host.getLength() <=
m_host.getLength())
221 sal_Int32 len =
m_host.getLength() -1;
222 return (0 == ::rtl_ustr_compareIgnoreAsciiCase_WithLength(
223 demanded_host.getStr() + demanded_host.getLength() - len, len,
224 m_host.pData->buffer + 1, len ));
226 if (demanded.m_wildCardHost)
232 if (! demanded.resolveHost())
234 return m_ip == demanded.m_ip;
237OUString SocketPermission::toString()
const
239 OUStringBuffer buf( 48 );
241 buf.append(
"com.sun.star.connection.SocketPermission (host=\""
245 buf.append(
"[" +
m_ip +
"]" );
261 buf.append(
"\", actions=\""
264 return buf.makeStringAndClear();
269class FilePermission :
public Permission
279 io::FilePermission
const & perm,
281 virtual bool implies( Permission
const & perm )
const override;
282 virtual OUString
toString()
const override;
291 static OUString s_workingDir = []() {
293 ::osl_getProcessWorkingDir(&workingDir.pData);
299FilePermission::FilePermission(
300 io::FilePermission
const & perm,
302 : Permission(
FILE, next )
314 else if (
m_url ==
"-" )
318 else if (!
m_url.startsWith(
"file:///"))
322 oslFileError rc = ::osl_getAbsoluteFileURL(
324 m_url = (osl_File_E_None == rc ? out : perm.URL);
328 if (9 <
m_url.getLength() &&
'|' ==
m_url[ 9 ])
330 constexpr OUStringLiteral s_colon =
u":";
340 if (FILE != perm.m_type)
342 FilePermission
const & demanded =
static_cast< FilePermission
const &
>( perm );
345 if ((
m_actions & demanded.m_actions) != demanded.m_actions)
351 if (demanded.m_allFiles)
355 if (
m_url.equalsIgnoreAsciiCase( demanded.m_url ))
358 if (
m_url == demanded.m_url )
361 if (
m_url.getLength() > demanded.m_url.getLength())
364 if (
m_url.endsWith(
"/-"))
367 sal_Int32 len =
m_url.getLength() -1;
369 return (0 == ::rtl_ustr_compareIgnoreAsciiCase_WithLength(
370 demanded.m_url.pData->buffer, len,
m_url.pData->buffer, len ));
372 return (0 == ::rtl_ustr_reverseCompare_WithLength(
373 demanded.m_url.pData->buffer, len,
m_url.pData->buffer, len ));
377 if (
m_url.endsWith(
"/*"))
380 sal_Int32 len =
m_url.getLength() -1;
382 return ((0 == ::rtl_ustr_compareIgnoreAsciiCase_WithLength(
383 demanded.m_url.pData->buffer, len,
m_url.pData->buffer, len )) &&
384 (0 > demanded.m_url.indexOf(
'/', len )));
386 return ((0 == ::rtl_ustr_reverseCompare_WithLength(
387 demanded.m_url.pData->buffer, len,
m_url.pData->buffer, len )) &&
388 (0 > demanded.m_url.indexOf(
'/', len )));
394OUString FilePermission::toString()
const
398 "com.sun.star.io.FilePermission (url=\"" +
m_url
405class RuntimePermission :
public Permission
411 security::RuntimePermission
const & perm,
413 : Permission( RUNTIME, next )
416 virtual bool implies( Permission
const & perm )
const override;
417 virtual OUString
toString()
const override;
425 if (RUNTIME != perm.m_type)
427 RuntimePermission
const & demanded =
static_cast< RuntimePermission
const &
>( perm );
430 return m_name == demanded.m_name;
433OUString RuntimePermission::toString()
const
435 return "com.sun.star.security.RuntimePermission (name=\"" +
445OUString AllPermission::toString()
const
447 return "com.sun.star.security.AllPermission";
451PermissionCollection::PermissionCollection(
453 : m_head( addition.m_head )
455 Any const * perms = permissions.getConstArray();
456 for ( sal_Int32 nPos = permissions.getLength(); nPos--; )
458 Any const & perm = perms[
nPos ];
459 Type const & perm_type = perm.getValueType();
464 m_head =
new FilePermission(
465 *
static_cast< io::FilePermission
const *
>( perm.pData ), m_head );
469 m_head =
new SocketPermission(
470 *
static_cast< connection::SocketPermission
const *
>( perm.pData ), m_head );
474 m_head =
new RuntimePermission(
475 *
static_cast< security::RuntimePermission
const *
>( perm.pData ), m_head );
483 throw RuntimeException(
"checking for unsupported permission type: " + perm_type.getTypeName() );
489Sequence< OUString > PermissionCollection::toStrings()
const
491 std::vector< OUString > strings;
492 strings.reserve( 8 );
493 for ( Permission * perm =
m_head.get(); perm; perm = perm->m_next.get() )
495 strings.push_back( perm->toString() );
497 return Sequence< OUString >( strings.data(), strings.size() );
504 for (
Permission * perm = head.get(); perm; perm = perm->m_next.get() )
506 if (perm->implies( demanded ))
514static void demanded_diag(
515 Permission
const & perm )
517 OUStringBuffer buf( 48 );
518 buf.append(
"demanding " );
519 buf.append( perm.toString() );
520 buf.append(
" => ok." );
523 SAL_INFO(
"stoc",(
"%s", str.getStr() );
528 Permission const & perm, Any
const & demanded_perm )
530 throw security::AccessControlException(
531 "access denied: " + perm.
toString(),
532 Reference< XInterface >(), demanded_perm );
537 Type const & demanded_type = perm.getValueType();
544 FilePermission demanded(
545 *
static_cast< io::FilePermission
const *
>( perm.pData ) );
549 demanded_diag( demanded );
557 SocketPermission demanded(
558 *
static_cast< connection::SocketPermission
const *
>( perm.pData ) );
562 demanded_diag( demanded );
570 RuntimePermission demanded(
571 *
static_cast< security::RuntimePermission
const *
>( perm.pData ) );
575 demanded_diag( demanded );
587 demanded_diag( demanded );
595 throw RuntimeException(
"checking for unsupported permission type: " + demanded_type.getTypeName() );
AllPermission(::rtl::Reference< Permission > const &next=::rtl::Reference< Permission >())
void checkPermission(css::uno::Any const &perm) const
::rtl::Reference< Permission > m_head
virtual OUString toString() const =0
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
std::unique_ptr< sal_Int32[]> pData
std::basic_string_view< charT, traits > trim(std::basic_string_view< charT, traits > str)
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
static OUString const & getWorkingDir()
static OUString makeStrings(sal_Int32 mask, char const *const *strings)
static sal_Int32 makeMask(OUString const &items, char const *const *strings)
static bool implies(::rtl::Reference< Permission > const &head, Permission const &demanded)
static void throwAccessControlException(Permission const &perm, Any const &demanded_perm)
OUString toString(OptionInfo const *info)
static char const * s_actions[]