Fri Feb 19 17:13:11 2010

Asterisk developer's documentation


manager.c File Reference

The Asterisk Management Interface - AMI. More...

#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_userast_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_variableastman_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_sessionfind_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_actionfirst_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.
eventqentmaster_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


Detailed Description

The Asterisk Management Interface - AMI.

Author:
Mark Spencer <markster@digium.com>
Channel Management and more

manager.conf

Definition in file manager.c.


Enumeration Type Documentation

anonymous enum

Enumerator:
FORMAT_RAW 
FORMAT_HTML 
FORMAT_XML 

Definition at line 2914 of file manager.c.

02914      {
02915    FORMAT_RAW,
02916    FORMAT_HTML,
02917    FORMAT_XML,
02918 };


Function Documentation

int astman_verify_session_readpermissions ( uint32_t  ident,
int  perm 
)

Verify a session's read permissions against a permission mask.

Parameters:
ident session identity
perm permission mask to verify
Returns:
1 if the session has the permission mask capabilities, otherwise 0

Definition at line 2876 of file manager.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), and s.

02877 {
02878    int result = 0;
02879    struct mansession_session *s;
02880 
02881    AST_LIST_LOCK(&sessions);
02882    AST_LIST_TRAVERSE(&sessions, s, list) {
02883       ast_mutex_lock(&s->__lock);
02884       if ((s->managerid == ident) && (s->readperm & perm)) {
02885          result = 1;
02886          ast_mutex_unlock(&s->__lock);
02887          break;
02888       }
02889       ast_mutex_unlock(&s->__lock);
02890    }
02891    AST_LIST_UNLOCK(&sessions);
02892    return result;
02893 }

int astman_verify_session_writepermissions ( uint32_t  ident,
int  perm 
)

Verify a session's write permissions against a permission mask.

Parameters:
ident session identity
perm permission mask to verify
Returns:
1 if the session has the permission mask capabilities, otherwise 0

Definition at line 2895 of file manager.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), and s.

02896 {
02897    int result = 0;
02898    struct mansession_session *s;
02899 
02900    AST_LIST_LOCK(&sessions);
02901    AST_LIST_TRAVERSE(&sessions, s, list) {
02902       ast_mutex_lock(&s->__lock);
02903       if ((s->managerid == ident) && (s->writeperm & perm)) {
02904          result = 1;
02905          ast_mutex_unlock(&s->__lock);
02906          break;
02907       }
02908       ast_mutex_unlock(&s->__lock);
02909    }
02910    AST_LIST_UNLOCK(&sessions);
02911    return result;
02912 }

static struct mansession_session* find_session ( uint32_t  ident  )  [static]

END Doxygen group

Definition at line 2858 of file manager.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), and s.

02859 {
02860    struct mansession_session *s;
02861 
02862    AST_LIST_LOCK(&sessions);
02863    AST_LIST_TRAVERSE(&sessions, s, list) {
02864       ast_mutex_lock(&s->__lock);
02865       if (s->sessiontimeout && (s->managerid == ident) && !s->needdestroy) {
02866          s->inuse++;
02867          break;
02868       }
02869       ast_mutex_unlock(&s->__lock);
02870    }
02871    AST_LIST_UNLOCK(&sessions);
02872 
02873    return s;
02874 }

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 2921 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().

