Sat Mar 26 11:28:02 2011

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
static struct ast_cli_entry cli_manager []
struct {
   char *   words [AST_MAX_CMD_LEN]
} command_blacklist []
static const int DEFAULT_BLOCKSOCKETS = 0
static const int DEFAULT_BROKENEVENTSACTION = 0
static const int DEFAULT_DISPLAYCONNECTS = 1
static const int DEFAULT_ENABLED = 0
static const int DEFAULT_HTTPTIMEOUT = 60
static const int DEFAULT_TIMESTAMPEVENTS = 0
static const int DEFAULT_WEBENABLED = 0
static int displayconnects
static int httptimeout
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 986 of file manager.c.

Referenced by astman_append().

#define GET_HEADER_FIRST_MATCH   0

Definition at line 889 of file manager.c.

Referenced by astman_get_header().

#define GET_HEADER_LAST_MATCH   1

Definition at line 890 of file manager.c.

Referenced by __astman_get_header().

#define GET_HEADER_SKIP_EMPTY   2

Definition at line 891 of file manager.c.

Referenced by __astman_get_header(), and process_message().

#define MANAGER_EVENT_BUF_INITSIZE   256

Definition at line 3364 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 153 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 1026 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 892 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().

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

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

00992 {

static void __init_manager_event_buf ( void   )  [static]

Definition at line 3363 of file manager.c.

03366 : Send AMI event to client */

static void __init_userevent_buf ( void   )  [static]

Definition at line 983 of file manager.c.

00992 {

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 3367 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.

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

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

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

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

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

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

01802 {
01803    const char *authtype = astman_get_header(m, "AuthType");
01804 
01805    if (!strcasecmp(authtype, "MD5")) {
01806       if (ast_strlen_zero(s->session->challenge))
01807          snprintf(s->session->challenge, sizeof(s->session->challenge), "%ld", ast_random());
01808       ast_mutex_lock(&s->session->__lock);
01809       astman_start_ack(s, m);
01810       astman_append(s, "Challenge: %s\r\n\r\n", s->session->challenge);
01811       ast_mutex_unlock(&s->session->__lock);
01812    } else {
01813       astman_send_error(s, m, "Must specify AuthType");
01814    }
01815    return 0;
01816 }

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

Manager command "command" - execute CLI command.

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

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

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

Show PBX core settings information.

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

02762 {
02763    const char *actionid = astman_get_header(m, "ActionID");
02764    char idText[150];
02765 
02766    if (!ast_strlen_zero(actionid))
02767       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", actionid);
02768    else
02769       idText[0] = '\0';
02770 
02771    astman_append(s, "Response: Success\r\n"
02772          "%s"
02773          "AMIversion: %s\r\n"
02774          "AsteriskVersion: %s\r\n"
02775          "SystemName: %s\r\n"
02776          "CoreMaxCalls: %d\r\n"
02777          "CoreMaxLoadAvg: %f\r\n"
02778          "CoreRunUser: %s\r\n"
02779          "CoreRunGroup: %s\r\n"
02780          "CoreMaxFilehandles: %d\r\n" 
02781          "CoreRealTimeEnabled: %s\r\n"
02782          "CoreCDRenabled: %s\r\n"
02783          "CoreHTTPenabled: %s\r\n"
02784          "\r\n",
02785          idText,
02786          AMI_VERSION,
02787          ast_get_version(), 
02788          ast_config_AST_SYSTEM_NAME,
02789          option_maxcalls,
02790          option_maxload,
02791          ast_config_AST_RUN_USER,
02792          ast_config_AST_RUN_GROUP,
02793          option_maxfiles,
02794          ast_realtime_enabled() ? "Yes" : "No",
02795          check_cdr_enabled() ? "Yes" : "No",
02796          check_webmanager_enabled() ? "Yes" : "No"
02797          );
02798    return 0;
02799 }

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

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

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

Show PBX core status information.

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

02808 {
02809    const char *actionid = astman_get_header(m, "ActionID");
02810    char idText[150];
02811    char startuptime[150];
02812    char reloadtime[150];
02813    struct ast_tm tm;
02814 
02815    if (!ast_strlen_zero(actionid))
02816       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", actionid);
02817    else
02818       idText[0] = '\0';
02819 
02820    ast_localtime(&ast_startuptime, &tm, NULL);
02821    ast_strftime(startuptime, sizeof(startuptime), "%H:%M:%S", &tm);
02822    ast_localtime(&ast_lastreloadtime, &tm, NULL);
02823    ast_strftime(reloadtime, sizeof(reloadtime), "%H:%M:%S", &tm);
02824 
02825    astman_append(s, "Response: Success\r\n"
02826          "%s"
02827          "CoreStartupTime: %s\r\n"
02828          "CoreReloadTime: %s\r\n"
02829          "CoreCurrentCalls: %d\r\n"
02830          "\r\n",
02831          idText,
02832          startuptime,
02833          reloadtime,
02834          ast_active_channels()
02835          );
02836    return 0;
02837 }

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

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

01588 {
01589    int fd;
01590    const char *fn = astman_get_header(m, "Filename");
01591    struct ast_str *filepath = ast_str_alloca(PATH_MAX);
01592    ast_str_set(&filepath, 0, "%s/", ast_config_AST_CONFIG_DIR);
01593    ast_str_append(&filepath, 0, "%s", fn);
01594 
01595    if ((fd = open(ast_str_buffer(filepath), O_CREAT | O_EXCL, AST_FILE_MODE)) != -1) {
01596       close(fd);
01597       astman_send_ack(s, m, "New configuration file created successfully");
01598    } else {
01599       astman_send_error(s, m, strerror(errno));
01600    }
01601 
01602    return 0;
01603 }

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

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

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

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

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

02644 {
02645    const char *exten = astman_get_header(m, "Exten");
02646    const char *context = astman_get_header(m, "Context");
02647    char hint[256] = "";
02648    int status;
02649    if (ast_strlen_zero(exten)) {
02650       astman_send_error(s, m, "Extension not specified");
02651       return 0;
02652    }
02653    if (ast_strlen_zero(context))
02654       context = "default";
02655    status = ast_extension_state(NULL, context, exten);
02656    ast_get_hint(hint, sizeof(hint) - 1, NULL, 0, NULL, context, exten);
02657    astman_start_ack(s, m);
02658    astman_append(s,   "Message: Extension Status\r\n"
02659             "Exten: %s\r\n"
02660             "Context: %s\r\n"
02661             "Hint: %s\r\n"
02662             "Status: %d\r\n\r\n",
02663             exten, context, hint, status);
02664    return 0;
02665 }

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

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

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

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

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

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

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

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

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

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

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

01824 {
01825    struct ast_channel *c = NULL;
01826    const char *name = astman_get_header(m, "Channel");
01827    if (ast_strlen_zero(name)) {
01828       astman_send_error(s, m, "No channel specified");
01829       return 0;
01830    }
01831    c = ast_get_channel_by_name_locked(name);
01832    if (!c) {
01833       astman_send_error(s, m, "No such channel");
01834       return 0;
01835    }
01836    ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
01837    ast_channel_unlock(c);
01838    astman_send_ack(s, m, "Channel Hungup");
01839    return 0;
01840 }

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

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

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

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

01714 {
01715    struct manager_action *cur;
01716    struct ast_str *temp = ast_str_alloca(BUFSIZ); /* XXX very large ? */
01717 
01718    astman_start_ack(s, m);
01719    AST_RWLIST_TRAVERSE(&actions, cur, list) {
01720       if (s->session->writeperm & cur->authority || cur->authority == 0)
01721          astman_append(s, "%s: %s (Priv: %s)\r\n",
01722             cur->action, cur->synopsis, authority_to_str(cur->authority, &temp));
01723    }
01724    astman_append(s, "\r\n");
01725 
01726    return 0;
01727 }

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

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

01784 {
01785    if (authenticate(s, m)) {
01786       sleep(1);
01787       astman_send_error(s, m, "Authentication failed");
01788       return -1;
01789    }
01790    s->session->authenticated = 1;
01791    if (manager_displayconnects(s->session))
01792       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));
01793    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));
01794    astman_send_ack(s, m, "Authentication accepted");
01795    if (ast_opt_send_fullybooted && ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
01796       manager_event(EVENT_FLAG_SYSTEM, "FullyBooted", "Status: Fully Booted\r\n");
01797    }
01798    return 0;
01799 }

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

