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_connectivity.hxx"
30 #include "hsqldb/HUser.hxx"
31 #include <com/sun/star/sdbc/XRow.hpp>
32 #include <com/sun/star/sdbc/XResultSet.hpp>
33 #include "connectivity/dbtools.hxx"
34 #include "connectivity/dbexception.hxx"
35 #include <com/sun/star/sdbcx/Privilege.hpp>
36 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
37 #include "TConnection.hxx"
38 #include "resource/hsqldb_res.hrc"
39 
40 using namespace connectivity;
41 using namespace connectivity::hsqldb;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::sdbcx;
45 using namespace ::com::sun::star::sdbc;
46 using namespace ::com::sun::star::container;
47 using namespace ::com::sun::star::lang;
48 // -------------------------------------------------------------------------
49 OHSQLUser::OHSQLUser(	const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : connectivity::sdbcx::OUser(sal_True)
50 				,m_xConnection(_xConnection)
51 {
52 	construct();
53 }
54 // -------------------------------------------------------------------------
55 OHSQLUser::OHSQLUser(   const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
56 				const ::rtl::OUString& _Name
57 			) : connectivity::sdbcx::OUser(_Name,sal_True)
58 				,m_xConnection(_xConnection)
59 {
60 	construct();
61 }
62 // -------------------------------------------------------------------------
63 void OHSQLUser::refreshGroups()
64 {
65 }
66 // -------------------------------------------------------------------------
67 OUserExtend::OUserExtend(	const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : OHSQLUser(_xConnection)
68 {
69 	construct();
70 }
71 // -------------------------------------------------------------------------
72 typedef connectivity::sdbcx::OUser	OUser_TYPEDEF;
73 void OUserExtend::construct()
74 {
75 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD),	PROPERTY_ID_PASSWORD,0,&m_Password,::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
76 }
77 // -----------------------------------------------------------------------------
78 cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
79 {
80 	Sequence< Property > aProps;
81 	describeProperties(aProps);
82 	return new cppu::OPropertyArrayHelper(aProps);
83 }
84 // -------------------------------------------------------------------------
85 cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
86 {
87 	return *OUserExtend_PROP::getArrayHelper();
88 }
89 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
90 // -----------------------------------------------------------------------------
91 sal_Int32 SAL_CALL OHSQLUser::getPrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
92 {
93 	::osl::MutexGuard aGuard(m_aMutex);
94 	checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
95 
96 	sal_Int32 nRights,nRightsWithGrant;
97 	findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
98 	return nRights;
99 }
100 // -----------------------------------------------------------------------------
101 void OHSQLUser::findPrivilegesAndGrantPrivileges(const ::rtl::OUString& objName, sal_Int32 objType,sal_Int32& nRights,sal_Int32& nRightsWithGrant) throw(SQLException, RuntimeException)
102 {
103 	nRightsWithGrant = nRights = 0;
104 	// first we need to create the sql stmt to select the privs
105 	Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
106 	::rtl::OUString sCatalog,sSchema,sTable;
107 	::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
108 	Reference<XResultSet> xRes;
109 	switch(objType)
110 	{
111 		case PrivilegeObject::TABLE:
112 		case PrivilegeObject::VIEW:
113 			{
114 				Any aCatalog;
115 				if ( sCatalog.getLength() )
116 					aCatalog <<= sCatalog;
117 				xRes = xMeta->getTablePrivileges(aCatalog,sSchema,sTable);
118 			}
119 			break;
120 
121 		case PrivilegeObject::COLUMN:
122 			{
123 				Any aCatalog;
124 				if ( sCatalog.getLength() )
125 					aCatalog <<= sCatalog;
126 				xRes = xMeta->getColumnPrivileges(aCatalog,sSchema,sTable,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%")));
127 			}
128 			break;
129 	}
130 
131 	if ( xRes.is() )
132 	{
133 		static const ::rtl::OUString sSELECT	= ::rtl::OUString::createFromAscii("SELECT");
134 		static const ::rtl::OUString sINSERT	= ::rtl::OUString::createFromAscii("INSERT");
135 		static const ::rtl::OUString sUPDATE	= ::rtl::OUString::createFromAscii("UPDATE");
136 		static const ::rtl::OUString sDELETE	= ::rtl::OUString::createFromAscii("DELETE");
137 		static const ::rtl::OUString sREAD		= ::rtl::OUString::createFromAscii("READ");
138 		static const ::rtl::OUString sCREATE	= ::rtl::OUString::createFromAscii("CREATE");
139 		static const ::rtl::OUString sALTER		= ::rtl::OUString::createFromAscii("ALTER");
140 		static const ::rtl::OUString sREFERENCE = ::rtl::OUString::createFromAscii("REFERENCE");
141 		static const ::rtl::OUString sDROP		= ::rtl::OUString::createFromAscii("DROP");
142 		static const ::rtl::OUString sYes		= ::rtl::OUString::createFromAscii("YES");
143 
144 		nRightsWithGrant = nRights = 0;
145 
146 		Reference<XRow> xCurrentRow(xRes,UNO_QUERY);
147 		while( xCurrentRow.is() && xRes->next() )
148 		{
149 			::rtl::OUString sGrantee	= xCurrentRow->getString(5);
150 			::rtl::OUString sPrivilege	= xCurrentRow->getString(6);
151 			::rtl::OUString sGrantable	= xCurrentRow->getString(7);
152 
153 			if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
154 				continue;
155 
156 			if (sPrivilege.equalsIgnoreAsciiCase(sSELECT))
157 			{
158 				nRights |= Privilege::SELECT;
159 				if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
160 					nRightsWithGrant |= Privilege::SELECT;
161 			}
162 			else if (sPrivilege.equalsIgnoreAsciiCase(sINSERT))
163 			{
164 				nRights |= Privilege::INSERT;
165 				if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
166 					nRightsWithGrant |= Privilege::INSERT;
167 			}
168 			else if (sPrivilege.equalsIgnoreAsciiCase(sUPDATE))
169 			{
170 				nRights |= Privilege::UPDATE;
171 				if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
172 					nRightsWithGrant |= Privilege::UPDATE;
173 			}
174 			else if (sPrivilege.equalsIgnoreAsciiCase(sDELETE))
175 			{
176 				nRights |= Privilege::DELETE;
177 				if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
178 					nRightsWithGrant |= Privilege::DELETE;
179 			}
180 			else if (sPrivilege.equalsIgnoreAsciiCase(sREAD))
181 			{
182 				nRights |= Privilege::READ;
183 				if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
184 					nRightsWithGrant |= Privilege::READ;
185 			}
186 			else if (sPrivilege.equalsIgnoreAsciiCase(sCREATE))
187 			{
188 				nRights |= Privilege::CREATE;
189 				if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
190 					nRightsWithGrant |= Privilege::CREATE;
191 			}
192 			else if (sPrivilege.equalsIgnoreAsciiCase(sALTER))
193 			{
194 				nRights |= Privilege::ALTER;
195 				if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
196 					nRightsWithGrant |= Privilege::ALTER;
197 			}
198 			else if (sPrivilege.equalsIgnoreAsciiCase(sREFERENCE))
199 			{
200 				nRights |= Privilege::REFERENCE;
201 				if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
202 					nRightsWithGrant |= Privilege::REFERENCE;
203 			}
204 			else if (sPrivilege.equalsIgnoreAsciiCase(sDROP))
205 			{
206 				nRights |= Privilege::DROP;
207 				if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
208 					nRightsWithGrant |= Privilege::DROP;
209 			}
210 		}
211 		::comphelper::disposeComponent(xRes);
212 	}
213 }
214 // -------------------------------------------------------------------------
215 sal_Int32 SAL_CALL OHSQLUser::getGrantablePrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
216 {
217 	::osl::MutexGuard aGuard(m_aMutex);
218 	checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
219 
220 	sal_Int32 nRights,nRightsWithGrant;
221 	findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
222 	return nRightsWithGrant;
223 }
224 // -------------------------------------------------------------------------
225 void SAL_CALL OHSQLUser::grantPrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
226 {
227     if ( objType != PrivilegeObject::TABLE )
228     {
229         ::connectivity::SharedResources aResources;
230         const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
231         ::dbtools::throwGenericSQLException(sError,*this);
232     } // if ( objType != PrivilegeObject::TABLE )
233 
234 
235 	::osl::MutexGuard aGuard(m_aMutex);
236 
237 	::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
238 	if(sPrivs.getLength())
239 	{
240 		::rtl::OUString sGrant;
241 		sGrant += ::rtl::OUString::createFromAscii("GRANT ");
242 		sGrant += sPrivs;
243 		sGrant += ::rtl::OUString::createFromAscii(" ON ");
244 		Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
245 		sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
246 		sGrant += ::rtl::OUString::createFromAscii(" TO ");
247 		sGrant += m_Name;
248 
249 		Reference<XStatement> xStmt = m_xConnection->createStatement();
250 		if(xStmt.is())
251 			xStmt->execute(sGrant);
252 		::comphelper::disposeComponent(xStmt);
253 	}
254 }
255 // -------------------------------------------------------------------------
256 void SAL_CALL OHSQLUser::revokePrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
257 {
258     if ( objType != PrivilegeObject::TABLE )
259     {
260         ::connectivity::SharedResources aResources;
261         const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
262         ::dbtools::throwGenericSQLException(sError,*this);
263     } // if ( objType != PrivilegeObject::TABLE )
264 
265 	::osl::MutexGuard aGuard(m_aMutex);
266 	checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
267     ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
268 	if(sPrivs.getLength())
269 	{
270 		::rtl::OUString sGrant;
271 		sGrant += ::rtl::OUString::createFromAscii("REVOKE ");
272 		sGrant += sPrivs;
273 		sGrant += ::rtl::OUString::createFromAscii(" ON ");
274 		Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
275 		sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
276 		sGrant += ::rtl::OUString::createFromAscii(" FROM ");
277 		sGrant += m_Name;
278 
279 		Reference<XStatement> xStmt = m_xConnection->createStatement();
280 		if(xStmt.is())
281 			xStmt->execute(sGrant);
282 		::comphelper::disposeComponent(xStmt);
283 	}
284 }
285 // -----------------------------------------------------------------------------
286 // XUser
287 void SAL_CALL OHSQLUser::changePassword( const ::rtl::OUString& /*oldPassword*/, const ::rtl::OUString& newPassword ) throw(SQLException, RuntimeException)
288 {
289 	::osl::MutexGuard aGuard(m_aMutex);
290 	checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
291 	::rtl::OUString sAlterPwd;
292 	sAlterPwd = ::rtl::OUString::createFromAscii("SET PASSWORD FOR ");
293 	sAlterPwd += m_Name;
294 	sAlterPwd += ::rtl::OUString::createFromAscii("@\"%\" = PASSWORD('") ;
295 	sAlterPwd += newPassword;
296 	sAlterPwd += ::rtl::OUString::createFromAscii("')") ;
297 
298 
299 	Reference<XStatement> xStmt = m_xConnection->createStatement();
300 	if ( xStmt.is() )
301 	{
302 		xStmt->execute(sAlterPwd);
303 		::comphelper::disposeComponent(xStmt);
304 	}
305 }
306 // -----------------------------------------------------------------------------
307 ::rtl::OUString OHSQLUser::getPrivilegeString(sal_Int32 nRights) const
308 {
309 	::rtl::OUString sPrivs;
310 	if((nRights & Privilege::INSERT) == Privilege::INSERT)
311 		sPrivs += ::rtl::OUString::createFromAscii("INSERT");
312 
313 	if((nRights & Privilege::DELETE) == Privilege::DELETE)
314 	{
315 		if(sPrivs.getLength())
316 			sPrivs += ::rtl::OUString::createFromAscii(",");
317 		sPrivs += ::rtl::OUString::createFromAscii("DELETE");
318 	}
319 
320 	if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
321 	{
322 		if(sPrivs.getLength())
323 			sPrivs += ::rtl::OUString::createFromAscii(",");
324 		sPrivs += ::rtl::OUString::createFromAscii("UPDATE");
325 	}
326 
327 	if((nRights & Privilege::ALTER) == Privilege::ALTER)
328 	{
329 		if(sPrivs.getLength())
330 			sPrivs += ::rtl::OUString::createFromAscii(",");
331 		sPrivs += ::rtl::OUString::createFromAscii("ALTER");
332 	}
333 
334 	if((nRights & Privilege::SELECT) == Privilege::SELECT)
335 	{
336 		if(sPrivs.getLength())
337 			sPrivs += ::rtl::OUString::createFromAscii(",");
338 		sPrivs += ::rtl::OUString::createFromAscii("SELECT");
339 	}
340 
341 	if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
342 	{
343 		if(sPrivs.getLength())
344 			sPrivs += ::rtl::OUString::createFromAscii(",");
345 		sPrivs += ::rtl::OUString::createFromAscii("REFERENCES");
346 	}
347 
348 	return sPrivs;
349 }
350 // -----------------------------------------------------------------------------
351 
352