Wed Aug 11 01:10:42 2010

Asterisk developer's documentation


AMI functions

callback to display queues status in manager More...


Data Structures

struct  actions
 list of actions registered More...
struct  all_events
struct  ast_manager_user
 user descriptor, as read from the config file. More...
struct  eventqent
struct  fast_originate_helper
 helper function for originate More...
struct  manager_hooks
 list of hooks registered More...
struct  mansession
struct  mansession_session
struct  permalias
struct  sessions
struct  users
 list of users found in the config file More...

Defines

#define ASTMAN_APPEND_BUF_INITSIZE   256
 initial allocated size for the astman_append_buf
#define GET_HEADER_FIRST_MATCH   0
#define GET_HEADER_LAST_MATCH   1
#define GET_HEADER_SKIP_EMPTY   2
#define MANAGER_EVENT_BUF_INITSIZE   256
#define MAX_BLACKLIST_CMD_LEN   2
 Descriptor for a manager session, either on the AMI socket or over HTTP.
#define MSG_MOREDATA   ((char *)astman_send_response)
 send a response with an optional message, and terminate it with an empty line. m is used only to grab the 'ActionID' field.

Enumerations

enum  error_type {
  UNKNOWN_ACTION = 1, UNKNOWN_CATEGORY, UNSPECIFIED_CATEGORY, UNSPECIFIED_ARGUMENT,
  FAILURE_ALLOCATION, FAILURE_NEWCAT, FAILURE_DELCAT, FAILURE_EMPTYCAT,
  FAILURE_UPDATE, FAILURE_DELETE, FAILURE_APPEND
}

Functions

static const char * __astman_get_header (const struct message *m, char *var, int mode)
static void __init_astman_append_buf (void)
 thread local buffer for astman_append
static void __init_manager_event_buf (void)
static void __init_userevent_buf (void)
int __manager_event (int category, const char *event, const char *file, int line, const char *func, const char *fmt,...)
 manager_event: Send AMI event to client
static int action_atxfer (struct mansession *s, const struct message *m)
static int action_challenge (struct mansession *s, const struct message *m)
static int action_command (struct mansession *s, const struct message *m)
 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_coreshowchannels (struct mansession *s, const struct message *m)
 Manager command "CoreShowChannels" - List currently defined channels and some information about them.
static int action_corestatus (struct mansession *s, const struct message *m)
 Show PBX core status information.
