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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/*
* $Id$
*
* 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:
* --------
* 2004-06-06 updated to the new DB api (andrei)
*/
#include "../../mem/shm_mem.h"
#include "../../lib/srdb1/db.h"
#include "../../dprint.h"
#include "../../str.h"
#include "cpl_db.h"
#define TABLE_VERSION 1
static db1_con_t* db_hdl=0;
static db_func_t cpl_dbf;
str cpl_username_col = str_init("username");
str cpl_domain_col = str_init("domain");
str cpl_xml_col = str_init("cpl_xml");
str cpl_bin_col = str_init("cpl_bin");
int cpl_db_bind(const str* db_url, const str *db_table)
{
if (db_bind_mod(db_url, &cpl_dbf )) {
LM_CRIT("cannot bind to database module! "
"Did you forget to load a database module ?\n");
return -1;
}
/* CPL module uses all database functions */
if (!DB_CAPABILITY(cpl_dbf, DB_CAP_ALL)) {
LM_CRIT("Database modules does not "
"provide all functions needed by cpl-c module\n");
return -1;
}
if ( cpl_db_init( db_url, db_table) )
return -1;
if(db_check_table_version(&cpl_dbf, db_hdl, db_table, TABLE_VERSION) < 0) {
LM_ERR("error during table version check.\n");
cpl_db_close();
return -1;
}
cpl_db_close();
return 0;
}
int cpl_db_init(const str* db_url, const str* db_table)
{
if (cpl_dbf.init==0){
LM_CRIT("BUG - unbound database module\n");
return -1;
}
db_hdl=cpl_dbf.init(db_url);
if (db_hdl==0){
LM_CRIT("cannot initialize database connection\n");
return -1;
}
if (cpl_dbf.use_table(db_hdl, db_table)<0) {
LM_CRIT("cannot select table \"%.*s\"\n",db_table->len, db_table->s);
cpl_db_close();
return -1;
}
return 0;
}
void cpl_db_close(void)
{
if (db_hdl && cpl_dbf.close){
cpl_dbf.close(db_hdl);
db_hdl=0;
}
}
/* gets from database the cpl script in binary format; the returned script is
* allocated in shared memory
* Returns: 1 - success
* -1 - error
*/
int get_user_script(str *username, str *domain, str *script, str* key)
{
db_key_t keys_cmp[2];
db_key_t keys_ret[1];
db_val_t vals[2];
db1_res_t *res = NULL;
int n;
keys_cmp[0] = &cpl_username_col;
keys_cmp[1] = &cpl_domain_col;
keys_ret[0] = key;
LM_DBG("fetching script for user <%.*s>\n",
username->len,username->s);
vals[0].type = DB1_STR;
vals[0].nul = 0;
vals[0].val.str_val = *username;
n = 1;
if (domain) {
vals[1].type = DB1_STR;
vals[1].nul = 0;
vals[1].val.str_val = *domain;
n++;
}
if (cpl_dbf.query(db_hdl, keys_cmp, 0, vals, keys_ret, n, 1, NULL, &res)
< 0){
LM_ERR("db_query failed\n");
goto error;
}
if (res->n==0) {
LM_DBG("user <%.*s> not found in db -> probably "
"he has no script\n",username->len, username->s);
script->s = 0;
script->len = 0;
} else {
if (res->rows[0].values[0].nul) {
LM_DBG("user <%.*s> has a NULL script\n",
username->len, username->s);
script->s = 0;
script->len = 0;
} else {
LM_DBG("we got the script len=%d\n",
res->rows[0].values[0].val.blob_val.len);
script->len = res->rows[0].values[0].val.blob_val.len;
script->s = shm_malloc( script->len );
if (!script->s) {
LM_ERR("no free sh_mem\n");
goto error;
}
memcpy( script->s, res->rows[0].values[0].val.blob_val.s,
script->len);
}
}
cpl_dbf.free_result( db_hdl, res);
return 1;
error:
if (res)
cpl_dbf.free_result( db_hdl, res);
script->s = 0;
script->len = 0;
return -1;
}
/* inserts into database a cpl script in XML format(xml) along with its binary
* format (bin)
* Returns: 1 - success
* -1 - error
*/
int write_to_db(str *username, str *domain, str *xml, str *bin)
{
db_key_t keys[4];
db_val_t vals[4];
db1_res_t *res = NULL;
int n;
/* lets see if the user is already in database */
keys[2] = &cpl_username_col;
vals[2].type = DB1_STR;
vals[2].nul = 0;
vals[2].val.str_val = *username;
n = 1;
if (domain) {
keys[3] = &cpl_domain_col;
vals[3].type = DB1_STR;
vals[3].nul = 0;
vals[3].val.str_val = *domain;
n++;
}
if (cpl_dbf.query(db_hdl, keys+2, 0, vals+2, keys+2, n, 1, NULL, &res)<0) {
LM_ERR("db_query failed\n");
goto error;
}
if (res->n>1) {
LM_ERR("Inconsistent CPL database:"
" %d records for user %.*s\n",res->n,username->len,username->s);
goto error;
}
/* cpl text */
keys[0] = &cpl_xml_col;
vals[0].type = DB1_BLOB;
vals[0].nul = 0;
vals[0].val.blob_val.s = xml->s;
vals[0].val.blob_val.len = xml->len;
n++;
/* cpl bin */
keys[1] = &cpl_bin_col;
vals[1].type = DB1_BLOB;
vals[1].nul = 0;
vals[1].val.blob_val.s = bin->s;
vals[1].val.blob_val.len = bin->len;
n++;
/* insert or update ? */
if (res->n==0) {
LM_DBG("no user %.*s in CPL database->insert\n",
username->len,username->s);
if (cpl_dbf.insert(db_hdl, keys, vals, n) < 0) {
LM_ERR("insert failed !\n");
goto error;
}
} else {
LM_DBG("user %.*s already in CPL database ->"
" update\n",username->len,username->s);
if (cpl_dbf.update(db_hdl, keys+2, 0, vals+2, keys, vals, n-2, 2) < 0) {
LM_ERR("update failed !\n");
goto error;
}
}
return 1;
error:
return -1;
}
/* delete from database the entity record for a given user - if a user has no
* script, he will be removed completely from db; users without script are not
* allowed into db ;-)
* Returns: 1 - success
* -1 - error
*/
int rmv_from_db(str *username, str *domain)
{
db_key_t keys[2];
db_val_t vals[2];
int n;
/* username */
keys[0] = &cpl_username_col;
vals[0].type = DB1_STR;
vals[0].nul = 0;
vals[0].val.str_val = *username;
n = 1;
if (domain) {
keys[1] = &cpl_domain_col;
vals[1].type = DB1_STR;
vals[1].nul = 0;
vals[1].val.str_val = *domain;
n++;
}
if (cpl_dbf.delete(db_hdl, keys, NULL, vals, n) < 0) {
LM_ERR("failed to delete script for "
"user \"%.*s\"\n",username->len,username->s);
return -1;
}
return 1;
}
|