/*! * \brief Attempt to access a database variable and increment it * * \note Only if the user defined db-family in alarmreceiver.conf * * The alarmreceiver app will write statistics to a few variables * in this family if it is defined. If the new key doesn't exist in the * family, then create it and set its value to 1. * * \param key A database key to increment * \return Nothing */ static void database_increment(char *key) { unsigned v; char value[16]; if (ast_strlen_zero(db_family)) { return; /* If not defined, don't do anything */ } if (ast_db_get(db_family, key, value, sizeof(value) - 1)) { ast_verb(4, "AlarmReceiver: Creating database entry %s and setting to 1\n", key); /* Guess we have to create it */ ast_db_put(db_family, key, "1"); return; } sscanf(value, "%30u", &v); v++; ast_verb(4, "AlarmReceiver: New value for %s: %u\n", key, v); snprintf(value, sizeof(value), "%u", v); if (ast_db_put(db_family, key, value)) { ast_verb(4, "AlarmReceiver: database_increment write error\n"); } return; }
static char *handle_cli_database_get(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { int res; char tmp[MAX_DB_FIELD]; switch (cmd) { case CLI_INIT: e->command = "database get"; e->usage = "Usage: database get <family> <key>\n" " Retrieves an entry in the Asterisk database for a given\n" " family and key.\n"; return NULL; case CLI_GENERATE: return NULL; } if (a->argc != 4) return CLI_SHOWUSAGE; res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp)); if (res) { ast_cli(a->fd, "Database entry not found.\n"); } else { ast_cli(a->fd, "Value: %s\n", tmp); } return CLI_SUCCESS; }
static void database_increment( char *key ) { int res = 0; unsigned v; char value[16]; if (ast_strlen_zero(db_family)) return; /* If not defined, don't do anything */ res = ast_db_get(db_family, key, value, sizeof(value) - 1); if(res){ if(option_verbose >= 4) ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: Creating database entry %s and setting to 1\n", key); /* Guess we have to create it */ res = ast_db_put(db_family, key, "1"); return; } sscanf(value, "%u", &v); v++; if(option_verbose >= 4) ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: New value for %s: %u\n", key, v); snprintf(value, sizeof(value), "%u", v); res = ast_db_put(db_family, key, value); if((res)&&(option_verbose >= 4)) ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: database_increment write error"); return; }
static int lookupblacklist_exec (struct ast_channel *chan, void *data) { char blacklist[1]; struct ast_module_user *u; int bl = 0; int priority_jump = 0; static int dep_warning = 0; u = ast_module_user_add(chan); if (!dep_warning) { dep_warning = 1; ast_log(LOG_WARNING, "LookupBlacklist is deprecated. Please use ${BLACKLIST()} instead.\n"); } if (!ast_strlen_zero(data)) { if (strchr(data, 'j')) priority_jump = 1; } if (chan->cid.cid_num) { if (!ast_db_get("blacklist", chan->cid.cid_num, blacklist, sizeof (blacklist))) { if (option_verbose > 2) ast_log(LOG_NOTICE, "Blacklisted number %s found\n",chan->cid.cid_num); bl = 1; } } if (chan->cid.cid_name) { if (!ast_db_get("blacklist", chan->cid.cid_name, blacklist, sizeof (blacklist))) { if (option_verbose > 2) ast_log (LOG_NOTICE,"Blacklisted name \"%s\" found\n",chan->cid.cid_name); bl = 1; } } if (bl) { if (priority_jump || ast_opt_priority_jumping) ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101); pbx_builtin_setvar_helper(chan, "LOOKUPBLSTATUS", "FOUND"); } else pbx_builtin_setvar_helper(chan, "LOOKUPBLSTATUS", "NOTFOUND"); ast_module_user_remove(u); return 0; }
static int blacklist_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) { char blacklist[1]; int bl = 0; if (chan->cid.cid_num) { if (!ast_db_get("blacklist", chan->cid.cid_num, blacklist, sizeof (blacklist))) bl = 1; } if (chan->cid.cid_name) { if (!ast_db_get("blacklist", chan->cid.cid_name, blacklist, sizeof (blacklist))) bl = 1; } snprintf(buf, len, "%d", bl); return 0; }
static int blacklist_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) { char blacklist[1]; int bl = 0; if (chan->caller.id.number.valid && chan->caller.id.number.str) { if (!ast_db_get("blacklist", chan->caller.id.number.str, blacklist, sizeof (blacklist))) bl = 1; } if (chan->caller.id.name.valid && chan->caller.id.name.str) { if (!ast_db_get("blacklist", chan->caller.id.name.str, blacklist, sizeof (blacklist))) bl = 1; } snprintf(buf, len, "%d", bl); return 0; }
static int blacklist_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) { char blacklist[1]; int bl = 0; if (!chan) { ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); return -1; } if (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str) { if (!ast_db_get("blacklist", ast_channel_caller(chan)->id.number.str, blacklist, sizeof (blacklist))) bl = 1; } if (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str) { if (!ast_db_get("blacklist", ast_channel_caller(chan)->id.name.str, blacklist, sizeof (blacklist))) bl = 1; } snprintf(buf, len, "%d", bl); return 0; }
static int database_get(int fd, int argc, char *argv[]) { int res; char tmp[256]; if (argc != 4) return RESULT_SHOWUSAGE; res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp)); if (res) { ast_cli(fd, "Database entry not found.\n"); } else { ast_cli(fd, "Value: %s\n", tmp); } return RESULT_SUCCESS; }
static int sorcery_astdb_delete(const struct ast_sorcery *sorcery, void *data, void *object) { const char *prefix = data; char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2]; char value[2]; snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object)); if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value))) { return -1; } return ast_db_del(family, ast_sorcery_object_get_id(object)); }
static int sorcery_astdb_update(const struct ast_sorcery *sorcery, void *data, void *object) { const char *prefix = data; char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2], value[2]; snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object)); /* It is okay for the value to be truncated, we are only checking that it exists */ if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value))) { return -1; } /* The only difference between update and create is that for update the object must already exist */ return sorcery_astdb_create(sorcery, data, object); }
static int lookupcidname_exec (struct ast_channel *chan, void *data) { char dbname[64]; struct localuser *u; LOCAL_USER_ADD (u); if (chan->cid.cid_num) { if (!ast_db_get ("cidname", chan->cid.cid_num, dbname, sizeof (dbname))) { ast_set_callerid (chan, NULL, dbname, NULL); if (option_verbose > 2) ast_verbose (VERBOSE_PREFIX_3 "Changed Caller*ID name to %s\n", dbname); } } LOCAL_USER_REMOVE (u); return 0; }
static int manager_dbget(struct mansession *s, const struct message *m) { const char *id = astman_get_header(m,"ActionID"); char idText[256] = ""; const char *family = astman_get_header(m, "Family"); const char *key = astman_get_header(m, "Key"); char tmp[MAX_DB_FIELD]; int res; if (ast_strlen_zero(family)) { astman_send_error(s, m, "No family specified."); return 0; } if (ast_strlen_zero(key)) { astman_send_error(s, m, "No key specified."); return 0; } if (!ast_strlen_zero(id)) snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id); res = ast_db_get(family, key, tmp, sizeof(tmp)); if (res) { astman_send_error(s, m, "Database entry not found"); } else { astman_send_ack(s, m, "Result will follow"); astman_append(s, "Event: DBGetResponse\r\n" "Family: %s\r\n" "Key: %s\r\n" "Val: %s\r\n" "%s" "\r\n", family, key, tmp, idText); astman_append(s, "Event: DBGetComplete\r\n" "%s" "\r\n", idText); } return 0; }
static int lookupcidname_exec (struct ast_channel *chan, void *data) { char dbname[64]; struct localuser *u; static int dep_warning = 0; LOCAL_USER_ADD (u); if (!dep_warning) { dep_warning = 1; ast_log(LOG_WARNING, "LookupCIDName is deprecated. Please use ${DB(cidname/${CALLERID(num)})} instead.\n"); } if (chan->cid.cid_num) { if (!ast_db_get ("cidname", chan->cid.cid_num, dbname, sizeof (dbname))) { ast_set_callerid (chan, NULL, dbname, NULL); if (option_verbose > 2) ast_verbose (VERBOSE_PREFIX_3 "Changed Caller*ID name to %s\n", dbname); } } LOCAL_USER_REMOVE (u); return 0; }
static int auth_exec(struct ast_channel *chan, void *data) { int res=0; int retries; struct localuser *u; char password[256]=""; char passwd[256]; char *opts; char *prompt; if (!data || ast_strlen_zero(data)) { ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n"); return -1; } LOCAL_USER_ADD(u); if (chan->_state != AST_STATE_UP) { res = ast_answer(chan); if (res) { LOCAL_USER_REMOVE(u); return -1; } } strncpy(password, data, sizeof(password) - 1); opts=strchr(password, '|'); if (opts) { *opts = 0; opts++; } else opts = ""; /* Start asking for password */ prompt = "agent-pass"; for (retries = 0; retries < 3; retries++) { res = ast_app_getdata(chan, prompt, passwd, sizeof(passwd) - 2, 0); if (res < 0) break; res = 0; if (password[0] == '/') { if (strchr(opts, 'd')) { char tmp[256]; /* Compare against a database key */ if (!ast_db_get(password + 1, passwd, tmp, sizeof(tmp))) { /* It's a good password */ if (strchr(opts, 'r')) { ast_db_del(password + 1, passwd); } break; } } else { /* Compare against a file */ FILE *f; f = fopen(password, "r"); if (f) { char buf[256] = ""; while(!feof(f)) { fgets(buf, sizeof(buf), f); if (!feof(f) && !ast_strlen_zero(buf)) { buf[strlen(buf) - 1] = '\0'; if (!ast_strlen_zero(buf) && !strcmp(passwd, buf)) break; } } fclose(f); if (!ast_strlen_zero(buf) && !strcmp(passwd, buf)) break; } else ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", password, strerror(errno)); } } else { /* Compare against a fixed password */ if (!strcmp(passwd, password)) break; } prompt="auth-incorrect"; } if ((retries < 3) && !res) { if (strchr(opts, 'a')) ast_cdr_setaccount(chan, passwd); res = ast_streamfile(chan, "auth-thankyou", chan->language); if (!res) res = ast_waitstream(chan, ""); } else { if (!res) res = ast_streamfile(chan, "vm-goodbye", chan->language); if (!res) res = ast_waitstream(chan, ""); res = -1; } LOCAL_USER_REMOVE(u); return res; }