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