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
|
/*
SQLite3.cpp
Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com)
Copyright (C) 2009 RedHat inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <sqlite3.h>
#include "abrtlib.h"
#include "SQLite3.h"
#include "ABRTException.h"
using namespace std;
#define ABRT_TABLE_VERSION 2
#define ABRT_TABLE_VERSION_STR "2"
#define ABRT_TABLE "abrt_v"ABRT_TABLE_VERSION_STR
#define SQLITE3_MASTER_TABLE "sqlite_master"
#define COL_UUID "UUID"
#define COL_UID "UID"
#define COL_DEBUG_DUMP_PATH "DebugDumpPath"
#define COL_COUNT "Count"
#define COL_REPORTED "Reported"
#define COL_TIME "Time"
#define COL_MESSAGE "Message"
// after a while, we can drop support for update, so a table can stay in
// normal limits
static const char *const upate_sql_commands[][ABRT_TABLE_VERSION + 1] = {
// v0 -> *
{
// v0 -> v0
";",
// v0 -> v1
"ALTER TABLE abrt ADD "COL_MESSAGE" VARCHAR NOT NULL DEFAULT '';",
// v0 -> v2
"BEGIN TRANSACTION;"
"ALTER TABLE abrt RENAME TO abrt_v2;"
"ALTER TABLE abrt_v2 ADD "COL_MESSAGE" VARCHAR NOT NULL DEFAULT '';"
"COMMIT;",
},
//v1 -> *
{
// v1 -> v0
// TODO: does it make sense to support downgrade?
";",
// v1 -> v1
";",
// v1 -> v2
"BEGIN TRANSACTION;"
"CREATE TABLE abrt_v2 ("
COL_UUID" VARCHAR NOT NULL,"
COL_UID" VARCHAR NOT NULL,"
COL_DEBUG_DUMP_PATH" VARCHAR NOT NULL,"
COL_COUNT" INT NOT NULL DEFAULT 1,"
COL_REPORTED" INT NOT NULL DEFAULT 0,"
COL_TIME" VARCHAR NOT NULL DEFAULT 0,"
COL_MESSAGE" VARCHAR NOT NULL DEFAULT '',"
"PRIMARY KEY ("COL_UUID","COL_UID"));"
"INSERT INTO abrt_v2 "
"SELECT "COL_UUID","
COL_UID","
COL_DEBUG_DUMP_PATH","
COL_COUNT","
COL_REPORTED","
COL_TIME","
COL_MESSAGE
" FROM abrt;"
"DROP TABLE abrt;"
"COMMIT;",
},
};
/* Is this string safe wrt SQL injection?
* PHP's mysql_real_escape_string() treats \, ', ", \x00, \n, \r, and \x1a as special.
* We are a bit more paranoid and disallow any control chars.
*/
static bool is_string_safe(const char *str)
{
// Apparently SQLite allows unescaped newlines. More surprisingly,
// it does not unescape escaped ones - I see lines ending with \ when I do it.
// I wonder whether this is a bug in SQLite, and whether using unescaped
// newlines is a danger with other SQL servers.
// For now, I disabled newline escaping...
const char *p = str;
while (*p)
{
unsigned char c = *p;
// if (c == '\\' && p[1] != '\0')
// {
// p += 2;
// continue;
// }
if ((c < ' ' && c != '\n')
|| strchr("\\\"\'", c)
) {
error_msg("Probable SQL injection: '%s'", str);
return false;
}
p++;
}
return true;
}
#ifdef UNUSED_FOR_NOW
/* Escape \n */
static string sql_escape(const char *str)
{
const char *s = str;
unsigned len = 0;
do
{
if (*s == '\n')
len++;
len++;
} while (*s++);
char buf[len];
s = str;
char *d = buf;
do
{
if (*s == '\n')
*d++ = '\\';
*d++ = *s;
} while (*s++);
return buf;
}
#endif
static void get_table(vector_database_rows_t& pTable,
sqlite3 *db, const char *fmt, ...)
{
va_list p;
va_start(p, fmt);
char *sql = xvasprintf(fmt, p);
va_end(p);
char **table;
int ncol, nrow;
char *err = NULL;
int ret = sqlite3_get_table(db, sql, &table, &nrow, &ncol, &err);
if (ret != SQLITE_OK)
{
string errstr = ssprintf("Error in SQL:'%s' error: %s", sql, err);
free(sql);
sqlite3_free(err);
throw CABRTException(EXCEP_PLUGIN, errstr.c_str());
}
VERB2 log("%d rows returned by SQL:%s", nrow, sql);
free(sql);
pTable.clear();
int ii;
for (ii = 0; ii < nrow; ii++)
{
int jj;
database_row_t row;
for (jj = 0; jj < ncol; jj++)
{
char *val = table[jj + (ncol*ii) + ncol];
switch (jj)
{
case 0: row.m_sUUID = val; break;
case 1: row.m_sUID = val; break;
case 2: row.m_sDebugDumpDir = val; break;
case 3: row.m_sCount = val; break;
case 4: row.m_sReported = val; break;
case 5: row.m_sTime = val; break;
case 6: row.m_sMessage = val; break;
}
}
pTable.push_back(row);
}
sqlite3_free_table(table);
}
static void execute_sql(sqlite3 *db, const char *fmt, ...)
{
va_list p;
va_start(p, fmt);
char *sql = xvasprintf(fmt, p);
va_end(p);
char *err = NULL;
int ret = sqlite3_exec(db, sql, /*callback:*/ NULL, /*callback param:*/ NULL, &err);
if (ret != SQLITE_OK)
{
string errstr = ssprintf("Error in SQL:'%s' error: %s", sql, err);
free(sql);
sqlite3_free(err);
throw CABRTException(EXCEP_PLUGIN, errstr.c_str());
}
VERB2 log("%d rows affected by SQL:%s", sqlite3_changes(db), sql);
free(sql);
}
static bool exists_uuid_uid(sqlite3 *db, const char *pUUID, const char *pUID)
{
vector_database_rows_t table;
get_table(table, db,
"SELECT "COL_REPORTED" FROM "ABRT_TABLE" WHERE "
COL_UUID" = '%s' "
"AND ("COL_UID" = '%s' OR "COL_UID" = '-1');",
pUUID, pUID
);
return !table.empty();
}
static void update_from_old_ver(sqlite3 *db, int pOldVersion)
{
execute_sql(db, upate_sql_commands[pOldVersion][ABRT_TABLE_VERSION]);
}
static bool check_table(sqlite3 *db)
{
const char *command = "SELECT NAME, SQL FROM "SQLITE3_MASTER_TABLE" "
"WHERE TYPE='table';";
char **table;
int ncol, nrow;
char *err;
int ret = sqlite3_get_table(db, command, &table, &nrow, &ncol, &err);
if (ret != SQLITE_OK)
{
/* Should never happen */
error_msg_and_die("SQLite3 database is corrupted");
}
if (!nrow)
{
return false;
}
string tableName = table[0 + ncol];
string::size_type pos = tableName.find("_");
if (pos != string::npos)
{
string tableVersion = tableName.substr(pos + 2);
if (xatoi_u(tableVersion.c_str()) < ABRT_TABLE_VERSION)
{
update_from_old_ver(db, xatoi_u(tableVersion.c_str()));
}
return true;
}
// TODO: after some time could be removed, and if the table is that old,
// then simply drop it and create new one
// hack for version 0 and 1
string sql = table[1 + ncol];
if (sql.find(COL_MESSAGE) != string::npos)
{
update_from_old_ver(db, 1);
return true;
}
update_from_old_ver(db, 0);
return true;
}
/*
static bool check_table()
{
vector_database_rows_t table;
get_table(table, m_pDB,
"SELECT NAME FROM "SQLITE3_MASTER_TABLE" "
"WHERE TYPE='table' AND NAME='"ABRT_TABLE"';");
return table.size() == 1;
}
*/
CSQLite3::CSQLite3() :
m_sDBPath(LOCALSTATEDIR "/cache/abrt/abrt-db"),
m_pDB(NULL)
{}
CSQLite3::~CSQLite3()
{
/* Paranoia. In C++, destructor will abort() if it was called while unwinding
* the stack and it throws an exception.
*/
try
{
DisConnect();
m_sDBPath.clear();
}
catch (...)
{
error_msg_and_die("Internal error");
}
}
void CSQLite3::DisConnect()
{
if (m_pDB)
{
sqlite3_close(m_pDB);
m_pDB = NULL;
}
}
void CSQLite3::Connect()
{
int ret = sqlite3_open_v2(m_sDBPath.c_str(),
&m_pDB,
SQLITE_OPEN_READWRITE,
NULL
);
if (ret != SQLITE_OK)
{
if (ret != SQLITE_CANTOPEN)
{
throw CABRTException(EXCEP_PLUGIN, "Can't open database '%s': %s", m_sDBPath.c_str(), sqlite3_errmsg(m_pDB));
}
ret = sqlite3_open_v2(m_sDBPath.c_str(),
&m_pDB,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
NULL
);
if (ret != SQLITE_OK)
{
throw CABRTException(EXCEP_PLUGIN, "Can't create database '%s': %s", m_sDBPath.c_str(), sqlite3_errmsg(m_pDB));
}
}
if (!check_table(m_pDB))
{
execute_sql(m_pDB,
"CREATE TABLE "ABRT_TABLE" ("
COL_UUID" VARCHAR NOT NULL,"
COL_UID" VARCHAR NOT NULL,"
COL_DEBUG_DUMP_PATH" VARCHAR NOT NULL,"
COL_COUNT" INT NOT NULL DEFAULT 1,"
COL_REPORTED" INT NOT NULL DEFAULT 0,"
COL_TIME" VARCHAR NOT NULL DEFAULT 0,"
COL_MESSAGE" VARCHAR NOT NULL DEFAULT '',"
"PRIMARY KEY ("COL_UUID","COL_UID"));"
);
}
}
void CSQLite3::Insert_or_Update(const char *pUUID,
const char *pUID,
const char *pDebugDumpPath,
const char *pTime)
{
if (!is_string_safe(pUUID)
|| !is_string_safe(pUID)
|| !is_string_safe(pDebugDumpPath)
|| !is_string_safe(pTime)
) {
return;
}
if (!exists_uuid_uid(m_pDB, pUUID, pUID))
{
execute_sql(m_pDB,
"INSERT INTO "ABRT_TABLE" ("
COL_UUID","
COL_UID","
COL_DEBUG_DUMP_PATH","
COL_TIME
")"
" VALUES ('%s','%s','%s','%s');",
pUUID, pUID, pDebugDumpPath, pTime
);
}
else
{
execute_sql(m_pDB,
"UPDATE "ABRT_TABLE" SET "
COL_COUNT" = "COL_COUNT" + 1, "
COL_TIME" = '%s'"
" WHERE "COL_UUID" = '%s'"
" AND "COL_UID" = '%s';",
pTime, pUUID, pUID
);
}
}
void CSQLite3::DeleteRow(const char *pUUID, const char *pUID)
{
if (!is_string_safe(pUUID)
|| !is_string_safe(pUID)
) {
return;
}
if (pUID[0] == '0' && !pUID[1])
{
execute_sql(m_pDB,
"DELETE FROM "ABRT_TABLE" "
"WHERE "COL_UUID" = '%s';",
pUUID
);
}
else if (exists_uuid_uid(m_pDB, pUUID, pUID))
{
execute_sql(m_pDB, "DELETE FROM "ABRT_TABLE" "
"WHERE "COL_UUID" = '%s' "
"AND ("COL_UID" = '%s' OR "COL_UID" = '-1');",
pUUID, pUID
);
}
else
{
error_msg("UUID,UID %s,%s is not found in DB", pUUID, pUID);
}
}
void CSQLite3::DeleteRows_by_dir(const char *dump_dir)
{
if (!is_string_safe(dump_dir))
{
return;
}
execute_sql(m_pDB,
"DELETE FROM "ABRT_TABLE" "
"WHERE "COL_DEBUG_DUMP_PATH" = '%s'",
dump_dir
);
}
void CSQLite3::SetReported(const char *pUUID, const char *pUID, const char *pMessage)
{
// string escaped_msg = sql_escape(pMessage);
//#define pMessage pMessage_must_not_be_used_below
if (!is_string_safe(pUUID)
|| !is_string_safe(pUID)
|| !is_string_safe(pMessage)
) {
return;
}
if (pUID[0] == '0' && !pUID[1])
{
execute_sql(m_pDB,
"UPDATE "ABRT_TABLE" "
"SET "COL_REPORTED" = 1 "
"WHERE "COL_UUID" = '%s';",
pUUID
);
execute_sql(m_pDB, "UPDATE "ABRT_TABLE" "
"SET "COL_MESSAGE" = '%s' "
"WHERE "COL_UUID" = '%s';",
pMessage, pUUID
);
}
else if (exists_uuid_uid(m_pDB, pUUID, pUID))
{
execute_sql(m_pDB,
"UPDATE "ABRT_TABLE" "
"SET "COL_REPORTED" = 1 "
"WHERE "COL_UUID" = '%s' "
"AND ("COL_UID" = '%s' OR "COL_UID" = '-1');",
pUUID, pUID
);
execute_sql(m_pDB,
"UPDATE "ABRT_TABLE" "
"SET "COL_MESSAGE" = '%s' "
"WHERE "COL_UUID" = '%s' "
"AND ("COL_UID" = '%s' OR "COL_UID" = '-1');",
pMessage, pUUID, pUID
);
}
else
{
error_msg("UUID,UID %s,%s is not found in DB", pUUID, pUID);
}
}
vector_database_rows_t CSQLite3::GetUIDData(const char *pUID)
{
vector_database_rows_t table;
if (!is_string_safe(pUID))
{
return table;
}
if (pUID[0] == '0' && !pUID[1])
{
get_table(table, m_pDB, "SELECT * FROM "ABRT_TABLE";");
}
else
{
get_table(table, m_pDB,
"SELECT * FROM "ABRT_TABLE
" WHERE "COL_UID" = '%s' OR "COL_UID" = '-1';",
pUID
);
}
return table;
}
database_row_t CSQLite3::GetRow(const char *pUUID, const char *pUID)
{
if (!is_string_safe(pUUID)
|| !is_string_safe(pUID)
) {
return database_row_t();
}
vector_database_rows_t table;
if (pUID[0] == '0' && !pUID[1])
{
get_table(table, m_pDB,
"SELECT * FROM "ABRT_TABLE" "
"WHERE "COL_UUID" = '%s';",
pUUID
);
}
else
{
get_table(table, m_pDB,
"SELECT * FROM "ABRT_TABLE" "
"WHERE "COL_UUID" = '%s' "
"AND ("COL_UID" = '%s' OR "COL_UID" = '-1');",
pUUID, pUID
);
}
if (table.size() == 0)
{
return database_row_t();
}
return table[0];
}
void CSQLite3::SetSettings(const map_plugin_settings_t& pSettings)
{
m_pSettings = pSettings;
map_plugin_settings_t::const_iterator end = pSettings.end();
map_plugin_settings_t::const_iterator it;
it = pSettings.find("DBPath");
if (it != end)
{
m_sDBPath = it->second;
}
}
//ok to delete?
//const map_plugin_settings_t& CSQLite3::GetSettings()
//{
// m_pSettings["DBPath"] = m_sDBPath;
//
// return m_pSettings;
//}
PLUGIN_INFO(DATABASE,
CSQLite3,
"SQLite3",
"0.0.2",
"Keeps SQLite3 database about all crashes",
"zprikryl@redhat.com,jmoskovc@redhat.com",
"https://fedorahosted.org/abrt/wiki",
"");
|