app_disa.c 13 KB

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