Fri Jul 24 00:41:07 2009

Asterisk developer's documentation


app_authenticate.c File Reference

Execute arbitrary authenticate commands. More...

#include "asterisk.h"
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/app.h"
#include "asterisk/astdb.h"
#include "asterisk/utils.h"

Go to the source code of this file.

Enumerations

enum  { OPT_ACCOUNT = (1 << 0), OPT_DATABASE = (1 << 1), OPT_MULTIPLE = (1 << 3), OPT_REMOVE = (1 << 4) }

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int auth_exec (struct ast_channel *chan, void *data)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Authentication Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, }
static char * app = "Authenticate"
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_app_option auth_app_options [128] = { [ 'a' ] = { .flag = OPT_ACCOUNT }, [ 'd' ] = { .flag = OPT_DATABASE }, [ 'm' ] = { .flag = OPT_MULTIPLE }, [ 'r' ] = { .flag = OPT_REMOVE },}
enum { ... }  auth_option_flags
static char * descrip
static char * synopsis = "Authenticate a user"


Detailed Description

Execute arbitrary authenticate commands.

Author:
Mark Spencer <markster@digium.com>

Definition in file app_authenticate.c.


Enumeration Type Documentation

anonymous enum

Enumerator:
OPT_ACCOUNT 
OPT_DATABASE 
OPT_MULTIPLE 
OPT_REMOVE 

Definition at line 41 of file app_authenticate.c.

00041      {
00042    OPT_ACCOUNT = (1 << 0),
00043    OPT_DATABASE = (1 << 1),
00044    OPT_MULTIPLE = (1 << 3),
00045    OPT_REMOVE = (1 << 4),
00046 } auth_option_flags;


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 226 of file app_authenticate.c.

static void __unreg_module ( void   )  [static]

Definition at line 226 of file app_authenticate.c.

static int auth_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 81 of file app_authenticate.c.

References ast_channel::_state, ast_answer(), AST_APP_ARG, ast_app_getdata(), ast_app_parse_options(), ast_db_del(), ast_db_get(), AST_DECLARE_APP_ARGS, ast_log(), AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_strdupa, ast_strlen_zero(), ast_test_flag, auth_app_options, chan, ast_flags::flags, LOG_WARNING, OPT_DATABASE, OPT_REMOVE, and prompt.

Referenced by load_module().

