/* * Check if uri belongs to a local user */ int does_uri_exist(struct sip_msg* _msg, char* _s1, char* _s2) { db_key_t keys[2]; db_val_t vals[2]; db_key_t cols[1]; db_res_t* res; if (parse_sip_msg_uri(_msg) < 0) { LOG(L_ERR, "does_uri_exist(): Error while parsing URI\n"); return -1; } if (use_uri_table) { if (db_use_table(db_handle, uri_table) < 0) { LOG(L_ERR, "does_uri_exist(): Error while trying to use uri table\n"); } keys[0] = uri_uriuser_col; keys[1] = uri_domain_col; cols[0] = uri_uriuser_col; } else { if (db_use_table(db_handle, subscriber_table) < 0) { LOG(L_ERR, "does_uri_exist(): Error while trying to use subscriber table\n"); } keys[0] = subscriber_user_col; keys[1] = subscriber_domain_col; cols[0] = subscriber_user_col; } VAL_TYPE(vals) = VAL_TYPE(vals + 1) = DB_STR; VAL_NULL(vals) = VAL_NULL(vals + 1) = 0; VAL_STR(vals) = _msg->parsed_uri.user; VAL_STR(vals + 1) = _msg->parsed_uri.host; if (db_query(db_handle, keys, 0, vals, cols, (use_domain ? 2 : 1), 1, 0, &res) < 0) { LOG(L_ERR, "does_uri_exist(): Error while querying database\n"); return -2; } if (RES_ROW_N(res) == 0) { DBG("does_uri_exit(): User in request uri does not exist\n"); db_free_query(db_handle, res); return -3; } else { DBG("does_uri_exit(): User in request uri does exist\n"); db_free_query(db_handle, res); return 1; } }
/** * Initialize childs */ static int child_init(int rank) { DBG("MSILO: init_child #%d / pid <%d>\n", rank, getpid()); db_con = db_init(db_url); if (!db_con) { LOG(L_ERR,"MSILO: child %d: Error while connecting database\n", rank); return -1; } else { db_use_table(db_con, db_table); DBG("MSILO: child %d: Database connection opened successfully\n", rank); } return 0; }
/* each child get a new connection to the database */ static int mod_child_init(int r) { DBG("PDT: mod_child_init #%d / pid <%d>\n", r, getpid()); db_con = db_init(db_url); if(!db_con) { LOG(L_ERR,"PDT: child %d: Error while connecting database\n",r); return -1; } else { db_use_table(db_con, db_table); DBG("PDT:child %d: Database connection opened successfuly\n",r); } return 0; }
int db_delete_urecord(urecord_t* _r) { char b[256]; db_key_t keys[2]; db_val_t vals[2]; char* dom; keys[0] = user_col; keys[1] = domain_col; vals[0].type = DB_STR; vals[0].nul = 0; vals[0].val.str_val.s = _r->aor.s; vals[0].val.str_val.len = _r->aor.len; if (use_domain) { dom = q_memchr(_r->aor.s, '@', _r->aor.len); if (!dom) { LOG(L_ERR, "db_delete_urecord(): You forgot to set modparam(\"registrar\", \"use_domain\", 1) in ser.cfg!\n"); vals[0].val.str_val.len = _r->aor.len; vals[1].type = DB_STR; vals[1].nul = 0; vals[1].val.str_val.s = _r->aor.s; vals[1].val.str_val.len = 0; } else { vals[0].val.str_val.len = dom - _r->aor.s; vals[1].type = DB_STR; vals[1].nul = 0; vals[1].val.str_val.s = dom + 1; vals[1].val.str_val.len = _r->aor.s + _r->aor.len - dom - 1; } } /* FIXME */ memcpy(b, _r->domain->s, _r->domain->len); b[_r->domain->len] = '\0'; db_use_table(db, b); if (db_delete(db, keys, 0, vals, (use_domain) ? (2) : (1)) < 0) { LOG(L_ERR, "db_delete_urecord(): Error while deleting from database\n"); return -1; } return 0; }
/* * Delete contact from the database */ int db_delete_ucontact(ucontact_t* _c) { char b[256]; char* dom; db_key_t keys[3]; db_val_t vals[3]; keys[0] = user_col; keys[1] = contact_col; keys[2] = domain_col; vals[0].type = DB_STR; vals[0].nul = 0; vals[0].val.str_val = *_c->aor; vals[1].type = DB_STR; vals[1].nul = 0; vals[1].val.str_val = _c->c; if (use_domain) { dom = q_memchr(_c->aor->s, '@', _c->aor->len); vals[0].val.str_val.len = dom - _c->aor->s; vals[2].type = DB_STR; vals[2].nul = 0; vals[2].val.str_val.s = dom + 1; vals[2].val.str_val.len = _c->aor->s + _c->aor->len - dom - 1; } /* FIXME */ memcpy(b, _c->domain->s, _c->domain->len); b[_c->domain->len] = '\0'; db_use_table(db, b); if (db_delete(db, keys, 0, vals, (use_domain) ? (3) : (2)) < 0) { LOG(L_ERR, "db_del_ucontact(): Error while deleting from database\n"); return -1; } return 0; }
int process_del_list(str* _d) { struct del_itm* p; char b[256]; db_key_t keys[2]; db_val_t vals[2]; keys[0] = user_col; keys[1] = contact_col; if (del_root) { /* FIXME */ memcpy(b, _d->s, _d->len); b[_d->len] = '\0'; db_use_table(db, b); VAL_TYPE(vals) = VAL_TYPE(vals + 1) = DB_STR; VAL_NULL(vals) = VAL_NULL(vals + 1) = 0; } while(del_root) { p = del_root; del_root = del_root->next; VAL_STR(vals).len = p->user_len; VAL_STR(vals).s = p->tail; VAL_STR(vals + 1).len = p->cont_len; VAL_STR(vals + 1).s = p->tail + p->user_len; if (db_delete(db, keys, 0, vals, 2) < 0) { LOG(L_ERR, "process_del_list(): Error while deleting from database\n"); return -1; } pkg_free(p); } return 0; }
/* * Store name of table that will be used by * subsequent database functions */ int db_postgres_use_table(db_con_t* _con, const str* _t) { return db_use_table(_con, _t); }
/* * Update contact in the database */ int db_update_ucontact(ucontact_t* _c) { char b[256]; char* dom; db_key_t keys1[3]; db_val_t vals1[3]; db_key_t keys2[7]; db_val_t vals2[7]; keys1[0] = user_col; keys1[1] = contact_col; keys1[2] = domain_col; keys2[0] = expires_col; keys2[1] = q_col; keys2[2] = callid_col; keys2[3] = cseq_col; keys2[4] = replicate_col; keys2[5] = state_col; keys2[6] = flags_col; vals1[0].type = DB_STR; vals1[0].nul = 0; vals1[0].val.str_val = *_c->aor; vals1[1].type = DB_STR; vals1[1].nul = 0; vals1[1].val.str_val = _c->c; vals2[0].type = DB_DATETIME; vals2[0].nul = 0; vals2[0].val.time_val = _c->expires; vals2[1].type = DB_DOUBLE; vals2[1].nul = 0; vals2[1].val.double_val = _c->q; vals2[2].type = DB_STR; vals2[2].nul = 0; vals2[2].val.str_val = _c->callid; vals2[3].type = DB_INT; vals2[3].nul = 0; vals2[3].val.int_val = _c->cseq; vals2[4].type = DB_INT; vals2[4].nul = 0; vals2[4].val.int_val = _c->replicate; vals2[5].type = DB_INT; vals2[5].nul = 0; if (_c->state < CS_ZOMBIE_N) { vals2[5].val.int_val = 0; } else { vals2[5].val.int_val = 1; } vals2[6].type = DB_INT; vals2[6].nul = 0; vals2[6].val.bitmap_val = _c->flags; if (use_domain) { dom = q_memchr(_c->aor->s, '@', _c->aor->len); vals1[0].val.str_val.len = dom - _c->aor->s; vals1[2].type = DB_STR; vals1[2].nul = 0; vals1[2].val.str_val.s = dom + 1; vals1[2].val.str_val.len = _c->aor->s + _c->aor->len - dom - 1; } /* FIXME */ memcpy(b, _c->domain->s, _c->domain->len); b[_c->domain->len] = '\0'; db_use_table(db, b); if (db_update(db, keys1, 0, vals1, keys2, vals2, (use_domain) ? (3) : (2), 7) < 0) { LOG(L_ERR, "db_upd_ucontact(): Error while updating database\n"); return -1; } return 0; }
/* * Insert contact into the database */ int db_insert_ucontact(ucontact_t* _c) { char b[256]; char* dom; db_key_t keys[10]; db_val_t vals[10]; keys[0] = user_col; keys[1] = contact_col; keys[2] = expires_col; keys[3] = q_col; keys[4] = callid_col; keys[5] = cseq_col; keys[6] = replicate_col; keys[7] = flags_col; keys[8] = state_col; keys[9] = domain_col; vals[0].type = DB_STR; vals[0].nul = 0; vals[0].val.str_val.s = _c->aor->s; vals[0].val.str_val.len = _c->aor->len; vals[1].type = DB_STR; vals[1].nul = 0; vals[1].val.str_val.s = _c->c.s; vals[1].val.str_val.len = _c->c.len; vals[2].type = DB_DATETIME; vals[2].nul = 0; vals[2].val.time_val = _c->expires; vals[3].type = DB_DOUBLE; vals[3].nul = 0; vals[3].val.double_val = _c->q; vals[4].type = DB_STR; vals[4].nul = 0; vals[4].val.str_val.s = _c->callid.s; vals[4].val.str_val.len = _c->callid.len; vals[5].type = DB_INT; vals[5].nul = 0; vals[5].val.int_val = _c->cseq; vals[6].type = DB_INT; vals[6].nul = 0; vals[6].val.int_val = _c->replicate; vals[7].type = DB_INT; vals[7].nul = 0; vals[7].val.bitmap_val = _c->flags; vals[8].type = DB_INT; vals[8].nul = 0; if (_c->state < CS_ZOMBIE_N) vals[8].val.int_val = 0; else vals[8].val.int_val = 1; if (use_domain) { dom = q_memchr(_c->aor->s, '@', _c->aor->len); vals[0].val.str_val.len = dom - _c->aor->s; vals[9].type = DB_STR; vals[9].nul = 0; vals[9].val.str_val.s = dom + 1; vals[9].val.str_val.len = _c->aor->s + _c->aor->len - dom - 1; } /* FIXME */ memcpy(b, _c->domain->s, _c->domain->len); b[_c->domain->len] = '\0'; db_use_table(db, b); if (db_insert(db, keys, vals, (use_domain) ? (10) : (9)) < 0) { LOG(L_ERR, "db_insert_ucontact(): Error while inserting contact\n"); return -1; } return 0; }
/** * init module function */ static int mod_init(void) { load_tm_f load_tm; #ifdef HAVE_IHTTP load_ih_f load_ih; #endif int i; DBG("XJAB:mod_init: initializing ...\n"); if(!jdomain) { LOG(L_ERR, "XJAB:mod_init: ERROR jdomain is NULL\n"); return -1; } /* import mysql functions */ if (bind_dbmod()) { LOG(L_ERR, "XJAB:mod_init: error - database module not found\n"); return -1; } db_con = (db_con_t**)shm_malloc(nrw*sizeof(db_con_t*)); if (db_con == NULL) { LOG(L_ERR, "XJAB:mod_init: Error while allocating db_con's\n"); return -1; } /* import the TM auto-loading function */ if ( !(load_tm=(load_tm_f)find_export("load_tm", NO_SCRIPT, 0))) { LOG(L_ERR, "ERROR: xjab:mod_init: can't import load_tm\n"); return -1; } /* let the auto-loading function load all TM stuff */ if (load_tm( &tmb )==-1) return -1; #ifdef HAVE_IHTTP /* import the iHTTP auto-loading function */ if ( !(load_ih=(load_ih_f)find_export("load_ih", IH_NO_SCRIPT_F, 0))) { LOG(L_ERR, "ERROR:xjab:mod_init: can't import load_ih\n"); return -1; } /* let the auto-loading function load all TM stuff */ if (load_ih( &ihb )==-1) return -1; #endif pipes = (int**)pkg_malloc(nrw*sizeof(int*)); if (pipes == NULL) { LOG(L_ERR, "XJAB:mod_init:Error while allocating pipes\n"); return -1; } for(i=0; i<nrw; i++) { pipes[i] = (int*)pkg_malloc(2*sizeof(int)); if (!pipes[i]) { LOG(L_ERR, "XJAB:mod_init: Error while allocating pipes\n"); return -1; } } for(i=0; i<nrw; i++) { db_con[i] = db_init(db_url); if (!db_con[i]) { LOG(L_ERR, "XJAB:mod_init: Error while connecting database\n"); return -1; } else { db_use_table(db_con[i], db_table); DBG("XJAB:mod_init: Database connection opened successfuly\n"); } } /** creating the pipees */ for(i=0;i<nrw;i++) { /* create the pipe*/ if (pipe(pipes[i])==-1) { LOG(L_ERR, "XJAB:mod_init: error - cannot create pipe!\n"); return -1; } DBG("XJAB:mod_init: pipe[%d] = <%d>-<%d>\n", i, pipes[i][0], pipes[i][1]); } if((jwl = xj_wlist_init(pipes,nrw,max_jobs,cache_time,sleep_time, delay_time)) == NULL) { LOG(L_ERR, "XJAB:mod_init: error initializing workers list\n"); return -1; } if(xj_wlist_set_aliases(jwl, jaliases, jdomain, proxy) < 0) { LOG(L_ERR, "XJAB:mod_init: error setting aliases and outbound proxy\n"); return -1; } DBG("XJAB:mod_init: initialized ...\n"); return 0; }
int process_ins_list(str* _d) { struct ins_itm* p; char b[256]; db_key_t keys[8]; db_val_t vals[8]; keys[0] = user_col; keys[1] = contact_col; keys[2] = expires_col; keys[3] = q_col; keys[4] = callid_col; keys[5] = cseq_col; keys[6] = replicate_col; keys[7] = state_col; if (ins_root) { /* FIXME */ memcpy(b, _d->s, _d->len); b[_d->len] = '\0'; db_use_table(db, b); VAL_TYPE(vals) = DB_STR; VAL_TYPE(vals + 1) = DB_STR; VAL_TYPE(vals + 2) = DB_DATETIME; VAL_TYPE(vals + 3) = DB_DOUBLE; VAL_TYPE(vals + 4) = DB_STR; VAL_TYPE(vals + 5) = DB_INT; VAL_TYPE(vals + 6) = DB_INT; VAL_TYPE(vals + 7) = DB_INT; VAL_NULL(vals) = 0; VAL_NULL(vals + 1) = 0; VAL_NULL(vals + 2) = 0; VAL_NULL(vals + 3) = 0; VAL_NULL(vals + 4) = 0; VAL_NULL(vals + 5) = 0; VAL_NULL(vals + 6) = 0; VAL_NULL(vals + 7) = 0; } while(ins_root) { p = ins_root; ins_root = ins_root->next; VAL_STR(vals).len = p->user->len; VAL_STR(vals).s = p->user->s; VAL_STR(vals + 1).len = p->cont->len; VAL_STR(vals + 1).s = p->cont->s; VAL_TIME(vals + 2) = p->expires; VAL_DOUBLE(vals + 3) = p->q; VAL_STR(vals + 4).len = p->cid_len; VAL_STR(vals + 4).s = p->callid; VAL_INT(vals + 5) = p->cseq; VAL_INT(vals + 6) = p->replicate; VAL_INT(vals + 7) = p->state; if (db_insert(db, keys, vals, 8) < 0) { LOG(L_ERR, "process_ins_list(): Error while inserting into database\n"); return -1; } pkg_free(p); } return 0; }
/* * Store name of table that will be used by * subsequent database functions */ int db_oracle_use_table(db_con_t* _h, const str* _t) { return db_use_table(_h, _t); }
/* * Store name of table that will be used by * subsequent database functions */ int db_unixodbc_use_table(db_con_t* _h, const str* _t) { return db_use_table(_h, _t); }
/* * Check if a header field contains the same username * as digest credentials */ static inline int check_username(struct sip_msg* _m, str* _uri) { struct hdr_field* h; auth_body_t* c; struct sip_uri puri; db_key_t keys[3]; db_val_t vals[3]; db_key_t cols[1]; db_res_t* res; if (!_uri) { LOG(L_ERR, "check_username(): Bad parameter\n"); return -1; } /* Get authorized digest credentials */ get_authorized_cred(_m->authorization, &h); if (!h) { get_authorized_cred(_m->proxy_auth, &h); if (!h) { LOG(L_ERR, "check_username(): No authorized credentials found (error in scripts)\n"); LOG(L_ERR, "check_username(): Call {www,proxy}_authorize before calling check_* function !\n"); return -2; } } c = (auth_body_t*)(h->parsed); /* Parse To/From URI */ if (parse_uri(_uri->s, _uri->len, &puri) < 0) { LOG(L_ERR, "check_username(): Error while parsing URI\n"); return -3; } /* Make sure that the URI contains username */ if (!puri.user.len) { LOG(L_ERR, "check_username(): Username not found in URI\n"); return -4; } /* If use_uri_table is set, use URI table to determine if Digest username * and To/From username match. URI table is a table enumerating all allowed * usernames for a single, thus a user can have several different usernames * (which are different from digest username and it will still match) */ if (use_uri_table) { /* Make sure that From/To URI domain and digest realm are equal * FIXME: Should we move this outside this condition and make it general ? */ if (puri.host.len != c->digest.realm.len) { LOG(L_ERR, "check_username(): Digest realm and URI domain do not match\n"); return -5; } if (strncasecmp(puri.host.s, c->digest.realm.s, puri.host.len) != 0) { DBG("check_username(): Digest realm and URI domain do not match\n"); return -6; } if (db_use_table(db_handle, uri_table) < 0) { LOG(L_ERR, "check_username(): Error while trying to use uri table\n"); } keys[0] = uri_user_col; keys[1] = uri_domain_col; keys[2] = uri_uriuser_col; cols[0] = uri_user_col; VAL_TYPE(vals) = VAL_TYPE(vals + 1) = VAL_TYPE(vals + 2) = DB_STR; VAL_NULL(vals) = VAL_NULL(vals + 1) = VAL_NULL(vals + 2) = 0; VAL_STR(vals) = c->digest.username.user; VAL_STR(vals + 1) = c->digest.realm; VAL_STR(vals + 2) = puri.user; if (db_query(db_handle, keys, 0, vals, cols, 3, 1, 0, &res) < 0) { LOG(L_ERR, "check_username(): Error while querying database\n"); return -7; } /* If the previous function returns at least one row, it means * there is an entry for given digest username and URI username * and thus this combination is allowed and the function will match */ if (RES_ROW_N(res) == 0) { DBG("check_username(): From/To user '%.*s' is spoofed\n", puri.user.len, ZSW(puri.user.s)); db_free_query(db_handle, res); return -8; } else { DBG("check_username(): From/To user '%.*s' and auth user match\n", puri.user.len, ZSW(puri.user.s)); db_free_query(db_handle, res); return 1; } } else { /* URI table not used, simply compare digest username and From/To * username, the comparison is case insensitive */ if (puri.user.len == c->digest.username.user.len) { if (!strncasecmp(puri.user.s, c->digest.username.user.s, puri.user.len)) { DBG("check_username(): Digest username and URI username match\n"); return 1; } } DBG("check_username(): Digest username and URI username do NOT match\n"); return -9; } }
/** * Store the name of table that will be used by subsequent database functions * \param _h database handle * \param _t table name * \return zero on success, negative value on failure */ int db_sqlite_use_table(db_con_t* _h, const str* _t) { return db_use_table(_h, _t); }
/** * init module function */ static int mod_init(void) { db_res_t* db_res = NULL; code_t i, code; dc_t* cell; DBG("PDT: initializing...\n"); if(hs_two_pow<0) { LOG(L_ERR, "PDT: mod_init: hash_size_two_pow must be" " positive and less than %d\n", MAX_HSIZE_TWO_POW); return -1; } if(code_terminator>9 || code_terminator<0) { LOG(L_ERR, "PDT: mod_init: code_terminator must be a digit\n"); return -1; } if(!prefix_valid()) return -1; next_code = (code_t*)shm_malloc(sizeof(code_t)); if(!next_code) { LOG(L_ERR, "PDT: mod_init: cannot allocate next_code!\n"); return -1; } if(lock_init(&l) == 0) { shm_free(next_code); LOG(L_ERR, "PDT: mod_init: cannot init the lock\n"); return -1; } if(register_fifo_cmd(get_domainprefix, "get_domainprefix", 0)<0) { LOG(L_ERR, "PDT: mod_init: cannot register fifo command 'get_domaincode'\n"); goto error1; } /* binding to mysql module */ if(bind_dbmod()) { LOG(L_ERR, "PDT: mod_init: Database module not found\n"); goto error1; } /* open a connection with the database */ db_con = db_init(db_url); if(!db_con) { LOG(L_ERR, "PDT: mod_init: Error while connecting to database\n"); goto error1; } else { db_use_table(db_con, db_table); DBG("PDT: mod_init: Database connection opened successfully\n"); } /* init hashes in share memory */ if( (hash = init_double_hash(hs_two_pow)) == NULL) { LOG(L_ERR, "PDT: mod_init: hash could not be allocated\n"); goto error2; } /* loading all information from database */ *next_code = 0; if(db_query(db_con, NULL, NULL, NULL, NULL, 0, 0, "code", &db_res)==0) { for(i=0; i<RES_ROW_N(db_res); i++) { code = RES_ROWS(db_res)[i].values[0].val.int_val; if (!code_valid(code)) { LOG(L_ERR, "PDT: mod_init: existing code contains the terminator\n"); goto error; } if (*next_code < code) *next_code = code; cell=new_cell( (char*)(RES_ROWS(db_res)[i].values[1].val.string_val), code); if(cell == NULL) goto error; if(add_to_double_hash(hash, cell)<0) { LOG(L_ERR, "PDT: mod_init: could not add information from database" " into shared-memory hashes\n"); goto error; } } // clear up here //print_hash(hash->dhash, hash->hash_size); //print_hash(hash->chash, hash->hash_size); (*next_code)++; if (*next_code < start_range) *next_code = start_range; *next_code = apply_correction(*next_code); DBG("PDT: mod_init: next_code:%d\n", *next_code); /* free up the space allocated for response */ if(db_free_query(db_con, db_res)<0) { LOG(L_ERR, "PDT: mod_init: error when freeing" " up the response space\n"); } } else { /* query to database failed */ LOG(L_ERR, "PDT: mod_init: query to database failed\n"); goto error; } db_close(db_con); /* janakj - close the connection */ /* success code */ return 0; error: free_double_hash(hash); error2: db_close(db_con); error1: shm_free(next_code); lock_destroy(&l); return -1; }
int dbt_use_table(db1_con_t* _h, const str* _t) { return db_use_table(_h, _t); }
/** * Store the name of table that will be used by subsequent database functions * \param _h database handle * \param _t table name * \return zero on success, negative value on failure */ int erlang_srdb1_use_table(db1_con_t* _h, const str* _t) { LM_DBG("erlang_srdb1_use_table %.*s\n", _t->len, _t->s); return db_use_table(_h, _t); }