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