manager.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Channel Management and more
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <pthread.h>
  16. #include <string.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19. #include <netdb.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <netinet/tcp.h>
  23. #include <arpa/inet.h>
  24. #include <signal.h>
  25. #include <errno.h>
  26. #include <unistd.h>
  27. #include <asterisk/channel.h>
  28. #include <asterisk/file.h>
  29. #include <asterisk/manager.h>
  30. #include <asterisk/config.h>
  31. #include <asterisk/lock.h>
  32. #include <asterisk/logger.h>
  33. #include <asterisk/options.h>
  34. #include <asterisk/cli.h>
  35. #include <asterisk/app.h>
  36. #include <asterisk/pbx.h>
  37. #include <asterisk/md5.h>
  38. #include <asterisk/acl.h>
  39. static int enabled = 0;
  40. static int portno = DEFAULT_MANAGER_PORT;
  41. static int asock = -1;
  42. static pthread_t t;
  43. static ast_mutex_t sessionlock = AST_MUTEX_INITIALIZER;
  44. static int block_sockets = 0;
  45. static struct permalias {
  46. int num;
  47. char *label;
  48. } perms[] = {
  49. { EVENT_FLAG_SYSTEM, "system" },
  50. { EVENT_FLAG_CALL, "call" },
  51. { EVENT_FLAG_LOG, "log" },
  52. { EVENT_FLAG_VERBOSE, "verbose" },
  53. { EVENT_FLAG_COMMAND, "command" },
  54. { EVENT_FLAG_AGENT, "agent" },
  55. { EVENT_FLAG_USER, "user" },
  56. { -1, "all" },
  57. };
  58. static struct mansession *sessions = NULL;
  59. static struct manager_action *first_action = NULL;
  60. static ast_mutex_t actionlock = AST_MUTEX_INITIALIZER;
  61. int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
  62. {
  63. /* Try to write string, but wait no more than ms milliseconds
  64. before timing out */
  65. int res=0;
  66. struct timeval tv;
  67. fd_set fds;
  68. while(len) {
  69. res = write(fd, s, len);
  70. if ((res < 0) && (errno != EAGAIN)) {
  71. return -1;
  72. }
  73. if (res < 0) res = 0;
  74. len -= res;
  75. s += res;
  76. tv.tv_sec = timeoutms / 1000;
  77. tv.tv_usec = timeoutms % 1000;
  78. FD_ZERO(&fds);
  79. FD_SET(fd, &fds);
  80. /* Wait until writable again */
  81. res = select(fd + 1, NULL, &fds, NULL, &tv);
  82. if (res < 1)
  83. return -1;
  84. }
  85. return res;
  86. }
  87. static int handle_showmancmds(int fd, int argc, char *argv[])
  88. {
  89. struct manager_action *cur = first_action;
  90. char *format = " %-15.15s %-45.45s\n";
  91. ast_mutex_lock(&actionlock);
  92. ast_cli(fd, format, "Action", "Synopsis");
  93. while(cur) { /* Walk the list of actions */
  94. ast_cli(fd, format, cur->action, cur->synopsis);
  95. cur = cur->next;
  96. }
  97. ast_mutex_unlock(&actionlock);
  98. return RESULT_SUCCESS;
  99. }
  100. static int handle_showmanconn(int fd, int argc, char *argv[])
  101. {
  102. struct mansession *s;
  103. char *format = " %-15.15s %-15.15s\n";
  104. ast_mutex_lock(&sessionlock);
  105. s = sessions;
  106. ast_cli(fd, format, "Username", "IP Address");
  107. while(s) {
  108. ast_cli(fd, format,s->username, inet_ntoa(s->sin.sin_addr));
  109. s = s->next;
  110. }
  111. ast_mutex_unlock(&sessionlock);
  112. return RESULT_SUCCESS;
  113. }
  114. static char showmancmds_help[] =
  115. "Usage: show manager commands\n"
  116. " Prints a listing of all the available manager commands.\n";
  117. static char showmanconn_help[] =
  118. "Usage: show manager connected\n"
  119. " Prints a listing of the users that are connected to the\n"
  120. "manager interface.\n";
  121. static struct ast_cli_entry show_mancmds_cli =
  122. { { "show", "manager", "commands", NULL },
  123. handle_showmancmds, "Show manager commands", showmancmds_help };
  124. static struct ast_cli_entry show_manconn_cli =
  125. { { "show", "manager", "connected", NULL },
  126. handle_showmanconn, "Show connected manager users", showmanconn_help };
  127. static void destroy_session(struct mansession *s)
  128. {
  129. struct mansession *cur, *prev = NULL;
  130. ast_mutex_lock(&sessionlock);
  131. cur = sessions;
  132. while(cur) {
  133. if (cur == s)
  134. break;
  135. prev = cur;
  136. cur = cur->next;
  137. }
  138. if (cur) {
  139. if (prev)
  140. prev->next = cur->next;
  141. else
  142. sessions = cur->next;
  143. if (s->fd > -1)
  144. close(s->fd);
  145. free(s);
  146. } else
  147. ast_log(LOG_WARNING, "Trying to delete non-existant session %p?\n", s);
  148. ast_mutex_unlock(&sessionlock);
  149. }
  150. char *astman_get_header(struct message *m, char *var)
  151. {
  152. char cmp[80];
  153. int x;
  154. snprintf(cmp, sizeof(cmp), "%s: ", var);
  155. for (x=0;x<m->hdrcount;x++)
  156. if (!strncasecmp(cmp, m->headers[x], strlen(cmp)))
  157. return m->headers[x] + strlen(cmp);
  158. return "";
  159. }
  160. void astman_send_error(struct mansession *s, struct message *m, char *error)
  161. {
  162. char *id = astman_get_header(m,"ActionID");
  163. ast_mutex_lock(&s->lock);
  164. ast_cli(s->fd, "Response: Error\r\n");
  165. if (id && strlen(id))
  166. ast_cli(s->fd, "ActionID: %s\r\n",id);
  167. ast_cli(s->fd, "Message: %s\r\n\r\n", error);
  168. ast_mutex_unlock(&s->lock);
  169. }
  170. void astman_send_response(struct mansession *s, struct message *m, char *resp, char *msg)
  171. {
  172. char *id = astman_get_header(m,"ActionID");
  173. ast_mutex_lock(&s->lock);
  174. ast_cli(s->fd, "Response: %s\r\n", resp);
  175. if (id && strlen(id))
  176. ast_cli(s->fd, "ActionID: %s\r\n",id);
  177. if (msg)
  178. ast_cli(s->fd, "Message: %s\r\n\r\n", msg);
  179. else
  180. ast_cli(s->fd, "\r\n");
  181. ast_mutex_unlock(&s->lock);
  182. }
  183. void astman_send_ack(struct mansession *s, struct message *m, char *msg)
  184. {
  185. astman_send_response(s, m, "Success", msg);
  186. }
  187. static int get_perm(char *instr)
  188. {
  189. char tmp[256];
  190. char *c;
  191. int x;
  192. int ret = 0;
  193. char *stringp=NULL;
  194. if (!instr)
  195. return 0;
  196. strncpy(tmp, instr, sizeof(tmp) - 1);
  197. stringp=tmp;
  198. c = strsep(&stringp, ",");
  199. while(c) {
  200. for (x=0;x<sizeof(perms) / sizeof(perms[0]);x++) {
  201. if (!strcasecmp(perms[x].label, c))
  202. ret |= perms[x].num;
  203. }
  204. c = strsep(&stringp, ",");
  205. }
  206. return ret;
  207. }
  208. static int set_eventmask(struct mansession *s, char *eventmask)
  209. {
  210. if (!eventmask)
  211. return -1;
  212. if (!strcasecmp(eventmask, "on") || ast_true(eventmask)) {
  213. ast_mutex_lock(&s->lock);
  214. s->send_events = 1;
  215. ast_mutex_unlock(&s->lock);
  216. return 1;
  217. } else if (!strcasecmp(eventmask, "off") || ast_false(eventmask)) {
  218. ast_mutex_lock(&s->lock);
  219. s->send_events = 0;
  220. ast_mutex_unlock(&s->lock);
  221. return 0;
  222. }
  223. return -1;
  224. }
  225. static int authenticate(struct mansession *s, struct message *m)
  226. {
  227. struct ast_config *cfg;
  228. char *cat;
  229. char *user = astman_get_header(m, "Username");
  230. char *pass = astman_get_header(m, "Secret");
  231. char *authtype = astman_get_header(m, "AuthType");
  232. char *key = astman_get_header(m, "Key");
  233. char *events = astman_get_header(m, "Events");
  234. cfg = ast_load("manager.conf");
  235. if (!cfg)
  236. return -1;
  237. cat = ast_category_browse(cfg, NULL);
  238. while(cat) {
  239. if (strcasecmp(cat, "general")) {
  240. /* This is a user */
  241. if (!strcasecmp(cat, user)) {
  242. struct ast_variable *v;
  243. struct ast_ha *ha = NULL;
  244. char *password = NULL;
  245. v = ast_variable_browse(cfg, cat);
  246. while (v) {
  247. if (!strcasecmp(v->name, "secret")) {
  248. password = v->value;
  249. } else if (!strcasecmp(v->name, "permit") ||
  250. !strcasecmp(v->name, "deny")) {
  251. ha = ast_append_ha(v->name, v->value, ha);
  252. }
  253. v = v->next;
  254. }
  255. if (ha && !ast_apply_ha(ha, &(s->sin))) {
  256. ast_log(LOG_NOTICE, "%s failed to pass IP ACL as '%s'\n", inet_ntoa(s->sin.sin_addr), user);
  257. ast_free_ha(ha);
  258. ast_destroy(cfg);
  259. return -1;
  260. } else if (ha)
  261. ast_free_ha(ha);
  262. if (!strcasecmp(authtype, "MD5")) {
  263. if (key && strlen(key) && s->challenge) {
  264. int x;
  265. int len=0;
  266. char md5key[256] = "";
  267. struct MD5Context md5;
  268. unsigned char digest[16];
  269. MD5Init(&md5);
  270. MD5Update(&md5, s->challenge, strlen(s->challenge));
  271. MD5Update(&md5, password, strlen(password));
  272. MD5Final(digest, &md5);
  273. for (x=0;x<16;x++)
  274. len += sprintf(md5key + len, "%2.2x", digest[x]);
  275. if (!strcmp(md5key, key))
  276. break;
  277. else {
  278. ast_destroy(cfg);
  279. return -1;
  280. }
  281. }
  282. } else if (password && !strcasecmp(password, pass)) {
  283. break;
  284. } else {
  285. ast_log(LOG_NOTICE, "%s failed to authenticate as '%s'\n", inet_ntoa(s->sin.sin_addr), user);
  286. ast_destroy(cfg);
  287. return -1;
  288. }
  289. }
  290. }
  291. cat = ast_category_browse(cfg, cat);
  292. }
  293. if (cat) {
  294. strncpy(s->username, cat, sizeof(s->username) - 1);
  295. s->readperm = get_perm(ast_variable_retrieve(cfg, cat, "read"));
  296. s->writeperm = get_perm(ast_variable_retrieve(cfg, cat, "write"));
  297. ast_destroy(cfg);
  298. if (events)
  299. set_eventmask(s, events);
  300. return 0;
  301. }
  302. ast_log(LOG_NOTICE, "%s tried to authenticate with non-existant user '%s'\n", inet_ntoa(s->sin.sin_addr), user);
  303. ast_destroy(cfg);
  304. return -1;
  305. }
  306. static int action_ping(struct mansession *s, struct message *m)
  307. {
  308. astman_send_response(s, m, "Pong", NULL);
  309. return 0;
  310. }
  311. static int action_events(struct mansession *s, struct message *m)
  312. {
  313. char *mask = astman_get_header(m, "EventMask");
  314. int res;
  315. res = set_eventmask(s, mask);
  316. if (res > 0)
  317. astman_send_response(s, m, "Events On", NULL);
  318. else if (res == 0)
  319. astman_send_response(s, m, "Events Off", NULL);
  320. else
  321. astman_send_response(s, m, "EventMask parse error", NULL);
  322. return 0;
  323. }
  324. static int action_logoff(struct mansession *s, struct message *m)
  325. {
  326. astman_send_response(s, m, "Goodbye", "Thanks for all the fish.");
  327. return -1;
  328. }
  329. static int action_hangup(struct mansession *s, struct message *m)
  330. {
  331. struct ast_channel *c = NULL;
  332. char *name = astman_get_header(m, "Channel");
  333. if (!strlen(name)) {
  334. astman_send_error(s, m, "No channel specified");
  335. return 0;
  336. }
  337. c = ast_channel_walk(NULL);
  338. while(c) {
  339. if (!strcasecmp(c->name, name)) {
  340. break;
  341. }
  342. c = ast_channel_walk(c);
  343. }
  344. if (!c) {
  345. astman_send_error(s, m, "No such channel");
  346. return 0;
  347. }
  348. ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
  349. astman_send_ack(s, m, "Channel Hungup");
  350. return 0;
  351. }
  352. static int action_status(struct mansession *s, struct message *m)
  353. {
  354. char *id = astman_get_header(m,"ActionID");
  355. char idText[256] = "";
  356. struct ast_channel *c;
  357. char bridge[256];
  358. astman_send_ack(s, m, "Channel status will follow");
  359. c = ast_channel_walk(NULL);
  360. if (id && strlen(id))
  361. snprintf(idText,256,"ActionID: %s\r\n",id);
  362. while(c) {
  363. if (c->bridge)
  364. snprintf(bridge, sizeof(bridge), "Link: %s\r\n", c->bridge->name);
  365. else
  366. strcpy(bridge, "");
  367. if (c->pbx) {
  368. ast_cli(s->fd,
  369. "Event: Status\r\n"
  370. "Channel: %s\r\n"
  371. "CallerID: %s\r\n"
  372. "State: %s\r\n"
  373. "Context: %s\r\n"
  374. "Extension: %s\r\n"
  375. "Priority: %d\r\n"
  376. "%s"
  377. "Uniqueid: %s\r\n"
  378. "%s"
  379. "\r\n",
  380. c->name, c->callerid ? c->callerid : "<unknown>",
  381. ast_state2str(c->_state), c->context,
  382. c->exten, c->priority, bridge, c->uniqueid, idText);
  383. } else {
  384. ast_cli(s->fd,
  385. "Event: Status\r\n"
  386. "Channel: %s\r\n"
  387. "CallerID: %s\r\n"
  388. "State: %s\r\n"
  389. "%s"
  390. "Uniqueid: %s\r\n"
  391. "%s"
  392. "\r\n",
  393. c->name, c->callerid ? c->callerid : "<unknown>",
  394. ast_state2str(c->_state), bridge, c->uniqueid, idText);
  395. }
  396. c = ast_channel_walk(c);
  397. }
  398. ast_cli(s->fd,
  399. "Event: StatusComplete\r\n"
  400. "%s"
  401. "\r\n",idText);
  402. return 0;
  403. }
  404. static int action_redirect(struct mansession *s, struct message *m)
  405. {
  406. char *name = astman_get_header(m, "Channel");
  407. char *name2 = astman_get_header(m, "ExtraChannel");
  408. char *exten = astman_get_header(m, "Exten");
  409. char *context = astman_get_header(m, "Context");
  410. char *priority = astman_get_header(m, "Priority");
  411. int pi = 0;
  412. int res;
  413. if (!name || !strlen(name)) {
  414. astman_send_error(s, m, "Channel not specified");
  415. return 0;
  416. }
  417. if (strlen(priority) && (sscanf(priority, "%d", &pi) != 1)) {
  418. astman_send_error(s, m, "Invalid priority\n");
  419. return 0;
  420. }
  421. res = ast_async_goto_by_name(name, context, exten, pi);
  422. if (!res) {
  423. if (strlen(name2)) {
  424. res = ast_async_goto_by_name(name2, context, exten, pi);
  425. if (!res)
  426. astman_send_ack(s, m, "Dual Redirect successful");
  427. else
  428. astman_send_error(s, m, "Secondary redirect failed");
  429. } else
  430. astman_send_ack(s, m, "Redirect successful");
  431. } else
  432. astman_send_error(s, m, "Redirect failed");
  433. return 0;
  434. }
  435. static int action_command(struct mansession *s, struct message *m)
  436. {
  437. char *cmd = astman_get_header(m, "Command");
  438. ast_mutex_lock(&s->lock);
  439. s->blocking = 1;
  440. ast_mutex_unlock(&s->lock);
  441. ast_cli(s->fd, "Response: Follows\r\n");
  442. /* FIXME: Wedge a ActionID response in here, waiting for later changes */
  443. ast_cli_command(s->fd, cmd);
  444. ast_cli(s->fd, "--END COMMAND--\r\n\r\n");
  445. ast_mutex_lock(&s->lock);
  446. s->blocking = 0;
  447. ast_mutex_unlock(&s->lock);
  448. return 0;
  449. }
  450. static int action_originate(struct mansession *s, struct message *m)
  451. {
  452. char *name = astman_get_header(m, "Channel");
  453. char *exten = astman_get_header(m, "Exten");
  454. char *context = astman_get_header(m, "Context");
  455. char *priority = astman_get_header(m, "Priority");
  456. char *timeout = astman_get_header(m, "Timeout");
  457. char *callerid = astman_get_header(m, "CallerID");
  458. char *variable = astman_get_header(m, "Variable");
  459. char *account = astman_get_header(m, "Account");
  460. char *app = astman_get_header(m, "Application");
  461. char *appdata = astman_get_header(m, "Data");
  462. char *tech, *data;
  463. int pi = 0;
  464. int res;
  465. int to = 30000;
  466. int reason = 0;
  467. char tmp[256];
  468. if (!name) {
  469. astman_send_error(s, m, "Channel not specified");
  470. return 0;
  471. }
  472. if (strlen(priority) && (sscanf(priority, "%d", &pi) != 1)) {
  473. astman_send_error(s, m, "Invalid priority\n");
  474. return 0;
  475. }
  476. if (strlen(timeout) && (sscanf(timeout, "%d", &to) != 1)) {
  477. astman_send_error(s, m, "Invalid timeout\n");
  478. return 0;
  479. }
  480. strncpy(tmp, name, sizeof(tmp) - 1);
  481. tech = tmp;
  482. data = strchr(tmp, '/');
  483. if (!data) {
  484. astman_send_error(s, m, "Invalid channel\n");
  485. return 0;
  486. }
  487. *data = '\0';
  488. data++;
  489. if (strlen(app)) {
  490. res = ast_pbx_outgoing_app(tech, AST_FORMAT_SLINEAR, data, to, app, appdata, &reason, 0, strlen(callerid) ? callerid : NULL, variable, account);
  491. } else {
  492. if (exten && context && pi)
  493. res = ast_pbx_outgoing_exten(tech, AST_FORMAT_SLINEAR, data, to, context, exten, pi, &reason, 0, strlen(callerid) ? callerid : NULL, variable, account);
  494. else {
  495. astman_send_error(s, m, "Originate with 'Exten' requires 'Context' and 'Priority'");
  496. return 0;
  497. }
  498. }
  499. if (!res)
  500. astman_send_ack(s, m, "Originate successfully queued");
  501. else
  502. astman_send_error(s, m, "Originate failed");
  503. return 0;
  504. }
  505. static int action_mailboxstatus(struct mansession *s, struct message *m)
  506. {
  507. char *mailbox = astman_get_header(m, "Mailbox");
  508. char *id = astman_get_header(m,"ActionID");
  509. char idText[256] = "";
  510. if (!mailbox || !strlen(mailbox)) {
  511. astman_send_error(s, m, "Mailbox not specified");
  512. return 0;
  513. }
  514. if (id && strlen(id))
  515. snprintf(idText,256,"ActionID: %s\r\n",id);
  516. ast_cli(s->fd, "Response: Success\r\n"
  517. "%s"
  518. "Message: Mailbox Status\r\n"
  519. "Mailbox: %s\r\n"
  520. "Waiting: %d\r\n\r\n", idText, mailbox, ast_app_has_voicemail(mailbox));
  521. return 0;
  522. }
  523. static int action_mailboxcount(struct mansession *s, struct message *m)
  524. {
  525. char *mailbox = astman_get_header(m, "Mailbox");
  526. char *id = astman_get_header(m,"ActionID");
  527. char idText[256] = "";
  528. int newmsgs = 0, oldmsgs = 0;
  529. if (!mailbox || !strlen(mailbox)) {
  530. astman_send_error(s, m, "Mailbox not specified");
  531. return 0;
  532. }
  533. ast_app_messagecount(mailbox, &newmsgs, &oldmsgs);
  534. if (id && strlen(id)) {
  535. snprintf(idText,256,"ActionID: %s\r\n",id);
  536. }
  537. ast_cli(s->fd, "Response: Success\r\n"
  538. "%s"
  539. "Message: Mailbox Message Count\r\n"
  540. "Mailbox: %s\r\n"
  541. "NewMessages: %d\r\n"
  542. "OldMessages: %d\r\n"
  543. "\r\n",
  544. idText,mailbox, newmsgs, oldmsgs);
  545. return 0;
  546. }
  547. static int action_extensionstate(struct mansession *s, struct message *m)
  548. {
  549. char *exten = astman_get_header(m, "Exten");
  550. char *context = astman_get_header(m, "Context");
  551. char *id = astman_get_header(m,"ActionID");
  552. char idText[256] = "";
  553. char hint[256] = "";
  554. int status;
  555. if (!exten || !strlen(exten)) {
  556. astman_send_error(s, m, "Extension not specified");
  557. return 0;
  558. }
  559. if (!context || !strlen(context))
  560. context = "default";
  561. status = ast_extension_state(NULL, context, exten);
  562. ast_get_hint(hint, sizeof(hint) - 1, NULL, context, exten);
  563. if (id && strlen(id)) {
  564. snprintf(idText,256,"ActionID: %s\r\n",id);
  565. }
  566. ast_cli(s->fd, "Response: Success\r\n"
  567. "%s"
  568. "Message: Extension Status\r\n"
  569. "Exten: %s\r\n"
  570. "Context: %s\r\n"
  571. "Hint: %s\r\n"
  572. "Status: %d\r\n\r\n",
  573. idText,exten, context, hint, status);
  574. return 0;
  575. }
  576. static int action_timeout(struct mansession *s, struct message *m)
  577. {
  578. struct ast_channel *c = NULL;
  579. char *name = astman_get_header(m, "Channel");
  580. int timeout = atoi(astman_get_header(m, "Timeout"));
  581. if (!strlen(name)) {
  582. astman_send_error(s, m, "No channel specified");
  583. return 0;
  584. }
  585. if (!timeout) {
  586. astman_send_error(s, m, "No timeout specified");
  587. return 0;
  588. }
  589. c = ast_channel_walk(NULL);
  590. while(c) {
  591. if (!strcasecmp(c->name, name)) {
  592. break;
  593. }
  594. c = ast_channel_walk(c);
  595. }
  596. if (!c) {
  597. astman_send_error(s, m, "No such channel");
  598. return 0;
  599. }
  600. ast_channel_setwhentohangup(c, timeout);
  601. astman_send_ack(s, m, "Timeout Set");
  602. return 0;
  603. }
  604. static int process_message(struct mansession *s, struct message *m)
  605. {
  606. char action[80];
  607. struct manager_action *tmp = first_action;
  608. char *id = astman_get_header(m,"ActionID");
  609. char idText[256] = "";
  610. strncpy(action, astman_get_header(m, "Action"), sizeof(action));
  611. ast_log( LOG_DEBUG, "Manager received command '%s'\n", action );
  612. if (!strlen(action)) {
  613. astman_send_error(s, m, "Missing action in request");
  614. return 0;
  615. }
  616. if (id && strlen(id)) {
  617. snprintf(idText,256,"ActionID: %s\r\n",id);
  618. }
  619. if (!s->authenticated) {
  620. if (!strcasecmp(action, "Challenge")) {
  621. char *authtype;
  622. authtype = astman_get_header(m, "AuthType");
  623. if (!strcasecmp(authtype, "MD5")) {
  624. if (!s->challenge || !strlen(s->challenge)) {
  625. ast_mutex_lock(&s->lock);
  626. snprintf(s->challenge, sizeof(s->challenge), "%d", rand());
  627. ast_mutex_unlock(&s->lock);
  628. }
  629. ast_cli(s->fd, "Response: Success\r\n"
  630. "%s"
  631. "Challenge: %s\r\n\r\n",
  632. idText,s->challenge);
  633. return 0;
  634. } else {
  635. astman_send_error(s, m, "Must specify AuthType");
  636. return 0;
  637. }
  638. } else if (!strcasecmp(action, "Login")) {
  639. if (authenticate(s, m)) {
  640. sleep(1);
  641. astman_send_error(s, m, "Authentication failed");
  642. return -1;
  643. } else {
  644. s->authenticated = 1;
  645. if (option_verbose > 1)
  646. ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged on from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
  647. ast_log(LOG_EVENT, "Manager '%s' logged on from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
  648. astman_send_ack(s, m, "Authentication accepted");
  649. }
  650. } else if (!strcasecmp(action, "Logoff")) {
  651. astman_send_ack(s, m, "See ya");
  652. return -1;
  653. } else
  654. astman_send_error(s, m, "Authentication Required");
  655. } else {
  656. while( tmp ) {
  657. if (!strcasecmp(action, tmp->action)) {
  658. if ((s->writeperm & tmp->authority) == tmp->authority) {
  659. if (tmp->func(s, m))
  660. return -1;
  661. } else {
  662. astman_send_error(s, m, "Permission denied");
  663. }
  664. return 0;
  665. }
  666. tmp = tmp->next;
  667. }
  668. astman_send_error(s, m, "Invalid/unknown command");
  669. }
  670. return 0;
  671. }
  672. static int get_input(struct mansession *s, char *output)
  673. {
  674. /* output must have at least sizeof(s->inbuf) space */
  675. int res;
  676. int x;
  677. fd_set fds;
  678. for (x=1;x<s->inlen;x++) {
  679. if ((s->inbuf[x] == '\n') && (s->inbuf[x-1] == '\r')) {
  680. /* Copy output data up to and including \r\n */
  681. memcpy(output, s->inbuf, x + 1);
  682. /* Add trailing \0 */
  683. output[x+1] = '\0';
  684. /* Move remaining data back to the front */
  685. memmove(s->inbuf, s->inbuf + x + 1, s->inlen - x);
  686. s->inlen -= (x + 1);
  687. return 1;
  688. }
  689. }
  690. if (s->inlen >= sizeof(s->inbuf) - 1) {
  691. ast_log(LOG_WARNING, "Dumping long line with no return from %s: %s\n", inet_ntoa(s->sin.sin_addr), s->inbuf);
  692. s->inlen = 0;
  693. }
  694. FD_ZERO(&fds);
  695. FD_SET(s->fd, &fds);
  696. res = ast_select(s->fd + 1, &fds, NULL, NULL, NULL);
  697. if (res < 0) {
  698. ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
  699. } else if (res > 0) {
  700. ast_mutex_lock(&s->lock);
  701. res = read(s->fd, s->inbuf + s->inlen, sizeof(s->inbuf) - 1 - s->inlen);
  702. ast_mutex_unlock(&s->lock);
  703. if (res < 1)
  704. return -1;
  705. }
  706. s->inlen += res;
  707. s->inbuf[s->inlen] = '\0';
  708. return 0;
  709. }
  710. static void *session_do(void *data)
  711. {
  712. struct mansession *s = data;
  713. struct message m;
  714. int res;
  715. ast_mutex_lock(&s->lock);
  716. ast_cli(s->fd, "Asterisk Call Manager/1.0\r\n");
  717. ast_mutex_unlock(&s->lock);
  718. memset(&m, 0, sizeof(&m));
  719. for (;;) {
  720. res = get_input(s, m.headers[m.hdrcount]);
  721. if (res > 0) {
  722. /* Strip trailing \r\n */
  723. if (strlen(m.headers[m.hdrcount]) < 2)
  724. continue;
  725. m.headers[m.hdrcount][strlen(m.headers[m.hdrcount]) - 2] = '\0';
  726. if (!strlen(m.headers[m.hdrcount])) {
  727. if (process_message(s, &m))
  728. break;
  729. memset(&m, 0, sizeof(&m));
  730. } else if (m.hdrcount < MAX_HEADERS - 1)
  731. m.hdrcount++;
  732. } else if (res < 0)
  733. break;
  734. }
  735. if (s->authenticated) {
  736. if (option_verbose > 1)
  737. ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged off from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
  738. ast_log(LOG_EVENT, "Manager '%s' logged off from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
  739. } else {
  740. if (option_verbose > 1)
  741. ast_verbose(VERBOSE_PREFIX_2 "Connect attempt from '%s' unable to authenticate\n", inet_ntoa(s->sin.sin_addr));
  742. ast_log(LOG_EVENT, "Failed attempt from %s\n", inet_ntoa(s->sin.sin_addr));
  743. }
  744. destroy_session(s);
  745. return NULL;
  746. }
  747. static void *accept_thread(void *ignore)
  748. {
  749. int as;
  750. struct sockaddr_in sin;
  751. int sinlen;
  752. struct mansession *s;
  753. struct protoent *p;
  754. int arg = 1;
  755. int flags;
  756. pthread_attr_t attr;
  757. pthread_attr_init(&attr);
  758. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  759. for (;;) {
  760. sinlen = sizeof(sin);
  761. as = accept(asock, (struct sockaddr *)&sin, &sinlen);
  762. if (as < 0) {
  763. ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
  764. continue;
  765. }
  766. p = getprotobyname("tcp");
  767. if( p ) {
  768. if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
  769. ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
  770. }
  771. }
  772. s = malloc(sizeof(struct mansession));
  773. if (!s) {
  774. ast_log(LOG_WARNING, "Failed to allocate management session: %s\n", strerror(errno));
  775. continue;
  776. }
  777. memset(s, 0, sizeof(struct mansession));
  778. memcpy(&s->sin, &sin, sizeof(sin));
  779. if(! block_sockets) {
  780. /* For safety, make sure socket is non-blocking */
  781. flags = fcntl(as, F_GETFL);
  782. fcntl(as, F_SETFL, flags | O_NONBLOCK);
  783. }
  784. ast_mutex_init(&s->lock);
  785. s->fd = as;
  786. s->send_events = 1;
  787. ast_mutex_lock(&sessionlock);
  788. s->next = sessions;
  789. sessions = s;
  790. ast_mutex_unlock(&sessionlock);
  791. if (pthread_create(&t, &attr, session_do, s))
  792. destroy_session(s);
  793. }
  794. pthread_attr_destroy(&attr);
  795. return NULL;
  796. }
  797. int manager_event(int category, char *event, char *fmt, ...)
  798. {
  799. struct mansession *s;
  800. char tmp[4096];
  801. va_list ap;
  802. ast_mutex_lock(&sessionlock);
  803. s = sessions;
  804. while(s) {
  805. if (((s->readperm & category) == category) && s->send_events) {
  806. ast_mutex_lock(&s->lock);
  807. if (!s->blocking) {
  808. ast_cli(s->fd, "Event: %s\r\n", event);
  809. va_start(ap, fmt);
  810. vsnprintf(tmp, sizeof(tmp), fmt, ap);
  811. va_end(ap);
  812. ast_carefulwrite(s->fd,tmp,strlen(tmp),100);
  813. ast_cli(s->fd, "\r\n");
  814. }
  815. ast_mutex_unlock(&s->lock);
  816. }
  817. s = s->next;
  818. }
  819. ast_mutex_unlock(&sessionlock);
  820. return 0;
  821. }
  822. int ast_manager_unregister( char *action ) {
  823. struct manager_action *cur = first_action, *prev = first_action;
  824. ast_mutex_lock(&actionlock);
  825. while( cur ) {
  826. if (!strcasecmp(action, cur->action)) {
  827. prev->next = cur->next;
  828. free(cur);
  829. if (option_verbose > 1)
  830. ast_verbose(VERBOSE_PREFIX_2 "Manager unregistered action %s\n", action);
  831. ast_mutex_unlock(&actionlock);
  832. return 0;
  833. }
  834. prev = cur;
  835. cur = cur->next;
  836. }
  837. ast_mutex_unlock(&actionlock);
  838. return 0;
  839. }
  840. static int manager_state_cb(char *context, char *exten, int state, void *data)
  841. {
  842. /* Notify managers of change */
  843. manager_event(EVENT_FLAG_CALL, "ExtensionStatus", "Exten: %s\r\nContext: %s\r\nStatus: %d\r\n", exten, context, state);
  844. return 0;
  845. }
  846. int ast_manager_register( char *action, int auth,
  847. int (*func)(struct mansession *s, struct message *m), char *synopsis)
  848. {
  849. struct manager_action *cur = first_action, *prev = NULL;
  850. ast_mutex_lock(&actionlock);
  851. while(cur) { /* Walk the list of actions */
  852. if (!strcasecmp(cur->action, action)) {
  853. ast_log(LOG_WARNING, "Manager: Action '%s' already registered\n", action);
  854. ast_mutex_unlock(&actionlock);
  855. return -1;
  856. }
  857. prev = cur;
  858. cur = cur->next;
  859. }
  860. cur = malloc( sizeof(struct manager_action) );
  861. if( !cur ) {
  862. ast_log(LOG_WARNING, "Manager: out of memory trying to register action\n");
  863. ast_mutex_unlock(&actionlock);
  864. return -1;
  865. }
  866. strncpy( cur->action, action, 255 );
  867. cur->authority = auth;
  868. cur->func = func;
  869. cur->synopsis = synopsis;
  870. cur->next = NULL;
  871. if( prev ) prev->next = cur;
  872. else first_action = cur;
  873. if (option_verbose > 1)
  874. ast_verbose(VERBOSE_PREFIX_2 "Manager registered action %s\n", action);
  875. ast_mutex_unlock(&actionlock);
  876. return 0;
  877. }
  878. static int registered = 0;
  879. int init_manager(void)
  880. {
  881. struct ast_config *cfg;
  882. char *val;
  883. int oldportno = portno;
  884. static struct sockaddr_in ba;
  885. int x = 1;
  886. if (!registered) {
  887. /* Register default actions */
  888. ast_manager_register( "Ping", 0, action_ping, "Ping" );
  889. ast_manager_register( "Events", 0, action_events, "Contol Event Flow" );
  890. ast_manager_register( "Logoff", 0, action_logoff, "Logoff Manager" );
  891. ast_manager_register( "Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel" );
  892. ast_manager_register( "Status", EVENT_FLAG_CALL, action_status, "Status" );
  893. ast_manager_register( "Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect" );
  894. ast_manager_register( "Originate", EVENT_FLAG_CALL, action_originate, "Originate Call" );
  895. ast_manager_register( "MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox" );
  896. ast_manager_register( "Command", EVENT_FLAG_COMMAND, action_command, "Execute Command" );
  897. ast_manager_register( "ExtensionState", EVENT_FLAG_CALL, action_extensionstate, "Check Extension Status" );
  898. ast_manager_register( "AbsoluteTimeout", EVENT_FLAG_CALL, action_timeout, "Set Absolute Timeout" );
  899. ast_manager_register( "MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count" );
  900. ast_cli_register(&show_mancmds_cli);
  901. ast_cli_register(&show_manconn_cli);
  902. ast_extension_state_add(NULL, NULL, manager_state_cb, NULL);
  903. registered = 1;
  904. }
  905. portno = DEFAULT_MANAGER_PORT;
  906. cfg = ast_load("manager.conf");
  907. if (!cfg) {
  908. ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf. Call management disabled.\n");
  909. return 0;
  910. }
  911. memset(&ba, 0, sizeof(ba));
  912. val = ast_variable_retrieve(cfg, "general", "enabled");
  913. if (val)
  914. enabled = ast_true(val);
  915. val = ast_variable_retrieve(cfg, "general", "block-sockets");
  916. if(val)
  917. block_sockets = ast_true(val);
  918. if ((val = ast_variable_retrieve(cfg, "general", "port"))) {
  919. if (sscanf(val, "%d", &portno) != 1) {
  920. ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
  921. portno = DEFAULT_MANAGER_PORT;
  922. }
  923. } else if ((val = ast_variable_retrieve(cfg, "general", "portno"))) {
  924. if (sscanf(val, "%d", &portno) != 1) {
  925. ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
  926. portno = DEFAULT_MANAGER_PORT;
  927. }
  928. ast_log(LOG_NOTICE, "Use of portno in manager.conf deprecated. Please use 'port=%s' instead.\n", val);
  929. }
  930. ba.sin_family = AF_INET;
  931. ba.sin_port = htons(portno);
  932. memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
  933. if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) {
  934. if (!inet_aton(val, &ba.sin_addr)) {
  935. ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val);
  936. memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
  937. }
  938. }
  939. if ((asock > -1) && ((portno != oldportno) || !enabled)) {
  940. #if 0
  941. /* Can't be done yet */
  942. close(asock);
  943. asock = -1;
  944. #else
  945. ast_log(LOG_WARNING, "Unable to change management port / enabled\n");
  946. #endif
  947. }
  948. ast_destroy(cfg);
  949. /* If not enabled, do nothing */
  950. if (!enabled) {
  951. return 0;
  952. }
  953. if (asock < 0) {
  954. asock = socket(AF_INET, SOCK_STREAM, 0);
  955. if (asock < 0) {
  956. ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
  957. return -1;
  958. }
  959. setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
  960. if (bind(asock, (struct sockaddr *)&ba, sizeof(ba))) {
  961. ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno));
  962. close(asock);
  963. asock = -1;
  964. return -1;
  965. }
  966. if (listen(asock, 2)) {
  967. ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno));
  968. close(asock);
  969. asock = -1;
  970. return -1;
  971. }
  972. if (option_verbose)
  973. ast_verbose("Asterisk Management interface listening on port %d\n", portno);
  974. pthread_create(&t, NULL, accept_thread, NULL);
  975. }
  976. return 0;
  977. }
  978. int reload_manager(void)
  979. {
  980. manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n");
  981. return init_manager();
  982. }