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