Definition at line 1777 of file manager.c.

References astman_send_response().

Referenced by __init_manager().

01778 {
01779    astman_send_response(s, m, "Goodbye", "Thanks for all the fish.");
01780    return -1;
01781 }

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

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

02612 {
02613    const char *mailbox = astman_get_header(m, "Mailbox");
02614    int newmsgs = 0, oldmsgs = 0, urgentmsgs = 0;;
02615 
02616    if (ast_strlen_zero(mailbox)) {
02617       astman_send_error(s, m, "Mailbox not specified");
02618       return 0;
02619    }
02620    ast_app_inboxcount2(mailbox, &urgentmsgs, &newmsgs, &oldmsgs);
02621    astman_start_ack(s, m);
02622    astman_append(s,   "Message: Mailbox Message Count\r\n"
02623             "Mailbox: %s\r\n"
02624             "UrgMessages: %d\r\n"
02625             "NewMessages: %d\r\n"
02626             "OldMessages: %d\r\n"
02627             "\r\n",
02628             mailbox, urgentmsgs, newmsgs, oldmsgs);
02629    return 0;
02630 }

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

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

02583 {
02584    const char *mailbox = astman_get_header(m, "Mailbox");
02585    int ret;
02586 
02587    if (ast_strlen_zero(mailbox)) {
02588       astman_send_error(s, m, "Mailbox not specified");
02589       return 0;
02590    }
02591    ret = ast_app_has_voicemail(mailbox, NULL);
02592    astman_start_ack(s, m);
02593    astman_append(s, "Message: Mailbox Status\r\n"
02594           "Mailbox: %s\r\n"
02595           "Waiting: %d\r\n\r\n", mailbox, ret);
02596    return 0;
02597 }

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

