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_data_count,
161 : sqlite3_db_handle,
162 : sqlite3_declare_vtab,
163 : sqlite3_enable_shared_cache,
164 : sqlite3_errcode,
165 : sqlite3_errmsg,
166 : sqlite3_errmsg16,
167 : sqlite3_exec,
168 : sqlite3_expired,
169 : sqlite3_finalize,
170 : sqlite3_free,
171 : sqlite3_free_table,
172 : sqlite3_get_autocommit,
173 : sqlite3_get_auxdata,
174 : sqlite3_get_table,
175 : 0, /* Was sqlite3_global_recover(), but that function is deprecated */
176 : sqlite3_interrupt,
177 : sqlite3_last_insert_rowid,
178 : sqlite3_libversion,
179 : sqlite3_libversion_number,
180 : sqlite3_malloc,
181 : sqlite3_mprintf,
182 : sqlite3_open,
183 : sqlite3_open16,
184 : sqlite3_prepare,
185 : sqlite3_prepare16,
186 : sqlite3_profile,
187 : sqlite3_progress_handler,
188 : sqlite3_realloc,
189 : sqlite3_reset,
190 : sqlite3_result_blob,
191 : sqlite3_result_double,
192 : sqlite3_result_error,
193 : sqlite3_result_error16,
194 : sqlite3_result_int,
195 : sqlite3_result_int64,
196 : sqlite3_result_null,
197 : sqlite3_result_text,
198 : sqlite3_result_text16,
199 : sqlite3_result_text16be,
200 : sqlite3_result_text16le,
201 : sqlite3_result_value,
202 : sqlite3_rollback_hook,
203 : sqlite3_set_authorizer,
204 : sqlite3_set_auxdata,
205 : sqlite3_snprintf,
206 : sqlite3_step,
207 : sqlite3_table_column_metadata,
208 : sqlite3_thread_cleanup,
209 : sqlite3_total_changes,
210 : sqlite3_trace,
211 : sqlite3_transfer_bindings,
212 : sqlite3_update_hook,
213 : sqlite3_user_data,
214 : sqlite3_value_blob,
215 : sqlite3_value_bytes,
216 : sqlite3_value_bytes16,
217 : sqlite3_value_double,
218 : sqlite3_value_int,
219 : sqlite3_value_int64,
220 : sqlite3_value_numeric_type,
221 : sqlite3_value_text,
222 : sqlite3_value_text16,
223 : sqlite3_value_text16be,
224 : sqlite3_value_text16le,
225 : sqlite3_value_type,
226 : sqlite3_vmprintf,
227 : /*
228 : ** The original API set ends here. All extensions can call any
229 : ** of the APIs above provided that the pointer is not NULL. But
230 : ** before calling APIs that follow, extension should check the
231 : ** sqlite3_libversion_number() to make sure they are dealing with
232 : ** a library that is new enough to support that API.
233 : *************************************************************************
234 : */
235 : sqlite3_overload_function,
236 :
237 : /*
238 : ** Added after 3.3.13
239 : */
240 : sqlite3_prepare_v2,
241 : sqlite3_prepare16_v2,
242 : sqlite3_clear_bindings,
243 : };
244 :
245 : /*
246 : ** Attempt to load an SQLite extension library contained in the file
247 : ** zFile. The entry point is zProc. zProc may be 0 in which case a
248 : ** default entry point name (sqlite3_extension_init) is used. Use
249 : ** of the default name is recommended.
250 : **
251 : ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
252 : **
253 : ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
254 : ** error message text. The calling function should free this memory
255 : ** by calling sqlite3_free().
256 : */
257 : int sqlite3_load_extension(
258 : sqlite3 *db, /* Load the extension into this database connection */
259 : const char *zFile, /* Name of the shared library containing extension */
260 : const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
261 : char **pzErrMsg /* Put error message here if not 0 */
262 0 : ){
263 : void *handle;
264 : int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
265 0 : char *zErrmsg = 0;
266 : void **aHandle;
267 :
268 : /* Ticket #1863. To avoid a creating security problems for older
269 : ** applications that relink against newer versions of SQLite, the
270 : ** ability to run load_extension is turned off by default. One
271 : ** must call sqlite3_enable_load_extension() to turn on extension
272 : ** loading. Otherwise you get the following error.
273 : */
274 0 : if( (db->flags & SQLITE_LoadExtension)==0 ){
275 0 : if( pzErrMsg ){
276 0 : *pzErrMsg = sqlite3_mprintf("not authorized");
277 : }
278 0 : return SQLITE_ERROR;
279 : }
280 :
281 0 : if( zProc==0 ){
282 0 : zProc = "sqlite3_extension_init";
283 : }
284 :
285 0 : handle = sqlite3OsDlopen(zFile);
286 0 : if( handle==0 ){
287 0 : if( pzErrMsg ){
288 0 : *pzErrMsg = sqlite3_mprintf("unable to open shared library [%s]", zFile);
289 : }
290 0 : return SQLITE_ERROR;
291 : }
292 0 : xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
293 : sqlite3OsDlsym(handle, zProc);
294 0 : if( xInit==0 ){
295 0 : if( pzErrMsg ){
296 0 : *pzErrMsg = sqlite3_mprintf("no entry point [%s] in shared library [%s]",
297 : zProc, zFile);
298 : }
299 0 : sqlite3OsDlclose(handle);
300 0 : return SQLITE_ERROR;
301 0 : }else if( xInit(db, &zErrmsg, &sqlite3_apis) ){
302 0 : if( pzErrMsg ){
303 0 : *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
304 : }
305 0 : sqlite3_free(zErrmsg);
306 0 : sqlite3OsDlclose(handle);
307 0 : return SQLITE_ERROR;
308 : }
309 :
310 : /* Append the new shared library handle to the db->aExtension array. */
311 0 : db->nExtension++;
312 0 : aHandle = sqliteMalloc(sizeof(handle)*db->nExtension);
313 0 : if( aHandle==0 ){
314 0 : return SQLITE_NOMEM;
315 : }
316 0 : if( db->nExtension>0 ){
317 0 : memcpy(aHandle, db->aExtension, sizeof(handle)*(db->nExtension-1));
318 : }
319 0 : sqliteFree(db->aExtension);
320 0 : db->aExtension = aHandle;
321 :
322 0 : db->aExtension[db->nExtension-1] = handle;
323 0 : return SQLITE_OK;
324 : }
325 :
326 : /*
327 : ** Call this routine when the database connection is closing in order
328 : ** to clean up loaded extensions
329 : */
330 129 : void sqlite3CloseExtensions(sqlite3 *db){
331 : int i;
332 129 : for(i=0; i<db->nExtension; i++){
333 0 : sqlite3OsDlclose(db->aExtension[i]);
334 : }
335 129 : sqliteFree(db->aExtension);
336 129 : }
337 :
338 : /*
339 : ** Enable or disable extension loading. Extension loading is disabled by
340 : ** default so as not to open security holes in older applications.
341 : */
342 0 : int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
343 0 : if( onoff ){
344 0 : db->flags |= SQLITE_LoadExtension;
345 : }else{
346 0 : db->flags &= ~SQLITE_LoadExtension;
347 : }
348 0 : return SQLITE_OK;
349 : }
350 :
351 : /*
352 : ** A list of automatically loaded extensions.
353 : **
354 : ** This list is shared across threads, so be sure to hold the
355 : ** mutex while accessing or changing it.
356 : */
357 : static int nAutoExtension = 0;
358 : static void **aAutoExtension = 0;
359 :
360 :
361 : /*
362 : ** Register a statically linked extension that is automatically
363 : ** loaded by every new database connection.
364 : */
365 0 : int sqlite3_auto_extension(void *xInit){
366 : int i;
367 0 : int rc = SQLITE_OK;
368 0 : sqlite3OsEnterMutex();
369 0 : for(i=0; i<nAutoExtension; i++){
370 0 : if( aAutoExtension[i]==xInit ) break;
371 : }
372 0 : if( i==nAutoExtension ){
373 0 : nAutoExtension++;
374 0 : aAutoExtension = sqlite3Realloc( aAutoExtension,
375 : nAutoExtension*sizeof(aAutoExtension[0]) );
376 0 : if( aAutoExtension==0 ){
377 0 : nAutoExtension = 0;
378 0 : rc = SQLITE_NOMEM;
379 : }else{
380 0 : aAutoExtension[nAutoExtension-1] = xInit;
381 : }
382 : }
383 0 : sqlite3OsLeaveMutex();
384 : assert( (rc&0xff)==rc );
385 0 : return rc;
386 : }
387 :
388 : /*
389 : ** Reset the automatic extension loading mechanism.
390 : */
391 0 : void sqlite3_reset_auto_extension(void){
392 0 : sqlite3OsEnterMutex();
393 0 : sqliteFree(aAutoExtension);
394 0 : aAutoExtension = 0;
395 0 : nAutoExtension = 0;
396 0 : sqlite3OsLeaveMutex();
397 0 : }
398 :
399 : /*
400 : ** Load all automatic extensions.
401 : */
402 130 : int sqlite3AutoLoadExtensions(sqlite3 *db){
403 : int i;
404 130 : int go = 1;
405 130 : int rc = SQLITE_OK;
406 : int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
407 :
408 130 : if( nAutoExtension==0 ){
409 : /* Common case: early out without every having to acquire a mutex */
410 130 : return SQLITE_OK;
411 : }
412 0 : for(i=0; go; i++){
413 0 : char *zErrmsg = 0;
414 0 : sqlite3OsEnterMutex();
415 0 : if( i>=nAutoExtension ){
416 0 : xInit = 0;
417 0 : go = 0;
418 : }else{
419 0 : xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
420 : aAutoExtension[i];
421 : }
422 0 : sqlite3OsLeaveMutex();
423 0 : if( xInit && xInit(db, &zErrmsg, &sqlite3_apis) ){
424 0 : sqlite3Error(db, SQLITE_ERROR,
425 : "automatic extension loading failed: %s", zErrmsg);
426 0 : go = 0;
427 0 : rc = SQLITE_ERROR;
428 : }
429 : }
430 0 : return rc;
431 : }
432 :
433 : #endif /* SQLITE_OMIT_LOAD_EXTENSION */
|