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