Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:58 2009

## Asterisk developer's documentation

# app_authenticate.c File Reference

Execute arbitrary authenticate commands. More...

`#include <stdlib.h>`
`#include <unistd.h>`
`#include <string.h>`
`#include <errno.h>`
`#include <stdio.h>`
`#include "asterisk.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 dependency graph for app_authenticate.c:

Go to the source code of this file.

## Functions

static int | auth_exec (struct ast_channel *chan, void *data)
char * | description (void)
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
int | load_module (void)
| Initialize the module.
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

static char * | app = "Authenticate"
static char * | descrip
| LOCAL_USER_DECL
| STANDARD_LOCAL_USER
static char * | synopsis = "Authenticate a user"
static char * | tdesc = "Authentication Application"

## Detailed Description

Execute arbitrary authenticate commands.

Definition in file app_authenticate.c.

## Function Documentation

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

Definition at line 75 of file app_authenticate.c.

References ast_channel::_state, ast_answer(), ast_app_getdata(), ast_cdr_setaccount(), ast_db_del(), ast_db_get(), ast_goto_if_exists(), ast_log(), ast_md5_hash(), AST_STATE_UP, ast_streamfile(), ast_strlen_zero(), ast_waitstream(), ast_channel::context, ast_channel::exten, ast_channel::language, LOCAL_USER_ADD, LOCAL_USER_REMOVE, LOG_WARNING, password, and ast_channel::priority.

Referenced by load_module().

    00076 {
    00077    int res=0;
    00078    int jump = 0;
    00079    int retries;
    00080    struct localuser *u;
    00081    char password[256]="";
    00082    char passwd[256];
    00083    char *opts;
    00084    char *prompt;
    00085
    00086    if (ast_strlen_zero(data)) {
    00087       ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n");
    00088       return -1;
    00089    }
    00090
    00091    LOCAL_USER_ADD(u);
    00092
    00093    if (chan->_state != AST_STATE_UP) {
    00094       res = ast_answer(chan);
    00095       if (res) {
    00096          LOCAL_USER_REMOVE(u);
    00097          return -1;
    00098       }
    00099    }
    00100
    00101    strncpy(password, data, sizeof(password) - 1);
    00102    opts=strchr(password, '|');
    00103    if (opts) {
    00104       *opts = 0;
    00105       opts++;
    00106    } else
    00107       opts = "";
    00108    if (strchr(opts, 'j'))
    00109       jump = 1;
    00110    /* Start asking for password */
    00111    prompt = "agent-pass";
    00112    for (retries = 0; retries < 3; retries++) {
    00113       res = ast_app_getdata(chan, prompt, passwd, sizeof(passwd) - 2, 0);
    00114       if (res < 0)
    00115          break;
    00116       res = 0;
    00117       if (password[0] == '/') {
    00118          if (strchr(opts, 'd')) {
    00119             char tmp[256];
    00120             /* Compare against a database key */
    00121             if (!ast_db_get(password + 1, passwd, tmp, sizeof(tmp))) {
    00122                /* It's a good password */
    00123                if (strchr(opts, 'r')) {
    00124                   ast_db_del(password + 1, passwd);
    00125                }
    00126                break;
    00127             }
    00128          } else {
    00129             /* Compare against a file */
    00130             FILE *f;
    00131             f = fopen(password, "r");
    00132             if (f) {
    00133                char buf[256] = "";
    00134                char md5passwd[33] = "";
    00135                char *md5secret = NULL;
    00136
    00137                while (!feof(f)) {
    00138                   fgets(buf, sizeof(buf), f);
    00139                   if (!feof(f) && !ast_strlen_zero(buf)) {
    00140                      buf[strlen(buf) - 1] = '\0';
    00141                      if (strchr(opts, 'm')) {
    00142                         md5secret = strchr(buf, ':');
    00143                         if (md5secret == NULL)
    00144                            continue;
    00145                         *md5secret = '\0';
    00146                         md5secret++;
    00147                         ast_md5_hash(md5passwd, passwd);
    00148                         if (!strcmp(md5passwd, md5secret)) {
    00149                            if (strchr(opts, 'a'))
    00150                               ast_cdr_setaccount(chan, buf);
    00151                            break;
    00152                         }
    00153                      } else {
    00154                         if (!strcmp(passwd, buf)) {
    00155                            if (strchr(opts, 'a'))
    00156                               ast_cdr_setaccount(chan, buf);
    00157                            break;
    00158                         }
    00159                      }
    00160                   }
    00161                }
    00162                fclose(f);
    00163                if (!ast_strlen_zero(buf)) {
    00164                   if (strchr(opts, 'm')) {
    00165                      if (md5secret && !strcmp(md5passwd, md5secret))
    00166                         break;
    00167                   } else {
    00168                      if (!strcmp(passwd, buf))
    00169                         break;
    00170                   }
    00171                }
    00172             } else
    00173                ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", password, strerror(errno));
    00174          }
    00175       } else {
    00176          /* Compare against a fixed password */
    00177          if (!strcmp(passwd, password))
    00178             break;
    00179       }
    00180       prompt="auth-incorrect";
    00181    }
    00182    if ((retries < 3) && !res) {
    00183       if (strchr(opts, 'a') && !strchr(opts, 'm'))
    00184          ast_cdr_setaccount(chan, passwd);
    00185       res = ast_streamfile(chan, "auth-thankyou", chan->language);
    00186       if (!res)
    00187          res = ast_waitstream(chan, "");
    00188    } else {
    00189       if (jump && ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101) == 0) {
    00190          res = 0;
    00191       } else {
    00192          if (!ast_streamfile(chan, "vm-goodbye", chan->language))
    00193             res = ast_waitstream(chan, "");
    00194          res = -1;
    00195       }
    00196    }
    00197    LOCAL_USER_REMOVE(u);
    00198    return res;
    00199 }

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 217 of file app_authenticate.c.

    00218 {
    00219    return tdesc;
    00220 }

char* key | ( | void |  | ) |

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

     char *key(void) {
             return ASTERISK_GPL_KEY;
     }

Returns:
  ASTERISK_GPL_KEY

Definition at line 229 of file app_authenticate.c.

References ASTERISK_GPL_KEY.

    00230 {
    00231    return ASTERISK_GPL_KEY;
    00232 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
  int Always 0.

TE STUFF END

Definition at line 212 of file app_authenticate.c.

References ast_register_application(), and auth_exec().

    00213 {
    00214    return ast_register_application(app, auth_exec, synopsis, descrip);
    00215 }

int unload_module | ( | void |  | ) |

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
  Zero on success, or non-zero on error.

Definition at line 201 of file app_authenticate.c.

References ast_unregister_application(), and STANDARD_HANGUP_LOCALUSERS.

    00202 {
    00203    int res;
    00204
    00205    res = ast_unregister_application(app);
    00206
    00207    STANDARD_HANGUP_LOCALUSERS;
    00208
    00209    return res;
    00210 }

int usecount | ( | void |  | ) |

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
  The module's usecount.

Definition at line 222 of file app_authenticate.c.

References STANDARD_USECOUNT.

    00223 {
    00224    int res;
    00225    STANDARD_USECOUNT(res);
    00226    return res;
    00227 }

## Variable Documentation

char* app = "Authenticate"`[static]`

Definition at line 47 of file app_authenticate.c.

char* descrip`[static]`

Definition at line 51 of file app_authenticate.c.

LOCAL_USER_DECL

Definition at line 73 of file app_authenticate.c.

STANDARD_LOCAL_USER

Definition at line 71 of file app_authenticate.c.

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

Definition at line 49 of file app_authenticate.c.

char* tdesc = "Authentication Application"`[static]`

Definition at line 45 of file app_authenticate.c.