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