dbd_redis.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /*
  2. * Copyright (C) 2011, 2012, 2013 Citrix Systems
  3. * Copyright (C) 2014 Vivocha S.p.A.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  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.
  18. *
  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
  29. * SUCH DAMAGE.
  30. */
  31. #include "../mainrelay.h"
  32. #include "dbd_redis.h"
  33. #if !defined(TURN_NO_HIREDIS)
  34. #include "hiredis_libevent2.h"
  35. #include <hiredis/hiredis.h>
  36. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. static int donot_print_connection_success = 0;
  38. static void turnFreeRedisReply(void *reply) {
  39. if(reply) {
  40. freeReplyObject(reply);
  41. }
  42. }
  43. struct _Ryconninfo {
  44. char *host;
  45. char *dbname;
  46. char *password;
  47. unsigned int connect_timeout;
  48. unsigned int port;
  49. };
  50. typedef struct _Ryconninfo Ryconninfo;
  51. static void RyconninfoFree(Ryconninfo *co) {
  52. if(co) {
  53. if(co->host) turn_free(co->host, strlen(co->host)+1);
  54. if(co->dbname) turn_free(co->dbname, strlen(co->dbname)+1);
  55. if(co->password) turn_free(co->password, strlen(co->password)+1);
  56. ns_bzero(co,sizeof(Ryconninfo));
  57. }
  58. }
  59. static Ryconninfo *RyconninfoParse(const char *userdb, char **errmsg) {
  60. Ryconninfo *co = (Ryconninfo*) turn_malloc(sizeof(Ryconninfo));
  61. ns_bzero(co,sizeof(Ryconninfo));
  62. if (userdb) {
  63. char *s0 = turn_strdup(userdb);
  64. char *s = s0;
  65. while (s && *s) {
  66. while (*s && (*s == ' '))
  67. ++s;
  68. char *snext = strstr(s, " ");
  69. if (snext) {
  70. *snext = 0;
  71. ++snext;
  72. }
  73. char* seq = strstr(s, "=");
  74. if (!seq) {
  75. RyconninfoFree(co);
  76. co = NULL;
  77. if (errmsg) {
  78. *errmsg = turn_strdup(s);
  79. }
  80. break;
  81. }
  82. *seq = 0;
  83. if (!strcmp(s, "host"))
  84. co->host = turn_strdup(seq + 1);
  85. else if (!strcmp(s, "ip"))
  86. co->host = turn_strdup(seq + 1);
  87. else if (!strcmp(s, "addr"))
  88. co->host = turn_strdup(seq + 1);
  89. else if (!strcmp(s, "ipaddr"))
  90. co->host = turn_strdup(seq + 1);
  91. else if (!strcmp(s, "hostaddr"))
  92. co->host = turn_strdup(seq + 1);
  93. else if (!strcmp(s, "dbname"))
  94. co->dbname = turn_strdup(seq + 1);
  95. else if (!strcmp(s, "db"))
  96. co->dbname = turn_strdup(seq + 1);
  97. else if (!strcmp(s, "database"))
  98. co->dbname = turn_strdup(seq + 1);
  99. else if (!strcmp(s, "user"))
  100. ;
  101. else if (!strcmp(s, "uname"))
  102. ;
  103. else if (!strcmp(s, "name"))
  104. ;
  105. else if (!strcmp(s, "username"))
  106. ;
  107. else if (!strcmp(s, "password"))
  108. co->password = turn_strdup(seq + 1);
  109. else if (!strcmp(s, "pwd"))
  110. co->password = turn_strdup(seq + 1);
  111. else if (!strcmp(s, "passwd"))
  112. co->password = turn_strdup(seq + 1);
  113. else if (!strcmp(s, "secret"))
  114. co->password = turn_strdup(seq + 1);
  115. else if (!strcmp(s, "port"))
  116. co->port = (unsigned int) atoi(seq + 1);
  117. else if (!strcmp(s, "p"))
  118. co->port = (unsigned int) atoi(seq + 1);
  119. else if (!strcmp(s, "connect_timeout"))
  120. co->connect_timeout = (unsigned int) atoi(seq + 1);
  121. else if (!strcmp(s, "timeout"))
  122. co->connect_timeout = (unsigned int) atoi(seq + 1);
  123. else {
  124. RyconninfoFree(co);
  125. co = NULL;
  126. if (errmsg) {
  127. *errmsg = turn_strdup(s);
  128. }
  129. break;
  130. }
  131. s = snext;
  132. }
  133. turn_free(s0, strlen(s0)+1);
  134. }
  135. if(co) {
  136. if(!(co->dbname))
  137. co->dbname=turn_strdup("0");
  138. if(!(co->host))
  139. co->host=turn_strdup("127.0.0.1");
  140. if(!(co->password))
  141. co->password=turn_strdup("");
  142. }
  143. return co;
  144. }
  145. redis_context_handle get_redis_async_connection(struct event_base *base, const char* connection_string, int delete_keys) {
  146. redis_context_handle ret = NULL;
  147. char *errmsg = NULL;
  148. if(base && connection_string && connection_string[0]) {
  149. Ryconninfo *co = RyconninfoParse(connection_string, &errmsg);
  150. if (!co) {
  151. if (errmsg) {
  152. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot open Redis DB connection <%s>, connection string format error: %s\n", connection_string, errmsg);
  153. turn_free(errmsg,strlen(errmsg)+1);
  154. } else {
  155. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot open Redis DB connection <%s>, connection string format error\n", connection_string);
  156. }
  157. } else if (errmsg) {
  158. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot open Redis DB connection <%s>, connection string format error: %s\n", connection_string, errmsg);
  159. turn_free(errmsg,strlen(errmsg)+1);
  160. RyconninfoFree(co);
  161. } else {
  162. if(delete_keys) {
  163. redisContext *rc = NULL;
  164. char ip[256] = "\0";
  165. int port = DEFAULT_REDIS_PORT;
  166. if (co->host)
  167. STRCPY(ip,co->host);
  168. if (!ip[0])
  169. STRCPY(ip,"127.0.0.1");
  170. if (co->port)
  171. port = (int) (co->port);
  172. if (co->connect_timeout) {
  173. struct timeval tv;
  174. tv.tv_usec = 0;
  175. tv.tv_sec = (time_t) (co->connect_timeout);
  176. rc = redisConnectWithTimeout(ip, port, tv);
  177. } else {
  178. rc = redisConnect(ip, port);
  179. }
  180. if (!rc) {
  181. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot initialize Redis DB async connection\n");
  182. } else {
  183. if (co->password) {
  184. turnFreeRedisReply(redisCommand(rc, "AUTH %s", co->password));
  185. }
  186. if (co->dbname) {
  187. turnFreeRedisReply(redisCommand(rc, "select %s", co->dbname));
  188. }
  189. {
  190. redisReply *reply = (redisReply*)redisCommand(rc, "keys turn/*/allocation/*/status");
  191. if(reply) {
  192. secrets_list_t keys;
  193. size_t isz = 0;
  194. char s[513];
  195. init_secrets_list(&keys);
  196. if (reply->type == REDIS_REPLY_ERROR) {
  197. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  198. } else if (reply->type != REDIS_REPLY_ARRAY) {
  199. if (reply->type != REDIS_REPLY_NIL)
  200. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  201. } else {
  202. size_t i;
  203. for (i = 0; i < reply->elements; ++i) {
  204. add_to_secrets_list(&keys,reply->element[i]->str);
  205. }
  206. }
  207. for(isz=0;isz<keys.sz;++isz) {
  208. snprintf(s,sizeof(s),"del %s", keys.secrets[isz]);
  209. turnFreeRedisReply(redisCommand(rc, s));
  210. }
  211. clean_secrets_list(&keys);
  212. turnFreeRedisReply(reply);
  213. }
  214. }
  215. redisFree(rc);
  216. }
  217. }
  218. ret = redisLibeventAttach(base, co->host, co->port, co->password, atoi(co->dbname));
  219. if (!ret) {
  220. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot initialize Redis DB connection\n");
  221. } else if (is_redis_asyncconn_good(ret) && !donot_print_connection_success) {
  222. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Redis DB async connection to be used: %s\n", connection_string);
  223. }
  224. RyconninfoFree(co);
  225. }
  226. }
  227. return ret;
  228. }
  229. static redisContext *get_redis_connection(void) {
  230. persistent_users_db_t *pud = get_persistent_users_db();
  231. redisContext *redisconnection = (redisContext*)(pud->connection);
  232. if(redisconnection) {
  233. if(redisconnection->err) {
  234. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: Cannot connect to redis, err=%d, flags=0x%lx\n", __FUNCTION__,(int)redisconnection->err,(unsigned long)redisconnection->flags);
  235. redisFree(redisconnection);
  236. pud->connection = NULL;
  237. redisconnection = NULL;
  238. }
  239. }
  240. if (!redisconnection) {
  241. char *errmsg = NULL;
  242. Ryconninfo *co = RyconninfoParse(pud->userdb, &errmsg);
  243. if (!co) {
  244. if (errmsg) {
  245. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot open Redis DB connection <%s>, connection string format error: %s\n", pud->userdb, errmsg);
  246. turn_free(errmsg,strlen(errmsg)+1);
  247. } else {
  248. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot open Redis DB connection <%s>, connection string format error\n", pud->userdb);
  249. }
  250. } else if (errmsg) {
  251. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot open Redis DB connection <%s>, connection string format error: %s\n", pud->userdb, errmsg);
  252. turn_free(errmsg,strlen(errmsg)+1);
  253. RyconninfoFree(co);
  254. } else {
  255. char ip[256] = "\0";
  256. int port = DEFAULT_REDIS_PORT;
  257. if (co->host)
  258. STRCPY(ip,co->host);
  259. if (!ip[0])
  260. STRCPY(ip,"127.0.0.1");
  261. if (co->port)
  262. port = (int) (co->port);
  263. if (co->connect_timeout) {
  264. struct timeval tv;
  265. tv.tv_usec = 0;
  266. tv.tv_sec = (time_t) (co->connect_timeout);
  267. redisconnection = redisConnectWithTimeout(ip, port, tv);
  268. } else {
  269. redisconnection = redisConnect(ip, port);
  270. }
  271. if (redisconnection) {
  272. if(redisconnection->err) {
  273. if(redisconnection->errstr[0]) {
  274. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Redis: %s\n",redisconnection->errstr);
  275. }
  276. redisFree(redisconnection);
  277. redisconnection = NULL;
  278. } else if (co->password) {
  279. void *reply = redisCommand(redisconnection, "AUTH %s", co->password);
  280. if(!reply) {
  281. if(redisconnection->err && redisconnection->errstr[0]) {
  282. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Redis: %s\n",redisconnection->errstr);
  283. }
  284. redisFree(redisconnection);
  285. redisconnection = NULL;
  286. } else {
  287. turnFreeRedisReply(reply);
  288. if (co->dbname) {
  289. reply = redisCommand(redisconnection, "select %s", co->dbname);
  290. if(!reply) {
  291. if(redisconnection->err && redisconnection->errstr[0]) {
  292. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Redis: %s\n",redisconnection->errstr);
  293. }
  294. redisFree(redisconnection);
  295. redisconnection = NULL;
  296. } else {
  297. turnFreeRedisReply(reply);
  298. }
  299. }
  300. }
  301. }
  302. }
  303. if (!redisconnection) {
  304. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot initialize Redis DB connection\n");
  305. } else if (!donot_print_connection_success) {
  306. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Redis DB sync connection success: %s\n", pud->userdb);
  307. }
  308. RyconninfoFree(co);
  309. }
  310. pud->connection = redisconnection;
  311. pud = get_persistent_users_db();
  312. redisconnection = (redisContext*)(pud->connection);
  313. }
  314. return redisconnection;
  315. }
  316. static int set_redis_realm_opt(char *realm, const char* key, unsigned long *value)
  317. {
  318. int found = 0;
  319. redisContext *rc = get_redis_connection();
  320. if(rc) {
  321. redisReply *rget = NULL;
  322. char s[1025];
  323. snprintf(s, sizeof(s), "get turn/realm/%s/%s", realm, key);
  324. rget = (redisReply *) redisCommand(rc, s);
  325. if (rget) {
  326. if (rget->type == REDIS_REPLY_ERROR)
  327. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", rget->str);
  328. else if (rget->type != REDIS_REPLY_STRING) {
  329. if (rget->type != REDIS_REPLY_NIL)
  330. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type);
  331. } else {
  332. lock_realms();
  333. *value = (unsigned long)atol(rget->str);
  334. unlock_realms();
  335. found = 1;
  336. }
  337. turnFreeRedisReply(rget);
  338. }
  339. }
  340. return found;
  341. }
  342. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  343. static int redis_get_auth_secrets(secrets_list_t *sl, u08bits *realm)
  344. {
  345. int ret = -1;
  346. redisContext *rc = get_redis_connection();
  347. if (rc) {
  348. redisReply *reply = (redisReply*) redisCommand(rc, "smembers turn/realm/%s/secret", (char*) realm);
  349. if (reply) {
  350. if (reply->type == REDIS_REPLY_ERROR)
  351. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  352. else if (reply->type != REDIS_REPLY_ARRAY) {
  353. if (reply->type != REDIS_REPLY_NIL)
  354. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  355. } else {
  356. size_t i;
  357. for (i = 0; i < reply->elements; ++i) {
  358. add_to_secrets_list(sl, reply->element[i]->str);
  359. }
  360. }
  361. ret = 0;
  362. turnFreeRedisReply(reply);
  363. }
  364. }
  365. return ret;
  366. }
  367. static int redis_get_user_key(u08bits *usname, u08bits *realm, hmackey_t key) {
  368. int ret = -1;
  369. redisContext * rc = get_redis_connection();
  370. if(rc) {
  371. char s[TURN_LONG_STRING_SIZE];
  372. snprintf(s,sizeof(s),"get turn/realm/%s/user/%s/key", (char*)realm, usname);
  373. redisReply *rget = (redisReply *)redisCommand(rc, s);
  374. if(rget) {
  375. if (rget->type == REDIS_REPLY_ERROR)
  376. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", rget->str);
  377. else if (rget->type != REDIS_REPLY_STRING) {
  378. if (rget->type != REDIS_REPLY_NIL)
  379. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type);
  380. } else {
  381. size_t sz = get_hmackey_size(turn_params.shatype);
  382. if(strlen(rget->str)<sz*2) {
  383. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key format: %s, user %s\n",rget->str,usname);
  384. } else if(convert_string_key_to_binary(rget->str, key, sz)<0) {
  385. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key: %s, user %s\n",rget->str,usname);
  386. } else {
  387. ret = 0;
  388. }
  389. }
  390. turnFreeRedisReply(rget);
  391. }
  392. if(ret == 0) {
  393. snprintf(s,sizeof(s),"get turn/realm/%s/user/%s/password", (char*)realm, usname);
  394. rget = (redisReply *)redisCommand(rc, s);
  395. if(rget) {
  396. if (rget->type == REDIS_REPLY_ERROR)
  397. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", rget->str);
  398. else if (rget->type != REDIS_REPLY_STRING) {
  399. if (rget->type != REDIS_REPLY_NIL)
  400. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type);
  401. } else {
  402. if(stun_produce_integrity_key_str((u08bits*)usname, realm, (u08bits*)rget->str, key, turn_params.shatype)>=0) {
  403. ret = 0;
  404. }
  405. }
  406. turnFreeRedisReply(rget);
  407. }
  408. }
  409. }
  410. return ret;
  411. }
  412. static int redis_get_oauth_key(const u08bits *kid, oauth_key_data_raw *key) {
  413. int ret = -1;
  414. redisContext * rc = get_redis_connection();
  415. if(rc) {
  416. char s[TURN_LONG_STRING_SIZE];
  417. ns_bzero(key,sizeof(oauth_key_data_raw));
  418. STRCPY(key->kid,kid);
  419. snprintf(s,sizeof(s),"hgetall turn/oauth/kid/%s", (const char*)kid);
  420. redisReply *reply = (redisReply *)redisCommand(rc, s);
  421. if(reply) {
  422. if (reply->type == REDIS_REPLY_ERROR)
  423. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  424. else if (reply->type != REDIS_REPLY_ARRAY) {
  425. if (reply->type != REDIS_REPLY_NIL)
  426. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  427. } else if(reply->elements > 1) {
  428. size_t i;
  429. for (i = 0; i < (reply->elements)/2; ++i) {
  430. char *kw = reply->element[2*i]->str;
  431. char *val = reply->element[2*i+1]->str;
  432. if(kw) {
  433. if(!strcmp(kw,"as_rs_alg")) {
  434. STRCPY(key->as_rs_alg,val);
  435. } else if(!strcmp(kw,"as_rs_key")) {
  436. STRCPY(key->as_rs_key,val);
  437. } else if(!strcmp(kw,"auth_key")) {
  438. STRCPY(key->auth_key,val);
  439. } else if(!strcmp(kw,"auth_alg")) {
  440. STRCPY(key->auth_alg,val);
  441. } else if(!strcmp(kw,"ikm_key")) {
  442. STRCPY(key->ikm_key,val);
  443. } else if(!strcmp(kw,"hkdf_hash_func")) {
  444. STRCPY(key->hkdf_hash_func,val);
  445. } else if(!strcmp(kw,"timestamp")) {
  446. key->timestamp = (u64bits)strtoull(val,NULL,10);
  447. } else if(!strcmp(kw,"lifetime")) {
  448. key->lifetime = (u32bits)strtoul(val,NULL,10);
  449. }
  450. }
  451. }
  452. ret = 0;
  453. }
  454. turnFreeRedisReply(reply);
  455. }
  456. }
  457. return ret;
  458. }
  459. static int redis_get_user_pwd(u08bits *usname, st_password_t pwd) {
  460. int ret = -1;
  461. redisContext * rc = get_redis_connection();
  462. if(rc) {
  463. char s[TURN_LONG_STRING_SIZE];
  464. snprintf(s,sizeof(s),"get turn/user/%s/password", usname);
  465. redisReply *rget = (redisReply *)redisCommand(rc, s);
  466. if(rget) {
  467. if (rget->type == REDIS_REPLY_ERROR)
  468. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", rget->str);
  469. else if (rget->type != REDIS_REPLY_STRING) {
  470. if (rget->type != REDIS_REPLY_NIL)
  471. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type);
  472. } else {
  473. strncpy((char*)pwd,rget->str,SHORT_TERM_PASSWORD_SIZE);
  474. pwd[SHORT_TERM_PASSWORD_SIZE]=0;
  475. ret = 0;
  476. }
  477. turnFreeRedisReply(rget);
  478. }
  479. }
  480. return ret;
  481. }
  482. static int redis_set_user_key(u08bits *usname, u08bits *realm, const char *key) {
  483. int ret = -1;
  484. redisContext *rc = get_redis_connection();
  485. if(rc) {
  486. char statement[TURN_LONG_STRING_SIZE];
  487. snprintf(statement,sizeof(statement),"set turn/realm/%s/user/%s/key %s",(char*)realm,usname,key);
  488. turnFreeRedisReply(redisCommand(rc, statement));
  489. snprintf(statement,sizeof(statement),"del turn/realm/%s/user/%s/password",(char*)realm,usname);
  490. turnFreeRedisReply(redisCommand(rc, statement));
  491. turnFreeRedisReply(redisCommand(rc, "save"));
  492. ret = 0;
  493. }
  494. return ret;
  495. }
  496. static int redis_set_oauth_key(oauth_key_data_raw *key) {
  497. int ret = -1;
  498. redisContext *rc = get_redis_connection();
  499. if(rc) {
  500. char statement[TURN_LONG_STRING_SIZE];
  501. snprintf(statement,sizeof(statement),"hmset turn/oauth/kid/%s ikm_key '%s' hkdf_hash_func '%s' as_rs_alg '%s' as_rs_key '%s' auth_alg '%s' auth_key '%s' timestamp %llu lifetime %lu",
  502. key->kid,key->ikm_key,key->hkdf_hash_func,key->as_rs_alg,key->as_rs_key,key->auth_alg,key->auth_key,(unsigned long long)key->timestamp,(unsigned long)key->lifetime);
  503. turnFreeRedisReply(redisCommand(rc, statement));
  504. turnFreeRedisReply(redisCommand(rc, "save"));
  505. ret = 0;
  506. }
  507. return ret;
  508. }
  509. static int redis_set_user_pwd(u08bits *usname, st_password_t pwd) {
  510. int ret = -1;
  511. redisContext *rc = get_redis_connection();
  512. if(rc) {
  513. char statement[TURN_LONG_STRING_SIZE];
  514. snprintf(statement,sizeof(statement),"set turn/user/%s/password %s",usname,pwd);
  515. turnFreeRedisReply(redisCommand(rc, statement));
  516. turnFreeRedisReply(redisCommand(rc, "save"));
  517. ret = 0;
  518. }
  519. return ret;
  520. }
  521. static int redis_del_user(u08bits *usname, int is_st, u08bits *realm) {
  522. int ret = -1;
  523. redisContext *rc = get_redis_connection();
  524. if(rc) {
  525. char statement[TURN_LONG_STRING_SIZE];
  526. if(is_st) {
  527. snprintf(statement,sizeof(statement),"del turn/user/%s/password",usname);
  528. turnFreeRedisReply(redisCommand(rc, statement));
  529. } else {
  530. snprintf(statement,sizeof(statement),"del turn/realm/%s/user/%s/key",(char*)realm,usname);
  531. turnFreeRedisReply(redisCommand(rc, statement));
  532. snprintf(statement,sizeof(statement),"del turn/realm/%s/user/%s/password",(char*)realm,usname);
  533. turnFreeRedisReply(redisCommand(rc, statement));
  534. }
  535. turnFreeRedisReply(redisCommand(rc, "save"));
  536. ret = 0;
  537. }
  538. return ret;
  539. }
  540. static int redis_del_oauth_key(const u08bits *kid) {
  541. int ret = -1;
  542. redisContext *rc = get_redis_connection();
  543. if(rc) {
  544. char statement[TURN_LONG_STRING_SIZE];
  545. snprintf(statement,sizeof(statement),"del turn/oauth/kid/%s",(const char*)kid);
  546. turnFreeRedisReply(redisCommand(rc, statement));
  547. turnFreeRedisReply(redisCommand(rc, "save"));
  548. ret = 0;
  549. }
  550. return ret;
  551. }
  552. static int redis_list_users(int is_st, u08bits *realm) {
  553. int ret = -1;
  554. redisContext *rc = get_redis_connection();
  555. if(rc) {
  556. secrets_list_t keys;
  557. size_t isz = 0;
  558. init_secrets_list(&keys);
  559. redisReply *reply = NULL;
  560. if(!is_st) {
  561. if(realm && realm[0]) {
  562. reply = (redisReply*)redisCommand(rc, "keys turn/realm/%s/user/*/key", (char*)realm);
  563. } else {
  564. reply = (redisReply*)redisCommand(rc, "keys turn/realm/*/user/*/key");
  565. }
  566. if(reply) {
  567. if (reply->type == REDIS_REPLY_ERROR)
  568. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  569. else if (reply->type != REDIS_REPLY_ARRAY) {
  570. if (reply->type != REDIS_REPLY_NIL)
  571. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  572. } else {
  573. size_t i;
  574. for (i = 0; i < reply->elements; ++i) {
  575. add_to_secrets_list(&keys,reply->element[i]->str);
  576. }
  577. }
  578. turnFreeRedisReply(reply);
  579. }
  580. if(realm && realm[0]) {
  581. reply = (redisReply*)redisCommand(rc, "keys turn/realm/%s/user/*/password", (char*)realm);
  582. } else {
  583. reply = (redisReply*)redisCommand(rc, "keys turn/realm/*/user/*/password");
  584. }
  585. if(reply) {
  586. if (reply->type == REDIS_REPLY_ERROR)
  587. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  588. else if (reply->type != REDIS_REPLY_ARRAY) {
  589. if (reply->type != REDIS_REPLY_NIL)
  590. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  591. } else {
  592. size_t i;
  593. for (i = 0; i < reply->elements; ++i) {
  594. add_to_secrets_list(&keys,reply->element[i]->str);
  595. }
  596. }
  597. turnFreeRedisReply(reply);
  598. }
  599. } else {
  600. reply = (redisReply*)redisCommand(rc, "keys turn/user/*/password");
  601. if(reply) {
  602. if (reply->type == REDIS_REPLY_ERROR)
  603. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  604. else if (reply->type != REDIS_REPLY_ARRAY) {
  605. if (reply->type != REDIS_REPLY_NIL)
  606. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  607. } else {
  608. size_t i;
  609. for (i = 0; i < reply->elements; ++i) {
  610. add_to_secrets_list(&keys,reply->element[i]->str);
  611. }
  612. }
  613. turnFreeRedisReply(reply);
  614. }
  615. }
  616. for(isz=0;isz<keys.sz;++isz) {
  617. char *s = keys.secrets[isz];
  618. char *sh = strstr(s,"/user/");
  619. if(sh) {
  620. sh += 6;
  621. char* st = strchr(sh,'/');
  622. if(st)
  623. *st=0;
  624. printf("%s\n",sh);
  625. }
  626. }
  627. clean_secrets_list(&keys);
  628. ret = 0;
  629. }
  630. return ret;
  631. }
  632. static int redis_list_oauth_keys(void) {
  633. int ret = -1;
  634. redisContext *rc = get_redis_connection();
  635. secrets_list_t keys;
  636. size_t isz = 0;
  637. init_secrets_list(&keys);
  638. if(rc) {
  639. redisReply *reply = NULL;
  640. reply = (redisReply*)redisCommand(rc, "keys turn/oauth/kid/*");
  641. if(reply) {
  642. if (reply->type == REDIS_REPLY_ERROR) {
  643. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  644. } else if (reply->type != REDIS_REPLY_ARRAY) {
  645. if (reply->type != REDIS_REPLY_NIL) {
  646. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  647. }
  648. } else {
  649. size_t i;
  650. for (i = 0; i < reply->elements; ++i) {
  651. add_to_secrets_list(&keys,reply->element[i]->str);
  652. }
  653. }
  654. turnFreeRedisReply(reply);
  655. }
  656. }
  657. for(isz=0;isz<keys.sz;++isz) {
  658. char *s = keys.secrets[isz];
  659. s += strlen("turn/oauth/kid/");
  660. oauth_key_data_raw key_;
  661. oauth_key_data_raw *key=&key_;
  662. if(redis_get_oauth_key((const u08bits*)s,key) == 0) {
  663. 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",
  664. key->kid, key->ikm_key, (unsigned long long)key->timestamp, (unsigned long)key->lifetime, key->hkdf_hash_func,
  665. key->as_rs_alg, key->as_rs_key, key->auth_alg, key->auth_key);
  666. }
  667. }
  668. clean_secrets_list(&keys);
  669. ret = 0;
  670. return ret;
  671. }
  672. static int redis_show_secret(u08bits *realm)
  673. {
  674. int ret = -1;
  675. donot_print_connection_success = 1;
  676. redisContext *rc = get_redis_connection();
  677. if (rc) {
  678. redisReply *reply = NULL;
  679. if (realm && realm[0]) {
  680. reply = (redisReply*) redisCommand(rc, "keys turn/realm/%s/secret", (char*) realm);
  681. } else {
  682. reply = (redisReply*) redisCommand(rc, "keys turn/realm/*/secret");
  683. }
  684. if (reply) {
  685. secrets_list_t keys;
  686. size_t isz = 0;
  687. char s[257];
  688. init_secrets_list(&keys);
  689. if (reply->type == REDIS_REPLY_ERROR)
  690. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  691. else if (reply->type != REDIS_REPLY_ARRAY) {
  692. if (reply->type != REDIS_REPLY_NIL)
  693. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  694. } else {
  695. size_t i;
  696. for (i = 0; i < reply->elements; ++i) {
  697. add_to_secrets_list(&keys, reply->element[i]->str);
  698. }
  699. }
  700. for (isz = 0; isz < keys.sz; ++isz) {
  701. snprintf(s, sizeof(s), "smembers %s", keys.secrets[isz]);
  702. redisReply *rget = (redisReply *) redisCommand(rc, s);
  703. if (rget) {
  704. if (rget->type == REDIS_REPLY_ERROR) {
  705. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", rget->str);
  706. } else if (rget->type == REDIS_REPLY_STRING) {
  707. printf("%s\n", rget->str);
  708. } else if (rget->type != REDIS_REPLY_ARRAY) {
  709. if (rget->type != REDIS_REPLY_NIL)
  710. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type);
  711. } else {
  712. size_t i;
  713. for (i = 0; i < rget->elements; ++i) {
  714. printf("%s\n", rget->element[i]->str);
  715. }
  716. }
  717. }
  718. turnFreeRedisReply(rget);
  719. }
  720. clean_secrets_list(&keys);
  721. turnFreeRedisReply(reply);
  722. ret = 0;
  723. }
  724. }
  725. return ret;
  726. }
  727. static int redis_del_secret(u08bits *secret, u08bits *realm)
  728. {
  729. int ret = -1;
  730. donot_print_connection_success = 1;
  731. redisContext *rc = get_redis_connection();
  732. if (rc) {
  733. turnFreeRedisReply(redisCommand(rc, "srem turn/realm/%s/secret %s", (char*) realm, (char*) secret));
  734. turnFreeRedisReply(redisCommand(rc, "save"));
  735. ret = 0;
  736. }
  737. return ret;
  738. }
  739. static int redis_set_secret(u08bits *secret, u08bits *realm)
  740. {
  741. int ret = -1;
  742. donot_print_connection_success = 1;
  743. redisContext *rc = get_redis_connection();
  744. if (rc) {
  745. char s[TURN_LONG_STRING_SIZE];
  746. redis_del_secret(secret, realm);
  747. snprintf(s, sizeof(s), "sadd turn/realm/%s/secret %s", (char*) realm, secret);
  748. turnFreeRedisReply(redisCommand(rc, s));
  749. turnFreeRedisReply(redisCommand(rc, "save"));
  750. ret = 0;
  751. }
  752. return ret;
  753. }
  754. static int redis_add_origin(u08bits *origin, u08bits *realm) {
  755. int ret = -1;
  756. redisContext *rc = get_redis_connection();
  757. if(rc) {
  758. char s[TURN_LONG_STRING_SIZE];
  759. snprintf(s,sizeof(s),"set turn/origin/%s %s", (char*)origin, (char*)realm);
  760. turnFreeRedisReply(redisCommand(rc, s));
  761. turnFreeRedisReply(redisCommand(rc, "save"));
  762. ret = 0;
  763. }
  764. return ret;
  765. }
  766. static int redis_del_origin(u08bits *origin) {
  767. int ret = -1;
  768. redisContext *rc = get_redis_connection();
  769. if(rc) {
  770. char s[TURN_LONG_STRING_SIZE];
  771. snprintf(s,sizeof(s),"del turn/origin/%s", (char*)origin);
  772. turnFreeRedisReply(redisCommand(rc, s));
  773. turnFreeRedisReply(redisCommand(rc, "save"));
  774. ret = 0;
  775. }
  776. return ret;
  777. }
  778. static int redis_list_origins(u08bits *realm) {
  779. int ret = -1;
  780. donot_print_connection_success = 1;
  781. redisContext *rc = get_redis_connection();
  782. if(rc) {
  783. secrets_list_t keys;
  784. size_t isz = 0;
  785. init_secrets_list(&keys);
  786. redisReply *reply = NULL;
  787. {
  788. reply = (redisReply*)redisCommand(rc, "keys turn/origin/*");
  789. if(reply) {
  790. if (reply->type == REDIS_REPLY_ERROR)
  791. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  792. else if (reply->type != REDIS_REPLY_ARRAY) {
  793. if (reply->type != REDIS_REPLY_NIL)
  794. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  795. } else {
  796. size_t i;
  797. size_t offset = strlen("turn/origin/");
  798. for (i = 0; i < reply->elements; ++i) {
  799. add_to_secrets_list(&keys,reply->element[i]->str+offset);
  800. }
  801. }
  802. turnFreeRedisReply(reply);
  803. }
  804. }
  805. for(isz=0;isz<keys.sz;++isz) {
  806. char *o = keys.secrets[isz];
  807. reply = (redisReply*)redisCommand(rc, "get turn/origin/%s",o);
  808. if(reply) {
  809. if (reply->type == REDIS_REPLY_ERROR)
  810. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  811. else if (reply->type != REDIS_REPLY_STRING) {
  812. if (reply->type != REDIS_REPLY_NIL)
  813. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  814. } else {
  815. if(!(realm && realm[0] && strcmp((char*)realm,reply->str))) {
  816. printf("%s ==>> %s\n",o,reply->str);
  817. }
  818. }
  819. turnFreeRedisReply(reply);
  820. }
  821. }
  822. clean_secrets_list(&keys);
  823. ret = 0;
  824. }
  825. return ret;
  826. }
  827. static int redis_set_realm_option_one(u08bits *realm, unsigned long value, const char* opt) {
  828. int ret = -1;
  829. redisContext *rc = get_redis_connection();
  830. if(rc) {
  831. char s[TURN_LONG_STRING_SIZE];
  832. if(value>0)
  833. snprintf(s,sizeof(s),"set turn/realm/%s/%s %lu", (char*)realm, opt, (unsigned long)value);
  834. else
  835. snprintf(s,sizeof(s),"del turn/realm/%s/%s", (char*)realm, opt);
  836. turnFreeRedisReply(redisCommand(rc, s));
  837. turnFreeRedisReply(redisCommand(rc, "save"));
  838. ret = 0;
  839. }
  840. return ret;
  841. }
  842. static int redis_list_realm_options(u08bits *realm) {
  843. int ret = -1;
  844. donot_print_connection_success = 1;
  845. redisContext *rc = get_redis_connection();
  846. if(rc) {
  847. secrets_list_t keys;
  848. size_t isz = 0;
  849. init_secrets_list(&keys);
  850. redisReply *reply = NULL;
  851. {
  852. if(realm && realm[0]) {
  853. reply = (redisReply*)redisCommand(rc, "keys turn/realm/%s/*",realm);
  854. } else {
  855. reply = (redisReply*)redisCommand(rc, "keys turn/realm/*");
  856. }
  857. if(reply) {
  858. if (reply->type == REDIS_REPLY_ERROR)
  859. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  860. else if (reply->type != REDIS_REPLY_ARRAY) {
  861. if (reply->type != REDIS_REPLY_NIL)
  862. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  863. } else {
  864. size_t i;
  865. for (i = 0; i < reply->elements; ++i) {
  866. if(strstr(reply->element[i]->str,"/max-bps")||
  867. strstr(reply->element[i]->str,"/total-quota")||
  868. strstr(reply->element[i]->str,"/user-quota")) {
  869. add_to_secrets_list(&keys,reply->element[i]->str);
  870. }
  871. }
  872. }
  873. turnFreeRedisReply(reply);
  874. }
  875. }
  876. size_t offset = strlen("turn/realm/");
  877. for(isz=0;isz<keys.sz;++isz) {
  878. char *o = keys.secrets[isz];
  879. reply = (redisReply*)redisCommand(rc, "get %s",o);
  880. if(reply) {
  881. if (reply->type == REDIS_REPLY_ERROR)
  882. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  883. else if (reply->type != REDIS_REPLY_STRING) {
  884. if (reply->type != REDIS_REPLY_NIL)
  885. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  886. } else {
  887. printf("%s = %s\n",o+offset,reply->str);
  888. }
  889. turnFreeRedisReply(reply);
  890. }
  891. }
  892. clean_secrets_list(&keys);
  893. ret = 0;
  894. }
  895. return ret;
  896. }
  897. static void redis_auth_ping(void * rch) {
  898. donot_print_connection_success = 1;
  899. redisContext *rc = get_redis_connection();
  900. if(rc) {
  901. turnFreeRedisReply(redisCommand(rc, "keys turn/origin/*"));
  902. }
  903. if(rch)
  904. send_message_to_redis((redis_context_handle)rch, "publish", "__XXX__", "__YYY__");
  905. }
  906. static int redis_get_ip_list(const char *kind, ip_range_list_t * list)
  907. {
  908. int ret = -1;
  909. redisContext *rc = get_redis_connection();
  910. if (rc) {
  911. char statement[TURN_LONG_STRING_SIZE];
  912. const char* header = "turn/realm/";
  913. size_t header_len = strlen(header);
  914. snprintf(statement, sizeof(statement), "keys %s*/%s-peer-ip", header,kind);
  915. redisReply *reply = (redisReply*) redisCommand(rc, statement);
  916. if (reply) {
  917. secrets_list_t keys;
  918. size_t isz = 0;
  919. char s[257];
  920. init_secrets_list(&keys);
  921. if (reply->type == REDIS_REPLY_ERROR)
  922. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  923. else if (reply->type != REDIS_REPLY_ARRAY) {
  924. if (reply->type != REDIS_REPLY_NIL)
  925. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  926. } else {
  927. size_t i;
  928. for (i = 0; i < reply->elements; ++i) {
  929. add_to_secrets_list(&keys, reply->element[i]->str);
  930. }
  931. }
  932. for (isz = 0; isz < keys.sz; ++isz) {
  933. char *realm = NULL;
  934. snprintf(s, sizeof(s), "smembers %s", keys.secrets[isz]);
  935. redisReply *rget = (redisReply *) redisCommand(rc, s);
  936. char *ptr = ((char*)keys.secrets[isz])+header_len;
  937. char *sep = strstr(ptr, "/");
  938. if (sep) {
  939. *sep = 0;
  940. realm = ptr;
  941. }
  942. if (rget) {
  943. if (rget->type == REDIS_REPLY_ERROR) {
  944. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", rget->str);
  945. } else if (rget->type == REDIS_REPLY_STRING) {
  946. add_ip_list_range(rget->str, realm, list);
  947. } else if (rget->type != REDIS_REPLY_ARRAY) {
  948. if (rget->type != REDIS_REPLY_NIL)
  949. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type);
  950. } else {
  951. size_t i;
  952. for (i = 0; i < rget->elements; ++i) {
  953. add_ip_list_range(rget->element[i]->str, realm, list);
  954. }
  955. }
  956. turnFreeRedisReply(rget);
  957. }
  958. if(sep) {
  959. *sep='/';
  960. }
  961. }
  962. clean_secrets_list(&keys);
  963. turnFreeRedisReply(reply);
  964. ret = 0;
  965. }
  966. }
  967. return ret;
  968. }
  969. static void redis_reread_realms(secrets_list_t * realms_list) {
  970. redisContext *rc = get_redis_connection();
  971. if (rc) {
  972. redisReply *reply = (redisReply*) redisCommand(rc, "keys turn/origin/*");
  973. if (reply) {
  974. ur_string_map *o_to_realm_new = ur_string_map_create(turn_free_simple);
  975. secrets_list_t keys;
  976. init_secrets_list(&keys);
  977. size_t isz = 0;
  978. char s[1025];
  979. if (reply->type == REDIS_REPLY_ERROR)
  980. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str);
  981. else if (reply->type != REDIS_REPLY_ARRAY) {
  982. if (reply->type != REDIS_REPLY_NIL)
  983. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type);
  984. } else {
  985. size_t i;
  986. for (i = 0; i < reply->elements; ++i) {
  987. add_to_secrets_list(&keys, reply->element[i]->str);
  988. }
  989. }
  990. size_t offset = strlen("turn/origin/");
  991. for (isz = 0; isz < keys.sz; ++isz) {
  992. char *origin = keys.secrets[isz] + offset;
  993. snprintf(s, sizeof(s), "get %s", keys.secrets[isz]);
  994. redisReply *rget = (redisReply *) redisCommand(rc, s);
  995. if (rget) {
  996. if (rget->type == REDIS_REPLY_ERROR)
  997. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", rget->str);
  998. else if (rget->type != REDIS_REPLY_STRING) {
  999. if (rget->type != REDIS_REPLY_NIL)
  1000. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type);
  1001. } else {
  1002. get_realm(rget->str);
  1003. ur_string_map_value_type value = turn_strdup(rget->str);
  1004. ur_string_map_put(o_to_realm_new, (const ur_string_map_key_type) origin, value);
  1005. }
  1006. turnFreeRedisReply(rget);
  1007. }
  1008. }
  1009. clean_secrets_list(&keys);
  1010. update_o_to_realm(o_to_realm_new);
  1011. turnFreeRedisReply(reply);
  1012. }
  1013. {
  1014. size_t i = 0;
  1015. size_t rlsz = 0;
  1016. lock_realms();
  1017. rlsz = realms_list->sz;
  1018. unlock_realms();
  1019. for (i = 0; i<rlsz; ++i) {
  1020. char *realm = realms_list->secrets[i];
  1021. realm_params_t* rp = get_realm(realm);
  1022. unsigned long value = 0;
  1023. if(!set_redis_realm_opt(realm,"max-bps",&value)) {
  1024. lock_realms();
  1025. rp->options.perf_options.max_bps = turn_params.max_bps;
  1026. unlock_realms();
  1027. }
  1028. rp->options.perf_options.max_bps = (band_limit_t)value;
  1029. if(!set_redis_realm_opt(realm,"total-quota",&value)) {
  1030. lock_realms();
  1031. rp->options.perf_options.total_quota = turn_params.total_quota;
  1032. unlock_realms();
  1033. }
  1034. rp->options.perf_options.total_quota = (vint)value;
  1035. if(!set_redis_realm_opt(realm,"user-quota",&value)) {
  1036. lock_realms();
  1037. rp->options.perf_options.user_quota = turn_params.user_quota;
  1038. unlock_realms();
  1039. }
  1040. rp->options.perf_options.user_quota = (vint)value;
  1041. }
  1042. }
  1043. }
  1044. }
  1045. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  1046. static turn_dbdriver_t driver = {
  1047. &redis_get_auth_secrets,
  1048. &redis_get_user_key,
  1049. &redis_get_user_pwd,
  1050. &redis_set_user_key,
  1051. &redis_set_user_pwd,
  1052. &redis_del_user,
  1053. &redis_list_users,
  1054. &redis_show_secret,
  1055. &redis_del_secret,
  1056. &redis_set_secret,
  1057. &redis_add_origin,
  1058. &redis_del_origin,
  1059. &redis_list_origins,
  1060. &redis_set_realm_option_one,
  1061. &redis_list_realm_options,
  1062. &redis_auth_ping,
  1063. &redis_get_ip_list,
  1064. &redis_reread_realms,
  1065. &redis_set_oauth_key,
  1066. &redis_get_oauth_key,
  1067. &redis_del_oauth_key,
  1068. &redis_list_oauth_keys
  1069. };
  1070. turn_dbdriver_t * get_redis_dbdriver(void) {
  1071. return &driver;
  1072. }
  1073. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  1074. #else
  1075. turn_dbdriver_t * get_redis_dbdriver(void) {
  1076. return NULL;
  1077. }
  1078. #endif