1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_stoc.hxx"
#include <vector>
#include <osl/process.h>
#include <osl/socket.hxx>
#include <osl/mutex.hxx>
#include <rtl/string.hxx>
#include <rtl/ustrbuf.hxx>
#include <com/sun/star/security/RuntimePermission.hpp>
#include <com/sun/star/security/AllPermission.hpp>
#include <com/sun/star/io/FilePermission.hpp>
#include <com/sun/star/connection/SocketPermission.hpp>
#include <com/sun/star/security/AccessControlException.hpp>
#include "permissions.h"
#define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
using namespace ::std;
using namespace ::osl;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using ::rtl::OUString;
using ::rtl::OUStringBuffer;
namespace stoc_sec
{
//--------------------------------------------------------------------------------------------------
static inline sal_Int32 makeMask(
OUString const & items, char const * const * strings ) SAL_THROW( () )
{
sal_Int32 mask = 0;
sal_Int32 n = 0;
do
{
OUString item( items.getToken( 0, ',', n ).trim() );
if (! item.getLength())
continue;
sal_Int32 nPos = 0;
while (strings[ nPos ])
{
if (item.equalsAscii( strings[ nPos ] ))
{
mask |= (0x80000000 >> nPos);
break;
}
++nPos;
}
#if OSL_DEBUG_LEVEL > 0
if (! strings[ nPos ])
{
OUStringBuffer buf( 48 );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("### ignoring unknown socket action: ") );
buf.append( item );
::rtl::OString str( ::rtl::OUStringToOString(
buf.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US ) );
OSL_TRACE( str.getStr() );
}
#endif
}
while (n >= 0); // all items
return mask;
}
//--------------------------------------------------------------------------------------------------
static inline OUString makeStrings(
sal_Int32 mask, char const * const * strings ) SAL_THROW( () )
{
OUStringBuffer buf( 48 );
while (mask)
{
if (0x80000000 & mask)
{
buf.appendAscii( *strings );
if (mask << 1) // more items following
buf.append( (sal_Unicode)',' );
}
mask = (mask << 1);
++strings;
}
return buf.makeStringAndClear();
}
//##################################################################################################
//==================================================================================================
class SocketPermission : public Permission
{
static char const * s_actions [];
sal_Int32 m_actions;
OUString m_host;
sal_Int32 m_lowerPort;
sal_Int32 m_upperPort;
mutable OUString m_ip;
mutable bool m_resolveErr;
mutable bool m_resolvedHost;
bool m_wildCardHost;
inline bool resolveHost() const SAL_THROW( () );
public:
SocketPermission(
connection::SocketPermission const & perm,
::rtl::Reference< Permission > const & next = ::rtl::Reference< Permission >() )
SAL_THROW( () );
virtual bool implies( Permission const & perm ) const SAL_THROW( () );
virtual OUString toString() const SAL_THROW( () );
};
//__________________________________________________________________________________________________
char const * SocketPermission::s_actions [] = { "accept", "connect", "listen", "resolve", 0 };
//__________________________________________________________________________________________________
SocketPermission::SocketPermission(
connection::SocketPermission const & perm,
::rtl::Reference< Permission > const & next )
SAL_THROW( () )
: Permission( SOCKET, next )
, m_actions( makeMask( perm.Actions, s_actions ) )
, m_host( perm.Host )
, m_lowerPort( 0 )
, m_upperPort( 65535 )
, m_resolveErr( false )
, m_resolvedHost( false )
, m_wildCardHost( perm.Host.getLength() && '*' == perm.Host.pData->buffer[ 0 ] )
{
if (0xe0000000 & m_actions) // if any (except resolve) is given => resolve implied
m_actions |= 0x10000000;
// separate host from portrange
sal_Int32 colon = m_host.indexOf( ':' );
if (colon >= 0) // port [range] given
{
sal_Int32 minus = m_host.indexOf( '-', colon +1 );
if (minus < 0)
{
m_lowerPort = m_upperPort = m_host.copy( colon +1 ).toInt32();
}
else if (minus == (colon +1)) // -N
{
m_upperPort = m_host.copy( minus +1 ).toInt32();
}
else if (minus == (m_host.getLength() -1)) // N-
{
m_lowerPort = m_host.copy( colon +1, m_host.getLength() -1 -colon -1 ).toInt32();
}
else // A-B
{
m_lowerPort = m_host.copy( colon +1, minus - colon -1 ).toInt32();
m_upperPort = m_host.copy( minus +1, m_host.getLength() -minus -1 ).toInt32();
}
m_host = m_host.copy( 0, colon );
}
}
//__________________________________________________________________________________________________
inline bool SocketPermission::resolveHost() const SAL_THROW( () )
{
if (m_resolveErr)
return false;
if (! m_resolvedHost)
{
// dns lookup
SocketAddr addr;
SocketAddr::resolveHostname( m_host, addr );
OUString ip;
m_resolveErr = (::osl_Socket_Ok != ::osl_getDottedInetAddrOfSocketAddr(
addr.getHandle(), &ip.pData ));
if (m_resolveErr)
return false;
MutexGuard guard( Mutex::getGlobalMutex() );
if (! m_resolvedHost)
{
m_ip = ip;
m_resolvedHost = true;
}
}
return m_resolvedHost;
}
//__________________________________________________________________________________________________
bool SocketPermission::implies( Permission const & perm ) const SAL_THROW( () )
{
// check type
if (SOCKET != perm.m_type)
return false;
SocketPermission const & demanded = static_cast< SocketPermission const & >( perm );
// check actions
if ((m_actions & demanded.m_actions) != demanded.m_actions)
return false;
// check ports
if (demanded.m_lowerPort < m_lowerPort)
return false;
if (demanded.m_upperPort > m_upperPort)
return false;
// quick check host (DNS names: RFC 1034/1035)
if (m_host.equalsIgnoreAsciiCase( demanded.m_host ))
return true;
// check for host wildcards
if (m_wildCardHost)
{
OUString const & demanded_host = demanded.m_host;
if (demanded_host.getLength() <= m_host.getLength())
return false;
sal_Int32 len = m_host.getLength() -1; // skip star
return (0 == ::rtl_ustr_compareIgnoreAsciiCase_WithLength(
demanded_host.getStr() + demanded_host.getLength() - len, len,
m_host.pData->buffer + 1, len ));
}
if (demanded.m_wildCardHost)
return false;
// compare IP addresses
if (! resolveHost())
return false;
if (! demanded.resolveHost())
return false;
return (sal_False != m_ip.equals( demanded.m_ip ));
}
//__________________________________________________________________________________________________
OUString SocketPermission::toString() const SAL_THROW( () )
{
OUStringBuffer buf( 48 );
// host
buf.appendAscii(
RTL_CONSTASCII_STRINGPARAM("com.sun.star.connection.SocketPermission (host=\"") );
buf.append( m_host );
if (m_resolvedHost)
{
buf.append( (sal_Unicode)'[' );
buf.append( m_ip );
buf.append( (sal_Unicode)']' );
}
// port
if (0 != m_lowerPort || 65535 != m_upperPort)
{
buf.append( (sal_Unicode)':' );
if (m_lowerPort > 0)
buf.append( m_lowerPort );
if (m_upperPort > m_lowerPort)
{
buf.append( (sal_Unicode)'-' );
if (m_upperPort < 65535)
buf.append( m_upperPort );
}
}
// actions
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\", actions=\"") );
buf.append( makeStrings( m_actions, s_actions ) );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\")") );
return buf.makeStringAndClear();
}
//##################################################################################################
//==================================================================================================
class FilePermission : public Permission
{
static char const * s_actions [];
sal_Int32 m_actions;
OUString m_url;
bool m_allFiles;
public:
FilePermission(
io::FilePermission const & perm,
::rtl::Reference< Permission > const & next = ::rtl::Reference< Permission >() )
SAL_THROW( () );
virtual bool implies( Permission const & perm ) const SAL_THROW( () );
virtual OUString toString() const SAL_THROW( () );
};
//__________________________________________________________________________________________________
char const * FilePermission::s_actions [] = { "read", "write", "execute", "delete", 0 };
//--------------------------------------------------------------------------------------------------
static OUString const & getWorkingDir() SAL_THROW( () )
{
static OUString * s_workingDir = 0;
if (! s_workingDir)
{
OUString workingDir;
::osl_getProcessWorkingDir( &workingDir.pData );
MutexGuard guard( Mutex::getGlobalMutex() );
if (! s_workingDir)
{
static OUString s_dir( workingDir );
s_workingDir = &s_dir;
}
}
return *s_workingDir;
}
//__________________________________________________________________________________________________
FilePermission::FilePermission(
io::FilePermission const & perm,
::rtl::Reference< Permission > const & next )
SAL_THROW( () )
: Permission( FILE, next )
, m_actions( makeMask( perm.Actions, s_actions ) )
, m_url( perm.URL )
, m_allFiles( sal_False != perm.URL.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("<<ALL FILES>>")) )
{
if (! m_allFiles)
{
if (m_url.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("*") ))
{
OUStringBuffer buf( 64 );
buf.append( getWorkingDir() );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("/*") );
m_url = buf.makeStringAndClear();
}
else if (m_url.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("-") ))
{
OUStringBuffer buf( 64 );
buf.append( getWorkingDir() );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("/-") );
m_url = buf.makeStringAndClear();
}
else if (0 != m_url.compareToAscii( RTL_CONSTASCII_STRINGPARAM("file:///") ))
{
// relative path
OUString out;
oslFileError rc = ::osl_getAbsoluteFileURL(
getWorkingDir().pData, perm.URL.pData, &out.pData );
m_url = (osl_File_E_None == rc ? out : perm.URL); // fallback
}
#ifdef SAL_W32
// correct win drive letters
if (9 < m_url.getLength() && '|' == m_url[ 9 ]) // file:///X|
{
static OUString s_colon = OUSTR(":");
// common case in API is a ':' (sal), so convert '|' to ':'
m_url = m_url.replaceAt( 9, 1, s_colon );
}
#endif
}
}
//__________________________________________________________________________________________________
bool FilePermission::implies( Permission const & perm ) const SAL_THROW( () )
{
// check type
if (FILE != perm.m_type)
return false;
FilePermission const & demanded = static_cast< FilePermission const & >( perm );
// check actions
if ((m_actions & demanded.m_actions) != demanded.m_actions)
return false;
// check url
if (m_allFiles)
return true;
if (demanded.m_allFiles)
return false;
#ifdef SAL_W32
if (m_url.equalsIgnoreAsciiCase( demanded.m_url ))
return true;
#else
if (m_url.equals( demanded.m_url ))
return true;
#endif
if (m_url.getLength() > demanded.m_url.getLength())
return false;
// check /- wildcard: all files and recursive in that path
if (1 < m_url.getLength() &&
0 == ::rtl_ustr_ascii_compare_WithLength( m_url.getStr() + m_url.getLength() - 2, 2, "/-" ))
{
// demanded url must start with granted path (including path trailing path sep)
sal_Int32 len = m_url.getLength() -1;
#ifdef SAL_W32
return (0 == ::rtl_ustr_compareIgnoreAsciiCase_WithLength(
demanded.m_url.pData->buffer, len, m_url.pData->buffer, len ));
#else
return (0 == ::rtl_ustr_reverseCompare_WithLength(
demanded.m_url.pData->buffer, len, m_url.pData->buffer, len ));
#endif
}
// check /* wildcard: all files in that path (not recursive!)
if (1 < m_url.getLength() &&
0 == ::rtl_ustr_ascii_compare_WithLength( m_url.getStr() + m_url.getLength() - 2, 2, "/*" ))
{
// demanded url must start with granted path (including path trailing path sep)
sal_Int32 len = m_url.getLength() -1;
#ifdef SAL_W32
return ((0 == ::rtl_ustr_compareIgnoreAsciiCase_WithLength(
demanded.m_url.pData->buffer, len, m_url.pData->buffer, len )) &&
(0 > demanded.m_url.indexOf( '/', len ))); // in addition, no deeper pathes
#else
return ((0 == ::rtl_ustr_reverseCompare_WithLength(
demanded.m_url.pData->buffer, len, m_url.pData->buffer, len )) &&
(0 > demanded.m_url.indexOf( '/', len ))); // in addition, no deeper pathes
#endif
}
return false;
}
//__________________________________________________________________________________________________
OUString FilePermission::toString() const SAL_THROW( () )
{
OUStringBuffer buf( 48 );
// url
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("com.sun.star.io.FilePermission (url=\"") );
buf.append( m_url );
// actions
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\", actions=\"") );
buf.append( makeStrings( m_actions, s_actions ) );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\")") );
return buf.makeStringAndClear();
}
//##################################################################################################
//==================================================================================================
class RuntimePermission : public Permission
{
OUString m_name;
public:
inline RuntimePermission(
security::RuntimePermission const & perm,
::rtl::Reference< Permission > const & next = ::rtl::Reference< Permission >() )
SAL_THROW( () )
: Permission( RUNTIME, next )
, m_name( perm.Name )
{}
virtual bool implies( Permission const & perm ) const SAL_THROW( () );
virtual OUString toString() const SAL_THROW( () );
};
//__________________________________________________________________________________________________
bool RuntimePermission::implies( Permission const & perm ) const SAL_THROW( () )
{
// check type
if (RUNTIME != perm.m_type)
return false;
RuntimePermission const & demanded = static_cast< RuntimePermission const & >( perm );
// check name
return (sal_False != m_name.equals( demanded.m_name ));
}
//__________________________________________________________________________________________________
OUString RuntimePermission::toString() const SAL_THROW( () )
{
OUStringBuffer buf( 48 );
buf.appendAscii(
RTL_CONSTASCII_STRINGPARAM("com.sun.star.security.RuntimePermission (name=\"") );
buf.append( m_name );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\")") );
return buf.makeStringAndClear();
}
//##################################################################################################
//__________________________________________________________________________________________________
bool AllPermission::implies( Permission const & ) const SAL_THROW( () )
{
return true;
}
//__________________________________________________________________________________________________
OUString AllPermission::toString() const SAL_THROW( () )
{
return OUSTR("com.sun.star.security.AllPermission");
}
//##################################################################################################
//__________________________________________________________________________________________________
PermissionCollection::PermissionCollection(
Sequence< Any > const & permissions, PermissionCollection const & addition )
SAL_THROW( (RuntimeException) )
: m_head( addition.m_head )
{
Any const * perms = permissions.getConstArray();
for ( sal_Int32 nPos = permissions.getLength(); nPos--; )
{
Any const & perm = perms[ nPos ];
Type const & perm_type = perm.getValueType();
// supported permission types
if (perm_type.equals( ::getCppuType( (io::FilePermission const *)0 ) ))
{
m_head = new FilePermission(
*reinterpret_cast< io::FilePermission const * >( perm.pData ), m_head );
}
else if (perm_type.equals( ::getCppuType( (connection::SocketPermission const *)0 ) ))
{
m_head = new SocketPermission(
*reinterpret_cast< connection::SocketPermission const * >( perm.pData ), m_head );
}
else if (perm_type.equals( ::getCppuType( (security::RuntimePermission const *)0 ) ))
{
m_head = new RuntimePermission(
*reinterpret_cast< security::RuntimePermission const * >( perm.pData ), m_head );
}
else if (perm_type.equals( ::getCppuType( (security::AllPermission const *)0 ) ))
{
m_head = new AllPermission( m_head );
}
else
{
OUStringBuffer buf( 48 );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
"checking for unsupported permission type: ") );
buf.append( perm_type.getTypeName() );
throw RuntimeException(
buf.makeStringAndClear(), Reference< XInterface >() );
}
}
}
#ifdef __DIAGNOSE
//__________________________________________________________________________________________________
Sequence< OUString > PermissionCollection::toStrings() const SAL_THROW( () )
{
vector< OUString > strings;
strings.reserve( 8 );
for ( Permission * perm = m_head.get(); perm; perm = perm->m_next.get() )
{
strings.push_back( perm->toString() );
}
return Sequence< OUString >(
strings.empty() ? 0 : &strings[ 0 ], strings.size() );
}
#endif
//__________________________________________________________________________________________________
inline static bool __implies(
::rtl::Reference< Permission > const & head, Permission const & demanded ) SAL_THROW( () )
{
for ( Permission * perm = head.get(); perm; perm = perm->m_next.get() )
{
if (perm->implies( demanded ))
return true;
}
return false;
}
#ifdef __DIAGNOSE
//--------------------------------------------------------------------------------------------------
static void demanded_diag(
Permission const & perm )
SAL_THROW( () )
{
OUStringBuffer buf( 48 );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("demanding ") );
buf.append( perm.toString() );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" => ok.") );
::rtl::OString str(
::rtl::OUStringToOString( buf.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US ) );
OSL_TRACE( str.getStr() );
}
#endif
//--------------------------------------------------------------------------------------------------
static void throwAccessControlException(
Permission const & perm, Any const & demanded_perm )
SAL_THROW( (security::AccessControlException) )
{
OUStringBuffer buf( 48 );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("access denied: ") );
buf.append( perm.toString() );
throw security::AccessControlException(
buf.makeStringAndClear(), Reference< XInterface >(), demanded_perm );
}
//==================================================================================================
void PermissionCollection::checkPermission( Any const & perm ) const
SAL_THROW( (RuntimeException) )
{
Type const & demanded_type = perm.getValueType();
// supported permission types
// stack object of SimpleReferenceObject are ok, as long as they are not
// assigned to a ::rtl::Reference<> (=> delete this)
if (demanded_type.equals( ::getCppuType( (io::FilePermission const *)0 ) ))
{
FilePermission demanded(
*reinterpret_cast< io::FilePermission const * >( perm.pData ) );
if (__implies( m_head, demanded ))
{
#ifdef __DIAGNOSE
demanded_diag( demanded );
#endif
return;
}
throwAccessControlException( demanded, perm );
}
else if (demanded_type.equals( ::getCppuType( (connection::SocketPermission const *)0 ) ))
{
SocketPermission demanded(
*reinterpret_cast< connection::SocketPermission const * >( perm.pData ) );
if (__implies( m_head, demanded ))
{
#ifdef __DIAGNOSE
demanded_diag( demanded );
#endif
return;
}
throwAccessControlException( demanded, perm );
}
else if (demanded_type.equals( ::getCppuType( (security::RuntimePermission const *)0 ) ))
{
RuntimePermission demanded(
*reinterpret_cast< security::RuntimePermission const * >( perm.pData ) );
if (__implies( m_head, demanded ))
{
#ifdef __DIAGNOSE
demanded_diag( demanded );
#endif
return;
}
throwAccessControlException( demanded, perm );
}
else if (demanded_type.equals( ::getCppuType( (security::AllPermission const *)0 ) ))
{
AllPermission demanded;
if (__implies( m_head, demanded ))
{
#ifdef __DIAGNOSE
demanded_diag( demanded );
#endif
return;
}
throwAccessControlException( demanded, perm );
}
else
{
OUStringBuffer buf( 48 );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("checking for unsupported permission type: ") );
buf.append( demanded_type.getTypeName() );
throw RuntimeException(
buf.makeStringAndClear(), Reference< XInterface >() );
}
}
}
|