1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cppuhelper.hxx"
26 
27 #include <cppuhelper/access_control.hxx>
28 
29 #include <com/sun/star/security/XAccessController.hpp>
30 #include <com/sun/star/security/RuntimePermission.hpp>
31 #include <com/sun/star/io/FilePermission.hpp>
32 #include <com/sun/star/connection/SocketPermission.hpp>
33 
34 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
35 
36 
37 using namespace ::rtl;
38 using namespace ::osl;
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 
42 namespace
43 {
str_ac_singleton()44     inline OUString str_ac_singleton()
45     {
46         return OUSTR("/singletons/com.sun.star.security.theAccessController");
47     }
48 }
49 
50 namespace cppu
51 {
52 //__________________________________________________________________________________________________
AccessControl(Reference<XComponentContext> const & xContext)53 AccessControl::AccessControl( Reference< XComponentContext > const & xContext )
54     SAL_THROW( (RuntimeException) )
55 {
56     if (! (xContext->getValueByName( str_ac_singleton() ) >>= m_xController))
57     {
58         throw SecurityException(
59             OUSTR("no access controller!"), Reference< XInterface >() );
60     }
61 }
62 //__________________________________________________________________________________________________
AccessControl(Reference<security::XAccessController> const & xController)63 AccessControl::AccessControl(
64     Reference< security::XAccessController > const & xController )
65     SAL_THROW( (RuntimeException) )
66     : m_xController( xController )
67 {
68     if (! m_xController.is())
69     {
70         throw SecurityException(
71             OUSTR("no access controller!"), Reference< XInterface >() );
72     }
73 }
74 //__________________________________________________________________________________________________
AccessControl(AccessControl const & ac)75 AccessControl::AccessControl( AccessControl const & ac )
76     SAL_THROW( (RuntimeException) )
77     : m_xController( ac.m_xController )
78 {
79     if (! m_xController.is())
80     {
81         throw SecurityException(
82             OUSTR("no access controller!"), Reference< XInterface >() );
83     }
84 }
85 
86 #ifdef SAL_W32
87 #pragma pack(push, 8)
88 #endif
89     // binary comp. to all Permission structs
90     struct __permission
91     {
92         rtl_uString * m_str1;
93         rtl_uString * m_str2;
94     };
95 #ifdef SAL_W32
96 #pragma pack(pop)
97 #endif
98 
99 //--------------------------------------------------------------------------------------------------
__checkPermission(Reference<security::XAccessController> const & xController,Type const & type,rtl_uString * str1,rtl_uString * str2)100 inline void __checkPermission(
101     Reference< security::XAccessController > const & xController,
102     Type const & type, rtl_uString * str1, rtl_uString * str2 )
103     SAL_THROW( (RuntimeException) )
104 {
105     __permission perm;
106     perm.m_str1 = str1;
107     perm.m_str2 = str2;
108 
109     uno_Any a;
110     a.pType = type.getTypeLibType();
111     a.pData = &perm;
112 
113     xController->checkPermission( * static_cast< Any * >( &a ) );
114 }
115 //__________________________________________________________________________________________________
checkRuntimePermission(OUString const & name)116 void AccessControl::checkRuntimePermission(
117     OUString const & name )
118     SAL_THROW( (RuntimeException) )
119 {
120     __checkPermission(
121         m_xController,
122         ::getCppuType( (security::RuntimePermission *)0 ), name.pData, 0 );
123 }
124 //__________________________________________________________________________________________________
checkFilePermission(OUString const & url,OUString const & actions)125 void AccessControl::checkFilePermission(
126     OUString const & url,
127     OUString const & actions )
128     SAL_THROW( (RuntimeException) )
129 {
130     __checkPermission(
131         m_xController,
132         ::getCppuType( (io::FilePermission *)0 ), url.pData, actions.pData );
133 }
134 //__________________________________________________________________________________________________
checkSocketPermission(OUString const & host,OUString const & actions)135 void AccessControl::checkSocketPermission(
136     OUString const & host,
137     OUString const & actions )
138     SAL_THROW( (RuntimeException) )
139 {
140     __checkPermission(
141         m_xController,
142         ::getCppuType( (connection::SocketPermission *)0 ), host.pData, actions.pData );
143 }
144 
145 }
146