static int action_createconfig (struct mansession *s, const struct message *m)
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_getconfigjson (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_listcategories (struct mansession *s, const struct message *m)
static int action_listcommands (struct mansession *s, const struct message *m)
static int action_login (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_reload (struct mansession *s, const struct message *m)
 Send a reload event.
static int action_sendtext (struct mansession *s, const struct message *m)
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 struct eventqent * advance_event (struct eventqent *e)
static int append_event (const char *str, int category)
static int ast_instring (const char *bigstr, const char *smallstr, const char delim)
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
void ast_manager_register_hook (struct manager_custom_hook *hook)
 Add a custom hook to be called when an event is fired.
static int ast_manager_register_struct (struct manager_action *act)
int ast_manager_unregister (char *action)
 Unregister a registered manager command.
void ast_manager_unregister_hook (struct manager_custom_hook *hook)
 Delete a custom hook to be called when an event is fired.
void astman_append (struct mansession *s, const char *fmt,...)
const char * astman_get_header (const struct message *m, char *var)
 Get header from mananger transaction.
struct ast_variable * astman_get_variables (const struct message *m)
 Get a linked list of the Variable: headers.
void astman_send_ack (struct mansession *s, const struct message *m, char *msg)
 Send ack in manager transaction.
void astman_send_error (struct mansession *s, const struct message *m, char *error)
 Send error in manager transaction.
void astman_send_listack (struct mansession *s, const struct message *m, char *msg, char *listflag)
 Send ack in manager list transaction.
void astman_send_response (struct mansession *s, const struct message *m, char *resp, char *msg)
 Send response in manager transaction.
static void astman_send_response_full (struct mansession *s, const struct message *m, char *resp, char *msg, char *listflag)
static void astman_start_ack (struct mansession *s, const struct message *m)
static int authenticate (struct mansession *s, const struct message *m)
static char * authority_to_str (int authority, struct ast_str **res)
 Convert authority code to a list of options.
static int check_blacklist (const char *cmd)
int check_manager_enabled ()
 Event list management functions. We assume that the event list always has at least one element, and the delete code will not remove the last entry even if the.
static int check_manager_session_inuse (const char *name)
int check_webmanager_enabled ()
 Check if AMI/HTTP is enabled.
static void destroy_session (struct mansession_session *session)
static int do_message (struct mansession *s)
static void * fast_originate (void *data)
static void free_session (struct mansession_session *session)
static int get_input (struct mansession *s, char *output)
static struct ast_manager_user * get_manager_by_name_locked (const char *name)
static int get_perm (const char *instr)
static struct eventqent * grab_last (void)
static char * handle_manager_reload (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 CLI command manager reload.
static char * handle_mandebug (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_showmanager (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_showmanagers (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_showmancmd (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_showmancmds (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 CLI command manager list commands.
static char * handle_showmanconn (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 CLI command manager list connected.
static char * handle_showmaneventq (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 CLI command manager list eventq.
static enum error_type handle_updates (struct mansession *s, const struct message *m, struct ast_config *cfg, const char *dfn)
static void json_escape (char *out, const char *in)
static int manager_displayconnects (struct mansession_session *session)
 Get displayconnects config option.
static int manager_modulecheck (struct mansession *s, const struct message *m)
static int manager_moduleload (struct mansession *s, const struct message *m)
static int manager_state_cb (char *context, char *exten, int state, void *data)
static int process_events (struct mansession *s)
static int process_message (struct mansession *s, const struct message *m)
static void purge_events (void)
static void purge_sessions (int n_max)
 remove at most n_max stale session from the list.
static int send_string (struct mansession *s, char *string)
static void * session_do (void *data)
 The body of the individual manager session. Call get_input() to read one line at a time (or be woken up on new events), collect the lines in a message until found an empty line, and execute the request. In any case, deliver events asynchronously through process_events() (called from here if no line is available, or at the end of process_message(). ).
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)

Variables

static int allowmultiplelogin = 1
static struct ast_threadstorage astman_append_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_astman_append_buf , .custom_init = NULL , }
static int block_sockets
static int broken_events_action = 0
static struct ast_cli_entry cli_manager []
struct {
   char *   words [AST_MAX_CMD_LEN]
} command_blacklist []
static int displayconnects = 1
static int httptimeout = 60
static int manager_debug
static int manager_enabled = 0
static struct ast_threadstorage manager_event_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_manager_event_buf , .custom_init = NULL , }
static char mandescr_atxfer []
static char mandescr_command []
static char mandescr_coresettings []
static char mandescr_coreshowchannels []
static char mandescr_corestatus []
static char mandescr_createconfig []
static char mandescr_events []
static char mandescr_extensionstate []
static char mandescr_getconfig []
static char mandescr_getconfigjson []
static char mandescr_getvar []
static char mandescr_hangup []
static char mandescr_listcategories []
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_modulecheck []
static char mandescr_moduleload []
static char mandescr_originate []
static char mandescr_ping []
 Manager PING.
static char mandescr_redirect []
static char mandescr_reload []
static char mandescr_sendtext []
static char mandescr_setvar []
static char mandescr_status []
static char mandescr_timeout []
static char mandescr_updateconfig []
static char mandescr_userevent []
static char mandescr_waitevent []
 Manager WAITEVENT.
static int num_sessions
static struct permalias perms []
static int timestampevents
static struct ast_threadstorage userevent_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_userevent_buf , .custom_init = NULL , }
static int webmanager_enabled = 0


Detailed Description

callback to display queues status in manager

callback to display list of locally configured nodes


Define Documentation

#define ASTMAN_APPEND_BUF_INITSIZE   256

initial allocated size for the astman_append_buf

Definition at line 978 of file manager.c.

Referenced by astman_append().

#define GET_HEADER_FIRST_MATCH   0

Definition at line 881 of file manager.c.

Referenced by astman_get_header().

#define GET_HEADER_LAST_MATCH   1

Definition at line 882 of file manager.c.

Referenced by __astman_get_header().

#define GET_HEADER_SKIP_EMPTY   2

Definition at line 883 of file manager.c.

Referenced by __astman_get_header(), and process_message().

#define MANAGER_EVENT_BUF_INITSIZE   256

Definition at line 3349 of file manager.c.

Referenced by __manager_event().

#define MAX_BLACKLIST_CMD_LEN   2

Descriptor for a manager session, either on the AMI socket or over HTTP.

Note:
AMI session have managerid == 0; the entry is created upon a connect, and destroyed with the socket. HTTP sessions have managerid != 0, the value is used as a search key to lookup sessions (using the mansession_id cookie).

Definition at line 145 of file manager.c.

Referenced by check_blacklist().

#define MSG_MOREDATA   ((char *)astman_send_response)

send a response with an optional message, and terminate it with an empty line. m is used only to grab the 'ActionID' field.

Note:
NOTE: XXX this comment is unclear and possibly wrong. Callers of astman_send_error(), astman_send_response() or astman_send_ack() must EITHER hold the session lock _or_ be running in an action callback (in which case s->session->busy will be non-zero). In either of these cases, there is no need to lock-protect the session's fd, since no other output will be sent (events will be queued), and no input will be read until either the current action finishes or get_input() obtains the session lock.
Use the explicit constant MSG_MOREDATA to remove the empty line. XXX MSG_MOREDATA should go to a header file.

Definition at line 1018 of file manager.c.

Referenced by astman_send_response_full(), and astman_start_ack().


Enumeration Type Documentation

enum error_type

Doxygen group

Enumerator:
UNKNOWN_ACTION 
UNKNOWN_CATEGORY 
UNSPECIFIED_CATEGORY 
UNSPECIFIED_ARGUMENT 
FAILURE_ALLOCATION 
FAILURE_NEWCAT 
FAILURE_DELCAT 
FAILURE_EMPTYCAT 
FAILURE_UPDATE 
FAILURE_DELETE 
FAILURE_APPEND 

Definition at line 78 of file manager.c.


Function Documentation

static const char* __astman_get_header ( const struct message *  m,
char *  var,
int  mode 
) [static]

Definition at line 884 of file manager.c.

References ast_strlen_zero(), GET_HEADER_LAST_MATCH, GET_HEADER_SKIP_EMPTY, message::hdrcount, and message::headers.

Referenced by astman_get_header(), and process_message().

00885 {
00886    int x, l = strlen(var);
00887    const char *result = "";
00888 
00889    for (x = 0; x < m->hdrcount; x++) {
00890       const char *h = m->headers[x];
00891       if (!strncasecmp(var, h, l) && h[l] == ':' && h[l+1] == ' ') {
00892          const char *value = h + l + 2;
00893          /* found a potential candidate */
00894          if (mode & GET_HEADER_SKIP_EMPTY && ast_strlen_zero(value))
00895             continue;   /* not interesting */
00896          if (mode & GET_HEADER_LAST_MATCH)
00897             result = value;   /* record the last match so far */
00898          else
00899             return value;
00900       }
00901    }
00902 
00903    return "";
00904 }

static void __init_astman_append_buf ( void   )  [static]

thread local buffer for astman_append

Note:
This can not be defined within the astman_append() function because it declares a couple of functions that get used to initialize the thread local storage key.

Definition at line 974 of file manager.c.

00984 {

static void __init_manager_event_buf ( void   )  [static]

Definition at line 3348 of file manager.c.

03351 : Send AMI event to client */

static void __init_userevent_buf ( void   )  [static]

Definition at line 975 of file manager.c.

00984 {

int __manager_event ( int  category,
const char *  event,
const char *  file,
int  line,
const char *  func,
const char *  fmt,
  ... 
)

manager_event: Send AMI event to client

Definition at line 3352 of file manager.c.

References mansession_session::__lock, append_event(), ast_atomic_fetchadd_int(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), AST_PTHREADT_NULL, AST_RWLIST_EMPTY, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_str_alloca, ast_str_append(), ast_str_append_va(), ast_str_buffer(), ast_str_set(), ast_str_thread_get(), ast_tvnow(), authority_to_str(), buf, manager_custom_hook::helper, manager_event_buf, MANAGER_EVENT_BUF_INITSIZE, mansession_session::pending_event, eventqent::seq, and mansession_session::waiting_thread.

03354 {
03355    struct mansession_session *session;
03356    struct manager_custom_hook *hook;
03357    struct ast_str *auth = ast_str_alloca(80);
03358    const char *cat_str;
03359    va_list ap;
03360    struct timeval now;
03361    struct ast_str *buf;
03362 
03363    /* Abort if there are neither any manager sessions nor hooks */
03364    if (!num_sessions && AST_RWLIST_EMPTY(&manager_hooks))
03365       return 0;
03366 
03367    if (!(buf = ast_str_thread_get(&manager_event_buf, MANAGER_EVENT_BUF_INITSIZE)))
03368       return -1;
03369 
03370    cat_str = authority_to_str(category, &auth);
03371    ast_str_set(&buf, 0,
03372          "Event: %s\r\nPrivilege: %s\r\n",
03373           event, cat_str);
03374 
03375    if (timestampevents) {
03376       now = ast_tvnow();
03377       ast_str_append(&buf, 0,
03378             "Timestamp: %ld.%06lu\r\n",
03379              (long)now.tv_sec, (unsigned long) now.tv_usec);
03380    }
03381    if (manager_debug) {
03382       static int seq;
03383       ast_str_append(&buf, 0,
03384             "SequenceNumber: %d\r\n",
03385              ast_atomic_fetchadd_int(&seq, 1));
03386       ast_str_append(&buf, 0,
03387             "File: %s\r\nLine: %d\r\nFunc: %s\r\n", file, line, func);
03388    }
03389 
03390    va_start(ap, fmt);
03391    ast_str_append_va(&buf, 0, fmt, ap);
03392    va_end(ap);
03393 
03394    ast_str_append(&buf, 0, "\r\n");
03395 
03396    append_event(ast_str_buffer(buf), category);
03397 
03398    if (num_sessions) {
03399       /* Wake up any sleeping sessions */
03400       AST_LIST_LOCK(&sessions);
03401       AST_LIST_TRAVERSE(&sessions, session, list) {
03402          ast_mutex_lock(&session->__lock);
03403          if (session->waiting_thread != AST_PTHREADT_NULL)
03404             pthread_kill(session->waiting_thread, SIGURG);
03405          else
03406             /* We have an event to process, but the mansession is
03407              * not waiting for it. We still need to indicate that there
03408              * is an event waiting so that get_input processes the pending
03409              * event instead of polling.
03410              */
03411             session->pending_event = 1;
03412          ast_mutex_unlock(&session->__lock);
03413       }
03414       AST_LIST_UNLOCK(&sessions);
03415    }
03416 
03417    if (!AST_RWLIST_EMPTY(&manager_hooks)) {
03418       AST_RWLIST_RDLOCK(&manager_hooks);
03419       AST_RWLIST_TRAVERSE(&manager_hooks, hook, list) {
03420          hook->helper(category, event, ast_str_buffer(buf));
03421       }
03422       AST_RWLIST_UNLOCK(&manager_hooks);
03423    }
03424 
03425    return 0;
03426 }

static int action_atxfer ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 2206 of file manager.c.

References ast_channel_unlock, ast_find_call_feature(), AST_FRAME_DTMF, ast_get_channel_by_name_locked(), ast_queue_frame(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), chan, context, ast_call_feature::exten, exten, name, and pbx_builtin_setvar_helper().

Referenced by __init_manager().

02207 {
02208    const char *name = astman_get_header(m, "Channel");
02209    const char *exten = astman_get_header(m, "Exten");
02210    const char *context = astman_get_header(m, "Context");
02211    struct ast_channel *chan = NULL;
02212    struct ast_call_feature *atxfer_feature = NULL;
02213    char *feature_code = NULL;
02214 
02215    if (ast_strlen_zero(name)) { 
02216       astman_send_error(s, m, "No channel specified");
02217       return 0;
02218    }
02219    if (ast_strlen_zero(exten)) {
02220       astman_send_error(s, m, "No extension specified");
02221       return 0;
02222    }
02223 
02224    if (!(atxfer_feature = ast_find_call_feature("atxfer"))) {
02225       astman_send_error(s, m, "No attended transfer feature found");
02226       return 0;
02227    }
02228 
02229    if (!(chan = ast_get_channel_by_name_locked(name))) {
02230       astman_send_error(s, m, "Channel specified does not exist");
02231       return 0;
02232    }
02233 
02234    if (!ast_strlen_zero(context)) {
02235       pbx_builtin_setvar_helper(chan, "TRANSFER_CONTEXT", context);
02236    }
02237 
02238    for (feature_code = atxfer_feature->exten; feature_code && *feature_code; ++feature_code) {
02239       struct ast_frame f = {AST_FRAME_DTMF, *feature_code};
02240       ast_queue_frame(chan, &f);
02241    }
02242 
02243    for (feature_code = (char *)exten; feature_code && *feature_code; ++feature_code) {
02244       struct ast_frame f = {AST_FRAME_DTMF, *feature_code};
02245       ast_queue_frame(chan, &f);
02246    }
02247 
02248    astman_send_ack(s, m, "Atxfer successfully queued");
02249    ast_channel_unlock(chan);
02250 
02251    return 0;
02252 }

static int action_challenge ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1793 of file manager.c.

References mansession_session::__lock, ast_mutex_lock(), ast_mutex_unlock(), ast_random(), ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_error(), astman_start_ack(), mansession_session::challenge, and mansession::session.

Referenced by __init_manager().

01794 {
01795    const char *authtype = astman_get_header(m, "AuthType");
01796 
01797    if (!strcasecmp(authtype, "MD5")) {
01798       if (ast_strlen_zero(s->session->challenge))
01799          snprintf(s->session->challenge, sizeof(s->session->challenge), "%ld", ast_random());
01800       ast_mutex_lock(&s->session->__lock);
01801       astman_start_ack(s, m);
01802       astman_append(s, "Challenge: %s\r\n\r\n", s->session->challenge);
01803       ast_mutex_unlock(&s->session->__lock);
01804    } else {
01805       astman_send_error(s, m, "Must specify AuthType");
01806    }
01807    return 0;
01808 }

static int action_command ( struct mansession *  s,
const struct message *  m 
) [static]

Manager command "command" - execute CLI command.

Definition at line 2296 of file manager.c.

References ast_calloc, ast_cli_command, ast_free, ast_log(), ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_error(), buf, check_blacklist(), errno, LOG_WARNING, S_OR, and term_strip().

Referenced by __init_manager().

02297 {
02298    const char *cmd = astman_get_header(m, "Command");
02299    const char *id = astman_get_header(m, "ActionID");
02300    char *buf, *final_buf;
02301    char template[] = "/tmp/ast-ami-XXXXXX";  /* template for temporary file */
02302    int fd;
02303    off_t l;
02304 
02305    if (ast_strlen_zero(cmd)) {
02306       astman_send_error(s, m, "No command provided");
02307       return 0;
02308    }
02309 
02310    if (check_blacklist(cmd)) {
02311       astman_send_error(s, m, "Command blacklisted");
02312       return 0;
02313    }
02314 
02315    fd = mkstemp(template);
02316 
02317    astman_append(s, "Response: Follows\r\nPrivilege: Command\r\n");
02318    if (!ast_strlen_zero(id))
02319       astman_append(s, "ActionID: %s\r\n", id);
02320    /* FIXME: Wedge a ActionID response in here, waiting for later changes */
02321    ast_cli_command(fd, cmd);  /* XXX need to change this to use a FILE * */
02322    l = lseek(fd, 0, SEEK_END);   /* how many chars available */
02323 
02324    /* This has a potential to overflow the stack.  Hence, use the heap. */
02325    buf = ast_calloc(1, l + 1);
02326    final_buf = ast_calloc(1, l + 1);
02327    if (buf) {
02328       lseek(fd, 0, SEEK_SET);
02329       if (read(fd, buf, l) < 0) {
02330          ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
02331       }
02332       buf[l] = '\0';
02333       if (final_buf) {
02334          term_strip(final_buf, buf, l);
02335          final_buf[l] = '\0';
02336       }
02337       astman_append(s, "%s", S_OR(final_buf, buf));
02338       ast_free(buf);
02339    }
02340    close(fd);
02341    unlink(template);
02342    astman_append(s, "--END COMMAND--\r\n\r\n");
02343    if (final_buf)
02344       ast_free(final_buf);
02345    return 0;
02346 }

static int action_coresettings ( struct mansession *  s,
const struct message *  m 
) [static]

Show PBX core settings information.

Definition at line 2746 of file manager.c.

References AMI_VERSION, ast_config_AST_RUN_GROUP, ast_config_AST_RUN_USER, ast_config_AST_SYSTEM_NAME, ast_get_version(), ast_realtime_enabled(), ast_strlen_zero(), astman_append(), astman_get_header(), check_cdr_enabled(), check_webmanager_enabled(), option_maxcalls, option_maxfiles, and option_maxload.

Referenced by __init_manager().

02747 {
02748    const char *actionid = astman_get_header(m, "ActionID");
02749    char idText[150];
02750 
02751    if (!ast_strlen_zero(actionid))
02752       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", actionid);
02753    else
02754       idText[0] = '\0';
02755 
02756    astman_append(s, "Response: Success\r\n"
02757          "%s"
02758          "AMIversion: %s\r\n"
02759          "AsteriskVersion: %s\r\n"
02760          "SystemName: %s\r\n"
02761          "CoreMaxCalls: %d\r\n"
02762          "CoreMaxLoadAvg: %f\r\n"
02763          "CoreRunUser: %s\r\n"
02764          "CoreRunGroup: %s\r\n"
02765          "CoreMaxFilehandles: %d\r\n" 
02766          "CoreRealTimeEnabled: %s\r\n"
02767          "CoreCDRenabled: %s\r\n"
02768          "CoreHTTPenabled: %s\r\n"
02769          "\r\n",
02770          idText,
02771          AMI_VERSION,
02772          ast_get_version(), 
02773          ast_config_AST_SYSTEM_NAME,
02774          option_maxcalls,
02775          option_maxload,
02776          ast_config_AST_RUN_USER,
02777          ast_config_AST_RUN_GROUP,
02778          option_maxfiles,
02779          ast_realtime_enabled() ? "Yes" : "No",
02780          check_cdr_enabled() ? "Yes" : "No",
02781          check_webmanager_enabled() ? "Yes" : "No"
02782          );
02783    return 0;
02784 }

static int action_coreshowchannels ( struct mansession *  s,
const struct message *  m 
) [static]

Manager command "CoreShowChannels" - List currently defined channels and some information about them.

Definition at line 2851 of file manager.c.

References ast_channel::_state, ast_channel::accountcode, ast_channel::appl, ast_bridged_channel(), ast_channel_unlock, ast_channel_walk_locked(), ast_state2str(), ast_strlen_zero(), ast_tvdiff_ms(), ast_tvnow(), ast_tvzero(), astman_append(), astman_get_header(), astman_send_listack(), ast_channel::cdr, ast_channel::cid, ast_callerid::cid_num, ast_channel::context, ast_channel::data, ast_channel::exten, ast_channel::name, ast_channel::priority, S_OR, ast_cdr::start, and ast_channel::uniqueid.

Referenced by __init_manager().

02852 {
02853    const char *actionid = astman_get_header(m, "ActionID");
02854    char idText[256];
02855    struct ast_channel *c = NULL;
02856    int numchans = 0;
02857    int duration, durh, durm, durs;
02858 
02859    if (!ast_strlen_zero(actionid))
02860       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", actionid);
02861    else
02862       idText[0] = '\0';
02863 
02864    astman_send_listack(s, m, "Channels will follow", "start"); 
02865 
02866    while ((c = ast_channel_walk_locked(c)) != NULL) {
02867       struct ast_channel *bc = ast_bridged_channel(c);
02868       char durbuf[10] = "";
02869 
02870       if (c->cdr && !ast_tvzero(c->cdr->start)) {
02871          duration = (int)(ast_tvdiff_ms(ast_tvnow(), c->cdr->start) / 1000);
02872          durh = duration / 3600;
02873          durm = (duration % 3600) / 60;
02874          durs = duration % 60;
02875          snprintf(durbuf, sizeof(durbuf), "%02d:%02d:%02d", durh, durm, durs);
02876       }
02877 
02878       astman_append(s,
02879          "Event: CoreShowChannel\r\n"
02880          "%s"
02881          "Channel: %s\r\n"
02882          "UniqueID: %s\r\n"
02883          "Context: %s\r\n"
02884          "Extension: %s\r\n"
02885          "Priority: %d\r\n"
02886          "ChannelState: %d\r\n"
02887          "ChannelStateDesc: %s\r\n"
02888          "Application: %s\r\n"
02889          "ApplicationData: %s\r\n"
02890          "CallerIDnum: %s\r\n"
02891          "Duration: %s\r\n"
02892          "AccountCode: %s\r\n"
02893          "BridgedChannel: %s\r\n"
02894          "BridgedUniqueID: %s\r\n"
02895          "\r\n", idText, c->name, c->uniqueid, c->context, c->exten, c->priority, c->_state,
02896          ast_state2str(c->_state), c->appl ? c->appl : "", c->data ? S_OR(c->data, "") : "",
02897          S_OR(c->cid.cid_num, ""), durbuf, S_OR(c->accountcode, ""), bc ? bc->name : "", bc ? bc->uniqueid : "");
02898       ast_channel_unlock(c);
02899       numchans++;
02900    }
02901 
02902    astman_append(s,
02903       "Event: CoreShowChannelsComplete\r\n"
02904       "EventList: Complete\r\n"
02905       "ListItems: %d\r\n"
02906       "%s"
02907       "\r\n", numchans, idText);
02908 
02909    return 0;
02910 }

static int action_corestatus ( struct mansession *  s,
const struct message *  m 
) [static]

Show PBX core status information.

Definition at line 2792 of file manager.c.

References ast_active_channels(), ast_lastreloadtime, ast_localtime(), ast_startuptime, ast_strftime(), ast_strlen_zero(), astman_append(), and astman_get_header().

Referenced by __init_manager().

02793 {
02794    const char *actionid = astman_get_header(m, "ActionID");
02795    char idText[150];
02796    char startuptime[150];
02797    char reloadtime[150];
02798    struct ast_tm tm;
02799 
02800    if (!ast_strlen_zero(actionid))
02801       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", actionid);
02802    else
02803       idText[0] = '\0';
02804 
02805    ast_localtime(&ast_startuptime, &tm, NULL);
02806    ast_strftime(startuptime, sizeof(startuptime), "%H:%M:%S", &tm);
02807    ast_localtime(&ast_lastreloadtime, &tm, NULL);
02808    ast_strftime(reloadtime, sizeof(reloadtime), "%H:%M:%S", &tm);
02809 
02810    astman_append(s, "Response: Success\r\n"
02811          "%s"
02812          "CoreStartupTime: %s\r\n"
02813          "CoreReloadTime: %s\r\n"
02814          "CoreCurrentCalls: %d\r\n"
02815          "\r\n",
02816          idText,
02817          startuptime,
02818          reloadtime,
02819          ast_active_channels()
02820          );
02821    return 0;
02822 }

static int action_createconfig ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1579 of file manager.c.

References ast_config_AST_CONFIG_DIR, AST_FILE_MODE, ast_str_alloca, ast_str_append(), ast_str_buffer(), ast_str_set(), astman_get_header(), astman_send_ack(), astman_send_error(), and errno.

Referenced by __init_manager().

01580 {
01581    int fd;
01582    const char *fn = astman_get_header(m, "Filename");
01583    struct ast_str *filepath = ast_str_alloca(PATH_MAX);
01584    ast_str_set(&filepath, 0, "%s/", ast_config_AST_CONFIG_DIR);
01585    ast_str_append(&filepath, 0, "%s", fn);
01586 
01587    if ((fd = open(ast_str_buffer(filepath), O_CREAT | O_EXCL, AST_FILE_MODE)) != -1) {
01588       close(fd);
01589       astman_send_ack(s, m, "New configuration file created successfully");
01590    } else {
01591       astman_send_error(s, m, strerror(errno));
01592    }
01593 
01594    return 0;
01595 }

static int action_events ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1729 of file manager.c.

References ARRAY_LEN, astman_append(), astman_get_header(), astman_send_error(), permalias::num, perms, and set_eventmask().

Referenced by __init_manager().

01730 {
01731    const char *mask = astman_get_header(m, "EventMask");
01732    int res, x;
01733 
01734    res = set_eventmask(s, mask);
01735    if (broken_events_action) {
01736       /* if this option is set we should not return a response on
01737        * error, or when all events are set */
01738 
01739       if (res > 0) {
01740          for (x = 0; x < ARRAY_LEN(perms); x++) {
01741             if (!strcasecmp(perms[x].label, "all") && res == perms[x].num) {
01742                return 0;
01743             }
01744          }
01745          astman_append(s, "Response: Success\r\n"
01746                 "Events: On\r\n\r\n");
01747       } else if (res == 0)
01748          astman_append(s, "Response: Success\r\n"
01749                 "Events: Off\r\n\r\n");
01750       return 0;
01751    }
01752 
01753    if (res > 0)
01754       astman_append(s, "Response: Success\r\n"
01755              "Events: On\r\n\r\n");
01756    else if (res == 0)
01757       astman_append(s, "Response: Success\r\n"
01758              "Events: Off\r\n\r\n");
01759    else
01760       astman_send_error(s, m, "Invalid event mask");
01761 
01762    return 0;
01763 }

static int action_extensionstate ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 2628 of file manager.c.

References ast_extension_state(), ast_get_hint(), ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_error(), astman_start_ack(), context, exten, and status.

Referenced by __init_manager().

02629 {
02630    const char *exten = astman_get_header(m, "Exten");
02631    const char *context = astman_get_header(m, "Context");
02632    char hint[256] = "";
02633    int status;
02634    if (ast_strlen_zero(exten)) {
02635       astman_send_error(s, m, "Extension not specified");
02636       return 0;
02637    }
02638    if (ast_strlen_zero(context))
02639       context = "default";
02640    status = ast_extension_state(NULL, context, exten);
02641    ast_get_hint(hint, sizeof(hint) - 1, NULL, 0, NULL, context, exten);
02642    astman_start_ack(s, m);
02643    astman_append(s,   "Message: Extension Status\r\n"
02644             "Exten: %s\r\n"
02645             "Context: %s\r\n"
02646             "Hint: %s\r\n"
02647             "Status: %d\r\n\r\n",
02648             exten, context, hint, status);
02649    return 0;
02650 }

static int action_getconfig ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1170 of file manager.c.

References ast_category_browse(), ast_config_destroy(), ast_config_load2(), ast_strlen_zero(), ast_variable_browse(), astman_append(), astman_get_header(), astman_send_error(), astman_start_ack(), eventqent::category, CONFIG_FLAG_NOCACHE, CONFIG_FLAG_WITHCOMMENTS, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, ast_variable::name, ast_variable::next, and ast_variable::value.

Referenced by __init_manager().

01171 {
01172    struct ast_config *cfg;
01173    const char *fn = astman_get_header(m, "Filename");
01174    const char *category = astman_get_header(m, "Category");
01175    int catcount = 0;
01176    int lineno = 0;
01177    char *cur_category = NULL;
01178    struct ast_variable *v;
01179    struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS | CONFIG_FLAG_NOCACHE };
01180 
01181    if (ast_strlen_zero(fn)) {
01182       astman_send_error(s, m, "Filename not specified");
01183       return 0;
01184    }
01185    cfg = ast_config_load2(fn, "manager", config_flags);
01186    if (cfg == CONFIG_STATUS_FILEMISSING) {
01187       astman_send_error(s, m, "Config file not found");
01188       return 0;
01189    } else if (cfg == CONFIG_STATUS_FILEINVALID) {
01190       astman_send_error(s, m, "Config file has invalid format");
01191       return 0;
01192    }
01193 
01194    astman_start_ack(s, m);
01195    while ((cur_category = ast_category_browse(cfg, cur_category))) {
01196       if (ast_strlen_zero(category) || (!ast_strlen_zero(category) && !strcmp(category, cur_category))) {
01197          lineno = 0;
01198          astman_append(s, "Category-%06d: %s\r\n", catcount, cur_category);
01199          for (v = ast_variable_browse(cfg, cur_category); v; v = v->next)
01200             astman_append(s, "Line-%06d-%06d: %s=%s\r\n", catcount, lineno++, v->name, v->value);
01201          catcount++;
01202       }
01203    }
01204    if (!ast_strlen_zero(category) && catcount == 0) /* TODO: actually, a config with no categories doesn't even get loaded */
01205       astman_append(s, "No categories found\r\n");
01206    ast_config_destroy(cfg);
01207    astman_append(s, "\r\n");
01208 
01209    return 0;
01210 }

static int action_getconfigjson ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1271 of file manager.c.

References ast_category_browse(), ast_config_destroy(), ast_config_load2(), ast_strlen_zero(), ast_variable_browse(), astman_append(), astman_get_header(), astman_send_error(), astman_start_ack(), buf, eventqent::category, CONFIG_FLAG_NOCACHE, CONFIG_FLAG_WITHCOMMENTS, CONFIG_STATUS_FILEINVALID, json_escape(), ast_variable::name, ast_variable::next, and ast_variable::value.

Referenced by __init_manager().

01272 {
01273    struct ast_config *cfg;
01274    const char *fn = astman_get_header(m, "Filename");
01275    char *category = NULL;
01276    struct ast_variable *v;
01277    int comma1 = 0;
01278    char *buf = NULL;
01279    unsigned int buf_len = 0;
01280    struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS | CONFIG_FLAG_NOCACHE };
01281 
01282    if (ast_strlen_zero(fn)) {
01283       astman_send_error(s, m, "Filename not specified");
01284       return 0;
01285    }
01286 
01287    if (!(cfg = ast_config_load2(fn, "manager", config_flags))) {
01288       astman_send_error(s, m, "Config file not found");
01289       return 0;
01290    } else if (cfg == CONFIG_STATUS_FILEINVALID) {
01291       astman_send_error(s, m, "Config file has invalid format");
01292       return 0;
01293    }
01294 
01295    buf_len = 512;
01296    buf = alloca(buf_len);
01297 
01298    astman_start_ack(s, m);
01299    astman_append(s, "JSON: {");
01300    while ((category = ast_category_browse(cfg, category))) {
01301       int comma2 = 0;
01302       if (buf_len < 2 * strlen(category) + 1) {
01303          buf_len *= 2;
01304          buf = alloca(buf_len);
01305       }
01306       json_escape(buf, category);
01307       astman_append(s, "%s\"%s\":[", comma1 ? "," : "", buf);
01308       if (!comma1)
01309          comma1 = 1;
01310       for (v = ast_variable_browse(cfg, category); v; v = v->next) {
01311          if (comma2)
01312             astman_append(s, ",");
01313          if (buf_len < 2 * strlen(v->name) + 1) {
01314             buf_len *= 2;
01315             buf = alloca(buf_len);
01316          }
01317          json_escape(buf, v->name);
01318          astman_append(s, "\"%s", buf);
01319          if (buf_len < 2 * strlen(v->value) + 1) {
01320             buf_len *= 2;
01321             buf = alloca(buf_len);
01322          }
01323          json_escape(buf, v->value);
01324          astman_append(s, "%s\"", buf);
01325          if (!comma2)
01326             comma2 = 1;
01327       }
01328       astman_append(s, "]");
01329    }
01330    astman_append(s, "}\r\n\r\n");
01331 
01332    ast_config_destroy(cfg);
01333 
01334    return 0;
01335 }

static int action_getvar ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1885 of file manager.c.

References ast_channel_alloc, ast_channel_free(), ast_channel_unlock, ast_func_read(), ast_get_channel_by_name_locked(), ast_log(), ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_error(), astman_start_ack(), LOG_ERROR, name, pbx_retrieve_variable(), and S_OR.

Referenced by __init_manager().

01886 {
01887    struct ast_channel *c = NULL;
01888    const char *name = astman_get_header(m, "Channel");
01889    const char *varname = astman_get_header(m, "Variable");
01890    char *varval;
01891    char workspace[1024] = "";
01892 
01893    if (ast_strlen_zero(varname)) {
01894       astman_send_error(s, m, "No variable specified");
01895       return 0;
01896    }
01897 
01898    if (!ast_strlen_zero(name)) {
01899       c = ast_get_channel_by_name_locked(name);
01900       if (!c) {
01901          astman_send_error(s, m, "No such channel");
01902          return 0;
01903       }
01904    }
01905 
01906    if (varname[strlen(varname) - 1] == ')') {
01907       if (!c) {
01908          c = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Bogus/manager");
01909          if (c) {
01910             ast_func_read(c, (char *) varname, workspace, sizeof(workspace));
01911             ast_channel_free(c);
01912             c = NULL;
01913          } else
01914             ast_log(LOG_ERROR, "Unable to allocate bogus channel for variable substitution.  Function results may be blank.\n");
01915       } else
01916          ast_func_read(c, (char *) varname, workspace, sizeof(workspace));
01917       varval = workspace;
01918    } else {
01919       pbx_retrieve_variable(c, varname, &varval, workspace, sizeof(workspace), NULL);
01920    }
01921 
01922    if (c)
01923       ast_channel_unlock(c);
01924    astman_start_ack(s, m);
01925    astman_append(s, "Variable: %s\r\nValue: %s\r\n\r\n", varname, S_OR(varval, ""));
01926 
01927    return 0;
01928 }

static int action_hangup ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1815 of file manager.c.

References ast_channel_unlock, ast_get_channel_by_name_locked(), ast_softhangup(), AST_SOFTHANGUP_EXPLICIT, ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), and name.

Referenced by __init_manager().

01816 {
01817    struct ast_channel *c = NULL;
01818    const char *name = astman_get_header(m, "Channel");
01819    if (ast_strlen_zero(name)) {
01820       astman_send_error(s, m, "No channel specified");
01821       return 0;
01822    }
01823    c = ast_get_channel_by_name_locked(name);
01824    if (!c) {
01825       astman_send_error(s, m, "No such channel");
01826       return 0;
01827    }
01828    ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
01829    ast_channel_unlock(c);
01830    astman_send_ack(s, m, "Channel Hungup");
01831    return 0;
01832 }

static int action_listcategories ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1218 of file manager.c.

References ast_category_browse(), ast_config_destroy(), ast_config_load2(), ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_error(), astman_start_ack(), eventqent::category, CONFIG_FLAG_NOCACHE, CONFIG_FLAG_WITHCOMMENTS, and CONFIG_STATUS_FILEINVALID.

Referenced by __init_manager().

01219 {
01220    struct ast_config *cfg;
01221    const char *fn = astman_get_header(m, "Filename");
01222    char *category = NULL;
01223    struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS | CONFIG_FLAG_NOCACHE };
01224    int catcount = 0;
01225 
01226    if (ast_strlen_zero(fn)) {
01227       astman_send_error(s, m, "Filename not specified");
01228       return 0;
01229    }
01230    if (!(cfg = ast_config_load2(fn, "manager", config_flags))) {
01231       astman_send_error(s, m, "Config file not found");
01232       return 0;
01233    } else if (cfg == CONFIG_STATUS_FILEINVALID) {
01234       astman_send_error(s, m, "Config file has invalid format");
01235       return 0;
01236    }
01237    astman_start_ack(s, m);
01238    while ((category = ast_category_browse(cfg, category))) {
01239       astman_append(s, "Category-%06d: %s\r\n", catcount, category);
01240       catcount++;
01241    }
01242    if (catcount == 0) /* TODO: actually, a config with no categories doesn't even get loaded */
01243       astman_append(s, "Error: no categories found\r\n");
01244    ast_config_destroy(cfg);
01245    astman_append(s, "\r\n");
01246 
01247    return 0;
01248 }

static int action_listcommands ( struct mansession *  s,
const struct message *  m 
) [static]

Note:
The actionlock is read-locked by the caller of this function

Definition at line 1705 of file manager.c.

References manager_action::action, AST_RWLIST_TRAVERSE, ast_str_alloca, astman_append(), astman_start_ack(), manager_action::authority, authority_to_str(), mansession::session, manager_action::synopsis, and mansession_session::writeperm.

Referenced by __init_manager().

01706 {
01707    struct manager_action *cur;
01708    struct ast_str *temp = ast_str_alloca(BUFSIZ); /* XXX very large ? */
01709 
01710    astman_start_ack(s, m);
01711    AST_RWLIST_TRAVERSE(&actions, cur, list) {
01712       if (s->session->writeperm & cur->authority || cur->authority == 0)
01713          astman_append(s, "%s: %s (Priv: %s)\r\n",
01714             cur->action, cur->synopsis, authority_to_str(cur->authority, &temp));
01715    }
01716    astman_append(s, "\r\n");
01717 
01718    return 0;
01719 }

static int action_login ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1775 of file manager.c.

References ast_inet_ntoa(), ast_log(), AST_OPT_FLAG_FULLY_BOOTED, ast_opt_send_fullybooted, ast_options, ast_test_flag, ast_verb, astman_send_ack(), astman_send_error(), authenticate(), mansession_session::authenticated, EVENT_FLAG_SYSTEM, LOG_EVENT, manager_displayconnects(), manager_event, mansession_session::managerid, mansession::session, mansession_session::sin, and mansession_session::username.

Referenced by __init_manager().

01776 {
01777    if (authenticate(s, m)) {
01778       sleep(1);
01779       astman_send_error(s, m, "Authentication failed");
01780       return -1;
01781    }
01782    s->session->authenticated = 1;
01783    if (manager_displayconnects(s->session))
01784       ast_verb(2, "%sManager '%s' logged on from %s\n", (s->session->managerid ? "HTTP " : ""), s->session->username, ast_inet_ntoa(s->session->sin.sin_addr));
01785    ast_log(LOG_EVENT, "%sManager '%s' logged on from %s\n", (s->session->managerid ? "HTTP " : ""), s->session->username, ast_inet_ntoa(s->session->sin.sin_addr));
01786    astman_send_ack(s, m, "Authentication accepted");
01787    if (ast_opt_send_fullybooted && ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
01788       manager_event(EVENT_FLAG_SYSTEM, "FullyBooted", "Status: Fully Booted\r\n");
01789    }
01790    return 0;
01791 }

static int action_logoff ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1769 of file manager.c.

References astman_send_response().

Referenced by __init_manager().

01770 {
01771    astman_send_response(s, m, "Goodbye", "Thanks for all the fish.");
01772    return -1;
01773 }

static int action_mailboxcount ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 2596 of file manager.c.

References ast_app_inboxcount2(), ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_error(), astman_start_ack(), and mailbox.

Referenced by __init_manager().

02597 {
02598    const char *mailbox = astman_get_header(m, "Mailbox");
02599    int newmsgs = 0, oldmsgs = 0, urgentmsgs = 0;;
02600 
02601    if (ast_strlen_zero(mailbox)) {
02602       astman_send_error(s, m, "Mailbox not specified");
02603       return 0;
02604    }
02605    ast_app_inboxcount2(mailbox, &urgentmsgs, &newmsgs, &oldmsgs);
02606    astman_start_ack(s, m);
02607    astman_append(s,   "Message: Mailbox Message Count\r\n"
02608             "Mailbox: %s\r\n"
02609             "UrgMessages: %d\r\n"
02610             "NewMessages: %d\r\n"
02611             "OldMessages: %d\r\n"
02612             "\r\n",
02613             mailbox, urgentmsgs, newmsgs, oldmsgs);
02614    return 0;
02615 }

static int action_mailboxstatus ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 2567 of file manager.c.

References ast_app_has_voicemail(), ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_error(), astman_start_ack(), and mailbox.

Referenced by __init_manager().

02568 {
02569    const char *mailbox = astman_get_header(m, "Mailbox");
02570    int ret;
02571 
02572    if (ast_strlen_zero(mailbox)) {
02573       astman_send_error(s, m, "Mailbox not specified");
02574       return 0;
02575    }
02576    ret = ast_app_has_voicemail(mailbox, NULL);
02577    astman_start_ack(s, m);
02578    astman_append(s, "Message: Mailbox Status\r\n"
02579           "Mailbox: %s\r\n"
02580           "Waiting: %d\r\n\r\n", mailbox, ret);
02581    return 0;
02582 }

static int action_originate ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 2430 of file manager.c.

References fast_originate_helper::account, fast_originate_helper::app, app, fast_originate_helper::appdata, ast_callerid_parse(), ast_calloc, ast_copy_string(), ast_findlabel_extension(), AST_FORMAT_SLINEAR, ast_free, ast_parse_allow_disallow(), ast_pbx_outgoing_app(), ast_pbx_outgoing_exten(), ast_pthread_create_detached, ast_shrink_phone_number(), ast_strlen_zero(), ast_true(), astman_get_header(), astman_get_variables(), astman_send_ack(), astman_send_error(), fast_originate_helper::cid_name, fast_originate_helper::cid_num, fast_originate_helper::context, context, fast_originate_helper::data, ast_frame::data, EVENT_FLAG_SYSTEM, fast_originate_helper::exten, exten, fast_originate(), fast_originate_helper::format, format, fast_originate_helper::idtext, name, fast_originate_helper::priority, mansession::session, strcasestr(), fast_originate_helper::tech, fast_originate_helper::timeout, fast_originate_helper::vars, and mansession_session::writeperm.

Referenced by __init_manager().

02431 {
02432    const char *name = astman_get_header(m, "Channel");
02433    const char *exten = astman_get_header(m, "Exten");
02434    const char *context = astman_get_header(m, "Context");
02435    const char *priority = astman_get_header(m, "Priority");
02436    const char *timeout = astman_get_header(m, "Timeout");
02437    const char *callerid = astman_get_header(m, "CallerID");
02438    const char *account = astman_get_header(m, "Account");
02439    const char *app = astman_get_header(m, "Application");
02440    const char *appdata = astman_get_header(m, "Data");
02441    const char *async = astman_get_header(m, "Async");
02442    const char *id = astman_get_header(m, "ActionID");
02443    const char *codecs = astman_get_header(m, "Codecs");
02444    struct ast_variable *vars = astman_get_variables(m);
02445    char *tech, *data;
02446    char *l = NULL, *n = NULL;
02447    int pi = 0;
02448    int res;
02449    int to = 30000;
02450    int reason = 0;
02451    char tmp[256];
02452    char tmp2[256];
02453    int format = AST_FORMAT_SLINEAR;
02454 
02455    pthread_t th;
02456    if (ast_strlen_zero(name)) {
02457       astman_send_error(s, m, "Channel not specified");
02458       return 0;
02459    }
02460    if (!ast_strlen_zero(priority) && (sscanf(priority, "%30d", &pi) != 1)) {
02461       if ((pi = ast_findlabel_extension(NULL, context, exten, priority, NULL)) < 1) {
02462          astman_send_error(s, m, "Invalid priority");
02463          return 0;
02464       }
02465    }
02466    if (!ast_strlen_zero(timeout) && (sscanf(timeout, "%30d", &to) != 1)) {
02467       astman_send_error(s, m, "Invalid timeout");
02468       return 0;
02469    }
02470    ast_copy_string(tmp, name, sizeof(tmp));
02471    tech = tmp;
02472    data = strchr(tmp, '/');
02473    if (!data) {
02474       astman_send_error(s, m, "Invalid channel");
02475       return 0;
02476    }
02477    *data++ = '\0';
02478    ast_copy_string(tmp2, callerid, sizeof(tmp2));
02479    ast_callerid_parse(tmp2, &n, &l);
02480    if (n) {
02481       if (ast_strlen_zero(n))
02482          n = NULL;
02483    }
02484    if (l) {
02485       ast_shrink_phone_number(l);
02486       if (ast_strlen_zero(l))
02487          l = NULL;
02488    }
02489    if (!ast_strlen_zero(codecs)) {
02490       format = 0;
02491       ast_parse_allow_disallow(NULL, &format, codecs, 1);
02492    }
02493    if (ast_true(async)) {
02494       struct fast_originate_helper *fast = ast_calloc(1, sizeof(*fast));
02495       if (!fast) {
02496          res = -1;
02497       } else {
02498          if (!ast_strlen_zero(id))
02499             snprintf(fast->idtext, sizeof(fast->idtext), "ActionID: %s", id);
02500          ast_copy_string(fast->tech, tech, sizeof(fast->tech));
02501             ast_copy_string(fast->data, data, sizeof(fast->data));
02502          ast_copy_string(fast->app, app, sizeof(fast->app));
02503          ast_copy_string(fast->appdata, appdata, sizeof(fast->appdata));
02504          if (l)
02505             ast_copy_string(fast->cid_num, l, sizeof(fast->cid_num));
02506          if (n)
02507             ast_copy_string(fast->cid_name, n, sizeof(fast->cid_name));
02508          fast->vars = vars;
02509          ast_copy_string(fast->context, context, sizeof(fast->context));
02510          ast_copy_string(fast->exten, exten, sizeof(fast->exten));
02511          ast_copy_string(fast->account, account, sizeof(fast->account));
02512          fast->format = format;
02513          fast->timeout = to;
02514          fast->priority = pi;
02515          if (ast_pthread_create_detached(&th, NULL, fast_originate, fast)) {
02516             ast_free(fast);
02517             res = -1;
02518          } else {
02519             res = 0;
02520          }
02521       }
02522    } else if (!ast_strlen_zero(app)) {
02523       /* To run the System application (or anything else that goes to shell), you must have the additional System privilege */
02524       if (!(s->session->writeperm & EVENT_FLAG_SYSTEM)
02525          && (
02526             strcasestr(app, "system") ||      /* System(rm -rf /)
02527                                                  TrySystem(rm -rf /)       */
02528             strcasestr(app, "exec") ||        /* Exec(System(rm -rf /))
02529                                                  TryExec(System(rm -rf /)) */
02530             strcasestr(app, "agi") ||         /* AGI(/bin/rm,-rf /)
02531                                                  EAGI(/bin/rm,-rf /)       */
02532             strstr(appdata, "SHELL") ||       /* NoOp(${SHELL(rm -rf /)})  */
02533             strstr(appdata, "EVAL")           /* NoOp(${EVAL(${some_var_containing_SHELL})}) */
02534             )) {
02535          astman_send_error(s, m, "Originate with certain 'Application' arguments requires the additional System privilege, which you do not have.");
02536          return 0;
02537       }
02538       res = ast_pbx_outgoing_app(tech, format, data, to, app, appdata, &reason, 1, l, n, vars, account, NULL);
02539    } else {
02540       if (exten && context && pi)
02541          res = ast_pbx_outgoing_exten(tech, format, data, to, context, exten, pi, &reason, 1, l, n, vars, account, NULL);
02542       else {
02543          astman_send_error(s, m, "Originate with 'Exten' requires 'Context' and 'Priority'");
02544          return 0;
02545       }
02546    }
02547    if (!res)
02548       astman_send_ack(s, m, "Originate successfully queued");
02549    else
02550       astman_send_error(s, m, "Originate failed");
02551    return 0;
02552 }

static int action_ping ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1151 of file manager.c.

References ast_strlen_zero(), astman_append(), and astman_get_header().

Referenced by __init_manager().

01152 {
01153    const char *actionid = astman_get_header(m, "ActionID");
01154 
01155    astman_append(s, "Response: Success\r\n");
01156    if (!ast_strlen_zero(actionid)){
01157       astman_append(s, "ActionID: %s\r\n", actionid);
01158    }
01159    astman_append(s, "Ping: Pong\r\n\r\n");
01160    return 0;
01161 }

static int action_redirect ( struct mansession *  s,
const struct message *  m 
) [static]

action_redirect: The redirect manager command

Definition at line 2122 of file manager.c.

References ast_async_goto(), ast_channel_lock, ast_channel_unlock, ast_check_hangup(), ast_findlabel_extension(), AST_FLAG_BRIDGE_HANGUP_DONT, ast_get_channel_by_name_locked(), ast_set_flag, ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), buf, chan, context, exten, name, ast_channel::pbx, and ast_channel::priority.

Referenced by __init_manager().

02123 {
02124    const char *name = astman_get_header(m, "Channel");
02125    const char *name2 = astman_get_header(m, "ExtraChannel");
02126    const char *exten = astman_get_header(m, "Exten");
02127    const char *context = astman_get_header(m, "Context");
02128    const char *priority = astman_get_header(m, "Priority");
02129    struct ast_channel *chan, *chan2 = NULL;
02130    int pi = 0;
02131    int res;
02132 
02133    if (ast_strlen_zero(name)) {
02134       astman_send_error(s, m, "Channel not specified");
02135       return 0;
02136    }
02137    if (!ast_strlen_zero(priority) && (sscanf(priority, "%30d", &pi) != 1)) {
02138       if ((pi = ast_findlabel_extension(NULL, context, exten, priority, NULL)) < 1) {
02139          astman_send_error(s, m, "Invalid priority");
02140          return 0;
02141       }
02142    }
02143    /* XXX watch out, possible deadlock - we are trying to get two channels!!! */
02144    chan = ast_get_channel_by_name_locked(name);
02145    if (!chan) {
02146       char buf[256];
02147       snprintf(buf, sizeof(buf), "Channel does not exist: %s", name);
02148       astman_send_error(s, m, buf);
02149       return 0;
02150    }
02151    if (ast_check_hangup(chan)) {
02152       astman_send_error(s, m, "Redirect failed, channel not up.");
02153       ast_channel_unlock(chan);
02154       return 0;
02155    }
02156    if (!ast_strlen_zero(name2))
02157       chan2 = ast_get_channel_by_name_locked(name2);
02158    if (chan2 && ast_check_hangup(chan2)) {
02159       astman_send_error(s, m, "Redirect failed, extra channel not up.");
02160       ast_channel_unlock(chan);
02161       ast_channel_unlock(chan2);
02162       return 0;
02163    }
02164    if (chan->pbx) {
02165       ast_channel_lock(chan);
02166       ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT); /* don't let the after-bridge code run the h-exten */
02167       ast_channel_unlock(chan);
02168    }
02169    res = ast_async_goto(chan, context, exten, pi);
02170    if (!res) {
02171       if (!ast_strlen_zero(name2)) {
02172          if (chan2) {
02173             if (chan2->pbx) {
02174                ast_channel_lock(chan2);
02175                ast_set_flag(chan2, AST_FLAG_BRIDGE_HANGUP_DONT); /* don't let the after-bridge code run the h-exten */
02176                ast_channel_unlock(chan2);
02177             }
02178             res = ast_async_goto(chan2, context, exten, pi);
02179          } else {
02180             res = -1;
02181          }
02182          if (!res)
02183             astman_send_ack(s, m, "Dual Redirect successful");
02184          else
02185             astman_send_error(s, m, "Secondary redirect failed");
02186       } else
02187          astman_send_ack(s, m, "Redirect successful");
02188    } else
02189       astman_send_error(s, m, "Redirect failed");
02190    if (chan)
02191       ast_channel_unlock(chan);
02192    if (chan2)
02193       ast_channel_unlock(chan2);
02194    return 0;
02195 }

static int action_reload ( struct mansession *  s,
const struct message *  m 
) [static]

Send a reload event.

Definition at line 2831 of file manager.c.

References ast_module_reload(), astman_get_header(), astman_send_ack(), astman_send_error(), and S_OR.

Referenced by __init_manager().

02832 {
02833    const char *module = astman_get_header(m, "Module");
02834    int res = ast_module_reload(S_OR(module, NULL));
02835 
02836    if (res == 2)
02837       astman_send_ack(s, m, "Module Reloaded");
02838    else
02839       astman_send_error(s, m, s == 0 ? "No such module" : "Module does not support reload");
02840    return 0;
02841 }

static int action_sendtext ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 2077 of file manager.c.

References ast_channel_unlock, ast_get_channel_by_name_locked(), ast_sendtext(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), and name.

Referenced by __init_manager().

02078 {
02079    struct ast_channel *c = NULL;
02080    const char *name = astman_get_header(m, "Channel");
02081    const char *textmsg = astman_get_header(m, "Message");
02082    int res = 0;
02083 
02084    if (ast_strlen_zero(name)) {
02085       astman_send_error(s, m, "No channel specified");
02086       return 0;
02087    }
02088 
02089    if (ast_strlen_zero(textmsg)) {
02090       astman_send_error(s, m, "No Message specified");
02091       return 0;
02092    }
02093 
02094    c = ast_get_channel_by_name_locked(name);
02095    if (!c) {
02096       astman_send_error(s, m, "No such channel");
02097       return 0;
02098    }
02099 
02100    res = ast_sendtext(c, textmsg);
02101    ast_channel_unlock(c);
02102    
02103    if (res > 0)
02104       astman_send_ack(s, m, "Success");
02105    else
02106       astman_send_error(s, m, "Failure");
02107    
02108    return res;
02109 }

static int action_setvar ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1841 of file manager.c.

References ast_channel_unlock, ast_func_write(), ast_get_channel_by_name_locked(), ast_strdupa, ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), name, pbx_builtin_setvar_helper(), and S_OR.

Referenced by __init_manager().

01842 {
01843    struct ast_channel *c = NULL;
01844    const char *name = astman_get_header(m, "Channel");
01845    const char *varname = astman_get_header(m, "Variable");
01846    const char *varval = astman_get_header(m, "Value");
01847    int res = 0;
01848    
01849    if (ast_strlen_zero(varname)) {
01850       astman_send_error(s, m, "No variable specified");
01851       return 0;
01852    }
01853 
01854    if (!ast_strlen_zero(name)) {
01855       c = ast_get_channel_by_name_locked(name);
01856       if (!c) {
01857          astman_send_error(s, m, "No such channel");
01858          return 0;
01859       }
01860    }
01861    if (varname[strlen(varname)-1] == ')') {
01862       char *function = ast_strdupa(varname);
01863       res = ast_func_write(c, function, varval);
01864    } else {
01865       pbx_builtin_setvar_helper(c, varname, S_OR(varval, ""));
01866    }
01867 
01868    if (c)
01869       ast_channel_unlock(c);
01870    if (res == 0) {
01871       astman_send_ack(s, m, "Variable Set"); 
01872    } else {
01873       astman_send_error(s, m, "Variable not set");
01874    }
01875    return 0;
01876 }

static int action_status ( struct mansession *  s,
const struct message *  m 
) [static]

Manager "status" command to show channels.

Definition at line 1942 of file manager.c.

References ast_channel::_bridge, ast_channel::_state, ast_channel::accountcode, AST_APP_ARG, ast_channel_unlock, ast_channel_walk_locked(), AST_DECLARE_APP_ARGS, ast_free, ast_func_read(), ast_get_channel_by_name_locked(), AST_STANDARD_APP_ARGS, ast_state2str(), ast_str_append(), ast_str_buffer(), ast_str_create(), ast_str_reset(), ast_strdupa, ast_strlen_zero(), ast_tvnow(), astman_append(), astman_get_header(), astman_send_ack(), astman_send_error(), ast_channel::bridge, ast_channel::cdr, ast_channel::cid, ast_callerid::cid_name, ast_callerid::cid_num, ast_channel::context, ast_channel::exten, ast_channel::name, name, ast_channel::pbx, pbx_retrieve_variable(), ast_channel::priority, S_OR, ast_cdr::start, str, and ast_channel::uniqueid.

Referenced by __init_manager().

01943 {
01944    const char *name = astman_get_header(m, "Channel");
01945    const char *cvariables = astman_get_header(m, "Variables");
01946    char *variables = ast_strdupa(S_OR(cvariables, ""));
01947    struct ast_channel *c;
01948    char bridge[256];
01949    struct timeval now = ast_tvnow();
01950    long elapsed_seconds = 0;
01951    int channels = 0;
01952    int all = ast_strlen_zero(name); /* set if we want all channels */
01953    const char *id = astman_get_header(m, "ActionID");
01954    char idText[256];
01955    AST_DECLARE_APP_ARGS(vars,
01956       AST_APP_ARG(name)[100];
01957    );
01958    struct ast_str *str = ast_str_create(1000);
01959 
01960    if (!ast_strlen_zero(id))
01961       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
01962    else
01963       idText[0] = '\0';
01964 
01965    if (all)
01966       c = ast_channel_walk_locked(NULL);
01967    else {
01968       c = ast_get_channel_by_name_locked(name);
01969       if (!c) {
01970          astman_send_error(s, m, "No such channel");
01971          ast_free(str);
01972          return 0;
01973       }
01974    }
01975    astman_send_ack(s, m, "Channel status will follow");
01976 
01977    if (!ast_strlen_zero(cvariables)) {
01978       AST_STANDARD_APP_ARGS(vars, variables);
01979    }
01980 
01981    /* if we look by name, we break after the first iteration */
01982    while (c) {
01983       if (!ast_strlen_zero(cvariables)) {
01984          int i;
01985          ast_str_reset(str);
01986          for (i = 0; i < vars.argc; i++) {
01987             char valbuf[512], *ret = NULL;
01988 
01989             if (vars.name[i][strlen(vars.name[i]) - 1] == ')') {
01990                if (ast_func_read(c, vars.name[i], valbuf, sizeof(valbuf)) < 0) {
01991                   valbuf[0] = '\0';
01992                }
01993                ret = valbuf;
01994             } else {
01995                pbx_retrieve_variable(c, vars.name[i], &ret, valbuf, sizeof(valbuf), NULL);
01996             }
01997 
01998             ast_str_append(&str, 0, "Variable: %s=%s\r\n", vars.name[i], ret);
01999          }
02000       }
02001 
02002       channels++;
02003       if (c->_bridge)
02004          snprintf(bridge, sizeof(bridge), "BridgedChannel: %s\r\nBridgedUniqueid: %s\r\n", c->_bridge->name, c->_bridge->uniqueid);
02005       else
02006          bridge[0] = '\0';
02007       if (c->pbx) {
02008          if (c->cdr) {
02009             elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
02010          }
02011          astman_append(s,
02012          "Event: Status\r\n"
02013          "Privilege: Call\r\n"
02014          "Channel: %s\r\n"
02015          "CallerIDNum: %s\r\n"
02016          "CallerIDName: %s\r\n"
02017          "Accountcode: %s\r\n"
02018          "ChannelState: %d\r\n"
02019          "ChannelStateDesc: %s\r\n"
02020          "Context: %s\r\n"
02021          "Extension: %s\r\n"
02022          "Priority: %d\r\n"
02023          "Seconds: %ld\r\n"
02024          "%s"
02025          "Uniqueid: %s\r\n"
02026          "%s"
02027          "%s"
02028          "\r\n",
02029          c->name,
02030          S_OR(c->cid.cid_num, ""),
02031          S_OR(c->cid.cid_name, ""),
02032          c->accountcode,
02033          c->_state,
02034          ast_state2str(c->_state), c->context,
02035          c->exten, c->priority, (long)elapsed_seconds, bridge, c->uniqueid, ast_str_buffer(str), idText);
02036       } else {
02037          astman_append(s,
02038          "Event: Status\r\n"
02039          "Privilege: Call\r\n"
02040          "Channel: %s\r\n"
02041          "CallerIDNum: %s\r\n"
02042          "CallerIDName: %s\r\n"
02043          "Account: %s\r\n"
02044          "State: %s\r\n"
02045          "%s"
02046          "Uniqueid: %s\r\n"
02047          "%s"
02048          "%s"
02049          "\r\n",
02050          c->name,
02051          S_OR(c->cid.cid_num, "<unknown>"),
02052          S_OR(c->cid.cid_name, "<unknown>"),
02053          c->accountcode,
02054          ast_state2str(c->_state), bridge, c->uniqueid, ast_str_buffer(str), idText);
02055       }
02056       ast_channel_unlock(c);
02057       if (!all)
02058          break;
02059       c = ast_channel_walk_locked(c);
02060    }
02061    astman_append(s,
02062    "Event: StatusComplete\r\n"
02063    "%s"
02064    "Items: %d\r\n"
02065    "\r\n", idText, channels);
02066    ast_free(str);
02067    return 0;
02068 }

static int action_timeout ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 2659 of file manager.c.

References ast_channel_setwhentohangup_tv(), ast_channel_unlock, ast_get_channel_by_name_locked(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), and name.

Referenced by __init_manager().

02660 {
02661    struct ast_channel *c;
02662    const char *name = astman_get_header(m, "Channel");
02663    double timeout = atof(astman_get_header(m, "Timeout"));
02664    struct timeval when = { timeout, 0 };
02665 
02666    if (ast_strlen_zero(name)) {
02667       astman_send_error(s, m, "No channel specified");
02668       return 0;
02669    }
02670    if (!timeout || timeout < 0) {
02671       astman_send_error(s, m, "No timeout specified");
02672       return 0;
02673    }
02674    c = ast_get_channel_by_name_locked(name);
02675    if (!c) {
02676       astman_send_error(s, m, "No such channel");
02677       return 0;
02678    }
02679 
02680    when.tv_usec = (timeout - when.tv_sec) * 1000000.0;
02681    ast_channel_setwhentohangup_tv(c, when);
02682    ast_channel_unlock(c);
02683    astman_send_ack(s, m, "Timeout Set");
02684    return 0;
02685 }

static int action_updateconfig ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1495 of file manager.c.

References ast_config_destroy(), ast_config_load2(), ast_config_text_file_save(), ast_include_rename(), ast_module_reload(), ast_strlen_zero(), ast_true(), astman_get_header(), astman_send_ack(), astman_send_error(), CONFIG_FLAG_NOCACHE, CONFIG_FLAG_WITHCOMMENTS, CONFIG_STATUS_FILEINVALID, FAILURE_ALLOCATION, FAILURE_APPEND, FAILURE_DELCAT, FAILURE_DELETE, FAILURE_EMPTYCAT, FAILURE_NEWCAT, FAILURE_UPDATE, handle_updates(), UNKNOWN_ACTION, UNKNOWN_CATEGORY, UNSPECIFIED_ARGUMENT, and UNSPECIFIED_CATEGORY.

Referenced by __init_manager().

01496 {
01497    struct ast_config *cfg;
01498    const char *sfn = astman_get_header(m, "SrcFilename");
01499    const char *dfn = astman_get_header(m, "DstFilename");
01500    int res;
01501    const char *rld = astman_get_header(m, "Reload");
01502    struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS | CONFIG_FLAG_NOCACHE };
01503    enum error_type result;
01504 
01505    if (ast_strlen_zero(sfn) || ast_strlen_zero(dfn)) {
01506       astman_send_error(s, m, "Filename not specified");
01507       return 0;
01508    }
01509    if (!(cfg = ast_config_load2(sfn, "manager", config_flags))) {
01510       astman_send_error(s, m, "Config file not found");
01511       return 0;
01512    } else if (cfg == CONFIG_STATUS_FILEINVALID) {
01513       astman_send_error(s, m, "Config file has invalid format");
01514       return 0;
01515    }
01516    result = handle_updates(s, m, cfg, dfn);
01517    if (!result) {
01518       ast_include_rename(cfg, sfn, dfn); /* change the include references from dfn to sfn, so things match up */
01519       res = ast_config_text_file_save(dfn, cfg, "Manager");
01520       ast_config_destroy(cfg);
01521       if (res) {
01522          astman_send_error(s, m, "Save of config failed");
01523          return 0;
01524       }
01525       astman_send_ack(s, m, NULL);
01526       if (!ast_strlen_zero(rld)) {
01527          if (ast_true(rld))
01528             rld = NULL;
01529          ast_module_reload(rld);
01530       }
01531    } else {
01532       ast_config_destroy(cfg);
01533       switch(result) {
01534       case UNKNOWN_ACTION:
01535          astman_send_error(s, m, "Unknown action command");
01536          break;
01537       case UNKNOWN_CATEGORY:
01538          astman_send_error(s, m, "Given category does not exist");
01539          break;
01540       case UNSPECIFIED_CATEGORY:
01541          astman_send_error(s, m, "Category not specified");
01542          break;
01543       case UNSPECIFIED_ARGUMENT:
01544          astman_send_error(s, m, "Problem with category, value, or line (if required)");
01545          break;
01546       case FAILURE_ALLOCATION:
01547          astman_send_error(s, m, "Memory allocation failure, this should not happen");
01548          break;
01549       case FAILURE_NEWCAT:
01550          astman_send_error(s, m, "Create category did not complete successfully");
01551          break;
01552       case FAILURE_DELCAT:
01553          astman_send_error(s, m, "Delete category did not complete successfully");
01554          break;
01555       case FAILURE_EMPTYCAT:
01556          astman_send_error(s, m, "Empty category did not complete successfully");
01557          break;
01558       case FAILURE_UPDATE:
01559          astman_send_error(s, m, "Update did not complete successfully");
01560          break;
01561       case FAILURE_DELETE:
01562          astman_send_error(s, m, "Delete did not complete successfully");
01563          break;
01564       case FAILURE_APPEND:
01565          astman_send_error(s, m, "Append did not complete successfully");
01566          break;
01567       }
01568    }
01569    return 0;
01570 }