Definition at line 2439 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(), ast_variables_destroy(), 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().

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

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

Definition at line 1159 of file manager.c.

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

Referenced by __init_manager().

01160 {
01161    const char *actionid = astman_get_header(m, "ActionID");
01162 
01163    astman_append(s, "Response: Success\r\n");
01164    if (!ast_strlen_zero(actionid)){
01165       astman_append(s, "ActionID: %s\r\n", actionid);
01166    }
01167    astman_append(s, "Ping: Pong\r\n\r\n");
01168    return 0;
01169 }

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

action_redirect: The redirect manager command

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

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

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

Send a reload event.

Definition at line 2846 of file manager.c.

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

Referenced by __init_manager().

02847 {
02848    const char *module = astman_get_header(m, "Module");
02849    int res = ast_module_reload(S_OR(module, NULL));
02850 
02851    if (res == 2)
02852       astman_send_ack(s, m, "Module Reloaded");
02853    else
02854       astman_send_error(s, m, s == 0 ? "No such module" : "Module does not support reload");
02855    return 0;
02856 }

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

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

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

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

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

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

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

Manager "status" command to show channels.

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

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

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

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

02675 {
02676    struct ast_channel *c;
02677    const char *name = astman_get_header(m, "Channel");
02678    double timeout = atof(astman_get_header(m, "Timeout"));
02679    struct timeval when = { timeout, 0 };
02680 
02681    if (ast_strlen_zero(name)) {
02682       astman_send_error(s, m, "No channel specified");
02683       return 0;
02684    }
02685    if (!timeout || timeout < 0) {
02686       astman_send_error(s, m, "No timeout specified");
02687       return 0;
02688    }
02689    c = ast_get_channel_by_name_locked(name);
02690    if (!c) {
02691       astman_send_error(s, m, "No such channel");
02692       return 0;
02693    }
02694 
02695    when.tv_usec = (timeout - when.tv_sec) * 1000000.0;
02696    ast_channel_setwhentohangup_tv(c, when);
02697    ast_channel_unlock(c);
02698    astman_send_ack(s, m, "Timeout Set");
02699    return 0;
02700 }

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

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

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

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

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

02737 {
02738    const char *event = astman_get_header(m, "UserEvent");
02739    struct ast_str *body = ast_str_thread_get(&userevent_buf, 16);
02740    int x;
02741 
02742    ast_str_reset(body);
02743 
02744    for (x = 0; x < m->hdrcount; x++) {
02745       if (strncasecmp("UserEvent:", m->headers[x], strlen("UserEvent:"))) {
02746          ast_str_append(&body, 0, "%s\r\n", m->headers[x]);
02747       }
02748    }
02749 
02750    astman_send_ack(s, m, "Event Sent");   
02751    manager_event(EVENT_FLAG_USER, "UserEvent", "UserEvent: %s\r\n%s", event, ast_str_buffer(body));
02752    return 0;
02753 }

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

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

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

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

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

00839 {
00840    struct eventqent *next;
00841 
00842    AST_RWLIST_RDLOCK(&all_events);
00843    if ((next = AST_RWLIST_NEXT(e, eq_next))) {
00844       ast_atomic_fetchadd_int(&next->usecount, 1);
00845       ast_atomic_fetchadd_int(&e->usecount, -1);
00846    }
00847    AST_RWLIST_UNLOCK(&all_events);
00848    return next;
00849 }

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

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

03340 {
03341    struct eventqent *tmp = ast_malloc(sizeof(*tmp) + strlen(str));
03342    static int seq;   /* sequence number */
03343 
03344    if (!tmp)
03345       return -1;
03346 
03347    /* need to init all fields, because ast_malloc() does not */
03348    tmp->usecount = 0;
03349    tmp->category = category;
03350    tmp->seq = ast_atomic_fetchadd_int(&seq, 1);
03351    tmp->tv = ast_tvnow();
03352    AST_RWLIST_NEXT(tmp, eq_next) = NULL;
03353    strcpy(tmp->eventdata, str);
03354 
03355    AST_RWLIST_WRLOCK(&all_events);
03356    AST_RWLIST_INSERT_TAIL(&all_events, tmp, eq_next);
03357    AST_RWLIST_UNLOCK(&all_events);
03358 
03359    return 0;
03360 }

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

