Fri Jul 3 20:38:41 2009

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 "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"

Include dependency graph for manager.c:

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  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_command (struct mansession *s, const struct message *m)
 action_command: Manager command "command" - execute CLI command
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)
struct 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)
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 connected.
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 *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 int enabled
static struct manager_action * first_action
static int httptimeout = 60
static struct ast_threadstorage manager_event_buf = { .once = PTHREAD_ONCE_INIT , .key_init = manager_event_buf_init , }
struct ast_http_uri manageruri
struct ast_http_uri managerxmluri
static char mandescr_command []
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.
struct eventqent * master_eventq = NULL
static int num_sessions
static struct permalias perms []
static int portno = DEFAULT_MANAGER_PORT
struct 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 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 2698 of file manager.c.

02698      {
02699    FORMAT_RAW,
02700    FORMAT_HTML,
02701    FORMAT_XML,
02702 };


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

References mansession::__lock, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), mansession::managerid, mansession::readperm, result, and s.

02661 {
02662    int result = 0;
02663    struct mansession *s;
02664 
02665    AST_LIST_LOCK(&sessions);
02666    AST_LIST_TRAVERSE(&sessions, s, list) {
02667       ast_mutex_lock(&s->__lock);
02668       if ((s->managerid == ident) && (s->readperm & perm)) {
02669          result = 1;
02670          ast_mutex_unlock(&s->__lock);
02671          break;
02672       }
02673       ast_mutex_unlock(&s->__lock);
02674    }
02675    AST_LIST_UNLOCK(&sessions);
02676    return result;
02677 }

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

References mansession::__lock, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), mansession::managerid, result, s, and mansession::writeperm.

02680 {
02681    int result = 0;
02682    struct mansession *s;
02683 
02684    AST_LIST_LOCK(&sessions);
02685    AST_LIST_TRAVERSE(&sessions, s, list) {
02686       ast_mutex_lock(&s->__lock);
02687       if ((s->managerid == ident) && (s->writeperm & perm)) {
02688          result = 1;
02689          ast_mutex_unlock(&s->__lock);
02690          break;
02691       }
02692       ast_mutex_unlock(&s->__lock);
02693    }
02694    AST_LIST_UNLOCK(&sessions);
02695    return result;
02696 }

static struct mansession* find_session ( uint32_t  ident  )  [static, read]

END Doxygen group

Definition at line 2642 of file manager.c.

References mansession::__lock, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), mansession::inuse, mansession::managerid, mansession::needdestroy, s, and mansession::sessiontimeout.

Referenced by generic_http_callback().

02643 {
02644    struct mansession *s;
02645 
02646    AST_LIST_LOCK(&sessions);
02647    AST_LIST_TRAVERSE(&sessions, s, list) {
02648       ast_mutex_lock(&s->__lock);
02649       if (s->sessiontimeout && (s->managerid == ident) && !s->needdestroy) {
02650          s->inuse++;
02651          break;
02652       }
02653       ast_mutex_unlock(&s->__lock);
02654    }
02655    AST_LIST_UNLOCK(&sessions);
02656 
02657    return s;
02658 }

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]

Note:
There is approximately a 1 in 1.8E19 chance that the following calculation will produce 0, which is an invalid ID, but due to the properties of the rand() function (and the constantcy of s), that won't happen twice in a row.

Definition at line 2705 of file manager.c.

References mansession::__lock, ast_atomic_fetchadd_int(), ast_build_string(), ast_calloc, ast_http_error(), ast_http_setcookie(), ast_inet_ntoa(), AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), AST_MAX_MANHEADERS, ast_mutex_init(), ast_mutex_lock(), ast_mutex_unlock(), AST_PTHREADT_NULL, ast_verbose(), mansession::authenticated, destroy_session(), mansession::fd, find_session(), FORMAT_HTML, FORMAT_XML, free, message::hdrcount, message::headers, html_translate(), mansession::inuse, len(), LOG_DEBUG, LOG_EVENT, malloc, mansession::managerid, ast_variable::name, mansession::needdestroy, eventqent::next, ast_variable::next, option_verbose, mansession::outputstr, process_message(), s, mansession::send_events, mansession::sessiontimeout, mansession::sin, ast_dynamic_str::str, strdup, mansession::username, ast_variable::value, VERBOSE_PREFIX_2, mansession::waiting_thread, and xml_translate().

Referenced by manager_http_callback(), mxml_http_callback(), and rawman_http_callback().

