Mon Mar 12 2012 21:23:16

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, const char *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_LOAD_ORDER , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static const 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 },}

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 45 of file app_authenticate.c.

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

Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 279 of file app_authenticate.c.

static void __unreg_module ( void  ) [static]

Definition at line 279 of file app_authenticate.c.

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

Definition at line 118 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_channel_lock, ast_channel_unlock, 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, 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_channel_lock(chan);
                     ast_cdr_setaccount(chan, buf);
                     ast_channel_unlock(chan);
                  }
                  break;
               }
            } else {
               if (!strcmp(passwd, buf)) {
                  if (ast_test_flag(&flags, OPT_ACCOUNT)) {
                     ast_channel_lock(chan);
                     ast_cdr_setaccount(chan, buf);
                     ast_channel_unlock(chan);
                  }
                  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_channel_lock(chan);
         ast_cdr_setaccount(chan, passwd);
         ast_channel_unlock(chan);
      }
      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 267 of file app_authenticate.c.

References ast_unregister_application().


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 279 of file app_authenticate.c.

const char app[] = "Authenticate" [static]

Definition at line 60 of file app_authenticate.c.

Definition at line 279 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 57 of file app_authenticate.c.

Referenced by auth_exec().