modules_k/userblacklist/db.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2007 1&1 Internet AG
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00030 #include "db.h"
00031 #include "db_userblacklist.h"
00032 
00033 #include "../../lib/srdb1/db.h"
00034 #include "../../mem/mem.h"
00035 #include "../../ut.h"
00036 #include "../../lib/trie/dtrie.h"
00037 
00038 
00039 
00045 extern int match_mode;
00046 
00047 int db_build_userbl_tree(const str *username, const str *domain, const str *table, struct dtrie_node_t *root, int use_domain)
00048 {
00049         db_key_t columns[2] = { &userblacklist_prefix_col, &userblacklist_whitelist_col };
00050         db_key_t key[2] = { &userblacklist_username_col, &userblacklist_domain_col };
00051 
00052         db_val_t val[2];
00053         db1_res_t *res;
00054         int i;
00055         int n = 0;
00056         void *nodeflags;
00057         VAL_TYPE(val) = VAL_TYPE(val + 1) = DB1_STR;
00058         VAL_NULL(val) = VAL_NULL(val + 1) = 0;
00059         VAL_STR(val).s = username->s;
00060         VAL_STR(val).len = username->len;
00061         VAL_STR(val + 1).s = domain->s;
00062         VAL_STR(val + 1).len = domain->len;
00063 
00064         
00065         if (userblacklist_dbf.use_table(userblacklist_dbh, table) < 0) {
00066                 LM_ERR("cannot use table '%.*s'.\n", table->len, table->s);
00067                 return -1;
00068         }
00069         if (userblacklist_dbf.query(userblacklist_dbh, key, 0, val, columns, (!use_domain) ? (1) : (2), 2, 0, &res) < 0) {
00070                 LM_ERR("error while executing query.\n");
00071                 return -1;
00072         }
00073 
00074         dtrie_clear(root, NULL, match_mode);
00075 
00076         if (RES_COL_N(res) > 1) {
00077                 for(i = 0; i < RES_ROW_N(res); i++) {
00078                         if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
00079                                 if ((RES_ROWS(res)[i].values[0].type == DB1_STRING) &&
00080                                         (RES_ROWS(res)[i].values[1].type == DB1_INT)) {
00081 
00082                                         /* LM_DBG("insert into tree prefix %s, whitelist %d",
00083                                                 RES_ROWS(res)[i].values[0].val.string_val,
00084                                                 RES_ROWS(res)[i].values[1].val.int_val); */
00085                                         if (RES_ROWS(res)[i].values[1].val.int_val == 0) {
00086                                                 nodeflags=(void *)MARK_BLACKLIST;
00087                                         } else {
00088                                                 nodeflags=(void *)MARK_WHITELIST;
00089                                         }
00090                                         if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val, strlen(RES_ROWS(res)[i].values[0].val.string_val),
00091                                                 nodeflags, match_mode) < 0) LM_ERR("could not insert values into trie.\n");
00092                                         n++;
00093                                 }
00094                                 else {
00095                                         LM_ERR("got invalid result type from query.\n");
00096                                 }
00097                         }
00098                 }
00099         }
00100         userblacklist_dbf.free_result(userblacklist_dbh, res);
00101 
00102         return n;
00103 }
00104 
00105 
00110 int db_reload_source(const str *table, struct dtrie_node_t *root)
00111 {
00112         db_key_t columns[2] = { &globalblacklist_prefix_col, &globalblacklist_whitelist_col };
00113         db1_res_t *res;
00114         int i;
00115         int n = 0;
00116         void *nodeflags;
00117         
00118         if (userblacklist_dbf.use_table(userblacklist_dbh, table) < 0) {
00119                 LM_ERR("cannot use table '%.*s'.\n", table->len, table->s);
00120                 return -1;
00121         }
00122         if (userblacklist_dbf.query(userblacklist_dbh, NULL, NULL, NULL, columns, 0, 2, NULL, &res) < 0) {
00123                 LM_ERR("error while executing query.\n");
00124                 return -1;
00125         }
00126 
00127         dtrie_clear(root, NULL, match_mode);
00128 
00129         if (RES_COL_N(res) > 1) {
00130                 for(i = 0; i < RES_ROW_N(res); i++) {
00131                         if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
00132                                 if ((RES_ROWS(res)[i].values[0].type == DB1_STRING) &&
00133                                         (RES_ROWS(res)[i].values[1].type == DB1_INT)) {
00134 
00135                                         /* LM_DBG("insert into tree prefix %s, whitelist %d",
00136                                                 RES_ROWS(res)[i].values[0].val.string_val,
00137                                                 RES_ROWS(res)[i].values[1].val.int_val); */
00138                                         if (RES_ROWS(res)[i].values[1].val.int_val == 0) nodeflags=(void *) MARK_BLACKLIST;
00139                                         else nodeflags=(void *)MARK_WHITELIST;
00140                                         if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val, strlen(RES_ROWS(res)[i].values[0].val.string_val),
00141                                                 nodeflags, match_mode) < 0) LM_ERR("could not insert values into trie.\n");
00142                                         n++;
00143                                 }
00144                                 else {
00145                                         LM_ERR("got invalid result type from query.\n");
00146                                 }
00147                         }
00148                 }
00149         }
00150         userblacklist_dbf.free_result(userblacklist_dbh, res);
00151 
00152         return n;
00153 }