References eventqent::next.

Referenced by get_perm().

00438 {
00439    const char *val = bigstr, *next;
00440 
00441    do {
00442       if ((next = strchr(val, delim))) {
00443          if (!strncmp(val, smallstr, (next - val)))
00444             return 1;
00445          else
00446             continue;
00447       } else
00448          return !strcmp(smallstr, val);
00449    } while (*(val = (next + 1)));
00450 
00451    return 0;
00452 }

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

03516 {
03517    struct manager_action *cur = NULL;
03518 
03519    if (!(cur = ast_calloc(1, sizeof(*cur))))
03520       return -1;
03521 
03522    cur->action = action;
03523    cur->authority = auth;
03524    cur->func = func;
03525    cur->synopsis = synopsis;
03526    cur->description = description;
03527 
03528    if (ast_manager_register_struct(cur)) {
03529       ast_free(cur);
03530       return -1;
03531    }
03532 
03533    return 0;
03534 }

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

References AST_RWLIST_INSERT_TAIL, AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

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

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

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

03480 {
03481    struct manager_action *cur, *prev = NULL;
03482    struct timespec tv = { 5, };
03483 
03484    if (AST_RWLIST_TIMEDWRLOCK(&actions, &tv)) {
03485       ast_log(LOG_ERROR, "Could not obtain lock on manager list\n");
03486       return -1;
03487    }
03488    AST_RWLIST_TRAVERSE(&actions, cur, list) {
03489       int ret = strcasecmp(cur->action, act->action);
03490       if (ret == 0) {
03491          ast_log(LOG_WARNING, "Manager: Action '%s' already registered\n", act->action);
03492          AST_RWLIST_UNLOCK(&actions);
03493          return -1;
03494       }
03495       if (ret > 0) { /* Insert these alphabetically */
03496          prev = cur;
03497          break;
03498       }
03499    }
03500 
03501    if (prev)
03502       AST_RWLIST_INSERT_AFTER(&actions, prev, act, list);
03503    else
03504       AST_RWLIST_INSERT_HEAD(&actions, act, list);
03505 
03506    ast_verb(2, "Manager registered action %s\n", act->action);
03507 
03508    AST_RWLIST_UNLOCK(&actions);
03509 
03510    return 0;
03511 }

int ast_manager_unregister ( char *  action  ) 

Unregister a registered manager command.

Parameters:
action Name of registered Action:

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

03447 {
03448    struct manager_action *cur;
03449    struct timespec tv = { 5, };
03450 
03451    if (AST_RWLIST_TIMEDWRLOCK(&actions, &tv)) {
03452       ast_log(LOG_ERROR, "Could not obtain lock on manager list\n");
03453       return -1;
03454    }
03455    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&actions, cur, list) {
03456       if (!strcasecmp(action, cur->action)) {
03457          AST_RWLIST_REMOVE_CURRENT(list);
03458          ast_free(cur);
03459          ast_verb(2, "Manager unregistered action %s\n", action);
03460          break;
03461       }
03462    }
03463    AST_RWLIST_TRAVERSE_SAFE_END;
03464    AST_RWLIST_UNLOCK(&actions);
03465 
03466    return 0;
03467 }

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

References AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

00274 {
00275    AST_RWLIST_WRLOCK(&manager_hooks);
00276    AST_RWLIST_REMOVE(&manager_hooks, hook, list);
00277    AST_RWLIST_UNLOCK(&manager_hooks);
00278    return;
00279 }

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

utility functions for creating AMI replies

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

00992 {
00993    va_list ap;
00994    struct ast_str *buf;
00995 
00996    if (!(buf = ast_str_thread_get(&astman_append_buf, ASTMAN_APPEND_BUF_INITSIZE)))
00997       return;
00998 
00999    va_start(ap, fmt);
01000    ast_str_set_va(&buf, 0, fmt, ap);
01001    va_end(ap);
01002 
01003    if (s->f != NULL || s->session->f != NULL) {
01004       send_string(s, ast_str_buffer(buf));
01005    } else {
01006       ast_verbose("fd == -1 in astman_append, should not happen\n");
01007    }
01008 }

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

Get header from mananger transaction.

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

00920 {
00921    return __astman_get_header(m, var, GET_HEADER_FIRST_MATCH);
00922 }

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

Get a linked list of the Variable: headers.

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

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

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

01050 {
01051    astman_send_response_full(s, m, "Error", error, NULL);
01052 }

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

References astman_send_response_full().

Referenced by action_logoff(), and action_waitevent().

01045 {
01046    astman_send_response_full(s, m, resp, msg, NULL);
01047 }

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

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

