2 * Asterisk -- A telephony toolkit for Linux.
4 * Channel Management and more
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
19 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <netinet/tcp.h>
24 #include <arpa/inet.h>
28 #include <asterisk/channel.h>
29 #include <asterisk/file.h>
30 #include <asterisk/manager.h>
31 #include <asterisk/config.h>
32 #include <asterisk/lock.h>
33 #include <asterisk/logger.h>
34 #include <asterisk/options.h>
35 #include <asterisk/cli.h>
36 #include <asterisk/app.h>
37 #include <asterisk/pbx.h>
38 #include <asterisk/md5.h>
39 #include <asterisk/acl.h>
41 static int enabled = 0;
42 static int portno = DEFAULT_MANAGER_PORT;
43 static int asock = -1;
45 static ast_mutex_t sessionlock = AST_MUTEX_INITIALIZER;
46 static int block_sockets = 0;
48 static struct permalias {
52 { EVENT_FLAG_SYSTEM, "system" },
53 { EVENT_FLAG_CALL, "call" },
54 { EVENT_FLAG_LOG, "log" },
55 { EVENT_FLAG_VERBOSE, "verbose" },
56 { EVENT_FLAG_COMMAND, "command" },
57 { EVENT_FLAG_AGENT, "agent" },
58 { EVENT_FLAG_USER, "user" },
62 static struct mansession *sessions = NULL;
63 static struct manager_action *first_action = NULL;
64 static ast_mutex_t actionlock = AST_MUTEX_INITIALIZER;
66 int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
68 /* Try to write string, but wait no more than ms milliseconds
74 res = write(fd, s, len);
75 if ((res < 0) && (errno != EAGAIN)) {
81 tv.tv_sec = timeoutms / 1000;
82 tv.tv_usec = timeoutms % 1000;
85 /* Wait until writable again */
86 res = select(fd + 1, NULL, &fds, NULL, &tv);
93 static int handle_showmancmds(int fd, int argc, char *argv[])
95 struct manager_action *cur = first_action;
96 char *format = " %-15.15s %-45.45s\n";
98 ast_mutex_lock(&actionlock);
99 ast_cli(fd, format, "Action", "Synopsis");
100 while(cur) { /* Walk the list of actions */
101 ast_cli(fd, format, cur->action, cur->synopsis);
105 ast_mutex_unlock(&actionlock);
106 return RESULT_SUCCESS;
109 static int handle_showmanconn(int fd, int argc, char *argv[])
111 struct mansession *s;
112 char *format = " %-15.15s %-15.15s\n";
113 ast_mutex_lock(&sessionlock);
115 ast_cli(fd, format, "Username", "IP Address");
117 ast_cli(fd, format,s->username, inet_ntoa(s->sin.sin_addr));
121 ast_mutex_unlock(&sessionlock);
122 return RESULT_SUCCESS;
125 static char showmancmds_help[] =
126 "Usage: show manager commands\n"
127 " Prints a listing of all the available manager commands.\n";
129 static char showmanconn_help[] =
130 "Usage: show manager connected\n"
131 " Prints a listing of the users that are connected to the\n"
132 "manager interface.\n";
134 static struct ast_cli_entry show_mancmds_cli =
135 { { "show", "manager", "commands", NULL },
136 handle_showmancmds, "Show manager commands", showmancmds_help };
138 static struct ast_cli_entry show_manconn_cli =
139 { { "show", "manager", "connected", NULL },
140 handle_showmanconn, "Show connected manager users", showmanconn_help };
142 static void destroy_session(struct mansession *s)
144 struct mansession *cur, *prev = NULL;
145 ast_mutex_lock(&sessionlock);
155 prev->next = cur->next;
157 sessions = cur->next;
162 ast_log(LOG_WARNING, "Trying to delete non-existant session %p?\n", s);
163 ast_mutex_unlock(&sessionlock);
167 char *astman_get_header(struct message *m, char *var)
171 snprintf(cmp, sizeof(cmp), "%s: ", var);
172 for (x=0;x<m->hdrcount;x++)
173 if (!strncasecmp(cmp, m->headers[x], strlen(cmp)))
174 return m->headers[x] + strlen(cmp);
178 void astman_send_error(struct mansession *s, struct message *m, char *error)
180 char *id = astman_get_header(m,"ActionID");
181 ast_mutex_lock(&s->lock);
182 ast_cli(s->fd, "Response: Error\r\n");
183 if (id && strlen(id))
184 ast_cli(s->fd, "ActionID: %s\r\n",id);
185 ast_cli(s->fd, "Message: %s\r\n\r\n", error);
186 ast_mutex_unlock(&s->lock);
189 void astman_send_response(struct mansession *s, struct message *m, char *resp, char *msg)
191 char *id = astman_get_header(m,"ActionID");
192 ast_mutex_lock(&s->lock);
193 ast_cli(s->fd, "Response: %s\r\n", resp);
194 if (id && strlen(id))
195 ast_cli(s->fd, "ActionID: %s\r\n",id);
197 ast_cli(s->fd, "Message: %s\r\n\r\n", msg);
199 ast_cli(s->fd, "\r\n");
200 ast_mutex_unlock(&s->lock);
203 void astman_send_ack(struct mansession *s, struct message *m, char *msg)
205 astman_send_response(s, m, "Success", msg);
208 static int get_perm(char *instr)
217 strncpy(tmp, instr, sizeof(tmp) - 1);
219 c = strsep(&stringp, ",");
221 for (x=0;x<sizeof(perms) / sizeof(perms[0]);x++) {
222 if (!strcasecmp(perms[x].label, c))
225 c = strsep(&stringp, ",");
230 static int set_eventmask(struct mansession *s, char *eventmask)
234 if (!strcasecmp(eventmask, "on") || ast_true(eventmask)) {
235 ast_mutex_lock(&s->lock);
237 ast_mutex_unlock(&s->lock);
239 } else if (!strcasecmp(eventmask, "off") || ast_false(eventmask)) {
240 ast_mutex_lock(&s->lock);
242 ast_mutex_unlock(&s->lock);
248 static int authenticate(struct mansession *s, struct message *m)
250 struct ast_config *cfg;
252 char *user = astman_get_header(m, "Username");
253 char *pass = astman_get_header(m, "Secret");
254 char *authtype = astman_get_header(m, "AuthType");
255 char *key = astman_get_header(m, "Key");
256 char *events = astman_get_header(m, "Events");
258 cfg = ast_load("manager.conf");
261 cat = ast_category_browse(cfg, NULL);
263 if (strcasecmp(cat, "general")) {
265 if (!strcasecmp(cat, user)) {
266 struct ast_variable *v;
267 struct ast_ha *ha = NULL;
268 char *password = NULL;
269 v = ast_variable_browse(cfg, cat);
271 if (!strcasecmp(v->name, "secret")) {
273 } else if (!strcasecmp(v->name, "permit") ||
274 !strcasecmp(v->name, "deny")) {
275 ha = ast_append_ha(v->name, v->value, ha);
279 if (ha && !ast_apply_ha(ha, &(s->sin))) {
280 ast_log(LOG_NOTICE, "%s failed to pass IP ACL as '%s'\n", inet_ntoa(s->sin.sin_addr), user);
286 if (!strcasecmp(authtype, "MD5")) {
287 if (key && strlen(key) && s->challenge) {
290 char md5key[256] = "";
291 struct MD5Context md5;
292 unsigned char digest[16];
294 MD5Update(&md5, s->challenge, strlen(s->challenge));
295 MD5Update(&md5, password, strlen(password));
296 MD5Final(digest, &md5);
298 len += sprintf(md5key + len, "%2.2x", digest[x]);
299 if (!strcmp(md5key, key))
306 } else if (password && !strcasecmp(password, pass)) {
309 ast_log(LOG_NOTICE, "%s failed to authenticate as '%s'\n", inet_ntoa(s->sin.sin_addr), user);
315 cat = ast_category_browse(cfg, cat);
318 strncpy(s->username, cat, sizeof(s->username) - 1);
319 s->readperm = get_perm(ast_variable_retrieve(cfg, cat, "read"));
320 s->writeperm = get_perm(ast_variable_retrieve(cfg, cat, "write"));
323 set_eventmask(s, events);
326 ast_log(LOG_NOTICE, "%s tried to authenticate with non-existant user '%s'\n", inet_ntoa(s->sin.sin_addr), user);
331 static int action_ping(struct mansession *s, struct message *m)
333 astman_send_response(s, m, "Pong", NULL);
337 static int action_events(struct mansession *s, struct message *m)
339 char *mask = astman_get_header(m, "EventMask");
342 res = set_eventmask(s, mask);
344 astman_send_response(s, m, "Events On", NULL);
346 astman_send_response(s, m, "Events Off", NULL);
348 astman_send_response(s, m, "EventMask parse error", NULL);
352 static int action_logoff(struct mansession *s, struct message *m)
354 astman_send_response(s, m, "Goodbye", "Thanks for all the fish.");
358 static int action_hangup(struct mansession *s, struct message *m)
360 struct ast_channel *c = NULL;
361 char *name = astman_get_header(m, "Channel");
363 astman_send_error(s, m, "No channel specified");
366 c = ast_channel_walk(NULL);
368 if (!strcasecmp(c->name, name)) {
371 c = ast_channel_walk(c);
374 astman_send_error(s, m, "No such channel");
377 ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
378 astman_send_ack(s, m, "Channel Hungup");
382 static int action_status(struct mansession *s, struct message *m)
384 char *id = astman_get_header(m,"ActionID");
385 char idText[256] = "";
386 struct ast_channel *c;
388 astman_send_ack(s, m, "Channel status will follow");
389 c = ast_channel_walk(NULL);
390 if (id && strlen(id))
391 snprintf(idText,256,"ActionID: %s\r\n",id);
394 snprintf(bridge, sizeof(bridge), "Link: %s\r\n", c->bridge->name);
410 c->name, c->callerid ? c->callerid : "<unknown>",
411 ast_state2str(c->_state), c->context,
412 c->exten, c->priority, bridge, c->uniqueid, idText);
423 c->name, c->callerid ? c->callerid : "<unknown>",
424 ast_state2str(c->_state), bridge, c->uniqueid, idText);
426 c = ast_channel_walk(c);
429 "Event: StatusComplete\r\n"
435 static int action_redirect(struct mansession *s, struct message *m)
437 char *name = astman_get_header(m, "Channel");
438 char *name2 = astman_get_header(m, "ExtraChannel");
439 char *exten = astman_get_header(m, "Exten");
440 char *context = astman_get_header(m, "Context");
441 char *priority = astman_get_header(m, "Priority");
444 if (!name || !strlen(name)) {
445 astman_send_error(s, m, "Channel not specified");
448 if (strlen(priority) && (sscanf(priority, "%d", &pi) != 1)) {
449 astman_send_error(s, m, "Invalid priority\n");
452 res = ast_async_goto_by_name(name, context, exten, pi);
455 res = ast_async_goto_by_name(name2, context, exten, pi);
457 astman_send_ack(s, m, "Dual Redirect successful");
459 astman_send_error(s, m, "Secondary redirect failed");
461 astman_send_ack(s, m, "Redirect successful");
463 astman_send_error(s, m, "Redirect failed");
467 static int action_command(struct mansession *s, struct message *m)
469 char *cmd = astman_get_header(m, "Command");
470 ast_mutex_lock(&s->lock);
472 ast_mutex_unlock(&s->lock);
473 ast_cli(s->fd, "Response: Follows\r\n");
474 /* FIXME: Wedge a ActionID response in here, waiting for later changes */
475 ast_cli_command(s->fd, cmd);
476 ast_cli(s->fd, "--END COMMAND--\r\n\r\n");
477 ast_mutex_lock(&s->lock);
479 ast_mutex_unlock(&s->lock);
483 static int action_originate(struct mansession *s, struct message *m)
485 char *name = astman_get_header(m, "Channel");
486 char *exten = astman_get_header(m, "Exten");
487 char *context = astman_get_header(m, "Context");
488 char *priority = astman_get_header(m, "Priority");
489 char *timeout = astman_get_header(m, "Timeout");
490 char *callerid = astman_get_header(m, "CallerID");
491 char *variable = astman_get_header(m, "Variable");
492 char *account = astman_get_header(m, "Account");
493 char *app = astman_get_header(m, "Application");
494 char *appdata = astman_get_header(m, "Data");
502 astman_send_error(s, m, "Channel not specified");
505 if (strlen(priority) && (sscanf(priority, "%d", &pi) != 1)) {
506 astman_send_error(s, m, "Invalid priority\n");
509 if (strlen(timeout) && (sscanf(timeout, "%d", &to) != 1)) {
510 astman_send_error(s, m, "Invalid timeout\n");
513 strncpy(tmp, name, sizeof(tmp) - 1);
515 data = strchr(tmp, '/');
517 astman_send_error(s, m, "Invalid channel\n");
523 res = ast_pbx_outgoing_app(tech, AST_FORMAT_SLINEAR, data, to, app, appdata, &reason, 0, strlen(callerid) ? callerid : NULL, variable, account);
525 if (exten && context && pi)
526 res = ast_pbx_outgoing_exten(tech, AST_FORMAT_SLINEAR, data, to, context, exten, pi, &reason, 0, strlen(callerid) ? callerid : NULL, variable, account);
528 astman_send_error(s, m, "Originate with 'Exten' requires 'Context' and 'Priority'");
533 astman_send_ack(s, m, "Originate successfully queued");
535 astman_send_error(s, m, "Originate failed");
539 static int action_mailboxstatus(struct mansession *s, struct message *m)
541 char *mailbox = astman_get_header(m, "Mailbox");
542 char *id = astman_get_header(m,"ActionID");
543 char idText[256] = "";
544 if (!mailbox || !strlen(mailbox)) {
545 astman_send_error(s, m, "Mailbox not specified");
548 if (id && strlen(id))
549 snprintf(idText,256,"ActionID: %s\r\n",id);
550 ast_cli(s->fd, "Response: Success\r\n"
552 "Message: Mailbox Status\r\n"
554 "Waiting: %d\r\n\r\n", idText, mailbox, ast_app_has_voicemail(mailbox));
558 static int action_mailboxcount(struct mansession *s, struct message *m)
560 char *mailbox = astman_get_header(m, "Mailbox");
561 char *id = astman_get_header(m,"ActionID");
562 char idText[256] = "";
563 int newmsgs = 0, oldmsgs = 0;
564 if (!mailbox || !strlen(mailbox)) {
565 astman_send_error(s, m, "Mailbox not specified");
568 ast_app_messagecount(mailbox, &newmsgs, &oldmsgs);
569 if (id && strlen(id)) {
570 snprintf(idText,256,"ActionID: %s\r\n",id);
572 ast_cli(s->fd, "Response: Success\r\n"
574 "Message: Mailbox Message Count\r\n"
576 "NewMessages: %d\r\n"
577 "OldMessages: %d\r\n"
579 idText,mailbox, newmsgs, oldmsgs);
583 static int action_extensionstate(struct mansession *s, struct message *m)
585 char *exten = astman_get_header(m, "Exten");
586 char *context = astman_get_header(m, "Context");
587 char *id = astman_get_header(m,"ActionID");
588 char idText[256] = "";
591 if (!exten || !strlen(exten)) {
592 astman_send_error(s, m, "Extension not specified");
595 if (!context || !strlen(context))
597 status = ast_extension_state(NULL, context, exten);
598 ast_get_hint(hint, sizeof(hint) - 1, NULL, context, exten);
599 if (id && strlen(id)) {
600 snprintf(idText,256,"ActionID: %s\r\n",id);
602 ast_cli(s->fd, "Response: Success\r\n"
604 "Message: Extension Status\r\n"
608 "Status: %d\r\n\r\n",
609 idText,exten, context, hint, status);
613 static int action_timeout(struct mansession *s, struct message *m)
615 struct ast_channel *c = NULL;
616 char *name = astman_get_header(m, "Channel");
617 int timeout = atoi(astman_get_header(m, "Timeout"));
619 astman_send_error(s, m, "No channel specified");
623 astman_send_error(s, m, "No timeout specified");
626 c = ast_channel_walk(NULL);
628 if (!strcasecmp(c->name, name)) {
631 c = ast_channel_walk(c);
634 astman_send_error(s, m, "No such channel");
637 ast_channel_setwhentohangup(c, timeout);
638 astman_send_ack(s, m, "Timeout Set");
642 static int process_message(struct mansession *s, struct message *m)
645 struct manager_action *tmp = first_action;
646 char *id = astman_get_header(m,"ActionID");
647 char idText[256] = "";
649 strncpy(action, astman_get_header(m, "Action"), sizeof(action));
650 ast_log( LOG_DEBUG, "Manager received command '%s'\n", action );
652 if (!strlen(action)) {
653 astman_send_error(s, m, "Missing action in request");
656 if (id && strlen(id)) {
657 snprintf(idText,256,"ActionID: %s\r\n",id);
659 if (!s->authenticated) {
660 if (!strcasecmp(action, "Challenge")) {
662 authtype = astman_get_header(m, "AuthType");
663 if (!strcasecmp(authtype, "MD5")) {
664 if (!s->challenge || !strlen(s->challenge)) {
665 ast_mutex_lock(&s->lock);
666 snprintf(s->challenge, sizeof(s->challenge), "%d", rand());
667 ast_mutex_unlock(&s->lock);
669 ast_cli(s->fd, "Response: Success\r\n"
671 "Challenge: %s\r\n\r\n",
672 idText,s->challenge);
675 astman_send_error(s, m, "Must specify AuthType");
678 } else if (!strcasecmp(action, "Login")) {
679 if (authenticate(s, m)) {
681 astman_send_error(s, m, "Authentication failed");
684 s->authenticated = 1;
685 if (option_verbose > 1)
686 ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged on from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
687 ast_log(LOG_EVENT, "Manager '%s' logged on from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
688 astman_send_ack(s, m, "Authentication accepted");
690 } else if (!strcasecmp(action, "Logoff")) {
691 astman_send_ack(s, m, "See ya");
694 astman_send_error(s, m, "Authentication Required");
697 if (!strcasecmp(action, tmp->action)) {
698 if ((s->writeperm & tmp->authority) == tmp->authority) {
702 astman_send_error(s, m, "Permission denied");
708 astman_send_error(s, m, "Invalid/unknown command");
713 static int get_input(struct mansession *s, char *output)
715 /* output must have at least sizeof(s->inbuf) space */
719 for (x=1;x<s->inlen;x++) {
720 if ((s->inbuf[x] == '\n') && (s->inbuf[x-1] == '\r')) {
721 /* Copy output data up to and including \r\n */
722 memcpy(output, s->inbuf, x + 1);
723 /* Add trailing \0 */
725 /* Move remaining data back to the front */
726 memmove(s->inbuf, s->inbuf + x + 1, s->inlen - x);
731 if (s->inlen >= sizeof(s->inbuf) - 1) {
732 ast_log(LOG_WARNING, "Dumping long line with no return from %s: %s\n", inet_ntoa(s->sin.sin_addr), s->inbuf);
737 res = ast_select(s->fd + 1, &fds, NULL, NULL, NULL);
739 ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
740 } else if (res > 0) {
741 ast_mutex_lock(&s->lock);
742 res = read(s->fd, s->inbuf + s->inlen, sizeof(s->inbuf) - 1 - s->inlen);
743 ast_mutex_unlock(&s->lock);
748 s->inbuf[s->inlen] = '\0';
752 static void *session_do(void *data)
754 struct mansession *s = data;
758 ast_mutex_lock(&s->lock);
759 ast_cli(s->fd, "Asterisk Call Manager/1.0\r\n");
760 ast_mutex_unlock(&s->lock);
761 memset(&m, 0, sizeof(&m));
763 res = get_input(s, m.headers[m.hdrcount]);
765 /* Strip trailing \r\n */
766 if (strlen(m.headers[m.hdrcount]) < 2)
768 m.headers[m.hdrcount][strlen(m.headers[m.hdrcount]) - 2] = '\0';
769 if (!strlen(m.headers[m.hdrcount])) {
770 if (process_message(s, &m))
772 memset(&m, 0, sizeof(&m));
773 } else if (m.hdrcount < MAX_HEADERS - 1)
778 if (s->authenticated) {
779 if (option_verbose > 1)
780 ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged off from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
781 ast_log(LOG_EVENT, "Manager '%s' logged off from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
783 if (option_verbose > 1)
784 ast_verbose(VERBOSE_PREFIX_2 "Connect attempt from '%s' unable to authenticate\n", inet_ntoa(s->sin.sin_addr));
785 ast_log(LOG_EVENT, "Failed attempt from %s\n", inet_ntoa(s->sin.sin_addr));
791 static void *accept_thread(void *ignore)
794 struct sockaddr_in sin;
796 struct mansession *s;
802 pthread_attr_init(&attr);
803 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
806 sinlen = sizeof(sin);
807 as = accept(asock, (struct sockaddr *)&sin, &sinlen);
809 ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
812 p = getprotobyname("tcp");
814 if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
815 ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
818 s = malloc(sizeof(struct mansession));
820 ast_log(LOG_WARNING, "Failed to allocate management session: %s\n", strerror(errno));
823 memset(s, 0, sizeof(struct mansession));
824 memcpy(&s->sin, &sin, sizeof(sin));
826 if(! block_sockets) {
827 /* For safety, make sure socket is non-blocking */
828 flags = fcntl(as, F_GETFL);
829 fcntl(as, F_SETFL, flags | O_NONBLOCK);
831 ast_mutex_init(&s->lock);
834 ast_mutex_lock(&sessionlock);
837 ast_mutex_unlock(&sessionlock);
838 if (pthread_create(&t, &attr, session_do, s))
841 pthread_attr_destroy(&attr);
845 int manager_event(int category, char *event, char *fmt, ...)
847 struct mansession *s;
851 ast_mutex_lock(&sessionlock);
854 if (((s->readperm & category) == category) && s->send_events) {
855 ast_mutex_lock(&s->lock);
857 ast_cli(s->fd, "Event: %s\r\n", event);
859 vsnprintf(tmp, sizeof(tmp), fmt, ap);
861 ast_carefulwrite(s->fd,tmp,strlen(tmp),100);
862 ast_cli(s->fd, "\r\n");
864 ast_mutex_unlock(&s->lock);
868 ast_mutex_unlock(&sessionlock);
872 int ast_manager_unregister( char *action ) {
873 struct manager_action *cur = first_action, *prev = first_action;
875 ast_mutex_lock(&actionlock);
877 if (!strcasecmp(action, cur->action)) {
878 prev->next = cur->next;
880 if (option_verbose > 1)
881 ast_verbose(VERBOSE_PREFIX_2 "Manager unregistered action %s\n", action);
882 ast_mutex_unlock(&actionlock);
888 ast_mutex_unlock(&actionlock);
892 static int manager_state_cb(char *context, char *exten, int state, void *data)
894 /* Notify managers of change */
895 manager_event(EVENT_FLAG_CALL, "ExtensionStatus", "Exten: %s\r\nContext: %s\r\nStatus: %d\r\n", exten, context, state);
899 int ast_manager_register( char *action, int auth,
900 int (*func)(struct mansession *s, struct message *m), char *synopsis)
902 struct manager_action *cur = first_action, *prev = NULL;
904 ast_mutex_lock(&actionlock);
905 while(cur) { /* Walk the list of actions */
906 if (!strcasecmp(cur->action, action)) {
907 ast_log(LOG_WARNING, "Manager: Action '%s' already registered\n", action);
908 ast_mutex_unlock(&actionlock);
914 cur = malloc( sizeof(struct manager_action) );
916 ast_log(LOG_WARNING, "Manager: out of memory trying to register action\n");
917 ast_mutex_unlock(&actionlock);
920 strncpy( cur->action, action, 255 );
921 cur->authority = auth;
923 cur->synopsis = synopsis;
926 if( prev ) prev->next = cur;
927 else first_action = cur;
929 if (option_verbose > 1)
930 ast_verbose(VERBOSE_PREFIX_2 "Manager registered action %s\n", action);
931 ast_mutex_unlock(&actionlock);
935 static int registered = 0;
937 int init_manager(void)
939 struct ast_config *cfg;
941 int oldportno = portno;
942 static struct sockaddr_in ba;
945 /* Register default actions */
946 ast_manager_register( "Ping", 0, action_ping, "Ping" );
947 ast_manager_register( "Events", 0, action_events, "Contol Event Flow" );
948 ast_manager_register( "Logoff", 0, action_logoff, "Logoff Manager" );
949 ast_manager_register( "Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel" );
950 ast_manager_register( "Status", EVENT_FLAG_CALL, action_status, "Status" );
951 ast_manager_register( "Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect" );
952 ast_manager_register( "Originate", EVENT_FLAG_CALL, action_originate, "Originate Call" );
953 ast_manager_register( "MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox" );
954 ast_manager_register( "Command", EVENT_FLAG_COMMAND, action_command, "Execute Command" );
955 ast_manager_register( "ExtensionState", EVENT_FLAG_CALL, action_extensionstate, "Check Extension Status" );
956 ast_manager_register( "AbsoluteTimeout", EVENT_FLAG_CALL, action_timeout, "Set Absolute Timeout" );
957 ast_manager_register( "MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count" );
959 ast_cli_register(&show_mancmds_cli);
960 ast_cli_register(&show_manconn_cli);
961 ast_extension_state_add(NULL, NULL, manager_state_cb, NULL);
964 portno = DEFAULT_MANAGER_PORT;
965 cfg = ast_load("manager.conf");
967 ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf. Call management disabled.\n");
970 memset(&ba, 0, sizeof(ba));
971 val = ast_variable_retrieve(cfg, "general", "enabled");
973 enabled = ast_true(val);
975 val = ast_variable_retrieve(cfg, "general", "block-sockets");
977 block_sockets = ast_true(val);
979 if ((val = ast_variable_retrieve(cfg, "general", "port"))) {
980 if (sscanf(val, "%d", &portno) != 1) {
981 ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
982 portno = DEFAULT_MANAGER_PORT;
984 } else if ((val = ast_variable_retrieve(cfg, "general", "portno"))) {
985 if (sscanf(val, "%d", &portno) != 1) {
986 ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
987 portno = DEFAULT_MANAGER_PORT;
989 ast_log(LOG_NOTICE, "Use of portno in manager.conf deprecated. Please use 'port=%s' instead.\n", val);
992 ba.sin_family = AF_INET;
993 ba.sin_port = htons(portno);
994 memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
996 if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) {
997 if (!inet_aton(val, &ba.sin_addr)) {
998 ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val);
999 memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
1003 if ((asock > -1) && ((portno != oldportno) || !enabled)) {
1005 /* Can't be done yet */
1009 ast_log(LOG_WARNING, "Unable to change management port / enabled\n");
1014 /* If not enabled, do nothing */
1019 asock = socket(AF_INET, SOCK_STREAM, 0);
1021 ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
1024 setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
1025 if (bind(asock, (struct sockaddr *)&ba, sizeof(ba))) {
1026 ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno));
1031 if (listen(asock, 2)) {
1032 ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno));
1038 ast_verbose("Asterisk Management interface listening on port %d\n", portno);
1039 pthread_create(&t, NULL, accept_thread, NULL);
1044 int reload_manager(void)
1046 manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n");
1047 return init_manager();