04092573810246f838c0ba6017ea6cf945d61105
[asterisk/asterisk.git] / apps / app_authenticate.c
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 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <errno.h>
33
34 #include "asterisk/lock.h"
35 #include "asterisk/file.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/app.h"
41 #include "asterisk/astdb.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/options.h"
44
45 enum {
46         OPT_ACCOUNT = (1 << 0),
47         OPT_DATABASE = (1 << 1),
48         OPT_MULTIPLE = (1 << 3),
49         OPT_REMOVE = (1 << 4),
50 } auth_option_flags;
51
52 AST_APP_OPTIONS(auth_app_options, {
53         AST_APP_OPTION('a', OPT_ACCOUNT),
54         AST_APP_OPTION('d', OPT_DATABASE),
55         AST_APP_OPTION('m', OPT_MULTIPLE),
56         AST_APP_OPTION('r', OPT_REMOVE),
57 });
58
59
60 static char *app = "Authenticate";
61
62 static char *synopsis = "Authenticate a user";
63
64 static char *descrip =
65 "  Authenticate(password[,options[,maxdigits]]): This application asks the caller\n"
66 "to enter a given password in order to continue dialplan execution. If the password\n"
67 "begins with the '/' character, it is interpreted as a file which contains a list of\n"
68 "valid passwords, listed 1 password per line in the file.\n"
69 "  When using a database key, the value associated with the key can be anything.\n"
70 "Users have three attempts to authenticate before the channel is hung up.\n"
71 "  Options:\n"
72 "     a - Set the channels' account code to the password that is entered\n"
73 "     d - Interpret the given path as database key, not a literal file\n"
74 "     m - Interpret the given path as a file which contains a list of account\n"
75 "         codes and password hashes delimited with ':', listed one per line in\n"
76 "         the file. When one of the passwords is matched, the channel will have\n"
77 "         its account code set to the corresponding account code in the file.\n"
78 "     r - Remove the database key upon successful entry (valid with 'd' only)\n"
79 "     maxdigits  - maximum acceptable number of digits. Stops reading after\n"
80 "         maxdigits have been entered (without requiring the user to\n"
81 "         press the '#' key).\n"
82 "         Defaults to 0 - no limit - wait for the user press the '#' key.\n"
83 ;
84
85 static int auth_exec(struct ast_channel *chan, void *data)
86 {
87         int res = 0, retries, maxdigits;
88         char passwd[256], *prompt = "agent-pass", *argcopy = NULL;
89         struct ast_flags flags = {0};
90
91         AST_DECLARE_APP_ARGS(arglist,
92                 AST_APP_ARG(password);
93                 AST_APP_ARG(options);
94                 AST_APP_ARG(maxdigits);
95         );
96         
97         if (ast_strlen_zero(data)) {
98                 ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n");
99                 return -1;
100         }
101
102         if (chan->_state != AST_STATE_UP) {
103                 if ((res = ast_answer(chan)))
104                         return -1;
105         }
106         
107         argcopy = ast_strdupa(data);
108
109         AST_STANDARD_APP_ARGS(arglist, argcopy);
110         
111         if (!ast_strlen_zero(arglist.options))
112                 ast_app_parse_options(auth_app_options, &flags, NULL, arglist.options);
113
114         if (!ast_strlen_zero(arglist.maxdigits)) {
115                 maxdigits = atoi(arglist.maxdigits);
116                 if ((maxdigits<1) || (maxdigits>sizeof(passwd)-2))
117                         maxdigits = sizeof(passwd) - 2;
118         } else {
119                 maxdigits = sizeof(passwd) - 2;
120         }
121
122         /* Start asking for password */
123         for (retries = 0; retries < 3; retries++) {
124                 if ((res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0)) < 0)
125                         break;
126                 res = 0;
127                 if (arglist.password[0] == '/') {
128                         if (ast_test_flag(&flags,OPT_DATABASE)) {
129                                 char tmp[256];
130                                 /* Compare against a database key */
131                                 if (!ast_db_get(arglist.password + 1, passwd, tmp, sizeof(tmp))) {
132                                         /* It's a good password */
133                                         if (ast_test_flag(&flags,OPT_REMOVE))
134                                                 ast_db_del(arglist.password + 1, passwd);
135                                         break;
136                                 }
137                         } else {
138                                 /* Compare against a file */
139                                 FILE *f;
140                                 char buf[256] = "", md5passwd[33] = "", *md5secret = NULL;
141                                 
142                                 if (!(f = fopen(arglist.password, "r"))) {
143                                         ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno));
144                                         continue;
145                                 }
146
147                                 while (!feof(f)) {
148                                         fgets(buf, sizeof(buf), f);
149                                         if (!feof(f) && !ast_strlen_zero(buf)) {
150                                                 buf[strlen(buf) - 1] = '\0';
151                                                 if (ast_test_flag(&flags,OPT_MULTIPLE)) {
152                                                         md5secret = strchr(buf, ':');
153                                                         if (md5secret == NULL)
154                                                                 continue;
155                                                         *md5secret = '\0';
156                                                         md5secret++;
157                                                         ast_md5_hash(md5passwd, passwd);
158                                                         if (!strcmp(md5passwd, md5secret)) {
159                                                                 if (ast_test_flag(&flags,OPT_ACCOUNT))
160                                                                         ast_cdr_setaccount(chan, buf);
161                                                                 break;
162                                                         }
163                                                 } else {
164                                                         if (!strcmp(passwd, buf)) {
165                                                                 if (ast_test_flag(&flags,OPT_ACCOUNT))
166                                                                         ast_cdr_setaccount(chan, buf);
167                                                                 break;
168                                                         }
169                                                 }
170                                         }
171                                 }
172                                 fclose(f);
173                                 if (!ast_strlen_zero(buf)) {
174                                         if (ast_test_flag(&flags,OPT_MULTIPLE)) {
175                                                 if (md5secret && !strcmp(md5passwd, md5secret))
176                                                         break;
177                                         } else {
178                                                 if (!strcmp(passwd, buf))
179                                                         break;
180                                         }
181                                 }
182                         }
183                 } else {
184                         /* Compare against a fixed password */
185                         if (!strcmp(passwd, arglist.password)) 
186                                 break;
187                 }
188                 prompt = "auth-incorrect";
189         }
190         if ((retries < 3) && !res) {
191                 if (ast_test_flag(&flags,OPT_ACCOUNT) && !ast_test_flag(&flags,OPT_MULTIPLE)) 
192                         ast_cdr_setaccount(chan, passwd);
193                 if (!(res = ast_streamfile(chan, "auth-thankyou", chan->language)))
194                         res = ast_waitstream(chan, "");
195         } else {
196                 if (!ast_streamfile(chan, "vm-goodbye", chan->language))
197                         res = ast_waitstream(chan, "");
198                 res = -1;
199         }
200
201         return res;
202 }
203
204 static int unload_module(void)
205 {
206         return ast_unregister_application(app);
207 }
208
209 static int load_module(void)
210 {
211         return ast_register_application(app, auth_exec, synopsis, descrip);
212 }
213
214 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Authentication Application");