01028 {
01029    const char *id = astman_get_header(m, "ActionID");
01030 
01031    astman_append(s, "Response: %s\r\n", resp);
01032    if (!ast_strlen_zero(id))
01033       astman_append(s, "ActionID: %s\r\n", id);
01034    if (listflag)
01035       astman_append(s, "EventList: %s\r\n", listflag);   /* Start, complete, cancelled */
01036    if (msg == MSG_MOREDATA)
01037       return;
01038    else if (msg)
01039       astman_append(s, "Message: %s\r\n\r\n", msg);
01040    else
01041       astman_append(s, "\r\n");
01042 }

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

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

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

Convert authority code to a list of options.

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

00414 {
00415    int i;
00416    char *sep = "";
00417 
00418    ast_str_reset(*res);
00419    for (i = 0; i < ARRAY_LEN(perms) - 1; i++) {
00420       if (authority & perms[i].num) {
00421          ast_str_append(res, 0, "%s%s", sep, perms[i].label);
00422          sep = ",";
00423       }
00424    }
00425 
00426    if (ast_str_strlen(*res) == 0)   /* replace empty string with something sensible */
00427       ast_str_append(res, 0, "<none>");
00428 
00429    return ast_str_buffer(*res);
00430 }

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

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

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

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

Referenced by handle_show_settings().

00325 {
00326    return manager_enabled;
00327 }

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

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

00497 {
00498    struct mansession_session *session = NULL;
00499 
00500    AST_LIST_LOCK(&sessions);
00501    AST_LIST_TRAVERSE(&sessions, session, list) {
00502       if (!strcasecmp(session->username, name)) 
00503          break;
00504    }
00505    AST_LIST_UNLOCK(&sessions);
00506 
00507    return session ? 1 : 0;
00508 }

int check_webmanager_enabled ( void   ) 

Check if AMI/HTTP is enabled.

Definition at line 329 of file manager.c.

Referenced by action_coresettings(), and handle_show_settings().

00330 {
00331    return (webmanager_enabled && manager_enabled);
00332 }

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

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

00875 {
00876    AST_LIST_LOCK(&sessions);
00877    AST_LIST_REMOVE(&sessions, session, list);
00878    ast_atomic_fetchadd_int(&num_sessions, -1);
00879    free_session(session);
00880    AST_LIST_UNLOCK(&sessions);
00881 }

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

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

03190 {
03191    struct message m = { 0 };
03192    char header_buf[sizeof(s->session->inbuf)] = { '\0' };
03193    int res;
03194 
03195    for (;;) {
03196       /* Check if any events are pending and do them if needed */
03197       if (process_events(s))
03198          return -1;
03199       res = get_input(s, header_buf);
03200       if (res == 0) {
03201          continue;
03202       } else if (res > 0) {
03203          if (ast_strlen_zero(header_buf))
03204             return process_message(s, &m) ? -1 : 0;
03205          else if (m.hdrcount < (AST_MAX_MANHEADERS - 1))
03206             m.headers[m.hdrcount++] = ast_strdupa(header_buf);
03207       } else {
03208          return res;
03209       }
03210    }
03211 }

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

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

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

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

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

00855 {
00856    struct eventqent *eqe = session->last_ev;
00857    struct ast_datastore *datastore;
00858 
00859    /* Get rid of each of the data stores on the session */
00860    while ((datastore = AST_LIST_REMOVE_HEAD(&session->datastores, entry))) {
00861       /* Free the data store */
00862       ast_datastore_free(datastore);
00863    }
00864 
00865    if (session->f != NULL)
00866       fclose(session->f);
00867    ast_mutex_destroy(&session->__lock);
00868    ast_free(session);
00869    if (eqe) {
00870       ast_atomic_fetchadd_int(&eqe->usecount, -1);
00871    }
00872 }

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

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

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

00516 {
00517    struct ast_manager_user *user = NULL;
00518 
00519    AST_RWLIST_TRAVERSE(&users, user, list)
00520       if (!strcasecmp(user->username, name))
00521          break;
00522    return user;
00523 }

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

Definition at line 454 of file manager.c.

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

Referenced by __init_manager(), and strings_to_mask().

00455 {
00456    int x = 0, ret = 0;
00457 
00458    if (!instr)
00459       return 0;
00460 
00461    for (x = 0; x < ARRAY_LEN(perms); x++) {
00462       if (ast_instring(instr, perms[x].label, ','))
00463          ret |= perms[x].num;
00464    }
00465 
00466    return ret;
00467 }

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

00339 {
00340    struct eventqent *ret;
00341 
00342    AST_RWLIST_RDLOCK(&all_events);
00343    ret = AST_RWLIST_LAST(&all_events);
00344    /* the list is never empty now, but may become so when
00345     * we optimize it in the future, so be prepared.
00346     */
00347    if (ret) {
00348       ast_atomic_fetchadd_int(&ret->usecount, 1);
00349    }
00350    AST_RWLIST_UNLOCK(&all_events);
00351    return ret;
00352 }

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 808 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.