static int action_userevent ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 2721 of file manager.c.

References ast_str_append(), ast_str_buffer(), ast_str_reset(), ast_str_thread_get(), astman_get_header(), astman_send_ack(), EVENT_FLAG_USER, message::hdrcount, message::headers, manager_event, and userevent_buf.

Referenced by __init_manager().

02722 {
02723    const char *event = astman_get_header(m, "UserEvent");
02724    struct ast_str *body = ast_str_thread_get(&userevent_buf, 16);
02725    int x;
02726 
02727    ast_str_reset(body);
02728 
02729    for (x = 0; x < m->hdrcount; x++) {
02730       if (strncasecmp("UserEvent:", m->headers[x], strlen("UserEvent:"))) {
02731          ast_str_append(&body, 0, "%s\r\n", m->headers[x]);
02732       }
02733    }
02734 
02735    astman_send_ack(s, m, "Event Sent");   
02736    manager_event(EVENT_FLAG_USER, "UserEvent", "UserEvent: %s\r\n%s", event, ast_str_buffer(body));
02737    return 0;
02738 }

static int action_waitevent ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1605 of file manager.c.

References mansession_session::__lock, advance_event(), ast_debug, ast_mutex_lock(), ast_mutex_unlock(), AST_PTHREADT_NULL, AST_RWLIST_NEXT, ast_strlen_zero(), ast_wait_for_input(), astman_append(), astman_get_header(), astman_send_response(), eventqent::category, eventqent::eventdata, mansession_session::fd, mansession_session::last_ev, mansession_session::managerid, mansession_session::needdestroy, mansession_session::readperm, mansession_session::send_events, mansession::session, mansession_session::sessiontimeout, and mansession_session::waiting_thread.

