#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include "asterisk/version.h"
#include "asterisk/channel.h"
#include "asterisk/file.h"
#include "asterisk/manager.h"
#include "asterisk/config.h"
#include "asterisk/callerid.h"
#include "asterisk/lock.h"
#include "asterisk/logger.h"
#include "asterisk/options.h"
#include "asterisk/cli.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/md5.h"
#include "asterisk/acl.h"
#include "asterisk/utils.h"
#include "asterisk/http.h"
#include "asterisk/threadstorage.h"
#include "asterisk/linkedlists.h"
#include "asterisk/term.h"
#include "asterisk/astobj2.h"
Go to the source code of this file.
Data Structures | |
struct | ast_manager_user |
struct | eventqent |
struct | fast_originate_helper |
struct | mansession |
struct | permalias |
struct | variable_count |
Defines | |
#define | ASTMAN_APPEND_BUF_INITSIZE 256 |
#define | MANAGER_EVENT_BUF_INITSIZE 256 |
#define | MAX_BLACKLIST_CMD_LEN 2 |
Enumerations | |
enum | { FORMAT_RAW, FORMAT_HTML, FORMAT_XML } |
Functions | |
static void * | accept_thread (void *ignore) |
static int | action_atxfer (struct mansession *s, const struct message *m) |
static int | action_command (struct mansession *s, const struct message *m) |
action_command: Manager command "command" - execute CLI command | |
static int | action_coresettings (struct mansession *s, const struct message *m) |
Show PBX core settings information. | |
static int | action_corestatus (struct mansession *s, const struct message *m) |
Show PBX core status information. | |
static int | action_events (struct mansession *s, const struct message *m) |
static int | action_extensionstate (struct mansession *s, const struct message *m) |
static int | action_getconfig (struct mansession *s, const struct message *m) |
static int | action_getvar (struct mansession *s, const struct message *m) |
static int | action_hangup (struct mansession *s, const struct message *m) |
static int | action_listcommands (struct mansession *s, const struct message *m) |
static int | action_logoff (struct mansession *s, const struct message *m) |
static int | action_mailboxcount (struct mansession *s, const struct message *m) |
static int | action_mailboxstatus (struct mansession *s, const struct message *m) |
static int | action_originate (struct mansession *s, const struct message *m) |
static int | action_ping (struct mansession *s, const struct message *m) |
static int | action_redirect (struct mansession *s, const struct message *m) |
action_redirect: The redirect manager command | |
static int | action_setvar (struct mansession *s, const struct message *m) |
static int | action_status (struct mansession *s, const struct message *m) |
Manager "status" command to show channels. | |
static int | action_timeout (struct mansession *s, const struct message *m) |
static int | action_updateconfig (struct mansession *s, const struct message *m) |
static int | action_userevent (struct mansession *s, const struct message *m) |
static int | action_waitevent (struct mansession *s, const struct message *m) |
static int | append_event (const char *str, int category) |
static struct ast_manager_user * | ast_get_manager_by_name_locked (const char *name) |
static int | ast_instring (const char *bigstr, const char *smallstr, char delim) |
static int | ast_is_number (const char *string) |
static | AST_LIST_HEAD_STATIC (users, ast_manager_user) |
static | AST_LIST_HEAD_STATIC (sessions, mansession) |
int | ast_manager_register2 (const char *action, int auth, int(*func)(struct mansession *s, const struct message *m), const char *synopsis, const char *description) |
register a new command with manager, including online help. This is the preferred way to register a manager command | |
static int | ast_manager_register_struct (struct manager_action *act) |
int | ast_manager_unregister (char *action) |
AST_RWLOCK_DEFINE_STATIC (actionlock) | |
AST_THREADSTORAGE (astman_append_buf, astman_append_buf_init) | |
AST_THREADSTORAGE (manager_event_buf, manager_event_buf_init) | |
void | astman_append (struct mansession *s, const char *fmt,...) |
const char * | astman_get_header (const struct message *m, char *var) |
ast_variable * | astman_get_variables (const struct message *m) |
void | astman_send_ack (struct mansession *s, const struct message *m, char *msg) |
void | astman_send_error (struct mansession *s, const struct message *m, char *error) |
void | astman_send_response (struct mansession *s, const struct message *m, char *resp, char *msg) |
int | astman_verify_session_readpermissions (uint32_t ident, int perm) |
Verify a session's read permissions against a permission mask. | |
int | astman_verify_session_writepermissions (uint32_t ident, int perm) |
Verify a session's write permissions against a permission mask. | |
static int | authenticate (struct mansession *s, const struct message *m) |
static char * | authority_to_str (int authority, char *res, int reslen) |
Convert authority code to string with serveral options. | |
static int | check_blacklist (const char *cmd) |
int | check_manager_enabled () |
Check if AMI is enabled. | |
int | check_webmanager_enabled () |
Check if AMI/HTTP is enabled. | |
static char * | complete_show_mancmd (const char *line, const char *word, int pos, int state) |
static int | compress_char (char c) |
static void | destroy_session (struct mansession *s) |
static int | do_message (struct mansession *s) |
static void * | fast_originate (void *data) |
static struct mansession * | find_session (uint32_t ident) |
static void | free_session (struct mansession *s) |
static char * | generic_http_callback (int format, struct sockaddr_in *requestor, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength) |
static int | get_input (struct mansession *s, char *output) |
static int | get_perm (const char *instr) |
static int | handle_showmanager (int fd, int argc, char *argv[]) |
static int | handle_showmanagers (int fd, int argc, char *argv[]) |
static int | handle_showmancmd (int fd, int argc, char *argv[]) |
static int | handle_showmancmds (int fd, int argc, char *argv[]) |
CLI command Should change to "manager show commands". | |
static int | handle_showmanconn (int fd, int argc, char *argv[]) |
CLI command show manager connected. | |
static int | handle_showmaneventq (int fd, int argc, char *argv[]) |
CLI command show manager eventq. | |
static void | handle_updates (struct mansession *s, const struct message *m, struct ast_config *cfg) |
static char * | html_translate (char *in) |
int | init_manager (void) |
int | manager_event (int category, const char *event, const char *fmt,...) |
manager_event: Send AMI event to client | |
static char * | manager_http_callback (struct sockaddr_in *requestor, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength) |
static int | manager_state_cb (char *context, char *exten, int state, void *data) |
static char * | mxml_http_callback (struct sockaddr_in *requestor, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength) |
static int | process_events (struct mansession *s) |
static int | process_message (struct mansession *s, const struct message *m) |
static char * | rawman_http_callback (struct sockaddr_in *requestor, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength) |
int | reload_manager (void) |
static void * | session_do (void *data) |
static int | set_eventmask (struct mansession *s, const char *eventmask) |
Rather than braindead on,off this now can also accept a specific int mask value or a ',' delim list of mask strings (the same as manager.conf) -anthm. | |
static int | strings_to_mask (const char *string) |
static void | unuse_eventqent (struct eventqent *e) |
static int | variable_count_cmp_fn (void *obj, void *vstr, int flags) |
static int | variable_count_hash_fn (const void *vvc, const int flags) |
static void | xml_copy_escape (char **dst, size_t *maxlen, const char *src, int lower) |
static char * | xml_translate (char *in, struct ast_variable *vars) |
Variables | |
static int | asock = -1 |
static int | block_sockets |
static struct ast_cli_entry | cli_manager [] |
static struct ast_cli_entry | cli_show_manager_command_deprecated |
static struct ast_cli_entry | cli_show_manager_commands_deprecated |
static struct ast_cli_entry | cli_show_manager_connected_deprecated |
static struct ast_cli_entry | cli_show_manager_eventq_deprecated |
struct { | |
char * words [AST_MAX_CMD_LEN] | |
} | command_blacklist [] |
static char * | contenttype [] = { "plain", "html", "xml" } |
static int | displayconnects = 1 |
static struct manager_action * | first_action |
static int | httptimeout = 60 |
static int | manager_enabled = 0 |
ast_http_uri | manageruri |
ast_http_uri | managerxmluri |
static char | mandescr_atxfer [] |
static char | mandescr_command [] |
static char | mandescr_coresettings [] |
static char | mandescr_corestatus [] |
static char | mandescr_events [] |
static char | mandescr_extensionstate [] |
static char | mandescr_getconfig [] |
static char | mandescr_getvar [] |
static char | mandescr_hangup [] |
static char | mandescr_listcommands [] |
static char | mandescr_logoff [] |
static char | mandescr_mailboxcount [] |
static char | mandescr_mailboxstatus [] |
Help text for manager command mailboxstatus. | |
static char | mandescr_originate [] |
static char | mandescr_ping [] |
Manager PING. | |
static char | mandescr_redirect [] |
static char | mandescr_setvar [] |
static char | mandescr_timeout [] |
static char | mandescr_updateconfig [] |
static char | mandescr_userevent [] |
static char | mandescr_waitevent [] |
Manager WAITEVENT. | |
eventqent * | master_eventq = NULL |
static int | num_sessions |
static struct permalias | perms [] |
static int | portno = DEFAULT_MANAGER_PORT |
ast_http_uri | rawmanuri |
static int | registered = 0 |
static char | showmanager_help [] |
static char | showmanagers_help [] |
static char | showmancmd_help [] |
static char | showmancmds_help [] |
static char | showmanconn_help [] |
static char | showmaneventq_help [] |
static pthread_t | t |
static int | timestampevents |
static int | webmanager_enabled = 0 |
static int | webregged = 0 |
Definition in file manager.c.
anonymous enum |
Definition at line 2818 of file manager.c.
02818 { 02819 FORMAT_RAW, 02820 FORMAT_HTML, 02821 FORMAT_XML, 02822 };
int astman_verify_session_readpermissions | ( | uint32_t | ident, | |
int | perm | |||
) |
Verify a session's read permissions against a permission mask.
ident | session identity | |
perm | permission mask to verify |
Definition at line 2780 of file manager.c.
References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), s, and sessions.
02781 { 02782 int result = 0; 02783 struct mansession *s; 02784 02785 AST_LIST_LOCK(&sessions); 02786 AST_LIST_TRAVERSE(&sessions, s, list) { 02787 ast_mutex_lock(&s->__lock); 02788 if ((s->managerid == ident) && (s->readperm & perm)) { 02789 result = 1; 02790 ast_mutex_unlock(&s->__lock); 02791 break; 02792 } 02793 ast_mutex_unlock(&s->__lock); 02794 } 02795 AST_LIST_UNLOCK(&sessions); 02796 return result; 02797 }
int astman_verify_session_writepermissions | ( | uint32_t | ident, | |
int | perm | |||
) |
Verify a session's write permissions against a permission mask.
ident | session identity | |
perm | permission mask to verify |
Definition at line 2799 of file manager.c.
References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), s, and sessions.
02800 { 02801 int result = 0; 02802 struct mansession *s; 02803 02804 AST_LIST_LOCK(&sessions); 02805 AST_LIST_TRAVERSE(&sessions, s, list) { 02806 ast_mutex_lock(&s->__lock); 02807 if ((s->managerid == ident) && (s->writeperm & perm)) { 02808 result = 1; 02809 ast_mutex_unlock(&s->__lock); 02810 break; 02811 } 02812 ast_mutex_unlock(&s->__lock); 02813 } 02814 AST_LIST_UNLOCK(&sessions); 02815 return result; 02816 }
static struct mansession* find_session | ( | uint32_t | ident | ) | [static] |
END Doxygen group
Definition at line 2762 of file manager.c.
References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), s, and sessions.
02763 { 02764 struct mansession *s; 02765 02766 AST_LIST_LOCK(&sessions); 02767 AST_LIST_TRAVERSE(&sessions, s, list) { 02768 ast_mutex_lock(&s->__lock); 02769 if (s->sessiontimeout && (s->managerid == ident) && !s->needdestroy) { 02770 s->inuse++; 02771 break; 02772 } 02773 ast_mutex_unlock(&s->__lock); 02774 } 02775 AST_LIST_UNLOCK(&sessions); 02776 02777 return s; 02778 }
static char* generic_http_callback | ( | int | format, | |
struct sockaddr_in * | requestor, | |||
const char * | uri, | |||
struct ast_variable * | params, | |||
int * | status, | |||
char ** | title, | |||
int * | contentlength | |||
) | [static] |
Definition at line 2825 of file manager.c.
References len, ast_variable::name, ast_variable::next, s, and ast_variable::value.
Referenced by manager_http_callback(), mxml_http_callback(), and rawman_http_callback().
02826 { 02827 struct mansession *s = NULL; 02828 uint32_t ident = 0; 02829 char workspace[512]; 02830 char cookie[128]; 02831 size_t len = sizeof(workspace); 02832 int blastaway = 0; 02833 char *c = workspace; 02834 char *retval = NULL; 02835 struct ast_variable *v; 02836 02837 for (v = params; v; v = v->next) { 02838 if (!strcasecmp(v->name, "mansession_id")) { 02839 sscanf(v->value, "%x", &ident); 02840 break; 02841 } 02842 } 02843 02844 if (!(s = find_session(ident))) { 02845 /* Create new session */ 02846 if (!(s = ast_calloc(1, sizeof(*s)))) { 02847 *status = 500; 02848 goto generic_callback_out; 02849 } 02850 memcpy(&s->sin, requestor, sizeof(s->sin)); 02851 s->fd = -1; 02852 s->waiting_thread = AST_PTHREADT_NULL; 02853 s->send_events = 0; 02854 ast_mutex_init(&s->__lock); 02855 ast_mutex_lock(&s->__lock); 02856 s->inuse = 1; 02857 /*!\note There is approximately a 1 in 1.8E19 chance that the following 02858 * calculation will produce 0, which is an invalid ID, but due to the 02859 * properties of the rand() function (and the constantcy of s), that 02860 * won't happen twice in a row. 02861 */ 02862 while ((s->managerid = rand() ^ (unsigned long) s) == 0); 02863 AST_LIST_LOCK(&sessions); 02864 AST_LIST_INSERT_HEAD(&sessions, s, list); 02865 /* Hook into the last spot in the event queue */ 02866 s->eventq = master_eventq; 02867 while (s->eventq->next) 02868 s->eventq = s->eventq->next; 02869 ast_atomic_fetchadd_int(&s->eventq->usecount, 1); 02870 ast_atomic_fetchadd_int(&num_sessions, 1); 02871 AST_LIST_UNLOCK(&sessions); 02872 } 02873 02874 /* Reset HTTP timeout. If we're not yet authenticated, keep it extremely short */ 02875 time(&s->sessiontimeout); 02876 if (!s->authenticated && (httptimeout > 5)) 02877 s->sessiontimeout += 5; 02878 else 02879 s->sessiontimeout += httptimeout; 02880 ast_mutex_unlock(&s->__lock); 02881 02882 if (s) { 02883 struct message m = { 0 }; 02884 char tmp[80]; 02885 unsigned int x; 02886 size_t hdrlen; 02887 02888 for (x = 0, v = params; v && (x < AST_MAX_MANHEADERS); x++, v = v->next) { 02889 hdrlen = strlen(v->name) + strlen(v->value) + 3; 02890 m.headers[m.hdrcount] = alloca(hdrlen); 02891 snprintf((char *) m.headers[m.hdrcount], hdrlen, "%s: %s", v->name, v->value); 02892 m.hdrcount = x + 1; 02893 } 02894 02895 if (process_message(s, &m)) { 02896 if (s->authenticated) { 02897 if (option_verbose > 1) { 02898 if (displayconnects) 02899 ast_verbose(VERBOSE_PREFIX_2 "HTTP Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(s->sin.sin_addr)); 02900 } 02901 ast_log(LOG_EVENT, "HTTP Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(s->sin.sin_addr)); 02902 } else { 02903 if (option_verbose > 1) { 02904 if (displayconnects) 02905 ast_verbose(VERBOSE_PREFIX_2 "HTTP Connect attempt from '%s' unable to authenticate\n", ast_inet_ntoa(s->sin.sin_addr)); 02906 } 02907 ast_log(LOG_EVENT, "HTTP Failed attempt from %s\n", ast_inet_ntoa(s->sin.sin_addr)); 02908 } 02909 s->needdestroy = 1; 02910 } 02911 ast_build_string(&c, &len, "Content-type: text/%s\r\n", contenttype[format]); 02912 sprintf(tmp, "%08x", s->managerid); 02913 ast_build_string(&c, &len, "%s\r\n", ast_http_setcookie("mansession_id", tmp, httptimeout, cookie, sizeof(cookie))); 02914 if (format == FORMAT_HTML) 02915 ast_build_string(&c, &len, "<title>Asterisk™ Manager Interface</title>"); 02916 if (format == FORMAT_XML) { 02917 ast_build_string(&c, &len, "<ajax-response>\n"); 02918 } else if (format == FORMAT_HTML) { 02919 ast_build_string(&c, &len, "<body bgcolor=\"#ffffff\"><table align=center bgcolor=\"#f1f1f1\" width=\"500\">\r\n"); 02920 ast_build_string(&c, &len, "<tr><td colspan=\"2\" bgcolor=\"#f1f1ff\"><h1> Manager Tester</h1></td></tr>\r\n"); 02921 } 02922 ast_mutex_lock(&s->__lock); 02923 if (s->outputstr) { 02924 char *tmp; 02925 if (format == FORMAT_XML) 02926 tmp = xml_translate(s->outputstr->str, params); 02927 else if (format == FORMAT_HTML) 02928 tmp = html_translate(s->outputstr->str); 02929 else 02930 tmp = s->outputstr->str; 02931 if (tmp) { 02932 retval = malloc(strlen(workspace) + strlen(tmp) + 128); 02933 if (retval) { 02934 strcpy(retval, workspace); 02935 strcpy(retval + strlen(retval), tmp); 02936 c = retval + strlen(retval); 02937 len = 120; 02938 } 02939 } 02940 if (tmp != s->outputstr->str) 02941 free(tmp); 02942 free(s->outputstr); 02943 s->outputstr = NULL; 02944 } 02945 ast_mutex_unlock(&s->__lock); 02946 /* Still okay because c would safely be pointing to workspace even 02947 if retval failed to allocate above */ 02948 if (format == FORMAT_XML) { 02949 ast_build_string(&c, &len, "</ajax-response>\n"); 02950 } else if (format == FORMAT_HTML) 02951 ast_build_string(&c, &len, "</table></body>\r\n"); 02952 } else { 02953 *status = 500; 02954 *title = strdup("Server Error"); 02955 } 02956 ast_mutex_lock(&s->__lock); 02957 if (s->needdestroy) { 02958 if (s->inuse == 1) { 02959 ast_log(LOG_DEBUG, "Need destroy, doing it now!\n"); 02960 blastaway = 1; 02961 } else { 02962 ast_log(LOG_DEBUG, "Need destroy, but can't do it yet!\n"); 02963 if (s->waiting_thread != AST_PTHREADT_NULL) 02964 pthread_kill(s->waiting_thread, SIGURG); 02965 s->inuse--; 02966 } 02967 } else 02968 s->inuse--; 02969 ast_mutex_unlock(&s->__lock); 02970 02971 if (blastaway) 02972 destroy_session(s); 02973 generic_callback_out: 02974 if (*status != 200) 02975 return ast_http_error(500, "Server Error", NULL, "Internal Server Error (out of memory)\n"); 02976 return retval; 02977 }
int init_manager | ( | void | ) |
Called by Asterisk initialization
Definition at line 3018 of file manager.c.
References action_atxfer(), action_command(), action_coresettings(), action_corestatus(), action_events(), action_extensionstate(), action_getconfig(), action_getvar(), action_hangup(), action_listcommands(), action_logoff(), action_mailboxcount(), action_mailboxstatus(), action_originate(), action_ping(), action_redirect(), action_setvar(), action_status(), action_timeout(), action_updateconfig(), action_userevent(), action_waitevent(), append_event(), asock, ast_calloc, ast_category_browse(), ast_cli_register_multiple(), ast_config_load(), ast_extension_state_add(), ast_get_manager_by_name_locked(), AST_LIST_INSERT_TAIL, AST_LIST_LOCK, ast_log(), ast_manager_register, ast_manager_register2(), ast_strdup, ast_true(), ast_variable_browse(), ast_variable_retrieve(), block_sockets, cli_manager, DEFAULT_MANAGER_PORT, ast_manager_user::deny, displayconnects, ast_manager_user::displayconnects, EVENT_FLAG_CALL, EVENT_FLAG_COMMAND, EVENT_FLAG_CONFIG, EVENT_FLAG_SYSTEM, EVENT_FLAG_USER, ast_channel::flags, free, ast_manager_user::keep, LOG_DEBUG, LOG_WARNING, manager_enabled, manager_state_cb(), ast_manager_user::permit, portno, ast_manager_user::read, ast_manager_user::secret, timestampevents, users, var, webmanager_enabled, and ast_manager_user::write.
Referenced by main(), and reload_manager().
03019 { 03020 struct ast_config *cfg = NULL, *ucfg = NULL; 03021 const char *val; 03022 char *cat = NULL; 03023 int oldportno = portno; 03024 static struct sockaddr_in ba; 03025 int x = 1; 03026 int flags; 03027 int newhttptimeout = 60; 03028 struct ast_manager_user *user = NULL; 03029 03030 if (!registered) { 03031 /* Register default actions */ 03032 ast_manager_register2("Ping", 0, action_ping, "Keepalive command", mandescr_ping); 03033 ast_manager_register2("Events", 0, action_events, "Control Event Flow", mandescr_events); 03034 ast_manager_register2("Logoff", 0, action_logoff, "Logoff Manager", mandescr_logoff); 03035 ast_manager_register2("Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel", mandescr_hangup); 03036 ast_manager_register("Status", EVENT_FLAG_CALL, action_status, "Lists channel status" ); 03037 ast_manager_register2("Setvar", EVENT_FLAG_CALL, action_setvar, "Set Channel Variable", mandescr_setvar ); 03038 ast_manager_register2("Getvar", EVENT_FLAG_CALL, action_getvar, "Gets a Channel Variable", mandescr_getvar ); 03039 ast_manager_register2("GetConfig", EVENT_FLAG_CONFIG, action_getconfig, "Retrieve configuration", mandescr_getconfig); 03040 ast_manager_register2("UpdateConfig", EVENT_FLAG_CONFIG, action_updateconfig, "Update basic configuration", mandescr_updateconfig); 03041 ast_manager_register2("Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect (transfer) a call", mandescr_redirect ); 03042 ast_manager_register2("Atxfer", EVENT_FLAG_CALL, action_atxfer, "Attended transfer", mandescr_atxfer ); 03043 ast_manager_register2("Originate", EVENT_FLAG_CALL, action_originate, "Originate Call", mandescr_originate); 03044 ast_manager_register2("Command", EVENT_FLAG_COMMAND, action_command, "Execute Asterisk CLI Command", mandescr_command ); 03045 ast_manager_register2("ExtensionState", EVENT_FLAG_CALL, action_extensionstate, "Check Extension Status", mandescr_extensionstate ); 03046 ast_manager_register2("AbsoluteTimeout", EVENT_FLAG_CALL, action_timeout, "Set Absolute Timeout", mandescr_timeout ); 03047 ast_manager_register2("MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox", mandescr_mailboxstatus ); 03048 ast_manager_register2("MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count", mandescr_mailboxcount ); 03049 ast_manager_register2("ListCommands", 0, action_listcommands, "List available manager commands", mandescr_listcommands); 03050 ast_manager_register2("UserEvent", EVENT_FLAG_USER, action_userevent, "Send an arbitrary event", mandescr_userevent); 03051 ast_manager_register2("CoreSettings", EVENT_FLAG_SYSTEM, action_coresettings, "Show PBX core settings (version etc)", mandescr_coresettings); 03052 ast_manager_register2("CoreStatus", EVENT_FLAG_SYSTEM, action_corestatus, "Show PBX core status variables", mandescr_corestatus); 03053 ast_manager_register2("WaitEvent", 0, action_waitevent, "Wait for an event to occur", mandescr_waitevent); 03054 03055 ast_cli_register_multiple(cli_manager, sizeof(cli_manager) / sizeof(struct ast_cli_entry)); 03056 ast_extension_state_add(NULL, NULL, manager_state_cb, NULL); 03057 registered = 1; 03058 /* Append placeholder event so master_eventq never runs dry */ 03059 append_event("Event: Placeholder\r\n\r\n", 0); 03060 } 03061 portno = DEFAULT_MANAGER_PORT; 03062 displayconnects = 1; 03063 cfg = ast_config_load("manager.conf"); 03064 if (!cfg) { 03065 ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf. Call management disabled.\n"); 03066 return 0; 03067 } 03068 if ((val = ast_variable_retrieve(cfg, "general", "enabled"))) { 03069 manager_enabled = ast_true(val); 03070 } 03071 if ((val = ast_variable_retrieve(cfg, "general", "block-sockets"))) { 03072 block_sockets = ast_true(val); 03073 } 03074 if((val = ast_variable_retrieve(cfg, "general", "webenabled"))) { 03075 webmanager_enabled = ast_true(val); 03076 } 03077 if ((val = ast_variable_retrieve(cfg, "general", "port"))) { 03078 if (sscanf(val, "%d", &portno) != 1) { 03079 ast_log(LOG_WARNING, "Invalid port number '%s'\n", val); 03080 portno = DEFAULT_MANAGER_PORT; 03081 } 03082 } 03083 if ((val = ast_variable_retrieve(cfg, "general", "displayconnects"))) { 03084 displayconnects = ast_true(val); 03085 } 03086 if ((val = ast_variable_retrieve(cfg, "general", "timestampevents"))) { 03087 timestampevents = ast_true(val); 03088 } 03089 if ((val = ast_variable_retrieve(cfg, "general", "httptimeout"))) { 03090 newhttptimeout = atoi(val); 03091 } 03092 03093 memset(&ba, 0, sizeof(ba)); 03094 ba.sin_family = AF_INET; 03095 ba.sin_port = htons(portno); 03096 03097 if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) { 03098 if (!inet_aton(val, &ba.sin_addr)) { 03099 ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val); 03100 memset(&ba.sin_addr, 0, sizeof(ba.sin_addr)); 03101 } 03102 } 03103 03104 03105 if ((asock > -1) && ((portno != oldportno) || !manager_enabled)) { 03106 #if 0 03107 /* Can't be done yet */ 03108 close(asock); 03109 asock = -1; 03110 #else 03111 ast_log(LOG_WARNING, "Unable to change management port / enabled\n"); 03112 #endif 03113 } 03114 03115 AST_LIST_LOCK(&users); 03116 03117 if ((ucfg = ast_config_load("users.conf"))) { 03118 while ((cat = ast_category_browse(ucfg, cat))) { 03119 int hasmanager = 0; 03120 struct ast_variable *var = NULL; 03121 03122 if (!strcasecmp(cat, "general")) { 03123 continue; 03124 } 03125 03126 if (!(hasmanager = ast_true(ast_variable_retrieve(ucfg, cat, "hasmanager")))) { 03127 continue; 03128 } 03129 03130 /* Look for an existing entry, if none found - create one and add it to the list */ 03131 if (!(user = ast_get_manager_by_name_locked(cat))) { 03132 if (!(user = ast_calloc(1, sizeof(*user)))) { 03133 break; 03134 } 03135 /* Copy name over */ 03136 ast_copy_string(user->username, cat, sizeof(user->username)); 03137 /* Insert into list */ 03138 AST_LIST_INSERT_TAIL(&users, user, list); 03139 } 03140 03141 /* Make sure we keep this user and don't destroy it during cleanup */ 03142 user->keep = 1; 03143 03144 for (var = ast_variable_browse(ucfg, cat); var; var = var->next) { 03145 if (!strcasecmp(var->name, "secret")) { 03146 if (user->secret) { 03147 free(user->secret); 03148 } 03149 user->secret = ast_strdup(var->value); 03150 } else if (!strcasecmp(var->name, "deny") ) { 03151 if (user->deny) { 03152 free(user->deny); 03153 } 03154 user->deny = ast_strdup(var->value); 03155 } else if (!strcasecmp(var->name, "permit") ) { 03156 if (user->permit) { 03157 free(user->permit); 03158 } 03159 user->permit = ast_strdup(var->value); 03160 } else if (!strcasecmp(var->name, "read") ) { 03161 if (user->read) { 03162 free(user->read); 03163 } 03164 user->read = ast_strdup(var->value); 03165 } else if (!strcasecmp(var->name, "write") ) { 03166 if (user->write) { 03167 free(user->write); 03168 } 03169 user->write = ast_strdup(var->value); 03170 } else if (!strcasecmp(var->name, "displayconnects") ) { 03171 user->displayconnects = ast_true(var->value); 03172 } else if (!strcasecmp(var->name, "hasmanager")) { 03173 /* already handled */ 03174 } else { 03175 ast_log(LOG_DEBUG, "%s is an unknown option (to the manager module).\n", var->name); 03176 } 03177 } 03178 } 03179 ast_config_destroy(ucfg); 03180 } 03181 03182 while ((cat = ast_category_browse(cfg, cat))) { 03183 struct ast_variable *var = NULL; 03184 03185 if (!strcasecmp(cat, "general")) 03186 continue; 03187 03188 /* Look for an existing entry, if none found - create one and add it to the list */ 03189 if (!(user = ast_get_manager_by_name_locked(cat))) { 03190 if (!(user = ast_calloc(1, sizeof(*user)))) 03191 break; 03192 /* Copy name over */ 03193 ast_copy_string(user->username, cat, sizeof(user->username)); 03194 /* Insert into list */ 03195 AST_LIST_INSERT_TAIL(&users, user, list); 03196 } 03197 03198 /* Make sure we keep this user and don't destroy it during cleanup */ 03199 user->keep = 1; 03200 03201 var = ast_variable_browse(cfg, cat); 03202 while (var) { 03203 if (!strcasecmp(var->name, "secret")) { 03204 if (user->secret) 03205 free(user->secret); 03206 user->secret = ast_strdup(var->value); 03207 } else if (!strcasecmp(var->name, "deny") ) { 03208 if (user->deny) 03209 free(user->deny); 03210 user->deny = ast_strdup(var->value); 03211 } else if (!strcasecmp(var->name, "permit") ) { 03212 if (user->permit) 03213 free(user->permit); 03214 user->permit = ast_strdup(var->value); 03215 } else if (!strcasecmp(var->name, "read") ) { 03216 if (user->read) 03217 free(user->read); 03218 user->read = ast_strdup(var->value); 03219 } else if (!strcasecmp(var->name, "write") ) { 03220 if (user->write) 03221 free(user->write); 03222 user->write = ast_strdup(var->value); 03223 } else if (!strcasecmp(var->name, "displayconnects") ) 03224 user->displayconnects = ast_true(var->value); 03225 else 03226 ast_log(LOG_DEBUG, "%s is an unknown option.\n", var->name); 03227 var = var->next; 03228 } 03229 } 03230 03231 /* Perform cleanup - essentially prune out old users that no longer exist */ 03232 AST_LIST_TRAVERSE_SAFE_BEGIN(&users, user, list) { 03233 if (user->keep) { 03234 user->keep = 0; 03235 continue; 03236 } 03237 /* We do not need to keep this user so take them out of the list */ 03238 AST_LIST_REMOVE_CURRENT(&users, list); 03239 /* Free their memory now */ 03240 if (user->secret) 03241 free(user->secret); 03242 if (user->deny) 03243 free(user->deny); 03244 if (user->permit) 03245 free(user->permit); 03246 if (user->read) 03247 free(user->read); 03248 if (user->write) 03249 free(user->write); 03250 free(user); 03251 } 03252 AST_LIST_TRAVERSE_SAFE_END 03253 03254 AST_LIST_UNLOCK(&users); 03255 03256 ast_config_destroy(cfg); 03257 03258 if (webmanager_enabled && manager_enabled) { 03259 if (!webregged) { 03260 ast_http_uri_link(&rawmanuri); 03261 ast_http_uri_link(&manageruri); 03262 ast_http_uri_link(&managerxmluri); 03263 webregged = 1; 03264 } 03265 } else { 03266 if (webregged) { 03267 ast_http_uri_unlink(&rawmanuri); 03268 ast_http_uri_unlink(&manageruri); 03269 ast_http_uri_unlink(&managerxmluri); 03270 webregged = 0; 03271 } 03272 } 03273 03274 if (newhttptimeout > 0) 03275 httptimeout = newhttptimeout; 03276 03277 /* If not enabled, do nothing */ 03278 if (!manager_enabled) 03279 return 0; 03280 03281 if (asock < 0) { 03282 asock = socket(AF_INET, SOCK_STREAM, 0); 03283 if (asock < 0) { 03284 ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno)); 03285 return -1; 03286 } 03287 setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x)); 03288 if (bind(asock, (struct sockaddr *)&ba, sizeof(ba))) { 03289 ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno)); 03290 close(asock); 03291 asock = -1; 03292 return -1; 03293 } 03294 if (listen(asock, 2)) { 03295 ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno)); 03296 close(asock); 03297 asock = -1; 03298 return -1; 03299 } 03300 flags = fcntl(asock, F_GETFL); 03301 fcntl(asock, F_SETFL, flags | O_NONBLOCK); 03302 if (option_verbose) 03303 ast_verbose("Asterisk Management interface listening on port %d\n", portno); 03304 ast_pthread_create_background(&t, NULL, accept_thread, NULL); 03305 } 03306 return 0; 03307 }
static char* manager_http_callback | ( | struct sockaddr_in * | requestor, | |
const char * | uri, | |||
struct ast_variable * | params, | |||
int * | status, | |||
char ** | title, | |||
int * | contentlength | |||
) | [static] |
Definition at line 2979 of file manager.c.
References FORMAT_HTML, and generic_http_callback().
02980 { 02981 return generic_http_callback(FORMAT_HTML, requestor, uri, params, status, title, contentlength); 02982 }
static char* mxml_http_callback | ( | struct sockaddr_in * | requestor, | |
const char * | uri, | |||
struct ast_variable * | params, | |||
int * | status, | |||
char ** | title, | |||
int * | contentlength | |||
) | [static] |
Definition at line 2984 of file manager.c.
References FORMAT_XML, and generic_http_callback().
02985 { 02986 return generic_http_callback(FORMAT_XML, requestor, uri, params, status, title, contentlength); 02987 }
static char* rawman_http_callback | ( | struct sockaddr_in * | requestor, | |
const char * | uri, | |||
struct ast_variable * | params, | |||
int * | status, | |||
char ** | title, | |||
int * | contentlength | |||
) | [static] |
Definition at line 2989 of file manager.c.
References FORMAT_RAW, and generic_http_callback().
02990 { 02991 return generic_http_callback(FORMAT_RAW, requestor, uri, params, status, title, contentlength); 02992 }
int reload_manager | ( | void | ) |
Definition at line 3309 of file manager.c.
References EVENT_FLAG_SYSTEM, init_manager(), and manager_event().
03310 { 03311 manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n"); 03312 return init_manager(); 03313 }
char* contenttype[] = { "plain", "html", "xml" } [static] |
struct ast_http_uri manageruri |
struct ast_http_uri managerxmluri |
struct ast_http_uri rawmanuri |
int registered = 0 [static] |
char* words[AST_MAX_CMD_LEN] |