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