Referenced by __init_manager().

01606 {
01607    const char *timeouts = astman_get_header(m, "Timeout");
01608    int timeout = -1;
01609    int x;
01610    int needexit = 0;
01611    const char *id = astman_get_header(m, "ActionID");
01612    char idText[256];
01613 
01614    if (!ast_strlen_zero(id))
01615       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
01616    else
01617       idText[0] = '\0';
01618 
01619    if (!ast_strlen_zero(timeouts)) {
01620       sscanf(timeouts, "%30i", &timeout);
01621       if (timeout < -1)
01622          timeout = -1;
01623       /* XXX maybe put an upper bound, or prevent the use of 0 ? */
01624    }
01625 
01626    ast_mutex_lock(&s->session->__lock);
01627    if (s->session->waiting_thread != AST_PTHREADT_NULL)
01628       pthread_kill(s->session->waiting_thread, SIGURG);
01629 
01630    if (s->session->managerid) { /* AMI-over-HTTP session */
01631       /*
01632        * Make sure the timeout is within the expire time of the session,
01633        * as the client will likely abort the request if it does not see
01634        * data coming after some amount of time.
01635        */
01636       time_t now = time(NULL);
01637       int max = s->session->sessiontimeout - now - 10;
01638 
01639       if (max < 0)   /* We are already late. Strange but possible. */
01640          max = 0;
01641       if (timeout < 0 || timeout > max)
01642          timeout = max;
01643       if (!s->session->send_events) /* make sure we record events */
01644          s->session->send_events = -1;
01645    }
01646    ast_mutex_unlock(&s->session->__lock);
01647 
01648    /* XXX should this go inside the lock ? */
01649    s->session->waiting_thread = pthread_self(); /* let new events wake up this thread */
01650    ast_debug(1, "Starting waiting for an event!\n");
01651 
01652    for (x = 0; x < timeout || timeout < 0; x++) {
01653       ast_mutex_lock(&s->session->__lock);
01654       if (AST_RWLIST_NEXT(s->session->last_ev, eq_next)) {
01655          needexit = 1;
01656       }
01657       /* We can have multiple HTTP session point to the same mansession entry.
01658        * The way we deal with it is not very nice: newcomers kick out the previous
01659        * HTTP session. XXX this needs to be improved.
01660        */
01661       if (s->session->waiting_thread != pthread_self())
01662          needexit = 1;
01663       if (s->session->needdestroy)
01664          needexit = 1;
01665       ast_mutex_unlock(&s->session->__lock);
01666       if (needexit)
01667          break;
01668       if (s->session->managerid == 0) {   /* AMI session */
01669          if (ast_wait_for_input(s->session->fd, 1000))
01670             break;
01671       } else { /* HTTP session */
01672          sleep(1);
01673       }
01674    }
01675    ast_debug(1, "Finished waiting for an event!\n");
01676    ast_mutex_lock(&s->session->__lock);
01677    if (s->session->waiting_thread == pthread_self()) {
01678       struct eventqent *eqe = s->session->last_ev;
01679       astman_send_response(s, m, "Success", "Waiting for Event completed.");
01680       while ((eqe = advance_event(eqe))) {
01681          if (((s->session->readperm & eqe->category) == eqe->category) &&
01682              ((s->session->send_events & eqe->category) == eqe->category)) {
01683             astman_append(s, "%s", eqe->eventdata);
01684          }
01685          s->session->last_ev = eqe;
01686       }
01687       astman_append(s,
01688          "Event: WaitEventComplete\r\n"
01689          "%s"
01690          "\r\n", idText);
01691       s->session->waiting_thread = AST_PTHREADT_NULL;
01692    } else {
01693       ast_debug(1, "Abandoning event request!\n");
01694    }
01695    ast_mutex_unlock(&s->session->__lock);
01696    return 0;
01697 }

static struct eventqent* advance_event ( struct eventqent *  e  )  [static, read]

Definition at line 830 of file manager.c.

References ast_atomic_fetchadd_int(), AST_RWLIST_NEXT, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, eventqent::next, and eventqent::usecount.

Referenced by action_waitevent(), and process_events().

00831 {
00832    struct eventqent *next;
00833 
00834    AST_RWLIST_RDLOCK(&all_events);
00835    if ((next = AST_RWLIST_NEXT(e, eq_next))) {
00836       ast_atomic_fetchadd_int(&next->usecount, 1);
00837       ast_atomic_fetchadd_int(&e->usecount, -1);
00838    }
00839    AST_RWLIST_UNLOCK(&all_events);
00840    return next;
00841 }

static int append_event ( const char *  str,
int  category 
) [static]

Definition at line 3324 of file manager.c.

References ast_atomic_fetchadd_int(), ast_malloc, AST_RWLIST_INSERT_TAIL, AST_RWLIST_NEXT, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_tvnow(), eventqent::category, eventqent::eventdata, eventqent::seq, eventqent::tv, and eventqent::usecount.

Referenced by __init_manager(), and __manager_event().

03325 {
03326    struct eventqent *tmp = ast_malloc(sizeof(*tmp) + strlen(str));
03327    static int seq;   /* sequence number */
03328 
03329    if (!tmp)
03330       return -1;
03331 
03332    /* need to init all fields, because ast_malloc() does not */
03333    tmp->usecount = 0;
03334    tmp->category = category;
03335    tmp->seq = ast_atomic_fetchadd_int(&seq, 1);
03336    tmp->tv = ast_tvnow();
03337    AST_RWLIST_NEXT(tmp, eq_next) = NULL;
03338    strcpy(tmp->eventdata, str);
03339 
03340    AST_RWLIST_WRLOCK(&all_events);
03341    AST_RWLIST_INSERT_TAIL(&all_events, tmp, eq_next);
03342    AST_RWLIST_UNLOCK(&all_events);
03343 
03344    return 0;
03345 }

static int ast_instring ( const char *  bigstr,
const char *  smallstr,
const char  delim 
) [static]

Tells you if smallstr exists inside bigstr which is delim by delim and uses no buf or stringsep ast_instring("this|that|more","this",'|') == 1;

feel free to move this to app.c -anthm

Definition at line 429 of file manager.c.

References eventqent::next.

Referenced by get_perm().

00430 {
00431    const char *val = bigstr, *next;
00432 
00433    do {
00434       if ((next = strchr(val, delim))) {
00435          if (!strncmp(val, smallstr, (next - val)))
00436             return 1;
00437          else
00438             continue;
00439       } else
00440          return !strcmp(smallstr, val);
00441    } while (*(val = (next + 1)));
00442 
00443    return 0;
00444 }

