Mon May 14 04:43:23 2007

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"

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_JUMP = (1 << 2), OPT_MULTIPLE = (1 << 3),
  OPT_REMOVE = (1 << 4)
}

Functions

 AST_APP_OPTIONS (auth_app_options,{AST_APP_OPTION('a', OPT_ACCOUNT), AST_APP_OPTION('d', OPT_DATABASE), AST_APP_OPTION('j', OPT_JUMP), AST_APP_OPTION('m', OPT_MULTIPLE), AST_APP_OPTION('r', OPT_REMOVE),})
 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Authentication Application")
static int auth_exec (struct ast_channel *chan, void *data)
static int load_module (void)
static int unload_module (void)

Variables

static char * app = "Authenticate"
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

AST_APP_OPTIONS ( auth_app_options   ) 

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Authentication Application"   
)

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, f, ast_flags::flags, 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                   fgets(buf, sizeof(buf), f);
00171                   if (!feof(f) && !ast_strlen_zero(buf)) {
00172                      buf[strlen(buf) - 1] = '\0';
00173                      if (ast_test_flag(&flags,OPT_MULTIPLE)) {
00174                         md5secret = strchr(buf, ':');
00175                         if (md5secret == NULL)
00176                            continue;
00177                         *md5secret = '\0';
00178                         md5secret++;
00179                         ast_md5_hash(md5passwd, passwd);
00180                         if (!strcmp(md5passwd, md5secret)) {
00181                            if (ast_test_flag(&flags,OPT_ACCOUNT))
00182                               ast_cdr_setaccount(chan, buf);
00183                            break;
00184                         }
00185                      } else {
00186                         if (!strcmp(passwd, buf)) {
00187                            if (ast_test_flag(&flags,OPT_ACCOUNT))
00188                               ast_cdr_setaccount(chan, buf);
00189                            break;
00190                         }
00191                      }
00192                   }
00193                }
00194                fclose(f);
00195                if (!ast_strlen_zero(buf)) {
00196                   if (ast_test_flag(&flags,OPT_MULTIPLE)) {
00197                      if (md5secret && !strcmp(md5passwd, md5secret))
00198                         break;
00199                   } else {
00200                      if (!strcmp(passwd, buf))
00201                         break;
00202                   }
00203                }
00204             } else 
00205                ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno));
00206          }
00207       } else {
00208          /* Compare against a fixed password */
00209          if (!strcmp(passwd, arglist.password)) 
00210             break;
00211       }
00212       prompt="auth-incorrect";
00213    }
00214    if ((retries < 3) && !res) {
00215       if (ast_test_flag(&flags,OPT_ACCOUNT) && !ast_test_flag(&flags,OPT_MULTIPLE)) 
00216          ast_cdr_setaccount(chan, passwd);
00217       res = ast_streamfile(chan, "auth-thankyou", chan->language);
00218       if (!res)
00219          res = ast_waitstream(chan, "");
00220    } else {
00221       if (ast_test_flag(&flags,OPT_JUMP) && ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101) == 0) {
00222          res = 0;
00223       } else {
00224          if (!ast_streamfile(chan, "vm-goodbye", chan->language))
00225             res = ast_waitstream(chan, "");
00226          res = -1;
00227       }
00228    }
00229    ast_module_user_remove(u);
00230    return res;
00231 }

static int load_module ( void   )  [static]

Definition at line 245 of file app_authenticate.c.

References ast_register_application(), and auth_exec().

00246 {
00247    return ast_register_application(app, auth_exec, synopsis, descrip);
00248 }

static int unload_module ( void   )  [static]

Definition at line 233 of file app_authenticate.c.

References ast_module_user_hangup_all, and ast_unregister_application().

00234 {
00235    int res;
00236 
00237    ast_module_user_hangup_all();
00238 
00239    res = ast_unregister_application(app);
00240 
00241    
00242    return res;
00243 }


Variable Documentation

char* app = "Authenticate" [static]

Definition at line 66 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 Mon May 14 04:43:24 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1