00082 {
00083    int res = 0, retries, maxdigits;
00084    char passwd[256], *prompt = "agent-pass", *argcopy = NULL;
00085    struct ast_flags flags = {0};
00086 
00087    AST_DECLARE_APP_ARGS(arglist,
00088       AST_APP_ARG(password);
00089       AST_APP_ARG(options);
00090       AST_APP_ARG(maxdigits);
00091    );
00092 
00093    if (ast_strlen_zero(data)) {
00094       ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n");
00095       return -1;
00096    }
00097 
00098    if (chan->_state != AST_STATE_UP) {
00099       if ((res = ast_answer(chan)))
00100          return -1;
00101    }
00102 
00103    argcopy = ast_strdupa(data);
00104 
00105    AST_STANDARD_APP_ARGS(arglist, argcopy);
00106 
00107    if (!ast_strlen_zero(arglist.options))
00108       ast_app_parse_options(auth_app_options, &flags, NULL, arglist.options);
00109 
00110    if (!ast_strlen_zero(arglist.maxdigits)) {
00111       maxdigits = atoi(arglist.maxdigits);
00112       if ((maxdigits<1) || (maxdigits>sizeof(passwd)-2))
00113          maxdigits = sizeof(passwd) - 2;
00114    } else {
00115       maxdigits = sizeof(passwd) - 2;
00116    }
00117 
00118    /* Start asking for password */
00119    for (retries = 0; retries < 3; retries++) {
00120       if ((res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0)) < 0)
00121          break;
00122 
00123       res = 0;
00124 
00125       if (arglist.password[0] != '/') {
00126          /* Compare against a fixed password */
00127          if (!strcmp(passwd, arglist.password))
00128             break;
00129       } else if (ast_test_flag(&flags,OPT_DATABASE)) {
00130          char tmp[256];
00131          /* Compare against a database key */
00132          if (!ast_db_get(arglist.password + 1, passwd, tmp, sizeof(tmp))) {
00133             /* It's a good password */
00134             if (ast_test_flag(&flags,OPT_REMOVE))
00135                ast_db_del(arglist.password + 1, passwd);
00136             break;
00137          }
00138       } else {
00139          /* Compare against a file */
00140          FILE *f;
00141          char buf[256] = "", md5passwd[33] = "", *md5secret = NULL;
00142 
00143          if (!(f = fopen(arglist.password, "r"))) {
00144             ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno));
00145             continue;
00146          }
00147 
00148          for (;;) {
00149             size_t len;
00150 
00151             if (feof(f))
00152                break;
00153 
00154             if (!fgets(buf, sizeof(buf), f)) {
00155                continue;
00156             }
00157 
00158             if (ast_strlen_zero(buf))
00159                continue;
00160 
00161             len = strlen(buf) - 1;
00162             if (buf[len] == '\n')
00163                buf[len] = '\0';
00164 
00165             if (ast_test_flag(&flags, OPT_MULTIPLE)) {
00166                md5secret = buf;
00167                strsep(&md5secret, ":");
00168                if (!md5secret)
00169                   continue;
00170                ast_md5_hash(md5passwd, passwd);
00171                if (!strcmp(md5passwd, md5secret)) {
00172                   if (ast_test_flag(&flags,OPT_ACCOUNT))
00173                      ast_cdr_setaccount(chan, buf);
00174                   break;
00175                }
00176             } else {
00177                if (!strcmp(passwd, buf)) {
00178                   if (ast_test_flag(&flags, OPT_ACCOUNT))
00179                      ast_cdr_setaccount(chan, buf);
00180                   break;
00181                }
00182             }
00183          }
00184 
00185          fclose(f);
00186 
00187          if (!ast_strlen_zero(buf)) {
00188             if (ast_test_flag(&flags, OPT_MULTIPLE)) {
00189                if (md5secret && !strcmp(md5passwd, md5secret))
00190                   break;
00191             } else {
00192                if (!strcmp(passwd, buf))
00193                   break;
00194             }
00195          }
00196       }
00197       prompt = "auth-incorrect";
00198    }
00199 
00200    if ((retries < 3) && !res) {
00201       if (ast_test_flag(&flags,OPT_ACCOUNT) && !ast_test_flag(&flags,OPT_MULTIPLE))
00202          ast_cdr_setaccount(chan, passwd);
00203       if (!(res = ast_streamfile(chan, "auth-thankyou", chan->language)))
00204          res = ast_waitstream(chan, "");
00205    } else {
00206       if (!ast_streamfile(chan, "vm-goodbye", chan->language))
00207          res = ast_waitstream(chan, "");
00208       res = -1;
00209    }
00210 
00211    return res;
00212 }

static int load_module ( void   )  [static]

Definition at line 219 of file app_authenticate.c.

References AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, ast_register_application, and auth_exec().

00220 {
00221    if (ast_register_application(app, auth_exec, synopsis, descrip))
00222       return AST_MODULE_LOAD_FAILURE;
00223    return AST_MODULE_LOAD_SUCCESS;
00224 }

static int unload_module ( void   )  [static]

Definition at line 214 of file app_authenticate.c.

References ast_unregister_application().

00215 {
00216    return ast_unregister_application(app);
00217 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Authentication Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, } [static]

Definition at line 226 of file app_authenticate.c.

char* app = "Authenticate" [static]

Definition at line 56 of file app_authenticate.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 226 of file app_authenticate.c.

struct ast_app_option auth_app_options[128] = { [ 'a' ] = { .flag = OPT_ACCOUNT }, [ 'd' ] = { .flag = OPT_DATABASE }, [ 'm' ] = { .flag = OPT_MULTIPLE }, [ 'r' ] = { .flag = OPT_REMOVE },} [static]

Definition at line 53 of file app_authenticate.c.

Referenced by auth_exec().

enum { ... } auth_option_flags

char* descrip [static]

Definition at line 60 of file app_authenticate.c.

char* synopsis = "Authenticate a user" [static]

Definition at line 58 of file app_authenticate.c.


Generated on Fri Jul 24 00:41:07 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7