Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
app_authenticate.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*! \file
20 *
21 * \brief Execute arbitrary authenticate commands
22 *
23 * \author Mark Spencer <markster@digium.com>
24 *
25 * \ingroup applications
26 */
27
28/*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32#include "asterisk.h"
33
34#include "asterisk/lock.h"
35#include "asterisk/file.h"
36#include "asterisk/channel.h"
37#include "asterisk/pbx.h"
38#include "asterisk/module.h"
39#include "asterisk/app.h"
40#include "asterisk/astdb.h"
41#include "asterisk/utils.h"
42
43enum {
44 OPT_ACCOUNT = (1 << 0),
45 OPT_DATABASE = (1 << 1),
46 OPT_MULTIPLE = (1 << 3),
47 OPT_REMOVE = (1 << 4),
48};
49
55});
56
57
58static const char app[] = "Authenticate";
59/*** DOCUMENTATION
60 <application name="Authenticate" language="en_US">
61 <since>
62 <version>0.4.0</version>
63 </since>
64 <synopsis>
65 Authenticate a user
66 </synopsis>
67 <syntax>
68 <parameter name="password" required="true">
69 <para>Password the user should know</para>
70 </parameter>
71 <parameter name="options" required="false">
72 <optionlist>
73 <option name="a">
74 <para>Set the channels' account code to the password that is entered</para>
75 </option>
76 <option name="d">
77 <para>Interpret the given path as database key, not a literal file.</para>
78 <note>
79 <para>The value is not used at all in the authentication when using this option.
80 If the family/key is set to <literal>/pin/100</literal> (value does not matter)
81 then the password field needs to be set to <literal>/pin</literal> and the pin entered
82 by the user would be authenticated against <literal>100</literal>.</para>
83 </note>
84 </option>
85 <option name="m">
86 <para>Interpret the given path as a file which contains a list of account
87 codes and password hashes delimited with <literal>:</literal>, listed one per line in
88 the file. When one of the passwords is matched, the channel will have
89 its account code set to the corresponding account code in the file.</para>
90 </option>
91 <option name="r">
92 <para>Remove the database key upon successful entry (valid with <literal>d</literal> only)</para>
93 </option>
94 </optionlist>
95 </parameter>
96 <parameter name="maxdigits" required="false">
97 <para>maximum acceptable number of digits. Stops reading after
98 maxdigits have been entered (without requiring the user to press the <literal>#</literal> key).
99 Defaults to 0 - no limit - wait for the user press the <literal>#</literal> key.</para>
100 </parameter>
101 <parameter name="prompt" required="false" argsep="&amp;">
102 <para>Override the &quot;agent-pass&quot; sound file. Can be
103 an ampersand separated list of filenames. If the filename
104 is a relative filename (it does not begin with a slash), it
105 will be searched for in the Asterisk sounds directory. If the
106 filename is able to be parsed as a URL, Asterisk will
107 download the file and then begin playback on it. To include a
108 literal <literal>&amp;</literal> in the URL you can enclose
109 the URL in single quotes.</para>
110 <argument name="prompt" required="true" />
111 <argument name="prompt2" multiple="true" />
112 </parameter>
113 </syntax>
114 <description>
115 <para>This application asks the caller to enter a given password in order to continue dialplan execution.</para>
116 <para>If the password begins with the <literal>/</literal> character,
117 it is interpreted as a file which contains a list of valid passwords, listed 1 password per line in the file.</para>
118 <para>When using a database key, the value associated with the key can be anything.</para>
119 <para>Users have three attempts to authenticate before the channel is hung up.</para>
120 </description>
121 <see-also>
122 <ref type="application">VMAuthenticate</ref>
123 <ref type="application">DISA</ref>
124 </see-also>
125 </application>
126 ***/
127
128static int auth_exec(struct ast_channel *chan, const char *data)
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}
276
277static int unload_module(void)
278{
280}
281
282static int load_module(void)
283{
287}
288
289AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Authentication Application");
@ OPT_MULTIPLE
@ OPT_DATABASE
@ OPT_ACCOUNT
@ OPT_REMOVE
static const char app[]
static int auth_exec(struct ast_channel *chan, const char *data)
static const struct ast_app_option auth_app_options[128]
static int load_module(void)
static int unload_module(void)
Persistent data storage (akin to *doze registry)
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:2786
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
#define ast_channel_lock(chan)
Definition: channel.h:2970
const char * ast_channel_language(const struct ast_channel *chan)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2834
#define ast_channel_unlock(chan)
Definition: channel.h:2971
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
Generic File Format Support. Should be included by clients of the file handling routines....
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1301
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1848
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
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.
#define AST_APP_OPTION(option, flagno)
Declares an application option that does not accept an argument.
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
Asterisk locking-related definitions:
int errno
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
@ 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
Core PBX routines and definitions.
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
Structure used to handle boolean flags.
Definition: utils.h:199
unsigned int flags
Definition: utils.h:200
static struct test_options options
Utility functions.
#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