loadext.c 14 KB

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