02922 {
02923    struct mansession_session *s = NULL;
02924    struct mansession ss = { .session = NULL, };
02925    uint32_t ident = 0;
02926    char workspace[512];
02927    char cookie[128];
02928    size_t len = sizeof(workspace);
02929    int blastaway = 0;
02930    char *c = workspace;
02931    char *retval = NULL;
02932    struct ast_variable *v;
02933 
02934    for (v = params; v; v = v->next) {
02935       if (!strcasecmp(v->name, "mansession_id")) {
02936          sscanf(v->value, "%30x", &ident);
02937          break;
02938       }
02939    }
02940    
02941    if (!(s = find_session(ident))) {
02942       /* Create new session */
02943       if (!(s = ast_calloc(1, sizeof(*s)))) {
02944          *status = 500;
02945          goto generic_callback_out;
02946       }
02947       memcpy(&s->sin, requestor, sizeof(s->sin));
02948       s->fd = -1;
02949       s->waiting_thread = AST_PTHREADT_NULL;
02950       s->send_events = 0;
02951       ast_mutex_init(&s->__lock);
02952       ast_mutex_lock(&s->__lock);
02953       s->inuse = 1;
02954       /*!\note There is approximately a 1 in 1.8E19 chance that the following
02955        * calculation will produce 0, which is an invalid ID, but due to the
02956        * properties of the rand() function (and the constantcy of s), that
02957        * won't happen twice in a row.
02958        */
02959       while ((s->managerid = rand() ^ (unsigned long) s) == 0);
02960       AST_LIST_LOCK(&sessions);
02961       AST_LIST_INSERT_HEAD(&sessions, s, list);
02962       /* Hook into the last spot in the event queue */
02963       s->eventq = master_eventq;
02964       while (s->eventq->next)
02965          s->eventq = s->eventq->next;
02966       ast_atomic_fetchadd_int(&s->eventq->usecount, 1);
02967       ast_atomic_fetchadd_int(&num_sessions, 1);
02968       AST_LIST_UNLOCK(&sessions);
02969    }
02970 
02971    /* Reset HTTP timeout.  If we're not yet authenticated, keep it extremely short */
02972    time(&s->sessiontimeout);
02973    if (!s->authenticated && (httptimeout > 5))
02974       s->sessiontimeout += 5;
02975    else
02976       s->sessiontimeout += httptimeout;
02977    ss.session = s;
02978    ast_mutex_unlock(&s->__lock);
02979 
02980    ss.f = tmpfile();
02981    ss.fd = fileno(ss.f);
02982 
02983    if (s) {
02984       struct message m = { 0 };
02985       char tmp[80];
02986       unsigned int x;
02987       size_t hdrlen;
02988 
02989       for (x = 0, v = params; v && (x < AST_MAX_MANHEADERS); x++, v = v->next) {
02990          hdrlen = strlen(v->name) + strlen(v->value) + 3;
02991          m.headers[m.hdrcount] = alloca(hdrlen);
02992          snprintf((char *) m.headers[m.hdrcount], hdrlen, "%s: %s", v->name, v->value);
02993          m.hdrcount = x + 1;
02994       }
02995 
02996       if (process_message(&ss, &m)) {
02997          if (s->authenticated) {
02998             if (option_verbose > 1) {
02999                if (displayconnects) 
03000                   ast_verbose(VERBOSE_PREFIX_2 "HTTP Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(s->sin.sin_addr));    
03001             }
03002             ast_log(LOG_EVENT, "HTTP Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(s->sin.sin_addr));
03003          } else {
03004             if (option_verbose > 1) {
03005                if (displayconnects)
03006                   ast_verbose(VERBOSE_PREFIX_2 "HTTP Connect attempt from '%s' unable to authenticate\n", ast_inet_ntoa(s->sin.sin_addr));
03007             }
03008             ast_log(LOG_EVENT, "HTTP Failed attempt from %s\n", ast_inet_ntoa(s->sin.sin_addr));
03009          }
03010          s->needdestroy = 1;
03011       }
03012       ast_build_string(&c, &len, "Content-type: text/%s\r\n", contenttype[format]);
03013       sprintf(tmp, "%08x", s->managerid);
03014       ast_build_string(&c, &len, "%s\r\n", ast_http_setcookie("mansession_id", tmp, httptimeout, cookie, sizeof(cookie)));
03015       if (format == FORMAT_HTML)
03016          ast_build_string(&c, &len, "<title>Asterisk&trade; Manager Interface</title>");
03017       if (format == FORMAT_XML) {
03018          ast_build_string(&c, &len, "<ajax-response>\n");
03019       } else if (format == FORMAT_HTML) {
03020          ast_build_string(&c, &len, "<body bgcolor=\"#ffffff\"><table align=center bgcolor=\"#f1f1f1\" width=\"500\">\r\n");
03021          ast_build_string(&c, &len, "<tr><td colspan=\"2\" bgcolor=\"#f1f1ff\"><h1>&nbsp;&nbsp;Manager Tester</h1></td></tr>\r\n");
03022       }
03023       ast_mutex_lock(&s->__lock);
03024       if (ss.fd > -1) {
03025          char *buf;
03026          size_t l;
03027 
03028          /* Ensure buffer is NULL-terminated */
03029          fprintf(ss.f, "%c", 0);
03030 
03031          if ((l = lseek(ss.fd, 0, SEEK_END)) > 0) {
03032             if (MAP_FAILED == (buf = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, ss.fd, 0))) {
03033                ast_log(LOG_WARNING, "mmap failed.  Manager request output was not processed\n");
03034             } else {
03035                char *tmpbuf;
03036                if (format == FORMAT_XML)
03037                   tmpbuf = xml_translate(buf, params);
03038                else if (format == FORMAT_HTML)
03039                   tmpbuf = html_translate(buf);
03040                else
03041                   tmpbuf = buf;
03042                if (tmpbuf) {
03043                   size_t wlen, tlen;
03044                   if ((retval = malloc((wlen = strlen(workspace)) + (tlen = strlen(tmpbuf)) + 128))) {
03045                      strcpy(retval, workspace);
03046                      strcpy(retval + wlen, tmpbuf);
03047                      c = retval + wlen + tlen;
03048                      /* Leftover space for footer, if any */
03049                      len = 120;
03050                   }
03051                }
03052                if (tmpbuf != buf)
03053                   free(tmpbuf);
03054                free(s->outputstr);
03055                s->outputstr = NULL;
03056                munmap(buf, l);
03057             }
03058          }
03059          fclose(ss.f);
03060          ss.f = NULL;
03061          ss.fd = -1;
03062       } else if (s->outputstr) {
03063          char *tmp;
03064          if (format == FORMAT_XML)
03065             tmp = xml_translate(s->outputstr->str, params);
03066          else if (format == FORMAT_HTML)
03067             tmp = html_translate(s->outputstr->str);
03068          else
03069             tmp = s->outputstr->str;
03070          if (tmp) {
03071             retval = malloc(strlen(workspace) + strlen(tmp) + 128);
03072             if (retval) {
03073                strcpy(retval, workspace);
03074                strcpy(retval + strlen(retval), tmp);
03075                c = retval + strlen(retval);
03076                len = 120;
03077             }
03078          }
03079          if (tmp != s->outputstr->str)
03080             free(tmp);
03081          free(s->outputstr);
03082          s->outputstr = NULL;
03083       }
03084       ast_mutex_unlock(&s->__lock);
03085       /* Still okay because c would safely be pointing to workspace even
03086          if retval failed to allocate above */
03087       if (format == FORMAT_XML) {
03088          ast_build_string(&c, &len, "</ajax-response>\n");
03089       } else if (format == FORMAT_HTML)
03090          ast_build_string(&c, &len, "</table></body>\r\n");
03091    } else {
03092       *status = 500;
03093       *title = strdup("Server Error");
03094    }
03095    ast_mutex_lock(&s->__lock);
03096    if (s->needdestroy) {
03097       if (s->inuse == 1) {
03098          ast_log(LOG_DEBUG, "Need destroy, doing it now!\n");
03099          blastaway = 1;
03100       } else {
03101          ast_log(LOG_DEBUG, "Need destroy, but can't do it yet!\n");
03102          if (s->waiting_thread != AST_PTHREADT_NULL)
03103             pthread_kill(s->waiting_thread, SIGURG);
03104          s->inuse--;
03105       }
03106    } else
03107       s->inuse--;
03108    ast_mutex_unlock(&s->__lock);
03109    
03110    if (blastaway)
03111       destroy_session(s);
03112 generic_callback_out:
03113    if (*status != 200)
03114       return ast_http_error(500, "Server Error", NULL, "Internal Server Error (out of memory)\n"); 
03115    return retval;
03116 }

int init_manager ( void   ) 

Called by Asterisk initialization

Definition at line 3157 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().

03158 {
03159    struct ast_config *cfg = NULL, *ucfg = NULL;
03160    const char *val;
03161    char *cat = NULL;
03162    int oldportno = portno;
03163    static struct sockaddr_in ba;
03164    int x = 1;
03165    int flags;
03166    int newhttptimeout = 60;
03167    struct ast_manager_user *user = NULL;
03168 
03169    if (!registered) {
03170       /* Register default actions */
03171       ast_manager_register2("Ping", 0, action_ping, "Keepalive command", mandescr_ping);
03172       ast_manager_register2("Events", 0, action_events, "Control Event Flow", mandescr_events);
03173       ast_manager_register2("Logoff", 0, action_logoff, "Logoff Manager", mandescr_logoff);
03174       ast_manager_register2("Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel", mandescr_hangup);
03175       ast_manager_register("Status", EVENT_FLAG_CALL, action_status, "Lists channel status" );
03176       ast_manager_register2("Setvar", EVENT_FLAG_CALL, action_setvar, "Set Channel Variable", mandescr_setvar );
03177       ast_manager_register2("Getvar", EVENT_FLAG_CALL, action_getvar, "Gets a Channel Variable", mandescr_getvar );
03178       ast_manager_register2("GetConfig", EVENT_FLAG_CONFIG, action_getconfig, "Retrieve configuration", mandescr_getconfig);
03179       ast_manager_register2("UpdateConfig", EVENT_FLAG_CONFIG, action_updateconfig, "Update basic configuration", mandescr_updateconfig);
03180       ast_manager_register2("Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect (transfer) a call", mandescr_redirect );
03181       ast_manager_register2("Atxfer", EVENT_FLAG_CALL, action_atxfer, "Attended transfer", mandescr_atxfer );
03182       ast_manager_register2("Originate", EVENT_FLAG_CALL, action_originate, "Originate Call", mandescr_originate);
03183       ast_manager_register2("Command", EVENT_FLAG_COMMAND, action_command, "Execute Asterisk CLI Command", mandescr_command );
03184       ast_manager_register2("ExtensionState", EVENT_FLAG_CALL, action_extensionstate, "Check Extension Status", mandescr_extensionstate );
03185       ast_manager_register2("AbsoluteTimeout", EVENT_FLAG_CALL, action_timeout, "Set Absolute Timeout", mandescr_timeout );
03186       ast_manager_register2("MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox", mandescr_mailboxstatus );
03187       ast_manager_register2("MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count", mandescr_mailboxcount );
03188       ast_manager_register2("ListCommands", 0, action_listcommands, "List available manager commands", mandescr_listcommands);
03189       ast_manager_register2("UserEvent", EVENT_FLAG_USER, action_userevent, "Send an arbitrary event", mandescr_userevent);
03190       ast_manager_register2("CoreSettings", EVENT_FLAG_SYSTEM, action_coresettings, "Show PBX core settings (version etc)", mandescr_coresettings);
03191       ast_manager_register2("CoreStatus", EVENT_FLAG_SYSTEM, action_corestatus, "Show PBX core status variables", mandescr_corestatus);
03192       ast_manager_register2("WaitEvent", 0, action_waitevent, "Wait for an event to occur", mandescr_waitevent);
03193 
03194       ast_cli_register_multiple(cli_manager, sizeof(cli_manager) / sizeof(struct ast_cli_entry));
03195       ast_extension_state_add(NULL, NULL, manager_state_cb, NULL);
03196       registered = 1;
03197       /* Append placeholder event so master_eventq never runs dry */
03198       append_event("Event: Placeholder\r\n\r\n", 0);
03199    }
03200    portno = DEFAULT_MANAGER_PORT;
03201    displayconnects = 1;
03202    cfg = ast_config_load("manager.conf");
03203    if (!cfg) {
03204       ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf.  Call management disabled.\n");
03205       return 0;
03206    }
03207    if ((val = ast_variable_retrieve(cfg, "general", "enabled"))) {
03208       manager_enabled = ast_true(val);
03209    }
03210    if ((val = ast_variable_retrieve(cfg, "general", "block-sockets"))) {
03211       block_sockets = ast_true(val);
03212    }
03213    if((val = ast_variable_retrieve(cfg, "general", "webenabled"))) {
03214       webmanager_enabled = ast_true(val);
03215    }
03216    if ((val = ast_variable_retrieve(cfg, "general", "port"))) {
03217       if (sscanf(val, "%5d", &portno) != 1) {
03218          ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
03219          portno = DEFAULT_MANAGER_PORT;
03220       }
03221    }
03222 
03223    if ((val = ast_variable_retrieve(cfg, "general", "displayconnects"))) {
03224       displayconnects = ast_true(val);
03225    }
03226    if ((val = ast_variable_retrieve(cfg, "general", "timestampevents"))) {
03227       timestampevents = ast_true(val);
03228    }
03229    if ((val = ast_variable_retrieve(cfg, "general", "httptimeout"))) {
03230       newhttptimeout = atoi(val);
03231    }
03232 
03233    memset(&ba, 0, sizeof(ba));
03234    ba.sin_family = AF_INET;
03235    ba.sin_port = htons(portno);
03236 
03237    if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) {
03238       if (!inet_aton(val, &ba.sin_addr)) { 
03239          ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val);
03240          memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
03241       }
03242    }
03243    
03244 
03245    if ((asock > -1) && ((portno != oldportno) || !manager_enabled)) {
03246 #if 0
03247       /* Can't be done yet */
03248       close(asock);
03249       asock = -1;
03250 #else
03251       ast_log(LOG_WARNING, "Unable to change management port / enabled\n");
03252 #endif
03253    }
03254 
03255    AST_LIST_LOCK(&users);
03256 
03257    if ((ucfg = ast_config_load("users.conf"))) {
03258       while ((cat = ast_category_browse(ucfg, cat))) {
03259          int hasmanager = 0;
03260          struct ast_variable *var = NULL;
03261 
03262          if (!strcasecmp(cat, "general")) {
03263             continue;
03264          }
03265 
03266          if (!(hasmanager = ast_true(ast_variable_retrieve(ucfg, cat, "hasmanager")))) {
03267             continue;
03268          }
03269 
03270          /* Look for an existing entry, if none found - create one and add it to the list */
03271          if (!(user = ast_get_manager_by_name_locked(cat))) {
03272             if (!(user = ast_calloc(1, sizeof(*user)))) {
03273                break;
03274             }
03275             /* Copy name over */
03276             ast_copy_string(user->username, cat, sizeof(user->username));
03277             /* Insert into list */
03278             AST_LIST_INSERT_TAIL(&users, user, list);
03279          }
03280 
03281          /* Make sure we keep this user and don't destroy it during cleanup */
03282          user->keep = 1;
03283 
03284          for (var = ast_variable_browse(ucfg, cat); var; var = var->next) {
03285             if (!strcasecmp(var->name, "secret")) {
03286                if (user->secret) {
03287                   free(user->secret);
03288                }
03289                user->secret = ast_strdup(var->value);
03290             } else if (!strcasecmp(var->name, "deny") ) {
03291                if (user->deny) {
03292                   free(user->deny);
03293                }
03294                user->deny = ast_strdup(var->value);
03295             } else if (!strcasecmp(var->name, "permit") ) {
03296                if (user->permit) {
03297                   free(user->permit);
03298                }
03299                user->permit = ast_strdup(var->value);
03300             } else if (!strcasecmp(var->name, "read") ) {
03301                if (user->read) {
03302                   free(user->read);
03303                }
03304                user->read = ast_strdup(var->value);
03305             } else if (!strcasecmp(var->name, "write") ) {
03306                if (user->write) {
03307                   free(user->write);
03308                }
03309                user->write = ast_strdup(var->value);
03310             } else if (!strcasecmp(var->name, "displayconnects") ) {
03311                user->displayconnects = ast_true(var->value);
03312             } else if (!strcasecmp(var->name, "hasmanager")) {
03313                /* already handled */
03314             } else {
03315                ast_log(LOG_DEBUG, "%s is an unknown option (to the manager module).\n", var->name);
03316             }
03317          }
03318       }
03319       ast_config_destroy(ucfg);
03320    }
03321 
03322    while ((cat = ast_category_browse(cfg, cat))) {
03323       struct ast_variable *var = NULL;
03324 
03325       if (!strcasecmp(cat, "general"))
03326          continue;
03327 
03328       /* Look for an existing entry, if none found - create one and add it to the list */
03329       if (!(user = ast_get_manager_by_name_locked(cat))) {
03330          if (!(user = ast_calloc(1, sizeof(*user))))
03331             break;
03332          /* Copy name over */
03333          ast_copy_string(user->username, cat, sizeof(user->username));
03334          /* Insert into list */
03335          AST_LIST_INSERT_TAIL(&users, user, list);
03336       }
03337 
03338       /* Make sure we keep this user and don't destroy it during cleanup */
03339       user->keep = 1;
03340 
03341       var = ast_variable_browse(cfg, cat);
03342       while (var) {
03343          if (!strcasecmp(var->name, "secret")) {
03344             if (user->secret)
03345                free(user->secret);
03346             user->secret = ast_strdup(var->value);
03347          } else if (!strcasecmp(var->name, "deny") ) {
03348             if (user->deny)
03349                free(user->deny);
03350             user->deny = ast_strdup(var->value);
03351          } else if (!strcasecmp(var->name, "permit") ) {
03352             if (user->permit)
03353                free(user->permit);
03354             user->permit = ast_strdup(var->value);
03355          }  else if (!strcasecmp(var->name, "read") ) {
03356             if (user->read)
03357                free(user->read);
03358             user->read = ast_strdup(var->value);
03359          }  else if (!strcasecmp(var->name, "write") ) {
03360             if (user->write)
03361                free(user->write);
03362             user->write = ast_strdup(var->value);
03363          }  else if (!strcasecmp(var->name, "displayconnects") )
03364             user->displayconnects = ast_true(var->value);
03365          else
03366             ast_log(LOG_DEBUG, "%s is an unknown option.\n", var->name);
03367          var = var->next;
03368       }
03369    }
03370 
03371    /* Perform cleanup - essentially prune out old users that no longer exist */
03372    AST_LIST_TRAVERSE_SAFE_BEGIN(&users, user, list) {
03373       if (user->keep) {
03374          user->keep = 0;
03375          continue;
03376       }
03377       /* We do not need to keep this user so take them out of the list */
03378       AST_LIST_REMOVE_CURRENT(&users, list);
03379       /* Free their memory now */
03380       if (user->secret)
03381          free(user->secret);
03382       if (user->deny)
03383          free(user->deny);
03384       if (user->permit)
03385          free(user->permit);
03386       if (user->read)
03387          free(user->read);
03388       if (user->write)
03389          free(user->write);
03390       free(user);
03391    }
03392    AST_LIST_TRAVERSE_SAFE_END
03393 
03394    AST_LIST_UNLOCK(&users);
03395 
03396    ast_config_destroy(cfg);
03397    
03398    if (webmanager_enabled && manager_enabled) {
03399       if (!webregged) {
03400          ast_http_uri_link(&rawmanuri);
03401          ast_http_uri_link(&manageruri);
03402          ast_http_uri_link(&managerxmluri);
03403          webregged = 1;
03404       }
03405    } else {
03406       if (webregged) {
03407          ast_http_uri_unlink(&rawmanuri);
03408          ast_http_uri_unlink(&manageruri);
03409          ast_http_uri_unlink(&managerxmluri);
03410          webregged = 0;
03411       }
03412    }
03413 
03414    if (newhttptimeout > 0)
03415       httptimeout = newhttptimeout;
03416 
03417    /* If not enabled, do nothing */
03418    if (!manager_enabled)
03419       return 0;
03420 
03421    if (asock < 0) {
03422       asock = socket(AF_INET, SOCK_STREAM, 0);
03423       if (asock < 0) {
03424          ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
03425          return -1;
03426       }
03427       setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
03428       if (bind(asock, (struct sockaddr *)&ba, sizeof(ba))) {
03429          ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno));
03430          close(asock);
03431          asock = -1;
03432          return -1;
03433       }
03434       if (listen(asock, 2)) {
03435          ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno));
03436          close(asock);
03437          asock = -1;
03438          return -1;
03439       }
03440       flags = fcntl(asock, F_GETFL);
03441       fcntl(asock, F_SETFL, flags | O_NONBLOCK);
03442       if (option_verbose)
03443          ast_verbose("Asterisk Management interface listening on port %d\n", portno);
03444       ast_pthread_create_background(&t, NULL, accept_thread, NULL);
03445    }
03446    return 0;
03447 }

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 3118 of file manager.c.