02706 {
02707    struct mansession *s = NULL;
02708    uint32_t ident = 0;
02709    char workspace[512];
02710    char cookie[128];
02711    size_t len = sizeof(workspace);
02712    int blastaway = 0;
02713    char *c = workspace;
02714    char *retval = NULL;
02715    struct ast_variable *v;
02716 
02717    for (v = params; v; v = v->next) {
02718       if (!strcasecmp(v->name, "mansession_id")) {
02719          sscanf(v->value, "%x", &ident);
02720          break;
02721       }
02722    }
02723    
02724    if (!(s = find_session(ident))) {
02725       /* Create new session */
02726       if (!(s = ast_calloc(1, sizeof(*s)))) {
02727          *status = 500;
02728          goto generic_callback_out;
02729       }
02730       memcpy(&s->sin, requestor, sizeof(s->sin));
02731       s->fd = -1;
02732       s->waiting_thread = AST_PTHREADT_NULL;
02733       s->send_events = 0;
02734       ast_mutex_init(&s->__lock);
02735       ast_mutex_lock(&s->__lock);
02736       s->inuse = 1;
02737       /*!\note There is approximately a 1 in 1.8E19 chance that the following
02738        * calculation will produce 0, which is an invalid ID, but due to the
02739        * properties of the rand() function (and the constantcy of s), that
02740        * won't happen twice in a row.
02741        */
02742       while ((s->managerid = rand() ^ (unsigned long) s) == 0);
02743       AST_LIST_LOCK(&sessions);
02744       AST_LIST_INSERT_HEAD(&sessions, s, list);
02745       /* Hook into the last spot in the event queue */
02746       s->eventq = master_eventq;
02747       while (s->eventq->next)
02748          s->eventq = s->eventq->next;
02749       ast_atomic_fetchadd_int(&s->eventq->usecount, 1);
02750       ast_atomic_fetchadd_int(&num_sessions, 1);
02751       AST_LIST_UNLOCK(&sessions);
02752    }
02753 
02754    /* Reset HTTP timeout.  If we're not yet authenticated, keep it extremely short */
02755    time(&s->sessiontimeout);
02756    if (!s->authenticated && (httptimeout > 5))
02757       s->sessiontimeout += 5;
02758    else
02759       s->sessiontimeout += httptimeout;
02760    ast_mutex_unlock(&s->__lock);
02761    
02762    if (s) {
02763       struct message m = { 0 };
02764       char tmp[80];
02765       unsigned int x;
02766       size_t hdrlen;
02767 
02768       for (x = 0, v = params; v && (x < AST_MAX_MANHEADERS); x++, v = v->next) {
02769          hdrlen = strlen(v->name) + strlen(v->value) + 3;
02770          m.headers[m.hdrcount] = alloca(hdrlen);
02771          snprintf((char *) m.headers[m.hdrcount], hdrlen, "%s: %s", v->name, v->value);
02772          m.hdrcount = x + 1;
02773       }
02774 
02775       if (process_message(s, &m)) {
02776          if (s->authenticated) {
02777             if (option_verbose > 1) {
02778                if (displayconnects) 
02779                   ast_verbose(VERBOSE_PREFIX_2 "HTTP Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(s->sin.sin_addr));    
02780             }
02781             ast_log(LOG_EVENT, "HTTP Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(s->sin.sin_addr));
02782          } else {
02783             if (option_verbose > 1) {
02784                if (displayconnects)
02785                   ast_verbose(VERBOSE_PREFIX_2 "HTTP Connect attempt from '%s' unable to authenticate\n", ast_inet_ntoa(s->sin.sin_addr));
02786             }
02787             ast_log(LOG_EVENT, "HTTP Failed attempt from %s\n", ast_inet_ntoa(s->sin.sin_addr));
02788          }
02789          s->needdestroy = 1;
02790       }
02791       ast_build_string(&c, &len, "Content-type: text/%s\r\n", contenttype[format]);
02792       sprintf(tmp, "%08x", s->managerid);
02793       ast_build_string(&c, &len, "%s\r\n", ast_http_setcookie("mansession_id", tmp, httptimeout, cookie, sizeof(cookie)));
02794       if (format == FORMAT_HTML)
02795          ast_build_string(&c, &len, "<title>Asterisk&trade; Manager Interface</title>");
02796       if (format == FORMAT_XML) {
02797          ast_build_string(&c, &len, "<ajax-response>\n");
02798       } else if (format == FORMAT_HTML) {
02799          ast_build_string(&c, &len, "<body bgcolor=\"#ffffff\"><table align=center bgcolor=\"#f1f1f1\" width=\"500\">\r\n");
02800          ast_build_string(&c, &len, "<tr><td colspan=\"2\" bgcolor=\"#f1f1ff\"><h1>&nbsp;&nbsp;Manager Tester</h1></td></tr>\r\n");
02801       }
02802       ast_mutex_lock(&s->__lock);
02803       if (s->outputstr) {
02804          char *tmp;
02805          if (format == FORMAT_XML)
02806             tmp = xml_translate(s->outputstr->str, params);
02807          else if (format == FORMAT_HTML)
02808             tmp = html_translate(s->outputstr->str);
02809          else
02810             tmp = s->outputstr->str;
02811          if (tmp) {
02812             retval = malloc(strlen(workspace) + strlen(tmp) + 128);
02813             if (retval) {
02814                strcpy(retval, workspace);
02815                strcpy(retval + strlen(retval), tmp);
02816                c = retval + strlen(retval);
02817                len = 120;
02818             }
02819          }
02820          if (tmp != s->outputstr->str)
02821             free(tmp);
02822          free(s->outputstr);
02823          s->outputstr = NULL;
02824       }
02825       ast_mutex_unlock(&s->__lock);
02826       /* Still okay because c would safely be pointing to workspace even
02827          if retval failed to allocate above */
02828       if (format == FORMAT_XML) {
02829          ast_build_string(&c, &len, "</ajax-response>\n");
02830       } else if (format == FORMAT_HTML)
02831          ast_build_string(&c, &len, "</table></body>\r\n");
02832    } else {
02833       *status = 500;
02834       *title = strdup("Server Error");
02835    }
02836    ast_mutex_lock(&s->__lock);
02837    if (s->needdestroy) {
02838       if (s->inuse == 1) {
02839          ast_log(LOG_DEBUG, "Need destroy, doing it now!\n");
02840          blastaway = 1;
02841       } else {
02842          ast_log(LOG_DEBUG, "Need destroy, but can't do it yet!\n");
02843          if (s->waiting_thread != AST_PTHREADT_NULL)
02844             pthread_kill(s->waiting_thread, SIGURG);
02845          s->inuse--;
02846       }
02847    } else
02848       s->inuse--;
02849    ast_mutex_unlock(&s->__lock);
02850    
02851    if (blastaway)
02852       destroy_session(s);
02853 generic_callback_out:
02854    if (*status != 200)
02855       return ast_http_error(500, "Server Error", NULL, "Internal Server Error (out of memory)\n"); 
02856    return retval;
02857 }

int init_manager ( void   ) 

Called by Asterisk initialization

Definition at line 2898 of file manager.c.

References accept_thread(), action_command(), 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(), ast_calloc, ast_category_browse(), ast_cli_register_multiple(), ast_config_destroy(), ast_config_load(), ast_copy_string(), ast_extension_state_add(), ast_get_manager_by_name_locked(), ast_http_uri_link(), ast_http_uri_unlink(), AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_log(), ast_manager_register, ast_manager_register2(), ast_pthread_create_background, ast_strdup, ast_true(), ast_variable_browse(), ast_variable_retrieve(), ast_verbose(), DEFAULT_MANAGER_PORT, ast_manager_user::deny, ast_manager_user::displayconnects, errno, EVENT_FLAG_CALL, EVENT_FLAG_COMMAND, EVENT_FLAG_CONFIG, EVENT_FLAG_USER, ast_channel::flags, free, ast_manager_user::keep, LOG_DEBUG, LOG_NOTICE, LOG_WARNING, manager_state_cb(), ast_variable::name, ast_variable::next, option_verbose, ast_manager_user::permit, ast_manager_user::read, ast_manager_user::secret, ast_manager_user::username, ast_variable::value, var, and ast_manager_user::write.

Referenced by main(), and reload_manager().

02899 {
02900    struct ast_config *cfg = NULL, *ucfg = NULL;
02901    const char *val;
02902    char *cat = NULL;
02903    int oldportno = portno;
02904    static struct sockaddr_in ba;
02905    int x = 1;
02906    int flags;
02907    int webenabled = 0;
02908    int newhttptimeout = 60;
02909    struct ast_manager_user *user = NULL;
02910 
02911    if (!registered) {
02912       /* Register default actions */
02913       ast_manager_register2("Ping", 0, action_ping, "Keepalive command", mandescr_ping);
02914       ast_manager_register2("Events", 0, action_events, "Control Event Flow", mandescr_events);
02915       ast_manager_register2("Logoff", 0, action_logoff, "Logoff Manager", mandescr_logoff);
02916       ast_manager_register2("Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel", mandescr_hangup);
02917       ast_manager_register("Status", EVENT_FLAG_CALL, action_status, "Lists channel status" );
02918       ast_manager_register2("Setvar", EVENT_FLAG_CALL, action_setvar, "Set Channel Variable", mandescr_setvar );
02919       ast_manager_register2("Getvar", EVENT_FLAG_CALL, action_getvar, "Gets a Channel Variable", mandescr_getvar );
02920       ast_manager_register2("GetConfig", EVENT_FLAG_CONFIG, action_getconfig, "Retrieve configuration", mandescr_getconfig);
02921       ast_manager_register2("UpdateConfig", EVENT_FLAG_CONFIG, action_updateconfig, "Update basic configuration", mandescr_updateconfig);
02922       ast_manager_register2("Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect (transfer) a call", mandescr_redirect );
02923       ast_manager_register2("Originate", EVENT_FLAG_CALL, action_originate, "Originate Call", mandescr_originate);
02924       ast_manager_register2("Command", EVENT_FLAG_COMMAND, action_command, "Execute Asterisk CLI Command", mandescr_command );
02925       ast_manager_register2("ExtensionState", EVENT_FLAG_CALL, action_extensionstate, "Check Extension Status", mandescr_extensionstate );
02926       ast_manager_register2("AbsoluteTimeout", EVENT_FLAG_CALL, action_timeout, "Set Absolute Timeout", mandescr_timeout );
02927       ast_manager_register2("MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox", mandescr_mailboxstatus );
02928       ast_manager_register2("MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count", mandescr_mailboxcount );
02929       ast_manager_register2("ListCommands", 0, action_listcommands, "List available manager commands", mandescr_listcommands);
02930       ast_manager_register2("UserEvent", EVENT_FLAG_USER, action_userevent, "Send an arbitrary event", mandescr_userevent);
02931       ast_manager_register2("WaitEvent", 0, action_waitevent, "Wait for an event to occur", mandescr_waitevent);
02932 
02933       ast_cli_register_multiple(cli_manager, sizeof(cli_manager) / sizeof(struct ast_cli_entry));
02934       ast_extension_state_add(NULL, NULL, manager_state_cb, NULL);
02935       registered = 1;
02936       /* Append placeholder event so master_eventq never runs dry */
02937       append_event("Event: Placeholder\r\n\r\n", 0);
02938    }
02939    portno = DEFAULT_MANAGER_PORT;
02940    displayconnects = 1;
02941    cfg = ast_config_load("manager.conf");
02942    if (!cfg) {
02943       ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf.  Call management disabled.\n");
02944       return 0;
02945    }
02946    val = ast_variable_retrieve(cfg, "general", "enabled");
02947    if (val)
02948       enabled = ast_true(val);
02949 
02950    val = ast_variable_retrieve(cfg, "general", "block-sockets");
02951    if (val)
02952       block_sockets = ast_true(val);
02953 
02954    val = ast_variable_retrieve(cfg, "general", "webenabled");
02955    if (val)
02956       webenabled = ast_true(val);
02957 
02958    if ((val = ast_variable_retrieve(cfg, "general", "port"))) {
02959       if (sscanf(val, "%d", &portno) != 1) {
02960          ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
02961          portno = DEFAULT_MANAGER_PORT;
02962       }
02963    }
02964 
02965    if ((val = ast_variable_retrieve(cfg, "general", "displayconnects")))
02966       displayconnects = ast_true(val);
02967 
02968    if ((val = ast_variable_retrieve(cfg, "general", "timestampevents")))
02969       timestampevents = ast_true(val);
02970 
02971    if ((val = ast_variable_retrieve(cfg, "general", "httptimeout")))
02972       newhttptimeout = atoi(val);
02973 
02974    memset(&ba, 0, sizeof(ba));
02975    ba.sin_family = AF_INET;
02976    ba.sin_port = htons(portno);
02977 
02978    if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) {
02979       if (!inet_aton(val, &ba.sin_addr)) { 
02980          ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val);
02981          memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
02982       }
02983    }
02984    
02985 
02986    if ((asock > -1) && ((portno != oldportno) || !enabled)) {
02987 #if 0
02988       /* Can't be done yet */
02989       close(asock);
02990       asock = -1;
02991 #else
02992       ast_log(LOG_WARNING, "Unable to change management port / enabled\n");
02993 #endif
02994    }
02995 
02996    AST_LIST_LOCK(&users);
02997 
02998    if ((ucfg = ast_config_load("users.conf"))) {
02999       while ((cat = ast_category_browse(ucfg, cat))) {
03000          int hasmanager = 0;
03001          struct ast_variable *var = NULL;
03002 
03003          if (!strcasecmp(cat, "general")) {
03004             continue;
03005          }
03006 
03007          if (!(hasmanager = ast_true(ast_variable_retrieve(ucfg, cat, "hasmanager")))) {
03008             continue;
03009          }
03010 
03011          /* Look for an existing entry, if none found - create one and add it to the list */
03012          if (!(user = ast_get_manager_by_name_locked(cat))) {
03013             if (!(user = ast_calloc(1, sizeof(*user)))) {
03014                break;
03015             }
03016             /* Copy name over */
03017             ast_copy_string(user->username, cat, sizeof(user->username));
03018             /* Insert into list */
03019             AST_LIST_INSERT_TAIL(&users, user, list);
03020          }
03021 
03022          /* Make sure we keep this user and don't destroy it during cleanup */
03023          user->keep = 1;
03024 
03025          for (var = ast_variable_browse(ucfg, cat); var; var = var->next) {
03026             if (!strcasecmp(var->name, "secret")) {
03027                if (user->secret) {
03028                   free(user->secret);
03029                }
03030                user->secret = ast_strdup(var->value);
03031             } else if (!strcasecmp(var->name, "deny") ) {
03032                if (user->deny) {
03033                   free(user->deny);
03034                }
03035                user->deny = ast_strdup(var->value);
03036             } else if (!strcasecmp(var->name, "permit") ) {
03037                if (user->permit) {
03038                   free(user->permit);
03039                }
03040                user->permit = ast_strdup(var->value);
03041             } else if (!strcasecmp(var->name, "read") ) {
03042                if (user->read) {
03043                   free(user->read);
03044                }
03045                user->read = ast_strdup(var->value);
03046             } else if (!strcasecmp(var->name, "write") ) {
03047                if (user->write) {
03048                   free(user->write);
03049                }
03050                user->write = ast_strdup(var->value);
03051             } else if (!strcasecmp(var->name, "displayconnects") ) {
03052                user->displayconnects = ast_true(var->value);
03053             } else if (!strcasecmp(var->name, "hasmanager")) {
03054                /* already handled */
03055             } else {
03056                ast_log(LOG_DEBUG, "%s is an unknown option (to the manager module).\n", var->name);
03057             }
03058          }
03059       }
03060       ast_config_destroy(ucfg);
03061    }
03062 
03063    while ((cat = ast_category_browse(cfg, cat))) {
03064       struct ast_variable *var = NULL;
03065 
03066       if (!strcasecmp(cat, "general"))
03067          continue;
03068 
03069       /* Look for an existing entry, if none found - create one and add it to the list */
03070       if (!(user = ast_get_manager_by_name_locked(cat))) {
03071          if (!(user = ast_calloc(1, sizeof(*user))))
03072             break;
03073          /* Copy name over */
03074          ast_copy_string(user->username, cat, sizeof(user->username));
03075          /* Insert into list */
03076          AST_LIST_INSERT_TAIL(&users, user, list);
03077       }
03078 
03079       /* Make sure we keep this user and don't destroy it during cleanup */
03080       user->keep = 1;
03081 
03082       var = ast_variable_browse(cfg, cat);
03083       while (var) {
03084          if (!strcasecmp(var->name, "secret")) {
03085             if (user->secret)
03086                free(user->secret);
03087             user->secret = ast_strdup(var->value);
03088          } else if (!strcasecmp(var->name, "deny") ) {
03089             if (user->deny)
03090                free(user->deny);
03091             user->deny = ast_strdup(var->value);
03092          } else if (!strcasecmp(var->name, "permit") ) {
03093             if (user->permit)
03094                free(user->permit);
03095             user->permit = ast_strdup(var->value);
03096          }  else if (!strcasecmp(var->name, "read") ) {
03097             if (user->read)
03098                free(user->read);
03099             user->read = ast_strdup(var->value);
03100          }  else if (!strcasecmp(var->name, "write") ) {
03101             if (user->write)
03102                free(user->write);
03103             user->write = ast_strdup(var->value);
03104          }  else if (!strcasecmp(var->name, "displayconnects") )
03105             user->displayconnects = ast_true(var->value);
03106          else
03107             ast_log(LOG_DEBUG, "%s is an unknown option.\n", var->name);
03108          var = var->next;
03109       }
03110    }
03111 
03112    /* Perform cleanup - essentially prune out old users that no longer exist */
03113    AST_LIST_TRAVERSE_SAFE_BEGIN(&users, user, list) {
03114       if (user->keep) {
03115          user->keep = 0;
03116          continue;
03117       }
03118       /* We do not need to keep this user so take them out of the list */
03119       AST_LIST_REMOVE_CURRENT(&users, list);
03120       /* Free their memory now */
03121       if (user->secret)
03122          free(user->secret);
03123       if (user->deny)
03124          free(user->deny);
03125       if (user->permit)
03126          free(user->permit);
03127       if (user->read)
03128          free(user->read);
03129       if (user->write)
03130          free(user->write);
03131       free(user);
03132    }
03133    AST_LIST_TRAVERSE_SAFE_END
03134 
03135    AST_LIST_UNLOCK(&users);
03136 
03137    ast_config_destroy(cfg);
03138    
03139    if (webenabled && enabled) {
03140       if (!webregged) {
03141          ast_http_uri_link(&rawmanuri);
03142          ast_http_uri_link(&manageruri);
03143          ast_http_uri_link(&managerxmluri);
03144          webregged = 1;
03145       }
03146    } else {
03147       if (webregged) {
03148          ast_http_uri_unlink(&rawmanuri);
03149          ast_http_uri_unlink(&manageruri);
03150          ast_http_uri_unlink(&managerxmluri);
03151          webregged = 0;
03152       }
03153    }
03154 
03155    if (newhttptimeout > 0)
03156       httptimeout = newhttptimeout;
03157 
03158    /* If not enabled, do nothing */
03159    if (!enabled)
03160       return 0;
03161 
03162    if (asock < 0) {
03163       asock = socket(AF_INET, SOCK_STREAM, 0);
03164       if (asock < 0) {
03165          ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
03166          return -1;
03167       }
03168       setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
03169       if (bind(asock, (struct sockaddr *)&ba, sizeof(ba))) {
03170          ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno));
03171          close(asock);
03172          asock = -1;
03173          return -1;
03174       }
03175       if (listen(asock, 2)) {
03176          ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno));
03177          close(asock);
03178          asock = -1;
03179          return -1;
03180       }
03181       flags = fcntl(asock, F_GETFL);
03182       fcntl(asock, F_SETFL, flags | O_NONBLOCK);
03183       if (option_verbose)
03184          ast_verbose("Asterisk Management interface listening on port %d\n", portno);
03185       ast_pthread_create_background(&t, NULL, accept_thread, NULL);
03186    }
03187    return 0;
03188 }

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

References FORMAT_HTML, and generic_http_callback().

02860 {
02861    return generic_http_callback(FORMAT_HTML, requestor, uri, params, status, title, contentlength);
02862 }

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

References FORMAT_XML, and generic_http_callback().

02865 {
02866    return generic_http_callback(FORMAT_XML, requestor, uri, params, status, title, contentlength);
02867 }

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

References FORMAT_RAW, and generic_http_callback().

02870 {
02871    return generic_http_callback(FORMAT_RAW, requestor, uri, params, status, title, contentlength);
02872 }

int reload_manager ( void   ) 

Definition at line 3190 of file manager.c.

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

03191 {
03192    manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n");
03193    return init_manager();
03194 }


Variable Documentation

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

Definition at line 2703 of file manager.c.

Definition at line 2881 of file manager.c.

Definition at line 2888 of file manager.c.

Definition at line 2874 of file manager.c.

int registered = 0 [static]

Definition at line 2895 of file manager.c.

int webregged = 0 [static]

Definition at line 2896 of file manager.c.

char* words[AST_MAX_CMD_LEN]

Definition at line 137 of file manager.c.

Referenced by check_blacklist(), and fix_complete_args().


Generated on Fri Jul 3 20:38:42 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9