src/modules/userblacklist/db.c
c64dbbd1
 /*
  * Copyright (C) 2007 1&1 Internet AG
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
c64dbbd1
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
c64dbbd1
  * 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
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
c64dbbd1
  * 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.
  *
dce52ed6
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
1f8e65ee
  */
 
 /*!
  * \file
  * \brief USERBLACKLIST :: database access
  * \ingroup userblacklist
  * - Module: \ref userblacklist
c64dbbd1
  */
 
 #include "db.h"
9230c04c
 #include "db_userblacklist.h"
c64dbbd1
 
865aa2a6
 #include "../../lib/srdb1/db.h"
cf83221d
 #include "../../core/mem/mem.h"
 #include "../../core/ut.h"
f1a4d217
 #include "../../lib/trie/dtrie.h"
c64dbbd1
 
 
2f8f8e58
 
c64dbbd1
 /**
  * Builds a d-tree using database entries.
  * \return negative on failure, postive on success, indicating the number of d-tree entries
  */
2f8f8e58
 
 extern int match_mode;
 
dce52ed6
 int db_build_userbl_tree(const str *username, const str *domain,
 		const str *dbtable, struct dtrie_node_t *root, int use_domain)
c64dbbd1
 {
9230c04c
 	db_key_t columns[2] = { &userblacklist_prefix_col, &userblacklist_whitelist_col };
 	db_key_t key[2] = { &userblacklist_username_col, &userblacklist_domain_col };
c64dbbd1
 
 	db_val_t val[2];
275899b1
 	db1_res_t *res;
 	int i;
 	int n = 0;
 	void *nodeflags;
865aa2a6
 	VAL_TYPE(val) = VAL_TYPE(val + 1) = DB1_STR;
c64dbbd1
 	VAL_NULL(val) = VAL_NULL(val + 1) = 0;
 	VAL_STR(val).s = username->s;
 	VAL_STR(val).len = username->len;
 	VAL_STR(val + 1).s = domain->s;
 	VAL_STR(val + 1).len = domain->len;
 
dce52ed6
 	if (userblacklist_dbf.use_table(userblacklist_dbh, dbtable) < 0) {
 		LM_ERR("cannot use db table '%.*s'.\n", dbtable->len, dbtable->s);
c64dbbd1
 		return -1;
 	}
dce52ed6
 	if (userblacklist_dbf.query(userblacklist_dbh, key, 0, val, columns,
 				(!use_domain) ? (1) : (2), 2, 0, &res) < 0) {
 		LM_ERR("error while executing query on db table '%.*s'\n",
 				dbtable->len, dbtable->s);
c64dbbd1
 		return -1;
 	}
 
2f8f8e58
 	dtrie_clear(root, NULL, match_mode);
c64dbbd1
 
 	if (RES_COL_N(res) > 1) {
 		for(i = 0; i < RES_ROW_N(res); i++) {
 			if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
865aa2a6
 				if ((RES_ROWS(res)[i].values[0].type == DB1_STRING) &&
 					(RES_ROWS(res)[i].values[1].type == DB1_INT)) {
c64dbbd1
 
 					/* LM_DBG("insert into tree prefix %s, whitelist %d",
 						RES_ROWS(res)[i].values[0].val.string_val,
 						RES_ROWS(res)[i].values[1].val.int_val); */
9566b04b
 					if (RES_ROWS(res)[i].values[1].val.int_val == 0) {
 						nodeflags=(void *)MARK_BLACKLIST;
 					} else {
 						nodeflags=(void *)MARK_WHITELIST;
 					}
dce52ed6
 
 					if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val,
 								strlen(RES_ROWS(res)[i].values[0].val.string_val),
 								nodeflags, match_mode) < 0)
 						LM_ERR("could not insert values into trie.\n");
 
c64dbbd1
 					n++;
 				}
 				else {
 					LM_ERR("got invalid result type from query.\n");
 				}
 			}
 		}
 	}
9230c04c
 	userblacklist_dbf.free_result(userblacklist_dbh, res);
c64dbbd1
 
 	return n;
 }
 
 
 /**
  * Rebuild d-tree using database entries
  * \return negative on failure, positive on success, indicating the number of d-tree entries
  */
dce52ed6
 int db_reload_source(const str *dbtable, struct dtrie_node_t *root)
c64dbbd1
 {
9230c04c
 	db_key_t columns[2] = { &globalblacklist_prefix_col, &globalblacklist_whitelist_col };
865aa2a6
 	db1_res_t *res;
c64dbbd1
 	int i;
 	int n = 0;
dc10e758
 	void *nodeflags;
dce52ed6
 
 	if (userblacklist_dbf.use_table(userblacklist_dbh, dbtable) < 0) {
 		LM_ERR("cannot use db table '%.*s'\n", dbtable->len, dbtable->s);
c64dbbd1
 		return -1;
 	}
dce52ed6
 	if (userblacklist_dbf.query(userblacklist_dbh, NULL, NULL, NULL,
 				columns, 0, 2, NULL, &res) < 0) {
 		LM_ERR("error while executing query on db table '%.*s'\n",
 				dbtable->len, dbtable->s);
c64dbbd1
 		return -1;
 	}
 
2f8f8e58
 	dtrie_clear(root, NULL, match_mode);
c64dbbd1
 
 	if (RES_COL_N(res) > 1) {
 		for(i = 0; i < RES_ROW_N(res); i++) {
 			if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
865aa2a6
 				if ((RES_ROWS(res)[i].values[0].type == DB1_STRING) &&
 					(RES_ROWS(res)[i].values[1].type == DB1_INT)) {
c64dbbd1
 
 					/* LM_DBG("insert into tree prefix %s, whitelist %d",
 						RES_ROWS(res)[i].values[0].val.string_val,
 						RES_ROWS(res)[i].values[1].val.int_val); */
dce52ed6
 					if (RES_ROWS(res)[i].values[1].val.int_val == 0) {
 						nodeflags=(void *)MARK_BLACKLIST;
 					} else {
 						nodeflags=(void *)MARK_WHITELIST;
 					}
 
 					if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val,
 								strlen(RES_ROWS(res)[i].values[0].val.string_val),
 								nodeflags, match_mode) < 0)
 						LM_ERR("could not insert values into trie.\n");
 
c64dbbd1
 					n++;
 				}
 				else {
 					LM_ERR("got invalid result type from query.\n");
 				}
 			}
 		}
 	}
9230c04c
 	userblacklist_dbf.free_result(userblacklist_dbh, res);
c64dbbd1
 
 	return n;
 }