1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppuhelper.hxx"
30 
31 #include <cppuhelper/access_control.hxx>
32 
33 #include <com/sun/star/security/XAccessController.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 
38 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
39 
40 
41 using namespace ::rtl;
42 using namespace ::osl;
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
45 
46 namespace
47 {
48     inline OUString str_ac_singleton()
49     {
50         return OUSTR("/singletons/com.sun.star.security.theAccessController");
51     }
52 }
53 
54 namespace cppu
55 {
56 //__________________________________________________________________________________________________
57 AccessControl::AccessControl( Reference< XComponentContext > const & xContext )
58     SAL_THROW( (RuntimeException) )
59 {
60     if (! (xContext->getValueByName( str_ac_singleton() ) >>= m_xController))
61     {
62         throw SecurityException(
63             OUSTR("no access controller!"), Reference< XInterface >() );
64     }
65 }
66 //__________________________________________________________________________________________________
67 AccessControl::AccessControl(
68     Reference< security::XAccessController > const & xController )
69     SAL_THROW( (RuntimeException) )
70     : m_xController( xController )
71 {
72     if (! m_xController.is())
73     {
74         throw SecurityException(
75             OUSTR("no access controller!"), Reference< XInterface >() );
76     }
77 }
78 //__________________________________________________________________________________________________
79 AccessControl::AccessControl( AccessControl const & ac )
80     SAL_THROW( (RuntimeException) )
81     : m_xController( ac.m_xController )
82 {
83     if (! m_xController.is())
84     {
85         throw SecurityException(
86             OUSTR("no access controller!"), Reference< XInterface >() );
87     }
88 }
89 
90 #ifdef SAL_W32
91 #pragma pack(push, 8)
92 #endif
93     // binary comp. to all Permission structs
94     struct __permission
95     {
96         rtl_uString * m_str1;
97         rtl_uString * m_str2;
98     };
99 #ifdef SAL_W32
100 #pragma pack(pop)
101 #endif
102 
103 //--------------------------------------------------------------------------------------------------
104 inline void __checkPermission(
105     Reference< security::XAccessController > const & xController,
106     Type const & type, rtl_uString * str1, rtl_uString * str2 )
107     SAL_THROW( (RuntimeException) )
108 {
109     __permission perm;
110     perm.m_str1 = str1;
111     perm.m_str2 = str2;
112 
113     uno_Any a;
114     a.pType = type.getTypeLibType();
115     a.pData = &perm;
116 
117     xController->checkPermission( * static_cast< Any * >( &a ) );
118 }
119 //__________________________________________________________________________________________________
120 void AccessControl::checkRuntimePermission(
121     OUString const & name )
122     SAL_THROW( (RuntimeException) )
123 {
124     __checkPermission(
125         m_xController,
126         ::getCppuType( (security::RuntimePermission *)0 ), name.pData, 0 );
127 }
128 //__________________________________________________________________________________________________
129 void AccessControl::checkFilePermission(
130     OUString const & url,
131     OUString const & actions )
132     SAL_THROW( (RuntimeException) )
133 {
134     __checkPermission(
135         m_xController,
136         ::getCppuType( (io::FilePermission *)0 ), url.pData, actions.pData );
137 }
138 //__________________________________________________________________________________________________
139 void AccessControl::checkSocketPermission(
140     OUString const & host,
141     OUString const & actions )
142     SAL_THROW( (RuntimeException) )
143 {
144     __checkPermission(
145         m_xController,
146         ::getCppuType( (connection::SocketPermission *)0 ), host.pData, actions.pData );
147 }
148 
149 }
150