From 242489940ab1e8d0fc8e02df084015d3b8aa289c Mon Sep 17 00:00:00 2001 From: file Date: Wed, 19 Sep 2007 17:20:43 +0000 Subject: Clean up code in app_authenticate. git-svn-id: http://svn.digium.com/svn/asterisk/trunk@83156 f38db490-d61c-443f-a65b-d21fe96a405b --- apps/app_authenticate.c | 98 ++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 54 deletions(-) (limited to 'apps/app_authenticate.c') diff --git a/apps/app_authenticate.c b/apps/app_authenticate.c index c7e7d96c7..8533f151d 100644 --- a/apps/app_authenticate.c +++ b/apps/app_authenticate.c @@ -88,12 +88,8 @@ static char *descrip = static int auth_exec(struct ast_channel *chan, void *data) { - int res=0; - int retries; - char passwd[256]; - char *prompt; - int maxdigits; - char *argcopy =NULL; + int res = 0, retries, maxdigits; + char passwd[256], *prompt = "agent-pass", *argcopy = NULL; struct ast_flags flags = {0}; AST_DECLARE_APP_ARGS(arglist, @@ -114,11 +110,10 @@ static int auth_exec(struct ast_channel *chan, void *data) argcopy = ast_strdupa(data); - AST_STANDARD_APP_ARGS(arglist,argcopy); + AST_STANDARD_APP_ARGS(arglist, argcopy); - if (!ast_strlen_zero(arglist.options)) { + if (!ast_strlen_zero(arglist.options)) ast_app_parse_options(auth_app_options, &flags, NULL, arglist.options); - } if (!ast_strlen_zero(arglist.maxdigits)) { maxdigits = atoi(arglist.maxdigits); @@ -129,10 +124,8 @@ static int auth_exec(struct ast_channel *chan, void *data) } /* Start asking for password */ - prompt = "agent-pass"; for (retries = 0; retries < 3; retries++) { - res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0); - if (res < 0) + if ((res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0)) < 0) break; res = 0; if (arglist.password[0] == '/') { @@ -141,70 +134,67 @@ static int auth_exec(struct ast_channel *chan, void *data) /* Compare against a database key */ if (!ast_db_get(arglist.password + 1, passwd, tmp, sizeof(tmp))) { /* It's a good password */ - if (ast_test_flag(&flags,OPT_REMOVE)) { + if (ast_test_flag(&flags,OPT_REMOVE)) ast_db_del(arglist.password + 1, passwd); - } break; } } else { /* Compare against a file */ FILE *f; - f = fopen(arglist.password, "r"); - if (f) { - char buf[256] = ""; - char md5passwd[33] = ""; - char *md5secret = NULL; - - while (!feof(f)) { - fgets(buf, sizeof(buf), f); - if (!feof(f) && !ast_strlen_zero(buf)) { - buf[strlen(buf) - 1] = '\0'; - if (ast_test_flag(&flags,OPT_MULTIPLE)) { - md5secret = strchr(buf, ':'); - if (md5secret == NULL) - continue; - *md5secret = '\0'; - md5secret++; - ast_md5_hash(md5passwd, passwd); - if (!strcmp(md5passwd, md5secret)) { - if (ast_test_flag(&flags,OPT_ACCOUNT)) - ast_cdr_setaccount(chan, buf); - break; - } - } else { - if (!strcmp(passwd, buf)) { - if (ast_test_flag(&flags,OPT_ACCOUNT)) - ast_cdr_setaccount(chan, buf); - break; - } - } - } - } - fclose(f); - if (!ast_strlen_zero(buf)) { + char buf[256] = "", md5passwd[33] = "", *md5secret = NULL; + + if (!(f = fopen(arglist.password, "r"))) { + ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno)); + continue; + } + + while (!feof(f)) { + fgets(buf, sizeof(buf), f); + if (!feof(f) && !ast_strlen_zero(buf)) { + buf[strlen(buf) - 1] = '\0'; if (ast_test_flag(&flags,OPT_MULTIPLE)) { - if (md5secret && !strcmp(md5passwd, md5secret)) + md5secret = strchr(buf, ':'); + if (md5secret == NULL) + continue; + *md5secret = '\0'; + md5secret++; + ast_md5_hash(md5passwd, passwd); + if (!strcmp(md5passwd, md5secret)) { + if (ast_test_flag(&flags,OPT_ACCOUNT)) + ast_cdr_setaccount(chan, buf); break; + } } else { - if (!strcmp(passwd, buf)) + if (!strcmp(passwd, buf)) { + if (ast_test_flag(&flags,OPT_ACCOUNT)) + ast_cdr_setaccount(chan, buf); break; + } } } - } else - ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno)); + } + fclose(f); + if (!ast_strlen_zero(buf)) { + if (ast_test_flag(&flags,OPT_MULTIPLE)) { + if (md5secret && !strcmp(md5passwd, md5secret)) + break; + } else { + if (!strcmp(passwd, buf)) + break; + } + } } } else { /* Compare against a fixed password */ if (!strcmp(passwd, arglist.password)) break; } - prompt="auth-incorrect"; + prompt = "auth-incorrect"; } if ((retries < 3) && !res) { if (ast_test_flag(&flags,OPT_ACCOUNT) && !ast_test_flag(&flags,OPT_MULTIPLE)) ast_cdr_setaccount(chan, passwd); - res = ast_streamfile(chan, "auth-thankyou", chan->language); - if (!res) + if (!(res = ast_streamfile(chan, "auth-thankyou", chan->language))) res = ast_waitstream(chan, ""); } else { if (!ast_streamfile(chan, "vm-goodbye", chan->language)) -- cgit v1.2.3