Thu Apr 28 2011 16:56:50

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"
Include dependency graph for app_authenticate.c:

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
__MODULE_INFO_SECTION 
__mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Authentication Application" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .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

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.

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

Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 260 of file app_authenticate.c.

static void __unreg_module ( void  ) [static]

Definition at line 260 of file app_authenticate.c.

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

Definition at line 108 of file app_authenticate.c.

References ast_channel::_state, ast_answer(), AST_APP_ARG, ast_app_getdata(), ast_app_parse_options(), ast_cdr_setaccount(), ast_db_del(), ast_db_get(), AST_DECLARE_APP_ARGS, ast_log(), ast_md5_hash(), AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_strdupa, ast_streamfile(), ast_strlen_zero(), ast_test_flag, ast_waitstream(), auth_app_options, buf, errno, f, ast_channel::language, len(), LOG_WARNING, OPT_ACCOUNT, OPT_DATABASE, OPT_MULTIPLE, OPT_REMOVE, prompt, and strsep().

Referenced by load_module().

{
   int res = 0, retries, maxdigits;
   char passwd[256], *prompt = "agent-pass", *argcopy = NULL;
   struct ast_flags flags = {0};

   AST_DECLARE_APP_ARGS(arglist,
      AST_APP_ARG(password);
      AST_APP_ARG(options);
      AST_APP_ARG(maxdigits);
      AST_APP_ARG(prompt);
   );

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n");
      return -1;
   }

   if (chan->_state != AST_STATE_UP) {
      if ((res = ast_answer(chan)))
         return -1;
   }

   argcopy = ast_strdupa(data);

   AST_STANDARD_APP_ARGS(arglist, argcopy);

   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);
      if ((maxdigits<1) || (maxdigits>sizeof(passwd)-2))
         maxdigits = sizeof(passwd) - 2;
   } else {
      maxdigits = sizeof(passwd) - 2;
   }

   if (!ast_strlen_zero(arglist.prompt)) {
      prompt = arglist.prompt;
   } else {
      prompt = "agent-pass";
   }
   
   /* Start asking for password */
   for (retries = 0; retries < 3; retries++) {
      if ((res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0)) < 0)
         break;

      res = 0;

      if (arglist.password[0] != '/') {
         /* Compare against a fixed password */
         if (!strcmp(passwd, arglist.password))
            break;
      } else if (ast_test_flag(&flags,OPT_DATABASE)) {
         char tmp[256];
         /* 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))
               ast_db_del(arglist.password + 1, passwd);
            break;
         }
      } else {
         /* Compare against a file */
         FILE *f;
         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;
         }

         for (;;) {
            size_t len;

            if (feof(f))
               break;

            if (!fgets(buf, sizeof(buf), f)) {
               continue;
            }

            if (ast_strlen_zero(buf))
               continue;

            len = strlen(buf) - 1;
            if (buf[len] == '\n')
               buf[len] = '\0';

            if (ast_test_flag(&flags, OPT_MULTIPLE)) {
               md5secret = buf;
               strsep(&md5secret, ":");
               if (!md5secret)
                  continue;
               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)) {
            if (ast_test_flag(&flags, OPT_MULTIPLE)) {
               if (md5secret && !strcmp(md5passwd, md5secret))
                  break;
            } else {
               if (!strcmp(passwd, buf))
                  break;
            }
         }
      }
      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);
      if (!(res = ast_streamfile(chan, "auth-thankyou", chan->language)))
         res = ast_waitstream(chan, "");
   } else {
      if (!ast_streamfile(chan, "vm-goodbye", chan->language))
         res = ast_waitstream(chan, "");
      res = -1;
   }

   return res;
}
static int load_module ( void  ) [static]
static int unload_module ( void  ) [static]

Definition at line 248 of file app_authenticate.c.

References ast_unregister_application().


Variable Documentation

struct ast_module_info __MODULE_INFO_SECTION __mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Authentication Application" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static]

Definition at line 260 of file app_authenticate.c.

char* app = "Authenticate" [static]

Definition at line 56 of file app_authenticate.c.

Definition at line 260 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