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 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
|
/* Distributed Checksum Clearinghouse
*
* control dcc server
*
* Copyright (c) 2005 by Rhyolite Software
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
* BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*
* Rhyolite Software DCC 1.2.74-1.134 $Revision$
*/
#include "dcc_ck.h"
#include "dcc_xhdr.h"
#include "dcc_heap_debug.h"
#include "dcc_ids.h"
#ifndef DCC_WIN32
#include <arpa/inet.h>
#endif
static DCC_EMSG dcc_emsg;
static DCC_CLNT_CTXT *ctxt;
static DCC_PATH info_map_nm = DCC_MAP_NM_DEF;
static const char *homedir;
static DCC_PASSWD passwd;
static u_char passwd_set;
static DCC_SRVR_NM srvr = DCC_SRVR_NM_DEF;
static u_char port_set;
static ID_TBL *srvr_clnt_tbl;
static DCC_CLNT_ID srvr_clnt_id;
static enum WHICH_MAP {MAP_TMP, MAP_INFO} which_map = MAP_INFO;
#if defined(NO_IPV6)
static u_char clnt_flags = 0;
#else
static u_char clnt_flags = DCC_CLNT_INFO_FG_IPV6;
#endif
static u_char grey_set;
static u_char quiet;
static u_char do_cmds(char *);
static u_char init_map(u_char);
struct cmd_tbl_entry;
/* -1=display help message, 0=command failed, 1=success */
typedef int CMD (const char *, const struct cmd_tbl_entry *);
typedef struct cmd_tbl_entry {
const char *cmd;
CMD (*fnc);
u_char args; /* 0=optional, 1=required, 2=none */
u_char privileged; /* 1=must have server's password */
u_char uid_0; /* 1=require set-UID privileges */
const char *help_str;
} CMD_TBL_ENTRY;
static CMD help_cmd;
static CMD exit_cmd;
static CMD grey_cmd;
static CMD file_cmd;
static CMD new_map_cmd;
static CMD delete_cmd;
static CMD add_cmd;
static CMD load_cmd;
static CMD host_cmd;
static CMD port_cmd;
static CMD passwd_cmd;
static CMD id_cmd;
static CMD homedir_cmd;
static CMD debug_cmd;
static CMD ipv6_cmd;
static CMD socks_cmd;
static CMD info_cmd;
static CMD rtt_cmd;
static CMD delck_cmd;
static CMD sleep_cmd;
static CMD clients_cmd;
static CMD anon_cmd;
static CMD flod_rewind;
static CMD ffwd_in;
static CMD ffwd_out;
static CMD flod_stats;
static CMD stats_cmd;
static CMD trace_def;
static const CMD_TBL_ENTRY cmd_tbl[] = {
{"help", help_cmd, 0, 0, 0, "help [cmd]"},
{"?", help_cmd, 0, 0, 0, 0},
{"exit", exit_cmd, 2, 0, 0, "exit"},
{"quit", exit_cmd, 2, 0, 0, 0},
{"grey", grey_cmd, 0, 0, 0, "grey [on|off]"},
{"homedir", homedir_cmd, 0, 0, 0, "homedir [path]"},
{"file", file_cmd, 0, 0, 0, "file [map]"},
{"new map", new_map_cmd, 0, 0, 0, "new map [map]"},
{"delete", delete_cmd, 1, 0, 1, "delete host[,port]"},
{"add", add_cmd, 1, 0, 1,
"add host,[port|-] [RTT+/-#] [ID [passwd]]"},
{"load", load_cmd, 1, 0, 1, "load {info-file | -}"},
{"host", host_cmd, 0, 0, 0, "host [hostname]"},
{"server", host_cmd, 0, 0, 0, 0},
{"port", port_cmd, 0, 0, 0, "port #"},
{"passwd", passwd_cmd, 0, 0, 0, "passwd secret"},
{"password", passwd_cmd, 0, 0, 0, 0},
{"id", id_cmd, 0, 0, 0, "id [ID]"},
{"debug", debug_cmd, 0, 0, 0, "debug [on|off|TTL=x]"},
{"IPv6", ipv6_cmd, 0, 0, 0, "IPv6 [on|off]"},
{"SOCKS", socks_cmd, 0, 0, 0, "SOCKS [on|off]"},
{"info", info_cmd, 0, 0, 0, "info [-N]"},
{"RTT", rtt_cmd, 0, 0, 0, "RTT [-N]"},
{"delck", delck_cmd, 1, 1, 0, "delck type hex1..4"},
{"sleep", sleep_cmd, 1, 0, 0, "sleep sec.onds"},
{"clients", clients_cmd, 0, 0, 0, "clients [-ns] [max [thold]]"},
{"anon delay", anon_cmd, 0, 0, 0, "anon delay [delay[,inflate]]"},
{"flood rewind",flod_rewind, 1, 1, 0, "flood rewind ID"},
{"flood FFWD in",ffwd_in, 1, 1, 0, "flood FFWD in ID"},
{"flood FFWD out",ffwd_out, 1, 1, 0, "flood FFWD out ID"},
{"flood stats", flod_stats, 1, 1, 0, "flood stats [clear] {ID|all}"},
{"stats", stats_cmd, 0, 0, 0, "stats [clear|all]"},
{"status", stats_cmd, 0, 0, 0, 0},
{"trace default",trace_def, 2, 1, 0, "trace default"},
};
#define PRV_MSG ";\n" \
" use the \"id server-ID\" command\n" \
" and either \"passwd secret\" or `su` to read passwords from %s"
static DCC_ADMN_RESP_VAL op_resp;
static struct timeval op_start, op_end;
static DCC_SOCKU op_resp_su;
static struct {
const char *op;
const char *help_str;
DCC_AOPS aop;
u_char privileged;
u_int32_t val;
} aop_tbl[] = {
{"stop", 0, DCC_AOP_STOP, 1, 0},
{"new IDs", "", DCC_AOP_NEW_IDS, 1, 0},
{"reload IDs", 0, DCC_AOP_NEW_IDS, 1, 0},
{"flood check", 0, DCC_AOP_FLOD, 1, DCC_AOP_FLOD_CHECK},
{"flood shutdown", 0, DCC_AOP_FLOD, 1, DCC_AOP_FLOD_SHUTDOWN},
{"flood halt", 0, DCC_AOP_FLOD, 1, DCC_AOP_FLOD_HALT},
{"flood resume", 0, DCC_AOP_FLOD, 1, DCC_AOP_FLOD_RESUME},
{"flood list", 0, DCC_AOP_FLOD, 0, DCC_AOP_FLOD_LIST},
{"DB unlock", 0, DCC_AOP_DB_UNLOCK, 1, 0},
{"DB new", 0, DCC_AOP_DB_NEW, 1, 0},
#define TMAC(s,b) \
{"trace "#s" on", "trace "#s" {on|off}", \
DCC_AOP_TRACE_ON, 1, DCC_TRACE_##b},\
{"trace "#s" off", "", DCC_AOP_TRACE_OFF, 1, DCC_TRACE_##b}
TMAC(admn,ADMN_BIT),
TMAC(anon,ANON_BIT),
TMAC(clnt,CLNT_BIT),
TMAC(rlim,RLIM_BIT),
TMAC(query,QUERY_BIT),
TMAC(ridc,RIDC_BIT),
TMAC(flood,FLOD_BIT),
TMAC(flood2,FLOD2_BIT),
TMAC(ids,IDS_BIT),
TMAC(bl,BL_BIT),
{"trace all on", "trace all {on|off}",
DCC_AOP_TRACE_ON, 1, DCC_TRACE_ON_BITS},
{"trace all off", "", DCC_AOP_TRACE_OFF, 1, DCC_TRACE_OFF_BITS},
#undef TMAC
};
static void NRATTRIB
usage(void)
{
dcc_logbad(EX_USAGE,
"usage: [-Vdq] [-h homedir] [-c ids] [op1 [op2] ... ]\n");
}
int NRATTRIB
main(int argc, char **argv)
{
char cmd_buf[500];
int i;
srvr.port = htons(DCC_SRVR_PORT);
srvr_clnt_id = srvr.clnt_id;
dcc_init_priv();
dcc_syslog_init(0, argv[0], 0);
while ((i = getopt(argc, argv, "Vdqh:c:")) != EOF) {
switch (i) {
case 'V':
fprintf(stderr, DCC_VERSION"\n");
break;
case 'd':
++dcc_clnt_debug;
break;
case 'q':
++quiet;
break;
case 'h':
homedir = optarg;
break;
case 'c':
ids_nm = optarg;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (!dcc_cdhome(dcc_emsg, homedir))
dcc_error_msg("%s", dcc_emsg);
dcc_clnt_unthread_init();
dcc_wf_init(&cmn_wf, 0, 0);
dcc_all_srvrs = 1;
if (!init_map(!quiet))
which_map = MAP_TMP;
else
dcc_ctxts_unlock();
/* with a list of commands, act as a batch utility */
if (argc != 0) {
for (;;) {
/* a final arg of "-" says switch to interactive mode */
if (argc == 1 && !strcmp(*argv, "-"))
break;
if (!do_cmds(*argv)) {
fputs(" ?\n", stderr);
exit(EX_UNAVAILABLE);
}
if (!dcc_info_unlock(dcc_emsg))
dcc_error_msg("%s", dcc_emsg);
++argv;
if (!--argc) {
exit(EX_OK);
}
}
}
/* Without an arg list of commands, look for commands from STDIN.
* Commands end with a semicolon or newline. */
for (;;) {
if (!dcc_info_unlock(dcc_emsg))
dcc_error_msg("%s", dcc_emsg);
printf("cdcc %s> ",
which_map == MAP_INFO ? info_map_nm : "-");
fflush(stderr);
fflush(stdout);
if (!fgets(cmd_buf, sizeof(cmd_buf), stdin)) {
fputc('\n', stdout);
exit(EX_OK);
}
if (!do_cmds(cmd_buf))
fputs(" ?\n", stderr);
}
}
static u_char /* 0=failed, 1=ok */
get_passwd(u_char privileged)
{
if (passwd_set) {
strncpy(srvr.passwd, passwd, sizeof(srvr.passwd));
return (!privileged || srvr.clnt_id != DCC_ID_ANON);
}
memset(srvr.passwd, 0, sizeof(srvr.passwd));
srvr_clnt_tbl = 0;
srvr_clnt_id = srvr.clnt_id;
if (srvr.clnt_id == DCC_ID_ANON)
return !privileged;
/* Fetch the common server passwords only if we can read them without
* set-UID. This keeps random local users from attacking local
* or remote servers with privileged commands, but does not slow
* down privilege users who could use an editor to read and use
* the cleartext passwords manually. */
dcc_rel_priv();
if (0 > access(ids_nm, R_OK)
&& errno == EACCES)
return !privileged;
if (load_ids(dcc_emsg, &srvr_clnt_tbl, srvr_clnt_id) < 0) {
if (srvr_clnt_id != DCC_ID_ANON) {
if (privileged)
dcc_error_msg("%s", dcc_emsg);
srvr.clnt_id = DCC_ID_ANON;
}
return !privileged;
}
if (srvr_clnt_tbl)
strncpy(srvr.passwd, srvr_clnt_tbl->cur_passwd,
sizeof(srvr.passwd));
return 1;
}
static void
clear_conn(void)
{
if (ctxt) {
dcc_rel_ctxt(0, ctxt);
ctxt = 0;
}
}
static void
set_which_map(enum WHICH_MAP new)
{
/* release things even if nothing seems to be changing
* to ensure that we bind a new socket */
if (!dcc_unmap_info(dcc_emsg))
dcc_error_msg("%s", dcc_emsg);
clear_conn();
which_map = new;
if (new == MAP_INFO)
passwd_set = 0;
}
/* start talking to the local map file
* on success the contexts and mapped file are locked */
static u_char /* 0=failed 1=mapped and locked */
init_map(u_char complain)
{
u_char result;
dcc_ctxts_lock();
if (which_map == MAP_TMP) {
result = dcc_map_lock_tmp_info(dcc_emsg, &srvr, clnt_flags);
} else {
result = dcc_map_lock_info(dcc_emsg, info_map_nm, -1);
}
if (result) {
clnt_flags = dcc_clnt_info->flags;
return 1;
}
dcc_ctxts_unlock();
if (complain)
dcc_error_msg("%s", dcc_emsg);
return 0;
}
/* start talking to a DCC server
* If we already had a private map file, forget it.
* on success the contexts are locked but the mapped file is not locked */
static u_char /* 0=failed, 1=ok */
init_conn(u_char no_srvr_ok)
{
if (ctxt) {
if (!dcc_clnt_rdy(dcc_emsg, ctxt,
(grey_on ? DCC_CLNT_FG_GREY : 0)
| (no_srvr_ok ? DCC_CLNT_FG_NO_SRVR_OK : 0)
| DCC_CLNT_FG_NO_FAIL)) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
/* check the other (greylist or not) server */
if (!dcc_clnt_rdy(dcc_emsg, ctxt,
(!grey_on ? DCC_CLNT_FG_GREY : 0)
| DCC_CLNT_FG_NO_SRVR_OK
| DCC_CLNT_FG_NO_FAIL)
&& dcc_clnt_debug)
dcc_error_msg("%s", dcc_emsg);
clnt_flags = dcc_clnt_info->flags;
return 1;
}
if (which_map == MAP_TMP) {
/* create a brand new temporary map */
ctxt = dcc_tmp_clnt_init(dcc_emsg, ctxt, &srvr,
grey_on, clnt_flags);
if (!ctxt) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
clnt_flags = dcc_clnt_info->flags;
return 1;
}
if (!init_map(1)) /* lock things */
return 0;
if (!grey_set) {
grey_on = (dcc_clnt_info->dcc.nms[0].hostname[0] == '\0'
&& dcc_clnt_info->grey.nms[0].hostname[0] != '\0');
}
ctxt = dcc_clnt_init(dcc_emsg, ctxt, info_map_nm,
(grey_on ? DCC_CLNT_FG_GREY : 0)
| (no_srvr_ok ? DCC_CLNT_FG_NO_SRVR_OK : 0)
| DCC_CLNT_FG_NO_FAIL);
if (!ctxt) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
/* check the other (greylist or not) server */
if (!dcc_clnt_rdy(dcc_emsg, ctxt,
(!grey_on ? DCC_CLNT_FG_GREY : 0)
| DCC_CLNT_FG_NO_SRVR_OK
| DCC_CLNT_FG_NO_FAIL)
&& dcc_clnt_debug)
dcc_error_msg("%s", dcc_emsg);
clnt_flags = dcc_clnt_info->flags;
return 1;
}
static void
dcc_map_changed(void)
{
if (which_map == MAP_INFO)
dcc_mmap_utime(dcc_info_nm, 1);
dcc_force_measure_rtt(&dcc_clnt_info->dcc, 1);
dcc_force_measure_rtt(&dcc_clnt_info->grey, 1);
}
/* compare ignoring case */
static const char * /* 0 or mismatch in str */
cmd_cmp(const char *str, const char *op)
{
char op_c, str_c;
int len;
len = 0;
for (;;) {
op_c = *op;
/* avoid tolower() to avoid build hassles on odd systems */
if (op_c >= 'A' && op_c <= 'Z')
op_c += 'a'-'A';
str_c = *str;
if (str_c == '\t')
str_c = ' ';
else if (str_c >= 'A' && str_c <= 'Z')
str_c += 'a'-'A';
if (op_c != str_c) {
/* compress bursts of blanks */
if (str_c == ' ' && len != 0 && *(op-1) == ' ') {
++str;
continue;
}
return str;
}
if (op_c == '\0')
return 0;
++op;
++str;
++len;
}
}
/* Display our name for the server and its address,
* while suppressing some duplicates */
static void
print_aop(int anum) /* -1 or server index */
{
const DCC_SRVR_CLASS *class;
const char *resp_addr;
char date_buf[40];
const char *srvr_nm;
struct tm tm;
resp_addr = dcc_su2str_opt(&op_resp_su, 0, '\0');
class = DCC_GREY2CLASS(grey_on);
/* Display the preferred server if anum is -1 */
if (anum < 0)
anum = class->act_inx;
if (anum < class->num_addrs) {
srvr_nm = class->nms[class->addrs[anum].nm_inx].hostname;
if (srvr_nm && strcmp(srvr_nm, resp_addr)) {
fputs(srvr_nm, stdout);
putchar(' ');
}
printf("%s\n server-ID %d",
dcc_su2str(&op_resp_su),
class->addrs[anum].srvr_id);
} else {
printf("%s\n ",
dcc_su2str(&op_resp_su));
}
if (srvr.clnt_id != DCC_ID_ANON)
printf(" client-ID %d", srvr.clnt_id);
if (which_map == MAP_INFO)
printf(" %s", dcc_info_nm);
strftime(date_buf, sizeof(date_buf), " %X",
dcc_localtime(op_start.tv_sec, &tm));
fputs(date_buf, stdout);
putchar('\n');
}
static u_char /* 0=some kind of problem, 1=done */
start_aop(DCC_AOPS aop, u_int32_t val1, int anum)
{
DCC_OPS result;
int result_len;
if (!init_conn(0))
return 0;
memset(&op_resp, 0, sizeof(op_resp));
gettimeofday(&op_start, 0);
result_len = sizeof(op_resp);
result = dcc_aop(dcc_emsg, ctxt, grey_on, anum, aop, val1, 0, 0, 0,
&op_resp, &result_len, &op_resp_su);
gettimeofday(&op_end, 0);
if (result == DCC_OP_INVALID
|| result == DCC_OP_ERROR) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
return 1;
}
static void
fin_aop(int anum, /* -1 or index of server */
u_char psrvr) /* 1=print server name */
{
if (psrvr)
print_aop(anum);
/* say what the server had to say */
fputs(op_resp.string, stdout);
putchar('\n');
if (dcc_clnt_debug) {
printf("%.2f ms\n",
((op_end.tv_sec-op_start.tv_sec)*1000.0
+ (op_end.tv_usec-op_start.tv_usec)/1000.0));
}
putchar('\n');
}
static u_char /* 0=some kind of problem, 1=done */
do_aop(DCC_AOPS aop, u_int32_t val, int anum, u_char psrvr)
{
if (!start_aop(aop, val, anum))
return 0;
fin_aop(anum, psrvr);
return 1;
}
static u_char /* 0=bad command, 1=did it */
do_op(const char *op)
{
int op_num;
op_num = 0;
for (;;) {
if (op_num >= DIM(aop_tbl)) {
dcc_error_msg("unrecognized command \"%s\"", op);
return 0;
}
/* do a command */
if (!cmd_cmp(op, aop_tbl[op_num].op))
break;
op_num++;
}
/* send an administrative request to the server */
if (!get_passwd(aop_tbl[op_num].privileged)) {
dcc_error_msg("\"%s\" is a privileged operation"PRV_MSG,
aop_tbl[op_num].op, DCC_NM2PATH(IDS_NM_DEF));
return 0;
}
/* try to send it */
return do_aop(aop_tbl[op_num].aop, aop_tbl[op_num].val, -1, 1);
}
static u_char
ck_priv_cmd(const CMD_TBL_ENTRY *ce, u_char uid_0, u_char privileged)
{
/* always call get_passwd() so we have always fetched a password */
if (!get_passwd(privileged)
|| (which_map != MAP_TMP
&& uid_0
&& 0 > access(info_map_nm, R_OK)
&& errno != ENOENT)) {
dcc_error_msg("\"%s\" is a privileged command"PRV_MSG,
ce->cmd, DCC_NM2PATH(IDS_NM_DEF));
return 0;
}
return 1;
}
static u_char /* 1=ok 0=bad command */
cmd(const char *p)
{
const char *arg;
int cmd_num, j;
const CMD_TBL_ENTRY *ce;
/* look for the string as a command and execute it if we find */
for (cmd_num = 0; cmd_num < DIM(cmd_tbl); ++cmd_num) {
ce = &cmd_tbl[cmd_num];
arg = cmd_cmp(p, ce->cmd);
/* if the command table entry and the command completely
* matched, then infer a null argument */
if (!arg) {
if (!ck_priv_cmd(ce, ce->uid_0, ce->privileged))
return 0;
if (ce->args != 1) {
j = ce->fnc("", ce);
if (j >= 0)
return j;
}
help_cmd(p, 0);
return 0;
}
/* if the command table entry is an initial sustring of
* the user's command, then the rest of the command must
* start with white space. Trim and use it as the argument */
j = strspn(arg, DCC_WHITESPACE);
if (j) {
if (ce->args == 2) {
help_cmd(p, 0); /* arg not allowed */
return 0;
}
if (!ck_priv_cmd(ce, ce->uid_0, ce->privileged))
return 0;
j = ce->fnc(arg+j, ce);
if (j >= 0)
return j;
help_cmd(p, 0);
return 0;
}
}
/* otherwise try to interpret it as a DCC administrative packet */
return do_op(p);
}
u_char /* 0=bad command, 1=ok */
do_cmds(char *cmd_buf)
{
char *next_cmd, *cur_cmd, *cmd_end;
char c;
next_cmd = cmd_buf;
for (;;) {
cur_cmd = next_cmd + strspn(next_cmd, DCC_WHITESPACE";");
if (*cur_cmd == '#' || *cur_cmd == '\0')
return 1;
next_cmd = cur_cmd + strcspn(cur_cmd, ";\n\r");
cmd_end = next_cmd;
next_cmd += strspn(next_cmd, ";\n\r");
/* null terminate and trim trailing white space from
* command or arg */
do {
*cmd_end-- = '\0';
c = *cmd_end;
} while (cmd_end >= cur_cmd
&& strchr(DCC_WHITESPACE";", c));
if (*cur_cmd == '\0') /* ignore blank commands */
continue;
if (!cmd(cur_cmd))
return 0;
}
}
static int
help_cmd_print(int pos, const char *str)
{
#define HELP_COL 24
int col;
col = strlen(str)+1;
col += HELP_COL - (col % HELP_COL);
pos += col;
if (pos > 78) {
putchar('\n');
pos = col;
}
printf("%-*s", col, str);
return pos;
#undef HELP_COL
}
static int
help_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
int i, pos;
const char *p;
/* say something about one command */
if (arg) {
for (i = 0; i < DIM(cmd_tbl); ++i) {
p = cmd_cmp(arg, cmd_tbl[i].cmd);
if (!p || *p == ' ' || *p == '\t') {
if (!cmd_tbl[i].help_str)
break;
printf("usage: %s\n", cmd_tbl[i].help_str);
return 1;
}
}
for (i = 0; i < DIM(aop_tbl); ++i) {
p = cmd_cmp(arg, aop_tbl[i].op);
if (!p || *p == ' ' || *p == '\t') {
p = aop_tbl[i].help_str;
if (!p || !*p)
p = aop_tbl[i].op;
printf("usage: %s\n", p);
return 1;
}
}
}
/* talk about all of the commands */
printf(" version "DCC_VERSION"\n");
pos = 0;
for (i = 0; i < DIM(cmd_tbl); ++i) {
p = cmd_tbl[i].help_str;
if (!p)
continue;
pos = help_cmd_print(pos, p);
}
for (i = 0; i < DIM(aop_tbl); ++i) {
p = aop_tbl[i].help_str;
if (!p) {
p = aop_tbl[i].op;
} else if (!*p) {
continue;
}
pos = help_cmd_print(pos, p);
}
putchar('\n');
return 1;
}
static int
exit_cmd(const char *arg UATTRIB, const CMD_TBL_ENTRY *ce UATTRIB)
{
exit(EX_OK);
return -1;
}
static int
grey_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
if (arg[0] == '\0') {
printf(" Greylist mode %s%s\n",
grey_on ? "on" : "off",
grey_set ? "" : " by default");
return 1;
}
if (!strcmp(arg, "off")) {
grey_on = 0;
grey_set = 1;
clear_conn();
} else if (!strcmp(arg, "on")) {
grey_on = 1;
grey_set = 1;
clear_conn();
} else {
return -1;
}
if (!port_set)
srvr.port = DCC_GREY2PORT(grey_on);
return 1;
}
static int
homedir_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
if (arg[0] != '\0') {
if (!dcc_cdhome(dcc_emsg, arg)) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
set_which_map(MAP_INFO);
}
printf(" homedir=%s\n", dcc_homedir);
return 1;
}
/* set name of map file */
static int
file_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
if (arg[0] == '\0') {
if (which_map == MAP_INFO)
printf(" using map file: %s\n",
dcc_info_nm);
else
printf(" map file %s but using temporary file\n",
info_map_nm);
return 1;
}
BUFCPY(info_map_nm, arg);
set_which_map(MAP_INFO);
return 1;
}
/* create a new client map or parameter file */
static int
new_map_cmd(const char *arg, const CMD_TBL_ENTRY *ce)
{
if (arg[0] == '\0')
arg = DCC_MAP_NM_DEF;
dcc_rel_priv();
if (!dcc_create_map(dcc_emsg, arg, 0, 0, 0, 0, 0, clnt_flags)) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
BUFCPY(info_map_nm, arg);
set_which_map(MAP_INFO);
if (!quiet) {
printf(" created %s\n", dcc_info_nm);
return info_cmd("", ce);
}
return 1;
}
/* server hostname */
static int
host_cmd(const char *arg, const CMD_TBL_ENTRY *ce)
{
DCC_SRVR_NM nm;
if (arg[0] == '\0') {
if (which_map == MAP_INFO)
return info_cmd("", ce);
printf(" %s server hostname \"%s\"\n",
grey_on ? "Greylist" : "DCC", srvr.hostname);
return 1;
}
if (!strcmp(arg, "-")) {
set_which_map(MAP_INFO);
if (!init_map(1)) {
set_which_map(MAP_TMP);
return 0;
}
dcc_ctxts_unlock();
return 1;
}
arg = dcc_parse_nm_port(0, arg, 0,
nm.hostname, sizeof(nm.hostname),
&nm.port, 0, 0,
0, 0);
if (!arg)
return 0;
arg += strspn(arg, DCC_WHITESPACE);
if (*arg != '\0')
return 0;
set_which_map(MAP_TMP);
memcpy(srvr.hostname, nm.hostname, sizeof(srvr.hostname));
if (nm.port != 0) {
srvr.port = nm.port;
port_set = 1;
}
return 1;
}
/* server port # */
static int
port_cmd(const char *arg, const CMD_TBL_ENTRY *ce)
{
u_int port;
if (arg[0] == '\0') {
if (which_map == MAP_INFO)
return info_cmd("", ce);
printf(" port=%d\n", ntohs(srvr.port));
return 1;
}
port = dcc_get_port(0, arg, DCC_GREY2PORT(grey_on), 0, 0);
if (port == DCC_GET_PORT_INVALID)
return 0;
srvr.port = port;
port_set = 1;
set_which_map(MAP_TMP);
return 1;
}
static int
ipv6_cmd(const char *arg, const CMD_TBL_ENTRY *ce)
{
u_char new_use_ipv6;
if (!init_map(1))
return 0;
if (arg[0] == '\0') {
printf(" IPv6 %s\n",
(clnt_flags & DCC_CLNT_INFO_FG_IPV6) ? "on" : "off");
return 1;
}
if (!ck_priv_cmd(ce, 1, 0))
return 0;
if (!strcmp(arg, "off")) {
new_use_ipv6 = 0;
} else if (!strcmp(arg, "on")) {
new_use_ipv6 = DCC_CLNT_INFO_FG_IPV6;
} else {
return -1;
}
dcc_ctxts_lock();
if (!dcc_resolve_lock(dcc_emsg)) {
dcc_error_msg("%s", dcc_emsg);
dcc_ctxts_unlock();
return 0;
}
if (dcc_clnt_info
&& (dcc_clnt_info->flags & DCC_CLNT_INFO_FG_IPV6) != new_use_ipv6) {
dcc_clnt_info->flags ^= DCC_CLNT_INFO_FG_IPV6;
clnt_flags = dcc_clnt_info->flags;
dcc_map_changed();
}
if (!dcc_resolve_unlock(dcc_emsg)) {
dcc_error_msg("%s", dcc_emsg);
dcc_ctxts_unlock();
return 0;
}
dcc_ctxts_unlock();
if (init_conn(1)
&& (dcc_clnt_info->flags & DCC_CLNT_INFO_FG_IPV6) != new_use_ipv6) {
#ifdef NO_IPV6
dcc_error_msg("IPv6 switch not changed;"
" No IPv6 support in this system?");
#else
dcc_error_msg("IPv6 switch not changed.");
#endif
}
return 1;
}
static int
socks_cmd(const char *arg, const CMD_TBL_ENTRY *ce)
{
u_char new_use_socks;
if (!init_map(1))
return 0;
if (arg[0] == '\0') {
printf(" SOCKS %s\n",
(clnt_flags & DCC_CLNT_INFO_FG_SOCKS) ? "on" : "off");
return 1;
}
if (!ck_priv_cmd(ce, 1, 0))
return 0;
if (!strcmp(arg, "off")) {
new_use_socks = 0;
} else if (!strcmp(arg, "on")) {
new_use_socks = DCC_CLNT_INFO_FG_SOCKS;
} else {
return -1;
}
dcc_ctxts_lock();
if (!dcc_resolve_lock(dcc_emsg)) {
dcc_error_msg("%s", dcc_emsg);
dcc_ctxts_unlock();
return 0;
}
if (dcc_clnt_info
&& (dcc_clnt_info->flags&DCC_CLNT_INFO_FG_SOCKS) != new_use_socks) {
dcc_clnt_info->flags ^= DCC_CLNT_INFO_FG_SOCKS;
clnt_flags = dcc_clnt_info->flags;
dcc_map_changed();
}
if (!dcc_resolve_unlock(dcc_emsg)) {
dcc_error_msg("%s", dcc_emsg);
dcc_ctxts_unlock();
return 0;
}
dcc_ctxts_unlock();
return 1;
}
static int
passwd_cmd(const char *arg, const CMD_TBL_ENTRY *ce)
{
DCC_PASSWD new_passwd;
if (arg[0] == '\0') {
if (which_map == MAP_INFO)
return info_cmd("", ce);
if (passwd_set)
printf(" password %s\n", passwd);
else
printf(" password not set\n");
return 1;
}
arg = dcc_parse_word(0, new_passwd, sizeof(passwd),
arg, "password", 0, 0);
if (!arg || *arg != '\0')
return -1;
strncpy(passwd, new_passwd, sizeof(passwd));
passwd_set = 1;
set_which_map(MAP_TMP);
return 1;
}
static int
id_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
DCC_CLNT_ID id;
if (arg[0] == '\0') {
printf(" ID=%d\n", srvr_clnt_id);
return 1;
}
id = dcc_get_id(0, arg, 0, 0);
if (id == DCC_ID_INVALID)
return -1;
srvr.clnt_id = srvr_clnt_id = id;
set_which_map(MAP_TMP);
get_passwd(1);
return 1;
}
static int
debug_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
char debug_str[24];
char ttl_str[24];
int new_ttl, new_debug;
char *p;
if (arg[0] == '\0') {
if (!dcc_clnt_debug)
snprintf(debug_str, sizeof(debug_str),
"debug off");
else if (dcc_clnt_debug == 1)
snprintf(debug_str, sizeof(debug_str),
"debug on");
else
snprintf(debug_str, sizeof(debug_str),
"debug on+%d\n", dcc_clnt_debug-1);
if (dcc_debug_ttl != 0)
snprintf(ttl_str, sizeof(ttl_str),
" TTL=%d", dcc_debug_ttl);
else
ttl_str[0] = '\0';
printf(" %s%s\n", debug_str, ttl_str);
return 1;
}
new_ttl = dcc_debug_ttl;
new_debug = dcc_clnt_debug;
for (;;) {
if (!CSTRCMP(arg, "off")) {
new_debug = 0;
arg += STRZ("off");
} else if (!CSTRCMP(arg, "on")) {
++new_debug;
arg += STRZ("on");
} else if (!CSTRCMP(arg, "ttl=")) {
new_ttl = strtoul(arg+STRZ("ttl="), &p, 0);
#if defined(IPPROTO_IP) && defined(IP_TTL)
if (new_ttl < 256)
arg = p;
#else
printf(" TTL setting not supported\n");
#endif
}
if (*arg == ' ' || *arg == '\t') {
arg += strspn(arg, DCC_WHITESPACE);
} else if (*arg == '\0') {
break;
} else {
return -1;
}
}
dcc_debug_ttl = new_ttl;
if (dcc_debug_ttl != 0)
set_which_map(MAP_TMP);
dcc_clnt_debug = new_debug;
if (dcc_clnt_debug > 1)
printf(" debug on+%d\n", dcc_clnt_debug-1);
return 1;
}
static int
delete_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
DCC_SRVR_CLASS *class;
DCC_SRVR_NM nm, *nmp;
DCC_SRVR_ADDR *addr;
u_char del_grey;
del_grey = grey_on;
if (!dcc_parse_srvr_nm(dcc_emsg, &nm, &del_grey, arg, 0, 0)) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
/* map and lock */
set_which_map(MAP_INFO);
if (!init_map(1))
return 0;
if (!dcc_resolve_lock(dcc_emsg)) {
dcc_ctxts_unlock();
dcc_error_msg("%s", dcc_emsg);
return 0;
}
class = DCC_GREY2CLASS(del_grey);
for (nmp = class->nms; nmp <= LAST(class->nms); ++nmp) {
if (strcasecmp(nmp->hostname, nm.hostname))
continue;
if (nmp->port == nm.port) {
/* Found it. Delete everything we've learned about
* its IP addresses so their values won't be saved
* during the re-measuring of RTT's */
for (addr = class->addrs;
addr <= LAST(class->addrs);
++addr) {
if (addr->nm_inx == nmp - class->nms)
memset(addr, 0, sizeof(*addr));
}
if (nmp != LAST(class->nms))
memmove(nmp, nmp+1,
(LAST(class->nms) - nmp)*sizeof(*nmp));
memset(LAST(class->nms), 0, sizeof(*nmp));
dcc_force_measure_rtt(class, 1);
if (!dcc_resolve_unlock(dcc_emsg))
dcc_error_msg("%s", dcc_emsg);
dcc_ctxts_unlock();
return 1;
}
}
if (!dcc_resolve_unlock(dcc_emsg))
dcc_error_msg("%s", dcc_emsg);
dcc_ctxts_unlock();
dcc_error_msg("entry not found");
return 0;
}
static int
add_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
DCC_SRVR_CLASS *class;
DCC_SRVR_NM nm, *nmp, *tgt_nmp;
u_char add_grey;
add_grey = grey_set && grey_on;
if (0 >= dcc_parse_srvr_nm(dcc_emsg, &nm, &add_grey, arg, 0, 0)) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
if (nm.clnt_id == DCC_ID_ANON && add_grey) {
dcc_error_msg("anonymous client-ID invalid"
" for Greylist server %s",
nm.hostname);
return 0;
}
/* map and lock the information */
set_which_map(MAP_INFO);
if (!init_map(1))
return 0;
/* lock the right to resolve hostnames */
if (!dcc_resolve_lock(dcc_emsg)) {
dcc_ctxts_unlock();
dcc_error_msg("%s", dcc_emsg);
return 0;
}
/* look for the old entry or a new, free entry */
class = DCC_GREY2CLASS(add_grey);
tgt_nmp = 0;
for (nmp = class->nms; nmp <= LAST(class->nms); ++nmp) {
if (nmp->hostname[0] == '\0') {
if (!tgt_nmp)
tgt_nmp = nmp;
continue;
}
if (!strcmp(nmp->hostname, nm.hostname)
&& nmp->port == nm.port) {
printf(" overwriting existing entry\n");
tgt_nmp = nmp;
break;
}
}
if (tgt_nmp) {
memcpy(tgt_nmp, &nm, sizeof(*tgt_nmp));
dcc_force_measure_rtt(class, 1);
if (!dcc_resolve_unlock(dcc_emsg))
dcc_error_msg("%s", dcc_emsg);
dcc_ctxts_unlock();
return 1;
}
if (!dcc_resolve_unlock(dcc_emsg))
dcc_error_msg("%s", dcc_emsg);
dcc_ctxts_unlock();
if (add_grey)
dcc_error_msg("too many Greylist server names");
else
dcc_error_msg("too many DCC server names");
return 0;
}
static void
add_new_nms(const DCC_SRVR_NM new_nms[DCC_MAX_SRVR_NMS],
DCC_SRVR_NM old_nms[DCC_MAX_SRVR_NMS])
{
const DCC_SRVR_NM *new_nmp;
DCC_SRVR_NM *old_nmp;
for (new_nmp = new_nms;
new_nmp < &new_nms[DCC_MAX_SRVR_NMS]
&& new_nmp->hostname[0] != '\0';
++new_nmp) {
for (old_nmp = old_nms;
old_nmp <= &old_nms[DCC_MAX_SRVR_NMS];
++old_nmp) {
if (old_nmp->hostname[0] == '\0'
|| (!strcmp(old_nmp->hostname, new_nmp->hostname)
&& old_nmp->port == new_nmp->port)) {
memcpy(old_nmp, new_nmp, sizeof(*old_nmp));
break;
}
}
}
}
static int
load_cmd(const char *lfile, const CMD_TBL_ENTRY *ce UATTRIB)
{
u_char new_clnt_flags, load_grey;
int flags_set;
u_char created;
DCC_SRVR_NM new_nm;
DCC_SRVR_NM dcc_nms[DCC_MAX_SRVR_NMS];
int num_dcc_nms;
DCC_SRVR_NM grey_nms[DCC_MAX_SRVR_NMS];
int num_grey_nms;
char buf[sizeof(DCC_SRVR_NM)*3];
const char *bufp, *cp;
FILE *f;
int fd, lineno;
if (*lfile == '\0')
return -1;
dcc_rel_priv();
if (!strcmp(lfile,"-")) {
lfile = 0;
fd = dup(fileno(stdin));
if (fd < 0) {
dcc_error_msg("dup(stdin): %s", ERROR_STR());
return 0;
}
f = fdopen(fd, "r");
if (!f) {
dcc_error_msg("fdopen(): %s", ERROR_STR());
return 0;
}
} else {
f = dcc_open_srvr_nm(dcc_emsg, lfile);
if (!f) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
}
/* parse the text file to create a pair of lists of server names */
flags_set = 0;
new_clnt_flags = clnt_flags;
num_dcc_nms = 0;
memset(dcc_nms, 0, sizeof(dcc_nms));
num_grey_nms = 0;
memset(grey_nms, 0, sizeof(grey_nms));
lineno = 0;
for (;;) {
bufp = fgets(buf, sizeof(buf), f);
if (!bufp) {
if (ferror(f)) {
dcc_error_msg("fgets(%s): %s",
!lfile
? "STDIN" : DCC_NM2PATH(lfile),
ERROR_STR());
fclose(f);
return 0;
}
break;
}
++lineno;
/* skip blank lines and comments */
bufp += strspn(bufp, DCC_WHITESPACE);
if (*bufp == '\0' || *bufp == '#')
continue;
/* look for flags in the first non-comment line */
if (!flags_set++) {
cp = bufp;
if (!strncmp(DCC_INFO_USE_IPV4, cp,
sizeof(DCC_INFO_USE_IPV4)-1)) {
cp += sizeof(DCC_INFO_USE_IPV4)-1;
new_clnt_flags = 0;
} else if (!strncmp(DCC_INFO_USE_IPV6, cp,
sizeof(DCC_INFO_USE_IPV6)-1)) {
cp += sizeof(DCC_INFO_USE_IPV6)-1;
new_clnt_flags = DCC_CLNT_INFO_FG_IPV6;
} else {
++flags_set;
}
if (flags_set == 1) {
/* We found "IPv6 on" or "off".
* Loof for "use SOCKS" */
cp += strspn(cp, DCC_WHITESPACE);
if (*cp == '\0')
continue;
if (!strcasecmp(DCC_INFO_USE_SOCKS"\n", cp)) {
new_clnt_flags|=DCC_CLNT_INFO_FG_SOCKS;
continue;
}
}
/* the first non-comment line must be a server name */
}
load_grey = 0;
if (0 >= dcc_parse_srvr_nm(dcc_emsg, &new_nm, &load_grey,
bufp, lfile, lineno)) {
dcc_error_msg("%s", dcc_emsg);
fclose(f);
return 0;
}
if (load_grey) {
if (new_nm.clnt_id == DCC_ID_ANON) {
dcc_error_msg("anonymous client-ID invalid"
" for Greylist server %s%s",
new_nm.hostname,
fnm_lineno(lfile, lineno));
fclose(f);
return 0;
}
if (num_grey_nms >= DIM(grey_nms)) {
dcc_error_msg("too many Greylist server names"
"%s",
fnm_lineno(lfile, lineno));
fclose(f);
return 0;
}
grey_nms[num_grey_nms++] = new_nm;
} else {
if (num_dcc_nms >= DIM(dcc_nms)) {
dcc_error_msg("too many DCC server names%s",
fnm_lineno(lfile, lineno));
fclose(f);
return 0;
}
dcc_nms[num_dcc_nms++] = new_nm;
}
}
fclose(f);
if (num_grey_nms == 0 && num_dcc_nms == 0) {
dcc_error_msg("no DCC server names%s",
fnm_lineno(lfile, lineno));
return 0;
}
/* create the map and lock, install, and unlock the information */
dcc_rel_priv();
dcc_ctxts_lock();
created = 0;
if (0 < dcc_map_info(dcc_emsg, info_map_nm, -1)) {
/* copy into an existing map */
if (!dcc_info_lock(dcc_emsg)) {
dcc_error_msg("%s", dcc_emsg);
dcc_ctxts_unlock();
return 0;
}
} else {
/* create a new map */
dcc_ctxts_unlock();
if (!dcc_create_map(dcc_emsg, info_map_nm, 0,
0, 0, 0, 0, new_clnt_flags)) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
created = 1;
printf(" created %s\n", dcc_info_nm);
set_which_map(MAP_INFO);
if (!init_map(1))
return 0;
}
if (!dcc_resolve_lock(dcc_emsg)) {
if (created)
unlink(dcc_info_nm);
dcc_error_msg("%s", dcc_emsg);
dcc_ctxts_unlock();
return 0;
}
/* merge the old and new entries */
add_new_nms(grey_nms, dcc_clnt_info->grey.nms);
add_new_nms(dcc_nms, dcc_clnt_info->dcc.nms);
dcc_clnt_info->flags = clnt_flags = new_clnt_flags;
dcc_map_changed();
if (!dcc_resolve_unlock(dcc_emsg))
dcc_error_msg("%s", dcc_emsg);
dcc_ctxts_unlock();
if (!lfile)
printf("##################\n\n");
set_which_map(MAP_INFO);
if (!quiet)
return info_cmd("", ce);
return 1;
}
static int
info_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
DCC_CLNT_INFO info;
u_char dcc, names;
if (*arg == '\0') {
names = 0;
} else if (!strcmp(arg, "-N")) {
names = 1;
} else {
return -1;
}
/* map, copy, and unlock the information
* prefer to talk to the server, but don't insist */
if (which_map == MAP_TMP) {
if (!init_conn(1))
return 0;
} else {
if (!init_conn(1)) {
set_which_map(MAP_TMP);
return 0;
}
}
/* snapshot the data and then release it while we print it */
memcpy(&info, dcc_clnt_info, sizeof(info));
dcc_ctxts_unlock();
if (!dcc_info_unlock(dcc_emsg)) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
dcc_rel_priv();
if (which_map == MAP_INFO) {
if (info.dcc.nms[0].hostname[0] != '\0'
|| !grey_on) {
dcc_print_info(dcc_info_nm, &info, 0, names,
0 <= access(dcc_info_nm, R_OK));
dcc = 1;
} else {
dcc = 0;
}
if (info.grey.nms[0].hostname[0] != '\0'
|| grey_on) {
if (dcc)
fputs("\n################\n", stdout);
dcc_print_info(dcc_info_nm, &info, 1, names,
0 <= access(dcc_info_nm, R_OK));
}
} else {
dcc_print_info(0, &info, 0, names, 1);
}
putchar('\n');
return 1;
}
static int
rtt_cmd(const char *arg, const CMD_TBL_ENTRY *ce)
{
if (!init_map(1))
return 0;
if (!dcc_resolve_lock(dcc_emsg)) {
dcc_error_msg("%s", dcc_emsg);
} else {
dcc_force_measure_rtt(&dcc_clnt_info->dcc, 0);
dcc_force_measure_rtt(&dcc_clnt_info->grey, 0);
if (!dcc_resolve_unlock(dcc_emsg))
dcc_error_msg("%s", dcc_emsg);
}
dcc_ctxts_unlock();
return info_cmd(arg, ce);
}
/* delete a checksum */
static int /* 1=ok, 0=bad checksum, -1=fatal */
delck_sub(DCC_EMSG emsg, DCC_WF *wf UATTRIB,
const char *fnm UATTRIB, int lineno UATTRIB,
DCC_CK_TYPES type, DCC_SUM sum, DCC_TGTS tgts UATTRIB)
{
struct timeval cmd_start, cmd_end;
char type_buf[DCC_XHDR_MAX_TYPE_LEN];
char ck_buf[sizeof(DCC_SUM)*3+2];
DCC_DELETE del;
union {
DCC_HDR hdr;
DCC_ERROR error;
} resp;
u_char result;
printf(" deleting %s %s\n",
dcc_type2str(type_buf, sizeof(type_buf), type, 0, 1),
dcc_ck2str(ck_buf, sizeof(ck_buf), type, sum));
memset(&del, 0, sizeof(del));
gettimeofday(&cmd_start, 0);
del.date = htonl(cmd_start.tv_sec);
del.ck.type = type;
del.ck.len = sizeof(del.ck);
memcpy(&del.ck.sum, sum, sizeof(DCC_SUM));
result = dcc_clnt_op(emsg, ctxt, DCC_CLNT_FG_NO_FAIL,
0, 0, &del.hdr, sizeof(del),
DCC_OP_DELETE, &resp.hdr, sizeof(resp), 0);
gettimeofday(&cmd_end, 0);
if (!result) {
dcc_error_msg("%s", dcc_emsg);
} else {
switch (resp.hdr.op) {
case DCC_OP_OK:
break;
case DCC_OP_ERROR:
dcc_error_msg(" %.*s",
(ntohs(resp.hdr.len)
-(int)(sizeof(resp.error)
- sizeof(resp.error.msg))),
resp.error.msg);
result = 0;
break;
default:
dcc_error_msg("unexpected response: %s",
dcc_hdr_op2str(&resp.hdr));
result = 0;
break;
}
}
if (dcc_clnt_debug) {
printf("%.2f ms\n",
((cmd_end.tv_sec-cmd_start.tv_sec)*1000.0
+ (cmd_end.tv_usec-cmd_start.tv_usec)/1000.0));
}
return result;
}
/* delete a simple checksum */
static int
delck_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
char type_str[DCC_XHDR_MAX_TYPE_LEN+1];
if (*arg == '\0')
return -1;
arg = dcc_parse_word(dcc_emsg, type_str, sizeof(type_str),
arg, 0, 0, 0);
if (!arg)
return -1;
if (!init_conn(0))
return 0;
return 0 < dcc_parse_hex_ck(dcc_emsg, &cmn_wf, 0, 0, type_str,
arg, 0, delck_sub);
}
static int
sleep_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
double s;
char *p;
s = strtod(arg, &p);
if (*p != '\0' || s < 0.001 || s > 1000)
return -1;
usleep((u_int)(s*1000000.0));
return 1;
}
/* get the server's list of recent clients */
static int
clients_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
#define CPY2(s) ((s[0]<<8) | s[1])
#define CPY3(s) ((s[0]<<16) | (s[1]<<8) | s[2])
#define CPY4(s) ((s[0]<<24) | (s[1]<<16) | (s[2]<<8) | s[3])
u_char nonames, sort, ids, avg, have_max_clients, have_thold;
u_int max_clients, thold;
int total, subtotal;
u_int num_clients, ct_size;
struct ct {
struct ct *lt, *gt, *up;
time_t last_used;
u_int32_t requests;
u_int rank;
u_int16_t nops;
u_char flags;
DCC_CLNT_ID id;
DCC_SOCKU su;
} *ctree, **ctptr, *ctup, *ct, *ctnew;
DCC_OPS result;
DCC_ADMN_RESP_CLIENTS *cl;
int cl_len, result_len;
char name[MAXHOSTNAMELEN];
char date_buf[40];
struct tm tm;
char *p;
int i;
max_clients = 10000;
have_max_clients = 0;
thold = 0;
have_thold = 0;
/* look for "-n", "-ns", "-n -s", etc. */
nonames = 0;
sort = 0;
ids = 0;
avg = 0;
while (*arg != 0) {
arg += strspn(arg, " \t");
if (*arg == '-') {
++arg;
do {
if (*arg == 'n') {
nonames = 1;
} else if (*arg == 's') {
sort = 1;
} else if (*arg == 'i') {
ids = 1;
} else if (*arg == 'a') {
avg = 1;
} else {
return -1;
}
} while (*++arg != ' ' && *arg != '\t' && *arg != '\0');
continue;
}
if (!have_max_clients
&& (i = strtoul(arg, &p, 10)) != 0
&& (*p == ' ' || *p == '\t' || *p == '\0')) {
have_max_clients = 1;
max_clients = i;
arg = p;
continue;
}
if (!have_thold
&& (i = strtoul(arg, &p, 10)) > 0
&& i < (1<<16)
&& (*p == ' ' || *p == '\t' || *p == '\0')) {
have_thold = 1;
thold = i;
arg = p;
continue;
}
return -1;
}
if (!ids
&& !ck_priv_cmd(ce, 0, 1))
return 0;
if (!init_conn(0))
return 0;
/* Collect all of the information before printing it to minimize
* the changes in the position of hosts and so deleted or missing
* entries. */
total = 0;
subtotal = 0;
ct_size = 0;
num_clients = 0;
ctree = 0;
for (;;) {
memset(&op_resp, 0, sizeof(op_resp));
gettimeofday(&op_start, 0);
result_len = sizeof(op_resp);
result = dcc_aop(dcc_emsg, ctxt, grey_on, -1,
ids ? DCC_AOP_CLIENTS_ID : DCC_AOP_CLIENTS,
(num_clients << 16) + thold,
sizeof(DCC_ADMN_RESP_VAL)
/ sizeof(DCC_ADMN_RESP_CLIENTS),
avg, 0,
&op_resp, &result_len, &op_resp_su);
if (result == DCC_OP_INVALID
|| result == DCC_OP_ERROR) {
dcc_error_msg("%s", dcc_emsg);
break;
}
if (!num_clients)
print_aop(-1);
if (result_len == 1)
break;
for (cl = op_resp.clients;
(char *)cl < result_len+(char *)op_resp.clients;
cl = (DCC_ADMN_RESP_CLIENTS *)((char *)cl + cl_len)) {
if (cl->flags & DCC_ADMN_RESP_CLIENTS_SKIP) {
num_clients += CPY3(cl->requests);
cl_len = (sizeof(*cl) - sizeof(cl->addr)
+ sizeof(cl->addr.ipv4));
continue;
}
++num_clients;
if (++ct_size > 10000) {
dcc_error_msg("too many clients");
goto stop;
}
ctnew = dcc_malloc(sizeof(*ctnew));
memset(ctnew, 0, sizeof(*ctnew));
ctnew->flags = cl->flags;
ctnew->id = CPY4(cl->id);
ctnew->last_used = CPY4(cl->last_used);
ctnew->requests = CPY3(cl->requests);
total += ctnew->requests;
ctnew->nops = CPY2(cl->nops);
if (cl->flags & DCC_ADMN_RESP_CLIENTS_IPV6) {
memcpy(&ctnew->su.ipv6.sin6_addr,
cl->addr.ipv6,
sizeof(ctnew->su.ipv6.sin6_addr));
ctnew->su.sa.sa_family = AF_INET6;
cl_len = (sizeof(*cl) - sizeof(cl->addr)
+ sizeof(cl->addr.ipv6));
} else {
memcpy(&ctnew->su.ipv4.sin_addr,
cl->addr.ipv4,
sizeof(ctnew->su.ipv4.sin_addr));
ctnew->su.sa.sa_family = AF_INET;
cl_len = (sizeof(*cl) - sizeof(cl->addr)
+ sizeof(cl->addr.ipv4));
}
/* add the new entry to the tree */
ctptr = &ctree;
ctup = 0;
for (;;) {
ct = *ctptr;
if (!ct) {
ctnew->up = ctup;
*ctptr = ctnew;
break;
}
i = !sort;
if (!i) {
i = (ct->flags
& DCC_ADMN_RESP_CLIENTS_BL);
i -= (ctnew->flags
& DCC_ADMN_RESP_CLIENTS_BL);
}
if (!i) {
i = ct->requests;
i -= ctnew->requests;
}
ctup = ct;
if (i >= 0) {
ctptr = &ct->lt;
} else {
/* update the threshold if sorting */
if (++ct->rank >= max_clients
&& thold < ct->requests)
thold = ct->requests;
ctptr = &ct->gt;
}
}
}
if ((char *)cl != result_len+(char *)op_resp.clients) {
dcc_error_msg("wrong sized clients response; %d != %d",
result_len,
(int)((char *)cl
- (char *)op_resp.clients));
break;
}
/* quit if we want only part of the list
* and we have it */
if (!sort && ct_size >= max_clients)
break;
}
stop:
if (!total)
total = 1;
/* print the list in the tree */
name[0] = '\0';
num_clients = 0;
for (ct = ctree; ct; ct = ctnew) {
ctnew = ct->gt;
if (ctnew) {
ct->gt = 0;
continue;
}
if (num_clients == 0) {
if (sort) {
fputs(" ops nops ", stdout);
if (ids)
fputs(" ID ", stdout);
fputs(" last\n", stdout);
} else {
fputs(" ops nops last ID\n",
stdout);
}
}
if (++num_clients <= max_clients) {
if (sort) {
subtotal += ct->requests;
printf("%3d%% %3d%% ",
ct->requests*100/total,
subtotal*100/total);
}
printf("%7d %5d ", ct->requests, ct->nops);
if (sort && ids)
printf("%5d ", ct->id);
strftime(date_buf, sizeof(date_buf), "%m/%d %X",
dcc_localtime(ct->last_used, &tm));
printf("%s", date_buf);
if (ct->flags & DCC_ADMN_RESP_CLIENTS_BL)
fputs(" BLACKLIST", stdout);
if (!sort)
printf(" %5d", ct->id);
if (!ids) {
if (!nonames)
dcc_su2name(name, sizeof(name),
&ct->su);
printf(" %-16s %s",
dcc_su2str_opt(&ct->su, 0, '\0'), name);
}
putchar('\n');
ctnew = ct->lt;
if (!ctnew) {
ctnew = ct->up;
} else {
ctnew->up = ct->up;
}
}
memset(ct, 0, sizeof(*ct));
dcc_free(ct);
}
putchar('\n');
return 1;
#undef CPY2
#undef CPY3
#undef CPY4
}
/* get and set the server's anonymous client delay */
static int
anon_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
int new_delay, old_delay, inflate;
DCC_OPS result;
int result_len;
char *inflate_str, *p;
inflate = 0;
if (*arg == '\0') {
new_delay = DCC_NO_ANON_DELAY;
} else {
if (!ck_priv_cmd(ce, 0, 1))
return 0;
if (!strcasecmp(arg, "forever")) {
new_delay = DCC_ANON_DELAY_FOREVER;
} else {
new_delay = strtoul(arg, &inflate_str, 10);
if (new_delay > DCC_ANON_DELAY_MAX
|| (*inflate_str != '\0' && *inflate_str != ',')) {
dcc_error_msg("invalid delay: \"%s\"", arg);
return 0;
}
if (*inflate_str == ',')
inflate_str += strspn(inflate_str,
DCC_WHITESPACE);
if (*inflate_str != '\0'
&& strcasecmp(inflate_str, "none")) {
inflate = strtoul(++inflate_str, &p, 10);
if (*p != '\0') {
dcc_error_msg("invalid delay inflation:"
" \"%s\"", inflate_str);
return 0;
}
}
}
}
if (!init_conn(0))
return 0;
memset(&op_resp, 0, sizeof(op_resp));
gettimeofday(&op_start, 0);
result_len = sizeof(op_resp);
result = dcc_aop(dcc_emsg, ctxt, grey_on, -1, DCC_AOP_ANON_DELAY,
inflate, new_delay>>8, new_delay, 0,
&op_resp, &result_len, &op_resp_su);
if (result == DCC_OP_INVALID
|| result == DCC_OP_ERROR) {
dcc_error_msg("%s", dcc_emsg);
return 0;
}
old_delay = ((op_resp.anon_delay.delay[0]<<8)
+ op_resp.anon_delay.delay[1]);
if (old_delay == DCC_ANON_DELAY_FOREVER) {
printf(" anon delay %s FOREVER\n",
new_delay != DCC_NO_ANON_DELAY ? "was" : "is");
} else {
printf(" anon delay %s %d",
new_delay != DCC_NO_ANON_DELAY ? "was" : "is",
old_delay);
inflate = ((op_resp.anon_delay.inflate[0]<<24)
+(op_resp.anon_delay.inflate[1]<<16)
+(op_resp.anon_delay.inflate[2]<<8)
+op_resp.anon_delay.inflate[3]);
if (inflate != 0)
printf(",%d", inflate);
putchar('\n');
}
return 1;
}
/* rewind the flood from a single server */
static int
flod_rewind(const char *arg UATTRIB, const CMD_TBL_ENTRY *ce UATTRIB)
{
DCC_CLNT_ID id;
if (!arg)
return -1;
id = dcc_get_id(0, arg, 0, 0);
if (id == DCC_ID_INVALID)
return -1;
return do_aop(DCC_AOP_FLOD, id*256 + DCC_AOP_FLOD_REWIND, -1, 1);
}
/* fast forward the flood to a single server */
static int
ffwd_out(const char *arg UATTRIB, const CMD_TBL_ENTRY *ce UATTRIB)
{
DCC_CLNT_ID id;
if (!arg)
return -1;
id = dcc_get_id(0, arg, 0, 0);
if (id == DCC_ID_INVALID)
return -1;
return do_aop(DCC_AOP_FLOD, id*256 + DCC_AOP_FLOD_FFWD_OUT, -1, 1);
}
/* fast forward the flood to a single server */
static int
ffwd_in(const char *arg UATTRIB, const CMD_TBL_ENTRY *ce UATTRIB)
{
DCC_CLNT_ID id;
if (!arg)
return -1;
id = dcc_get_id(0, arg, 0, 0);
if (id == DCC_ID_INVALID)
return -1;
return do_aop(DCC_AOP_FLOD, id*256 + DCC_AOP_FLOD_FFWD_IN, -1, 1);
}
/* get the flood counts for a server */
static int
flod_stats(const char *arg UATTRIB, const CMD_TBL_ENTRY *ce UATTRIB)
{
u_int32_t id, next_id;
DCC_AOP_FLODS op;
u_char heading;
int sresult;
if (!arg)
return -1;
if (!CSTRCMP(arg, "clear")) {
arg += STRZ("clear");
arg += strspn(arg, DCC_WHITESPACE);
op = DCC_AOP_FLOD_STATS_CLEAR;
} else {
op = DCC_AOP_FLOD_STATS;
}
heading = 1;
if (!strcasecmp(arg, "all")) {
id = DCC_SRVR_ID_MAX+1;
for (;;) {
if (!start_aop(DCC_AOP_FLOD, id*256 + op, -1))
return 0;
sresult = sscanf(op_resp.string,
DCC_AOP_FLOD_STATS_ID, &next_id);
if (1 == sresult
&& id == next_id)
return 1;
fin_aop(-1, heading);
heading = 0;
if (1 != sresult)
return 0;
id = next_id+DCC_SRVR_ID_MAX+1;
}
}
id = dcc_get_id(0, arg, 0, 0);
if (id == DCC_ID_INVALID)
return -1;
return do_aop(DCC_AOP_FLOD, id*256 + op, -1, heading);
}
/* get the statistics from all known servers */
static int
stats_cmd(const char *arg, const CMD_TBL_ENTRY *ce UATTRIB)
{
DCC_SRVR_CLASS *class;
int anum, srvrs_gen;
DCC_AOPS aop;
/* look for "clear" or "all" */
anum = -1;
aop = DCC_AOP_STATS;
while (*arg != 0) {
arg += strspn(arg, " \t");
if (anum == -1
&& !CSTRCMP(arg, "clear")) {
arg += STRZ("clear");
aop = DCC_AOP_STATS_CLEAR;
if (!get_passwd(aop_tbl[aop].privileged)) {
dcc_error_msg("\"stats clear\""
" is a privileged operation"
PRV_MSG,
DCC_NM2PATH(IDS_NM_DEF));
return 0;
}
} else if (aop == DCC_AOP_STATS
&& !CSTRCMP(arg, "all")) {
arg += STRZ("all");
anum = 0;
}
if (*arg != '\0' && *arg != ' ' && *arg != '\t')
return -1;
}
if (!init_conn(0))
return 0;
class = DCC_GREY2CLASS(grey_on);
srvrs_gen = class->gen;
do {
if (srvrs_gen != class->gen) {
dcc_error_msg("list of servers changed");
return 0;
}
/* skip dead servers */
if (class->addrs[anum].srvr_id == DCC_ID_INVALID
&& anum >= 0)
continue;
do_aop(aop, sizeof(op_resp), anum, 1);
fflush(stderr);
fflush(stdout);
} while (anum >= 0 && ++anum < class->num_addrs);
return 1;
}
/* restore tracing to default */
static int
trace_def(const char *arg UATTRIB, const CMD_TBL_ENTRY *ce UATTRIB)
{
if (!init_conn(0))
return 0;
return (do_aop(DCC_AOP_TRACE_ON, DCC_TRACE_ON_DEF_BITS, -1, 1)
&& do_aop(DCC_AOP_TRACE_OFF, DCC_TRACE_OFF_DEF_BITS, -1, 1));
}
|