sl@0
|
1 |
// Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include "SqlSrvAuthorizer.h" //MSqlPolicyInspector
|
sl@0
|
17 |
#include "SqlSrvMain.h" //CSqlServer
|
sl@0
|
18 |
#include "SqlSrvSecurityMap.h" //RSqlSecurityMap
|
sl@0
|
19 |
#include "SqlSrvDatabase.h" //CSqlSrvDatabase
|
sl@0
|
20 |
#include "SqlSecurityImpl.h" //CSqlSecurityPolicy
|
sl@0
|
21 |
#include "SqlSrvDbSysSettings.h"//TSqlDbSysSettings
|
sl@0
|
22 |
#include "SqlSrvUtil.h" //Global server functions
|
sl@0
|
23 |
#include "SqlSrvStatementUtil.h"//Global sql statement related functions
|
sl@0
|
24 |
#include "SqlSrvStrings.h" //KTempDb
|
sl@0
|
25 |
#include "sqlite3.h"
|
sl@0
|
26 |
#include "SqliteSymbian.h" //sqlite3SymbianLastOsError()
|
sl@0
|
27 |
|
sl@0
|
28 |
//This macro is used to suppress "function argument not used" compiler warning.
|
sl@0
|
29 |
#define UNUSED_ARG(arg) arg = (arg)
|
sl@0
|
30 |
|
sl@0
|
31 |
//Array of pragma commands
|
sl@0
|
32 |
const TPtrC8 KPragmaCommands[] =
|
sl@0
|
33 |
{
|
sl@0
|
34 |
KAutoVacuum(), KCacheSize(), KCaseSensitiveLike(), KCountChanges(), KDefaultCacheSize(),
|
sl@0
|
35 |
KEmptyResultCallbacks(), KEncoding(), KFullColumnNames(), KFullfsync(), KIncrementalVacuum(),
|
sl@0
|
36 |
KJournalMode(), KJournalSizeLimit(), KLegacyFileFormat(), KLockingMode(), KPageSize(),
|
sl@0
|
37 |
KMaxPageCount(), KReadUncommitted(), KShortColumnNames(), KSynchronousFlag(), KTempStore(),
|
sl@0
|
38 |
KTempStoreDirectory(), KDatabaseList(), KForeignKeyList(), KFreelistCount(), KIndexInfo(),
|
sl@0
|
39 |
KIndexIist(), KPageCount(),KTableInfo(), KSchemaVersion(), KUserVersion(),
|
sl@0
|
40 |
KIntegrityCheck(),KParserTrace(), KVdbeTrace(), KdbeListing()
|
sl@0
|
41 |
};
|
sl@0
|
42 |
|
sl@0
|
43 |
const TInt KMaxPragmaCommands = sizeof(KPragmaCommands) / sizeof(KPragmaCommands[0]);
|
sl@0
|
44 |
|
sl@0
|
45 |
|
sl@0
|
46 |
//Define the different ways of calling a pragam depending on the following
|
sl@0
|
47 |
// 1) If its a secure or non secure database
|
sl@0
|
48 |
// 2) If the pragma is called with a parameter (write) or without a parameter (read)
|
sl@0
|
49 |
struct TPragmaAccess
|
sl@0
|
50 |
{
|
sl@0
|
51 |
TInt iNonSecureRead;
|
sl@0
|
52 |
TInt iNonSecureWrite;
|
sl@0
|
53 |
TInt iSecureRead;
|
sl@0
|
54 |
TInt iSecureWrite;
|
sl@0
|
55 |
};
|
sl@0
|
56 |
|
sl@0
|
57 |
//Table specifying the permissions for each pragma command for secure (shared) and non-secure (public and private)
|
sl@0
|
58 |
//databases. For each database permissions for the following situations are specified
|
sl@0
|
59 |
//1) With Parameter - e.g "Pragma auto_vacuum = 0"
|
sl@0
|
60 |
//2) Without Parameter - e.g "Pragma auto_vacuum"
|
sl@0
|
61 |
|
sl@0
|
62 |
//Permissions "without parameters" usually apply to a pragma query (or read)
|
sl@0
|
63 |
//Permissions "with parameters" usually apply to pragama set (or write)
|
sl@0
|
64 |
//However please note that this is not always the case. e.g "index_info" requires a parameter but is used to query
|
sl@0
|
65 |
//(or read) the database and not a pragma set.
|
sl@0
|
66 |
const TPragmaAccess KPermissionsTable[KMaxPragmaCommands] =
|
sl@0
|
67 |
{
|
sl@0
|
68 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
sl@0
|
69 |
// NON_SECURE | SECURE |
|
sl@0
|
70 |
// W/Out Parameter |With Parameter |W/Out Parameter|With Parameter |Pragma Command
|
sl@0
|
71 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
sl@0
|
72 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //0. auto_vacuum
|
sl@0
|
73 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //1.cache_size
|
sl@0
|
74 |
{SQLITE_OK, SQLITE_OK, SQLITE_DENY, SQLITE_DENY}, //2.case_sensitive_like
|
sl@0
|
75 |
{SQLITE_OK, SQLITE_OK, SQLITE_DENY, SQLITE_DENY}, //3.count_changes
|
sl@0
|
76 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //4.cache_size
|
sl@0
|
77 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //5.empty_result_callbacks
|
sl@0
|
78 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //6.encoding
|
sl@0
|
79 |
{SQLITE_OK, SQLITE_OK, SQLITE_DENY, SQLITE_DENY}, //7.full_column_names
|
sl@0
|
80 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //8.fullfsync
|
sl@0
|
81 |
{SQLITE_IGNORE, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //9.incremental_vacuum
|
sl@0
|
82 |
{SQLITE_IGNORE, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //10.journal_mode
|
sl@0
|
83 |
{SQLITE_IGNORE, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //11.journal_size_limit
|
sl@0
|
84 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //12.legacy_file_format
|
sl@0
|
85 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //13.locking_mode
|
sl@0
|
86 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //14.page_size
|
sl@0
|
87 |
{SQLITE_IGNORE, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //15.max_page_count
|
sl@0
|
88 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //16.read_uncommitted
|
sl@0
|
89 |
{SQLITE_OK, SQLITE_OK, SQLITE_DENY, SQLITE_DENY}, //17.short_column_names
|
sl@0
|
90 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //18.synchronous
|
sl@0
|
91 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //19.temp_store
|
sl@0
|
92 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //20.temp_store_directory
|
sl@0
|
93 |
{SQLITE_OK, SQLITE_OK, SQLITE_DENY, SQLITE_DENY}, //21.database_list
|
sl@0
|
94 |
{SQLITE_OK, SQLITE_OK, SQLITE_DENY, SQLITE_DENY}, //22.foreign_key_list
|
sl@0
|
95 |
{SQLITE_IGNORE, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //23.freelist_count
|
sl@0
|
96 |
{SQLITE_OK, SQLITE_OK, SQLITE_DENY, SQLITE_DENY}, //24.index_info
|
sl@0
|
97 |
{SQLITE_OK, SQLITE_OK, SQLITE_DENY, SQLITE_DENY}, //25.index_list
|
sl@0
|
98 |
{SQLITE_IGNORE, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //26.page_count
|
sl@0
|
99 |
{SQLITE_OK, SQLITE_OK, SQLITE_DENY, SQLITE_DENY}, //27.table_info
|
sl@0
|
100 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //28.schema_version
|
sl@0
|
101 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //29.user_version
|
sl@0
|
102 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //30.integrity_check
|
sl@0
|
103 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //31.parser_trace
|
sl@0
|
104 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //32.vdbe_trace
|
sl@0
|
105 |
{SQLITE_OK, SQLITE_IGNORE, SQLITE_DENY, SQLITE_DENY}, //33.vdbe_listing
|
sl@0
|
106 |
};
|
sl@0
|
107 |
|
sl@0
|
108 |
|
sl@0
|
109 |
//This const array describes the relation between the database operation type and
|
sl@0
|
110 |
//the number of the authorizer argument where the table name is.
|
sl@0
|
111 |
//For example:
|
sl@0
|
112 |
//- SQLITE_CREATE_TEMP_TABLE operation. The table name is in aDbObjName1 argument, so the array element value is 1.
|
sl@0
|
113 |
//- SQLITE_PRAGMA operation. No table name for this operation, so the array element value is 0.
|
sl@0
|
114 |
//- SQLITE_CREATE_TEMP_TRIGGER operation. The table name is in aDbObjName2 argument, so the array element value is 2.
|
sl@0
|
115 |
const TUint8 KTableNameArgIndex[] =
|
sl@0
|
116 |
{
|
sl@0
|
117 |
/////////////////////////////////////////////////////////////////
|
sl@0
|
118 |
// aDbObjName1 aDbObjName2
|
sl@0
|
119 |
/////////////////////////////////////////////////////////////////
|
sl@0
|
120 |
1, //SQLITE_COPY Table Name Filename
|
sl@0
|
121 |
2, //SQLITE_CREATE_INDEX Index Name Table Name
|
sl@0
|
122 |
1, //SQLITE_CREATE_TABLE Table Name NULL
|
sl@0
|
123 |
2, //SQLITE_CREATE_TEMP_INDEX Index Name Table Name
|
sl@0
|
124 |
1, //SQLITE_CREATE_TEMP_TABLE Table Name NULL
|
sl@0
|
125 |
2, //SQLITE_CREATE_TEMP_TRIGGER Trigger Name Table Name
|
sl@0
|
126 |
0, //SQLITE_CREATE_TEMP_VIEW View Name NULL
|
sl@0
|
127 |
2, //SQLITE_CREATE_TRIGGER Trigger Name Table Name
|
sl@0
|
128 |
0, //SQLITE_CREATE_VIEW View Name NULL
|
sl@0
|
129 |
1, //SQLITE_DELETE Table Name NULL
|
sl@0
|
130 |
2, //SQLITE_DROP_INDEX Index Name Table Name
|
sl@0
|
131 |
1, //SQLITE_DROP_TABLE Table Name NULL
|
sl@0
|
132 |
2, //SQLITE_DROP_TEMP_INDEX Index Name Table Name
|
sl@0
|
133 |
1, //SQLITE_DROP_TEMP_TABLE Table Name NULL
|
sl@0
|
134 |
2, //SQLITE_DROP_TEMP_TRIGGER Trigger Name Table Name
|
sl@0
|
135 |
0, //SQLITE_DROP_TEMP_VIEW View Name NULL
|
sl@0
|
136 |
2, //SQLITE_DROP_TRIGGER Trigger Name Table Name
|
sl@0
|
137 |
0, //SQLITE_DROP_VIEW View Name NULL
|
sl@0
|
138 |
1, //SQLITE_INSERT Table Name NULL
|
sl@0
|
139 |
0, //SQLITE_PRAGMA Pragma Name 1st arg or NULL
|
sl@0
|
140 |
1, //SQLITE_READ Table Name Column Name
|
sl@0
|
141 |
0, //SQLITE_SELECT NULL NULL
|
sl@0
|
142 |
0, //SQLITE_TRANSACTION NULL NULL
|
sl@0
|
143 |
1, //SQLITE_UPDATE Table Name Column Name
|
sl@0
|
144 |
0, //SQLITE_ATTACH Filename NULL
|
sl@0
|
145 |
0, //SQLITE_DETACH Database Name NULL
|
sl@0
|
146 |
2, //SQLITE_ALTER_TABLE Database Name Table Name
|
sl@0
|
147 |
0, //SQLITE_REINDEX Index Name NULL
|
sl@0
|
148 |
1, //SQLITE_ANALYZE Table Name NULL
|
sl@0
|
149 |
1, //SQLITE_CREATE_VTABLE Table Name Module Name
|
sl@0
|
150 |
1, //SQLITE_DROP_VTABLE Table Name Module Name
|
sl@0
|
151 |
0 //SQLITE_FUNCTION Function Name NULL
|
sl@0
|
152 |
};
|
sl@0
|
153 |
|
sl@0
|
154 |
//The function returns the argument number where the table name is.
|
sl@0
|
155 |
inline TInt DbOp2TableNameArgIndex(TInt aDbOpType)
|
sl@0
|
156 |
{
|
sl@0
|
157 |
__ASSERT_DEBUG(aDbOpType > 0 && aDbOpType <= SQLITE_FUNCTION, __SQLPANIC2(ESqlPanicInternalError));
|
sl@0
|
158 |
return KTableNameArgIndex[aDbOpType];
|
sl@0
|
159 |
}
|
sl@0
|
160 |
|
sl@0
|
161 |
//The function returns the table name, which may be in aDbObjName1 or aDbObjName2, depending on aDbOpType value.
|
sl@0
|
162 |
//The return value is "const char" pointer to a zero terminated string.
|
sl@0
|
163 |
inline const char* DbOp2TableName(TInt aDbOpType, const char* aDbObjName1, const char* aDbObjName2)
|
sl@0
|
164 |
{
|
sl@0
|
165 |
TInt pos = DbOp2TableNameArgIndex(aDbOpType);
|
sl@0
|
166 |
if(pos == 2)
|
sl@0
|
167 |
{
|
sl@0
|
168 |
__ASSERT_DEBUG(aDbObjName2 != NULL, __SQLPANIC2(ESqlPanicInternalError));
|
sl@0
|
169 |
return aDbObjName2;
|
sl@0
|
170 |
}
|
sl@0
|
171 |
else if(pos == 1)
|
sl@0
|
172 |
{
|
sl@0
|
173 |
__ASSERT_DEBUG(aDbObjName1 != NULL, __SQLPANIC2(ESqlPanicInternalError));
|
sl@0
|
174 |
return aDbObjName1;
|
sl@0
|
175 |
}
|
sl@0
|
176 |
return NULL;//Some database operations do not use table name
|
sl@0
|
177 |
}
|
sl@0
|
178 |
|
sl@0
|
179 |
//This function returns the database name which may be in aDbObjName1 or aDbName depending on aDbOpType value.
|
sl@0
|
180 |
//The return value is "const char" pointer to a zero terminated string.
|
sl@0
|
181 |
inline const char* DbOp2DbName(TInt aDbOpType, const char* aDbObjName1, const char* aDbName)
|
sl@0
|
182 |
{
|
sl@0
|
183 |
if(aDbOpType == SQLITE_DETACH || aDbOpType == SQLITE_ALTER_TABLE)
|
sl@0
|
184 |
{
|
sl@0
|
185 |
__ASSERT_DEBUG(aDbObjName1 != NULL, __SQLPANIC2(ESqlPanicInternalError));
|
sl@0
|
186 |
return aDbObjName1;
|
sl@0
|
187 |
}
|
sl@0
|
188 |
return aDbName;//It may be NULL for some database operations
|
sl@0
|
189 |
}
|
sl@0
|
190 |
|
sl@0
|
191 |
/**
|
sl@0
|
192 |
This function performs pragma permission checks for non-secure and secure databases
|
sl@0
|
193 |
|
sl@0
|
194 |
@param aDbObjName1 Database, Table, View, Trigger, Index, Pragma or File name. It depends on the
|
sl@0
|
195 |
values of aDbOpType argument. UTF8 encoded, zero-terminated.
|
sl@0
|
196 |
@param aParamUsed ETrue if the pragma command has been executed with a parameter, EFalse otherwise
|
sl@0
|
197 |
@param aSecure ETrue if the pragam check if for secure database, EFalse otherwise
|
sl@0
|
198 |
|
sl@0
|
199 |
@return SQLITE_OK Access is allowed
|
sl@0
|
200 |
@return SQLITE_DENY The entire SQL statement should be aborted
|
sl@0
|
201 |
@return SQLITE_IGNORE The column should be treated as it has NULL value
|
sl@0
|
202 |
|
sl@0
|
203 |
@internalComponent
|
sl@0
|
204 |
*/
|
sl@0
|
205 |
static TInt PragmaCheck(const char* aDbObjName1, TBool aParamUsed, TBool aSecure)
|
sl@0
|
206 |
{
|
sl@0
|
207 |
//Retreive the pragma name
|
sl@0
|
208 |
TPtrC8 DbObjName1(KNullDesC8);
|
sl@0
|
209 |
DbObjName1.Set(reinterpret_cast <const TUint8*> (aDbObjName1));
|
sl@0
|
210 |
|
sl@0
|
211 |
//Access the pragma permissions table depending if its :-
|
sl@0
|
212 |
// 1) Secure or non-secure database.
|
sl@0
|
213 |
// 2) Parameter was used or not.
|
sl@0
|
214 |
for (TInt index=0; index<KMaxPragmaCommands; index++)
|
sl@0
|
215 |
{
|
sl@0
|
216 |
if (CompareNoCase8(DbObjName1,KPragmaCommands[index])== 0)
|
sl@0
|
217 |
{
|
sl@0
|
218 |
if (aSecure)
|
sl@0
|
219 |
{
|
sl@0
|
220 |
if(aParamUsed)
|
sl@0
|
221 |
return KPermissionsTable[index].iSecureWrite;
|
sl@0
|
222 |
else
|
sl@0
|
223 |
return KPermissionsTable[index].iSecureRead;
|
sl@0
|
224 |
}
|
sl@0
|
225 |
else
|
sl@0
|
226 |
{
|
sl@0
|
227 |
if(aParamUsed)
|
sl@0
|
228 |
return KPermissionsTable[index].iNonSecureWrite;
|
sl@0
|
229 |
else
|
sl@0
|
230 |
return KPermissionsTable[index].iNonSecureRead;
|
sl@0
|
231 |
}
|
sl@0
|
232 |
}
|
sl@0
|
233 |
}
|
sl@0
|
234 |
//If the pragma is not on the list then deny access
|
sl@0
|
235 |
return SQLITE_DENY;
|
sl@0
|
236 |
}
|
sl@0
|
237 |
|
sl@0
|
238 |
|
sl@0
|
239 |
/**
|
sl@0
|
240 |
This function performs additional permission checks for non-secure (private and public) databases
|
sl@0
|
241 |
|
sl@0
|
242 |
@param aDbOpType Database operation type, which needs to be authorized.
|
sl@0
|
243 |
@param aDbObjName1 Database, Table, View, Trigger, Index, Pragma or File name. It depends on the
|
sl@0
|
244 |
values of aDbOpType argument. UTF8 encoded, zero-terminated.
|
sl@0
|
245 |
@param aDbObjName2 Table or Column name. It depends on the values of aDbOpType argument. UTF8 encoded, zero-terminated.
|
sl@0
|
246 |
|
sl@0
|
247 |
@return SQLITE_OK Access is allowed
|
sl@0
|
248 |
@return SQLITE_DENY The entire SQL statement should be aborted
|
sl@0
|
249 |
@return SQLITE_IGNORE The column should be treated as it has NULL value
|
sl@0
|
250 |
|
sl@0
|
251 |
@panic SqlDb 7 In _DEBUG mode. Unknown/invalid aDbOpType argument.
|
sl@0
|
252 |
|
sl@0
|
253 |
@internalComponent
|
sl@0
|
254 |
*/
|
sl@0
|
255 |
static TInt NonSecureChecks(TInt aDbOpType,const char* aDbObjName1, const char* aDbObjName2)
|
sl@0
|
256 |
{
|
sl@0
|
257 |
//=================================================================
|
sl@0
|
258 |
// aDbOpType aDbObjName1 aDbObjName2
|
sl@0
|
259 |
//=================================================================
|
sl@0
|
260 |
TInt res = SQLITE_OK;
|
sl@0
|
261 |
switch(aDbOpType)
|
sl@0
|
262 |
{
|
sl@0
|
263 |
case SQLITE_CREATE_INDEX:// Index Name Table Name
|
sl@0
|
264 |
case SQLITE_CREATE_TABLE:// Table Name NULL
|
sl@0
|
265 |
case SQLITE_CREATE_TRIGGER:// Trigger Name Table Name
|
sl@0
|
266 |
case SQLITE_CREATE_VIEW:// View Name NULL
|
sl@0
|
267 |
case SQLITE_DROP_INDEX:// Index Name Table Name
|
sl@0
|
268 |
case SQLITE_DROP_TABLE:// Table Name NULL
|
sl@0
|
269 |
case SQLITE_DROP_TRIGGER:// Trigger Name Table Name
|
sl@0
|
270 |
case SQLITE_DROP_VIEW:// View Name NULL
|
sl@0
|
271 |
case SQLITE_ALTER_TABLE:// Database Name Table Name
|
sl@0
|
272 |
case SQLITE_SELECT:// NULL NULL
|
sl@0
|
273 |
case SQLITE_TRANSACTION:// NULL NULL
|
sl@0
|
274 |
case SQLITE_DELETE:// Table Name NULL
|
sl@0
|
275 |
case SQLITE_INSERT:// Table Name NULL
|
sl@0
|
276 |
case SQLITE_UPDATE:// Table Name Column Name
|
sl@0
|
277 |
case SQLITE_READ:// Table Name Column Name
|
sl@0
|
278 |
case SQLITE_ATTACH:// Filename NULL
|
sl@0
|
279 |
case SQLITE_DETACH:// Database Name NULL
|
sl@0
|
280 |
case SQLITE_REINDEX:// Index Name NULL
|
sl@0
|
281 |
case SQLITE_ANALYZE:// Table Name NULL
|
sl@0
|
282 |
case SQLITE_FUNCTION:
|
sl@0
|
283 |
break;
|
sl@0
|
284 |
case SQLITE_PRAGMA:// Pragma Name 1st arg or NULL
|
sl@0
|
285 |
res = PragmaCheck(aDbObjName1, (aDbObjName2 != NULL), EFalse);
|
sl@0
|
286 |
break;
|
sl@0
|
287 |
//All "temp" operations are handled earlier, in CSqlSrvDatabase::AuthorizeCallback(), where a check for "temp"
|
sl@0
|
288 |
//database name is performed.
|
sl@0
|
289 |
// case SQLITE_CREATE_TEMP_INDEX:// Index Name Table Name
|
sl@0
|
290 |
// case SQLITE_CREATE_TEMP_TABLE:// Table Name NULL
|
sl@0
|
291 |
// case SQLITE_CREATE_TEMP_TRIGGER:// Trigger Name Table Name
|
sl@0
|
292 |
// case SQLITE_CREATE_TEMP_VIEW:// View Name NULL
|
sl@0
|
293 |
// case SQLITE_DROP_TEMP_INDEX:// Index Name Table Name
|
sl@0
|
294 |
// case SQLITE_DROP_TEMP_TABLE:// Table Name NULL
|
sl@0
|
295 |
// case SQLITE_DROP_TEMP_TRIGGER:// Trigger Name Table Name
|
sl@0
|
296 |
// case SQLITE_DROP_TEMP_VIEW:// View Name NULL
|
sl@0
|
297 |
//"CREATE VIRTUAL TABLE" and "DROP VIRTUAL TABLE" sql statements are not supported
|
sl@0
|
298 |
// case SQLITE_CREATE_VTABLE:
|
sl@0
|
299 |
// case SQLITE_DROP_VTABLE:
|
sl@0
|
300 |
default:
|
sl@0
|
301 |
__ASSERT_DEBUG(EFalse, __SQLPANIC2(ESqlPanicInternalError));
|
sl@0
|
302 |
break;
|
sl@0
|
303 |
}
|
sl@0
|
304 |
return res;
|
sl@0
|
305 |
}
|
sl@0
|
306 |
|
sl@0
|
307 |
/**
|
sl@0
|
308 |
This function performs additional permission checks for secure databases
|
sl@0
|
309 |
|
sl@0
|
310 |
@param aSecurityPolicy Security policy corresponding to this database
|
sl@0
|
311 |
@param aDbOpType Database operation type, which needs to be authorized.
|
sl@0
|
312 |
@param aDbObjName1 Database, Table, View, Trigger, Index, Pragma or File name. It depends on the
|
sl@0
|
313 |
values of aDbOpType argument. UTF8 encoded, zero-terminated.
|
sl@0
|
314 |
@param aDbObjName2 Table or Column name. It depends on the values of aDbOpType argument. UTF8 encoded, zero-terminated.
|
sl@0
|
315 |
|
sl@0
|
316 |
@return SQLITE_OK Access is allowed
|
sl@0
|
317 |
@return SQLITE_DENY The entire SQL statement should be aborted
|
sl@0
|
318 |
@return SQLITE_IGNORE The column should be treated as it has NULL value
|
sl@0
|
319 |
|
sl@0
|
320 |
@panic SqlDb 7 In _DEBUG mode. Unknown/invalid aDbOpType argument.
|
sl@0
|
321 |
|
sl@0
|
322 |
@internalComponent
|
sl@0
|
323 |
*/
|
sl@0
|
324 |
static TInt SecureChecks(const CSqlSecurityPolicy* aSecurityPolicy,TInt aDbOpType,const char* aDbObjName1, const char* aDbObjName2)
|
sl@0
|
325 |
{
|
sl@0
|
326 |
TPtrC8 tblName(KNullDesC8);
|
sl@0
|
327 |
const char* tblNamePtr = DbOp2TableName(aDbOpType, aDbObjName1, aDbObjName2);
|
sl@0
|
328 |
if(tblNamePtr)
|
sl@0
|
329 |
{
|
sl@0
|
330 |
tblName.Set(reinterpret_cast <const TUint8*> (tblNamePtr));
|
sl@0
|
331 |
}
|
sl@0
|
332 |
|
sl@0
|
333 |
//Under no circumstances is allowed to do any operation with the system tables.
|
sl@0
|
334 |
//(Even SQLITE_READ operation, because the system tables data is read at the moment when the database
|
sl@0
|
335 |
// is created/opened)
|
sl@0
|
336 |
if(::IsSystemTableName(tblName))
|
sl@0
|
337 |
{
|
sl@0
|
338 |
return SQLITE_DENY;
|
sl@0
|
339 |
}
|
sl@0
|
340 |
//=================================================================
|
sl@0
|
341 |
// aDbOpType aDbObjName1 aDbObjName2
|
sl@0
|
342 |
//=================================================================
|
sl@0
|
343 |
MSqlPolicyInspector& inspector = ::SqlServer().SecurityInspector();
|
sl@0
|
344 |
TSecurityPolicy schemaPolicy = aSecurityPolicy->DbPolicy(RSqlSecurityPolicy::ESchemaPolicy);
|
sl@0
|
345 |
TSecurityPolicy writePolicy = aSecurityPolicy->DbPolicy(RSqlSecurityPolicy::EWritePolicy);
|
sl@0
|
346 |
TSecurityPolicy readPolicy = aSecurityPolicy->DbPolicy(RSqlSecurityPolicy::EReadPolicy);
|
sl@0
|
347 |
TInt res = SQLITE_OK;
|
sl@0
|
348 |
switch(aDbOpType)
|
sl@0
|
349 |
{
|
sl@0
|
350 |
//"Database schema policy" check
|
sl@0
|
351 |
case SQLITE_CREATE_INDEX:// Index Name Table Name
|
sl@0
|
352 |
case SQLITE_CREATE_TABLE:// Table Name NULL
|
sl@0
|
353 |
case SQLITE_CREATE_TRIGGER:// Trigger Name Table Name
|
sl@0
|
354 |
case SQLITE_CREATE_VIEW:// View Name NULL
|
sl@0
|
355 |
case SQLITE_DROP_INDEX:// Index Name Table Name
|
sl@0
|
356 |
case SQLITE_DROP_TABLE:// Table Name NULL
|
sl@0
|
357 |
case SQLITE_DROP_TRIGGER:// Trigger Name Table Name
|
sl@0
|
358 |
case SQLITE_DROP_VIEW:// View Name NULL
|
sl@0
|
359 |
case SQLITE_ALTER_TABLE:// Database Name Table Name
|
sl@0
|
360 |
if(!inspector.Check(schemaPolicy))
|
sl@0
|
361 |
{
|
sl@0
|
362 |
res = SQLITE_DENY;
|
sl@0
|
363 |
}
|
sl@0
|
364 |
break;
|
sl@0
|
365 |
//No policy check
|
sl@0
|
366 |
case SQLITE_SELECT:// NULL NULL
|
sl@0
|
367 |
case SQLITE_TRANSACTION:// NULL NULL
|
sl@0
|
368 |
break;
|
sl@0
|
369 |
//"Database schema policy" for sqlite tables
|
sl@0
|
370 |
//"Database schema policy" || "Database write policy" for user tables
|
sl@0
|
371 |
case SQLITE_DELETE:// Table Name NULL
|
sl@0
|
372 |
case SQLITE_INSERT:// Table Name NULL
|
sl@0
|
373 |
case SQLITE_UPDATE:// Table Name Column Name
|
sl@0
|
374 |
if(!inspector.Check(schemaPolicy))
|
sl@0
|
375 |
{
|
sl@0
|
376 |
res = SQLITE_DENY;
|
sl@0
|
377 |
if(!::IsSqliteTableName(tblName))
|
sl@0
|
378 |
{
|
sl@0
|
379 |
if(inspector.Check(writePolicy))
|
sl@0
|
380 |
{
|
sl@0
|
381 |
res = SQLITE_OK;
|
sl@0
|
382 |
}
|
sl@0
|
383 |
}
|
sl@0
|
384 |
}
|
sl@0
|
385 |
break;
|
sl@0
|
386 |
//"Database schema policy" || "Database read policy" || "Database write policy" for sqlite tables
|
sl@0
|
387 |
//"Database schema policy" || "Database read policy" for user tables
|
sl@0
|
388 |
case SQLITE_READ:// Table Name Column Name
|
sl@0
|
389 |
if(!(inspector.Check(schemaPolicy) || inspector.Check(readPolicy)))
|
sl@0
|
390 |
{
|
sl@0
|
391 |
res = SQLITE_DENY;
|
sl@0
|
392 |
if(::IsSqliteTableName(tblName))
|
sl@0
|
393 |
{
|
sl@0
|
394 |
if(inspector.Check(writePolicy))
|
sl@0
|
395 |
{
|
sl@0
|
396 |
res = SQLITE_OK;
|
sl@0
|
397 |
}
|
sl@0
|
398 |
}
|
sl@0
|
399 |
}
|
sl@0
|
400 |
break;
|
sl@0
|
401 |
case SQLITE_PRAGMA:// Pragma Name 1st arg or NULL
|
sl@0
|
402 |
res = PragmaCheck(aDbObjName1, (aDbObjName2 != NULL), ETrue);
|
sl@0
|
403 |
break;
|
sl@0
|
404 |
case SQLITE_ATTACH:// Filename NULL
|
sl@0
|
405 |
case SQLITE_DETACH:// Database Name NULL
|
sl@0
|
406 |
//If the operation is SQLITE_ATTACH or SQLITE_DETACH, return SQLITE_DENY.
|
sl@0
|
407 |
//"ATTACH DATABASE"/"DETACH DATABASE" operations are performed by separate "attach/detach db" methods.
|
sl@0
|
408 |
res = SQLITE_DENY;
|
sl@0
|
409 |
break;
|
sl@0
|
410 |
//No policy check
|
sl@0
|
411 |
case SQLITE_REINDEX:// Index Name NULL
|
sl@0
|
412 |
case SQLITE_ANALYZE:// Table Name NULL
|
sl@0
|
413 |
break;
|
sl@0
|
414 |
//No policy check
|
sl@0
|
415 |
case SQLITE_FUNCTION:
|
sl@0
|
416 |
break;
|
sl@0
|
417 |
//All "temp" operations are handled earlier, in CSqlSrvDatabase::AuthorizeCallback(), where a check for "temp"
|
sl@0
|
418 |
//database name is performed.
|
sl@0
|
419 |
// case SQLITE_CREATE_TEMP_INDEX:// Index Name Table Name
|
sl@0
|
420 |
// case SQLITE_CREATE_TEMP_TABLE:// Table Name NULL
|
sl@0
|
421 |
// case SQLITE_CREATE_TEMP_TRIGGER:// Trigger Name Table Name
|
sl@0
|
422 |
// case SQLITE_CREATE_TEMP_VIEW:// View Name NULL
|
sl@0
|
423 |
// case SQLITE_DROP_TEMP_INDEX:// Index Name Table Name
|
sl@0
|
424 |
// case SQLITE_DROP_TEMP_TABLE:// Table Name NULL
|
sl@0
|
425 |
// case SQLITE_DROP_TEMP_TRIGGER:// Trigger Name Table Name
|
sl@0
|
426 |
// case SQLITE_DROP_TEMP_VIEW:// View Name NULL
|
sl@0
|
427 |
//"CREATE VIRTUAL TABLE" and "DROP VIRTUAL TABLE" sql statements are not supported
|
sl@0
|
428 |
// case SQLITE_CREATE_VTABLE:
|
sl@0
|
429 |
// case SQLITE_DROP_VTABLE:
|
sl@0
|
430 |
default:
|
sl@0
|
431 |
__ASSERT_DEBUG(EFalse, __SQLPANIC2(ESqlPanicInternalError));
|
sl@0
|
432 |
break;
|
sl@0
|
433 |
}
|
sl@0
|
434 |
return res;
|
sl@0
|
435 |
}
|
sl@0
|
436 |
|
sl@0
|
437 |
/**
|
sl@0
|
438 |
This callback function is invoked by the SQLITE engine at SQL statement compile time
|
sl@0
|
439 |
for each attempt to access a column of a table in the database.
|
sl@0
|
440 |
|
sl@0
|
441 |
The callback returns SQLITE_OK if access is allowed,
|
sl@0
|
442 |
SQLITE_DENY if the entire SQL statement should be aborted with an error and
|
sl@0
|
443 |
SQLITE_IGNORE if the column should be treated as a NULL value.
|
sl@0
|
444 |
|
sl@0
|
445 |
@param aDb "This" pointer (to the rellated CSqlSrvDatabase object).
|
sl@0
|
446 |
@param aDbOpType Database operation type, which needs to be authorized. It could be one of these:
|
sl@0
|
447 |
|
sl@0
|
448 |
@code
|
sl@0
|
449 |
=================================================================
|
sl@0
|
450 |
aDbOpType aDbObjName1 aDbObjName2
|
sl@0
|
451 |
=================================================================
|
sl@0
|
452 |
SQLITE_CREATE_INDEX Index Name Table Name
|
sl@0
|
453 |
SQLITE_CREATE_TABLE Table Name NULL
|
sl@0
|
454 |
SQLITE_CREATE_TEMP_INDEX Index Name Table Name
|
sl@0
|
455 |
SQLITE_CREATE_TEMP_TABLE Table Name NULL
|
sl@0
|
456 |
SQLITE_CREATE_TEMP_TRIGGER Trigger Name Table Name
|
sl@0
|
457 |
SQLITE_CREATE_TEMP_VIEW View Name NULL
|
sl@0
|
458 |
SQLITE_CREATE_TRIGGER Trigger Name Table Name
|
sl@0
|
459 |
SQLITE_CREATE_VIEW View Name NULL
|
sl@0
|
460 |
SQLITE_DELETE Table Name NULL
|
sl@0
|
461 |
SQLITE_DROP_INDEX Index Name Table Name
|
sl@0
|
462 |
SQLITE_DROP_TABLE Table Name NULL
|
sl@0
|
463 |
SQLITE_DROP_TEMP_INDEX Index Name Table Name
|
sl@0
|
464 |
SQLITE_DROP_TEMP_TABLE Table Name NULL
|
sl@0
|
465 |
SQLITE_DROP_TEMP_TRIGGER Trigger Name Table Name
|
sl@0
|
466 |
SQLITE_DROP_TEMP_VIEW View Name NULL
|
sl@0
|
467 |
SQLITE_DROP_TRIGGER Trigger Name Table Name
|
sl@0
|
468 |
SQLITE_DROP_VIEW View Name NULL
|
sl@0
|
469 |
SQLITE_INSERT Table Name NULL
|
sl@0
|
470 |
SQLITE_PRAGMA Pragma Name 1st arg or NULL
|
sl@0
|
471 |
SQLITE_READ Table Name Column Name
|
sl@0
|
472 |
SQLITE_SELECT NULL NULL
|
sl@0
|
473 |
SQLITE_TRANSACTION NULL NULL
|
sl@0
|
474 |
SQLITE_UPDATE Table Name Column Name
|
sl@0
|
475 |
SQLITE_ATTACH Filename NULL
|
sl@0
|
476 |
SQLITE_DETACH Database Name NULL
|
sl@0
|
477 |
SQLITE_ALTER_TABLE Database Name Table Name
|
sl@0
|
478 |
SQLITE_REINDEX Index Name NULL
|
sl@0
|
479 |
SQLITE_ANALYZE Table Name NULL
|
sl@0
|
480 |
SQLITE_CREATE_VTABLE Table Name Module Name
|
sl@0
|
481 |
SQLITE_DROP_VTABLE Table Name Module Name
|
sl@0
|
482 |
SQLITE_FUNCTION Function Name NULL
|
sl@0
|
483 |
=================================================================
|
sl@0
|
484 |
@endcode
|
sl@0
|
485 |
|
sl@0
|
486 |
@param aDbObjName1 Database, Table, View, Trigger, Index, Pragma or File name. It depends on the
|
sl@0
|
487 |
values of aDbOpType argument. UTF8 encoded, zero-terminated.
|
sl@0
|
488 |
@param aDbObjName2 Table or Column name. It depends on the values of aDbOpType argument. UTF8 encoded, zero-terminated.
|
sl@0
|
489 |
@param aDbName Database name - "main", "temp", etc. UTF8 encoded, zero-terminated.
|
sl@0
|
490 |
@param aTrgOrViewName The name of the inner-most trigger or view that is responsible for the access
|
sl@0
|
491 |
attempt or NULL if this access attempt is directly from input SQL code. UTF8 encoded, zero-terminated.
|
sl@0
|
492 |
|
sl@0
|
493 |
@return SQLITE_OK Access is allowed
|
sl@0
|
494 |
@return SQLITE_DENY The entire SQL statement should be aborted
|
sl@0
|
495 |
@return SQLITE_IGNORE The column should be treated as it has NULL value
|
sl@0
|
496 |
|
sl@0
|
497 |
@panic SqlDb 4 In _DEBUG mode. The authorizer was called with NULL aDb argument.
|
sl@0
|
498 |
|
sl@0
|
499 |
@internalComponent
|
sl@0
|
500 |
*/
|
sl@0
|
501 |
TInt CSqlSrvDatabase::AuthorizeCallback(void* aDb, TInt aDbOpType,
|
sl@0
|
502 |
const char* aDbObjName1, const char* aDbObjName2,
|
sl@0
|
503 |
const char* aDbName, const char* aTrgOrViewName)
|
sl@0
|
504 |
{
|
sl@0
|
505 |
UNUSED_ARG(aTrgOrViewName);
|
sl@0
|
506 |
__ASSERT_DEBUG(aDb != NULL, __SQLPANIC2(ESqlPanicBadArgument));
|
sl@0
|
507 |
|
sl@0
|
508 |
#ifdef _SQL_AUTHORIZER_TRACE_ENABLED
|
sl@0
|
509 |
enum TDbOpType {EOpCreateIndex = 1, EOpCreateTable, EOpCreateTempIndex, EOpCreateTempTable,
|
sl@0
|
510 |
EOpCreateTempTrigger, EOpCreateTempView, EOpCreateTrigger, EOpCreateView, EOpDelete, EOpDropIndex,
|
sl@0
|
511 |
EOpDropTable, EOpDropTempIndex, EOpDropTempTable, EOpDropTempTrigger, EOpDropTempView, EOpDropTrigger,
|
sl@0
|
512 |
EOpDropView, EOpInsert, EOpPragma, EOpRead, EOpSelect, EOpTransaction, EOpUpdate, EOpAttach, EOpDettach,
|
sl@0
|
513 |
EOpAlterTable, EOpReindex, EOpAnalyze, EOpCreateVTable, EOpDropVTable, EOpFunctionCall};
|
sl@0
|
514 |
TDbOpType dbOpType = static_cast <TDbOpType> (aDbOpType);//can be seen now in the debugger
|
sl@0
|
515 |
::PrintAuthorizerArguments(dbOpType, aDbObjName1, aDbObjName2, aDbName, aTrgOrViewName);
|
sl@0
|
516 |
#endif
|
sl@0
|
517 |
|
sl@0
|
518 |
CSqlSrvDatabase& db = *static_cast <CSqlSrvDatabase*> (aDb);
|
sl@0
|
519 |
|
sl@0
|
520 |
//1. If the authorizer is currently disabled - return SQLITE_OK.
|
sl@0
|
521 |
// (This happens when a database is attached/detached)
|
sl@0
|
522 |
if(db.iAuthorizerDisabled)
|
sl@0
|
523 |
{
|
sl@0
|
524 |
return SQLITE_OK;
|
sl@0
|
525 |
}
|
sl@0
|
526 |
|
sl@0
|
527 |
TPtrC8 dbName(KNullDesC8);
|
sl@0
|
528 |
const char* dbNamePtr = DbOp2DbName(aDbOpType, aDbObjName1, aDbName);//dbNamePtr is zero terminated
|
sl@0
|
529 |
if(dbNamePtr)
|
sl@0
|
530 |
{
|
sl@0
|
531 |
dbName.Set(reinterpret_cast <const TUint8*> (dbNamePtr));
|
sl@0
|
532 |
}
|
sl@0
|
533 |
aDbName = NULL;//No more use of aDbName argument inside the function.
|
sl@0
|
534 |
|
sl@0
|
535 |
//2. If the database name is KTempDb, then allow the access. It is a local database
|
sl@0
|
536 |
// (for the client), deleted when closed.
|
sl@0
|
537 |
if(dbName.Compare(KTempDb8) == 0) //dbName is guaranteed to be in lower case if it is "temp",
|
sl@0
|
538 |
{ //so it is possible to use binary string comparison
|
sl@0
|
539 |
return SQLITE_OK;
|
sl@0
|
540 |
}
|
sl@0
|
541 |
|
sl@0
|
542 |
//3. Find the security policies. For DefaultAccess initialized with NULL.
|
sl@0
|
543 |
const CSqlSecurityPolicy* securityPolicy = NULL;
|
sl@0
|
544 |
if(dbName.Compare(KMainDb8) == 0||dbName.Length() == 0) //dbName is guaranteed to be in lower case if it is "main",
|
sl@0
|
545 |
{ //so it is possible to use binary string comparison
|
sl@0
|
546 |
//4. This is the main database.
|
sl@0
|
547 |
securityPolicy = db.iSecurityPolicy;
|
sl@0
|
548 |
}
|
sl@0
|
549 |
else
|
sl@0
|
550 |
{
|
sl@0
|
551 |
//5. This is an attached database. Find the attached database security policies.
|
sl@0
|
552 |
//dbNamePtr is used here because it is zero terminated
|
sl@0
|
553 |
TSqlAttachDbPair* attachDbPair = db.iAttachDbMap.Entry(reinterpret_cast <const TUint8*> (dbNamePtr));
|
sl@0
|
554 |
if(attachDbPair)
|
sl@0
|
555 |
{//secure database, find the security policies
|
sl@0
|
556 |
const TUint8* securityMapKey = attachDbPair->iData;
|
sl@0
|
557 |
RSqlSecurityMap& map = ::SqlServer().SecurityMap();
|
sl@0
|
558 |
TSqlSecurityPair* pair = map.Entry(securityMapKey);
|
sl@0
|
559 |
if(pair)
|
sl@0
|
560 |
{
|
sl@0
|
561 |
securityPolicy = pair->iData;
|
sl@0
|
562 |
}
|
sl@0
|
563 |
}
|
sl@0
|
564 |
}
|
sl@0
|
565 |
|
sl@0
|
566 |
//Here we have:
|
sl@0
|
567 |
// - valid database name (not NULL);
|
sl@0
|
568 |
|
sl@0
|
569 |
//6. Default or Security Policy Checks
|
sl@0
|
570 |
return !securityPolicy ? NonSecureChecks(aDbOpType,aDbObjName1,aDbObjName2): SecureChecks(securityPolicy,aDbOpType,aDbObjName1,aDbObjName2);
|
sl@0
|
571 |
}
|