Asterisk - The Open Source Telephony Project GIT-master-7e7a603
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 = 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 286 of file app_authenticate.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 286 of file app_authenticate.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 286 of file app_authenticate.c.

◆ auth_exec()

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

Definition at line 125 of file app_authenticate.c.

126{
127 int res = 0, retries, maxdigits;
128 char passwd[256], *prompt = "agent-pass", *argcopy = NULL;
129 struct ast_flags flags = {0};
130
131 AST_DECLARE_APP_ARGS(arglist,
132 AST_APP_ARG(password);
134 AST_APP_ARG(maxdigits);
136 );
137
138 if (ast_strlen_zero(data)) {
139 ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n");
140 return -1;
141 }
142
143 if (ast_channel_state(chan) != AST_STATE_UP) {
144 if ((res = ast_answer(chan)))
145 return -1;
146 }
147
148 argcopy = ast_strdupa(data);
149
150 AST_STANDARD_APP_ARGS(arglist, argcopy);
151
152 if (!ast_strlen_zero(arglist.options))
154
155 if (!ast_strlen_zero(arglist.maxdigits)) {
156 maxdigits = atoi(arglist.maxdigits);
157 if ((maxdigits<1) || (maxdigits>sizeof(passwd)-2))
158 maxdigits = sizeof(passwd) - 2;
159 } else {
160 maxdigits = sizeof(passwd) - 2;
161 }
162
163 if (!ast_strlen_zero(arglist.prompt)) {
164 prompt = arglist.prompt;
165 } else {
166 prompt = "agent-pass";
167 }
168
169 /* Start asking for password */
170 for (retries = 0; retries < 3; retries++) {
171 if ((res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0)) < 0)
172 break;
173
174 res = 0;
175
176 if (arglist.password[0] != '/') {
177 /* Compare against a fixed password */
178 if (!strcmp(passwd, arglist.password))
179 break;
180 } else if (ast_test_flag(&flags,OPT_DATABASE)) {
181 char tmp[256];
182 /* Compare against a database key */
183 if (!ast_db_get(arglist.password + 1, passwd, tmp, sizeof(tmp))) {
184 /* It's a good password */
186 ast_db_del(arglist.password + 1, passwd);
187 break;
188 }
189 } else {
190 /* Compare against a file */
191 FILE *f;
192 char buf[256] = "", md5passwd[33] = "", *md5secret = NULL;
193
194 if (!(f = fopen(arglist.password, "r"))) {
195 ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno));
196 continue;
197 }
198
199 for (;;) {
200 size_t len;
201
202 if (feof(f))
203 break;
204
205 if (!fgets(buf, sizeof(buf), f)) {
206 continue;
207 }
208
209 if (ast_strlen_zero(buf))
210 continue;
211
212 len = strlen(buf) - 1;
213 if (buf[len] == '\n')
214 buf[len] = '\0';
215
217 md5secret = buf;
218 strsep(&md5secret, ":");
219 if (!md5secret)
220 continue;
221 ast_md5_hash(md5passwd, passwd);
222 if (!strcmp(md5passwd, md5secret)) {
224 ast_channel_lock(chan);
225 ast_channel_accountcode_set(chan, buf);
226 ast_channel_unlock(chan);
227 }
228 break;
229 }
230 } else {
231 if (!strcmp(passwd, buf)) {
233 ast_channel_lock(chan);
234 ast_channel_accountcode_set(chan, buf);
235 ast_channel_unlock(chan);
236 }
237 break;
238 }
239 }
240 }
241
242 fclose(f);
243
244 if (!ast_strlen_zero(buf)) {
246 if (md5secret && !strcmp(md5passwd, md5secret))
247 break;
248 } else {
249 if (!strcmp(passwd, buf))
250 break;
251 }
252 }
253 }
254 prompt = "auth-incorrect";
255 }
256
257 if ((retries < 3) && !res) {
259 ast_channel_lock(chan);
260 ast_channel_accountcode_set(chan, passwd);
261 ast_channel_unlock(chan);
262 }
263 if (!(res = ast_streamfile(chan, "auth-thankyou", ast_channel_language(chan))))
264 res = ast_waitstream(chan, "");
265 } else {
266 if (!ast_streamfile(chan, "vm-goodbye", ast_channel_language(chan)))
267 res = ast_waitstream(chan, "");
268 res = -1;
269 }
270
271 return res;
272}
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: main/db.c:427
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: main/db.c:476
static struct ast_str * prompt
Definition: asterisk.c:2763
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
static int tmp()
Definition: bt_open.c:389
#define ast_channel_lock(chan)
Definition: channel.h:2922
const char * ast_channel_language(const struct ast_channel *chan)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2805
#define ast_channel_unlock(chan)
Definition: channel.h:2923
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_UP
Definition: channelstate.h:42
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:1293
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1840
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:3056
char * strsep(char **str, const char *delims)
#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:199
unsigned int flags
Definition: utils.h:200
static struct test_options options
#define ast_test_flag(p, flag)
Definition: utils.h:63
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, strsep(), and tmp().

Referenced by load_module().

◆ load_module()

static int load_module ( void  )
static

Definition at line 279 of file app_authenticate.c.

280{
284}
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:626

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

275{
277}
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 = "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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

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