Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
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 = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .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};
@ OPT_MULTIPLE
@ OPT_DATABASE
@ OPT_ACCOUNT
@ OPT_REMOVE

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 289 of file app_authenticate.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 289 of file app_authenticate.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 289 of file app_authenticate.c.

◆ auth_exec()

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

Definition at line 128 of file app_authenticate.c.

129{
130 int res = 0, retries, maxdigits;
131 char passwd[256], *prompt = "agent-pass", *argcopy = NULL;
132 struct ast_flags flags = {0};
133
134 AST_DECLARE_APP_ARGS(arglist,
135 AST_APP_ARG(password);
137 AST_APP_ARG(maxdigits);
139 );
140
141 if (ast_strlen_zero(data)) {
142 ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n");
143 return -1;
144 }
145
146 if (ast_channel_state(chan) != AST_STATE_UP) {
147 if ((res = ast_answer(chan)))
148 return -1;
149 }
150
151 argcopy = ast_strdupa(data);
152
153 AST_STANDARD_APP_ARGS(arglist, argcopy);
154
155 if (!ast_strlen_zero(arglist.options))
157
158 if (!ast_strlen_zero(arglist.maxdigits)) {
159 maxdigits = atoi(arglist.maxdigits);
160 if ((maxdigits<1) || (maxdigits>sizeof(passwd)-2))
161 maxdigits = sizeof(passwd) - 2;
162 } else {
163 maxdigits = sizeof(passwd) - 2;
164 }
165
166 if (!ast_strlen_zero(arglist.prompt)) {
167 prompt = arglist.prompt;
168 } else {
169 prompt = "agent-pass";
170 }
171
172 /* Start asking for password */
173 for (retries = 0; retries < 3; retries++) {
174 if ((res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0)) < 0)
175 break;
176
177 res = 0;
178
179 if (arglist.password[0] != '/') {
180 /* Compare against a fixed password */
181 if (!strcmp(passwd, arglist.password))
182 break;
183 } else if (ast_test_flag(&flags,OPT_DATABASE)) {
184 char tmp[256];
185 /* Compare against a database key */
186 if (!ast_db_get(arglist.password + 1, passwd, tmp, sizeof(tmp))) {
187 /* It's a good password */
189 ast_db_del(arglist.password + 1, passwd);
190 break;
191 }
192 } else {
193 /* Compare against a file */
194 FILE *f;
195 char buf[256] = "", md5passwd[33] = "", *md5secret = NULL;
196
197 if (!(f = fopen(arglist.password, "r"))) {
198 ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno));
199 continue;
200 }
201
202 for (;;) {
203 size_t len;
204
205 if (feof(f))
206 break;
207
208 if (!fgets(buf, sizeof(buf), f)) {
209 continue;
210 }
211
212 if (ast_strlen_zero(buf))
213 continue;
214
215 len = strlen(buf) - 1;
216 if (buf[len] == '\n')
217 buf[len] = '\0';
218
220 md5secret = buf;
221 strsep(&md5secret, ":");
222 if (!md5secret)
223 continue;
224 ast_md5_hash(md5passwd, passwd);
225 if (!strcmp(md5passwd, md5secret)) {
227 ast_channel_lock(chan);
228 ast_channel_accountcode_set(chan, buf);
229 ast_channel_unlock(chan);
230 }
231 break;
232 }
233 } else {
234 if (!strcmp(passwd, buf)) {
236 ast_channel_lock(chan);
237 ast_channel_accountcode_set(chan, buf);
238 ast_channel_unlock(chan);
239 }
240 break;
241 }
242 }
243 }
244
245 fclose(f);
246
247 if (!ast_strlen_zero(buf)) {
249 if (md5secret && !strcmp(md5passwd, md5secret))
250 break;
251 } else {
252 if (!strcmp(passwd, buf))
253 break;
254 }
255 }
256 }
257 prompt = "auth-incorrect";
258 }
259
260 if ((retries < 3) && !res) {
262 ast_channel_lock(chan);
263 ast_channel_accountcode_set(chan, passwd);
264 ast_channel_unlock(chan);
265 }
266 if (!(res = ast_streamfile(chan, "auth-thankyou", ast_channel_language(chan))))
267 res = ast_waitstream(chan, "");
268 } else {
269 if (!ast_streamfile(chan, "vm-goodbye", ast_channel_language(chan)))
270 res = ast_waitstream(chan, "");
271 res = -1;
272 }
273
274 return res;
275}
static const struct ast_app_option auth_app_options[128]
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition db.c:421
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition db.c:472
char * strsep(char **str, const char *delims)
static struct ast_str * prompt
Definition asterisk.c:2795
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
#define ast_log
Definition astobj2.c:42
#define ast_channel_lock(chan)
Definition channel.h:2982
const char * ast_channel_language(const struct ast_channel *chan)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition channel.c:2803
#define ast_channel_unlock(chan)
Definition channel.h:2983
ast_channel_state
ast_channel states
@ AST_STATE_UP
char buf[BUFSIZE]
Definition eagi_proxy.c:66
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition file.c:1312
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition file.c:1874
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define AST_APP_ARG(name)
Define an application argument.
enum ast_getdata_result 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:188
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
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:3066
#define LOG_WARNING
int errno
#define NULL
Definition resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
Structure used to handle boolean flags.
Definition utils.h:220
unsigned int flags
Definition utils.h:221
static struct test_options options
#define ast_test_flag(p, flag)
Definition utils.h:64
void ast_md5_hash(char *output, const char *input)
Produces MD5 hash based on input string.
Definition utils.c:250

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, ast_flags::flags, len(), LOG_WARNING, NULL, OPT_ACCOUNT, OPT_DATABASE, OPT_MULTIPLE, OPT_REMOVE, options, prompt, and strsep().

Referenced by load_module().

◆ load_module()

static int load_module ( void  )
static

Definition at line 282 of file app_authenticate.c.

283{
287}
static const char app[]
static int auth_exec(struct ast_channel *chan, const char *data)
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
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:640

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

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 277 of file app_authenticate.c.

278{
280}
int ast_unregister_application(const char *app)
Unregister an application.
Definition pbx_app.c:392

References app, and ast_unregister_application().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Authentication Application" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 289 of file app_authenticate.c.

◆ app

const char app[] = "Authenticate"
static

Definition at line 58 of file app_authenticate.c.

Referenced by load_module(), and unload_module().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 289 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().