2
* Copyright (C) 2011, 2012, 2013 Citrix Systems
3
* Copyright (C) 2014 Vivocha S.p.A.
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
* 3. Neither the name of the project nor the names of its contributors
16
* may be used to endorse or promote products derived from this software
17
* without specific prior written permission.
19
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
#include "../mainrelay.h"
33
#include "dbd_sqlite.h"
35
#if !defined(TURN_NO_SQLITE)
40
#include <sys/types.h>
43
///////////////////////////////////////////////////////////////////////////////////////////////////////////
45
static int sqlite_init_multithreaded(void) {
49
if (sqlite3_threadsafe() > 0) {
50
int retCode = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
51
if (retCode != SQLITE_OK) {
52
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "setting sqlite thread safe mode to serialized failed!!! return code: %d\n", retCode);
56
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Your SQLite database is not compiled to be threadsafe.\n");
63
static int donot_print_connection_success = 0;
65
static void fix_user_directory(char *dir0) {
67
while(*dir == ' ') ++dir;
69
char *home=getenv("HOME");
71
struct passwd *pwd = getpwuid(getuid());
73
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot figure out the user's HOME directory (1)\n");
77
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot figure out the user's HOME directory\n");
82
size_t szh = strlen(home);
83
size_t sz = strlen(dir0)+1+szh;
84
char* dir_fixed = (char*)turn_malloc(sz);
85
strncpy(dir_fixed,home,szh);
86
strncpy(dir_fixed+szh,dir+1,(sz-szh-1));
87
strncpy(dir0,dir_fixed,sz);
88
turn_free(dir_fixed,sz);
92
static void init_sqlite_database(sqlite3 *sqliteconnection) {
94
const char * statements[] = {
95
"CREATE TABLE turnusers_lt ( realm varchar(512) default '', name varchar(512), hmackey char(128), PRIMARY KEY (realm,name))",
96
"CREATE TABLE turnusers_st (name varchar(512) PRIMARY KEY, password varchar(512))",
97
"CREATE TABLE turn_secret (realm varchar(512) default '', value varchar(512), primary key (realm,value))",
98
"CREATE TABLE allowed_peer_ip (realm varchar(512) default '', ip_range varchar(256), primary key (realm,ip_range))",
99
"CREATE TABLE denied_peer_ip (realm varchar(512) default '', ip_range varchar(256), primary key (realm,ip_range))",
100
"CREATE TABLE turn_origin_to_realm (origin varchar(512),realm varchar(512),primary key (origin))",
101
"CREATE TABLE turn_realm_option (realm varchar(512) default '', opt varchar(32), value varchar(128), primary key (realm,opt))",
102
"CREATE TABLE oauth_key (kid varchar(128),ikm_key varchar(256) default '',timestamp bigint default 0,lifetime integer default 0,hkdf_hash_func varchar(64) default '',as_rs_alg varchar(64) default '',as_rs_key varchar(256) default '',auth_alg varchar(64) default '',auth_key varchar(256) default '',primary key (kid))",
107
while(statements[i]) {
108
sqlite3_stmt *statement = NULL;
110
if ((rc = sqlite3_prepare(sqliteconnection, statements[i], -1, &statement, 0)) == SQLITE_OK) {
111
sqlite3_step(statement);
113
sqlite3_finalize(statement);
118
static sqlite3 * get_sqlite_connection(void) {
120
persistent_users_db_t *pud = get_persistent_users_db();
122
sqlite3 *sqliteconnection = (sqlite3 *)(pud->connection);
123
if(!sqliteconnection) {
124
fix_user_directory(pud->userdb);
125
sqlite_init_multithreaded();
126
int rc = sqlite3_open(pud->userdb, &sqliteconnection);
127
if(!sqliteconnection || (rc != SQLITE_OK)) {
128
const char* errmsg = sqlite3_errmsg(sqliteconnection);
129
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot open SQLite DB connection: <%s>, runtime error:\n %s\n (If your intention is to use a database for the TURN server, then\n check the TURN server process / file / DB directory permissions and\n re-start the TURN server)\n",pud->userdb,errmsg);
130
if(sqliteconnection) {
131
sqlite3_close(sqliteconnection);
132
sqliteconnection=NULL;
134
turn_params.default_users_db.userdb_type = TURN_USERDB_TYPE_UNKNOWN;
135
} else if(!donot_print_connection_success){
136
init_sqlite_database(sqliteconnection);
137
TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "SQLite DB connection success: %s\n",pud->userdb);
139
pud->connection = sqliteconnection;
141
return sqliteconnection;
144
///////////////////////////////////////////////////////////////////////////////////////////////////////////
146
static int sqlite_get_auth_secrets(secrets_list_t *sl, u08bits *realm)
149
sqlite3 *sqliteconnection = get_sqlite_connection();
150
if (sqliteconnection) {
151
char statement[TURN_LONG_STRING_SIZE];
152
sqlite3_stmt *st = NULL;
154
snprintf(statement, sizeof(statement) - 1, "select value from turn_secret where realm='%s'", realm);
155
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
157
int ctotal = sqlite3_column_count(st);
162
int res = sqlite3_step(st);
163
if (res == SQLITE_ROW) {
165
int type = sqlite3_column_type(st, 0);
166
if (type != SQLITE_NULL)
167
add_to_secrets_list(sl, (const char*) sqlite3_column_text(st, 0));
169
} else if (res == SQLITE_DONE) {
172
const char* errmsg = sqlite3_errmsg(sqliteconnection);
173
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
179
const char* errmsg = sqlite3_errmsg(sqliteconnection);
180
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
182
sqlite3_finalize(st);
187
static int sqlite_get_user_key(u08bits *usname, u08bits *realm, hmackey_t key)
190
sqlite3 *sqliteconnection = get_sqlite_connection();
191
if (sqliteconnection) {
192
char statement[TURN_LONG_STRING_SIZE];
193
sqlite3_stmt *st = NULL;
195
snprintf(statement, sizeof(statement), "select hmackey from turnusers_lt where name='%s' and realm='%s'", usname, realm);
196
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
197
int res = sqlite3_step(st);
198
if (res == SQLITE_ROW) {
199
char *kval = turn_strdup((const char*) sqlite3_column_text(st, 0));
200
size_t sz = get_hmackey_size(turn_params.shatype);
201
if (convert_string_key_to_binary(kval, key, sz) < 0) {
202
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key: %s, user %s\n", kval, usname);
206
turn_free(kval,strlen(kval)+1);
209
const char* errmsg = sqlite3_errmsg(sqliteconnection);
210
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
213
sqlite3_finalize(st);
218
static int sqlite_get_user_pwd(u08bits *usname, st_password_t pwd)
221
char statement[TURN_LONG_STRING_SIZE];
222
sqlite3_stmt *st = NULL;
224
snprintf(statement, sizeof(statement), "select password from turnusers_st where name='%s'", usname);
226
sqlite3 *sqliteconnection = get_sqlite_connection();
227
if (sqliteconnection) {
228
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
229
int res = sqlite3_step(st);
230
if (res == SQLITE_ROW) {
231
const char *kval = (const char*) sqlite3_column_text(st, 0);
233
strncpy((char*) pwd, kval, sizeof(st_password_t));
236
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong password data for user %s: NULL\n", usname);
240
const char* errmsg = sqlite3_errmsg(sqliteconnection);
241
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
244
sqlite3_finalize(st);
249
static int sqlite_get_oauth_key(const u08bits *kid, oauth_key_data_raw *key) {
253
char statement[TURN_LONG_STRING_SIZE];
254
sqlite3_stmt *st = NULL;
256
snprintf(statement,sizeof(statement),"select ikm_key,timestamp,lifetime,hkdf_hash_func,as_rs_alg,as_rs_key,auth_alg,auth_key from oauth_key where kid='%s'",(const char*)kid);
258
sqlite3 *sqliteconnection = get_sqlite_connection();
259
if(sqliteconnection) {
261
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
263
int res = sqlite3_step(st);
264
if (res == SQLITE_ROW) {
266
STRCPY((char*)key->ikm_key,sqlite3_column_text(st, 0));
267
key->timestamp = (u64bits)strtoll((const char*)sqlite3_column_text(st, 1),NULL,10);
268
key->lifetime = (u32bits)strtol((const char*)sqlite3_column_text(st, 2),NULL,10);
269
STRCPY((char*)key->hkdf_hash_func,sqlite3_column_text(st, 3));
270
STRCPY((char*)key->as_rs_alg,sqlite3_column_text(st, 4));
271
STRCPY((char*)key->as_rs_key,sqlite3_column_text(st, 5));
272
STRCPY((char*)key->auth_alg,sqlite3_column_text(st, 6));
273
STRCPY((char*)key->auth_key,sqlite3_column_text(st, 7));
274
STRCPY((char*)key->kid,kid);
278
const char* errmsg = sqlite3_errmsg(sqliteconnection);
279
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
282
sqlite3_finalize(st);
288
static int sqlite_list_oauth_keys(void) {
290
oauth_key_data_raw key_;
291
oauth_key_data_raw *key=&key_;
293
donot_print_connection_success=1;
297
char statement[TURN_LONG_STRING_SIZE];
298
sqlite3_stmt *st = NULL;
300
snprintf(statement,sizeof(statement),"select ikm_key,timestamp,lifetime,hkdf_hash_func,as_rs_alg,as_rs_key,auth_alg,auth_key,kid from oauth_key order by kid");
302
sqlite3 *sqliteconnection = get_sqlite_connection();
303
if(sqliteconnection) {
305
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
309
int res = sqlite3_step(st);
310
if (res == SQLITE_ROW) {
312
STRCPY((char*)key->ikm_key,sqlite3_column_text(st, 0));
313
key->timestamp = (u64bits)strtoll((const char*)sqlite3_column_text(st, 1),NULL,10);
314
key->lifetime = (u32bits)strtol((const char*)sqlite3_column_text(st, 2),NULL,10);
315
STRCPY((char*)key->hkdf_hash_func,sqlite3_column_text(st, 3));
316
STRCPY((char*)key->as_rs_alg,sqlite3_column_text(st, 4));
317
STRCPY((char*)key->as_rs_key,sqlite3_column_text(st, 5));
318
STRCPY((char*)key->auth_alg,sqlite3_column_text(st, 6));
319
STRCPY((char*)key->auth_key,sqlite3_column_text(st, 7));
320
STRCPY((char*)key->kid,sqlite3_column_text(st, 7));
322
printf(" kid=%s, ikm_key=%s, timestamp=%llu, lifetime=%lu, hkdf_hash_func=%s, as_rs_alg=%s, as_rs_key=%s, auth_alg=%s, auth_key=%s\n",
323
key->kid, key->ikm_key, (unsigned long long)key->timestamp, (unsigned long)key->lifetime, key->hkdf_hash_func,
324
key->as_rs_alg, key->as_rs_key, key->auth_alg, key->auth_key);
326
} else if (res == SQLITE_DONE) {
329
const char* errmsg = sqlite3_errmsg(sqliteconnection);
330
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
336
const char* errmsg = sqlite3_errmsg(sqliteconnection);
337
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
340
sqlite3_finalize(st);
346
static int sqlite_set_user_key(u08bits *usname, u08bits *realm, const char *key)
349
char statement[TURN_LONG_STRING_SIZE];
350
sqlite3_stmt *st = NULL;
353
donot_print_connection_success=1;
355
sqlite3 *sqliteconnection = get_sqlite_connection();
356
if (sqliteconnection) {
358
snprintf(statement, sizeof(statement), "insert or replace into turnusers_lt (realm,name,hmackey) values('%s','%s','%s')", realm, usname, key);
360
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
364
const char* errmsg = sqlite3_errmsg(sqliteconnection);
365
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
367
sqlite3_finalize(st);
372
static int sqlite_set_oauth_key(oauth_key_data_raw *key)
376
char statement[TURN_LONG_STRING_SIZE];
377
sqlite3_stmt *st = NULL;
380
donot_print_connection_success=1;
382
sqlite3 *sqliteconnection = get_sqlite_connection();
383
if (sqliteconnection) {
387
"insert or replace into oauth_key (kid,ikm_key,timestamp,lifetime,hkdf_hash_func,as_rs_alg,as_rs_key,auth_alg,auth_key) values('%s','%s',%llu,%lu,'%s','%s','%s','%s','%s')",
388
key->kid, key->ikm_key, (unsigned long long) key->timestamp, (unsigned long) key->lifetime, key->hkdf_hash_func, key->as_rs_alg, key->as_rs_key, key->auth_alg,
391
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
395
const char* errmsg = sqlite3_errmsg(sqliteconnection);
396
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
398
sqlite3_finalize(st);
403
static int sqlite_set_user_pwd(u08bits *usname, st_password_t pwd)
406
char statement[TURN_LONG_STRING_SIZE];
407
sqlite3_stmt *st = NULL;
410
donot_print_connection_success=1;
412
sqlite3 *sqliteconnection = get_sqlite_connection();
413
if (sqliteconnection) {
414
snprintf(statement, sizeof(statement), "insert or replace into turnusers_st values('%s','%s')", usname, pwd);
415
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
419
const char* errmsg = sqlite3_errmsg(sqliteconnection);
420
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
422
sqlite3_finalize(st);
427
static int sqlite_del_user(u08bits *usname, int is_st, u08bits *realm)
430
char statement[TURN_LONG_STRING_SIZE];
431
sqlite3_stmt *st = NULL;
434
donot_print_connection_success=1;
436
sqlite3 *sqliteconnection = get_sqlite_connection();
437
if (sqliteconnection) {
439
snprintf(statement, sizeof(statement), "delete from turnusers_st where name='%s'", usname);
441
snprintf(statement, sizeof(statement), "delete from turnusers_lt where name='%s' and realm='%s'", usname, realm);
443
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
447
const char* errmsg = sqlite3_errmsg(sqliteconnection);
448
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
450
sqlite3_finalize(st);
455
static int sqlite_del_oauth_key(const u08bits *kid)
458
char statement[TURN_LONG_STRING_SIZE];
459
sqlite3_stmt *st = NULL;
462
donot_print_connection_success=1;
464
sqlite3 *sqliteconnection = get_sqlite_connection();
465
if (sqliteconnection) {
466
snprintf(statement, sizeof(statement), "delete from oauth_key where kid = '%s'", (const char*) kid);
468
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
472
const char* errmsg = sqlite3_errmsg(sqliteconnection);
473
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
475
sqlite3_finalize(st);
481
static int sqlite_list_users(int is_st, u08bits *realm)
484
char statement[TURN_LONG_STRING_SIZE];
485
sqlite3_stmt *st = NULL;
488
donot_print_connection_success=1;
490
sqlite3 *sqliteconnection = get_sqlite_connection();
491
if (sqliteconnection) {
493
snprintf(statement, sizeof(statement), "select name,'' from turnusers_st order by name");
494
} else if (realm && realm[0]) {
495
snprintf(statement, sizeof(statement), "select name,realm from turnusers_lt where realm='%s' order by name", realm);
497
snprintf(statement, sizeof(statement), "select name,realm from turnusers_lt order by name");
499
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
503
int res = sqlite3_step(st);
504
if (res == SQLITE_ROW) {
506
const char* kval = (const char*) sqlite3_column_text(st, 0);
507
const char* rval = (const char*) sqlite3_column_text(st, 1);
510
printf("%s[%s]\n", kval, rval);
512
printf("%s\n", kval);
515
} else if (res == SQLITE_DONE) {
518
const char* errmsg = sqlite3_errmsg(sqliteconnection);
519
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
525
const char* errmsg = sqlite3_errmsg(sqliteconnection);
526
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
528
sqlite3_finalize(st);
533
static int sqlite_show_secret(u08bits *realm)
536
char statement[TURN_LONG_STRING_SIZE];
537
sqlite3_stmt *st = NULL;
539
snprintf(statement,sizeof(statement)-1,"select value from turn_secret where realm='%s'",realm);
541
donot_print_connection_success=1;
543
sqlite3 *sqliteconnection = get_sqlite_connection();
544
if(sqliteconnection) {
545
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
546
int res = sqlite3_step(st);
547
if (res == SQLITE_ROW) {
549
const char* kval = (const char*) sqlite3_column_text(st, 0);
555
const char* errmsg = sqlite3_errmsg(sqliteconnection);
556
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
558
sqlite3_finalize(st);
563
static int sqlite_del_secret(u08bits *secret, u08bits *realm)
566
donot_print_connection_success=1;
567
char statement[TURN_LONG_STRING_SIZE];
568
sqlite3_stmt *st = NULL;
571
sqlite3 *sqliteconnection = get_sqlite_connection();
572
if (sqliteconnection) {
573
if(!secret || (secret[0]==0))
574
snprintf(statement,sizeof(statement),"delete from turn_secret where realm='%s'",realm);
576
snprintf(statement,sizeof(statement),"delete from turn_secret where value='%s' and realm='%s'",secret,realm);
578
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
582
const char* errmsg = sqlite3_errmsg(sqliteconnection);
583
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
585
sqlite3_finalize(st);
590
static int sqlite_set_secret(u08bits *secret, u08bits *realm)
593
donot_print_connection_success = 1;
594
char statement[TURN_LONG_STRING_SIZE];
595
sqlite3_stmt *st = NULL;
598
sqlite3 *sqliteconnection = get_sqlite_connection();
599
if (sqliteconnection) {
600
snprintf(statement,sizeof(statement),"insert or replace into turn_secret (realm,value) values('%s','%s')",realm,secret);
601
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
605
const char* errmsg = sqlite3_errmsg(sqliteconnection);
606
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
608
sqlite3_finalize(st);
613
static int sqlite_add_origin(u08bits *origin, u08bits *realm)
616
char statement[TURN_LONG_STRING_SIZE];
617
sqlite3_stmt *st = NULL;
619
donot_print_connection_success=1;
621
sqlite3 *sqliteconnection = get_sqlite_connection();
622
if(sqliteconnection) {
623
snprintf(statement,sizeof(statement),"insert or replace into turn_origin_to_realm (origin,realm) values('%s','%s')",origin,realm);
624
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
628
const char* errmsg = sqlite3_errmsg(sqliteconnection);
629
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
631
sqlite3_finalize(st);
636
static int sqlite_del_origin(u08bits *origin)
639
char statement[TURN_LONG_STRING_SIZE];
640
sqlite3_stmt *st = NULL;
642
donot_print_connection_success=1;
644
sqlite3 *sqliteconnection = get_sqlite_connection();
645
if(sqliteconnection) {
646
snprintf(statement,sizeof(statement),"delete from turn_origin_to_realm where origin='%s'",origin);
647
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
651
const char* errmsg = sqlite3_errmsg(sqliteconnection);
652
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
654
sqlite3_finalize(st);
659
static int sqlite_list_origins(u08bits *realm)
662
donot_print_connection_success = 1;
663
char statement[TURN_LONG_STRING_SIZE];
664
sqlite3_stmt *st = NULL;
667
sqlite3 *sqliteconnection = get_sqlite_connection();
668
if (sqliteconnection) {
669
if (realm && realm[0]) {
670
snprintf(statement, sizeof(statement), "select origin,realm from turn_origin_to_realm where realm='%s' order by origin", realm);
672
snprintf(statement, sizeof(statement), "select origin,realm from turn_origin_to_realm order by origin,realm");
674
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
678
int res = sqlite3_step(st);
679
if (res == SQLITE_ROW) {
681
const char* kval = (const char*) sqlite3_column_text(st, 0);
682
const char* rval = (const char*) sqlite3_column_text(st, 1);
684
printf("%s ==>> %s\n",kval,rval);
686
} else if (res == SQLITE_DONE) {
689
const char* errmsg = sqlite3_errmsg(sqliteconnection);
690
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
696
const char* errmsg = sqlite3_errmsg(sqliteconnection);
697
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
699
sqlite3_finalize(st);
704
static int sqlite_set_realm_option_one(u08bits *realm, unsigned long value, const char* opt)
707
char statement[TURN_LONG_STRING_SIZE];
708
sqlite3_stmt *st = NULL;
710
donot_print_connection_success=1;
712
sqlite3 *sqliteconnection = get_sqlite_connection();
713
if(sqliteconnection) {
715
snprintf(statement,sizeof(statement),"insert or replace into turn_realm_option (realm,opt,value) values('%s','%s','%lu')",realm,opt,(unsigned long)value);
716
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
720
const char* errmsg = sqlite3_errmsg(sqliteconnection);
721
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
723
sqlite3_finalize(st);
729
static int sqlite_list_realm_options(u08bits *realm)
732
donot_print_connection_success = 1;
733
char statement[TURN_LONG_STRING_SIZE];
734
sqlite3_stmt *st = NULL;
737
sqlite3 *sqliteconnection = get_sqlite_connection();
738
if (sqliteconnection) {
739
if (realm && realm[0]) {
740
snprintf(statement, sizeof(statement), "select realm,opt,value from turn_realm_option where realm='%s' order by realm,opt", realm);
742
snprintf(statement, sizeof(statement), "select realm,opt,value from turn_realm_option order by realm,opt");
744
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
749
int res = sqlite3_step(st);
750
if (res == SQLITE_ROW) {
752
const char* rval = (const char*) sqlite3_column_text(st, 0);
753
const char* oval = (const char*) sqlite3_column_text(st, 1);
754
const char* vval = (const char*) sqlite3_column_text(st, 2);
756
printf("%s[%s]=%s\n",oval,rval,vval);
758
} else if (res == SQLITE_DONE) {
761
const char* errmsg = sqlite3_errmsg(sqliteconnection);
762
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
768
const char* errmsg = sqlite3_errmsg(sqliteconnection);
769
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
771
sqlite3_finalize(st);
776
static void sqlite_auth_ping(void * rch)
781
static int sqlite_get_ip_list(const char *kind, ip_range_list_t * list)
785
sqlite3 *sqliteconnection = get_sqlite_connection();
786
if (sqliteconnection) {
787
char statement[TURN_LONG_STRING_SIZE];
788
sqlite3_stmt *st = NULL;
790
snprintf(statement, sizeof(statement), "select ip_range,realm from %s_peer_ip", kind);
791
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
796
int res = sqlite3_step(st);
797
if (res == SQLITE_ROW) {
799
const char* kval = (const char*) sqlite3_column_text(st, 0);
800
const char* rval = (const char*) sqlite3_column_text(st, 1);
802
add_ip_list_range(kval, rval, list);
804
} else if (res == SQLITE_DONE) {
807
const char* errmsg = sqlite3_errmsg(sqliteconnection);
808
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
814
const char* errmsg = sqlite3_errmsg(sqliteconnection);
815
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
817
sqlite3_finalize(st);
822
static void sqlite_reread_realms(secrets_list_t * realms_list)
824
sqlite3 *sqliteconnection = get_sqlite_connection();
825
if(sqliteconnection) {
826
char statement[TURN_LONG_STRING_SIZE];
827
sqlite3_stmt *st = NULL;
830
snprintf(statement,sizeof(statement),"select origin,realm from turn_origin_to_realm");
831
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
833
ur_string_map *o_to_realm_new = ur_string_map_create(turn_free_simple);
836
int res = sqlite3_step(st);
837
if (res == SQLITE_ROW) {
839
char* oval = turn_strdup((const char*) sqlite3_column_text(st, 0));
840
char* rval = turn_strdup((const char*) sqlite3_column_text(st, 1));
843
ur_string_map_value_type value = rval;
844
ur_string_map_put(o_to_realm_new, (const ur_string_map_key_type) oval, value);
846
turn_free(oval,strlen(oval)+1);
848
} else if (res == SQLITE_DONE) {
851
const char* errmsg = sqlite3_errmsg(sqliteconnection);
852
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
857
update_o_to_realm(o_to_realm_new);
860
const char* errmsg = sqlite3_errmsg(sqliteconnection);
861
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
863
sqlite3_finalize(st);
872
rlsz = realms_list->sz;
875
for (i = 0; i<rlsz; ++i) {
877
char *realm = realms_list->secrets[i];
879
realm_params_t* rp = get_realm(realm);
882
rp->options.perf_options.max_bps = turn_params.max_bps;
886
rp->options.perf_options.total_quota = turn_params.total_quota;
890
rp->options.perf_options.user_quota = turn_params.user_quota;
896
snprintf(statement,sizeof(statement),"select realm,opt,value from turn_realm_option");
897
if ((rc = sqlite3_prepare(sqliteconnection, statement, -1, &st, 0)) == SQLITE_OK) {
900
int res = sqlite3_step(st);
901
if (res == SQLITE_ROW) {
903
char* rval = turn_strdup((const char*) sqlite3_column_text(st, 0));
904
const char* oval = (const char*) sqlite3_column_text(st, 1);
905
const char* vval = (const char*) sqlite3_column_text(st, 2);
907
realm_params_t* rp = get_realm(rval);
908
if(!strcmp(oval,"max-bps"))
909
rp->options.perf_options.max_bps = (band_limit_t)atol(vval);
910
else if(!strcmp(oval,"total-quota"))
911
rp->options.perf_options.total_quota = (vint)atoi(vval);
912
else if(!strcmp(oval,"user-quota"))
913
rp->options.perf_options.user_quota = (vint)atoi(vval);
915
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown realm option: %s\n", oval);
918
turn_free(rval,strlen(rval)+1);
920
} else if (res == SQLITE_DONE) {
923
const char* errmsg = sqlite3_errmsg(sqliteconnection);
924
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
929
const char* errmsg = sqlite3_errmsg(sqliteconnection);
930
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving SQLite DB information: %s\n", errmsg);
932
sqlite3_finalize(st);
937
///////////////////////////////////////////////////////////////////////////////////////////////////////////
939
static turn_dbdriver_t driver = {
940
&sqlite_get_auth_secrets,
941
&sqlite_get_user_key,
942
&sqlite_get_user_pwd,
943
&sqlite_set_user_key,
944
&sqlite_set_user_pwd,
952
&sqlite_list_origins,
953
&sqlite_set_realm_option_one,
954
&sqlite_list_realm_options,
957
&sqlite_reread_realms,
958
&sqlite_set_oauth_key,
959
&sqlite_get_oauth_key,
960
&sqlite_del_oauth_key,
961
&sqlite_list_oauth_keys
964
//////////////////////////////////////////////////
966
turn_dbdriver_t * get_sqlite_dbdriver(void) {
970
//////////////////////////////////////////////////