00809 {
00810    switch (cmd) {
00811    case CLI_INIT:
00812       e->command = "manager reload";
00813       e->usage =
00814          "Usage: manager reload\n"
00815          "       Reloads the manager configuration.\n";
00816       return NULL;
00817    case CLI_GENERATE:
00818       return NULL;
00819    }
00820    if (a->argc > 2)
00821       return CLI_SHOWUSAGE;
00822    reload_manager();
00823    return CLI_SUCCESS;
00824 }

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

Definition at line 589 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.

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

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

Definition at line 612 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.

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

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

Definition at line 675 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.

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

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

Definition at line 542 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.

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

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 719 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.

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

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 747 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.

00748 {
00749    struct mansession_session *session;
00750    time_t now = time(NULL);
00751 #define HSMCONN_FORMAT1 "  %-15.15s  %-15.15s  %-10.10s  %-10.10s  %-8.8s  %-8.8s  %-5.5s  %-5.5s\n"
00752 #define HSMCONN_FORMAT2 "  %-15.15s  %-15.15s  %-10d  %-10d  %-8d  %-8d  %-5.5d  %-5.5d\n"
00753    int count = 0;
00754    switch (cmd) {
00755    case CLI_INIT:
00756       e->command = "manager show connected";
00757       e->usage = 
00758          "Usage: manager show connected\n"
00759          "  Prints a listing of the users that are currently connected to the\n"
00760          "Asterisk manager interface.\n";
00761       return NULL;
00762    case CLI_GENERATE:
00763       return NULL;   
00764    }
00765 
00766    ast_cli(a->fd, HSMCONN_FORMAT1, "Username", "IP Address", "Start", "Elapsed", "FileDes", "HttpCnt", "Read", "Write");
00767 
00768    AST_LIST_LOCK(&sessions);
00769    AST_LIST_TRAVERSE(&sessions, session, list) {
00770       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);
00771       count++;
00772    }
00773    AST_LIST_UNLOCK(&sessions);
00774 
00775    ast_cli(a->fd, "%d users connected.\n", count);
00776 
00777    return CLI_SUCCESS;
00778 }

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 782 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.

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

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

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

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

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

Referenced by action_getconfigjson().

01263 {
01264    for (; *in; in++) {
01265       if (*in == '\\' || *in == '\"')
01266          *out++ = '\\';
01267       *out++ = *in;
01268    }
01269    *out = '\0';
01270 }

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

00530 {
00531    struct ast_manager_user *user = NULL;
00532    int ret = 0;
00533 
00534    AST_RWLIST_RDLOCK(&users);
00535    if ((user = get_manager_by_name_locked (session->username)))
00536       ret = user->displayconnects;
00537    AST_RWLIST_UNLOCK(&users);
00538    
00539    return ret;
00540 }

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

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

02938 {
02939    int res;
02940    const char *module = astman_get_header(m, "Module");
02941    const char *id = astman_get_header(m, "ActionID");
02942    char idText[256];
02943 #if !defined(LOW_MEMORY)
02944    const char *version;
02945 #endif
02946    char filename[PATH_MAX];
02947    char *cut;
02948 
02949    ast_copy_string(filename, module, sizeof(filename));
02950    if ((cut = strchr(filename, '.'))) {
02951       *cut = '\0';
02952    } else {
02953       cut = filename + strlen(filename);
02954    }
02955    snprintf(cut, (sizeof(filename) - strlen(filename)) - 1, ".so");
02956    ast_log(LOG_DEBUG, "**** ModuleCheck .so file %s\n", filename);
02957    res = ast_module_check(filename);
02958    if (!res) {
02959       astman_send_error(s, m, "Module not loaded");
02960       return 0;
02961    }
02962    snprintf(cut, (sizeof(filename) - strlen(filename)) - 1, ".c");
02963    ast_log(LOG_DEBUG, "**** ModuleCheck .c file %s\n", filename);
02964 #if !defined(LOW_MEMORY)
02965    version = ast_file_version_find(filename);
02966 #endif
02967 
02968    if (!ast_strlen_zero(id))
02969       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
02970    else
02971       idText[0] = '\0';
02972    astman_append(s, "Response: Success\r\n%s", idText);
02973 #if !defined(LOW_MEMORY)
02974    astman_append(s, "Version: %s\r\n\r\n", version ? version : "");
02975 #endif
02976    return 0;
02977 }

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

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

