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