Applications no longer need to call ast_module_user_add and ast_module_user_remove...
[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;
92         int retries;
93         char passwd[256];
94         char *prompt;
95         int maxdigits;
96         char *argcopy =NULL;
97         struct ast_flags flags = {0};
98
99         AST_DECLARE_APP_ARGS(arglist,
100                 AST_APP_ARG(password);
101                 AST_APP_ARG(options);
102                 AST_APP_ARG(maxdigits);
103         );
104         
105         if (ast_strlen_zero(data)) {
106                 ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n");
107                 return -1;
108         }
109
110         if (chan->_state != AST_STATE_UP) {
111                 if ((res = ast_answer(chan)))
112                         return -1;
113         }
114         
115         argcopy = ast_strdupa(data);
116
117         AST_STANDARD_APP_ARGS(arglist,argcopy);
118         
119         if (!ast_strlen_zero(arglist.options)) {
120                 ast_app_parse_options(auth_app_options, &flags, NULL, arglist.options);
121         }
122
123         if (!ast_strlen_zero(arglist.maxdigits)) {
124                 maxdigits = atoi(arglist.maxdigits);
125                 if ((maxdigits<1) || (maxdigits>sizeof(passwd)-2))
126                         maxdigits = sizeof(passwd) - 2;
127         } else {
128                 maxdigits = sizeof(passwd) - 2;
129         }
130
131         /* Start asking for password */
132         prompt = "agent-pass";
133         for (retries = 0; retries < 3; retries++) {
134                 res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0);
135                 if (res < 0)
136                         break;
137                 res = 0;
138                 if (arglist.password[0] == '/') {
139                         if (ast_test_flag(&flags,OPT_DATABASE)) {
140                                 char tmp[256];
141                                 /* Compare against a database key */
142                                 if (!ast_db_get(arglist.password + 1, passwd, tmp, sizeof(tmp))) {
143                                         /* It's a good password */
144                                         if (ast_test_flag(&flags,OPT_REMOVE)) {
145                                                 ast_db_del(arglist.password + 1, passwd);
146                                         }
147                                         break;
148                                 }
149                         } else {
150                                 /* Compare against a file */
151                                 FILE *f;
152                                 f = fopen(arglist.password, "r");
153                                 if (f) {
154                                         char buf[256] = "";
155                                         char md5passwd[33] = "";
156                                         char *md5secret = NULL;
157
158                                         while (!feof(f)) {
159                                                 fgets(buf, sizeof(buf), f);
160                                                 if (!feof(f) && !ast_strlen_zero(buf)) {
161                                                         buf[strlen(buf) - 1] = '\0';
162                                                         if (ast_test_flag(&flags,OPT_MULTIPLE)) {
163                                                                 md5secret = strchr(buf, ':');
164                                                                 if (md5secret == NULL)
165                                                                         continue;
166                                                                 *md5secret = '\0';
167                                                                 md5secret++;
168                                                                 ast_md5_hash(md5passwd, passwd);
169                                                                 if (!strcmp(md5passwd, md5secret)) {
170                                                                         if (ast_test_flag(&flags,OPT_ACCOUNT))
171                                                                                 ast_cdr_setaccount(chan, buf);
172                                                                         break;
173                                                                 }
174                                                         } else {
175                                                                 if (!strcmp(passwd, buf)) {
176                                                                         if (ast_test_flag(&flags,OPT_ACCOUNT))
177                                                                                 ast_cdr_setaccount(chan, buf);
178                                                                         break;
179                                                                 }
180                                                         }
181                                                 }
182                                         }
183                                         fclose(f);
184                                         if (!ast_strlen_zero(buf)) {
185                                                 if (ast_test_flag(&flags,OPT_MULTIPLE)) {
186                                                         if (md5secret && !strcmp(md5passwd, md5secret))
187                                                                 break;
188                                                 } else {
189                                                         if (!strcmp(passwd, buf))
190                                                                 break;
191                                                 }
192                                         }
193                                 } else 
194                                         ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno));
195                         }
196                 } else {
197                         /* Compare against a fixed password */
198                         if (!strcmp(passwd, arglist.password)) 
199                                 break;
200                 }
201                 prompt="auth-incorrect";
202         }
203         if ((retries < 3) && !res) {
204                 if (ast_test_flag(&flags,OPT_ACCOUNT) && !ast_test_flag(&flags,OPT_MULTIPLE)) 
205                         ast_cdr_setaccount(chan, passwd);
206                 res = ast_streamfile(chan, "auth-thankyou", chan->language);
207                 if (!res)
208                         res = ast_waitstream(chan, "");
209         } else {
210                 if (!ast_streamfile(chan, "vm-goodbye", chan->language))
211                         res = ast_waitstream(chan, "");
212                 res = -1;
213         }
214
215         return res;
216 }
217
218 static int unload_module(void)
219 {
220         return ast_unregister_application(app);
221 }
222
223 static int load_module(void)
224 {
225         return ast_register_application(app, auth_exec, synopsis, descrip);
226 }
227
228 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Authentication Application");