modules/userblacklist/db.c
Go to the documentation of this file.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 #include "db.h"
00031 #include "db_userblacklist.h"
00032
00033 #include "../../db/db.h"
00034 #include "../../mem/mem.h"
00035 #include "../../ut.h"
00036 #include "../../trie/dtrie.h"
00037
00038
00039
00040
00041
00042
00043 int db_build_userbl_tree(const str *username, const str *domain, const str *table, struct dtrie_node_t *root, int use_domain)
00044 {
00045 db_key_t columns[2] = { &userblacklist_prefix_col, &userblacklist_whitelist_col };
00046 db_key_t key[2] = { &userblacklist_username_col, &userblacklist_domain_col };
00047
00048 db_val_t val[2];
00049 VAL_TYPE(val) = VAL_TYPE(val + 1) = DB_STR;
00050 VAL_NULL(val) = VAL_NULL(val + 1) = 0;
00051 VAL_STR(val).s = username->s;
00052 VAL_STR(val).len = username->len;
00053 VAL_STR(val + 1).s = domain->s;
00054 VAL_STR(val + 1).len = domain->len;
00055
00056 db_res_t *res;
00057 int i;
00058 int n = 0;
00059 void *nodeflags;
00060
00061 if (userblacklist_dbf.use_table(userblacklist_dbh, table) < 0) {
00062 LM_ERR("cannot use table '%.*s'.\n", table->len, table->s);
00063 return -1;
00064 }
00065 if (userblacklist_dbf.query(userblacklist_dbh, key, 0, val, columns, (!use_domain) ? (1) : (2), 2, 0, &res) < 0) {
00066 LM_ERR("error while executing query.\n");
00067 return -1;
00068 }
00069
00070 dtrie_clear(root, NULL, 10);
00071
00072 if (RES_COL_N(res) > 1) {
00073 for(i = 0; i < RES_ROW_N(res); i++) {
00074 if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
00075 if ((RES_ROWS(res)[i].values[0].type == DB_STRING) &&
00076 (RES_ROWS(res)[i].values[1].type == DB_INT)) {
00077
00078
00079
00080
00081 if (RES_ROWS(res)[i].values[1].val.int_val == 0) nodeflags=(void *)MARK_BLACKLIST;
00082 if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val, strlen(RES_ROWS(res)[i].values[0].val.string_val),
00083 nodeflags, 10) < 0) LM_ERR("could not insert values into trie.\n");
00084 n++;
00085 }
00086 else {
00087 LM_ERR("got invalid result type from query.\n");
00088 }
00089 }
00090 }
00091 }
00092 userblacklist_dbf.free_result(userblacklist_dbh, res);
00093
00094 return n;
00095 }
00096
00097
00098
00099
00100
00101
00102 int db_reload_source(const str *table, struct dtrie_node_t *root)
00103 {
00104 db_key_t columns[2] = { &globalblacklist_prefix_col, &globalblacklist_whitelist_col };
00105 db_res_t *res;
00106 int i;
00107 int n = 0;
00108 void *nodeflags;
00109
00110 if (userblacklist_dbf.use_table(userblacklist_dbh, table) < 0) {
00111 LM_ERR("cannot use table '%.*s'.\n", table->len, table->s);
00112 return -1;
00113 }
00114 if (userblacklist_dbf.query(userblacklist_dbh, NULL, NULL, NULL, columns, 0, 2, NULL, &res) < 0) {
00115 LM_ERR("error while executing query.\n");
00116 return -1;
00117 }
00118
00119 dtrie_clear(root, NULL, 10);
00120
00121 if (RES_COL_N(res) > 1) {
00122 for(i = 0; i < RES_ROW_N(res); i++) {
00123 if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
00124 if ((RES_ROWS(res)[i].values[0].type == DB_STRING) &&
00125 (RES_ROWS(res)[i].values[1].type == DB_INT)) {
00126
00127
00128
00129
00130 if (RES_ROWS(res)[i].values[1].val.int_val == 0) nodeflags=(void *) MARK_BLACKLIST;
00131 else nodeflags=(void *)MARK_WHITELIST;
00132 if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val, strlen(RES_ROWS(res)[i].values[0].val.string_val),
00133 nodeflags, 10) < 0) LM_ERR("could not insert values into trie.\n");
00134 n++;
00135 }
00136 else {
00137 LM_ERR("got invalid result type from query.\n");
00138 }
00139 }
00140 }
00141 }
00142 userblacklist_dbf.free_result(userblacklist_dbh, res);
00143
00144 return n;
00145 }