#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 <sys/mman.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 | mansession_session |
struct | permalias |
struct | sessions |
struct | users |
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) |
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) |
void | astman_append (struct mansession *s, const char *fmt,...) |
static void | astman_append_buf_init (void) |
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_session *s) |
static int | do_message (struct mansession *s) |
static void * | fast_originate (void *data) |
static struct mansession_session * | find_session (uint32_t ident) |
static void | free_session (struct mansession_session *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_session *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 void | manager_event_buf_init (void) |
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_session *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 ast_rwlock_t | actionlock = PTHREAD_RWLOCK_INITIALIZER |
static int | asock = -1 |
static struct ast_threadstorage | astman_append_buf = { .once = PTHREAD_ONCE_INIT, .key_init = astman_append_buf_init , } |
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 |
static struct ast_threadstorage | manager_event_buf = { .once = PTHREAD_ONCE_INIT, .key_init = manager_event_buf_init , } |
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 2911 of file manager.c.
02911 { 02912 FORMAT_RAW, 02913 FORMAT_HTML, 02914 FORMAT_XML, 02915 };
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 2873 of file manager.c.
References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), and s.
02874 { 02875 int result = 0; 02876 struct mansession_session *s; 02877 02878 AST_LIST_LOCK(&sessions); 02879 AST_LIST_TRAVERSE(&sessions, s, list) { 02880 ast_mutex_lock(&s->__lock); 02881 if ((s->managerid == ident) && (s->readperm & perm)) { 02882 result = 1; 02883 ast_mutex_unlock(&s->__lock); 02884 break; 02885 } 02886 ast_mutex_unlock(&s->__lock); 02887 } 02888 AST_LIST_UNLOCK(&sessions); 02889 return result; 02890 }
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 2892 of file manager.c.
References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), and s.
02893 { 02894 int result = 0; 02895 struct mansession_session *s; 02896 02897 AST_LIST_LOCK(&sessions); 02898 AST_LIST_TRAVERSE(&sessions, s, list) { 02899 ast_mutex_lock(&s->__lock); 02900 if ((s->managerid == ident) && (s->writeperm & perm)) { 02901 result = 1; 02902 ast_mutex_unlock(&s->__lock); 02903 break; 02904 } 02905 ast_mutex_unlock(&s->__lock); 02906 } 02907 AST_LIST_UNLOCK(&sessions); 02908 return result; 02909 }
static struct mansession_session* find_session | ( | uint32_t | ident | ) | [static] |
END Doxygen group
Definition at line 2855 of file manager.c.
References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), and s.
02856 { 02857 struct mansession_session *s; 02858 02859 AST_LIST_LOCK(&sessions); 02860 AST_LIST_TRAVERSE(&sessions, s, list) { 02861 ast_mutex_lock(&s->__lock); 02862 if (s->sessiontimeout && (s->managerid == ident) && !s->needdestroy) { 02863 s->inuse++; 02864 break; 02865 } 02866 ast_mutex_unlock(&s->__lock); 02867 } 02868 AST_LIST_UNLOCK(&sessions); 02869 02870 return s; 02871 }
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 2918 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().
02919 { 02920 struct mansession_session *s = NULL; 02921 struct mansession ss = { .session = NULL, }; 02922 uint32_t ident = 0; 02923 char workspace[512]; 02924 char cookie[128]; 02925 size_t len = sizeof(workspace); 02926 int blastaway = 0; 02927 char *c = workspace; 02928 char *retval = NULL; 02929 struct ast_variable *v; 02930 02931 for (v = params; v; v = v->next) { 02932 if (!strcasecmp(v->name, "mansession_id")) { 02933 sscanf(v->value, "%x", &ident); 02934 break; 02935 } 02936 } 02937 02938 if (!(s = find_session(ident))) { 02939 /* Create new session */ 02940 if (!(s = ast_calloc(1, sizeof(*s)))) { 02941 *status = 500; 02942 goto generic_callback_out; 02943 } 02944 memcpy(&s->sin, requestor, sizeof(s->sin)); 02945 s->fd = -1; 02946 s->waiting_thread = AST_PTHREADT_NULL; 02947 s->send_events = 0; 02948 ast_mutex_init(&s->__lock); 02949 ast_mutex_lock(&s->__lock); 02950 s->inuse = 1; 02951 /*!\note There is approximately a 1 in 1.8E19 chance that the following 02952 * calculation will produce 0, which is an invalid ID, but due to the 02953 * properties of the rand() function (and the constantcy of s), that 02954 * won't happen twice in a row. 02955 */ 02956 while ((s->managerid = rand() ^ (unsigned long) s) == 0); 02957 AST_LIST_LOCK(&sessions); 02958 AST_LIST_INSERT_HEAD(&sessions, s, list); 02959 /* Hook into the last spot in the event queue */ 02960 s->eventq = master_eventq; 02961 while (s->eventq->next) 02962 s->eventq = s->eventq->next; 02963 ast_atomic_fetchadd_int(&s->eventq->usecount, 1); 02964 ast_atomic_fetchadd_int(&num_sessions, 1); 02965 AST_LIST_UNLOCK(&sessions); 02966 } 02967 02968 /* Reset HTTP timeout. If we're not yet authenticated, keep it extremely short */ 02969 time(&s->sessiontimeout); 02970 if (!s->authenticated && (httptimeout > 5)) 02971 s->sessiontimeout += 5; 02972 else 02973 s->sessiontimeout += httptimeout; 02974 ss.session = s; 02975 ast_mutex_unlock(&s->__lock); 02976 02977 ss.f = tmpfile(); 02978 ss.fd = fileno(ss.f); 02979 02980 if (s) { 02981 struct message m = { 0 }; 02982 char tmp[80]; 02983 unsigned int x; 02984 size_t hdrlen; 02985 02986 for (x = 0, v = params; v && (x < AST_MAX_MANHEADERS); x++, v = v->next) { 02987 hdrlen = strlen(v->name) + strlen(v->value) + 3; 02988 m.headers[m.hdrcount] = alloca(hdrlen); 02989 snprintf((char *) m.headers[m.hdrcount], hdrlen, "%s: %s", v->name, v->value); 02990 m.hdrcount = x + 1; 02991 } 02992 02993 if (process_message(&ss, &m)) { 02994 if (s->authenticated) { 02995 if (option_verbose > 1) { 02996 if (displayconnects) 02997 ast_verbose(VERBOSE_PREFIX_2 "HTTP Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(s->sin.sin_addr)); 02998 } 02999 ast_log(LOG_EVENT, "HTTP Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(s->sin.sin_addr)); 03000 } else { 03001 if (option_verbose > 1) { 03002 if (displayconnects) 03003 ast_verbose(VERBOSE_PREFIX_2 "HTTP Connect attempt from '%s' unable to authenticate\n", ast_inet_ntoa(s->sin.sin_addr)); 03004 } 03005 ast_log(LOG_EVENT, "HTTP Failed attempt from %s\n", ast_inet_ntoa(s->sin.sin_addr)); 03006 } 03007 s->needdestroy = 1; 03008 } 03009 ast_build_string(&c, &len, "Content-type: text/%s\r\n", contenttype[format]); 03010 sprintf(tmp, "%08x", s->managerid); 03011 ast_build_string(&c, &len, "%s\r\n", ast_http_setcookie("mansession_id", tmp, httptimeout, cookie, sizeof(cookie))); 03012 if (format == FORMAT_HTML) 03013 ast_build_string(&c, &len, "<title>Asterisk™ Manager Interface</title>"); 03014 if (format == FORMAT_XML) { 03015 ast_build_string(&c, &len, "<ajax-response>\n"); 03016 } else if (format == FORMAT_HTML) { 03017 ast_build_string(&c, &len, "<body bgcolor=\"#ffffff\"><table align=center bgcolor=\"#f1f1f1\" width=\"500\">\r\n"); 03018 ast_build_string(&c, &len, "<tr><td colspan=\"2\" bgcolor=\"#f1f1ff\"><h1> Manager Tester</h1></td></tr>\r\n"); 03019 } 03020 ast_mutex_lock(&s->__lock); 03021 if (ss.fd > -1) { 03022 char *buf; 03023 size_t l = lseek(ss.fd, 0, SEEK_END); 03024 if (l) { 03025 if (MAP_FAILED == (buf = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, ss.fd, 0))) { 03026 ast_log(LOG_WARNING, "mmap failed. Manager request output was not processed\n"); 03027 } else { 03028 char *tmpbuf; 03029 if (format == FORMAT_XML) 03030 tmpbuf = xml_translate(buf, params); 03031 else if (format == FORMAT_HTML) 03032 tmpbuf = html_translate(buf); 03033 else 03034 tmpbuf = buf; 03035 if (tmpbuf) { 03036 size_t wlen, tlen; 03037 if ((retval = malloc((wlen = strlen(workspace)) + (tlen = strlen(tmpbuf)) + 128))) { 03038 strcpy(retval, workspace); 03039 strcpy(retval + wlen, tmpbuf); 03040 c = retval + wlen + tlen; 03041 /* Leftover space for footer, if any */ 03042 len = 120; 03043 } 03044 } 03045 if (tmpbuf != buf) 03046 free(tmpbuf); 03047 free(s->outputstr); 03048 s->outputstr = NULL; 03049 munmap(buf, l); 03050 } 03051 } 03052 fclose(ss.f); 03053 ss.f = NULL; 03054 ss.fd = -1; 03055 } else if (s->outputstr) { 03056 char *tmp; 03057 if (format == FORMAT_XML) 03058 tmp = xml_translate(s->outputstr->str, params); 03059 else if (format == FORMAT_HTML) 03060 tmp = html_translate(s->outputstr->str); 03061 else 03062 tmp = s->outputstr->str; 03063 if (tmp) { 03064 retval = malloc(strlen(workspace) + strlen(tmp) + 128); 03065 if (retval) { 03066 strcpy(retval, workspace); 03067 strcpy(retval + strlen(retval), tmp); 03068 c = retval + strlen(retval); 03069 len = 120; 03070 } 03071 } 03072 if (tmp != s->outputstr->str) 03073 free(tmp); 03074 free(s->outputstr); 03075 s->outputstr = NULL; 03076 } 03077 ast_mutex_unlock(&s->__lock); 03078 /* Still okay because c would safely be pointing to workspace even 03079 if retval failed to allocate above */ 03080 if (format == FORMAT_XML) { 03081 ast_build_string(&c, &len, "</ajax-response>\n"); 03082 } else if (format == FORMAT_HTML) 03083 ast_build_string(&c, &len, "</table></body>\r\n"); 03084 } else { 03085 *status = 500; 03086 *title = strdup("Server Error"); 03087 } 03088 ast_mutex_lock(&s->__lock); 03089 if (s->needdestroy) { 03090 if (s->inuse == 1) { 03091 ast_log(LOG_DEBUG, "Need destroy, doing it now!\n"); 03092 blastaway = 1; 03093 } else { 03094 ast_log(LOG_DEBUG, "Need destroy, but can't do it yet!\n"); 03095 if (s->waiting_thread != AST_PTHREADT_NULL) 03096 pthread_kill(s->waiting_thread, SIGURG); 03097 s->inuse--; 03098 } 03099 } else 03100 s->inuse--; 03101 ast_mutex_unlock(&s->__lock); 03102 03103 if (blastaway) 03104 destroy_session(s); 03105 generic_callback_out: 03106 if (*status != 200) 03107 return ast_http_error(500, "Server Error", NULL, "Internal Server Error (out of memory)\n"); 03108 return retval; 03109 }
int init_manager | ( | void | ) |
Called by Asterisk initialization
Definition at line 3150 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_copy_string(), 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(), mandescr_atxfer, mandescr_command, mandescr_events, mandescr_extensionstate, mandescr_getconfig, mandescr_getvar, mandescr_hangup, mandescr_listcommands, mandescr_logoff, mandescr_mailboxcount, mandescr_mailboxstatus, mandescr_originate, mandescr_ping, mandescr_redirect, mandescr_setvar, mandescr_timeout, mandescr_updateconfig, mandescr_userevent, mandescr_waitevent, ast_manager_user::permit, portno, ast_manager_user::read, ast_manager_user::secret, timestampevents, var, webmanager_enabled, and ast_manager_user::write.
Referenced by main(), and reload_manager().
03151 { 03152 struct ast_config *cfg = NULL, *ucfg = NULL; 03153 const char *val; 03154 char *cat = NULL; 03155 int oldportno = portno; 03156 static struct sockaddr_in ba; 03157 int x = 1; 03158 int flags; 03159 int newhttptimeout = 60; 03160 struct ast_manager_user *user = NULL; 03161 03162 if (!registered) { 03163 /* Register default actions */ 03164 ast_manager_register2("Ping", 0, action_ping, "Keepalive command", mandescr_ping); 03165 ast_manager_register2("Events", 0, action_events, "Control Event Flow", mandescr_events); 03166 ast_manager_register2("Logoff", 0, action_logoff, "Logoff Manager", mandescr_logoff); 03167 ast_manager_register2("Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel", mandescr_hangup); 03168 ast_manager_register("Status", EVENT_FLAG_CALL, action_status, "Lists channel status" ); 03169 ast_manager_register2("Setvar", EVENT_FLAG_CALL, action_setvar, "Set Channel Variable", mandescr_setvar ); 03170 ast_manager_register2("Getvar", EVENT_FLAG_CALL, action_getvar, "Gets a Channel Variable", mandescr_getvar ); 03171 ast_manager_register2("GetConfig", EVENT_FLAG_CONFIG, action_getconfig, "Retrieve configuration", mandescr_getconfig); 03172 ast_manager_register2("UpdateConfig", EVENT_FLAG_CONFIG, action_updateconfig, "Update basic configuration", mandescr_updateconfig); 03173 ast_manager_register2("Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect (transfer) a call", mandescr_redirect ); 03174 ast_manager_register2("Atxfer", EVENT_FLAG_CALL, action_atxfer, "Attended transfer", mandescr_atxfer ); 03175 ast_manager_register2("Originate", EVENT_FLAG_CALL, action_originate, "Originate Call", mandescr_originate); 03176 ast_manager_register2("Command", EVENT_FLAG_COMMAND, action_command, "Execute Asterisk CLI Command", mandescr_command ); 03177 ast_manager_register2("ExtensionState", EVENT_FLAG_CALL, action_extensionstate, "Check Extension Status", mandescr_extensionstate ); 03178 ast_manager_register2("AbsoluteTimeout", EVENT_FLAG_CALL, action_timeout, "Set Absolute Timeout", mandescr_timeout ); 03179 ast_manager_register2("MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox", mandescr_mailboxstatus ); 03180 ast_manager_register2("MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count", mandescr_mailboxcount ); 03181 ast_manager_register2("ListCommands", 0, action_listcommands, "List available manager commands", mandescr_listcommands); 03182 ast_manager_register2("UserEvent", EVENT_FLAG_USER, action_userevent, "Send an arbitrary event", mandescr_userevent); 03183 ast_manager_register2("CoreSettings", EVENT_FLAG_SYSTEM, action_coresettings, "Show PBX core settings (version etc)", mandescr_coresettings); 03184 ast_manager_register2("CoreStatus", EVENT_FLAG_SYSTEM, action_corestatus, "Show PBX core status variables", mandescr_corestatus); 03185 ast_manager_register2("WaitEvent", 0, action_waitevent, "Wait for an event to occur", mandescr_waitevent); 03186 03187 ast_cli_register_multiple(cli_manager, sizeof(cli_manager) / sizeof(struct ast_cli_entry)); 03188 ast_extension_state_add(NULL, NULL, manager_state_cb, NULL); 03189 registered = 1; 03190 /* Append placeholder event so master_eventq never runs dry */ 03191 append_event("Event: Placeholder\r\n\r\n", 0); 03192 } 03193 portno = DEFAULT_MANAGER_PORT; 03194 displayconnects = 1; 03195 cfg = ast_config_load("manager.conf"); 03196 if (!cfg) { 03197 ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf. Call management disabled.\n"); 03198 return 0; 03199 } 03200 if ((val = ast_variable_retrieve(cfg, "general", "enabled"))) { 03201 manager_enabled = ast_true(val); 03202 } 03203 if ((val = ast_variable_retrieve(cfg, "general", "block-sockets"))) { 03204 block_sockets = ast_true(val); 03205 } 03206 if((val = ast_variable_retrieve(cfg, "general", "webenabled"))) { 03207 webmanager_enabled = ast_true(val); 03208 } 03209 if ((val = ast_variable_retrieve(cfg, "general", "port"))) { 03210 if (sscanf(val, "%d", &portno) != 1) { 03211 ast_log(LOG_WARNING, "Invalid port number '%s'\n", val); 03212 portno = DEFAULT_MANAGER_PORT; 03213 } 03214 } 03215 if ((val = ast_variable_retrieve(cfg, "general", "displayconnects"))) { 03216 displayconnects = ast_true(val); 03217 } 03218 if ((val = ast_variable_retrieve(cfg, "general", "timestampevents"))) { 03219 timestampevents = ast_true(val); 03220 } 03221 if ((val = ast_variable_retrieve(cfg, "general", "httptimeout"))) { 03222 newhttptimeout = atoi(val); 03223 } 03224 03225 memset(&ba, 0, sizeof(ba)); 03226 ba.sin_family = AF_INET; 03227 ba.sin_port = htons(portno); 03228 03229 if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) { 03230 if (!inet_aton(val, &ba.sin_addr)) { 03231 ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val); 03232 memset(&ba.sin_addr, 0, sizeof(ba.sin_addr)); 03233 } 03234 } 03235 03236 03237 if ((asock > -1) && ((portno != oldportno) || !manager_enabled)) { 03238 #if 0 03239 /* Can't be done yet */ 03240 close(asock); 03241 asock = -1; 03242 #else 03243 ast_log(LOG_WARNING, "Unable to change management port / enabled\n"); 03244 #endif 03245 } 03246 03247 AST_LIST_LOCK(&users); 03248 03249 if ((ucfg = ast_config_load("users.conf"))) { 03250 while ((cat = ast_category_browse(ucfg, cat))) { 03251 int hasmanager = 0; 03252 struct ast_variable *var = NULL; 03253 03254 if (!strcasecmp(cat, "general")) { 03255 continue; 03256 } 03257 03258 if (!(hasmanager = ast_true(ast_variable_retrieve(ucfg, cat, "hasmanager")))) { 03259 continue; 03260 } 03261 03262 /* Look for an existing entry, if none found - create one and add it to the list */ 03263 if (!(user = ast_get_manager_by_name_locked(cat))) { 03264 if (!(user = ast_calloc(1, sizeof(*user)))) { 03265 break; 03266 } 03267 /* Copy name over */ 03268 ast_copy_string(user->username, cat, sizeof(user->username)); 03269 /* Insert into list */ 03270 AST_LIST_INSERT_TAIL(&users, user, list); 03271 } 03272 03273 /* Make sure we keep this user and don't destroy it during cleanup */ 03274 user->keep = 1; 03275 03276 for (var = ast_variable_browse(ucfg, cat); var; var = var->next) { 03277 if (!strcasecmp(var->name, "secret")) { 03278 if (user->secret) { 03279 free(user->secret); 03280 } 03281 user->secret = ast_strdup(var->value); 03282 } else if (!strcasecmp(var->name, "deny") ) { 03283 if (user->deny) { 03284 free(user->deny); 03285 } 03286 user->deny = ast_strdup(var->value); 03287 } else if (!strcasecmp(var->name, "permit") ) { 03288 if (user->permit) { 03289 free(user->permit); 03290 } 03291 user->permit = ast_strdup(var->value); 03292 } else if (!strcasecmp(var->name, "read") ) { 03293 if (user->read) { 03294 free(user->read); 03295 } 03296 user->read = ast_strdup(var->value); 03297 } else if (!strcasecmp(var->name, "write") ) { 03298 if (user->write) { 03299 free(user->write); 03300 } 03301 user->write = ast_strdup(var->value); 03302 } else if (!strcasecmp(var->name, "displayconnects") ) { 03303 user->displayconnects = ast_true(var->value); 03304 } else if (!strcasecmp(var->name, "hasmanager")) { 03305 /* already handled */ 03306 } else { 03307 ast_log(LOG_DEBUG, "%s is an unknown option (to the manager module).\n", var->name); 03308 } 03309 } 03310 } 03311 ast_config_destroy(ucfg); 03312 } 03313 03314 while ((cat = ast_category_browse(cfg, cat))) { 03315 struct ast_variable *var = NULL; 03316 03317 if (!strcasecmp(cat, "general")) 03318 continue; 03319 03320 /* Look for an existing entry, if none found - create one and add it to the list */ 03321 if (!(user = ast_get_manager_by_name_locked(cat))) { 03322 if (!(user = ast_calloc(1, sizeof(*user)))) 03323 break; 03324 /* Copy name over */ 03325 ast_copy_string(user->username, cat, sizeof(user->username)); 03326 /* Insert into list */ 03327 AST_LIST_INSERT_TAIL(&users, user, list); 03328 } 03329 03330 /* Make sure we keep this user and don't destroy it during cleanup */ 03331 user->keep = 1; 03332 03333 var = ast_variable_browse(cfg, cat); 03334 while (var) { 03335 if (!strcasecmp(var->name, "secret")) { 03336 if (user->secret) 03337 free(user->secret); 03338 user->secret = ast_strdup(var->value); 03339 } else if (!strcasecmp(var->name, "deny") ) { 03340 if (user->deny) 03341 free(user->deny); 03342 user->deny = ast_strdup(var->value); 03343 } else if (!strcasecmp(var->name, "permit") ) { 03344 if (user->permit) 03345 free(user->permit); 03346 user->permit = ast_strdup(var->value); 03347 } else if (!strcasecmp(var->name, "read") ) { 03348 if (user->read) 03349 free(user->read); 03350 user->read = ast_strdup(var->value); 03351 } else if (!strcasecmp(var->name, "write") ) { 03352 if (user->write) 03353 free(user->write); 03354 user->write = ast_strdup(var->value); 03355 } else if (!strcasecmp(var->name, "displayconnects") ) 03356 user->displayconnects = ast_true(var->value); 03357 else 03358 ast_log(LOG_DEBUG, "%s is an unknown option.\n", var->name); 03359 var = var->next; 03360 } 03361 } 03362 03363 /* Perform cleanup - essentially prune out old users that no longer exist */ 03364 AST_LIST_TRAVERSE_SAFE_BEGIN(&users, user, list) { 03365 if (user->keep) { 03366 user->keep = 0; 03367 continue; 03368 } 03369 /* We do not need to keep this user so take them out of the list */ 03370 AST_LIST_REMOVE_CURRENT(&users, list); 03371 /* Free their memory now */ 03372 if (user->secret) 03373 free(user->secret); 03374 if (user->deny) 03375 free(user->deny); 03376 if (user->permit) 03377 free(user->permit); 03378 if (user->read) 03379 free(user->read); 03380 if (user->write) 03381 free(user->write); 03382 free(user); 03383 } 03384 AST_LIST_TRAVERSE_SAFE_END 03385 03386 AST_LIST_UNLOCK(&users); 03387 03388 ast_config_destroy(cfg); 03389 03390 if (webmanager_enabled && manager_enabled) { 03391 if (!webregged) { 03392 ast_http_uri_link(&rawmanuri); 03393 ast_http_uri_link(&manageruri); 03394 ast_http_uri_link(&managerxmluri); 03395 webregged = 1; 03396 } 03397 } else { 03398 if (webregged) { 03399 ast_http_uri_unlink(&rawmanuri); 03400 ast_http_uri_unlink(&manageruri); 03401 ast_http_uri_unlink(&managerxmluri); 03402 webregged = 0; 03403 } 03404 } 03405 03406 if (newhttptimeout > 0) 03407 httptimeout = newhttptimeout; 03408 03409 /* If not enabled, do nothing */ 03410 if (!manager_enabled) 03411 return 0; 03412 03413 if (asock < 0) { 03414 asock = socket(AF_INET, SOCK_STREAM, 0); 03415 if (asock < 0) { 03416 ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno)); 03417 return -1; 03418 } 03419 setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x)); 03420 if (bind(asock, (struct sockaddr *)&ba, sizeof(ba))) { 03421 ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno)); 03422 close(asock); 03423 asock = -1; 03424 return -1; 03425 } 03426 if (listen(asock, 2)) { 03427 ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno)); 03428 close(asock); 03429 asock = -1; 03430 return -1; 03431 } 03432 flags = fcntl(asock, F_GETFL); 03433 fcntl(asock, F_SETFL, flags | O_NONBLOCK); 03434 if (option_verbose) 03435 ast_verbose("Asterisk Management interface listening on port %d\n", portno); 03436 ast_pthread_create_background(&t, NULL, accept_thread, NULL); 03437 } 03438 return 0; 03439 }
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 3111 of file manager.c.
References FORMAT_HTML, and generic_http_callback().
03112 { 03113 return generic_http_callback(FORMAT_HTML, requestor, uri, params, status, title, contentlength); 03114 }
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 3116 of file manager.c.
References FORMAT_XML, and generic_http_callback().
03117 { 03118 return generic_http_callback(FORMAT_XML, requestor, uri, params, status, title, contentlength); 03119 }
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 3121 of file manager.c.
References FORMAT_RAW, and generic_http_callback().
03122 { 03123 return generic_http_callback(FORMAT_RAW, requestor, uri, params, status, title, contentlength); 03124 }
int reload_manager | ( | void | ) |
Definition at line 3441 of file manager.c.
References EVENT_FLAG_SYSTEM, init_manager(), and manager_event().
03442 { 03443 manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n"); 03444 return init_manager(); 03445 }
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] |