References FORMAT_HTML, and generic_http_callback().

03119 {
03120    return generic_http_callback(FORMAT_HTML, requestor, uri, params, status, title, contentlength);
03121 }

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 3123 of file manager.c.

References FORMAT_XML, and generic_http_callback().

03124 {
03125    return generic_http_callback(FORMAT_XML, requestor, uri, params, status, title, contentlength);
03126 }

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 3128 of file manager.c.

References FORMAT_RAW, and generic_http_callback().

03129 {
03130    return generic_http_callback(FORMAT_RAW, requestor, uri, params, status, title, contentlength);
03131 }

int reload_manager ( void   ) 

Definition at line 3449 of file manager.c.

References EVENT_FLAG_SYSTEM, init_manager(), and manager_event().

03450 {
03451    manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n");
03452    return init_manager();
03453 }


Variable Documentation

char* contenttype[] = { "plain", "html", "xml" } [static]

Definition at line 2919 of file manager.c.

struct ast_http_uri manageruri

Definition at line 3140 of file manager.c.

struct ast_http_uri managerxmluri

Definition at line 3147 of file manager.c.

struct ast_http_uri rawmanuri

Definition at line 3133 of file manager.c.

int registered = 0 [static]

Definition at line 3154 of file manager.c.

int webregged = 0 [static]

Definition at line 3155 of file manager.c.

char* words[AST_MAX_CMD_LEN]

Definition at line 141 of file manager.c.

Referenced by check_blacklist().


Generated on Fri Feb 19 17:13:11 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7