int ast_manager_register2 ( const char *  action,
int  auth,
int(*)(struct mansession *s, const struct message *m)  func,
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

Register a manager command with the manager interface.

Definition at line 3500 of file manager.c.

References manager_action::action, ast_calloc, ast_free, ast_manager_register_struct(), manager_action::authority, manager_action::description, manager_action::func, and manager_action::synopsis.

Referenced by __init_manager(), ast_features_init(), load_module(), and load_pbx().

03501 {
03502    struct manager_action *cur = NULL;
03503 
03504    if (!(cur = ast_calloc(1, sizeof(*cur))))
03505       return -1;
03506 
03507    cur->action = action;
03508    cur->authority = auth;
03509    cur->func = func;
03510    cur->synopsis = synopsis;
03511    cur->description = description;
03512 
03513    if (ast_manager_register_struct(cur)) {
03514       ast_free(cur);
03515       return -1;
03516    }
03517 
03518    return 0;
03519 }

void ast_manager_register_hook ( struct manager_custom_hook *  hook  ) 

Add a custom hook to be called when an event is fired.

Add a custom hook to be called when an event is fired

Parameters:
hook struct manager_custom_hook object to add

Definition at line 256 of file manager.c.

References AST_RWLIST_INSERT_TAIL, AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

00257 {
00258    AST_RWLIST_WRLOCK(&manager_hooks);
00259    AST_RWLIST_INSERT_TAIL(&manager_hooks, hook, list);
00260    AST_RWLIST_UNLOCK(&manager_hooks);
00261    return;
00262 }

static int ast_manager_register_struct ( struct manager_action *  act  )  [static]

Definition at line 3464 of file manager.c.

References manager_action::action, ast_log(), AST_RWLIST_INSERT_AFTER, AST_RWLIST_INSERT_HEAD, AST_RWLIST_TIMEDWRLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_verb, LOG_ERROR, LOG_WARNING, and eventqent::tv.

Referenced by ast_manager_register2().

03465 {
03466    struct manager_action *cur, *prev = NULL;
03467    struct timespec tv = { 5, };
03468 
03469    if (AST_RWLIST_TIMEDWRLOCK(&actions, &tv)) {
03470       ast_log(LOG_ERROR, "Could not obtain lock on manager list\n");
03471       return -1;
03472    }
03473    AST_RWLIST_TRAVERSE(&actions, cur, list) {
03474       int ret = strcasecmp(cur->action, act->action);
03475       if (ret == 0) {
03476          ast_log(LOG_WARNING, "Manager: Action '%s' already registered\n", act->action);
03477          AST_RWLIST_UNLOCK(&actions);
03478          return -1;
03479       }
03480       if (ret > 0) { /* Insert these alphabetically */
03481          prev = cur;
03482          break;
03483       }
03484    }
03485 
03486    if (prev)
03487       AST_RWLIST_INSERT_AFTER(&actions, prev, act, list);
03488    else
03489       AST_RWLIST_INSERT_HEAD(&actions, act, list);
03490 
03491    ast_verb(2, "Manager registered action %s\n", act->action);
03492 
03493    AST_RWLIST_UNLOCK(&actions);
03494 
03495    return 0;
03496 }

int ast_manager_unregister ( char *  action  ) 

Unregister a registered manager command.

Parameters:
action Name of registered Action:

Definition at line 3431 of file manager.c.

References manager_action::action, ast_free, ast_log(), AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TIMEDWRLOCK, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, ast_verb, LOG_ERROR, and eventqent::tv.

Referenced by __unload_module(), and unload_module().

03432 {
03433    struct manager_action *cur;
03434    struct timespec tv = { 5, };
03435 
03436    if (AST_RWLIST_TIMEDWRLOCK(&actions, &tv)) {
03437       ast_log(LOG_ERROR, "Could not obtain lock on manager list\n");
03438       return -1;
03439    }
03440    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&actions, cur, list) {
03441       if (!strcasecmp(action, cur->action)) {
03442          AST_RWLIST_REMOVE_CURRENT(list);
03443          ast_free(cur);
03444          ast_verb(2, "Manager unregistered action %s\n", action);
03445          break;
03446       }
03447    }
03448    AST_RWLIST_TRAVERSE_SAFE_END;
03449    AST_RWLIST_UNLOCK(&actions);
03450 
03451    return 0;
03452 }

void ast_manager_unregister_hook ( struct manager_custom_hook *  hook  ) 

Delete a custom hook to be called when an event is fired.

Delete a custom hook to be called when an event is fired

Parameters:
hook struct manager_custom_hook object to delete

Definition at line 265 of file manager.c.

References AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

00266 {
00267    AST_RWLIST_WRLOCK(&manager_hooks);
00268    AST_RWLIST_REMOVE(&manager_hooks, hook, list);
00269    AST_RWLIST_UNLOCK(&manager_hooks);
00270    return;
00271 }

void astman_append ( struct mansession *  s,
const char *  fmt,
  ... 
)

utility functions for creating AMI replies

Definition at line 983 of file manager.c.

References ast_str_buffer(), ast_str_set_va(), ast_str_thread_get(), ast_verbose, astman_append_buf, ASTMAN_APPEND_BUF_INITSIZE, buf, mansession_session::f, mansession::f, send_string(), and mansession::session.

Referenced by __iax2_show_peers(), _sip_show_peer(), _sip_show_peers(), _skinny_show_device(), _skinny_show_devices(), _skinny_show_line(), _skinny_show_lines(), action_agents(), action_challenge(), action_command(), action_coresettings(), action_coreshowchannels(), action_corestatus(), action_dahdishowchannels(), action_events(), action_extensionstate(), action_getconfig(), action_getconfigjson(), action_getvar(), action_listcategories(), action_listcommands(), action_mailboxcount(), action_mailboxstatus(), action_meetmelist(), action_ping(), action_status(), action_waitevent(), ast_cli_netstats(), astman_send_response_full(), do_print(), manager_dbget(), manager_iax2_show_netstats(), manager_iax2_show_peer_list(), manager_iax2_show_registry(), manager_jabber_send(), manager_list_voicemail_users(), manager_modulecheck(), manager_parking_status(), manager_queue_rule_show(), manager_queues_show(), manager_queues_status(), manager_queues_summary(), manager_rpt_local_nodes(), manager_rpt_status(), manager_show_dialplan(), manager_show_dialplan_helper(), manager_show_registry(), manager_sip_qualify_peer(), manager_sip_show_peer(), manager_sip_show_peers(), manager_skinny_show_device(), manager_skinny_show_devices(), manager_skinny_show_line(), manager_skinny_show_lines(), rpt_manager_do_stats(), rpt_manager_success(), and session_do().

00984 {
00985    va_list ap;
00986    struct ast_str *buf;
00987 
00988    if (!(buf = ast_str_thread_get(&astman_append_buf, ASTMAN_APPEND_BUF_INITSIZE)))
00989       return;
00990 
00991    va_start(ap, fmt);
00992    ast_str_set_va(&buf, 0, fmt, ap);
00993    va_end(ap);
00994 
00995    if (s->f != NULL || s->session->f != NULL) {
00996       send_string(s, ast_str_buffer(buf));
00997    } else {
00998       ast_verbose("fd == -1 in astman_append, should not happen\n");
00999    }
01000 }

const char* astman_get_header ( const struct message *  m,
char *  var 
)

Get header from mananger transaction.

Definition at line 911 of file manager.c.

References __astman_get_header(), and GET_HEADER_FIRST_MATCH.

Referenced by _sip_show_peer(), _sip_show_peers(), _skinny_show_devices(), _skinny_show_lines(), action_add_agi_cmd(), action_agent_logoff(), action_agents(), action_atxfer(), action_bridge(), action_challenge(), action_command(), action_coresettings(), action_coreshowchannels(), action_corestatus(), action_createconfig(), action_dahdidialoffhook(), action_dahdidndoff(), action_dahdidndon(), action_dahdishowchannels(), action_events(), action_extensionstate(), action_getconfig(), action_getconfigjson(), action_getvar(), action_hangup(), action_listcategories(), action_mailboxcount(), action_mailboxstatus(), action_meetmelist(), action_originate(), action_ping(), action_redirect(), action_reload(), action_sendtext(), action_setvar(), action_status(), action_timeout(), action_transfer(), action_transferhangup(), action_updateconfig(), action_userevent(), action_waitevent(), astman_send_response_full(), authenticate(), change_monitor_action(), do_pause_or_unpause(), handle_updates(), manager_add_queue_member(), manager_dbdel(), manager_dbdeltree(), manager_dbget(), manager_dbput(), manager_iax2_show_peer_list(), manager_iax2_show_peers(), manager_iax2_show_registry(), manager_jabber_send(), manager_list_voicemail_users(), manager_modulecheck(), manager_moduleload(), manager_park(), manager_parking_status(), manager_pause_queue_member(), manager_play_dtmf(), manager_queue_log_custom(), manager_queue_member_penalty(), manager_queue_reload(), manager_queue_reset(), manager_queue_rule_show(), manager_queues_status(), manager_queues_summary(), manager_remove_queue_member(), manager_rpt_status(), manager_show_dialplan(), manager_show_registry(), manager_sip_qualify_peer(), manager_sip_show_peer(), manager_sip_show_peers(), manager_sipnotify(), manager_skinny_show_device(), manager_skinny_show_devices(), manager_skinny_show_line(), manager_skinny_show_lines(), meetmemute(), process_message(), rpt_manager_do_stats(), rpt_manager_success(), start_monitor_action(), and stop_monitor_action().

00912 {
00913    return __astman_get_header(m, var, GET_HEADER_FIRST_MATCH);
00914 }

struct ast_variable* astman_get_variables ( const struct message *  m  )  [read]

Get a linked list of the Variable: headers.

Definition at line 917 of file manager.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), ast_variable_new(), message::hdrcount, message::headers, parse(), strsep(), and var.

Referenced by action_originate(), and manager_sipnotify().

00918 {
00919    int varlen, x, y;
00920    struct ast_variable *head = NULL, *cur;
00921 
00922    AST_DECLARE_APP_ARGS(args,
00923       AST_APP_ARG(vars)[32];
00924    );
00925 
00926    varlen = strlen("Variable: ");
00927 
00928    for (x = 0; x < m->hdrcount; x++) {
00929       char *parse, *var, *val;
00930 
00931       if (strncasecmp("Variable: ", m->headers[x], varlen))
00932          continue;
00933       parse = ast_strdupa(m->headers[x] + varlen);
00934 
00935       AST_STANDARD_APP_ARGS(args, parse);
00936       if (!args.argc)
00937          continue;
00938       for (y = 0; y < args.argc; y++) {
00939          if (!args.vars[y])
00940             continue;
00941          var = val = ast_strdupa(args.vars[y]);
00942          strsep(&val, "=");
00943          if (!val || ast_strlen_zero(var))
00944             continue;
00945          cur = ast_variable_new(var, val, "");
00946          cur->next = head;
00947          head = cur;
00948       }
00949    }
00950 
00951    return head;
00952 }

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 
)

Send error in manager transaction.

Definition at line 1041 of file manager.c.

References astman_send_response_full().

Referenced by _sip_qualify_peer(), _sip_show_peer(), action_add_agi_cmd(), action_agent_logoff(), action_atxfer(), action_bridge(), action_challenge(), action_command(), action_createconfig(), action_dahdidialoffhook(), action_dahdidndoff(), action_dahdidndon(), action_dahdirestart(), action_events(), action_extensionstate(), action_getconfig(), action_getconfigjson(), action_getvar(), action_hangup(), action_listcategories(), action_login(), action_mailboxcount(), action_mailboxstatus(), action_meetmelist(), action_originate(), action_redirect(), action_reload(), action_sendtext(), action_setvar(), action_status(), action_timeout(), action_transfer(), action_transferhangup(), action_updateconfig(), change_monitor_action(), do_pause_or_unpause(), manager_add_queue_member(), manager_dbdel(), manager_dbdeltree(), manager_dbget(), manager_dbput(), manager_jabber_send(), manager_modulecheck(), manager_moduleload(), manager_park(), manager_pause_queue_member(), manager_play_dtmf(), manager_queue_log_custom(), manager_queue_member_penalty(), manager_queue_reload(), manager_queue_reset(), manager_remove_queue_member(), manager_rpt_status(), manager_show_dialplan(), manager_show_dialplan_helper(), manager_sip_qualify_peer(), manager_sip_show_peer(), manager_sipnotify(), manager_skinny_show_device(), manager_skinny_show_line(), meetmemute(), process_message(), rpt_manager_do_stats(), start_monitor_action(), and stop_monitor_action().

01042 {
01043    astman_send_response_full(s, m, "Error", error, NULL);
01044 }

void astman_send_listack ( struct mansession *  s,
const struct message *  m,
char *  msg,
char *  listflag 
)

void astman_send_response ( struct mansession *  s,
const struct message *  m,
char *  resp,
char *  msg 
)

Send response in manager transaction.

Definition at line 1036 of file manager.c.

References astman_send_response_full().

Referenced by action_logoff(), and action_waitevent().

01037 {
01038    astman_send_response_full(s, m, resp, msg, NULL);
01039 }

static void astman_send_response_full ( struct mansession *  s,
const struct message *  m,
char *  resp,
char *  msg,
char *  listflag 
) [static]

Definition at line 1019 of file manager.c.

References ast_strlen_zero(), astman_append(), astman_get_header(), and MSG_MOREDATA.

Referenced by astman_send_ack(), astman_send_error(), astman_send_listack(), astman_send_response(), and astman_start_ack().

01020 {
01021    const char *id = astman_get_header(m, "ActionID");
01022 
01023    astman_append(s, "Response: %s\r\n", resp);
01024    if (!ast_strlen_zero(id))
01025       astman_append(s, "ActionID: %s\r\n", id);
01026    if (listflag)
01027       astman_append(s, "EventList: %s\r\n", listflag);   /* Start, complete, cancelled */
01028    if (msg == MSG_MOREDATA)
01029       return;
01030    else if (msg)
01031       astman_append(s, "Message: %s\r\n\r\n", msg);
01032    else
01033       astman_append(s, "\r\n");
01034 }

static void astman_start_ack ( struct mansession *  s,
const struct message *  m 
) [static]

static int authenticate ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1085 of file manager.c.

References ast_apply_ha(), ast_copy_string(), ast_debug, ast_inet_ntoa(), ast_log(), AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_strlen_zero(), astman_get_header(), mansession_session::challenge, get_manager_by_name_locked(), ast_manager_user::ha, len(), LOG_NOTICE, MD5Final(), MD5Init(), MD5Update(), ast_manager_user::readperm, mansession_session::readperm, S_OR, ast_manager_user::secret, mansession::session, mansession_session::sessionstart, set_eventmask(), mansession_session::sin, mansession_session::username, ast_manager_user::writeperm, mansession_session::writeperm, ast_manager_user::writetimeout, and mansession_session::writetimeout.

Referenced by action_login().

01086 {
01087    const char *username = astman_get_header(m, "Username");
01088    const char *password = astman_get_header(m, "Secret");
01089    int error = -1;
01090    struct ast_manager_user *user = NULL;
01091 
01092    if (ast_strlen_zero(username))   /* missing username */
01093       return -1;
01094 
01095    /* locate user in locked state */
01096    AST_RWLIST_WRLOCK(&users);
01097 
01098    if (!(user = get_manager_by_name_locked(username))) {
01099       ast_log(LOG_NOTICE, "%s tried to authenticate with nonexistent user '%s'\n", ast_inet_ntoa(s->session->sin.sin_addr), username);
01100    } else if (user->ha && !ast_apply_ha(user->ha, &(s->session->sin))) {
01101       ast_log(LOG_NOTICE, "%s failed to pass IP ACL as '%s'\n", ast_inet_ntoa(s->session->sin.sin_addr), username);
01102    } else if (!strcasecmp(astman_get_header(m, "AuthType"), "MD5")) {
01103       const char *key = astman_get_header(m, "Key");
01104       if (!ast_strlen_zero(key) && !ast_strlen_zero(s->session->challenge) && user->secret) {
01105          int x;
01106          int len = 0;
01107          char md5key[256] = "";
01108          struct MD5Context md5;
01109          unsigned char digest[16];
01110 
01111          MD5Init(&md5);
01112          MD5Update(&md5, (unsigned char *) s->session->challenge, strlen(s->session->challenge));
01113          MD5Update(&md5, (unsigned char *) user->secret, strlen(user->secret));
01114          MD5Final(digest, &md5);
01115          for (x = 0; x < 16; x++)
01116             len += sprintf(md5key + len, "%2.2x", digest[x]);
01117          if (!strcmp(md5key, key))
01118             error = 0;
01119       } else {
01120          ast_debug(1, "MD5 authentication is not possible.  challenge: '%s'\n", 
01121             S_OR(s->session->challenge, ""));
01122       }
01123    } else if (password && user->secret && !strcmp(password, user->secret))
01124       error = 0;
01125 
01126    if (error) {
01127       ast_log(LOG_NOTICE, "%s failed to authenticate as '%s'\n", ast_inet_ntoa(s->session->sin.sin_addr), username);
01128       AST_RWLIST_UNLOCK(&users);
01129       return -1;
01130    }
01131 
01132    /* auth complete */
01133    
01134    ast_copy_string(s->session->username, username, sizeof(s->session->username));
01135    s->session->readperm = user->readperm;
01136    s->session->writeperm = user->writeperm;
01137    s->session->writetimeout = user->writetimeout;
01138    s->session->sessionstart = time(NULL);
01139    set_eventmask(s, astman_get_header(m, "Events"));
01140    
01141    AST_RWLIST_UNLOCK(&users);
01142    return 0;
01143 }

static char* authority_to_str ( int  authority,
struct ast_str **  res 
) [static]

Convert authority code to a list of options.

Definition at line 405 of file manager.c.

References ARRAY_LEN, ast_str_append(), ast_str_buffer(), ast_str_reset(), ast_str_strlen(), num, and perms.

Referenced by __manager_event(), action_listcommands(), handle_showmanager(), handle_showmancmd(), and handle_showmancmds().

00406 {
00407    int i;
00408    char *sep = "";
00409 
00410    ast_str_reset(*res);
00411    for (i = 0; i < ARRAY_LEN(perms) - 1; i++) {
00412       if (authority & perms[i].num) {
00413          ast_str_append(res, 0, "%s%s", sep, perms[i].label);
00414          sep = ",";
00415       }
00416    }
00417 
00418    if (ast_str_strlen(*res) == 0)   /* replace empty string with something sensible */
00419       ast_str_append(res, 0, "<none>");
00420 
00421    return ast_str_buffer(*res);
00422 }

static int check_blacklist ( const char *  cmd  )  [static]

Definition at line 2254 of file manager.c.

References ARRAY_LEN, ast_strdupa, ast_strip(), ast_strlen_zero(), command_blacklist, match(), MAX_BLACKLIST_CMD_LEN, strsep(), and words.

Referenced by action_command().

02255 {
02256    char *cmd_copy, *cur_cmd;
02257    char *cmd_words[MAX_BLACKLIST_CMD_LEN] = { NULL, };
02258    int i;
02259 
02260    cmd_copy = ast_strdupa(cmd);
02261    for (i = 0; i < MAX_BLACKLIST_CMD_LEN && (cur_cmd = strsep(&cmd_copy, " ")); i++) {
02262       cur_cmd = ast_strip(cur_cmd);
02263       if (ast_strlen_zero(cur_cmd)) {
02264          i--;
02265          continue;
02266       }
02267 
02268       cmd_words[i] = cur_cmd;
02269    }
02270 
02271    for (i = 0; i < ARRAY_LEN(command_blacklist); i++) {
02272       int j, match = 1;
02273 
02274       for (j = 0; command_blacklist[i].words[j]; j++) {
02275          if (ast_strlen_zero(cmd_words[j]) || strcasecmp(cmd_words[j], command_blacklist[i].words[j])) {
02276             match = 0;
02277             break;
02278          }
02279       }
02280 
02281       if (match) {
02282          return 1;
02283       }
02284    }
02285 
02286    return 0;
02287 }

