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