1 /* 2 ** 2006 June 7 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file contains code used to dynamically load extensions into 13 ** the SQLite library. 14 ** 15 ** $Id: loadext.c,v 1.60 2009/06/03 01:24:54 drh Exp $ 16 */ 17 18 #ifndef SQLITE_CORE 19 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ 20 #endif 21 #include "sqlite3ext.h" 22 #include "sqliteInt.h" 23 #include <string.h> 24 25 #ifndef SQLITE_OMIT_LOAD_EXTENSION 26 27 /* 28 ** Some API routines are omitted when various features are 29 ** excluded from a build of SQLite. Substitute a NULL pointer 30 ** for any missing APIs. 31 */ 32 #ifndef SQLITE_ENABLE_COLUMN_METADATA 33 # define sqlite3_column_database_name 0 34 # define sqlite3_column_database_name16 0 35 # define sqlite3_column_table_name 0 36 # define sqlite3_column_table_name16 0 37 # define sqlite3_column_origin_name 0 38 # define sqlite3_column_origin_name16 0 39 # define sqlite3_table_column_metadata 0 40 #endif 41 42 #ifdef SQLITE_OMIT_AUTHORIZATION 43 # define sqlite3_set_authorizer 0 44 #endif 45 46 #ifdef SQLITE_OMIT_UTF16 47 # define sqlite3_bind_text16 0 48 # define sqlite3_collation_needed16 0 49 # define sqlite3_column_decltype16 0 50 # define sqlite3_column_name16 0 51 # define sqlite3_column_text16 0 52 # define sqlite3_complete16 0 53 # define sqlite3_create_collation16 0 54 # define sqlite3_create_function16 0 55 # define sqlite3_errmsg16 0 56 # define sqlite3_open16 0 57 # define sqlite3_prepare16 0 58 # define sqlite3_prepare16_v2 0 59 # define sqlite3_result_error16 0 60 # define sqlite3_result_text16 0 61 # define sqlite3_result_text16be 0 62 # define sqlite3_result_text16le 0 63 # define sqlite3_value_text16 0 64 # define sqlite3_value_text16be 0 65 # define sqlite3_value_text16le 0 66 # define sqlite3_column_database_name16 0 67 # define sqlite3_column_table_name16 0 68 # define sqlite3_column_origin_name16 0 69 #endif 70 71 #ifdef SQLITE_OMIT_COMPLETE 72 # define sqlite3_complete 0 73 # define sqlite3_complete16 0 74 #endif 75 76 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK 77 # define sqlite3_progress_handler 0 78 #endif 79 80 #ifdef SQLITE_OMIT_VIRTUALTABLE 81 # define sqlite3_create_module 0 82 # define sqlite3_create_module_v2 0 83 # define sqlite3_declare_vtab 0 84 #endif 85 86 #ifdef SQLITE_OMIT_SHARED_CACHE 87 # define sqlite3_enable_shared_cache 0 88 #endif 89 90 #ifdef SQLITE_OMIT_TRACE 91 # define sqlite3_profile 0 92 # define sqlite3_trace 0 93 #endif 94 95 #ifdef SQLITE_OMIT_GET_TABLE 96 # define sqlite3_free_table 0 97 # define sqlite3_get_table 0 98 #endif 99 100 #ifdef SQLITE_OMIT_INCRBLOB 101 #define sqlite3_bind_zeroblob 0 102 #define sqlite3_blob_bytes 0 103 #define sqlite3_blob_close 0 104 #define sqlite3_blob_open 0 105 #define sqlite3_blob_read 0 106 #define sqlite3_blob_write 0 107 #endif 108 109 /* 110 ** The following structure contains pointers to all SQLite API routines. 111 ** A pointer to this structure is passed into extensions when they are 112 ** loaded so that the extension can make calls back into the SQLite 113 ** library. 114 ** 115 ** When adding new APIs, add them to the bottom of this structure 116 ** in order to preserve backwards compatibility. 117 ** 118 ** Extensions that use newer APIs should first call the 119 ** sqlite3_libversion_number() to make sure that the API they 120 ** intend to use is supported by the library. Extensions should 121 ** also check to make sure that the pointer to the function is 122 ** not NULL before calling it. 123 */ 124 static const sqlite3_api_routines sqlite3Apis = { 125 sqlite3_aggregate_context, 126 #ifndef SQLITE_OMIT_DEPRECATED 127 sqlite3_aggregate_count, 128 #else 129 0, 130 #endif 131 sqlite3_bind_blob, 132 sqlite3_bind_double, 133 sqlite3_bind_int, 134 sqlite3_bind_int64, 135 sqlite3_bind_null, 136 sqlite3_bind_parameter_count, 137 sqlite3_bind_parameter_index, 138 sqlite3_bind_parameter_name, 139 sqlite3_bind_text, 140 sqlite3_bind_text16, 141 sqlite3_bind_value, 142 sqlite3_busy_handler, 143 sqlite3_busy_timeout, 144 sqlite3_changes, 145 sqlite3_close, 146 sqlite3_collation_needed, 147 sqlite3_collation_needed16, 148 sqlite3_column_blob, 149 sqlite3_column_bytes, 150 sqlite3_column_bytes16, 151 sqlite3_column_count, 152 sqlite3_column_database_name, 153 sqlite3_column_database_name16, 154 sqlite3_column_decltype, 155 sqlite3_column_decltype16, 156 sqlite3_column_double, 157 sqlite3_column_int, 158 sqlite3_column_int64, 159 sqlite3_column_name, 160 sqlite3_column_name16, 161 sqlite3_column_origin_name, 162 sqlite3_column_origin_name16, 163 sqlite3_column_table_name, 164 sqlite3_column_table_name16, 165 sqlite3_column_text, 166 sqlite3_column_text16, 167 sqlite3_column_type, 168 sqlite3_column_value, 169 sqlite3_commit_hook, 170 sqlite3_complete, 171 sqlite3_complete16, 172 sqlite3_create_collation, 173 sqlite3_create_collation16, 174 sqlite3_create_function, 175 sqlite3_create_function16, 176 sqlite3_create_module, 177 sqlite3_data_count, 178 sqlite3_db_handle, 179 sqlite3_declare_vtab, 180 sqlite3_enable_shared_cache, 181 sqlite3_errcode, 182 sqlite3_errmsg, 183 sqlite3_errmsg16, 184 sqlite3_exec, 185 #ifndef SQLITE_OMIT_DEPRECATED 186 sqlite3_expired, 187 #else 188 0, 189 #endif 190 sqlite3_finalize, 191 sqlite3_free, 192 sqlite3_free_table, 193 sqlite3_get_autocommit, 194 sqlite3_get_auxdata, 195 sqlite3_get_table, 196 0, /* Was sqlite3_global_recover(), but that function is deprecated */ 197 sqlite3_interrupt, 198 sqlite3_last_insert_rowid, 199 sqlite3_libversion, 200 sqlite3_libversion_number, 201 sqlite3_malloc, 202 sqlite3_mprintf, 203 sqlite3_open, 204 sqlite3_open16, 205 sqlite3_prepare, 206 sqlite3_prepare16, 207 sqlite3_profile, 208 sqlite3_progress_handler, 209 sqlite3_realloc, 210 sqlite3_reset, 211 sqlite3_result_blob, 212 sqlite3_result_double, 213 sqlite3_result_error, 214 sqlite3_result_error16, 215 sqlite3_result_int, 216 sqlite3_result_int64, 217 sqlite3_result_null, 218 sqlite3_result_text, 219 sqlite3_result_text16, 220 sqlite3_result_text16be, 221 sqlite3_result_text16le, 222 sqlite3_result_value, 223 sqlite3_rollback_hook, 224 sqlite3_set_authorizer, 225 sqlite3_set_auxdata, 226 sqlite3_snprintf, 227 sqlite3_step, 228 sqlite3_table_column_metadata, 229 #ifndef SQLITE_OMIT_DEPRECATED 230 sqlite3_thread_cleanup, 231 #else 232 0, 233 #endif 234 sqlite3_total_changes, 235 sqlite3_trace, 236 #ifndef SQLITE_OMIT_DEPRECATED 237 sqlite3_transfer_bindings, 238 #else 239 0, 240 #endif 241 sqlite3_update_hook, 242 sqlite3_user_data, 243 sqlite3_value_blob, 244 sqlite3_value_bytes, 245 sqlite3_value_bytes16, 246 sqlite3_value_double, 247 sqlite3_value_int, 248 sqlite3_value_int64, 249 sqlite3_value_numeric_type, 250 sqlite3_value_text, 251 sqlite3_value_text16, 252 sqlite3_value_text16be, 253 sqlite3_value_text16le, 254 sqlite3_value_type, 255 sqlite3_vmprintf, 256 /* 257 ** The original API set ends here. All extensions can call any 258 ** of the APIs above provided that the pointer is not NULL. But 259 ** before calling APIs that follow, extension should check the 260 ** sqlite3_libversion_number() to make sure they are dealing with 261 ** a library that is new enough to support that API. 262 ************************************************************************* 263 */ 264 sqlite3_overload_function, 265 266 /* 267 ** Added after 3.3.13 268 */ 269 sqlite3_prepare_v2, 270 sqlite3_prepare16_v2, 271 sqlite3_clear_bindings, 272 273 /* 274 ** Added for 3.4.1 275 */ 276 sqlite3_create_module_v2, 277 278 /* 279 ** Added for 3.5.0 280 */ 281 sqlite3_bind_zeroblob, 282 sqlite3_blob_bytes, 283 sqlite3_blob_close, 284 sqlite3_blob_open, 285 sqlite3_blob_read, 286 sqlite3_blob_write, 287 sqlite3_create_collation_v2, 288 sqlite3_file_control, 289 sqlite3_memory_highwater, 290 sqlite3_memory_used, 291 #ifdef SQLITE_MUTEX_OMIT 292 0, 293 0, 294 0, 295 0, 296 0, 297 #else 298 sqlite3_mutex_alloc, 299 sqlite3_mutex_enter, 300 sqlite3_mutex_free, 301 sqlite3_mutex_leave, 302 sqlite3_mutex_try, 303 #endif 304 sqlite3_open_v2, 305 sqlite3_release_memory, 306 sqlite3_result_error_nomem, 307 sqlite3_result_error_toobig, 308 sqlite3_sleep, 309 sqlite3_soft_heap_limit, 310 sqlite3_vfs_find, 311 sqlite3_vfs_register, 312 sqlite3_vfs_unregister, 313 314 /* 315 ** Added for 3.5.8 316 */ 317 sqlite3_threadsafe, 318 sqlite3_result_zeroblob, 319 sqlite3_result_error_code, 320 sqlite3_test_control, 321 sqlite3_randomness, 322 sqlite3_context_db_handle, 323 324 /* 325 ** Added for 3.6.0 326 */ 327 sqlite3_extended_result_codes, 328 sqlite3_limit, 329 sqlite3_next_stmt, 330 sqlite3_sql, 331 sqlite3_status, 332 }; 333 334 /* 335 ** Attempt to load an SQLite extension library contained in the file 336 ** zFile. The entry point is zProc. zProc may be 0 in which case a 337 ** default entry point name (sqlite3_extension_init) is used. Use 338 ** of the default name is recommended. 339 ** 340 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong. 341 ** 342 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 343 ** error message text. The calling function should free this memory 344 ** by calling sqlite3DbFree(db, ). 345 */ 346 static int sqlite3LoadExtension( 347 sqlite3 *db, /* Load the extension into this database connection */ 348 const char *zFile, /* Name of the shared library containing extension */ 349 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ 350 char **pzErrMsg /* Put error message here if not 0 */ 351 ){ 352 sqlite3_vfs *pVfs = db->pVfs; 353 void *handle; 354 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); 355 char *zErrmsg = 0; 356 void **aHandle; 357 const int nMsg = 300; 358 359 if( pzErrMsg ) *pzErrMsg = 0; 360 361 /* Ticket #1863. To avoid a creating security problems for older 362 ** applications that relink against newer versions of SQLite, the 363 ** ability to run load_extension is turned off by default. One 364 ** must call sqlite3_enable_load_extension() to turn on extension 365 ** loading. Otherwise you get the following error. 366 */ 367 if( (db->flags & SQLITE_LoadExtension)==0 ){ 368 if( pzErrMsg ){ 369 *pzErrMsg = sqlite3_mprintf("not authorized"); 370 } 371 return SQLITE_ERROR; 372 } 373 374 if( zProc==0 ){ 375 zProc = "sqlite3_extension_init"; 376 } 377 378 handle = sqlite3OsDlOpen(pVfs, zFile); 379 if( handle==0 ){ 380 if( pzErrMsg ){ 381 zErrmsg = sqlite3StackAllocZero(db, nMsg); 382 if( zErrmsg ){ 383 sqlite3_snprintf(nMsg, zErrmsg, 384 "unable to open shared library [%s]", zFile); 385 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); 386 *pzErrMsg = sqlite3DbStrDup(0, zErrmsg); 387 sqlite3StackFree(db, zErrmsg); 388 } 389 } 390 return SQLITE_ERROR; 391 } 392 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) 393 sqlite3OsDlSym(pVfs, handle, zProc); 394 if( xInit==0 ){ 395 if( pzErrMsg ){ 396 zErrmsg = sqlite3StackAllocZero(db, nMsg); 397 if( zErrmsg ){ 398 sqlite3_snprintf(nMsg, zErrmsg, 399 "no entry point [%s] in shared library [%s]", zProc,zFile); 400 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); 401 *pzErrMsg = sqlite3DbStrDup(0, zErrmsg); 402 sqlite3StackFree(db, zErrmsg); 403 } 404 sqlite3OsDlClose(pVfs, handle); 405 } 406 return SQLITE_ERROR; 407 }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){ 408 if( pzErrMsg ){ 409 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg); 410 } 411 sqlite3_free(zErrmsg); 412 sqlite3OsDlClose(pVfs, handle); 413 return SQLITE_ERROR; 414 } 415 416 /* Append the new shared library handle to the db->aExtension array. */ 417 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1)); 418 if( aHandle==0 ){ 419 return SQLITE_NOMEM; 420 } 421 if( db->nExtension>0 ){ 422 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension); 423 } 424 sqlite3DbFree(db, db->aExtension); 425 db->aExtension = aHandle; 426 427 db->aExtension[db->nExtension++] = handle; 428 return SQLITE_OK; 429 } 430 int sqlite3_load_extension( 431 sqlite3 *db, /* Load the extension into this database connection */ 432 const char *zFile, /* Name of the shared library containing extension */ 433 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ 434 char **pzErrMsg /* Put error message here if not 0 */ 435 ){ 436 int rc; 437 sqlite3_mutex_enter(db->mutex); 438 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg); 439 rc = sqlite3ApiExit(db, rc); 440 sqlite3_mutex_leave(db->mutex); 441 return rc; 442 } 443 444 /* 445 ** Call this routine when the database connection is closing in order 446 ** to clean up loaded extensions 447 */ 448 void sqlite3CloseExtensions(sqlite3 *db){ 449 int i; 450 assert( sqlite3_mutex_held(db->mutex) ); 451 for(i=0; i<db->nExtension; i++){ 452 sqlite3OsDlClose(db->pVfs, db->aExtension[i]); 453 } 454 sqlite3DbFree(db, db->aExtension); 455 } 456 457 /* 458 ** Enable or disable extension loading. Extension loading is disabled by 459 ** default so as not to open security holes in older applications. 460 */ 461 int sqlite3_enable_load_extension(sqlite3 *db, int onoff){ 462 sqlite3_mutex_enter(db->mutex); 463 if( onoff ){ 464 db->flags |= SQLITE_LoadExtension; 465 }else{ 466 db->flags &= ~SQLITE_LoadExtension; 467 } 468 sqlite3_mutex_leave(db->mutex); 469 return SQLITE_OK; 470 } 471 472 #endif /* SQLITE_OMIT_LOAD_EXTENSION */ 473 474 /* 475 ** The auto-extension code added regardless of whether or not extension 476 ** loading is supported. We need a dummy sqlite3Apis pointer for that 477 ** code if regular extension loading is not available. This is that 478 ** dummy pointer. 479 */ 480 #ifdef SQLITE_OMIT_LOAD_EXTENSION 481 static const sqlite3_api_routines sqlite3Apis = { 0 }; 482 #endif 483 484 485 /* 486 ** The following object holds the list of automatically loaded 487 ** extensions. 488 ** 489 ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER 490 ** mutex must be held while accessing this list. 491 */ 492 typedef struct sqlite3AutoExtList sqlite3AutoExtList; 493 static SQLITE_WSD struct sqlite3AutoExtList { 494 int nExt; /* Number of entries in aExt[] */ 495 void (**aExt)(void); /* Pointers to the extension init functions */ 496 } sqlite3Autoext = { 0, 0 }; 497 498 /* The "wsdAutoext" macro will resolve to the autoextension 499 ** state vector. If writable static data is unsupported on the target, 500 ** we have to locate the state vector at run-time. In the more common 501 ** case where writable static data is supported, wsdStat can refer directly 502 ** to the "sqlite3Autoext" state vector declared above. 503 */ 504 #ifdef SQLITE_OMIT_WSD 505 # define wsdAutoextInit \ 506 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext) 507 # define wsdAutoext x[0] 508 #else 509 # define wsdAutoextInit 510 # define wsdAutoext sqlite3Autoext 511 #endif 512 513 514 /* 515 ** Register a statically linked extension that is automatically 516 ** loaded by every new database connection. 517 */ 518 int sqlite3_auto_extension(void (*xInit)(void)){ 519 int rc = SQLITE_OK; 520 #ifndef SQLITE_OMIT_AUTOINIT 521 rc = sqlite3_initialize(); 522 if( rc ){ 523 return rc; 524 }else 525 #endif 526 { 527 int i; 528 #if SQLITE_THREADSAFE 529 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); 530 #endif 531 wsdAutoextInit; 532 sqlite3_mutex_enter(mutex); 533 for(i=0; i<wsdAutoext.nExt; i++){ 534 if( wsdAutoext.aExt[i]==xInit ) break; 535 } 536 if( i==wsdAutoext.nExt ){ 537 int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]); 538 void (**aNew)(void); 539 aNew = sqlite3_realloc(wsdAutoext.aExt, nByte); 540 if( aNew==0 ){ 541 rc = SQLITE_NOMEM; 542 }else{ 543 wsdAutoext.aExt = aNew; 544 wsdAutoext.aExt[wsdAutoext.nExt] = xInit; 545 wsdAutoext.nExt++; 546 } 547 } 548 sqlite3_mutex_leave(mutex); 549 assert( (rc&0xff)==rc ); 550 return rc; 551 } 552 } 553 554 /* 555 ** Reset the automatic extension loading mechanism. 556 */ 557 void sqlite3_reset_auto_extension(void){ 558 #ifndef SQLITE_OMIT_AUTOINIT 559 if( sqlite3_initialize()==SQLITE_OK ) 560 #endif 561 { 562 #if SQLITE_THREADSAFE 563 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); 564 #endif 565 wsdAutoextInit; 566 sqlite3_mutex_enter(mutex); 567 sqlite3_free(wsdAutoext.aExt); 568 wsdAutoext.aExt = 0; 569 wsdAutoext.nExt = 0; 570 sqlite3_mutex_leave(mutex); 571 } 572 } 573 574 /* 575 ** Load all automatic extensions. 576 ** 577 ** If anything goes wrong, set an error in the database connection. 578 */ 579 void sqlite3AutoLoadExtensions(sqlite3 *db){ 580 int i; 581 int go = 1; 582 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); 583 584 wsdAutoextInit; 585 if( wsdAutoext.nExt==0 ){ 586 /* Common case: early out without every having to acquire a mutex */ 587 return; 588 } 589 for(i=0; go; i++){ 590 char *zErrmsg; 591 #if SQLITE_THREADSAFE 592 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); 593 #endif 594 sqlite3_mutex_enter(mutex); 595 if( i>=wsdAutoext.nExt ){ 596 xInit = 0; 597 go = 0; 598 }else{ 599 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) 600 wsdAutoext.aExt[i]; 601 } 602 sqlite3_mutex_leave(mutex); 603 zErrmsg = 0; 604 if( xInit && xInit(db, &zErrmsg, &sqlite3Apis) ){ 605 sqlite3Error(db, SQLITE_ERROR, 606 "automatic extension loading failed: %s", zErrmsg); 607 go = 0; 608 } 609 sqlite3_free(zErrmsg); 610 } 611 } 612