int check_manager_enabled ( void   ) 

Event list management functions. We assume that the event list always has at least one element, and the delete code will not remove the last entry even if the.

Check if AMI is enabled.

Definition at line 316 of file manager.c.

Referenced by handle_show_settings().

00317 {
00318    return manager_enabled;
00319 }

static int check_manager_session_inuse ( const char *  name  )  [static]

Definition at line 488 of file manager.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, mansession_session::list, and mansession_session::username.

Referenced by process_message().

00489 {
00490    struct mansession_session *session = NULL;
00491 
00492    AST_LIST_LOCK(&sessions);
00493    AST_LIST_TRAVERSE(&sessions, session, list) {
00494       if (!strcasecmp(session->username, name)) 
00495          break;
00496    }
00497    AST_LIST_UNLOCK(&sessions);
00498 
00499    return session ? 1 : 0;
00500 }

int check_webmanager_enabled ( void   ) 

Check if AMI/HTTP is enabled.

Definition at line 321 of file manager.c.

Referenced by action_coresettings(), and handle_show_settings().

00322 {
00323    return (webmanager_enabled && manager_enabled);
00324 }

static void destroy_session ( struct mansession_session *  session  )  [static]

Definition at line 866 of file manager.c.

References ast_atomic_fetchadd_int(), AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, and free_session().

Referenced by generic_http_callback(), and session_do().

00867 {
00868    AST_LIST_LOCK(&sessions);
00869    AST_LIST_REMOVE(&sessions, session, list);
00870    ast_atomic_fetchadd_int(&num_sessions, -1);
00871    free_session(session);
00872    AST_LIST_UNLOCK(&sessions);
00873 }

static int do_message ( struct mansession *  s  )  [static]

Definition at line 3174 of file manager.c.

References AST_MAX_MANHEADERS, ast_strdupa, ast_strlen_zero(), get_input(), message::hdrcount, message::headers, mansession_session::inbuf, process_events(), process_message(), and mansession::session.

Referenced by session_do().

03175 {
03176    struct message m = { 0 };
03177    char header_buf[sizeof(s->session->inbuf)] = { '\0' };
03178    int res;
03179 
03180    for (;;) {
03181       /* Check if any events are pending and do them if needed */
03182       if (process_events(s))
03183          return -1;
03184       res = get_input(s, header_buf);
03185       if (res == 0) {
03186          continue;
03187       } else if (res > 0) {
03188          if (ast_strlen_zero(header_buf))
03189             return process_message(s, &m) ? -1 : 0;
03190          else if (m.hdrcount < (AST_MAX_MANHEADERS - 1))
03191             m.headers[m.hdrcount++] = ast_strdupa(header_buf);
03192       } else {
03193          return res;
03194       }
03195    }
03196 }

static void* fast_originate ( void *  data  )  [static]

Definition at line 2367 of file manager.c.

References fast_originate_helper::account, fast_originate_helper::app, fast_originate_helper::appdata, AST_CHANNEL_NAME, ast_channel_unlock, ast_free, ast_pbx_outgoing_app(), ast_pbx_outgoing_exten(), ast_strlen_zero(), chan, fast_originate_helper::cid_name, fast_originate_helper::cid_num, fast_originate_helper::context, fast_originate_helper::data, EVENT_FLAG_CALL, fast_originate_helper::exten, fast_originate_helper::format, fast_originate_helper::idtext, manager_event, ast_channel::name, fast_originate_helper::priority, S_OR, fast_originate_helper::tech, fast_originate_helper::timeout, ast_channel::uniqueid, and fast_originate_helper::vars.

Referenced by action_originate().

02368 {
02369    struct fast_originate_helper *in = data;
02370    int res;
02371    int reason = 0;
02372    struct ast_channel *chan = NULL;
02373    char requested_channel[AST_CHANNEL_NAME];
02374 
02375    if (!ast_strlen_zero(in->app)) {
02376       res = ast_pbx_outgoing_app(in->tech, in->format, in->data, in->timeout, in->app, in->appdata, &reason, 1,
02377          S_OR(in->cid_num, NULL),
02378          S_OR(in->cid_name, NULL),
02379          in->vars, in->account, &chan);
02380    } else {
02381       res = ast_pbx_outgoing_exten(in->tech, in->format, in->data, in->timeout, in->context, in->exten, in->priority, &reason, 1,
02382          S_OR(in->cid_num, NULL),
02383          S_OR(in->cid_name, NULL),
02384          in->vars, in->account, &chan);
02385    }
02386 
02387    if (!chan)
02388       snprintf(requested_channel, AST_CHANNEL_NAME, "%s/%s", in->tech, in->data);   
02389    /* Tell the manager what happened with the channel */
02390    manager_event(EVENT_FLAG_CALL, "OriginateResponse",
02391       "%s%s"
02392       "Response: %s\r\n"
02393       "Channel: %s\r\n"
02394       "Context: %s\r\n"
02395       "Exten: %s\r\n"
02396       "Reason: %d\r\n"
02397       "Uniqueid: %s\r\n"
02398       "CallerIDNum: %s\r\n"
02399       "CallerIDName: %s\r\n",
02400       in->idtext, ast_strlen_zero(in->idtext) ? "" : "\r\n", res ? "Failure" : "Success", 
02401       chan ? chan->name : requested_channel, in->context, in->exten, reason, 
02402       chan ? chan->uniqueid : "<null>",
02403       S_OR(in->cid_num, "<unknown>"),
02404       S_OR(in->cid_name, "<unknown>")
02405       );
02406 
02407    /* Locked by ast_pbx_outgoing_exten or ast_pbx_outgoing_app */
02408    if (chan)
02409       ast_channel_unlock(chan);
02410    ast_free(in);
02411    return NULL;
02412 }

static void free_session ( struct mansession_session *  session  )  [static]

Definition at line 846 of file manager.c.

References mansession_session::__lock, ast_atomic_fetchadd_int(), ast_datastore_free(), ast_free, AST_LIST_REMOVE_HEAD, ast_mutex_destroy(), mansession_session::datastores, ast_datastore::entry, mansession_session::f, mansession_session::last_ev, and eventqent::usecount.

Referenced by destroy_session(), and purge_sessions().

00847 {
00848    struct eventqent *eqe = session->last_ev;
00849    struct ast_datastore *datastore;
00850 
00851    /* Get rid of each of the data stores on the session */
00852    while ((datastore = AST_LIST_REMOVE_HEAD(&session->datastores, entry))) {
00853       /* Free the data store */
00854       ast_datastore_free(datastore);
00855    }
00856 
00857    if (session->f != NULL)
00858       fclose(session->f);
00859    ast_mutex_destroy(&session->__lock);
00860    ast_free(session);
00861    if (eqe) {
00862       ast_atomic_fetchadd_int(&eqe->usecount, -1);
00863    }
00864 }

static int get_input ( struct mansession *  s,
char *  output 
) [static]

Read one full line (including crlf) from the manager socket.

Note:
 * \r\n is the only valid terminator for the line.
 * (Note that, later, '\0' will be considered as the end-of-line marker,
 * so everything between the '\0' and the '\r\n' will not be used).
 * Also note that we assume output to have at least "maxlen" space.
 * 

Definition at line 3104 of file manager.c.

References mansession_session::__lock, ast_inet_ntoa(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), AST_PTHREADT_NULL, ast_wait_for_input(), errno, mansession_session::f, mansession_session::fd, mansession_session::inbuf, mansession_session::inlen, LOG_WARNING, mansession_session::pending_event, mansession::session, mansession_session::sin, ast_frame::src, and mansession_session::waiting_thread.

Referenced by do_message().

03105 {
03106    int res, x;
03107    int maxlen = sizeof(s->session->inbuf) - 1;
03108    char *src = s->session->inbuf;
03109 
03110    /*
03111     * Look for \r\n within the buffer. If found, copy to the output
03112     * buffer and return, trimming the \r\n (not used afterwards).
03113     */
03114    for (x = 0; x < s->session->inlen; x++) {
03115       int cr;  /* set if we have \r */
03116       if (src[x] == '\r' && x+1 < s->session->inlen && src[x+1] == '\n')
03117          cr = 2;  /* Found. Update length to include \r\n */
03118       else if (src[x] == '\n')
03119          cr = 1;  /* also accept \n only */
03120       else
03121          continue;
03122       memmove(output, src, x);   /*... but trim \r\n */
03123       output[x] = '\0';    /* terminate the string */
03124       x += cr;       /* number of bytes used */
03125       s->session->inlen -= x;       /* remaining size */
03126       memmove(src, src + x, s->session->inlen); /* remove used bytes */
03127       return 1;
03128    }
03129    if (s->session->inlen >= maxlen) {
03130       /* no crlf found, and buffer full - sorry, too long for us */
03131       ast_log(LOG_WARNING, "Dumping long line with no return from %s: %s\n", ast_inet_ntoa(s->session->sin.sin_addr), src);
03132       s->session->inlen = 0;
03133    }
03134    res = 0;
03135    while (res == 0) {
03136       /* XXX do we really need this locking ? */
03137       ast_mutex_lock(&s->session->__lock);
03138       if (s->session->pending_event) {
03139          s->session->pending_event = 0;
03140          ast_mutex_unlock(&s->session->__lock);
03141          return 0;
03142       }
03143       s->session->waiting_thread = pthread_self();
03144       ast_mutex_unlock(&s->session->__lock);
03145 
03146       res = ast_wait_for_input(s->session->fd, -1);   /* return 0 on timeout ? */
03147 
03148       ast_mutex_lock(&s->session->__lock);
03149       s->session->waiting_thread = AST_PTHREADT_NULL;
03150       ast_mutex_unlock(&s->session->__lock);
03151    }
03152    if (res < 0) {
03153       /* If we get a signal from some other thread (typically because
03154        * there are new events queued), return 0 to notify the caller.
03155        */
03156       if (errno == EINTR || errno == EAGAIN)
03157          return 0;
03158       ast_log(LOG_WARNING, "poll() returned error: %s\n", strerror(errno));
03159       return -1;
03160    }
03161    ast_mutex_lock(&s->session->__lock);
03162    res = fread(src + s->session->inlen, 1, maxlen - s->session->inlen, s->session->f);
03163    if (res < 1)
03164       res = -1;   /* error return */
03165    else {
03166       s->session->inlen += res;
03167       src[s->session->inlen] = '\0';
03168       res = 0;
03169    }
03170    ast_mutex_unlock(&s->session->__lock);
03171    return res;
03172 }

static struct ast_manager_user* get_manager_by_name_locked ( const char *  name  )  [static, read]

lookup an entry in the list of registered users. must be called with the list lock held.

Definition at line 507 of file manager.c.

References AST_RWLIST_TRAVERSE, ast_manager_user::list, and ast_manager_user::username.

Referenced by __init_manager(), authenticate(), handle_showmanager(), and manager_displayconnects().

00508 {
00509    struct ast_manager_user *user = NULL;
00510 
00511    AST_RWLIST_TRAVERSE(&users, user, list)
00512       if (!strcasecmp(user->username, name))
00513          break;
00514    return user;
00515 }

static int get_perm ( const char *  instr  )  [static]

Definition at line 446 of file manager.c.

References ARRAY_LEN, ast_instring(), num, and perms.

Referenced by __init_manager(), and strings_to_mask().

00447 {
00448    int x = 0, ret = 0;
00449 
00450    if (!instr)
00451       return 0;
00452 
00453    for (x = 0; x < ARRAY_LEN(perms); x++) {
00454       if (ast_instring(instr, perms[x].label, ','))
00455          ret |= perms[x].num;
00456    }
00457 
00458    return ret;
00459 }

static struct eventqent* grab_last ( void   )  [static, read]

Grab a reference to the last event, update usecount as needed. Can handle a NULL pointer.

Definition at line 330 of file manager.c.

References ast_atomic_fetchadd_int(), AST_RWLIST_LAST, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, and eventqent::usecount.

Referenced by generic_http_callback(), and session_do().

00331 {
00332    struct eventqent *ret;
00333 
00334    AST_RWLIST_RDLOCK(&all_events);
00335    ret = AST_RWLIST_LAST(&all_events);
00336    /* the list is never empty now, but may become so when
00337     * we optimize it in the future, so be prepared.
00338     */
00339    if (ret) {
00340       ast_atomic_fetchadd_int(&ret->usecount, 1);
00341    }
00342    AST_RWLIST_UNLOCK(&all_events);
00343    return ret;
00344 }

static char* handle_manager_reload ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

CLI command manager reload.

Definition at line 800 of file manager.c.

References ast_cli_args::argc, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, reload_manager(), and ast_cli_entry::usage.

00801 {
00802    switch (cmd) {
00803    case CLI_INIT:
00804       e->command = "manager reload";
00805       e->usage =
00806          "Usage: manager reload\n"
00807          "       Reloads the manager configuration.\n";
00808       return NULL;
00809    case CLI_GENERATE:
00810       return NULL;
00811    }
00812    if (a->argc > 2)
00813       return CLI_SHOWUSAGE;
00814    reload_manager();
00815    return CLI_SUCCESS;
00816 }

static char* handle_mandebug ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 581 of file manager.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, and ast_cli_entry::usage.

00582 {
00583    switch (cmd) {
00584    case CLI_INIT:
00585       e->command = "manager set debug [on|off]";
00586       e->usage = "Usage: manager set debug [on|off]\n Show, enable, disable debugging of the manager code.\n";
00587       return NULL;
00588    case CLI_GENERATE:
00589       return NULL;   
00590    }
00591    if (a->argc == 3)
00592       ast_cli(a->fd, "manager debug is %s\n", manager_debug? "on" : "off");
00593    else if (a->argc == 4) {
00594       if (!strcasecmp(a->argv[3], "on"))
00595          manager_debug = 1;
00596       else if (!strcasecmp(a->argv[3], "off"))
00597          manager_debug = 0;
00598       else
00599          return CLI_SHOWUSAGE;
00600    }
00601    return CLI_SUCCESS;
00602 }

