userdb.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (C) 2011, 2012, 2013 Citrix Systems
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #ifndef __USERDB__
  31. #define __USERDB__
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "hiredis_libevent2.h"
  35. #include "ns_turn_maps.h"
  36. #include "ns_turn_server.h"
  37. #include "ns_turn_utils.h"
  38. #include "apputils.h"
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. //////////// REALM //////////////
  43. struct _realm_status_t;
  44. typedef struct _realm_status_t realm_status_t;
  45. struct _realm_params_t;
  46. typedef struct _realm_params_t realm_params_t;
  47. struct _realm_status_t {
  48. vint total_current_allocs;
  49. ur_string_map *alloc_counters;
  50. };
  51. struct _realm_params_t {
  52. int is_default_realm;
  53. realm_options_t options;
  54. realm_status_t status;
  55. };
  56. void lock_realms(void);
  57. void unlock_realms(void);
  58. void update_o_to_realm(ur_string_map *o_to_realm_new);
  59. //////////// USER DB //////////////////////////////
  60. struct auth_message {
  61. turnserver_id id;
  62. turn_credential_type ct;
  63. int in_oauth;
  64. int out_oauth;
  65. int max_session_time;
  66. uint8_t username[STUN_MAX_USERNAME_SIZE + 1];
  67. uint8_t realm[STUN_MAX_REALM_SIZE + 1];
  68. hmackey_t key;
  69. password_t pwd;
  70. get_username_resume_cb resume_func;
  71. ioa_net_data in_buffer;
  72. uint64_t ctxkey;
  73. int success;
  74. };
  75. enum _TURN_USERDB_TYPE {
  76. #if !defined(TURN_NO_SQLITE)
  77. TURN_USERDB_TYPE_UNKNOWN = -1,
  78. TURN_USERDB_TYPE_SQLITE = 0
  79. #else
  80. TURN_USERDB_TYPE_UNKNOWN = 0
  81. #endif
  82. #if !defined(TURN_NO_PQ)
  83. ,
  84. TURN_USERDB_TYPE_PQ
  85. #endif
  86. #if !defined(TURN_NO_MYSQL)
  87. ,
  88. TURN_USERDB_TYPE_MYSQL
  89. #endif
  90. #if !defined(TURN_NO_MONGO)
  91. ,
  92. TURN_USERDB_TYPE_MONGO
  93. #endif
  94. ,
  95. TURN_USERDB_TYPE_REDIS
  96. };
  97. typedef enum _TURN_USERDB_TYPE TURN_USERDB_TYPE;
  98. enum _TURNADMIN_COMMAND_TYPE {
  99. TA_COMMAND_UNKNOWN,
  100. TA_PRINT_KEY,
  101. TA_UPDATE_USER,
  102. TA_DELETE_USER,
  103. TA_LIST_USERS,
  104. TA_SET_SECRET,
  105. TA_SHOW_SECRET,
  106. TA_DEL_SECRET,
  107. TA_ADD_ORIGIN,
  108. TA_DEL_ORIGIN,
  109. TA_LIST_ORIGINS,
  110. TA_SET_REALM_OPTION,
  111. TA_LIST_REALM_OPTIONS
  112. };
  113. typedef enum _TURNADMIN_COMMAND_TYPE TURNADMIN_COMMAND_TYPE;
  114. /////////// SHARED SECRETS //////////////////
  115. struct _secrets_list {
  116. char **secrets;
  117. size_t sz;
  118. };
  119. typedef struct _secrets_list secrets_list_t;
  120. /////////// USERS PARAM /////////////////////
  121. #define TURN_LONG_STRING_SIZE (1025)
  122. typedef struct _redis_stats_db_t {
  123. char connection_string[TURN_LONG_STRING_SIZE];
  124. char connection_string_sanitized[TURN_LONG_STRING_SIZE];
  125. } redis_stats_db_t;
  126. typedef struct _ram_users_db_t {
  127. size_t users_number;
  128. ur_string_map *static_accounts;
  129. secrets_list_t static_auth_secrets;
  130. } ram_users_db_t;
  131. typedef struct _persistent_users_db_t {
  132. char userdb[TURN_LONG_STRING_SIZE];
  133. char userdb_sanitized[TURN_LONG_STRING_SIZE];
  134. } persistent_users_db_t;
  135. typedef struct _default_users_db_t {
  136. TURN_USERDB_TYPE userdb_type;
  137. persistent_users_db_t persistent_users_db;
  138. ram_users_db_t ram_db;
  139. } default_users_db_t;
  140. /////////////////////////////////////////////
  141. realm_params_t *get_realm(char *name);
  142. void set_default_realm_name(char *realm);
  143. int change_total_quota(char *realm, int value);
  144. int change_user_quota(char *realm, int value);
  145. /////////////////////////////////////////////
  146. void init_secrets_list(secrets_list_t *sl);
  147. void init_dynamic_ip_lists(void);
  148. void update_white_and_black_lists(void);
  149. void clean_secrets_list(secrets_list_t *sl);
  150. size_t get_secrets_list_size(secrets_list_t *sl);
  151. const char *get_secrets_list_elem(secrets_list_t *sl, size_t i);
  152. void add_to_secrets_list(secrets_list_t *sl, const char *elem);
  153. /////////// USER DB CHECK //////////////////
  154. int get_user_key(int in_oauth, int *out_oauth, int *max_session_time, uint8_t *uname, uint8_t *realm, hmackey_t key,
  155. ioa_network_buffer_handle nbh);
  156. uint8_t *start_user_check(turnserver_id id, turn_credential_type ct, int in_oauth, int *out_oauth, uint8_t *uname,
  157. uint8_t *realm, get_username_resume_cb resume, ioa_net_data *in_buffer, uint64_t ctxkey,
  158. int *postpone_reply);
  159. int check_new_allocation_quota(uint8_t *username, int oauth, uint8_t *realm);
  160. void release_allocation_quota(uint8_t *username, int oauth, uint8_t *realm);
  161. /////////// Handle user DB /////////////////
  162. #if defined(DB_TEST)
  163. void run_db_test(void);
  164. #endif
  165. void auth_ping(redis_context_handle rch);
  166. void reread_realms(void);
  167. int add_static_user_account(char *user);
  168. int adminuser(uint8_t *user, uint8_t *realm, uint8_t *pwd, uint8_t *secret, uint8_t *origin, TURNADMIN_COMMAND_TYPE ct,
  169. perf_options_t *po, int is_admin);
  170. int add_ip_list_range(const char *range, const char *realm, ip_range_list_t *list);
  171. int check_ip_list_range(const char *range);
  172. ip_range_list_t *get_ip_list(const char *kind);
  173. void ip_list_free(ip_range_list_t *l);
  174. ///////////// Redis //////////////////////
  175. #if !defined(TURN_NO_HIREDIS)
  176. redis_context_handle get_redis_async_connection(struct event_base *base, redis_stats_db_t *redis_stats_db,
  177. int delete_keys);
  178. #endif
  179. ////////////////////////////////////////////
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif
  184. /// __USERDB__///