fa94238bccb80f2cd7bc8358c167591a74121091
[asterisk/asterisk.git] / apps / app_disa.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  *
7  * Made only slightly more sane by Mark Spencer <markster@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \brief DISA -- Direct Inward System Access Application
23  *
24  * \author Jim Dixon <jim@lambdatel.com>
25  *
26  * \ingroup applications
27  */
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include <math.h>
34 #include <sys/time.h>
35
36 #include "asterisk/lock.h"
37 #include "asterisk/file.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/app.h"
40 #include "asterisk/indications.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/ulaw.h"
45 #include "asterisk/callerid.h"
46 #include "asterisk/stringfields.h"
47
48 /*** DOCUMENTATION
49         <application name="DISA" language="en_US">
50                 <synopsis>
51                         Direct Inward System Access.
52                 </synopsis>
53                 <syntax>
54                         <parameter name="passcode|filename" required="true">
55                                 <para>If you need to present a DISA dialtone without entering a password,
56                                 simply set <replaceable>passcode</replaceable> to <literal>no-password</literal></para>
57                                 <para>You may specified a <replaceable>filename</replaceable> instead of a
58                                 <replaceable>passcode</replaceable>, this filename must contain individual passcodes</para>
59                         </parameter>
60                         <parameter name="context">
61                                 <para>Specifies the dialplan context in which the user-entered extension
62                                 will be matched. If no context is specified, the DISA application defaults
63                                 to the <literal>disa</literal> context. Presumably a normal system will have a special
64                                 context set up for DISA use with some or a lot of restrictions.</para>
65                         </parameter>
66                         <parameter name="cid">
67                                 <para>Specifies a new (different) callerid to be used for this call.</para>
68                         </parameter>
69                         <parameter name="mailbox" argsep="@">
70                                 <para>Will cause a stutter-dialtone (indication <emphasis>dialrecall</emphasis>)
71                                 to be used, if the specified mailbox contains any new messages.</para>
72                                 <argument name="mailbox" required="true" />
73                                 <argument name="context" required="false" />
74                         </parameter>
75                         <parameter name="options">
76                                 <optionlist>
77                                         <option name="n">
78                                                 <para>The DISA application will not answer initially.</para>
79                                         </option>
80                                         <option name="p">
81                                                 <para>The extension entered will be considered complete when a <literal>#</literal>
82                                                 is entered.</para>
83                                         </option>
84                                 </optionlist>
85                         </parameter>
86                 </syntax>
87                 <description>
88                         <para>The DISA, Direct Inward System Access, application allows someone from
89                         outside the telephone switch (PBX) to obtain an <emphasis>internal</emphasis> system
90                         dialtone and to place calls from it as if they were placing a call from
91                         within the switch.
92                         DISA plays a dialtone. The user enters their numeric passcode, followed by
93                         the pound sign <literal>#</literal>. If the passcode is correct, the user is then given
94                         system dialtone within <replaceable>context</replaceable> on which a call may be placed.
95                         If the user enters an invalid extension and extension <literal>i</literal> exists in the specified
96                         <replaceable>context</replaceable>, it will be used.
97                         </para>
98                         <para>Be aware that using this may compromise the security of your PBX.</para>
99                         <para>The arguments to this application (in <filename>extensions.conf</filename>) allow either
100                         specification of a single global <replaceable>passcode</replaceable> (that everyone uses), or
101                         individual passcodes contained in a file (<replaceable>filename</replaceable>).</para>
102                         <para>The file that contains the passcodes (if used) allows a complete
103                         specification of all of the same arguments available on the command
104                         line, with the sole exception of the options. The file may contain blank
105                         lines, or comments starting with <literal>#</literal> or <literal>;</literal>.</para>
106                 </description>
107                 <see-also>
108                         <ref type="application">Authenticate</ref>
109                         <ref type="application">VMAuthenticate</ref>
110                 </see-also>
111         </application>
112  ***/
113 static const char app[] = "DISA";
114
115 enum {
116         NOANSWER_FLAG = (1 << 0),
117         POUND_TO_END_FLAG = (1 << 1),
118 };
119
120 AST_APP_OPTIONS(app_opts, {
121         AST_APP_OPTION('n', NOANSWER_FLAG),
122         AST_APP_OPTION('p', POUND_TO_END_FLAG),
123 });
124
125 static void play_dialtone(struct ast_channel *chan, char *mailbox)
126 {
127         struct ast_tone_zone_sound *ts = NULL;
128
129         if (ast_app_has_voicemail(mailbox, NULL)) {
130                 ts = ast_get_indication_tone(chan->zone, "dialrecall");
131         } else {
132                 ts = ast_get_indication_tone(chan->zone, "dial");
133         }
134
135         if (ts) {
136                 ast_playtones_start(chan, 0, ts->data, 0);
137                 ts = ast_tone_zone_sound_unref(ts);
138         } else {
139                 ast_tonepair_start(chan, 350, 440, 0, 0);
140         }
141 }
142
143 static int disa_exec(struct ast_channel *chan, const char *data)
144 {
145         int i = 0, j, k = 0, did_ignore = 0, special_noanswer = 0;
146         int firstdigittimeout = (chan->pbx ? chan->pbx->rtimeoutms : 20000);
147         int digittimeout = (chan->pbx ? chan->pbx->dtimeoutms : 10000);
148         struct ast_flags flags;
149         char *tmp, exten[AST_MAX_EXTENSION] = "", acctcode[20]="";
150         char pwline[256];
151         char ourcidname[256],ourcidnum[256];
152         struct ast_frame *f;
153         struct timeval lastdigittime;
154         int res;
155         FILE *fp;
156         AST_DECLARE_APP_ARGS(args,
157                 AST_APP_ARG(passcode);
158                 AST_APP_ARG(context);
159                 AST_APP_ARG(cid);
160                 AST_APP_ARG(mailbox);
161                 AST_APP_ARG(options);
162         );
163
164         if (ast_strlen_zero(data)) {
165                 ast_log(LOG_WARNING, "DISA requires an argument (passcode/passcode file)\n");
166                 return -1;
167         }
168
169         ast_debug(1, "Digittimeout: %d\n", digittimeout);
170         ast_debug(1, "Responsetimeout: %d\n", firstdigittimeout);
171
172         tmp = ast_strdupa(data);
173
174         AST_STANDARD_APP_ARGS(args, tmp);
175
176         if (ast_strlen_zero(args.context))
177                 args.context = "disa";
178         if (ast_strlen_zero(args.mailbox))
179                 args.mailbox = "";
180         if (!ast_strlen_zero(args.options))
181                 ast_app_parse_options(app_opts, &flags, NULL, args.options);
182
183         ast_debug(1, "Mailbox: %s\n",args.mailbox);
184
185         if (!ast_test_flag(&flags, NOANSWER_FLAG)) {
186                 if (chan->_state != AST_STATE_UP) {
187                         /* answer */
188                         ast_answer(chan);
189                 }
190         } else
191                 special_noanswer = 1;
192
193         ast_debug(1, "Context: %s\n",args.context);
194
195         if (!strcasecmp(args.passcode, "no-password")) {
196                 k |= 1; /* We have the password */
197                 ast_debug(1, "DISA no-password login success\n");
198         }
199
200         lastdigittime = ast_tvnow();
201
202         play_dialtone(chan, args.mailbox);
203
204         ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
205
206         for (;;) {
207                   /* if outa time, give em reorder */
208                 if (ast_tvdiff_ms(ast_tvnow(), lastdigittime) > ((k&2) ? digittimeout : firstdigittimeout)) {
209                         ast_debug(1,"DISA %s entry timeout on chan %s\n",
210                                 ((k&1) ? "extension" : "password"),chan->name);
211                         break;
212                 }
213
214                 if ((res = ast_waitfor(chan, -1) < 0)) {
215                         ast_debug(1, "Waitfor returned %d\n", res);
216                         continue;
217                 }
218
219                 if (!(f = ast_read(chan))) {
220                         ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
221                         return -1;
222                 }
223
224                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
225                         if (f->data.uint32)
226                                 chan->hangupcause = f->data.uint32;
227                         ast_frfree(f);
228                         ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
229                         return -1;
230                 }
231
232                 /* If the frame coming in is not DTMF, just drop it and continue */
233                 if (f->frametype != AST_FRAME_DTMF) {
234                         ast_frfree(f);
235                         continue;
236                 }
237
238                 j = f->subclass;  /* save digit */
239                 ast_frfree(f);
240
241                 if (!i) {
242                         k |= 2; /* We have the first digit */
243                         ast_playtones_stop(chan);
244                 }
245
246                 lastdigittime = ast_tvnow();
247
248                 /* got a DTMF tone */
249                 if (i < AST_MAX_EXTENSION) { /* if still valid number of digits */
250                         if (!(k&1)) { /* if in password state */
251                                 if (j == '#') { /* end of password */
252                                           /* see if this is an integer */
253                                         if (sscanf(args.passcode,"%30d",&j) < 1) { /* nope, it must be a filename */
254                                                 fp = fopen(args.passcode,"r");
255                                                 if (!fp) {
256                                                         ast_log(LOG_WARNING,"DISA password file %s not found on chan %s\n",args.passcode,chan->name);
257                                                         ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
258                                                         return -1;
259                                                 }
260                                                 pwline[0] = 0;
261                                                 while(fgets(pwline,sizeof(pwline) - 1,fp)) {
262                                                         if (!pwline[0])
263                                                                 continue;
264                                                         if (pwline[strlen(pwline) - 1] == '\n')
265                                                                 pwline[strlen(pwline) - 1] = 0;
266                                                         if (!pwline[0])
267                                                                 continue;
268                                                          /* skip comments */
269                                                         if (pwline[0] == '#')
270                                                                 continue;
271                                                         if (pwline[0] == ';')
272                                                                 continue;
273
274                                                         AST_STANDARD_APP_ARGS(args, pwline);
275
276                                                         ast_debug(1, "Mailbox: %s\n",args.mailbox);
277
278                                                         /* password must be in valid format (numeric) */
279                                                         if (sscanf(args.passcode,"%30d", &j) < 1)
280                                                                 continue;
281                                                          /* if we got it */
282                                                         if (!strcmp(exten,args.passcode)) {
283                                                                 if (ast_strlen_zero(args.context))
284                                                                         args.context = "disa";
285                                                                 if (ast_strlen_zero(args.mailbox))
286                                                                         args.mailbox = "";
287                                                                 break;
288                                                         }
289                                                 }
290                                                 fclose(fp);
291                                         }
292                                         /* compare the two */
293                                         if (strcmp(exten,args.passcode)) {
294                                                 ast_log(LOG_WARNING,"DISA on chan %s got bad password %s\n",chan->name,exten);
295                                                 goto reorder;
296
297                                         }
298                                          /* password good, set to dial state */
299                                         ast_debug(1,"DISA on chan %s password is good\n",chan->name);
300                                         play_dialtone(chan, args.mailbox);
301
302                                         k|=1; /* In number mode */
303                                         i = 0;  /* re-set buffer pointer */
304                                         exten[sizeof(acctcode)] = 0;
305                                         ast_copy_string(acctcode, exten, sizeof(acctcode));
306                                         exten[0] = 0;
307                                         ast_debug(1,"Successful DISA log-in on chan %s\n", chan->name);
308                                         continue;
309                                 }
310                         } else {
311                                 if (j == '#') { /* end of extension .. maybe */
312                                         if (i == 0 && 
313                                                         (ast_matchmore_extension(chan, args.context, "#", 1, chan->cid.cid_num) ||
314                                                          ast_exists_extension(chan, args.context, "#", 1, chan->cid.cid_num)) ) {
315                                                 /* Let the # be the part of, or the entire extension */
316                                         } else {
317                                                 break;
318                                         }
319                                 }
320                         }
321
322                         exten[i++] = j;  /* save digit */
323                         exten[i] = 0;
324                         if (!(k&1))
325                                 continue; /* if getting password, continue doing it */
326                         /* if this exists */
327
328                         /* user wants end of number, remove # */
329                         if (ast_test_flag(&flags, POUND_TO_END_FLAG) && j == '#') {
330                                 exten[--i] = 0;
331                                 break;
332                         }
333
334                         if (ast_ignore_pattern(args.context, exten)) {
335                                 play_dialtone(chan, "");
336                                 did_ignore = 1;
337                         } else
338                                 if (did_ignore) {
339                                         ast_playtones_stop(chan);
340                                         did_ignore = 0;
341                                 }
342
343                         /* if can do some more, do it */
344                         if (!ast_matchmore_extension(chan,args.context,exten,1, chan->cid.cid_num)) {
345                                 break;
346                         }
347                 }
348         }
349
350         ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
351
352         if (k == 3) {
353                 int recheck = 0;
354                 struct ast_flags cdr_flags = { AST_CDR_FLAG_POSTED };
355
356                 if (!ast_exists_extension(chan, args.context, exten, 1, chan->cid.cid_num)) {
357                         pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
358                         exten[0] = 'i';
359                         exten[1] = '\0';
360                         recheck = 1;
361                 }
362                 if (!recheck || ast_exists_extension(chan, args.context, exten, 1, chan->cid.cid_num)) {
363                         ast_playtones_stop(chan);
364                         /* We're authenticated and have a target extension */
365                         if (!ast_strlen_zero(args.cid)) {
366                                 ast_callerid_split(args.cid, ourcidname, sizeof(ourcidname), ourcidnum, sizeof(ourcidnum));
367                                 ast_set_callerid(chan, ourcidnum, ourcidname, ourcidnum);
368                         }
369
370                         if (!ast_strlen_zero(acctcode))
371                                 ast_string_field_set(chan, accountcode, acctcode);
372
373                         if (special_noanswer) cdr_flags.flags = 0;
374                         ast_cdr_reset(chan->cdr, &cdr_flags);
375                         ast_explicit_goto(chan, args.context, exten, 1);
376                         return 0;
377                 }
378         }
379
380         /* Received invalid, but no "i" extension exists in the given context */
381
382 reorder:
383         /* Play congestion for a bit */
384         ast_indicate(chan, AST_CONTROL_CONGESTION);
385         ast_safe_sleep(chan, 10*1000);
386
387         ast_playtones_stop(chan);
388
389         return -1;
390 }
391
392 static int unload_module(void)
393 {
394         return ast_unregister_application(app);
395 }
396
397 static int load_module(void)
398 {
399         return ast_register_application_xml(app, disa_exec) ?
400                 AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
401 }
402
403 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "DISA (Direct Inward System Access) Application");