02991 {
02992    int res;
02993    const char *module = astman_get_header(m, "Module");
02994    const char *loadtype = astman_get_header(m, "LoadType");
02995 
02996    if (!loadtype || strlen(loadtype) == 0)
02997       astman_send_error(s, m, "Incomplete ModuleLoad action.");
02998    if ((!module || strlen(module) == 0) && strcasecmp(loadtype, "reload") != 0)
02999       astman_send_error(s, m, "Need module name");
03000 
03001    if (!strcasecmp(loadtype, "load")) {
03002       res = ast_load_resource(module);
03003       if (res)
03004          astman_send_error(s, m, "Could not load module.");
03005       else
03006          astman_send_ack(s, m, "Module loaded.");
03007    } else if (!strcasecmp(loadtype, "unload")) {
03008       res = ast_unload_resource(module, AST_FORCE_SOFT);
03009       if (res)
03010          astman_send_error(s, m, "Could not unload module.");
03011       else
03012          astman_send_ack(s, m, "Module unloaded.");
03013    } else if (!strcasecmp(loadtype, "reload")) {
03014       if (module != NULL) {
03015          res = ast_module_reload(module);
03016          if (res == 0)
03017             astman_send_error(s, m, "No such module.");
03018          else if (res == 1)
03019             astman_send_error(s, m, "Module does not support reload action.");
03020          else
03021             astman_send_ack(s, m, "Module reloaded.");
03022       } else {
03023          ast_module_reload(NULL);   /* Reload all modules */
03024          astman_send_ack(s, m, "All modules reloaded");
03025       }
03026    } else 
03027       astman_send_error(s, m, "Incomplete ModuleLoad action.");
03028    return 0;
03029 }

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

Definition at line 3469 of file manager.c.

References ast_get_hint(), EVENT_FLAG_CALL, and manager_event.

Referenced by __init_manager().

03470 {
03471    /* Notify managers of change */
03472    char hint[512];
03473    ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, context, exten);
03474 
03475    manager_event(EVENT_FLAG_CALL, "ExtensionStatus", "Exten: %s\r\nContext: %s\r\nHint: %s\r\nStatus: %d\r\n", exten, context, hint, state);
03476    return 0;
03477 }

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

02708 {
02709    int ret = 0;
02710 
02711    ast_mutex_lock(&s->session->__lock);
02712    if (s->session->f != NULL) {
02713       struct eventqent *eqe = s->session->last_ev;
02714 
02715       while ((eqe = advance_event(eqe))) {
02716          if (!ret && s->session->authenticated &&
02717              (s->session->readperm & eqe->category) == eqe->category &&
02718              (s->session->send_events & eqe->category) == eqe->category) {
02719             if (send_string(s, eqe->eventdata) < 0)
02720                ret = -1;   /* don't send more */
02721          }
02722          s->session->last_ev = eqe;
02723       }
02724    }
02725    ast_mutex_unlock(&s->session->__lock);
02726    return ret;
02727 }

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

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

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

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

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

static void purge_sessions ( int  n_max  )  [static]

remove at most n_max stale session from the list.

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

03313 {
03314    struct mansession_session *session;
03315    time_t now = time(NULL);
03316 
03317    AST_LIST_LOCK(&sessions);
03318    AST_LIST_TRAVERSE_SAFE_BEGIN(&sessions, session, list) {
03319       if (session->sessiontimeout && (now > session->sessiontimeout) && !session->inuse) {
03320          AST_LIST_REMOVE_CURRENT(list);
03321          ast_atomic_fetchadd_int(&num_sessions, -1);
03322          if (session->authenticated && (VERBOSITY_ATLEAST(2)) && manager_displayconnects(session)) {
03323             ast_verb(2, "HTTP Manager '%s' timed out from %s\n",
03324                session->username, ast_inet_ntoa(session->sin.sin_addr));
03325          }
03326          free_session(session);  /* XXX outside ? */
03327          if (--n_max <= 0)
03328             break;
03329       }
03330    }
03331    AST_LIST_TRAVERSE_SAFE_END;
03332    AST_LIST_UNLOCK(&sessions);
03333 }

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

00967 {
00968    if (s->f) {
00969       return ast_careful_fwrite(s->f, s->fd, string, strlen(string), s->session->writetimeout);
00970    } else {
00971       return ast_careful_fwrite(s->session->f, s->session->fd, string, strlen(string), s->session->writetimeout);
00972    }
00973 }

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 3221 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.

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

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

01075 {
01076    int maskint = strings_to_mask(eventmask);
01077 
01078    ast_mutex_lock(&s->session->__lock);
01079    if (maskint >= 0)
01080       s->session->send_events = maskint;
01081    ast_mutex_unlock(&s->session->__lock);
01082 
01083    return maskint;
01084 }

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

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

Referenced by set_eventmask().

00474 {
00475    const char *p;
00476 
00477    if (ast_strlen_zero(string))
00478       return -1;
00479 
00480    for (p = string; *p; p++)
00481       if (*p < '0' || *p > '9')
00482          break;
00483    if (!*p) /* all digits */
00484       return atoi(string);
00485    if (ast_false(string))
00486       return 0;
00487    if (ast_true(string)) { /* all permissions */
00488       int x, ret = 0;
00489       for (x = 0; x < ARRAY_LEN(perms); x++)
00490          ret |= perms[x].num;
00491       return ret;
00492    }
00493    return get_perm(string);
00494 }


