00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include "../../sr_module.h"
00039 #include "../../dprint.h"
00040 #include "../../ut.h"
00041 #include "../../error.h"
00042 #include "../../mem/mem.h"
00043 #include "uridb_mod.h"
00044 #include "checks.h"
00045
00046 MODULE_VERSION
00047
00048
00049
00050
00051
00052
00053 #define URI_TABLE_VERSION 1
00054 #define SUBSCRIBER_TABLE_VERSION 6
00055
00056 static void destroy(void);
00057 static int child_init(int rank);
00058 static int mod_init(void);
00059
00060
00061 #define URI_TABLE "uri"
00062 #define URI_TABLE_LEN (sizeof(URI_TABLE) - 1)
00063
00064 #define USER_COL "username"
00065 #define USER_COL_LEN (sizeof(USER_COL) - 1)
00066
00067 #define DOMAIN_COL "domain"
00068 #define DOMAIN_COL_LEN (sizeof(DOMAIN_COL) - 1)
00069
00070 #define URI_USER_COL "uri_user"
00071 #define URI_USER_COL_LEN (sizeof(URI_USER_COL) - 1)
00072
00073 #define SUBSCRIBER_TABLE "subscriber"
00074 #define SUBSCRIBER_TABLE_LEN (sizeof(SUBSCRIBER_TABLE) - 1)
00075
00076
00077
00078
00079
00080 static str db_url = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
00081 str db_table = {SUBSCRIBER_TABLE, SUBSCRIBER_TABLE_LEN};
00082 str uridb_user_col = {USER_COL, USER_COL_LEN};
00083 str uridb_domain_col = {DOMAIN_COL, DOMAIN_COL_LEN};
00084 str uridb_uriuser_col = {URI_USER_COL, URI_USER_COL_LEN};
00085
00086 int use_uri_table = 0;
00087 int use_domain = 0;
00088
00089 static int fixup_exist(void** param, int param_no);
00090
00091
00092
00093
00094 static cmd_export_t cmds[] = {
00095 {"check_to", (cmd_function)check_to, 0, 0, 0,
00096 REQUEST_ROUTE},
00097 {"check_from", (cmd_function)check_from, 0, 0, 0,
00098 REQUEST_ROUTE},
00099 {"does_uri_exist", (cmd_function)does_uri_exist, 0, 0, fixup_exist,
00100 REQUEST_ROUTE|LOCAL_ROUTE},
00101 {0, 0, 0, 0, 0, 0}
00102 };
00103
00104
00105
00106
00107
00108 static param_export_t params[] = {
00109 {"db_url", STR_PARAM, &db_url.s },
00110 {"db_table", STR_PARAM, &db_table.s },
00111 {"user_column", STR_PARAM, &uridb_user_col.s },
00112 {"domain_column", STR_PARAM, &uridb_domain_col.s },
00113 {"uriuser_column", STR_PARAM, &uridb_uriuser_col.s },
00114 {"use_uri_table", INT_PARAM, &use_uri_table },
00115 {"use_domain", INT_PARAM, &use_domain },
00116 {0, 0, 0}
00117 };
00118
00119
00120
00121
00122
00123 struct module_exports exports = {
00124 "uri_db",
00125 DEFAULT_DLFLAGS,
00126 cmds,
00127 params,
00128 0,
00129 0 ,
00130 0,
00131 0,
00132 mod_init,
00133 0,
00134 destroy,
00135 child_init
00136 };
00137
00138
00139
00140
00141
00142 static int child_init(int rank)
00143 {
00144 if (db_url.len)
00145 return uridb_db_init(&db_url);
00146 else
00147 return 0;
00148 }
00149
00150
00151
00152
00153
00154 static int mod_init(void)
00155 {
00156 int ver;
00157 db_url.len = strlen(db_url.s);
00158
00159 if (db_url.len == 0) {
00160 if (use_uri_table) {
00161 LM_ERR("configuration error - no database URL, "
00162 "but use_uri_table is set!\n");
00163 return -1;
00164 }
00165 return 0;
00166 }
00167
00168 db_table.len = strlen(db_table.s);
00169 uridb_user_col.len = strlen(uridb_user_col.s);
00170 uridb_domain_col.len = strlen(uridb_domain_col.s);
00171 uridb_uriuser_col.len = strlen(uridb_uriuser_col.s);
00172
00173 if (uridb_db_bind(&db_url)) {
00174 LM_ERR("No database module found\n");
00175 return -1;
00176 }
00177
00178
00179 ver = uridb_db_ver(&db_url, &db_table);
00180 if (ver < 0) {
00181 LM_ERR("Error while querying table version\n");
00182 return -1;
00183 } else {
00184 if (use_uri_table) {
00185 if (ver != URI_TABLE_VERSION) {
00186 LM_ERR("Invalid table version of the uri table\n");
00187 return -1;
00188 }
00189 } else {
00190 if (ver != SUBSCRIBER_TABLE_VERSION) {
00191 LM_ERR("Invalid table version of the subscriber table\n");
00192 return -1;
00193 }
00194 }
00195 }
00196 return 0;
00197 }
00198
00199
00200 static void destroy(void)
00201 {
00202 uridb_db_close();
00203 }
00204
00205
00206 static int fixup_exist(void** param, int param_no)
00207 {
00208 if (db_url.len == 0) {
00209 LM_ERR("configuration error - does_uri_exist() called with no database URL!\n");
00210 return E_CFG;
00211 }
00212 return 0;
00213 }