app_chanspy.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005 Anthony Minessale II (anthmct@yahoo.com)
  5. * Copyright (C) 2005 - 2008, Digium, Inc.
  6. *
  7. * A license has been granted to Digium (via disclaimer) for the use of
  8. * this code.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief ChanSpy: Listen in on any channel.
  23. *
  24. * \author Anthony Minessale II <anthmct@yahoo.com>
  25. * \author Joshua Colp <jcolp@digium.com>
  26. * \author Russell Bryant <russell@digium.com>
  27. *
  28. * \ingroup applications
  29. */
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include <ctype.h>
  33. #include <errno.h>
  34. #include "asterisk/paths.h" /* use ast_config_AST_MONITOR_DIR */
  35. #include "asterisk/file.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/audiohook.h"
  38. #include "asterisk/features.h"
  39. #include "asterisk/app.h"
  40. #include "asterisk/utils.h"
  41. #include "asterisk/say.h"
  42. #include "asterisk/pbx.h"
  43. #include "asterisk/translate.h"
  44. #include "asterisk/module.h"
  45. #include "asterisk/lock.h"
  46. #include "asterisk/options.h"
  47. #define AST_NAME_STRLEN 256
  48. static const char *tdesc = "Listen to a channel, and optionally whisper into it";
  49. static const char *app_chan = "ChanSpy";
  50. static const char *desc_chan =
  51. " ChanSpy([chanprefix][,options]): This application is used to listen to the\n"
  52. "audio from an Asterisk channel. This includes the audio coming in and\n"
  53. "out of the channel being spied on. If the 'chanprefix' parameter is specified,\n"
  54. "only channels beginning with this string will be spied upon.\n"
  55. " While spying, the following actions may be performed:\n"
  56. " - Dialing # cycles the volume level.\n"
  57. " - Dialing * will stop spying and look for another channel to spy on.\n"
  58. " - Dialing a series of digits followed by # builds a channel name to append\n"
  59. " to 'chanprefix'. For example, executing ChanSpy(Agent) and then dialing\n"
  60. " the digits '1234#' while spying will begin spying on the channel\n"
  61. " 'Agent/1234'.\n"
  62. " Note: The X option supersedes the three features above in that if a valid\n"
  63. " single digit extension exists in the correct context ChanSpy will\n"
  64. " exit to it. This also disables choosing a channel based on 'chanprefix'\n"
  65. " and a digit sequence.\n"
  66. " Options:\n"
  67. " b - Only spy on channels involved in a bridged call.\n"
  68. " g(grp) - Match only channels where their SPYGROUP variable is set to\n"
  69. " contain 'grp' in an optional : delimited list.\n"
  70. " q - Don't play a beep when beginning to spy on a channel, or speak the\n"
  71. " selected channel name.\n"
  72. " r[(basename)] - Record the session to the monitor spool directory. An\n"
  73. " optional base for the filename may be specified. The\n"
  74. " default is 'chanspy'.\n"
  75. " v([value]) - Adjust the initial volume in the range from -4 to 4. A\n"
  76. " negative value refers to a quieter setting.\n"
  77. " w - Enable 'whisper' mode, so the spying channel can talk to\n"
  78. " the spied-on channel.\n"
  79. " W - Enable 'private whisper' mode, so the spying channel can\n"
  80. " talk to the spied-on channel but cannot listen to that\n"
  81. " channel.\n"
  82. " o - Only listen to audio coming from this channel.\n"
  83. " X - Allow the user to exit ChanSpy to a valid single digit\n"
  84. " numeric extension in the current context or the context\n"
  85. " specified by the SPY_EXIT_CONTEXT channel variable. The\n"
  86. " name of the last channel that was spied on will be stored\n"
  87. " in the SPY_CHANNEL variable.\n"
  88. " e(ext) - Enable 'enforced' mode, so the spying channel can\n"
  89. " only monitor extensions whose name is in the 'ext' : \n"
  90. " delimited list.\n"
  91. ;
  92. static const char *app_ext = "ExtenSpy";
  93. static const char *desc_ext =
  94. " ExtenSpy(exten[@context][,options]): This application is used to listen to the\n"
  95. "audio from an Asterisk channel. This includes the audio coming in and\n"
  96. "out of the channel being spied on. Only channels created by outgoing calls for the\n"
  97. "specified extension will be selected for spying. If the optional context is not\n"
  98. "supplied, the current channel's context will be used.\n"
  99. " While spying, the following actions may be performed:\n"
  100. " - Dialing # cycles the volume level.\n"
  101. " - Dialing * will stop spying and look for another channel to spy on.\n"
  102. " Note: The X option superseeds the two features above in that if a valid\n"
  103. " single digit extension exists in the correct context it ChanSpy will\n"
  104. " exit to it.\n"
  105. " Options:\n"
  106. " b - Only spy on channels involved in a bridged call.\n"
  107. " g(grp) - Match only channels where their ${SPYGROUP} variable is set to\n"
  108. " contain 'grp' in an optional : delimited list.\n"
  109. " q - Don't play a beep when beginning to spy on a channel, or speak the\n"
  110. " selected channel name.\n"
  111. " r[(basename)] - Record the session to the monitor spool directory. An\n"
  112. " optional base for the filename may be specified. The\n"
  113. " default is 'chanspy'.\n"
  114. " v([value]) - Adjust the initial volume in the range from -4 to 4. A\n"
  115. " negative value refers to a quieter setting.\n"
  116. " w - Enable 'whisper' mode, so the spying channel can talk to\n"
  117. " the spied-on channel.\n"
  118. " W - Enable 'private whisper' mode, so the spying channel can\n"
  119. " talk to the spied-on channel but cannot listen to that\n"
  120. " channel.\n"
  121. " o - Only listen to audio coming from this channel.\n"
  122. " X - Allow the user to exit ChanSpy to a valid single digit\n"
  123. " numeric extension in the current context or the context\n"
  124. " specified by the SPY_EXIT_CONTEXT channel variable. The\n"
  125. " name of the last channel that was spied on will be stored\n"
  126. " in the SPY_CHANNEL variable.\n"
  127. ;
  128. enum {
  129. OPTION_QUIET = (1 << 0), /* Quiet, no announcement */
  130. OPTION_BRIDGED = (1 << 1), /* Only look at bridged calls */
  131. OPTION_VOLUME = (1 << 2), /* Specify initial volume */
  132. OPTION_GROUP = (1 << 3), /* Only look at channels in group */
  133. OPTION_RECORD = (1 << 4),
  134. OPTION_WHISPER = (1 << 5),
  135. OPTION_PRIVATE = (1 << 6), /* Private Whisper mode */
  136. OPTION_READONLY = (1 << 7), /* Don't mix the two channels */
  137. OPTION_EXIT = (1 << 8), /* Exit to a valid single digit extension */
  138. OPTION_ENFORCED = (1 << 9), /* Enforced mode */
  139. } chanspy_opt_flags;
  140. enum {
  141. OPT_ARG_VOLUME = 0,
  142. OPT_ARG_GROUP,
  143. OPT_ARG_RECORD,
  144. OPT_ARG_ENFORCED,
  145. OPT_ARG_ARRAY_SIZE,
  146. } chanspy_opt_args;
  147. AST_APP_OPTIONS(spy_opts, {
  148. AST_APP_OPTION('q', OPTION_QUIET),
  149. AST_APP_OPTION('b', OPTION_BRIDGED),
  150. AST_APP_OPTION('w', OPTION_WHISPER),
  151. AST_APP_OPTION('W', OPTION_PRIVATE),
  152. AST_APP_OPTION_ARG('v', OPTION_VOLUME, OPT_ARG_VOLUME),
  153. AST_APP_OPTION_ARG('g', OPTION_GROUP, OPT_ARG_GROUP),
  154. AST_APP_OPTION_ARG('r', OPTION_RECORD, OPT_ARG_RECORD),
  155. AST_APP_OPTION_ARG('e', OPTION_ENFORCED, OPT_ARG_ENFORCED),
  156. AST_APP_OPTION('o', OPTION_READONLY),
  157. AST_APP_OPTION('X', OPTION_EXIT),
  158. });
  159. static int next_unique_id_to_use = 0;
  160. struct chanspy_translation_helper {
  161. /* spy data */
  162. struct ast_audiohook spy_audiohook;
  163. struct ast_audiohook whisper_audiohook;
  164. int fd;
  165. int volfactor;
  166. };
  167. static void *spy_alloc(struct ast_channel *chan, void *data)
  168. {
  169. /* just store the data pointer in the channel structure */
  170. return data;
  171. }
  172. static void spy_release(struct ast_channel *chan, void *data)
  173. {
  174. /* nothing to do */
  175. }
  176. static int spy_generate(struct ast_channel *chan, void *data, int len, int samples)
  177. {
  178. struct chanspy_translation_helper *csth = data;
  179. struct ast_frame *f, *cur;
  180. ast_audiohook_lock(&csth->spy_audiohook);
  181. if (csth->spy_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) {
  182. /* Channel is already gone more than likely */
  183. ast_audiohook_unlock(&csth->spy_audiohook);
  184. return -1;
  185. }
  186. if (ast_test_flag(&csth->spy_audiohook, OPTION_READONLY)) {
  187. /* Option 'o' was set, so don't mix channel audio */
  188. f = ast_audiohook_read_frame(&csth->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_READ, AST_FORMAT_SLINEAR);
  189. } else {
  190. f = ast_audiohook_read_frame(&csth->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, AST_FORMAT_SLINEAR);
  191. }
  192. ast_audiohook_unlock(&csth->spy_audiohook);
  193. if (!f)
  194. return 0;
  195. for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
  196. if (ast_write(chan, cur)) {
  197. ast_frfree(f);
  198. return -1;
  199. }
  200. if (csth->fd) {
  201. if (write(csth->fd, cur->data, cur->datalen) < 0) {
  202. ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
  203. }
  204. }
  205. }
  206. ast_frfree(f);
  207. return 0;
  208. }
  209. static struct ast_generator spygen = {
  210. .alloc = spy_alloc,
  211. .release = spy_release,
  212. .generate = spy_generate,
  213. };
  214. static int start_spying(struct ast_channel *chan, const char *spychan_name, struct ast_audiohook *audiohook)
  215. {
  216. int res = 0;
  217. struct ast_channel *peer = NULL;
  218. ast_log(LOG_NOTICE, "Attaching %s to %s\n", spychan_name, chan->name);
  219. ast_set_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC | AST_AUDIOHOOK_SMALL_QUEUE);
  220. res = ast_audiohook_attach(chan, audiohook);
  221. if (!res && ast_test_flag(chan, AST_FLAG_NBRIDGE) && (peer = ast_bridged_channel(chan))) {
  222. ast_softhangup(peer, AST_SOFTHANGUP_UNBRIDGE);
  223. }
  224. return res;
  225. }
  226. struct chanspy_ds {
  227. struct ast_channel *chan;
  228. char unique_id[20];
  229. ast_mutex_t lock;
  230. };
  231. static int channel_spy(struct ast_channel *chan, struct chanspy_ds *spyee_chanspy_ds,
  232. int *volfactor, int fd, const struct ast_flags *flags, char *exitcontext)
  233. {
  234. struct chanspy_translation_helper csth;
  235. int running = 0, res, x = 0;
  236. char inp[24] = {0};
  237. char *name;
  238. struct ast_frame *f;
  239. struct ast_silence_generator *silgen = NULL;
  240. struct ast_channel *spyee = NULL;
  241. const char *spyer_name;
  242. ast_channel_lock(chan);
  243. spyer_name = ast_strdupa(chan->name);
  244. ast_channel_unlock(chan);
  245. ast_mutex_lock(&spyee_chanspy_ds->lock);
  246. while ((spyee = spyee_chanspy_ds->chan) && ast_channel_trylock(spyee)) {
  247. /* avoid a deadlock here, just in case spyee is masqueraded and
  248. * chanspy_ds_chan_fixup() is called with the channel locked */
  249. DEADLOCK_AVOIDANCE(&spyee_chanspy_ds->lock);
  250. }
  251. ast_mutex_unlock(&spyee_chanspy_ds->lock);
  252. if (!spyee)
  253. return 0;
  254. /* We now hold the channel lock on spyee */
  255. if (ast_check_hangup(chan) || ast_check_hangup(spyee)) {
  256. ast_channel_unlock(spyee);
  257. return 0;
  258. }
  259. name = ast_strdupa(spyee->name);
  260. ast_verb(2, "Spying on channel %s\n", name);
  261. memset(&csth, 0, sizeof(csth));
  262. ast_copy_flags(&csth.spy_audiohook, flags, AST_FLAGS_ALL);
  263. ast_audiohook_init(&csth.spy_audiohook, AST_AUDIOHOOK_TYPE_SPY, "ChanSpy");
  264. if (start_spying(spyee, spyer_name, &csth.spy_audiohook)) {
  265. ast_audiohook_destroy(&csth.spy_audiohook);
  266. ast_channel_unlock(spyee);
  267. return 0;
  268. }
  269. if (ast_test_flag(flags, OPTION_WHISPER)) {
  270. ast_audiohook_init(&csth.whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "ChanSpy");
  271. start_spying(spyee, spyer_name, &csth.whisper_audiohook);
  272. }
  273. ast_channel_unlock(spyee);
  274. spyee = NULL;
  275. csth.volfactor = *volfactor;
  276. if (csth.volfactor) {
  277. csth.spy_audiohook.options.read_volume = csth.volfactor;
  278. csth.spy_audiohook.options.write_volume = csth.volfactor;
  279. }
  280. csth.fd = fd;
  281. if (ast_test_flag(flags, OPTION_PRIVATE))
  282. silgen = ast_channel_start_silence_generator(chan);
  283. else
  284. ast_activate_generator(chan, &spygen, &csth);
  285. /* We can no longer rely on 'spyee' being an actual channel;
  286. it can be hung up and freed out from under us. However, the
  287. channel destructor will put NULL into our csth.spy.chan
  288. field when that happens, so that is our signal that the spyee
  289. channel has gone away.
  290. */
  291. /* Note: it is very important that the ast_waitfor() be the first
  292. condition in this expression, so that if we wait for some period
  293. of time before receiving a frame from our spying channel, we check
  294. for hangup on the spied-on channel _after_ knowing that a frame
  295. has arrived, since the spied-on channel could have gone away while
  296. we were waiting
  297. */
  298. while ((res = ast_waitfor(chan, -1) > -1) && csth.spy_audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING) {
  299. if (!(f = ast_read(chan)) || ast_check_hangup(chan)) {
  300. running = -1;
  301. break;
  302. }
  303. if (ast_test_flag(flags, OPTION_WHISPER) && f->frametype == AST_FRAME_VOICE) {
  304. ast_audiohook_lock(&csth.whisper_audiohook);
  305. ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f);
  306. ast_audiohook_unlock(&csth.whisper_audiohook);
  307. ast_frfree(f);
  308. continue;
  309. }
  310. res = (f->frametype == AST_FRAME_DTMF) ? f->subclass : 0;
  311. ast_frfree(f);
  312. if (!res)
  313. continue;
  314. if (x == sizeof(inp))
  315. x = 0;
  316. if (res < 0) {
  317. running = -1;
  318. break;
  319. }
  320. if (ast_test_flag(flags, OPTION_EXIT)) {
  321. char tmp[2];
  322. tmp[0] = res;
  323. tmp[1] = '\0';
  324. if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) {
  325. ast_debug(1, "Got DTMF %c, goto context %s\n", tmp[0], exitcontext);
  326. pbx_builtin_setvar_helper(chan, "SPY_CHANNEL", name);
  327. running = -2;
  328. break;
  329. } else {
  330. ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
  331. }
  332. } else if (res >= '0' && res <= '9') {
  333. inp[x++] = res;
  334. }
  335. if (res == '*') {
  336. running = 0;
  337. break;
  338. } else if (res == '#') {
  339. if (!ast_strlen_zero(inp)) {
  340. running = atoi(inp);
  341. break;
  342. }
  343. (*volfactor)++;
  344. if (*volfactor > 4)
  345. *volfactor = -4;
  346. ast_verb(3, "Setting spy volume on %s to %d\n", chan->name, *volfactor);
  347. csth.volfactor = *volfactor;
  348. csth.spy_audiohook.options.read_volume = csth.volfactor;
  349. csth.spy_audiohook.options.write_volume = csth.volfactor;
  350. }
  351. }
  352. if (ast_test_flag(flags, OPTION_PRIVATE))
  353. ast_channel_stop_silence_generator(chan, silgen);
  354. else
  355. ast_deactivate_generator(chan);
  356. if (ast_test_flag(flags, OPTION_WHISPER)) {
  357. ast_audiohook_lock(&csth.whisper_audiohook);
  358. ast_audiohook_detach(&csth.whisper_audiohook);
  359. ast_audiohook_unlock(&csth.whisper_audiohook);
  360. ast_audiohook_destroy(&csth.whisper_audiohook);
  361. }
  362. ast_audiohook_lock(&csth.spy_audiohook);
  363. ast_audiohook_detach(&csth.spy_audiohook);
  364. ast_audiohook_unlock(&csth.spy_audiohook);
  365. ast_audiohook_destroy(&csth.spy_audiohook);
  366. ast_verb(2, "Done Spying on channel %s\n", name);
  367. return running;
  368. }
  369. /*!
  370. * \note This relies on the embedded lock to be recursive, as it may be called
  371. * due to a call to chanspy_ds_free with the lock held there.
  372. */
  373. static void chanspy_ds_destroy(void *data)
  374. {
  375. struct chanspy_ds *chanspy_ds = data;
  376. /* Setting chan to be NULL is an atomic operation, but we don't want this
  377. * value to change while this lock is held. The lock is held elsewhere
  378. * while it performs non-atomic operations with this channel pointer */
  379. ast_mutex_lock(&chanspy_ds->lock);
  380. chanspy_ds->chan = NULL;
  381. ast_mutex_unlock(&chanspy_ds->lock);
  382. }
  383. static void chanspy_ds_chan_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  384. {
  385. struct chanspy_ds *chanspy_ds = data;
  386. ast_mutex_lock(&chanspy_ds->lock);
  387. chanspy_ds->chan = new_chan;
  388. ast_mutex_unlock(&chanspy_ds->lock);
  389. }
  390. static const struct ast_datastore_info chanspy_ds_info = {
  391. .type = "chanspy",
  392. .destroy = chanspy_ds_destroy,
  393. .chan_fixup = chanspy_ds_chan_fixup,
  394. };
  395. static struct chanspy_ds *chanspy_ds_free(struct chanspy_ds *chanspy_ds)
  396. {
  397. struct ast_channel *chan;
  398. if (!chanspy_ds) {
  399. return NULL;
  400. }
  401. ast_mutex_lock(&chanspy_ds->lock);
  402. while ((chan = chanspy_ds->chan)) {
  403. struct ast_datastore *datastore;
  404. if (ast_channel_trylock(chan)) {
  405. DEADLOCK_AVOIDANCE(&chanspy_ds->lock);
  406. continue;
  407. }
  408. if ((datastore = ast_channel_datastore_find(chan, &chanspy_ds_info, chanspy_ds->unique_id))) {
  409. ast_channel_datastore_remove(chan, datastore);
  410. /* chanspy_ds->chan is NULL after this call */
  411. chanspy_ds_destroy(datastore->data);
  412. datastore->data = NULL;
  413. ast_channel_datastore_free(datastore);
  414. }
  415. ast_channel_unlock(chan);
  416. break;
  417. }
  418. ast_mutex_unlock(&chanspy_ds->lock);
  419. return NULL;
  420. }
  421. /*! \note Returns the channel in the chanspy_ds locked as well as the chanspy_ds locked */
  422. static struct chanspy_ds *setup_chanspy_ds(struct ast_channel *chan, struct chanspy_ds *chanspy_ds)
  423. {
  424. struct ast_datastore *datastore = NULL;
  425. ast_mutex_lock(&chanspy_ds->lock);
  426. if (!(datastore = ast_channel_datastore_alloc(&chanspy_ds_info, chanspy_ds->unique_id))) {
  427. ast_mutex_unlock(&chanspy_ds->lock);
  428. chanspy_ds = chanspy_ds_free(chanspy_ds);
  429. ast_channel_unlock(chan);
  430. return NULL;
  431. }
  432. chanspy_ds->chan = chan;
  433. datastore->data = chanspy_ds;
  434. ast_channel_datastore_add(chan, datastore);
  435. return chanspy_ds;
  436. }
  437. static struct chanspy_ds *next_channel(struct ast_channel *chan,
  438. const struct ast_channel *last, const char *spec,
  439. const char *exten, const char *context, struct chanspy_ds *chanspy_ds)
  440. {
  441. struct ast_channel *next;
  442. const size_t pseudo_len = strlen("DAHDI/pseudo");
  443. redo:
  444. if (!ast_strlen_zero(spec))
  445. next = ast_walk_channel_by_name_prefix_locked(last, spec, strlen(spec));
  446. else if (!ast_strlen_zero(exten))
  447. next = ast_walk_channel_by_exten_locked(last, exten, context);
  448. else
  449. next = ast_channel_walk_locked(last);
  450. if (!next)
  451. return NULL;
  452. if (!strncmp(next->name, "DAHDI/pseudo", pseudo_len)) {
  453. last = next;
  454. ast_channel_unlock(next);
  455. goto redo;
  456. } else if (next == chan) {
  457. last = next;
  458. ast_channel_unlock(next);
  459. goto redo;
  460. }
  461. return setup_chanspy_ds(next, chanspy_ds);
  462. }
  463. static int common_exec(struct ast_channel *chan, const struct ast_flags *flags,
  464. int volfactor, const int fd, const char *mygroup, const char *myenforced,
  465. const char *spec, const char *exten, const char *context)
  466. {
  467. char nameprefix[AST_NAME_STRLEN];
  468. char peer_name[AST_NAME_STRLEN + 5];
  469. char exitcontext[AST_MAX_CONTEXT] = "";
  470. signed char zero_volume = 0;
  471. int waitms;
  472. int res;
  473. char *ptr;
  474. int num;
  475. int num_spyed_upon = 1;
  476. struct chanspy_ds chanspy_ds = { 0, };
  477. if (ast_test_flag(flags, OPTION_EXIT)) {
  478. const char *c;
  479. if ((c = pbx_builtin_getvar_helper(chan, "SPY_EXIT_CONTEXT")))
  480. ast_copy_string(exitcontext, c, sizeof(exitcontext));
  481. else if (!ast_strlen_zero(chan->macrocontext))
  482. ast_copy_string(exitcontext, chan->macrocontext, sizeof(exitcontext));
  483. else
  484. ast_copy_string(exitcontext, chan->context, sizeof(exitcontext));
  485. }
  486. ast_mutex_init(&chanspy_ds.lock);
  487. snprintf(chanspy_ds.unique_id, sizeof(chanspy_ds.unique_id), "%d", ast_atomic_fetchadd_int(&next_unique_id_to_use, +1));
  488. if (chan->_state != AST_STATE_UP)
  489. ast_answer(chan);
  490. ast_set_flag(chan, AST_FLAG_SPYING); /* so nobody can spy on us while we are spying */
  491. waitms = 100;
  492. for (;;) {
  493. struct chanspy_ds *peer_chanspy_ds = NULL, *next_chanspy_ds = NULL;
  494. struct ast_channel *prev = NULL, *peer = NULL;
  495. if (!ast_test_flag(flags, OPTION_QUIET) && num_spyed_upon) {
  496. res = ast_streamfile(chan, "beep", chan->language);
  497. if (!res)
  498. res = ast_waitstream(chan, "");
  499. else if (res < 0) {
  500. ast_clear_flag(chan, AST_FLAG_SPYING);
  501. break;
  502. }
  503. if (!ast_strlen_zero(exitcontext)) {
  504. char tmp[2];
  505. tmp[0] = res;
  506. tmp[1] = '\0';
  507. if (!ast_goto_if_exists(chan, exitcontext, tmp, 1))
  508. goto exit;
  509. else
  510. ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
  511. }
  512. }
  513. res = ast_waitfordigit(chan, waitms);
  514. if (res < 0) {
  515. ast_clear_flag(chan, AST_FLAG_SPYING);
  516. break;
  517. }
  518. if (!ast_strlen_zero(exitcontext)) {
  519. char tmp[2];
  520. tmp[0] = res;
  521. tmp[1] = '\0';
  522. if (!ast_goto_if_exists(chan, exitcontext, tmp, 1))
  523. goto exit;
  524. else
  525. ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
  526. }
  527. /* reset for the next loop around, unless overridden later */
  528. waitms = 100;
  529. num_spyed_upon = 0;
  530. for (peer_chanspy_ds = next_channel(chan, prev, spec, exten, context, &chanspy_ds);
  531. peer_chanspy_ds;
  532. chanspy_ds_free(peer_chanspy_ds), prev = peer,
  533. peer_chanspy_ds = next_chanspy_ds ? next_chanspy_ds :
  534. next_channel(chan, prev, spec, exten, context, &chanspy_ds), next_chanspy_ds = NULL) {
  535. int igrp = !mygroup;
  536. int ienf = !myenforced;
  537. char *s;
  538. peer = peer_chanspy_ds->chan;
  539. ast_mutex_unlock(&peer_chanspy_ds->lock);
  540. if (peer == prev) {
  541. ast_channel_unlock(peer);
  542. chanspy_ds_free(peer_chanspy_ds);
  543. break;
  544. }
  545. if (ast_check_hangup(chan)) {
  546. ast_channel_unlock(peer);
  547. chanspy_ds_free(peer_chanspy_ds);
  548. break;
  549. }
  550. if (ast_test_flag(flags, OPTION_BRIDGED) && !ast_bridged_channel(peer)) {
  551. ast_channel_unlock(peer);
  552. continue;
  553. }
  554. if (ast_check_hangup(peer) || ast_test_flag(peer, AST_FLAG_SPYING)) {
  555. ast_channel_unlock(peer);
  556. continue;
  557. }
  558. if (mygroup) {
  559. int num_groups = 0;
  560. char dup_group[512];
  561. char *groups[25];
  562. const char *group;
  563. int x;
  564. if ((group = pbx_builtin_getvar_helper(peer, "SPYGROUP"))) {
  565. ast_copy_string(dup_group, group, sizeof(dup_group));
  566. num_groups = ast_app_separate_args(dup_group, ':', groups,
  567. ARRAY_LEN(groups));
  568. }
  569. for (x = 0; x < num_groups; x++) {
  570. if (!strcmp(mygroup, groups[x])) {
  571. igrp = 1;
  572. break;
  573. }
  574. }
  575. }
  576. if (!igrp) {
  577. ast_channel_unlock(peer);
  578. continue;
  579. }
  580. if (myenforced) {
  581. char ext[AST_CHANNEL_NAME + 3];
  582. char buffer[512];
  583. char *end;
  584. snprintf(buffer, sizeof(buffer) - 1, ":%s:", myenforced);
  585. ast_copy_string(ext + 1, peer->name, sizeof(ext) - 1);
  586. if ((end = strchr(ext, '-'))) {
  587. *end++ = ':';
  588. *end = '\0';
  589. }
  590. ext[0] = ':';
  591. if (strcasestr(buffer, ext)) {
  592. ienf = 1;
  593. }
  594. }
  595. if (!ienf) {
  596. continue;
  597. }
  598. strcpy(peer_name, "spy-");
  599. strncat(peer_name, peer->name, AST_NAME_STRLEN - 4 - 1);
  600. ptr = strchr(peer_name, '/');
  601. *ptr++ = '\0';
  602. for (s = peer_name; s < ptr; s++)
  603. *s = tolower(*s);
  604. /* We have to unlock the peer channel here to avoid a deadlock.
  605. * So, when we need to dereference it again, we have to lock the
  606. * datastore and get the pointer from there to see if the channel
  607. * is still valid. */
  608. ast_channel_unlock(peer);
  609. if (!ast_test_flag(flags, OPTION_QUIET)) {
  610. if (ast_fileexists(peer_name, NULL, NULL) != -1) {
  611. res = ast_streamfile(chan, peer_name, chan->language);
  612. if (!res)
  613. res = ast_waitstream(chan, "");
  614. if (res) {
  615. chanspy_ds_free(peer_chanspy_ds);
  616. break;
  617. }
  618. } else
  619. res = ast_say_character_str(chan, peer_name, "", chan->language);
  620. if ((num = atoi(ptr)))
  621. ast_say_digits(chan, atoi(ptr), "", chan->language);
  622. }
  623. res = channel_spy(chan, peer_chanspy_ds, &volfactor, fd, flags, exitcontext);
  624. num_spyed_upon++;
  625. if (res == -1) {
  626. chanspy_ds_free(peer_chanspy_ds);
  627. goto exit;
  628. } else if (res == -2) {
  629. res = 0;
  630. chanspy_ds_free(peer_chanspy_ds);
  631. goto exit;
  632. } else if (res > 1 && spec) {
  633. struct ast_channel *next;
  634. snprintf(nameprefix, AST_NAME_STRLEN, "%s/%d", spec, res);
  635. if ((next = ast_get_channel_by_name_prefix_locked(nameprefix, strlen(nameprefix)))) {
  636. peer_chanspy_ds = chanspy_ds_free(peer_chanspy_ds);
  637. next_chanspy_ds = setup_chanspy_ds(next, &chanspy_ds);
  638. } else {
  639. /* stay on this channel, if it is still valid */
  640. ast_mutex_lock(&peer_chanspy_ds->lock);
  641. if (peer_chanspy_ds->chan) {
  642. ast_channel_lock(peer_chanspy_ds->chan);
  643. next_chanspy_ds = peer_chanspy_ds;
  644. peer_chanspy_ds = NULL;
  645. } else {
  646. /* the channel is gone */
  647. ast_mutex_unlock(&peer_chanspy_ds->lock);
  648. next_chanspy_ds = NULL;
  649. }
  650. }
  651. peer = NULL;
  652. }
  653. }
  654. if (res == -1 || ast_check_hangup(chan))
  655. break;
  656. }
  657. exit:
  658. ast_clear_flag(chan, AST_FLAG_SPYING);
  659. ast_channel_setoption(chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0);
  660. ast_mutex_lock(&chanspy_ds.lock);
  661. ast_mutex_unlock(&chanspy_ds.lock);
  662. ast_mutex_destroy(&chanspy_ds.lock);
  663. return res;
  664. }
  665. static int chanspy_exec(struct ast_channel *chan, void *data)
  666. {
  667. char *myenforced = NULL;
  668. char *mygroup = NULL;
  669. char *recbase = NULL;
  670. int fd = 0;
  671. struct ast_flags flags;
  672. int oldwf = 0;
  673. int volfactor = 0;
  674. int res;
  675. AST_DECLARE_APP_ARGS(args,
  676. AST_APP_ARG(spec);
  677. AST_APP_ARG(options);
  678. );
  679. char *opts[OPT_ARG_ARRAY_SIZE];
  680. data = ast_strdupa(data);
  681. AST_STANDARD_APP_ARGS(args, data);
  682. if (args.spec && !strcmp(args.spec, "all"))
  683. args.spec = NULL;
  684. if (args.options) {
  685. ast_app_parse_options(spy_opts, &flags, opts, args.options);
  686. if (ast_test_flag(&flags, OPTION_GROUP))
  687. mygroup = opts[OPT_ARG_GROUP];
  688. if (ast_test_flag(&flags, OPTION_RECORD) &&
  689. !(recbase = opts[OPT_ARG_RECORD]))
  690. recbase = "chanspy";
  691. if (ast_test_flag(&flags, OPTION_VOLUME) && opts[OPT_ARG_VOLUME]) {
  692. int vol;
  693. if ((sscanf(opts[OPT_ARG_VOLUME], "%30d", &vol) != 1) || (vol > 4) || (vol < -4))
  694. ast_log(LOG_NOTICE, "Volume factor must be a number between -4 and 4\n");
  695. else
  696. volfactor = vol;
  697. }
  698. if (ast_test_flag(&flags, OPTION_PRIVATE))
  699. ast_set_flag(&flags, OPTION_WHISPER);
  700. if (ast_test_flag(&flags, OPTION_ENFORCED))
  701. myenforced = opts[OPT_ARG_ENFORCED];
  702. } else
  703. ast_clear_flag(&flags, AST_FLAGS_ALL);
  704. oldwf = chan->writeformat;
  705. if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
  706. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  707. return -1;
  708. }
  709. if (recbase) {
  710. char filename[PATH_MAX];
  711. snprintf(filename, sizeof(filename), "%s/%s.%d.raw", ast_config_AST_MONITOR_DIR, recbase, (int) time(NULL));
  712. if ((fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, AST_FILE_MODE)) <= 0) {
  713. ast_log(LOG_WARNING, "Cannot open '%s' for recording\n", filename);
  714. fd = 0;
  715. }
  716. }
  717. res = common_exec(chan, &flags, volfactor, fd, mygroup, myenforced, args.spec, NULL, NULL);
  718. if (fd)
  719. close(fd);
  720. if (oldwf && ast_set_write_format(chan, oldwf) < 0)
  721. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  722. return res;
  723. }
  724. static int extenspy_exec(struct ast_channel *chan, void *data)
  725. {
  726. char *ptr, *exten = NULL;
  727. char *mygroup = NULL;
  728. char *recbase = NULL;
  729. int fd = 0;
  730. struct ast_flags flags;
  731. int oldwf = 0;
  732. int volfactor = 0;
  733. int res;
  734. AST_DECLARE_APP_ARGS(args,
  735. AST_APP_ARG(context);
  736. AST_APP_ARG(options);
  737. );
  738. data = ast_strdupa(data);
  739. AST_STANDARD_APP_ARGS(args, data);
  740. if (!ast_strlen_zero(args.context) && (ptr = strchr(args.context, '@'))) {
  741. exten = args.context;
  742. *ptr++ = '\0';
  743. args.context = ptr;
  744. }
  745. if (ast_strlen_zero(args.context))
  746. args.context = ast_strdupa(chan->context);
  747. if (args.options) {
  748. char *opts[OPT_ARG_ARRAY_SIZE];
  749. ast_app_parse_options(spy_opts, &flags, opts, args.options);
  750. if (ast_test_flag(&flags, OPTION_GROUP))
  751. mygroup = opts[OPT_ARG_GROUP];
  752. if (ast_test_flag(&flags, OPTION_RECORD) &&
  753. !(recbase = opts[OPT_ARG_RECORD]))
  754. recbase = "chanspy";
  755. if (ast_test_flag(&flags, OPTION_VOLUME) && opts[OPT_ARG_VOLUME]) {
  756. int vol;
  757. if ((sscanf(opts[OPT_ARG_VOLUME], "%30d", &vol) != 1) || (vol > 4) || (vol < -4))
  758. ast_log(LOG_NOTICE, "Volume factor must be a number between -4 and 4\n");
  759. else
  760. volfactor = vol;
  761. }
  762. if (ast_test_flag(&flags, OPTION_PRIVATE))
  763. ast_set_flag(&flags, OPTION_WHISPER);
  764. } else
  765. ast_clear_flag(&flags, AST_FLAGS_ALL);
  766. oldwf = chan->writeformat;
  767. if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
  768. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  769. return -1;
  770. }
  771. if (recbase) {
  772. char filename[PATH_MAX];
  773. snprintf(filename, sizeof(filename), "%s/%s.%d.raw", ast_config_AST_MONITOR_DIR, recbase, (int) time(NULL));
  774. if ((fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, AST_FILE_MODE)) <= 0) {
  775. ast_log(LOG_WARNING, "Cannot open '%s' for recording\n", filename);
  776. fd = 0;
  777. }
  778. }
  779. res = common_exec(chan, &flags, volfactor, fd, mygroup, NULL, NULL, exten, args.context);
  780. if (fd)
  781. close(fd);
  782. if (oldwf && ast_set_write_format(chan, oldwf) < 0)
  783. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  784. return res;
  785. }
  786. static int unload_module(void)
  787. {
  788. int res = 0;
  789. res |= ast_unregister_application(app_chan);
  790. res |= ast_unregister_application(app_ext);
  791. return res;
  792. }
  793. static int load_module(void)
  794. {
  795. int res = 0;
  796. res |= ast_register_application(app_chan, chanspy_exec, tdesc, desc_chan);
  797. res |= ast_register_application(app_ext, extenspy_exec, tdesc, desc_ext);
  798. return res;
  799. }
  800. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Listen to the audio of an active channel");