Newer
Older
/*
* Asterisk -- A telephony toolkit for Linux.
*
* Channel Management and more
*
* Copyright (C) 1999, Mark Spencer
*
* Mark Spencer <markster@linux-support.net>
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <sys/poll.h>
#include <asterisk/channel.h>
#include <asterisk/file.h>
#include <asterisk/manager.h>
#include <asterisk/config.h>
#include <asterisk/lock.h>
#include <asterisk/logger.h>
#include <asterisk/options.h>
#include <asterisk/cli.h>
struct fast_originate_helper
{
char tech[256];
char data[256];
int timeout;
char app[256];
char appdata[256];
char callerid[256];
char variable[256];
char account[256];
char context[256];
char exten[256];
int priority;
};
static int enabled = 0;
static int portno = DEFAULT_MANAGER_PORT;
static int asock = -1;
static pthread_t t;
AST_MUTEX_DEFINE_STATIC(sessionlock);
static int block_sockets = 0;
static struct permalias {
int num;
char *label;
} perms[] = {
{ EVENT_FLAG_SYSTEM, "system" },
{ EVENT_FLAG_CALL, "call" },
{ EVENT_FLAG_LOG, "log" },
{ EVENT_FLAG_VERBOSE, "verbose" },
{ EVENT_FLAG_COMMAND, "command" },
{ EVENT_FLAG_AGENT, "agent" },
{ EVENT_FLAG_USER, "user" },
static struct mansession *sessions = NULL;
static struct manager_action *first_action = NULL;
AST_MUTEX_DEFINE_STATIC(actionlock);
int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
{
/* Try to write string, but wait no more than ms milliseconds
before timing out */
int res=0;
struct pollfd fds[1];
while(len) {
res = write(fd, s, len);
if ((res < 0) && (errno != EAGAIN)) {
return -1;
}
if (res < 0) res = 0;
len -= res;
s += res;
fds[0].fd = fd;
fds[0].events = POLLOUT;
/* Wait until writable again */
res = poll(fds, 1, timeoutms);
if (res < 1)
return -1;
}
return res;
}
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
static char *complete_show_mancmd(char *line, char *word, int pos, int state)
{
struct manager_action *cur = first_action;
int which = 0;
ast_mutex_lock(&actionlock);
while (cur) { /* Walk the list of actions */
if (!strncasecmp(word, cur->action, strlen(word))) {
if (++which > state) {
char *ret = strdup(cur->action);
ast_mutex_unlock(&actionlock);
return ret;
}
}
cur = cur->next;
}
ast_mutex_unlock(&actionlock);
return NULL;
}
static int handle_showmancmd(int fd, int argc, char *argv[])
{
struct manager_action *cur = first_action;
int num;
if (argc != 4)
return RESULT_SHOWUSAGE;
ast_mutex_lock(&actionlock);
while (cur) { /* Walk the list of actions */
for (num = 3; num < argc; num++) {
if (!strcasecmp(cur->action, argv[num])) {
ast_cli(fd, "Action: %s\nSynopsis: %s\n%s\n", cur->action, cur->synopsis, cur->description ? cur->description : "");
}
}
cur = cur->next;
}
ast_mutex_unlock(&actionlock);
return RESULT_SUCCESS;
}
static int handle_showmancmds(int fd, int argc, char *argv[])
{
struct manager_action *cur = first_action;
ast_mutex_lock(&actionlock);
while (cur) { /* Walk the list of actions */
ast_cli(fd, format, cur->action, cur->synopsis);
ast_mutex_unlock(&actionlock);
static int handle_showmanconn(int fd, int argc, char *argv[])
{
struct mansession *s;
Mark Spencer
committed
char iabuf[80];
ast_mutex_lock(&sessionlock);
ast_cli(fd, format, "Username", "IP Address");
while (s) {
Mark Spencer
committed
ast_cli(fd, format,s->username, ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
ast_mutex_unlock(&sessionlock);
static char showmancmd_help[] =
"Usage: show manager command <actionname>\n"
" Shows the detailed description for a specific manager command.\n";
static char showmancmds_help[] =
"Usage: show manager commands\n"
" Prints a listing of all the available manager commands.\n";
static char showmanconn_help[] =
"Usage: show manager connected\n"
" Prints a listing of the users that are connected to the\n"
"manager interface.\n";
static struct ast_cli_entry show_mancmd_cli =
{ { "show", "manager", "command", NULL },
handle_showmancmd, "Show manager command", showmancmd_help, complete_show_mancmd };
static struct ast_cli_entry show_mancmds_cli =
{ { "show", "manager", "commands", NULL },
handle_showmancmds, "Show manager commands", showmancmds_help };
static struct ast_cli_entry show_manconn_cli =
{ { "show", "manager", "connected", NULL },
handle_showmanconn, "Show connected manager users", showmanconn_help };
static void destroy_session(struct mansession *s)
{
struct mansession *cur, *prev = NULL;
ast_mutex_lock(&sessionlock);
cur = sessions;
while(cur) {
if (cur == s)
break;
prev = cur;
cur = cur->next;
}
if (cur) {
if (prev)
prev->next = cur->next;
else
sessions = cur->next;
if (s->fd > -1)
close(s->fd);
Mark Spencer
committed
ast_mutex_destroy(&s->lock);
free(s);
} else
ast_log(LOG_WARNING, "Trying to delete non-existant session %p?\n", s);
ast_mutex_unlock(&sessionlock);
char *astman_get_header(struct message *m, char *var)
{
char cmp[80];
int x;
snprintf(cmp, sizeof(cmp), "%s: ", var);
for (x=0;x<m->hdrcount;x++)
if (!strncasecmp(cmp, m->headers[x], strlen(cmp)))
return m->headers[x] + strlen(cmp);
return "";
}
void astman_send_error(struct mansession *s, struct message *m, char *error)
char *id = astman_get_header(m,"ActionID");
ast_mutex_lock(&s->lock);
ast_mutex_unlock(&s->lock);
void astman_send_response(struct mansession *s, struct message *m, char *resp, char *msg)
char *id = astman_get_header(m,"ActionID");
ast_mutex_lock(&s->lock);
if (msg)
ast_cli(s->fd, "Message: %s\r\n\r\n", msg);
else
ast_cli(s->fd, "\r\n");
ast_mutex_unlock(&s->lock);
void astman_send_ack(struct mansession *s, struct message *m, char *msg)
astman_send_response(s, m, "Success", msg);
}
static int get_perm(char *instr)
{
char tmp[256];
char *c;
int x;
int ret = 0;
while(c) {
for (x=0;x<sizeof(perms) / sizeof(perms[0]);x++) {
if (!strcasecmp(perms[x].label, c))
ret |= perms[x].num;
}
static int set_eventmask(struct mansession *s, char *eventmask)
{
if (!eventmask)
return -1;
if (!strcasecmp(eventmask, "on") || ast_true(eventmask)) {
ast_mutex_lock(&s->lock);
s->send_events = 1;
ast_mutex_unlock(&s->lock);
return 1;
} else if (!strcasecmp(eventmask, "off") || ast_false(eventmask)) {
ast_mutex_lock(&s->lock);
s->send_events = 0;
ast_mutex_unlock(&s->lock);
return 0;
}
return -1;
}
static int authenticate(struct mansession *s, struct message *m)
{
struct ast_config *cfg;
Mark Spencer
committed
char iabuf[80];
char *user = astman_get_header(m, "Username");
char *pass = astman_get_header(m, "Secret");
char *authtype = astman_get_header(m, "AuthType");
char *key = astman_get_header(m, "Key");
char *events = astman_get_header(m, "Events");
cfg = ast_load("manager.conf");
if (!cfg)
return -1;
cat = ast_category_browse(cfg, NULL);
while(cat) {
if (strcasecmp(cat, "general")) {
/* This is a user */
if (!strcasecmp(cat, user)) {
struct ast_variable *v;
struct ast_ha *ha = NULL;
char *password = NULL;
v = ast_variable_browse(cfg, cat);
while (v) {
if (!strcasecmp(v->name, "secret")) {
password = v->value;
} else if (!strcasecmp(v->name, "permit") ||
!strcasecmp(v->name, "deny")) {
ha = ast_append_ha(v->name, v->value, ha);
}
v = v->next;
}
if (ha && !ast_apply_ha(ha, &(s->sin))) {
Mark Spencer
committed
ast_log(LOG_NOTICE, "%s failed to pass IP ACL as '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), user);
ast_free_ha(ha);
ast_destroy(cfg);
return -1;
} else if (ha)
ast_free_ha(ha);
if (key && !ast_strlen_zero(key) && s->challenge) {
int x;
int len=0;
char md5key[256] = "";
struct MD5Context md5;
unsigned char digest[16];
MD5Init(&md5);
MD5Update(&md5, s->challenge, strlen(s->challenge));
MD5Update(&md5, password, strlen(password));
MD5Final(digest, &md5);
for (x=0;x<16;x++)
len += sprintf(md5key + len, "%2.2x", digest[x]);
if (!strcmp(md5key, key))
break;
else {
ast_destroy(cfg);
return -1;
}
}
} else if (password && !strcasecmp(password, pass)) {
Mark Spencer
committed
ast_log(LOG_NOTICE, "%s failed to authenticate as '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), user);
ast_destroy(cfg);
return -1;
}
}
}
cat = ast_category_browse(cfg, cat);
}
if (cat) {
strncpy(s->username, cat, sizeof(s->username) - 1);
s->readperm = get_perm(ast_variable_retrieve(cfg, cat, "read"));
s->writeperm = get_perm(ast_variable_retrieve(cfg, cat, "write"));
ast_destroy(cfg);
if (events)
set_eventmask(s, events);
Mark Spencer
committed
ast_log(LOG_NOTICE, "%s tried to authenticate with non-existant user '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), user);
static char mandescr_ping[] =
"Description: A 'Ping' action will ellicit a 'Pong' response. Used to keep the "
" manager connection open.\n"
"Variables: NONE\n";
static int action_ping(struct mansession *s, struct message *m)
{
astman_send_response(s, m, "Pong", NULL);
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
static char mandescr_listcommands[] =
"Description: Returns the action name and synopsis for every\n"
" action that is available to the user\n"
"Variables: NONE\n";
static int action_listcommands(struct mansession *s, struct message *m)
{
struct manager_action *cur = first_action;
char idText[256] = "";
char *id = astman_get_header(m,"ActionID");
if (id && !ast_strlen_zero(id))
snprintf(idText,256,"ActionID: %s\r\n",id);
ast_cli(s->fd, "Response: Success\r\n%s", idText);
ast_mutex_lock(&actionlock);
while (cur) { /* Walk the list of actions */
if ((s->writeperm & cur->authority) == cur->authority)
ast_cli(s->fd, "%s: %s\r\n", cur->action, cur->synopsis);
cur = cur->next;
}
ast_mutex_unlock(&actionlock);
ast_cli(s->fd, "\r\n");
return 0;
}
static char mandescr_events[] =
"Description: Enable/Disable sending of events to this manager\n"
" client.\n"
"Variables:\n"
" EventMask: 'on' if events should be sent, 'off' if not\n";
static int action_events(struct mansession *s, struct message *m)
{
char *mask = astman_get_header(m, "EventMask");
int res;
res = set_eventmask(s, mask);
if (res > 0)
astman_send_response(s, m, "Events On", NULL);
else if (res == 0)
astman_send_response(s, m, "Events Off", NULL);
else
astman_send_response(s, m, "EventMask parse error", NULL);
static char mandescr_logoff[] =
"Description: Logoff this manager session\n"
"Variables: NONE\n";
static int action_logoff(struct mansession *s, struct message *m)
{
astman_send_response(s, m, "Goodbye", "Thanks for all the fish.");
static char mandescr_hangup[] =
"Description: Hangup a channel\n"
"Variables: \n"
" Channel: The channel name to be hungup\n";
static int action_hangup(struct mansession *s, struct message *m)
{
struct ast_channel *c = NULL;
char *name = astman_get_header(m, "Channel");
astman_send_error(s, m, "No channel specified");
c = ast_channel_walk_locked(NULL);
while(c) {
if (!strcasecmp(c->name, name)) {
break;
}
ast_mutex_unlock(&c->lock);
c = ast_channel_walk_locked(c);
astman_send_error(s, m, "No such channel");
return 0;
}
ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
ast_mutex_unlock(&c->lock);
astman_send_ack(s, m, "Channel Hungup");
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
static int action_setvar(struct mansession *s, struct message *m)
{
struct ast_channel *c = NULL;
char *name = astman_get_header(m, "Channel");
char *varname = astman_get_header(m, "Variable");
char *varval = astman_get_header(m, "Value");
if (!strlen(name)) {
astman_send_error(s, m, "No channel specified");
return 0;
}
if (!strlen(varname)) {
astman_send_error(s, m, "No variable specified");
return 0;
}
c = ast_channel_walk_locked(NULL);
while(c) {
if (!strcasecmp(c->name, name)) {
break;
}
ast_mutex_unlock(&c->lock);
c = ast_channel_walk_locked(c);
}
if (!c) {
astman_send_error(s, m, "No such channel");
return 0;
}
pbx_builtin_setvar_helper(c,varname,varval);
ast_mutex_unlock(&c->lock);
astman_send_ack(s, m, "Variable Set");
return 0;
}
static int action_getvar(struct mansession *s, struct message *m)
{
struct ast_channel *c = NULL;
char *name = astman_get_header(m, "Channel");
char *varname = astman_get_header(m, "Variable");
char *varval;
if (!strlen(name)) {
astman_send_error(s, m, "No channel specified");
return 0;
}
if (!strlen(varname)) {
astman_send_error(s, m, "No variable specified");
return 0;
}
c = ast_channel_walk_locked(NULL);
while(c) {
if (!strcasecmp(c->name, name)) {
break;
}
ast_mutex_unlock(&c->lock);
c = ast_channel_walk_locked(c);
}
if (!c) {
astman_send_error(s, m, "No such channel");
return 0;
}
varval=pbx_builtin_getvar_helper(c,varname);
ast_mutex_unlock(&c->lock);
ast_cli(s->fd, "Response: Success\r\n"
"%s: %s",varname,varval);
return 0;
}
static int action_status(struct mansession *s, struct message *m)
{
char *id = astman_get_header(m,"ActionID");
Mark Spencer
committed
char *name = astman_get_header(m,"Channel");
astman_send_ack(s, m, "Channel status will follow");
c = ast_channel_walk_locked(NULL);
snprintf(idText,256,"ActionID: %s\r\n",id);
Mark Spencer
committed
if (name && !ast_strlen_zero(name)) {
while (c) {
if (!strcasecmp(c->name, name)) {
break;
}
ast_mutex_unlock(&c->lock);
c = ast_channel_walk_locked(c);
}
if (!c) {
astman_send_error(s, m, "No such channel");
return 0;
}
}
while(c) {
if (c->bridge)
snprintf(bridge, sizeof(bridge), "Link: %s\r\n", c->bridge->name);
else
strcpy(bridge, "");
if (c->pbx) {
ast_cli(s->fd,
"Event: Status\r\n"
"Channel: %s\r\n"
"CallerID: %s\r\n"
Mark Spencer
committed
"Account: %s\r\n"
"State: %s\r\n"
"Context: %s\r\n"
"Extension: %s\r\n"
"Priority: %d\r\n"
"%s"
Mark Spencer
committed
c->accountcode,
c->exten, c->priority, bridge, c->uniqueid, idText);
} else {
ast_cli(s->fd,
"Event: Status\r\n"
"Channel: %s\r\n"
"CallerID: %s\r\n"
Mark Spencer
committed
"Account: %s\r\n"
Mark Spencer
committed
c->accountcode,
ast_state2str(c->_state), bridge, c->uniqueid, idText);
ast_mutex_unlock(&c->lock);
Mark Spencer
committed
if (name && !ast_strlen_zero(name)) {
break;
}
c = ast_channel_walk_locked(c);
ast_cli(s->fd,
"Event: StatusComplete\r\n"
"%s"
"\r\n",idText);
return 0;
}
static int action_redirect(struct mansession *s, struct message *m)
{
char *name = astman_get_header(m, "Channel");
char *name2 = astman_get_header(m, "ExtraChannel");
char *exten = astman_get_header(m, "Exten");
char *context = astman_get_header(m, "Context");
char *priority = astman_get_header(m, "Priority");
struct ast_channel *chan, *chan2 = NULL;
astman_send_error(s, m, "Channel not specified");
if (!ast_strlen_zero(priority) && (sscanf(priority, "%d", &pi) != 1)) {
astman_send_error(s, m, "Invalid priority\n");
chan = ast_get_channel_by_name_locked(name);
if (!chan) {
astman_send_error(s, m, "Channel not existant");
return 0;
}
chan2 = ast_get_channel_by_name_locked(name2);
res = ast_async_goto(chan, context, exten, pi);
if (chan2)
res = ast_async_goto(chan2, context, exten, pi);
else
res = -1;
astman_send_ack(s, m, "Dual Redirect successful");
astman_send_error(s, m, "Secondary redirect failed");
astman_send_ack(s, m, "Redirect successful");
astman_send_error(s, m, "Redirect failed");
if (chan)
ast_mutex_unlock(&chan->lock);
if (chan2)
ast_mutex_unlock(&chan2->lock);
static int action_command(struct mansession *s, struct message *m)
{
char *cmd = astman_get_header(m, "Command");
char *id = astman_get_header(m, "ActionID");
ast_mutex_lock(&s->lock);
ast_mutex_unlock(&s->lock);
ast_cli(s->fd, "ActionID: %s\r\n", id);
/* FIXME: Wedge a ActionID response in here, waiting for later changes */
ast_cli_command(s->fd, cmd);
ast_cli(s->fd, "--END COMMAND--\r\n\r\n");
ast_mutex_lock(&s->lock);
ast_mutex_unlock(&s->lock);
static void *fast_originate(void *data)
{
struct fast_originate_helper *in = data;
int res;
int reason = 0;
if (!ast_strlen_zero(in->app)) {
res = ast_pbx_outgoing_app(in->tech, AST_FORMAT_SLINEAR, in->data, in->timeout, in->app, in->appdata, &reason, 1, !ast_strlen_zero(in->callerid) ? in->callerid : NULL, in->variable, in->account);
} else {
res = ast_pbx_outgoing_exten(in->tech, AST_FORMAT_SLINEAR, in->data, in->timeout, in->context, in->exten, in->priority, &reason, 1, !ast_strlen_zero(in->callerid) ? in->callerid : NULL, in->variable, in->account);
}
free(in);
return NULL;
}
static char mandescr_originate[] =
"Description: Generates an outgoing call to a Extension/Context/Priority or\n"
" Application/Data\n"
"Variables: (Names marked with * are required)\n"
" *Channel: Channel name to call\n"
" Exten: Extension to use (requires 'Context' and 'Priority')\n"
" Context: Context to use (requires 'Exten' and 'Priority')\n"
" Priority: Priority to use (requires 'Exten' and 'Context')\n"
" Application: Application to use\n"
" Data: Data to use (requires 'Application')\n"
" Timeout: How long to wait for call to be answered (in ms)\n"
" CallerID: Caller ID to be set on the outgoing channel\n"
" Variable: Channel variable to set (VAR1=value1|VAR2=value2)\n"
" Account: Account code\n"
" Async: Set to 'true' for fast origination\n";
static int action_originate(struct mansession *s, struct message *m)
{
char *name = astman_get_header(m, "Channel");
char *exten = astman_get_header(m, "Exten");
char *context = astman_get_header(m, "Context");
char *priority = astman_get_header(m, "Priority");
char *timeout = astman_get_header(m, "Timeout");
char *callerid = astman_get_header(m, "CallerID");
char *variable = astman_get_header(m, "Variable");
char *account = astman_get_header(m, "Account");
char *app = astman_get_header(m, "Application");
char *appdata = astman_get_header(m, "Data");
char *async = astman_get_header(m, "Async");
char *tech, *data;
int pi = 0;
int res;
int to = 30000;
int reason = 0;
char tmp[256];
pthread_t th;
pthread_attr_t attr;
astman_send_error(s, m, "Channel not specified");
if (!ast_strlen_zero(priority) && (sscanf(priority, "%d", &pi) != 1)) {
astman_send_error(s, m, "Invalid priority\n");
if (!ast_strlen_zero(timeout) && (sscanf(timeout, "%d", &to) != 1)) {
astman_send_error(s, m, "Invalid timeout\n");
return 0;
}
strncpy(tmp, name, sizeof(tmp) - 1);
tech = tmp;
data = strchr(tmp, '/');
if (!data) {
astman_send_error(s, m, "Invalid channel\n");
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
if (ast_true(async))
{
struct fast_originate_helper *fast = malloc(sizeof(struct fast_originate_helper));
if (!fast)
{
res = -1;
}
else
{
memset(fast, 0, sizeof(struct fast_originate_helper));
strncpy(fast->tech, tech, sizeof(fast->tech) - 1);
strncpy(fast->data, data, sizeof(fast->data) - 1);
strncpy(fast->app, app, sizeof(fast->app) - 1);
strncpy(fast->appdata, appdata, sizeof(fast->appdata) - 1);
strncpy(fast->callerid, callerid, sizeof(fast->callerid) - 1);
strncpy(fast->variable, variable, sizeof(fast->variable) - 1);
strncpy(fast->account, account, sizeof(fast->account) - 1);
strncpy(fast->context, context, sizeof(fast->context) - 1);
strncpy(fast->exten, exten, sizeof(fast->exten) - 1);
fast->timeout = to;
fast->priority = pi;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&th, &attr, fast_originate, fast))
{
res = -1;
}
else
{
res = 0;
}
}
} else if (!ast_strlen_zero(app)) {
res = ast_pbx_outgoing_app(tech, AST_FORMAT_SLINEAR, data, to, app, appdata, &reason, 0, !ast_strlen_zero(callerid) ? callerid : NULL, variable, account);
} else {
if (exten && context && pi)
res = ast_pbx_outgoing_exten(tech, AST_FORMAT_SLINEAR, data, to, context, exten, pi, &reason, 0, !ast_strlen_zero(callerid) ? callerid : NULL, variable, account);
else {
astman_send_error(s, m, "Originate with 'Exten' requires 'Context' and 'Priority'");
return 0;
}
astman_send_ack(s, m, "Originate successfully queued");
astman_send_error(s, m, "Originate failed");
static int action_mailboxstatus(struct mansession *s, struct message *m)
{
char *mailbox = astman_get_header(m, "Mailbox");
char *id = astman_get_header(m,"ActionID");
char idText[256] = "";
if (!mailbox || ast_strlen_zero(mailbox)) {
astman_send_error(s, m, "Mailbox not specified");
snprintf(idText,256,"ActionID: %s\r\n",id);
"Message: Mailbox Status\r\n"
"Mailbox: %s\r\n"
"Waiting: %d\r\n\r\n", idText, mailbox, ast_app_has_voicemail(mailbox));
return 0;
}
static int action_mailboxcount(struct mansession *s, struct message *m)
{
char *mailbox = astman_get_header(m, "Mailbox");
char *id = astman_get_header(m,"ActionID");
char idText[256] = "";
if (!mailbox || ast_strlen_zero(mailbox)) {
astman_send_error(s, m, "Mailbox not specified");
return 0;
}
ast_app_messagecount(mailbox, &newmsgs, &oldmsgs);
snprintf(idText,256,"ActionID: %s\r\n",id);
}
ast_cli(s->fd, "Response: Success\r\n"
"Message: Mailbox Message Count\r\n"
"Mailbox: %s\r\n"
"NewMessages: %d\r\n"
"OldMessages: %d\r\n"
"\r\n",
static int action_extensionstate(struct mansession *s, struct message *m)
{
char *exten = astman_get_header(m, "Exten");
char *context = astman_get_header(m, "Context");
char *id = astman_get_header(m,"ActionID");
char idText[256] = "";
if (!exten || ast_strlen_zero(exten)) {
astman_send_error(s, m, "Extension not specified");
return 0;
}
if (!context || ast_strlen_zero(context))
context = "default";
status = ast_extension_state(NULL, context, exten);
ast_get_hint(hint, sizeof(hint) - 1, NULL, context, exten);
snprintf(idText,256,"ActionID: %s\r\n",id);
}
"Message: Extension Status\r\n"
"Exten: %s\r\n"
"Context: %s\r\n"
"Hint: %s\r\n"
"Status: %d\r\n\r\n",
idText,exten, context, hint, status);
static int action_timeout(struct mansession *s, struct message *m)
{
struct ast_channel *c = NULL;
char *name = astman_get_header(m, "Channel");
int timeout = atoi(astman_get_header(m, "Timeout"));
astman_send_error(s, m, "No channel specified");
astman_send_error(s, m, "No timeout specified");
c = ast_channel_walk_locked(NULL);
while(c) {
if (!strcasecmp(c->name, name)) {
break;
}
ast_mutex_unlock(&c->lock);
c = ast_channel_walk_locked(c);
astman_send_error(s, m, "No such channel");
return 0;
}
ast_channel_setwhentohangup(c, timeout);
ast_mutex_unlock(&c->lock);
static int process_message(struct mansession *s, struct message *m)
{
char action[80];
char *id = astman_get_header(m,"ActionID");
char idText[256] = "";
Mark Spencer
committed
char iabuf[80];
strncpy(action, astman_get_header(m, "Action"), sizeof(action));
ast_log( LOG_DEBUG, "Manager received command '%s'\n", action );
astman_send_error(s, m, "Missing action in request");
snprintf(idText,256,"ActionID: %s\r\n",id);
}
if (!strcasecmp(action, "Challenge")) {
char *authtype;
authtype = astman_get_header(m, "AuthType");
if (!s->challenge || ast_strlen_zero(s->challenge)) {
ast_mutex_lock(&s->lock);
snprintf(s->challenge, sizeof(s->challenge), "%d", rand());
ast_mutex_unlock(&s->lock);
ast_cli(s->fd, "Response: Success\r\n"
"%s"
"Challenge: %s\r\n\r\n",
idText,s->challenge);
astman_send_error(s, m, "Must specify AuthType");
return 0;
}
} else if (!strcasecmp(action, "Login")) {
astman_send_error(s, m, "Authentication failed");
return -1;
} else {
s->authenticated = 1;
if (option_verbose > 1)
Mark Spencer
committed
ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged on from %s\n", s->username, ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
ast_log(LOG_EVENT, "Manager '%s' logged on from %s\n", s->username, ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
astman_send_ack(s, m, "Authentication accepted");
} else if (!strcasecmp(action, "Logoff")) {
astman_send_error(s, m, "Authentication Required");
while( tmp ) {
if (!strcasecmp(action, tmp->action)) {
if ((s->writeperm & tmp->authority) == tmp->authority) {
if (tmp->func(s, m))
astman_send_error(s, m, "Permission denied");