Debian Codegraph @

apps/app_authenticate.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <[email protected]>
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 <[email protected]>
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
43 enum {
44 OPT_ACCOUNT = (1 << 0),
45 OPT_DATABASE = (1 << 1),
46 OPT_MULTIPLE = (1 << 3),
47 OPT_REMOVE = (1 << 4),
48 };
49
50 AST_APP_OPTIONS(auth_app_options, {
51 AST_APP_OPTION('a', OPT_ACCOUNT),
52 AST_APP_OPTION('d', OPT_DATABASE),
53 AST_APP_OPTION('m', OPT_MULTIPLE),
54 AST_APP_OPTION('r', OPT_REMOVE),
55 });
56
57
58 static 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
128 static 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);
136 AST_APP_ARG(options);
137 AST_APP_ARG(maxdigits);
138 AST_APP_ARG(prompt);
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))
156 ast_app_parse_options(auth_app_options, &flags, NULL, 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 */
188 if (ast_test_flag(&flags,OPT_REMOVE))
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
219 if (ast_test_flag(&flags, OPT_MULTIPLE)) {
220 md5secret = buf;
221 strsep(&md5secret, ":");
222 if (!md5secret)
223 continue;
224 ast_md5_hash(md5passwd, passwd);
225 if (!strcmp(md5passwd, md5secret)) {
226 if (ast_test_flag(&flags, OPT_ACCOUNT)) {
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)) {
235 if (ast_test_flag(&flags, OPT_ACCOUNT)) {
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)) {
248 if (ast_test_flag(&flags, OPT_MULTIPLE)) {
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) {
261 if (ast_test_flag(&flags,OPT_ACCOUNT) && !ast_test_flag(&flags,OPT_MULTIPLE)) {
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
277 static int unload_module(void)
278 {
279 return ast_unregister_application(app);
280 }
281
282 static int load_module(void)
283 {
284 if (ast_register_application_xml(app, auth_exec))
285 return AST_MODULE_LOAD_DECLINE;
286 return AST_MODULE_LOAD_SUCCESS;
287 }
288
289 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Authentication Application");