app_disa.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * DISA -- Direct Inward System Access Application 6/20/2001
  5. *
  6. * Copyright (C) 2001, Linux Support Services, Inc.
  7. *
  8. * Jim Dixon <jim@lambdatel.com>
  9. *
  10. * Made only slightly more sane by Mark Spencer <markster@digium.com>
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License
  14. */
  15. #include <asterisk/lock.h>
  16. #include <asterisk/file.h>
  17. #include <asterisk/logger.h>
  18. #include <asterisk/channel.h>
  19. #include <asterisk/indications.h>
  20. #include <asterisk/pbx.h>
  21. #include <asterisk/module.h>
  22. #include <asterisk/translate.h>
  23. #include <asterisk/ulaw.h>
  24. #include <asterisk/utils.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <math.h>
  29. #include <sys/time.h>
  30. static char *tdesc = "DISA (Direct Inward System Access) Application";
  31. static char *app = "DISA";
  32. static char *synopsis = "DISA (Direct Inward System Access)";
  33. static char *descrip =
  34. "DISA (Direct Inward System Access) -- Allows someone from outside\n"
  35. "the telephone switch (PBX) to obtain an \"internal\" system dialtone\n"
  36. "and to place calls from it as if they were placing a call from within\n"
  37. "the switch. A user calls a number that connects to the DISA application\n"
  38. "and is given dialtone. The user enters their passcode, followed by the\n"
  39. "pound sign (#). If the passcode is correct, the user is then given\n"
  40. "system dialtone on which a call may be placed. Obviously, this type\n"
  41. "of access has SERIOUS security implications, and GREAT care must be\n"
  42. "taken NOT to compromise your security.\n\n"
  43. "There is a possibility of accessing DISA without password. Simply\n"
  44. "exchange your password with no-password.\n\n"
  45. " Example: exten => s,1,DISA,no-password|local\n\n"
  46. "but be aware of using this for your security compromising.\n\n"
  47. "The arguments to this application (in extensions.conf) allow either\n"
  48. "specification of a single global password (that everyone uses), or\n"
  49. "individual passwords contained in a file. It also allow specification\n"
  50. "of the context on which the user will be dialing. If no context is\n"
  51. "specified, the DISA application defaults the context to \"disa\"\n"
  52. "presumably that a normal system will have a special context set up\n"
  53. "for DISA use with some or a lot of restrictions. The arguments are\n"
  54. "one of the following:\n\n"
  55. " numeric-passcode\n"
  56. " numeric-passcode|context\n"
  57. " full-pathname-of-file-that-contains-passcodes\n\n"
  58. "The file that contains the passcodes (if used) allows specification\n"
  59. "of either just a passcode (defaulting to the \"disa\" context, or\n"
  60. "passcode|context on each line of the file. The file may contain blank\n"
  61. "lines, or comments starting with \"#\" or \";\". In addition, the\n"
  62. "above arguments may have |new-callerid-string appended to them, to\n"
  63. "specify a new (different) callerid to be used for this call, for\n"
  64. "example: numeric-passcode|context|\"My Phone\" <(234) 123-4567> or \n"
  65. "full-pathname-of-passcode-file|\"My Phone\" <(234) 123-4567>. Note that\n"
  66. "in the case of specifying the numeric-passcode, the context must be\n"
  67. "specified if the callerid is specified also.\n\n"
  68. "If login is successful, the application parses the dialed number in\n"
  69. "the specified (or default) context, and returns 0 with the new extension\n"
  70. "context filled-in and the priority set to 1, so that the PBX may\n"
  71. "re-apply the routing tables to it and complete the call normally.";
  72. STANDARD_LOCAL_USER;
  73. LOCAL_USER_DECL;
  74. static int firstdigittimeout = 20000; /* 20 seconds first digit timeout */
  75. static int digittimeout = 10000; /* 10 seconds subsequent digit timeout */
  76. static int ms_diff(struct timeval *tv1, struct timeval *tv2)
  77. {
  78. int ms;
  79. ms = (tv1->tv_sec - tv2->tv_sec) * 1000;
  80. ms += (tv1->tv_usec - tv2->tv_usec) / 1000;
  81. return(ms);
  82. }
  83. static void play_dialtone(struct ast_channel *chan)
  84. {
  85. const struct tone_zone_sound *ts = NULL;
  86. ts = ast_get_indication_tone(chan->zone, "dial");
  87. if (ts)
  88. ast_playtones_start(chan, 0, ts->data, 0);
  89. else
  90. ast_tonepair_start(chan, 350, 440, 0, 0);
  91. }
  92. static int disa_exec(struct ast_channel *chan, void *data)
  93. {
  94. int i,j,k,x,did_ignore;
  95. struct localuser *u;
  96. char tmp[256],arg2[256]="",exten[AST_MAX_EXTENSION],acctcode[20]="";
  97. char *ourcontext,*ourcallerid;
  98. struct ast_frame *f;
  99. struct timeval lastout, now, lastdigittime;
  100. int res;
  101. time_t rstart;
  102. FILE *fp;
  103. char *stringp=NULL;
  104. if (ast_set_write_format(chan,AST_FORMAT_ULAW))
  105. {
  106. ast_log(LOG_WARNING, "Unable to set write format to Mu-law on %s\n",chan->name);
  107. return -1;
  108. }
  109. if (ast_set_read_format(chan,AST_FORMAT_ULAW))
  110. {
  111. ast_log(LOG_WARNING, "Unable to set read format to Mu-law on %s\n",chan->name);
  112. return -1;
  113. }
  114. lastout.tv_sec = lastout.tv_usec = 0;
  115. if (!data || !strlen((char *)data)) {
  116. ast_log(LOG_WARNING, "disa requires an argument (passcode/passcode file)\n");
  117. return -1;
  118. }
  119. strncpy(tmp, (char *)data, sizeof(tmp)-1);
  120. stringp=tmp;
  121. strsep(&stringp, "|");
  122. ourcontext = strsep(&stringp, "|");
  123. /* if context specified, save 2nd arg and parse third */
  124. if (ourcontext) {
  125. strncpy(arg2,ourcontext, sizeof(arg2) - 1);
  126. ourcallerid = strsep(&stringp,"|");
  127. }
  128. /* if context not specified, use "disa" */
  129. else {
  130. arg2[0] = 0;
  131. ourcallerid = NULL;
  132. ourcontext = "disa";
  133. }
  134. LOCAL_USER_ADD(u);
  135. if (chan->_state != AST_STATE_UP)
  136. {
  137. /* answer */
  138. ast_answer(chan);
  139. }
  140. i = k = x = 0; /* k is 0 for pswd entry, 1 for ext entry */
  141. did_ignore = 0;
  142. exten[0] = 0;
  143. acctcode[0] = 0;
  144. /* can we access DISA without password? */
  145. ast_log(LOG_DEBUG, "Context: %s\n",ourcontext);
  146. if (!strcasecmp(tmp, "no-password"))
  147. {;
  148. k |= 1; /* We have the password */
  149. ast_log(LOG_DEBUG, "DISA no-password login success\n");
  150. }
  151. gettimeofday(&lastdigittime,NULL);
  152. play_dialtone(chan);
  153. for(;;)
  154. {
  155. gettimeofday(&now,NULL);
  156. /* if outa time, give em reorder */
  157. if (ms_diff(&now,&lastdigittime) >
  158. ((k&2) ? digittimeout : firstdigittimeout))
  159. {
  160. ast_log(LOG_DEBUG,"DISA %s entry timeout on chan %s\n",
  161. ((k&1) ? "extension" : "password"),chan->name);
  162. break;
  163. }
  164. if ((res = ast_waitfor(chan, -1) < 0)) {
  165. ast_log(LOG_DEBUG, "Waitfor returned %d\n", res);
  166. continue;
  167. }
  168. f = ast_read(chan);
  169. if (f == NULL)
  170. {
  171. LOCAL_USER_REMOVE(u);
  172. return -1;
  173. }
  174. if ((f->frametype == AST_FRAME_CONTROL) &&
  175. (f->subclass == AST_CONTROL_HANGUP))
  176. {
  177. ast_frfree(f);
  178. LOCAL_USER_REMOVE(u);
  179. return -1;
  180. }
  181. if (f->frametype == AST_FRAME_VOICE) {
  182. ast_frfree(f);
  183. continue;
  184. }
  185. /* if not DTMF, just do it again */
  186. if (f->frametype != AST_FRAME_DTMF)
  187. {
  188. ast_frfree(f);
  189. continue;
  190. }
  191. j = f->subclass; /* save digit */
  192. ast_frfree(f);
  193. if (i == 0)
  194. {
  195. k|=2; /* We have the first digit */
  196. ast_playtones_stop(chan);
  197. }
  198. gettimeofday(&lastdigittime,NULL);
  199. /* got a DTMF tone */
  200. if (i < AST_MAX_EXTENSION) /* if still valid number of digits */
  201. {
  202. if (!(k&1)) /* if in password state */
  203. {
  204. if (j == '#') /* end of password */
  205. {
  206. /* see if this is an integer */
  207. if (sscanf(tmp,"%d",&j) < 1)
  208. { /* nope, it must be a filename */
  209. fp = fopen(tmp,"r");
  210. if (!fp)
  211. {
  212. ast_log(LOG_WARNING,"DISA password file %s not found on chan %s\n",tmp,chan->name);
  213. LOCAL_USER_REMOVE(u);
  214. return -1;
  215. }
  216. tmp[0] = 0;
  217. while(fgets(tmp,sizeof(tmp) - 1,fp))
  218. {
  219. char *stringp=NULL,*stringp2;
  220. if (!tmp[0]) continue;
  221. if (tmp[strlen(tmp) - 1] == '\n')
  222. tmp[strlen(tmp) - 1] = 0;
  223. if (!tmp[0]) continue;
  224. /* skip comments */
  225. if (tmp[0] == '#') continue;
  226. if (tmp[0] == ';') continue;
  227. stringp=tmp;
  228. strsep(&stringp, "|");
  229. stringp2=strsep(&stringp, "|");
  230. if (stringp2) {
  231. ourcontext=stringp2;
  232. stringp2=strsep(&stringp, "|");
  233. if (stringp2) ourcallerid=stringp2;
  234. }
  235. /* password must be in valid format (numeric) */
  236. if (sscanf(tmp,"%d",&j) < 1) continue;
  237. /* if we got it */
  238. if (!strcmp(exten,tmp)) break;
  239. }
  240. fclose(fp);
  241. }
  242. /* compare the two */
  243. if (strcmp(exten,tmp))
  244. {
  245. ast_log(LOG_WARNING,"DISA on chan %s got bad password %s\n",chan->name,exten);
  246. goto reorder;
  247. }
  248. /* password good, set to dial state */
  249. ast_log(LOG_DEBUG,"DISA on chan %s password is good\n",chan->name);
  250. play_dialtone(chan);
  251. k|=1; /* In number mode */
  252. i = 0; /* re-set buffer pointer */
  253. exten[sizeof(acctcode)] = 0;
  254. strncpy(acctcode,exten, sizeof(acctcode) - 1);
  255. exten[0] = 0;
  256. ast_log(LOG_DEBUG,"Successful DISA log-in on chan %s\n",chan->name);
  257. continue;
  258. }
  259. }
  260. exten[i++] = j; /* save digit */
  261. exten[i] = 0;
  262. if (!(k&1)) continue; /* if getting password, continue doing it */
  263. /* if this exists */
  264. if (ast_ignore_pattern(ourcontext, exten)) {
  265. play_dialtone(chan);
  266. did_ignore = 1;
  267. } else
  268. if (did_ignore) {
  269. ast_playtones_stop(chan);
  270. did_ignore = 0;
  271. }
  272. /* if can do some more, do it */
  273. if (!ast_matchmore_extension(chan,ourcontext,exten,1, chan->callerid)) {
  274. break;
  275. }
  276. }
  277. }
  278. if ((k==3) && (ast_exists_extension(chan,ourcontext,exten,1, chan->callerid)))
  279. {
  280. ast_playtones_stop(chan);
  281. /* We're authenticated and have a valid extension */
  282. if (ourcallerid && *ourcallerid)
  283. {
  284. if (chan->callerid) free(chan->callerid);
  285. chan->callerid = strdup(ourcallerid);
  286. }
  287. strncpy(chan->exten, exten, sizeof(chan->exten) - 1);
  288. strncpy(chan->context, ourcontext, sizeof(chan->context) - 1);
  289. if (!ast_strlen_zero(acctcode))
  290. strncpy(chan->accountcode, acctcode, sizeof(chan->accountcode) - 1);
  291. chan->priority = 0;
  292. ast_cdr_init(chan->cdr,chan);
  293. LOCAL_USER_REMOVE(u);
  294. return 0;
  295. }
  296. reorder:
  297. ast_indicate(chan,AST_CONTROL_CONGESTION);
  298. /* something is invalid, give em reorder for several seconds */
  299. time(&rstart);
  300. while(time(NULL) < rstart + 10)
  301. {
  302. if (ast_waitfor(chan, -1) < 0)
  303. break;
  304. f = ast_read(chan);
  305. if (!f)
  306. break;
  307. ast_frfree(f);
  308. }
  309. ast_playtones_stop(chan);
  310. LOCAL_USER_REMOVE(u);
  311. return -1;
  312. }
  313. int unload_module(void)
  314. {
  315. STANDARD_HANGUP_LOCALUSERS;
  316. return ast_unregister_application(app);
  317. }
  318. int load_module(void)
  319. {
  320. return ast_register_application(app, disa_exec, synopsis, descrip);
  321. }
  322. char *description(void)
  323. {
  324. return tdesc;
  325. }
  326. int usecount(void)
  327. {
  328. int res;
  329. STANDARD_USECOUNT(res);
  330. return res;
  331. }
  332. char *key(void)
  333. {
  334. return ASTERISK_GPL_KEY;
  335. }