1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 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 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
|
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2005, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief The Asterisk Management Interface - AMI
*
* Channel Management and more
*
* \ref amiconf
*/
/*! \addtogroup Group_AMI AMI functions
*/
/*! @{
Doxygen group */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 9581 $")
#include "asterisk/channel.h"
#include "asterisk/file.h"
#include "asterisk/manager.h"
#include "asterisk/config.h"
#include "asterisk/callerid.h"
#include "asterisk/lock.h"
#include "asterisk/logger.h"
#include "asterisk/options.h"
#include "asterisk/cli.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/md5.h"
#include "asterisk/acl.h"
#include "asterisk/utils.h"
struct fast_originate_helper {
char tech[AST_MAX_MANHEADER_LEN];
char data[AST_MAX_MANHEADER_LEN];
int timeout;
char app[AST_MAX_APP];
char appdata[AST_MAX_MANHEADER_LEN];
char cid_name[AST_MAX_MANHEADER_LEN];
char cid_num[AST_MAX_MANHEADER_LEN];
char context[AST_MAX_CONTEXT];
char exten[AST_MAX_EXTENSION];
char idtext[AST_MAX_MANHEADER_LEN];
char account[AST_MAX_ACCOUNT_CODE];
int priority;
struct ast_variable *vars;
};
static int enabled = 0;
static int portno = DEFAULT_MANAGER_PORT;
static int asock = -1;
static int displayconnects = 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" },
{ -1, "all" },
{ 0, "none" },
};
static struct mansession *sessions = NULL;
static struct manager_action *first_action = NULL;
AST_MUTEX_DEFINE_STATIC(actionlock);
/*! If you are calling ast_carefulwrite, it is assumed that you are calling
it on a file descriptor that _DOES_ have NONBLOCK set. This way,
there is only one system call made to do a write, unless we actually
have a need to wait. This way, we get better performance. */
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;
res = 0;
if (len) {
fds[0].fd = fd;
fds[0].events = POLLOUT;
/* Wait until writable again */
res = poll(fds, 1, timeoutms);
if (res < 1)
return -1;
}
}
return res;
}
/*! authority_to_str: Convert authority code to string with serveral options */
static char *authority_to_str(int authority, char *res, int reslen)
{
int running_total = 0, i;
memset(res, 0, reslen);
for (i=0; i<sizeof(perms) / sizeof(perms[0]) - 1; i++) {
if (authority & perms[i].num) {
if (*res) {
strncat(res, ",", (reslen > running_total) ? reslen - running_total : 0);
running_total++;
}
strncat(res, perms[i].label, (reslen > running_total) ? reslen - running_total : 0);
running_total += strlen(perms[i].label);
}
}
if (ast_strlen_zero(res)) {
ast_copy_string(res, "<none>", reslen);
}
return res;
}
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;
char authority[80];
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\nPrivilege: %s\n%s\n", cur->action, cur->synopsis, authority_to_str(cur->authority, authority, sizeof(authority) -1), cur->description ? cur->description : "");
}
}
cur = cur->next;
}
ast_mutex_unlock(&actionlock);
return RESULT_SUCCESS;
}
/*! \brief handle_showmancmds: CLI command */
/* Should change to "manager show commands" */
static int handle_showmancmds(int fd, int argc, char *argv[])
{
struct manager_action *cur = first_action;
char authority[80];
char *format = " %-15.15s %-15.15s %-55.55s\n";
ast_mutex_lock(&actionlock);
ast_cli(fd, format, "Action", "Privilege", "Synopsis");
ast_cli(fd, format, "------", "---------", "--------");
while (cur) { /* Walk the list of actions */
ast_cli(fd, format, cur->action, authority_to_str(cur->authority, authority, sizeof(authority) -1), cur->synopsis);
cur = cur->next;
}
ast_mutex_unlock(&actionlock);
return RESULT_SUCCESS;
}
/*! \brief handle_showmanconn: CLI command show manager connected */
/* Should change to "manager show connected" */
static int handle_showmanconn(int fd, int argc, char *argv[])
{
struct mansession *s;
char iabuf[INET_ADDRSTRLEN];
char *format = " %-15.15s %-15.15s\n";
ast_mutex_lock(&sessionlock);
s = sessions;
ast_cli(fd, format, "Username", "IP Address");
while (s) {
ast_cli(fd, format,s->username, ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
s = s->next;
}
ast_mutex_unlock(&sessionlock);
return RESULT_SUCCESS;
}
static char showmancmd_help[] =
"Usage: show manager command <actionname>\n"
" Shows the detailed description for a specific Asterisk manager interface command.\n";
static char showmancmds_help[] =
"Usage: show manager commands\n"
" Prints a listing of all the available Asterisk manager interface commands.\n";
static char showmanconn_help[] =
"Usage: show manager connected\n"
" Prints a listing of the users that are currently connected to the\n"
"Asterisk manager interface.\n";
static struct ast_cli_entry show_mancmd_cli =
{ { "show", "manager", "command", NULL },
handle_showmancmd, "Show a manager interface command", showmancmd_help, complete_show_mancmd };
static struct ast_cli_entry show_mancmds_cli =
{ { "show", "manager", "commands", NULL },
handle_showmancmds, "List manager interface commands", showmancmds_help };
static struct ast_cli_entry show_manconn_cli =
{ { "show", "manager", "connected", NULL },
handle_showmanconn, "Show connected manager interface users", showmanconn_help };
static void free_session(struct mansession *s)
{
struct eventqent *eqe;
if (s->fd > -1)
close(s->fd);
ast_mutex_destroy(&s->__lock);
while(s->eventq) {
eqe = s->eventq;
s->eventq = s->eventq->next;
free(eqe);
}
free(s);
}
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;
free_session(s);
} else
ast_log(LOG_WARNING, "Trying to delete nonexistent 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 "";
}
struct ast_variable *astman_get_variables(struct message *m)
{
int varlen, x, y;
struct ast_variable *head = NULL, *cur;
char *var, *val;
unsigned int var_count;
char *vars[32];
varlen = strlen("Variable: ");
for (x = 0; x < m->hdrcount; x++) {
if (strncasecmp("Variable: ", m->headers[x], varlen))
continue;
if (!(var = ast_strdupa(m->headers[x] + varlen)))
return head;
if ((var_count = ast_app_separate_args(var, '|', vars, sizeof(vars) / sizeof(vars[0])))) {
for (y = 0; y < var_count; y++) {
if (!vars[y])
continue;
var = val = ast_strdupa(vars[y]);
strsep(&val, "=");
if (!val || ast_strlen_zero(var))
continue;
cur = ast_variable_new(var, val);
if (head) {
cur->next = head;
head = cur;
} else
head = cur;
}
}
}
return head;
}
/*! NOTE:
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->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.
*/
void astman_send_error(struct mansession *s, struct message *m, char *error)
{
char *id = astman_get_header(m,"ActionID");
ast_cli(s->fd, "Response: Error\r\n");
if (!ast_strlen_zero(id))
ast_cli(s->fd, "ActionID: %s\r\n",id);
ast_cli(s->fd, "Message: %s\r\n\r\n", error);
}
void astman_send_response(struct mansession *s, struct message *m, char *resp, char *msg)
{
char *id = astman_get_header(m,"ActionID");
ast_cli(s->fd, "Response: %s\r\n", resp);
if (!ast_strlen_zero(id))
ast_cli(s->fd, "ActionID: %s\r\n",id);
if (msg)
ast_cli(s->fd, "Message: %s\r\n\r\n", msg);
else
ast_cli(s->fd, "\r\n");
}
void astman_send_ack(struct mansession *s, struct message *m, char *msg)
{
astman_send_response(s, m, "Success", msg);
}
/*! 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 */
static int ast_instring(char *bigstr, char *smallstr, char delim)
{
char *val = bigstr, *next;
do {
if ((next = strchr(val, delim))) {
if (!strncmp(val, smallstr, (next - val)))
return 1;
else
continue;
} else
return !strcmp(smallstr, val);
} while (*(val = (next + 1)));
return 0;
}
static int get_perm(char *instr)
{
int x = 0, ret = 0;
if (!instr)
return 0;
for (x=0; x<sizeof(perms) / sizeof(perms[0]); x++)
if (ast_instring(instr, perms[x].label, ','))
ret |= perms[x].num;
return ret;
}
static int ast_is_number(char *string)
{
int ret = 1, x = 0;
if (!string)
return 0;
for (x=0; x < strlen(string); x++) {
if (!(string[x] >= 48 && string[x] <= 57)) {
ret = 0;
break;
}
}
return ret ? atoi(string) : 0;
}
static int ast_strings_to_mask(char *string)
{
int x, ret = -1;
x = ast_is_number(string);
if (x) {
ret = x;
} else if (ast_strlen_zero(string)) {
ret = -1;
} else if (ast_false(string)) {
ret = 0;
} else if (ast_true(string)) {
ret = 0;
for (x=0; x<sizeof(perms) / sizeof(perms[0]); x++)
ret |= perms[x].num;
} else {
ret = 0;
for (x=0; x<sizeof(perms) / sizeof(perms[0]); x++) {
if (ast_instring(string, perms[x].label, ','))
ret |= perms[x].num;
}
}
return ret;
}
/*!
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 set_eventmask(struct mansession *s, char *eventmask)
{
int maskint = ast_strings_to_mask(eventmask);
ast_mutex_lock(&s->__lock);
if (maskint >= 0)
s->send_events = maskint;
ast_mutex_unlock(&s->__lock);
return maskint;
}
static int authenticate(struct mansession *s, struct message *m)
{
struct ast_config *cfg;
char iabuf[INET_ADDRSTRLEN];
char *cat;
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_config_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);
} else if (!strcasecmp(v->name, "writetimeout")) {
int val = atoi(v->value);
if (val < 100)
ast_log(LOG_WARNING, "Invalid writetimeout value '%s' at line %d\n", v->value, v->lineno);
else
s->writetimeout = val;
}
v = v->next;
}
if (ha && !ast_apply_ha(ha, &(s->sin))) {
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_config_destroy(cfg);
return -1;
} else if (ha)
ast_free_ha(ha);
if (!strcasecmp(authtype, "MD5")) {
if (!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, (unsigned char *) s->challenge, strlen(s->challenge));
MD5Update(&md5, (unsigned char *) 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_config_destroy(cfg);
return -1;
}
}
} else if (password && !strcasecmp(password, pass)) {
break;
} else {
ast_log(LOG_NOTICE, "%s failed to authenticate as '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), user);
ast_config_destroy(cfg);
return -1;
}
}
}
cat = ast_category_browse(cfg, cat);
}
if (cat) {
ast_copy_string(s->username, cat, sizeof(s->username));
s->readperm = get_perm(ast_variable_retrieve(cfg, cat, "read"));
s->writeperm = get_perm(ast_variable_retrieve(cfg, cat, "write"));
ast_config_destroy(cfg);
if (events)
set_eventmask(s, events);
return 0;
}
ast_log(LOG_NOTICE, "%s tried to authenticate with nonexistent user '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), user);
ast_config_destroy(cfg);
return -1;
}
/*! \brief PING: Manager PING */
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);
return 0;
}
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 temp[BUFSIZ];
char *id = astman_get_header(m,"ActionID");
if (!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 (Priv: %s)\r\n", cur->action, cur->synopsis, authority_to_str(cur->authority, temp, sizeof(temp)) );
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 all events should be sent,\n"
" 'off' if no events should be sent,\n"
" 'system,call,log' to select which flags events should have to be sent.\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);
return 0;
}
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.");
return -1;
}
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");
if (ast_strlen_zero(name)) {
astman_send_error(s, m, "No channel specified");
return 0;
}
c = ast_get_channel_by_name_locked(name);
if (!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");
return 0;
}
static char mandescr_setvar[] =
"Description: Set a global or local channel variable.\n"
"Variables: (Names marked with * are required)\n"
" Channel: Channel to set variable for\n"
" *Variable: Variable name\n"
" *Value: Value\n";
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 (ast_strlen_zero(varname)) {
astman_send_error(s, m, "No variable specified");
return 0;
}
if (ast_strlen_zero(varval)) {
astman_send_error(s, m, "No value specified");
return 0;
}
if (!ast_strlen_zero(name)) {
c = ast_get_channel_by_name_locked(name);
if (!c) {
astman_send_error(s, m, "No such channel");
return 0;
}
}
pbx_builtin_setvar_helper(c, varname, varval);
if (c)
ast_mutex_unlock(&c->lock);
astman_send_ack(s, m, "Variable Set");
return 0;
}
static char mandescr_getvar[] =
"Description: Get the value of a global or local channel variable.\n"
"Variables: (Names marked with * are required)\n"
" Channel: Channel to read variable from\n"
" *Variable: Variable name\n"
" ActionID: Optional Action id for message matching.\n";
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 *id = astman_get_header(m,"ActionID");
char *varval;
char *varval2=NULL;
if (!strlen(varname)) {
astman_send_error(s, m, "No variable specified");
return 0;
}
if (strlen(name)) {
c = ast_get_channel_by_name_locked(name);
if (!c) {
astman_send_error(s, m, "No such channel");
return 0;
}
}
varval=pbx_builtin_getvar_helper(c,varname);
if (varval)
varval2 = ast_strdupa(varval);
if (!varval2)
varval2 = "";
if (c)
ast_mutex_unlock(&c->lock);
ast_cli(s->fd, "Response: Success\r\n"
"Variable: %s\r\nValue: %s\r\n" ,varname,varval2);
if (!ast_strlen_zero(id))
ast_cli(s->fd, "ActionID: %s\r\n",id);
ast_cli(s->fd, "\r\n");
return 0;
}
/*! \brief action_status: Manager "status" command to show channels */
/* Needs documentation... */
static int action_status(struct mansession *s, struct message *m)
{
char *id = astman_get_header(m,"ActionID");
char *name = astman_get_header(m,"Channel");
char idText[256] = "";
struct ast_channel *c;
char bridge[256];
struct timeval now = ast_tvnow();
long elapsed_seconds=0;
int all = ast_strlen_zero(name); /* set if we want all channels */
astman_send_ack(s, m, "Channel status will follow");
if (!ast_strlen_zero(id))
snprintf(idText,256,"ActionID: %s\r\n",id);
if (all)
c = ast_channel_walk_locked(NULL);
else {
c = ast_get_channel_by_name_locked(name);
if (!c) {
astman_send_error(s, m, "No such channel");
return 0;
}
}
/* if we look by name, we break after the first iteration */
while(c) {
if (c->_bridge)
snprintf(bridge, sizeof(bridge), "Link: %s\r\n", c->_bridge->name);
else
bridge[0] = '\0';
if (c->pbx) {
if (c->cdr) {
elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
}
ast_cli(s->fd,
"Event: Status\r\n"
"Privilege: Call\r\n"
"Channel: %s\r\n"
"CallerID: %s\r\n"
"CallerIDName: %s\r\n"
"Account: %s\r\n"
"State: %s\r\n"
"Context: %s\r\n"
"Extension: %s\r\n"
"Priority: %d\r\n"
"Seconds: %ld\r\n"
"%s"
"Uniqueid: %s\r\n"
"%s"
"\r\n",
c->name,
c->cid.cid_num ? c->cid.cid_num : "<unknown>",
c->cid.cid_name ? c->cid.cid_name : "<unknown>",
c->accountcode,
ast_state2str(c->_state), c->context,
c->exten, c->priority, (long)elapsed_seconds, bridge, c->uniqueid, idText);
} else {
ast_cli(s->fd,
"Event: Status\r\n"
"Privilege: Call\r\n"
"Channel: %s\r\n"
"CallerID: %s\r\n"
"CallerIDName: %s\r\n"
"Account: %s\r\n"
"State: %s\r\n"
"%s"
"Uniqueid: %s\r\n"
"%s"
"\r\n",
c->name,
c->cid.cid_num ? c->cid.cid_num : "<unknown>",
c->cid.cid_name ? c->cid.cid_name : "<unknown>",
c->accountcode,
ast_state2str(c->_state), bridge, c->uniqueid, idText);
}
ast_mutex_unlock(&c->lock);
if (!all)
break;
c = ast_channel_walk_locked(c);
}
ast_cli(s->fd,
"Event: StatusComplete\r\n"
"%s"
"\r\n",idText);
return 0;
}
static char mandescr_redirect[] =
"Description: Redirect (transfer) a call.\n"
"Variables: (Names marked with * are required)\n"
" *Channel: Channel to redirect\n"
" ExtraChannel: Second call leg to transfer (optional)\n"
" *Exten: Extension to transfer to\n"
" *Context: Context to transfer to\n"
" *Priority: Priority to transfer to\n"
" ActionID: Optional Action id for message matching.\n";
/*! \brief action_redirect: The redirect manager command */
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;
int pi = 0;
int res;
if (ast_strlen_zero(name)) {
astman_send_error(s, m, "Channel not specified");
return 0;
}
if (!ast_strlen_zero(priority) && (sscanf(priority, "%d", &pi) != 1)) {
astman_send_error(s, m, "Invalid priority\n");
return 0;
}
chan = ast_get_channel_by_name_locked(name);
if (!chan) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "Channel does not exist: %s", name);
astman_send_error(s, m, buf);
return 0;
}
if (!ast_strlen_zero(name2))
chan2 = ast_get_channel_by_name_locked(name2);
res = ast_async_goto(chan, context, exten, pi);
if (!res) {
if (!ast_strlen_zero(name2)) {
if (chan2)
res = ast_async_goto(chan2, context, exten, pi);
else
res = -1;
if (!res)
astman_send_ack(s, m, "Dual Redirect successful");
else
astman_send_error(s, m, "Secondary redirect failed");
} else
astman_send_ack(s, m, "Redirect successful");
} else
astman_send_error(s, m, "Redirect failed");
if (chan)
ast_mutex_unlock(&chan->lock);
if (chan2)
ast_mutex_unlock(&chan2->lock);
return 0;
}
static char mandescr_command[] =
"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";
/*! \brief action_command: Manager command "command" - execute CLI command */
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_cli(s->fd, "Response: Follows\r\nPrivilege: Command\r\n");
if (!ast_strlen_zero(id))
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");
return 0;
}
static void *fast_originate(void *data)
{
struct fast_originate_helper *in = data;
int res;
int reason = 0;
struct ast_channel *chan = NULL;
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->cid_num) ? in->cid_num : NULL,
!ast_strlen_zero(in->cid_name) ? in->cid_name : NULL,
in->vars, in->account, &chan);
} 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->cid_num) ? in->cid_num : NULL,
!ast_strlen_zero(in->cid_name) ? in->cid_name : NULL,
in->vars, in->account, &chan);
}
if (!res)
manager_event(EVENT_FLAG_CALL,
"OriginateSuccess",
"%s"
"Channel: %s/%s\r\n"
"Context: %s\r\n"
"Exten: %s\r\n"
"Reason: %d\r\n"
"Uniqueid: %s\r\n",
in->idtext, in->tech, in->data, in->context, in->exten, reason, chan ? chan->uniqueid : "<null>");
else
manager_event(EVENT_FLAG_CALL,
"OriginateFailure",
"%s"
"Channel: %s/%s\r\n"
"Context: %s\r\n"
"Exten: %s\r\n"
"Reason: %d\r\n"
"Uniqueid: %s\r\n",
in->idtext, in->tech, in->data, in->context, in->exten, reason, chan ? chan->uniqueid : "<null>");
/* Locked by ast_pbx_outgoing_exten or ast_pbx_outgoing_app */
if (chan)
ast_mutex_unlock(&chan->lock);
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, multiple Variable: headers are allowed\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 *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 *id = astman_get_header(m, "ActionID");
struct ast_variable *vars = astman_get_variables(m);
char *tech, *data;
char *l=NULL, *n=NULL;
int pi = 0;
int res;
int to = 30000;
int reason = 0;
char tmp[256];
char tmp2[256];
pthread_t th;
pthread_attr_t attr;
if (!name) {
astman_send_error(s, m, "Channel not specified");
return 0;
}
if (!ast_strlen_zero(priority) && (sscanf(priority, "%d", &pi) != 1)) {
astman_send_error(s, m, "Invalid priority\n");
return 0;
}
if (!ast_strlen_zero(timeout) && (sscanf(timeout, "%d", &to) != 1)) {
astman_send_error(s, m, "Invalid timeout\n");
return 0;
}
ast_copy_string(tmp, name, sizeof(tmp));
tech = tmp;
data = strchr(tmp, '/');
if (!data) {
astman_send_error(s, m, "Invalid channel\n");
return 0;
}
*data = '\0';
data++;
ast_copy_string(tmp2, callerid, sizeof(tmp2));
ast_callerid_parse(tmp2, &n, &l);
if (n) {
if (ast_strlen_zero(n))
n = NULL;
}
if (l) {
ast_shrink_phone_number(l);
if (ast_strlen_zero(l))
l = NULL;
}
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));
if (!ast_strlen_zero(id))
snprintf(fast->idtext, sizeof(fast->idtext), "ActionID: %s\r\n", id);
ast_copy_string(fast->tech, tech, sizeof(fast->tech));
ast_copy_string(fast->data, data, sizeof(fast->data));
ast_copy_string(fast->app, app, sizeof(fast->app));
ast_copy_string(fast->appdata, appdata, sizeof(fast->appdata));
if (l)
ast_copy_string(fast->cid_num, l, sizeof(fast->cid_num));
if (n)
ast_copy_string(fast->cid_name, n, sizeof(fast->cid_name));
fast->vars = vars;
ast_copy_string(fast->context, context, sizeof(fast->context));
ast_copy_string(fast->exten, exten, sizeof(fast->exten));
ast_copy_string(fast->account, account, sizeof(fast->account));
fast->timeout = to;
fast->priority = pi;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (ast_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, 1, l, n, vars, account, NULL);
} else {
if (exten && context && pi)
res = ast_pbx_outgoing_exten(tech, AST_FORMAT_SLINEAR, data, to, context, exten, pi, &reason, 1, l, n, vars, account, NULL);
else {
astman_send_error(s, m, "Originate with 'Exten' requires 'Context' and 'Priority'");
return 0;
}
}
if (!res)
astman_send_ack(s, m, "Originate successfully queued");
else
astman_send_error(s, m, "Originate failed");
return 0;
}
/*! \brief Help text for manager command mailboxstatus
*/
static char mandescr_mailboxstatus[] =
"Description: Checks a voicemail account for status.\n"
"Variables: (Names marked with * are required)\n"
" *Mailbox: Full mailbox ID <mailbox>@<vm-context>\n"
" ActionID: Optional ActionID for message matching.\n"
"Returns number of messages.\n"
" Message: Mailbox Status\n"
" Mailbox: <mailboxid>\n"
" Waiting: <count>\n"
"\n";
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] = "";
int ret;
if (ast_strlen_zero(mailbox)) {
astman_send_error(s, m, "Mailbox not specified");
return 0;
}
if (!ast_strlen_zero(id))
snprintf(idText,256,"ActionID: %s\r\n",id);
ret = ast_app_has_voicemail(mailbox, NULL);
ast_cli(s->fd, "Response: Success\r\n"
"%s"
"Message: Mailbox Status\r\n"
"Mailbox: %s\r\n"
"Waiting: %d\r\n\r\n", idText, mailbox, ret);
return 0;
}
static char mandescr_mailboxcount[] =
"Description: Checks a voicemail account for new messages.\n"
"Variables: (Names marked with * are required)\n"
" *Mailbox: Full mailbox ID <mailbox>@<vm-context>\n"
" ActionID: Optional ActionID for message matching.\n"
"Returns number of new and old messages.\n"
" Message: Mailbox Message Count\n"
" Mailbox: <mailboxid>\n"
" NewMessages: <count>\n"
" OldMessages: <count>\n"
"\n";
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] = "";
int newmsgs = 0, oldmsgs = 0;
if (ast_strlen_zero(mailbox)) {
astman_send_error(s, m, "Mailbox not specified");
return 0;
}
ast_app_messagecount(mailbox, &newmsgs, &oldmsgs);
if (!ast_strlen_zero(id)) {
snprintf(idText,256,"ActionID: %s\r\n",id);
}
ast_cli(s->fd, "Response: Success\r\n"
"%s"
"Message: Mailbox Message Count\r\n"
"Mailbox: %s\r\n"
"NewMessages: %d\r\n"
"OldMessages: %d\r\n"
"\r\n",
idText,mailbox, newmsgs, oldmsgs);
return 0;
}
static char mandescr_extensionstate[] =
"Description: Report the extension state for given extension.\n"
" If the extension has a hint, will use devicestate to check\n"
" the status of the device connected to the extension.\n"
"Variables: (Names marked with * are required)\n"
" *Exten: Extension to check state on\n"
" *Context: Context for extension\n"
" ActionId: Optional ID for this transaction\n"
"Will return an \"Extension Status\" message.\n"
"The response will include the hint for the extension and the status.\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] = "";
char hint[256] = "";
int status;
if (ast_strlen_zero(exten)) {
astman_send_error(s, m, "Extension not specified");
return 0;
}
if (ast_strlen_zero(context))
context = "default";
status = ast_extension_state(NULL, context, exten);
ast_get_hint(hint, sizeof(hint) - 1, NULL, 0, NULL, context, exten);
if (!ast_strlen_zero(id)) {
snprintf(idText,256,"ActionID: %s\r\n",id);
}
ast_cli(s->fd, "Response: Success\r\n"
"%s"
"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);
return 0;
}
static char mandescr_timeout[] =
"Description: Hangup a channel after a certain time.\n"
"Variables: (Names marked with * are required)\n"
" *Channel: Channel name to hangup\n"
" *Timeout: Maximum duration of the call (sec)\n"
"Acknowledges set time with 'Timeout Set' message\n";
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"));
if (ast_strlen_zero(name)) {
astman_send_error(s, m, "No channel specified");
return 0;
}
if (!timeout) {
astman_send_error(s, m, "No timeout specified");
return 0;
}
c = ast_get_channel_by_name_locked(name);
if (!c) {
astman_send_error(s, m, "No such channel");
return 0;
}
ast_channel_setwhentohangup(c, timeout);
ast_mutex_unlock(&c->lock);
astman_send_ack(s, m, "Timeout Set");
return 0;
}
static int process_message(struct mansession *s, struct message *m)
{
char action[80] = "";
struct manager_action *tmp = first_action;
char *id = astman_get_header(m,"ActionID");
char idText[256] = "";
char iabuf[INET_ADDRSTRLEN];
ast_copy_string(action, astman_get_header(m, "Action"), sizeof(action));
ast_log( LOG_DEBUG, "Manager received command '%s'\n", action );
if (ast_strlen_zero(action)) {
astman_send_error(s, m, "Missing action in request");
return 0;
}
if (!ast_strlen_zero(id)) {
snprintf(idText,256,"ActionID: %s\r\n",id);
}
if (!s->authenticated) {
if (!strcasecmp(action, "Challenge")) {
char *authtype;
authtype = astman_get_header(m, "AuthType");
if (!strcasecmp(authtype, "MD5")) {
if (ast_strlen_zero(s->challenge))
snprintf(s->challenge, sizeof(s->challenge), "%d", rand());
ast_mutex_lock(&s->__lock);
ast_cli(s->fd, "Response: Success\r\n"
"%s"
"Challenge: %s\r\n\r\n",
idText,s->challenge);
ast_mutex_unlock(&s->__lock);
return 0;
} else {
astman_send_error(s, m, "Must specify AuthType");
return 0;
}
} else if (!strcasecmp(action, "Login")) {
if (authenticate(s, m)) {
sleep(1);
astman_send_error(s, m, "Authentication failed");
return -1;
} else {
s->authenticated = 1;
if (option_verbose > 1) {
if ( displayconnects ) {
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_ack(s, m, "See ya");
return -1;
} else
astman_send_error(s, m, "Authentication Required");
} else {
int ret=0;
struct eventqent *eqe;
ast_mutex_lock(&s->__lock);
s->busy = 1;
ast_mutex_unlock(&s->__lock);
while( tmp ) {
if (!strcasecmp(action, tmp->action)) {
if ((s->writeperm & tmp->authority) == tmp->authority) {
if (tmp->func(s, m))
ret = -1;
} else {
astman_send_error(s, m, "Permission denied");
}
break;
}
tmp = tmp->next;
}
if (!tmp)
astman_send_error(s, m, "Invalid/unknown command");
ast_mutex_lock(&s->__lock);
s->busy = 0;
while(s->eventq) {
if (ast_carefulwrite(s->fd, s->eventq->eventdata, strlen(s->eventq->eventdata), s->writetimeout) < 0) {
ret = -1;
break;
}
eqe = s->eventq;
s->eventq = s->eventq->next;
free(eqe);
}
ast_mutex_unlock(&s->__lock);
return ret;
}
return 0;
}
static int get_input(struct mansession *s, char *output)
{
/* output must have at least sizeof(s->inbuf) space */
int res;
int x;
struct pollfd fds[1];
char iabuf[INET_ADDRSTRLEN];
for (x=1;x<s->inlen;x++) {
if ((s->inbuf[x] == '\n') && (s->inbuf[x-1] == '\r')) {
/* Copy output data up to and including \r\n */
memcpy(output, s->inbuf, x + 1);
/* Add trailing \0 */
output[x+1] = '\0';
/* Move remaining data back to the front */
memmove(s->inbuf, s->inbuf + x + 1, s->inlen - x);
s->inlen -= (x + 1);
return 1;
}
}
if (s->inlen >= sizeof(s->inbuf) - 1) {
ast_log(LOG_WARNING, "Dumping long line with no return from %s: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), s->inbuf);
s->inlen = 0;
}
fds[0].fd = s->fd;
fds[0].events = POLLIN;
do {
res = poll(fds, 1, -1);
if (res < 0) {
if (errno == EINTR) {
if (s->dead)
return -1;
continue;
}
ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
return -1;
} else if (res > 0) {
ast_mutex_lock(&s->__lock);
res = read(s->fd, s->inbuf + s->inlen, sizeof(s->inbuf) - 1 - s->inlen);
ast_mutex_unlock(&s->__lock);
if (res < 1)
return -1;
break;
}
} while(1);
s->inlen += res;
s->inbuf[s->inlen] = '\0';
return 0;
}
static void *session_do(void *data)
{
struct mansession *s = data;
struct message m;
char iabuf[INET_ADDRSTRLEN];
int res;
ast_mutex_lock(&s->__lock);
ast_cli(s->fd, "Asterisk Call Manager/1.0\r\n");
ast_mutex_unlock(&s->__lock);
memset(&m, 0, sizeof(m));
for (;;) {
res = get_input(s, m.headers[m.hdrcount]);
if (res > 0) {
/* Strip trailing \r\n */
if (strlen(m.headers[m.hdrcount]) < 2)
continue;
m.headers[m.hdrcount][strlen(m.headers[m.hdrcount]) - 2] = '\0';
if (ast_strlen_zero(m.headers[m.hdrcount])) {
if (process_message(s, &m))
break;
memset(&m, 0, sizeof(m));
} else if (m.hdrcount < AST_MAX_MANHEADERS - 1)
m.hdrcount++;
} else if (res < 0)
break;
}
if (s->authenticated) {
if (option_verbose > 1) {
if (displayconnects)
ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
}
ast_log(LOG_EVENT, "Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
} else {
if (option_verbose > 1) {
if ( displayconnects )
ast_verbose(VERBOSE_PREFIX_2 "Connect attempt from '%s' unable to authenticate\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
}
ast_log(LOG_EVENT, "Failed attempt from %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
}
destroy_session(s);
return NULL;
}
static void *accept_thread(void *ignore)
{
int as;
struct sockaddr_in sin;
socklen_t sinlen;
struct mansession *s;
struct protoent *p;
int arg = 1;
int flags;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
for (;;) {
sinlen = sizeof(sin);
as = accept(asock, (struct sockaddr *)&sin, &sinlen);
if (as < 0) {
ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
continue;
}
p = getprotobyname("tcp");
if (p) {
if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
}
}
s = malloc(sizeof(struct mansession));
if (!s) {
ast_log(LOG_WARNING, "Failed to allocate management session: %s\n", strerror(errno));
continue;
}
memset(s, 0, sizeof(struct mansession));
memcpy(&s->sin, &sin, sizeof(sin));
s->writetimeout = 100;
if(! block_sockets) {
/* For safety, make sure socket is non-blocking */
flags = fcntl(as, F_GETFL);
fcntl(as, F_SETFL, flags | O_NONBLOCK);
}
ast_mutex_init(&s->__lock);
s->fd = as;
s->send_events = -1;
ast_mutex_lock(&sessionlock);
s->next = sessions;
sessions = s;
ast_mutex_unlock(&sessionlock);
if (ast_pthread_create(&s->t, &attr, session_do, s))
destroy_session(s);
}
pthread_attr_destroy(&attr);
return NULL;
}
static int append_event(struct mansession *s, const char *str)
{
struct eventqent *tmp, *prev=NULL;
tmp = malloc(sizeof(struct eventqent) + strlen(str));
if (tmp) {
tmp->next = NULL;
strcpy(tmp->eventdata, str);
if (s->eventq) {
prev = s->eventq;
while(prev->next)
prev = prev->next;
prev->next = tmp;
} else {
s->eventq = tmp;
}
return 0;
}
return -1;
}
/*! \brief manager_event: Send AMI event to client */
int manager_event(int category, char *event, char *fmt, ...)
{
struct mansession *s;
char auth[80];
char tmp[4096] = "";
char *tmp_next = tmp;
size_t tmp_left = sizeof(tmp) - 2;
va_list ap;
ast_mutex_lock(&sessionlock);
for (s = sessions; s; s = s->next) {
if ((s->readperm & category) != category)
continue;
if ((s->send_events & category) != category)
continue;
if (ast_strlen_zero(tmp)) {
ast_build_string(&tmp_next, &tmp_left, "Event: %s\r\nPrivilege: %s\r\n",
event, authority_to_str(category, auth, sizeof(auth)));
va_start(ap, fmt);
ast_build_string_va(&tmp_next, &tmp_left, fmt, ap);
va_end(ap);
*tmp_next++ = '\r';
*tmp_next++ = '\n';
*tmp_next = '\0';
}
ast_mutex_lock(&s->__lock);
if (s->busy) {
append_event(s, tmp);
} else if (!s->dead) {
if (ast_carefulwrite(s->fd, tmp, tmp_next - tmp, s->writetimeout) < 0) {
ast_log(LOG_WARNING, "Disconnecting slow (or gone) manager session!\n");
s->dead = 1;
pthread_kill(s->t, SIGURG);
}
}
ast_mutex_unlock(&s->__lock);
}
ast_mutex_unlock(&sessionlock);
return 0;
}
int ast_manager_unregister( char *action )
{
struct manager_action *cur = first_action, *prev = first_action;
ast_mutex_lock(&actionlock);
while( cur ) {
if (!strcasecmp(action, cur->action)) {
prev->next = cur->next;
free(cur);
if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_2 "Manager unregistered action %s\n", action);
ast_mutex_unlock(&actionlock);
return 0;
}
prev = cur;
cur = cur->next;
}
ast_mutex_unlock(&actionlock);
return 0;
}
static int manager_state_cb(char *context, char *exten, int state, void *data)
{
/* Notify managers of change */
manager_event(EVENT_FLAG_CALL, "ExtensionStatus", "Exten: %s\r\nContext: %s\r\nStatus: %d\r\n", exten, context, state);
return 0;
}
static int ast_manager_register_struct(struct manager_action *act)
{
struct manager_action *cur = first_action, *prev = NULL;
int ret;
ast_mutex_lock(&actionlock);
while(cur) { /* Walk the list of actions */
ret = strcasecmp(cur->action, act->action);
if (ret == 0) {
ast_log(LOG_WARNING, "Manager: Action '%s' already registered\n", act->action);
ast_mutex_unlock(&actionlock);
return -1;
} else if (ret > 0) {
/* Insert these alphabetically */
if (prev) {
act->next = prev->next;
prev->next = act;
} else {
act->next = first_action;
first_action = act;
}
break;
}
prev = cur;
cur = cur->next;
}
if (!cur) {
if (prev)
prev->next = act;
else
first_action = act;
act->next = NULL;
}
if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_2 "Manager registered action %s\n", act->action);
ast_mutex_unlock(&actionlock);
return 0;
}
/*! \brief register a new command with manager, including online help. This is
the preferred way to register a manager command */
int ast_manager_register2(const char *action, int auth, int (*func)(struct mansession *s, struct message *m), const char *synopsis, const char *description)
{
struct manager_action *cur;
cur = malloc(sizeof(struct manager_action));
if (!cur) {
ast_log(LOG_WARNING, "Manager: out of memory trying to register action\n");
return -1;
}
cur->action = action;
cur->authority = auth;
cur->func = func;
cur->synopsis = synopsis;
cur->description = description;
cur->next = NULL;
ast_manager_register_struct(cur);
return 0;
}
/*! @}
END Doxygen group */
static int registered = 0;
int init_manager(void)
{
struct ast_config *cfg;
char *val;
int oldportno = portno;
static struct sockaddr_in ba;
int x = 1;
if (!registered) {
/* Register default actions */
ast_manager_register2("Ping", 0, action_ping, "Keepalive command", mandescr_ping);
ast_manager_register2("Events", 0, action_events, "Control Event Flow", mandescr_events);
ast_manager_register2("Logoff", 0, action_logoff, "Logoff Manager", mandescr_logoff);
ast_manager_register2("Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel", mandescr_hangup);
ast_manager_register("Status", EVENT_FLAG_CALL, action_status, "Lists channel status" );
ast_manager_register2("Setvar", EVENT_FLAG_CALL, action_setvar, "Set Channel Variable", mandescr_setvar );
ast_manager_register2("Getvar", EVENT_FLAG_CALL, action_getvar, "Gets a Channel Variable", mandescr_getvar );
ast_manager_register2("Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect (transfer) a call", mandescr_redirect );
ast_manager_register2("Originate", EVENT_FLAG_CALL, action_originate, "Originate Call", mandescr_originate);
ast_manager_register2("Command", EVENT_FLAG_COMMAND, action_command, "Execute Asterisk CLI Command", mandescr_command );
ast_manager_register2("ExtensionState", EVENT_FLAG_CALL, action_extensionstate, "Check Extension Status", mandescr_extensionstate );
ast_manager_register2("AbsoluteTimeout", EVENT_FLAG_CALL, action_timeout, "Set Absolute Timeout", mandescr_timeout );
ast_manager_register2("MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox", mandescr_mailboxstatus );
ast_manager_register2("MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count", mandescr_mailboxcount );
ast_manager_register2("ListCommands", 0, action_listcommands, "List available manager commands", mandescr_listcommands);
ast_cli_register(&show_mancmd_cli);
ast_cli_register(&show_mancmds_cli);
ast_cli_register(&show_manconn_cli);
ast_extension_state_add(NULL, NULL, manager_state_cb, NULL);
registered = 1;
}
portno = DEFAULT_MANAGER_PORT;
displayconnects = 1;
cfg = ast_config_load("manager.conf");
if (!cfg) {
ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf. Call management disabled.\n");
return 0;
}
memset(&ba, 0, sizeof(ba));
val = ast_variable_retrieve(cfg, "general", "enabled");
if (val)
enabled = ast_true(val);
val = ast_variable_retrieve(cfg, "general", "block-sockets");
if(val)
block_sockets = ast_true(val);
if ((val = ast_variable_retrieve(cfg, "general", "port"))) {
if (sscanf(val, "%d", &portno) != 1) {
ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
portno = DEFAULT_MANAGER_PORT;
}
} else if ((val = ast_variable_retrieve(cfg, "general", "portno"))) {
if (sscanf(val, "%d", &portno) != 1) {
ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
portno = DEFAULT_MANAGER_PORT;
}
ast_log(LOG_NOTICE, "Use of portno in manager.conf deprecated. Please use 'port=%s' instead.\n", val);
}
/* Parsing the displayconnects */
if ((val = ast_variable_retrieve(cfg, "general", "displayconnects"))) {
displayconnects = ast_true(val);;
}
ba.sin_family = AF_INET;
ba.sin_port = htons(portno);
memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) {
if (!inet_aton(val, &ba.sin_addr)) {
ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val);
memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
}
}
if ((asock > -1) && ((portno != oldportno) || !enabled)) {
#if 0
/* Can't be done yet */
close(asock);
asock = -1;
#else
ast_log(LOG_WARNING, "Unable to change management port / enabled\n");
#endif
}
ast_config_destroy(cfg);
/* If not enabled, do nothing */
if (!enabled) {
return 0;
}
if (asock < 0) {
asock = socket(AF_INET, SOCK_STREAM, 0);
if (asock < 0) {
ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
return -1;
}
setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
if (bind(asock, (struct sockaddr *)&ba, sizeof(ba))) {
ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno));
close(asock);
asock = -1;
return -1;
}
if (listen(asock, 2)) {
ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno));
close(asock);
asock = -1;
return -1;
}
if (option_verbose)
ast_verbose("Asterisk Management interface listening on port %d\n", portno);
ast_pthread_create(&t, NULL, accept_thread, NULL);
}
return 0;
}
int reload_manager(void)
{
manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n");
return init_manager();
}
|