Wed Jan 8 2020 09:49:53

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"

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 = "ac1f6a56484a8820659555499174e588" , .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 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
Enumerator
OPT_ACCOUNT 
OPT_DATABASE 
OPT_MULTIPLE 
OPT_REMOVE 

Definition at line 45 of file app_authenticate.c.

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

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().

119 {
120  int res = 0, retries, maxdigits;
121  char passwd[256], *prompt = "agent-pass", *argcopy = NULL;
122  struct ast_flags flags = {0};
123 
124  AST_DECLARE_APP_ARGS(arglist,
125  AST_APP_ARG(password);
126  AST_APP_ARG(options);
127  AST_APP_ARG(maxdigits);
128  AST_APP_ARG(prompt);
129  );
130 
131  if (ast_strlen_zero(data)) {
132  ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n");
133  return -1;
134  }
135 
136  if (chan->_state != AST_STATE_UP) {
137  if ((res = ast_answer(chan)))
138  return -1;
139  }
140 
141  argcopy = ast_strdupa(data);
142 
143  AST_STANDARD_APP_ARGS(arglist, argcopy);
144 
145  if (!ast_strlen_zero(arglist.options))
146  ast_app_parse_options(auth_app_options, &flags, NULL, arglist.options);
147 
148  if (!ast_strlen_zero(arglist.maxdigits)) {
149  maxdigits = atoi(arglist.maxdigits);
150  if ((maxdigits<1) || (maxdigits>sizeof(passwd)-2))
151  maxdigits = sizeof(passwd) - 2;
152  } else {
153  maxdigits = sizeof(passwd) - 2;
154  }
155 
156  if (!ast_strlen_zero(arglist.prompt)) {
157  prompt = arglist.prompt;
158  } else {
159  prompt = "agent-pass";
160  }
161 
162  /* Start asking for password */
163  for (retries = 0; retries < 3; retries++) {
164  if ((res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0)) < 0)
165  break;
166 
167  res = 0;
168 
169  if (arglist.password[0] != '/') {
170  /* Compare against a fixed password */
171  if (!strcmp(passwd, arglist.password))
172  break;
173  } else if (ast_test_flag(&flags,OPT_DATABASE)) {
174  char tmp[256];
175  /* Compare against a database key */
176  if (!ast_db_get(arglist.password + 1, passwd, tmp, sizeof(tmp))) {
177  /* It's a good password */
178  if (ast_test_flag(&flags,OPT_REMOVE))
179  ast_db_del(arglist.password + 1, passwd);
180  break;
181  }
182  } else {
183  /* Compare against a file */
184  FILE *f;
185  char buf[256] = "", md5passwd[33] = "", *md5secret = NULL;
186 
187  if (!(f = fopen(arglist.password, "r"))) {
188  ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno));
189  continue;
190  }
191 
192  for (;;) {
193  size_t len;
194 
195  if (feof(f))
196  break;
197 
198  if (!fgets(buf, sizeof(buf), f)) {
199  continue;
200  }
201 
202  if (ast_strlen_zero(buf))
203  continue;
204 
205  len = strlen(buf) - 1;
206  if (buf[len] == '\n')
207  buf[len] = '\0';
208 
209  if (ast_test_flag(&flags, OPT_MULTIPLE)) {
210  md5secret = buf;
211  strsep(&md5secret, ":");
212  if (!md5secret)
213  continue;
214  ast_md5_hash(md5passwd, passwd);
215  if (!strcmp(md5passwd, md5secret)) {
216  if (ast_test_flag(&flags,OPT_ACCOUNT)) {
217  ast_channel_lock(chan);
218  ast_cdr_setaccount(chan, buf);
219  ast_channel_unlock(chan);
220  }
221  break;
222  }
223  } else {
224  if (!strcmp(passwd, buf)) {
225  if (ast_test_flag(&flags, OPT_ACCOUNT)) {
226  ast_channel_lock(chan);
227  ast_cdr_setaccount(chan, buf);
228  ast_channel_unlock(chan);
229  }
230  break;
231  }
232  }
233  }
234 
235  fclose(f);
236 
237  if (!ast_strlen_zero(buf)) {
238  if (ast_test_flag(&flags, OPT_MULTIPLE)) {
239  if (md5secret && !strcmp(md5passwd, md5secret))
240  break;
241  } else {
242  if (!strcmp(passwd, buf))
243  break;
244  }
245  }
246  }
247  prompt = "auth-incorrect";
248  }
249 
250  if ((retries < 3) && !res) {
251  if (ast_test_flag(&flags,OPT_ACCOUNT) && !ast_test_flag(&flags,OPT_MULTIPLE)) {
252  ast_channel_lock(chan);
253  ast_cdr_setaccount(chan, passwd);
254  ast_channel_unlock(chan);
255  }
256  if (!(res = ast_streamfile(chan, "auth-thankyou", chan->language)))
257  res = ast_waitstream(chan, "");
258  } else {
259  if (!ast_streamfile(chan, "vm-goodbye", chan->language))
260  res = ast_waitstream(chan, "");
261  res = -1;
262  }
263 
264  return res;
265 }
#define ast_channel_lock(chan)
Definition: channel.h:2466
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:946
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: app.c:178
char * strsep(char **str, const char *delims)
int ast_db_get(const char *family, const char *key, char *out, int outlen)
Get key value specified by family/key.
Definition: db.c:348
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define LOG_WARNING
Definition: logger.h:144
static struct ast_app_option auth_app_options[128]
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: app.c:2101
int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
Set account code, will generate AMI event.
Definition: cdr.c:990
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
unsigned int flags
Definition: utils.h:201
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
enum ast_channel_state _state
Definition: channel.h:839
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_channel_unlock(chan)
Definition: channel.h:2467
int errno
static struct ast_format f[]
Definition: format_g726.c:181
Structure used to handle boolean flags.
Definition: utils.h:200
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: db.c:365
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1343
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:3086
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
static struct ast_str * prompt
Definition: asterisk.c:2395
void ast_md5_hash(char *output, const char *input)
Produces MD5 hash based on input string.
Definition: utils.c:245
const ast_string_field language
Definition: channel.h:787
static int load_module ( void  )
static

Definition at line 272 of file app_authenticate.c.

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

273 {
277 }
static const char app[]
static int auth_exec(struct ast_channel *chan, const char *data)
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
static int unload_module ( void  )
static

Definition at line 267 of file app_authenticate.c.

References ast_unregister_application().

268 {
270 }
static const char app[]
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705

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 = "ac1f6a56484a8820659555499174e588" , .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().