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