| 1 |
/* |
| 2 |
* Copyright (c) 2013 .SE (The Internet Infrastructure Foundation) |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
softhsm2-dump-db.cpp |
| 29 |
|
| 30 |
This program can be used for dumping SoftHSM v2 database. |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <config.h> |
| 34 |
|
| 35 |
#include <sched.h> |
| 36 |
#include <sqlite3.h> |
| 37 |
#include <string.h> |
| 38 |
|
| 39 |
#include "common.h" |
| 40 |
|
| 41 |
// Attribute types in database arrays |
| 42 |
enum AttributeKind |
| 43 |
{ |
| 44 |
akUnknown, |
| 45 |
akBoolean, |
| 46 |
akInteger, |
| 47 |
akBinary, |
| 48 |
akArray |
| 49 |
}; |
| 50 |
|
| 51 |
// Attribute specialization |
| 52 |
typedef AttributeTK<CK_ATTRIBUTE_TYPE, AttributeKind, unsigned long> Attribute; |
| 53 |
|
| 54 |
template<> |
| 55 |
bool Attribute::isBoolean() const |
| 56 |
{ |
| 57 |
return kind == akBoolean; |
| 58 |
} |
| 59 |
|
| 60 |
template<> |
| 61 |
bool Attribute::isInteger() const |
| 62 |
{ |
| 63 |
return kind == akInteger; |
| 64 |
} |
| 65 |
|
| 66 |
template<> |
| 67 |
bool Attribute::isBinary() const |
| 68 |
{ |
| 69 |
return kind == akBinary; |
| 70 |
} |
| 71 |
|
| 72 |
template<> |
| 73 |
bool Attribute::isMechSet() const |
| 74 |
{ |
| 75 |
// Mechanism sets are stored as binary in the database |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
template<> |
| 80 |
void Attribute::dumpType() const |
| 81 |
{ |
| 82 |
if (sizeof(type) == 4) |
| 83 |
{ |
| 84 |
dumpU32((uint32_t)type, true); |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
dumpULong(type, true); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
template<> |
| 93 |
void Attribute::dumpKind() const |
| 94 |
{ |
| 95 |
dumpU32((uint32_t) kind, true); |
| 96 |
} |
| 97 |
|
| 98 |
template<> |
| 99 |
void Attribute::dumpBoolValue() const |
| 100 |
{ |
| 101 |
dumpBool1(boolValue, true); |
| 102 |
} |
| 103 |
|
| 104 |
template<> |
| 105 |
void Attribute::dumpULongValue(unsigned long value) const |
| 106 |
{ |
| 107 |
if (sizeof(unsigned long) == 4) |
| 108 |
{ |
| 109 |
dumpU32(value, true); |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
dumpULong(value, true); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// dumpArray specialization |
| 118 |
typedef std::vector<Attribute> va_type; |
| 119 |
|
| 120 |
void dumpArray(const va_type& value) |
| 121 |
{ |
| 122 |
for (va_type::const_iterator attr = value.begin(); attr != value.end(); ++attr) |
| 123 |
attr->dump(); |
| 124 |
} |
| 125 |
|
| 126 |
// Get a boolean (in fact unsigned 8 bit long) value |
| 127 |
bool getBool(sqlite3* db, long long oid, long long id, uint64_t& type, uint8_t& value) |
| 128 |
{ |
| 129 |
int rv; |
| 130 |
sqlite3_stmt* sql = NULL; |
| 131 |
std::string command = "select type,value from attribute_boolean where object_id=? and id=?;"; |
| 132 |
|
| 133 |
value = 0; |
| 134 |
|
| 135 |
rv = sqlite3_prepare_v2(db, command.c_str(), -1, &sql, NULL); |
| 136 |
if (rv != SQLITE_OK) |
| 137 |
{ |
| 138 |
fprintf(stderr, |
| 139 |
"can't find boolean attribute id=%lld object=%lld: %d(%s)\n", |
| 140 |
id, oid, rv, sqlite3_errmsg(db)); |
| 141 |
sqlite3_finalize(sql); |
| 142 |
return false; |
| 143 |
} |
| 144 |
rv = sqlite3_bind_int64(sql, 1, oid); |
| 145 |
if (rv != SQLITE_OK) |
| 146 |
{ |
| 147 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 148 |
rv, sqlite3_errmsg(db)); |
| 149 |
sqlite3_finalize(sql); |
| 150 |
return false; |
| 151 |
} |
| 152 |
sqlite3_bind_int64(sql, 2, id); |
| 153 |
if (rv != SQLITE_OK) |
| 154 |
{ |
| 155 |
fprintf(stderr, "can't bind the attribute id: %d(%s)\n", |
| 156 |
rv, sqlite3_errmsg(db)); |
| 157 |
sqlite3_finalize(sql); |
| 158 |
return false; |
| 159 |
} |
| 160 |
while ((rv = sqlite3_step(sql)) == SQLITE_BUSY) |
| 161 |
{ |
| 162 |
sched_yield(); |
| 163 |
} |
| 164 |
if (rv != SQLITE_ROW) |
| 165 |
{ |
| 166 |
fprintf(stderr, |
| 167 |
"can't read boolean attribute id=%lld object=%lld: %d(%s)\n", |
| 168 |
id, oid, rv, sqlite3_errmsg(db)); |
| 169 |
sqlite3_finalize(sql); |
| 170 |
return false; |
| 171 |
} |
| 172 |
type = sqlite3_column_int64(sql, 0); |
| 173 |
value = sqlite3_column_int(sql, 1); |
| 174 |
sqlite3_finalize(sql); |
| 175 |
|
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
// Get an unsigned 64 bit long value |
| 180 |
bool getULong(sqlite3* db, long long oid, long long id, uint64_t& type, uint64_t& value) |
| 181 |
{ |
| 182 |
int rv; |
| 183 |
sqlite3_stmt* sql = NULL; |
| 184 |
std::string command = "select type,value from attribute_integer where object_id=? and id=?;"; |
| 185 |
|
| 186 |
value = 0ULL; |
| 187 |
|
| 188 |
rv = sqlite3_prepare_v2(db, command.c_str(), -1, &sql, NULL); |
| 189 |
if (rv != SQLITE_OK) |
| 190 |
{ |
| 191 |
fprintf(stderr, |
| 192 |
"can't find integer attribute id=%lld object=%lld: %d(%s)\n", |
| 193 |
id, oid, rv, sqlite3_errmsg(db)); |
| 194 |
sqlite3_finalize(sql); |
| 195 |
return false; |
| 196 |
} |
| 197 |
rv = sqlite3_bind_int64(sql, 1, oid); |
| 198 |
if (rv != SQLITE_OK) |
| 199 |
{ |
| 200 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 201 |
rv, sqlite3_errmsg(db)); |
| 202 |
sqlite3_finalize(sql); |
| 203 |
return false; |
| 204 |
} |
| 205 |
sqlite3_bind_int64(sql, 2, id); |
| 206 |
if (rv != SQLITE_OK) |
| 207 |
{ |
| 208 |
fprintf(stderr, "can't bind the attribute id: %d(%s)\n", |
| 209 |
rv, sqlite3_errmsg(db)); |
| 210 |
sqlite3_finalize(sql); |
| 211 |
return false; |
| 212 |
} |
| 213 |
while ((rv = sqlite3_step(sql)) == SQLITE_BUSY) |
| 214 |
{ |
| 215 |
sched_yield(); |
| 216 |
} |
| 217 |
if (rv != SQLITE_ROW) |
| 218 |
{ |
| 219 |
fprintf(stderr, |
| 220 |
"can't read integer attribute id=%lld object=%lld: %d(%s)\n", |
| 221 |
id, oid, rv, sqlite3_errmsg(db)); |
| 222 |
sqlite3_finalize(sql); |
| 223 |
return false; |
| 224 |
} |
| 225 |
type = sqlite3_column_int64(sql, 0); |
| 226 |
value = sqlite3_column_int64(sql, 1); |
| 227 |
sqlite3_finalize(sql); |
| 228 |
|
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
// Get a byte string (aka uint8_t vector) value |
| 233 |
bool getBytes(sqlite3* db, long long oid, long long id, uint64_t& type, std::vector<uint8_t>& value) |
| 234 |
{ |
| 235 |
int rv; |
| 236 |
sqlite3_stmt* sql = NULL; |
| 237 |
std::string command = "select type,value from attribute_binary where object_id=? and id=?;"; |
| 238 |
size_t len; |
| 239 |
const uint8_t* val; |
| 240 |
|
| 241 |
value.clear(); |
| 242 |
|
| 243 |
rv = sqlite3_prepare_v2(db, command.c_str(), -1, &sql, NULL); |
| 244 |
if (rv != SQLITE_OK) |
| 245 |
{ |
| 246 |
fprintf(stderr, |
| 247 |
"can't find binary attribute id=%lld object=%lld: %d(%s)\n", |
| 248 |
id, oid, rv, sqlite3_errmsg(db)); |
| 249 |
sqlite3_finalize(sql); |
| 250 |
return false; |
| 251 |
} |
| 252 |
rv = sqlite3_bind_int64(sql, 1, oid); |
| 253 |
if (rv != SQLITE_OK) |
| 254 |
{ |
| 255 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 256 |
rv, sqlite3_errmsg(db)); |
| 257 |
sqlite3_finalize(sql); |
| 258 |
return false; |
| 259 |
} |
| 260 |
sqlite3_bind_int64(sql, 2, id); |
| 261 |
if (rv != SQLITE_OK) |
| 262 |
{ |
| 263 |
fprintf(stderr, "can't bind the attribute id: %d(%s)\n", |
| 264 |
rv, sqlite3_errmsg(db)); |
| 265 |
sqlite3_finalize(sql); |
| 266 |
return false; |
| 267 |
} |
| 268 |
while ((rv = sqlite3_step(sql)) == SQLITE_BUSY) |
| 269 |
{ |
| 270 |
sched_yield(); |
| 271 |
} |
| 272 |
if (rv != SQLITE_ROW) |
| 273 |
{ |
| 274 |
fprintf(stderr, |
| 275 |
"can't read binary attribute id=%lld object=%lld: %d(%s)\n", |
| 276 |
id, oid, rv, sqlite3_errmsg(db)); |
| 277 |
sqlite3_finalize(sql); |
| 278 |
return false; |
| 279 |
} |
| 280 |
type = sqlite3_column_int64(sql, 0); |
| 281 |
len = sqlite3_column_bytes(sql, 1); |
| 282 |
val = (const uint8_t*) sqlite3_column_blob(sql, 1); |
| 283 |
for (size_t i = 0; i < len; ++i) |
| 284 |
{ |
| 285 |
value.push_back(val[i]); |
| 286 |
} |
| 287 |
sqlite3_finalize(sql); |
| 288 |
|
| 289 |
return true; |
| 290 |
} |
| 291 |
|
| 292 |
// Get an array (aka Attribute vector) value |
| 293 |
bool getArray(sqlite3* db, long long oid, long long id, uint64_t& type, std::vector<Attribute>& value) |
| 294 |
{ |
| 295 |
int rv; |
| 296 |
sqlite3_stmt* sql = NULL; |
| 297 |
std::string command = "select type,value from attribute_array where object_id=? and id=?;"; |
| 298 |
size_t len; |
| 299 |
const uint8_t* val; |
| 300 |
|
| 301 |
value.clear(); |
| 302 |
|
| 303 |
rv = sqlite3_prepare_v2(db, command.c_str(), -1, &sql, NULL); |
| 304 |
if (rv != SQLITE_OK) |
| 305 |
{ |
| 306 |
fprintf(stderr, |
| 307 |
"can't find array attribute id=%lld object=%lld: %d(%s)\n", |
| 308 |
id, oid, rv, sqlite3_errmsg(db)); |
| 309 |
sqlite3_finalize(sql); |
| 310 |
return false; |
| 311 |
} |
| 312 |
rv = sqlite3_bind_int64(sql, 1, oid); |
| 313 |
if (rv != SQLITE_OK) |
| 314 |
{ |
| 315 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 316 |
rv, sqlite3_errmsg(db)); |
| 317 |
sqlite3_finalize(sql); |
| 318 |
return false; |
| 319 |
} |
| 320 |
sqlite3_bind_int64(sql, 2, id); |
| 321 |
if (rv != SQLITE_OK) |
| 322 |
{ |
| 323 |
fprintf(stderr, "can't bind the attribute id: %d(%s)\n", |
| 324 |
rv, sqlite3_errmsg(db)); |
| 325 |
sqlite3_finalize(sql); |
| 326 |
return false; |
| 327 |
} |
| 328 |
while ((rv = sqlite3_step(sql)) == SQLITE_BUSY) |
| 329 |
{ |
| 330 |
sched_yield(); |
| 331 |
} |
| 332 |
if (rv != SQLITE_ROW) |
| 333 |
{ |
| 334 |
fprintf(stderr, |
| 335 |
"can't read array attribute id=%lld object=%lld: %d(%s)\n", |
| 336 |
id, oid, rv, sqlite3_errmsg(db)); |
| 337 |
sqlite3_finalize(sql); |
| 338 |
return false; |
| 339 |
} |
| 340 |
type = sqlite3_column_int64(sql, 0); |
| 341 |
len = sqlite3_column_bytes(sql, 1); |
| 342 |
val = (const uint8_t*) sqlite3_column_blob(sql, 1); |
| 343 |
|
| 344 |
// CK_ATTRIBUTE_TYPE type, AttributeKind kind |
| 345 |
// bool -> int, integer -> unsigned long, binary -> unsigned long + vector |
| 346 |
|
| 347 |
for (size_t pos = 0; pos < len; ) |
| 348 |
{ |
| 349 |
// finished? |
| 350 |
if (pos == len) break; |
| 351 |
|
| 352 |
Attribute attr; |
| 353 |
|
| 354 |
if (pos + sizeof(attr.type) > len) |
| 355 |
{ |
| 356 |
fprintf(stderr, "overflow array item type\n"); |
| 357 |
sqlite3_finalize(sql); |
| 358 |
return false; |
| 359 |
} |
| 360 |
memcpy(&attr.type, val + pos, sizeof(attr.type)); |
| 361 |
pos += sizeof(attr.type); |
| 362 |
|
| 363 |
if (pos + sizeof(attr.kind) > len) |
| 364 |
{ |
| 365 |
fprintf(stderr, "overflow array item kind\n"); |
| 366 |
sqlite3_finalize(sql); |
| 367 |
return false; |
| 368 |
} |
| 369 |
memcpy(&attr.kind, val + pos, sizeof(attr.kind)); |
| 370 |
pos += sizeof(attr.kind); |
| 371 |
|
| 372 |
if (attr.kind == akBoolean) |
| 373 |
{ |
| 374 |
if (pos + sizeof(attr.boolValue) > len) |
| 375 |
{ |
| 376 |
fprintf(stderr, "overflow array boolean item\n"); |
| 377 |
sqlite3_finalize(sql); |
| 378 |
return false; |
| 379 |
} |
| 380 |
memcpy(&attr.boolValue, val + pos, sizeof(attr.boolValue)); |
| 381 |
pos += sizeof(attr.boolValue); |
| 382 |
} |
| 383 |
else if (attr.kind == akInteger) |
| 384 |
{ |
| 385 |
if (pos + sizeof(attr.ulongValue) > len) |
| 386 |
{ |
| 387 |
fprintf(stderr, "overflow array integer item\n"); |
| 388 |
sqlite3_finalize(sql); |
| 389 |
return false; |
| 390 |
} |
| 391 |
memcpy(&attr.ulongValue, val + pos, sizeof(attr.ulongValue)); |
| 392 |
pos += sizeof(attr.ulongValue); |
| 393 |
} |
| 394 |
else if (attr.kind == akBinary) |
| 395 |
{ |
| 396 |
unsigned long size; |
| 397 |
if (pos + sizeof(size) > len) |
| 398 |
{ |
| 399 |
fprintf(stderr, "overflow array binary item\n"); |
| 400 |
sqlite3_finalize(sql); |
| 401 |
return false; |
| 402 |
} |
| 403 |
memcpy(&size, val + pos, sizeof(size)); |
| 404 |
pos += sizeof(size); |
| 405 |
|
| 406 |
if (pos + size > len) |
| 407 |
{ |
| 408 |
fprintf(stderr, "overflow array binary item\n"); |
| 409 |
sqlite3_finalize(sql); |
| 410 |
return false; |
| 411 |
} |
| 412 |
attr.bytestrValue.resize(size); |
| 413 |
for (unsigned long i = 0; i < size; ++i) |
| 414 |
{ |
| 415 |
attr.bytestrValue[i] = val[pos + i]; |
| 416 |
} |
| 417 |
pos += size; |
| 418 |
} |
| 419 |
else |
| 420 |
{ |
| 421 |
fprintf(stderr, "unknown array item\n"); |
| 422 |
sqlite3_finalize(sql); |
| 423 |
return false; |
| 424 |
} |
| 425 |
|
| 426 |
value.push_back(attr); |
| 427 |
} |
| 428 |
sqlite3_finalize(sql); |
| 429 |
|
| 430 |
return true; |
| 431 |
} |
| 432 |
|
| 433 |
// Dump boolean attributes of an object |
| 434 |
void dump_booleans(sqlite3* db, long long oid) |
| 435 |
{ |
| 436 |
int rv; |
| 437 |
unsigned long count; |
| 438 |
sqlite3_stmt* sqlcnt = NULL; |
| 439 |
sqlite3_stmt* sqlid = NULL; |
| 440 |
std::string commandcnt = "select count(id) from attribute_boolean where object_id=?;"; |
| 441 |
std::string commandid = "select id from attribute_boolean where object_id=?;"; |
| 442 |
rv = sqlite3_prepare_v2(db, commandcnt.c_str(), -1, &sqlcnt, NULL); |
| 443 |
if (rv != SQLITE_OK) |
| 444 |
{ |
| 445 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 446 |
rv, sqlite3_errmsg(db)); |
| 447 |
sqlite3_finalize(sqlcnt); |
| 448 |
return; |
| 449 |
} |
| 450 |
rv = sqlite3_bind_int64(sqlcnt, 1, oid); |
| 451 |
if (rv != SQLITE_OK) |
| 452 |
{ |
| 453 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 454 |
rv, sqlite3_errmsg(db)); |
| 455 |
sqlite3_finalize(sqlcnt); |
| 456 |
return; |
| 457 |
} |
| 458 |
while ((rv = sqlite3_step(sqlcnt)) == SQLITE_BUSY) |
| 459 |
{ |
| 460 |
sched_yield(); |
| 461 |
} |
| 462 |
if (rv != SQLITE_ROW) |
| 463 |
{ |
| 464 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 465 |
rv, sqlite3_errmsg(db)); |
| 466 |
sqlite3_finalize(sqlcnt); |
| 467 |
return; |
| 468 |
} |
| 469 |
count = sqlite3_column_int(sqlcnt, 0); |
| 470 |
sqlite3_finalize(sqlcnt); |
| 471 |
if (count == 0) |
| 472 |
return; |
| 473 |
|
| 474 |
printf("%lu boolean attributes for object %lld\n", count, oid); |
| 475 |
|
| 476 |
rv = sqlite3_prepare_v2(db, commandid.c_str(), -1, &sqlid, NULL); |
| 477 |
if (rv != SQLITE_OK) |
| 478 |
{ |
| 479 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 480 |
rv, sqlite3_errmsg(db)); |
| 481 |
sqlite3_finalize(sqlid); |
| 482 |
return; |
| 483 |
} |
| 484 |
rv = sqlite3_bind_int64(sqlid, 1, oid); |
| 485 |
if (rv != SQLITE_OK) |
| 486 |
{ |
| 487 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 488 |
rv, sqlite3_errmsg(db)); |
| 489 |
sqlite3_finalize(sqlid); |
| 490 |
return; |
| 491 |
} |
| 492 |
while (count-- > 0) { |
| 493 |
while ((rv = sqlite3_step(sqlid)) == SQLITE_BUSY) |
| 494 |
{ |
| 495 |
sched_yield(); |
| 496 |
} |
| 497 |
if (rv != SQLITE_ROW) |
| 498 |
{ |
| 499 |
if (rv != SQLITE_DONE) |
| 500 |
{ |
| 501 |
fprintf(stderr, |
| 502 |
"can't get next object id: %d(%s)\n", |
| 503 |
rv, sqlite3_errmsg(db)); |
| 504 |
} |
| 505 |
sqlite3_finalize(sqlid); |
| 506 |
return; |
| 507 |
} |
| 508 |
long long id = sqlite3_column_int64(sqlid, 0); |
| 509 |
|
| 510 |
uint64_t type; |
| 511 |
uint8_t value; |
| 512 |
if (!getBool(db, oid, id, type, value)) |
| 513 |
{ |
| 514 |
return; |
| 515 |
} |
| 516 |
dumpULong(type); |
| 517 |
if ((uint64_t)((uint32_t)type) != type) |
| 518 |
{ |
| 519 |
printf("overflow attribute type\n"); |
| 520 |
} |
| 521 |
else |
| 522 |
{ |
| 523 |
dumpCKA((unsigned long) type, 48); |
| 524 |
printf("\n"); |
| 525 |
} |
| 526 |
|
| 527 |
dumpBool1(value); |
| 528 |
printf("\n"); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
// Dump integer attributes of an object |
| 533 |
void dump_integers(sqlite3* db, long long oid) |
| 534 |
{ |
| 535 |
int rv; |
| 536 |
unsigned long count; |
| 537 |
sqlite3_stmt* sqlcnt = NULL; |
| 538 |
sqlite3_stmt* sqlid = NULL; |
| 539 |
std::string commandcnt = "select count(id) from attribute_integer where object_id=?;"; |
| 540 |
std::string commandid = "select id from attribute_integer where object_id=?;"; |
| 541 |
rv = sqlite3_prepare_v2(db, commandcnt.c_str(), -1, &sqlcnt, NULL); |
| 542 |
if (rv != SQLITE_OK) |
| 543 |
{ |
| 544 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 545 |
rv, sqlite3_errmsg(db)); |
| 546 |
sqlite3_finalize(sqlcnt); |
| 547 |
return; |
| 548 |
} |
| 549 |
rv = sqlite3_bind_int64(sqlcnt, 1, oid); |
| 550 |
if (rv != SQLITE_OK) |
| 551 |
{ |
| 552 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 553 |
rv, sqlite3_errmsg(db)); |
| 554 |
sqlite3_finalize(sqlcnt); |
| 555 |
return; |
| 556 |
} |
| 557 |
while ((rv = sqlite3_step(sqlcnt)) == SQLITE_BUSY) |
| 558 |
{ |
| 559 |
sched_yield(); |
| 560 |
} |
| 561 |
if (rv != SQLITE_ROW) |
| 562 |
{ |
| 563 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 564 |
rv, sqlite3_errmsg(db)); |
| 565 |
sqlite3_finalize(sqlcnt); |
| 566 |
return; |
| 567 |
} |
| 568 |
count = sqlite3_column_int(sqlcnt, 0); |
| 569 |
sqlite3_finalize(sqlcnt); |
| 570 |
if (count == 0) |
| 571 |
return; |
| 572 |
|
| 573 |
printf("%lu integer attributes for object %lld\n", count, oid); |
| 574 |
|
| 575 |
rv = sqlite3_prepare_v2(db, commandid.c_str(), -1, &sqlid, NULL); |
| 576 |
if (rv != SQLITE_OK) |
| 577 |
{ |
| 578 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 579 |
rv, sqlite3_errmsg(db)); |
| 580 |
sqlite3_finalize(sqlid); |
| 581 |
return; |
| 582 |
} |
| 583 |
rv = sqlite3_bind_int64(sqlid, 1, oid); |
| 584 |
if (rv != SQLITE_OK) |
| 585 |
{ |
| 586 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 587 |
rv, sqlite3_errmsg(db)); |
| 588 |
sqlite3_finalize(sqlid); |
| 589 |
return; |
| 590 |
} |
| 591 |
while (count-- > 0) { |
| 592 |
while ((rv = sqlite3_step(sqlid)) == SQLITE_BUSY) |
| 593 |
{ |
| 594 |
sched_yield(); |
| 595 |
} |
| 596 |
if (rv != SQLITE_ROW) |
| 597 |
{ |
| 598 |
if (rv != SQLITE_DONE) |
| 599 |
{ |
| 600 |
fprintf(stderr, |
| 601 |
"can't get next object id: %d(%s)\n", |
| 602 |
rv, sqlite3_errmsg(db)); |
| 603 |
} |
| 604 |
sqlite3_finalize(sqlid); |
| 605 |
return; |
| 606 |
} |
| 607 |
long long id = sqlite3_column_int64(sqlid, 0); |
| 608 |
|
| 609 |
uint64_t type; |
| 610 |
uint64_t value; |
| 611 |
if (!getULong(db, oid, id, type, value)) |
| 612 |
{ |
| 613 |
return; |
| 614 |
} |
| 615 |
dumpULong(type); |
| 616 |
if ((uint64_t)((uint32_t)type) != type) |
| 617 |
{ |
| 618 |
printf("overflow attribute type\n"); |
| 619 |
} |
| 620 |
else |
| 621 |
{ |
| 622 |
dumpCKA((unsigned long) type, 48); |
| 623 |
printf("\n"); |
| 624 |
} |
| 625 |
dumpULong(value); |
| 626 |
dumpCKx(type, value, 48); |
| 627 |
printf("\n"); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
// Dump binary attributes of an object |
| 632 |
void dump_binaries(sqlite3* db, long long oid) |
| 633 |
{ |
| 634 |
int rv; |
| 635 |
unsigned long count; |
| 636 |
sqlite3_stmt* sqlcnt = NULL; |
| 637 |
sqlite3_stmt* sqlid = NULL; |
| 638 |
std::string commandcnt = "select count(id) from attribute_binary where object_id=?;"; |
| 639 |
std::string commandid = "select id from attribute_binary where object_id=?;"; |
| 640 |
rv = sqlite3_prepare_v2(db, commandcnt.c_str(), -1, &sqlcnt, NULL); |
| 641 |
if (rv != SQLITE_OK) |
| 642 |
{ |
| 643 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 644 |
rv, sqlite3_errmsg(db)); |
| 645 |
sqlite3_finalize(sqlcnt); |
| 646 |
return; |
| 647 |
} |
| 648 |
rv = sqlite3_bind_int64(sqlcnt, 1, oid); |
| 649 |
if (rv != SQLITE_OK) |
| 650 |
{ |
| 651 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 652 |
rv, sqlite3_errmsg(db)); |
| 653 |
sqlite3_finalize(sqlcnt); |
| 654 |
return; |
| 655 |
} |
| 656 |
while ((rv = sqlite3_step(sqlcnt)) == SQLITE_BUSY) |
| 657 |
{ |
| 658 |
sched_yield(); |
| 659 |
} |
| 660 |
if (rv != SQLITE_ROW) |
| 661 |
{ |
| 662 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 663 |
rv, sqlite3_errmsg(db)); |
| 664 |
sqlite3_finalize(sqlcnt); |
| 665 |
return; |
| 666 |
} |
| 667 |
count = sqlite3_column_int(sqlcnt, 0); |
| 668 |
sqlite3_finalize(sqlcnt); |
| 669 |
if (count == 0) |
| 670 |
return; |
| 671 |
|
| 672 |
printf("%lu binary attributes for object %lld\n", count, oid); |
| 673 |
|
| 674 |
rv = sqlite3_prepare_v2(db, commandid.c_str(), -1, &sqlid, NULL); |
| 675 |
if (rv != SQLITE_OK) |
| 676 |
{ |
| 677 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 678 |
rv, sqlite3_errmsg(db)); |
| 679 |
sqlite3_finalize(sqlid); |
| 680 |
return; |
| 681 |
} |
| 682 |
rv = sqlite3_bind_int64(sqlid, 1, oid); |
| 683 |
if (rv != SQLITE_OK) |
| 684 |
{ |
| 685 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 686 |
rv, sqlite3_errmsg(db)); |
| 687 |
sqlite3_finalize(sqlid); |
| 688 |
return; |
| 689 |
} |
| 690 |
while (count-- > 0) { |
| 691 |
while ((rv = sqlite3_step(sqlid)) == SQLITE_BUSY) |
| 692 |
{ |
| 693 |
sched_yield(); |
| 694 |
} |
| 695 |
if (rv != SQLITE_ROW) |
| 696 |
{ |
| 697 |
if (rv != SQLITE_DONE) |
| 698 |
{ |
| 699 |
fprintf(stderr, |
| 700 |
"can't get next object id: %d(%s)\n", |
| 701 |
rv, sqlite3_errmsg(db)); |
| 702 |
} |
| 703 |
sqlite3_finalize(sqlid); |
| 704 |
return; |
| 705 |
} |
| 706 |
long long id = sqlite3_column_int64(sqlid, 0); |
| 707 |
|
| 708 |
uint64_t type; |
| 709 |
std::vector<uint8_t> value; |
| 710 |
if (!getBytes(db, oid, id, type, value)) |
| 711 |
{ |
| 712 |
return; |
| 713 |
} |
| 714 |
dumpULong(type); |
| 715 |
if ((uint64_t)((uint32_t)type) != type) |
| 716 |
{ |
| 717 |
printf("overflow attribute type\n"); |
| 718 |
} |
| 719 |
else |
| 720 |
{ |
| 721 |
dumpCKA((unsigned long) type, 48); |
| 722 |
printf("\n"); |
| 723 |
} |
| 724 |
dumpULong((uint64_t) value.size()); |
| 725 |
printf("(length %lu)\n", (unsigned long) value.size()); |
| 726 |
dumpBytes(value); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
// Dump array attributes of an object |
| 731 |
void dump_arrays(sqlite3* db, long long oid) |
| 732 |
{ |
| 733 |
int rv; |
| 734 |
unsigned long count; |
| 735 |
sqlite3_stmt* sqlcnt = NULL; |
| 736 |
sqlite3_stmt* sqlid = NULL; |
| 737 |
std::string commandcnt = "select count(id) from attribute_array where object_id=?;"; |
| 738 |
std::string commandid = "select id from attribute_array where object_id=?;"; |
| 739 |
rv = sqlite3_prepare_v2(db, commandcnt.c_str(), -1, &sqlcnt, NULL); |
| 740 |
if (rv != SQLITE_OK) |
| 741 |
{ |
| 742 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 743 |
rv, sqlite3_errmsg(db)); |
| 744 |
sqlite3_finalize(sqlcnt); |
| 745 |
return; |
| 746 |
} |
| 747 |
rv = sqlite3_bind_int64(sqlcnt, 1, oid); |
| 748 |
if (rv != SQLITE_OK) |
| 749 |
{ |
| 750 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 751 |
rv, sqlite3_errmsg(db)); |
| 752 |
sqlite3_finalize(sqlcnt); |
| 753 |
return; |
| 754 |
} |
| 755 |
while ((rv = sqlite3_step(sqlcnt)) == SQLITE_BUSY) |
| 756 |
{ |
| 757 |
sched_yield(); |
| 758 |
} |
| 759 |
if (rv != SQLITE_ROW) |
| 760 |
{ |
| 761 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 762 |
rv, sqlite3_errmsg(db)); |
| 763 |
sqlite3_finalize(sqlcnt); |
| 764 |
return; |
| 765 |
} |
| 766 |
count = sqlite3_column_int(sqlcnt, 0); |
| 767 |
sqlite3_finalize(sqlcnt); |
| 768 |
if (count == 0) |
| 769 |
return; |
| 770 |
|
| 771 |
printf("%lu array attributes for object %lld\n", count, oid); |
| 772 |
|
| 773 |
rv = sqlite3_prepare_v2(db, commandid.c_str(), -1, &sqlid, NULL); |
| 774 |
if (rv != SQLITE_OK) |
| 775 |
{ |
| 776 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 777 |
rv, sqlite3_errmsg(db)); |
| 778 |
sqlite3_finalize(sqlid); |
| 779 |
return; |
| 780 |
} |
| 781 |
rv = sqlite3_bind_int64(sqlid, 1, oid); |
| 782 |
if (rv != SQLITE_OK) |
| 783 |
{ |
| 784 |
fprintf(stderr, "can't bind the object id: %d(%s)\n", |
| 785 |
rv, sqlite3_errmsg(db)); |
| 786 |
sqlite3_finalize(sqlid); |
| 787 |
return; |
| 788 |
} |
| 789 |
while (count-- > 0) { |
| 790 |
while ((rv = sqlite3_step(sqlid)) == SQLITE_BUSY) |
| 791 |
{ |
| 792 |
sched_yield(); |
| 793 |
} |
| 794 |
if (rv != SQLITE_ROW) |
| 795 |
{ |
| 796 |
if (rv != SQLITE_DONE) |
| 797 |
{ |
| 798 |
fprintf(stderr, |
| 799 |
"can't get next object id: %d(%s)\n", |
| 800 |
rv, sqlite3_errmsg(db)); |
| 801 |
} |
| 802 |
sqlite3_finalize(sqlid); |
| 803 |
return; |
| 804 |
} |
| 805 |
long long id = sqlite3_column_int64(sqlid, 0); |
| 806 |
|
| 807 |
uint64_t type; |
| 808 |
std::vector<Attribute> value; |
| 809 |
if (!getArray(db, oid, id, type, value)) |
| 810 |
{ |
| 811 |
return; |
| 812 |
} |
| 813 |
dumpULong(type); |
| 814 |
if ((uint64_t)((uint32_t)type) != type) |
| 815 |
{ |
| 816 |
printf("overflow attribute type\n"); |
| 817 |
} |
| 818 |
else |
| 819 |
{ |
| 820 |
dumpCKA((unsigned long) type, 48); |
| 821 |
printf("\n"); |
| 822 |
} |
| 823 |
dumpULong((uint64_t) value.size()); |
| 824 |
printf("(length %lu)\n", (unsigned long) value.size()); |
| 825 |
dumpArray(value); |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
// Dump an object |
| 830 |
void dump_object(sqlite3* db, long long oid) |
| 831 |
{ |
| 832 |
printf("dump object id=%lld\n", oid); |
| 833 |
dump_booleans(db, oid); |
| 834 |
dump_integers(db, oid); |
| 835 |
dump_binaries(db, oid); |
| 836 |
dump_arrays(db, oid); |
| 837 |
} |
| 838 |
|
| 839 |
// Core function |
| 840 |
void dump(sqlite3* db) |
| 841 |
{ |
| 842 |
int rv; |
| 843 |
unsigned long count; |
| 844 |
sqlite3_stmt* sqlcnt = NULL; |
| 845 |
sqlite3_stmt* sqlid = NULL; |
| 846 |
std::string commandcnt = "select count(id) from object;"; |
| 847 |
std::string commandid = "select id from object;"; |
| 848 |
|
| 849 |
rv = sqlite3_prepare_v2(db, commandcnt.c_str(), -1, &sqlcnt, NULL); |
| 850 |
if (rv != SQLITE_OK) |
| 851 |
{ |
| 852 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 853 |
rv, sqlite3_errmsg(db)); |
| 854 |
sqlite3_finalize(sqlcnt); |
| 855 |
return; |
| 856 |
} |
| 857 |
while ((rv = sqlite3_step(sqlcnt)) == SQLITE_BUSY) |
| 858 |
{ |
| 859 |
sched_yield(); |
| 860 |
} |
| 861 |
if (rv != SQLITE_ROW) |
| 862 |
{ |
| 863 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 864 |
rv, sqlite3_errmsg(db)); |
| 865 |
sqlite3_finalize(sqlcnt); |
| 866 |
return; |
| 867 |
} |
| 868 |
count = sqlite3_column_int(sqlcnt, 0); |
| 869 |
sqlite3_finalize(sqlcnt); |
| 870 |
printf("%lu objects\n", count); |
| 871 |
|
| 872 |
rv = sqlite3_prepare_v2(db, commandid.c_str(), -1, &sqlid, NULL); |
| 873 |
if (rv != SQLITE_OK) |
| 874 |
{ |
| 875 |
fprintf(stderr, "can't count the object table: %d(%s)\n", |
| 876 |
rv, sqlite3_errmsg(db)); |
| 877 |
sqlite3_finalize(sqlid); |
| 878 |
return; |
| 879 |
} |
| 880 |
while (count-- > 0) { |
| 881 |
while ((rv = sqlite3_step(sqlid)) == SQLITE_BUSY) |
| 882 |
{ |
| 883 |
sched_yield(); |
| 884 |
} |
| 885 |
if (rv != SQLITE_ROW) |
| 886 |
{ |
| 887 |
if (rv != SQLITE_DONE) |
| 888 |
{ |
| 889 |
fprintf(stderr, |
| 890 |
"can't get next object id: %d(%s)\n", |
| 891 |
rv, sqlite3_errmsg(db)); |
| 892 |
} |
| 893 |
sqlite3_finalize(sqlid); |
| 894 |
return; |
| 895 |
} |
| 896 |
long long oid = sqlite3_column_int64(sqlid, 0); |
| 897 |
dump_object(db, oid); |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
// Display the usage |
| 902 |
void usage() |
| 903 |
{ |
| 904 |
printf("SoftHSM dump tool. From SoftHSM v2 database.\n"); |
| 905 |
printf("Usage: softhsm2-dump-db path\n"); |
| 906 |
} |
| 907 |
|
| 908 |
// Check the existence of a table |
| 909 |
void check_table_exist(sqlite3* db, std::string name) |
| 910 |
{ |
| 911 |
int rv; |
| 912 |
std::string command = "select count(id) from " + name + ";"; |
| 913 |
|
| 914 |
rv = sqlite3_exec(db, command.c_str(), NULL, NULL, NULL); |
| 915 |
if (rv != SQLITE_OK) |
| 916 |
{ |
| 917 |
fprintf(stderr, "can't find '%s' table\n", name.c_str()); |
| 918 |
sqlite3_close(db); |
| 919 |
exit(0); |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
// The main function |
| 924 |
int main(int argc, char* argv[]) |
| 925 |
{ |
| 926 |
int rv; |
| 927 |
sqlite3* db = NULL; |
| 928 |
|
| 929 |
if (argc != 2) |
| 930 |
{ |
| 931 |
usage(); |
| 932 |
exit(0); |
| 933 |
} |
| 934 |
|
| 935 |
rv = sqlite3_open_v2(argv[1], &db, SQLITE_OPEN_READONLY, NULL); |
| 936 |
if (rv != SQLITE_OK) |
| 937 |
{ |
| 938 |
if (db == NULL) |
| 939 |
{ |
| 940 |
fprintf(stderr, |
| 941 |
"can't open database file %s\n", |
| 942 |
argv[1]); |
| 943 |
} |
| 944 |
else |
| 945 |
{ |
| 946 |
fprintf(stderr, |
| 947 |
"can't open database file %s: %d(%s)\n", |
| 948 |
argv[1], |
| 949 |
rv, |
| 950 |
sqlite3_errmsg(db)); |
| 951 |
} |
| 952 |
sqlite3_close(db); |
| 953 |
exit(0); |
| 954 |
} |
| 955 |
|
| 956 |
// No user version to check |
| 957 |
|
| 958 |
check_table_exist(db, "object"); |
| 959 |
check_table_exist(db, "attribute_boolean"); |
| 960 |
check_table_exist(db, "attribute_integer"); |
| 961 |
check_table_exist(db, "attribute_binary"); |
| 962 |
check_table_exist(db, "attribute_array"); |
| 963 |
|
| 964 |
printf("Dump of object file \"%s\"\n", argv[1]); |
| 965 |
dump(db); |
| 966 |
sqlite3_close(db); |
| 967 |
exit(1); |
| 968 |
} |