Sat Aug 6 00:39:33 2011

Asterisk developer's documentation


app_authenticate.c File Reference

Execute arbitrary authenticate commands. More...

#include "asterisk.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/logger.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 "asterisk/options.h"

Go to the source code of this file.

Enumerations

enum  {
  OPT_ACCOUNT = (1 << 0), OPT_DATABASE = (1 << 1), OPT_JUMP = (1 << 2), 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 | AST_MODFLAG_BUILDSUM, .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 = "361d7bb937402d51e4658efb5b4d76e4" , .load = load_module, .unload = unload_module, }
static char * app = "Authenticate"
static const 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 }, [ 'j' ] = { .flag = OPT_JUMP }, [ '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_JUMP 
OPT_MULTIPLE 
OPT_REMOVE 

Definition at line 49 of file app_authenticate.c.

00049      {
00050    OPT_ACCOUNT = (1 << 0),
00051    OPT_DATABASE = (1 << 1),
00052    OPT_JUMP = (1 << 2),
00053    OPT_MULTIPLE = (1 << 3),
00054    OPT_REMOVE = (1 << 4),
00055 } auth_option_flags;


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 254 of file app_authenticate.c.

static void __unreg_module ( void   )  [static]

Definition at line 254 of file app_authenticate.c.

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

Definition at line 94 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_module_user_add, ast_module_user_remove, AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_strdupa, ast_strlen_zero(), ast_test_flag, errno, f, ast_flags::flags, len(), LOG_WARNING, OPT_ACCOUNT, OPT_DATABASE, OPT_MULTIPLE, OPT_REMOVE, and password.

Referenced by load_module().

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

static int load_module ( void   )  [static]

Definition at line 249 of file app_authenticate.c.

References ast_register_application(), and auth_exec().

00250 {
00251    return ast_register_application(app, auth_exec, synopsis, descrip);
00252 }

static int unload_module ( void   )  [static]

Definition at line 237 of file app_authenticate.c.

References ast_module_user_hangup_all, and ast_unregister_application().

00238 {
00239    int res;
00240 
00241    ast_module_user_hangup_all();
00242 
00243    res = ast_unregister_application(app);
00244 
00245    
00246    return res;
00247 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .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 = "361d7bb937402d51e4658efb5b4d76e4" , .load = load_module, .unload = unload_module, } [static]

Definition at line 254 of file app_authenticate.c.

char* app = "Authenticate" [static]

Definition at line 66 of file app_authenticate.c.

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 254 of file app_authenticate.c.

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

Definition at line 63 of file app_authenticate.c.

enum { ... } auth_option_flags

char* descrip [static]

Definition at line 70 of file app_authenticate.c.

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

Definition at line 68 of file app_authenticate.c.


Generated on Sat Aug 6 00:39:33 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7