Asterisk - The Open Source Telephony Project  18.5.0
Enumerations | Functions | Variables
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)
 
struct ast_moduleAST_MODULE_SELF_SYM (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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const char app [] = "Authenticate"
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const 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 marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file app_authenticate.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
OPT_ACCOUNT 
OPT_DATABASE 
OPT_MULTIPLE 
OPT_REMOVE 

Definition at line 43 of file app_authenticate.c.

43  {
44  OPT_ACCOUNT = (1 << 0),
45  OPT_DATABASE = (1 << 1),
46  OPT_MULTIPLE = (1 << 3),
47  OPT_REMOVE = (1 << 4),
48 };

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 277 of file app_authenticate.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 277 of file app_authenticate.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 277 of file app_authenticate.c.

◆ auth_exec()

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

Definition at line 116 of file app_authenticate.c.

References ast_answer(), AST_APP_ARG, ast_app_getdata(), ast_app_parse_options(), ast_channel_language(), 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, buf, errno, len(), LOG_WARNING, NULL, OPT_ACCOUNT, OPT_DATABASE, OPT_MULTIPLE, OPT_REMOVE, options, password, prompt, strsep(), and tmp().

Referenced by load_module().

117 {
118  int res = 0, retries, maxdigits;
119  char passwd[256], *prompt = "agent-pass", *argcopy = NULL;
120  struct ast_flags flags = {0};
121 
122  AST_DECLARE_APP_ARGS(arglist,
125  AST_APP_ARG(maxdigits);
126  AST_APP_ARG(prompt);
127  );
128 
129  if (ast_strlen_zero(data)) {
130  ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n");
131  return -1;
132  }
133 
134  if (ast_channel_state(chan) != AST_STATE_UP) {
135  if ((res = ast_answer(chan)))
136  return -1;
137  }
138 
139  argcopy = ast_strdupa(data);
140 
141  AST_STANDARD_APP_ARGS(arglist, argcopy);
142 
143  if (!ast_strlen_zero(arglist.options))
144  ast_app_parse_options(auth_app_options, &flags, NULL, arglist.options);
145 
146  if (!ast_strlen_zero(arglist.maxdigits)) {
147  maxdigits = atoi(arglist.maxdigits);
148  if ((maxdigits<1) || (maxdigits>sizeof(passwd)-2))
149  maxdigits = sizeof(passwd) - 2;
150  } else {
151  maxdigits = sizeof(passwd) - 2;
152  }
153 
154  if (!ast_strlen_zero(arglist.prompt)) {
155  prompt = arglist.prompt;
156  } else {
157  prompt = "agent-pass";
158  }
159 
160  /* Start asking for password */
161  for (retries = 0; retries < 3; retries++) {
162  if ((res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0)) < 0)
163  break;
164 
165  res = 0;
166 
167  if (arglist.password[0] != '/') {
168  /* Compare against a fixed password */
169  if (!strcmp(passwd, arglist.password))
170  break;
171  } else if (ast_test_flag(&flags,OPT_DATABASE)) {
172  char tmp[256];
173  /* Compare against a database key */
174  if (!ast_db_get(arglist.password + 1, passwd, tmp, sizeof(tmp))) {
175  /* It's a good password */
176  if (ast_test_flag(&flags,OPT_REMOVE))
177  ast_db_del(arglist.password + 1, passwd);
178  break;
179  }
180  } else {
181  /* Compare against a file */
182  FILE *f;
183  char buf[256] = "", md5passwd[33] = "", *md5secret = NULL;
184 
185  if (!(f = fopen(arglist.password, "r"))) {
186  ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno));
187  continue;
188  }
189 
190  for (;;) {
191  size_t len;
192 
193  if (feof(f))
194  break;
195 
196  if (!fgets(buf, sizeof(buf), f)) {
197  continue;
198  }
199 
200  if (ast_strlen_zero(buf))
201  continue;
202 
203  len = strlen(buf) - 1;
204  if (buf[len] == '\n')
205  buf[len] = '\0';
206 
207  if (ast_test_flag(&flags, OPT_MULTIPLE)) {
208  md5secret = buf;
209  strsep(&md5secret, ":");
210  if (!md5secret)
211  continue;
212  ast_md5_hash(md5passwd, passwd);
213  if (!strcmp(md5passwd, md5secret)) {
214  if (ast_test_flag(&flags, OPT_ACCOUNT)) {
215  ast_channel_lock(chan);
216  ast_channel_accountcode_set(chan, buf);
217  ast_channel_unlock(chan);
218  }
219  break;
220  }
221  } else {
222  if (!strcmp(passwd, buf)) {
223  if (ast_test_flag(&flags, OPT_ACCOUNT)) {
224  ast_channel_lock(chan);
225  ast_channel_accountcode_set(chan, buf);
226  ast_channel_unlock(chan);
227  }
228  break;
229  }
230  }
231  }
232 
233  fclose(f);
234 
235  if (!ast_strlen_zero(buf)) {
236  if (ast_test_flag(&flags, OPT_MULTIPLE)) {
237  if (md5secret && !strcmp(md5passwd, md5secret))
238  break;
239  } else {
240  if (!strcmp(passwd, buf))
241  break;
242  }
243  }
244  }
245  prompt = "auth-incorrect";
246  }
247 
248  if ((retries < 3) && !res) {
249  if (ast_test_flag(&flags,OPT_ACCOUNT) && !ast_test_flag(&flags,OPT_MULTIPLE)) {
250  ast_channel_lock(chan);
251  ast_channel_accountcode_set(chan, passwd);
252  ast_channel_unlock(chan);
253  }
254  if (!(res = ast_streamfile(chan, "auth-thankyou", ast_channel_language(chan))))
255  res = ast_waitstream(chan, "");
256  } else {
257  if (!ast_streamfile(chan, "vm-goodbye", ast_channel_language(chan)))
258  res = ast_waitstream(chan, "");
259  res = -1;
260  }
261 
262  return res;
263 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1250
#define ast_test_flag(p, flag)
Definition: utils.h:63
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
static int tmp()
Definition: bt_open.c:389
unsigned int flags
Definition: utils.h:200
ast_channel_state
ast_channel states
Definition: channelstate.h:35
#define NULL
Definition: resample.c:96
static struct ast_str * password
Definition: cdr_mysql.c:77
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:2906
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int errno
#define ast_channel_unlock(chan)
Definition: channel.h:2946
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:412
int ast_app_getdata(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout)
Plays a stream and gets DTMF data from a channel.
Definition: main/app.c:197
Structure used to handle boolean flags.
Definition: utils.h:199
char * strsep(char **str, const char *delims)
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: main/db.c:429
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1776
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2814
const char * ast_channel_language(const struct ast_channel *chan)
static struct test_options options
static struct ast_str * prompt
Definition: asterisk.c:2725
void ast_md5_hash(char *output, const char *input)
Produces MD5 hash based on input string.
Definition: main/utils.c:248
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
#define AST_APP_ARG(name)
Define an application argument.
static const struct ast_app_option auth_app_options[128]

◆ load_module()

static int load_module ( void  )
static

Definition at line 270 of file app_authenticate.c.

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_register_application_xml, and auth_exec().

271 {
275 }
static const char app[]
static int auth_exec(struct ast_channel *chan, const char *data)
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 265 of file app_authenticate.c.

References ast_unregister_application().

266 {
268 }
static const char app[]
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392

Variable Documentation

◆ __mod_info

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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 277 of file app_authenticate.c.

◆ app

const char app[] = "Authenticate"
static

Definition at line 58 of file app_authenticate.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 277 of file app_authenticate.c.

◆ auth_app_options

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

Referenced by auth_exec().