uridb_mod.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * $Id$
  3. *
  4. * Various URI related functions
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * -------
  26. * 2003-03-11: New module interface (janakj)
  27. * 2003-03-16: flags export parameter added (janakj)
  28. * 2003-03-19 replaces all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  29. * 2003-04-05: default_uri #define used (jiri)
  30. * 2004-03-20: has_totag introduced (jiri)
  31. * 2004-06-07 updated to the new DB api (andrei)
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "../../sr_module.h"
  37. #include "../../dprint.h"
  38. #include "../../ut.h"
  39. #include "../../error.h"
  40. #include "../../mem/mem.h"
  41. #include "uridb_mod.h"
  42. #include "checks.h"
  43. MODULE_VERSION
  44. /*
  45. * Version of domain table required by the module,
  46. * increment this value if you change the table in
  47. * an backwards incompatible way
  48. */
  49. #define URI_TABLE_VERSION 1
  50. #define SUBSCRIBER_TABLE_VERSION 6
  51. static void destroy(void); /* Module destroy function */
  52. static int child_init(int rank); /* Per-child initialization function */
  53. static int mod_init(void); /* Module initialization function */
  54. #define URI_TABLE "uri"
  55. #define USER_COL "username"
  56. #define DOMAIN_COL "domain"
  57. #define URI_USER_COL "uri_user"
  58. #define SUBSCRIBER_TABLE "subscriber"
  59. /*
  60. * Module parameter variables
  61. */
  62. static str db_url = str_init(DEFAULT_RODB_URL);
  63. str db_table = str_init(SUBSCRIBER_TABLE);
  64. str uridb_user_col = str_init(USER_COL);
  65. str uridb_domain_col = str_init(DOMAIN_COL);
  66. str uridb_uriuser_col = str_init(URI_USER_COL);
  67. int use_uri_table = 0; /* Should uri table be used */
  68. int use_domain = 0; /* Should does_uri_exist honor the domain part ? */
  69. static int fixup_exist(void** param, int param_no);
  70. /*
  71. * Exported functions
  72. */
  73. static cmd_export_t cmds[] = {
  74. {"check_to", (cmd_function)check_to, 0, 0, 0,
  75. REQUEST_ROUTE},
  76. {"check_from", (cmd_function)check_from, 0, 0, 0,
  77. REQUEST_ROUTE},
  78. {"does_uri_exist", (cmd_function)does_uri_exist, 0, 0, fixup_exist,
  79. REQUEST_ROUTE|LOCAL_ROUTE},
  80. {0, 0, 0, 0, 0, 0}
  81. };
  82. /*
  83. * Exported parameters
  84. */
  85. static param_export_t params[] = {
  86. {"db_url", PARAM_STR, &db_url },
  87. {"db_table", PARAM_STR, &db_table },
  88. {"user_column", PARAM_STR, &uridb_user_col },
  89. {"domain_column", PARAM_STR, &uridb_domain_col },
  90. {"uriuser_column", PARAM_STR, &uridb_uriuser_col },
  91. {"use_uri_table", INT_PARAM, &use_uri_table },
  92. {"use_domain", INT_PARAM, &use_domain },
  93. {0, 0, 0}
  94. };
  95. /*
  96. * Module interface
  97. */
  98. struct module_exports exports = {
  99. "uri_db",
  100. DEFAULT_DLFLAGS, /* dlopen flags */
  101. cmds, /* Exported functions */
  102. params, /* Exported parameters */
  103. 0, /* exported statistics */
  104. 0 , /* exported MI functions */
  105. 0, /* exported pseudo-variables */
  106. 0, /* extra processes */
  107. mod_init, /* module initialization function */
  108. 0, /* response function */
  109. destroy, /* destroy function */
  110. child_init /* child initialization function */
  111. };
  112. /**
  113. * Module initialization function callee in each child separately
  114. */
  115. static int child_init(int rank)
  116. {
  117. if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  118. return 0; /* do nothing for the main process */
  119. if (db_url.len)
  120. return uridb_db_init(&db_url);
  121. else
  122. return 0;
  123. }
  124. /**
  125. * Module initialization function that is called before the main process forks
  126. */
  127. static int mod_init(void)
  128. {
  129. int ver;
  130. if (db_url.len == 0) {
  131. if (use_uri_table) {
  132. LM_ERR("configuration error - no database URL, "
  133. "but use_uri_table is set!\n");
  134. return -1;
  135. }
  136. return 0;
  137. }
  138. if (uridb_db_bind(&db_url)) {
  139. LM_ERR("No database module found\n");
  140. return -1;
  141. }
  142. /* Check table version */
  143. ver = uridb_db_ver(&db_url, &db_table);
  144. if (ver < 0) {
  145. LM_ERR("Error while querying table version\n");
  146. return -1;
  147. } else {
  148. if (use_uri_table) {
  149. if (ver != URI_TABLE_VERSION) {
  150. LM_ERR("Invalid table version of the uri table\n");
  151. return -1;
  152. }
  153. } else {
  154. if (ver != SUBSCRIBER_TABLE_VERSION) {
  155. LM_ERR("Invalid table version of the subscriber table\n");
  156. return -1;
  157. }
  158. }
  159. }
  160. return 0;
  161. }
  162. static void destroy(void)
  163. {
  164. uridb_db_close();
  165. }
  166. static int fixup_exist(void** param, int param_no)
  167. {
  168. if (db_url.len == 0) {
  169. LM_ERR("configuration error - does_uri_exist() called with no database URL!\n");
  170. return E_CFG;
  171. }
  172. return 0;
  173. }