astman.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*
  19. *
  20. * ASTerisk MANager
  21. *
  22. */
  23. /*** MODULEINFO
  24. <support_level>extended</support_level>
  25. ***/
  26. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include "asterisk.h"
  29. #include <newt.h>
  30. #include <stdio.h>
  31. #include <sys/time.h>
  32. #include <netdb.h>
  33. #include <netinet/in.h>
  34. #include <arpa/inet.h>
  35. #include <sys/socket.h>
  36. #include <sys/select.h>
  37. #include <fcntl.h>
  38. #include <string.h>
  39. #include <errno.h>
  40. #include <unistd.h>
  41. #include <stdlib.h>
  42. #include "asterisk/md5.h"
  43. #include "asterisk/linkedlists.h"
  44. #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
  45. #undef gethostbyname
  46. #define MAX_HEADERS 80
  47. #define MAX_LEN 256
  48. /*
  49. * 2005.05.27 - different versions of newt define the type of the buffer
  50. * for the 5th argument to newtEntry() as char ** or const char ** . To
  51. * let the code compile cleanly with -Werror, we cast it to void * through
  52. * _NEWT_CAST.
  53. */
  54. #define _NEWT_CAST (void *)
  55. #define DEFAULT_MANAGER_PORT 5038
  56. struct message {
  57. unsigned int hdrcount;
  58. char headers[MAX_HEADERS][MAX_LEN];
  59. };
  60. static struct ast_mansession {
  61. struct sockaddr_in sin;
  62. int fd;
  63. char inbuf[MAX_LEN];
  64. int inlen;
  65. } session;
  66. struct ast_chan {
  67. char name[80];
  68. char exten[20];
  69. char context[20];
  70. char priority[20];
  71. char callerid[40];
  72. char state[10];
  73. AST_LIST_ENTRY(ast_chan) list;
  74. };
  75. static AST_LIST_HEAD_NOLOCK_STATIC(chans, ast_chan);
  76. /* dummy functions to be compatible with the Asterisk core for md5.c */
  77. void ast_register_file_version(const char *file, const char *version);
  78. void ast_register_file_version(const char *file, const char *version)
  79. {
  80. }
  81. void ast_unregister_file_version(const char *file);
  82. void ast_unregister_file_version(const char *file)
  83. {
  84. }
  85. #if !defined(LOW_MEMORY)
  86. int ast_add_profile(const char *, uint64_t scale);
  87. int ast_add_profile(const char *s, uint64_t scale)
  88. {
  89. return -1;
  90. }
  91. int64_t ast_profile(int, int64_t);
  92. int64_t ast_profile(int key, int64_t val)
  93. {
  94. return 0;
  95. }
  96. int64_t ast_mark(int, int start1_stop0);
  97. int64_t ast_mark(int key, int start1_stop0)
  98. {
  99. return 0;
  100. }
  101. #endif /* LOW_MEMORY */
  102. /* end of dummy functions */
  103. static struct ast_chan *find_chan(char *name)
  104. {
  105. struct ast_chan *chan;
  106. AST_LIST_TRAVERSE(&chans, chan, list) {
  107. if (!strcmp(name, chan->name))
  108. return chan;
  109. }
  110. chan = malloc(sizeof(struct ast_chan));
  111. if (chan) {
  112. memset(chan, 0, sizeof(struct ast_chan));
  113. strncpy(chan->name, name, sizeof(chan->name) - 1);
  114. AST_LIST_INSERT_TAIL(&chans, chan, list);
  115. }
  116. return chan;
  117. }
  118. static void del_chan(char *name)
  119. {
  120. struct ast_chan *chan;
  121. AST_LIST_TRAVERSE_SAFE_BEGIN(&chans, chan, list) {
  122. if (!strcmp(name, chan->name)) {
  123. AST_LIST_REMOVE_CURRENT(list);
  124. free(chan);
  125. return;
  126. }
  127. }
  128. AST_LIST_TRAVERSE_SAFE_END;
  129. }
  130. static void __attribute__((format(printf, 2, 3))) fdprintf(int fd, char *fmt, ...)
  131. {
  132. char stuff[4096];
  133. va_list ap;
  134. va_start(ap, fmt);
  135. vsnprintf(stuff, sizeof(stuff), fmt, ap);
  136. va_end(ap);
  137. if (write(fd, stuff, strlen(stuff)) < 0) {
  138. fprintf(stderr, "write() failed: %s\n", strerror(errno));
  139. }
  140. }
  141. static char *get_header(struct message *m, char *var)
  142. {
  143. char cmp[80];
  144. int x;
  145. snprintf(cmp, sizeof(cmp), "%s: ", var);
  146. for (x=0;x<m->hdrcount;x++)
  147. if (!strncasecmp(cmp, m->headers[x], strlen(cmp)))
  148. return m->headers[x] + strlen(cmp);
  149. return "";
  150. }
  151. static int event_newstate(struct ast_mansession *s, struct message *m)
  152. {
  153. struct ast_chan *chan;
  154. chan = find_chan(get_header(m, "Channel"));
  155. strncpy(chan->state, get_header(m, "State"), sizeof(chan->state) - 1);
  156. return 0;
  157. }
  158. static int event_newexten(struct ast_mansession *s, struct message *m)
  159. {
  160. struct ast_chan *chan;
  161. chan = find_chan(get_header(m, "Channel"));
  162. strncpy(chan->exten, get_header(m, "Extension"), sizeof(chan->exten) - 1);
  163. strncpy(chan->context, get_header(m, "Context"), sizeof(chan->context) - 1);
  164. strncpy(chan->priority, get_header(m, "Priority"), sizeof(chan->priority) - 1);
  165. return 0;
  166. }
  167. static int event_newchannel(struct ast_mansession *s, struct message *m)
  168. {
  169. struct ast_chan *chan;
  170. chan = find_chan(get_header(m, "Channel"));
  171. strncpy(chan->state, get_header(m, "State"), sizeof(chan->state) - 1);
  172. strncpy(chan->callerid, get_header(m, "Callerid"), sizeof(chan->callerid) - 1);
  173. return 0;
  174. }
  175. static int event_status(struct ast_mansession *s, struct message *m)
  176. {
  177. struct ast_chan *chan;
  178. chan = find_chan(get_header(m, "Channel"));
  179. strncpy(chan->state, get_header(m, "State"), sizeof(chan->state) - 1);
  180. strncpy(chan->callerid, get_header(m, "Callerid"), sizeof(chan->callerid) - 1);
  181. strncpy(chan->exten, get_header(m, "Extension"), sizeof(chan->exten) - 1);
  182. strncpy(chan->context, get_header(m, "Context"), sizeof(chan->context) - 1);
  183. strncpy(chan->priority, get_header(m, "Priority"), sizeof(chan->priority) - 1);
  184. return 0;
  185. }
  186. static int event_hangup(struct ast_mansession *s, struct message *m)
  187. {
  188. del_chan(get_header(m, "Channel"));
  189. return 0;
  190. }
  191. static int event_ignore(struct ast_mansession *s, struct message *m)
  192. {
  193. return 0;
  194. }
  195. static int event_rename(struct ast_mansession *s, struct message *m)
  196. {
  197. struct ast_chan *chan;
  198. chan = find_chan(get_header(m, "Oldname"));
  199. strncpy(chan->name, get_header(m, "Newname"), sizeof(chan->name) - 1);
  200. return 0;
  201. }
  202. static struct event {
  203. char *event;
  204. int (*func)(struct ast_mansession *s, struct message *m);
  205. } events[] = {
  206. { "Newstate", event_newstate },
  207. { "Newchannel", event_newchannel },
  208. { "Newexten", event_newexten },
  209. { "Hangup", event_hangup },
  210. { "Rename", event_rename },
  211. { "Status", event_status },
  212. { "Link", event_ignore },
  213. { "Unlink", event_ignore },
  214. { "StatusComplete", event_ignore },
  215. { "Dial", event_ignore },
  216. { "PeerStatus", event_ignore },
  217. { "MessageWaiting", event_ignore },
  218. { "Newcallerid", event_ignore },
  219. { "AGIExec", event_ignore},
  220. { "VarSet", event_ignore},
  221. { "MeetmeTalking", event_ignore},
  222. { "MeetmeJoin", event_ignore},
  223. { "MeetmeLeave", event_ignore},
  224. { "MeetmeEnd", event_ignore},
  225. { "MeetmeMute", event_ignore},
  226. { "Masquerade", event_ignore},
  227. };
  228. static int process_message(struct ast_mansession *s, struct message *m)
  229. {
  230. int x;
  231. char event[80] = "";
  232. strncpy(event, get_header(m, "Event"), sizeof(event) - 1);
  233. if (!strlen(event)) {
  234. fprintf(stderr, "Missing event in request");
  235. return 0;
  236. }
  237. for (x = 0; x < ARRAY_LEN(events); x++) {
  238. if (!strcasecmp(event, events[x].event)) {
  239. if (events[x].func(s, m))
  240. return -1;
  241. break;
  242. }
  243. }
  244. if (x >= ARRAY_LEN(events))
  245. fprintf(stderr, "Ignoring unknown event '%s'", event);
  246. #if 0
  247. for (x=0;x<m->hdrcount;x++) {
  248. printf("Header: %s\n", m->headers[x]);
  249. }
  250. #endif
  251. return 0;
  252. }
  253. static void rebuild_channels(newtComponent c)
  254. {
  255. void *prev = NULL;
  256. struct ast_chan *chan;
  257. char tmpn[sizeof(chan->name) + sizeof(chan->callerid) + 3 - 1];
  258. char tmp[256];
  259. int x=0;
  260. prev = newtListboxGetCurrent(c);
  261. newtListboxClear(c);
  262. AST_LIST_TRAVERSE(&chans, chan, list) {
  263. snprintf(tmpn, sizeof(tmpn), "%s (%s)", chan->name, chan->callerid);
  264. if (strlen(chan->exten))
  265. snprintf(tmp, sizeof(tmp), "%-30s %8s -> %s@%s:%s",
  266. tmpn, chan->state,
  267. chan->exten, chan->context, chan->priority);
  268. else
  269. snprintf(tmp, sizeof(tmp), "%-30s %8s",
  270. tmpn, chan->state);
  271. newtListboxAppendEntry(c, tmp, chan);
  272. x++;
  273. }
  274. if (!x)
  275. newtListboxAppendEntry(c, " << No Active Channels >> ", NULL);
  276. newtListboxSetCurrentByKey(c, prev);
  277. }
  278. static int has_input(struct ast_mansession *s)
  279. {
  280. int x;
  281. for (x=1;x<s->inlen;x++)
  282. if ((s->inbuf[x] == '\n') && (s->inbuf[x-1] == '\r'))
  283. return 1;
  284. return 0;
  285. }
  286. static int get_input(struct ast_mansession *s, char *output)
  287. {
  288. /* output must have at least sizeof(s->inbuf) space */
  289. int res;
  290. int x;
  291. struct timeval tv = {0, 0};
  292. fd_set fds;
  293. for (x=1;x<s->inlen;x++) {
  294. if ((s->inbuf[x] == '\n') && (s->inbuf[x-1] == '\r')) {
  295. /* Copy output data up to and including \r\n */
  296. memcpy(output, s->inbuf, x + 1);
  297. /* Add trailing \0 */
  298. output[x+1] = '\0';
  299. /* Move remaining data back to the front */
  300. memmove(s->inbuf, s->inbuf + x + 1, s->inlen - x);
  301. s->inlen -= (x + 1);
  302. return 1;
  303. }
  304. }
  305. if (s->inlen >= sizeof(s->inbuf) - 1) {
  306. fprintf(stderr, "Dumping long line with no return from %s: %s\n", inet_ntoa(s->sin.sin_addr), s->inbuf);
  307. s->inlen = 0;
  308. }
  309. FD_ZERO(&fds);
  310. FD_SET(s->fd, &fds);
  311. res = select(s->fd + 1, &fds, NULL, NULL, &tv);
  312. if (res < 0) {
  313. fprintf(stderr, "Select returned error: %s\n", strerror(errno));
  314. } else if (res > 0) {
  315. res = read(s->fd, s->inbuf + s->inlen, sizeof(s->inbuf) - 1 - s->inlen);
  316. if (res < 1)
  317. return -1;
  318. s->inlen += res;
  319. s->inbuf[s->inlen] = '\0';
  320. } else {
  321. return 2;
  322. }
  323. return 0;
  324. }
  325. static int input_check(struct ast_mansession *s, struct message **mout)
  326. {
  327. static struct message m;
  328. int res;
  329. if (mout)
  330. *mout = NULL;
  331. for(;;) {
  332. res = get_input(s, m.headers[m.hdrcount]);
  333. if (res == 1) {
  334. #if 0
  335. fprintf(stderr, "Got header: %s", m.headers[m.hdrcount]);
  336. fgetc(stdin);
  337. #endif
  338. /* Strip trailing \r\n */
  339. if (strlen(m.headers[m.hdrcount]) < 2)
  340. continue;
  341. m.headers[m.hdrcount][strlen(m.headers[m.hdrcount]) - 2] = '\0';
  342. if (!strlen(m.headers[m.hdrcount])) {
  343. if (mout && strlen(get_header(&m, "Response"))) {
  344. *mout = &m;
  345. return 0;
  346. }
  347. if (process_message(s, &m))
  348. break;
  349. memset(&m, 0, sizeof(m));
  350. } else if (m.hdrcount < MAX_HEADERS - 1)
  351. m.hdrcount++;
  352. } else if (res < 0) {
  353. return -1;
  354. } else if (res == 2)
  355. return 0;
  356. }
  357. return -1;
  358. }
  359. static struct message *wait_for_response(int timeout)
  360. {
  361. struct message *m;
  362. struct timeval tv;
  363. int res;
  364. fd_set fds;
  365. for (;;) {
  366. tv.tv_sec = timeout / 1000;
  367. tv.tv_usec = (timeout % 1000) * 1000;
  368. FD_SET(session.fd, &fds);
  369. res = select(session.fd + 1, &fds, NULL, NULL, &tv);
  370. if (res < 1)
  371. break;
  372. if (input_check(&session, &m) < 0) {
  373. return NULL;
  374. }
  375. if (m)
  376. return m;
  377. }
  378. return NULL;
  379. }
  380. static int __attribute__((format(printf, 2, 3))) manager_action(char *action, char *fmt, ...)
  381. {
  382. struct ast_mansession *s;
  383. char tmp[4096];
  384. va_list ap;
  385. int res;
  386. s = &session;
  387. fdprintf(s->fd, "Action: %s\r\n", action);
  388. va_start(ap, fmt);
  389. vsnprintf(tmp, sizeof(tmp), fmt, ap);
  390. va_end(ap);
  391. if ((res = write(s->fd, tmp, strlen(tmp))) < 0) {
  392. fprintf(stderr, "write() failed: %s\n", strerror(errno));
  393. }
  394. fdprintf(s->fd, "\r\n");
  395. return 0;
  396. }
  397. static int show_message(char *title, char *msg)
  398. {
  399. newtComponent form;
  400. newtComponent label;
  401. newtComponent ok;
  402. struct newtExitStruct es;
  403. newtCenteredWindow(60,7, title);
  404. label = newtLabel(4,1,msg);
  405. ok = newtButton(27, 3, "OK");
  406. form = newtForm(NULL, NULL, 0);
  407. newtFormAddComponents(form, label, ok, NULL);
  408. newtFormRun(form, &es);
  409. newtPopWindow();
  410. newtFormDestroy(form);
  411. return 0;
  412. }
  413. static newtComponent showform;
  414. static int show_doing(char *title, char *tmp)
  415. {
  416. struct newtExitStruct es;
  417. newtComponent label;
  418. showform = newtForm(NULL, NULL, 0);
  419. newtCenteredWindow(70,4, title);
  420. label = newtLabel(3,1,tmp);
  421. newtFormAddComponents(showform,label, NULL);
  422. newtFormSetTimer(showform, 200);
  423. newtFormRun(showform, &es);
  424. return 0;
  425. }
  426. static int hide_doing(void)
  427. {
  428. newtPopWindow();
  429. newtFormDestroy(showform);
  430. return 0;
  431. }
  432. static void try_status(void)
  433. {
  434. struct message *m;
  435. manager_action("Status", "%s", "");
  436. m = wait_for_response(10000);
  437. if (!m) {
  438. show_message("Status Failed", "Timeout waiting for response");
  439. } else if (strcasecmp(get_header(m, "Response"), "Success")) {
  440. show_message("Status Failed Failed", get_header(m, "Message"));
  441. }
  442. }
  443. static void try_hangup(newtComponent c)
  444. {
  445. struct ast_chan *chan;
  446. struct message *m;
  447. chan = newtListboxGetCurrent(c);
  448. if (chan) {
  449. manager_action("Hangup", "Channel: %s\r\n", chan->name);
  450. m = wait_for_response(10000);
  451. if (!m) {
  452. show_message("Hangup Failed", "Timeout waiting for response");
  453. } else if (strcasecmp(get_header(m, "Response"), "Success")) {
  454. show_message("Hangup Failed", get_header(m, "Message"));
  455. }
  456. }
  457. }
  458. static int get_user_input(char *msg, char *buf, int buflen)
  459. {
  460. newtComponent form;
  461. newtComponent ok;
  462. newtComponent cancel;
  463. newtComponent inpfield;
  464. const char *input;
  465. int res = -1;
  466. struct newtExitStruct es;
  467. newtCenteredWindow(60,7, msg);
  468. inpfield = newtEntry(5, 2, "", 50, _NEWT_CAST &input, 0);
  469. ok = newtButton(22, 3, "OK");
  470. cancel = newtButton(32, 3, "Cancel");
  471. form = newtForm(NULL, NULL, 0);
  472. newtFormAddComponents(form, inpfield, ok, cancel, NULL);
  473. newtFormRun(form, &es);
  474. strncpy(buf, input, buflen - 1);
  475. if (es.u.co == ok)
  476. res = 0;
  477. else
  478. res = -1;
  479. newtPopWindow();
  480. newtFormDestroy(form);
  481. return res;
  482. }
  483. static void try_redirect(newtComponent c)
  484. {
  485. struct ast_chan *chan;
  486. char dest[256];
  487. struct message *m;
  488. static const char tmp_prefix[] = "Enter new extension for ";
  489. char channame[256];
  490. char tmp[sizeof(tmp_prefix) + sizeof(channame)];
  491. char *context;
  492. chan = newtListboxGetCurrent(c);
  493. if (chan) {
  494. strncpy(channame, chan->name, sizeof(channame) - 1);
  495. snprintf(tmp, sizeof(tmp), "%s%s", tmp_prefix, channame);
  496. if (get_user_input(tmp, dest, sizeof(dest)))
  497. return;
  498. if ((context = strchr(dest, '@'))) {
  499. *context = '\0';
  500. context++;
  501. manager_action("Redirect", "Channel: %s\r\nContext: %s\r\nExten: %s\r\nPriority: 1\r\n", chan->name,context,dest);
  502. } else {
  503. manager_action("Redirect", "Channel: %s\r\nExten: %s\r\nPriority: 1\r\n", chan->name, dest);
  504. }
  505. m = wait_for_response(10000);
  506. if (!m) {
  507. show_message("Hangup Failed", "Timeout waiting for response");
  508. } else if (strcasecmp(get_header(m, "Response"), "Success")) {
  509. show_message("Hangup Failed", get_header(m, "Message"));
  510. }
  511. }
  512. }
  513. static int manage_calls(char *host)
  514. {
  515. newtComponent form;
  516. newtComponent quit;
  517. newtComponent hangup;
  518. newtComponent redirect;
  519. newtComponent channels;
  520. struct newtExitStruct es;
  521. char tmp[80];
  522. /* Mark: If there's one thing you learn from this code, it is this...
  523. Never, ever fly Air France. Their customer service is absolutely
  524. the worst. I've never heard the words "That's not my problem" as
  525. many times as I have from their staff -- It should, without doubt
  526. be their corporate motto if it isn't already. Don't bother giving
  527. them business because you're just a pain in their side and they
  528. will be sure to let you know the first time you speak to them.
  529. If you ever want to make me happy just tell me that you, too, will
  530. never fly Air France again either (in spite of their excellent
  531. cuisine).
  532. Update by oej: The merger with KLM has transferred this
  533. behaviour to KLM as well.
  534. Don't bother giving them business either...
  535. Only if you want to travel randomly without luggage, you
  536. might pick either of them.
  537. */
  538. snprintf(tmp, sizeof(tmp), "Asterisk Manager at %s", host);
  539. newtCenteredWindow(74, 20, tmp);
  540. form = newtForm(NULL, NULL, 0);
  541. newtFormWatchFd(form, session.fd, NEWT_FD_READ);
  542. newtFormSetTimer(form, 100);
  543. quit = newtButton(62, 16, "Quit");
  544. redirect = newtButton(35, 16, "Redirect");
  545. hangup = newtButton(50, 16, "Hangup");
  546. channels = newtListbox(1,1,14, NEWT_FLAG_SCROLL);
  547. newtFormAddComponents(form, channels, redirect, hangup, quit, NULL);
  548. newtListboxSetWidth(channels, 72);
  549. show_doing("Getting Status", "Retrieving system status...");
  550. try_status();
  551. hide_doing();
  552. for(;;) {
  553. newtFormRun(form, &es);
  554. if (has_input(&session) || (es.reason == NEWT_EXIT_FDREADY)) {
  555. if (input_check(&session, NULL)) {
  556. show_message("Disconnected", "Disconnected from remote host");
  557. break;
  558. }
  559. } else if (es.reason == NEWT_EXIT_COMPONENT) {
  560. if (es.u.co == quit)
  561. break;
  562. if (es.u.co == hangup) {
  563. try_hangup(channels);
  564. } else if (es.u.co == redirect) {
  565. try_redirect(channels);
  566. }
  567. }
  568. rebuild_channels(channels);
  569. }
  570. newtFormDestroy(form);
  571. return 0;
  572. }
  573. static int manager_login(char *hostname)
  574. {
  575. newtComponent form;
  576. newtComponent cancel;
  577. newtComponent login;
  578. newtComponent username;
  579. newtComponent password;
  580. newtComponent label;
  581. newtComponent ulabel;
  582. newtComponent plabel;
  583. const char *user;
  584. const char *pass;
  585. struct message *m;
  586. struct newtExitStruct es;
  587. char tmp[55];
  588. struct hostent *hp;
  589. int res = -1;
  590. session.fd = socket(AF_INET, SOCK_STREAM, 0);
  591. if (session.fd < 0) {
  592. snprintf(tmp, sizeof(tmp), "socket() failed: %s\n", strerror(errno));
  593. show_message("Socket failed", tmp);
  594. return -1;
  595. }
  596. snprintf(tmp, sizeof(tmp), "Looking up %s\n", hostname);
  597. show_doing("Connecting....", tmp);
  598. hp = gethostbyname(hostname);
  599. if (!hp) {
  600. snprintf(tmp, sizeof(tmp), "No such address: %s\n", hostname);
  601. show_message("Host lookup failed", tmp);
  602. return -1;
  603. }
  604. hide_doing();
  605. snprintf(tmp, sizeof(tmp), "Connecting to %s", hostname);
  606. show_doing("Connecting...", tmp);
  607. session.sin.sin_family = AF_INET;
  608. session.sin.sin_port = htons(DEFAULT_MANAGER_PORT);
  609. memcpy(&session.sin.sin_addr, hp->h_addr, sizeof(session.sin.sin_addr));
  610. if (connect(session.fd,(struct sockaddr*)&session.sin, sizeof(session.sin))) {
  611. snprintf(tmp, sizeof(tmp), "%s failed: %s\n", hostname, strerror(errno));
  612. show_message("Connect Failed", tmp);
  613. return -1;
  614. }
  615. hide_doing();
  616. login = newtButton(5, 6, "Login");
  617. cancel = newtButton(25, 6, "Cancel");
  618. newtCenteredWindow(40, 10, "Asterisk Manager Login");
  619. snprintf(tmp, sizeof(tmp), "Host: %s", hostname);
  620. label = newtLabel(4,1, tmp);
  621. ulabel = newtLabel(4,2,"Username:");
  622. plabel = newtLabel(4,3,"Password:");
  623. username = newtEntry(14, 2, "", 20, _NEWT_CAST &user, 0);
  624. password = newtEntry(14, 3, "", 20, _NEWT_CAST &pass, NEWT_FLAG_HIDDEN);
  625. form = newtForm(NULL, NULL, 0);
  626. newtFormAddComponents(form, username, password, login, cancel, label, ulabel, plabel,NULL);
  627. newtFormRun(form, &es);
  628. if (es.reason == NEWT_EXIT_COMPONENT) {
  629. if (es.u.co == login) {
  630. snprintf(tmp, sizeof(tmp), "Logging in '%s'...", user);
  631. show_doing("Logging in", tmp);
  632. /* Check to see if the remote host supports MD5 Authentication */
  633. manager_action("Challenge", "AuthType: MD5\r\n");
  634. m = wait_for_response(10000);
  635. if (m && !strcasecmp(get_header(m, "Response"), "Success")) {
  636. char *challenge = get_header(m, "Challenge");
  637. int x;
  638. int len = 0;
  639. char md5key[256] = "";
  640. struct MD5Context md5;
  641. unsigned char digest[16];
  642. MD5Init(&md5);
  643. MD5Update(&md5, (unsigned char *)challenge, strlen(challenge));
  644. MD5Update(&md5, (unsigned char *)pass, strlen(pass));
  645. MD5Final(digest, &md5);
  646. for (x=0; x<16; x++)
  647. len += sprintf(md5key + len, "%02hhx", digest[x]);
  648. manager_action("Login",
  649. "AuthType: MD5\r\n"
  650. "Username: %s\r\n"
  651. "Key: %s\r\n",
  652. user, md5key);
  653. m = wait_for_response(10000);
  654. hide_doing();
  655. if (!strcasecmp(get_header(m, "Response"), "Success")) {
  656. res = 0;
  657. } else {
  658. show_message("Login Failed", get_header(m, "Message"));
  659. }
  660. } else {
  661. manager_action("Login",
  662. "Username: %s\r\n"
  663. "Secret: %s\r\n",
  664. user, pass);
  665. m = wait_for_response(10000);
  666. hide_doing();
  667. if (m) {
  668. if (!strcasecmp(get_header(m, "Response"), "Success")) {
  669. res = 0;
  670. } else {
  671. show_message("Login Failed", get_header(m, "Message"));
  672. }
  673. }
  674. }
  675. }
  676. }
  677. newtFormDestroy(form);
  678. return res;
  679. }
  680. int main(int argc, char *argv[])
  681. {
  682. if (argc < 2) {
  683. fprintf(stderr, "Usage: astman <host>\n");
  684. exit(1);
  685. }
  686. newtInit();
  687. newtCls();
  688. newtDrawRootText(0, 0, "Asterisk Manager (C)2002, Linux Support Services, Inc.");
  689. newtPushHelpLine("Welcome to the Asterisk Manager!");
  690. if (manager_login(argv[1])) {
  691. newtFinished();
  692. exit(1);
  693. }
  694. manage_calls(argv[1]);
  695. newtFinished();
  696. return 0;
  697. }