Variable Documentation

int allowmultiplelogin = 1 [static]

Definition at line 132 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 982 of file manager.c.

Referenced by astman_append().

int block_sockets [static]

Definition at line 139 of file manager.c.

int broken_events_action [static]

Definition at line 135 of file manager.c.

struct ast_cli_entry cli_manager[] [static]

Definition at line 827 of file manager.c.

Referenced by __init_manager().

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

Referenced by check_blacklist().

const int DEFAULT_BLOCKSOCKETS = 0 [static]

Default setting for block-sockets

Definition at line 125 of file manager.c.

const int DEFAULT_BROKENEVENTSACTION = 0 [static]

Default setting for brokeneventsaction

Definition at line 129 of file manager.c.

const int DEFAULT_DISPLAYCONNECTS = 1 [static]

Default setting for displaying manager connections

Definition at line 126 of file manager.c.

const int DEFAULT_ENABLED = 0 [static]

Default setting for manager to be enabled

Definition at line 123 of file manager.c.

const int DEFAULT_HTTPTIMEOUT = 60 [static]

Default manager http timeout

Definition at line 128 of file manager.c.

const int DEFAULT_TIMESTAMPEVENTS = 0 [static]

Default setting for timestampevents

Definition at line 127 of file manager.c.

const int DEFAULT_WEBENABLED = 0 [static]

Default setting for the web interface to be enabled

Definition at line 124 of file manager.c.

int displayconnects [static]

Definition at line 131 of file manager.c.

int httptimeout [static]

Definition at line 134 of file manager.c.

int manager_debug [static]

enable some debugging code in the manager

Definition at line 142 of file manager.c.

int manager_enabled = 0 [static]

Definition at line 136 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 3363 of file manager.c.

Referenced by __manager_event().

char mandescr_atxfer[] [static]

Definition at line 2205 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 2297 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 2755 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 2858 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 2801 of file manager.c.

Referenced by __init_manager().

char mandescr_createconfig[] [static]

Definition at line 1580 of file manager.c.

Referenced by __init_manager().

char mandescr_events[] [static]

Definition at line 1729 of file manager.c.

Referenced by __init_manager().

char mandescr_extensionstate[] [static]

Definition at line 2632 of file manager.c.

Referenced by __init_manager().

char mandescr_getconfig[] [static]

Definition at line 1171 of file manager.c.

Referenced by __init_manager().

char mandescr_getconfigjson[] [static]

Definition at line 1272 of file manager.c.

Referenced by __init_manager().

char mandescr_getvar[] [static]

Definition at line 1886 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 1818 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 1220 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 1707 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 1773 of file manager.c.

Referenced by __init_manager().

char mandescr_mailboxcount[] [static]

Definition at line 2599 of file manager.c.

Referenced by __init_manager().

char mandescr_mailboxstatus[] [static]

Help text for manager command mailboxstatus.

Definition at line 2571 of file manager.c.

Referenced by __init_manager().

char mandescr_modulecheck[] [static]

Definition at line 2927 of file manager.c.

Referenced by __init_manager().

char mandescr_moduleload[] [static]

Definition at line 2979 of file manager.c.

Referenced by __init_manager().

char mandescr_originate[] [static]

Definition at line 2422 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 1154 of file manager.c.

Referenced by __init_manager().

char mandescr_redirect[] [static]

Definition at line 2119 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 2839 of file manager.c.

Referenced by __init_manager().

char mandescr_sendtext[] [static]

Definition at line 2078 of file manager.c.

Referenced by __init_manager().

char mandescr_setvar[] [static]

Definition at line 1842 of file manager.c.

Referenced by __init_manager().

char mandescr_status[] [static]

Definition at line 1938 of file manager.c.

Referenced by __init_manager().

char mandescr_timeout[] [static]

Definition at line 2667 of file manager.c.

Referenced by __init_manager().

char mandescr_updateconfig[] [static]

Definition at line 1489 of file manager.c.

Referenced by __init_manager().

char mandescr_userevent[] [static]

Definition at line 2729 of file manager.c.

Referenced by __init_manager().

char mandescr_waitevent[] [static]

Manager WAITEVENT.

Definition at line 1606 of file manager.c.

Referenced by __init_manager().

int num_sessions [static]

Definition at line 140 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 133 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 983 of file manager.c.

Referenced by action_userevent().

int webmanager_enabled = 0 [static]

Definition at line 137 of file manager.c.

char* words[AST_MAX_CMD_LEN] [inherited]

Definition at line 155 of file manager.c.


Generated on Sat Mar 26 11:28:04 2011 for Asterisk - the Open Source PBX by  doxygen 1.5.9