static int dbops_foreach_func(struct sip_msg* m, char* handle, char* route_no) { int res; db_rec_t* rec; struct dbops_handle *a = (void *) handle; struct run_act_ctx ra_ctx; if ((long)route_no >= main_rt.idx) { BUG("invalid routing table number #%ld of %d\n", (long) route_no, main_rt.idx); return -1; } if (!main_rt.rlist[(long) route_no]) { WARN(MODULE_NAME": route not declared (hash:%ld)\n", (long) route_no); return -1; } res = check_query_opened(a, "for_each"); if (res < 0) return res; res = -1; a->cur_row_no = 0; for(rec = db_first(a->result); rec != NULL; rec = db_next(a->result), a->cur_row_no++) { /* exec the routing script */ init_run_actions_ctx(&ra_ctx); res = run_actions(&ra_ctx, main_rt.rlist[(long) route_no], m); if (res <= 0) break; } if (!rec) a->cur_row_no = -1; return res; }
/* * Load attributes from domain_attrs table */ int db_load_domain_attrs(domain_t* d) { int_str name, v; str avp_val; db_res_t* res; db_rec_t* rec; unsigned short flags; load_attrs_cmd->match[0].v.lstr = d->did; if (db_exec(&res, load_attrs_cmd) < 0) { ERR("Error while querying database\n"); return -1; } rec = db_first(res); while(rec) { if (rec->fld[0].flags & DB_NULL || rec->fld[1].flags & DB_NULL || rec->fld[3].flags & DB_NULL) { ERR("Skipping row containing NULL entries\n"); goto skip; } if ((rec->fld[3].v.int4 & SRDB_LOAD_SER) == 0) goto skip; /* Get AVP name */ name.s = rec->fld[0].v.lstr; /* Test for NULL value */ if (rec->fld[2].flags & DB_NULL) { avp_val.s = 0; avp_val.len = 0; } else { avp_val = rec->fld[2].v.lstr; } flags = AVP_CLASS_DOMAIN | AVP_NAME_STR; if (rec->fld[1].v.int4 == AVP_VAL_STR) { /* String AVP */ v.s = avp_val; flags |= AVP_VAL_STR; } else { /* Integer AVP */ str2int(&avp_val, (unsigned*)&v.n); } if (add_avp_list(&d->attrs, flags, name, v) < 0) { ERR("Error while adding domain attribute %.*s to domain %.*s, " "skipping\n", name.s.len, ZSW(name.s.s), d->did.len, ZSW(d->did.s)); } skip: rec = db_next(res); } db_res_free(res); return 0; }
void list() { char buf[4096]; key k; result res; size_t bytes; buf[0] = 0; k.data.count = 0; res = db_next(k); while (res.status == SUCCESS) { #ifdef PATCHED if (strlen(buf) + strlen((char*)res.rec.data->k.data.data) + 30 > sizeof(buf)) break; #endif item_details *d = (item_details *)res.rec.data->data.data; sprintf(buf, "%s%s (Price: %d.%02d, Count: %d)\n", buf, res.rec.data->k.data.data, (int)(d->price / 100), abs(d->price % 100), d->count); res = db_next(res.rec.data->k); } transmit(STDOUT, buf, strlen(buf), &bytes); }
static int do_seek(db_res_t* result, int *cur_row_no, int row_no) { if (row_no == *cur_row_no) return 0; if (row_no < *cur_row_no) *cur_row_no = -1; DEBUG(MODULE_NAME": do_seek: currowno:%d, rowno=%d\n", *cur_row_no, row_no); if (*cur_row_no < 0) { if (!db_first(result)) return -1; *cur_row_no = 0; } while (*cur_row_no < row_no) { if (!db_next(result)) { *cur_row_no = -1; return -1; } (*cur_row_no)++; } return 0; }
int sd_lookup(struct sip_msg* _msg, char* _index, char* _str2) { int i; str user_s, uid, did; db_res_t* res = NULL; db_rec_t* rec; /* init */ i = (int)(long)_index; /* Retrieve the owner of the record */ if (get_from_uid(&uid, _msg) < 0) { LOG(L_ERR, "sd_lookup: Unable to get user identity\n"); return -1; } /* Retrieve the called domain id */ if (get_to_did(&did, _msg) < 0) { LOG(L_ERR, "sd_lookup: Destination domain ID not known\n"); return -1; } tables[i].lookup_num->match[0].v.lstr = uid; tables[i].lookup_num->match[1].v.lstr = did; /* Get the called username */ if (parse_sip_msg_uri(_msg) < 0) { LOG(L_ERR, "sd_lookup: Error while parsing Request-URI\n"); goto err_badreq; } tables[i].lookup_num->match[2].v.lstr = _msg->parsed_uri.user; DBG("speeddial: Looking up (uid:%.*s,username:%.*s,did:%.*s)\n", uid.len, uid.s, _msg->parsed_uri.user.len, _msg->parsed_uri.user.s, did.len, did.s); if (db_exec(&res, tables[i].lookup_num) < 0) { ERR("speeddial: Error while executing database command\n"); goto err_server; } if (res == NULL) { DBG("speeddial: No SIP URI found for speeddial (num:%.*s, uid:%.*s," " did:%.*s)\n", _msg->parsed_uri.user.len, _msg->parsed_uri.user.s, uid.len, uid.s, did.len, did.s); return -1; } user_s.s = useruri_buf + 4; rec = db_first(res); while(rec) { if (rec->fld[0].flags & DB_NULL) goto skip; strncpy(user_s.s, rec->fld[0].v.lstr.s, rec->fld[0].v.lstr.len); user_s.len = rec->fld[0].v.lstr.len; user_s.s[user_s.len] = '\0'; goto out; skip: rec = db_next(res); } if (rec == NULL) { DBG("speeddial: No usable SIP URI found for (num:%.*s, uid:%.*s," " did:%.*s)\n", _msg->parsed_uri.user.len, _msg->parsed_uri.user.s, uid.len, uid.s, did.len, did.s); db_res_free(res); return -1; } out: /* check 'sip:' */ if(user_s.len<4 || strncmp(user_s.s, "sip:", 4)) { memcpy(useruri_buf, "sip:", 4); user_s.s -= 4; user_s.len += 4; } db_res_free(res); /* set the URI */ DBG("sd_lookup: URI of sd from R-URI [%s]\n", user_s.s); if(rewrite_uri(_msg, &user_s)<0) { LOG(L_ERR, "sd_lookup: Cannot replace the R-URI\n"); goto err_server; } return 1; err_server: if (slb.zreply(_msg, 500, "Server Internal Error") == -1) { LOG(L_ERR, "sd_lookup: Error while sending reply\n"); } return 0; err_badreq: if (slb.zreply(_msg, 400, "Bad Request") == -1) { LOG(L_ERR, "sd_lookup: Error while sending reply\n"); } return 0; }
static int sel_get_field(str* res, int *cur_row_no, int field_no, db_res_t* result) { /* return string in static buffer, I'm not sure if local static variable is OK, e.g. when comparing 2 selects */ int len; db_rec_t* rec; len = xlbuf_size-(xlbuf_tail-xlbuf); res->s = xlbuf_tail; res->len = 0; if (field_no == -2) { /* cur_row_no */ res->len = snprintf(res->s, len, "%d", *cur_row_no); } else if (field_no < 0) { /* count(*) | is empty */ int n; if (*cur_row_no < 0) { rec = db_first(result); if (rec) { *cur_row_no = 0; } } if (field_no == -3) { /* is_empty */ if (*cur_row_no >= 0) n = 0; else n = 1; } else { n = 0; if (*cur_row_no >= 0) { do { n++; rec = db_next(result); } while (rec); } *cur_row_no = -1; } res->len = snprintf(res->s, len, "%d", n); } else { if (*cur_row_no < 0) { ERR(MODULE_NAME": cursor points beyond data\n"); return -1; } if (field_no >= result->field_count) { ERR(MODULE_NAME": field (%d) does not exist, num fields: %d\n", field_no, result->field_count); return -1; } rec = result->cur_rec; if (!(rec->fld[field_no].flags & DB_NULL)) { switch (rec->fld[field_no].type) { case DB_INT: res->len = snprintf(res->s, len, "%d", rec->fld[field_no].v.int4); break; case DB_FLOAT: res->len = snprintf(res->s, len, "%f", rec->fld[field_no].v.flt); break; case DB_DOUBLE: res->len = snprintf(res->s, len, "%f", rec->fld[field_no].v.dbl); break; case DB_STR: res->len = snprintf(res->s, len, "%.*s", rec->fld[field_no].v.lstr.len, rec->fld[field_no].v.lstr.s); break; case DB_BLOB: res->len = snprintf(res->s, len, "%.*s", rec->fld[field_no].v.blob.len, rec->fld[field_no].v.blob.s); break; case DB_CSTR: res->len = snprintf(res->s, len, "%s", rec->fld[field_no].v.cstr); break; case DB_DATETIME: res->len = snprintf(res->s, len, "%u", (unsigned int) rec->fld[field_no].v.time); break; case DB_BITMAP: res->len = snprintf(res->s, len, "%u", (unsigned int) rec->fld[field_no].v.bitmap); break; default: break; } } } xlbuf_tail += res->len; return 0; }
/* module initialization function */ static int mod_init(void) { static str default_s = STR_STATIC_INIT("<default>"); db_cmd_t *cmd; db_res_t *res; db_rec_t *rec; db_fld_t cols[7]; DBG(MODULE_NAME": mod_init: initializing\n"); /* get default values from translation table */ if (connect_db() < 0) return E_CFG; DBG(MODULE_NAME": mod_init: getting default values from translation table\n"); if (exec_transl(&default_s, &cmd, &res) < 0) return E_CFG; rec = db_first(res); if (rec) { CSTRDUP(def_cfg_table, rec->fld[0]); CSTRDUP(def_cfg_table_group_name_field, rec->fld[1]); CSTRDUP(def_cfg_table_name_field, rec->fld[2]); CSTRDUP(def_cfg_table_value_field, rec->fld[3]); } // db_rec_free(rec); // ---> causes next db_cmd is aborted !!! db_res_free(res); db_cmd_free(cmd); DBG(MODULE_NAME": mod_init: default values: table='%s', group_name_field='%s', name_field='%s', value_field='%s'\n", def_cfg_table, def_cfg_table_group_name_field, def_cfg_table_name_field, def_cfg_table_value_field); /* get custom parameters from database */ DBG(MODULE_NAME": mod_init: getting custom parameters from '%s'\n", custom_tbl); memset(cols, 0, sizeof(cols)); cols[0].name = custom_tbl_group_name_fld; cols[0].type = DB_CSTR; cols[1].name = custom_tbl_name_fld; cols[1].type = DB_CSTR; cols[2].name = custom_tbl_value_type_fld; cols[2].type = DB_CSTR; cols[3].name = custom_tbl_min_value_fld; cols[3].type = DB_INT; cols[4].name = custom_tbl_max_value_fld; cols[4].type = DB_INT; cols[5].name = custom_tbl_decription_fld; cols[5].type = DB_CSTR; cmd = db_cmd(DB_GET, db_cntx, custom_tbl, cols, NULL, NULL); if (!cmd) { ERR(MODULE_NAME": Error preparing query '%s'\n", custom_tbl); return E_CFG; } if (db_exec(&res, cmd) < 0) { ERR(MODULE_NAME": Error executing query '%s'\n", custom_tbl); db_cmd_free(cmd); return E_CFG; } rec = db_first(res); while (rec) { DBG(MODULE_NAME": custom parameter '%s.%s' type:%s\n", rec->fld[0].v.cstr, rec->fld[1].v.cstr, rec->fld[2].v.cstr); if (((rec->fld[0].flags & DB_NULL) || strlen(rec->fld[0].v.cstr) == 0) || ((rec->fld[1].flags & DB_NULL) || strlen(rec->fld[1].v.cstr) == 0) || ((rec->fld[2].flags & DB_NULL) || strlen(rec->fld[2].v.cstr) == 0)) { ERR(MODULE_NAME": empty group_name,name or type value in table '%s'\n", custom_tbl); return E_CFG; } switch (rec->fld[2].v.cstr[0]) { case 'i': case 'I': if (cfg_declare_int(rec->fld[0].v.cstr, rec->fld[1].v.cstr, 0, rec->fld[3].v.int4, rec->fld[4].v.int4, rec->fld[5].v.cstr) < 0) { ERR(MODULE_NAME": Error declaring cfg int '%s.%s'\n", rec->fld[0].v.cstr, rec->fld[1].v.cstr); return E_CFG; } break; case 's': case 'S': if (cfg_declare_str(rec->fld[0].v.cstr, rec->fld[1].v.cstr, "", rec->fld[5].v.cstr) < 0) { ERR(MODULE_NAME": Error declaring cfg str '%s.%s'\n", rec->fld[0].v.cstr, rec->fld[1].v.cstr); return E_CFG; } break; default: ERR(MODULE_NAME": bad custom value type '%s'\n", rec->fld[2].v.cstr); return E_CFG; } rec = db_next(res); } db_res_free(res); db_cmd_free(cmd); /* register into config framework */ DBG(MODULE_NAME": mod_init: registering cfg callback\n"); if (cfg_register_ctx(&cfg_ctx, on_declare) < 0) { ERR(MODULE_NAME": failed to register cfg context\n"); return -1; } return 0; }
/* translate name using translation table, returns 0..not found, 1..success, -1..error */ static int find_cfg_var(str *group_name, char *def_name, db_res_t *transl_res) { db_rec_t *transl_rec; int ret = -1; DBG(MODULE_NAME": find_cfg_var('%.*s', '%s', ...)\n", group_name->len, group_name->s, def_name); transl_rec = db_first(transl_res); /* iterate through each candidate where cfg def may be found */ while (transl_rec) { static db_cmd_t* cmd; db_rec_t *rec; db_res_t *res; db_fld_t params[3], cols[2]; memset(cols, 0, sizeof(cols)); cols[0].name = GETCSTR(transl_rec->fld[3], def_cfg_table_value_field); cols[0].type = DB_NONE; memset(params, 0, sizeof(params)); params[0].name = GETCSTR(transl_rec->fld[1], def_cfg_table_group_name_field); params[0].type = DB_STR; params[0].op = DB_EQ; params[1].name = GETCSTR(transl_rec->fld[2], def_cfg_table_name_field); params[1].type = DB_CSTR; params[1].op = DB_EQ; DBG(MODULE_NAME": exec_transl: looking in '%s'\n", GETCSTR(transl_rec->fld[0], def_cfg_table)); cmd = db_cmd(DB_GET, db_cntx, GETCSTR(transl_rec->fld[0], def_cfg_table), cols, params, NULL); if (!cmd) { ERR(MODULE_NAME": Error preparing query '%s'\n", transl_tbl); return -1; } cmd->match[0].flags &= ~DB_NULL; cmd->match[0].v.lstr = *group_name; cmd->match[1].flags &= ~DB_NULL; cmd->match[1].v.cstr = def_name; // FIXME: proprietary code! db_setopt(cmd, "key", "bySerGroup"); db_setopt(cmd, "key_omit", 0); if (db_exec(&res, cmd) < 0) { ERR(MODULE_NAME": Error executing query '%s'\n", transl_tbl); db_cmd_free(cmd); return -1; } rec = db_first(res); if (rec) { /* var found in config table */ str def_name_s; def_name_s.s = def_name; def_name_s.len = strlen(def_name); DBG(MODULE_NAME": exec_transl: found record, type:%d\n", rec->fld[0].type); /* read and set cfg var */ switch (rec->fld[0].type) { case DB_STR: if (cfg_set_now(cfg_ctx, group_name, NULL /* group id */, &def_name_s, &rec->fld[0].v.lstr, CFG_VAR_STR) < 0) goto err; break; case DB_CSTR: if (cfg_set_now_string(cfg_ctx, group_name, NULL /* group id */, &def_name_s, rec->fld[0].v.cstr) < 0) goto err; break; case DB_INT: if (cfg_set_now_int(cfg_ctx, group_name, NULL /* group id */, &def_name_s, rec->fld[0].v.int4) < 0) goto err; break; default: ERR(MODULE_NAME": unexpected field type (%d), table:'%s', field:'%s'\n", rec->fld[0].type, GETCSTR(transl_rec->fld[0], def_cfg_table), GETCSTR(transl_rec->fld[3], def_cfg_table_value_field) ); goto err; } ret = 1; err: db_res_free(res); db_cmd_free(cmd); return ret; } db_res_free(res); db_cmd_free(cmd); transl_rec = db_next(transl_res); } return 0; }
void process_commands(enum CID cid) { cursor_t old_c; char * dbName; switch (cid) { case NOPE: puts("-- команда не распознана --"); break; case SET_NAME: if (db_ship()) { printf("Имя>"); strncpy(db_ship()->name,input_str(),64); show(); } break; case SET_YEAR: if (db_ship()) { printf("Год>"); db_ship()->year = strtoul( input_str(),NULL, 10); show(); } break; case COPY: db_copy(); break; case PASTE: db_paste(); show(); break; case ALL: old_c = db_get_cursor(); db_first(); while (db_get_cursor() != db_get_end()) { db_load(); if (!db_is_deleted()) show(); db_next(); } db_set_cursor(old_c); db_load(); break; case REFRESH: db_load(); case SHOW: show(); break; case NEW: db_new(); show(); break; case REMOVE: db_delete(); break; case SAVE: db_save(); break; case HELP: show_help(); break; case OPEN: printf("Файл>"); dbName = input_str(); db_open( strlen(dbName) == 0 ? "ships.db" : dbName); break; case CLOSE: db_close(); break; case FIRST: first: db_first(); while (db_get_cursor() != db_get_end()) { db_load(); if (!db_is_deleted()) { show(); return; } db_next(); } puts("-- в базе нет записей. --"); break; case NEXT: db_next(); while (db_get_cursor() != db_get_end()) { db_load(); if (!db_is_deleted()) { show(); return; } db_next(); } puts("-- больше записей нет. перемотка в начало. --"); goto first; case PREV: while (db_get_cursor() != db_get_begin()) { db_prev(); db_load(); if (!db_is_deleted()) { show(); return; } } puts("-- первая запись --"); goto first; break; default: puts("== команда не обработана =="); } }
/* * Create domain list from domain table */ int load_domains(domain_t** dest) { db_res_t* res = NULL; db_rec_t* rec; unsigned int flags; domain_t* d, *list; list = 0; if (db_exec(&res, load_domains_cmd) < 0) { ERR("Error while querying database\n"); return -1; } rec = db_first(res); while(rec) { /* Do not assume that the database server performs any constrain * checking (dbtext does not) and perform sanity checks here to * make sure that we only load good entried */ if (rec->fld[0].flags & DB_NULL || rec->fld[1].flags & DB_NULL || rec->fld[2].flags & DB_NULL) { ERR("Row with NULL column(s), skipping\n"); goto skip; } flags = rec->fld[2].v.int4; /* Skip entries that are disabled/scheduled for removal */ if (flags & SRDB_DISABLED) goto skip; /* Skip entries that are for serweb/ser-ctl only */ if (!(flags & SRDB_LOAD_SER)) goto skip; DBG("Processing entry (%.*s, %.*s, %u)\n", rec->fld[0].v.lstr.len, ZSW(rec->fld[0].v.lstr.s), rec->fld[1].v.lstr.len, ZSW(rec->fld[1].v.lstr.s), flags); d = domain_search(list, &rec->fld[0].v.lstr); if (d) { /* DID exists in the list, update it */ if (domain_add(d, &rec->fld[1].v.lstr, flags) < 0) goto error; } else { /* DID does not exist yet, create a new entry */ d = new_domain(&rec->fld[0].v.lstr, &rec->fld[1].v.lstr, flags); if (!d) goto error; d->next = list; list = d; } skip: rec = db_next(res); } db_res_free(res); if (load_domain_attrs) { d = list; while(d) { if (db_load_domain_attrs(d) < 0) goto error; d = d->next; } } *dest = list; return 0; error: if (res) db_res_free(res); free_domain_list(list); return 1; }
static inline int get_ha1(struct username* username, str* did, str* realm, authdb_table_info_t *table_info, char* ha1, db_res_t** res, db_rec_t** row) { str result; db_cmd_t *q = NULL; if (calc_ha1) { q = table_info->query_password; DBG("querying plain password\n"); } else { if (username->domain.len) { q = table_info->query_pass2; DBG("querying ha1b\n"); } else { q = table_info->query_pass; DBG("querying ha1\n"); } } q->match[0].v.lstr = username->user; q->match[1].v.lstr = *realm; if (use_did) q->match[2].v.lstr = *did; if (db_exec(res, q) < 0 ) { ERR("Error while querying database\n"); return -1; } if (*res) *row = db_first(*res); else *row = NULL; while (*row) { if (IS_NULL((*row)->fld[0]) || IS_NULL((*row)->fld[1])) { LOG(L_ERR, "auth_db:get_ha1: Credentials for '%.*s'@'%.*s' contain NULL value, skipping\n", username->user.len, ZSW(username->user.s), realm->len, ZSW(realm->s)); } else { if ((*row)->fld[1].v.int4 & DB_DISABLED) { /* disabled rows ignored */ } else { if ((*row)->fld[1].v.int4 & DB_LOAD_SER) { /* *row = i; */ break; } } } *row = db_next(*res); } if (!*row) { DBG("auth_db:get_ha1: Credentials for '%.*s'@'%.*s' not found\n", username->user.len, ZSW(username->user.s), realm->len, ZSW(realm->s)); return 1; } result.s = (*row)->fld[0].v.cstr; result.len = strlen(result.s); if (calc_ha1) { /* Only plaintext passwords are stored in database, * we have to calculate HA1 */ auth_api.calc_HA1(HA_MD5, &username->whole, realm, &result, 0, 0, ha1); DBG("auth_db:get_ha1: HA1 string calculated: %s\n", ha1); } else { memcpy(ha1, result.s, result.len); ha1[result.len] = '\0'; } return 0; }
/* this is a dirty work around to check the credentials of all users, * if the database query returned more then one result * * Fills res (which must be db_free'd afterwards if the call was succesfull) * returns 0 on success, 1 on no match (?) * and -1 on error (memory, db a.s.o). * WARNING: if -1 is returned res _must_ _not_ be freed (it's empty) * */ static inline int check_all_ha1(struct sip_msg* msg, struct hdr_field* hdr, dig_cred_t* dig, str* method, str* did, str* realm, authdb_table_info_t *table_info, db_res_t** res) { char ha1[256]; db_rec_t *row; str result; db_cmd_t *q; if (calc_ha1) { q = table_info->query_password; DBG("querying plain password\n"); } else { if (dig->username.domain.len) { q = table_info->query_pass2; DBG("querying ha1b\n"); } else { q = table_info->query_pass; DBG("querying ha1\n"); } } q->match[0].v.lstr = dig->username.user; if (dig->username.domain.len) q->match[1].v.lstr = dig->username.domain; else q->match[1].v.lstr = *realm; if (use_did) q->match[2].v.lstr = *did; if (db_exec(res, q) < 0 ) { ERR("Error while querying database\n"); } if (*res) row = db_first(*res); else row = NULL; while (row) { if (IS_NULL(row->fld[0]) || IS_NULL(row->fld[1])) { LOG(L_ERR, "auth_db:check_all_ha1: Credentials for '%.*s'@'%.*s' contain NULL value, skipping\n", dig->username.user.len, ZSW(dig->username.user.s), realm->len, ZSW(realm->s)); } else { if (row->fld[1].v.int4 & DB_DISABLED) { /* disabled rows ignored */ } else { if (row->fld[1].v.int4 & DB_LOAD_SER) { result.s = row->fld[0].v.cstr; result.len = strlen(result.s); if (calc_ha1) { /* Only plaintext passwords are stored in database, * we have to calculate HA1 */ auth_api.calc_HA1(HA_MD5, &(dig->username.whole), realm, &result, 0, 0, ha1); DBG("auth_db:check_all_ha1: HA1 string calculated: %s\n", ha1); } else { memcpy(ha1, result.s, result.len); ha1[result.len] = '\0'; } if (!check_response(dig, method, ha1)) { if (auth_api.post_auth(msg, hdr) == AUTHENTICATED) { generate_avps(*res, row); return 0; } } } } } row = db_next(*res); } if (!row) { DBG("auth_db:check_all_ha1: Credentials for '%.*s'@'%.*s' not found", dig->username.user.len, ZSW(dig->username.user.s), realm->len, ZSW(realm->s)); } return 1; }