static char* handle_showmanager ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 604 of file manager.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_str_alloca, ast_strdup, authority_to_str(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_manager_user::displayconnects, ast_cli_args::fd, get_manager_by_name_locked(), ast_manager_user::ha, ast_cli_args::n, ast_cli_args::pos, ast_manager_user::readperm, ast_manager_user::secret, ast_cli_entry::usage, ast_manager_user::username, ast_cli_args::word, and ast_manager_user::writeperm.

00605 {
00606    struct ast_manager_user *user = NULL;
00607    int l, which;
00608    char *ret = NULL;
00609    struct ast_str *rauthority = ast_str_alloca(128);
00610    struct ast_str *wauthority = ast_str_alloca(128);
00611 
00612    switch (cmd) {
00613    case CLI_INIT:
00614       e->command = "manager show user";
00615       e->usage = 
00616          " Usage: manager show user <user>\n"
00617          "        Display all information related to the manager user specified.\n";
00618       return NULL;
00619    case CLI_GENERATE:
00620       l = strlen(a->word);
00621       which = 0;
00622       if (a->pos != 3)
00623          return NULL;
00624       AST_RWLIST_RDLOCK(&users);
00625       AST_RWLIST_TRAVERSE(&users, user, list) {
00626          if ( !strncasecmp(a->word, user->username, l) && ++which > a->n ) {
00627             ret = ast_strdup(user->username);
00628             break;
00629          }
00630       }
00631       AST_RWLIST_UNLOCK(&users);
00632       return ret;
00633    }
00634 
00635    if (a->argc != 4)
00636       return CLI_SHOWUSAGE;
00637 
00638    AST_RWLIST_RDLOCK(&users);
00639 
00640    if (!(user = get_manager_by_name_locked(a->argv[3]))) {
00641       ast_cli(a->fd, "There is no manager called %s\n", a->argv[3]);
00642       AST_RWLIST_UNLOCK(&users);
00643       return CLI_SUCCESS;
00644    }
00645 
00646    ast_cli(a->fd, "\n");
00647    ast_cli(a->fd,
00648       "       username: %s\n"
00649       "         secret: %s\n"
00650       "            acl: %s\n"
00651       "      read perm: %s\n"
00652       "     write perm: %s\n"
00653       "displayconnects: %s\n",
00654       (user->username ? user->username : "(N/A)"),
00655       (user->secret ? "<Set>" : "(N/A)"),
00656       (user->ha ? "yes" : "no"),
00657       authority_to_str(user->readperm, &rauthority),
00658       authority_to_str(user->writeperm, &wauthority),
00659       (user->displayconnects ? "yes" : "no"));
00660 
00661    AST_RWLIST_UNLOCK(&users);
00662 
00663    return CLI_SUCCESS;
00664 }

static char* handle_showmanagers ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 667 of file manager.c.

References ast_cli_args::argc, ast_cli(), AST_RWLIST_EMPTY, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, ast_manager_user::list, ast_cli_entry::usage, and ast_manager_user::username.

00668 {
00669    struct ast_manager_user *user = NULL;
00670    int count_amu = 0;
00671    switch (cmd) {
00672    case CLI_INIT:
00673       e->command = "manager show users";
00674       e->usage = 
00675          "Usage: manager show users\n"
00676          "       Prints a listing of all managers that are currently configured on that\n"
00677          " system.\n";
00678       return NULL;
00679    case CLI_GENERATE:
00680       return NULL;
00681    }
00682    if (a->argc != 3)
00683       return CLI_SHOWUSAGE;
00684 
00685    AST_RWLIST_RDLOCK(&users);
00686 
00687    /* If there are no users, print out something along those lines */
00688    if (AST_RWLIST_EMPTY(&users)) {
00689       ast_cli(a->fd, "There are no manager users.\n");
00690       AST_RWLIST_UNLOCK(&users);
00691       return CLI_SUCCESS;
00692    }
00693 
00694    ast_cli(a->fd, "\nusername\n--------\n");
00695 
00696    AST_RWLIST_TRAVERSE(&users, user, list) {
00697       ast_cli(a->fd, "%s\n", user->username);
00698       count_amu++;
00699    }
00700 
00701    AST_RWLIST_UNLOCK(&users);
00702 
00703    ast_cli(a->fd, "-------------------\n");
00704    ast_cli(a->fd, "%d manager users configured.\n", count_amu);
00705 
00706    return CLI_SUCCESS;
00707 }

static char* handle_showmancmd ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 534 of file manager.c.

References manager_action::action, ast_cli_args::argc, ast_cli_args::argv, ast_cli(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_str_alloca, ast_strdup, manager_action::authority, authority_to_str(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, manager_action::description, ast_cli_args::fd, ast_cli_args::n, num, S_OR, manager_action::synopsis, ast_cli_entry::usage, and ast_cli_args::word.

00535 {
00536    struct manager_action *cur;
00537    struct ast_str *authority;
00538    int num, l, which;
00539    char *ret = NULL;
00540    switch (cmd) {
00541    case CLI_INIT:
00542       e->command = "manager show command";
00543       e->usage = 
00544          "Usage: manager show command <actionname> [<actionname> [<actionname> [...]]]\n"
00545          "  Shows the detailed description for a specific Asterisk manager interface command.\n";
00546       return NULL;
00547    case CLI_GENERATE:
00548       l = strlen(a->word);
00549       which = 0;
00550       AST_RWLIST_RDLOCK(&actions);
00551       AST_RWLIST_TRAVERSE(&actions, cur, list) {
00552          if (!strncasecmp(a->word, cur->action, l) && ++which > a->n) {
00553             ret = ast_strdup(cur->action);
00554             break;   /* make sure we exit even if ast_strdup() returns NULL */
00555          }
00556       }
00557       AST_RWLIST_UNLOCK(&actions);
00558       return ret;
00559    }
00560    authority = ast_str_alloca(80);
00561    if (a->argc < 4) {
00562       return CLI_SHOWUSAGE;
00563    }
00564 
00565    AST_RWLIST_RDLOCK(&actions);
00566    AST_RWLIST_TRAVERSE(&actions, cur, list) {
00567       for (num = 3; num < a->argc; num++) {
00568          if (!strcasecmp(cur->action, a->argv[num])) {
00569             ast_cli(a->fd, "Action: %s\nSynopsis: %s\nPrivilege: %s\n%s\n",
00570                cur->action, cur->synopsis,
00571                authority_to_str(cur->authority, &authority),
00572                S_OR(cur->description, ""));
00573          }
00574       }
00575    }
00576    AST_RWLIST_UNLOCK(&actions);
00577 
00578    return CLI_SUCCESS;
00579 }

static char* handle_showmancmds ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

CLI command manager list commands.

Definition at line 711 of file manager.c.

References manager_action::action, ast_cli(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_str_alloca, manager_action::authority, authority_to_str(), CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, HSMC_FORMAT, manager_action::synopsis, and ast_cli_entry::usage.

00712 {
00713    struct manager_action *cur;
00714    struct ast_str *authority;
00715 #define HSMC_FORMAT "  %-15.15s  %-15.15s  %-55.55s\n"
00716    switch (cmd) {
00717    case CLI_INIT:
00718       e->command = "manager show commands";
00719       e->usage = 
00720          "Usage: manager show commands\n"
00721          "  Prints a listing of all the available Asterisk manager interface commands.\n";
00722       return NULL;
00723    case CLI_GENERATE:
00724       return NULL;   
00725    }  
00726    authority = ast_str_alloca(80);
00727    ast_cli(a->fd, HSMC_FORMAT, "Action", "Privilege", "Synopsis");
00728    ast_cli(a->fd, HSMC_FORMAT, "------", "---------", "--------");
00729 
00730    AST_RWLIST_RDLOCK(&actions);
00731    AST_RWLIST_TRAVERSE(&actions, cur, list)
00732       ast_cli(a->fd, HSMC_FORMAT, cur->action, authority_to_str(cur->authority, &authority), cur->synopsis);
00733    AST_RWLIST_UNLOCK(&actions);
00734 
00735    return CLI_SUCCESS;
00736 }

static char* handle_showmanconn ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

CLI command manager list connected.

Definition at line 739 of file manager.c.

References ast_cli(), ast_inet_ntoa(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, mansession_session::fd, ast_cli_args::fd, HSMCONN_FORMAT1, HSMCONN_FORMAT2, mansession_session::inuse, mansession_session::list, mansession_session::readperm, mansession_session::sessionstart, mansession_session::sin, ast_cli_entry::usage, mansession_session::username, and mansession_session::writeperm.

00740 {
00741    struct mansession_session *session;
00742    time_t now = time(NULL);
00743 #define HSMCONN_FORMAT1 "  %-15.15s  %-15.15s  %-10.10s  %-10.10s  %-8.8s  %-8.8s  %-5.5s  %-5.5s\n"
00744 #define HSMCONN_FORMAT2 "  %-15.15s  %-15.15s  %-10d  %-10d  %-8d  %-8d  %-5.5d  %-5.5d\n"
00745    int count = 0;
00746    switch (cmd) {
00747    case CLI_INIT:
00748       e->command = "manager show connected";
00749       e->usage = 
00750          "Usage: manager show connected\n"
00751          "  Prints a listing of the users that are currently connected to the\n"
00752          "Asterisk manager interface.\n";
00753       return NULL;
00754    case CLI_GENERATE:
00755       return NULL;   
00756    }
00757 
00758    ast_cli(a->fd, HSMCONN_FORMAT1, "Username", "IP Address", "Start", "Elapsed", "FileDes", "HttpCnt", "Read", "Write");
00759 
00760    AST_LIST_LOCK(&sessions);
00761    AST_LIST_TRAVERSE(&sessions, session, list) {
00762       ast_cli(a->fd, HSMCONN_FORMAT2, session->username, ast_inet_ntoa(session->sin.sin_addr), (int)(session->sessionstart), (int)(now - session->sessionstart), session->fd, session->inuse, session->readperm, session->writeperm);
00763       count++;
00764    }
00765    AST_LIST_UNLOCK(&sessions);
00766 
00767    ast_cli(a->fd, "%d users connected.\n", count);
00768 
00769    return CLI_SUCCESS;
00770 }

static char* handle_showmaneventq ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

CLI command manager list eventq.

Definition at line 774 of file manager.c.

References ast_cli(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, eventqent::category, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, eventqent::eventdata, ast_cli_args::fd, s, ast_cli_entry::usage, and eventqent::usecount.

00775 {
00776    struct eventqent *s;
00777    switch (cmd) {
00778    case CLI_INIT:
00779       e->command = "manager show eventq";
00780       e->usage = 
00781          "Usage: manager show eventq\n"
00782          "  Prints a listing of all events pending in the Asterisk manger\n"
00783          "event queue.\n";
00784       return NULL;
00785    case CLI_GENERATE:
00786       return NULL;
00787    }
00788    AST_RWLIST_RDLOCK(&all_events);
00789    AST_RWLIST_TRAVERSE(&all_events, s, eq_next) {
00790       ast_cli(a->fd, "Usecount: %d\n", s->usecount);
00791       ast_cli(a->fd, "Category: %d\n", s->category);
00792       ast_cli(a->fd, "Event:\n%s", s->eventdata);
00793    }
00794    AST_RWLIST_UNLOCK(&all_events);
00795 
00796    return CLI_SUCCESS;
00797 }

static enum error_type handle_updates ( struct mansession *  s,
const struct message *  m,
struct ast_config *  cfg,
const char *  dfn 
) [static]

Definition at line 1338 of file manager.c.

References ast_category_append(), ast_category_delete(), ast_category_empty(), ast_category_get(), ast_category_insert(), ast_category_new(), ast_category_rename(), ast_free, ast_log(), ast_str_create(), ast_strlen_zero(), ast_variable_append(), ast_variable_delete(), ast_variable_insert(), ast_variable_new(), ast_variable_update(), astman_get_header(), eventqent::category, FAILURE_ALLOCATION, FAILURE_DELCAT, FAILURE_DELETE, FAILURE_EMPTYCAT, FAILURE_NEWCAT, FAILURE_UPDATE, LOG_WARNING, match(), ast_variable::object, UNKNOWN_ACTION, UNKNOWN_CATEGORY, UNSPECIFIED_ARGUMENT, UNSPECIFIED_CATEGORY, and var.

Referenced by action_updateconfig().

01339 {
01340    int x;
01341    char hdr[40];
01342    const char *action, *cat, *var, *value, *match, *line;
01343    struct ast_category *category;
01344    struct ast_variable *v;
01345    struct ast_str *str1 = ast_str_create(16), *str2 = ast_str_create(16);
01346    enum error_type result = 0;
01347 
01348    for (x = 0; x < 100000; x++) {   /* 100000 = the max number of allowed updates + 1 */
01349       unsigned int object = 0;
01350 
01351       snprintf(hdr, sizeof(hdr), "Action-%06d", x);
01352       action = astman_get_header(m, hdr);
01353       if (ast_strlen_zero(action))     /* breaks the for loop if no action header */
01354          break;                        /* this could cause problems if actions come in misnumbered */
01355 
01356       snprintf(hdr, sizeof(hdr), "Cat-%06d", x);
01357       cat = astman_get_header(m, hdr);
01358       if (ast_strlen_zero(cat)) {      /* every action needs a category */
01359          result =  UNSPECIFIED_CATEGORY;
01360          break;
01361       }
01362 
01363       snprintf(hdr, sizeof(hdr), "Var-%06d", x);
01364       var = astman_get_header(m, hdr);
01365 
01366       snprintf(hdr, sizeof(hdr), "Value-%06d", x);
01367       value = astman_get_header(m, hdr);
01368 
01369       if (!ast_strlen_zero(value) && *value == '>') {
01370          object = 1;
01371          value++;
01372       }
01373    
01374       snprintf(hdr, sizeof(hdr), "Match-%06d", x);
01375       match = astman_get_header(m, hdr);
01376 
01377       snprintf(hdr, sizeof(hdr), "Line-%06d", x);
01378       line = astman_get_header(m, hdr);
01379 
01380       if (!strcasecmp(action, "newcat")) {
01381          if (ast_category_get(cfg,cat)) { /* check to make sure the cat doesn't */
01382             result = FAILURE_NEWCAT;   /* already exist */
01383             break;
01384          }
01385          if (!(category = ast_category_new(cat, dfn, -1))) {
01386             result = FAILURE_ALLOCATION;
01387             break;
01388          }
01389          if (ast_strlen_zero(match)) {
01390             ast_category_append(cfg, category);
01391          } else
01392             ast_category_insert(cfg, category, match);
01393       } else if (!strcasecmp(action, "renamecat")) {
01394          if (ast_strlen_zero(value)) {
01395             result = UNSPECIFIED_ARGUMENT;
01396             break;
01397          }
01398          if (!(category = ast_category_get(cfg, cat))) {
01399             result = UNKNOWN_CATEGORY;
01400             break;
01401          }
01402          ast_category_rename(category, value);
01403       } else if (!strcasecmp(action, "delcat")) {
01404          if (ast_category_delete(cfg, cat)) {
01405             result = FAILURE_DELCAT;
01406             break;
01407          }
01408       } else if (!strcasecmp(action, "emptycat")) {
01409          if (ast_category_empty(cfg, cat)) {
01410             result = FAILURE_EMPTYCAT;
01411             break;
01412          }
01413       } else if (!strcasecmp(action, "update")) {
01414          if (ast_strlen_zero(var)) {
01415             result = UNSPECIFIED_ARGUMENT;
01416             break;
01417          }
01418          if (!(category = ast_category_get(cfg,cat))) {
01419             result = UNKNOWN_CATEGORY;
01420             break;
01421          }
01422          if (ast_variable_update(category, var, value, match, object)) {
01423             result = FAILURE_UPDATE;
01424             break;
01425          }
01426       } else if (!strcasecmp(action, "delete")) {
01427          if ((ast_strlen_zero(var) && ast_strlen_zero(line))) {
01428             result = UNSPECIFIED_ARGUMENT;
01429             break;
01430          }
01431          if (!(category = ast_category_get(cfg, cat))) {
01432             result = UNKNOWN_CATEGORY;
01433             break;
01434          }
01435          if (ast_variable_delete(category, var, match, line)) {
01436             result = FAILURE_DELETE;
01437             break;
01438          }
01439       } else if (!strcasecmp(action, "append")) {
01440          if (ast_strlen_zero(var)) {
01441             result = UNSPECIFIED_ARGUMENT;
01442             break;
01443          }
01444          if (!(category = ast_category_get(cfg, cat))) {
01445             result = UNKNOWN_CATEGORY; 
01446             break;
01447          }
01448          if (!(v = ast_variable_new(var, value, dfn))) {
01449             result = FAILURE_ALLOCATION;
01450             break;
01451          }
01452          if (object || (match && !strcasecmp(match, "object")))
01453             v->object = 1;
01454          ast_variable_append(category, v);
01455       } else if (!strcasecmp(action, "insert")) {
01456          if (ast_strlen_zero(var) || ast_strlen_zero(line)) {
01457             result = UNSPECIFIED_ARGUMENT;
01458             break;
01459          }
01460          if (!(category = ast_category_get(cfg, cat))) {
01461             result = UNKNOWN_CATEGORY;
01462             break;
01463          }
01464          if (!(v = ast_variable_new(var, value, dfn))) {
01465             result = FAILURE_ALLOCATION;
01466             break;
01467          }
01468          ast_variable_insert(category, v, line);
01469       }
01470       else {
01471          ast_log(LOG_WARNING, "Action-%06d: %s not handled\n", x, action);
01472          result = UNKNOWN_ACTION;
01473          break;
01474       }
01475    }
01476    ast_free(str1);
01477    ast_free(str2);
01478    return result;
01479 }

static void json_escape ( char *  out,
const char *  in 
) [static]

The amount of space in out must be at least ( 2 * strlen(in) + 1 )

Definition at line 1254 of file manager.c.

Referenced by action_getconfigjson().

01255 {
01256    for (; *in; in++) {
01257       if (*in == '\\' || *in == '\"')
01258          *out++ = '\\';
01259       *out++ = *in;
01260    }
01261    *out = '\0';
01262 }

static int manager_displayconnects ( struct mansession_session *  session  )  [static]

Get displayconnects config option.

Parameters:
session manager session to get parameter from.
Returns:
displayconnects config option value.

Definition at line 521 of file manager.c.

References AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_manager_user::displayconnects, get_manager_by_name_locked(), and mansession_session::username.

Referenced by action_login(), generic_http_callback(), purge_sessions(), and session_do().

00522 {
00523    struct ast_manager_user *user = NULL;
00524    int ret = 0;
00525 
00526    AST_RWLIST_RDLOCK(&users);
00527    if ((user = get_manager_by_name_locked (session->username)))
00528       ret = user->displayconnects;
00529    AST_RWLIST_UNLOCK(&users);
00530    
00531    return ret;
00532 }

static int manager_modulecheck ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 2922 of file manager.c.

References ast_copy_string(), ast_file_version_find(), ast_log(), ast_module_check(), ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_error(), LOG_DEBUG, and version.

Referenced by __init_manager().

02923 {
02924    int res;
02925    const char *module = astman_get_header(m, "Module");
02926    const char *id = astman_get_header(m, "ActionID");
02927    char idText[256];
02928 #if !defined(LOW_MEMORY)
02929    const char *version;
02930 #endif
02931    char filename[PATH_MAX];
02932    char *cut;
02933 
02934    ast_copy_string(filename, module, sizeof(filename));
02935    if ((cut = strchr(filename, '.'))) {
02936       *cut = '\0';
02937    } else {
02938       cut = filename + strlen(filename);
02939    }
02940    snprintf(cut, (sizeof(filename) - strlen(filename)) - 1, ".so");
02941    ast_log(LOG_DEBUG, "**** ModuleCheck .so file %s\n", filename);
02942    res = ast_module_check(filename);
02943    if (!res) {
02944       astman_send_error(s, m, "Module not loaded");
02945       return 0;
02946    }
02947    snprintf(cut, (sizeof(filename) - strlen(filename)) - 1, ".c");
02948    ast_log(LOG_DEBUG, "**** ModuleCheck .c file %s\n", filename);
02949 #if !defined(LOW_MEMORY)
02950    version = ast_file_version_find(filename);
02951 #endif
02952 
02953    if (!ast_strlen_zero(id))
02954       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
02955    else
02956       idText[0] = '\0';
02957    astman_append(s, "Response: Success\r\n%s", idText);
02958 #if !defined(LOW_MEMORY)
02959    astman_append(s, "Version: %s\r\n\r\n", version ? version : "");
02960 #endif
02961    return 0;
02962 }

static int manager_moduleload ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 2975 of file manager.c.

References AST_FORCE_SOFT, ast_load_resource(), ast_module_reload(), ast_unload_resource(), astman_get_header(), astman_send_ack(), and astman_send_error().

Referenced by __init_manager().

02976 {
02977    int res;
02978    const char *module = astman_get_header(m, "Module");
02979    const char *loadtype = astman_get_header(m, "LoadType");
02980 
02981    if (!loadtype || strlen(loadtype) == 0)
02982       astman_send_error(s, m, "Incomplete ModuleLoad action.");
02983    if ((!module || strlen(module) == 0) && strcasecmp(loadtype, "reload") != 0)
02984       astman_send_error(s, m, "Need module name");
02985 
02986    if (!strcasecmp(loadtype, "load")) {
02987       res = ast_load_resource(module);
02988       if (res)
02989          astman_send_error(s, m, "Could not load module.");
02990       else
02991          astman_send_ack(s, m, "Module loaded.");
02992    } else if (!strcasecmp(loadtype, "unload")) {
02993       res = ast_unload_resource(module, AST_FORCE_SOFT);
02994       if (res)
02995          astman_send_error(s, m, "Could not unload module.");
02996       else
02997          astman_send_ack(s, m, "Module unloaded.");
02998    } else if (!strcasecmp(loadtype, "reload")) {
02999       if (module != NULL) {
03000          res = ast_module_reload(module);
03001          if (res == 0)
03002             astman_send_error(s, m, "No such module.");
03003          else if (res == 1)
03004             astman_send_error(s, m, "Module does not support reload action.");
03005          else
03006             astman_send_ack(s, m, "Module reloaded.");
03007       } else {
03008          ast_module_reload(NULL);   /* Reload all modules */
03009          astman_send_ack(s, m, "All modules reloaded");
03010       }
03011    } else 
03012       astman_send_error(s, m, "Incomplete ModuleLoad action.");
03013    return 0;
03014 }

static int manager_state_cb ( char *  context,
char *  exten,
int  state,
void *  data 
) [static]

Definition at line 3454 of file manager.c.

References ast_get_hint(), EVENT_FLAG_CALL, and manager_event.

Referenced by __init_manager().

03455 {
03456    /* Notify managers of change */
03457    char hint[512];
03458    ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, context, exten);
03459 
03460    manager_event(EVENT_FLAG_CALL, "ExtensionStatus", "Exten: %s\r\nContext: %s\r\nHint: %s\r\nStatus: %d\r\n", exten, context, hint, state);
03461    return 0;
03462 }

static int process_events ( struct mansession *  s  )  [static]

Send any applicable events to the client listening on this socket. Wait only for a finite time on each event, and drop all events whether they are successfully sent or not.

Definition at line 2692 of file manager.c.

References mansession_session::__lock, advance_event(), ast_mutex_lock(), ast_mutex_unlock(), mansession_session::authenticated, eventqent::category, eventqent::eventdata, mansession_session::f, mansession_session::last_ev, mansession_session::readperm, mansession_session::send_events, send_string(), and mansession::session.

Referenced by do_message(), and process_message().

02693 {
02694    int ret = 0;
02695 
02696    ast_mutex_lock(&s->session->__lock);
02697    if (s->session->f != NULL) {
02698       struct eventqent *eqe = s->session->last_ev;
02699 
02700       while ((eqe = advance_event(eqe))) {
02701          if (!ret && s->session->authenticated &&
02702              (s->session->readperm & eqe->category) == eqe->category &&
02703              (s->session->send_events & eqe->category) == eqe->category) {
02704             if (send_string(s, eqe->eventdata) < 0)
02705                ret = -1;   /* don't send more */
02706          }
02707          s->session->last_ev = eqe;
02708       }
02709    }
02710    ast_mutex_unlock(&s->session->__lock);
02711    return ret;
02712 }

static int process_message ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 3029 of file manager.c.

References __astman_get_header(), mansession_session::__lock, manager_action::action, ast_copy_string(), ast_debug, ast_mutex_lock(), ast_mutex_unlock(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_strlen_zero(), astman_get_header(), astman_send_error(), mansession_session::authenticated, manager_action::authority, buf, check_manager_session_inuse(), manager_action::func, GET_HEADER_SKIP_EMPTY, process_events(), mansession::session, and mansession_session::writeperm.

Referenced by do_message(), and generic_http_callback().

03030 {
03031    char action[80] = "";
03032    int ret = 0;
03033    struct manager_action *tmp;
03034    const char *user = astman_get_header(m, "Username");
03035 
03036    ast_copy_string(action, __astman_get_header(m, "Action", GET_HEADER_SKIP_EMPTY), sizeof(action));
03037    ast_debug(1, "Manager received command '%s'\n", action);
03038 
03039    if (ast_strlen_zero(action)) {
03040       ast_mutex_lock(&s->session->__lock);
03041       astman_send_error(s, m, "Missing action in request");
03042       ast_mutex_unlock(&s->session->__lock);
03043       return 0;
03044    }
03045 
03046    if (!s->session->authenticated && strcasecmp(action, "Login") && strcasecmp(action, "Logoff") && strcasecmp(action, "Challenge")) {
03047       ast_mutex_lock(&s->session->__lock);
03048       astman_send_error(s, m, "Permission denied");
03049       ast_mutex_unlock(&s->session->__lock);
03050       return 0;
03051    }
03052 
03053    if (!allowmultiplelogin && !s->session->authenticated && user &&
03054       (!strcasecmp(action, "Login") || !strcasecmp(action, "Challenge"))) {
03055       if (check_manager_session_inuse(user)) {
03056          sleep(1);
03057          ast_mutex_lock(&s->session->__lock);
03058          astman_send_error(s, m, "Login Already In Use");
03059          ast_mutex_unlock(&s->session->__lock);
03060          return -1;
03061       }
03062    }
03063 
03064    AST_RWLIST_RDLOCK(&actions);
03065    AST_RWLIST_TRAVERSE(&actions, tmp, list) {
03066       if (strcasecmp(action, tmp->action))
03067          continue;
03068       if (s->session->writeperm & tmp->authority || tmp->authority == 0)
03069          ret = tmp->func(s, m);
03070       else
03071          astman_send_error(s, m, "Permission denied");
03072       break;
03073    }
03074    AST_RWLIST_UNLOCK(&actions);
03075 
03076    if (!tmp) {
03077       char buf[512];
03078       snprintf(buf, sizeof(buf), "Invalid/unknown command: %s. Use Action: ListCommands to show available commands.", action);
03079       ast_mutex_lock(&s->session->__lock);
03080       astman_send_error(s, m, buf);
03081       ast_mutex_unlock(&s->session->__lock);
03082    }
03083    if (ret)
03084       return ret;
03085    /* Once done with our message, deliver any pending events unless the
03086       requester doesn't want them as part of this response.
03087    */
03088    if (ast_strlen_zero(astman_get_header(m, "SuppressEvents"))) {
03089       return process_events(s);
03090    } else {
03091       return ret;
03092    }
03093 }

static void purge_events ( void   )  [static]

Purge unused events. Remove elements from the head as long as their usecount is 0 and there is a next element.

Definition at line 350 of file manager.c.

References ast_free, AST_RWLIST_FIRST, AST_RWLIST_NEXT, AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_REMOVE_HEAD, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_tvdiff_sec(), ast_tvnow(), eventqent::tv, and eventqent::usecount.

Referenced by purge_old_stuff().

00351 {
00352    struct eventqent *ev;
00353    struct timeval now = ast_tvnow();
00354 
00355    AST_RWLIST_WRLOCK(&all_events);
00356    while ( (ev = AST_RWLIST_FIRST(&all_events)) &&
00357        ev->usecount == 0 && AST_RWLIST_NEXT(ev, eq_next)) {
00358       AST_RWLIST_REMOVE_HEAD(&all_events, eq_next);
00359       ast_free(ev);
00360    }
00361 
00362    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&all_events, ev, eq_next) {
00363       /* Never release the last event */
00364       if (!AST_RWLIST_NEXT(ev, eq_next)) {
00365          break;
00366       }
00367 
00368       /* 2.5 times whatever the HTTP timeout is (maximum 2.5 hours) is the maximum time that we will definitely cache an event */
00369       if (ev->usecount == 0 && ast_tvdiff_sec(now, ev->tv) > (httptimeout > 3600 ? 3600 : httptimeout) * 2.5) {
00370          AST_RWLIST_REMOVE_CURRENT(eq_next);
00371          ast_free(ev);
00372       }
00373    }
00374    AST_RWLIST_TRAVERSE_SAFE_END;
00375    AST_RWLIST_UNLOCK(&all_events);
00376 }

static void purge_sessions ( int  n_max  )  [static]

remove at most n_max stale session from the list.

Definition at line 3297 of file manager.c.

References ast_atomic_fetchadd_int(), ast_inet_ntoa(), AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_verb, mansession_session::authenticated, free_session(), mansession_session::inuse, manager_displayconnects(), mansession_session::sessiontimeout, mansession_session::sin, mansession_session::username, and VERBOSITY_ATLEAST.

Referenced by purge_old_stuff().

03298 {
03299    struct mansession_session *session;
03300    time_t now = time(NULL);
03301 
03302    AST_LIST_LOCK(&sessions);
03303    AST_LIST_TRAVERSE_SAFE_BEGIN(&sessions, session, list) {
03304       if (session->sessiontimeout && (now > session->sessiontimeout) && !session->inuse) {
03305          AST_LIST_REMOVE_CURRENT(list);
03306          ast_atomic_fetchadd_int(&num_sessions, -1);
03307          if (session->authenticated && (VERBOSITY_ATLEAST(2)) && manager_displayconnects(session)) {
03308             ast_verb(2, "HTTP Manager '%s' timed out from %s\n",
03309                session->username, ast_inet_ntoa(session->sin.sin_addr));
03310          }
03311          free_session(session);  /* XXX outside ? */
03312          if (--n_max <= 0)
03313             break;
03314       }
03315    }
03316    AST_LIST_TRAVERSE_SAFE_END;
03317    AST_LIST_UNLOCK(&sessions);
03318 }

static int send_string ( struct mansession *  s,
char *  string 
) [static]

helper function to send a string to the socket. Return -1 on error (e.g. buffer full).

Definition at line 958 of file manager.c.

References ast_careful_fwrite(), mansession_session::f, mansession::f, mansession_session::fd, mansession::fd, mansession::session, and mansession_session::writetimeout.

Referenced by astman_append(), and process_events().

00959 {
00960    if (s->f) {
00961       return ast_careful_fwrite(s->f, s->fd, string, strlen(string), s->session->writetimeout);
00962    } else {
00963       return ast_careful_fwrite(s->session->f, s->session->fd, string, strlen(string), s->session->writetimeout);
00964    }
00965 }

static void* session_do ( void *  data  )  [static]

The body of the individual manager session. Call get_input() to read one line at a time (or be woken up on new events), collect the lines in a message until found an empty line, and execute the request. In any case, deliver events asynchronously through process_events() (called from here if no line is available, or at the end of process_message(). ).

Definition at line 3206 of file manager.c.

References mansession_session::__lock, AMI_VERSION, ao2_ref, ast_atomic_fetchadd_int(), ast_calloc, ast_inet_ntoa(), AST_LIST_HEAD_INIT_NOLOCK, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), ast_mutex_init(), AST_PTHREADT_NULL, ast_verb, astman_append(), mansession_session::authenticated, mansession_session::datastores, destroy_session(), do_message(), errno, ast_tcptls_session_instance::f, mansession_session::f, mansession_session::fd, ast_tcptls_session_instance::fd, ast_frame::flags, grab_last(), mansession_session::last_ev, LOG_EVENT, LOG_WARNING, manager_displayconnects(), ast_tcptls_session_instance::remote_address, s, mansession_session::send_events, mansession::session, mansession_session::sin, mansession_session::username, mansession_session::waiting_thread, and mansession_session::writetimeout.

03207 {
03208    struct ast_tcptls_session_instance *ser = data;
03209    struct mansession_session *session = ast_calloc(1, sizeof(*session));
03210    struct mansession s = {.session = NULL, };
03211    int flags;
03212    int res;
03213    struct protoent *p;
03214 
03215    if (session == NULL)
03216       goto done;
03217 
03218    /* here we set TCP_NODELAY on the socket to disable Nagle's algorithm.
03219     * This is necessary to prevent delays (caused by buffering) as we
03220     * write to the socket in bits and peices. */
03221    p = getprotobyname("tcp");
03222    if (p) {
03223       int arg = 1;
03224       if( setsockopt(ser->fd, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
03225          ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY mode: %s\nSome manager actions may be slow to respond.\n", strerror(errno));
03226       }
03227    } else {
03228       ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY, getprotobyname(\"tcp\") failed\nSome manager actions may be slow to respond.\n");
03229    }
03230 
03231    session->writetimeout = 100;
03232    session->waiting_thread = AST_PTHREADT_NULL;
03233 
03234    flags = fcntl(ser->fd, F_GETFL);
03235    if (!block_sockets) /* make sure socket is non-blocking */
03236       flags |= O_NONBLOCK;
03237    else
03238       flags &= ~O_NONBLOCK;
03239    fcntl(ser->fd, F_SETFL, flags);
03240 
03241    ast_mutex_init(&session->__lock);
03242    session->send_events = -1;
03243    /* Hook to the tail of the event queue */
03244    session->last_ev = grab_last();
03245 
03246    /* these fields duplicate those in the 'ser' structure */
03247    session->fd = ser->fd;
03248    session->f = ser->f;
03249    session->sin = ser->remote_address;
03250    s.session = session;
03251 
03252    AST_LIST_HEAD_INIT_NOLOCK(&session->datastores);
03253 
03254    AST_LIST_LOCK(&sessions);
03255    AST_LIST_INSERT_HEAD(&sessions, session, list);
03256    ast_atomic_fetchadd_int(&num_sessions, 1);
03257    AST_LIST_UNLOCK(&sessions);
03258 
03259    astman_append(&s, "Asterisk Call Manager/%s\r\n", AMI_VERSION);   /* welcome prompt */
03260    for (;;) {
03261       if ((res = do_message(&s)) < 0)
03262          break;
03263    }
03264    /* session is over, explain why and terminate */
03265    if (session->authenticated) {
03266          if (manager_displayconnects(session))
03267          ast_verb(2, "Manager '%s' logged off from %s\n", session->username, ast_inet_ntoa(session->sin.sin_addr));
03268       ast_log(LOG_EVENT, "Manager '%s' logged off from %s\n", session->username, ast_inet_ntoa(session->sin.sin_addr));
03269    } else {
03270          if (displayconnects)
03271          ast_verb(2, "Connect attempt from '%s' unable to authenticate\n", ast_inet_ntoa(session->sin.sin_addr));
03272       ast_log(LOG_EVENT, "Failed attempt from %s\n", ast_inet_ntoa(session->sin.sin_addr));
03273    }
03274 
03275    /* It is possible under certain circumstances for this session thread
03276       to complete its work and exit *before* the thread that created it
03277       has finished executing the ast_pthread_create_background() function.
03278       If this occurs, some versions of glibc appear to act in a buggy
03279       fashion and attempt to write data into memory that it thinks belongs
03280       to the thread but is in fact not owned by the thread (or may have
03281       been freed completely).
03282 
03283       Causing this thread to yield to other threads at least one time
03284       appears to work around this bug.
03285    */
03286    usleep(1);
03287 
03288    destroy_session(session);
03289 
03290 done:
03291    ao2_ref(ser, -1);
03292    ser = NULL;
03293    return NULL;
03294 }

static int set_eventmask ( struct mansession *  s,
const char *  eventmask 
) [static]

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.

Definition at line 1066 of file manager.c.

References mansession_session::__lock, ast_mutex_lock(), ast_mutex_unlock(), mansession_session::send_events, mansession::session, and strings_to_mask().

Referenced by action_events(), and authenticate().

01067 {
01068    int maskint = strings_to_mask(eventmask);
01069 
01070    ast_mutex_lock(&s->session->__lock);
01071    if (maskint >= 0)
01072       s->session->send_events = maskint;
01073    ast_mutex_unlock(&s->session->__lock);
01074 
01075    return maskint;
01076 }

static int strings_to_mask ( const char *  string  )  [static]

A number returns itself, false returns 0, true returns all flags, other strings return the flags that are set.

Definition at line 465 of file manager.c.

References ARRAY_LEN, ast_false(), ast_strlen_zero(), ast_true(), get_perm(), num, and perms.

Referenced by set_eventmask().

00466 {
00467    const char *p;
00468 
00469    if (ast_strlen_zero(string))
00470       return -1;
00471 
00472    for (p = string; *p; p++)
00473       if (*p < '0' || *p > '9')
00474          break;
00475    if (!*p) /* all digits */
00476       return atoi(string);
00477    if (ast_false(string))
00478       return 0;
00479    if (ast_true(string)) { /* all permissions */
00480       int x, ret = 0;
00481       for (x = 0; x < ARRAY_LEN(perms); x++)
00482          ret |= perms[x].num;
00483       return ret;
00484    }
00485    return get_perm(string);
00486 }


Variable Documentation

int allowmultiplelogin = 1 [static]

Definition at line 124 of file manager.c.

struct ast_threadstorage astman_append_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_astman_append_buf , .custom_init = NULL , } [static]

Definition at line 974 of file manager.c.

Referenced by astman_append().

int block_sockets [static]

Definition at line 131 of file manager.c.

int broken_events_action = 0 [static]

Definition at line 127 of file manager.c.

struct ast_cli_entry cli_manager[] [static]

Definition at line 819 of file manager.c.

Referenced by __init_manager().

struct { ... } command_blacklist[] [static]

Referenced by check_blacklist().

int displayconnects = 1 [static]

Definition at line 123 of file manager.c.

int httptimeout = 60 [static]

Definition at line 126 of file manager.c.

int manager_debug [static]

enable some debugging code in the manager

Definition at line 134 of file manager.c.

int manager_enabled = 0 [static]

Definition at line 128 of file manager.c.

struct ast_threadstorage manager_event_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_manager_event_buf , .custom_init = NULL , } [static]

Definition at line 3348 of file manager.c.

Referenced by __manager_event().

char mandescr_atxfer[] [static]

Definition at line 2197 of file manager.c.

Referenced by __init_manager().

char mandescr_command[] [static]

Initial value:

"Description: Run a CLI command.\n"
"Variables: (Names marked with * are required)\n"
"  *Command: Asterisk CLI command to run\n"
"  ActionID: Optional Action id for message matching.\n"

Definition at line 2289 of file manager.c.

Referenced by __init_manager().

char mandescr_coresettings[] [static]

Initial value:

"Description: Query for Core PBX settings.\n"
"Variables: (Names marked with * are optional)\n"
"       *ActionID: ActionID of this transaction\n"

Definition at line 2740 of file manager.c.

Referenced by __init_manager().

char mandescr_coreshowchannels[] [static]

Initial value:

"Description: List currently defined channels and some information\n"
"             about them.\n"
"Variables:\n"
"          ActionID: Optional Action id for message matching.\n"

Definition at line 2843 of file manager.c.

Referenced by __init_manager().

char mandescr_corestatus[] [static]

Initial value:

"Description: Query for Core PBX status.\n"
"Variables: (Names marked with * are optional)\n"
"       *ActionID: ActionID of this transaction\n"

Definition at line 2786 of file manager.c.

Referenced by __init_manager().

char mandescr_createconfig[] [static]

Definition at line 1572 of file manager.c.

Referenced by __init_manager().

char mandescr_events[] [static]

Definition at line 1721 of file manager.c.

Referenced by __init_manager().

char mandescr_extensionstate[] [static]

Definition at line 2617 of file manager.c.

Referenced by __init_manager().

char mandescr_getconfig[] [static]

Definition at line 1163 of file manager.c.

Referenced by __init_manager().

char mandescr_getconfigjson[] [static]

Definition at line 1264 of file manager.c.

Referenced by __init_manager().

char mandescr_getvar[] [static]

Definition at line 1878 of file manager.c.

Referenced by __init_manager().

char mandescr_hangup[] [static]

Initial value:

"Description: Hangup a channel\n"
"Variables: \n"
"  Channel: The channel name to be hungup\n"

Definition at line 1810 of file manager.c.

Referenced by __init_manager().

char mandescr_listcategories[] [static]

Initial value:

"Description: A 'ListCategories' action will dump the categories in\n"
"a given file.\n"
"Variables:\n"
"   Filename: Configuration filename (e.g. foo.conf)\n"

Definition at line 1212 of file manager.c.

Referenced by __init_manager().

char mandescr_listcommands[] [static]

Initial value:

"Description: Returns the action name and synopsis for every\n"
"  action that is available to the user\n"
"Variables: NONE\n"

Definition at line 1699 of file manager.c.

Referenced by __init_manager().

char mandescr_logoff[] [static]

Initial value:

"Description: Logoff this manager session\n"
"Variables: NONE\n"

Definition at line 1765 of file manager.c.

Referenced by __init_manager().

char mandescr_mailboxcount[] [static]

Definition at line 2584 of file manager.c.

Referenced by __init_manager().

char mandescr_mailboxstatus[] [static]

Help text for manager command mailboxstatus.

Definition at line 2556 of file manager.c.

Referenced by __init_manager().

char mandescr_modulecheck[] [static]

Definition at line 2912 of file manager.c.

Referenced by __init_manager().

char mandescr_moduleload[] [static]

Definition at line 2964 of file manager.c.

Referenced by __init_manager().

char mandescr_originate[] [static]

Definition at line 2414 of file manager.c.

Referenced by __init_manager().

char mandescr_ping[] [static]

Initial value:

"Description: A 'Ping' action will ellicit a 'Pong' response.  Used to keep the\n"
"  manager connection open.\n"
"Variables: NONE\n"
Manager PING.

Definition at line 1146 of file manager.c.

Referenced by __init_manager().

char mandescr_redirect[] [static]

Definition at line 2111 of file manager.c.

Referenced by __init_manager().

char mandescr_reload[] [static]

Initial value:

"Description: Send a reload event.\n"
"Variables: (Names marked with * are optional)\n"
"       *ActionID: ActionID of this transaction\n"
"       *Module: Name of the module to reload\n"

Definition at line 2824 of file manager.c.

Referenced by __init_manager().

char mandescr_sendtext[] [static]

Definition at line 2070 of file manager.c.

Referenced by __init_manager().

char mandescr_setvar[] [static]

Definition at line 1834 of file manager.c.

Referenced by __init_manager().

char mandescr_status[] [static]

Definition at line 1930 of file manager.c.

Referenced by __init_manager().

char mandescr_timeout[] [static]

Definition at line 2652 of file manager.c.

Referenced by __init_manager().

char mandescr_updateconfig[] [static]

Definition at line 1481 of file manager.c.

Referenced by __init_manager().

char mandescr_userevent[] [static]

Definition at line 2714 of file manager.c.

Referenced by __init_manager().

char mandescr_waitevent[] [static]

Manager WAITEVENT.

Definition at line 1598 of file manager.c.

Referenced by __init_manager().

int num_sessions [static]

Definition at line 132 of file manager.c.

struct permalias perms[] [static]

helper functions to convert back and forth between string and numeric representation of set of flags

Referenced by action_events(), authority_to_str(), get_perm(), and strings_to_mask().

int timestampevents [static]

Definition at line 125 of file manager.c.

struct ast_threadstorage userevent_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_userevent_buf , .custom_init = NULL , } [static]

Definition at line 975 of file manager.c.

Referenced by action_userevent().

int webmanager_enabled = 0 [static]

Definition at line 129 of file manager.c.

char* words[AST_MAX_CMD_LEN] [inherited]

Definition at line 147 of file manager.c.


Generated on Wed Aug 11 01:10:43 2010 for Asterisk - the Open Source PBX by  doxygen 1.5.9