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 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020 13021 13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033 13034 13035 13036 13037 13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056 13057 13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080 13081 13082 13083 13084 13085 13086 13087 13088 13089 13090 13091 13092 13093 13094 13095 13096 13097 13098 13099 13100 13101 13102 13103 13104 13105 13106 13107 13108 13109 13110 13111 13112 13113 13114 13115 13116 13117 13118 13119 13120 13121 13122 13123 13124 13125 13126 13127 13128 13129 13130 13131 13132 13133 13134 13135 13136 13137 13138 13139 13140 13141 13142 13143 13144 13145 13146 13147 13148 13149 13150 13151 13152 13153 13154 13155 13156 13157 13158 13159 13160 13161 13162 13163 13164 13165 13166 13167 13168 13169 13170 13171 13172 13173 13174 13175 13176 13177 13178 13179 13180 13181 13182 13183 13184 13185 13186 13187 13188 13189 13190 13191 13192 13193 13194 13195 13196 13197 13198 13199 13200 13201 13202 13203 13204 13205 13206 13207 13208 13209 13210 13211 13212 13213 13214 13215 13216 13217 13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 13231 13232 13233 13234 13235 13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 13273 13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 13292 13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 13303 13304 13305 13306 13307 13308 13309 13310 13311 13312 13313 13314 13315 13316 13317 13318 13319 13320 13321 13322 13323 13324 13325 13326 13327 13328 13329 13330 13331 13332 13333 13334 13335 13336 13337 13338 13339 13340 13341 13342 13343 13344 13345 13346 13347 13348 13349 13350 13351 13352 13353 13354 13355 13356 13357 13358 13359 13360 13361 13362 13363 13364 13365 13366 13367 13368 13369 13370 13371 13372 13373 13374 13375 13376 13377 13378 13379 13380 13381 13382 13383 13384 13385 13386 13387 13388 13389 13390 13391 13392 13393 13394 13395 13396 13397 13398 13399 13400 13401 13402 13403 13404 13405 13406 13407 13408 13409 13410 13411 13412 13413 13414 13415 13416 13417 13418 13419 13420 13421 13422 13423 13424 13425 13426 13427 13428 13429 13430 13431 13432 13433 13434 13435 13436 13437 13438 13439 13440 13441 13442 13443 13444 13445 13446 13447 13448 13449 13450 13451 13452 13453 13454 13455 13456 13457 13458 13459 13460 13461 13462 13463 13464 13465 13466 13467 13468 13469 13470 13471 13472 13473 13474 13475 13476 13477 13478 13479 13480 13481 13482 13483 13484 13485 13486 13487 13488 13489 13490 13491 13492 13493 13494 13495 13496 13497 13498 13499 13500 13501 13502 13503 13504 13505 13506 13507 13508 13509 13510 13511 13512 13513 13514 13515 13516 13517 13518 13519 13520 13521 13522 13523 13524 13525 13526 13527 13528 13529 13530 13531 13532 13533 13534 13535 13536 13537 13538 13539 13540 13541 13542 13543 13544 13545 13546 13547 13548 13549 13550 13551 13552 13553 13554 13555 13556 13557 13558 13559 13560 13561 13562 13563 13564 13565 13566 13567 13568 13569 13570 13571 13572 13573 13574 13575 13576 13577 13578 13579 13580 13581 13582 13583 13584 13585 13586 13587 13588 13589 13590 13591 13592 13593 13594 13595 13596 13597 13598 13599 13600 13601 13602 13603 13604 13605 13606 13607 13608 13609 13610 13611 13612 13613 13614 13615 13616 13617 13618 13619 13620 13621 13622 13623 13624 13625 13626 13627 13628 13629 13630 13631 13632 13633 13634 13635 13636 13637 13638 13639 13640 13641 13642 13643 13644 13645 13646 13647 13648 13649 13650 13651 13652 13653 13654 13655 13656 13657 13658 13659 13660 13661 13662 13663 13664 13665 13666 13667 13668 13669 13670 13671 13672 13673 13674 13675 13676 13677 13678 13679 13680 13681 13682 13683 13684 13685 13686 13687 13688 13689 13690 13691 13692 13693 13694 13695 13696 13697 13698 13699 13700 13701 13702 13703 13704 13705 13706 13707 13708 13709 13710 13711 13712 13713 13714 13715 13716 13717 13718 13719 13720 13721 13722 13723 13724 13725 13726 13727 13728 13729 13730 13731 13732 13733 13734 13735 13736 13737 13738 13739 13740 13741 13742 13743 13744 13745 13746 13747 13748 13749 13750 13751 13752 13753 13754 13755 13756 13757 13758 13759 13760 13761 13762 13763 13764 13765 13766 13767 13768 13769 13770 13771 13772 13773 13774 13775 13776 13777 13778 13779 13780 13781 13782 13783 13784 13785 13786 13787 13788 13789 13790 13791 13792 13793 13794 13795 13796 13797 13798 13799 13800 13801 13802 13803 13804 13805 13806 13807 13808 13809 13810 13811 13812 13813 13814 13815 13816 13817 13818 13819 13820 13821 13822 13823 13824 13825 13826 13827 13828 13829 13830 13831 13832 13833 13834 13835 13836 13837 13838 13839 13840 13841 13842 13843 13844 13845 13846 13847 13848 13849 13850 13851 13852 13853 13854 13855 13856 13857 13858 13859 13860 13861 13862 13863 13864 13865 13866 13867 13868 13869 13870 13871 13872 13873 13874 13875 13876 13877 13878 13879 13880 13881 13882 13883 13884 13885 13886 13887 13888 13889 13890 13891 13892 13893 13894 13895 13896 13897 13898 13899 13900 13901 13902 13903 13904 13905 13906 13907 13908 13909 13910 13911 13912 13913 13914 13915 13916 13917 13918 13919 13920 13921 13922 13923 13924 13925 13926 13927 13928 13929 13930 13931 13932 13933 13934 13935 13936 13937 13938 13939 13940 13941 13942 13943 13944 13945 13946 13947 13948 13949 13950 13951 13952 13953 13954 13955 13956 13957 13958 13959 13960 13961 13962 13963 13964 13965 13966 13967 13968 13969 13970 13971 13972 13973 13974 13975 13976 13977 13978 13979 13980 13981 13982 13983 13984 13985 13986 13987 13988 13989 13990 13991 13992 13993 13994 13995 13996 13997 13998 13999 14000 14001 14002 14003 14004 14005 14006 14007 14008 14009 14010 14011 14012 14013 14014 14015 14016 14017 14018 14019 14020 14021 14022 14023 14024 14025 14026 14027 14028 14029 14030 14031 14032 14033 14034 14035 14036 14037 14038 14039 14040 14041 14042 14043 14044 14045 14046 14047 14048 14049 14050 14051 14052 14053 14054 14055 14056 14057 14058 14059 14060 14061 14062 14063 14064 14065 14066 14067 14068 14069 14070 14071 14072 14073 14074 14075 14076 14077 14078 14079 14080 14081 14082 14083 14084 14085 14086 14087 14088 14089 14090 14091 14092 14093 14094 14095 14096 14097 14098 14099 14100 14101 14102 14103 14104 14105 14106 14107 14108 14109 14110 14111 14112 14113 14114 14115 14116 14117 14118 14119 14120 14121 14122 14123 14124 14125 14126 14127 14128 14129 14130 14131 14132 14133 14134 14135 14136 14137 14138 14139 14140 14141 14142 14143 14144 14145 14146 14147 14148 14149 14150 14151 14152 14153 14154 14155 14156 14157 14158 14159 14160 14161 14162 14163 14164 14165 14166 14167 14168 14169 14170 14171 14172 14173 14174 14175 14176 14177 14178 14179 14180 14181 14182 14183 14184 14185 14186 14187 14188 14189 14190 14191 14192 14193 14194 14195 14196 14197 14198 14199 14200 14201 14202 14203 14204 14205 14206 14207 14208 14209 14210 14211 14212 14213 14214 14215 14216 14217 14218 14219 14220 14221 14222 14223 14224 14225 14226 14227 14228 14229 14230 14231 14232 14233 14234 14235 14236 14237 14238 14239 14240 14241 14242 14243 14244 14245 14246 14247 14248 14249 14250 14251 14252 14253 14254 14255 14256 14257 14258 14259 14260 14261 14262 14263 14264 14265 14266 14267 14268 14269 14270 14271 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298 14299 14300 14301 14302 14303 14304 14305 14306 14307 14308 14309 14310 14311 14312 14313 14314 14315 14316 14317 14318 14319 14320 14321 14322 14323 14324 14325 14326 14327 14328 14329 14330 14331 14332 14333 14334 14335 14336 14337 14338 14339 14340 14341 14342 14343 14344 14345 14346 14347 14348 14349 14350 14351 14352 14353 14354 14355 14356 14357 14358 14359 14360 14361 14362 14363 14364 14365 14366 14367 14368 14369 14370 14371 14372 14373 14374 14375 14376 14377 14378 14379 14380 14381 14382 14383 14384 14385 14386 14387 14388 14389 14390 14391 14392 14393 14394 14395 14396 14397 14398 14399 14400 14401 14402 14403 14404 14405 14406 14407 14408 14409 14410 14411 14412 14413 14414 14415 14416 14417 14418 14419 14420 14421 14422 14423 14424 14425 14426 14427 14428 14429 14430 14431 14432 14433 14434 14435 14436 14437 14438 14439 14440 14441 14442 14443 14444 14445 14446 14447 14448 14449 14450 14451 14452 14453 14454 14455 14456 14457 14458 14459 14460 14461 14462 14463 14464 14465 14466 14467 14468 14469 14470 14471 14472 14473 14474 14475 14476 14477 14478 14479 14480 14481 14482 14483 14484 14485 14486 14487 14488 14489 14490 14491 14492 14493 14494 14495 14496 14497 14498 14499 14500 14501 14502 14503 14504 14505 14506 14507 14508 14509 14510 14511 14512 14513 14514 14515 14516 14517 14518 14519 14520 14521 14522 14523 14524 14525 14526 14527 14528 14529 14530 14531 14532 14533 14534 14535 14536 14537 14538 14539 14540 14541 14542 14543 14544 14545 14546 14547 14548 14549 14550 14551 14552 14553 14554 14555 14556 14557 14558 14559 14560 14561 14562 14563 14564 14565 14566 14567 14568 14569 14570 14571 14572 14573 14574 14575 14576 14577 14578 14579 14580 14581 14582 14583 14584 14585 14586 14587 14588 14589 14590 14591 14592 14593 14594 14595 14596 14597 14598 14599 14600 14601 14602 14603 14604 14605 14606 14607 14608 14609 14610 14611 14612 14613 14614 14615 14616 14617 14618 14619 14620 14621 14622 14623 14624 14625 14626 14627 14628 14629 14630 14631 14632 14633 14634 14635 14636 14637 14638 14639 14640 14641 14642 14643 14644 14645 14646 14647 14648 14649 14650 14651 14652 14653 14654 14655 14656 14657 14658 14659 14660 14661 14662 14663 14664 14665 14666 14667 14668 14669 14670 14671 14672 14673 14674 14675 14676 14677 14678 14679 14680 14681 14682 14683 14684 14685 14686 14687 14688 14689 14690 14691 14692 14693 14694 14695 14696 14697 14698 14699 14700 14701 14702 14703 14704 14705 14706 14707 14708 14709 14710 14711 14712 14713 14714 14715 14716 14717 14718 14719 14720 14721 14722 14723 14724 14725 14726 14727 14728 14729 14730 14731 14732 14733 14734 14735 14736 14737 14738 14739 14740 14741 14742 14743 14744 14745 14746 14747 14748 14749 14750 14751 14752 14753 14754 14755 14756 14757 14758 14759 14760 14761 14762 14763 14764 14765 14766 14767 14768 14769 14770 14771 14772 14773 14774 14775 14776 14777 14778 14779 14780 14781 14782 14783 14784 14785 14786 14787 14788 14789 14790 14791 14792 14793 14794 14795 14796 14797 14798 14799 14800 14801 14802 14803 14804 14805 14806 14807 14808 14809 14810 14811 14812 14813 14814 14815 14816 14817 14818 14819 14820 14821 14822 14823 14824 14825 14826 14827 14828 14829 14830
|
/* -*-mode: c; indent-tabs-mode: nil; c-basic-offset: 2; -*-
*/
/**
* mod_qos.c: Quality of service module for Apache Web Server.
*
* The Apache Web Servers requires threads and processes to serve
* requests. Each TCP connection to the web server occupies one
* thread or process. Sometimes, a server gets too busy to serve
* every request due the lack of free processes or threads.
*
* This module implements control mechanisms that can provide
* different priority to different requests.
*
* mod_qos requires OpenSSL, PCRE, threading and shared memory
* support. It has been designed, developed and fully
* tested for Apache httpd MPM worker binaries and it is optimized
* to be used in a reverse proxy.
*
* See http://mod-qos.sourceforge.net/ for further
* details and to obtain the latest version of this module.
*
* Copyright (C) 2023 Pascal Buchbinder
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/************************************************************************
* Version
***********************************************************************/
static const char revision[] = "$Id: mod_qos.c 2706 2023-05-16 19:52:59Z pbuchbinder $";
static const char g_revision[] = "11.74";
/************************************************************************
* Includes
***********************************************************************/
/* std */
#include <ctype.h>
#include <time.h>
#ifndef WIN32
# include <arpa/inet.h>
# include <unistd.h>
#else
# include <ws2tcpip.h>
# include <windows.h>
# include <direct.h>
#endif
#include <stdlib.h>
// for socket options
#ifdef __unix__
#include <sys/types.h>
#include <sys/socket.h>
#endif
/* apache */
#include <httpd.h>
#include <http_core.h>
#include <http_main.h>
#include <http_protocol.h>
#include <http_request.h>
#include <http_connection.h>
#define CORE_PRIVATE
#include <http_config.h>
#include <http_log.h>
#include <util_filter.h>
#include <ap_mpm.h>
#include <scoreboard.h>
#include <ap_config.h>
#include <ap_regex.h>
#include <mpm_common.h>
#include <util_md5.h>
/* apr / scrlib */
#include <apr_strings.h>
#include <apr_file_info.h>
#include <apr_base64.h>
#include <apr_hooks.h>
#include <apr_lib.h>
#ifdef AP_NEED_SET_MUTEX_PERMS
#include <unixd.h>
#endif
/* mod_qos requires OpenSSL */
#include <openssl/rand.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
/* additional modules */
#include "mod_status.h"
/* Preprocessor Definitions
* this (optional header file allowing other modules to register to our hoos: */
#ifdef QS_MOD_EXT_HOOKS
#include "mod_qos.h"
#endif
/************************************************************************
* defines
***********************************************************************/
#define QOS_LOG_PFX(id) "mod_qos("#id"): "
#define QOS_LOGD_PFX "mod_qos(): "
#define QOS_LOG_MSGCT 200
#define QOS_HASH_LEN 16
#define QOS_MAX_AGE "3600"
#define QOS_COOKIE_NAME "MODQOS"
#define QOS_USER_TRACKING "mod_qos_user_id"
#define QOS_USER_TRACKING_NEW "QOS_USER_ID_NEW"
#define QOS_USER_TRACKING_RENEW "QOS_USER_ID_RENEW"
#define QOS_DISABLE_UTC_ENFORCEMENT "DISABLE_UTC_ENFORCEMENT"
#define QOS_MILESTONE "mod_qos_milestone"
#define QOS_MILESTONE_TIMEOUT 3600
#define QOS_MILESTONE_COOKIE "QSSCD"
#define QS_SIM_IP_LEN 100
#define QS_USR_SPE "mod_qos::user"
#define QS_REC_COOKIE "mod_qos::gc"
#define QS_R010_ALREADY_BLOCKED "R010B"
#define QS_R012_ALREADY_BLOCKED "R012B"
#define QS_R013_ALREADY_BLOCKED "R013B"
#define QS_PKT_RATE_TH 3
#define QS_BW_SAMPLING_RATE 10
// split linear QS_SrvMaxConnPerIP* entry (conn->conn_ip) search:
#ifndef QS_MEM_SEG
#define QS_MEM_SEG 4
#endif
// buffer if srv opens new connections faster than it closes existing ones
#define QS_DOUBLE_CONN_H 128
#define QS_DOUBLE_CONN 32
#define QS_CONN_ABORT "mod_qos_connection_aborted"
/* Preprocessor Definitions
* QSLOG_CLID, QSLOG_EVENT, QSLOG_AVERAGE define parameters for QSLog: */
#ifndef QSLOG_CLID
#define QSLOG_CLID "mod_qos_user_id"
#endif
#ifndef QSLOG_EVENT
#define QSLOG_EVENT "Event"
#endif
#ifndef QSLOG_AVERAGE
#define QSLOG_AVERAGE "QS_AllConn"
#endif
/* Preprocessor Definitions
* logs repeating messages only once: */
#ifndef QS_LOG_REPEAT
#define QS_LOG_REPEAT 20
#endif
#define QS_IP4IN6 "::ffff:"
#define QS_PARP_Q "qos-parp-query"
#define QS_PARP_QUERY "qos-query"
#define QS_PARP_PATH "qos-path"
#define QS_PARP_LOC "qos-loc"
#define QSLOGFORMAT "ISBiDUkEQaC"
#define QS_SET_DSCP "QS_Set_DSCP"
#define QS_RESDELYATIME "QS_ResponseDelayTime"
#define QS_CONNID "QS_ConnectionId"
#define QS_COUNTRY "QS_Country"
#define QS_SERIALIZE "QS_Serialize"
#define QS_SRVSERIALIZE "QS_SrvSerialize"
#define QS_ErrorNotes "QS_ErrorNotes"
#define QS_BLOCK "QS_Block"
#define QS_BLOCK_SEEN "QS_Block_seen"
#define QS_BLOCK_DEC "QS_Block_Decrement"
#define QS_LIMIT_NAME_PFX "QS_Limit_VAR_NAME_IDX"
#define QS_LIMIT_DEFAULT "QS_Limit"
#define QS_LIMIT_SEEN "QS_Limit_seen"
#define QS_COUNTER_SUFFIX "_Counter"
#define QS_LIMIT_CLEAR "_Clear"
#define QS_LIMIT_DEC "_Decrement"
#define QS_LIMIT_REMAINING "_Remaining"
#define QS_EVENT "QS_Event"
#define QS_COND "QS_Cond"
#define QS_ISVIPREQ "QS_IsVipRequest"
#define QS_VipRequest "QS_VipRequest"
#define QS_KEEPALIVE "QS_KeepAliveTimeout"
#define QS_MAXKEEPALIVEREQ "QS_MaxKeepAliveRequests"
#define QS_TIMEOUT "QS_Timeout"
#define QS_CLOSE "QS_SrvMinDataRate"
#define QS_MAXIP "QS_SrvMaxConnPerIP"
#define QS_EMPTY_CON "NullConnection"
#define QS_BROKEN_CON "BrokenConnection"
#define QS_RuleId "QS_RuleId"
// enable connection counter if one of the following feature is used
#define QS_COUNT_CONNECTIONS(sconf) (sconf->max_conn != -1) || \
(sconf->min_rate_max != -1) || \
(sconf->max_conn_close != -1) || \
(sconf->max_conn_per_ip_connections != 1) || \
sconf->geodb
// "3758096128","3758096383","AU"
#define QS_GEO_PATTERN "\"([0-9]+)\",\"([0-9]+)\",\"([A-Z0-9]{2}|-)\""
static const char *m_env_variables[] = {
QS_ErrorNotes,
QS_SERIALIZE,
QS_SRVSERIALIZE,
QS_BLOCK,
QS_BLOCK_SEEN,
QS_BLOCK_DEC,
QS_LIMIT_DEFAULT,
QS_LIMIT_SEEN,
QS_EVENT,
QS_COND,
QS_ISVIPREQ,
QS_VipRequest,
QS_KEEPALIVE,
QS_MAXKEEPALIVEREQ,
QS_CLOSE,
QS_EMPTY_CON,
QS_BROKEN_CON,
QS_RuleId,
NULL
};
static const char *m_note_variables[] = {
QS_PARP_PATH,
QS_PARP_QUERY,
NULL
};
#define QS_INCTX_ID inctx->id
/* Preprocessor Definitions
This is the measure rate for QS_SrvRequestRate/QS_SrvMinDataRate which may
be increased to 10 or 30 seconds in order to compensate bandwidth variations.
You may also use the QS_SrvSampleRate directive to override this default.
Set it greater than the Apache Timeout directive to prevent from closing
unused speculative TCP pre-connections. */
#ifndef QS_REQ_RATE_TM
#define QS_REQ_RATE_TM 5
#endif
#define QS_MAX_DELAY 5000000
#define QOS_DEC_MODE_FLAGS_URL 0x00
#define QOS_DEC_MODE_FLAGS_HTML 0x01
#define QOS_DEC_MODE_FLAGS_UNI 0x02
#define QOS_DEC_MODE_FLAGS_ANSI 0x04
#define QOS_LOW_FLAG_PKGRATE 0x01
#define QOS_LOW_FLAG_BEHAVIOR_OK 0x02
#define QOS_LOW_FLAG_BEHAVIOR_BAD 0x04
#define QOS_LOW_FLAG_EVENTBLOCK 0x08
#define QOS_LOW_FLAG_EVENTLIMIT 0x10
#define QOS_LOW_FLAG_TIMEOUT 0x20
#define QOS_LOW_TIMEOUT 86400
#define QOS_CC_BEHAVIOR_THR 50000
#define QOS_CC_BEHAVIOR_THR_SINGLE 50
#ifdef QS_INTERNAL_TEST
#undef QOS_CC_BEHAVIOR_THR
#undef QOS_CC_BEHAVIOR_THR_SINGLE
#define QOS_CC_BEHAVIOR_THR 50
#define QOS_CC_BEHAVIOR_THR_SINGLE 20
#endif
#define QOS_CC_BEHAVIOR_TOLERANCE_STR "20"
/* Apache 2.2 testing needs patch in util_pcre.c line 134 as it does not know AP_REG_DOTALL
* Add:
* if ((cflags & 0x40) != 0) options |= 0x04; */
#ifndef AP_REG_DOTALL
#define AP_REG_DOTALL 0x40
#endif
#define QS_ERR_TIME_FORMAT "%a %b %d %H:%M:%S %Y"
#define QSMOD 4
#define QOS_DELIM ";"
// Apache 2.4 compat
#if (AP_SERVER_MINORVERSION_NUMBER == 4)
#define QS_APACHE_24 1
#if (AP_SERVER_PATCHLEVEL_NUMBER > 17)
#define QS_CONN_REMOTEIP(c) c->master ? c->master->client_ip : c->client_ip
#define QS_CONN_REMOTEADDR(c) c->master ? c->master->client_addr : c->client_addr
#define QS_CONN_MASTER(c) (c->master ? c->master : c)
#else
#define QS_CONN_REMOTEIP(c) c->client_ip
#define QS_CONN_REMOTEADDR(c) c->client_addr
#define QS_CONN_MASTER(c) (c)
#endif
#define QOS_MY_GENERATION(g) ap_mpm_query(AP_MPMQ_GENERATION, &g)
#define qos_unixd_set_global_mutex_perms ap_unixd_set_global_mutex_perms
#define QS_ISDEBUG(s) APLOG_IS_LEVEL(s, APLOG_DEBUG)
#else
#define QS_CONN_REMOTEIP(c) c->remote_ip
#define QS_CONN_REMOTEADDR(c) c->remote_addr
#define QS_CONN_MASTER(c) (c)
#define QOS_MY_GENERATION(g) g = ap_my_generation
#define qos_unixd_set_global_mutex_perms unixd_set_global_mutex_perms
#define QS_ISDEBUG(s) s->loglevel >= APLOG_DEBUG
#endif
#ifdef QS_MOD_EXT_HOOKS
APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(qos, QOS, apr_status_t, path_decode_hook,
(request_rec *r, char **path, int *len),
(r, path, len),
OK, DECLINED)
APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(qos, QOS, apr_status_t, query_decode_hook,
(request_rec *r, char **query, int *len),
(r, query, len),
OK, DECLINED)
#endif
/************************************************************************
* structures
***********************************************************************/
typedef struct {
const char *name; /* variable name */
ap_regex_t *preg;
const char *url; /* redirect url */
int code;
} qos_redirectif_entry_t;
typedef struct {
unsigned long start;
unsigned long end;
char country[3];
} qos_geo_entry_t;
typedef struct {
qos_geo_entry_t *data; // fist element
int size; // number of elements
const char *path;
} qos_geo_t;
typedef struct {
const char *url;
const char *path;
} qos_errelt_t;
static const qos_errelt_t m_error_pages[] = {
{ "/errorpages/server_error.html", "work/errorpages/server_error.html" },
{ "/errorpages/forbidden.html", "work/errorpages/forbidden.html" },
{ "/errorpages/500.html", "work/errorpages/500.html" },
{ "/errorpages/error.html", "work/errorpages/error.html" },
{ "/errorpages/error500.html", "work/errorpages/error500.html" },
{ "/errorpages/gateway_error.html", "work/errorpages/gateway_error.html" },
{ NULL, NULL }
};
typedef struct {
int id;
const char *name;
} qos_dscp_t;
static const qos_dscp_t m_dscp_desc[] = {
{ 0, "none" },
{ 8, "class selector 1" },
{ 10, "assured forwarding 11" },
{ 12, "assured forwarding 12" },
{ 14, "assured forwarding 13" },
{ 16, "class selector 2" },
{ 18, "assured forwarding 21" },
{ 20, "assured forwarding 22" },
{ 22, "assured forwarding 23" },
{ 24, "class selector 3" },
{ 26, "assured forwarding 31" },
{ 28, "assured forwarding 32" },
{ 30, "assured forwarding 33" },
{ 32, "class selector 4" },
{ 34, "assured forwarding 41" },
{ 36, "assured forwarding 42" },
{ 38, "assured forwarding 43" },
{ 40, "class selector 5" },
{ 44, "voice admit" },
{ 46, "expedited forwarding" },
{ 48, "class selector 6" },
{ 56, "class selector 7" },
{ -1, "unknown" }
};
typedef struct {
unsigned short int limit;
time_t limitTime;
} qos_s_entry_limit_t;
typedef struct {
unsigned short int limit;
time_t limitTime;
const char *eventClearStr; // name of the var clearing the counter
const char *eventDecStr; // name of the var decrementing the counter
const char *condStr;
ap_regex_t *preg;
} qos_s_entry_limit_conf_t;
typedef struct {
apr_uint64_t ip6[2];
time_t lowrate;
unsigned int lowratestatus;
/* behavior */
unsigned int html;
unsigned int cssjs;
unsigned int img;
unsigned int other;
unsigned int notmodified;
unsigned int events;
/* serialization flag */
unsigned int serialize;
apr_time_t serializeQueue;
/* prefer */
short int vip;
/* ev block */
unsigned short int block;
short int blockMsg;
time_t time;
time_t blockTime;
qos_s_entry_limit_t *limit;
/* ev/sec */
time_t interval;
long req;
long req_per_sec;
int req_per_sec_block_rate;
int event_req;
} qos_s_entry_t;
typedef struct {
time_t t;
/* index */
qos_s_entry_t **ipd;
qos_s_entry_t **timed;
/* shm */
apr_shm_t *m;
char *lock_file;
apr_global_mutex_t *lock;
/* size */
int num;
int max;
int msize;
/* limit table settings */
apr_table_t *limitTable;
/* av. behavior */
unsigned long long html;
unsigned long long cssjs;
unsigned long long img;
unsigned long long other;
unsigned long long notmodified;
/* data */
int connections;
/* remember for which clients this rec has created (shared mem) */
int generation_locked; // indicates the the counters have been cleared for this generation
/* log counter */
unsigned long long eventTotal[QOS_LOG_MSGCT]; // total number of events since initial start
unsigned long long eventLast[QOS_LOG_MSGCT]; // number of events since last read
} qos_s_t;
typedef enum {
QS_IP_V6_DEFAULT = 0,
QS_IP_V6,
QS_IP_V4
} qs_ip_type_e;
typedef enum {
QS_CONN_STATE_NEW = 0,
QS_CONN_STATE_HEAD,
QS_CONN_STATE_BODY,
QS_CONN_STATE_CHUNKED,
QS_CONN_STATE_KEEP,
QS_CONN_STATE_RESPONSE,
QS_CONN_STATE_END,
QS_CONN_STATE_DESTROY
} qs_conn_state_e;
typedef enum {
QS_HEADERFILTER_OFF_DEFAULT = 0,
QS_HEADERFILTER_OFF,
QS_HEADERFILTER_ON,
QS_HEADERFILTER_SIZE_ONLY,
QS_HEADERFILTER_SILENT
} qs_headerfilter_mode_e;
typedef enum {
QS_FLT_ACTION_DROP,
QS_FLT_ACTION_DENY
} qs_flt_action_e;
typedef enum {
QS_EVENT_ACTION_DENY = 0
} qs_event_action_e;
typedef enum {
QS_DENY_REQUEST_LINE,
QS_DENY_PATH,
QS_DENY_QUERY,
QS_DENY_EVENT,
QS_PERMIT_URI
} qs_rfilter_type_e;
typedef enum {
QS_LOG = 0,
QS_DENY,
QS_OFF_DEFAULT,
QS_OFF
} qs_rfilter_action_e;
enum qos_cmp {
QS_CMP_EQ,
QS_CMP_NE,
QS_CMP_GT,
QS_CMP_LT
};
typedef struct {
enum qos_cmp cmp;
const char *left;
const char *right;
const char *variable;
const char *value;
} qos_cmp_entry_t;
typedef struct {
char *variable1;
char *variable2;
ap_regex_t *preg;
char *name;
char *value;
} qos_setenvif_t;
typedef struct {
ap_regex_t *preg;
char *name;
char *value;
} qos_setenvifquery_t;
typedef struct {
ap_regex_t *pregx;
char *name;
char *value;
} qos_setenvifparpbody_t;
/**
* generic request filter
*/
typedef struct {
ap_regex_t *preg;
char *text;
char *id;
qs_rfilter_type_e type;
qs_rfilter_action_e action;
} qos_rfilter_t;
/**
* list of in_filter ctx
*/
typedef struct {
apr_table_t *table;
#if APR_HAS_THREADS
apr_thread_mutex_t *lock;
apr_thread_t *thread;
#endif
int exit;
} qos_ifctx_list_t;
/**
* ip entry
*/
typedef struct qs_ip_entry_st {
apr_uint64_t ip6[2];
int counter;
int error;
} qs_ip_entry_t;
typedef struct {
qs_ip_entry_t *conn_ip;
int conn_ip_len;
int connections;
int max_client;
} qs_conn_t;
typedef struct {
apr_time_t q1; // first request in queue
apr_time_t q2; // second request in queue
int locked;
} qs_serial_t;
/**
* session cookie
*/
typedef struct {
time_t time;
} qos_session_t;
/**
* cfg/act entry for event limitation
*/
typedef struct {
const char *env_var;// configured environment variable name
const char *eventDecStr;
int max; // configured max. num
int seconds; // configured duration
int limit; // event counter
time_t limitTime; // timer
qs_event_action_e action;
const char *condStr;
ap_regex_t *preg;
} qos_event_limit_entry_t;
/**
* access control table entry
*/
typedef struct qs_acentry_st {
int id;
/** pointer to lock of the actable */
apr_global_mutex_t *lock;
/** location rules */
char *url;
int url_len;
char *event;
ap_regex_t *regex;
ap_regex_t *regex_var;
ap_regex_t *condition;
int counter;
int limit;
/* measurement */
apr_time_t interval;
long req;
long req_per_sec;
long req_per_sec_limit;
int req_per_sec_block_rate;
long bytes; // transferred bytes
apr_time_t kbytes_interval_us; // elapsed time
apr_off_t kbytes_per_sec; // actual kbytes/sec (measured)
apr_off_t kbytes_per_sec_limit; // configured limitation
apr_off_t kbytes_per_sec_block_rate; // current wait time
struct qs_acentry_st *next;
} qs_acentry_t;
/**
* access control table (act)
*/
typedef struct qs_actable_st {
apr_size_t size;
apr_shm_t *m;
apr_pool_t *pool;
/** process pool is used to create user space data */
apr_pool_t *ppool;
/** rule entry list */
qs_acentry_t *entry; /* shm pointer */
int has_events;
/** event limit list */
qos_event_limit_entry_t *event_entry;
/** mutex */
char *lock_file;
apr_global_mutex_t *lock;
/** ip/conn data */
qs_conn_t *conn; /* shm pointer */
unsigned int timeout;
/* settings */
int child_init;
/* serialize */
qs_serial_t *serialize; /* shm pointer */
time_t *qsstatustimer; /* shm pointer */
} qs_actable_t;
/**
* network table (total connections, vip connections, first update, last update)
*/
typedef struct qs_netstat_st {
// int counter;
int vip;
// time_t first;
// time_t last;
} qs_netstat_t;
/**
* user space
*/
typedef struct {
int server_start;
apr_table_t *act_table;
/* client control */
qos_s_t *qos_cc;
} qos_user_t;
/**
* directory config
*/
typedef struct {
char *path;
apr_table_t *rfilter_table;
int inheritoff;
qs_headerfilter_mode_e headerfilter;
qs_headerfilter_mode_e resheaderfilter;
int bodyfilter_d;
int bodyfilter_p;
int dec_mode;
apr_off_t maxpost;
qs_rfilter_action_e urldecoding;
const char *response_pattern;
int response_pattern_len;
const char *response_pattern_var;
apr_array_header_t *redirectif;
int decodings;
apr_table_t *disable_reqrate_events;
apr_table_t *setenvstatus_t;
apr_array_header_t *setenvif_t;
apr_table_t *setenvifquery_t;
apr_array_header_t* setenvcmp;
} qos_dir_config;
/**
* server configuration
*/
typedef struct {
apr_pool_t *pool;
int is_virtual;
server_rec *base_server;
char *mfile;
qs_actable_t *act;
const char *error_page;
apr_table_t *location_t;
apr_table_t *setenv_t;
apr_table_t *setreqheader_t;
apr_table_t *setreqheaderlate_t;
apr_table_t *unsetresheader_t;
apr_table_t *unsetreqheader_t;
apr_array_header_t *setenvif_t;
apr_table_t *setenvifquery_t;
apr_table_t *setenvifparp_t;
apr_table_t *setenvifparpbody_t;
apr_table_t *setenvstatus_t;
apr_table_t *setenvresheader_t;
apr_table_t *setenvresheadermatch_t;
apr_table_t *setenvres_t;
qs_headerfilter_mode_e headerfilter;
qs_headerfilter_mode_e resheaderfilter;
apr_array_header_t *redirectif;
char *cookie_name;
char *cookie_path;
char *user_tracking_cookie;
char *user_tracking_cookie_force;
int user_tracking_cookie_session;
int user_tracking_cookie_jsredirect;
char *user_tracking_cookie_domain;
int max_age;
unsigned char key[EVP_MAX_KEY_LENGTH];
const unsigned char *rawKey;
int rawKeyLen;
int keyset;
char *header_name;
int header_name_drop;
ap_regex_t *header_name_regex;
apr_table_t *disable_reqrate_events;
char *ip_header_name;
int ip_header_name_drop;
ap_regex_t *ip_header_name_regex;
int vip_user;
int vip_ip_user;
int has_conn_counter;
int max_conn;
int max_conn_close;
int max_conn_close_percent;
int max_conn_per_ip;
int max_conn_per_ip_connections;
int max_conn_per_ip_ignore_vip;
int serialize;
int serializeTMO;
apr_table_t *exclude_ip;
qos_ifctx_list_t *inctx_t;
apr_table_t *hfilter_table; /* GLOBAL ONLY */
apr_table_t *reshfilter_table; /* GLOBAL ONLY */
/* event rule (enables rule validation) */
int has_event_filter;
int has_event_limit;
apr_array_header_t *event_limit_a;
/* min data rate */
int req_rate; /* GLOBAL ONLY */
int req_rate_start; /* GLOBAL ONLY */
int min_rate; /* GLOBAL ONLY */
int min_rate_max; /* GLOBAL ONLY */
int min_rate_off;
int req_ignore_vip_rate; /* GLOBAL ONLY */
int max_clients;
int max_clients_conf;
#ifdef QS_INTERNAL_TEST
apr_table_t *testip;
int enable_testip;
#endif
int disable_handler;
int log_only; /* GLOBAL ONLY */
int log_env;
/* client control */
int has_qos_cc; /* GLOBAL ONLY */
int qos_cc_size; /* GLOBAL ONLY */
int qos_cc_prefer; /* GLOBAL ONLY */
apr_table_t *cc_exclude_ip; /* GLOBAL ONLY */
int qos_cc_prefer_limit;
int qos_cc_event; /* GLOBAL ONLY */
int qos_cc_event_req; /* GLOBAL ONLY */
int qos_cc_block; /* GLOBAL ONLY */
int qos_cc_blockTime; /* GLOBAL ONLY */
apr_table_t *qos_cc_limitTable; /* GLOBAL ONLY */
char *qos_cc_forwardedfor; /* GLOBAL ONLY */
int qos_cc_serialize; /* GLOBAL ONLY */
apr_off_t maxpost;
int cc_tolerance; /* GLOBAL ONLY */
int cc_tolerance_max; /* GLOBAL ONLY */
int cc_tolerance_min; /* GLOBAL ONLY */
int qs_req_rate_tm; /* GLOBAL ONLY */
qos_geo_t *geodb; /* GLOBAL ONLY */
int geo_limit; /* GLOBAL ONLY */
apr_table_t *geo_priv; /* GLOBAL ONLY */
int geo_excludeUnknown; /* GLOBAL ONLY */
qs_ip_type_e ip_type; /* GLOBAL ONLY */
int qsstatus; /* GLOBAL ONLY */
int qsevents; /* GLOBAL ONLY */
apr_array_header_t *milestones;
time_t milestoneTimeout;
/* predefined client behavior */
int static_on;
unsigned long long static_html;
unsigned long long static_cssjs;
unsigned long long static_img;
unsigned long long static_other;
unsigned long long static_notmodified;
apr_file_t *qslog_p; /* GLOBAL ONLY */
const char *qslog_str; /* GLOBAL ONLY */
} qos_srv_config;
#if APR_HAS_THREADS
typedef struct {
apr_thread_t *thread;
int exit;
int maxclients;
time_t *qsstatustimer; /* shm in act */
apr_global_mutex_t *lock; /* lock of act */
apr_pool_t *pool;
qos_srv_config *sconf;
} qsstatus_t;
#endif
/**
* in_filter ctx
*/
typedef struct {
apr_socket_t *clientSocket;
qs_conn_state_e status;
apr_off_t cl_val;
conn_rec *c;
request_rec *r;
/* upload bandwidth (received bytes and start time) */
time_t time;
apr_size_t nbytes; // measuring the bytes/sec
int hasBytes;
int shutdown;
int errors;
int disabled;
int lowrate;
char *id;
qos_srv_config *sconf;
} qos_ifctx_t;
/**
* connection configuration
*/
typedef struct {
apr_uint64_t ip6[2];
conn_rec *mc; // master (real) connection
char *evmsg;
qos_srv_config *sconf;
int is_vip; /* is vip, either by request or by session or by ip */
int set_vip_by_header; /* received vip header from application/or auth. user (propagate to IP store)*/
int has_lowrate;
qs_conn_t *conn;
} qs_conn_ctx;
typedef struct {
qs_conn_ctx *cconf;
conn_rec *c;
qos_srv_config *sconf;
int requests; // number of requests processed (received) by this connection
apr_socket_t *clientSocket;
} qs_conn_base_ctx;
/**
* request configuration
*/
typedef struct {
qs_acentry_t *entry;
qs_acentry_t *entry_cond;
apr_table_t *event_entries;
char *evmsg;
int is_vip;
apr_off_t maxpostcount;
int cc_event_req_set;
apr_uint64_t cc_event_ip[2];
int cc_serialize_set;
apr_uint64_t cc_serialize_ip[2];
int srv_serialize_set;
char *body_window;
apr_off_t response_delayed; // indicates, if the response has been delayed (T)
} qs_req_ctx;
/**
* Delay filter context
*/
typedef struct {
qs_acentry_t *entry;
qs_req_ctx *rctx;
} qos_delay_ctx_t;
/**
* rule set
*/
typedef struct {
char *url;
char *event;
int limit;
ap_regex_t *regex;
ap_regex_t *regex_var;
ap_regex_t *condition;
long req_per_sec_limit;
apr_off_t kbytes_per_sec_limit;
} qs_rule_ctx_t;
typedef struct {
const char* name;
const char* pattern;
qs_flt_action_e action;
int size;
} qos_her_t;
typedef struct {
ap_regex_t *preg;
char *name;
char *value;
} qos_pregval_t;
typedef struct {
int num;
int thinktime;
const char* pattern;
ap_regex_t *preg;
qs_rfilter_action_e action;
} qos_milestone_t;
typedef struct {
char *text;
ap_regex_t *preg;
qs_flt_action_e action;
int size;
} qos_fhlt_r_t;
typedef struct {
apr_time_t request_time;
unsigned int in_addr;
unsigned int conn;
#if APR_HAS_THREADS
apr_os_thread_t tid;
#endif
unsigned int unique_id_counter;
} qos_unique_id_t;
/************************************************************************
* Globals
***********************************************************************/
module AP_MODULE_DECLARE_DATA qos_module;
static int m_forced_close = 1;
static int m_retcode = HTTP_INTERNAL_SERVER_ERROR;
static int m_threaded_mpm = 1; // note: mod_qos is fully tested for Apache 2.2 worker MPM only
static int m_event_mpm = 0;
static double m_event_mpm_worker_factor = 2;
static unsigned int m_hostcode = 0;
static int m_generation = 0; // parent process (restart generation)
static int m_qos_cc_partition = QSMOD;
static qos_unique_id_t m_unique_id;
static const char qos_basis_64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
#ifdef QS_INTERNAL_TEST
static int m_qs_sim_ip_len = QS_SIM_IP_LEN;
#endif
#ifdef QS_APACHE_24
APLOG_USE_MODULE(qos);
#endif
/* mod_parp, forward and optional function */
static apr_status_t qos_cleanup_conn(void *p);
static apr_status_t qos_base_cleanup_conn(void *p);
static qs_ip_type_e m_ip_type = QS_IP_V6_DEFAULT;
APR_DECLARE_OPTIONAL_FN(apr_table_t *, parp_hp_table, (request_rec *));
APR_DECLARE_OPTIONAL_FN(char *, parp_body_data, (request_rec *, apr_size_t *));
static APR_OPTIONAL_FN_TYPE(parp_hp_table) *qos_parp_hp_table_fn = NULL;
static APR_OPTIONAL_FN_TYPE(parp_body_data) *qos_parp_body_data_fn = NULL;
static int m_requires_parp = 0;
static int m_enable_audit = 0;
/* mod_ssl, forward and optional function */
APR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *));
static APR_OPTIONAL_FN_TYPE(ssl_is_https) *qos_is_https = NULL;
APR_DECLARE_OPTIONAL_FN(char *, ssl_var_lookup, (apr_pool_t *, server_rec *, conn_rec *, request_rec *, char *));
static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *qos_ssl_var = NULL;
static void qs_inc_eventcounter(apr_pool_t *ppool, int event, int locked);
#define QS_INC_EVENT(sconf, event) if(sconf->qsevents) qs_inc_eventcounter(sconf->act->ppool, event, 0)
#define QS_INC_EVENT_LOCKED(sconf, event) if(sconf->qsevents) qs_inc_eventcounter(sconf->act->ppool, event, 1)
static int m_knownEvents[] = { 10, 11, 12, 13, 21, 23, 25, 30, 31, 34, 35, 36, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 60, 61, 62, 65, 66, 67, 68, 69, 101, 147, 0 };
/* simple header rules allowing "the usual" header formats only (even drop requests using
extensions which are used rarely) */
/* reserved (to be escaped): {}[]()^$.|*+?\ */
static const qos_her_t qs_header_rules[] = {
#define QS_URL_UNRESERVED "a-zA-Z0-9._~% -"
#define QS_URL_GEN ":/?#\\[\\]@"
#define QS_URL_SUB "!$&'()*+,;="
#define QS_URL "["QS_URL_GEN""QS_URL_SUB""QS_URL_UNRESERVED"]"
#define QS_2616TOKEN "[\\x21\\x23-\\x27\\x2a-\\x2e0-9A-Z\\x5-\\x60a-z\\x7e]+"
#define QS_B64_SP "[a-zA-Z0-9 +/$=:]"
#define QS_PIPE "\\|"
#define QS_WEAK "(W/)?"
#define QS_H_ACCEPT "[a-zA-Z0-9_*+-]+/[a-zA-Z0-9_*+.-]+(;[ ]?[a-zA-Z0-9]+=[0-9]+)?[ ]?(;[ ]?[qv]=[a-z0-9.]+)?"
#define QS_H_ACCEPT_C "[a-zA-Z0-9*-]+(;[ ]?q=[0-9.]+)?"
#define QS_H_ACCEPT_E "[a-zA-Z0-9*-]+(;[ ]?q=[0-9.]+)?"
#define QS_H_ACCEPT_L "[a-zA-Z*-]+[0-9]{0,3}(;[ ]?q=[0-9.]+)?"
#define QS_H_CACHE "no-cache|no-store|max-age=[0-9]+|max-stale(=[0-9]+)?|min-fresh=[0-9]+|no-transform|only-if-chached"
#define QS_H_CONTENT "[\"a-zA-Z0-9*/; =-]+"
#define QS_H_COOKIE "["QS_URL_GEN""QS_URL_SUB"\""QS_URL_UNRESERVED"]"
#define QS_H_EXPECT "[a-zA-Z0-9= ;.,-]"
#define QS_H_PRAGMA "[a-zA-Z0-9= ;.,-]"
#define QS_H_FROM "[a-zA-Z0-9=@;.,()-]"
#define QS_H_HOST "[a-zA-Z0-9.-]+(:[0-9]+)?"
#define QS_H_IFMATCH "[a-zA-Z0-9=@;.,*\"-]"
#define QS_H_DATE "[a-zA-Z0-9 :,]"
#define QS_H_TE "[a-zA-Z0-9*-]+(;[ ]?q=[0-9.]+)?"
{ "Accept", "^("QS_H_ACCEPT"){1}([ ]?,[ ]?("QS_H_ACCEPT"))*$", QS_FLT_ACTION_DROP, 300 },
{ "Accept-Charset", "^("QS_H_ACCEPT_C"){1}([ ]?,[ ]?("QS_H_ACCEPT_C"))*$", QS_FLT_ACTION_DROP, 300 },
{ "Accept-Encoding", "^("QS_H_ACCEPT_E"){1}([ ]?,[ ]?("QS_H_ACCEPT_E"))*$", QS_FLT_ACTION_DROP, 500 },
{ "Accept-Language", "^("QS_H_ACCEPT_L"){1}([ ]?,[ ]?("QS_H_ACCEPT_L"))*$", QS_FLT_ACTION_DROP, 200 },
{ "Access-Control-Request-Method", "^[a-zA-Z]+$", QS_FLT_ACTION_DROP, 10 },
{ "Access-Control-Request-Headers", "^([a-zA-Z0-9-]+){1}([ ]?,[ ]?([a-zA-Z0-9-]+))*$", QS_FLT_ACTION_DROP, 500 },
{ "Authorization", "^"QS_B64_SP"+$", QS_FLT_ACTION_DROP, 4000 },
{ "Cache-Control", "^("QS_H_CACHE"){1}([ ]?,[ ]?("QS_H_CACHE"))*$", QS_FLT_ACTION_DROP, 100 },
{ "Connection", "^([teTE]+,[ ]?)?([a-zA-Z0-9-]+){1}([ ]?,[ ]?([teTE]+))?$", QS_FLT_ACTION_DROP, 100 },
{ "Content-Encoding", "^[a-zA-Z0-9-]+(,[ ]*[a-zA-Z0-9-]+)*$", QS_FLT_ACTION_DENY, 100 },
{ "Content-Language", "^([0-9a-zA-Z]{0,8}(-[0-9a-zA-Z]{0,8})*)(,[ ]*([0-9a-zA-Z]{0,8}(-[0-9a-zA-Z]{0,8})*))*$", QS_FLT_ACTION_DROP, 100 },
{ "Content-Length", "^[0-9]+$", QS_FLT_ACTION_DENY, 10 },
{ "Content-Location", "^"QS_URL"+$", QS_FLT_ACTION_DENY, 200 },
{ "Content-md5", "^"QS_B64_SP"+$", QS_FLT_ACTION_DENY, 50 },
{ "Content-Range", "^(bytes[ ]+([0-9]+-[0-9]+)/([0-9]+|\\*))$", QS_FLT_ACTION_DENY, 50 },
{ "Content-Type", "^("QS_H_CONTENT"){1}([ ]?,[ ]?("QS_H_CONTENT"))*$", QS_FLT_ACTION_DENY, 200 },
{ "Cookie", "^"QS_H_COOKIE"+$", QS_FLT_ACTION_DROP, 3000 },
{ "Cookie2", "^"QS_H_COOKIE"+$", QS_FLT_ACTION_DROP, 3000 },
{ "DNT", "^[0-9]+$", QS_FLT_ACTION_DROP, 3 },
{ "Expect", "^"QS_H_EXPECT"+$", QS_FLT_ACTION_DROP, 200 },
{ "From", "^"QS_H_FROM"+$", QS_FLT_ACTION_DROP, 100 },
{ "Host", "^"QS_H_HOST"$", QS_FLT_ACTION_DROP, 100 },
{ "If-Invalid", "^[a-zA-Z0-9_.:;() /+!-]+$", QS_FLT_ACTION_DROP, 500 },
{ "If-Match", "^"QS_WEAK""QS_H_IFMATCH"+$", QS_FLT_ACTION_DROP, 100 },
{ "If-Modified-Since", "^"QS_H_DATE"+$", QS_FLT_ACTION_DROP, 100 },
{ "If-None-Match", "^"QS_WEAK""QS_H_IFMATCH"+$", QS_FLT_ACTION_DROP, 100 },
{ "If-Range", "^"QS_H_IFMATCH"+$", QS_FLT_ACTION_DROP, 100 },
{ "If-Unmodified-Since", "^"QS_H_DATE"+$", QS_FLT_ACTION_DROP, 100 },
{ "If-Valid", "^[a-zA-Z0-9_.:;() /+!-]+$", QS_FLT_ACTION_DROP, 500 },
{ "Keep-Alive", "^[0-9]+$", QS_FLT_ACTION_DROP, 20 },
{ "Max-Forwards", "^[0-9]+$", QS_FLT_ACTION_DROP, 20 },
{ "Origin", "^"QS_URL"+$", QS_FLT_ACTION_DROP, 2000 },
{ "Proxy-Authorization", "^"QS_B64_SP"+$", QS_FLT_ACTION_DROP, 400 },
{ "Pragma", "^"QS_H_PRAGMA"+$", QS_FLT_ACTION_DROP, 200 },
{ "Range", "^[a-zA-Z0-9=_.:;() /+!-]+$", QS_FLT_ACTION_DROP, 200 },
{ "Referer", "^"QS_URL"+$", QS_FLT_ACTION_DROP, 2000 },
{ "TE", "^("QS_H_TE"){1}([ ]?,[ ]?("QS_H_TE"))*$", QS_FLT_ACTION_DROP, 100 },
{ "Transfer-Encoding", "^(chunked|Chunked|compress|Compress|deflate|Deflate|gzip|Gzip|identity|Identity)([ ]?,[ ]?(chunked|Chunked|compress|Compress|deflate|Deflate|gzip|Gzip|identity|Identity))*$", QS_FLT_ACTION_DENY, 100 },
{ "Unless-Modified-Since", "^"QS_H_DATE"+$", QS_FLT_ACTION_DROP, 100 },
{ "User-Agent", "^[a-zA-Z0-9]+[a-zA-Z0-9_.:;()\\[\\]@ /+!=,-]+$", QS_FLT_ACTION_DROP, 300 },
{ "Upgrade-Insecure-Requests", "^1$", QS_FLT_ACTION_DROP, 1 },
{ "Via", "^[a-zA-Z0-9_.:;() /+!-]+$", QS_FLT_ACTION_DROP, 100 },
{ "X-Forwarded-For", "^[a-zA-Z0-9_.:-]+(, [a-zA-Z0-9_.:-]+)*$", QS_FLT_ACTION_DROP, 100 },
{ "X-Forwarded-Host", "^[a-zA-Z0-9_.:-]+$", QS_FLT_ACTION_DROP, 100 },
{ "X-Forwarded-Server", "^[a-zA-Z0-9_.:-]+$", QS_FLT_ACTION_DROP, 100 },
{ "X-lori-time-1", "^[0-9]+$", QS_FLT_ACTION_DROP, 20 },
{ "X-Do-Not-Track", "^[0-9]+$", QS_FLT_ACTION_DROP, 20 },
{ NULL, NULL, 0, 0 }
};
/* list of allowed standard response headers */
static const qos_her_t qs_res_header_rules[] = {
{ "Age", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Accept-Ranges", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Access-Control-Allow-Origin", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Access-Control-Allow-Methods", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Access-Control-Allow-Headers", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Access-Control-Max-Age", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Access-Control-Allow-Credentials", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Access-Control-Expose-Headers", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Allow", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Cache-Control", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Content-Disposition", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Content-Encoding", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Content-Language", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Content-Length", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Content-Location", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Content-MD5", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Content-Range", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Content-Security-Policy", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 8000 },
{ "Content-Type", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Connection", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Date", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "ETag", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Expect", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Expires", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Keep-Alive", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Last-Modified", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Location", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Proxy-Authenticate", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Public-Key-Pins", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Public-Key-Pins-Report-Only", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Retry-After", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Pragma", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Server", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Set-Cookie", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Set-Cookie2", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Strict-Transport-Security", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "Vary", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "WWW-Authenticate", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "X-Content-Security-Policy", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 8000 },
{ "X-Content-Type-Options", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "X-Frame-Options", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ "X-XSS-Protection", "^[\\x20-\\xFF]*$", QS_FLT_ACTION_DROP, 4000 },
{ NULL, NULL, 0, 0 }
};
/************************************************************************
* private functions
***********************************************************************/
static int qos_regexec_len(apr_pool_t *pool, const ap_regex_t *preg,
const char *buff, apr_size_t len) {
#if (AP_SERVER_MINORVERSION_NUMBER < 4) && defined QS_INTERNAL_TEST
// Apache 2.2 is no longer supported (test only)
char *data = apr_palloc(pool, len + 1);
memcpy(data, buff, len);
data[len] = '\0';
return ap_regexec(preg, data, 0, NULL, 0); // won't work for null chars!
#else
return ap_regexec_len(preg, buff, len, 0, NULL, 0);
#endif
}
/**
* Converts an ip long array back to a string representation
*
* @param pool
* @param src Array of two unsigned long
* @return String or null for an invalid address
*/
static char *qos_ip_long2str(apr_pool_t *pool, const void *src) {
char *dst = apr_pcalloc(pool, INET6_ADDRSTRLEN);
char *ret = (char *)inet_ntop(AF_INET6, src, dst, INET6_ADDRSTRLEN);
if(ret) {
if((strncmp(ret, QS_IP4IN6, 7) == 0) &&
strchr(ret, '.')) {
ret = &ret[7];
}
}
return ret;
}
/**
* Converts an ip string to long array (128 bit) representation
*
* @param src String representation, e.g. 139.12.33.1 or 1::8
* @param dst Pointer to array of unsigned long (2) (contains "{ 0, 0 }" on error)
* @return 1 on success, 0 on error
*/
static int qos_ip_str2long(const char *src, apr_uint64_t *dst) {
char str[INET6_ADDRSTRLEN];
const char *convert = src;
apr_uint64_t *n = dst;
n[0] = 0;
n[1] = 0;
if(convert == NULL) {
return 0;
}
if((strchr(convert, ':') == NULL) &&
(strlen(convert) <= 15)) {
// looks like an IPv4 address
sprintf(str, QS_IP4IN6"%s", src);
convert = str;
}
return inet_pton(AF_INET6, convert, dst);
}
static int qos_encode64_binary(char *encoded, const char *string, int len) {
int i;
char *p;
p = encoded;
for (i = 0; i < len - 2; i += 3) {
*p++ = qos_basis_64[(string[i] >> 2) & 0x3F];
*p++ = qos_basis_64[((string[i] & 0x3) << 4) |
((int) (string[i + 1] & 0xF0) >> 4)];
*p++ = qos_basis_64[((string[i + 1] & 0xF) << 2) |
((int) (string[i + 2] & 0xC0) >> 6)];
*p++ = qos_basis_64[string[i + 2] & 0x3F];
}
if (i < len) {
*p++ = qos_basis_64[(string[i] >> 2) & 0x3F];
if (i == (len - 1)) {
*p++ = qos_basis_64[((string[i] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = qos_basis_64[((string[i] & 0x3) << 4) |
((int) (string[i + 1] & 0xF0) >> 4)];
*p++ = qos_basis_64[((string[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}
*p++ = '\0';
return (int)(p - encoded);
}
/**
* loads the default header rules into the server configuration (see rules
* above).
* @param pool To allocate memory
* @param outHdrFltTable Table to add rules to
* @param hdrFltRuleDefArray built-in header rules
* @return error message (NULL on success)
*/
static char *qos_load_headerfilter(apr_pool_t *pool, apr_table_t *outHdrFltTable,
const qos_her_t *hdrFltRuleDefArray) {
const qos_her_t* hdrFltRuleDefEntry;
for(hdrFltRuleDefEntry = hdrFltRuleDefArray; hdrFltRuleDefEntry->name != NULL ; ++hdrFltRuleDefEntry) {
qos_fhlt_r_t *hdrFltElement = apr_pcalloc(pool, sizeof(qos_fhlt_r_t));
hdrFltElement->text = apr_pstrdup(pool, hdrFltRuleDefEntry->pattern);
hdrFltElement->preg = ap_pregcomp(pool, hdrFltRuleDefEntry->pattern, AP_REG_DOTALL);
hdrFltElement->action = hdrFltRuleDefEntry->action;
hdrFltElement->size = hdrFltRuleDefEntry->size;
if(hdrFltElement->preg == NULL) {
return apr_psprintf(pool, "could not compile regular expression '%s' for %s header",
hdrFltElement->text, hdrFltRuleDefEntry->name);
}
apr_table_setn(outHdrFltTable, hdrFltRuleDefEntry->name, (char *)hdrFltElement);
}
return NULL;
}
/**
* Returns string representation of filter type (for logging purposes)
* @param pool To allocate string
* @param type Rule type
* @return Name of the directive used to configure the rule
*/
static char *qos_rfilter_type2text(apr_pool_t *pool, qs_rfilter_type_e type) {
if(type == QS_DENY_REQUEST_LINE) return apr_pstrdup(pool, "QS_DenyRequestLine");
if(type == QS_DENY_PATH) return apr_pstrdup(pool, "QS_DenyPath");
if(type == QS_DENY_QUERY) return apr_pstrdup(pool, "QS_DenyQuery");
if(type == QS_DENY_EVENT) return apr_pstrdup(pool, "QS_DenyEvent");
if(type == QS_PERMIT_URI) return apr_pstrdup(pool, "QS_PermitUri");
return apr_pstrdup(pool, "UNKNOWN");
}
/**
* Sets unique apache instance id (hopefully) to the global m_hostcore variable
* @param ptemp Pool to allocate memory from
* @param s Base server record
*/
static void qos_hostcode(apr_pool_t *ptemp, server_rec *s) {
char *key = apr_psprintf(ptemp, "%s%s%s%d%s"
#ifdef ap_http_scheme
/* Apache 2.2 */
"%s"
#endif
"%s",
s->defn_name ? s->defn_name : "",
s->server_admin ? s->server_admin : "",
s->server_hostname ? s->server_hostname : "",
s->addrs ? s->addrs->host_port : 0,
s->path ? s->path : "",
s->error_fname ? s->error_fname : ""
#ifdef ap_http_scheme
/* Apache 2.2 */
,s->server_scheme ? s->server_scheme : ""
#endif
);
int len = strlen(key);
int i;
char *p;
for(p = key, i = len; i; i--, p++) {
m_hostcode = m_hostcode * 33 + *p;
}
}
/**
* temp file name for the main/virtual serve
* @param pool Pool to allocate the file name from
* @param s Server record
* @return absolute file name
*/
static char *qos_tmpnam(apr_pool_t *pool, server_rec *s) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(s->module_config,
&qos_module);
const char *path = NULL;
char *ret;
char *file;
if(apr_temp_dir_get(&path, pool) != APR_SUCCESS) {
path = apr_pstrdup(pool, "/var/tmp");
}
if(sconf && sconf->mfile) {
path = sconf->mfile;
}
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
QOS_LOGD_PFX"temporary directory for semaphores/shared memory: %s"
" (use QS_SemMemFile to override it).", path);
if(s) {
unsigned int scode = 0;
char *key = apr_psprintf(pool, "%u%s.%s.%d",
m_hostcode,
s->is_virtual ? "v" : "b",
s->server_hostname == NULL ? "-" : s->server_hostname,
s->addrs == NULL ? 0 : s->addrs->host_port);
int len = strlen(key);
int i;
char *p;
for(p = key, i = len; i; i--, p++) {
scode = scode * 33 + *p;
}
file = apr_psprintf(pool, "%u", scode);
} else {
file = apr_psprintf(pool, "%u", m_hostcode);
}
file[0] += 25; /* non numeric */
apr_filepath_merge(&ret, path, file, APR_FILEPATH_NATIVE, pool);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
QOS_LOGD_PFX"temporary file: %s", ret);
return ret;
}
/**
* QS_LimitRequestBody settings. Environment variable (dynamic) has higher prio than
* configuration (static) value.
* @param r
* @param sconf
* @param dconf
*/
static apr_off_t qos_maxpost(request_rec *r, qos_srv_config *sconf,
qos_dir_config *dconf) {
if(r->subprocess_env) {
const char *bytes = apr_table_get(r->subprocess_env, "QS_LimitRequestBody");
if(bytes) {
apr_off_t s;
#ifdef ap_http_scheme
/* Apache 2.2 */
char *errp = NULL;
if(APR_SUCCESS == apr_strtoff(&s, bytes, &errp, 10)) {
return s;
}
#else
if((s = apr_atoi64(bytes)) >= 0) {
return s;
}
#endif
}
}
if(dconf->maxpost != -1) {
return dconf->maxpost;
}
return sconf->maxpost;
}
/**
* Similar to strstr but restricting the length of s1 (supports strings which
* are not NULL terminated).
*
* @param s1 String to search in
* @param s2 Pattern to ind
* @param len Length of s1
* @return pointer to the beginning of the substring s2 within s1, or NULL
* if the substring is not found
*/
static char *qos_strnstr(const char *s1, const char *s2, int len) {
const char *e1 = &s1[len-1];
char *p1, *p2;
if (*s2 == '\0') {
/* an empty s2 */
return((char *)s1);
}
while(1) {
for ( ; (*s1 != '\0') && (s1 <= e1) && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
if (*s1 == '\0' || s1 > e1) {
return(NULL);
}
/* found first character of s2, see if the rest matches */
p1 = (char *)s1;
p2 = (char *)s2;
for (++p1, ++p2; (apr_tolower(*p1) == apr_tolower(*p2)) && (p1 <= e1); ++p1, ++p2) {
if((p1 > e1) && (*p2 != '\0')) {
// reached the end without match
return NULL;
}
if (*p2 == '\0') {
/* both strings ended together */
return((char *)s1);
}
}
if (*p2 == '\0') {
/* second string ended, a match */
break;
}
/* didn't find a match here, try starting at next character in s1 */
s1++;
}
return((char *)s1);
}
/**
* Determines, if the client IP shall be excluded from rule enforcement
*
* @param connection Connection to get the IP
* @param exclude_ip Table containing the rules
* @return 1 on match otherwise 0
*/
static int qos_is_excluded_ip(conn_rec *connection, apr_table_t *exclude_ip) {
conn_rec *c = connection;
if(apr_table_elts(exclude_ip)->nelts > 0) {
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(exclude_ip)->elts;
for(i = 0; i < apr_table_elts(exclude_ip)->nelts; i++) {
if(entry[i].val[0] == 'r') {
if(strncmp(entry[i].key, QS_CONN_REMOTEIP(c), strlen(entry[i].key)) == 0) {
return 1;
}
} else {
if(strcmp(entry[i].key, QS_CONN_REMOTEIP(c)) == 0) {
return 1;
}
}
}
}
return 0;
}
/**
* Comperator (ip search) for the client ip store qos_cc_*()
* functions (used by bsearch/qsort)
*/
static int qos_cc_comp(const void *_pA, const void *_pB) {
qos_s_entry_t *pA=*(( qos_s_entry_t **)_pA);
qos_s_entry_t *pB=*(( qos_s_entry_t **)_pB);
if(pA->ip6[0] > pB->ip6[0]) return 2;
if(pA->ip6[0] < pB->ip6[0]) return -2;
if(pA->ip6[1] > pB->ip6[1]) return 1;
if(pA->ip6[1] < pB->ip6[1]) return -1;
return 0;
}
static int qos_cc_compv4(const void *_pA, const void *_pB) {
qos_s_entry_t *pA=*(( qos_s_entry_t **)_pA);
qos_s_entry_t *pB=*(( qos_s_entry_t **)_pB);
if(pA->ip6[1] > pB->ip6[1]) return 1;
if(pA->ip6[1] < pB->ip6[1]) return -1;
return 0;
}
/**
* Comperator (time search) for the client ip store qos_cc_*()
* functions (used by bsearch/qsort)
*/
static int qos_cc_comp_time(const void *_pA, const void *_pB) {
qos_s_entry_t *pA=*(( qos_s_entry_t **)_pA);
qos_s_entry_t *pB=*(( qos_s_entry_t **)_pB);
if(pA->time > pB->time) return 1;
if(pA->time < pB->time) return -1;
return 0;
}
/**
* creates new per client store
* @param pool Persistent process pool
* @param srec Server rec for sem/mutex
* @param size Number of entries
* @param limitTable Table of "QS_Limit" events
* @return pointer to the per client data array
*/
static qos_s_t *qos_cc_new(apr_pool_t *pool, server_rec *srec, int size,
apr_table_t *limitTable) {
char *file = "-";
apr_shm_t *clientMem; // per client memory table
apr_shm_t *limitMem; // "limit" memory table
apr_status_t res;
int limitTableSize = apr_table_elts(limitTable)->nelts;
int limitMemSize = 0;
int clientMemSize = APR_ALIGN_DEFAULT(sizeof(qos_s_t)) +
(APR_ALIGN_DEFAULT(sizeof(qos_s_entry_t)) * size) +
(2 * APR_ALIGN_DEFAULT(sizeof(qos_s_entry_t *)) * size);
int i;
qos_s_t *clientDataArray;
qos_s_entry_t *clientDataEntry;
qos_s_entry_limit_t *limitTableEntry = NULL;
clientMemSize = clientMemSize + 1024;
if(limitTableSize > 0) {
limitMemSize = APR_ALIGN_DEFAULT(sizeof(qos_s_entry_limit_t)) * limitTableSize * size;
limitMemSize = limitMemSize + 1024;
}
/* use anonymous shm by default */
if(limitTableSize > 0) {
apr_shm_create(&limitMem, limitMemSize, NULL, pool);
}
res = apr_shm_create(&clientMem, clientMemSize, NULL, pool);
if(APR_STATUS_IS_ENOTIMPL(res)) {
char *lfile = apr_psprintf(pool, "%s_cc_ml.mod_qos",
qos_tmpnam(pool, srec));
file = apr_psprintf(pool, "%s_cc_m.mod_qos",
qos_tmpnam(pool, srec));
#ifdef ap_http_scheme
/* Apache 2.2 */
if(limitTableSize > 0) {
apr_shm_remove(lfile, pool);
}
apr_shm_remove(file, pool);
#endif
if(limitTableSize > 0) {
apr_shm_create(&limitMem, limitMemSize, lfile, pool);
}
res = apr_shm_create(&clientMem, clientMemSize, file, pool);
}
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, srec,
QOS_LOGD_PFX"create shared memory (client control)(%s): %d bytes",
file, clientMemSize + limitMemSize);
if(res != APR_SUCCESS) {
char buf[MAX_STRING_LEN];
apr_strerror(res, buf, sizeof(buf));
ap_log_error(APLOG_MARK, APLOG_EMERG, 0, srec,
QOS_LOG_PFX(002)"failed to create shared memory (client control)(%s): "
"%s (%d bytes)",
file, buf, clientMemSize);
return NULL;
}
clientDataArray = apr_shm_baseaddr_get(clientMem);
clientDataArray->m = clientMem;
clientDataArray->generation_locked = -1;
if(limitTableSize > 0) {
apr_table_entry_t *te = (apr_table_entry_t *)apr_table_elts(limitTable)->elts;
limitTableEntry = apr_shm_baseaddr_get(limitMem);
clientDataArray->limitTable = apr_table_make(pool, limitTableSize+10);
for(i = 0; i < limitTableSize; i++) {
char *eventName = apr_pstrdup(pool, te[i].key);
qos_s_entry_limit_conf_t *eventLimitConf = apr_pcalloc(pool, sizeof(qos_s_entry_limit_conf_t));
qos_s_entry_limit_conf_t *src = (qos_s_entry_limit_conf_t*)te[i].val;
eventLimitConf->limit = src->limit;
eventLimitConf->limitTime = src->limitTime;
eventLimitConf->eventClearStr = apr_pstrcat(pool, eventName, QS_LIMIT_CLEAR, NULL);
eventLimitConf->eventDecStr = apr_pstrcat(pool, eventName, QS_LIMIT_DEC, NULL);
eventLimitConf->condStr = NULL;
eventLimitConf->preg = NULL;
if(src->condStr) {
eventLimitConf->condStr = apr_pstrdup(pool, src->condStr);
eventLimitConf->preg = ap_pregcomp(pool, src->condStr, AP_REG_EXTENDED);
}
apr_table_addn(clientDataArray->limitTable, eventName, (char *)eventLimitConf);
}
} else {
clientDataArray->limitTable = NULL;
}
clientDataArray->lock_file = apr_psprintf(pool, "%s_ccl.mod_qos",
qos_tmpnam(pool, srec));
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, srec,
QOS_LOGD_PFX"create mutex (client control)(%s)",
clientDataArray->lock_file);
res = apr_global_mutex_create(&clientDataArray->lock, clientDataArray->lock_file, APR_LOCK_DEFAULT, pool);
if(res != APR_SUCCESS) {
char buf[MAX_STRING_LEN];
apr_strerror(res, buf, sizeof(buf));
ap_log_error(APLOG_MARK, APLOG_EMERG, 0, srec,
QOS_LOG_PFX(004)"failed to create mutex (client control)(%s): %s",
clientDataArray->lock_file, buf);
apr_shm_destroy(clientDataArray->m);
return NULL;
}
#ifdef AP_NEED_SET_MUTEX_PERMS
qos_unixd_set_global_mutex_perms(clientDataArray->lock);
#endif
clientDataEntry = (qos_s_entry_t *)&clientDataArray[1];
clientDataArray->ipd = (qos_s_entry_t **)&clientDataEntry[size];
clientDataArray->timed = (qos_s_entry_t **)&clientDataArray->ipd[size];
clientDataArray->num = 0;
clientDataArray->max = size;
clientDataArray->msize = clientMemSize;
clientDataArray->connections = 0;
clientDataArray->html = 0;
clientDataArray->cssjs = 0;
clientDataArray->img = 0;
clientDataArray->other = 0;
clientDataArray->notmodified = 0;
for(i = 0; i < size; i++) {
clientDataArray->ipd[i] = clientDataEntry;
clientDataArray->timed[i] = clientDataEntry;
if(limitTableSize > 0) {
clientDataEntry->limit = limitTableEntry;
limitTableEntry += limitTableSize;
} else {
clientDataEntry->limit = NULL;
}
clientDataEntry++;
}
clientDataArray->t = time(NULL);
for(i = 0; i < QOS_LOG_MSGCT; i++) {
clientDataArray->eventTotal[i] = 0;
clientDataArray->eventLast[i] = 0;
}
return clientDataArray;
}
/**
* Destroys the client data store
*/
static void qos_cc_free(qos_s_t *s) {
/*
We don't need to destroy locks or shared memory
manually as apr_global_mutex_create() and
apr_shm_create() register the cleanup methods
themself to the pool we used when allocating the
locks/memory.
if(s->lock) {
apr_global_mutex_destroy(s->lock);
}
if(s->m) {
apr_shm_destroy(s->m);
}
if(s->lm) {
apr_shm_destroy(s->lm);
}
*/
}
/**
* searches an entry
* @param s Client store (locked)
* @param pA IP to search
* @param now Current time (update access to the entry)
* @return client entry or NULL if not available
*/
static qos_s_entry_t **qos_cc_get0(qos_s_t *s, qos_s_entry_t *pA, time_t now) {
qos_s_entry_t **pB;
unsigned char *b = (void *)&pA->ip6[1];
int mod = b[7] % m_qos_cc_partition;
int max = (s->max / m_qos_cc_partition);
int start = mod * max;
if(m_ip_type == QS_IP_V4) {
pB = bsearch((const void *)&pA, (const void *)&s->ipd[start],
max, sizeof(qos_s_entry_t *), qos_cc_compv4);
} else {
pB = bsearch((const void *)&pA, (const void *)&s->ipd[start],
max, sizeof(qos_s_entry_t *), qos_cc_comp);
}
if(pB) {
if(now != 0) {
s->t = now;
}
(*pB)->time = s->t;
}
return pB;
}
/**
* inserts a new entry to the client data store
* @param s Client store (locked)
* @param pA IP to insert
* @param now Current time (last access)
* @return inserted entry
*/
static qos_s_entry_t **qos_cc_set(qos_s_t *s, qos_s_entry_t *pA, time_t now) {
qos_s_entry_t **pB;
unsigned char *b = (void *)&pA->ip6[1];
int mod = b[7] % m_qos_cc_partition;
int max = (s->max / m_qos_cc_partition);
int start = mod * max;
s->t = now;
qsort(&s->timed[start], max, sizeof(qos_s_entry_t *), qos_cc_comp_time);
if(s->num < s->max) {
s->num++;
}
pB = &s->timed[start];
(*pB)->ip6[0] = pA->ip6[0];
(*pB)->ip6[1] = pA->ip6[1];
(*pB)->time = now;
if(m_ip_type == QS_IP_V4) {
qsort(&s->ipd[start], max, sizeof(qos_s_entry_t *), qos_cc_compv4);
} else {
qsort(&s->ipd[start], max, sizeof(qos_s_entry_t *), qos_cc_comp);
}
(*pB)->vip = 0;
(*pB)->lowrate = 0;
(*pB)->lowratestatus = 0;
(*pB)->block = 0;
(*pB)->blockMsg = 0;
(*pB)->blockTime = 0;
if(s->limitTable) {
int i;
for(i = 0; i < apr_table_elts(s->limitTable)->nelts; i++) {
(*pB)->limit[i].limit = 0;
(*pB)->limit[i].limitTime = 0;
}
}
(*pB)->interval = now;
(*pB)->req = 0;
(*pB)->req_per_sec = 0;
(*pB)->req_per_sec_block_rate = 0;
(*pB)->event_req = 0;
(*pB)->serialize = 0;
(*pB)->serializeQueue = 0;
(*pB)->html = 1;
(*pB)->cssjs = 1;
(*pB)->img = 1;
(*pB)->other = 1;
(*pB)->notmodified = 1;
(*pB)->events = 0;
return pB;
}
/**
* searches or inserts (if not available) an entry from/to the client data store
* @param s Client store (locked)
* @param pA IP to insert
* @param now Current time (last access)
* @return inserted entry
*/
static qos_s_entry_t **qos_cc_getOrSet(qos_s_t *s, qos_s_entry_t *pA, time_t now) {
qos_s_entry_t **clientEntry = clientEntry = qos_cc_get0(s, pA, now);
if(!clientEntry) {
clientEntry = qos_cc_set(s, pA, now != 0 ? now : time(NULL));
}
return clientEntry;
}
static int qos_isnum(const char *x) {
const char *p = x;
if(x == NULL || x[0] == 0) {
return 0;
}
while(p && p[0]) {
if(!apr_isdigit(p[0])) {
return 0;
}
p++;
}
return 1;
}
/* 000-255 */
int qos_dec32c(const char *x) {
char buf[4];
strncpy(buf, x, 3);
buf[3] = '\0';
return atoi(buf);
}
int qos_dec22c(const char *x) {
char buf[4];
strncpy(buf, x, 2);
buf[2] = '\0';
return atoi(buf);
}
/**
* hex value for the char
* @param x
* @return hex value
*/
int qos_hex2c(const char *x) {
int i, ch;
ch = x[0];
if (isdigit(ch)) {
i = ch - '0';
}else if (isupper(ch)) {
i = ch - ('A' - 10);
} else {
i = ch - ('a' - 10);
}
i <<= 4;
ch = x[1];
if (isdigit(ch)) {
i += ch - '0';
} else if (isupper(ch)) {
i += ch - ('A' - 10);
} else {
i += ch - ('a' - 10);
}
return i;
}
#define QOS_ISHEX(x) (((x >= '0') && (x <= '9')) || \
((x >= 'a') && (x <= 'f')) || \
((x >= 'A') && (x <= 'F')))
/**
* url unescaping (%xx, \xHH, '+')
* optional decoding:
* - uni: MS IIS unicode %uXXXX
* - ansi: ansi c esc (\n, \r, ...), not implemented
* - char: charset conv, not implemented
* - html: (amp/angelbr, &#xHH;, &#DDD;, &#DD;), not implemented ('&' is delimiter)
*/
static int qos_unescaping(char *x, int mode, int *error) {
/* start with standard url decoding*/
int i, j, ch;
if(x == 0) {
return 0;
}
if(x[0] == '\0') {
return 0;
}
for(i = 0, j = 0; x[i] != '\0'; i++, j++) {
ch = x[i];
if(ch == '%') {
if(QOS_ISHEX(x[i + 1]) && QOS_ISHEX(x[i + 2])) {
/* url %xx */
ch = qos_hex2c(&x[i + 1]);
i += 2;
} else if((mode & QOS_DEC_MODE_FLAGS_UNI) &&
((x[i + 1] == 'u') || (x[i + 1] == 'U')) &&
QOS_ISHEX(x[i + 2]) &&
QOS_ISHEX(x[i + 3]) &&
QOS_ISHEX(x[i + 4]) &&
QOS_ISHEX(x[i + 5])) {
/* unicode %uXXXX */
ch = qos_hex2c(&x[i + 4]);
if((ch > 0x00) && (ch < 0x5f) &&
((x[i + 2] == 'f') || (x[i + 2] == 'F')) &&
((x[i + 3] == 'f') || (x[i + 3] == 'F'))) {
ch += 0x20;
}
i += 5;
} else {
(*error)++;
}
} else if((ch == '\\') &&
(mode & QOS_DEC_MODE_FLAGS_UNI) &&
((x[i + 1] == 'u') || (x[i + 1] == 'U'))) {
if(QOS_ISHEX(x[i + 2]) &&
QOS_ISHEX(x[i + 3]) &&
QOS_ISHEX(x[i + 4]) &&
QOS_ISHEX(x[i + 5])) {
/* unicode \uXXXX */
ch = qos_hex2c(&x[i + 4]);
if((ch > 0x00) && (ch < 0x5f) &&
((x[i + 2] == 'f') || (x[i + 2] == 'F')) &&
((x[i + 3] == 'f') || (x[i + 3] == 'F'))) {
ch += 0x20;
}
i += 5;
} else {
(*error)++;
}
} else if(ch == '\\' && (x[i + 1] == 'x')) {
if(QOS_ISHEX(x[i + 2]) && QOS_ISHEX(x[i + 3])) {
/* url \xHH */
ch = qos_hex2c(&x[i + 2]);
i += 3;
} else {
(*error)++;
}
} else if(ch == '+') {
ch = ' ';
}
x[j] = ch;
}
x[j] = '\0';
return j;
}
/**
* returns the request id from mod_unique_id (if available)
*/
static const char *qos_unique_id(request_rec *r, const char *eid) {
const char *uid = apr_table_get(r->subprocess_env, "UNIQUE_ID");
if(eid) {
apr_table_set(r->notes, "error-notes", eid);
apr_table_set(r->subprocess_env, QS_ErrorNotes, eid);
}
if(uid == NULL) {
/* generate simple id if mod_unique_id has not been not loaded */
qos_unique_id_t id;
char *uidstr;
int len;
m_unique_id.unique_id_counter++;
id.request_time = r->request_time;
id.in_addr = m_unique_id.in_addr;
#if APR_HAS_THREADS
id.tid = apr_os_thread_current();
#endif
id.conn = r->connection->id;
id.unique_id_counter = m_unique_id.unique_id_counter;
uidstr = (char *)apr_pcalloc(r->pool, apr_base64_encode_len(sizeof(qos_unique_id_t)));
len = qos_encode64_binary(uidstr, (const char *)&id, sizeof(qos_unique_id_t));
uidstr[len-2] = (id.unique_id_counter%8)+50;
uid = uidstr;
apr_table_set(r->subprocess_env, "UNIQUE_ID", uid);
}
return uid;
}
static void qos_log_env(request_rec *r, const char *handler) {
char *msg = "";
int i;
apr_table_entry_t *e = (apr_table_entry_t *) apr_table_elts(r->subprocess_env)->elts;
for (i = 0; i < apr_table_elts(r->subprocess_env)->nelts; ++i) {
msg = apr_psprintf(r->pool, "%s=%s;%s", e[i].key, e[i].val, msg);
}
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r,
QOS_LOG_PFX(210)"ENV %s %s %s", handler, msg, qos_unique_id(r, NULL));
return;
}
static char *qos_ipv6_hash(request_rec *r, const char *var) {
char *md = ap_md5_binary(r->pool, (unsigned char *)var, strlen(var));
char *hash = apr_pcalloc(r->pool, 64);
char *d = hash;
int c = 0;
while(md[0]) {
d[0] = md[0];
d++;
c++;
md++;
if(c == 4 && md[0]) {
c=0;
d[0] = ':';
d++;
}
}
d[0] = '\0';
return hash;
}
static const char *qos_forwardedfor_fromHeader(request_rec *r, const char *header) {
const char *forwardedfor = apr_table_get(r->headers_in, header);
if(forwardedfor == NULL && r->prev) {
// internal redirect?
forwardedfor = apr_table_get(r->prev->headers_in, header);
}
if(forwardedfor == NULL && r->main) {
// internal redirect?
forwardedfor = apr_table_get(r->main->headers_in, header);
}
return forwardedfor;
}
static const char *qos_forwardedfor_fromSSL(request_rec *r) {
if(qos_ssl_var) {
const char *dn = qos_ssl_var(r->pool, r->server, r->connection, r, "SSL_CLIENT_S_DN");
const char *issuer = qos_ssl_var(r->pool, r->server, r->connection, r, "SSL_CLIENT_I_DN");
char *header = apr_pstrcat(r->pool, dn, issuer, NULL);
if(header && header[0]) {
return header;
}
}
return NULL;
}
static const char *qos_pseudoip(request_rec *r, const char *header) {
const char *forwardedfor = NULL;
if(strcmp("SSL_CLIENT_S_DN", header) == 0) {
forwardedfor = qos_forwardedfor_fromSSL(r);
} else {
forwardedfor = qos_forwardedfor_fromHeader(r, header);
}
if(forwardedfor && forwardedfor[0]) {
return qos_ipv6_hash(r, forwardedfor);
}
return NULL;
}
static const char *qos_forwardedfor(request_rec *r, const char *header) {
const char *forwardedfor = NULL;
if(header[0] == '#') {
forwardedfor = qos_pseudoip(r, &header[1]);
} else {
forwardedfor = qos_forwardedfor_fromHeader(r, header);
}
return forwardedfor;
}
/**
* Returns the client IP, either from the connection
* of from the forwarded-for header if configured
*
* @param r Request to get the IP from the header
* @param sconf
* @param cconf (if available) to get the real IP from
* @param caller Which caller of the method
* @param ip6 The client's IP address to be used (pointer to array of unsigned long (2))
* @return The client's IP address as a string (for logging)
*/
static const char *qos_get_clientIP(request_rec *r, qos_srv_config *sconf,
qs_conn_ctx *cconf, const char *caller,
apr_uint64_t *ip6) {
const char *forwardedForLogIP;
if(sconf->qos_cc_forwardedfor) {
const char *forwardedfor = qos_forwardedfor(r, sconf->qos_cc_forwardedfor);
if(forwardedfor) {
if(qos_ip_str2long(forwardedfor, ip6) == 0) {
if(apr_table_get(r->notes, "QOS_LOG_PFX069") == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(069)"no valid IP header found (@%s):"
" invalid header value '%s',"
" fallback to connection's IP %s, id=%s",
caller,
forwardedfor,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" :
QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "069"));
apr_table_set(r->notes, "QOS_LOG_PFX069", "log once");
QS_INC_EVENT(sconf, 69);
}
} else {
// done
return forwardedfor;
}
} else {
if(apr_table_get(r->notes, "QOS_LOG_PFX069") == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(069)"no valid IP header found (@%s):"
" header '%s' not available,"
" fallback to connection's IP %s, id=%s",
caller,
sconf->qos_cc_forwardedfor,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "069"));
apr_table_set(r->notes, "QOS_LOG_PFX069", "log once");
QS_INC_EVENT(sconf, 69);
}
}
}
// use the real IP
if(cconf) {
forwardedForLogIP = QS_CONN_REMOTEIP(cconf->mc);
// works for real connections only (no HTTP/2)
ip6[0] = cconf->ip6[0];
ip6[1] = cconf->ip6[1];
} else {
// HTTP/2
forwardedForLogIP = QS_CONN_REMOTEIP(r->connection);
qos_ip_str2long(forwardedForLogIP, ip6);
}
return forwardedForLogIP;
}
/**
* returns the version number of mod_qos
* @param p Pool to alloc version string from
* @return Version string
*/
static char *qos_revision(apr_pool_t *p) {
return apr_pstrdup(p, g_revision);
}
/**
* returns the request context
*/
static qs_req_ctx *qos_rctx_config_get(request_rec *r) {
qs_req_ctx *rctx = ap_get_module_config(r->request_config, &qos_module);
if(rctx == NULL) {
rctx = apr_pcalloc(r->pool, sizeof(qs_req_ctx));
rctx->entry = NULL;
rctx->entry_cond = NULL;
rctx->evmsg = NULL;
rctx->is_vip = 0;
rctx->event_entries = apr_table_make(r->pool, 1);
rctx->maxpostcount = 0;
rctx->cc_event_req_set = 0;
rctx->cc_event_ip[0] = 0;
rctx->cc_event_ip[1] = 0;
rctx->cc_serialize_set = 0;
rctx->cc_serialize_ip[0] = 0;
rctx->cc_serialize_ip[1] = 0;
rctx->srv_serialize_set = 0;
rctx->body_window = NULL;
rctx->response_delayed = 0;
ap_set_module_config(r->request_config, &qos_module, rctx);
}
return rctx;
}
/**
* Adds the defined mod_qos_ev id to the request context (creates
* the req ctx if necessary).
*
* @param r
* @param id ID to set, e.g. "L;" or "D;"
*/
static void qs_set_evmsg(request_rec *r, const char *id) {
qs_req_ctx *rctx = qos_rctx_config_get(r);
if(rctx->evmsg == NULL || (strstr(rctx->evmsg, id) == NULL)) {
rctx->evmsg = apr_pstrcat(r->pool, id, rctx->evmsg, NULL);
}
}
/**
* Encrypts and base64 encodes the provided buffer
* @param r
* @param sconf Secret to use (sconf->key)
* @param b Buffer to encrypt
* @param l Length of the buffer
* @return Encrypted string (or NULL on error)
*/
static char *qos_encrypt(request_rec *r, qos_srv_config *sconf,
const unsigned char *b, int l) {
#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_CIPHER_CTX cipher_ctx;
EVP_CIPHER_CTX *cipher_ctx_p = &cipher_ctx;
HMAC_CTX hmac;
HMAC_CTX *hmac_p = &hmac;
#else
EVP_CIPHER_CTX *cipher_ctx_p;
HMAC_CTX *hmac_p;
#endif
unsigned char hash[HMAC_MAX_MD_CBLOCK];
unsigned int hashLen = HMAC_MAX_MD_CBLOCK;
int buf_len = 0;
int len = 0;
unsigned char *buf = apr_pcalloc(r->pool, +
EVP_MAX_IV_LENGTH +
QOS_HASH_LEN +
l +
EVP_CIPHER_block_size(EVP_des_ede3_cbc()));
#if APR_HAS_RANDOM
if(apr_generate_random_bytes(buf, EVP_MAX_IV_LENGTH) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
QOS_LOG_PFX(080)"Can't generate random data, id=%s",
qos_unique_id(r, NULL));
}
#else
if(!RAND_bytes(buf, EVP_MAX_IV_LENGTH)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
QOS_LOG_PFX(080)"Can't generate random data, id=%s",
qos_unique_id(r, NULL));
}
#endif
/* checksum */
#if OPENSSL_VERSION_NUMBER < 0x10100000L
HMAC_CTX_init(hmac_p);
#else
hmac_p = HMAC_CTX_new();
#endif
#ifndef OPENSSL_NO_MD5
HMAC_Init_ex(hmac_p, sconf->rawKey, sconf->rawKeyLen, EVP_md5(), NULL);
#else
HMAC_Init_ex(hmac_p, sconf->rawKey, sconf->rawKeyLen, EVP_sha256(), NULL);
#endif
HMAC_Update(hmac_p, b, l);
HMAC_Final(hmac_p, hash, &hashLen);
#if OPENSSL_VERSION_NUMBER < 0x10100000L
HMAC_CTX_cleanup(hmac_p);
#else
HMAC_CTX_free(hmac_p);
#endif
/* sym enc */
#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_CIPHER_CTX_init(cipher_ctx_p);
#else
cipher_ctx_p = EVP_CIPHER_CTX_new();
#endif
EVP_EncryptInit(cipher_ctx_p, EVP_des_ede3_cbc(), sconf->key, (unsigned char *)buf);
// skip iv, enc(hash + data)
buf_len = EVP_MAX_IV_LENGTH;
if(!EVP_EncryptUpdate(cipher_ctx_p, &buf[buf_len], &len, hash, QOS_HASH_LEN)) {
goto failed;
}
buf_len+=len;
if(!EVP_EncryptUpdate(cipher_ctx_p, &buf[buf_len], &len, b, l)) {
goto failed;
}
buf_len+=len;
if(!EVP_EncryptFinal(cipher_ctx_p, &buf[buf_len], &len)) {
goto failed;
}
buf_len+=len;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_CIPHER_CTX_cleanup(cipher_ctx_p);
#else
EVP_CIPHER_CTX_free(cipher_ctx_p);
#endif
/* b64 encode */
{
char *data = (char *)apr_pcalloc(r->pool, 1 + apr_base64_encode_len(buf_len));
len = apr_base64_encode(data, (const char *)buf, buf_len);
data[len] = '\0';
return data;
}
failed:
#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_CIPHER_CTX_cleanup(cipher_ctx_p);
#else
EVP_CIPHER_CTX_free(cipher_ctx_p);
#endif
if(QS_ISDEBUG(r->server)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
QOS_LOGD_PFX"qos_encrypt() encryption operation failed, id=%s",
qos_unique_id(r, NULL));
}
return NULL;
}
/**
* Decryptes the base64 encoded string (see qos_encrypt()).
* @param r
* @param sconf To access the secret
* @param ret_buf Pointer to the decypted data (result), NULL if decyption fails
* @param value Base64 encded string to decrypt
* @return Length of the decypted data (0 if decyption failed)
*/
static int qos_decrypt(request_rec *r, qos_srv_config* sconf,
unsigned char **ret_buf, const char *value) {
#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_CIPHER_CTX cipher_ctx;
EVP_CIPHER_CTX *cipher_ctx_p = &cipher_ctx;
#else
EVP_CIPHER_CTX *cipher_ctx_p;
#endif
/* decode */
char *dec = (char *)apr_pcalloc(r->pool, 1 + apr_base64_decode_len(value));
int dec_len = apr_base64_decode(dec, value);
*ret_buf = NULL;
if(dec_len < (EVP_MAX_IV_LENGTH + QOS_HASH_LEN)) {
if(QS_ISDEBUG(r->server)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
QOS_LOGD_PFX"qos_decrypt() base64 decoding failed, id=%s",
qos_unique_id(r, NULL));
}
return 0;
} else {
/* decrypt */
#if OPENSSL_VERSION_NUMBER < 0x10100000L
HMAC_CTX hmac;
HMAC_CTX *hmac_p = &hmac;
#else
HMAC_CTX *hmac_p;
#endif
unsigned char hash[HMAC_MAX_MD_CBLOCK];
unsigned int hashLen = HMAC_MAX_MD_CBLOCK;
int len = 0;
int buf_len = 0;
unsigned char *buf;
dec_len -= EVP_MAX_IV_LENGTH;
buf = apr_pcalloc(r->pool, dec_len);
/* sym dec */
#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_CIPHER_CTX_init(cipher_ctx_p);
#else
cipher_ctx_p = EVP_CIPHER_CTX_new();
#endif
EVP_DecryptInit(cipher_ctx_p, EVP_des_ede3_cbc(), sconf->key, (unsigned char *)dec);
if(!EVP_DecryptUpdate(cipher_ctx_p, (unsigned char *)&buf[buf_len], &len,
(const unsigned char *)&dec[EVP_MAX_IV_LENGTH], dec_len)) {
goto failed;
}
buf_len+=len;
if(!EVP_DecryptFinal(cipher_ctx_p, (unsigned char *)&buf[buf_len], &len)) {
goto failed;
}
buf_len+=len;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_CIPHER_CTX_cleanup(cipher_ctx_p);
#else
EVP_CIPHER_CTX_free(cipher_ctx_p);
#endif
// hash + data
if(buf_len < (QOS_HASH_LEN + 1)) {
if(QS_ISDEBUG(r->server)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
QOS_LOGD_PFX"qos_decrypt() misshing hash, id=%s",
qos_unique_id(r, NULL));
}
return 0;
}
/* checksum */
buf_len -= QOS_HASH_LEN;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
HMAC_CTX_init(hmac_p);
#else
hmac_p = HMAC_CTX_new();
#endif
#ifndef OPENSSL_NO_MD5
HMAC_Init_ex(hmac_p, sconf->rawKey, sconf->rawKeyLen, EVP_md5(), NULL);
#else
HMAC_Init_ex(hmac_p, sconf->rawKey, sconf->rawKeyLen, EVP_sha256(), NULL);
#endif
HMAC_Update(hmac_p, &buf[QOS_HASH_LEN], buf_len);
HMAC_Final(hmac_p, hash, &hashLen);
#if OPENSSL_VERSION_NUMBER < 0x10100000L
HMAC_CTX_cleanup(hmac_p);
#else
HMAC_CTX_free(hmac_p);
#endif
if(hashLen > QOS_HASH_LEN) {
// we don't keep more than 16 bytes
hashLen = QOS_HASH_LEN;
}
if(memcmp(hash, buf, hashLen) != 0) {
if(QS_ISDEBUG(r->server)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
QOS_LOGD_PFX"qos_decrypt() invalid hash, id=%s",
qos_unique_id(r, NULL));
}
return 0;
}
/* decrypted and valid */
*ret_buf = &buf[QOS_HASH_LEN];
return buf_len;
}
failed:
#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_CIPHER_CTX_cleanup(cipher_ctx_p);
#else
EVP_CIPHER_CTX_free(cipher_ctx_p);
#endif
if(QS_ISDEBUG(r->server)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
QOS_LOGD_PFX"qos_decrypt() decryption operation failed, id=%s",
qos_unique_id(r, NULL));
}
return 0;
}
/**
* Adds the user tracking cookie to r->headers_out if QOS_USER_TRACKING_NEW env variable
* has been set.
* @param r
* @param sconf
* @param status (302 or other)
*/
static void qos_send_user_tracking_cookie(request_rec *r, qos_srv_config* sconf,
int status) {
const char *new_user = apr_table_get(r->subprocess_env, QOS_USER_TRACKING_NEW);
if(new_user) {
char *setCookieVal;
apr_size_t retcode;
char timeString[MAX_STRING_LEN];
apr_time_exp_t timeEx;
int new_user_len = strlen(new_user);
int len = 2 + new_user_len;
unsigned char *value = apr_pcalloc(r->pool, len + 1);
char *encryptedVal;
char *domain = NULL;
apr_time_exp_gmt(&timeEx, r->request_time);
apr_strftime(timeString, &retcode, sizeof(timeString), "%m", &timeEx);
#ifdef QS_INTERNAL_TEST
{
const char *m = apr_table_get(r->headers_in, "X-TEST-USER-TRACK-MONTH");
if(m) {
strcpy(timeString, m);
}
}
#endif
memcpy(value, timeString, 2);
memcpy(&value[2], new_user, new_user_len);
value[len] = '\0';
encryptedVal = qos_encrypt(r, sconf, value, len + 1);
if(sconf->user_tracking_cookie_domain != NULL) {
domain = apr_pstrcat(r->pool, "; Domain=", sconf->user_tracking_cookie_domain, NULL);
}
/* set cookie valid for 300 days or for this session only */
setCookieVal = apr_psprintf(r->pool, "%s=%s; Path=/%s%s",
sconf->user_tracking_cookie, encryptedVal,
sconf->user_tracking_cookie_session < 1 ? "; Max-Age=25920000" : "",
domain != NULL ? domain : "");
if(status != HTTP_MOVED_TEMPORARILY) {
apr_table_add(r->headers_out, "Set-Cookie", setCookieVal);
} else {
apr_table_add(r->err_headers_out, "Set-Cookie", setCookieVal);
}
}
return;
}
/**
* Verifies and sets the user tracking cookie
* - QOS_USER_TRACKING if the cookie was available
* - QOS_USER_TRACKING_NEW if a new cookie needs to be set
* - QOS_USER_TRACKING_RENEW if the cookie shall be renewed
*
* syntax: b64(enc(<month><UNIQUE_ID>))
*
* shall be called after(!) mod_unique_id has created an id
*
* @param r
* @param sconf
* @param value Cookie received from the client, possibly null (see qos_get_remove_cookie())
*/
static void qos_get_create_user_tracking(request_rec *r, qos_srv_config* sconf,
const char *value) {
const char *uid = qos_unique_id(r, NULL);
const char *verified = NULL;
const char *newUID = NULL;
if(value != NULL) {
int buf_len = 0;
unsigned char *buf;
buf_len = qos_decrypt(r, sconf, &buf, value);
if(buf_len > 0) {
verified = (char *)buf;
}
}
if(verified == NULL) {
newUID = uid;
apr_table_set(r->subprocess_env, QOS_USER_TRACKING_NEW, newUID);
qs_set_evmsg(r, "u;");
} else if(strlen(verified) > 2) {
apr_size_t retcode;
char timeString[MAX_STRING_LEN];
apr_time_exp_t timeEx;
apr_time_exp_gmt(&timeEx, r->request_time);
apr_strftime(timeString, &retcode, sizeof(timeString), "%m", &timeEx);
if(strncmp(timeString, verified, 2) != 0) {
/* renew, if not from this month */
apr_table_set(r->subprocess_env, QOS_USER_TRACKING_NEW, &verified[2]);
apr_table_set(r->subprocess_env, QOS_USER_TRACKING_RENEW, "1");
}
newUID = &verified[2];
} else {
newUID = uid;
apr_table_set(r->subprocess_env, QOS_USER_TRACKING_NEW, newUID);
}
apr_table_set(r->subprocess_env, QOS_USER_TRACKING, newUID);
return;
}
/**
* Adds new milestone cookie to the response headers if QOS_MILESTONE_COOKIE
* has been set.
* See qos_verify_milestone() about the syntax.
*/
static void qos_update_milestone(request_rec *r, qos_srv_config* sconf) {
const char *new_ms = apr_table_get(r->subprocess_env, QOS_MILESTONE_COOKIE);
if(new_ms) {
apr_time_t now = apr_time_sec(r->request_time);
int new_ms_len = strlen(new_ms);
int len = sizeof(apr_time_t) + new_ms_len;
unsigned char *value = apr_pcalloc(r->pool, len + 1);
char *encryptedVal;
apr_table_unset(r->subprocess_env, QOS_MILESTONE_COOKIE);
memcpy(value, &now, sizeof(apr_time_t));
memcpy(&value[sizeof(apr_time_t)], new_ms, new_ms_len);
value[len] = '\0';
encryptedVal = qos_encrypt(r, sconf, value, len);
apr_table_add(r->headers_out, "Set-Cookie",
apr_psprintf(r->pool, "%s=%s; Path=/;",
QOS_MILESTONE_COOKIE, encryptedVal));
}
return;
}
/**
* Verifies the milestone. Evaluates rule and enforces it. Does also set the
* QOS_MILESTONE_COOKIE variable if a new milestone has been reached.
*
* milestone cookie syntax: b64(enc(<time><milestone>))
*
* @param r
* @param sconf
* @param value Cookie received from the client (contains the already reached milestones)
* @return APR_SUCCESS if request is allowed, otherwise HTTP_FORBIDDEN
*/
static int qos_verify_milestone(request_rec *r, qos_srv_config* sconf, const char *value) {
char *the_request;
int the_request_len;
int escerr = 0;
qos_milestone_t *milestone = NULL;
qos_milestone_t *entries;
int i;
int ms = -1; // milestone the user has reached
int required = -1; // required for this request
apr_time_t age = 0; // milestone's age
if(value != NULL) {
int buf_len = 0;
unsigned char *buf;
buf_len = qos_decrypt(r, sconf, &buf, value);
if(buf_len >= (sizeof(apr_time_t) + 1)) {
apr_time_t *t = (apr_time_t *)buf;
apr_time_t now = apr_time_sec(r->request_time);
age = now - *t;
if(now <= (*t + sconf->milestoneTimeout)) {
ms = atoi((char *)&buf[sizeof(apr_time_t)]);
}
}
}
the_request = apr_pstrdup(r->pool, r->the_request);
the_request_len = qos_unescaping(the_request, QOS_DEC_MODE_FLAGS_URL, &escerr);
entries = (qos_milestone_t *)sconf->milestones->elts;
for(i = 0; i < sconf->milestones->nelts; ++i) {
milestone = &entries[i];
if(qos_regexec_len(r->pool, milestone->preg, the_request, the_request_len) == 0) {
required = milestone->num;
break;
}
}
if(milestone && (required >= 0)) {
int severity = milestone->action == QS_DENY ? APLOG_ERR : APLOG_WARNING;
if(ms < (required - 1)) {
/* not allowed */
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|severity, 0, r,
QOS_LOG_PFX(047)"access denied, reached milestone '%d' (%s),"
" user has already passed '%s',"
" action=%s, c=%s, id=%s",
required, milestone->pattern,
ms == -1 ? "none" : apr_psprintf(r->pool, "%d", ms),
!sconf->log_only && milestone->action == QS_DENY ?
"deny" : "log only (pass milestone)",
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "047"));
QS_INC_EVENT(sconf, 47);
if(milestone->action == QS_DENY) {
return HTTP_FORBIDDEN;
}
}
if(milestone->thinktime > 0) {
if(age < milestone->thinktime) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|severity, 0, r,
QOS_LOG_PFX(147)"access denied, reached milestone '%d' (%s),"
" earlier than expected (right after %"APR_TIME_T_FMT" instead of %d seconds),"
" action=%s, c=%s, id=%s",
required, milestone->pattern,
age, milestone->thinktime,
!sconf->log_only && milestone->action == QS_DENY ?
"deny" : "log only (pass milestone)",
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "147"));
QS_INC_EVENT(sconf, 147);
if(milestone->action == QS_DENY) {
return HTTP_FORBIDDEN;
}
}
}
if(required > ms) {
/* update milestone */
apr_table_set(r->subprocess_env, QOS_MILESTONE_COOKIE, apr_psprintf(r->pool, "%d", required));
}
}
return APR_SUCCESS;
}
/**
* Extracts the cookie from the request.
* @param r
* @param cookieName Name of the cookie to remove from the request headers
* @param Cookie if available of NULL if not
*/
static char *qos_get_remove_cookie(request_rec *r, const char *cookieName) {
const char *cookieHdr = apr_table_get(r->headers_in, "cookie");
if(cookieHdr) {
char *cn = apr_pstrcat(r->pool, cookieName, "=", NULL);
char *pt = ap_strcasestr(cookieHdr, cn);
char *p = NULL;
while(pt && !p) {
// ensure we found the real cookie (and not an ending b64 str)
if(pt == cookieHdr) {
// @beginning of the header
p = pt;
pt = NULL;
} else {
char pre = pt[-1];
if(pre == ' ' ||
pre == ';') {
// @beginning of a cookie
p = pt;
pt = NULL;
} else {
// found patter somewhere else
pt++;
pt = ap_strcasestr(pt, cn);
}
}
}
if(p) {
char *sp = p;
char *value = NULL;
p[0] = '\0'; /* terminates the beginning of the cookie header */
sp--; /* deletes spaces "in front" of the qos cookie */
while((sp > cookieHdr) && (sp[0] == ' ')) {
sp[0] = '\0';
sp--;
}
p = &p[strlen(cn)];
value = ap_getword(r->pool, (const char **)&p, ';');
while(p && (p[0] == ' ')) p++;
/* skip a path, if there is any */
if(p && (strncasecmp(p, "$path=", strlen("$path=")) == 0)) {
ap_getword(r->pool, (const char **)&p, ';');
}
/* restore cookie header appending the part left*/
if(p && p[0]) {
if(cookieHdr[0]) {
if(p[0] == ' ') {
cookieHdr = apr_pstrcat(r->pool, cookieHdr, p, NULL);
} else {
cookieHdr = apr_pstrcat(r->pool, cookieHdr, " ", p, NULL);
}
} else {
cookieHdr = apr_pstrcat(r->pool, p, NULL);
}
}
if(strlen(cookieHdr) == 0) {
apr_table_unset(r->headers_in, "cookie");
} else {
if((strncasecmp(cookieHdr, "$Version=", strlen("$Version=")) == 0) &&
(strlen(cookieHdr) <= strlen("$Version=X; "))) {
/* nothing left */
apr_table_unset(r->headers_in, "cookie");
} else {
apr_table_set(r->headers_in, "cookie", cookieHdr);
}
}
return value;
}
}
return NULL;
}
/**
* verifies the session cookie 0=failed, 1=succeeded
*/
static int qos_verify_session(request_rec *r, qos_srv_config* sconf) {
int buf_len = 0;
unsigned char *buf;
char *value = qos_get_remove_cookie(r, sconf->cookie_name);
if(value == NULL) {
return 0;
}
buf_len = qos_decrypt(r, sconf, &buf, value);
if(buf_len != sizeof(qos_session_t)) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, r,
QOS_LOG_PFX(021)"session cookie verification failed, "
"decoding failed, id=%s", qos_unique_id(r, "021"));
QS_INC_EVENT(sconf, 21);
return 0;
} else {
qos_session_t *s = (qos_session_t *)buf;
if(s->time < (apr_time_sec(r->request_time) - sconf->max_age)) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, r,
QOS_LOG_PFX(023)"session cookie verification failed, "
"expired, id=%s", qos_unique_id(r, "023"));
QS_INC_EVENT(sconf, 23);
return 0;
}
}
/* success */
apr_table_set(r->notes, QS_REC_COOKIE, "");
return 1;
}
/**
* set/update the session cookie
*/
static void qos_set_session(request_rec *r, qos_srv_config *sconf) {
qos_session_t *s = (qos_session_t *)apr_pcalloc(r->pool, sizeof(qos_session_t));
char *cookie;
char *session;
qs_set_evmsg(r, "V;"); // log VIP session creation
/* payload */
s->time = time(NULL);
session = qos_encrypt(r, sconf, (const unsigned char *)s, sizeof(qos_session_t));
if(session == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, r,
QOS_LOG_PFX(025)"failed to create session cookie, id=%s",
qos_unique_id(r, "025"));
QS_INC_EVENT(sconf, 25);
return;
}
cookie = apr_psprintf(r->pool, "%s=%s; Path=%s; Max-Age=%d",
sconf->cookie_name, session,
sconf->cookie_path, sconf->max_age);
apr_table_add(r->headers_out,"Set-Cookie", cookie);
return;
}
/**
* destroy shared memory and mutexes
*/
static void qos_destroy_act(qs_actable_t *act) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
QOS_LOGD_PFX"cleanup shared memory: %"APR_SIZE_T_FMT" bytes",
act->size);
act->child_init = 0;
if(act->lock_file && act->lock_file[0]) {
/*
We don't need to destroy locks manually as
apr_global_mutex_create() registers the cleanup
method itself to the pool we used when allocating the
lock.
if(act->lock) {
apr_global_mutex_destroy(act->lock);
}
*/
act->lock_file[0] = '\0';
act->lock_file = NULL;
}
/*
We don't need to destroy shared memory
manually as apr_shm_create() registers the
cleanup method to the pool we used when
allocating the memory.
if(act->m) {
apr_shm_destroy(act->m);
}
*/
apr_pool_destroy(act->pool);
}
/**
* returns the persistent configuration (restarts)
*/
static qos_user_t *qos_get_user_conf(apr_pool_t *ppool) {
void *v;
qos_user_t *u;
apr_pool_userdata_get(&v, QS_USR_SPE, ppool);
u = v;
if(v) {
return v;
}
u = (qos_user_t *)apr_pcalloc(ppool, sizeof(qos_user_t));
u->server_start = 0;
u->act_table = apr_table_make(ppool, 2);
apr_pool_userdata_set(u, QS_USR_SPE, apr_pool_cleanup_null, ppool);
u->qos_cc = NULL;
return u;
}
/**
* increments the event counters for the specified event
* @param ppool Process pool to fetch user ctx from
* @param event Event number
* @param locked Set this to 1 if user ctx is already locked
*/
static void qs_inc_eventcounter(apr_pool_t *ppool, int event, int locked) {
qos_user_t *u = qos_get_user_conf(ppool);
if(u->qos_cc == NULL) {
return;
}
if(!locked) {
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT49 */
}
u->qos_cc->eventTotal[event]++;
u->qos_cc->eventLast[event]++;
if(!locked) {
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT49 */
}
}
static int qs_calc_maxClients(server_rec *bs, qos_srv_config *bsconf) {
int maxThreads = 0;
int maxDaemons = 0;
int maxClientsCalc = 0;
int maxClients = 0;
ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &maxDaemons);
ap_mpm_query(AP_MPMQ_MAX_THREADS, &maxThreads);
maxClientsCalc = (maxThreads == 0 ? 1 : maxThreads) * (maxDaemons == 0 ? 1 : maxDaemons);
if(m_event_mpm) {
/* QS_MaxClients = (AsyncRequestWorkerFactor + 1) * MaxRequestWorkers */
maxClientsCalc = (m_event_mpm_worker_factor + 1) * maxClientsCalc;
}
if(bsconf->max_clients_conf > 0) {
maxClients = bsconf->max_clients_conf;
} else {
maxClients = maxClientsCalc;
}
if(maxClients != maxClientsCalc) {
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, bs,
QOS_LOG_PFX(007)"calculated MaxClients/MaxRequestWorkers (max connections): %d,"
" applied limit: %d (QS_MaxClients)",
maxClientsCalc, maxClients);
} else {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, bs,
QOS_LOGD_PFX"calculated MaxClients/MaxRequestWorkers (max connections): %d",
maxClients);
}
return maxClients;
}
#ifdef QS_APACHE_24
/**
* tells if server is terminating immediately or not
*/
static int qos_is_graceful() {
if(ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_EXITING) {
return 0;
}
if(ap_state_query(AP_SQ_CONFIG_GEN) == 0) {
return 0;
}
return 1;
}
#else
/**
* tells if server is terminating immediately or not
*/
static int qos_is_graceful() {
int mpm_gen = 0;
QOS_MY_GENERATION(mpm_gen);
if(mpm_gen != m_generation) return 1;
return 0;
}
#endif
/* clear all counters of the per client data store at graceful restart
used to prevent counter grow due blocked/crashed client processes*/
static void qos_clear_cc(qos_user_t *u) {
if(u->qos_cc) {
qos_s_entry_t **entry;
int i;
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT37 */
u->qos_cc->connections = 0;
if(m_generation > 0) {
// this process generation must not update the connections counter anymore
u->qos_cc->generation_locked = m_generation;
}
entry = u->qos_cc->ipd;
for(i = 0; i < u->qos_cc->max; i++) {
(*entry)->event_req = 0;
(*entry)->serialize = 0;
entry++;
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT37 */
}
}
/**
* destroys the act
* shared memory must not be destroyed before graceful restart has
* been finished due running requests still need the shared memory
* till they have finished.
* keep the memory leak as little as possible ...
*/
static apr_status_t qos_cleanup_shm(void *p) {
qs_actable_t *act = p;
qos_user_t *u = qos_get_user_conf(act->ppool);
char *this_generation;
char *last_generation;
int i;
apr_table_entry_t *entry;
this_generation = apr_psprintf(act->ppool, "%d", m_generation);
last_generation = apr_psprintf(act->pool, "%d", m_generation-1);
qos_clear_cc(u);
/* delete acts from the last graceful restart */
entry = (apr_table_entry_t *)apr_table_elts(u->act_table)->elts;
for(i = 0; i < apr_table_elts(u->act_table)->nelts; i++) {
if(strcmp(entry[i].key, last_generation) == 0) {
qs_actable_t *a = (qs_actable_t *)entry[i].val;
#ifdef QS_INTERNAL_TEST
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, NULL,
QOS_LOGD_PFX"clear ACT generation '%s' at '%d'", last_generation, m_generation);
#endif
qos_destroy_act(a);
}
}
apr_table_unset(u->act_table, last_generation);
if(qos_is_graceful()) {
/* don't delete this act now, but at next server restart ... */
apr_table_addn(u->act_table, this_generation, (char *)act);
} else {
if(u->qos_cc) {
qos_cc_free(u->qos_cc);
u->qos_cc = NULL;
}
#ifdef QS_INTERNAL_TEST
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, NULL,
QOS_LOGD_PFX"clear ACT generation 'current' at '%d'", m_generation);
#endif
qos_destroy_act(act);
}
return APR_SUCCESS;
}
/**
* init the shared memory of the act
* act->serialize <- start
act->qsstatustimer <- start + sizeof(serialize)
* act->conn <- start + sizeof(serialize) + sizeof(time_t)
* act->conn->conn_ip <- start + sizeof(serialize) + sizeof(time_t) + sizeof(conn) * QS_MEM_SEG
* + [max_ip]
* act->entry <-
* + [rule_entries]
* act->event_limit <-
* + [event_limit_entries]
*/
static apr_status_t qos_init_shm(server_rec *s, qos_srv_config *sconf, qs_actable_t *act,
apr_table_t *locRuleCfgTable, int maxClients) {
char *file = "-";
apr_status_t res;
int i;
int ruleEntries = apr_table_elts(locRuleCfgTable)->nelts;
apr_table_entry_t *locRuleEntry = (apr_table_entry_t *)apr_table_elts(locRuleCfgTable)->elts;
int event_limit_entries = sconf->event_limit_a->nelts;
qs_acentry_t *actEntry = NULL;
int max_ip = maxClients;
act->size = ((max_ip+QS_DOUBLE_CONN) * QS_MEM_SEG * APR_ALIGN_DEFAULT(sizeof(qs_ip_entry_t))) +
(ruleEntries * APR_ALIGN_DEFAULT(sizeof(qs_acentry_t))) +
(event_limit_entries * APR_ALIGN_DEFAULT(sizeof(qos_event_limit_entry_t))) +
APR_ALIGN_DEFAULT(sizeof(qs_conn_t)) +
APR_ALIGN_DEFAULT(sizeof(qs_serial_t)) +
APR_ALIGN_DEFAULT(sizeof(time_t)) +
2048;
/* use anonymous shm by default */
res = apr_shm_create(&act->m, act->size, NULL, act->pool);
if(APR_STATUS_IS_ENOTIMPL(res)) {
file = apr_psprintf(act->pool, "%s_m.mod_qos",
qos_tmpnam(act->pool, s));
#ifdef ap_http_scheme
/* Apache 2.2 */
apr_shm_remove(file, act->pool);
#endif
res = apr_shm_create(&act->m, act->size, file, act->pool);
}
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
QOS_LOGD_PFX"%s(%s), create shared memory (ACT)(%s): %"APR_SIZE_T_FMT" bytes"
" (r=%d,ip=%d)",
s->server_hostname == NULL ? "-" : s->server_hostname,
s->is_virtual ? "v" : "b",
file,
act->size,
ruleEntries, max_ip);
if(res != APR_SUCCESS) {
char buf[MAX_STRING_LEN];
apr_strerror(res, buf, sizeof(buf));
ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
QOS_LOG_PFX(002)"failed to create shared memory (ACT)(%s): %s"
" (%"APR_SIZE_T_FMT" bytes)",
file, buf, act->size);
return res;
} else {
qs_serial_t *sp = apr_shm_baseaddr_get(act->m);
time_t *qsstatustimer = (time_t *)&sp[1];
qs_conn_t *connEntryP = (qs_conn_t *)&qsstatustimer[1];
qs_ip_entry_t *conn_ip = (qs_ip_entry_t *)&connEntryP[1];
apr_time_t now = apr_time_now();
act->serialize = sp;
act->serialize->q1 = 0;
act->serialize->q2 = 0;
act->serialize->locked = 0;
act->qsstatustimer = qsstatustimer;
*act->qsstatustimer = 0;
act->conn = connEntryP;
act->conn->conn_ip_len = (max_ip + QS_DOUBLE_CONN) * QS_MEM_SEG;
act->conn->conn_ip = conn_ip;
act->conn->max_client = max_ip;
act->conn->connections = 0;
for(i = 0; i < act->conn->conn_ip_len; i++) {
conn_ip->ip6[0] = 0;
conn_ip->ip6[1] = 0;
conn_ip->counter = 0;
conn_ip->error = 0;
conn_ip++;
}
if(ruleEntries) {
act->entry = (qs_acentry_t *)conn_ip;
actEntry = act->entry;
} else {
act->entry = NULL;
}
/* init rule entries (link data, init mutex) */
for(i = 0; i < ruleEntries; i++) {
qs_rule_ctx_t *rule = (qs_rule_ctx_t *)locRuleEntry[i].val;
actEntry->next = &actEntry[1];
actEntry->id = i;
actEntry->url = rule->url;
actEntry->url_len = strlen(actEntry->url);
actEntry->event = rule->event;
if(actEntry->event) {
act->has_events++;
}
actEntry->regex = rule->regex;
actEntry->condition = rule->condition;
actEntry->regex_var = rule->regex_var;
actEntry->limit = rule->limit;
if(actEntry->limit == 0 ) {
if((actEntry->condition == NULL) && (actEntry->event == NULL)) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, s,
QOS_LOG_PFX(003)"request level rule %s has no "
"concurrent request limitations",
actEntry->url);
}
}
actEntry->kbytes_interval_us = now;
actEntry->interval = apr_time_sec(now);
actEntry->bytes = 0;
actEntry->req_per_sec_limit = rule->req_per_sec_limit;
actEntry->kbytes_per_sec_limit = rule->kbytes_per_sec_limit;
actEntry->kbytes_per_sec = 0;
actEntry->counter = 0;
actEntry->lock = act->lock;
if(i < ruleEntries - 1) {
actEntry = actEntry->next;
} else {
actEntry->next = NULL;
}
}
if(event_limit_entries == 0) {
act->event_entry = NULL;
} else {
// source (config) event limit array
qos_event_limit_entry_t *eventEntrySrc = (qos_event_limit_entry_t *)sconf->event_limit_a->elts;
// target (act) event limit array
qos_event_limit_entry_t *eventEntryDst;
if(actEntry) {
// end of the last act rule entry
act->event_entry = (qos_event_limit_entry_t *)&actEntry[1];
} else {
// end of the last connection entry
act->event_entry = (qos_event_limit_entry_t *)conn_ip;
}
eventEntryDst = act->event_entry;
// set config
for(i = 0; i < event_limit_entries; i++) {
eventEntryDst->env_var = eventEntrySrc->env_var;
eventEntryDst->eventDecStr = eventEntrySrc->eventDecStr;
eventEntryDst->max = eventEntrySrc->max;
eventEntryDst->seconds = eventEntrySrc->seconds;
eventEntryDst->limit = 0;
eventEntryDst->limitTime = 0;
eventEntryDst->action = eventEntrySrc->action;
eventEntryDst->condStr = eventEntrySrc->condStr;
eventEntryDst->preg = eventEntrySrc->preg;
eventEntryDst++;
eventEntrySrc++;
}
}
}
return APR_SUCCESS;
}
/**
* Loads the geo database. See QS_GEO_PATTERN about the file format.
* @param pool To allocate memory from
* @param geodb Data structore to initialize
* @param msg Error message if something went wrong while loading the db
* @param errors Number of errors
* @return APR_SUCCESS if data could be loaded resp. lines have been counted
*/
static apr_status_t qos_loadgeo(apr_pool_t *pool, qos_geo_t *geodb,
char **msg, int *errors) {
ap_regmatch_t ma[AP_MAX_REG_MATCH];
ap_regex_t *preg;
qos_geo_entry_t *entry = NULL;
qos_geo_entry_t *last = NULL;
int lines = 0;
char line[HUGE_STRING_LEN];
FILE *file;
preg = ap_pregcomp(pool, QS_GEO_PATTERN, AP_REG_EXTENDED);
if(preg == NULL) {
// internal error
*msg = apr_pstrdup(pool, "failed to compile regular"
" expression "QS_GEO_PATTERN);
(*errors)++;
return APR_INCOMPLETE;
}
file = fopen(geodb->path, "r");
if(!file) {
*msg = apr_psprintf(pool, "could not open file %s (%s)",
geodb->path, strerror(errno));
(*errors)++;
return APR_INCOMPLETE;
}
// check syntax and determine required memory size
while(fgets(line, sizeof(line), file) != NULL) {
if(strlen(line) > 0) {
if(ap_regexec(preg, line, 0, NULL, 0) == 0) {
lines++;
} else {
*msg = apr_psprintf(pool, "invalid entry in database: '%s'", line);
(*errors)++;
}
}
}
if(*errors != 0) {
return APR_INCOMPLETE;
}
geodb->size = lines;
geodb->data = apr_pcalloc(pool, APR_ALIGN_DEFAULT(sizeof(qos_geo_entry_t)) * geodb->size);
// load the file into the memory
entry = geodb->data;
fseek(file, 0, SEEK_SET);
lines = 0;
while(fgets(line, sizeof(line), file) != NULL) {
lines++;
if(strlen(line) > 0) {
if(ap_regexec(preg, line, AP_MAX_REG_MATCH, ma, 0) == 0) {
line[ma[1].rm_eo] = '\0';
line[ma[2].rm_eo] = '\0';
line[ma[3].rm_eo] = '\0';
entry->start = atoll(&line[ma[1].rm_so]);
entry->end = atoll(&line[ma[2].rm_so]);
strncpy(entry->country, &line[ma[3].rm_so], 2);
if(last) {
if(entry->start < last->start) {
*msg = apr_psprintf(pool, "wrong order/lines not sorted (line %d)",
lines);
(*errors)++;
}
}
last = entry;
entry++;
}
}
}
fclose(file);
if(*errors == 0) {
return APR_SUCCESS;
} else {
return APR_INCOMPLETE;
}
}
/**
* Verifies if the string is a number
* @param num Number to test
* @param 1 if numeric (0 if not)
*/
static int qos_is_num(const char *num) {
int i = 0;
while(num[i]) {
if(!isdigit(num[i])) {
return 0;
}
i++;
}
return 1;
}
/**
* Helper for the status viewer (unsigned long to char).
*/
static void qos_collect_ip(request_rec *r, qos_srv_config *sconf,
apr_table_t *entries, int limit,
int html) {
int i = sconf->act->conn->conn_ip_len;
qs_ip_entry_t *conn_ip = sconf->act->conn->conn_ip;
apr_global_mutex_lock(sconf->act->lock); /* @CRT8 */
while(i) {
if(conn_ip->ip6[0] || conn_ip->ip6[1]) {
char *red = "style=\"background-color: rgb(240,153,155);\"";
if(html) {
apr_table_addn(entries, apr_psprintf(r->pool, "%s</td><td %s colspan=\"3\">%d",
qos_ip_long2str(r->pool, conn_ip->ip6),
((limit != -1) && conn_ip->counter >= limit) ? red : "",
conn_ip->counter), "");
} else {
apr_table_addn(entries, qos_ip_long2str(r->pool, conn_ip->ip6), apr_psprintf(r->pool, "%d", conn_ip->counter));
}
}
conn_ip++;
i--;
}
apr_global_mutex_unlock(sconf->act->lock); /* @CRT8 */
}
/**
* Count's the number of free ip entries (for the status viewer only)
*/
static int qos_count_free_ip(qos_srv_config *sconf) {
int c = sconf->act->conn->max_client;
int i = sconf->act->conn->conn_ip_len;
qs_ip_entry_t *conn_ip = sconf->act->conn->conn_ip;
apr_global_mutex_lock(sconf->act->lock); /* @CRT7 */
while(i) {
if((conn_ip->ip6[0] != 0) ||
(conn_ip->ip6[1] != 0)) {
c--;
}
conn_ip++;
i--;
}
apr_global_mutex_unlock(sconf->act->lock); /* @CRT7 */
return c > 0 ? c : 0;
}
/**
* Checks the subprocess_env table for
* event variable and returns its numeric value.
*
* @param r
* @param variable Name of the variable to lookup
* @return Number within the variable or 0 if not set
*/
static int get_qs_event(request_rec *r, const char *variable) {
const char *eventStr = apr_table_get(r->subprocess_env, variable);
if(eventStr == NULL) {
return 0;
}
if(qos_is_num(eventStr) && (strlen(eventStr) > 0)) {
int num = atoi(eventStr);
if(num <= 0) {
num = 1;
}
return num;
}
return 1;
}
/**
* adds an ip entry (insert or increment)
*
* @param sconf
* @param cconf Configuration record containing the ip table(s)
* @param e Pointer to the IP entry
* NOTE: we can't sort the list since the address of this pointer
* must not be change (we don't keep the lock)
* @return The number of connections open by this IP
*/
static int qos_inc_ip(qos_srv_config *sconf,
qs_conn_ctx *cconf, qs_ip_entry_t **e) {
int num = -1;
qs_ip_entry_t *free = NULL;
int i = cconf->sconf->act->conn->conn_ip_len / QS_MEM_SEG; // size of the array
int seqnum = (cconf->ip6[1] % QS_MEM_SEG) * i; // array offset
qs_ip_entry_t *conn_ip = cconf->sconf->act->conn->conn_ip;
conn_ip = &conn_ip[seqnum]; // address of the first entry
apr_global_mutex_lock(cconf->sconf->act->lock); /* @CRT1 */
// search the whole list (until we find an exiting entry for this ip)
while(i) {
if((conn_ip->ip6[0] == 0) &&
(conn_ip->ip6[1] == 0) &&
(free == NULL)) {
// first free entry
free = conn_ip;
}
if((conn_ip->ip6[0] == cconf->ip6[0]) &&
(conn_ip->ip6[1] == cconf->ip6[1])) {
// found an existing entry
conn_ip->counter++;
num = conn_ip->counter;
*e = conn_ip;
break;
}
conn_ip++;
i--;
}
if(num == -1) {
// no entry found, use the first free entry
if(free) {
free->ip6[0] = cconf->ip6[0];
free->ip6[1] = cconf->ip6[1];
free->counter++;
num = free->counter;
*e = free;
} else {
ap_log_error(APLOG_MARK, APLOG_ALERT, 0, sconf->base_server,
QOS_LOG_PFX(035)"QS_SrvMaxConn: no free IP slot available!"
" Check log for unclean child exit and consider"
" to do a graceful server restart if this condition persists."
" You might also increase the number of supported connections"
" using the 'QS_MaxClients' directive.");
QS_INC_EVENT_LOCKED(sconf, 35);
}
}
apr_global_mutex_unlock(cconf->sconf->act->lock); /* @CRT1 */
return num;
}
/**
* removes an ip entry (deletes/decrements)
*/
static void qos_dec_ip(qs_conn_ctx *cconf) {
int i = cconf->sconf->act->conn->conn_ip_len / QS_MEM_SEG;
int seqnum = (cconf->ip6[1] % QS_MEM_SEG) * i;
qs_ip_entry_t *conn_ip = cconf->sconf->act->conn->conn_ip;
conn_ip = &conn_ip[seqnum];
apr_global_mutex_lock(cconf->sconf->act->lock); /* @CRT2 */
while(i) {
if((conn_ip->ip6[0] == cconf->ip6[0]) &&
(conn_ip->ip6[1] == cconf->ip6[1])) {
// entry found, decrement and exit
conn_ip->counter--;
if(conn_ip->counter == 0) {
// entry is no longer used by this ip
conn_ip->ip6[0] = 0;
conn_ip->ip6[1] = 0;
conn_ip->error = 0;
}
break;
}
conn_ip++;
i--;
}
apr_global_mutex_unlock(cconf->sconf->act->lock); /* @CRT2 */
}
static apr_status_t qos_cleanup_inctx(void *p) {
qos_ifctx_t *inctx = p;
qos_srv_config *sconf = inctx->sconf;
#if APR_HAS_THREADS
if(sconf && sconf->inctx_t && !sconf->inctx_t->exit) {
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT25 */
inctx->status = QS_CONN_STATE_DESTROY;
apr_table_unset(sconf->inctx_t->table,
QS_INCTX_ID);
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT25 */
}
#endif
return APR_SUCCESS;
}
/**
* creates a new connection ctx (remember to set the socket, connection and timeout)
*/
static qos_ifctx_t *qos_create_ifctx(conn_rec *connection, qos_srv_config *sconf) {
conn_rec *c = connection;
char buf[128];
qos_ifctx_t *inctx = apr_pcalloc(c->pool, sizeof(qos_ifctx_t));
inctx->clientSocket = NULL;
inctx->status = QS_CONN_STATE_NEW;
inctx->cl_val = 0;
inctx->c = c;
inctx->r = NULL;
inctx->clientSocket = NULL;
inctx->time = 0;
inctx->nbytes = 0;
inctx->hasBytes = 0;
inctx->shutdown = 0;
inctx->disabled = 0;
inctx->lowrate = -1;
sprintf(buf, "%p", inctx);
inctx->id = apr_psprintf(c->pool, "%s%.16lx", buf, c->id);
inctx->sconf = sconf;
apr_pool_pre_cleanup_register(c->pool, inctx, qos_cleanup_inctx);
return inctx;
}
/**
* returns the context from the r->connection->input_filters
*/
static qos_ifctx_t *qos_get_ifctx(ap_filter_t *f) {
qos_ifctx_t *inctx = NULL;
while(f) {
if(strcmp(f->frec->name, "qos-in-filter") == 0) {
inctx = f->ctx;
break;
}
f = f->next;
}
return inctx;
}
static qs_conn_base_ctx *qos_create_conn_base_ctx(conn_rec *connection, qos_srv_config *sconf) {
conn_rec *c = connection;
qs_conn_base_ctx *base = apr_pcalloc(c->pool, sizeof(qs_conn_base_ctx));
base->cconf = NULL;
base->requests = 0;
base->c = c;
base->sconf = sconf;
base->clientSocket = NULL;
ap_set_module_config(c->conn_config, &qos_module, base);
apr_pool_pre_cleanup_register(c->pool, base, qos_base_cleanup_conn);
return base;
}
static qs_conn_base_ctx *qos_get_conn_base_ctx(conn_rec *connection) {
conn_rec *c = connection;
qs_conn_base_ctx *base = (qs_conn_base_ctx*)ap_get_module_config(c->conn_config, &qos_module);
return base;
}
/**
* send server error, used for connection errors
*/
static int qos_return_error_andclose(conn_rec *connection, apr_socket_t *socket) {
conn_rec *c = connection;
char *line = apr_pstrcat(c->pool, AP_SERVER_PROTOCOL, " ",
ap_get_status_line(500), CRLF CRLF, NULL);
apr_bucket *e = apr_bucket_pool_create(line, strlen(line), c->pool, c->bucket_alloc);
apr_bucket_brigade *bb = apr_brigade_create(c->pool, c->bucket_alloc);
c->keepalive = AP_CONN_CLOSE;
c->aborted = 1;
if(c->cs) {
c->cs->state = CONN_STATE_LINGER;
}
apr_table_set(c->notes, QS_CONN_ABORT, QS_CONN_ABORT);
if (m_forced_close == 0) {
return DECLINED;
}
//apr_brigade_cleanup(bb);
APR_BRIGADE_INSERT_HEAD(bb, e);
e = apr_bucket_flush_create(c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, e);
ap_pass_brigade(c->output_filters, bb);
// if(socket) {
// // speed up connection termination
// qos_ifctx_t *inctx = qos_get_ifctx(c->input_filters);
//#ifdef QS_INTERNAL_TEST
// struct timespec delay;
// delay.tv_sec = 0;
// delay.tv_nsec = 1000000; // 1ms to allow testing
// nanosleep(&delay, NULL);
//#endif
// apr_socket_shutdown(socket, APR_SHUTDOWN_READ);
// if(inctx) {
// qos_cleanup_inctx(inctx);
// }
// }
return HTTP_INTERNAL_SERVER_ERROR;
}
/**
* returns custom error page
*/
static int qos_error_response(request_rec *r, const char *error_page) {
if(r->subprocess_env) {
const char *v = apr_table_get(r->subprocess_env, "QS_ErrorPage");
if(v) {
error_page = v;
}
}
if(error_page) {
/* do (almost) the same as ap_die() does */
const char *error_notes;
r->status = m_retcode;
r->connection->keepalive = AP_CONN_CLOSE;
r->no_local_copy = 1;
apr_table_setn(r->subprocess_env, "REQUEST_METHOD", r->method);
if ((error_notes = apr_table_get(r->notes,
"error-notes")) != NULL) {
apr_table_setn(r->subprocess_env, "ERROR_NOTES", error_notes);
}
/* external or internal redirect */
if(strncasecmp(error_page, "http", 4) == 0) {
apr_table_set(r->headers_out, "Location", error_page);
return HTTP_MOVED_TEMPORARILY;
} else {
r->method = apr_pstrdup(r->pool, "GET");
r->method_number = M_GET;
ap_internal_redirect(error_page, r);
return DONE;
}
}
return DECLINED;
}
/**
* returns the matching regex with the lowest limitation
*/
static qs_acentry_t *qos_getrule_byregex(request_rec *r, qos_srv_config *sconf) {
qs_acentry_t *ret = NULL;
qs_actable_t *act = sconf->act;
qs_acentry_t *actEntry = act->entry;
int limit = -1;
while(actEntry) {
if((actEntry->event == NULL) && (actEntry->regex != NULL) && (actEntry->condition == NULL)) {
if((limit == -1) || (actEntry->limit < limit)) {
if(ap_regexec(actEntry->regex, r->unparsed_uri, 0, NULL, 0) == 0) {
if(limit == -1) {
ret = actEntry;
limit = actEntry->limit;
} else if(actEntry->limit < limit) {
ret = actEntry;
limit = actEntry->limit;
}
}
}
}
actEntry = actEntry->next;
}
return ret;
}
/**
* returns the matching conditional regex with the lowest limitation
*/
static qs_acentry_t *qos_getcondrule_byregex(request_rec *r, qos_srv_config *sconf) {
qs_acentry_t *ret = NULL;
qs_actable_t *act = sconf->act;
qs_acentry_t *actEntry = act->entry;
int limit = -1;
while(actEntry) {
if((actEntry->event == NULL) && (actEntry->regex != NULL) && (actEntry->condition != NULL)) {
if((limit == -1) || (actEntry->limit < limit)) {
if(ap_regexec(actEntry->regex, r->unparsed_uri, 0, NULL, 0) == 0) {
if(limit == -1) {
ret = actEntry;
limit = actEntry->limit;
} else if(actEntry->limit < limit) {
ret = actEntry;
limit = actEntry->limit;
}
}
}
}
actEntry = actEntry->next;
}
return ret;
}
/**
* returns the best matching location entry
*/
static qs_acentry_t *qos_getrule_bylocation(request_rec * r, qos_srv_config *sconf) {
qs_acentry_t *ret = NULL;
qs_actable_t *act = sconf->act;
qs_acentry_t *actEntry = act->entry;
int match_len = 0;
while(actEntry) {
if((actEntry->event == NULL) && (actEntry->regex == NULL)) {
/* per location limitation */
if(actEntry->url && (strncmp(actEntry->url, r->parsed_uri.path, actEntry->url_len) == 0)) {
/* best match */
if(actEntry->url_len > match_len) {
match_len = actEntry->url_len;
ret = actEntry;
}
}
}
actEntry = actEntry->next;
}
return ret;
}
/**
* checks for VIP user (may pass restrictions)
*/
static int qos_is_vip(request_rec *r, qos_srv_config *sconf) {
if(qos_verify_session(r, sconf)) {
apr_table_set(r->subprocess_env, QS_VipRequest, "yes");
apr_table_set(r->subprocess_env, QS_ISVIPREQ, "yes");
return 1;
}
if(r->subprocess_env) {
const char *v = apr_table_get(r->subprocess_env, QS_VipRequest);
if(v && (strcasecmp(v, "yes") == 0)) {
apr_table_set(r->subprocess_env, QS_ISVIPREQ, "yes");
return 1;
}
}
return 0;
}
/**
* writes the parp table to a single query line
*/
static const char *qos_parp_query(request_rec *r, apr_table_t *tl, const char *add) {
int add_len = 0;
char *query = NULL;
int len = 0;
char *p;
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(tl)->elts;
for(i = 0; i < apr_table_elts(tl)->nelts; i++) {
len = len +
(entry[i].key == NULL ? 0 : strlen(entry[i].key)) +
(entry[i].val == NULL ? 0 : strlen(entry[i].val)) +
2;
}
if(add && add[0]) {
add_len = strlen(add);
len = len + add_len + 1;
}
query = apr_pcalloc(r->pool, len + 2);
query[0] = '?';
if(add_len) {
memcpy(&query[1], add, add_len);
p = &query[add_len];
} else {
p = &query[1];
}
p[0] = '\0';
for(i = 0; i < apr_table_elts(tl)->nelts; i++) {
int l = strlen(entry[i].key);
if(p != &query[1]) {
p[0] = '&';
p++;
p[0] = '\0';
}
memcpy(p, entry[i].key, l);
p += l;
p[0] = '=';
p++;
l = strlen(entry[i].val);
memcpy(p, entry[i].val, l);
p += l;
p[0] = '\0';
}
apr_table_setn(r->notes, apr_pstrdup(r->pool, QS_PARP_QUERY), query);
return &query[1];
}
/* filter events */
static int qos_per_dir_event_rules(request_rec *r, qos_srv_config *sconf,
qos_dir_config *dconf) {
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(dconf->rfilter_table)->elts;
int i;
for(i = 0; i < apr_table_elts(dconf->rfilter_table)->nelts; i++) {
if(entry[i].key[0] == '+') {
int deny_rule = 0;
int ex = -1;
qos_rfilter_t *rfilter = (qos_rfilter_t *)entry[i].val;
if(rfilter->type == QS_DENY_EVENT) {
deny_rule = 1;
if(rfilter->text[0] == '!') {
if(apr_table_get(r->subprocess_env, &rfilter->text[1]) == NULL) {
ex = 0;
}
} else {
if(apr_table_get(r->subprocess_env, rfilter->text) != NULL) {
ex = 0;
}
}
}
if(deny_rule && (ex == 0)) {
int severity = rfilter->action == QS_DENY ? APLOG_ERR : APLOG_WARNING;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|severity, 0, r,
QOS_LOG_PFX(040)"access denied, %s rule id: %s (%s),"
" action=%s, c=%s, id=%s",
qos_rfilter_type2text(r->pool, rfilter->type),
rfilter->id,
rfilter->text,
(!sconf->log_only) && (rfilter->action == QS_DENY) ? "deny" : "log only",
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "040"));
QS_INC_EVENT(sconf, 40);
if(rfilter->action == QS_DENY) {
return HTTP_FORBIDDEN;
}
}
}
}
return APR_SUCCESS;
}
/* json parser start ------------------------------------------------------- */
#define QOS_J_ERROR "HTTP_BAD_REQUEST QOS JSON PARSER: FORMAT ERROR"
#define QOS_j_RECURSION 80
static int j_val(apr_pool_t *pool, char **val, apr_table_t *tl, char *name, int rec);
static char *j_escape_url(apr_pool_t *pool, const char *c) {
char buf[4];
char special[] = " \t()<>@,;:\\/[]?={}\"'&%+";
char *r = apr_pcalloc(pool, 3 * strlen(c));
const char *p = c;
int i = 0;
while(p && p[0]) {
char c = p[0];
if(!apr_isprint(c) || strchr(special, c)) {
sprintf(buf, "%02x", p[0]);
r[i] = '%'; i++;
r[i] = buf[0]; i++;
r[i] = buf[1]; i++;
} else {
r[i] = c;
i++;
}
p++;
}
return r;
}
static char *j_strchr(char *data, char d) {
char *q = data;
if(!q) {
return NULL;
}
if(q[0] == d) {
return q;
}
while(q[0]) {
if((q[0] == d) && (q[-1] != '\\')) {
return q;
}
q++;
}
return NULL;
}
static char *j_skip(char *in) {
if(!in) return NULL;
while(in[0] && ((in[0] == ' ') ||
(in[0] == '\t') ||
(in[0] == '\r') ||
(in[0] == '\n') ||
(in[0] == '\f'))) {
in++;
}
return in;
}
static int j_string(apr_pool_t *pool, char **val, apr_table_t *tl, char *name, char **n) {
char *d = *val;
char *v = d;
char *end = j_strchr(d, '"');
if(!end) {
apr_table_add(tl, QOS_J_ERROR, "error while parsing string (no ending double quote)");
return HTTP_BAD_REQUEST;
}
end[0] = '\0';
end++;
*val = j_skip(end);
/* TODO, improve string format validation */
while(v[0]) {
if(v[0] < ' ') {
apr_table_add(tl, QOS_J_ERROR, "error while parsing string (invalid character)");
return HTTP_BAD_REQUEST;
}
v++;
}
*n = d;
return APR_SUCCESS;
}
static int j_num(apr_pool_t *pool, char **val, apr_table_t *tl, char *name, char **n) {
char *s = *val;
char *d = *val;
while(d && ((d[0] >= '0' && d[0] <= '9') ||
d[0] == '.' ||
d[0] == 'e' ||
d[0] == 'E' ||
d[0] == '+' ||
d[0] == '-')) {
d++;
}
*n = apr_pstrndup(pool, s, d-s);
*val = d;
return APR_SUCCESS;
}
static int j_obj(apr_pool_t *pool, char **val, apr_table_t *tl, char *name, int rec) {
char *d = j_skip(*val);
int rc;
while(d && d[0]) {
if(*d != '\"') {
apr_table_add(tl, QOS_J_ERROR, "error while parsing object (missing string)");
return HTTP_BAD_REQUEST;
} else {
/* list of string ":" value pairs (sepated by ',') */
char *v = NULL;
char *thisname;
d++;
rc = j_string(pool, &d, tl, name, &v);
if(rc != APR_SUCCESS) {
return rc;
}
thisname = apr_pstrcat(pool, name, "_" , v, NULL);
d = j_skip(d);
if(!d || d[0] != ':') {
apr_table_add(tl, QOS_J_ERROR, "error while parsing object (missing value/wrong delimiter)");
return HTTP_BAD_REQUEST;
}
d++;
rc = j_val(pool, &d, tl, thisname, rec);
if(rc != APR_SUCCESS) {
return rc;
}
d = j_skip(d);
if(!d) {
apr_table_add(tl, QOS_J_ERROR, "error while parsing object (unexpected end)");
return HTTP_BAD_REQUEST;
}
if(d[0] == '}') {
d++;
*val = d;
return APR_SUCCESS;
} else if(d[0] == ',') {
d = j_strchr(d, '"');
} else {
apr_table_add(tl, QOS_J_ERROR, "error while parsing object (unexpected end/wrong delimiter)");
return HTTP_BAD_REQUEST;
}
}
}
return APR_SUCCESS;
}
static int j_ar(apr_pool_t *pool, char **val, apr_table_t *tl, char *name, int rec) {
char *d = j_skip(*val);
int rc;
int index = 0;
while(d && d[0]) {
rc = j_val(pool, &d, tl, apr_psprintf(pool, "%s%d", name, index), rec);
if(rc != APR_SUCCESS) {
return rc;
}
d = j_skip(d);
if(!d) {
apr_table_add(tl, QOS_J_ERROR, "error while parsing array (unexpected end)");
return HTTP_BAD_REQUEST;
}
if(d[0] == ']') {
d++;
*val = d;
return APR_SUCCESS;
} else if(d[0] == ',') {
d++;
d = j_skip(d);
} else {
apr_table_add(tl, QOS_J_ERROR, "error while parsing array (unexpected end/wrong delimiter)");
return HTTP_BAD_REQUEST;
}
index++;
}
return APR_SUCCESS;
}
static int j_val(apr_pool_t *pool, char **val, apr_table_t *tl, char *name, int rec) {
char *d = j_skip(*val);
int rc = APR_SUCCESS;
rec++;
if(rec > QOS_j_RECURSION) {
apr_table_add(tl, QOS_J_ERROR, "error while parsing string (reached recursion limit)");
return HTTP_BAD_REQUEST;
}
/* either object, array, string, number, "true", "false", or "null" */
if(d[0] == '{') {
d++;
rc = j_obj(pool, &d, tl, apr_pstrcat(pool, name, "_o", NULL), rec);
} else if(d[0] == '[') {
d++;
rc = j_ar(pool, &d, tl, apr_pstrcat(pool, name, "_a", NULL), rec);
} else if(strncmp(d,"null",4) == 0) {
d+=4;
apr_table_add(tl, apr_pstrcat(pool, j_escape_url(pool, name), "_b", NULL), "null");
} else if(strncmp(d,"true",4) == 0) {
apr_table_add(tl, apr_pstrcat(pool, j_escape_url(pool, name), "_b", NULL), "true");
d+=4;
} else if(strncmp(d,"false",5) == 0) {
apr_table_add(tl, apr_pstrcat(pool, j_escape_url(pool, name), "_b", NULL), "false");
d+=5;
} else if(*d == '-' || (*d >= '0' && *d <= '9')) {
char *n = apr_pstrcat(pool, name, "_n", NULL);
char *v = NULL;
rc = j_num(pool, &d, tl, n, &v);
if(rc == APR_SUCCESS) {
apr_table_addn(tl, j_escape_url(pool, n), j_escape_url(pool, v));
}
} else if(*d == '\"') {
char *n = apr_pstrcat(pool, name, "_v", NULL);
char *v = NULL;
d++;
rc = j_string(pool, &d, tl, n, &v);
if(rc == APR_SUCCESS) {
apr_table_addn(tl, j_escape_url(pool, n), j_escape_url(pool, v));
}
} else {
/* error */
apr_table_add(tl, QOS_J_ERROR, "error while parsing value (invalid type)");
return HTTP_BAD_REQUEST;
}
if(rc != APR_SUCCESS) {
return rc;
}
*val = d;
rec--;
return APR_SUCCESS;
}
/* json parser end --------------------------------------------------------- */
/**
* Process json data retrieved from parp (request body)
* @param r
* @param dconf
* @param query Query to add data
* @param msg Error message if paring fails
* @return APR_SUCCESS if processed without errors.
*/
static int qos_json(request_rec *r, qos_dir_config *dconf, const char **query, const char **msg) {
const char *contenttype = apr_table_get(r->headers_in, "Content-Type");
if(contenttype && (strncasecmp(contenttype, "application/json", 16) == 0)) {
apr_size_t len = 0;
const char *data = NULL;
/* check if parp has body data to process (requires "PARP_BodyData application/json")
or if the json message is stored within the query */
if(qos_parp_body_data_fn) {
data = qos_parp_body_data_fn(r, &len);
}
if(data == NULL) {
data = *query;
if(data && (data[0] == '[' || data[0] == '{')) {
int escerr = 0;
char *copyq = apr_pstrdup(r->pool, data);
*query = NULL;
// the query needs to be unescaped before getting parsed
len = qos_unescaping(copyq, dconf->dec_mode, &escerr);
#ifdef QS_MOD_EXT_HOOKS
qos_run_path_decode_hook(r, ©q, &len);
#endif
data = copyq;
if(strlen(data) != len) {
*msg = apr_pstrdup(r->pool, "null character within data structure in query");
return HTTP_BAD_REQUEST;
}
} else {
// does not look like a json structure (strict)
data = NULL;
}
}
if(data && (len > 0)) {
char *value = apr_pstrndup(r->pool, data, len);
apr_table_t *tl = apr_table_make(r->pool, 200);
int rc;
if(strlen(value) != len) {
*msg = apr_pstrdup(r->pool, "null character within data structure");
return HTTP_BAD_REQUEST;
}
rc = j_val(r->pool, &value, tl, "J", 0);
if(rc != APR_SUCCESS) {
*msg = apr_table_get(tl, QOS_J_ERROR);
apr_table_unset(tl, QOS_J_ERROR);
return rc;
}
if(value && value[0]) {
value = j_skip(value);
if(value && value[0]) {
/* error, there is still some data */
*msg = apr_pstrdup(r->pool, "more than one element");
}
}
*query = qos_parp_query(r, tl, *query);
if(*query) {
apr_table_setn(r->notes, apr_pstrdup(r->pool, QS_PARP_Q), *query);
}
}
}
return APR_SUCCESS;
}
/**
* processes the per location rules QS_Permit* and QS_Deny*
*/
static int qos_per_dir_rules(request_rec *r, qos_srv_config *sconf,
qos_dir_config *dconf) {
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(dconf->rfilter_table)->elts;
int i;
char *path = apr_pstrdup(r->pool, r->parsed_uri.path);
char *query = NULL;
char *fragment = NULL;
char *request_line = apr_pstrdup(r->pool, r->the_request);
char *uri = path;
int request_line_len;
int path_len;
int query_len = 0;
int fragment_len = 0;
int uri_len;
int permit_rule = 0;
int permit_rule_match = 0;
int permit_rule_action = QS_DENY;
int escerr = 0;
request_line_len = qos_unescaping(request_line, dconf->dec_mode, &escerr);
path_len = qos_unescaping(path, dconf->dec_mode, &escerr);
#ifdef QS_MOD_EXT_HOOKS
qos_run_path_decode_hook(r, &path, &path_len);
#endif
uri_len = path_len;
if(dconf->bodyfilter_p == 1 || dconf->bodyfilter_d == 1) {
const char *q = apr_table_get(r->notes, QS_PARP_Q);
if((q == NULL) && qos_parp_hp_table_fn) {
const char *msg = NULL;
apr_table_t *tl = qos_parp_hp_table_fn(r);
if(tl) {
if(apr_table_elts(tl)->nelts > 0) {
q = qos_parp_query(r, tl, NULL);
if(q) {
apr_table_setn(r->notes, apr_pstrdup(r->pool, QS_PARP_Q), q);
}
}
} else {
/* no table provided by mod_parp (unsupported content type?),
use query string if available */
if(r->parsed_uri.query) {
q = r->parsed_uri.query;
}
}
if(qos_json(r, dconf, &q, &msg) != APR_SUCCESS) {
/* parser error */
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(048)"access denied, invalid JSON syntax (%s),"
" action=%s, c=%s, id=%s",
msg ? msg : "-",
sconf->log_only ? "log only" : "deny",
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "048"));
QS_INC_EVENT(sconf, 48);
return HTTP_FORBIDDEN;
}
}
if(q) {
/* prepare unescaped body query (parp) */
char *q1 = apr_pstrdup(r->pool, q);
int q1_len = 0;
q1 = apr_pstrdup(r->pool, q);
q1_len = qos_unescaping(q1, dconf->dec_mode, &escerr);
#ifdef QS_MOD_EXT_HOOKS
qos_run_query_decode_hook(r, &q1, &q1_len);
#endif
if(dconf->bodyfilter_d == 1) {
/* use body for query deny filter */
query = q1;
query_len = q1_len;
} else {
/* don't use body for query deny filter */
if(r->parsed_uri.query) {
query = apr_pstrdup(r->pool, r->parsed_uri.query);
query_len = qos_unescaping(query, dconf->dec_mode, &escerr);
#ifdef QS_MOD_EXT_HOOKS
qos_run_query_decode_hook(r, &query, &query_len);
#endif
}
}
if(dconf->bodyfilter_p != 1) {
/* don' use body for permit filter */
if(r->parsed_uri.query) {
q1 = apr_pstrdup(r->pool, r->parsed_uri.query);
q1_len = qos_unescaping(q1, dconf->dec_mode, &escerr);
#ifdef QS_MOD_EXT_HOOKS
qos_run_query_decode_hook(r, &q1, &q1_len);
#endif
} else {
q1 = NULL;
q1_len = 0;
}
}
if(q1) {
uri = apr_pcalloc(r->pool, path_len + 1 + q1_len + 1);
memcpy(uri, path, path_len);
uri[path_len] = '?';
memcpy(&uri[path_len+1], q1, q1_len);
uri[path_len+1+q1_len] = '\0';
uri_len = path_len + 1 + q1_len;
}
}
} else {
if(r->parsed_uri.query) {
query = apr_pstrdup(r->pool, r->parsed_uri.query);
query_len = qos_unescaping(query, dconf->dec_mode, &escerr);
#ifdef QS_MOD_EXT_HOOKS
qos_run_query_decode_hook(r, &query, &query_len);
#endif
uri = apr_pcalloc(r->pool, path_len + 1 + query_len + 1);
memcpy(uri, path, path_len);
uri[path_len] = '?';
memcpy(&uri[path_len+1], query, query_len);
uri[path_len+1+query_len] = '\0';
uri_len = path_len + 1 + query_len;
}
}
if(r->parsed_uri.fragment) {
fragment = apr_pstrdup(r->pool, r->parsed_uri.fragment);
fragment_len = qos_unescaping(fragment, dconf->dec_mode, &escerr);
uri = apr_pcalloc(r->pool, path_len + 1 + fragment_len + 1);
memcpy(uri, path, path_len);
uri[path_len] = '?';
memcpy(&uri[path_len+1], fragment, fragment_len);
uri[path_len+1+fragment_len] = '\0';
uri_len = path_len + 1 + fragment_len;
}
if(escerr > 0 && (dconf->urldecoding < QS_OFF_DEFAULT)) {
int severity = dconf->urldecoding == QS_DENY ? APLOG_ERR : APLOG_WARNING;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|severity, 0, r,
QOS_LOG_PFX(046)"access denied, invalid url encoding, action=%s, c=%s, id=%s",
(!sconf->log_only) && (dconf->urldecoding == QS_DENY) ? "deny" : "log only",
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "046"));
QS_INC_EVENT(sconf, 46);
if(dconf->urldecoding == QS_DENY) {
return HTTP_FORBIDDEN;
}
}
/* process deny- and allow- list rules in one loop */
for(i = 0; i < apr_table_elts(dconf->rfilter_table)->nelts; i++) {
if(entry[i].key[0] == '+') {
int deny_rule = 0;
int ex = -1;
qos_rfilter_t *rfilter = (qos_rfilter_t *)entry[i].val;
if(rfilter->type == QS_DENY_REQUEST_LINE) {
deny_rule = 1;
ex = qos_regexec_len(r->pool, rfilter->preg, request_line, request_line_len);
} else if(rfilter->type == QS_DENY_PATH) {
deny_rule = 1;
ex = qos_regexec_len(r->pool, rfilter->preg, path, path_len);
} else if(rfilter->type == QS_DENY_QUERY) {
deny_rule = 1;
ex = qos_regexec_len(r->pool, rfilter->preg, query, query_len);
} else if(rfilter->type == QS_DENY_EVENT) {
/* event rules are processed separately */
} else {
permit_rule = 1;
ex = qos_regexec_len(r->pool, rfilter->preg, uri, uri_len);
permit_rule_action = rfilter->action;
if(ex == 0) {
permit_rule_match = 1;
}
}
if(deny_rule && (ex == 0)) {
int severity = rfilter->action == QS_DENY ? APLOG_ERR : APLOG_WARNING;
apr_table_set(r->subprocess_env, QS_RuleId, rfilter->id);
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|severity, 0, r,
QOS_LOG_PFX(040)"access denied, %s rule id: %s (%s),"
" action=%s, c=%s, id=%s",
qos_rfilter_type2text(r->pool, rfilter->type),
rfilter->id,
rfilter->text,
(!sconf->log_only) && (rfilter->action == QS_DENY) ? "deny" : "log only",
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "040"));
QS_INC_EVENT(sconf, 40);
if(rfilter->action == QS_DENY) {
return HTTP_FORBIDDEN;
}
}
}
}
if(permit_rule && !permit_rule_match) {
int severity = permit_rule_action == QS_DENY ? APLOG_ERR : APLOG_WARNING;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|severity, 0, r,
QOS_LOG_PFX(041)"access denied, no permit rule match, action=%s, c=%s, id=%s",
(!sconf->log_only) && (permit_rule_action == QS_DENY) ? "deny" : "log only",
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "041"));
QS_INC_EVENT(sconf, 41);
if(permit_rule_action == QS_DENY) {
return HTTP_FORBIDDEN;
}
}
return APR_SUCCESS;
}
/**
* request/response header filter, drops headers which are not allowed
*/
static int qos_header_filter(request_rec *r, qos_srv_config *sconf,
apr_table_t *headers, const char *type,
apr_table_t *hfilter_table,
qs_headerfilter_mode_e mode) {
apr_table_t *delete = apr_table_make(r->pool, 1);
apr_table_t *reason = NULL;
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(headers)->elts;
for(i = 0; i < apr_table_elts(headers)->nelts; i++) {
qos_fhlt_r_t *he = (qos_fhlt_r_t *)apr_table_get(hfilter_table, entry[i].key);
int denied = 0;
if(he) {
if(mode != QS_HEADERFILTER_SIZE_ONLY) {
if(ap_regexec(he->preg, entry[i].val, 0, NULL, 0) != 0) {
denied = 1;
}
}
if(strlen(entry[i].val) > he->size) {
denied += 2;
}
if(denied) {
char *pattern = apr_psprintf(r->pool, "(pattern=%s, max. length=%d)",
he->text, he->size);
if(he->action == QS_FLT_ACTION_DENY) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(043)"access denied%s, %s header: \'%s: %s\', %s, c=%s, id=%s",
sconf->log_only ? " (log only)" : "",
type,
entry[i].key, entry[i].val,
pattern,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "043"));
QS_INC_EVENT(sconf, 43);
return HTTP_FORBIDDEN;
}
if(reason == NULL) {
reason = apr_table_make(r->pool, 1);
}
apr_table_add(delete, entry[i].key, entry[i].val);
apr_table_add(reason, entry[i].key, pattern);
}
} else {
if(reason == NULL) {
reason = apr_table_make(r->pool, 1);
}
apr_table_add(delete, entry[i].key, entry[i].val);
apr_table_add(reason, entry[i].key, "(no rule available)");
}
}
entry = (apr_table_entry_t *)apr_table_elts(delete)->elts;
for(i = 0; i < apr_table_elts(delete)->nelts; i++) {
if(mode != QS_HEADERFILTER_SILENT) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, r,
QOS_LOG_PFX(042)"drop %s header%s: \'%s: %s\', %s, c=%s, id=%s",
type,
sconf->log_only ? " (log only)" : "",
entry[i].key, entry[i].val,
apr_table_get(reason, entry[i].key),
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "042"));
QS_INC_EVENT(sconf, 42);
}
if(!sconf->log_only) {
apr_table_unset(headers, entry[i].key);
}
}
return APR_SUCCESS;
}
/**
* returns list of all query name=value pairs
*/
static apr_table_t *qos_get_query_table(request_rec *r) {
apr_table_t *av = apr_table_make(r->pool, 2);
if(r->parsed_uri.query) {
const char *q = apr_pstrdup(r->pool, r->parsed_uri.query);
while(q && q[0]) {
const char *t = ap_getword(r->pool, &q, '&');
const char *name = ap_getword(r->pool, &t, '=');
const char *value = t;
if(name && (strlen(name) > 0)) {
if(value && (strlen(value) > 0)) {
apr_table_add(av, name, value);
} else if((strlen(name) > 0)) {
apr_table_add(av, name, "");
}
}
}
}
return av;
}
/** add "\n" */
#define QOS_ALERT_LINE_LEN 65
static char *qos_crline(request_rec *r, const char *line) {
char *string = "";
const char *pos = line;
while(pos && pos[0]) {
int len = strlen(pos);
if(len > QOS_ALERT_LINE_LEN) {
string = apr_pstrcat(r->pool, string,
apr_psprintf(r->pool, "%.*s", QOS_ALERT_LINE_LEN, pos), "\n", NULL);
pos = &pos[QOS_ALERT_LINE_LEN];
} else {
string = apr_pstrcat(r->pool, string, pos, NULL);
pos = NULL;
}
}
return string;
}
/**
* calculates the rec/sec block rate
*/
static void qos_cal_req_sec(qos_srv_config *sconf, request_rec *r, qs_acentry_t *e) {
if(e->req_per_sec > e->req_per_sec_limit) {
int factor = ((e->req_per_sec * 100) / e->req_per_sec_limit) - 100;
e->req_per_sec_block_rate = e->req_per_sec_block_rate + factor;
if(e->req_per_sec_block_rate > QS_MAX_DELAY/1000) {
e->req_per_sec_block_rate = QS_MAX_DELAY/1000;
}
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, r,
QOS_LOG_PFX(050)"request rate limit, rule: %s(%ld), req/sec=%ld,"
" delay=%dms%s",
e->url, e->req_per_sec_limit,
e->req_per_sec, e->req_per_sec_block_rate,
e->req_per_sec_block_rate == QS_MAX_DELAY/1000 ? " (max)" : "");
QS_INC_EVENT(sconf, 50);
} else if(e->req_per_sec_block_rate > 0) {
if(e->req_per_sec_block_rate < 50) {
e->req_per_sec_block_rate = 0;
} else {
int factor = e->req_per_sec_block_rate / 4;
e->req_per_sec_block_rate = e->req_per_sec_block_rate - factor;
}
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, r,
QOS_LOG_PFX(051)"request rate limit, rule: %s(%ld), req/sec=%ld,"
" delay=%dms",
e->url, e->req_per_sec_limit,
e->req_per_sec, e->req_per_sec_block_rate);
QS_INC_EVENT(sconf, 51);
}
}
/**
* QS_DenyEvent enforcement at header parser
* @param r
* @param sconf
* @param dconf
# returns DECLINED if no events has been detected
*/
static int qos_hp_event_deny_filter(request_rec *r, qos_srv_config *sconf, qos_dir_config *dconf) {
apr_status_t rv = qos_per_dir_event_rules(r, sconf, dconf);
if(rv != APR_SUCCESS) {
int rc;
const char *error_page = sconf->error_page;
qs_set_evmsg(r, "D;");
if(!sconf->log_only) {
rc = qos_error_response(r, error_page);
if((rc == DONE) || (rc == HTTP_MOVED_TEMPORARILY)) {
return rc;
}
return rv;
}
}
return DECLINED;
}
/**
* QS_Permit* / QS_Deny* enforcement at header parser
* @param r
* @param sconf
* @param dconf
* @return
*/
static int qos_hp_filter(request_rec *r, qos_srv_config *sconf, qos_dir_config *dconf) {
apr_status_t rv = APR_SUCCESS;
if(sconf->milestones) {
char *value = qos_get_remove_cookie(r, QOS_MILESTONE_COOKIE);
rv = qos_verify_milestone(r, sconf, value);
}
if((rv == APR_SUCCESS) && (apr_table_elts(dconf->rfilter_table)->nelts > 0)) {
rv = qos_per_dir_rules(r, sconf, dconf);
}
if(rv != APR_SUCCESS) {
int rc;
const char *error_page = sconf->error_page;
qs_set_evmsg(r, "D;");
if(!sconf->log_only) {
rc = qos_error_response(r, error_page);
if((rc == DONE) || (rc == HTTP_MOVED_TEMPORARILY)) {
return rc;
}
return rv;
}
}
return DECLINED;
}
/**
* QS_SetEnvRes (outfilter)
* Detects events at response time.
*/
static void qos_setenvres(request_rec *r, qos_srv_config *sconf) {
ap_regmatch_t regm[AP_MAX_REG_MATCH];
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(sconf->setenvres_t)->elts;
for(i = 0; i < apr_table_elts(sconf->setenvres_t)->nelts; i++) {
const char *val = apr_table_get(r->subprocess_env, entry[i].key);
if(val) {
qos_pregval_t *pregval = (qos_pregval_t *)entry[i].val;
if(ap_regexec(pregval->preg, val, AP_MAX_REG_MATCH, regm, 0) == 0) {
if(pregval->value) {
char *replaced = ap_pregsub(r->pool, pregval->value, val, AP_MAX_REG_MATCH, regm);
apr_table_set(r->subprocess_env, pregval->name, replaced);
} else {
apr_table_set(r->subprocess_env, pregval->name, "1");
}
}
}
}
}
/**
* QS_SetEnvResHeader(Match) (outfilter)
* Matches response headers and sets an event on match.
* @param r
* @param sconf
*/
static void qos_setenvresheader(request_rec *r, qos_srv_config *sconf) {
apr_table_t *headers = r->headers_out;
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(sconf->setenvresheader_t)->elts;
apr_table_entry_t *entryMatch = (apr_table_entry_t *)apr_table_elts(sconf->setenvresheadermatch_t)->elts;
while(headers) {
for(i = 0; i < apr_table_elts(sconf->setenvresheadermatch_t)->nelts; i++) {
const char *val = apr_table_get(headers, entryMatch[i].key);
if(val) {
ap_regex_t *preg = (ap_regex_t *)entryMatch[i].val;
if(ap_regexec(preg, val, 0, NULL, 0) == 0) {
apr_table_set(r->subprocess_env, entryMatch[i].key, val);
}
}
}
for(i = 0; i < apr_table_elts(sconf->setenvresheader_t)->nelts; i++) {
const char *val = apr_table_get(headers, entry[i].key);
if(val) {
apr_table_set(r->subprocess_env, entry[i].key, val);
if(strcasecmp(entry[i].val, "drop") == 0) {
apr_table_unset(headers, entry[i].key);
}
}
}
if(headers == r->headers_out) {
headers = r->err_headers_out;
} else {
headers = NULL;
}
}
}
/**
* QS_SetEnvIfStatus
* Match response status code
*
* @param r
* @param sconf
* @param dconf
*/
static void qos_setenvstatus(request_rec *r, qos_srv_config *sconf, qos_dir_config *dconf) {
char *code = apr_psprintf(r->pool, "%d", r->status);
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(sconf->setenvstatus_t)->elts;
for(i = 0; i < apr_table_elts(sconf->setenvstatus_t)->nelts; i++) {
if(strcmp(entry[i].key, code) == 0) {
char *var = apr_pstrdup(r->pool, entry[i].val);
char *value = strchr(var, '=');
if(value) {
// a value has been defined
value[0] = '\0';
value++;
} else {
if(strcmp(var, QS_BLOCK) == 0) {
// QS_Block optionally defines the weight for error codes
value = apr_pstrdup(r->pool, "1");
} else {
// by default, the value becomes the status code
value = code;
}
}
apr_table_set(r->subprocess_env, var, value);
}
}
if(dconf) {
entry = (apr_table_entry_t *)apr_table_elts(dconf->setenvstatus_t)->elts;
for(i = 0; i < apr_table_elts(dconf->setenvstatus_t)->nelts; i++) {
if(strcmp(entry[i].key, code) == 0) {
char *var = apr_pstrdup(r->pool, entry[i].val);
char *value = strchr(var, '=');
if(value) {
value[0] = '\0';
value++;
} else {
value = code;
}
apr_table_set(r->subprocess_env, var, value);
}
}
}
}
/**
* Returns the best matching server name (the configured ServerName,
* supporting ServerAlias directive or the value provided by the
* caller (usually the Host header)).
*
* @param r
* @param server_hostname Host name the client expects (Host header) <host>[:<port>]
* @param match Indicates if the provide name matches the ServerName/ServerAlias
* @return hostname
*/
static char *qos_server_alias(request_rec *r, const char *server_hostname, int *match) {
char *server = apr_pstrdup(r->pool, r->server->server_hostname); // default (hostname, no port)
*match = 0;
if(server_hostname) {
const char *search = server_hostname;
char *port = strchr(search, ':');
if(port) {
// without the port
search = apr_pstrndup(r->pool, search, port - search);
}
if(strcasecmp(search, r->server->server_hostname) == 0) {
/* match ServerName */
// we already did: server = apr_pstrdup(r->pool, r->server->server_hostname);
*match = 1;
} else if(r->server->names) {
int i;
apr_array_header_t *names = r->server->names;
char **name = (char **)names->elts;
for(i = 0; i < names->nelts; ++i) {
if(!name[i]) continue;
if(strcasecmp(search, name[i]) == 0) {
/* match ServerAlias */
server = apr_pstrdup(r->pool, name[i]);
*match = 1;
}
}
} else if(r->server->wild_names) {
int i;
apr_array_header_t *names = r->server->wild_names;
char **name = (char **)names->elts;
for(i = 0; i < names->nelts; ++i) {
if(!name[i]) continue;
if(!ap_strcasecmp_match(search, name[i])) {
/* match ServerAlias using wildcards */
server = apr_pstrdup(r->pool, search);
*match = 1;
}
}
}
}
return server;
}
/**
* Returns the url to this server, e.g. https://server1 or http://server1:8080
* used for redirects.
*
* @param r
* @return schema/hostname
*/
static char *qos_this_host(request_rec *r) {
const char *hostport = apr_table_get(r->headers_in, "Host");
int port = 0;
int ssl = 0;
int default_port;
const char *server_hostname = r->server->server_hostname;
if(qos_is_https) {
ssl = qos_is_https(r->connection);
}
if(hostport) {
char *p;
int match;
hostport = apr_pstrdup(r->pool, hostport);
if((p = strchr(hostport, ':')) != NULL) {
p[0] = '\0';
p++;
port = atoi(p);
}
server_hostname = qos_server_alias(r, hostport, &match);
}
if(port == 0) {
// pref. vhost
port = r->server->addrs->host_port;
}
if(port == 0) {
// main srv
port = r->server->port;
}
default_port = ssl ? 443 : 80;
if(port == default_port) {
return apr_psprintf(r->pool, "%s%s",
ssl ? "https://" : "http://",
server_hostname);
}
return apr_psprintf(r->pool, "%s%s:%d",
ssl ? "https://" : "http://",
server_hostname,
port);
}
/**
* Enables mod_parp if mod_qos requires access to the request body.
* @param r
*/
static void qos_enable_parp(request_rec *r) {
const char *ct = apr_table_get(r->headers_in, "Content-Type");
if(ct) {
if(ap_strcasestr(ct, "application/x-www-form-urlencoded") ||
ap_strcasestr(ct, "multipart/form-data") ||
ap_strcasestr(ct, "multipart/mixed") ||
ap_strcasestr(ct, "application/json")) {
apr_table_set(r->subprocess_env, "parp", "mod_qos");
}
}
}
/**
* Generic request validation / sanity check:
* We ensure to have at least a valid, decoded request uri received.
* (no further uri validation required in your code)
* @param r
* @param sconf
* @return HTTP_BAD_REQUEST for requests which may not be processed by mod_qos, otherwise
* APR_SUCCESS
*/
static apr_status_t qos_request_check(request_rec *r, qos_srv_config *sconf) {
if((r->unparsed_uri == NULL) || (r->parsed_uri.path == NULL)) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(045)"access denied, invalid request line:"
" can't parse uri, c=%s, id=%s",
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "045"));
QS_INC_EVENT(sconf, 45);
return HTTP_BAD_REQUEST;
}
return APR_SUCCESS;
}
/**
* QS_SetEnvIfParp (prr), enable parp
*/
static apr_status_t qos_parp_prr(request_rec *r, qos_srv_config *sconf) {
if(apr_table_elts(sconf->setenvifparp_t)->nelts > 0) {
qos_enable_parp(r);
}
return DECLINED;
}
/**
* QS_SetEnvIfQuery/QS_SetEnvIfParp
*/
static void qos_setenvif_ex(request_rec *r, const char *query, apr_table_t *table_setenvif) {
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(table_setenvif)->elts;
for(i = 0; i < apr_table_elts(table_setenvif)->nelts; i++) {
qos_setenvifquery_t *setenvif = (qos_setenvifquery_t *)entry[i].val;
char *name = setenvif->name;
ap_regmatch_t regm[AP_MAX_REG_MATCH];
if(ap_regexec(setenvif->preg, query, AP_MAX_REG_MATCH, regm, 0) == 0) {
if(name[0] == '!') {
apr_table_unset(r->subprocess_env, &name[1]);
} else {
char *replaced = "";
if(setenvif->value) {
replaced = ap_pregsub(r->pool, setenvif->value, query, AP_MAX_REG_MATCH, regm);
}
apr_table_set(r->subprocess_env, name, replaced);
}
}
}
}
/**
* Process body events (QS_SetEnvIfBody) and sets the r->subprocess_env variables
* @param r
* @param sconf
*/
static void qos_parp_hp_body(request_rec *r, qos_srv_config *sconf) {
apr_size_t len;
const char *data = qos_parp_body_data_fn(r, &len);
if(data && (len > 0)) {
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(sconf->setenvifparpbody_t)->elts;
#if (AP_SERVER_MINORVERSION_NUMBER < 4) && defined QS_INTERNAL_TEST
// Apache 2.2 is no longer supported (test only)
char *tmpData = apr_palloc(r->pool, len + 1);
memcpy(tmpData, data, len);
tmpData[len] = '\0';
data = (const char *)tmpData;
#endif
for(i = 0; i < apr_table_elts(sconf->setenvifparpbody_t)->nelts; i++) {
qos_setenvifparpbody_t *setenvif = (qos_setenvifparpbody_t *)entry[i].val;
ap_regmatch_t regm[AP_MAX_REG_MATCH];
#if (AP_SERVER_MINORVERSION_NUMBER < 4) && defined QS_INTERNAL_TEST
if(ap_regexec(setenvif->pregx, data, AP_MAX_REG_MATCH, regm, 0) == 0) { // won't work for null chars!
#else
if(ap_regexec_len(setenvif->pregx, data, len, AP_MAX_REG_MATCH, regm, 0) == 0) {
#endif
char *name = setenvif->name;
if(name[0] == '!') {
apr_table_unset(r->subprocess_env, &name[1]);
} else {
char *value = apr_pstrdup(r->pool, setenvif->value);
char *p = strstr(value, "$1");
if(p) {
char *c = apr_pstrndup(r->pool, &data[regm[0].rm_so], regm[0].rm_eo - regm[0].rm_so);
if(ap_regexec(setenvif->pregx, c, AP_MAX_REG_MATCH, regm, 0) == 0) {
value = ap_pregsub(r->pool, value, c, AP_MAX_REG_MATCH, regm);
}
}
apr_table_set(r->subprocess_env, name, value != NULL ? value : "");
}
}
}
}
}
/**
* Setting events based on request payload (query), QS_SetEnvIfParp (hp)
* @param r
* @param sconf
*/
static void qos_parp_hp(request_rec *r, qos_srv_config *sconf) {
const char *query = apr_table_get(r->notes, QS_PARP_Q);
if((query == NULL) && qos_parp_hp_table_fn) {
apr_table_t *tl = qos_parp_hp_table_fn(r);
if(tl) {
if(apr_table_elts(tl)->nelts > 0) {
query = qos_parp_query(r, tl, NULL);
if(query) {
apr_table_setn(r->notes, apr_pstrdup(r->pool, QS_PARP_Q), query);
}
}
} else {
/* no table provided by mod_parp (unsupported content type?),
use query string if available */
if(r->parsed_uri.query) {
query = r->parsed_uri.query;
}
}
}
if(query) {
qos_setenvif_ex(r, query, sconf->setenvifparp_t);
}
}
/**
* Replaces ${var} by the value in var
* @param p Pool for memory allocation
* @param vars Available variables to lookup
* @param string String to replace variables
* @return 1 on success or 0 if string still contains "${"
*/
static int qos_reslove_variable(apr_pool_t *p, apr_table_t *vars, char **string) {
int i;
int start;
int line_end;
char *var_name;
char *new_line = *string;
char *line = *string;
const char *val;
once_again:
i = 0;
while(line[i] != 0) {
if((line[i] == '$') && (line[i+1] == '{')) {
line_end = i;
i=i+2;
start = i;
while((line[i] != 0) && (line[i] != '}')) {
i++;
}
if(line[i] != '}') {
/* no end found */
break;
} else {
var_name = apr_pstrndup(p, &line[start], i - start);
val = apr_table_get(vars, var_name);
if(val) {
line[line_end] = 0;
i++;
new_line = apr_pstrcat(p, line, val, &line[i], NULL);
line = new_line;
goto once_again;
}
}
}
i++;
}
if(!new_line[0] || strstr(new_line, "${")) {
return 0;
}
*string = new_line;
return 1;
}
/**
* QS_SetEnvIfQuery (hp)
* @param r
* @param sconf
* @param dconf
*/
static void qos_setenvifquery(request_rec *r, qos_srv_config *sconf, qos_dir_config *dconf) {
qos_setenvif_ex(r, r->parsed_uri.query, sconf->setenvifquery_t);
qos_setenvif_ex(r, r->parsed_uri.query, dconf->setenvifquery_t);
}
/**
* QS_SetEnv
* @param r
* @param sconf
*/
static void qos_setenv(request_rec *r, qos_srv_config *sconf) {
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(sconf->setenv_t)->elts;
for(i = 0; i < apr_table_elts(sconf->setenv_t)->nelts; i++) {
char *variable = entry[i].val;
char *value = apr_pstrdup(r->pool, strchr(entry[i].key, '='));
value++;
if(qos_reslove_variable(r->pool, r->subprocess_env, &value)) {
apr_table_set(r->subprocess_env, variable, value);
}
}
}
/**
* QS_SetReqHeader
* @param r
* @param header_t
*/
static void qos_setreqheader(request_rec *r, apr_table_t *header_t) {
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(header_t)->elts;
for(i = 0; i < apr_table_elts(header_t)->nelts; i++) {
char *header = entry[i].val;
char *variable = apr_pstrdup(r->pool, strchr(entry[i].key, '='));
const char *val;
variable++;
val = apr_table_get(r->subprocess_env, variable);
if(val) {
if(header[0] == '!') {
apr_table_unset(r->headers_in, &header[1]);
} else {
apr_table_set(r->headers_in, header, val);
}
}
}
}
/**
* QS_SetEnvIf (hp and logger)
* @param r
* @param setenvif_t
*/
static void qos_setenvif(request_rec *r, apr_array_header_t *setenvif_t) {
int i;
qos_setenvif_t *entries = (qos_setenvif_t *)setenvif_t->elts;
for(i = 0; i < setenvif_t->nelts; i++) {
qos_setenvif_t *setenvif = &entries[i];
if(setenvif->preg == NULL) {
// mode 1 (boolean AND operator)
if((setenvif->variable1[0] == '!') && (setenvif->variable2[0] == '!')) {
if(!apr_table_get(r->subprocess_env, &setenvif->variable1[1]) &&
!apr_table_get(r->subprocess_env, &setenvif->variable2[1])) {
if(setenvif->name[0] == '!') {
apr_table_unset(r->subprocess_env, &setenvif->name[1]);
} else {
apr_table_set(r->subprocess_env, setenvif->name, setenvif->value);
}
}
} else if(setenvif->variable1[0] == '!') {
if(!apr_table_get(r->subprocess_env, &setenvif->variable1[1]) &&
apr_table_get(r->subprocess_env, setenvif->variable2)) {
if(setenvif->name[0] == '!') {
apr_table_unset(r->subprocess_env, &setenvif->name[1]);
} else {
apr_table_set(r->subprocess_env, setenvif->name, setenvif->value);
}
}
} else if(setenvif->variable2[0] == '!') {
if(apr_table_get(r->subprocess_env, setenvif->variable1) &&
!apr_table_get(r->subprocess_env, &setenvif->variable2[1])) {
if(setenvif->name[0] == '!') {
apr_table_unset(r->subprocess_env, &setenvif->name[1]);
} else {
apr_table_set(r->subprocess_env, setenvif->name, setenvif->value);
}
}
} else {
if(apr_table_get(r->subprocess_env, setenvif->variable1) &&
apr_table_get(r->subprocess_env, setenvif->variable2)) {
if(setenvif->name[0] == '!') {
apr_table_unset(r->subprocess_env, &setenvif->name[1]);
} else {
apr_table_set(r->subprocess_env, setenvif->name, setenvif->value);
}
}
}
} else {
// mode 2 (pattern match)
const char *value = apr_table_get(r->subprocess_env, setenvif->variable1);
if(value) {
ap_regmatch_t regm[AP_MAX_REG_MATCH];
if(ap_regexec(setenvif->preg, value, AP_MAX_REG_MATCH, regm, 0) == 0) {
if(setenvif->name[0] == '!') {
apr_table_unset(r->subprocess_env, &setenvif->name[1]);
} else {
char *replaced = ap_pregsub(r->pool, setenvif->value, value, AP_MAX_REG_MATCH, regm);
apr_table_set(r->subprocess_env, setenvif->name, replaced);
}
}
}
}
}
}
/**
* QS_RequestHeaderFilter enforcement
* @param r
* @param sconf
* @parm dconf
* @return
*/
static int qos_hp_header_filter(request_rec *r, qos_srv_config *sconf, qos_dir_config *dconf) {
qs_headerfilter_mode_e mode = sconf->headerfilter;
if(dconf->headerfilter > QS_HEADERFILTER_OFF_DEFAULT) {
// overrides server configuration
mode = dconf->headerfilter;
}
if(mode > QS_HEADERFILTER_OFF) {
apr_status_t rv = qos_header_filter(r, sconf, r->headers_in, "request",
sconf->hfilter_table, mode);
if(rv != APR_SUCCESS) {
int rc;
const char *error_page = sconf->error_page;
qs_set_evmsg(r, "D;");
if(!sconf->log_only) {
rc = qos_error_response(r, error_page);
if((rc == DONE) || (rc == HTTP_MOVED_TEMPORARILY)) {
return rc;
}
return rv;
}
}
}
return DECLINED;
}
/**
* Dynamic keep alive.
* Creates a copy of the server_rec and adjusts the keep-aliva settings
* for this request.
*
* @param r
* @param sconf
*/
static void qos_keepalive(request_rec *r, qos_srv_config *sconf) {
if(r->subprocess_env) {
const char *vtmo = apr_table_get(r->subprocess_env, QS_KEEPALIVE);
const char *vmax = apr_table_get(r->subprocess_env, QS_MAXKEEPALIVEREQ);
int ka = -1; // keep alive timeout
int km = -1; // max keep alive requests
if(vtmo) {
ka = atoi(vtmo);
if(ka == 0 && vtmo[0] != '0') {
ka = -1;
}
}
if(vmax) {
km = atoi(vmax);
if(km == 0 && vmax[0] != '0') {
km = -1;
}
}
if(ka >= 0 || km >= 0) {
qs_req_ctx *rctx = qos_rctx_config_get(r);
if(m_event_mpm) {
ap_log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r,
QOS_LOG_PFX(037)"loaded MPM is 'event'"
" and the QS_KeepAliveTimeout/QS_MaxKeepAliveRequests"
" directives can't be used.");
QS_INC_EVENT(sconf, 37);
return;
}
if(QS_ISDEBUG(r->server)) {
int kaorig = apr_time_sec(r->server->keep_alive_timeout);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
QOS_LOGD_PFX"set keepalive timeout to %d seconds and max"
" keepalive requests to %d%s, id=%s",
ka >= 0 ? ka : kaorig,
km >= 0 ? km : r->server->keep_alive_max,
sconf->log_only ? " (log only)" : "",
qos_unique_id(r, NULL));
}
/* copy the server record (I know......., but this works) */
if(!rctx->evmsg || !strstr(rctx->evmsg, "T;")) {
/* copy it only once (@hp or @out-filter) */
if(!sconf->log_only) {
server_rec *sr = apr_pcalloc(r->connection->pool, sizeof(server_rec));
server_rec *sc = apr_pcalloc(r->connection->pool, sizeof(server_rec));
memcpy(sr, r->server, sizeof(server_rec));
memcpy(sc, r->connection->base_server, sizeof(server_rec));
r->server = sr;
r->connection->base_server = sc;
}
qs_set_evmsg(r, "T;");
}
if(!sconf->log_only) {
if(ka >= 0) {
apr_interval_time_t kat = apr_time_from_sec(ka);
r->server->keep_alive_timeout = kat;
r->connection->base_server->keep_alive_timeout = kat;
}
if(km >= 0) {
r->server->keep_alive_max = km;
r->connection->base_server->keep_alive_max = km;
}
}
}
}
}
/**
* QS_EventPerSecLimit
*/
static void qos_lg_event_update(request_rec *r, apr_time_t *t) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config,
&qos_module);
qs_actable_t *act = sconf->act;
if(act->has_events && (apr_table_get(r->notes, QS_R012_ALREADY_BLOCKED) == NULL)) {
apr_time_t now = apr_time_sec(r->request_time);
qs_acentry_t *actEntry = act->entry;
*t = now;
if(actEntry) {
apr_global_mutex_lock(act->lock); /* @CRT13 */
while(actEntry) {
if(actEntry->event) {
if(((actEntry->event[0] != '!') && apr_table_get(r->subprocess_env, actEntry->event)) ||
((actEntry->event[0] == '!') && !apr_table_get(r->subprocess_env, &actEntry->event[1]))) {
if(actEntry->req < LONG_MAX) {
actEntry->req++;
}
if(now > (actEntry->interval + QS_BW_SAMPLING_RATE)) {
if(actEntry->req_per_sec_limit) {
/* QS_EventPerSecLimit */
actEntry->req_per_sec = actEntry->req / (now - actEntry->interval);
actEntry->req = 0;
actEntry->interval = now;
qos_cal_req_sec(sconf, r, actEntry);
}
}
}
}
actEntry = actEntry->next;
}
apr_global_mutex_unlock(act->lock); /* @CRT13 */
}
}
}
/**
* QS_EventLimitCount, propagte variable
*/
static void qos_pr_event_limit(request_rec *r, qos_srv_config *sconf) {
qs_actable_t *act = sconf->act;
if(act->event_entry && (sconf->event_limit_a->nelts > 0)) {
int i;
qos_event_limit_entry_t *entry = act->event_entry;
apr_time_t now = apr_time_sec(r->request_time);
apr_global_mutex_lock(act->lock); /* @CRT46 */
for(i = 0; i < sconf->event_limit_a->nelts; i++) {
if(entry->action == QS_EVENT_ACTION_DENY) {
// propagte to environment (previous value)
if(entry->limitTime + entry->seconds >= now) {
apr_table_set(r->subprocess_env,
apr_pstrcat(r->pool, entry->env_var, QS_COUNTER_SUFFIX, NULL),
apr_psprintf(r->pool, "%d", entry->limit));
}
}
// next rule
entry++;
}
apr_global_mutex_unlock(act->lock); /* @CRT46 */
}
}
/**
* QS_EventLimitCount, detect and enforce
*/
static int qos_hp_event_limit(request_rec *r, qos_srv_config *sconf) {
apr_status_t rv = DECLINED;
qs_actable_t *act = sconf->act;
if(act->event_entry) {
apr_time_t now = apr_time_sec(r->request_time);
int i;
qos_event_limit_entry_t *entry = act->event_entry;
apr_global_mutex_lock(act->lock); /* @CRT41 */
for(i = 0; i < sconf->event_limit_a->nelts; i++) {
if(entry->action == QS_EVENT_ACTION_DENY) {
if(apr_table_get(r->subprocess_env, entry->env_var) != NULL) {
char *eventLimitId = apr_pstrcat(r->pool, QS_R013_ALREADY_BLOCKED, entry->env_var, NULL);
apr_table_set(r->notes, eventLimitId, "");
// reset required (expired)?
if(entry->limitTime + entry->seconds < now) {
entry->limit = 0;
entry->limitTime = 0;
}
/* increment limit event */
if(entry->limit < INT_MAX) {
entry->limit++;
}
if(entry->limit == 1) {
/* ... and start timer */
entry->limitTime = now;
}
// check limit
if(entry->limit > entry->max) {
int block = 1;
char *conditional = "";
if(entry->condStr != NULL) {
// conditional enforcement...
const char *condition = apr_table_get(r->subprocess_env, QS_COND);
conditional = apr_pstrdup(r->pool, "Cond");
if(condition == NULL) {
block = 0; // variable not set
} else {
if(ap_regexec(entry->preg, condition, 0, NULL, 0) != 0) {
block = 0; // pattern does not match
}
}
}
if(block) {
rv = m_retcode;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(013)"access denied%s,"
" QS_%sEventLimitCount rule: %s,"
" max=%d, current=%d,"
" c=%s, id=%s",
sconf->log_only ? " (log only)" : "",
conditional,
entry->env_var, entry->max, entry->limit,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "013"));
QS_INC_EVENT_LOCKED(sconf, 13);
}
}
}
// propagte to environment (current)
apr_table_set(r->subprocess_env,
apr_pstrcat(r->pool, entry->env_var, QS_COUNTER_SUFFIX, NULL),
apr_psprintf(r->pool, "%d", entry->limit));
}
// next rule
entry++;
}
apr_global_mutex_unlock(act->lock); /* @CRT41 */
}
if(rv != DECLINED) {
int rc;
const char *error_page = sconf->error_page;
qs_set_evmsg(r, "D;");
if(!sconf->log_only) {
rc = qos_error_response(r, error_page);
if((rc == DONE) || (rc == HTTP_MOVED_TEMPORARILY)) {
rv = rc;
}
} else {
return DECLINED;
}
}
return rv;
}
/**
* QS_EventRequestLimit
*/
static int qos_hp_event_filter(request_rec *r, qos_srv_config *sconf) {
apr_status_t rv = DECLINED;
qs_req_ctx *rctx = qos_rctx_config_get(r);
qs_actable_t *act = sconf->act;
if(act->has_events) {
qs_acentry_t *actEntry = act->entry;
if(actEntry) {
apr_global_mutex_lock(act->lock); /* @CRT31 */
while(actEntry) {
if(actEntry->event && (actEntry->limit != -1)) {
const char *var = apr_table_get(r->subprocess_env, actEntry->event);
if(var) {
int match = 1;
if(actEntry->regex_var) {
if(ap_regexec(actEntry->regex_var, var, 0, NULL, 0) != 0) {
match = 0;
}
}
if(match) {
apr_table_addn(rctx->event_entries, actEntry->url, (char *)actEntry);
if(actEntry->counter < INT_MAX) {
actEntry->counter++;
}
if(actEntry->counter > actEntry->limit) {
rv = m_retcode;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(012)"access denied%s,"
" QS_EventRequestLimit rule: %s(%d),"
" concurrent requests=%d,"
" c=%s, id=%s",
sconf->log_only ? " (log only)" : "",
actEntry->url, actEntry->limit, actEntry->counter,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "012"));
apr_table_set(r->notes, QS_R012_ALREADY_BLOCKED, "");
QS_INC_EVENT_LOCKED(sconf, 12);
}
apr_table_add(r->subprocess_env,
apr_psprintf(r->pool, "QS_EventRequestLimit_%s_Counter", actEntry->event),
apr_psprintf(r->pool, "%d", actEntry->counter));
}
}
}
actEntry = actEntry->next;
}
apr_global_mutex_unlock(act->lock); /* @CRT31 */
}
}
if(rv != DECLINED) {
int rc;
const char *error_page = sconf->error_page;
qs_set_evmsg(r, "D;");
if(!sconf->log_only) {
rc = qos_error_response(r, error_page);
if((rc == DONE) || (rc == HTTP_MOVED_TEMPORARILY)) {
rv = rc;
}
} else {
return DECLINED;
}
}
return rv;
}
static qs_conn_ctx *qos_get_cconf(conn_rec *connection) {
conn_rec *c = QS_CONN_MASTER(connection);
qs_conn_ctx *cconf = NULL;
qs_conn_base_ctx *base = qos_get_conn_base_ctx(c);
if(base) {
cconf = base->cconf;
}
return cconf;
}
static qs_conn_ctx *qos_create_cconf(conn_rec *connection, qos_srv_config *sconf) {
conn_rec *c = QS_CONN_MASTER(connection);
qs_conn_base_ctx *base = qos_get_conn_base_ctx(c);
qs_conn_ctx *cconf = apr_pcalloc(c->pool, sizeof(qs_conn_ctx));
cconf->mc = c;
cconf->ip6[0] = 0;
cconf->ip6[1] = 0;
cconf->evmsg = NULL;
cconf->sconf = sconf;
cconf->is_vip = 0;
cconf->set_vip_by_header = 0;
cconf->has_lowrate = 0;
apr_pool_pre_cleanup_register(c->pool, cconf, qos_cleanup_conn);
if(base == NULL) {
base = qos_create_conn_base_ctx(c, sconf);
}
base->cconf = cconf;
return cconf;
}
/*
* QS_SrvSerialize
*/
static void qos_hp_srv_serialize(request_rec *r, qos_srv_config *sconf,
qs_req_ctx * rctx) {
int loops = 0;
int locked = 0; // we got the lock for this request
if(!rctx) {
rctx = qos_rctx_config_get(r);
}
while(!locked) {
apr_global_mutex_lock(sconf->act->lock); /* @CRT44 */
if(sconf->act->serialize->locked == 0) {
// free!! check if we might get the lock for this request
if(sconf->act->serialize->q1 == 0) {
// yes: no other request waiting
locked = 1;
} else if(sconf->act->serialize->q1 == r->request_time) {
// yes: waiting for this request (we are the next in the queue)
locked = 1;
sconf->act->serialize->q1 = sconf->act->serialize->q2;
sconf->act->serialize->q2 = 0;
} else if(sconf->act->serialize->q1 > r->request_time) {
// yes: not yet in the queue but this request is waiting for a longer time
// keep the others in the queue
locked = 1;
} else if(sconf->act->serialize->q2 == 0 || sconf->act->serialize->q2 > r->request_time) {
// no: it's not yet our time... but take the second place in the queue
sconf->act->serialize->q2 = r->request_time;
}
} else {
// put this request into one of the queues if possible
if(sconf->act->serialize->q1 == 0) {
// no other request waiting
sconf->act->serialize->q1 = r->request_time;
} else if(sconf->act->serialize->q1 == r->request_time) {
// already next in the queue
} else if(sconf->act->serialize->q1 > r->request_time) {
// older request is waiting, take over
sconf->act->serialize->q2 = sconf->act->serialize->q1;
sconf->act->serialize->q1 = r->request_time;
} else if(sconf->act->serialize->q2 == 0) {
// no one in on the second place
sconf->act->serialize->q2 = r->request_time;
} else if(sconf->act->serialize->q2 == r->request_time) {
// already next in the queue
} else if(sconf->act->serialize->q2 > r->request_time) {
// older request is waiting in the second position, take over
sconf->act->serialize->q2 = r->request_time;
}
}
if(locked) {
sconf->act->serialize->locked = 1;
rctx->srv_serialize_set = 1;
}
apr_global_mutex_unlock(sconf->act->lock); /* @CRT44 */
if(!locked) {
/* sleep 50ms */
qs_set_evmsg(r, "s;");
if(sconf->log_only) {
return;
}
apr_sleep(50000);
}
if(loops >= sconf->serializeTMO) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, r,
QOS_LOG_PFX(068)"QS_SrvSerialize exceeds limit of %d seconds, "
"id=%s",
sconf->serializeTMO / 20,
qos_unique_id(r, "037"));
QS_INC_EVENT(sconf, 37);
/* remove this request from the queue resp. clear the queue
to avoid a deadlock */
apr_global_mutex_lock(sconf->act->lock); /* @CRT44.1 */
sconf->act->serialize->q2 = 0;
sconf->act->serialize->q1 = 0;
apr_global_mutex_unlock(sconf->act->lock); /* @CRT44.1 */
break;
}
loops++;
}
}
/*
* QS_ClientSerialize
*/
static void qos_hp_cc_serialize(request_rec *r, qos_srv_config *sconf, qs_req_ctx * rctx) {
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
if(!rctx) {
rctx = qos_rctx_config_get(r);
}
if(u && cconf) {
int loops = 0;
int locked = 0;
qos_s_entry_t searchE;
const char *forwardedForLogIP = qos_get_clientIP(r, sconf, cconf,
"hp", rctx->cc_serialize_ip);
searchE.ip6[0] = rctx->cc_serialize_ip[0];
searchE.ip6[1] = rctx->cc_serialize_ip[1];
/* wait until we get a lock */
while(!locked) {
qos_s_entry_t **clientEntry = NULL;
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT36 */
clientEntry = qos_cc_getOrSet(u->qos_cc, &searchE, apr_time_sec(r->request_time));
if((*clientEntry)->serialize == 0) {
// free! check if this request is the next in the queue */
if(((*clientEntry)->serializeQueue == 0) || (r->request_time <= (*clientEntry)->serializeQueue)) {
(*clientEntry)->serialize = 1;
(*clientEntry)->serializeQueue = 0;
rctx->cc_serialize_set = 1;
locked = 1;
}
} else {
// put the request into the queue
if((*clientEntry)->serializeQueue == 0) {
// the only waiting req
(*clientEntry)->serializeQueue = r->request_time;
} else {
if((*clientEntry)->serializeQueue > r->request_time) {
// this request is waiting for a longer time
(*clientEntry)->serializeQueue = r->request_time;
}
}
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT36 */
if(!locked) {
/* sleep 100ms */
qs_set_evmsg(r, "s;");
if(sconf->log_only) {
return;
}
apr_sleep(100000);
}
// max wait time: 5 minutes
if(loops >= 3000) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, r,
QOS_LOG_PFX(068)"QS_ClientSerialize exceeds limit of 5 minutes, "
"c=%s, id=%s",
forwardedForLogIP == NULL ? "-" : forwardedForLogIP,
qos_unique_id(r, "068"));
QS_INC_EVENT(sconf, 68);
/* remove this request from the queue resp. clear the queue
to avoid a deadlock */
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT36.1 */
clientEntry = qos_cc_getOrSet(u->qos_cc, &searchE, apr_time_sec(r->request_time));
(*clientEntry)->serializeQueue = 0;
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT36.1 */
break;
}
loops++;
}
}
}
/*
* QS_ClientEventRequestLimit
*/
static int qos_hp_cc_event_count(request_rec *r, qos_srv_config *sconf,
qs_req_ctx * rctx) {
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
if(!rctx) {
rctx = qos_rctx_config_get(r);
}
if(u && cconf &&
r->subprocess_env && apr_table_get(r->subprocess_env, "QS_EventRequest")) {
int vip = 0;
int count = 0;
qos_s_entry_t **clientEntry = NULL;
qos_s_entry_t searchE;
const char *forwardedForLogIP = qos_get_clientIP(r, sconf, cconf,
"hp", rctx->cc_event_ip);
rctx->cc_event_req_set = 1;
searchE.ip6[0] = rctx->cc_event_ip[0];
searchE.ip6[1] = rctx->cc_event_ip[1];
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT33 */
clientEntry = qos_cc_getOrSet(u->qos_cc, &searchE, apr_time_sec(r->request_time));
if((*clientEntry)->event_req < INT_MAX) {
(*clientEntry)->event_req++;
}
count = (*clientEntry)->event_req;
if((*clientEntry)->vip || rctx->is_vip) {
vip = 1;
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT33 */
if(vip) {
apr_table_set(r->subprocess_env, QS_ISVIPREQ, "yes");
}
if(count > sconf->qos_cc_event_req) {
if(vip) {
qs_set_evmsg(r, "S;");
} else {
int rc;
const char *error_page = sconf->error_page;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(065)"access denied%s,"
" QS_ClientEventRequestLimit rule:"
" max=%d, current=%d, c=%s, id=%s",
sconf->log_only ? " (log only)" : "",
sconf->qos_cc_event_req,
count,
forwardedForLogIP == NULL ? "-" :
forwardedForLogIP,
qos_unique_id(r, "065"));
QS_INC_EVENT(sconf, 65);
qs_set_evmsg(r, "D;");
if(!sconf->log_only) {
rc = qos_error_response(r, error_page);
if((rc == DONE) || (rc == HTTP_MOVED_TEMPORARILY)) {
return rc;
}
return m_retcode;
}
}
}
}
return DECLINED;
}
/* QS_SetEnvIfCmp */
static void qos_setenvifcmp(request_rec *r, apr_array_header_t *cmps) {
int i;
qos_cmp_entry_t *entries = (qos_cmp_entry_t *)cmps->elts;
for(i = 0; i < cmps->nelts; ++i) {
qos_cmp_entry_t *b = &entries[i];
const char *leftStr = apr_table_get(r->subprocess_env, b->left);
const char *rightStr = apr_table_get(r->subprocess_env, b->right);
if(leftStr != NULL && rightStr != NULL) {
int set = 0;
if(qos_isnum(leftStr) && qos_isnum(rightStr)) {
int left = atoi(leftStr);
int right = atoi(rightStr);
switch (b->cmp) {
case QS_CMP_EQ:
if(left == right) {
set = 1;
}
break;
case QS_CMP_NE:
if(left != right) {
set = 1;
}
break;
case QS_CMP_GT:
if(left > right) {
set = 1;
}
break;
case QS_CMP_LT:
if(left < right) {
set = 1;
}
break;
}
} else {
int c = strcasecmp(leftStr, rightStr);
switch (b->cmp) {
case QS_CMP_EQ:
if(c == 0) {
set = 1;
}
break;
case QS_CMP_NE:
if(c != 0) {
set = 1;
}
break;
case QS_CMP_GT:
if(c < 0) {
set = 1;
}
break;
case QS_CMP_LT:
if(c > 0) {
set = 1;
}
break;
}
}
if(set) {
if(b->variable[0] == '!') {
apr_table_unset(r->subprocess_env, &b->variable[1]);
} else {
apr_table_set(r->subprocess_env, b->variable, b->value);
}
}
}
}
return;
}
/*
* QS_EventPerSecLimit/QS_EventKBytesPerSecLimit
* returns the max req_per_sec_block_rate/kbytes_per_sec_limit and the event
* with the lowest kbytes_per_sec_limit.
*/
static qs_acentry_t *qos_hp_event_count(request_rec *r,
int *req_per_sec_block,
apr_off_t *kbytes_per_sec_limit) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config,
&qos_module);
qs_actable_t *act = sconf->act;
qs_acentry_t *event_kbytes_limit = NULL;
*req_per_sec_block = 0;
*kbytes_per_sec_limit = 0;
if(act->has_events) {
qs_acentry_t *actEntry = act->entry;
if(actEntry) {
apr_global_mutex_lock(act->lock); /* @CRT12 */
while(actEntry) {
if(actEntry->event && (actEntry->limit == -1)) {
if(((actEntry->event[0] != '!') && apr_table_get(r->subprocess_env, actEntry->event)) ||
((actEntry->event[0] == '!') && !apr_table_get(r->subprocess_env, &actEntry->event[1]))) {
if(actEntry->req_per_sec_limit) {
/* QS_EventPerSecLimit */
if(actEntry->req_per_sec_block_rate > *req_per_sec_block) {
*req_per_sec_block = actEntry->req_per_sec_block_rate;
}
} else {
/* QS_EventKBytesPerSecLimit */
if(actEntry->kbytes_per_sec_limit) {
if((*kbytes_per_sec_limit == 0) ||
(actEntry->kbytes_per_sec_limit < *kbytes_per_sec_limit)) {
*kbytes_per_sec_limit = actEntry->kbytes_per_sec_limit;
event_kbytes_limit = actEntry;
}
}
}
}
}
actEntry = actEntry->next;
}
apr_global_mutex_unlock(act->lock); /* @CRT12 */
}
}
return event_kbytes_limit;
}
static apr_size_t qos_packet_rate(qos_ifctx_t *inctx, apr_bucket_brigade *bb) {
apr_bucket *b;
apr_size_t total = 0;
for(b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b)) {
if(b->length) {
total = total + b->length;
}
}
return total;
}
/**
* start packet rate measure (if filter has not already been inserted)
*/
static void qos_pktrate_pc(conn_rec *connection, qos_srv_config *sconf) {
conn_rec *c = connection;
qos_ifctx_t *inctx = qos_get_ifctx(c->input_filters);
if(inctx == NULL) {
inctx = qos_create_ifctx(c, sconf);
ap_add_input_filter("qos-in-filter", inctx, NULL, c);
}
inctx->lowrate = 0;
}
/**
* timeout control at process connection handler
*/
static void qos_timeout_pc(conn_rec *connection, qos_srv_config *sconf) {
conn_rec *c = connection;
qos_ifctx_t *inctx = qos_get_ifctx(c->input_filters);
if(inctx) {
inctx->status = QS_CONN_STATE_HEAD;
inctx->time = time(NULL);
inctx->nbytes = 0;
#if APR_HAS_THREADS
if(sconf->inctx_t && !sconf->inctx_t->exit && sconf->min_rate_off == 0) {
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT22 */
apr_table_setn(sconf->inctx_t->table,
QS_INCTX_ID,
(char *)inctx);
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT22 */
}
#endif
}
}
/**
* determines client behavior based on accessed content types
*
* @return 0=normal, -1=unknown (not enough data), >=1 abnormal
*/
static int qos_content_type(request_rec *r, qos_srv_config *sconf,
qos_s_t *s, qos_s_entry_t *e, int limit) {
int penalty = -1;
const char *ct = apr_table_get(r->headers_out, "Content-Type");
e->events++; // events counts requests and connections
if(r->status == 304) {
e->notmodified ++;
s->notmodified ++;
}
if(ct) {
if(ap_strcasestr(ct, "html")) {
e->events++; // learn faster if user requests HTML content (main pages)
e->html++;
s->html++;
goto end;
} else if(ap_strcasestr(ct, "image")) {
e->img++;
s->img++;
goto end;
} else if(ap_strcasestr(ct, "css")) {
e->cssjs++;
s->cssjs++;
goto end;
} else if(ap_strcasestr(ct, "javascript")) {
e->cssjs++;
s->cssjs++;
goto end;
}
}
e->other++;
s->other++;
end:
/* compare this client with other clients */
if(e->events > QOS_CC_BEHAVIOR_THR_SINGLE) {
penalty = 0;
if(limit &&
((sconf->static_on == 1) ||
(s->html > QOS_CC_BEHAVIOR_THR && s->html && s->img && s->cssjs && s->other && s->notmodified))) {
int i;
unsigned int server[5];
unsigned int client[5];
// note: all e->* variables are initialized by "1" to avoid FPE
if(sconf->static_on == 1) {
/* use predefined value */
unsigned long e_all = e->html + e->img + e->cssjs + e->other + e->notmodified;
server[0] = sconf->static_html;
server[1] = sconf->static_cssjs;
server[2] = sconf->static_img;
server[3] = sconf->static_other;
server[4] = sconf->static_notmodified;
client[0] = 100 * e->html / e_all;
client[1] = 100 * e->cssjs / e_all;
client[2] = 100 * e->img / e_all;
client[3] = 100 * e->other / e_all;
client[4] = 100 * e->notmodified / e_all;
} else {
/* learn average */
unsigned long long s_all = s->html + s->img + s->cssjs + s->other + s->notmodified;
unsigned long e_all = e->html + e->img + e->cssjs + e->other + e->notmodified;
server[0] = 100 * s->html / s_all;
server[1] = 100 * s->cssjs / s_all;
server[2] = 100 * s->img / s_all;
server[3] = 100 * s->other / s_all;
server[4] = 100 * s->notmodified / s_all;
client[0] = 100 * e->html / e_all;
client[1] = 100 * e->cssjs / e_all;
client[2] = 100 * e->img / e_all;
client[3] = 100 * e->other / e_all;
client[4] = 100 * e->notmodified / e_all;
}
for(i = 0; i < 5; i++) {
if(client[i] > (server[i] + sconf->cc_tolerance)) {
penalty++;
} else {
if((server[i] > sconf->cc_tolerance) &&
(client[i] < (server[i] - sconf->cc_tolerance))) {
penalty++;
}
}
}
}
}
return penalty;
}
//static void qos_error_log(const char *file, int line, int level,
// apr_status_t status, const server_rec *s,
// const request_rec *r, apr_pool_t *pool,
// const char *errstr) {
// return;
//}
/**
* QS_EventLimitCount, detect/update only
*/
static void qos_logger_event_limit(request_rec *r, qos_srv_config *sconf) {
qs_actable_t *act = sconf->act;
if(act->event_entry && (sconf->event_limit_a->nelts > 0)) {
apr_time_t now = apr_time_sec(r->request_time);
int i;
qos_event_limit_entry_t *actEntry = act->event_entry;
apr_global_mutex_lock(act->lock); /* @CRT42 */
for(i = 0; i < sconf->event_limit_a->nelts; i++) {
if(actEntry->action == QS_EVENT_ACTION_DENY) {
int decEvent = get_qs_event(r, actEntry->eventDecStr);
if(decEvent > 0) {
if(decEvent >= actEntry->limit) {
actEntry->limit = 0;
actEntry->limitTime = 0;
} else {
actEntry->limit = actEntry->limit - decEvent;
}
}
if(apr_table_get(r->subprocess_env, actEntry->env_var) != NULL) {
// increment only once
char *eventLimitId = apr_pstrcat(r->pool, QS_R013_ALREADY_BLOCKED, actEntry->env_var, NULL);
if(apr_table_get(r->notes, eventLimitId) == NULL) {
// reset required (expired)?
if(actEntry->limitTime + actEntry->seconds < now) {
actEntry->limit = 0;
actEntry->limitTime = 0;
}
/* increment limit event */
if(actEntry->limit < INT_MAX) {
actEntry->limit++;
}
if(actEntry->limit == 1) {
/* ... and start timer */
actEntry->limitTime = now;
}
}
}
}
// next rule
actEntry++;
}
apr_global_mutex_unlock(act->lock); /* @CRT42 */
}
}
/**
* client control rules at log transaction
*/
static void qos_logger_cc(request_rec *r, qos_srv_config *sconf, qs_req_ctx *rctx) {
int lowrate = 0;
int unusual_behavior = -1;
int block_event = get_qs_event(r, QS_BLOCK);
const char *block_seen = apr_table_get(r->subprocess_env, QS_BLOCK_SEEN);
int block_dec = get_qs_event(r, QS_BLOCK_DEC);
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
apr_time_t now = apr_time_sec(r->request_time);
qos_s_entry_t **clientEntry = NULL;
qos_s_entry_t **clientEntryFromHdr = NULL; // client ip entry from header
qos_s_entry_t searchE;
qos_s_entry_t searchEFromHdr;
if(block_seen != NULL) {
block_event = 0;
}
if(sconf->qos_cc_prefer_limit || (sconf->req_rate != -1)) {
qos_ifctx_t *inctx = qos_get_ifctx(r->connection->input_filters);
if(inctx) {
if(inctx->lowrate > QS_PKT_RATE_TH) {
lowrate = inctx->lowrate;
}
if(inctx->lowrate != -1) {
inctx->lowrate = 0;
}
if(inctx->status > QS_CONN_STATE_NEW) {
inctx->r = NULL;
inctx->status = QS_CONN_STATE_KEEP;
}
if(inctx->shutdown) {
lowrate++;
inctx->shutdown = 0;
}
}
}
if(cconf) {
// works for real connections only (no HTTP/2)
searchE.ip6[0] = cconf->ip6[0];
searchE.ip6[1] = cconf->ip6[1];
} else {
// HTTP/2
apr_uint64_t ci6[2];
qos_ip_str2long(QS_CONN_REMOTEIP(r->connection), ci6);
searchE.ip6[0] = ci6[0];
searchE.ip6[1] = ci6[1];
}
qos_get_clientIP(r, sconf, cconf, "logger", searchEFromHdr.ip6);
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT19 */
clientEntry = qos_cc_getOrSet(u->qos_cc, &searchE, apr_time_sec(r->request_time));
if(searchEFromHdr.ip6[0] == searchE.ip6[0] && searchEFromHdr.ip6[1] == searchE.ip6[1]) {
clientEntryFromHdr = clientEntry; // same as connection
} else {
clientEntryFromHdr = qos_cc_getOrSet(u->qos_cc, &searchEFromHdr, apr_time_sec(r->request_time));
}
if(rctx->cc_event_req_set) {
/* QS_ClientEventRequestLimit */
qos_s_entry_t **eEvent = NULL;
rctx->cc_event_req_set = 0;
if(rctx->cc_event_ip[0] == searchE.ip6[0] &&
rctx->cc_event_ip[1] == searchE.ip6[1]) {
// connection ip
eEvent = clientEntry;
} else if(rctx->cc_event_ip[0] == searchEFromHdr.ip6[0] &&
rctx->cc_event_ip[1] == searchEFromHdr.ip6[1]) {
// from header
eEvent = clientEntryFromHdr;
} else {
// looks like the header has changed or is no longer available
qos_s_entry_t searchEvent;
searchEvent.ip6[0] = rctx->cc_event_ip[0];
searchEvent.ip6[1] = rctx->cc_event_ip[1];
eEvent = qos_cc_get0(u->qos_cc, &searchEvent, apr_time_sec(r->request_time));
}
if(eEvent) {
if((*eEvent)->event_req > 0) {
(*eEvent)->event_req--;
}
}
}
if(rctx->cc_serialize_set) {
/* QS_ClientSerialize */
qos_s_entry_t **eSerialize = NULL;
rctx->cc_serialize_set = 0;
if(rctx->cc_serialize_ip[0] == searchE.ip6[0] &&
rctx->cc_serialize_ip[1] == searchE.ip6[1]) {
// connection ip
eSerialize = clientEntry;
} else if(rctx->cc_serialize_ip[0] == searchEFromHdr.ip6[0] &&
rctx->cc_serialize_ip[1] == searchEFromHdr.ip6[1]) {
// from header
eSerialize = clientEntryFromHdr;
} else {
// looks like the header has changed or is no longer available
qos_s_entry_t searchSerialize;
searchSerialize.ip6[0] = rctx->cc_serialize_ip[0];
searchSerialize.ip6[1] = rctx->cc_serialize_ip[1];
eSerialize = qos_cc_get0(u->qos_cc, &searchSerialize, apr_time_sec(r->request_time));
}
if(eSerialize) {
(*eSerialize)->serialize = 0;
}
}
if(sconf->qos_cc_prefer) {
// QS_ClientPrefer is not enabled
unusual_behavior = qos_content_type(r, sconf, u->qos_cc, *clientEntry, sconf->qos_cc_prefer_limit);
if(unusual_behavior == 0) {
// normal behavior
(*clientEntry)->lowratestatus |= QOS_LOW_FLAG_BEHAVIOR_OK;
(*clientEntry)->lowratestatus &= ~QOS_LOW_FLAG_BEHAVIOR_BAD;
} else {
// unknown or bad behavior
(*clientEntry)->lowratestatus &= ~QOS_LOW_FLAG_BEHAVIOR_OK;
}
}
if(block_event || block_dec || lowrate || (unusual_behavior > 0)) {
if(((*clientEntry)->blockTime + sconf->qos_cc_blockTime) < now) {
/* reset expired events */
if((*clientEntry)->blockMsg > QS_LOG_REPEAT) {
// write remaining log lines
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r->connection->base_server,
QOS_LOG_PFX(060)"access denied (previously), "
"QS_ClientEventBlockCount rule: "
"max=%d, current=%hu, "
"message repeated %d times, "
"c=%s",
sconf->qos_cc_block,
(*clientEntry)->block,
(*clientEntry)->blockMsg % QS_LOG_REPEAT,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" :
QS_CONN_REMOTEIP(r->connection)/*no id here, this r is okay*/);
QS_INC_EVENT_LOCKED(sconf, 60);
(*clientEntry)->blockMsg = 0;
}
(*clientEntry)->block = 0;
(*clientEntry)->blockTime = 0;
}
/* mark lowpkt client */
if(lowrate || (unusual_behavior > 0)) {
(*clientEntry)->lowrate = apr_time_sec(r->request_time);
if(lowrate) {
(*clientEntry)->lowratestatus |= QOS_LOW_FLAG_PKGRATE;
}
if(unusual_behavior > 1) {
(*clientEntry)->lowratestatus |= QOS_LOW_FLAG_BEHAVIOR_BAD;
(*clientEntry)->lowratestatus &= ~QOS_LOW_FLAG_BEHAVIOR_OK;
}
qs_set_evmsg(r, "r;");
}
if(block_dec > 0) {
if(block_dec >= (*clientEntry)->block) {
(*clientEntry)->block = 0;
(*clientEntry)->blockTime = 0;
} else {
(*clientEntry)->block = (*clientEntry)->block - block_dec;
}
}
if(block_event) {
int newValue = (*clientEntry)->block + block_event;
if((*clientEntry)->block == 0) {
/* start timer */
(*clientEntry)->blockTime = now;
}
/* ...and increment/increase block event counter */
(*clientEntry)->block = newValue > USHRT_MAX ? USHRT_MAX : newValue;
}
} else if((*clientEntry)->lowrate) {
/* reset low prio client after 24h (resp. QOS_LOW_TIMEOUT seconds) */
if(((*clientEntry)->lowrate + QOS_LOW_TIMEOUT) < now) {
(*clientEntry)->lowrate = 0;
if((*clientEntry)->lowratestatus & QOS_LOW_FLAG_BEHAVIOR_OK) {
(*clientEntry)->lowratestatus = QOS_LOW_FLAG_BEHAVIOR_OK;
} else {
(*clientEntry)->lowratestatus = 0;
}
}
}
/* QS_Limit* */
if(u->qos_cc->limitTable) {
int limitTableIndex;
apr_table_entry_t *limitTableEntry = (apr_table_entry_t *)apr_table_elts(u->qos_cc->limitTable)->elts;
for(limitTableIndex = 0;
limitTableIndex < apr_table_elts(u->qos_cc->limitTable)->nelts;
limitTableIndex++) {
int eventSet = 0;
const char *eventName = limitTableEntry[limitTableIndex].key;
qos_s_entry_limit_conf_t *eventLimitConf = (qos_s_entry_limit_conf_t *)limitTableEntry[limitTableIndex].val;
const char *clearEvent = apr_table_get(r->subprocess_env, eventLimitConf->eventClearStr);
int decEvent = get_qs_event(r, eventLimitConf->eventDecStr);
/*
* reset expired events, clear event counter, decrement event counter
*/
if(clearEvent ||
(((*clientEntryFromHdr)->limit[limitTableIndex].limitTime + eventLimitConf->limitTime) < now)) {
(*clientEntryFromHdr)->limit[limitTableIndex].limit = 0;
(*clientEntryFromHdr)->limit[limitTableIndex].limitTime = 0;
}
if(decEvent > 0) {
if(decEvent >= (*clientEntryFromHdr)->limit[limitTableIndex].limit) {
(*clientEntryFromHdr)->limit[limitTableIndex].limit = 0;
(*clientEntryFromHdr)->limit[limitTableIndex].limitTime = 0;
} else {
(*clientEntryFromHdr)->limit[limitTableIndex].limit = (*clientEntryFromHdr)->limit[limitTableIndex].limit - decEvent;
}
}
/*
* check for new events
*/
eventSet = get_qs_event(r, eventName);
if(eventSet) {
char *seenEvent;
if(strcasecmp(eventName, QS_LIMIT_DEFAULT) == 0) {
// backward compat/event forwarding
seenEvent = apr_pstrcat(r->pool, QS_LIMIT_SEEN, NULL);
} else {
seenEvent = apr_pstrcat(r->pool, QS_LIMIT_SEEN, eventName, NULL);
}
if(apr_table_get(r->subprocess_env, seenEvent) == NULL) {
int newValue = (*clientEntryFromHdr)->limit[limitTableIndex].limit + eventSet;
/* only once per request */
apr_table_set(r->subprocess_env, seenEvent, "");
if((*clientEntryFromHdr)->limit[limitTableIndex].limit == 0) {
/* start timer... */
(*clientEntryFromHdr)->limit[limitTableIndex].limitTime = now;
}
/* ... and increase limit event */
(*clientEntryFromHdr)->limit[limitTableIndex].limit = newValue > USHRT_MAX ? USHRT_MAX : newValue;
}
}
}
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT19 */
if(block_event) {
/* only once per request */
apr_table_set(r->subprocess_env, QS_BLOCK_SEEN, "");
apr_table_set(r->connection->notes, QS_BLOCK_SEEN, "");
}
}
/**
* client control rules at header parser
*/
static int qos_hp_cc(request_rec *r, qos_srv_config *sconf, char **msg, char **uid) {
int ret = DECLINED;
if(sconf->has_qos_cc) {
int req_per_sec_block_rate = 0;
qos_s_entry_t **clientEntry = NULL;
qos_s_entry_t **clientEntryFromHdr = NULL;
qos_s_entry_t searchE;
qos_s_entry_t searchEFromHdr;
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
const char *forwardedForLogIP = QS_CONN_REMOTEIP(r->connection);
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
int excludeFromBlock = qos_is_excluded_ip(r->connection, sconf->cc_exclude_ip);
if(cconf) {
searchE.ip6[0] = cconf->ip6[0];
searchE.ip6[1] = cconf->ip6[1];
} else {
// HTTP/2
apr_uint64_t ci6[2];
qos_ip_str2long(QS_CONN_REMOTEIP(r->connection), ci6);
searchE.ip6[0] = ci6[0];
searchE.ip6[1] = ci6[1];
}
forwardedForLogIP = qos_get_clientIP(r, sconf, cconf, "hp",
searchEFromHdr.ip6);
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT17 */
clientEntry = qos_cc_get0(u->qos_cc, &searchE, apr_time_sec(r->request_time));
if(!clientEntry) {
clientEntry = qos_cc_set(u->qos_cc, &searchE, apr_time_sec(r->request_time));
} else {
/* update time */
(*clientEntry)->time = apr_time_sec(r->request_time);
}
if(searchEFromHdr.ip6[0] == searchE.ip6[0] && searchEFromHdr.ip6[1] == searchE.ip6[1]) {
clientEntryFromHdr = clientEntry; // same as connection
} else {
clientEntryFromHdr = qos_cc_get0(u->qos_cc, &searchEFromHdr, apr_time_sec(r->request_time));
if(!clientEntryFromHdr) {
clientEntryFromHdr = qos_cc_set(u->qos_cc, &searchEFromHdr, apr_time_sec(r->request_time));
} else {
/* update time */
(*clientEntryFromHdr)->time = apr_time_sec(r->request_time);
}
}
if(sconf->qos_cc_event) {
apr_time_t now = apr_time_sec(r->request_time);
const char *v = apr_table_get(r->subprocess_env, QS_EVENT);
if(v) {
if((*clientEntry)->req < LONG_MAX) {
(*clientEntry)->req++;
}
if(now > (*clientEntry)->interval + QS_BW_SAMPLING_RATE) {
/* calc req/sec */
(*clientEntry)->req_per_sec = (*clientEntry)->req / (now - (*clientEntry)->interval);
(*clientEntry)->req = 0;
(*clientEntry)->interval = now;
/* calc block rate */
if((*clientEntry)->req_per_sec > sconf->qos_cc_event) {
int factor = (((*clientEntry)->req_per_sec * 100) / sconf->qos_cc_event) - 100;
(*clientEntry)->req_per_sec_block_rate = (*clientEntry)->req_per_sec_block_rate + factor;
if((*clientEntry)->req_per_sec_block_rate > QS_MAX_DELAY/1000) {
(*clientEntry)->req_per_sec_block_rate = QS_MAX_DELAY/1000;
}
/* QS_ClientEventPerSecLimit */
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, r,
QOS_LOG_PFX(061)"request rate limit,"
" rule: "QS_EVENT"(%d), req/sec=%ld,"
" delay=%dms%s",
sconf->qos_cc_event,
(*clientEntry)->req_per_sec, (*clientEntry)->req_per_sec_block_rate,
(*clientEntry)->req_per_sec_block_rate == QS_MAX_DELAY/1000 ? " (max)" : "");
QS_INC_EVENT_LOCKED(sconf, 61);
} else if((*clientEntry)->req_per_sec_block_rate > 0) {
if((*clientEntry)->req_per_sec_block_rate < 50) {
(*clientEntry)->req_per_sec_block_rate = 0;
} else {
int factor = (*clientEntry)->req_per_sec_block_rate / 4;
(*clientEntry)->req_per_sec_block_rate = (*clientEntry)->req_per_sec_block_rate - factor;
}
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, r,
QOS_LOG_PFX(062)"request rate limit,"
" rule: "QS_EVENT"(%d), req/sec=%ld,"
" delay=%dms",
sconf->qos_cc_event,
(*clientEntry)->req_per_sec, (*clientEntry)->req_per_sec_block_rate);
QS_INC_EVENT_LOCKED(sconf, 62);
}
}
req_per_sec_block_rate = (*clientEntry)->req_per_sec_block_rate;
}
}
if(sconf->qos_cc_block && !excludeFromBlock) {
apr_time_t now = apr_time_sec(r->request_time);
int block_event = get_qs_event(r, QS_BLOCK);
if(((*clientEntry)->blockTime + sconf->qos_cc_blockTime) < now) {
/* reset expired events */
if((*clientEntry)->blockMsg > QS_LOG_REPEAT) {
// write remaining log lines
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r->connection->base_server,
QOS_LOG_PFX(060)"access denied (previously), "
"QS_ClientEventBlockCount rule: "
"max=%d, current=%hu, "
"message repeated %d times, "
"c=%s",
sconf->qos_cc_block,
(*clientEntry)->block,
(*clientEntry)->blockMsg % QS_LOG_REPEAT,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" :
QS_CONN_REMOTEIP(r->connection)/*no id here, this r is okay*/);
QS_INC_EVENT_LOCKED(sconf, 60);
(*clientEntry)->blockMsg = 0;
}
(*clientEntry)->block = 0;
(*clientEntry)->blockTime = 0;
}
if(block_event) {
if((*clientEntry)->block == 0) {
/* start timer */
(*clientEntry)->blockTime = now;
}
/* ... and increment block event */
(*clientEntry)->block += block_event;
/* only once per request */
apr_table_set(r->subprocess_env, QS_BLOCK_SEEN, "");
apr_table_set(r->connection->notes, QS_BLOCK_SEEN, "");
}
if((*clientEntry)->block >= sconf->qos_cc_block) {
*uid = apr_pstrdup(r->connection->pool, "060");
*msg = apr_psprintf(r->connection->pool,
QOS_LOG_PFX(060)"access denied%s, "
"QS_ClientEventBlockCount rule: "
"max=%d, current=%hu, age=%"APR_TIME_T_FMT", c=%s",
sconf->log_only ? " (log only)" : "",
sconf->qos_cc_block,
(*clientEntry)->block,
now - (*clientEntry)->blockTime,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" :
QS_CONN_REMOTEIP(r->connection));
QS_INC_EVENT_LOCKED(sconf, 60);
ret = m_retcode;
(*clientEntry)->lowrate = apr_time_sec(r->request_time);
(*clientEntry)->lowratestatus |= QOS_LOW_FLAG_EVENTBLOCK;
qs_set_evmsg(r, "r;");
}
}
if(u->qos_cc->limitTable) {
apr_time_t now = apr_time_sec(r->request_time);
int limitTableIndex;
apr_table_entry_t *limitTableEntry = (apr_table_entry_t *)apr_table_elts(u->qos_cc->limitTable)->elts;
for(limitTableIndex = 0;
limitTableIndex < apr_table_elts(u->qos_cc->limitTable)->nelts;
limitTableIndex++) {
time_t remaining = 0;
int eventSet = 0;
const char *eventName = limitTableEntry[limitTableIndex].key;
qos_s_entry_limit_conf_t *eventLimitConf = (qos_s_entry_limit_conf_t *)limitTableEntry[limitTableIndex].val;
const char *clearEvent = apr_table_get(r->subprocess_env, eventLimitConf->eventClearStr);
/*
* reset expired events or clear event counter
*/
if(clearEvent ||
(((*clientEntryFromHdr)->limit[limitTableIndex].limitTime + eventLimitConf->limitTime) < now)) {
(*clientEntryFromHdr)->limit[limitTableIndex].limit = 0;
(*clientEntryFromHdr)->limit[limitTableIndex].limitTime = 0;
}
/*
* check for new events
*/
eventSet = get_qs_event(r, eventName);
if(eventSet) {
char *seenEvent;
if(strcasecmp(eventName, QS_LIMIT_DEFAULT) == 0) {
// backward compat/event forwarding
seenEvent = apr_pstrcat(r->pool, QS_LIMIT_SEEN, NULL);
} else {
seenEvent = apr_pstrcat(r->pool, QS_LIMIT_SEEN, eventName, NULL);
}
if(apr_table_get(r->subprocess_env, seenEvent) == NULL) {
int newValue = (*clientEntryFromHdr)->limit[limitTableIndex].limit + eventSet;
// first occurrence
apr_table_set(r->subprocess_env, seenEvent, "");
if((*clientEntryFromHdr)->limit[limitTableIndex].limit == 0) {
/* .start timer */
(*clientEntryFromHdr)->limit[limitTableIndex].limitTime = now;
}
/* increment limit event */
(*clientEntryFromHdr)->limit[limitTableIndex].limit = newValue > USHRT_MAX ? USHRT_MAX : newValue;
}
}
/*
* propagate to env
*/
apr_table_set(r->subprocess_env,
apr_pstrcat(r->pool, QS_LIMIT_NAME_PFX, eventName, NULL),
eventName);
apr_table_set(r->subprocess_env,
apr_pstrcat(r->pool, eventName, QS_COUNTER_SUFFIX, NULL),
apr_psprintf(r->pool, "%d", (*clientEntryFromHdr)->limit[limitTableIndex].limit));
remaining = ((*clientEntryFromHdr)->limit[limitTableIndex].limitTime + eventLimitConf->limitTime) - now;
apr_table_set(r->subprocess_env,
apr_pstrcat(r->pool, eventName, QS_LIMIT_REMAINING, NULL),
apr_psprintf(r->pool, "%"APR_TIME_T_FMT, remaining > 0 ? remaining : 0));
/*
* enforce limit
*/
if((*clientEntryFromHdr)->limit[limitTableIndex].limit >= eventLimitConf->limit) {
int block = 1;
char *conditional = "";
if(eventLimitConf->condStr != NULL) {
// conditional enforcement...
const char *condition = apr_table_get(r->subprocess_env, QS_COND);
conditional = apr_pstrdup(r->pool, "Cond");
if(condition == NULL) {
block = 0; // variable not set
} else {
if(ap_regexec(eventLimitConf->preg, condition, 0, NULL, 0) != 0) {
block = 0; // pattern does not match
}
}
}
if(block) {
if(ret == DECLINED || clientEntryFromHdr != clientEntry) {
/* log only one error (either block or limit) */
*uid = apr_pstrdup(r->connection->pool, "067");
*msg = apr_psprintf(r->connection->pool,
QOS_LOG_PFX(067)"access denied%s, "
"QS_%sClientEventLimitCount rule: "
"event=%s, "
"max=%hu, current=%hu, age=%"APR_TIME_T_FMT", c=%s",
sconf->log_only ? " (log only)" : "",
conditional,
eventName,
eventLimitConf->limit,
(*clientEntryFromHdr)->limit[limitTableIndex].limit,
now - (*clientEntryFromHdr)->limit[limitTableIndex].limitTime,
forwardedForLogIP == NULL ? "-" : forwardedForLogIP);
QS_INC_EVENT_LOCKED(sconf, 67);
ret = m_retcode;
}
}
if(clientEntryFromHdr == clientEntry) {
(*clientEntry)->lowrate = apr_time_sec(r->request_time);
(*clientEntry)->lowratestatus |= QOS_LOW_FLAG_EVENTLIMIT;
qs_set_evmsg(r, "r;");
}
}
}
}
/* reset low prio client after 24h */
if(clientEntry && sconf->qos_cc_prefer) {
apr_time_t now = apr_time_sec(r->request_time);
if(((*clientEntry)->lowrate + QOS_LOW_TIMEOUT) < now) {
(*clientEntry)->lowrate = 0;
if((*clientEntry)->lowratestatus & QOS_LOW_FLAG_BEHAVIOR_OK) {
(*clientEntry)->lowratestatus = QOS_LOW_FLAG_BEHAVIOR_OK;
} else {
(*clientEntry)->lowratestatus = 0;
}
}
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT17 */
if(req_per_sec_block_rate) {
qs_set_evmsg(r, "L;");
if(!sconf->log_only) {
apr_sleep(req_per_sec_block_rate*1000);
}
}
}
return ret;
}
#if APR_HAS_THREADS
static apr_status_t qos_cleanup_status_thread(void *selfv) {
qsstatus_t *s = selfv;
s->exit = 1;
/* may long up to 100ms */
if(m_threaded_mpm) {
apr_status_t status;
apr_thread_join(&status, s->thread);
//apr_pool_destroy(s->pool);
}
return APR_SUCCESS;
}
// checks if connection counting is enabled (any host)
static int qos_count_connections(qos_srv_config *sconf) {
server_rec *s = sconf->base_server;
qos_srv_config *bsconf = (qos_srv_config*)ap_get_module_config(s->module_config, &qos_module);
if(QS_COUNT_CONNECTIONS(bsconf)) {
return 1;
}
s = s->next;
while(s) {
qos_srv_config *sc = (qos_srv_config*)ap_get_module_config(s->module_config, &qos_module);
if(QS_COUNT_CONNECTIONS(sc)) {
return 1;
}
s = s->next;
}
return 0;
}
// total (server/all hosts) connections
static int qos_server_connections(qos_srv_config *sconf) {
server_rec *s = sconf->base_server;
qos_srv_config *bsconf = (qos_srv_config*)ap_get_module_config(s->module_config, &qos_module);
int connections = bsconf->act->conn->connections;
s = s->next;
while(s) {
qos_srv_config *sc = (qos_srv_config*)ap_get_module_config(s->module_config, &qos_module);
if(sc->act->conn != bsconf->act->conn) {
// server has either his own counter or the base server's counter
connections += sc->act->conn->connections;
}
s = s->next;
}
return connections;
/*
int i, j;
worker_score *ws_record;
process_score *ps_record;
for(i = 0; i < sconf->server_limit; ++i) {
ps_record = ap_get_scoreboard_process(i);
for(j = 0; j < sconf->thread_limit; ++j) {
ws_record = ap_get_scoreboard_worker(i, j);
if(!ps_record->quiescing && ps_record->pid) {
if(ws_record->status == SERVER_READY && ps_record->generation == qos_my_generation) {
ready++;
}
}
}
}
*/
}
/**
* Status logger thread
*
* @param thread
* @param selfv Base server_rec
*/
static void *APR_THREAD_FUNC qos_status_thread(apr_thread_t *thread, void *selfv) {
qsstatus_t *s = selfv;
int server_limit, thread_limit;
ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
while(!s->exit) {
char clientContentTypes[8192];
char allConn[64];
int s_busy = 0;
int s_open = 0;
int s_ready = 0;
int s_read = 0;
int s_write = 0;
int s_keep = 0;
int s_start = 0;
int s_log = 0;
int s_dns = 0;
int s_closing = 0;
int s_usr1 = 0;
int s_kill = 0;
worker_score ws_record;
int c, i, e;
time_t now = time(NULL);
int run = 0;
e = 60 - (now % 60);
e = e * 10;
for(c = 0; c < e; c++) {
apr_sleep(100000);
if(s->exit) {
run = 0;
break;
}
}
if(!s->exit) {
apr_global_mutex_lock(s->lock); /* @CRT47 */
now = time(NULL);
if(*s->qsstatustimer <= now) {
// set next and fetch the data
*s->qsstatustimer = (now/60*60 + 60);
run = 1;
} else {
// another child already did the job
run = 0;
}
apr_global_mutex_unlock(s->lock); /* @CRT47 */
}
if(!s->exit && run) {
for(i = 0; i < server_limit; ++i) {
int j;
for(j = 0; j < thread_limit; ++j) {
int res;
ap_copy_scoreboard_worker(&ws_record, i, j);
res = ws_record.status;
if(res == SERVER_DEAD) {
s_open++;
} else if(res == SERVER_READY) {
s_ready++;
} else if(res == SERVER_BUSY_READ) {
s_read++;
s_busy++;
} else if(res == SERVER_BUSY_WRITE) {
s_write++;
s_busy++;
} else if(res == SERVER_BUSY_KEEPALIVE) {
s_keep++;
s_busy++;
} else if(res == SERVER_STARTING) {
s_start++;
} else if(res == SERVER_BUSY_LOG) {
s_log++;
s_busy++;
} else if(res == SERVER_BUSY_DNS) {
s_dns++;
s_busy++;
} else if(res == SERVER_CLOSING) {
s_closing++;
} else if(res == SERVER_GRACEFUL) {
s_usr1++;
} else if(res == SERVER_IDLE_KILL) {
s_kill++;
}
}
}
clientContentTypes[0] = '\0';
if(s->sconf->qos_cc_prefer) {
qos_user_t *u = qos_get_user_conf(s->sconf->act->ppool);
if(u) {
unsigned long long html;
unsigned long long cssjs;
unsigned long long img;
unsigned long long other;
unsigned long long notmodified;
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT48 */
html = u->qos_cc->html;
cssjs = u->qos_cc->cssjs;
img = u->qos_cc->img;
other = u->qos_cc->other;
notmodified = u->qos_cc->notmodified;
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT48 */
snprintf(clientContentTypes, 8191, ", \"clientContentTypes\": { "
"\"html\": %llu, \"css/js\": %llu,"
" \"images\": %llu, \"other\": %llu, \"304\": %llu }",
html, cssjs,
img, other, notmodified
);
}
}
allConn[0] = '\0';
if(qos_count_connections(s->sconf)) {
apr_global_mutex_lock(s->lock); /* @CRT52 */
int all_connections = qos_server_connections(s->sconf);
snprintf(allConn, 64, ", \"QS_AllConn\": %d",
all_connections
);
apr_global_mutex_unlock(s->lock); /* @CRT52 */
}
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s->sconf->base_server,
QOS_LOG_PFX(200)"{ \"scoreboard\": { "
"\"open\": %d, \"waiting\": %d, \"read\": %d, "
"\"write\": %d, \"keepalive\": %d, "
"\"start\": %d, \"log\": %d, "
"\"dns\": %d, \"closing\": %d, "
"\"finishing\": %d, \"idle\": %d }, "
"\"maxclients\": { "
"\"max\": %d, \"busy\": %d"
"%s }"
"%s }",
s_open, s_ready, s_read,
s_write, s_keep,
s_start, s_log,
s_dns, s_closing,
s_usr1, s_kill,
s->maxclients, s_busy,
allConn,
clientContentTypes);
}
}
if(m_threaded_mpm) {
apr_thread_exit(thread, APR_SUCCESS);
}
return NULL;
}
/**
* Starts the stats logger thread
*/
static void qos_init_status_thread(apr_pool_t *p, qos_srv_config *sconf, int maxclients) {
qs_actable_t *act = sconf->act;
apr_pool_t *pool;
apr_threadattr_t *tattr;
qsstatus_t *s;
apr_pool_create(&pool, NULL);
s = apr_pcalloc(pool, sizeof(qsstatus_t));
s->exit = 0;
s->pool = pool;
s->maxclients = maxclients;
s->qsstatustimer = act->qsstatustimer;
s->lock = act->lock;
s->sconf = sconf;
if(apr_threadattr_create(&tattr, pool) == APR_SUCCESS) {
if(apr_thread_create(&s->thread, tattr, qos_status_thread, s, pool) == APR_SUCCESS) {
apr_pool_pre_cleanup_register(p, s, qos_cleanup_status_thread);
}
}
}
#endif
/**
* client control rules at process connection handler
*/
static int qos_cc_pc_filter(conn_rec *connection, qs_conn_ctx *cconf, qos_user_t *u, char **msg) {
conn_rec *c = connection;
int ret = DECLINED;
if(cconf && cconf->sconf->has_qos_cc) {
qos_s_entry_t **clientEntry = NULL;
qos_s_entry_t searchE;
searchE.ip6[0] = cconf->ip6[0];
searchE.ip6[1] = cconf->ip6[1];
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT14 */
clientEntry = qos_cc_getOrSet(u->qos_cc, &searchE, 0);
/* early vip detection */
if((*clientEntry)->vip) {
cconf->is_vip = 1;
apr_table_set(c->notes, QS_ISVIPREQ, "yes");
}
/* max connections */
if(cconf->sconf->has_qos_cc && cconf->sconf->qos_cc_prefer) {
if(m_generation != u->qos_cc->generation_locked) {
u->qos_cc->connections++;
} else {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, c->base_server,
QOS_LOG_PFX(166)"unexpected connection dispatching, skipping"
" connection counter update for QS_ClientPrefer rule, c=%s",
QS_CONN_REMOTEIP(cconf->mc) == NULL ? "-" :
QS_CONN_REMOTEIP(cconf->mc));
}
if((*clientEntry)->lowrate) {
if(c->notes) {
char *flags = apr_psprintf(c->pool, "0x%02x", (*clientEntry)->lowratestatus);
apr_table_set(c->notes, "QS_ClientLowPrio", flags);
}
}
/* non vip (allow all vip addresses - no restrictions) */
if(!(*clientEntry)->vip) {
if(u->qos_cc->connections > cconf->sconf->qos_cc_prefer_limit) {
int penalty = 4; // 2 to 12 points
int reqSpare = 0;
if((*clientEntry)->lowrate) {
if((*clientEntry)->lowratestatus & QOS_LOW_FLAG_PKGRATE) {
penalty++;
}
if((*clientEntry)->lowratestatus & QOS_LOW_FLAG_BEHAVIOR_BAD) {
penalty+=2;
}
if((*clientEntry)->lowratestatus & QOS_LOW_FLAG_EVENTBLOCK) {
penalty+=2;
}
if((*clientEntry)->lowratestatus & QOS_LOW_FLAG_EVENTLIMIT) {
penalty+=2;
}
if((*clientEntry)->lowratestatus & QOS_LOW_FLAG_TIMEOUT) {
penalty++;
}
}
if((*clientEntry)->lowratestatus & QOS_LOW_FLAG_BEHAVIOR_OK) {
// normal behavior
penalty-=2;
}
// calculate the min. required free connections allowing us to serve request by this IP
reqSpare = (cconf->sconf->max_clients - cconf->sconf->qos_cc_prefer_limit) * penalty / 12;
if((cconf->sconf->max_clients - u->qos_cc->connections) < reqSpare) {
/* not enough free connections */
if(u->qos_cc->connections > cconf->sconf->qos_cc_prefer_limit) {
*msg = apr_psprintf(cconf->mc->pool,
QOS_LOG_PFX(066)"access denied%s, "
"QS_ClientPrefer rule (penalty=%d 0x%02x): "
"max=%d, concurrent connections=%d, c=%s",
cconf->sconf->log_only ? " (log only)" : "",
penalty, (*clientEntry)->lowratestatus,
cconf->sconf->qos_cc_prefer_limit, u->qos_cc->connections,
QS_CONN_REMOTEIP(cconf->mc) == NULL ? "-" :
QS_CONN_REMOTEIP(cconf->mc));
QS_INC_EVENT_LOCKED(cconf->sconf, 66);
ret = m_retcode;
}
}
}
}
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT14 */
}
return ret;
}
/**
* calculates the current minimal up/download bandwidth
*/
static int qos_req_rate_calc(qos_srv_config *sconf, int *current) {
int req_rate = sconf->req_rate;
if(sconf->min_rate_max != -1) {
int connections = qos_server_connections(sconf);
if(connections > sconf->req_rate_start) {
/* keep the minimal rate until reaching the min connections */
req_rate = req_rate + (sconf->min_rate_max * connections / sconf->max_clients);
if(connections > sconf->max_clients) {
// limit the max rate to its max if we have more connections then expected
if(connections > (sconf->max_clients + QS_DOUBLE_CONN_H)) {
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, sconf->base_server,
QOS_LOG_PFX(036)"QS_SrvMinDataRate: unexpected connection status!"
" connections=%d,"
" cal. request rate=%d,"
" max. limit=%d."
" Check log for unclean child exit and consider"
" to do a graceful server restart if this condition persists."
" You might also increase the number of supported connections"
" using the 'QS_MaxClients' directive.",
connections, req_rate, sconf->min_rate_max);
}
QS_INC_EVENT(sconf, 36);
req_rate = sconf->min_rate_max;
}
}
*current = connections;
}
return req_rate;
}
qos_s_entry_limit_conf_t *qos_getQSLimitEvent(qos_user_t *u, const char *event,
int *limitTableIndex) {
int i = 0;
apr_table_entry_t *limitTableEntry = (apr_table_entry_t *)apr_table_elts(u->qos_cc->limitTable)->elts;
for(i = 0; i < apr_table_elts(u->qos_cc->limitTable)->nelts; i++) {
const char *eventName = limitTableEntry[i].key;
if(strcasecmp(eventName, event) == 0) {
*limitTableIndex = i;
return (qos_s_entry_limit_conf_t *)limitTableEntry[i].val;
}
}
return NULL;
}
/************************************************************************
* "public"
***********************************************************************/
/**
* short status viewer
*/
static void qos_ext_status_short(request_rec *r, apr_table_t *qt) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config,
&qos_module);
server_rec *s = sconf->base_server;
qos_srv_config *bsconf = (qos_srv_config*)ap_get_module_config(s->module_config,
&qos_module);
const char *option = apr_table_get(qt, "option");
const char *all_connections = apr_table_get(r->subprocess_env, "QS_AllConn");
apr_time_t now = apr_time_sec(r->request_time);
double av[1];
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
#if (!defined(WIN32) && !defined(__MINGW32__) && !defined(_WIN64) && !defined(CYGWIN))
getloadavg(av, 1);
ap_rprintf(r, "b"QOS_DELIM"system.load: %.2f\n", av[0]);
#endif
if(u->qos_cc && sconf->qos_cc_prefer) {
unsigned long long html;
unsigned long long cssjs;
unsigned long long img;
unsigned long long other;
unsigned long long notmodified;
char clientContentTypes[8192];
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT51 */
html = u->qos_cc->html;
cssjs = u->qos_cc->cssjs;
img = u->qos_cc->img;
other = u->qos_cc->other;
notmodified = u->qos_cc->notmodified;
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT51 */
snprintf(clientContentTypes, 8191,
"b;clientContentTypes(html,css/js,images,other,304): "
"%llu %llu %llu %llu %llu",
html, cssjs, img, other, notmodified
);
ap_rprintf(r, "%s\n", clientContentTypes);
}
while(s) {
char *sn = apr_psprintf(r->pool, "%s"QOS_DELIM"%s"QOS_DELIM"%d",
s->is_virtual ? "v" : "b",
s->server_hostname == NULL ? "-" :
ap_escape_html(r->pool, s->server_hostname),
s->addrs->host_port);
sconf = (qos_srv_config*)ap_get_module_config(s->module_config, &qos_module);
if(all_connections && !s->is_virtual) {
ap_rprintf(r, "%s"QOS_DELIM"QS_AllConn: %s\n", sn, all_connections);
}
if((s->is_virtual && (sconf != bsconf)) || !s->is_virtual) {
qs_acentry_t *actEntry;
if(!s->is_virtual && sconf->has_qos_cc && sconf->qos_cc_prefer_limit) {
int hc = u->qos_cc->connections; /* not synchronized ... */
ap_rprintf(r, "%s"QOS_DELIM"QS_ClientPrefer"QOS_DELIM"%d[]: %d\n", sn,
sconf->qos_cc_prefer_limit, hc);
}
/* request level */
actEntry = sconf->act->entry;
while(actEntry) {
if((actEntry->limit > 0) && !actEntry->condition && !actEntry->event) {
ap_rprintf(r, "%s"QOS_DELIM"QS_LocRequestLimit%s"QOS_DELIM"%d[%s]: %d\n", sn,
actEntry->regex == NULL ? "" : "Match",
actEntry->limit,
actEntry->url,
actEntry->counter);
}
if((actEntry->req_per_sec_limit > 0) && !actEntry->event) {
ap_rprintf(r, "%s"QOS_DELIM"QS_LocRequestPerSecLimit%s"QOS_DELIM"%ld[%s]: %ld\n", sn,
actEntry->regex == NULL ? "" : "Match",
actEntry->req_per_sec_limit,
actEntry->url,
actEntry->req_per_sec);
}
if((actEntry->kbytes_per_sec_limit > 0) && !actEntry->event) {
ap_rprintf(r, "%s"QOS_DELIM"QS_LocKBytesPerSecLimit%s"QOS_DELIM"%"APR_OFF_T_FMT"[%s]: %"APR_OFF_T_FMT"\n", sn,
actEntry->regex == NULL ? "" : "Match",
actEntry->kbytes_per_sec_limit,
actEntry->url,
actEntry->kbytes_per_sec);
}
if(actEntry->condition && !actEntry->event) {
ap_rprintf(r, "%s"QOS_DELIM"QS_CondLocRequestLimitMatch"QOS_DELIM"%d[%s]: %d\n", sn,
actEntry->limit,
actEntry->url,
actEntry->counter);
}
if(actEntry->event && (actEntry->limit != -1)) {
ap_rprintf(r, "%s"QOS_DELIM"QS_EventRequestLimit"QOS_DELIM"%d[%s]: %d\n", sn,
actEntry->limit,
actEntry->url,
actEntry->counter);
}
if(actEntry->event && (actEntry->kbytes_per_sec_limit != 0)) {
ap_rprintf(r, "%s"QOS_DELIM"QS_EventKBytesPerSecLimit"QOS_DELIM"%"APR_OFF_T_FMT"[%s]: %"APR_OFF_T_FMT"\n", sn,
actEntry->kbytes_per_sec_limit,
actEntry->url,
now > (apr_time_sec(actEntry->kbytes_interval_us) + (QS_BW_SAMPLING_RATE*10)) ? 0 : actEntry->kbytes_per_sec);
}
if(actEntry->event && (actEntry->req_per_sec_limit > 0)) {
ap_rprintf(r, "%s"QOS_DELIM"QS_EventPerSecLimit"QOS_DELIM"%ld[%s]: %ld\n", sn,
actEntry->req_per_sec_limit,
actEntry->url,
now > (actEntry->interval + (QS_BW_SAMPLING_RATE*3)) ? 0 : actEntry->req_per_sec);
}
actEntry = actEntry->next;
}
/* event limit */
if(sconf->event_limit_a->nelts > 0) {
int ie = 0;
qos_event_limit_entry_t *event_limit = sconf->act->event_entry;
for(ie = 0; ie < sconf->event_limit_a->nelts; ie++) {
int elimit = event_limit->limit;
if(event_limit->limitTime + event_limit->seconds <= now) {
elimit = 0;
}
if(event_limit->action == QS_EVENT_ACTION_DENY) {
ap_rprintf(r, "%s"QOS_DELIM"QS_%sEventLimitCount"QOS_DELIM"%d/%d[%s]: %d\n",
sn,
event_limit->condStr == NULL ? "" : "Cond",
event_limit->max,
event_limit->seconds,
event_limit->env_var,
elimit);
}
event_limit++;
}
}
if(!s->is_virtual || sconf->act->conn != bsconf->act->conn) {
if(sconf->max_conn != -1) {
ap_rprintf(r, "%s"QOS_DELIM"QS_SrvMaxConn"QOS_DELIM"%d[]: %d\n", sn,
sconf->max_conn,
sconf->act->conn->connections);
}
if(sconf->max_conn_close != -1) {
ap_rprintf(r, "%s"QOS_DELIM"QS_SrvMaxConnClose"QOS_DELIM"%d[]: %d\n", sn,
sconf->max_conn_close,
sconf->act->conn->connections);
}
if(option && strstr(option, "ip")) {
if(sconf->act->conn->connections) {
apr_table_t *entries = apr_table_make(r->pool, 100);
int j;
apr_table_entry_t *entry;
qos_collect_ip(r, sconf, entries, sconf->max_conn_per_ip, 0);
entry = (apr_table_entry_t *)apr_table_elts(entries)->elts;
for(j = 0; j < apr_table_elts(entries)->nelts; j++) {
ap_rprintf(r, "%s"QOS_DELIM"QS_SrvMaxConnPerIP"QOS_DELIM"%s: %s\n",
sn,
entry[j].key, entry[j].val);
}
}
}
}
}
s = s->next;
}
if(u->qos_cc && sconf->qsevents) {
int i = 0;
apr_table_t *eTable = apr_table_make(r->pool, QOS_LOG_MSGCT);
apr_table_entry_t *entry;
char buf1[1024];
char buf2[1024];
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT50 */
while(m_knownEvents[i] > 0) {
snprintf(buf1, 1023, "e;mod_qos(%03d);new: %llu",
m_knownEvents[i], u->qos_cc->eventLast[m_knownEvents[i]]);
snprintf(buf2, 1023, "e;mod_qos(%03d);total: %llu",
m_knownEvents[i], u->qos_cc->eventTotal[m_knownEvents[i]]);
apr_table_add(eTable, buf1, buf2);
u->qos_cc->eventLast[m_knownEvents[i]] = 0;
i++;
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT50 */
entry = (apr_table_entry_t *)apr_table_elts(eTable)->elts;
for(i = 0; i < apr_table_elts(eTable)->nelts; ++i) {
ap_rprintf(r, "%s\n%s\n", entry[i].key, entry[i].val);
}
}
}
/**
* Comperator for bsearch function
*/
static int qos_geo_comp(const void *_pA, const void *_pB) {
unsigned long *pA = (unsigned long *)_pA;
qos_geo_entry_t *pB = (qos_geo_entry_t *)_pB;
unsigned long search = *pA;
if((search >= pB->start) && (search <= pB->end)) return 0;
if(search > pB->start) return 1;
if(search < pB->start) return -1;
return -1; // error
}
/**
* Translates an IP address (from geo csv) to a numeric value.
*
* @param pool To dup the string while parsing.
* @param ip
* @return
*/
static unsigned long qos_geo_str2long(apr_pool_t *pool, const char *ip) {
char *p;
char *i = apr_pstrdup(pool, ip);
unsigned long addr = 0;
p = strchr(i, '.');
if(!p) return 0;
p[0] = '\0';
if(!qos_is_num(i)) return 0;
addr += (atol(i) * 16777216);
i = p;
i++;
p = strchr(i, '.');
if(!p) return 0;
p[0] = '\0';
if(!qos_is_num(i)) return 0;
addr += (atol(i) * 65536);
i = p;
i++;
p = strchr(i, '.');
if(!p) return 0;
p[0] = '\0';
if(!qos_is_num(i)) return 0;
addr += (atol(i) * 256);
i = p;
i++;
if(!qos_is_num(i)) return 0;
addr += (atol(i));
return addr;
}
/**
* Viewer settings about ip address information.
*/
static void qos_show_ip(request_rec *r, qos_srv_config *sconf, apr_table_t *qt) {
int max_conn_per_ip = 0;
server_rec *s = sconf->base_server;
apr_time_t now = apr_time_sec(r->request_time);
while(s) {
// enable per client connection search if any server has enabled QS_SrvMaxConnPerIP
qos_srv_config *conf = (qos_srv_config*)ap_get_module_config(s->module_config, &qos_module);
if(conf->max_conn_per_ip != -1) {
max_conn_per_ip = 1;
break;
}
s = s->next;
}
if(sconf->has_qos_cc || max_conn_per_ip) {
const char *option = apr_table_get(qt, "option");
const char *refresh = apr_table_get(qt, "refresh");
const char *address = apr_table_get(qt, "address");
if(address) {
int escerr = 0;
char *ta = apr_pstrdup(r->pool, address);
qos_unescaping(ta, QOS_DEC_MODE_FLAGS_URL, &escerr);
address = ta;
}
if(strcmp(r->handler, "qos-viewer") == 0) {
ap_rputs("<table class=\"btable\"><tbody>\n", r);
ap_rputs(" <tr class=\"row\"><td>\n", r);
} else {
ap_rputs("<table border=\"1\"><tbody>\n", r);
ap_rputs(" <tr><td>\n", r);
}
if(strcmp(r->handler, "qos-viewer") == 0) {
ap_rputs(" <table border=\"0\" cellpadding=\"2\" "
"cellspacing=\"2\" style=\"width: 100%\"><tbody>\n",r);
} else {
ap_rputs(" <table border=\"1\" cellpadding=\"2\" "
"cellspacing=\"2\" style=\"width: 100%\"><tbody>\n",r);
}
ap_rputs(" <tr class=\"rowe\">\n", r);
ap_rputs(" <td colspan=\"9\">viewer settings</td>\n", r);
ap_rputs(" </tr>\n", r);
/* auto refresh */
ap_rputs(" <tr class=\"rows\">\n"
" <td colspan=\"1\">auto refresh</td>\n", r);
ap_rputs(" <td colspan=\"8\">\n", r);
ap_rprintf(r, " <form action=\"%s\" method=\"get\">\n",
ap_escape_html(r->pool, r->parsed_uri.path));
if(option && strstr(option, "ip")) {
ap_rprintf(r, " <input name=\"option\" value=\"ip\" type=\"hidden\">\n");
}
if(address) {
ap_rprintf(r, " <input name=\"address\" value=\"%s\" type=\"hidden\">\n",
ap_escape_html(r->pool, address));
}
if(refresh) {
ap_rprintf(r, " <input name=\"action\" value=\"disable\" type=\"submit\">\n");
} else {
ap_rprintf(r, " <input name=\"action\" value=\"enable\" type=\"submit\">\n");
ap_rprintf(r, " <input name=\"refresh\" value=\"\" type=\"hidden\">\n");
}
ap_rputs(" </form>\n", r);
ap_rputs(" </td>\n", r);
ap_rputs(" </tr>\n", r);
/* show ip addresses and their connections */
ap_rputs(" <tr class=\"rows\">\n"
" <td colspan=\"1\">client ip connections</td>\n", r);
ap_rputs(" <td colspan=\"8\">\n", r);
ap_rprintf(r, " <form action=\"%s\" method=\"get\">\n",
ap_escape_html(r->pool, r->parsed_uri.path));
if(!option || (option && !strstr(option, "ip")) ) {
ap_rprintf(r, " <input name=\"option\" value=\"ip\" type=\"hidden\">\n");
ap_rprintf(r, " <input name=\"action\" value=\"enable\" type=\"submit\">\n");
} else {
ap_rprintf(r, " <input name=\"option\" value=\"no\" type=\"hidden\">\n");
ap_rprintf(r, " <input name=\"action\" value=\"disable\" type=\"submit\">\n");
}
if(address) {
ap_rprintf(r, " <input name=\"address\" value=\"%s\" type=\"hidden\">\n",
ap_escape_html(r->pool, address));
}
if(refresh) {
ap_rprintf(r, " <input name=\"refresh\" value=\"\" type=\"hidden\">\n");
}
ap_rputs(" </form>\n", r);
ap_rputs(" </td>\n", r);
ap_rputs(" </tr>\n", r);
if(sconf->has_qos_cc) {
ap_rputs(" <tr class=\"rows\">\n"
" <td colspan=\"1\">search a client ip entry</td>\n", r);
ap_rputs(" <td colspan=\"8\">\n", r);
ap_rprintf(r, " <form action=\"%s\" method=\"get\">\n",
ap_escape_html(r->pool, r->parsed_uri.path));
if(option && strstr(option, "ip")) {
ap_rprintf(r, " <input name=\"option\" value=\"ip\" type=\"hidden\">\n");
}
if(refresh) {
ap_rprintf(r, " <input name=\"refresh\" value=\"\" type=\"hidden\">\n");
}
ap_rprintf(r, " <input name=\"address\" value=\"%s\" type=\"text\">\n",
address ? ap_escape_html(r->pool, address) : "0.0.0.0");
ap_rprintf(r, " <input name=\"action\" value=\"search\" type=\"submit\">\n");
ap_rputs(" </form>\n", r);
ap_rputs(" </td>\n", r);
ap_rputs(" </tr>\n", r);
if(address) {
apr_uint64_t ip[2];
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
if(qos_ip_str2long(address, ip)) {
unsigned long html;
unsigned long cssjs;
unsigned long img;
unsigned long other;
unsigned long notmodified;
qos_s_entry_t **clientEntry = NULL;
qos_s_entry_t searchE;
int found = 0;
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT20 */
html = u->qos_cc->html;
cssjs = u->qos_cc->cssjs;
img = u->qos_cc->img;
other = u->qos_cc->other;
notmodified = u->qos_cc->notmodified;
searchE.ip6[0] = ip[0];
searchE.ip6[1] = ip[1];
clientEntry = qos_cc_get0(u->qos_cc, &searchE, 0);
if(clientEntry) {
found = 1;
searchE.vip = (*clientEntry)->vip;
searchE.lowrate = (*clientEntry)->lowrate;
searchE.lowratestatus = (*clientEntry)->lowratestatus;
searchE.time = (*clientEntry)->time;
searchE.block = (*clientEntry)->block;
searchE.blockTime = (*clientEntry)->blockTime;
searchE.limit = (*clientEntry)->limit;
searchE.req_per_sec = (*clientEntry)->req_per_sec;
searchE.req_per_sec_block_rate = (*clientEntry)->req_per_sec_block_rate;
searchE.other = (*clientEntry)->other - 1;
searchE.html = (*clientEntry)->html - 1;
searchE.cssjs = (*clientEntry)->cssjs - 1;
searchE.img = (*clientEntry)->img - 1;
searchE.notmodified = (*clientEntry)->notmodified - 1;
searchE.event_req = (*clientEntry)->event_req;
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT20 */
ap_rputs(" <tr class=\"rowt\">\n", r);
ap_rputs(" <td colspan=\"1\">IP</td>\n", r);
ap_rputs(" <td colspan=\"2\">last request</td>\n", r);
ap_rputs(" <td colspan=\"1\">"
"<div title=\"QS_VipHeaderName|QS_VipIPHeaderName\">vip</div></td>\n", r);
ap_rputs(" <td colspan=\"1\">"
"<div title=\"QS_ClientEventBlockCount\">blocked</div></td>\n", r);
ap_rputs(" <td colspan=\"1\">"
"<div title=\"QS_ClientEventLimitCount (QS_Limit)\">limited</div></td>\n", r);
ap_rputs(" <td colspan=\"2\">"
"<div title=\"QS_ClientEventPerSecLimit\">events/sec</div></td>\n", r);
ap_rputs(" <td colspan=\"1\">"
"<div title=\"QS_ClientPrefer
0x01 bad pkg rate
0x02 normal behavior
0x04 bad behavior
0x08 blocked
0x10 limited
0x20 timeout\">low prio</div></td>\n", r);
ap_rputs(" </tr>\n", r);
ap_rprintf(r, " <tr class=\"rows\">"
"<td colspan=\"1\">%s</td>", ap_escape_html(r->pool, address));
if(!found) {
ap_rputs("<td colspan=\"8\"><i>not found</i></td>\n", r);
} else {
char buf[1024];
struct tm *ptr = localtime(&searchE.time);
strftime(buf, sizeof(buf), "%d.%m.%Y %H:%M:%S", ptr);
ap_rprintf(r, "<td colspan=\"2\">%s</td>", buf);
ap_rprintf(r, "<td colspan=\"1\">%s</td>", searchE.vip ? "yes" : "no");
if(sconf->qos_cc_blockTime > (time(NULL) - searchE.blockTime)) {
ap_rprintf(r, "<td colspan=\"1\">%d, %ld sec</td>",
searchE.block, time(NULL) - searchE.blockTime);
} else {
ap_rprintf(r, "<td colspan=\"1\">no</td>");
}
if(u->qos_cc->limitTable) {
int limitTableIndex;
qos_s_entry_limit_conf_t *eventLimitConf = qos_getQSLimitEvent(u, QS_LIMIT_DEFAULT, &limitTableIndex);
if(eventLimitConf) {
if(eventLimitConf->limitTime > (time(NULL) - searchE.limit[limitTableIndex].limitTime)) {
ap_rprintf(r, "<td colspan=\"1\">%hu, %ld sec</td>",
searchE.limit[limitTableIndex].limit, time(NULL) - searchE.limit[limitTableIndex].limitTime);
} else {
ap_rprintf(r, "<td colspan=\"1\">no</td>");
}
} else {
ap_rprintf(r, "<td colspan=\"1\">off</td>");
}
} else {
ap_rprintf(r, "<td colspan=\"1\">off</td>");
}
ap_rprintf(r, "<td colspan=\"1\">%ld</td>", searchE.req_per_sec);
ap_rprintf(r, "<td colspan=\"1\">%d ms</td>", searchE.req_per_sec_block_rate);
ap_rprintf(r, "<td colspan=\"1\">%s (0x%02x)</td>\n",
(searchE.lowrate + QOS_LOW_TIMEOUT) > now ? "yes" : "no",
searchE.lowratestatus);
ap_rputs("</tr>\n", r);
ap_rprintf(r, " <tr class=\"rows\">"
"<td colspan=\"6\"> </td>"
"<td>"
"<div title=\"QS_ClientEventRequestLimit\">events:</div></td>"
"<td style=\"width:9%%\">%s</td>"
"<td colspan=\"1\"></td>"
"</tr>\n", (sconf->qos_cc_event_req == -1 ? "off" : apr_psprintf(r->pool, "%d", searchE.event_req)));
}
if(sconf->qos_cc_prefer) {
ap_rprintf(r, " <tr class=\"rowt\">"
"<td colspan=\"4\"></td>"
"<td style=\"width:9%%\">html</td>"
"<td style=\"width:9%%\">css/js</td>"
"<td style=\"width:9%%\">images</td>"
"<td style=\"width:9%%\">other</td>"
"<td style=\"width:9%%\">304</td>"
"</tr>\n");
if(found) {
ap_rprintf(r, " <tr class=\"rows\">"
"<td colspan=\"4\"></td>"
"<td style=\"width:9%%\">%u</td>"
"<td style=\"width:9%%\">%u</td>"
"<td style=\"width:9%%\">%u</td>"
"<td style=\"width:9%%\">%u</td>"
"<td style=\"width:9%%\">%u</td>"
"</tr>\n", searchE.html,
searchE.cssjs,
searchE.img,
searchE.other,
searchE.notmodified);
}
ap_rprintf(r, " <tr class=\"rows\">"
"<td colspan=\"3\"></td>"
"<td style=\"width:9%%\"><div title=\"QS_ClientContentTypes\">all clients</div></td>"
"<td style=\"width:9%%\">%lu</td>"
"<td style=\"width:9%%\">%lu</td>"
"<td style=\"width:9%%\">%lu</td>"
"<td style=\"width:9%%\">%lu</td>"
"<td style=\"width:9%%\">%lu</td>"
"</tr>\n", html, cssjs, img, other, notmodified);
if(sconf->static_on == 1) {
unsigned long shtml = sconf->static_html;
unsigned long scssjs = sconf->static_cssjs;
unsigned long simg = sconf->static_img;
unsigned long sother = sconf->static_other;
unsigned long snotmodified = sconf->static_notmodified;
ap_rprintf(r, " <tr class=\"rows\">"
"<td colspan=\"3\"></td>"
"<td style=\"width:9%%\"><div title=\"QS_ClientContentTypes\">configured (global)</div></td>"
"<td style=\"width:9%%\">%lu</td>"
"<td style=\"width:9%%\">%lu</td>"
"<td style=\"width:9%%\">%lu</td>"
"<td style=\"width:9%%\">%lu</td>"
"<td style=\"width:9%%\">%lu</td>"
"</tr>\n", shtml, scssjs,
simg, sother, snotmodified);
}
}
}
}
}
ap_rprintf(r, " <tr class=\"row\">"
"<td style=\"width:28%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"</tr>\n");
ap_rputs(" </tbody></table>\n", r);
ap_rputs(" </tr></td>\n", r);
ap_rputs("</tbody></table>\n", r);
}
}
/**
* Daws the load/connection bars at the top of the status page
*
* @param r
* @param bs
*/
static void qos_bars(request_rec *r, server_rec *bs) {
server_rec *s = bs;
qos_srv_config *bsconf = (qos_srv_config*)ap_get_module_config(s->module_config, &qos_module);
if(bsconf->act && bsconf->act->conn) {
int connections = -1;
double av[1];
int load = 0;
#if (!defined(WIN32) && !defined(__MINGW32__) && !defined(_WIN64) && !defined(CYGWIN))
getloadavg(av, 1);
load = av[0];
if(load > 0) {
load = 100*load/(10+load);
}
#endif
ap_rputs("<table class=\"btable\"><tbody>\n", r);
ap_rputs(" <tr class=\"row\"><td>\n", r);
ap_rputs("<table border=\"0\" cellpadding=\"2\" "
"cellspacing=\"2\" style=\"width: 100%\"><tbody>\n",r);
ap_rputs("<tr class=\"rowe\">\n", r);
ap_rputs("<td colspan=\"2\">overview</td>", r);
ap_rputs("</tr>\n", r);
if(bsconf->log_only) {
ap_rputs("<tr class=\"rowt\">\n", r);
ap_rputs("<td colspan=\"2\">running in 'log only' mode - rules are NOT enforced</td>", r);
ap_rputs("</tr>\n", r);
}
if(qos_count_connections(bsconf)) {
connections = qos_server_connections(bsconf);
}
if(connections != -1) {
#if (!defined(WIN32) && !defined(__MINGW32__) && !defined(_WIN64))
ap_rprintf(r, "<tr class=\"rowt\">"
"<td colspan=\"1\">connections: %d</td>"
"<td colspan=\"1\">load: %.2f</td>"
"</tr>\n", connections, av[0]);
#else
ap_rprintf(r, "<tr class=\"rowt\">"
"<td colspan=\"1\">connections: %d</td>"
"<td colspan=\"1\">load: n/a</td>"
"</tr>\n", connections);
#endif
} else {
#if (!defined(WIN32) && !defined(__MINGW32__) && !defined(_WIN64))
ap_rprintf(r, "<tr class=\"rowt\">"
"<td colspan=\"1\">connections: n/a</td>"
"<td colspan=\"1\">load: %.2f</td>"
"</tr>\n", av[0]);
#else
ap_rprintf(r, "<tr class=\"rowt\">"
"<td colspan=\"1\">connections: n/a</td>"
"<td colspan=\"1\">load: n/a</td>"
"</tr>\n");
#endif
}
ap_rprintf(r, "<tr class=\"rows\">");
ap_rprintf(r, "<td>");
if(connections != -1) {
int percentage = 100 * connections / bsconf->max_clients;
if(percentage > 100) percentage = 100;
ap_rprintf(r, "<div class=\"prog-border\">"
"<div class=\"%s\" style=\"width: %d%%;\"></div></div>",
percentage < 90 ? "prog-bar" : "prog-bar-limit",
percentage);
ap_rprintf(r, "</td>");
} else {
ap_rprintf(r, " ");
}
ap_rprintf(r, "<td>");
ap_rprintf(r, "<div class=\"prog-border\">"
"<div class=\"prog-bar\" style=\"width: %d%%;\"></div></div>",
load);
ap_rprintf(r, "</td>");
ap_rprintf(r, "</tr>\n");
ap_rputs("</tbody></table>\n", r);
ap_rputs(" </tr></td>\n", r);
ap_rputs("</tbody></table>\n", r);
}
}
/**
* (Extendet-)Status viewer, used by internal and mod_status handler.
*/
static int qos_ext_status_hook(request_rec *r, int flags) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config,
&qos_module);
server_rec *s = sconf->base_server;
int i = 0;
apr_time_t now = apr_time_sec(r->request_time);
qos_srv_config *bsconf = (qos_srv_config*)ap_get_module_config(s->module_config,
&qos_module);
apr_table_t *qt = qos_get_query_table(r);
const char *option = apr_table_get(qt, "option");
if(sconf->disable_handler == 1) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(072)"handler has been disabled for this host, id=%s",
qos_unique_id(r, "072"));
return OK;
}
if (flags & AP_STATUS_SHORT) {
qos_ext_status_short(r, qt);
return OK;
}
if(qt && (apr_table_get(qt, "auto") != NULL)) {
qos_ext_status_short(r, qt);
return OK;
}
if(strcmp(r->handler, "qos-viewer") != 0) {
ap_rputs("<hr>\n", r);
ap_rputs("<table style=\"width:400px\" cellspacing=0 cellpadding=0>\n", r);
ap_rputs(" <tr><td bgcolor=\"#000000\">\n", r);
ap_rputs(" <b><font color=\"#ffffff\" face=\"Arial,Helvetica\">", r);
ap_rprintf(r, "mod_qos %s", ap_escape_html(r->pool, qos_revision(r->pool)));
ap_rputs(" </font></b>\r", r);
ap_rputs(" </td></tr>\n", r);
ap_rputs("</table>\n", r);
if(sconf->log_only) {
ap_rputs("<p>running in 'log only' mode - rules are NOT enforced</p>\n", r);
}
}
#ifdef QS_INTERNAL_TEST
{
apr_uint64_t remoteip[2];
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
qos_ip_str2long(QS_CONN_REMOTEIP(r->connection), remoteip);
if(cconf) {
remoteip[0] = cconf->ip6[0];
remoteip[1] = cconf->ip6[1];
}
ap_rputs("<p>TEST BINARY, NOT FOR PRODUCTIVE USE<br>\n", r);
ap_rprintf(r, "client ip=%s</p>\n", qos_ip_long2str(r->pool, remoteip));
}
#endif
if(strcmp(r->handler, "qos-viewer") == 0) {
qos_bars(r, s);
}
qos_show_ip(r, bsconf, qt);
if(strcmp(r->handler, "qos-viewer") == 0) {
ap_rputs("<table class=\"btable\"><tbody>\n", r);
ap_rputs(" <tr class=\"row\"><td>\n", r);
} else {
ap_rputs("<table border=\"1\"><tbody>\n", r);
ap_rputs(" <tr><td>\n", r);
}
while(s) {
qs_acentry_t *actEntry;
if(strcmp(r->handler, "qos-viewer") == 0) {
ap_rputs(" <table border=\"0\" cellpadding=\"2\" "
"cellspacing=\"2\" style=\"width: 100%\"><tbody>\n",r);
} else {
ap_rputs(" <table border=\"1\" cellpadding=\"2\" "
"cellspacing=\"2\" style=\"width: 100%\"><tbody>\n",r);
}
ap_rputs(" <tr class=\"rowe\">\n", r);
ap_rprintf(r, " <td colspan=\"9\">%s:%d (%s)</td>\n",
s->server_hostname == NULL ? "-" : ap_escape_html(r->pool, s->server_hostname),
s->addrs->host_port,
s->is_virtual ? "virtual" : "base");
ap_rputs(" </tr>\n", r);
sconf = (qos_srv_config*)ap_get_module_config(s->module_config, &qos_module);
if((sconf == bsconf) && s->is_virtual) {
ap_rputs(" <tr class=\"rows\">\n"
" <td colspan=\"9\"><i>uses base server settings and counters</i></td>\n </tr>\n", r);
} else {
if(!s->is_virtual && sconf->has_qos_cc) {
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
int num = 0;
int max = 0;
int hc = -1;
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT16 */
hc = u->qos_cc->connections;
num = u->qos_cc->num;
max = u->qos_cc->max;
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT16 */
ap_rputs("<tr class=\"rowt\">"
"<td colspan=\"6\">client control</td>"
"<td >max</td>"
"<td >limit </td>"
"<td >current </td>", r);
ap_rputs("</tr>\n", r);
ap_rprintf(r, "<tr class=\"rows\">");
ap_rprintf(r, "<td colspan=\"6\"><div title=\"QS_ClientEntries\">clients in memory</div></td>");
ap_rprintf(r, "<td >%d</td>", max);
ap_rprintf(r, "<td >-</td>");
ap_rprintf(r, "<td >%d</td>", num);
ap_rputs("</tr>\n", r);
if(sconf->qos_cc_prefer) {
ap_rprintf(r, "<tr class=\"rows\">");
ap_rprintf(r, "<td colspan=\"6\"><div title=\"QS_ClientPrefer\">connections</div></td>");
ap_rprintf(r, "<td >%d</td>", sconf->qos_cc_prefer);
ap_rprintf(r, "<td >%d</td>", sconf->qos_cc_prefer_limit);
ap_rprintf(r, "<td >%d</td>", hc);
ap_rputs("</tr>\n", r);
}
/*
if(sconf->qos_cc_block) {
ap_rprintf(r, "<tr class=\"rows\">");
ap_rprintf(r, "<td colspan=\"6\">block event</td>");
ap_rprintf(r, "<td >%d</td>", sconf->qos_cc_block);
ap_rprintf(r, "<td > </td>");
ap_rprintf(r, "<td >%d</td>", blocked);
ap_rputs("</tr>\n", r);
}
*/
}
/* request level */
actEntry = sconf->act->entry;
if(actEntry) {
ap_rputs(" <tr class=\"rowt\">"
"<td colspan=\"1\">rule</td>"
"<td colspan=\"2\">"
"<div title=\"QS_LocRequestLimitMatch|QS_LocRequestLimit"
"|QS_CondLocRequestLimitMatch|QS_EventRequestLimit\">"
"concurrent requests</div></td>"
"<td colspan=\"3\">"
"<div title=\"QS_LocRequestPerSecLimitMatch|"
"QS_LocRequestPerSecLimit|QS_EventPerSecLimit\">"
"requests/second</div></td>"
"<td colspan=\"3\">"
"<div title=\"QS_LocKBytesPerSecLimitMatch|QS_LocKBytesPerSecLimit\">"
"kbytes/second</div></td>", r);
ap_rputs("</tr>\n", r);
ap_rputs(" <tr class=\"rowt\">"
"<td > </td>"
"<td >limit</td>"
"<td >current</td>"
"<td >wait rate</td>"
"<td >limit</td>"
"<td >current</td>"
"<td >wait rate</td>"
"<td >limit</td>"
"<td >current</td>", r);
ap_rputs("</tr>\n", r);
}
while(actEntry) {
char *red = "style=\"background-color: rgb(240,153,155);\"";
ap_rputs(" <tr class=\"rows\">", r);
ap_rprintf(r, "<!--%d--><td>%s%s</a></td>", i,
ap_escape_html(r->pool, qos_crline(r, actEntry->url)),
actEntry->condition == NULL ? "" : " <small>(conditional)</small>");
if((actEntry->limit == 0) || (actEntry->limit == -1)) {
ap_rprintf(r, "<td>-</td>");
ap_rprintf(r, "<td>-</td>");
} else {
ap_rprintf(r, "<td>%d</td>", actEntry->limit);
ap_rprintf(r, "<td %s>%d</td>",
((actEntry->counter * 100) / actEntry->limit) > 90 ? red : "",
actEntry->counter);
}
if(actEntry->req_per_sec_limit == 0) {
ap_rprintf(r, "<td>-</td>");
ap_rprintf(r, "<td>-</td>");
ap_rprintf(r, "<td>-</td>");
} else {
ap_rprintf(r, "<td %s>%d ms</td>",
actEntry->req_per_sec_block_rate ? red : "",
actEntry->req_per_sec_block_rate);
ap_rprintf(r, "<td>%ld</td>", actEntry->req_per_sec_limit);
ap_rprintf(r, "<td %s>%ld</td>",
((actEntry->req_per_sec * 100) / actEntry->req_per_sec_limit) > 90 ? red : "",
now > (actEntry->interval + (QS_BW_SAMPLING_RATE*3)) ? 0 : actEntry->req_per_sec);
}
if(actEntry->kbytes_per_sec_limit == 0) {
ap_rprintf(r, "<td>-</td>");
ap_rprintf(r, "<td>-</td>");
ap_rprintf(r, "<td>-</td>");
} else {
int hasActualData = now > (apr_time_sec(actEntry->kbytes_interval_us) + QS_BW_SAMPLING_RATE) ? 0 : 1;
ap_rprintf(r, "<td %s>%"APR_OFF_T_FMT" ms</td>",
hasActualData && (actEntry->kbytes_per_sec_block_rate >= 1000) ? red : "",
actEntry->kbytes_per_sec_block_rate / 1000);
ap_rprintf(r, "<td>%"APR_OFF_T_FMT"</td>", actEntry->kbytes_per_sec_limit);
ap_rprintf(r, "<td %s>%"APR_OFF_T_FMT"</td>",
hasActualData && (((actEntry->kbytes_per_sec * 100) / actEntry->kbytes_per_sec_limit) > 90) ? red : "",
hasActualData ? actEntry->kbytes_per_sec : 0);
}
ap_rputs("</tr>\n", r);
actEntry = actEntry->next;
}
/* event limit */
if(sconf->event_limit_a->nelts > 0) {
char *red = "style=\"background-color: rgb(240,153,155);\"";
int ie = 0;
qos_event_limit_entry_t *event_limit = sconf->act->event_entry;
ap_rputs(" <tr class=\"rowt\">"
"<td colspan=\"5\"><div title=\"QS_EventLimitCount\">events</div></td>"
"<td colspan=\"1\">limit</td>"
"<td colspan=\"1\">seconds</td>"
"<td colspan=\"2\">current</td>", r);
ap_rputs("</tr>\n", r);
for(ie = 0; ie < sconf->event_limit_a->nelts; ie++) {
int edelta = event_limit->limitTime + event_limit->seconds - now;
int elimit = event_limit->limit;
if(event_limit->limitTime + event_limit->seconds <= now) {
elimit = 0;
edelta = 0;
}
if(event_limit->action == QS_EVENT_ACTION_DENY) {
ap_rprintf(r, " <tr class=\"rows\">"
"<td colspan=\"5\">%s%s</td>"
"<td>%d</td><td>%ds</td><td %s>%d</td><td>%ds</td>"
"</tr>\n",
event_limit->env_var,
event_limit->condStr == NULL ? "" : " <small>(conditional)</small>",
event_limit->max,
event_limit->seconds,
elimit >= event_limit->max ? red : "",
elimit,
edelta);
}
event_limit++;
}
}
/* connection level */
if(sconf->has_conn_counter == 1 || !s->is_virtual) {
char *red = "style=\"background-color: rgb(240,153,155);\"";
int c = qos_count_free_ip(sconf);
ap_rputs(" <tr class=\"rowt\">"
"<td colspan=\"9\">connections</td>", r);
ap_rputs("</tr>\n", r);
ap_rprintf(r, " <tr class=\"rows\">"
"<!--%d--><td colspan=\"6\">"
"<div title=\"QS_SrvMaxConnPerIP\">free ip entries</div></td>"
"<td colspan=\"3\">%d</td></tr>\n", i, c);
ap_rprintf(r, " <tr class=\"rows\">"
"<!--%d--><td colspan=\"6\">"
"<div title=\"QS_SrvMaxConn|QS_SrvMaxConnClose\">current connections</div></td>"
"<td %s colspan=\"3\">%d</td></tr>\n", i,
( ( (sconf->max_conn_close != -1) &&
(sconf->act->conn->connections >= sconf->max_conn_close) ) ||
( (sconf->max_conn != -1) &&
(sconf->act->conn->connections >= sconf->max_conn) ) ) ? red : "",
sconf->act->conn->connections);
if(!s->is_virtual) {
ap_rprintf(r, " <tr class=\"rows\">"
"<!--base--><td colspan=\"6\">"
"<div>total connections</div></td>"
"<td colspan=\"3\">%d</td></tr>\n",
qos_server_connections(sconf));
}
if(option && strstr(option, "ip")) {
apr_table_t *entries = apr_table_make(r->pool, 100);
int j;
apr_table_entry_t *entry;
ap_rputs(" <tr class=\"rowt\">"
"<td colspan=\"6\">"
"<div title=\"QS_SrvMaxConnPerIP\">client ip connections</div></td>"
"<td colspan=\"3\">current </td>", r);
ap_rputs("</tr>\n", r);
qos_collect_ip(r, sconf, entries, sconf->max_conn_per_ip, 1);
entry = (apr_table_entry_t *)apr_table_elts(entries)->elts;
for(j = 0; j < apr_table_elts(entries)->nelts; j++) {
ap_rputs(" <tr class=\"rows\">", r);
ap_rputs("<td colspan=\"6\">", r);
ap_rprintf(r, "%s</td></tr>\n", entry[j].key);
}
}
ap_rputs(" <tr class=\"rowt\">"
"<td colspan=\"9\">connection settings</td>", r);
ap_rputs("</tr>\n", r);
ap_rprintf(r, " <tr class=\"rows\">"
"<td colspan=\"6\">"
"<div title=\"QS_SrvMaxConn\">max connections</div></td>");
if(sconf->max_conn == -1) {
ap_rprintf(r, "<td colspan=\"3\">-</td></tr>\n");
} else {
ap_rprintf(r, "<td colspan=\"3\">%d</td></tr>\n", sconf->max_conn);
}
ap_rprintf(r, " <tr class=\"rows\">"
"<td colspan=\"6\">"
"<div title=\"QS_SrvMaxConnClose\">max connections with keep-alive</div></td>");
if(sconf->max_conn_close == -1) {
ap_rprintf(r, "<td colspan=\"3\">-</td></tr>\n");
} else {
ap_rprintf(r, "<td colspan=\"3\">%d</td></tr>\n", sconf->max_conn_close);
}
ap_rprintf(r, " <tr class=\"rows\">"
"<td colspan=\"6\">"
"<div title=\"QS_SrvMaxConnPerIP\">max connections per client ip</div></td>");
if(sconf->max_conn_per_ip == -1) {
ap_rprintf(r, "<td colspan=\"3\">-</td></tr>\n");
} else {
ap_rprintf(r, "<td colspan=\"3\">%d</td></tr>\n", sconf->max_conn_per_ip);
}
ap_rprintf(r, " <tr class=\"rows\">"
"<td colspan=\"6\">"
"<div title=\"QS_SrvMinDataRate|QS_SrvRequestRate\">"
"min. data rate (bytes/sec) (min/max/current)</div></td>");
if(sconf->req_rate == -1) {
ap_rprintf(r, "<td colspan=\"3\">-</td></tr>\n");
} else {
int connections;
int rt = qos_req_rate_calc(sconf, &connections);
ap_rprintf(r, "<td colspan=\"3\">%d/%d/%d</td></tr>\n",
sconf->req_rate,
sconf->min_rate_max == -1 ? sconf->req_rate : sconf->min_rate_max,
rt);
}
} else {
ap_rputs(" <tr class=\"rowt\">"
"<td colspan=\"9\">connections</td>", r);
ap_rputs("</tr>\n", r);
ap_rputs(" <tr class=\"rows\">\n"
" <td colspan=\"9\"><i>uses base server settings and counters</i></td>\n </tr>\n", r);
}
}
ap_rprintf(r, " <tr class=\"row\">"
"<td style=\"width:28%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"<td style=\"width:9%%\"></td>"
"</tr>");
i++;
s = s->next;
ap_rputs("</tbody></table>\n", r);
}
ap_rputs(" </td></tr>\n", r);
ap_rputs("</tbody></table>\n", r);
return OK;
}
/**
* Disables request rate enforcements for all child processes (at start/fork) if
* init has failed.
*
* @param bs Base server_rec to iterate through all client configurations
* @param msg Error message to log (reason what has failed @init).
*/
static void qos_disable_req_rate(server_rec *bs, const char *msg) {
server_rec *s = bs->next;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(bs->module_config, &qos_module);
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, bs,
QOS_LOG_PFX(008)"could not create supervisor thread (%s),"
" disable request rate enforcement", msg);
sconf->req_rate = -1;
while(s) {
sconf = (qos_srv_config*)ap_get_module_config(s->module_config, &qos_module);
sconf->req_rate = -1;
s = s->next;
}
}
/**
* stores the IP of the connection into the array and
* increments the array pointer (for QS_Block for connection errors)
*/
static apr_uint64_t *qos_inc_block(conn_rec *connection, qos_srv_config *sconf,
qs_conn_ctx *cconf, apr_uint64_t *ip) {
conn_rec *c = connection;
if(sconf->qos_cc_block &&
apr_table_get(sconf->setenvstatus_t, QS_CLOSE) &&
!apr_table_get(c->notes, QS_BLOCK_SEEN)) {
apr_table_set(c->notes, QS_BLOCK_SEEN, "");
*ip = cconf->ip6[0];
ip++;
*ip = cconf->ip6[1];
ip++;
}
return ip;
}
#if APR_HAS_THREADS
/**
* Supervisior thread monitoring the bandith of registered connections.
*
* Connections are closed by a apr_socket_close/shutdown which must be
* detected by the thread processing the connection in order to
* de-register the connection and to terminate the pending request in
* order to free resources (thread).
*
* @param thread
* @param selfv Base server_rec
*/
static void *APR_THREAD_FUNC qos_req_rate_thread(apr_thread_t *thread, void *selfv) {
server_rec *bs = selfv;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(bs->module_config, &qos_module);
// list of ip addr. for whose we shall inc. block count
apr_uint64_t *ips = calloc(sconf->max_clients * 2, APR_ALIGN_DEFAULT(sizeof(apr_uint64_t)));
while(!sconf->inctx_t->exit) {
apr_uint64_t *ip = ips;
int current_con = 0;
int req_rate = qos_req_rate_calc(sconf, ¤t_con);
apr_time_t now = apr_time_sec(apr_time_now());
apr_time_t interval = now - sconf->qs_req_rate_tm;
int i;
apr_table_entry_t *entry;
// every second
apr_sleep(APR_USEC_PER_SEC);
if(sconf->inctx_t->exit) {
break;
}
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT21 */
entry = (apr_table_entry_t *)apr_table_elts(sconf->inctx_t->table)->elts;
for(i = 0; i < apr_table_elts(sconf->inctx_t->table)->nelts; i++) {
qos_ifctx_t *inctx = (qos_ifctx_t *)entry[i].val;
if(inctx->status == QS_CONN_STATE_KEEP) {
/* enforce keep alive */
apr_interval_time_t current_timeout = 0;
apr_socket_timeout_get(inctx->clientSocket, ¤t_timeout);
/* add 5sec tolerance to receive the request line
or let Apache close the connection */
/* ignored by event MPM ! */
if(!m_event_mpm &&
(now > (apr_time_sec(current_timeout) + 5 + inctx->time))) {
qs_conn_ctx *cconf = qos_get_cconf(inctx->c);
int level = APLOG_ERR;
if(cconf) {
/* disabled by vip priv */
if(cconf->is_vip && sconf->req_ignore_vip_rate != 1) {
level = APLOG_DEBUG;
cconf->has_lowrate = 1; /* mark connection low rate */
}
/* disabled for this request/connection */
if(inctx->disabled) {
level = APLOG_DEBUG;
cconf->has_lowrate = 1; /* mark connection low rate */
}
/* enable only if min. num of connection reached */
if(current_con < sconf->req_rate_start) {
level = APLOG_DEBUG;
cconf->has_lowrate = 1; /* mark connection low rate */
}
ip = qos_inc_block(inctx->c, sconf, cconf, ip);
ap_log_error(APLOG_MARK, APLOG_NOERRNO|level, 0, inctx->c->base_server,
QOS_LOG_PFX(034)"%s,"
" QS_SrvMinDataRate rule (enforce keep-alive),"
" c=%s",
(level == APLOG_DEBUG) || sconf->log_only ?
"log only (allowed)"
: "access denied",
QS_CONN_REMOTEIP(inctx->c) == NULL ? "-" : QS_CONN_REMOTEIP(inctx->c));
QS_INC_EVENT_LOCKED(sconf, 34);
inctx->time = now;
inctx->nbytes = 0;
if((level == APLOG_ERR) &&
!sconf->log_only) {
apr_socket_shutdown(inctx->clientSocket, APR_SHUTDOWN_READ);
}
/* mark slow clients (QS_ClientPrefer) even they are VIP */
inctx->shutdown = 1;
}
//else {
// // HTTP/2
//}
}
} else {
if(interval > inctx->time) {
int rate = inctx->nbytes / sconf->qs_req_rate_tm;
if(rate < req_rate) {
if(inctx->clientSocket) {
qs_conn_ctx *cconf = qos_get_cconf(inctx->c);
int level = APLOG_ERR;
int notUsed = 0;
if(cconf) {
/* disabled by vip priv */
if(cconf->is_vip && sconf->req_ignore_vip_rate != 1) {
level = APLOG_DEBUG;
cconf->has_lowrate = 1; /* mark connection low rate */
}
/* disabled for this request/connection */
if(inctx->disabled) {
level = APLOG_DEBUG;
cconf->has_lowrate = 1; /* mark connection low rate */
}
/* enable only if min. num of connection reached */
if(current_con < sconf->req_rate_start) {
level = APLOG_DEBUG;
cconf->has_lowrate = 1; /* mark connection low rate */
}
ip = qos_inc_block(inctx->c, sconf, cconf, ip);
if((inctx->hasBytes == 0) &&
(inctx->c->keepalives == 0) &&
(inctx->status == QS_CONN_STATE_HEAD)) {
// not even received the request line
notUsed = 1;
}
ap_log_error(APLOG_MARK, APLOG_NOERRNO|level, 0, inctx->c->base_server,
QOS_LOG_PFX(034)"%s, QS_SrvMinDataRate rule (%s%s): min=%d,"
" this connection=%d,"
" c=%s",
(level == APLOG_DEBUG) || sconf->log_only ?
"log only (allowed)"
: "access denied",
inctx->status == QS_CONN_STATE_RESPONSE ? "out" : "in",
notUsed ? ":0" : "",
req_rate,
rate,
QS_CONN_REMOTEIP(inctx->c) == NULL ? "-" : QS_CONN_REMOTEIP(inctx->c));
QS_INC_EVENT_LOCKED(sconf, 34);
inctx->time = interval + sconf->qs_req_rate_tm;
inctx->nbytes = 0;
if((level == APLOG_ERR) && !sconf->log_only) {
if(inctx->status == QS_CONN_STATE_RESPONSE) {
apr_socket_shutdown(inctx->clientSocket, APR_SHUTDOWN_WRITE);
/* close out socket (the hard way) */
apr_socket_close(inctx->clientSocket);
} else {
apr_socket_shutdown(inctx->clientSocket, APR_SHUTDOWN_READ);
}
}
/* mark slow clients (QS_ClientPrefer) even they are VIP */
inctx->shutdown = 1;
}
}
//else {
// // HTTP/2
//}
} else {
inctx->time = interval + sconf->qs_req_rate_tm;
inctx->nbytes = 0;
}
}
}
}
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT21 */
/* QS_Block for connection errors */
while(ip != ips) {
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
qos_s_entry_t **clientEntry = NULL;
qos_s_entry_t searchE;
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT38 */
ip--;
searchE.ip6[1] = *ip;
ip--;
searchE.ip6[0] = *ip;
clientEntry = qos_cc_getOrSet(u->qos_cc, &searchE, 0);
/* increment block event */
if((*clientEntry)->block < USHRT_MAX) {
(*clientEntry)->block++;
}
if((*clientEntry)->block == 1) {
/* ... and start timer */
(*clientEntry)->blockTime = apr_time_sec(apr_time_now());
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT38 */
}
}
// apr_thread_mutex_lock(sconf->inctx_t->lock);
// apr_thread_mutex_unlock(sconf->inctx_t->lock);
// called via apr_pool_cleanup_register():
// apr_thread_mutex_destroy(sconf->inctx_t->lock);
free(ips);
if(m_threaded_mpm) {
apr_thread_exit(thread, APR_SUCCESS);
}
return NULL;
}
/**
* Terminates the connection supervisor thread.
* (works for mpm_worker only)
*
* @param selfv The base server_rec
* @return APR_SUCCESS
*/
static apr_status_t qos_cleanup_req_rate_thread(void *selfv) {
server_rec *bs = selfv;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(bs->module_config, &qos_module);
sconf->inctx_t->exit = 1;
/* may long up to one second */
if(m_threaded_mpm) {
apr_status_t status;
apr_thread_join(&status, sconf->inctx_t->thread);
}
return APR_SUCCESS;
}
#endif
/**
* Writes the query/body to the env variables which may be used
* for the qsfilter* audit log.
*
* @param r
* @param dconf
*/
static void qos_audit(request_rec *r, qos_dir_config *dconf) {
const char *q = NULL;
const char *u = apr_table_get(r->notes, QS_PARP_PATH);
if(dconf->bodyfilter_p == 1 || dconf->bodyfilter_d == 1) {
q = apr_table_get(r->notes, QS_PARP_QUERY);
}
if(u == NULL) {
if(r->parsed_uri.path) {
u = apr_pstrdup(r->pool, r->parsed_uri.path);
} else {
u = apr_pstrdup(r->pool, "");
}
apr_table_setn(r->notes, apr_pstrdup(r->pool, QS_PARP_PATH), u);
}
if(q == NULL) {
if(r->parsed_uri.query) {
q = apr_pstrcat(r->pool, "?", r->parsed_uri.query, NULL);
} else {
q = apr_pstrdup(r->pool, "");
}
apr_table_setn(r->notes, apr_pstrdup(r->pool, QS_PARP_QUERY), q);
}
apr_table_setn(r->notes, apr_pstrdup(r->pool, QS_PARP_LOC), dconf->path);
if(r->next) {
apr_table_setn(r->next->notes, apr_pstrdup(r->pool, QS_PARP_PATH), u);
apr_table_setn(r->next->notes, apr_pstrdup(r->pool, QS_PARP_QUERY), q);
apr_table_setn(r->next->notes, apr_pstrdup(r->pool, QS_PARP_LOC), dconf->path);
}
}
/**
* Adds the configured (QS_Delay env var) delay to the request
*
* @param r
* @param sconf Do set log-only mode
*/
static void qos_delay(request_rec *r, qos_srv_config *sconf) {
const char *d = apr_table_get(r->subprocess_env, "QS_Delay");
if(d) {
apr_off_t s;
#ifdef ap_http_scheme
/* Apache 2.2 */
char *errp = NULL;
if((APR_SUCCESS == apr_strtoff(&s, d, &errp, 10)) && s > 0)
#else
if((s = apr_atoi64(d)) > 0)
#endif
{
qs_set_evmsg(r, "L;");
if(!sconf->log_only) {
apr_sleep(s*1000);
}
}
}
}
/**
* Enables mod_deflate
* QS_DeflateReqBody (if parp has been enabled)
*
* @param r
*/
static void qos_deflate(request_rec *r) {
if(apr_table_get(r->subprocess_env, "QS_DeflateReqBody") &&
apr_table_get(r->subprocess_env, "parp")) {
ap_add_input_filter("DEFLATE", NULL, r, r->connection);
}
}
/**
* Adjusts the content-length header
*
* @param r
*/
static void qos_deflate_contentlength(request_rec *r) {
if(apr_table_get(r->subprocess_env, "QS_DeflateReqBody") &&
apr_table_get(r->subprocess_env, "parp")) {
const char *PARPContentLength = apr_table_get(r->subprocess_env, "PARPContentLength");
const char *contentLength = apr_table_get(r->headers_in, "Content-Length");
if(PARPContentLength && contentLength) {
apr_table_set(r->headers_in, "Content-Length", PARPContentLength);
}
}
}
/**
* Verifies Apache and MPM version and writes error message (notice)
* for incompatibel version/type.
*
* Apache MPM worker binaries is the only configuration which
* has been fully tested (as mentioned in the documentation, see index.html).
*
* - old (2.0.x, 2.2.x) MPM Apache prefork versions do not unload the
* DSO properly or child exit may cause a segfault (pool cleanup)
* - regular expression are only fully supported with Apach 2.4
* - Apache 2.4 should work (but is not fully tested)
* (see CHANGES.txt for more information)
* - Apache 2.0 does not support all directives (e.g. QS_ClientPrefer) and
* we do no longer test against this version (the module does probably
* not even compile with version 2.0)
*
*/
static void qos_version_check(server_rec *bs) {
ap_version_t version;
if(strcasecmp(ap_show_mpm(), "event") == 0) {
ap_directive_t *pdir;
m_event_mpm = 1; // disable features like keep-alive control
for(pdir = ap_conftree; pdir != NULL; pdir = pdir->next) {
if(strcasecmp(pdir->directive, "AsyncRequestWorkerFactor") == 0) {
double val = 0;
char *endptr;
const char *arg = pdir->args;
if(arg == NULL) {
continue;
}
val = strtod(arg, &endptr);
if(*endptr) {
continue;
}
if(val < 0) {
continue;
}
m_event_mpm_worker_factor = val;
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, bs,
QOS_LOGD_PFX"found AsyncRequestWorkerFactor directive '%f'",
m_event_mpm_worker_factor);
}
}
} else if(strcasecmp(ap_show_mpm(), "worker") != 0) {
// mod_qos is fully tested for MPM worker (and "works" with event)
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, bs,
QOS_LOG_PFX(009)"loaded MPM is '%s'"
" but mod_qos should be used with MPM 'Worker' or 'Event' only.",
ap_show_mpm());
}
if(strcasecmp(ap_show_mpm(), "prefork") == 0) {
m_threaded_mpm = 0; // disable child cleanup (if neither worker nor event MPM)
}
ap_get_server_revision(&version);
if(version.major == 2 && version.minor == 4 && version.patch == 49) {
// 2.4.49 compat: prevents Apache segfault on connection close
m_forced_close = 0;
}
if(version.major != 2 || version.minor != 4) {
// 2.4 should work fine / older or newer versions are not tested
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, bs,
QOS_LOG_PFX(009)"server version is %d.%d"
" but mod_qos should be used with Apache 2.4 only.",
version.major, version.minor);
}
}
/**
* enforces the QS_RedirectIf variable
* @param r
* @param sconf
* @param rules Rules array
* @return HTTP_MOVED_TEMPORARILY/HTTP_TEMPORARY_REDIRECT or DECLINED
*/
static int qos_redirectif(request_rec *r, qos_srv_config *sconf,
apr_array_header_t *rules) {
ap_regmatch_t regm[AP_MAX_REG_MATCH];
int i;
qos_redirectif_entry_t *entries = (qos_redirectif_entry_t *)rules->elts;
for(i = 0; i < rules->nelts; ++i) {
qos_redirectif_entry_t *entry = &entries[i];
const char *val = apr_table_get(r->subprocess_env, entry->name);
if(val) {
if(ap_regexec(entry->preg, val, AP_MAX_REG_MATCH, regm, 0) == 0) {
int severity = sconf->log_only ? APLOG_WARNING : APLOG_ERR;
char *replaced = ap_pregsub(r->pool, entry->url, val, AP_MAX_REG_MATCH, regm);
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|severity, 0, r,
QOS_LOG_PFX(049)"redirect to %s,"
" var=%s,"
" action=%s, c=%s, id=%s",
replaced,
entry->name,
sconf->log_only ? "log only" : "redirect",
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" :
QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "049"));
QS_INC_EVENT(sconf, 49);
if(!sconf->log_only) {
apr_table_set(r->headers_out, "Location", replaced);
return entry->code;
}
}
}
}
return DECLINED;
}
static void qos_init_unique_id(apr_pool_t *p, server_rec *bs) {
char str[APRMAXHOSTLEN + 1];
apr_sockaddr_t *sockaddr;
str[APRMAXHOSTLEN] = '\0';
unsigned int in_addr = 0;
if(apr_gethostname(str, sizeof(str) - 1, p) == APR_SUCCESS) {
if(apr_sockaddr_info_get(&sockaddr, str, AF_INET, 0, 0, p) == APR_SUCCESS) {
in_addr = sockaddr->sa.sin.sin_addr.s_addr;
}
}
pid_t pid = getpid();
m_unique_id.in_addr = pid^in_addr;
m_unique_id.unique_id_counter = time(NULL);
}
/**
* propagates environment variables to sub-requests (for logging)
*/
static void qos_propagate_events(request_rec *r) {
request_rec *mr = NULL;
const char **var;
if(r->prev) {
mr = r->prev;
} else if(r->main) {
mr = r->main;
} else if(r->next) {
mr = r->next;
}
var = m_env_variables;
while(*var) {
int propagated = 0;
if(mr) {
const char *p = apr_table_get(mr->subprocess_env, *var);
if(p) {
propagated = 1;
apr_table_set(r->subprocess_env, *var, p);
}
if(!propagated) {
p = apr_table_get(r->subprocess_env, *var);
if(p) {
propagated = 1;
apr_table_set(mr->subprocess_env, *var, p);
}
}
}
var++;
}
if(r->prev) {
// internal redirect (e.g. error page)
int i;
int len = strlen(QS_LIMIT_NAME_PFX);
request_rec *mp = r->prev;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(mp->subprocess_env)->elts;
for(i = 0; i < apr_table_elts(mp->subprocess_env)->nelts; ++i) {
if(strncmp(entry[i].key, QS_LIMIT_NAME_PFX, len) == 0) {
const char *eventName = entry[i].val;
const char *name = apr_pstrcat(r->pool, eventName, QS_COUNTER_SUFFIX, NULL);
const char *value = apr_table_get(mp->subprocess_env, name);
if(value) {
apr_table_set(r->subprocess_env, name, value);
}
name = eventName;
value = apr_table_get(mp->subprocess_env, name);
if(value) {
apr_table_set(r->subprocess_env, name, value);
}
name = apr_pstrcat(r->pool, eventName, QS_LIMIT_REMAINING, NULL);
value = apr_table_get(mp->subprocess_env, name);
if(value) {
apr_table_set(r->subprocess_env, name, value);
}
name = apr_pstrcat(r->pool, eventName, QS_LIMIT_SEEN, NULL);
value = apr_table_get(mp->subprocess_env, name);
if(value) {
apr_table_set(r->subprocess_env, name, value);
}
}
}
}
}
/**
* ensure that every request record has the error notes to log
*/
static void qos_propagate_notes(request_rec *r) {
request_rec *mr = NULL;
const char **var;
if(r->prev) {
mr = r->prev;
} else if(r->main) {
mr = r->main;
} else if(r->next) {
mr = r->next;
}
var = m_note_variables;
while(*var) {
int propagated = 0;
if(mr) {
const char *p = apr_table_get(mr->notes, *var);
if(p) {
propagated = 1;
apr_table_setn(r->notes, *var, p);
}
if(!propagated) {
p = apr_table_get(r->notes, *var);
if(p) {
propagated = 1;
apr_table_setn(mr->notes, *var, p);
}
}
}
var++;
}
}
/* QS_UnsetReqHeader */
static void qos_unset_reqheader(request_rec *r, qos_srv_config *sconf) {
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(sconf->unsetreqheader_t)->elts;
for(i = 0; i < apr_table_elts(sconf->unsetreqheader_t)->nelts; i++) {
apr_table_unset(r->headers_in, entry[i].key);
}
return;
}
/* QS_UnsetResHeader */
static void qos_unset_resheader(request_rec *r, qos_srv_config *sconf) {
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(sconf->unsetresheader_t)->elts;
for(i = 0; i < apr_table_elts(sconf->unsetresheader_t)->nelts; i++) {
apr_table_unset(r->headers_out, entry[i].key);
apr_table_unset(r->err_headers_out, entry[i].key);
}
return;
}
/************************************************************************
* handlers
***********************************************************************/
/**
* Destructor to handle connections which do not have been established
* successfully resp. have been closed unexpectedly.
*
* Increments block counter.
*
* @param p Connection base context
* @return APR_SUCCESS
*/
static apr_status_t qos_base_cleanup_conn(void *p) {
qs_conn_base_ctx *base = p;
if(base->sconf->has_qos_cc || base->sconf->qos_cc_prefer) {
int connRuleViolation = 0;
const char *type = QS_EMPTY_CON;
if(base->requests == 0 &&
apr_table_get(base->sconf->setenvstatus_t, QS_EMPTY_CON) &&
!apr_table_get(base->c->notes, QS_BLOCK_SEEN)) {
connRuleViolation = 1;
apr_table_set(base->c->notes, QS_BLOCK_SEEN, "");
}
if(apr_table_get(base->c->notes, QS_BROKEN_CON)) {
connRuleViolation = 1;
type = QS_BROKEN_CON;
}
if(apr_table_get(base->c->notes, QS_MAXIP)) {
connRuleViolation = 1;
type = QS_MAXIP;
}
if(connRuleViolation) {
qos_user_t *u = qos_get_user_conf(base->sconf->act->ppool);
qos_s_entry_t **clientEntry = NULL;
qos_s_entry_t searchE;
qos_ip_str2long(QS_CONN_REMOTEIP(base->c), searchE.ip6); // no ip simulation here
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT40 */
clientEntry = qos_cc_getOrSet(u->qos_cc, &searchE, 0);
/* increment block event */
if((*clientEntry)->block < USHRT_MAX) {
(*clientEntry)->block++;
}
if((*clientEntry)->block == 1) {
/* ... and start timer */
(*clientEntry)->blockTime = apr_time_sec(apr_time_now());
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT40 */
if(QS_ISDEBUG(base->c->base_server)) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, base->c->base_server,
QOS_LOGD_PFX"QS_ClientEventBlockCount rule: "
"%s event detected "
"c=%s",
type,
QS_CONN_REMOTEIP(base->c) == NULL ? "-" : QS_CONN_REMOTEIP(base->c));
}
}
}
return APR_SUCCESS;
}
/**
* Connection destructor.
*
* Updates per IP events and connection counter.
*
* @param p Connection context
* @return APR_SUCCESS
*/
static apr_status_t qos_cleanup_conn(void *p) {
qs_conn_ctx *cconf = p;
if(cconf->sconf->has_qos_cc || cconf->sconf->qos_cc_prefer) {
qos_user_t *u = qos_get_user_conf(cconf->sconf->act->ppool);
qos_s_entry_t **clientEntry = NULL;
qos_s_entry_t searchE;
searchE.ip6[0] = cconf->ip6[0];
searchE.ip6[1] = cconf->ip6[1];
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT15 */
// generation: check if it has been cleard for this process generation
if((m_generation != u->qos_cc->generation_locked) && u->qos_cc->connections > 0) {
u->qos_cc->connections--;
}
clientEntry = qos_cc_getOrSet(u->qos_cc, &searchE, 0);
if((*clientEntry)->events < UINT_MAX) {
(*clientEntry)->events++; // update event activity even there is no valid request (logger)
}
if(cconf->set_vip_by_header) {
(*clientEntry)->vip = 1;
}
if(cconf->has_lowrate) {
(*clientEntry)->lowrate = time(NULL);
(*clientEntry)->lowratestatus |= QOS_LOW_FLAG_PKGRATE;
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT15 */
}
/* QS_SrvMaxConn or Geo */
if(qos_count_connections(cconf->sconf)) {
apr_global_mutex_lock(cconf->sconf->act->lock); /* @CRT3 */
if(cconf->sconf->act->conn && cconf->sconf->act->conn->connections > 0) {
cconf->sconf->act->conn->connections--;
}
apr_global_mutex_unlock(cconf->sconf->act->lock); /* @CRT3 */
}
if(cconf->sconf->max_conn_per_ip != -1) {
qos_dec_ip(cconf);
}
return APR_SUCCESS;
}
/**
* handler to close the connection in the case the abort
* by the pre-connect hook was ignored (Apache 2.4.28 Event MPM)
*/
static int qos_process_connection(conn_rec *connection) {
#if (AP_SERVER_MINORVERSION_NUMBER == 4)
#if (AP_SERVER_PATCHLEVEL_NUMBER > 17)
if(connection->master) {
// skip slave connections / process "real" connections only
return DECLINED;
}
#endif
#endif
if(connection->aborted == 1 && apr_table_get(connection->notes, QS_CONN_ABORT)) {
if(connection->cs) {
connection->cs->state = CONN_STATE_LINGER;
}
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, connection->base_server,
QOS_LOG_PFX(167)"closing connection at process connection hook, c=%s",
QS_CONN_REMOTEIP(connection) == NULL ? "-" :
QS_CONN_REMOTEIP(connection));
return HTTP_INTERNAL_SERVER_ERROR;
}
return DECLINED;
}
/**
* Connection constructor. Rules that are applied to established connections.
*
* @param c
* @return
*/
static int qos_pre_process_connection(conn_rec *connection, void *skt) {
conn_rec *c = connection;
qs_conn_ctx *cconf;
int vip = 0;
apr_socket_t *socket = skt;
if(c->sbh == NULL) {
// proxy connections do NOT have any relation to the score board, don't handle them
return DECLINED;
}
#if (AP_SERVER_MINORVERSION_NUMBER == 4)
#if (AP_SERVER_PATCHLEVEL_NUMBER > 17)
if(connection->master) {
// skip slave connections / process "real" connections only
return DECLINED;
}
#endif
#endif
cconf = qos_get_cconf(c);
if(cconf == NULL) {
int client_control = DECLINED;
int connections = 0;
int all_connections = 0;
int current = 0;
qs_ip_entry_t *conn_ip = NULL;
char *msg = NULL;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(c->base_server->module_config,
&qos_module);
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
cconf = qos_create_cconf(c, sconf);
/* control timeout */
if(sconf->req_rate != -1) {
qos_timeout_pc(c, sconf);
}
/* packet rate */
if(sconf->qos_cc_prefer_limit) {
qos_pktrate_pc(c, sconf);
}
/* evaluates client ip */
qos_ip_str2long(QS_CONN_REMOTEIP(c), cconf->ip6);
#ifdef QS_INTERNAL_TEST
/* use one of the predefined ip addresses */
if(cconf->sconf->enable_testip) {
char *testid = apr_psprintf(c->pool, "%d", rand()%(m_qs_sim_ip_len-1));
const char *testip = apr_table_get(cconf->sconf->testip, testid);
qos_ip_str2long(testip, cconf->ip6);
}
#endif
/* ------------------------------------------------------------
* update data
*/
/* client control */
client_control = qos_cc_pc_filter(c, cconf, u, &msg);
/* QS_SrvMaxConn: vhost connections or Geo */
if(qos_count_connections(sconf)) {
apr_global_mutex_lock(cconf->sconf->act->lock); /* @CRT4 */
if(cconf->sconf->act->conn) {
cconf->sconf->act->conn->connections++;
all_connections = qos_server_connections(sconf);
connections = cconf->sconf->act->conn->connections;
apr_table_set(c->notes, "QS_SrvConn", apr_psprintf(c->pool, "%d", connections));
apr_table_set(c->notes, "QS_AllConn", apr_psprintf(c->pool, "%d", all_connections));
}
apr_global_mutex_unlock(cconf->sconf->act->lock); /* @CRT4 */
}
/* single source ip */
if(sconf->max_conn_per_ip != -1) {
current = qos_inc_ip(sconf, cconf, &conn_ip);
apr_table_set(c->notes, "QS_IPConn", apr_psprintf(c->pool, "%d", current));
}
/* Check for vip (by ip) */
vip = qos_is_excluded_ip(cconf->mc, sconf->exclude_ip);
if(vip == 0) {
// check if qos_cc_pc_filter() got vip status form the cc store
vip = cconf->is_vip;
}
if(vip) {
/* propagate vip to connection */
cconf->is_vip = vip;
if(!cconf->evmsg || !strstr(cconf->evmsg, "S;")) {
cconf->evmsg = apr_pstrcat(c->pool, "S;", cconf->evmsg, NULL);
}
}
/* ------------------------------------------------------------
* enforce rules
*/
/* client control */
if((client_control != DECLINED) && !vip) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, c->base_server,
"%s",
msg == NULL ? "-" : msg);
if(!sconf->log_only) {
return qos_return_error_andclose(c, socket);
}
}
/* Geo */
if(sconf->geodb) {
unsigned long ip = qos_geo_str2long(c->pool, QS_CONN_REMOTEIP(c));
qos_geo_entry_t *pB = bsearch(&ip,
sconf->geodb->data,
sconf->geodb->size,
sizeof(qos_geo_entry_t),
qos_geo_comp);
if(pB) {
apr_table_set(c->notes, QS_COUNTRY, pB->country);
}
if(sconf->geo_limit != -1) {
if(all_connections >= sconf->geo_limit) {
if(sconf->geo_excludeUnknown == 1 && (pB == NULL || pB->country[0] == '\0')) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, c->base_server,
QOS_LOGD_PFX"skip QS_ClientGeoCountryPriv enforcement"
" for client address %s which could not be mapped to country code",
QS_CONN_REMOTEIP(c) ? QS_CONN_REMOTEIP(c) : "UNKNOWN");
} else if(pB == NULL || apr_table_get(sconf->geo_priv, pB->country) == NULL) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, c->base_server,
QOS_LOG_PFX(101)"access denied%s,"
" QS_ClientGeoCountryPriv rule: max=%d,"
" concurrent connections=%d,"
" c=%s"
" country=%s",
sconf->log_only ? " (log only)" : "",
sconf->geo_limit,
all_connections,
QS_CONN_REMOTEIP(c) == NULL ? "-" : QS_CONN_REMOTEIP(c),
pB != NULL ? pB->country : "--");
QS_INC_EVENT(sconf, 101);
if(!sconf->log_only) {
return qos_return_error_andclose(c, socket);
}
}
}
}
}
/* QS_SrvMaxConn: vhost connections */
if((sconf->max_conn != -1) && !vip) {
if(connections > sconf->max_conn) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, c->base_server,
QOS_LOG_PFX(030)"access denied%s, QS_SrvMaxConn rule: max=%d,"
" concurrent connections=%d,"
" c=%s",
sconf->log_only ? " (log only)" : "",
sconf->max_conn, connections,
QS_CONN_REMOTEIP(c) == NULL ? "-" : QS_CONN_REMOTEIP(c));
QS_INC_EVENT(sconf, 30);
if(!sconf->log_only) {
return qos_return_error_andclose(c, socket);
}
}
}
/* single source ip */
if((sconf->max_conn_per_ip != -1) && (!vip || sconf->max_conn_per_ip_ignore_vip == 1)) {
if((current > sconf->max_conn_per_ip) &&
(all_connections >= sconf->max_conn_per_ip_connections)) {
conn_ip->error++;
if(apr_table_get(sconf->setenvstatus_t, QS_MAXIP)) {
apr_table_set(c->notes, QS_MAXIP, "1");
}
/* only print the first 20 messages for this client */
QS_INC_EVENT(sconf, 31);
if(conn_ip->error <= QS_LOG_REPEAT) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, c->base_server,
QOS_LOG_PFX(031)"access denied%s,"
" QS_SrvMaxConnPerIP rule: max=%d,"
" concurrent connections=%d,"
" c=%s",
sconf->log_only ? " (log only)" : "",
sconf->max_conn_per_ip, current,
QS_CONN_REMOTEIP(c) == NULL ? "-" : QS_CONN_REMOTEIP(c));
} else {
if((conn_ip->error % QS_LOG_REPEAT) == 0) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, c->base_server,
QOS_LOG_PFX(031)"access denied%s,"
" QS_SrvMaxConnPerIP rule: max=%d,"
" concurrent connections=%d,"
" message repeated %d times,"
" c=%s",
sconf->log_only ? " (log only)" : "",
sconf->max_conn_per_ip, current,
QS_LOG_REPEAT,
QS_CONN_REMOTEIP(c) == NULL ? "-" : QS_CONN_REMOTEIP(c));
}
}
if(!sconf->log_only) {
return qos_return_error_andclose(c, socket);
}
} else {
if(conn_ip) {
if(conn_ip->error > QS_LOG_REPEAT) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, c->base_server,
QOS_LOG_PFX(031)"access denied (previously),"
" QS_SrvMaxConnPerIP rule: max=%d,"
" concurrent connections=%d,"
" message repeated %d times,"
" c=%s",
sconf->max_conn_per_ip, current,
conn_ip->error % QS_LOG_REPEAT,
QS_CONN_REMOTEIP(c) == NULL ? "-" : QS_CONN_REMOTEIP(c));
}
conn_ip->error = 0;
}
}
}
}
return DECLINED;
}
/**
* Pre connection
* - constructs the connection ctx (stores socket ref)
* - enforces block counter (as early as possible)
*/
static int qos_pre_connection(conn_rec *connection, void *skt) {
conn_rec *c = connection;
int ret = DECLINED;
qos_srv_config *sconf;
qs_conn_base_ctx *base;
int excludeFromBlock;
if(c->sbh == NULL) {
// proxy connections do NOT have any relation to the score board, don't handle them
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, c->base_server,
QOS_LOGD_PFX"skip processing of outgoing/virtual connection %s<->%s",
QS_CONN_REMOTEIP(c) ? QS_CONN_REMOTEIP(c) : "UNKNOWN",
c->local_ip ? c->local_ip : "UNKNOWN");
return ret;
}
#if (AP_SERVER_MINORVERSION_NUMBER == 4)
#if (AP_SERVER_PATCHLEVEL_NUMBER > 17)
if(connection->master) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, c->base_server,
QOS_LOGD_PFX"skip slave connection %s",
QS_CONN_REMOTEIP(c) ? QS_CONN_REMOTEIP(c) : "UNKNOWN");
return ret;
}
#endif
#endif
sconf = (qos_srv_config*)ap_get_module_config(c->base_server->module_config, &qos_module);
excludeFromBlock = qos_is_excluded_ip(c, sconf->cc_exclude_ip);
base = qos_get_conn_base_ctx(c);
if(base == NULL) {
base = qos_create_conn_base_ctx(c, sconf);
base->clientSocket = skt;
}
if(sconf && (sconf->req_rate != -1)) {
qos_ifctx_t *inctx = qos_create_ifctx(c, sconf);
inctx->clientSocket = skt;
ap_add_input_filter("qos-in-filter", inctx, NULL, c);
}
/* blocked by event (block only, no limit) - very aggressive */
if(sconf->qos_cc_block && !excludeFromBlock) {
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
qos_s_entry_t **clientEntry = NULL;
qos_s_entry_t searchE;
qos_ip_str2long(QS_CONN_REMOTEIP(c), searchE.ip6); // no ip simulation here
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT39 */
clientEntry = qos_cc_getOrSet(u->qos_cc, &searchE, 0);
if((*clientEntry)->block >= sconf->qos_cc_block) {
apr_time_t now = time(NULL);
if(((*clientEntry)->blockTime + sconf->qos_cc_blockTime) > now) {
(*clientEntry)->blockMsg++;;
// stop logging every event if we have logged it many times
QS_INC_EVENT_LOCKED(sconf, 60);
if((*clientEntry)->blockMsg > QS_LOG_REPEAT) {
if(((*clientEntry)->blockMsg % QS_LOG_REPEAT) == 0) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, c->base_server,
QOS_LOG_PFX(060)"access denied, "
"QS_ClientEventBlockCount rule: "
"max=%d, current=%hu, "
"message repeated %d times, "
"c=%s",
sconf->qos_cc_block,
(*clientEntry)->block,
QS_LOG_REPEAT,
QS_CONN_REMOTEIP(c) == NULL ? "-" : QS_CONN_REMOTEIP(c));
}
} else {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, c->base_server,
QOS_LOG_PFX(060)"access denied, QS_ClientEventBlockCount rule: "
"max=%d, current=%hu, age=%"APR_TIME_T_FMT", c=%s",
sconf->qos_cc_block,
(*clientEntry)->block,
now - (*clientEntry)->blockTime,
QS_CONN_REMOTEIP(c) == NULL ? "-" : QS_CONN_REMOTEIP(c));
}
if(!sconf->log_only) {
apr_table_set(c->notes, QS_BLOCK_SEEN, ""); // suppress NullConnection messages
c->keepalive = AP_CONN_CLOSE;
c->aborted = 1;
if(c->cs) {
c->cs->state = CONN_STATE_LINGER;
}
apr_table_set(c->notes, QS_CONN_ABORT, QS_CONN_ABORT);
if (m_forced_close == 0) {
ret = DECLINED;
} else {
ret = m_retcode;
}
}
} else {
/* release */
if((*clientEntry)->blockMsg > QS_LOG_REPEAT) {
// write remaining log lines
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, c->base_server,
QOS_LOG_PFX(060)"access denied (previously), QS_ClientEventBlockCount rule: "
"max=%d, current=%hu, "
"message repeated %d times, "
"c=%s",
sconf->qos_cc_block,
(*clientEntry)->block,
(*clientEntry)->blockMsg % QS_LOG_REPEAT,
QS_CONN_REMOTEIP(c) == NULL ? "-" : QS_CONN_REMOTEIP(c));
(*clientEntry)->blockMsg = 0;
}
(*clientEntry)->block = 0;
(*clientEntry)->blockTime = 0;
}
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT39 */
}
return ret;
}
/**
* Process user tracking cookie (QS_UserTrackingCookieName)
*
* @param r
* @return DECLINED or 302
*/
static int qos_post_read_request_later(request_rec *r) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config,
&qos_module);
char *value;
const char *ignore;
if(sconf->log_env == 1) {
qos_log_env(r, ">PR_2");
}
if(!ap_is_initial_req(r) || !sconf->user_tracking_cookie) {
return DECLINED;
}
value = qos_get_remove_cookie(r, sconf->user_tracking_cookie);
qos_get_create_user_tracking(r, sconf, value);
if(!sconf->user_tracking_cookie_force) {
return DECLINED;
}
if(qos_request_check(r, sconf) != APR_SUCCESS) {
return HTTP_BAD_REQUEST;
}
if(strcmp("/favicon.ico", r->parsed_uri.path) == 0) {
return DECLINED;
}
ignore = apr_table_get(r->subprocess_env, QOS_DISABLE_UTC_ENFORCEMENT);
if(ignore) {
return DECLINED;
}
if(strcmp(sconf->user_tracking_cookie_force, r->parsed_uri.path) == 0) {
/* access to check url */
if(sconf->user_tracking_cookie_jsredirect == 1) {
apr_table_set(r->subprocess_env, "QS_UT_NAME", sconf->user_tracking_cookie);
apr_table_set(r->subprocess_env, "QS_UT_INITIAL_URI", "/");
apr_table_set(r->subprocess_env, "QS_UT_QUERY", "qs=init");
if(r->parsed_uri.query && strcmp(r->parsed_uri.query, "qs=init") == 0) {
apr_table_add(r->headers_out, "Cache-Control", "no-cache, no-store");
qos_send_user_tracking_cookie(r, sconf, HTTP_OK);
return DECLINED;
}
if(r->parsed_uri.query && (strncmp(r->parsed_uri.query, "r=", 2) == 0)) {
char *redirect_page;
int buf_len = 0;
unsigned char *buf;
char *q = r->parsed_uri.query;
buf_len = qos_decrypt(r, sconf, &buf, &q[2]);
if(buf_len > 0) {
redirect_page = apr_psprintf(r->pool, "%.*s",
buf_len, buf);
apr_table_set(r->subprocess_env, "QS_UT_INITIAL_URI", redirect_page);
}
}
}
if(apr_table_get(r->subprocess_env, QOS_USER_TRACKING_NEW) == NULL) {
if(r->parsed_uri.query && (strncmp(r->parsed_uri.query, "r=", 2) == 0)) {
/* client has send a cookie, redirect to original url */
int buf_len = 0;
unsigned char *buf;
char *q = r->parsed_uri.query;
buf_len = qos_decrypt(r, sconf, &buf, &q[2]);
if(buf_len > 0) {
char *redirect_page = apr_psprintf(r->pool, "%s%.*s",
qos_this_host(r),
buf_len, buf);
apr_table_set(r->headers_out, "Location", redirect_page);
return HTTP_MOVED_TEMPORARILY;
}
}
} /* else, "grant access" to the error page */
/* but prevent page caching (the browser shall always access the
server when redireced to this url */
apr_table_add(r->headers_out, "Cache-Control", "no-cache, no-store");
} else if(apr_table_get(r->subprocess_env, QOS_USER_TRACKING_NEW) != NULL) {
if((r->method_number == M_GET || sconf->user_tracking_cookie_jsredirect == 1) &&
(apr_table_get(r->subprocess_env, QOS_USER_TRACKING_RENEW) == NULL)) {
/* no valid cookie in request, redirect to check page */
char *redirect_page = apr_pstrcat(r->pool, qos_this_host(r),
sconf->user_tracking_cookie_force,
"?r=",
qos_encrypt(r, sconf,
(unsigned char *)r->unparsed_uri,
strlen(r->unparsed_uri)),
NULL);
apr_table_set(r->headers_out, "Location", redirect_page);
if(sconf->user_tracking_cookie_jsredirect < 1) {
qos_send_user_tracking_cookie(r, sconf, HTTP_MOVED_TEMPORARILY);
}
return HTTP_MOVED_TEMPORARILY;
}
}
return DECLINED;
}
/**
* All headers has been read. End/updates connection level filters and propagtes
* per connection events to the request_rec.
*
* @param r
* @return DECLINED
*/
static int qos_post_read_request(request_rec *r) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config,
&qos_module);
qos_ifctx_t *inctx = NULL;
/* propagate connection env vars to req, geo data and QS_SrvMaxConn */
const char *country = apr_table_get(r->connection->notes, QS_COUNTRY);
const char *connections = apr_table_get(r->connection->notes, "QS_SrvConn");
const char *all_connections = apr_table_get(r->connection->notes, "QS_AllConn");
const char *fromCurrentIp = apr_table_get(r->connection->notes, "QS_IPConn");
const char *connectionid = apr_table_get(r->connection->notes, QS_CONNID);
const char *lowPrioFlags = apr_table_get(r->connection->notes, "QS_ClientLowPrio");
const char *isVipIP = apr_table_get(r->connection->notes, QS_ISVIPREQ);
/* QS_UnsetReqHeader */
qos_unset_reqheader(r, sconf);
if(sconf->geodb) {
if(sconf->qos_cc_forwardedfor) {
// override country determined on a per connection basis
const char *forwardedfor = qos_forwardedfor_fromHeader(r, sconf->qos_cc_forwardedfor);
if(forwardedfor) {
unsigned long ip = qos_geo_str2long(r->pool, forwardedfor);
if(ip) {
qos_geo_entry_t *pB = bsearch(&ip,
sconf->geodb->data,
sconf->geodb->size,
sizeof(qos_geo_entry_t),
qos_geo_comp);
if(pB) {
country = apr_pstrdup(r->pool, pB->country);
}
} else {
if(apr_table_get(r->notes, "QOS_LOG_PFX069") == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(069)"no valid IP header found (@prr):"
" invalid header value '%s', fallback to connection's IP %s, id=%s",
forwardedfor,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "069"));
apr_table_set(r->notes, "QOS_LOG_PFX069", "log once");
QS_INC_EVENT(sconf, 69);
}
}
} else {
if(apr_table_get(r->notes, "QOS_LOG_PFX069") == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(069)"no valid IP header found (@prr):"
" header '%s' not available, fallback to connection's IP %s, id=%s",
sconf->qos_cc_forwardedfor,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "069"));
apr_table_set(r->notes, "QOS_LOG_PFX069", "log once");
QS_INC_EVENT(sconf, 69);
}
}
}
}
if(country) {
apr_table_set(r->subprocess_env, QS_COUNTRY, country);
}
if(connections) {
apr_table_set(r->subprocess_env, "QS_SrvConn", connections);
}
if(fromCurrentIp) {
apr_table_set(r->subprocess_env, "QS_IPConn", fromCurrentIp);
}
if(all_connections) {
apr_table_set(r->subprocess_env, "QS_AllConn", all_connections);
}
if(connectionid == NULL) {
connectionid = apr_psprintf(r->pool, "%"APR_TIME_T_FMT"%.2ld%.5"APR_PID_T_FMT,
r->request_time,
r->connection->id % 100,
getpid());
apr_table_set(r->connection->notes, QS_CONNID, connectionid);
}
apr_table_set(r->subprocess_env, QS_CONNID, connectionid);
if(!ap_is_initial_req(r)) {
// sub-request
qos_propagate_events(r);
} else {
qos_pr_event_limit(r, sconf);
}
/* QS_ClientPrefer: propagate connection env vars to req */
if(lowPrioFlags) {
apr_table_set(r->subprocess_env, "QS_ClientLowPrio", lowPrioFlags);
}
/* QS_IsVipRequest is set due VIP IP */
if(isVipIP) {
apr_table_set(r->subprocess_env, QS_ISVIPREQ, isVipIP);
}
if(sconf->log_env == 1) {
qos_log_env(r, ">PR_1");
}
if(qos_request_check(r, sconf) != APR_SUCCESS) {
return HTTP_BAD_REQUEST;
}
if(!ap_is_initial_req(r)) {
// we are done for this request (e.g. error page)
return DECLINED;
}
qos_parp_prr(r, sconf);
if(sconf && (sconf->req_rate != -1)) {
inctx = qos_get_ifctx(r->connection->input_filters);
if(inctx) {
const char *te = apr_table_get(r->headers_in, "Transfer-Encoding");
inctx->r = r;
if(r->read_chunked || (te && (strcasecmp(te, "chunked") == 0))) {
ap_add_input_filter("qos-in-filter2", inctx, r, r->connection);
inctx->status = QS_CONN_STATE_CHUNKED;
} else {
const char *cl = apr_table_get(r->headers_in, "Content-Length");
if(cl == NULL) {
inctx->status = QS_CONN_STATE_END;
#if APR_HAS_THREADS
if(!sconf->inctx_t->exit) {
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT26 */
apr_table_unset(sconf->inctx_t->table,
QS_INCTX_ID);
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT26 */
}
#endif
} else {
#ifdef ap_http_scheme
/* Apache 2.2 */
if(APR_SUCCESS == apr_strtoff(&inctx->cl_val, cl, NULL, 0))
#else
if((inctx->cl_val = apr_atoi64(cl)) >= 0)
#endif
{
ap_add_input_filter("qos-in-filter2", inctx, r, r->connection);
inctx->status = QS_CONN_STATE_BODY;
} else {
/* header filter should block this request */
}
}
}
}
}
return DECLINED;
}
/**
* QS_LimitRequestBody, if content-length header is available.
*
* @param r
* @param sconf Either server or dir config is used (or env var)
* @param dconf Either server or dir config is used (or env var)
* @return HTTP_REQUEST_ENTITY_TOO_LARGE if not allowed
*/
static apr_status_t qos_limitrequestbody_ctl(request_rec *r, qos_srv_config *sconf,
qos_dir_config *dconf) {
apr_off_t maxpost = qos_maxpost(r, sconf, dconf);
if(maxpost != -1) {
const char *l = apr_table_get(r->headers_in, "Content-Length");
if(l != NULL) {
apr_off_t s;
#ifdef ap_http_scheme
/* Apache 2.2 */
char *errp = NULL;
if((APR_SUCCESS != apr_strtoff(&s, l, &errp, 10)) || (s < 0))
#else
if(((s = apr_atoi64(l)) < 0) || (s < 0))
#endif
{
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(044)"access denied%s, QS_LimitRequestBody:"
" invalid content-length header, c=%s, id=%s",
sconf->log_only ? " (log only)" : "",
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "044"));
QS_INC_EVENT(sconf, 44);
return HTTP_REQUEST_ENTITY_TOO_LARGE;
}
if(s > maxpost) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(044)"access denied%s, QS_LimitRequestBody:"
" max=%"APR_OFF_T_FMT" this=%"APR_OFF_T_FMT", c=%s, id=%s",
sconf->log_only ? " (log only)" : "",
maxpost, s,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "044"));
QS_INC_EVENT(sconf, 44);
return HTTP_REQUEST_ENTITY_TOO_LARGE;
}
} else {
int read_chunked = 0;
if(r->read_chunked) {
read_chunked = 1;
} else {
// Apache 2.4
const char *tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
if(tenc && strcasecmp(tenc, "chunked") == 0) {
read_chunked = 1;
}
}
if(ap_is_initial_req(r) && read_chunked) {
ap_add_input_filter("qos-in-filter3", NULL, r, r->connection);
}
}
}
return APR_SUCCESS;
}
/**
* Header parser (executed after mod_setenvif but before mod_parp).
* Implements content-length based request body size limit and activates
* content-length adijustmen for compressed request body.
*
* @param r
* @return
*/
static int qos_header_parser1(request_rec * r) {
if(ap_is_initial_req(r)) {
apr_status_t rv;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config,
&qos_module);
qos_dir_config *dconf = (qos_dir_config*)ap_get_module_config(r->per_dir_config,
&qos_module);
if(sconf->log_env == 1) {
qos_log_env(r, ">HP_2");
}
qos_deflate(r);
/** QS_LimitRequestBody */
rv = qos_limitrequestbody_ctl(r, sconf, dconf);
if(rv != APR_SUCCESS) {
int rc;
const char *error_page = sconf->error_page;
qs_set_evmsg(r, "D;");
if(!sconf->log_only) {
rc = qos_error_response(r, error_page);
if((rc == DONE) || (rc == HTTP_MOVED_TEMPORARILY)) {
return rc;
}
return rv;
}
}
}
return DECLINED;
}
/**
* Header parser (executed before mod_setenvif or mod_parp).
* Enables mod_parp if request body processing (filter) has been enabled
* and implements the request header filter.
*
* @param r
* @return
*/
static int qos_header_parser0(request_rec * r) {
if(ap_is_initial_req(r)) {
int rc = DECLINED;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config,
&qos_module);
qos_dir_config *dconf = (qos_dir_config*)ap_get_module_config(r->per_dir_config,
&qos_module);
if(sconf->log_env == 1) {
qos_log_env(r, ">HP_1");
}
/*
* QS_DenyBody requires mod_parp
*/
if(dconf && (dconf->bodyfilter_p == 1 || dconf->bodyfilter_d == 1)) {
qos_enable_parp(r);
}
/*
* QS_RequestHeaderFilter enforcement
*/
rc = qos_hp_header_filter(r, sconf, dconf);
if(rc != DECLINED) {
return rc;
}
}
return DECLINED;
}
/**
* Header parser implements restrictions on a per location (url) basis.
*
* @param r
* @return
*/
static int qos_header_parser(request_rec * r) {
/* apply rules only to main request (avoid filtering of error documents) */
if(ap_is_initial_req(r)) {
char *msg = NULL;
char *uid = NULL;
int req_per_sec_block = 0;
apr_off_t kbytes_per_sec_limit = 0;
qs_acentry_t *event_kbytes_per_sec = NULL;
int status;
const char *tmostr = NULL;
qs_acentry_t *actEntry = NULL;
qs_acentry_t *actEntryCond = NULL;
qs_acentry_t *actEntryMain = NULL; // either e or e_cond (used for locking)
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config,
&qos_module);
qos_dir_config *dconf = (qos_dir_config*)ap_get_module_config(r->per_dir_config,
&qos_module);
qs_req_ctx *rctx = NULL;
const char *error_page = sconf->error_page;
if(sconf->log_env == 1) {
qos_log_env(r, ">HP_3");
}
qos_deflate_contentlength(r);
/* QS_SetEnvIfResBody */
if(dconf && dconf->response_pattern) {
ap_add_output_filter("qos-out-filter-body", NULL, r, r->connection);
}
/* enable broken connection detection (connection abort by client) */
if(apr_table_get(sconf->setenvstatus_t, QS_BROKEN_CON)) {
ap_add_output_filter("qos-out-filter-brokencon", NULL, r, r->connection);
}
/*
* QS_Permit* / QS_Deny* enforcement (but not QS_DenyEvent)
*/
status = qos_hp_filter(r, sconf, dconf);
/* prepare audit log */
if(m_enable_audit && dconf) {
qos_audit(r, dconf);
}
if(status != DECLINED) {
return status;
}
/*
* Dynamic keep alive
*/
if(!sconf->log_only) {
qos_keepalive(r, sconf);
}
/*
* VIP control
*/
if(sconf->header_name || sconf->vip_user) {
rctx = qos_rctx_config_get(r);
rctx->is_vip = qos_is_vip(r, sconf);
if(rctx->is_vip) {
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
if(cconf) {
cconf->is_vip = 1;
}
}
}
/*
* additional variables
*/
if(apr_table_elts(sconf->setenvifparp_t)->nelts > 0) {
qos_parp_hp(r, sconf);
}
if((apr_table_elts(sconf->setenvifparpbody_t)->nelts > 0) && qos_parp_body_data_fn) {
qos_parp_hp_body(r, sconf);
}
if(r->parsed_uri.query) {
qos_setenvifquery(r, sconf, dconf);
}
if(sconf->setenvif_t->nelts > 0) {
qos_setenvif(r, sconf->setenvif_t);
}
if(dconf->setenvif_t->nelts > 0) {
qos_setenvif(r, dconf->setenvif_t);
}
if(apr_table_elts(sconf->setenv_t)->nelts > 0) {
qos_setenv(r, sconf);
}
if(apr_table_elts(sconf->setreqheader_t)->nelts > 0) {
qos_setreqheader(r, sconf->setreqheader_t);
}
tmostr = apr_table_get(r->subprocess_env, QS_TIMEOUT);
if(tmostr) {
apr_interval_time_t timeout = apr_time_from_sec(atoi(tmostr));
if(timeout > 0) {
qs_conn_base_ctx *bctx = qos_get_conn_base_ctx(r->connection);
if(bctx && bctx->clientSocket) {
if(QS_ISDEBUG(r->server)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
QOS_LOGD_PFX"set connection timeout to %s seconds, id=%s",
tmostr, qos_unique_id(r, NULL));
}
apr_socket_timeout_set(bctx->clientSocket, timeout);
}
}
}
/*
* QS_DenyEvent
*/
if(apr_table_elts(dconf->rfilter_table)->nelts > 0) {
status = qos_hp_event_deny_filter(r, sconf, dconf);
if(status != DECLINED) {
return status;
}
}
/*
* QS_EventLimitCount
*/
if(sconf->event_limit_a->nelts > 0) {
status = qos_hp_event_limit(r, sconf);
if(status != DECLINED) {
return status;
}
}
/*
* QS_EventRequestLimit
*/
if(sconf->has_event_filter) {
status = qos_hp_event_filter(r, sconf);
if(status != DECLINED) {
return status;
}
}
/*
* QS_EventPerSecLimit/QS_EventKBytesPerSecLimit
*/
if(sconf->has_event_limit) {
event_kbytes_per_sec = qos_hp_event_count(r, &req_per_sec_block, &kbytes_per_sec_limit);
}
/*
* QS_SetEnvIfCmp
*/
qos_setenvifcmp(r, dconf->setenvcmp);
/*
* QS_ClientEventRequestLimit
*/
if(sconf->qos_cc_event_req >= 0) {
status = qos_hp_cc_event_count(r, sconf, rctx);
if(status != DECLINED) {
return status;
}
}
/*
* QS_ClientSerialize
*/
if(sconf->qos_cc_serialize && apr_table_get(r->subprocess_env, QS_SERIALIZE)) {
qos_hp_cc_serialize(r, sconf, rctx);
}
/*
* QS_SrvSerialize
*/
if((sconf->serialize == 1) && apr_table_get(r->subprocess_env, QS_SRVSERIALIZE)) {
qos_hp_srv_serialize(r, sconf, rctx);
}
/*
* client control
*/
if(qos_hp_cc(r, sconf, &msg, &uid) != DECLINED) {
int rc;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
"%s, id=%s", msg == NULL ? "-" : msg,
qos_unique_id(r, uid));
qs_set_evmsg(r, "D;");
if(!sconf->log_only) {
rc = qos_error_response(r, error_page);
if((rc == DONE) || (rc == HTTP_MOVED_TEMPORARILY)) {
return rc;
}
return m_retcode;
}
}
/*
* Request level control
* get rule with conditional enforcement
*/
actEntryCond = qos_getcondrule_byregex(r, sconf);
/* 1st prio has "Match" rule */
actEntry = qos_getrule_byregex(r, sconf);
/* 2th prio has "URL" rule */
if(!actEntry) actEntry = qos_getrule_bylocation(r, sconf);
if(actEntry) {
actEntryMain = actEntry;
} else if(actEntryCond) {
actEntryMain = actEntryCond;
}
if(!rctx) {
rctx = qos_rctx_config_get(r);
}
// optimistic locking (write only)
if(actEntryMain) {
rctx->entry_cond = actEntryCond;
rctx->entry = actEntry;
apr_global_mutex_lock(actEntryMain->lock); /* @CRT5 */
if(actEntryCond) {
actEntryCond->counter++;
}
if(actEntry) {
actEntry->counter++;
if(actEntry->req_per_sec_block_rate > req_per_sec_block) {
/* update req_per_sec_block if event restriction has returned worse block rate */
req_per_sec_block = actEntry->req_per_sec_block_rate;
}
}
apr_global_mutex_unlock(actEntryMain->lock); /* @CRT5 */
}
if(actEntry) {
/*
* QS_LocRequestLimitMatch/QS_LocRequestLimit/QS_LocRequestLimitDefault enforcement
*/
if(actEntry->limit && (actEntry->counter > actEntry->limit)) {
/* vip session has no limitation */
if(rctx->is_vip) {
qs_set_evmsg(r, "S;");
} else {
/* std user */
int rc;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(010)"access denied%s, QS_LocRequestLimit* rule: %s(%d),"
" concurrent requests=%d,"
" c=%s, id=%s",
sconf->log_only ? " (log only)" : "",
actEntry->url, actEntry->limit, actEntry->counter,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "010"));
QS_INC_EVENT(sconf, 10);
qs_set_evmsg(r, "D;");
// request has already been blocked, don't cont this request for req/sec violations!
apr_table_set(r->notes, QS_R010_ALREADY_BLOCKED, "");
if(!sconf->log_only) {
rc = qos_error_response(r, error_page);
if((rc == DONE) || (rc == HTTP_MOVED_TEMPORARILY)) {
return rc;
}
return m_retcode;
}
}
}
/*
* QS_LocRequestPerSecLimit/QS_EventPerSecLimit enforcement
*/
if(req_per_sec_block) {
if(rctx->is_vip) {
qs_set_evmsg(r, "S;");
} else {
qs_set_evmsg(r, "L;");
if(!sconf->log_only) {
apr_sleep(req_per_sec_block*1000);
}
/* don't wait more than once */
req_per_sec_block = 0;
}
}
/*
* QS_LocKBytesPerSecLimit selection
*/
if(actEntry->kbytes_per_sec_limit) {
if(kbytes_per_sec_limit) {
if(actEntry->kbytes_per_sec_limit < kbytes_per_sec_limit) {
// this is lower than the event limitation
kbytes_per_sec_limit = actEntry->kbytes_per_sec_limit;
event_kbytes_per_sec = actEntry;
}
} else {
kbytes_per_sec_limit = actEntry->kbytes_per_sec_limit;
event_kbytes_per_sec = actEntry;
}
}
}
/*
* QS_EventKBytesPerSecLimit or QS_LocKBytesPerSecLimit enforcement
*/
if(kbytes_per_sec_limit) {
if(rctx->is_vip) {
qs_set_evmsg(r, "S;");
} else {
qos_delay_ctx_t *dctx = apr_pcalloc(r->pool, sizeof(qos_delay_ctx_t));
dctx->rctx = rctx;
dctx->entry = event_kbytes_per_sec;
if(!sconf->log_only) {
ap_add_output_filter("qos-out-filter-delay", dctx, r, r->connection);
}
}
}
if(actEntryCond) {
/*
* QS_CondLocRequestLimitMatch
*/
if(actEntryCond->limit && (actEntryCond->counter > actEntryCond->limit)) {
/* check condition */
const char *condition = apr_table_get(r->subprocess_env, QS_COND);
if(condition) {
if(ap_regexec(actEntryCond->condition, condition, 0, NULL, 0) == 0) {
/* vip session has no limitation */
if(rctx->is_vip) {
qs_set_evmsg(r, "S;");
} else {
/* std user */
int rc;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(011)"access denied, QS_CondLocRequestLimitMatch"
" rule: %s(%d),"
" concurrent requests=%d,"
" c=%s, id=%s",
actEntryCond->url, actEntryCond->limit, actEntryCond->counter,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "011"));
QS_INC_EVENT(sconf, 11);
qs_set_evmsg(r, "D;");
if(!sconf->log_only) {
rc = qos_error_response(r, error_page);
if((rc == DONE) || (rc == HTTP_MOVED_TEMPORARILY)) {
return rc;
}
return m_retcode;
}
}
}
}
}
}
/*
* QS_EventPerSecLimit
*/
if(req_per_sec_block) {
qs_set_evmsg(r, "L;");
if(!sconf->log_only) {
apr_sleep(req_per_sec_block*1000);
}
}
/*
* QS_Delay
*/
qos_delay(r, sconf);
}
return DECLINED;
}
/**
* QS_LimitRequestBody
* Input filter limiting request body size for chunked encoded requests.
*
* @param f
* @param bb
* @param mode
* @param block
* @param nbytes
* @return
*/
static apr_status_t qos_in_filter3(ap_filter_t *f, apr_bucket_brigade *bb,
ap_input_mode_t mode, apr_read_type_e block,
apr_off_t nbytes) {
apr_status_t rv = ap_get_brigade(f->next, bb, mode, block, nbytes);
request_rec *r = f->r;
qos_srv_config *sconf = ap_get_module_config(r->server->module_config, &qos_module);
qos_dir_config *dconf = ap_get_module_config(r->per_dir_config, &qos_module);
apr_off_t maxpost = qos_maxpost(r, sconf, dconf);
if(rv != APR_SUCCESS) {
return rv;
}
if(maxpost != -1) {
apr_size_t bytes = 0;
apr_bucket *b;
qs_req_ctx *rctx = qos_rctx_config_get(r);
for(b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b)) {
bytes = bytes + b->length;
}
rctx->maxpostcount += bytes;
if(rctx->maxpostcount > maxpost) {
int rc;
const char *error_page = sconf->error_page;
qs_req_ctx *rctx = qos_rctx_config_get(r);
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(044)"access denied%s, QS_LimitRequestBody:"
" max=%"APR_OFF_T_FMT" this=%"APR_OFF_T_FMT", c=%s, id=%s",
sconf->log_only ? " (log only)" : "",
maxpost, rctx->maxpostcount,
QS_CONN_REMOTEIP(r->connection) == NULL ? "-" : QS_CONN_REMOTEIP(r->connection),
qos_unique_id(r, "044"));
QS_INC_EVENT(sconf, 44);
qs_set_evmsg(r, "D;");
if(!sconf->log_only) {
rc = qos_error_response(r, error_page);
if((rc == DONE) || (rc == HTTP_MOVED_TEMPORARILY)) {
return rc;
}
return HTTP_REQUEST_ENTITY_TOO_LARGE;
}
}
}
return APR_SUCCESS;
}
/**
* Input filter removes connection from sconf->inctx_t->table
* when reading EOS.
*
* @param f
* @param bb
* @param mode
* @param block
* @param nbytes
* @return
*/
static apr_status_t qos_in_filter2(ap_filter_t *f, apr_bucket_brigade *bb,
ap_input_mode_t mode, apr_read_type_e block,
apr_off_t nbytes) {
qos_ifctx_t *inctx = f->ctx;
apr_status_t rv = ap_get_brigade(f->next, bb, mode, block, nbytes);
if((rv == APR_SUCCESS) && APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(inctx->c->base_server->module_config,
&qos_module);
ap_remove_input_filter(f);
#if APR_HAS_THREADS
if(!sconf->inctx_t->exit) {
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT28 */
apr_table_unset(sconf->inctx_t->table,
QS_INCTX_ID);
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT28 */
}
#endif
}
return rv;
}
/**
* Input filter, used to log timeout event, mark slow clients,
* and to calculate packet rate.
*
* Adds/removes the connection from the sconf->inctx_t->table
* dapending of the request state (read head/body, keepalive, ...).
*
* @param f
* @param bb
* @param mode
* @param block
* @param nbytes
* @return
*/
static apr_status_t qos_in_filter(ap_filter_t *f, apr_bucket_brigade *bb,
ap_input_mode_t mode, apr_read_type_e block,
apr_off_t nbytes) {
apr_status_t rv;
qos_ifctx_t *inctx = f->ctx;
apr_size_t bytes = 0;
int crs = inctx->status;
rv = ap_get_brigade(f->next, bb, mode, block, nbytes);
if(rv == APR_SUCCESS) {
if(inctx->lowrate != -1) {
bytes = qos_packet_rate(inctx, bb);
}
}
if(inctx->status == QS_CONN_STATE_KEEP) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(inctx->c->base_server->module_config,
&qos_module);
inctx->time = time(NULL);
inctx->nbytes = 0;
inctx->status = QS_CONN_STATE_HEAD;
#if APR_HAS_THREADS
if(sconf->inctx_t && !sconf->inctx_t->exit && sconf->min_rate_off == 0) {
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT23 */
apr_table_setn(sconf->inctx_t->table,
QS_INCTX_ID,
(char *)inctx);
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT23 */
}
#endif
}
if(rv != APR_SUCCESS) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(inctx->c->base_server->module_config,
&qos_module);
inctx->status = QS_CONN_STATE_END;
inctx->time = 0;
inctx->nbytes = 0;
#if APR_HAS_THREADS
if(sconf->inctx_t && !sconf->inctx_t->exit) {
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT24 */
apr_table_unset(sconf->inctx_t->table,
QS_INCTX_ID);
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT24 */
}
#endif
}
if(inctx->status > QS_CONN_STATE_NEW) {
if(rv == APR_SUCCESS) {
if(bytes == 0) {
apr_bucket *b;
for(b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b)) {
bytes = bytes + b->length;
}
}
inctx->nbytes = inctx->nbytes + bytes;
inctx->hasBytes = inctx->nbytes;
if(inctx->status == QS_CONN_STATE_BODY) {
if(inctx->cl_val >= bytes) {
inctx->cl_val = inctx->cl_val - bytes;
}
if(inctx->cl_val == 0) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(inctx->c->base_server->module_config,
&qos_module);
#if APR_HAS_THREADS
if(!sconf->inctx_t->exit) {
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT27 */
apr_table_unset(sconf->inctx_t->table,
QS_INCTX_ID);
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT27 */
}
#endif
}
}
}
if((rv == APR_TIMEUP) &&
(crs != QS_CONN_STATE_END) &&
(crs != QS_CONN_STATE_KEEP)) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(inctx->c->base_server->module_config,
&qos_module);
/* mark clients causing a timeout */
if(sconf && sconf->has_qos_cc) {
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
qos_s_entry_t **clientEntry = NULL;
qos_s_entry_t searchE;
request_rec *r = f->r;
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT18 */
qos_ip_str2long(QS_CONN_REMOTEIP(inctx->c), searchE.ip6);
clientEntry = qos_cc_getOrSet(u->qos_cc, &searchE, 0);
(*clientEntry)->lowrate = time(NULL);
(*clientEntry)->lowratestatus |= QOS_LOW_FLAG_TIMEOUT;
if(r) {
qs_set_evmsg(r, "r;");
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT18 */
}
inctx->lowrate = QS_PKT_RATE_TH + 1;
}
}
return rv;
}
/**
* BrokenConnection
*/
static apr_status_t qos_out_filter_brokencon(ap_filter_t *f, apr_bucket_brigade *bb) {
apr_status_t rc = ap_pass_brigade(f->next, bb);
if(rc == APR_ECONNABORTED || rc == APR_EPIPE) {
// client closed the connection (abort or broken pipe)
request_rec *r = f->r;
qs_set_evmsg(r, "A;");
apr_table_set(r->connection->notes, QS_BROKEN_CON, "");
}
return rc;
}
/**
* QS_SetEnvIfResBody
*
* Searches the response body for the pattern defined by the QS_SetEnvIfResBody
* directive (supports only one search pattern (literal string)).
*
* @param f
* @param bb
* @return
*/
static apr_status_t qos_out_filter_body(ap_filter_t *f, apr_bucket_brigade *bb) {
request_rec *r = f->r;
qos_dir_config *dconf = ap_get_module_config(r->per_dir_config, &qos_module);
qs_req_ctx *rctx;
int len;
apr_bucket *b;
if((dconf == NULL) || (dconf->response_pattern == NULL)) {
// not used
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, bb);
}
rctx = qos_rctx_config_get(r);
len = dconf->response_pattern_len;
if((apr_table_get(r->subprocess_env, "QS_SetEnvIfResBodyIgnore") != NULL) &&
rctx->body_window == NULL) {
// skip this response (disabled and nothing processed yet)
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, bb);
}
for(b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b)) {
if(APR_BUCKET_IS_EOS(b)) {
/* If we ever see an EOS, make sure to FLUSH. */
apr_bucket *flush = apr_bucket_flush_create(f->c->bucket_alloc);
APR_BUCKET_INSERT_BEFORE(b, flush);
}
if(!(APR_BUCKET_IS_METADATA(b))) {
const char *buf;
apr_size_t nbytes;
if(apr_bucket_read(b, &buf, &nbytes, APR_BLOCK_READ) == APR_SUCCESS) {
if(nbytes > 0) {
int blen = nbytes > len ? len : nbytes - 1;
/* 1. overlap: this buffer avoids that we miss a string if it is cut apart
within two buckets
e.g., [Logi][n Page] instead of [Login Page] when searching for "Login Page" */
if(rctx->body_window == NULL) {
// first call, create a window buffer
rctx->body_window = apr_pcalloc(r->pool, (len*2)+1);
rctx->body_window[0] = '\0';
} else {
// subsequent call, searches within the window too
int wlen = strlen(rctx->body_window);
strncpy(&rctx->body_window[wlen], buf, blen);
rctx->body_window[wlen+blen+1] = '\0';
if(strstr(rctx->body_window, dconf->response_pattern)) {
/* found pattern */
if(dconf->response_pattern_var[0] == '!') {
apr_table_unset(r->subprocess_env, &dconf->response_pattern_var[1]);
} else {
apr_table_set(r->subprocess_env, dconf->response_pattern_var, dconf->response_pattern);
}
ap_remove_output_filter(f);
}
}
/* 2. new buffer (don't want to copy the data) */
if(qos_strnstr(buf, dconf->response_pattern, nbytes)) {
/* found pattern */
if(dconf->response_pattern_var[0] == '!') {
apr_table_unset(r->subprocess_env, &dconf->response_pattern_var[1]);
} else {
apr_table_set(r->subprocess_env, dconf->response_pattern_var, dconf->response_pattern);
}
ap_remove_output_filter(f);
}
/* 3. store the end (for next loop) */
strncpy(rctx->body_window, &buf[nbytes - blen], blen);
rctx->body_window[blen] = '\0';
}
}
}
}
return ap_pass_brigade(f->next, bb);
}
/**
* Helper used by qos_out_filter_delay() to calculate/update
* the delay rate. Shall be called for every bucket we are
* sending to the client.
* @param request_time Time this request has started
* @param entry Rule entry to update / measure
* @param length Bytes we are going to transfer
* @return Wait time in microsseconds
*/
static apr_off_t qos_calc_kbytes_per_sec_wait_time(apr_time_t request_time,
qs_acentry_t *entry,
apr_off_t length) {
apr_off_t kbps_wait_time;
apr_global_mutex_lock(entry->lock); /* @CRT43 */
kbps_wait_time = entry->kbytes_per_sec_block_rate;
if(((entry->bytes / 1024) > entry->kbytes_per_sec_limit) ||
(request_time > (entry->kbytes_interval_us + APR_USEC_PER_SEC))) {
/* transferred more than the limit/sec OR
it's long time ago since we last updated the rate
=> check within which time we did */
apr_time_t now = apr_time_now();
apr_time_t duration = now - entry->kbytes_interval_us;
apr_off_t kbs;
if(duration == 0) {
duration = 1;
}
kbs = entry->bytes * 1000 / duration;
entry->kbytes_per_sec = (entry->kbytes_per_sec + kbs) / 2;
if(duration > APR_USEC_PER_SEC) {
// lower than the defined kbytes/sec rate
if(kbps_wait_time > 0) {
// reduce wait time
apr_off_t newtime = kbps_wait_time * kbs / entry->kbytes_per_sec_limit;
// PI like closed-loop control
kbps_wait_time = (kbps_wait_time + newtime) / 2;
}
} else {
// higher than the defined kbytes/sec rate
if(kbps_wait_time == 0) {
kbps_wait_time = 1000; // start with 1 ms
} else {
// increase wait time
apr_off_t newtime = kbps_wait_time * kbs / entry->kbytes_per_sec_limit;
// PI like closed-loop control
kbps_wait_time = (kbps_wait_time + newtime) / 2;
}
}
if(kbps_wait_time > QS_MAX_DELAY) {
kbps_wait_time = QS_MAX_DELAY;
}
entry->kbytes_interval_us = now;
entry->bytes = 0;
}
entry->bytes = entry->bytes + length;
entry->kbytes_per_sec_block_rate = kbps_wait_time;
apr_global_mutex_unlock(entry->lock); /* @CRT43 */
return kbps_wait_time;
}
/**
* Output filter adds response delay.
*
* @param f
* @param bb
* @return
*/
static apr_status_t qos_out_filter_delay(ap_filter_t *f, apr_bucket_brigade *bb) {
qos_delay_ctx_t *dctx = f->ctx;
qs_acentry_t *entry = dctx->entry;
request_rec *r = f->r;
if(entry) {
apr_off_t length;
if(apr_brigade_length(bb, 1, &length) == APR_SUCCESS) {
if(length > 0) {
if(length > APR_BUCKET_BUFF_SIZE) {
// split (no proxy)
while(!APR_BRIGADE_EMPTY(bb)) {
apr_bucket *b, *first, *next;
apr_bucket_brigade *tmp_bb;
apr_status_t rv;
apr_off_t kbps_wait_time;
rv = apr_brigade_partition(bb, APR_BUCKET_BUFF_SIZE, &next);
if(rv != APR_SUCCESS && rv != APR_INCOMPLETE) {
return rv;
}
if(rv == APR_INCOMPLETE) { /* no split needed */
break;
}
first = APR_BRIGADE_FIRST(bb);
APR_BUCKET_REMOVE(first);
kbps_wait_time = qos_calc_kbytes_per_sec_wait_time(r->request_time,
entry, first->length);
if(kbps_wait_time > 0) {
dctx->rctx->response_delayed = (1 + dctx->rctx->response_delayed + kbps_wait_time) / 2;
apr_sleep(kbps_wait_time);
}
tmp_bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(tmp_bb, first);
b = apr_bucket_flush_create(f->c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(tmp_bb, b);
rv = ap_pass_brigade(f->next, tmp_bb);
if(rv != APR_SUCCESS) {
return rv;
}
}
} else {
// sleep once every 8k
apr_off_t kbps_wait_time = qos_calc_kbytes_per_sec_wait_time(r->request_time,
entry, length);
if(length < APR_BUCKET_BUFF_SIZE) {
// be fair with very small responses
kbps_wait_time = kbps_wait_time * length / APR_BUCKET_BUFF_SIZE;
}
if(kbps_wait_time > 0) {
dctx->rctx->response_delayed = (1 + dctx->rctx->response_delayed + kbps_wait_time) / 2;
apr_sleep(kbps_wait_time);
}
}
}
}
}
return ap_pass_brigade(f->next, bb);
}
/**
* Out filter measuring the minimal download bandwidth.
*
* @param f
* @param bb
* @return
*/
static apr_status_t qos_out_filter_min(ap_filter_t *f, apr_bucket_brigade *bb) {
request_rec *r = f->r;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config, &qos_module);
qos_ifctx_t *inctx = qos_get_ifctx(r->connection->input_filters);
if(APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
#if APR_HAS_THREADS
if(!sconf->inctx_t->exit) {
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT30 */
apr_table_unset(sconf->inctx_t->table,
QS_INCTX_ID);
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT30 */
}
#endif
inctx->status = QS_CONN_STATE_END;
ap_remove_output_filter(f);
} else {
apr_size_t total = 0;
apr_bucket *b;
for(b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b)) {
total = total + b->length;
}
inctx->nbytes = inctx->nbytes + total;
}
return ap_pass_brigade(f->next, bb);
}
/**
* Merges two rule tables. Entries whose key/name begin with a "+" are added
* while those with a "-" prefix are removed.
*
* @param p Pool to allocate new table from.
* @param b_rfilter_table Base rule table (parent)
* @param o_rfilter_table Over rule table (child)
* @return Merged table
*/
apr_table_t *qos_table_merge_create(apr_pool_t *p, apr_table_t *b_rfilter_table,
apr_table_t *o_rfilter_table) {
int i;
apr_table_t *rfilter_table = apr_table_make(p, apr_table_elts(b_rfilter_table)->nelts +
apr_table_elts(o_rfilter_table)->nelts);
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(b_rfilter_table)->elts;
// add additional (+) entries from the base/parent table
for(i = 0; i < apr_table_elts(b_rfilter_table)->nelts; ++i) {
if(entry[i].key[0] == '+') {
apr_table_setn(rfilter_table, entry[i].key, entry[i].val);
}
}
// add additional (+) entries from the over/child table
entry = (apr_table_entry_t *)apr_table_elts(o_rfilter_table)->elts;
for(i = 0; i < apr_table_elts(o_rfilter_table)->nelts; ++i) {
if(entry[i].key[0] == '+') {
apr_table_setn(rfilter_table, entry[i].key, entry[i].val);
}
}
// remove the "-" entries
for(i = 0; i < apr_table_elts(o_rfilter_table)->nelts; ++i) {
if(entry[i].key[0] == '-') {
char *id = apr_psprintf(p, "+%s", &entry[i].key[1]);
apr_table_unset(rfilter_table, id);
}
}
return rfilter_table;
}
/* QS_SrvMinDataRateOffEvent */
#if APR_HAS_THREADS
static void qos_disable_rate(request_rec *r, qos_srv_config *sconf,
qos_dir_config *dconf) {
if(dconf && sconf && (sconf->req_rate != -1) && (sconf->min_rate != -1)) {
apr_table_t *disable_reqrate_events = dconf->disable_reqrate_events;
if(apr_table_elts(sconf->disable_reqrate_events)->nelts > 0) {
disable_reqrate_events = qos_table_merge_create(r->pool, sconf->disable_reqrate_events,
dconf->disable_reqrate_events);
}
if(apr_table_elts(disable_reqrate_events)->nelts > 0) {
qos_ifctx_t *inctx = qos_get_ifctx(r->connection->input_filters);
if(inctx) {
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(disable_reqrate_events)->elts;
int i;
for(i = 0; i < apr_table_elts(disable_reqrate_events)->nelts; i++) {
char *v = entry[i].key;
if(apr_table_get(r->subprocess_env, &v[1])) {
inctx->disabled = 1;
break;
}
}
}
}
}
}
#endif
static void qos_start_res_rate(request_rec *r, qos_srv_config *sconf) {
if(sconf && (sconf->req_rate != -1) && (sconf->min_rate != -1)) {
qos_ifctx_t *inctx = qos_get_ifctx(r->connection->input_filters);
if(inctx) {
inctx->status = QS_CONN_STATE_RESPONSE;
inctx->time = time(NULL);
inctx->nbytes = 0;
#if APR_HAS_THREADS
if(sconf->inctx_t && !sconf->inctx_t->exit && sconf->min_rate_off == 0) {
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT29 */
apr_table_setn(sconf->inctx_t->table,
QS_INCTX_ID,
(char *)inctx);
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT29 */
}
ap_add_output_filter("qos-out-filter-min", NULL, r, r->connection);
#endif
}
}
}
/* QS_SET_DSCP */
static void qos_set_dscp(request_rec *r) {
const char *dscpStr = apr_table_get(r->subprocess_env, QS_SET_DSCP);
if(dscpStr) {
#ifdef __unix__
qs_conn_base_ctx *base = qos_get_conn_base_ctx(r->connection);
int rc = -2;
int hasSocket = 0;
if(base!=NULL && base->clientSocket!=NULL) {
apr_socket_t *sock = base->clientSocket;
int fd;
int dscp = atoi(dscpStr);
hasSocket = 1;
apr_os_sock_get(&fd, sock);
if(dscp >= 0 && dscp < 64) {
int tos = dscp << 2;
if(QS_ISDEBUG(r->server)) {
int n = 0;
const char *d = "unknown";
while(m_dscp_desc[n].id >= 0) {
if(m_dscp_desc[n].id == dscp) {
d = m_dscp_desc[n].name;
}
n++;
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
QOS_LOGD_PFX"%s=%s, tos=0x%02x, dscp=0x%02x (%s), id=%s",
QS_SET_DSCP, dscpStr, tos, dscp, d, qos_unique_id(r, NULL));
}
rc = setsockopt(fd,
IPPROTO_IP, IP_TOS,
&tos, sizeof(tos));
}
}
if(rc != 0) {
ap_log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r,
QOS_LOG_PFX(038)"DSCP, failed to set socket options, "
QS_SET_DSCP"=%s, socket=%s, rc=%d, id=%s",
dscpStr,
hasSocket ? "yes" : "no",
rc,
qos_unique_id(r, "038"));
}
#else
ap_log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r,
QOS_LOG_PFX(038)QS_SET_DSCP" is not available for this platform");
#endif
}
}
static void qos_end_res_rate(request_rec *r, qos_srv_config *sconf) {
if((sconf->req_rate != -1) && (sconf->min_rate != -1)) {
qos_ifctx_t *inctx = qos_get_ifctx(r->connection->input_filters);
if(inctx) {
inctx->time = time(NULL);
inctx->nbytes = 0;
if(r->connection->keepalive == AP_CONN_CLOSE) {
if(!sconf->inctx_t->exit) {
#if APR_HAS_THREADS
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT30 */
#endif
inctx->status = QS_CONN_STATE_END;
#if APR_HAS_THREADS
apr_table_unset(sconf->inctx_t->table,
QS_INCTX_ID);
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT30 */
#endif
}
} else {
if(!sconf->inctx_t->exit) {
#if APR_HAS_THREADS
apr_thread_mutex_lock(sconf->inctx_t->lock); /* @CRT30 */
#endif
if(inctx->status != QS_CONN_STATE_DESTROY) {
inctx->status = QS_CONN_STATE_KEEP;
#if APR_HAS_THREADS
apr_table_setn(sconf->inctx_t->table,
QS_INCTX_ID, (char *)inctx);
#endif
}
#if APR_HAS_THREADS
apr_thread_mutex_unlock(sconf->inctx_t->lock); /* @CRT30 */
#endif
}
}
}
}
}
/**
* process response:
* - start min data measure
* - setenvif header
* - detects vip header and create session
* - header filter
*/
static apr_status_t qos_out_filter(ap_filter_t *f, apr_bucket_brigade *bb) {
request_rec *r = f->r;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config, &qos_module);
qos_dir_config *dconf = ap_get_module_config(r->per_dir_config, &qos_module);
qs_headerfilter_mode_e mode;
qos_start_res_rate(r, sconf);
qos_setenvstatus(r, sconf, dconf);
qos_setenvresheader(r, sconf);
qos_setenvres(r, sconf);
if(sconf->user_tracking_cookie) {
if((sconf->user_tracking_cookie_jsredirect < 1) ||
(apr_table_get(r->subprocess_env, QOS_USER_TRACKING_RENEW) != NULL)) {
qos_send_user_tracking_cookie(r, sconf, r->status);
}
}
if(sconf->milestones) {
qos_update_milestone(r, sconf);
}
if(sconf->ip_header_name) {
const char *ctrl_h = apr_table_get(r->headers_out, sconf->ip_header_name);
if(ctrl_h) {
int match = 1;
if(sconf->ip_header_name_regex) {
if(ap_regexec(sconf->ip_header_name_regex, ctrl_h, 0, NULL, 0) != 0) {
match = 0;
}
}
if(match) {
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
if(cconf) {
qs_set_evmsg(r, "v;");
cconf->is_vip = 1;
cconf->set_vip_by_header = 1;
apr_table_set(r->subprocess_env, QS_ISVIPREQ, "yes");
}
}
if(sconf->ip_header_name_drop) {
apr_table_unset(r->headers_out, sconf->ip_header_name);
}
}
}
if(sconf->header_name) {
/* got a vip header: create new session (if non exists) */
const char *ctrl_h = apr_table_get(r->headers_out, sconf->header_name);
if(ctrl_h && !apr_table_get(r->notes, QS_REC_COOKIE)) {
int match = 1;
if(sconf->header_name_regex) {
if(ap_regexec(sconf->header_name_regex, ctrl_h, 0, NULL, 0) != 0) {
match = 0;
}
}
if(match) {
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
qos_set_session(r, sconf);
if(cconf) {
qs_set_evmsg(r, "v;");
cconf->is_vip = 1;
cconf->set_vip_by_header = 1;
apr_table_set(r->subprocess_env, QS_ISVIPREQ, "yes");
}
apr_table_set(r->notes, QS_REC_COOKIE, "");
}
if(sconf->header_name_drop) {
apr_table_unset(r->headers_out, sconf->header_name);
}
}
}
if(sconf->vip_user && r->user) {
if(!apr_table_get(r->notes, QS_REC_COOKIE)) {
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
qos_set_session(r, sconf);
if(cconf) {
qs_set_evmsg(r, "v;");
cconf->is_vip = 1;
cconf->set_vip_by_header = 1;
apr_table_set(r->subprocess_env, QS_ISVIPREQ, "yes");
}
apr_table_set(r->notes, QS_REC_COOKIE, "");
}
}
if(sconf->vip_ip_user && r->user) {
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
if(cconf) {
qs_set_evmsg(r, "v;");
cconf->is_vip = 1;
cconf->set_vip_by_header = 1;
apr_table_set(r->subprocess_env, QS_ISVIPREQ, "yes");
}
}
qos_unset_resheader(r, sconf);
/* don't handle response status since response header filter use "drop" action only */
mode = sconf->resheaderfilter;
if(dconf->resheaderfilter > QS_HEADERFILTER_OFF_DEFAULT) {
// override server configuration
mode = dconf->resheaderfilter;
}
if(mode > QS_HEADERFILTER_OFF) {
qos_header_filter(r, sconf, r->headers_out, "response",
sconf->reshfilter_table, mode);
}
qos_set_dscp(r);
qos_keepalive(r, sconf);
if(sconf->max_conn_close != -1) {
if(sconf->act->conn->connections > sconf->max_conn_close) {
qs_set_evmsg(r, "K;");
r->connection->keepalive = AP_CONN_CLOSE;
}
}
/* disable request rate for certain connections */
#if APR_HAS_THREADS
qos_disable_rate(r, sconf, dconf);
#endif
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, bb);
}
static apr_status_t qos_out_err_filter(ap_filter_t *f, apr_bucket_brigade *bb) {
request_rec *r = f->r;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config, &qos_module);
if(sconf) {
qos_dir_config *dconf = ap_get_module_config(r->per_dir_config, &qos_module);
qos_setenvstatus(r, sconf, dconf);
qos_setenvresheader(r, sconf);
qos_setenvres(r, sconf);
if(sconf->milestones) {
qos_update_milestone(r, sconf);
}
}
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, bb);
}
/**
* QS_SrvSerialize
*/
static void qos_logger_serialize(qos_srv_config *sconf, qs_req_ctx *rctx) {
if(rctx->srv_serialize_set) {
apr_global_mutex_lock(sconf->act->lock); /* @CRT45 */
sconf->act->serialize->locked = 0;
apr_global_mutex_unlock(sconf->act->lock); /* @CRT45 */
}
}
/**
* QS_EventRequestLimit
* reset event counter
*/
static void qos_event_reset(qos_srv_config *sconf, qs_req_ctx *rctx) {
int i;
apr_table_entry_t *entry;
apr_global_mutex_lock(sconf->act->lock); /* @CRT32 */
entry = (apr_table_entry_t *)apr_table_elts(rctx->event_entries)->elts;
for(i = 0; i < apr_table_elts(rctx->event_entries)->nelts; i++) {
qs_acentry_t *e = (qs_acentry_t *)entry[i].val;
if(e->counter > 0) {
e->counter--;
}
}
apr_global_mutex_unlock(sconf->act->lock); /* @CRT32 */
}
static int qos_fixup(request_rec * r) {
int rc = 0;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config,
&qos_module);
qos_dir_config *dconf = (qos_dir_config*)ap_get_module_config(r->per_dir_config,
&qos_module);
/* QS_VipUser/QS_VipIpUser */
if(sconf && (sconf->vip_user || sconf->vip_ip_user) && r->user) {
/* check r->user early (final status is update is implemented in output-filter) */
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
if(cconf) {
qs_set_evmsg(r, "v;");
cconf->is_vip = 1;
cconf->set_vip_by_header = 1;
apr_table_set(r->subprocess_env, QS_ISVIPREQ, "yes");
}
}
if(sconf->log_env == 1) {
qos_log_env(r, ">FX_1");
}
#if APR_HAS_THREADS
qos_disable_rate(r, sconf, dconf);
#endif
if(apr_table_elts(sconf->setreqheaderlate_t)->nelts > 0) {
qos_setreqheader(r, sconf->setreqheaderlate_t);
}
rc = qos_redirectif(r, sconf, sconf->redirectif);
if(rc != DECLINED) {
return rc;
}
rc = qos_redirectif(r, sconf, dconf->redirectif);
if(rc != DECLINED) {
return rc;
}
return DECLINED;
}
/**
* "free resources" and update stats
*/
static int qos_logger(request_rec *r) {
qs_req_ctx *rctx = qos_rctx_config_get(r);
qs_acentry_t *actEntry = rctx->entry;
qs_acentry_t *actEntryCond = rctx->entry_cond;
qs_acentry_t *actEntryMain = actEntry;
qs_conn_base_ctx *base = qos_get_conn_base_ctx(r->connection);
qs_conn_ctx *cconf = qos_get_cconf(r->connection);
apr_time_t now = 0;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config, &qos_module);
qos_dir_config *dconf = ap_get_module_config(r->per_dir_config, &qos_module);
if(actEntryMain == NULL) {
actEntryMain = actEntryCond;
}
if(rctx->response_delayed) {
qs_set_evmsg(r, "L;");
apr_table_set(r->subprocess_env, QS_RESDELYATIME,
apr_psprintf(r->pool, "%"APR_OFF_T_FMT, rctx->response_delayed));
}
qos_propagate_notes(r);
qos_propagate_events(r);
if(sconf->log_env == 1) {
qos_log_env(r, "<LG_1");
}
qos_end_res_rate(r, sconf);
if(sconf->setenvif_t->nelts > 0) {
qos_setenvif(r, sconf->setenvif_t);
}
if(dconf->setenvif_t->nelts > 0) {
qos_setenvif(r, dconf->setenvif_t);
}
if(sconf->has_qos_cc) {
qos_logger_cc(r, sconf, rctx);
}
qos_logger_event_limit(r, sconf);
if(base) {
base->requests++;
}
if(cconf) {
if(cconf->evmsg) {
rctx->evmsg = apr_pstrcat(r->pool, cconf->evmsg, rctx->evmsg, NULL);
}
}
if(sconf->has_event_filter) {
qos_event_reset(sconf, rctx);
}
if(sconf->serialize == 1) {
qos_logger_serialize(sconf, rctx);
}
if(sconf->has_event_limit) {
qos_lg_event_update(r, &now);
}
if(actEntryMain) {
char *h;
if(!now) {
now = apr_time_sec(r->request_time);
}
apr_global_mutex_lock(actEntryMain->lock); /* @CRT6 */
h = apr_psprintf(r->pool, "%d", actEntryMain->counter);
if(actEntryCond) {
if(actEntryCond->counter > 0) {
actEntryCond->counter--;
}
}
if(actEntry) {
if(actEntry->counter > 0) {
actEntry->counter--;
}
if(apr_table_get(r->notes, QS_R010_ALREADY_BLOCKED) == NULL) {
if(actEntry->req < LONG_MAX) {
actEntry->req++;
}
if(now > (actEntry->interval + QS_BW_SAMPLING_RATE)) {
actEntry->req_per_sec = actEntry->req / (now - actEntry->interval);
actEntry->req = 0;
actEntry->interval = now;
if(actEntry->req_per_sec_limit) {
qos_cal_req_sec(sconf, r, actEntry);
}
}
}
}
apr_global_mutex_unlock(actEntryMain->lock); /* @CRT6 */
/* allow logging of the current location usage */
apr_table_set(r->subprocess_env, "mod_qos_cr", h);
if(r->next) {
apr_table_set(r->next->subprocess_env, "mod_qos_cr", h);
}
/* decrement only once */
ap_set_module_config(r->request_config, &qos_module, NULL);
}
if(cconf && (cconf->sconf->max_conn != -1)) {
char *cc = apr_psprintf(r->pool, "%d", cconf->sconf->act->conn->connections);
apr_table_set(r->subprocess_env, "mod_qos_con", cc);
if(r->next) {
apr_table_set(r->next->subprocess_env, "mod_qos_con", cc);
}
}
if(rctx->evmsg) {
apr_table_set(r->subprocess_env, "mod_qos_ev", rctx->evmsg);
if(r->next) {
apr_table_set(r->next->subprocess_env, "mod_qos_ev", rctx->evmsg);
}
}
#if APR_HAS_THREADS
qos_disable_rate(r, sconf, dconf);
#endif
if(sconf->qslog_p) {
// ISBiDUkEQaC equiv %h %>s %B %{Content-Length}i %D %{mod_qos_user_id}e %k %{Event}e %{mod_qos_ev}e %{QS_AllConn}e '%v:%U'
apr_size_t nbytes;
const char *clID = apr_table_get(r->subprocess_env, QSLOG_CLID); // client identifier (individual users)
const char *event = apr_table_get(r->subprocess_env, QSLOG_EVENT); // generic event variable
const char *mod_qos_ev = apr_table_get(r->subprocess_env, "mod_qos_ev"); // qos events
const char *averageConn = apr_table_get(r->subprocess_env, QSLOG_AVERAGE); // average counter, e.g. connections
// TODO: measure the real traffic instead of using the (optional) content-length header
const char *contentLength = apr_table_get(r->headers_in, "Content-Length");
char *qslogstr = apr_psprintf(r->pool, "%s %s %s %s %s %s %d %s %s %s '%s:%s'\n",
QS_CONN_REMOTEIP(r->connection), /* %h */
(r->status <= 0) ? "-" : apr_itoa(r->pool, r->status), /* %s */
(!r->sent_bodyct || !r->bytes_sent) ? "0" : apr_off_t_toa(r->pool, r->bytes_sent), /* %B */
contentLength == NULL ? "-" : contentLength, /* %{content-length} */
apr_psprintf(r->pool, "%" APR_TIME_T_FMT, (apr_time_now() - r->request_time)), /* %D */
clID == NULL ? "-" : clID, /* %{mod_qos_user_id}e */
r->connection->keepalives ? r->connection->keepalives - 1 : 0, /* %k */
event == NULL ? "-" : event, /* %{Event}e */
mod_qos_ev == NULL ? "-" : mod_qos_ev, /* %{mod_qos_ev}e */
averageConn == NULL ? "- " : averageConn, /* %{QS_AllConn}e */
ap_escape_logitem(r->pool, ap_get_server_name(r)), /* %v */
ap_escape_logitem(r->pool, r->uri) /* %U */
);
nbytes = strlen(qslogstr);
apr_file_write(sconf->qslog_p, qslogstr, &nbytes);
}
return DECLINED;
}
static void qos_audit_check(ap_directive_t * node) {
ap_directive_t *pdir;
for(pdir = node; pdir != NULL; pdir = pdir->next) {
if(pdir->args &&
strstr(pdir->args, "%{"QS_PARP_PATH"}n") &&
strstr(pdir->args, "%{"QS_PARP_QUERY"}n")) {
m_enable_audit = 1;
}
if(pdir->first_child != NULL) {
qos_audit_check(pdir->first_child);
}
}
}
static int qos_module_check(const char *m) {
module *modp = NULL;
for(modp = ap_top_module; modp; modp = modp->next) {
if(strcmp(modp->name, m) == 0) {
return APR_SUCCESS;
}
}
return DECLINED;
}
/**
* inits each child
*/
static void qos_child_init(apr_pool_t *p, server_rec *bs) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(bs->module_config, &qos_module);
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
qos_ifctx_list_t *inctx_t = NULL;
#ifdef QS_INTERNAL_TEST
#ifdef PREFORK_MPM
int seed = getpid() + time(NULL);
#if APR_HAS_THREADS
seed += apr_os_thread_current();
#endif
srand(seed);
#endif
#endif
qos_init_unique_id(p, bs);
#if APR_HAS_THREADS
if(sconf->req_rate != -1) {
inctx_t = apr_pcalloc(p, sizeof(qos_ifctx_list_t));
inctx_t->exit = 0;
inctx_t->table = apr_table_make(p, 64);
sconf->inctx_t = inctx_t;
if(apr_thread_mutex_create(&sconf->inctx_t->lock, APR_THREAD_MUTEX_DEFAULT, p) != APR_SUCCESS) {
qos_disable_req_rate(bs, "create mutex");
} else {
apr_threadattr_t *tattr;
if(apr_threadattr_create(&tattr, p) != APR_SUCCESS) {
qos_disable_req_rate(bs, "create thread attr");
} else {
if(apr_thread_create(&sconf->inctx_t->thread, tattr,
qos_req_rate_thread, bs, p) != APR_SUCCESS) {
qos_disable_req_rate(bs, "create thread");
} else {
server_rec *sn = bs->next;
apr_pool_pre_cleanup_register(p, bs, qos_cleanup_req_rate_thread);
while(sn) {
qos_srv_config *sc = (qos_srv_config*)ap_get_module_config(sn->module_config, &qos_module);
sc->inctx_t = inctx_t;
sn = sn->next;
}
}
}
}
}
#endif
if(sconf->has_qos_cc) {
apr_global_mutex_child_init(&u->qos_cc->lock, u->qos_cc->lock_file, p);
}
if(!sconf->act->child_init) {
sconf->act->child_init = 1;
/* propagate mutex to child process (required by some platforms) */
apr_global_mutex_child_init(&sconf->act->lock, sconf->act->lock_file, p);
}
#if APR_HAS_THREADS
if(sconf->qsstatus) {
qos_init_status_thread(p, sconf, sconf->max_clients);
}
#endif
}
/*
static const char *qos_search_docroot(apr_pool_t *pconf, server_rec *bs,
ap_directive_t *node) {
ap_directive_t *pdir;
for(pdir = node; pdir != NULL; pdir = pdir->next) {
if(strcasecmp(pdir->directive, "DocumentRoot") == 0) {
return pdir->args;
}
if(pdir->first_child != NULL) {
const char *docroot = qos_search_docroot(pconf, bs, pdir->first_child);
if(docroot != NULL) {
return docroot;
}
}
}
return NULL;
}
*/
static const char *detectErrorPage(apr_pool_t *ptemp, server_rec *bs, ap_directive_t *pdir) {
const qos_errelt_t *e = m_error_pages;
apr_finfo_t finfo;
/*
const char *docroot = qos_search_docroot(ptemp, bs, pdir);
if(docroot) {
docroot = ap_server_root_relative(ptemp, docroot);
}
*/
while(e->path != NULL) {
char *path = ap_server_root_relative(ptemp, e->path);
if(apr_stat(&finfo, path, APR_FINFO_TYPE, ptemp) == APR_SUCCESS) {
return e->url;
}
/*
if(docroot) {
path = apr_pstrcat(ptemp, docroot, "/", e->path, NULL);
if(apr_stat(&finfo, path, APR_FINFO_TYPE, ptemp) == APR_SUCCESS) {
return e->url;
}
}
*/
e++;
}
return NULL;
}
/**
* inits the server configuration
*/
static int qos_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *bs) {
qos_srv_config *bsconf = (qos_srv_config*)ap_get_module_config(bs->module_config, &qos_module);
char *rev = qos_revision(ptemp);
qos_user_t *u;
int maxClients;
int cc_net_prefer_limit = 0;
apr_status_t rv;
ap_directive_t *pdir = ap_conftree;
const char *error_page = detectErrorPage(ptemp, bs, pdir);
int auto_error_page = 0;
/* verify if this Apache version is supported/mod_qos has been tested for
and enable/disable features */
qos_version_check(bs);
maxClients = qs_calc_maxClients(bs, bsconf);
QOS_MY_GENERATION(m_generation);
bsconf->max_clients = maxClients;
if(bsconf->ip_type == QS_IP_V4) {
m_ip_type = QS_IP_V4;
} else {
m_ip_type = QS_IP_V6;
}
qos_hostcode(ptemp, bs);
if(bsconf->log_only) {
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, bs,
QOS_LOG_PFX(009)"running in 'log only' mode - rules are NOT enforced!");
}
if(bsconf->geo_limit != -1 && !bsconf->geodb) {
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, bs,
QOS_LOG_PFX(100)"QS_ClientGeoCountryDB has not been configured");
}
if(bsconf->max_conn_close_percent) {
bsconf->max_conn_close = maxClients * bsconf->max_conn_close_percent / 100;
}
cc_net_prefer_limit = maxClients * bsconf->qos_cc_prefer / 100;
if(bsconf->qos_cc_prefer) {
bsconf->qos_cc_prefer = maxClients;
bsconf->qos_cc_prefer_limit = cc_net_prefer_limit;
} else {
bsconf->qos_cc_prefer = 0;
bsconf->qos_cc_prefer_limit = 0;
}
u = qos_get_user_conf(bs->process->pool);
if(u == NULL) return !OK;
u->server_start++;
/* mutex init */
if(bsconf->act->lock_file == NULL) {
bsconf->act->lock_file = apr_psprintf(bsconf->act->pool, "%s.mod_qos",
qos_tmpnam(bsconf->act->pool, bs));
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, bs,
QOS_LOGD_PFX"create mutex (ACT)(%s)",
bsconf->act->lock_file);
rv = apr_global_mutex_create(&bsconf->act->lock, bsconf->act->lock_file,
APR_LOCK_DEFAULT, bsconf->act->pool);
if (rv != APR_SUCCESS) {
char buf[MAX_STRING_LEN];
apr_strerror(rv, buf, sizeof(buf));
ap_log_error(APLOG_MARK, APLOG_EMERG, 0, bs,
QOS_LOG_PFX(004)"failed to create mutex (ACT)(%s): %s",
bsconf->act->lock_file, buf);
exit(1);
}
#ifdef AP_NEED_SET_MUTEX_PERMS
qos_unixd_set_global_mutex_perms(bsconf->act->lock);
#endif
}
bsconf->base_server = bs;
bsconf->act->timeout = apr_time_sec(bs->timeout);
if(bsconf->act->timeout == 0) bsconf->act->timeout = 300;
if(qos_init_shm(bs, bsconf, bsconf->act, bsconf->location_t, maxClients) != APR_SUCCESS) {
return !OK;
}
apr_pool_pre_cleanup_register(bsconf->pool, bsconf->act, qos_cleanup_shm);
if((qos_module_check("mod_unique_id.c") != APR_SUCCESS) &&
(qos_module_check("mod_navajo.cpp") != APR_SUCCESS)) {
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, bs,
QOS_LOG_PFX(009)"mod_unique_id not available"
" (mod_qos generates simple request id if required)");
}
qos_audit_check(ap_conftree);
qos_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
qos_ssl_var = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
if(qos_is_https == NULL || qos_ssl_var == NULL) {
ap_log_error(APLOG_MARK, APLOG_INFO, 0, bs,
QOS_LOG_PFX(009)"could not retrieve mod_ssl functions");
}
if(m_requires_parp) {
if(qos_module_check("mod_parp.c") != APR_SUCCESS) {
qos_parp_hp_table_fn = NULL;
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, bs,
QOS_LOG_PFX(009)"mod_parp not available"
" (required by some directives)");
} else {
qos_parp_hp_table_fn = APR_RETRIEVE_OPTIONAL_FN(parp_hp_table);
qos_parp_body_data_fn = APR_RETRIEVE_OPTIONAL_FN(parp_body_data);
}
}
if(u->server_start == 2) {
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(bsconf->hfilter_table)->elts;
for(i = 0; i < apr_table_elts(bsconf->hfilter_table)->nelts; i++) {
qos_fhlt_r_t *he = (qos_fhlt_r_t *)entry[i].val;
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, bs,
QOS_LOGD_PFX"request header filter rule (%s) %s: %s max=%d",
he->action == QS_FLT_ACTION_DROP ? "drop" : "deny", entry[i].key,
he->text, he->size);
}
entry = (apr_table_entry_t *)apr_table_elts(bsconf->reshfilter_table)->elts;
for(i = 0; i < apr_table_elts(bsconf->reshfilter_table)->nelts; i++) {
qos_fhlt_r_t *he = (qos_fhlt_r_t *)entry[i].val;
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, bs,
QOS_LOGD_PFX"response header filter rule (%s) %s: %s max=%d",
he->action == QS_FLT_ACTION_DROP ? "drop" : "deny", entry[i].key,
he->text, he->size);
}
}
if(bsconf->has_qos_cc) {
if(!u->qos_cc) {
u->qos_cc = qos_cc_new(bs->process->pool, bs, bsconf->qos_cc_size, bsconf->qos_cc_limitTable);
if(u->qos_cc == NULL) {
return !OK;
}
} else {
int configOk = 1;
int limitTableSize = apr_table_elts(bsconf->qos_cc_limitTable)->nelts;
if(u->qos_cc->limitTable) {
int i;
apr_table_entry_t *te = (apr_table_entry_t *)apr_table_elts(bsconf->qos_cc_limitTable)->elts;
for(i = 0; i < limitTableSize; i++) {
const char *name = te[i].key;
qos_s_entry_limit_conf_t *newentry = (qos_s_entry_limit_conf_t *)te[i].val;
qos_s_entry_limit_conf_t *entryConf = (qos_s_entry_limit_conf_t *)apr_table_get(u->qos_cc->limitTable, name);
if(entryConf) {
entryConf->limit = newentry->limit;
entryConf->limitTime = newentry->limitTime;
entryConf->condStr = NULL;
entryConf->preg = NULL;
if(newentry->condStr) {
entryConf->condStr = apr_pstrdup(bs->process->pool, newentry->condStr);
entryConf->preg = ap_pregcomp(bs->process->pool, newentry->condStr, AP_REG_EXTENDED);
}
} else {
// new variable
configOk = 0;
}
if(apr_table_elts(u->qos_cc->limitTable)->nelts != limitTableSize) {
// removed variable
configOk = 0;
}
}
} else {
if(limitTableSize > 0) {
// enabled after graceful restart
configOk = 0;
}
}
if(!configOk) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, bs,
QOS_LOG_PFX(001)"QS_ClientEventLimitCount directives"
" can't be added/removed by graceful restart. A server"
" restart is required to apply the new configuration!");
}
}
}
if(bsconf->error_page == NULL && error_page != NULL) {
bsconf->error_page = error_page;
auto_error_page = 1;
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, bs,
QOS_LOGD_PFX"QS_ErrorPage: use %s for server %s:%d (global)",
error_page,
bs->server_hostname == NULL ? "-" : bs->server_hostname,
bs->addrs->host_port);
}
if(bsconf->qslog_str) {
char *qslogarg = apr_psprintf(pconf, "%s -f %s", bsconf->qslog_str, QSLOGFORMAT);
piped_log *pl = ap_open_piped_log(pconf, qslogarg);
if(pl == NULL) {
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, bs,
QOS_LOG_PFX(009)"failed to initialize the qslog facility '%s'", qslogarg);
}
bsconf->qslog_p = ap_piped_log_write_fd(pl);
}
{
server_rec *s = bs->next;
while(s) {
qos_srv_config *ssconf = (qos_srv_config*)ap_get_module_config(s->module_config, &qos_module);
/* mutex init */
if(ssconf->act->lock == NULL) {
ssconf->act->lock_file = bsconf->act->lock_file;
ssconf->act->lock = bsconf->act->lock;
}
ssconf->base_server = bs;
ssconf->act->timeout = apr_time_sec(s->timeout);
ssconf->qos_cc_prefer = bsconf->qos_cc_prefer;
ssconf->qos_cc_prefer_limit = bsconf->qos_cc_prefer_limit;
ssconf->max_clients = bsconf->max_clients;
ssconf->max_clients_conf = bsconf->max_clients_conf;
if(ssconf->max_conn_close_percent) {
ssconf->max_conn_close = maxClients * ssconf->max_conn_close_percent / 100;
}
if(ssconf->act->timeout == 0) {
ssconf->act->timeout = 300;
}
ssconf->qslog_p = bsconf->qslog_p;
if(ssconf->is_virtual) {
if(qos_init_shm(s, ssconf, ssconf->act, ssconf->location_t, maxClients) != APR_SUCCESS) {
return !OK;
}
apr_pool_pre_cleanup_register(ssconf->pool, ssconf->act,
qos_cleanup_shm);
if(ssconf->has_conn_counter == 0 && bsconf->has_conn_counter == 1) {
// shall use global counter because vhost has not QS_SrvMaxConn* directive
ssconf->act->conn = bsconf->act->conn;
}
}
if(ssconf->error_page == NULL && error_page != NULL) {
ssconf->error_page = error_page;
auto_error_page |= 2;
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, bs,
QOS_LOGD_PFX"QS_ErrorPage: use %s for server %s:%d",
error_page,
s->server_hostname == NULL ? "-" : s->server_hostname,
s->addrs->host_port);
}
s = s->next;
}
}
if(auto_error_page) {
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, bs,
QOS_LOG_PFX(009)"found default error document '%s'. Use the QS_ErrorPage"
" directive to override this default page.",
error_page);
}
ap_add_version_component(pconf, apr_psprintf(pconf, "mod_qos/%s", rev));
#ifdef QS_INTERNAL_TEST
fprintf(stdout, "\033[1mmod_qos TEST BINARY, NOT FOR PRODUCTIVE USE\033[0m\n");
fflush(stdout);
#endif
#ifndef QS_NO_STATUS_HOOK
APR_OPTIONAL_HOOK(ap, status_hook, qos_ext_status_hook, NULL, NULL, APR_HOOK_MIDDLE);
#endif
return DECLINED;
}
/**
* mod_qos
*/
static int qos_favicon(request_rec *r) {
int i;
unsigned const char ico[] = {
0x00,0x00,0x01,0x00,0x01,0x00,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x68,0x05,
0x00,0x00,0x16,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x20,0x00,
0x00,0x00,0x01,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0f,0x29,0x21,0x00,0x11,0x29,0x21,0x00,0x9c,0x9d,0x9c,0x00,0x8d,0x8e,
0x8d,0x00,0x65,0x65,0x65,0x00,0x73,0xaf,0x9d,0x00,0xf1,0xf3,0xf2,0x00,0x04,0x0e,
0x0b,0x00,0x05,0x0e,0x0b,0x00,0x1b,0x3b,0x31,0x00,0x26,0x60,0x4d,0x00,0x45,0x45,
0x45,0x00,0x9c,0xc8,0xb9,0x00,0x38,0x89,0x6e,0x00,0x35,0x7d,0x67,0x00,0x7d,0x7d,
0x7d,0x00,0x6f,0x27,0x80,0x00,0x3d,0x28,0x3d,0x00,0x0c,0x10,0x0f,0x00,0x04,0x05,
0x05,0x00,0x5f,0x64,0x62,0x00,0x20,0x50,0x42,0x00,0x85,0xca,0xb6,0x00,0x61,0x22,
0x98,0x00,0x76,0xb4,0xa2,0x00,0x69,0x6a,0x6a,0x00,0x02,0x03,0x03,0x00,0xaa,0xda,
0xca,0x00,0x25,0x5c,0x4a,0x00,0xfc,0xfc,0xfc,0x00,0x87,0xae,0xa2,0x00,0xaa,0xcc,
0xc0,0x00,0x01,0x01,0x01,0x00,0x6a,0xa0,0x91,0x00,0x31,0x75,0x5f,0x00,0x44,0xa5,
0x85,0x00,0xe6,0xe5,0xec,0x00,0x31,0x7a,0x62,0x00,0x0b,0x1d,0x16,0x00,0xc2,0xcb,
0xdc,0x00,0x2e,0x6c,0x58,0x00,0x22,0x53,0x44,0x00,0xa5,0xd4,0xc4,0x00,0x3e,0x42,
0x41,0x00,0x68,0x85,0x7b,0x00,0x31,0x5a,0x51,0x00,0x55,0x4e,0xd5,0x00,0x8b,0x8b,
0x8a,0x00,0x02,0x06,0x05,0x00,0x04,0x06,0x05,0x00,0x48,0x62,0x5b,0x00,0x0c,0x1d,
0x17,0x00,0x01,0x04,0x03,0x00,0x03,0x04,0x03,0x00,0x2f,0x3d,0x38,0x00,0x65,0x81,
0x77,0x00,0xef,0xf1,0xf5,0x00,0x57,0x25,0x51,0x00,0xc1,0xbd,0xc3,0x00,0x34,0x81,
0x69,0x00,0x39,0x5d,0x52,0x00,0xff,0xff,0xff,0x00,0x2f,0x31,0x31,0x00,0x79,0x7d,
0xd5,0x00,0x1b,0x46,0x39,0x00,0x4d,0x46,0xdd,0x00,0x13,0x13,0x13,0x00,0x5a,0x40,
0x71,0x00,0xb4,0xb4,0xb4,0x00,0x71,0x74,0x73,0x00,0x4c,0x59,0x55,0x00,0x02,0x02,
0x02,0x00,0xec,0xec,0xec,0x00,0x6f,0x72,0x71,0x00,0x67,0x67,0x67,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x3e,
0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,
0x3e,0x3e,0x3e,0x4a,0x26,0x26,0x15,0x3e,0x3e,0x3e,0x3e,0x1e,0x28,0x39,0x3e,0x3f,
0x3e,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x20,0x2f,0x11,0x18,0x25,0x3e,0x3e,0x3e,
0x00,0x24,0x08,0x00,0x05,0x4b,0x21,0x42,0x3a,0x0f,0x40,0x3e,0x3e,0x3e,0x3e,0x17,
0x2e,0x00,0x0c,0x45,0x00,0x00,0x44,0x12,0x24,0x09,0x3c,0x3e,0x3e,0x3e,0x3c,0x3c,
0x00,0x3c,0x00,0x00,0x0d,0x2b,0x24,0x24,0x00,0x00,0x3c,0x46,0x3e,0x3e,0x3c,0x3c,
0x30,0x43,0x24,0x31,0x1c,0x1c,0x0e,0x00,0x48,0x3b,0x3c,0x3c,0x3e,0x3e,0x3c,0x06,
0x3e,0x00,0x23,0x1c,0x1c,0x1c,0x1c,0x00,0x36,0x3e,0x16,0x3c,0x3e,0x3e,0x3c,0x22,
0x3e,0x00,0x33,0x1c,0x37,0x1f,0x1c,0x3d,0x14,0x3e,0x41,0x3c,0x3e,0x3e,0x3c,0x3c,
0x49,0x00,0x00,0x32,0x1c,0x1c,0x2a,0x24,0x00,0x3e,0x3c,0x3c,0x3e,0x3e,0x47,0x3c,
0x00,0x00,0x27,0x24,0x29,0x13,0x00,0x02,0x24,0x00,0x3c,0x0a,0x3e,0x3e,0x3e,0x3c,
0x19,0x34,0x24,0x21,0x48,0x1b,0x00,0x00,0x01,0x0b,0x3c,0x3e,0x3e,0x3e,0x3e,0x3e,
0x1d,0x3c,0x00,0x1a,0x3e,0x3e,0x10,0x00,0x3c,0x35,0x3e,0x3e,0x3e,0x3e,0x3e,0x2c,
0x3e,0x3c,0x3c,0x3c,0x2d,0x38,0x3c,0x3c,0x3c,0x07,0x3f,0x3e,0x3e,0x3e,0x3e,0x3e,
0x3e,0x3e,0x03,0x3c,0x3c,0x3c,0x3c,0x04,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,
0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x3e,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
ap_set_content_type(r, "image/x-icon");
for(i=0; i < sizeof(ico); i++) {
ap_rputc(ico[i], r);
}
return OK;
}
static int qos_console_dump(request_rec * r, const char *event) {
qos_srv_config *sconf = sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config,
&qos_module);
if(sconf && sconf->has_qos_cc) {
int i = 0;
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
qos_s_entry_t **clientEntry = NULL;
/* table requires heap (100'000 ~ 4MB) but we avoid io with drawn lock */
apr_table_t *iptable = apr_table_make(r->pool, u->qos_cc->max);
apr_table_entry_t *entry;
apr_time_t now = apr_time_sec(r->request_time);
ap_set_content_type(r, "text/plain");
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT35 */
clientEntry = u->qos_cc->ipd;
for(i = 0; i < u->qos_cc->max; i++) {
if((clientEntry[i]->ip6[0] != 0) ||
(clientEntry[i]->ip6[1] != 0)) {
char *k;
int limit = 0;
time_t limitTime = 0;
if(u->qos_cc->limitTable) {
int limitTableIndex;
qos_s_entry_limit_conf_t *eventLimitConf = qos_getQSLimitEvent(u, event, &limitTableIndex);
if(eventLimitConf) {
limit = clientEntry[i]->limit[limitTableIndex].limit;
limitTime = (eventLimitConf->limitTime >= (time(NULL) - clientEntry[i]->limit[limitTableIndex].limitTime)) ?
(eventLimitConf->limitTime - (time(NULL) - clientEntry[i]->limit[limitTableIndex].limitTime)) : 0;
}
}
k = apr_psprintf(r->pool,
"%010d %s vip=%s lowprio=%s block=%hu/%ld limit=%d/%ld %ld",
i,
qos_ip_long2str(r->pool, clientEntry[i]->ip6),
clientEntry[i]->vip ? "yes" : "no",
(clientEntry[i]->lowrate + QOS_LOW_TIMEOUT) > now ? "yes" : "no",
clientEntry[i]->block,
(sconf->qos_cc_blockTime >= (time(NULL) - clientEntry[i]->blockTime)) ?
(sconf->qos_cc_blockTime - (time(NULL) - clientEntry[i]->blockTime)) : 0,
limit,
limitTime,
clientEntry[i]->time);
apr_table_addn(iptable, k, NULL);
}
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT35 */
entry = (apr_table_entry_t *)apr_table_elts(iptable)->elts;
for(i = 0; i < apr_table_elts(iptable)->nelts; ++i) {
ap_rprintf(r, "%s\n", entry[i].key);
}
return OK;
}
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(070)"console, not acceptable, "
"qos client control has not been activated, id=%s",
qos_unique_id(r, "070"));
return HTTP_NOT_ACCEPTABLE;
}
#ifdef QS_INTERNAL_TEST
static int qos_handler_headerfilter(request_rec * r) {
int i;
apr_table_entry_t *entry;
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config, &qos_module);
if(strcmp(r->handler, "qos-headerfilter") != 0) {
return DECLINED;
}
ap_set_content_type(r, "text/plain");
ap_rprintf(r, "\nQS_RequestHeaderFilter rules:\n\n");
entry = (apr_table_entry_t *)apr_table_elts(sconf->hfilter_table)->elts;
for(i = 0; i < apr_table_elts(sconf->hfilter_table)->nelts; i++) {
qos_fhlt_r_t *he = (qos_fhlt_r_t *)entry[i].val;
ap_rprintf(r, " name=%s, action=%s, size=%d, pattern=%s\n",
entry[i].key,
he->action == QS_FLT_ACTION_DROP ? "drop" : "deny",
he->size, he->text);
}
ap_rprintf(r, "\nQS_ResponseHeaderFilter rules:\n\n");
entry = (apr_table_entry_t *)apr_table_elts(sconf->reshfilter_table)->elts;
for(i = 0; i < apr_table_elts(sconf->reshfilter_table)->nelts; i++) {
qos_fhlt_r_t *he = (qos_fhlt_r_t *)entry[i].val;
ap_rprintf(r, " name=%s, action=%s, size=%d, pattern=%s\n",
entry[i].key,
he->action == QS_FLT_ACTION_DROP ? "drop" : "deny",
he->size, he->text);
}
ap_rprintf(r, "\nmod_qos %s\n", g_revision);
return OK;
}
static int qos_handler_man1(request_rec * r) {
module *modp = NULL;
if(strcmp(r->handler, "qos-man1") != 0) {
return DECLINED;
}
ap_set_content_type(r, "text/plain");
for(modp = ap_top_module; modp; modp = modp->next) {
if(strcmp(modp->name, "mod_qos.c") == 0) {
const command_rec *cmd = modp->cmds;
char time_string[64];
time_t tm = time(NULL);
struct tm *ptr = localtime(&tm);
strftime(time_string, sizeof(time_string), "%B %Y", ptr);
ap_rprintf(r, ".TH MOD_QOS 1 \"%s\" \"mod_qos Apache Module\" \"mod_qos\"\n", time_string);
ap_rprintf(r, ".SH NAME\n");
ap_rprintf(r, "mod_qos - quality of service module for the Apache Web server\n");
ap_rprintf(r, ".SH DESCRIPTION\n");
ap_rprintf(r, "mod_qos is a quality of service module for the Apache web server implementing control mechanisms that can provide different levels of priority to different HTTP requests.\n");
ap_rprintf(r, ".SH OPTIONS\n");
while(cmd) {
if(cmd->name) {
if(cmd->errmsg && cmd->errmsg[0] &&
((strstr(cmd->errmsg, "QS_") != NULL) || (strstr(cmd->errmsg, "QSLog") != NULL))) {
ap_rprintf(r, ".TP\n");
ap_rprintf(r, "%s\n", cmd->errmsg);
}
cmd++;
} else {
break;
}
}
ap_rprintf(r, ".SH AUTHOR\n");
ap_rprintf(r, "Pascal Buchbinder, http://mod-qos.sourceforge.net/\n");
}
}
return OK;
}
#endif
static int qos_handler_console(request_rec * r) {
apr_table_t *qt;
const char *ip;
const char *cmd;
const char *event;
apr_uint64_t addr[2];
qos_srv_config *sconf;
int status = HTTP_NOT_ACCEPTABLE;;
if (strcmp(r->handler, "qos-console") != 0) {
return DECLINED;
}
sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config, &qos_module);
if(sconf->disable_handler == 1) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(072)"handler has been disabled for this host, id=%s",
qos_unique_id(r, "072"));
return DECLINED;
}
apr_table_add(r->err_headers_out, "Cache-Control", "no-cache");
qt = qos_get_query_table(r);
ip = apr_table_get(qt, "address");
cmd = apr_table_get(qt, "action");
event = apr_table_get(qt, "event");
if(event == NULL) {
event = apr_pstrdup(r->pool, QS_LIMIT_DEFAULT);
}
if(!cmd || !ip) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(070)"console, not acceptable,"
" missing request query (action/address), id=%s",
qos_unique_id(r, "070"));
return HTTP_NOT_ACCEPTABLE;
}
if(ip) {
int escerr = 0;
char *ta = apr_pstrdup(r->pool, ip);
qos_unescaping(ta, QOS_DEC_MODE_FLAGS_URL, &escerr);
ip = ta;
}
if(!sconf->has_qos_cc) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(070)"console, not acceptable,"
" client data store has not been enabled, id=%s",
qos_unique_id(r, "070"));
return HTTP_NOT_ACCEPTABLE;
}
if((strcasecmp(cmd, "search") == 0) && (strcmp(ip, "*") == 0)) {
return qos_console_dump(r, event);
}
if(qos_ip_str2long(ip, addr) == 0) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(070)"console, not acceptable,"
" invalid ip/wrong format, id=%s",
qos_unique_id(r, "070"));
return HTTP_NOT_ACCEPTABLE;
}
if(sconf->has_qos_cc) {
char *msg = "not available";
qos_user_t *u = qos_get_user_conf(sconf->act->ppool);
qos_s_entry_t **clientEntry = NULL;
qos_s_entry_t searchE;
int limitTableIndex = 0;
qos_s_entry_limit_conf_t *eventLimitConf = NULL;
int limit = 0;
time_t limitTime = 0;
apr_time_t now = apr_time_sec(r->request_time);
apr_global_mutex_lock(u->qos_cc->lock); /* @CRT34 */
searchE.ip6[0] = addr[0];
searchE.ip6[1] = addr[1];
clientEntry = qos_cc_get0(u->qos_cc, &searchE, apr_time_sec(r->request_time));
if(!clientEntry) {
if(strcasecmp(cmd, "search") != 0) {
clientEntry = qos_cc_set(u->qos_cc, &searchE, time(NULL));
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, r,
QOS_LOG_PFX(071)"console, add new client ip entry '%s', is=%s",
ip, qos_unique_id(r, "071"));
}
}
status = OK;
if(u->qos_cc->limitTable) {
eventLimitConf = qos_getQSLimitEvent(u, event, &limitTableIndex);
}
if(strcasecmp(cmd, "setvip") == 0) {
(*clientEntry)->vip = 1;
} else if(strcasecmp(cmd, "unsetvip") == 0) {
(*clientEntry)->vip = 0;
} else if(strcasecmp(cmd, "setlowprio") == 0) {
(*clientEntry)->lowrate = time(NULL);
(*clientEntry)->lowratestatus = 0xff;
(*clientEntry)->lowratestatus &= ~QOS_LOW_FLAG_BEHAVIOR_OK;
} else if(strcasecmp(cmd, "unsetlowprio") == 0) {
(*clientEntry)->lowrate = 0;
if((*clientEntry)->lowratestatus & QOS_LOW_FLAG_BEHAVIOR_OK) {
(*clientEntry)->lowratestatus = QOS_LOW_FLAG_BEHAVIOR_OK;
} else {
(*clientEntry)->lowratestatus = 0;
}
} else if(strcasecmp(cmd, "unblock") == 0) {
(*clientEntry)->blockTime = 0;
(*clientEntry)->block = 0;
} else if(strcasecmp(cmd, "block") == 0) {
(*clientEntry)->blockTime = time(NULL);
(*clientEntry)->block = sconf->qos_cc_block + 1000;
} else if(strcasecmp(cmd, "unlimit") == 0) {
if(eventLimitConf) {
(*clientEntry)->limit[limitTableIndex].limitTime = 0;
(*clientEntry)->limit[limitTableIndex].limit = 0;
}
} else if(strcasecmp(cmd, "limit") == 0) {
if(eventLimitConf) {
(*clientEntry)->limit[limitTableIndex].limitTime = time(NULL);
(*clientEntry)->limit[limitTableIndex].limit = eventLimitConf->limit + 1000;
}
} else if(strcasecmp(cmd, "inclimit") == 0) {
if(eventLimitConf) {
if(((*clientEntry)->limit[limitTableIndex].limitTime + eventLimitConf->limitTime) < now) {
// expired
(*clientEntry)->limit[limitTableIndex].limit = 0;
(*clientEntry)->limit[limitTableIndex].limitTime = 0;
}
// increment limit event
if((*clientEntry)->limit[limitTableIndex].limit < USHRT_MAX) {
(*clientEntry)->limit[limitTableIndex].limit++;
}
if((*clientEntry)->limit[limitTableIndex].limit == 1) {
// first, start timer
(*clientEntry)->limit[limitTableIndex].limitTime = now;
}
}
} else if(strcasecmp(cmd, "search") == 0) {
/* nothing to do here */
} else {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(070)"console, not acceptable, unknown action '%s', id=%s",
cmd, qos_unique_id(r, "070"));
status = HTTP_NOT_ACCEPTABLE;
}
if(clientEntry) {
if(eventLimitConf) {
limit = (*clientEntry)->limit[limitTableIndex].limit;
limitTime = (eventLimitConf->limitTime >= (time(NULL) - (*clientEntry)->limit[limitTableIndex].limitTime)) ?
(eventLimitConf->limitTime - (time(NULL) - (*clientEntry)->limit[limitTableIndex].limitTime)) : 0;
}
msg = apr_psprintf(r->pool, "%s vip=%s lowprio=%s block=%hu/%ld limit=%d/%ld", ip,
(*clientEntry)->vip ? "yes" : "no",
((*clientEntry)->lowrate + QOS_LOW_TIMEOUT) > now ? "yes" : "no",
(*clientEntry)->block,
(sconf->qos_cc_blockTime >= (time(NULL) - (*clientEntry)->blockTime)) ?
(sconf->qos_cc_blockTime - (time(NULL) - (*clientEntry)->blockTime)) : 0,
limit,
limitTime);
}
apr_global_mutex_unlock(u->qos_cc->lock); /* @CRT34 */
if(status == OK) {
ap_set_content_type(r, "text/plain");
ap_rprintf(r, "%s\n", msg);
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, r,
QOS_LOG_PFX(071)"console, action '%s' applied to client ip entry '%s', id=%s",
cmd, ip, qos_unique_id(r, "071"));
}
} else {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(070)"console, not acceptable,"
" qos client control has not been activated, id=%s",
qos_unique_id(r, "070"));
status = HTTP_NOT_ACCEPTABLE;
}
return status;
}
/**
* viewer which may be used as an alternative to mod_status
*/
static int qos_handler_view(request_rec * r) {
qos_srv_config *sconf;
apr_table_t *qt;
if (strcmp(r->handler, "qos-viewer") != 0) {
return DECLINED;
}
sconf = (qos_srv_config*)ap_get_module_config(r->server->module_config, &qos_module);
if(sconf->disable_handler == 1) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
QOS_LOG_PFX(072)"handler has been disabled for this host, id=%s",
qos_unique_id(r, "072"));
return DECLINED;
}
if(strstr(r->parsed_uri.path, "favicon.ico") != NULL) {
apr_table_add(r->err_headers_out, "Cache-Control", "public, max-age=2592000");
return qos_favicon(r);
}
apr_table_add(r->err_headers_out, "Cache-Control", "no-cache");
qt = qos_get_query_table(r);
if(qt && (apr_table_get(qt, "refresh") != NULL)) {
apr_table_add(r->err_headers_out, "Refresh", "10");
}
if(qt && (apr_table_get(qt, "auto") != NULL)) {
ap_set_content_type(r, "text/plain");
qos_ext_status_short(r, qt);
return OK;
}
ap_set_content_type(r, "text/html");
if(!r->header_only) {
int hasSlash = 1;
if(strlen(r->parsed_uri.path) > 0) {
if(r->parsed_uri.path[strlen(r->parsed_uri.path)-1] != '/') {
hasSlash = 0;
}
}
ap_rputs("<html><head><title>mod_qos</title>\n", r);
ap_rprintf(r,"<link rel=\"shortcut icon\" href=\"%s%sfavicon.ico\"/>\n",
ap_escape_html(r->pool, r->parsed_uri.path),
hasSlash ? "" : "/");
ap_rputs("<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\">\n", r);
ap_rputs("<meta name=\"author\" content=\"Pascal Buchbinder\">\n", r);
ap_rputs("<meta http-equiv=\"Pragma\" content=\"no-cache\">\n", r);
ap_rputs("<style TYPE=\"text/css\">\n", r);
ap_rputs("<!--", r);
ap_rputs(" body {\n\
background-color: rgb(248,250,246);\n\
color: black;\n\
font-family: arial, helvetica, verdana, sans-serif;\n\
}\n\
.btable{\n\
background-color: white;\n\
border: 1px solid; padding: 0px;\n\
margin: 6px; width: 920px;\n\
font-weight: normal;\n\
border-collapse: collapse;\n\
}\n\
.rowts {\n\
background-color: rgb(150,165,158);\n\
vertical-align: top;\n\
border: 1px solid;\n\
border-color: black;\n\
font-weight: normal;\n\
padding: 0px;\n\
margin: 0px;\n\
}\n\
.rowt {\n\
background-color: rgb(210,220,215);\n\
vertical-align: top;\n\
border: 1px solid;\n\
border-color: black;\n\
font-weight: normal;\n\
padding: 0px;\n\
margin: 0px;\n\
}\n\
.rows {\n\
background-color: rgb(228,235,230);\n\
vertical-align: top;\n\
border: 1px solid;\n\
border-color: black;\n\
font-weight: normal;\n\
padding: 0px;\n\
margin: 0px;\n\
}\n\
.row {\n\
background-color: white;\n\
vertical-align: top;\n\
border: 1px solid;\n\
border-color: black;\n\
font-weight: normal;\n\
padding: 0px;\n\
margin: 0px;\n\
}\n\
.rowe {\n\
background-color: rgb(186,200,190);\n\
vertical-align: top;\n\
border: 1px solid;\n\
border-color: black;\n\
font-weight: normal;\n\
padding: 0px;\n\
margin: 0px;\n\
}\n\
.small {\n\
font-size: 0.75em;\n\
font-family: courier;\n\
}\n\
.prog-border {\n\
height: 10px;\n\
width: 150px;\n\
background: #eee;\n\
border: 1px solid #000;\n\
padding: 2px;\n\
font-family: arial, helvetica, verdana, sans-serif; font-size: 10px; color: #000;\n\
}\n\
.prog-bar {\n\
height: 10px;\n\
padding: 0;\n\
background: #339900;\n\
font-family: arial, helvetica, verdana, sans-serif; font-size: 10px; color: #000;\n\
}\n\
.prog-bar-limit {\n\
height: 10px;\n\
padding: 0;\n\
background: #993300;\n\
font-family: arial, helvetica, verdana, sans-serif; font-size: 10px; color: #000;\n\
}\n\
form { display: inline; }\n", r);
ap_rputs("-->\n", r);
ap_rputs("</style>\n", r);
ap_rputs("</head><body>\n", r);
qos_ext_status_hook(r, 0);
{
apr_time_t nowtime = apr_time_now();
ap_rvputs(r, "<div class=\"small\">",
ap_ht_time(r->pool, nowtime, QS_ERR_TIME_FORMAT, 0), NULL);
ap_rprintf(r, ", mod_qos %s\n", ap_escape_html(r->pool, qos_revision(r->pool)));
}
ap_rputs("</body></html>", r);
}
return OK;
}
static int qos_handler(request_rec * r) {
int status = qos_handler_view(r);
if(status != DECLINED) {
return status;
}
status = qos_handler_console(r);
if(status != DECLINED) {
return status;
}
#ifdef QS_INTERNAL_TEST
status = qos_handler_man1(r);
if(status != DECLINED) {
return status;
}
status = qos_handler_headerfilter(r);
if(status != DECLINED) {
return status;
}
#endif
return DECLINED;
}
/**
* insert response filter
*/
static void qos_insert_filter(request_rec *r) {
ap_add_output_filter("qos-out-filter", NULL, r, r->connection);
}
static void qos_insert_err_filter(request_rec *r) {
ap_add_output_filter("qos-out-err-filter", NULL, r, r->connection);
}
/************************************************************************
* directiv handlers
***********************************************************************/
static void qos_table_merge(apr_table_t *o, apr_table_t *b) {
int i;
apr_table_entry_t *entry = (apr_table_entry_t *)apr_table_elts(b)->elts;
for(i = 0; i < apr_table_elts(b)->nelts; ++i) {
if(apr_table_get(o, entry[i].key) == NULL) {
// copy the pointer only!!!
apr_table_setn(o, entry[i].key, entry[i].val);
}
}
}
static void *qos_dir_config_create(apr_pool_t *p, char *d) {
qos_dir_config *dconf = apr_pcalloc(p, sizeof(qos_dir_config));
dconf->path = d;
dconf->rfilter_table = apr_table_make(p, 1);
dconf->inheritoff = 0;
dconf->headerfilter = QS_HEADERFILTER_OFF_DEFAULT;
dconf->resheaderfilter = QS_HEADERFILTER_OFF_DEFAULT;
dconf->bodyfilter_p = -1;
dconf->bodyfilter_d = -1;
dconf->dec_mode = QOS_DEC_MODE_FLAGS_URL;
dconf->maxpost = -1;
dconf->urldecoding = QS_OFF_DEFAULT;
dconf->response_pattern = NULL;
dconf->response_pattern_var = NULL;
dconf->redirectif = apr_array_make(p, 20, sizeof(qos_redirectif_entry_t));
dconf->disable_reqrate_events = apr_table_make(p, 1);
dconf->setenvstatus_t = apr_table_make(p, 5);
dconf->setenvif_t = apr_array_make(p, 20, sizeof(qos_setenvif_t));
dconf->setenvifquery_t = apr_table_make(p, 1);
dconf->setenvcmp = apr_array_make(p, 2, sizeof(qos_cmp_entry_t));
return dconf;
}
/**
* merges dir config, inheritoff disables merge of rfilter_table.
*/
static void *qos_dir_config_merge(apr_pool_t *p, void *basev, void *addv) {
qos_dir_config *b = (qos_dir_config *)basev;
qos_dir_config *o = (qos_dir_config *)addv;
qos_dir_config *dconf = apr_pcalloc(p, sizeof(qos_dir_config));
dconf->path = o->path;
if(o->headerfilter != QS_HEADERFILTER_OFF_DEFAULT) {
dconf->headerfilter = o->headerfilter;
} else {
dconf->headerfilter = b->headerfilter;
}
if(o->resheaderfilter != QS_HEADERFILTER_OFF_DEFAULT) {
dconf->resheaderfilter = o->resheaderfilter;
} else {
dconf->resheaderfilter = b->resheaderfilter;
}
if(o->bodyfilter_p != -1) {
dconf->bodyfilter_p = o->bodyfilter_p;
} else {
dconf->bodyfilter_p = b->bodyfilter_p;
}
if(o->bodyfilter_d != -1) {
dconf->bodyfilter_d = o->bodyfilter_d;
} else {
dconf->bodyfilter_d = b->bodyfilter_d;
}
if((o->dec_mode != QOS_DEC_MODE_FLAGS_URL) ||
(o->inheritoff)) {
dconf->dec_mode = o->dec_mode;
} else {
dconf->dec_mode = b->dec_mode;
}
if(o->inheritoff) {
dconf->rfilter_table = o->rfilter_table;
} else {
dconf->rfilter_table = qos_table_merge_create(p, b->rfilter_table, o->rfilter_table);
}
if(o->maxpost != -1) {
dconf->maxpost = o->maxpost;
} else {
dconf->maxpost = b->maxpost;
}
if(o->urldecoding == QS_OFF_DEFAULT) {
dconf->urldecoding = b->urldecoding;
} else {
dconf->urldecoding = o->urldecoding;
}
if(o->response_pattern) {
dconf->response_pattern = o->response_pattern;
dconf->response_pattern_len = o->response_pattern_len;
dconf->response_pattern_var = o->response_pattern_var;
} else {
dconf->response_pattern = b->response_pattern;
dconf->response_pattern_len = b->response_pattern_len;
dconf->response_pattern_var = b->response_pattern_var;
}
dconf->disable_reqrate_events = qos_table_merge_create(p, b->disable_reqrate_events,
o->disable_reqrate_events);
dconf->redirectif = apr_array_append(p, b->redirectif, o->redirectif);
dconf->setenvstatus_t = apr_table_copy(p, b->setenvstatus_t);
qos_table_merge(dconf->setenvstatus_t, o->setenvstatus_t);
dconf->setenvif_t = apr_array_append(p, b->setenvif_t, o->setenvif_t);
dconf->setenvifquery_t = apr_table_copy(p, b->setenvifquery_t);
qos_table_merge(dconf->setenvifquery_t, o->setenvifquery_t);
dconf->setenvcmp = apr_array_append(p, b->setenvcmp, o->setenvcmp);
return dconf;
}
static void *qos_srv_config_create(apr_pool_t *p, server_rec *s) {
qos_srv_config *sconf;
apr_pool_t *act_pool;
apr_pool_create(&act_pool, NULL);
sconf =(qos_srv_config *)apr_pcalloc(p, sizeof(qos_srv_config));
sconf->pool = p;
sconf->location_t = apr_table_make(sconf->pool, 2);
sconf->setenvif_t = apr_array_make(sconf->pool, 20, sizeof(qos_setenvif_t));
sconf->setenv_t = apr_table_make(sconf->pool, 1);
sconf->setreqheader_t = apr_table_make(sconf->pool, 5);
sconf->setreqheaderlate_t = apr_table_make(sconf->pool, 5);
sconf->unsetreqheader_t = apr_table_make(sconf->pool, 5);
sconf->unsetresheader_t = apr_table_make(sconf->pool, 5);
sconf->setenvifquery_t = apr_table_make(sconf->pool, 1);
sconf->setenvifparp_t = apr_table_make(sconf->pool, 1);
sconf->setenvifparpbody_t = apr_table_make(sconf->pool, 1);
sconf->setenvstatus_t = apr_table_make(sconf->pool, 5);
sconf->setenvresheader_t = apr_table_make(sconf->pool, 1);
sconf->setenvresheadermatch_t = apr_table_make(sconf->pool, 1);
sconf->setenvres_t = apr_table_make(sconf->pool, 1);
sconf->headerfilter = QS_HEADERFILTER_OFF_DEFAULT;
sconf->resheaderfilter = QS_HEADERFILTER_OFF_DEFAULT;
sconf->redirectif = apr_array_make(p, 20, sizeof(qos_redirectif_entry_t));
sconf->error_page = NULL;
sconf->req_rate = -1;
sconf->req_rate_start = 0;
sconf->min_rate = -1;
sconf->min_rate_max = -1;
sconf->min_rate_off = 0;
sconf->req_ignore_vip_rate = -1;
sconf->max_clients = 1024;
sconf->max_clients_conf = -1;
sconf->has_event_filter = 0;
sconf->has_event_limit = 0;
sconf->event_limit_a = apr_array_make(p, 2, sizeof(qos_event_limit_entry_t));
sconf->mfile = NULL;
sconf->act = (qs_actable_t *)apr_pcalloc(act_pool, sizeof(qs_actable_t));
sconf->act->pool = act_pool;
sconf->act->ppool = s->process->pool;
sconf->act->child_init = 0;
sconf->act->timeout = apr_time_sec(s->timeout);
sconf->act->has_events = 0;
sconf->act->lock_file = NULL;
sconf->act->lock = NULL;
sconf->is_virtual = s->is_virtual;
sconf->cookie_name = apr_pstrdup(sconf->pool, QOS_COOKIE_NAME);
sconf->cookie_path = apr_pstrdup(sconf->pool, "/");
sconf->user_tracking_cookie = NULL;
sconf->user_tracking_cookie_force = NULL;
sconf->user_tracking_cookie_session = -1;
sconf->user_tracking_cookie_jsredirect = -1;
sconf->user_tracking_cookie_domain = NULL;
sconf->max_age = atoi(QOS_MAX_AGE);
sconf->header_name = NULL;
sconf->header_name_drop = 0;
sconf->header_name_regex = NULL;
sconf->ip_header_name = NULL;
sconf->ip_header_name_drop = 0;
sconf->ip_header_name_regex = NULL;
sconf->vip_user = 0;
sconf->vip_ip_user = 0;
sconf->has_conn_counter = 0;
sconf->max_conn = -1;
sconf->max_conn_close = -1;
sconf->max_conn_per_ip = -1;
sconf->max_conn_per_ip_connections = -1;
sconf->max_conn_per_ip_ignore_vip = -1;
sconf->serialize = -1;
sconf->exclude_ip = apr_table_make(sconf->pool, 2);
sconf->hfilter_table = apr_table_make(p, 5);
sconf->reshfilter_table = apr_table_make(p, 5);
sconf->disable_reqrate_events = apr_table_make(p, 1);
sconf->log_only = 0;
sconf->log_env = -1;
sconf->has_qos_cc = 0;
sconf->cc_exclude_ip = apr_table_make(sconf->pool, 2);
sconf->qos_cc_size = 50000;
sconf->qos_cc_prefer = 0;
sconf->qos_cc_prefer_limit = 0;
sconf->qos_cc_event = 0;
sconf->qos_cc_event_req = -1;
sconf->qos_cc_block = 0;
sconf->qos_cc_serialize = 0;
sconf->serializeTMO = 6000; // 6000 * 50ms = 5 minutes
sconf->cc_tolerance = atoi(QOS_CC_BEHAVIOR_TOLERANCE_STR);
sconf->qs_req_rate_tm = QS_REQ_RATE_TM;
sconf->geodb = NULL;
sconf->geo_limit = -1;
sconf->geo_priv = apr_table_make(p, 20);
sconf->geo_excludeUnknown = -1;
sconf->qslog_p = NULL;
sconf->qsstatus = 0;
sconf->qsevents = 0;
sconf->qslog_str = NULL;
sconf->ip_type = QS_IP_V6_DEFAULT;
sconf->qos_cc_blockTime = 600;
sconf->qos_cc_limitTable = apr_table_make(p, 5);
sconf->qos_cc_forwardedfor = NULL;
sconf->disable_handler = -1;
sconf->maxpost = -1;
sconf->milestones = NULL;
sconf->milestoneTimeout = QOS_MILESTONE_TIMEOUT;
sconf->static_on = -1;
sconf->static_html = 0;
sconf->static_cssjs = 0;
sconf->static_img = 0;
sconf->static_other = 0;
sconf->static_notmodified = 0;
if(!s->is_virtual) {
char *msg = qos_load_headerfilter(p, sconf->hfilter_table, qs_header_rules);
if(msg) {
ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
QOS_LOG_PFX(006)"could not compile request header filter rules: %s", msg);
exit(1);
}
msg = qos_load_headerfilter(p, sconf->reshfilter_table, qs_res_header_rules);
if(msg) {
ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s,
QOS_LOG_PFX(006)"could not compile response header filter rules: %s", msg);
exit(1);
}
}
{
int len = EVP_MAX_KEY_LENGTH;
unsigned char *rand = apr_pcalloc(p, len);
#if APR_HAS_RANDOM
if(apr_generate_random_bytes(rand, len) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
QOS_LOG_PFX(083)"Can't generate random data.");
}
#else
if(!RAND_bytes(rand, len)) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
QOS_LOG_PFX(083)"Can't generate random data.");
}
#endif
EVP_BytesToKey(EVP_des_ede3_cbc(), EVP_sha1(), NULL, rand, len, 1, sconf->key, NULL);
sconf->rawKey = rand;
sconf->rawKeyLen = len;
sconf->keyset = 0;
}
#ifdef QS_INTERNAL_TEST
{
int i;
sconf->testip = apr_table_make(sconf->pool, m_qs_sim_ip_len);
sconf->enable_testip = 1;
for(i = 0; i < (m_qs_sim_ip_len*3/4); i++) {
char *qsmi = apr_psprintf(p, "%d.%d.%d.%d", rand()%255, rand()%255, rand()%255, rand()%255);
apr_table_add(sconf->testip, apr_psprintf(p, "%d", i), qsmi);
}
for(i = m_qs_sim_ip_len*3/4; i < m_qs_sim_ip_len; i++) {
char *qsmi = apr_psprintf(p, "fe%d::%d:%d:%d", rand()%100, rand()%4000, rand()%4000, rand()%400);
apr_table_add(sconf->testip, apr_psprintf(p, "%d", i), qsmi);
}
}
#endif
return sconf;
}
/**
* "merges" server configuration: virtual host overwrites global settings (if
* any rule has been specified)
* but: global settings such as header filter table and connection timeouts
* are always used from the base server
*/
static void *qos_srv_config_merge(apr_pool_t *p, void *basev, void *addv) {
qos_srv_config *b = (qos_srv_config *)basev;
qos_srv_config *o = (qos_srv_config *)addv;
/* GLOBAL ONLY directives: */
o->hfilter_table = b->hfilter_table;
o->reshfilter_table = b->reshfilter_table;
o->log_only = b->log_only;
if(o->log_env == -1) {
o->log_env = b->log_env;
}
o->has_qos_cc = b->has_qos_cc;
o->cc_exclude_ip = b->cc_exclude_ip;
o->qos_cc_size = b->qos_cc_size;
o->qos_cc_prefer = b->qos_cc_prefer;
o->qos_cc_prefer_limit = b->qos_cc_prefer_limit;
o->qos_cc_event = b->qos_cc_event;
o->qos_cc_event_req = b->qos_cc_event_req;
o->qos_cc_block = b->qos_cc_block;
o->qos_cc_blockTime = b->qos_cc_blockTime;
o->qos_cc_limitTable = b->qos_cc_limitTable;
o->qos_cc_forwardedfor = b->qos_cc_forwardedfor;
o->qos_cc_serialize = b->qos_cc_serialize;
o->cc_tolerance = b->cc_tolerance;
o->qs_req_rate_tm = b->qs_req_rate_tm;
o->geodb = b->geodb;
o->geo_limit = b->geo_limit;
o->geo_priv = b->geo_priv;
o->geo_excludeUnknown = b->geo_excludeUnknown;
o->qslog_p = b->qslog_p;
o->qsstatus = b->qsstatus;
o->qsevents = b->qsevents;
o->qslog_str = b->qslog_str;
o->ip_type = b->ip_type;
o->req_rate = b->req_rate;
o->req_rate_start = b->req_rate_start;
o->min_rate = b->min_rate;
o->min_rate_max = b->min_rate_max;
o->req_ignore_vip_rate = b->req_ignore_vip_rate;
o->event_limit_a = apr_array_append(p, b->event_limit_a, o->event_limit_a);
/* end GLOBAL ONLY directives */
if(o->disable_handler == -1) {
o->disable_handler = b->disable_handler;
}
#ifdef QS_INTERNAL_TEST
o->enable_testip = b->enable_testip;
#endif
if(o->error_page == NULL) {
o->error_page = b->error_page;
}
qos_table_merge(o->location_t, b->location_t);
o->setenvif_t = apr_array_append(p, b->setenvif_t, o->setenvif_t);
qos_table_merge(o->setenv_t, b->setenv_t);
qos_table_merge(o->setreqheader_t, b->setreqheader_t);
qos_table_merge(o->setreqheaderlate_t, b->setreqheaderlate_t);
qos_table_merge(o->unsetreqheader_t, b->unsetreqheader_t);
qos_table_merge(o->unsetresheader_t, b->unsetresheader_t);
qos_table_merge(o->setenvifquery_t, b->setenvifquery_t);
qos_table_merge(o->setenvifparp_t, b->setenvifparp_t);
qos_table_merge(o->setenvifparpbody_t, b->setenvifparpbody_t);
qos_table_merge(o->setenvstatus_t, b->setenvstatus_t);
qos_table_merge(o->setenvresheader_t, b->setenvresheader_t);
qos_table_merge(o->setenvresheadermatch_t, b->setenvresheadermatch_t);
qos_table_merge(o->setenvres_t, b->setenvres_t);
qos_table_merge(o->exclude_ip, b->exclude_ip);
o->disable_reqrate_events = qos_table_merge_create(p, b->disable_reqrate_events,
o->disable_reqrate_events);
if(o->headerfilter == QS_HEADERFILTER_OFF_DEFAULT) {
o->headerfilter = b->headerfilter;
}
if(o->resheaderfilter == QS_HEADERFILTER_OFF_DEFAULT) {
o->resheaderfilter = b->resheaderfilter;
}
o->redirectif = apr_array_append(p, b->redirectif, o->redirectif);
if(o->mfile == NULL) {
o->mfile = b->mfile;
}
if(strcmp(o->cookie_name, QOS_COOKIE_NAME) == 0) {
o->cookie_name = b->cookie_name;
}
if(strcmp(o->cookie_path, "/") == 0) {
o->cookie_path = b->cookie_path;
}
if(o->max_age == atoi(QOS_MAX_AGE)) {
o->max_age = b->max_age;
}
if(o->user_tracking_cookie == NULL) {
o->user_tracking_cookie = b->user_tracking_cookie;
o->user_tracking_cookie_force = b->user_tracking_cookie_force;
o->user_tracking_cookie_session = b->user_tracking_cookie_session;
o->user_tracking_cookie_jsredirect = b->user_tracking_cookie_jsredirect;
o->user_tracking_cookie_domain = b->user_tracking_cookie_domain;
}
if(o->keyset == 0) {
memcpy(o->key, b->key, sizeof(o->key));
o->rawKey = b->rawKey;
o->rawKeyLen = b->rawKeyLen;
}
if(o->header_name == NULL) {
o->header_name = b->header_name;
o->header_name_drop = b->header_name_drop;
o->header_name_regex = b->header_name_regex;
}
if(o->ip_header_name == NULL) {
o->ip_header_name = b->ip_header_name;
o->ip_header_name_drop = b->ip_header_name_drop;
o->ip_header_name_regex = b->ip_header_name_regex;
}
if(o->vip_user == 0) {
o->vip_user = b->vip_user;
}
if(o->vip_ip_user == 0) {
o->vip_ip_user = b->vip_ip_user;
}
if(o->max_conn == -1) {
o->max_conn = b->max_conn;
}
if(o->max_conn_close == -1) {
o->max_conn_close = b->max_conn_close;
o->max_conn_close_percent = b->max_conn_close_percent;
}
if(o->max_conn_per_ip == -1) {
o->max_conn_per_ip = b->max_conn_per_ip;
}
if(o->max_conn_per_ip_ignore_vip == -1) {
o->max_conn_per_ip_ignore_vip = b->max_conn_per_ip_ignore_vip;
}
if(o->max_conn_per_ip_connections == -1) {
o->max_conn_per_ip_connections = b->max_conn_per_ip_connections;
}
if(o->serialize == -1) {
o->serialize = b->serialize;
o->serializeTMO = b->serializeTMO;
}
if(o->has_event_filter == 0) {
o->has_event_filter = b->has_event_filter;
}
if(o->has_event_limit == 0) {
o->has_event_limit = b->has_event_limit;
}
if(o->maxpost == -1) {
o->maxpost = b->maxpost;
}
if(o->milestones == NULL) {
o->milestones = b->milestones;
o->milestoneTimeout = b->milestoneTimeout;
}
if(o->static_on == -1) {
/* use base settings if not configured per vhost */
o->static_on = b->static_on;
o->static_html = b->static_html;
o->static_cssjs = b->static_cssjs;
o->static_img = b->static_img;
o->static_other = b->static_other;
o->static_notmodified = b->static_notmodified;
}
return o;
}
const char *qos_logonly_cmd(cmd_parms *cmd, void *dcfg, int flag) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->log_only = flag;
return NULL;
}
const char *qos_logenv_cmd(cmd_parms *cmd, void *dcfg, int flag) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->log_env = flag;
return NULL;
}
/**
* QS_MaxClients
*/
const char *qos_maxclients_cmd(cmd_parms *cmd, void *dcfg, const char *arg1) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->max_clients_conf = atoi(arg1);
if(sconf->max_clients_conf <= 0) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >=0",
cmd->directive->directive);
}
return NULL;
}
const char *qos_mfile_cmd(cmd_parms *cmd, void *dcfg, const char *path) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
apr_finfo_t finfo;
apr_status_t rc;
if(!path[0]) {
return apr_psprintf(cmd->pool, "%s: invalid path",
cmd->directive->directive);
}
if((rc = apr_stat(&finfo, path, APR_FINFO_TYPE, cmd->pool)) != APR_SUCCESS) {
char *p = apr_pstrdup(cmd->pool, path);
/* file? */
if(p[strlen(p)-1] == '/') {
return apr_psprintf(cmd->pool, "%s: path does not exist",
cmd->directive->directive);
} else {
char *e = strrchr(p, '/');
if(e) {
e[0] = '\0';
}
if(((rc = apr_stat(&finfo, p, APR_FINFO_TYPE, cmd->pool)) != APR_SUCCESS) ||
(finfo.filetype != APR_DIR)){
return apr_psprintf(cmd->pool, "%s: path does not exist",
cmd->directive->directive);
}
}
}
sconf->mfile = apr_pstrdup(cmd->pool, path);
return NULL;
}
/**
* command to define the concurrent request limitation for a location
*/
const char *qos_loc_con_cmd(cmd_parms *cmd, void *dcfg, const char *loc, const char *limit) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qs_rule_ctx_t *rule = (qs_rule_ctx_t *)apr_table_get(sconf->location_t, loc);
if(rule == NULL) {
rule = (qs_rule_ctx_t *)apr_pcalloc(cmd->pool, sizeof(qs_rule_ctx_t));
rule->url = apr_pstrdup(cmd->pool, loc);
}
rule->limit = atoi(limit);
if((rule->limit < 0) || ((rule->limit == 0) && limit && (strcmp(limit, "0") != 0))) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >=0",
cmd->directive->directive);
}
rule->event = NULL;
rule->regex = NULL;
rule->condition = NULL;
apr_table_setn(sconf->location_t, apr_pstrdup(cmd->pool, loc), (char *)rule);
return NULL;
}
/**
* QS_LocRequestPerSecLimit: command to define the req/sec limitation for a location
*/
const char *qos_loc_rs_cmd(cmd_parms *cmd, void *dcfg, const char *loc, const char *limit) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qs_rule_ctx_t *rule = (qs_rule_ctx_t *)apr_table_get(sconf->location_t, loc);
if(rule == NULL) {
rule = (qs_rule_ctx_t *)apr_pcalloc(cmd->pool, sizeof(qs_rule_ctx_t));
rule->url = apr_pstrdup(cmd->pool, loc);
}
rule->req_per_sec_limit = atol(limit);
if(rule->req_per_sec_limit == 0) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >0",
cmd->directive->directive);
}
rule->event = NULL;
rule->regex = NULL;
rule->condition = NULL;
apr_table_setn(sconf->location_t, apr_pstrdup(cmd->pool, loc), (char *)rule);
return NULL;
}
/**
* QS_LocKBytesPerSecLimit: command to define the kbytes/sec limitation for a location
*/
const char *qos_loc_bs_cmd(cmd_parms *cmd, void *dcfg, const char *loc, const char *limit) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qs_rule_ctx_t *rule = (qs_rule_ctx_t *)apr_table_get(sconf->location_t, loc);
if(rule == NULL) {
rule = (qs_rule_ctx_t *)apr_pcalloc(cmd->pool, sizeof(qs_rule_ctx_t));
rule->url = apr_pstrdup(cmd->pool, loc);
}
rule->kbytes_per_sec_limit = atol(limit);
if(rule->kbytes_per_sec_limit == 0) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >0",
cmd->directive->directive);
}
rule->event = NULL;
rule->regex = NULL;
rule->condition = NULL;
apr_table_setn(sconf->location_t, apr_pstrdup(cmd->pool, loc), (char *)rule);
return NULL;
}
/**
* QS_LocRequestLimitMatch: defines the maximum of concurrent requests matching the specified
* request line pattern
*/
const char *qos_match_con_cmd(cmd_parms *cmd, void *dcfg, const char *match, const char *limit) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qs_rule_ctx_t *rule = (qs_rule_ctx_t *)apr_table_get(sconf->location_t, match);
if(rule == NULL) {
rule = (qs_rule_ctx_t *)apr_pcalloc(cmd->pool, sizeof(qs_rule_ctx_t));
rule->url = apr_pstrdup(cmd->pool, match);
}
rule->limit = atoi(limit);
if((rule->limit < 0) || ((rule->limit == 0) && limit && (strcmp(limit, "0") != 0))) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >=0",
cmd->directive->directive);
}
rule->regex = ap_pregcomp(cmd->pool, match, AP_REG_EXTENDED);
if(rule->regex == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regular expression (%s)",
cmd->directive->directive, match);
}
rule->event = NULL;
rule->condition = NULL;
apr_table_setn(sconf->location_t, apr_pstrdup(cmd->pool, match), (char *)rule);
return NULL;
}
/**
* QS_CondLocRequestLimitMatch: defines the maximum of concurrent requests
* matching the specified request line pattern
*/
const char *qos_cond_match_con_cmd(cmd_parms *cmd, void *dcfg, const char *match,
const char *limit, const char *pattern) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qs_rule_ctx_t *rule = (qs_rule_ctx_t *)apr_pcalloc(cmd->pool, sizeof(qs_rule_ctx_t));
rule->url = apr_pstrdup(cmd->pool, match);
rule->limit = atoi(limit);
if((rule->limit < 0) || ((rule->limit == 0) && limit && (strcmp(limit, "0") != 0))) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >=0",
cmd->directive->directive);
}
rule->regex = ap_pregcomp(cmd->pool, match, AP_REG_EXTENDED);
rule->condition = ap_pregcomp(cmd->pool, pattern, AP_REG_EXTENDED);
if(rule->regex == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regular expression (%s)",
cmd->directive->directive, match);
}
if(rule->condition == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regular expression (%s)",
cmd->directive->directive, pattern);
}
rule->event = NULL;
apr_table_setn(sconf->location_t, apr_pstrcat(cmd->pool, match, "##conditional##", NULL), (char *)rule);
return NULL;
}
/**
* QS_LocRequestPerSecLimitMatch: defines the maximum requests/sec for
* the matching request line pattern
*/
const char *qos_match_rs_cmd(cmd_parms *cmd, void *dcfg, const char *match, const char *limit) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qs_rule_ctx_t *rule = (qs_rule_ctx_t *)apr_table_get(sconf->location_t, match);
if(rule == NULL) {
rule = (qs_rule_ctx_t *)apr_pcalloc(cmd->pool, sizeof(qs_rule_ctx_t));
rule->url = apr_pstrdup(cmd->pool, match);
}
rule->req_per_sec_limit = atol(limit);
if(rule->req_per_sec_limit == 0) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >0",
cmd->directive->directive);
}
rule->regex = ap_pregcomp(cmd->pool, match, AP_REG_EXTENDED);
if(rule->regex == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regular expression (%s)",
cmd->directive->directive, match);
}
rule->event = NULL;
rule->condition = NULL;
apr_table_setn(sconf->location_t, apr_pstrdup(cmd->pool, match), (char *)rule);
return NULL;
}
/**
* QS_LocKBytesPerSecLimitMatch: defines the maximum kbytes/sec for
* the matching request line pattern
*/
const char *qos_match_bs_cmd(cmd_parms *cmd, void *dcfg, const char *match, const char *limit) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qs_rule_ctx_t *rule = (qs_rule_ctx_t *)apr_table_get(sconf->location_t, match);
if(rule == NULL) {
rule = (qs_rule_ctx_t *)apr_pcalloc(cmd->pool, sizeof(qs_rule_ctx_t));
rule->url = apr_pstrdup(cmd->pool, match);
}
rule->kbytes_per_sec_limit = atol(limit);
if(rule->kbytes_per_sec_limit == 0) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >0",
cmd->directive->directive);
}
rule->regex = ap_pregcomp(cmd->pool, match, AP_REG_EXTENDED);
if(rule->regex == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regular expression (%s)",
cmd->directive->directive, match);
}
rule->event = NULL;
rule->condition = NULL;
apr_table_setn(sconf->location_t, apr_pstrdup(cmd->pool, match), (char *)rule);
return NULL;
}
/**
* sets the default limitation of cuncurrent requests
*/
const char *qos_loc_con_def_cmd(cmd_parms *cmd, void *dcfg, const char *limit) {
return qos_loc_con_cmd(cmd, dcfg, "/", limit);
}
/**
* QS_EventRequestLimit: defines the number of concurrent events
*/
const char *qos_event_req_cmd(cmd_parms *cmd, void *dcfg, const char *event, const char *limit) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qs_rule_ctx_t *rule = (qs_rule_ctx_t *)apr_pcalloc(cmd->pool, sizeof(qs_rule_ctx_t));
char *p = strchr(event, '=');
rule->url = apr_pstrcat(cmd->pool, "var=(", event, ")", NULL);
rule->limit = atoi(limit);
rule->req_per_sec_limit = 0;
rule->req_per_sec_limit = 0;
if((rule->limit < 0) || ((rule->limit == 0) && limit && (strcmp(limit, "0") != 0))) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >=0",
cmd->directive->directive);
}
sconf->has_event_filter = 1;
if(p) {
p++;
rule->regex_var = ap_pregcomp(cmd->pool, p, AP_REG_EXTENDED);
if(rule->regex_var == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regex (%s)",
cmd->directive->directive, p);
}
rule->event = apr_pstrndup(cmd->pool, event, p - event - 1);
} else {
rule->regex_var = NULL;
rule->event = apr_pstrdup(cmd->pool, event);
}
rule->regex = NULL;
rule->condition = NULL;
apr_table_setn(sconf->location_t, rule->url, (char *)rule);
return NULL;
}
/**
* QS_EventPerSecLimit: defines the maximum requests/sec for the matching variable.
*/
const char *qos_event_rs_cmd(cmd_parms *cmd, void *dcfg, const char *event, const char *limit) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qs_rule_ctx_t *rule = (qs_rule_ctx_t *)apr_pcalloc(cmd->pool, sizeof(qs_rule_ctx_t));
rule->url = apr_pstrcat(cmd->pool, "var=[", event, "]", NULL);
rule->req_per_sec_limit = atol(limit);
rule->kbytes_per_sec_limit = 0;
if(rule->req_per_sec_limit == 0) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >0",
cmd->directive->directive);
}
sconf->has_event_limit = 1;
rule->event = apr_pstrdup(cmd->pool, event);
rule->regex = NULL;
rule->condition = NULL;
rule->limit = -1;
apr_table_setn(sconf->location_t, rule->url, (char *)rule);
return NULL;
}
/**
* QS_EventKBytesPerSecLimit: maximum download per event
*/
const char *qos_event_bps_cmd(cmd_parms *cmd, void *dcfg, const char *event, const char *limit) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qs_rule_ctx_t *rule = (qs_rule_ctx_t *)apr_pcalloc(cmd->pool, sizeof(qs_rule_ctx_t));
rule->url = apr_pstrcat(cmd->pool, "var={", event, "}", NULL);
rule->kbytes_per_sec_limit = atol(limit);
rule->req_per_sec_limit = 0;
if(rule->kbytes_per_sec_limit == 0) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >0",
cmd->directive->directive);
}
sconf->has_event_limit = 1;
rule->event = apr_pstrdup(cmd->pool, event);
rule->regex = NULL;
rule->condition = NULL;
rule->limit = -1;
apr_table_setn(sconf->location_t, rule->url, (char *)rule);
return NULL;
}
// QS_CondEventLimitCount
const char *qos_cond_event_limit_cmd(cmd_parms *cmd, void *dcfg, int argc, char *const argv[]) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qos_event_limit_entry_t *new = apr_array_push(sconf->event_limit_a);
if(argc < 4) {
return apr_psprintf(cmd->pool, "%s: takes 3 arguments",
cmd->directive->directive);
}
new->env_var = apr_pstrdup(cmd->pool, argv[0]);
new->eventDecStr = apr_pstrcat(cmd->pool, argv[0], QS_LIMIT_DEC, NULL);
new->max = atoi(argv[1]);
new->seconds = atoi(argv[2]);
new->action = QS_EVENT_ACTION_DENY;
if(new->max == 0) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >0",
cmd->directive->directive);
}
if(new->seconds == 0) {
return apr_psprintf(cmd->pool, "%s: seconds must be numeric value >0",
cmd->directive->directive);
}
new->condStr = apr_pstrdup(cmd->pool, argv[3]);
new->preg = ap_pregcomp(cmd->pool, new->condStr, AP_REG_EXTENDED);
if(new->preg == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regex (%s)",
cmd->directive->directive, new->condStr);
}
return NULL;
}
// QS_EventLimitCount
const char *qos_event_limit_cmd(cmd_parms *cmd, void *dcfg, const char *event,
const char *number, const char *seconds) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qos_event_limit_entry_t *new = apr_array_push(sconf->event_limit_a);
new->env_var = apr_pstrdup(cmd->pool, event);
new->max = atoi(number);
new->seconds = atoi(seconds);
new->action = QS_EVENT_ACTION_DENY;
new->condStr = NULL;
if(new->max == 0) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >0",
cmd->directive->directive);
}
if(new->seconds == 0) {
return apr_psprintf(cmd->pool, "%s: seconds must be numeric value >0",
cmd->directive->directive);
}
return NULL;
}
const char *qos_event_setenvifstatus_cmd(cmd_parms *cmd, void *dcfg, const char *rc, const char *var) {
apr_table_t *setenvstatus_t;
if(cmd->path) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
setenvstatus_t = dconf->setenvstatus_t;
} else {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
setenvstatus_t = sconf->setenvstatus_t;
}
if(strcasecmp(rc, QS_CLOSE) == 0) {
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if(err != NULL) {
return apr_psprintf(cmd->pool, "%s: "QS_CLOSE" may only be defined globally",
cmd->directive->directive);
}
if(strcasecmp(var, QS_BLOCK) != 0) {
return apr_psprintf(cmd->pool, "%s: "QS_CLOSE" may only be defined for the event "QS_BLOCK,
cmd->directive->directive);
}
} else if(strcasecmp(rc, QS_MAXIP) == 0) {
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if(err != NULL) {
return apr_psprintf(cmd->pool, "%s: "QS_MAXIP" may only be defined globally",
cmd->directive->directive);
}
if(strcasecmp(var, QS_BLOCK) != 0) {
return apr_psprintf(cmd->pool, "%s: "QS_MAXIP" may only be defined for the event "QS_BLOCK,
cmd->directive->directive);
}
} else if(strcasecmp(rc, QS_EMPTY_CON) == 0) {
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if(err != NULL) {
return apr_psprintf(cmd->pool, "%s: "QS_EMPTY_CON" may only be defined globally",
cmd->directive->directive);
}
if(strcasecmp(var, QS_BLOCK) != 0) {
return apr_psprintf(cmd->pool, "%s: "QS_EMPTY_CON" may only be defined for the event "QS_BLOCK,
cmd->directive->directive);
}
} else if(strcasecmp(rc, QS_BROKEN_CON) == 0) {
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if(err != NULL) {
return apr_psprintf(cmd->pool, "%s: "QS_BROKEN_CON" may only be defined globally",
cmd->directive->directive);
}
if(strcasecmp(var, QS_BLOCK) != 0) {
return apr_psprintf(cmd->pool, "%s: "QS_BROKEN_CON" may only be defined for the event "QS_BLOCK,
cmd->directive->directive);
}
} else {
int code = atoi(rc);
if(code <= 0) {
return apr_psprintf(cmd->pool, "%s: invalid HTTP status code",
cmd->directive->directive);
}
}
apr_table_set(setenvstatus_t, rc, var);
return NULL;
}
/** QS_SetEnvIfResBody */
const char *qos_event_setenvifresbody_cmd(cmd_parms *cmd, void *dcfg, const char *pattern,
const char *var) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
if(dconf->response_pattern) {
return apr_psprintf(cmd->pool, "%s: only one pattern must be configured for a location",
cmd->directive->directive);
}
dconf->response_pattern = apr_pstrdup(cmd->pool, pattern);
dconf->response_pattern_len = strlen(dconf->response_pattern);
dconf->response_pattern_var = apr_pstrdup(cmd->pool, var);
if(var[0] == '!' && !var[1]) {
return apr_psprintf(cmd->pool, "%s: variable name is too short",
cmd->directive->directive);
}
return NULL;
}
/* QS_SetEnv */
const char *qos_setenv_cmd(cmd_parms *cmd, void *dcfg, const char *variable,
const char *value) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
if(!variable[0] || !value[0]) {
return apr_psprintf(cmd->pool, "%s: invalid parameter",
cmd->directive->directive);
}
if(strchr(variable, '=')) {
return apr_psprintf(cmd->pool, "%s: variable must not contain a '='",
cmd->directive->directive);
}
apr_table_set(sconf->setenv_t, apr_pstrcat(cmd->pool, variable, "=", value, NULL), variable);
return NULL;
}
/* QS_SetReqHeader */
const char *qos_setreqheader_cmd(cmd_parms *cmd, void *dcfg, const char *header,
const char *variable, const char *late) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
if(!variable[0] || !header[0]) {
return apr_psprintf(cmd->pool, "%s: invalid parameter",
cmd->directive->directive);
}
if(header[0] == '!' && !header[1]) {
return apr_psprintf(cmd->pool, "%s: header name is too short",
cmd->directive->directive);
}
if(strchr(header, '=')) {
return apr_psprintf(cmd->pool, "%s: header name must not contain a '='",
cmd->directive->directive);
}
if(late != NULL) {
if(strcasecmp(late, "late") != 0) {
return apr_psprintf(cmd->pool, "%s: third parameter can only be 'late'",
cmd->directive->directive);
}
apr_table_set(sconf->setreqheaderlate_t,
apr_pstrcat(cmd->pool, header, "=", variable, NULL), header);
} else {
apr_table_set(sconf->setreqheader_t,
apr_pstrcat(cmd->pool, header, "=", variable, NULL), header);
}
return NULL;
}
/* QS_UnsetReqHeader */
const char *qos_unsetreqheader_cmd(cmd_parms *cmd, void *dcfg, const char *header) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
apr_table_set(sconf->unsetreqheader_t, header, "");
return NULL;
}
/* QS_UnsetResHeader */
const char *qos_unsetresheader_cmd(cmd_parms *cmd, void *dcfg, const char *header) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
apr_table_set(sconf->unsetresheader_t, header, "");
return NULL;
}
const char *qos_event_setenvresheader_cmd(cmd_parms *cmd, void *dcfg, const char *hdr,
const char *action) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
apr_table_set(sconf->setenvresheader_t, hdr, action == NULL ? "" : action);
return NULL;
}
const char *qos_event_setenvresheadermatch_cmd(cmd_parms *cmd, void *dcfg, const char *hdr,
const char *pcres) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
ap_regex_t *preg = ap_pregcomp(cmd->pool, pcres, AP_REG_DOTALL | AP_REG_ICASE);
if(preg == NULL) {
return apr_psprintf(cmd->pool, "%s: could not compile regular expression '%s'",
cmd->directive->directive, pcres);
}
apr_table_setn(sconf->setenvresheadermatch_t, apr_pstrdup(cmd->pool, hdr), (char *)preg);
return NULL;
}
const char *qos_redirectif_cmd(cmd_parms *cmd, void *dcfg, const char *var,
const char *pattern, const char *url) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qos_dir_config *dconf = (qos_dir_config*)dcfg;
qos_redirectif_entry_t *new;
if(cmd->path) {
new = apr_array_push(dconf->redirectif);
} else {
new = apr_array_push(sconf->redirectif);
}
new->name = apr_pstrdup(cmd->pool, var);
new->preg = ap_pregcomp(cmd->pool, pattern, (AP_REG_EXTENDED | AP_REG_ICASE));
if(new->preg == NULL) {
return apr_psprintf(cmd->pool, "%s: could not compile regular expression %s",
cmd->directive->directive, pattern);
}
if(strncasecmp(url, "307:", 4) == 0) {
new->code = HTTP_TEMPORARY_REDIRECT;
new->url = apr_pstrdup(cmd->pool, &url[4]);
} else if(strncasecmp(url, "301:", 4) == 0) {
new->code = HTTP_MOVED_PERMANENTLY;
new->url = apr_pstrdup(cmd->pool, &url[4]);
} else if(strncasecmp(url, "302:", 4) == 0) {
new->code = HTTP_MOVED_TEMPORARILY;
new->url = apr_pstrdup(cmd->pool, &url[4]);
} else {
new->code = HTTP_MOVED_TEMPORARILY;
new->url = apr_pstrdup(cmd->pool, url);
}
return NULL;
}
const char *qos_setenvres_cmd(cmd_parms *cmd, void *dcfg, const char *var,
const char *pattern, const char *var2) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qos_pregval_t *pregval = apr_pcalloc(cmd->pool, sizeof(qos_pregval_t));
pregval->name = apr_pstrdup(cmd->pool, var2);
pregval->value = strchr(pregval->name, '=');
if(pregval->value) {
pregval->value[0] = '\0';
pregval->value++;
}
pregval->preg = ap_pregcomp(cmd->pool, pattern, AP_REG_EXTENDED);
if(pregval->preg == NULL) {
return apr_psprintf(cmd->pool, "%s: could not compile regular expression '%s'",
cmd->directive->directive, pattern);
}
apr_table_addn(sconf->setenvres_t, apr_pstrdup(cmd->pool, var), (char *)pregval);
return NULL;
}
/** QS_SetEnvIf */
const char *qos_event_setenvif_cmd(cmd_parms *cmd, void *dcfg, const char *v1, const char *v2,
const char *a3) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qos_setenvif_t *setenvif;
if(cmd->path) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
setenvif = apr_array_push(dconf->setenvif_t);
} else {
setenvif = apr_array_push(sconf->setenvif_t);
}
if(a3) {
// mode 1 (boolean AND operator)
setenvif->variable1 = apr_pstrdup(cmd->pool, v1);
setenvif->variable2 = apr_pstrdup(cmd->pool, v2);
setenvif->preg = NULL;
setenvif->name = apr_pstrdup(cmd->pool, a3);
setenvif->value = strchr(setenvif->name, '=');
if(setenvif->value == NULL) {
if(setenvif->name[0] == '!') {
setenvif->value = apr_pstrdup(cmd->pool, "");
} else {
return apr_psprintf(cmd->pool, "%s: new variable must have the format <name>=<value>",
cmd->directive->directive);
}
} else {
setenvif->value[0] = '\0';
setenvif->value++;
}
} else {
// mode 2 (pattern match)
char *pattern;
setenvif->variable1 = apr_pstrdup(cmd->pool, v1);
pattern = strchr(setenvif->variable1, '=');
if(pattern == NULL) {
return apr_psprintf(cmd->pool, "%s: missing pattern for variable1",
cmd->directive->directive);
}
pattern[0] = '\0';
pattern++;
setenvif->variable2 = NULL;
setenvif->preg = ap_pregcomp(cmd->pool, pattern, AP_REG_EXTENDED);
if(setenvif->preg == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regex (%s)",
cmd->directive->directive, pattern);
}
setenvif->name = apr_pstrdup(cmd->pool, v2);
setenvif->value = strchr(setenvif->name, '=');
if(setenvif->value == NULL) {
if(setenvif->name[0] == '!') {
setenvif->value = apr_pstrdup(cmd->pool, "");
} else {
return apr_psprintf(cmd->pool, "%s: new variable must have the format <name>=<value>",
cmd->directive->directive);
}
} else {
setenvif->value[0] = '\0';
setenvif->value++;
}
}
return NULL;
}
/** QS_SetEnvIfCmp */
const char *qos_cmp_cmd(cmd_parms *cmd, void *dcfg, int argc, char *const argv[]) {
qos_cmp_entry_t *new;
qos_dir_config *conf = dcfg;
char *del;
if(argc != 4) {
return apr_psprintf(cmd->pool, "%s: requires 4 arguments",
cmd->directive->directive);
}
new = apr_array_push(conf->setenvcmp);
new->left = apr_pstrdup(cmd->pool, argv[0]);
if(strcasecmp(argv[1], "eq") == 0) {
new->cmp = QS_CMP_EQ;
} else if(strcasecmp(argv[1], "ne") == 0) {
new->cmp = QS_CMP_NE;
} else if(strcasecmp(argv[1], "lt") == 0) {
new->cmp = QS_CMP_LT;
} else if(strcasecmp(argv[1], "gt") == 0) {
new->cmp = QS_CMP_GT;
} else {
return apr_psprintf(cmd->pool, "%s: invalid operator '%s",
cmd->directive->directive, argv[1]);
}
new->right = apr_pstrdup(cmd->pool, argv[2]);
new->variable = apr_pstrdup(cmd->pool, argv[3]);
del = strchr(new->variable, '=');
if(del) {
new->value = &del[1];
del[0] = '\0';
} else {
new->value = apr_pstrdup(cmd->pool, "");
}
return NULL;
}
/** QS_SetEnvIfQuery */
const char *qos_event_setenvifquery_cmd(cmd_parms *cmd, void *dcfg, const char *rx, const char *v) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qos_setenvifquery_t *setenvif = apr_pcalloc(cmd->pool, sizeof(qos_setenvifquery_t));
char *p;
setenvif->preg = ap_pregcomp(cmd->pool, rx, AP_REG_EXTENDED);
if(setenvif->preg == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regex (%s)",
cmd->directive->directive, rx);
}
if(strlen(v) < 2) {
return apr_psprintf(cmd->pool, "%s: variable name is too short (%s)",
cmd->directive->directive, v);
}
setenvif->name = apr_pstrdup(cmd->pool, v);
p = strchr(setenvif->name, '=');
if(p == NULL) {
setenvif->value = apr_pstrdup(cmd->pool, "");
} else {
p[0] = '\0';
p++;
setenvif->value = p;
}
if(cmd->path) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
apr_table_setn(dconf->setenvifquery_t, apr_pstrdup(cmd->pool, rx), (char *)setenvif);
} else {
apr_table_setn(sconf->setenvifquery_t, apr_pstrdup(cmd->pool, rx), (char *)setenvif);
}
return NULL;
}
const char *qos_event_setenvifparpbody_cmd(cmd_parms *cmd, void *dcfg,
const char *rx, const char *v) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qos_setenvifparpbody_t *setenvif = apr_pcalloc(cmd->pool, sizeof(qos_setenvifparpbody_t));
char *p;
setenvif->pregx = ap_pregcomp(cmd->pool, rx, AP_REG_DOTALL | AP_REG_EXTENDED | AP_REG_ICASE);
if(setenvif->pregx == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regex (%s)",
cmd->directive->directive, rx);
}
setenvif->name = apr_pstrdup(cmd->pool, v);
p = strchr(setenvif->name, '=');
if(p == NULL) {
setenvif->value = apr_pstrdup(cmd->pool, "");
} else {
p[0] = '\0';
p++;
setenvif->value = p;
}
m_requires_parp = 1;
apr_table_setn(sconf->setenvifparpbody_t, apr_pstrdup(cmd->pool, rx), (char *)setenvif);
return NULL;
}
const char *qos_event_setenvifparp_cmd(cmd_parms *cmd, void *dcfg, const char *rx, const char *v) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qos_setenvifquery_t *setenvif = apr_pcalloc(cmd->pool, sizeof(qos_setenvifquery_t));
char *p;
setenvif->preg = ap_pregcomp(cmd->pool, rx, AP_REG_EXTENDED);
if(setenvif->preg == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regex (%s)",
cmd->directive->directive, rx);
}
if(strlen(v) < 2) {
return apr_psprintf(cmd->pool, "%s: variable name is too short (%s)",
cmd->directive->directive, v);
}
setenvif->name = apr_pstrdup(cmd->pool, v);
p = strchr(setenvif->name, '=');
if(p == NULL) {
setenvif->value = apr_pstrdup(cmd->pool, "");
} else {
p[0] = '\0';
p++;
setenvif->value = p;
}
m_requires_parp = 1;
apr_table_setn(sconf->setenvifparp_t, apr_pstrdup(cmd->pool, rx), (char *)setenvif);
return NULL;
}
/**
* defines custom error page
*/
const char *qos_error_page_cmd(cmd_parms *cmd, void *dcfg, const char *path) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->error_page = apr_pstrdup(cmd->pool, path);
if((sconf->error_page[0] != '/') &&
(strncmp(sconf->error_page, "http", 4) != 0)) {
return apr_psprintf(cmd->pool, "%s: requires absolute path (%s)",
cmd->directive->directive, sconf->error_page);
}
return NULL;
}
#if APR_HAS_THREADS
/**
* QS_Status
*/
const char *qos_qsstatus_cmd(cmd_parms *cmd, void *dcfg, int flag) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->qsstatus = flag;
return NULL;
}
#endif
/**
* QS_EventCount
*/
const char *qos_qsevents_cmd(cmd_parms *cmd, void *dcfg, int flag) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->qsevents = flag;
return NULL;
}
/**
* pipe to global qslog tool (per Apache instance stat)
*/
const char *qos_qlog_cmd(cmd_parms *cmd, void *dcfg, const char *arg) {
qos_srv_config *sconf = ap_get_module_config(cmd->server->module_config, &qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->qslog_str = apr_pstrdup(cmd->pool, arg);
return NULL;
}
/**
* global error code setting
*/
const char *qos_error_code_cmd(cmd_parms *cmd, void *dcfg, const char *arg) {
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
int idx500 = ap_index_of_response(HTTP_INTERNAL_SERVER_ERROR);
if (err != NULL) {
return err;
}
m_retcode = atoi(arg);
if((m_retcode < 400) || (m_retcode > 599)) {
return apr_psprintf(cmd->pool, "%s: HTTP response code code must be a"
" numeric value between 400 and 599",
cmd->directive->directive);
}
if(m_retcode != 500) {
if(ap_index_of_response(m_retcode) == idx500) {
return apr_psprintf(cmd->pool, "%s: unsupported HTTP response code",
cmd->directive->directive);
}
}
return NULL;
}
/**
* global connection close behavior
*/
const char *qos_forced_close_cmd(cmd_parms *cmd, void *dcfg, int flag) {
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
m_forced_close = flag;
return NULL;
}
/** QS_UserTrackingCookieName */
#ifdef AP_TAKE_ARGV
const char *qos_user_tracking_cookie_cmd(cmd_parms *cmd, void *dcfg,
int argc, char *const argv[]) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
int pos = 1;
if(argc == 0) {
return apr_psprintf(cmd->pool, "%s: takes 1 to 4 arguments",
cmd->directive->directive);
}
sconf->user_tracking_cookie = apr_pstrdup(cmd->pool, argv[0]);
while(pos < argc) {
const char *value = argv[pos];
if(value[0] == '/') {
sconf->user_tracking_cookie_force = apr_pstrdup(cmd->pool, value);
} else if(strcasecmp(value, "session") == 0) {
sconf->user_tracking_cookie_session = 1;
} else if(strcasecmp(value, "jsredirect") == 0) {
sconf->user_tracking_cookie_jsredirect = 1;
} else {
if(sconf->user_tracking_cookie_domain != NULL) {
return apr_psprintf(cmd->pool, "%s: invalid attribute"
" (expects <name>, <path>, 'session', or <domain>",
cmd->directive->directive);
}
sconf->user_tracking_cookie_domain = apr_pstrdup(cmd->pool, value);
}
pos++;
}
return NULL;
}
#else
const char *qos_user_tracking_cookie_cmd(cmd_parms *cmd, void *dcfg,
const char *name,
const char *option1,
const char *option2) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *force = NULL;
sconf->user_tracking_cookie = apr_pstrdup(cmd->pool, name);
if(option1) {
if((strcasecmp(option1, "session") == 0) ||
(strcasecmp(option1, "'session'") == 0)) {
sconf->user_tracking_cookie_session = 1;
} else {
force = option1;
}
}
if(option2) {
if((strcasecmp(option2, "session") == 0) ||
(strcasecmp(option2, "'session'") == 0)) {
sconf->user_tracking_cookie_session = 1;
} else {
if(force == NULL) {
force = option2;
}
}
}
if(force) {
if(force[0] != '/') {
return apr_psprintf(cmd->pool, "%s: invalid path '%s'",
cmd->directive->directive, force);
}
sconf->user_tracking_cookie_force = apr_pstrdup(cmd->pool, force);
}
return NULL;
}
#endif
/**
* session definitions: cookie name and path, expiration/max-age
*/
const char *qos_cookie_name_cmd(cmd_parms *cmd, void *dcfg, const char *name) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->cookie_name = apr_pstrdup(cmd->pool, name);
return NULL;
}
const char *qos_cookie_path_cmd(cmd_parms *cmd, void *dcfg, const char *path) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->cookie_path = apr_pstrdup(cmd->pool, path);
return NULL;
}
const char *qos_timeout_cmd(cmd_parms *cmd, void *dcfg, const char *sec) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->max_age = atoi(sec);
if(sconf->max_age == 0) {
return apr_psprintf(cmd->pool, "%s: timeout must be numeric value >0",
cmd->directive->directive);
}
return NULL;
}
const char *qos_key_cmd(cmd_parms *cmd, void *dcfg, const char *seed) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->rawKey = (unsigned char *)apr_pstrdup(cmd->pool, seed);
sconf->rawKeyLen = strlen(seed);
EVP_BytesToKey(EVP_des_ede3_cbc(), EVP_sha1(), NULL,
sconf->rawKey, sconf->rawKeyLen, 1, sconf->key, NULL);
sconf->keyset = 1;
return NULL;
}
/**
* name of the http header to mark a vip
*/
const char *qos_header_name_cmd(cmd_parms *cmd, void *dcfg, const char *n, const char *drop) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
char *name = apr_pstrdup(cmd->pool, n);
char *p = strchr(name, '=');
if(p) {
p[0] = '\0';
p++;
sconf->header_name_regex = ap_pregcomp(cmd->pool, p, AP_REG_EXTENDED);
if(sconf->header_name_regex == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regex (%s)",
cmd->directive->directive, p);
}
} else {
sconf->header_name_regex = NULL;
}
if(drop && (strcasecmp(drop, "drop") == 0)) {
sconf->header_name_drop = 1;
} else {
sconf->header_name_drop = 0;
}
sconf->header_name = name;
return NULL;
}
const char *qos_ip_header_name_cmd(cmd_parms *cmd, void *dcfg, const char *n, const char *drop) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
char *name = apr_pstrdup(cmd->pool, n);
char *p = strchr(name, '=');
if(p) {
p[0] = '\0';
p++;
sconf->ip_header_name_regex = ap_pregcomp(cmd->pool, p, AP_REG_EXTENDED);
if(sconf->ip_header_name_regex == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regex (%s)",
cmd->directive->directive, p);
}
} else {
sconf->ip_header_name_regex = NULL;
}
if(drop && (strcasecmp(drop, "drop") == 0)) {
sconf->ip_header_name_drop = 1;
} else {
sconf->ip_header_name_drop = 0;
}
sconf->has_qos_cc = 1;
sconf->ip_header_name = name;
return NULL;
}
const char *qos_vip_u_cmd(cmd_parms *cmd, void *dcfg) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->vip_user = 1;
return NULL;
}
const char *qos_vip_ip_u_cmd(cmd_parms *cmd, void *dcfg) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->vip_ip_user = 1;
return NULL;
}
/**
* max concurrent connections per server
*/
const char *qos_max_conn_cmd(cmd_parms *cmd, void *dcfg, const char *number) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->has_conn_counter = 1;
sconf->max_conn = atoi(number);
if(sconf->max_conn == 0) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >0",
cmd->directive->directive);
}
return NULL;
}
/**
* QS_SrvMaxConnClose, disable keep-alive
*/
const char *qos_max_conn_close_cmd(cmd_parms *cmd, void *dcfg, const char *number) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
char *n = apr_pstrdup(cmd->temp_pool, number);
sconf->has_conn_counter = 1;
if((strlen(n) > 1) &&
(n[strlen(n)-1] == '%')) {
n[strlen(n)-1] = '\0';
sconf->max_conn_close = atoi(n);
sconf->max_conn_close_percent = sconf->max_conn_close;
if(sconf->max_conn_close > 99) {
return apr_psprintf(cmd->pool, "%s: number must be a percentage <100",
cmd->directive->directive);
}
} else {
sconf->max_conn_close = atoi(n);
sconf->max_conn_close_percent = 0;
}
if(sconf->max_conn_close == 0) {
return apr_psprintf(cmd->pool, "%s: number must be >0",
cmd->directive->directive);
}
return NULL;
}
/**
* max concurrent connections per client ip
*/
const char *qos_max_conn_ip_cmd(cmd_parms *cmd, void *dcfg, const char *number,
const char *connections) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->has_conn_counter = 1;
sconf->max_conn_per_ip = atoi(number);
if(sconf->max_conn_per_ip == 0) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >0",
cmd->directive->directive);
}
if(connections) {
sconf->max_conn_per_ip_connections = atoi(connections);
if((sconf->max_conn_per_ip_connections == 0) &&
(strcmp(connections, "0") != 0)) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >0",
cmd->directive->directive);
}
}
return NULL;
}
/* QS_SrvMaxConnPerIPIgnoreVIP */
const char *qos_max_conn_ip_vip_off_cmd(cmd_parms *cmd, void *dcfg, int flag) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->max_conn_per_ip_ignore_vip = flag;
return NULL;
}
/**
* QS_SrvSerialize
*/
const char *qos_serialize_cmd(cmd_parms *cmd, void *dcfg, const char * flag,
const char *seconds) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
if(strcasecmp(flag, "on") == 0) {
sconf->serialize = 1;
} else if(strcasecmp(flag, "off") == 0) {
sconf->serialize = 0;
} else {
return apr_psprintf(cmd->pool, "%s: flag needs to be either 'on' or 'off'",
cmd->directive->directive);
}
if(seconds) {
sconf->serializeTMO = atoi(seconds);
if(sconf->serializeTMO <= 0) {
return apr_psprintf(cmd->pool, "%s: timeout (seconds) must be a numeric value >0",
cmd->directive->directive);
}
// n * 50 milliseconds
sconf->serializeTMO = sconf->serializeTMO * 20;
}
return NULL;
}
/**
* ip address without any limitation
*/
const char *qos_max_conn_ex_cmd(cmd_parms *cmd, void *dcfg, const char *addr) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
if(addr[strlen(addr)-1] == '.') {
/* address range */
apr_table_add(sconf->exclude_ip, addr, "r");
} else if(addr[strlen(addr)-1] == ':') {
/* address range */
apr_table_add(sconf->exclude_ip, addr, "r");
} else {
/* single ip */
apr_table_add(sconf->exclude_ip, addr, "s");
}
return NULL;
}
const char *qos_req_rate_off_cmd(cmd_parms *cmd, void *dcfg) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->min_rate_off = 1;
return NULL;
}
/** verify, that the platform supports "%p" in sprintf */
static int qos_sprintfcheck() {
char buf[128];
char buf2[128];
sprintf(buf, "%p", buf);
sprintf(buf2, "%p", buf2);
if((strcmp(buf, buf2) == 0) || (strlen(buf) < 4)) {
/* not okay */
return 0;
}
return 1;
}
const char *qos_req_rate_cmd(cmd_parms *cmd, void *dcfg, const char *sec, const char *secmax) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
if(!qos_sprintfcheck()) {
return apr_psprintf(cmd->pool, "%s: directive can't be used on this platform",
cmd->directive->directive);
}
if(sconf->req_rate != -1) {
return apr_psprintf(cmd->pool, "%s: directive can't be used together with QS_SrvMinDataRate",
cmd->directive->directive);
}
sconf->req_rate = atoi(sec);
if(sconf->req_rate <= 0) {
return apr_psprintf(cmd->pool, "%s: request rate must be a numeric value >0",
cmd->directive->directive);
}
if(secmax) {
sconf->min_rate_max = atoi(secmax);
if(sconf->min_rate_max <= sconf->min_rate) {
return apr_psprintf(cmd->pool, "%s: max. data rate must be a greater than min. value",
cmd->directive->directive);
}
}
return NULL;
}
/* QS_SrvMinDataRateOffEvent */
const char *qos_min_rate_off_cmd(cmd_parms *cmd, void *dcfg, const char *var) {
apr_table_t *disable_reqrate_events;
if(cmd->path) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
disable_reqrate_events = dconf->disable_reqrate_events;
} else {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
disable_reqrate_events = sconf->disable_reqrate_events;
}
if(((var[0] != '+') && (var[0] != '-')) || (strlen(var) < 2)) {
return apr_psprintf(cmd->pool, "%s: invalid variable (requires +/- prefix)",
cmd->directive->directive);
}
apr_table_set(disable_reqrate_events, var, "");
return NULL;
}
#ifdef AP_TAKE_ARGV
const char *qos_min_rate_cmd(cmd_parms *cmd, void *dcfg, int argc, char *const argv[])
#else
const char *qos_min_rate_cmd(cmd_parms *cmd, void *dcfg, const char *_sec, const char *_secmax)
#endif
{
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
const char *sec = NULL;
const char *secmax = NULL;
const char *connections = NULL;
#ifdef AP_TAKE_ARGV
if(argc == 0) {
return apr_psprintf(cmd->pool, "%s: takes 1 to 3 arguments",
cmd->directive->directive);
}
sec = argv[0];
if(argc > 1) {
secmax = argv[1];
}
if(argc > 2) {
connections = argv[2];
}
#else
sec = _sec;
secmax = _secmax;
#endif
if (err != NULL) {
return err;
}
if(!qos_sprintfcheck()) {
return apr_psprintf(cmd->pool, "%s: directive can't be used on this platform",
cmd->directive->directive);
}
if(sconf->req_rate != -1) {
return apr_psprintf(cmd->pool, "%s: directive can't be used together with QS_SrvRequestRate",
cmd->directive->directive);
}
sconf->req_rate = atoi(sec);
sconf->min_rate = sconf->req_rate;
if(connections) {
sconf->req_rate_start = atoi(connections);
if(sconf->req_rate_start <= 0) {
return apr_psprintf(cmd->pool, "%s: number of connections must be a numeric value >0",
cmd->directive->directive);
}
}
if(sconf->req_rate <= 0) {
return apr_psprintf(cmd->pool, "%s: minimal data rate must be a numeric value >0",
cmd->directive->directive);
}
if(secmax) {
sconf->min_rate_max = atoi(secmax);
if(sconf->min_rate_max <= sconf->min_rate) {
return apr_psprintf(cmd->pool, "%s: max. data rate must be a greater than min. value",
cmd->directive->directive);
}
}
return NULL;
}
/* QS_SrvMinDataRateIgnoreVIP */
const char *qos_min_rate_vip_off_cmd(cmd_parms *cmd, void *dcfg, int flag) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->req_ignore_vip_rate = flag;
return NULL;
}
/**
* generic filter command
*/
const char *qos_deny_cmd(cmd_parms *cmd, void *dcfg,
const char *id, const char *action, const char *pcres,
qs_rfilter_type_e type, int options) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
qos_rfilter_t *flt = apr_pcalloc(cmd->pool, sizeof(qos_rfilter_t));
flt->type = type;
if(((id[0] != '+') && (id[0] != '-')) || (strlen(id) < 2)) {
return apr_psprintf(cmd->pool, "%s: invalid rule id",
cmd->directive->directive);
}
flt->id = apr_pstrdup(cmd->pool, &id[1]);
if(strcasecmp(action, "log") == 0) {
flt->action = QS_LOG;
} else if(strcasecmp(action, "deny") == 0) {
flt->action = QS_DENY;
} else {
return apr_psprintf(cmd->pool, "%s: invalid action",
cmd->directive->directive);
}
if(flt->type != QS_DENY_EVENT) {
flt->preg = ap_pregcomp(cmd->pool, pcres, AP_REG_DOTALL | options);
if(flt->preg == NULL) {
return apr_psprintf(cmd->pool, "%s: could not compile regular expression '%s'",
cmd->directive->directive,
pcres);
}
}
flt->text = apr_pstrdup(cmd->pool, pcres);
apr_table_setn(dconf->rfilter_table, apr_pstrdup(cmd->pool, id), (char *)flt);
return NULL;
}
const char *qos_deny_rql_cmd(cmd_parms *cmd, void *dcfg,
const char *id, const char *action, const char *pcres) {
return qos_deny_cmd(cmd, dcfg, id, action, pcres, QS_DENY_REQUEST_LINE, AP_REG_ICASE);
}
const char *qos_deny_path_cmd(cmd_parms *cmd, void *dcfg,
const char *id, const char *action, const char *pcres) {
return qos_deny_cmd(cmd, dcfg, id, action, pcres, QS_DENY_PATH, AP_REG_ICASE);
}
const char *qos_deny_query_cmd(cmd_parms *cmd, void *dcfg,
const char *id, const char *action, const char *pcres) {
return qos_deny_cmd(cmd, dcfg, id, action, pcres, QS_DENY_QUERY, AP_REG_ICASE);
}
const char *qos_deny_event_cmd(cmd_parms *cmd, void *dcfg,
const char *id, const char *action, const char *event) {
return qos_deny_cmd(cmd, dcfg, id, action, event, QS_DENY_EVENT, 0);
}
const char *qos_permit_uri_cmd(cmd_parms *cmd, void *dcfg,
const char *id, const char *action, const char *pcres) {
return qos_deny_cmd(cmd, dcfg, id, action, pcres, QS_PERMIT_URI, 0);
}
const char *qos_deny_urlenc_cmd(cmd_parms *cmd, void *dcfg, const char *mode) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
if(strcasecmp(mode, "log") == 0) {
dconf->urldecoding = QS_LOG;
} else if(strcasecmp(mode, "deny") == 0) {
dconf->urldecoding = QS_DENY;
} else if(strcasecmp(mode, "off") == 0) {
dconf->urldecoding = QS_OFF;
} else {
return apr_psprintf(cmd->pool, "%s: invalid action",
cmd->directive->directive);
}
return NULL;
}
const char *qos_milestone_tmo_cmd(cmd_parms *cmd, void *dcfg, const char *sec) {
qos_srv_config *sconf = ap_get_module_config(cmd->server->module_config, &qos_module);
sconf->milestoneTimeout = atoi(sec);
if(sconf->milestoneTimeout <= 0) {
return apr_psprintf(cmd->pool, "%s: timeout must be numeric value >0",
cmd->directive->directive);
}
return NULL;
}
const char *qos_milestone_cmd(cmd_parms *cmd, void *dcfg, const char *action,
const char *pattern, const char *thinktimestr) {
qos_srv_config *sconf = ap_get_module_config(cmd->server->module_config, &qos_module);
qos_milestone_t *ms;
if(sconf->milestones == NULL) {
sconf->milestones = apr_array_make(cmd->pool, 100, sizeof(qos_milestone_t));
}
ms = apr_array_push(sconf->milestones);
ms->num = sconf->milestones->nelts - 1;
if(thinktimestr != NULL) {
ms->thinktime = atoi(thinktimestr);
if(ms->thinktime <= 0) {
return apr_psprintf(cmd->pool, "%s: invalid 'think time' (must be numeric value >0)",
cmd->directive->directive);
}
} else {
ms->thinktime = 0;
}
ms->preg = ap_pregcomp(cmd->pool, pattern, AP_REG_DOTALL);
if(ms->preg == NULL) {
return apr_psprintf(cmd->pool, "%s: could not compile regular expression '%s'",
cmd->directive->directive, pattern);
}
ms->pattern = apr_pstrdup(cmd->pool, pattern);
if(strcasecmp(action, "deny") == 0) {
ms->action = QS_DENY;
} else if(strcasecmp(action, "log") == 0) {
ms->action = QS_LOG;
} else {
return apr_psprintf(cmd->pool, "%s: invalid action %s",
cmd->directive->directive, action);
}
return NULL;
}
const char *qos_maxpost_cmd(cmd_parms *cmd, void *dcfg, const char *bytes) {
apr_off_t s;
char *errp = NULL;
#ifdef ap_http_scheme
/* Apache 2.2 */
if(APR_SUCCESS != apr_strtoff(&s, bytes, &errp, 10))
#else
if((s = apr_atoi64(bytes)) < 0)
#endif
{
return "QS_LimitRequestBody argument is not parsable";
}
if(s < 0) {
return "QS_LimitRequestBody requires a non-negative integer";
}
if(cmd->path == NULL) {
/* server */
qos_srv_config *sconf = ap_get_module_config(cmd->server->module_config, &qos_module);
sconf->maxpost = s;
} else {
/* location */
qos_dir_config *dconf = (qos_dir_config*)dcfg;
dconf->maxpost = s;
}
return NULL;
}
/* QS_Decoding */
const char *qos_dec_cmd(cmd_parms *cmd, void *dcfg, const char *arg) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
// if(strcasecmp(arg, "html") == 0) {
// dconf->dec_mode |= QOS_DEC_MODE_FLAGS_HTML;
// } else
if(strcasecmp(arg, "uni") == 0) {
dconf->dec_mode |= QOS_DEC_MODE_FLAGS_UNI;
// } if(strcasecmp(arg, "ansi") == 0) {
// dconf->dec_mode |= QOS_DEC_MODE_FLAGS_ANSI;
} else {
return apr_psprintf(cmd->pool, "%s: unknown decoding '%s'",
cmd->directive->directive, arg);
}
return NULL;
}
const char *qos_denyinheritoff_cmd(cmd_parms *cmd, void *dcfg) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
dconf->inheritoff = 1;
return NULL;
}
const char *qos_denybody_cmd(cmd_parms *cmd, void *dcfg, int flag) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
dconf->bodyfilter_p = flag;
dconf->bodyfilter_d = flag;
if(flag) {
m_requires_parp = 1;
}
return NULL;
}
const char *qos_denybody_d_cmd(cmd_parms *cmd, void *dcfg, int flag) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
dconf->bodyfilter_d = flag;
if(flag) {
m_requires_parp = 1;
}
return NULL;
}
const char *qos_denybody_p_cmd(cmd_parms *cmd, void *dcfg, int flag) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
dconf->bodyfilter_p = flag;
if(flag) {
m_requires_parp = 1;
}
return NULL;
}
/* QS_RequestHeaderFilter enables/disables header filter */
const char *qos_headerfilter_cmd(cmd_parms *cmd, void *dcfg, const char *flag) {
qs_headerfilter_mode_e headerfilter;
if(strcasecmp(flag, "on") == 0) {
headerfilter = QS_HEADERFILTER_ON;
} else if(strcasecmp(flag, "off") == 0) {
headerfilter = QS_HEADERFILTER_OFF;
} else if(strcasecmp(flag, "size") == 0) {
headerfilter = QS_HEADERFILTER_SIZE_ONLY;
} else {
return apr_psprintf(cmd->pool, "%s: invalid argument",
cmd->directive->directive);
}
if(cmd->path) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
dconf->headerfilter = headerfilter;
} else {
qos_srv_config *sconf = ap_get_module_config(cmd->server->module_config, &qos_module);
sconf->headerfilter = headerfilter;
}
return NULL;
}
/* QS_ResponseHeaderFilter */
const char *qos_resheaderfilter_cmd(cmd_parms *cmd, void *dcfg, const char *flag) {
qos_dir_config *dconf = (qos_dir_config*)dcfg;
if(strcasecmp(flag, "on") == 0) {
dconf->resheaderfilter = QS_HEADERFILTER_ON;
} else if(strcasecmp(flag, "off") == 0) {
dconf->resheaderfilter = QS_HEADERFILTER_OFF;
} else if(strcasecmp(flag, "silent") == 0) {
dconf->resheaderfilter = QS_HEADERFILTER_SILENT;
} else {
return apr_psprintf(cmd->pool, "%s: invalid argument",
cmd->directive->directive);
}
return NULL;
}
/* QS_RequestHeaderFilterRule: set custom header rules (global only)
name, action, pcre, size */
#ifdef AP_TAKE_ARGV
const char *qos_headerfilter_rule_cmd(cmd_parms *cmd, void *dcfg, int argc, char *const argv[])
#else
const char *qos_headerfilter_rule_cmd(cmd_parms *cmd, void *dcfg,
const char *header, const char *action,
const char *rule)
#endif
{
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qos_fhlt_r_t *he;
#ifdef AP_TAKE_ARGV
const char *header;
const char *rule;
const char *action;
#endif
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
#ifdef AP_TAKE_ARGV
if(argc != 4) {
return apr_psprintf(cmd->pool, "%s: takes 4 arguments",
cmd->directive->directive);
}
#endif
he = apr_pcalloc(cmd->pool, sizeof(qos_fhlt_r_t));
#ifdef AP_TAKE_ARGV
header = argv[0];
action = argv[1];
rule = argv[2];
he->size = atoi(argv[3]);
#else
he->size = 9000;
#endif
he->text = apr_pstrdup(cmd->pool, rule);
he->preg = ap_pregcomp(cmd->pool, rule, AP_REG_DOTALL);
if(strcasecmp(action, "deny") == 0) {
he->action = QS_FLT_ACTION_DENY;
} else if(strcasecmp(action, "drop") == 0) {
he->action = QS_FLT_ACTION_DROP;
} else {
return apr_psprintf(cmd->pool, "%s: invalid action %s",
cmd->directive->directive, action);
}
if(he->preg == NULL) {
return apr_psprintf(cmd->pool, "%s: could not compile regular expression '%s'",
cmd->directive->directive, rule);
}
if(he->size <= 0) {
return apr_psprintf(cmd->pool, "%s: size must be numeric value >0",
cmd->directive->directive);
}
apr_table_setn(sconf->hfilter_table, apr_pstrdup(cmd->pool, header), (char *)he);
return NULL;
}
const char *qos_resheaderfilter_rule_cmd(cmd_parms *cmd, void *dcfg,
const char *header,
const char *rule, const char *size) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
qos_fhlt_r_t *he;
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
he = apr_pcalloc(cmd->pool, sizeof(qos_fhlt_r_t));
he->size = atoi(size);
he->text = apr_pstrdup(cmd->pool, rule);
he->preg = ap_pregcomp(cmd->pool, rule, AP_REG_DOTALL);
he->action = QS_FLT_ACTION_DROP;
if(he->preg == NULL) {
return apr_psprintf(cmd->pool, "%s: could not compile regular expression '%s'",
cmd->directive->directive, rule);
}
if(he->size <= 0) {
return apr_psprintf(cmd->pool, "%s: size must be numeric value >0",
cmd->directive->directive);
}
apr_table_setn(sconf->reshfilter_table, apr_pstrdup(cmd->pool, header), (char *)he);
return NULL;
}
const char *qos_geodb_cmd(cmd_parms *cmd, void *dcfg, const char *arg1) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
char *msg = NULL;
int errors = 0;
qos_geo_t *geodb = apr_pcalloc(cmd->pool, sizeof(qos_geo_t));
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->geodb = geodb;
sconf->geodb->data = NULL;
sconf->geodb->path = ap_server_root_relative(cmd->pool, arg1);
sconf->geodb->size = 0;
if(qos_loadgeo(cmd->pool, sconf->geodb, &msg, &errors) != APR_SUCCESS) {
return apr_psprintf(cmd->pool, "%s: failed to load the database: %s"
" (total %d errors)",
cmd->directive->directive,
msg ? msg : "-",
errors);
}
return NULL;
}
const char *qos_geopriv_cmd(cmd_parms *cmd, void *dcfg, const char *list, const char *con,
const char *excludeUnknown) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
char *next = apr_pstrdup(cmd->pool, list);
int geo_limit;
char *name;
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
name = apr_strtok(next, ",", &next);
if(name == NULL) {
return apr_psprintf(cmd->pool, "%s: empty list",
cmd->directive->directive);
}
while(name) {
apr_table_set(sconf->geo_priv, name, "");
name = apr_strtok(NULL, ",", &next);
}
geo_limit = atoi(con);
if(geo_limit <= 0 && con[0] != '0' && con[1] != '\0') {
return apr_psprintf(cmd->pool, "%s: invalid connection number",
cmd->directive->directive);
}
if(sconf->geo_limit != -1 && sconf->geo_limit != geo_limit) {
return apr_psprintf(cmd->pool, "%s: already configured with a different limitation",
cmd->directive->directive);
}
if(excludeUnknown != NULL) {
if(strcasecmp(excludeUnknown, excludeUnknown) != 0) {
return apr_psprintf(cmd->pool, "%s: invalid argument %s",
cmd->directive->directive, excludeUnknown);
}
sconf->geo_excludeUnknown = 1;
}
sconf->geo_limit = geo_limit;
return NULL;
}
const char *qos_enable_ipv6_cmd(cmd_parms *cmd, void *dcfg, int flag) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
if(flag) {
sconf->ip_type = QS_IP_V6;
} else {
sconf->ip_type = QS_IP_V4;
}
return NULL;
}
const char *qos_client_ex_cmd(cmd_parms *cmd, void *dcfg, const char *addr) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
if(strlen(addr) == 0) {
return apr_psprintf(cmd->pool, "%s: invalid address",
cmd->directive->directive);
}
if(addr[strlen(addr)-1] == '.') {
/* address range */
apr_table_add(sconf->cc_exclude_ip, addr, "r");
} else if(addr[strlen(addr)-1] == ':') {
/* address range */
apr_table_add(sconf->cc_exclude_ip, addr, "r");
} else {
/* single ip */
apr_table_add(sconf->cc_exclude_ip, addr, "s");
}
return NULL;
}
const char *qos_client_cmd(cmd_parms *cmd, void *dcfg, const char *arg1) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->qos_cc_size = atoi(arg1);
#ifdef QS_INTERNAL_TEST
sconf->qos_cc_size = sconf->qos_cc_size / 100 * 100 ;
#else
sconf->qos_cc_size = sconf->qos_cc_size / 640 * 640 ;
#endif
if(sconf->qos_cc_size < 50000) {
m_qos_cc_partition = 2;
}
if(sconf->qos_cc_size >= 100000) {
m_qos_cc_partition = 8;
}
if(sconf->qos_cc_size >= 500000) {
m_qos_cc_partition = 16;
}
if(sconf->qos_cc_size >= 1000000) {
m_qos_cc_partition = 32;
}
if(sconf->qos_cc_size >= 4000000) {
m_qos_cc_partition = 64;
}
if(sconf->qos_cc_size <= 0 || sconf->qos_cc_size > 10000000) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value gearter than 640"
" and less than 10000000",
cmd->directive->directive);
}
return NULL;
}
#ifdef AP_TAKE_ARGV
const char *qos_client_pref_cmd(cmd_parms *cmd, void *dcfg, int argc, char *const argv[])
#else
const char *qos_client_pref_cmd(cmd_parms *cmd, void *dcfg)
#endif
{
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->has_qos_cc = 1;
sconf->qos_cc_prefer = 80;
#ifdef AP_TAKE_ARGV
if(argc) {
char *copy = apr_pstrdup(cmd->pool, argv[0]);
char *p = strchr(copy, '%');
if(p) {
p[0] = '\0';
}
sconf->qos_cc_prefer = atoi(copy);
}
#endif
if((sconf->qos_cc_prefer < 1) || (sconf->qos_cc_prefer > 99)) {
return apr_psprintf(cmd->pool, "%s: percentage must be a numeric value"
" between 1 and 99",
cmd->directive->directive);
}
#ifdef AP_TAKE_ARGV
if(argc > 1) {
return apr_psprintf(cmd->pool, "%s: command takes not more than one argument",
cmd->directive->directive);
}
#endif
return NULL;
}
const char *qos_client_block_cmd(cmd_parms *cmd, void *dcfg, const char *arg1,
const char *arg2) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->has_qos_cc = 1;
sconf->qos_cc_block = atoi(arg1);
if((sconf->qos_cc_block < 0) || sconf->qos_cc_block >= (USHRT_MAX-1) || ((sconf->qos_cc_block == 0) && (strcmp(arg1, "0") != 0))) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >=0 and <%d.",
cmd->directive->directive, USHRT_MAX-1);
}
if(arg2) {
sconf->qos_cc_blockTime = atoi(arg2);
}
if(sconf->qos_cc_blockTime == 0) {
return apr_psprintf(cmd->pool, "%s: time must be numeric value >0",
cmd->directive->directive);
}
return NULL;
}
const char *qos_client_limit_int_cmd(cmd_parms *cmd, void *dcfg, const char *arg_number,
const char *arg_sec, const char *arg_varname,
const char *arg_condition) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
char *limit_name = QS_LIMIT_DEFAULT;
int limit;
time_t limitTime = 600;
qos_s_entry_limit_conf_t *entry = apr_pcalloc(cmd->pool, sizeof(qos_s_entry_limit_conf_t));
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->has_qos_cc = 1;
limit = atoi(arg_number);
if((limit < 0) || limit >= (USHRT_MAX-1) || ((limit == 0) && (strcmp(arg_number, "0") != 0))) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >=0 and <%d.",
cmd->directive->directive, USHRT_MAX-1);
}
if(arg_sec) {
limitTime = atoi(arg_sec);
}
if(limitTime == 0) {
return apr_psprintf(cmd->pool, "%s: time must be numeric value >0",
cmd->directive->directive);
}
if(arg_varname) {
limit_name = apr_pstrdup(cmd->pool, arg_varname);
}
entry->limit = limit;
entry->limitTime = limitTime;
entry->condStr = NULL;
entry->preg = NULL;
if(arg_condition) {
entry->condStr = apr_pstrdup(cmd->pool, arg_condition);
entry->preg = ap_pregcomp(cmd->pool, entry->condStr, AP_REG_EXTENDED);
if(entry->preg == NULL) {
return apr_psprintf(cmd->pool, "%s: failed to compile regex (%s)",
cmd->directive->directive, entry->condStr);
}
}
if(apr_table_get(sconf->qos_cc_limitTable, limit_name) != NULL) {
return apr_psprintf(cmd->pool, "%s: variable %s has already been used by"
" another QS_[Cond]ClientEventLimitCount directive",
cmd->directive->directive, limit_name);
}
apr_table_setn(sconf->qos_cc_limitTable, limit_name, (char *)entry);
return NULL;
}
/* QS_ClientEventLimitCount <number> <seconds> <variable> */
const char *qos_client_limit_cmd(cmd_parms *cmd, void *dcfg, const char *arg_number,
const char *arg_sec, const char *arg_varname) {
return qos_client_limit_int_cmd(cmd, dcfg, arg_number, arg_sec, arg_varname, NULL);
}
#ifdef AP_TAKE_ARGV
/* QS_CondClientEventLimitCount <number> <seconds> <variable> <pattern> */
const char *qos_cond_client_limit_cmd(cmd_parms *cmd, void *dcfg, int argc, char *const argv[]) {
if(argc != 4) {
return apr_psprintf(cmd->pool, "%s: takes 4 arguments",
cmd->directive->directive);
}
return qos_client_limit_int_cmd(cmd, dcfg, argv[0], argv[1], argv[2], argv[3]);
}
#endif
const char *qos_client_forwardedfor_cmd(cmd_parms *cmd, void *dcfg, const char *header) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->qos_cc_forwardedfor = apr_pstrdup(cmd->pool, header);
return NULL;
}
const char *qos_client_serial_cmd(cmd_parms *cmd, void *dcfg) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->has_qos_cc = 1;
sconf->qos_cc_serialize = 1;
return NULL;
}
const char *qos_req_rate_tm_cmd(cmd_parms *cmd, void *dcfg, const char *arg1) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->qs_req_rate_tm= atoi(arg1);
if(sconf->qs_req_rate_tm < 2) {
return apr_psprintf(cmd->pool, "%s: must be numeric value between >1",
cmd->directive->directive);
}
return NULL;
}
const char *qos_client_tolerance_cmd(cmd_parms *cmd, void *dcfg, const char *arg1) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
char *value = apr_pstrdup(cmd->pool, arg1);
char *p = strchr(value, '%');
if(p) {
p[0] = '\0';
}
if (err != NULL) {
return err;
}
sconf->cc_tolerance = atoi(value);
if(sconf->cc_tolerance < 5 || sconf->cc_tolerance > 80) {
return apr_psprintf(cmd->pool, "%s: must be numeric value between 5 and 80",
cmd->directive->directive);
}
return NULL;
}
#ifdef AP_TAKE_ARGV
const char *qos_client_contenttype(cmd_parms *cmd, void *dcfg, int argc, char *const argv[]) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
if(argc != 5) {
return apr_psprintf(cmd->pool, "%s: requires five arguments",
cmd->directive->directive);
}
sconf->static_on = 1;
sconf->static_html = atol(argv[0]);
sconf->static_cssjs = atol(argv[1]);
sconf->static_img = atol(argv[2]);
sconf->static_other = atol(argv[3]);
sconf->static_notmodified = atol(argv[4]);
if(sconf->static_html == 0 ||
sconf->static_cssjs == 0 ||
sconf->static_img == 0 ||
sconf->static_other == 0 ||
sconf->static_notmodified == 0) {
return apr_psprintf(cmd->pool, "%s: requires numeric values greater than 0",
cmd->directive->directive);
} else {
unsigned long long s_all = sconf->static_html + sconf->static_img + sconf->static_cssjs +
sconf->static_other + sconf->static_notmodified;
unsigned long long s_2html = 100 * sconf->static_html / s_all;
unsigned long long s_2cssjs = 100 * sconf->static_cssjs / s_all;
unsigned long long s_2img = 100 * sconf->static_img / s_all;
unsigned long long s_2other = 100 * sconf->static_other / s_all;
unsigned long long s_2notmodified = 100 * sconf->static_notmodified / s_all;
sconf->static_html = s_2html;
sconf->static_cssjs = s_2cssjs;
sconf->static_img = s_2img;
sconf->static_other = s_2other;
sconf->static_notmodified = s_2notmodified;
}
return NULL;
}
#endif
const char *qos_client_event_cmd(cmd_parms *cmd, void *dcfg, const char *arg1) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->has_qos_cc = 1;
sconf->qos_cc_event = atoi(arg1);
if((sconf->qos_cc_event < 0) || ((sconf->qos_cc_event == 0) && (strcmp(arg1, "0") != 0))) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >=0",
cmd->directive->directive);
}
return NULL;
}
const char *qos_client_event_req_cmd(cmd_parms *cmd, void *dcfg, const char *arg1) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
sconf->has_qos_cc = 1;
sconf->qos_cc_event_req = atoi(arg1);
if((sconf->qos_cc_event_req < 0) || ((sconf->qos_cc_event_req == 0) && (strcmp(arg1, "0") != 0))) {
return apr_psprintf(cmd->pool, "%s: number must be numeric value >=0",
cmd->directive->directive);
}
return NULL;
}
const char *qos_disable_handler_cmd(cmd_parms *cmd, void *dcfg, int flag) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
sconf->disable_handler = flag;
return NULL;
}
#ifdef QS_INTERNAL_TEST
const char *qos_disable_int_ip_cmd(cmd_parms *cmd, void *dcfg, const char *arg) {
qos_srv_config *sconf = (qos_srv_config*)ap_get_module_config(cmd->server->module_config,
&qos_module);
if(strcasecmp(arg, "off") == 0 ) {
sconf->enable_testip = 0;
} else if(strcasecmp(arg, "on") == 0 ) {
sconf->enable_testip = 1;
} else {
sconf->enable_testip = 1;
m_qs_sim_ip_len = atoi(arg);
if(m_qs_sim_ip_len == 0) {
return apr_psprintf(cmd->pool, "%s: must be on/off or the number of IPs",
cmd->directive->directive);
}
}
return NULL;
}
#endif
static const command_rec qos_config_cmds[] = {
/* request limitation per location */
AP_INIT_TAKE1("QS_LocRequestLimitDefault", qos_loc_con_def_cmd, NULL,
RSRC_CONF,
"QS_LocRequestLimitDefault <number>, defines the default for the"
" QS_LocRequestLimit and QS_LocRequestLimitMatch directive."),
AP_INIT_TAKE2("QS_LocRequestLimit", qos_loc_con_cmd, NULL,
RSRC_CONF,
"QS_LocRequestLimit <location> <number>, defines the maximum number of"
" concurrent requests allowed to access the specified location. Default is defined by the"
" QS_LocRequestLimitDefault directive."),
AP_INIT_TAKE2("QS_LocRequestPerSecLimit", qos_loc_rs_cmd, NULL,
RSRC_CONF,
"QS_LocRequestPerSecLimit <location> <number>, defines the allowed"
" number of requests per second to a location. Requests are limited"
" by adding a delay to each requests. This directive should be used"
" in conjunction with QS_LocRequestLimit only."),
AP_INIT_TAKE2("QS_LocKBytesPerSecLimit", qos_loc_bs_cmd, NULL,
RSRC_CONF,
"QS_LocKBytesPerSecLimit <location> <kbytes>, defines the allowed"
" download bandwidth to the defined kbytes per second. Responses are"
"slowed by adding a delay to each response (non-linear, bigger files"
" get longer delay than smaller ones). This directive should be used"
" in conjunction with QS_LocRequestLimit only."),
AP_INIT_TAKE2("QS_LocRequestLimitMatch", qos_match_con_cmd, NULL,
RSRC_CONF,
"QS_LocRequestLimitMatch <regex> <number>, defines the number of"
" concurrent requests to the uri (path and query) pattern."
" Default is defined by the QS_LocRequestLimitDefault directive."),
AP_INIT_TAKE2("QS_LocRequestPerSecLimitMatch", qos_match_rs_cmd, NULL,
RSRC_CONF,
"QS_LocRequestPerSecLimitMatch <regex> <number>, defines the allowed"
" number of requests per second to the uri (path and query) pattern."
" Requests are limited by adding a delay to each requests."
" This directive should be used in conjunction with"
" QS_LocRequestLimitMatch only."),
AP_INIT_TAKE2("QS_LocKBytesPerSecLimitMatch", qos_match_bs_cmd, NULL,
RSRC_CONF,
"QS_LocKBytesPerSecLimitMatch <regex> <kbytes>, defines the allowed"
" download bandwidth to the location matching the defined URL (path"
" and query) pattern. Responses are slowed down"
" by adding a delay to each response (non-linear, bigger files"
" get longer delay than smaller ones). This directive should be used"
" in conjunction with QS_LocRequestLimitMatch only."),
/* conditional per location */
AP_INIT_TAKE3("QS_CondLocRequestLimitMatch", qos_cond_match_con_cmd, NULL,
RSRC_CONF,
"QS_CondLocRequestLimitMatch <regex> <number> <pattern>, defines the number of"
" concurrent requests to the uri (path and query) regex."
" Rule is only enforced if the "QS_COND" variable matches the specified"
" pattern (regex)."),
/* event based rules */
AP_INIT_TAKE2("QS_EventRequestLimit", qos_event_req_cmd, NULL,
RSRC_CONF,
"QS_EventRequestLimit <variable>[=<regex>] <number>, defines the"
" number of concurrent events. Directive works similar to"
" QS_LocRequestLimit, but counts the requests having the same"
" environment variable (and optionally matching its value, too)"
" rather than those that have the same URL pattern."),
AP_INIT_TAKE2("QS_EventPerSecLimit", qos_event_rs_cmd, NULL,
RSRC_CONF,
"QS_EventPerSecLimit [!]<variable> <number>, defines how"
" often requests may have the defined environment variable"
" (literal string) set. It measures the occurrences of the defined"
" environment variable on a request per seconds level and tries to"
" limit this occurrence to the defined number. It works similar to"
" as QS_LocRequestPerSecLimit, but counts only the requests with the"
" specified variable (or without it if the variable name is"
" prefixed by a '!'). If a request matches multiple events, the"
" rule with the lowest bandwidth is applied. Events are limited"
" by adding a delay to each request causing an event."),
AP_INIT_TAKE2("QS_EventKBytesPerSecLimit", qos_event_bps_cmd, NULL,
RSRC_CONF,
"QS_EventKBytesPerSecLimit [!]<variable> <kbytes>, throttles the"
" download bandwidth of all requests having the defined"
" variable set to the defined kbytes per second. Responses are slowed"
" by adding a delay to each response (non-linear, bigger files get"
" longer delay than smaller ones). By default, no limitation is active."
" This directive should be used in conjunction with QS_EventRequestLimit"
" only (you must use the same variable name for both directives)."),
AP_INIT_TAKE3("QS_EventLimitCount", qos_event_limit_cmd, NULL,
RSRC_CONF,
"QS_EventLimitCount <env-variable> <number> <seconds>,"
" defines the maximum number of events allowed within the defined"
" time. Requests are denied when reaching this limitation for the"
" specified time (blocked at request level)."),
#ifdef AP_TAKE_ARGV
AP_INIT_TAKE_ARGV("QS_CondEventLimitCount", qos_cond_event_limit_cmd, NULL,
RSRC_CONF,
"QS_CondEventLimitCount <env-variable> <number> <seconds> <pattern>,"
" same as QS_EventLimitCount but blocks requests only if the "QS_COND
" variable matches the specified pattern (regex)."),
#endif
/* server / connection limitation */
AP_INIT_TAKE1("QS_SrvMaxConn", qos_max_conn_cmd, NULL,
RSRC_CONF,
"QS_SrvMaxConn <number>, defines the maximum number of concurrent"
" TCP connections for this server (virtual host)."),
AP_INIT_TAKE1("QS_SrvMaxConnClose", qos_max_conn_close_cmd, NULL,
RSRC_CONF,
"QS_SrvMaxConnClose <number>[%], defines the maximum number of"
" concurrent TCP connections until the server disables"
" keep-alive for this server (closes the connection after"
" each requests. You may specify the number of connections"
" as a percentage of MaxClients if adding the suffix '%'"
" to the specified value."),
AP_INIT_TAKE12("QS_SrvMaxConnPerIP", qos_max_conn_ip_cmd, NULL,
RSRC_CONF,
"QS_SrvMaxConnPerIP <number> [<connections>], defines the maximum number"
" of connections per source IP address for this server (virtual host)."
" 'connections' defines the number of busy connections of the server"
" (all virtual hosts) to enable this limitation, default is 0."),
AP_INIT_TAKE1("QS_SrvMaxConnExcludeIP", qos_max_conn_ex_cmd, NULL,
RSRC_CONF,
"QS_SrvMaxConnExcludeIP <addr>, excludes an IP address or"
" address range from being limited."),
AP_INIT_FLAG("QS_SrvMaxConnPerIPIgnoreVIP", qos_max_conn_ip_vip_off_cmd, NULL,
RSRC_CONF,
"QS_SrvMinDataRateIgnoreVIP tells the QS_SrvMaxConnPerIP"
" directive to ignore (if set to \"on\") the VIP status"
" of clients. Default is \"off\", which means that"
" QS_SrvMaxConnPerIP is disabled for VIPs."),
AP_INIT_TAKE12("QS_SrvSerialize", qos_serialize_cmd, NULL,
RSRC_CONF,
"QS_SrvSerialize 'on'|'off' [<seconds>], ensures that not more than one request"
" having the "QS_SRVSERIALIZE" variable set is processed"
" at the same time by serializing them (process one after"
" each other)."),
#if APR_HAS_THREADS
AP_INIT_NO_ARGS("QS_SrvDataRateOff", qos_req_rate_off_cmd, NULL,
RSRC_CONF,
"QS_SrvDataRateOff,"
" disables the QS_SrvRequestRate and QS_SrvMinDataRate enforcement for"
" a virtual host (only port/address based but not for name based"
" virtual hosts)."),
AP_INIT_TAKE12("QS_SrvRequestRate", qos_req_rate_cmd, NULL,
RSRC_CONF,
"QS_SrvRequestRate <bytes per seconds> [<max bytes per second>],"
" defines the minimum upload"
" throughput a client must generate. See also QS_SrvMinDataRate."),
#ifdef AP_TAKE_ARGV
AP_INIT_TAKE_ARGV("QS_SrvMinDataRate", qos_min_rate_cmd, NULL,
RSRC_CONF,
"QS_SrvMinDataRate <bytes per seconds> [<max bytes per second> [<connections>]],"
" defines the minimum upload/download"
" throughput a client must generate (the bytes send/received by the client"
" per seconds). This bandwidth is measured while transmitting the data"
" (request line, header fields, request body, or response data). The"
" client connection get closed if the client does not fulfill the"
" required data rate and the IP address of the causing client get marked"
" in order to be handled with low priority (see the QS_ClientPrefer"
" directive)."
" The \"max bytes per second\" activates dynamic"
" minimum throughput control: The required minimal throughput"
" is increased in parallel to the number of concurrent clients"
" sending/receiving data. The \"max bytes per second\""
" setting is reached when the number of sending/receiving"
" clients is equal to the MaxClients setting."
" The \"connections\" argument is used to specify the"
" number of busy TCP connections a server must have to"
" enable this feature (0 by default)."
" No limitation is set by default."),
#else
AP_INIT_TAKE12("QS_SrvMinDataRate", qos_min_rate_cmd, NULL,
RSRC_CONF,
"QS_SrvMinDataRate <bytes per seconds> [<max bytes per second>],"
" defines the minimum upload/download throughput"
" a client must generate (the bytes send/received by the client"
" per seconds). This bandwidth is measured while transmitting the data"
" (request line, header fields, request body, or response data). The"
" client connection get closed if the client does not fulfill the"
" required data rate and the IP address of the causing client get marked"
" in order to be handled with low priority (see the QS_ClientPrefer"
" directive)."
" The \"max bytes per second\" activates dynamic"
" minimum throughput control: The required minimal throughput"
" is increased in parallel to the number of concurrent clients"
" sending/receiving data. The \"max bytes per second\""
" setting is reached when the number of sending/receiving"
" clients is equal to the MaxClients setting."
" No limitation is set by default."),
#endif // ARGV
AP_INIT_TAKE1("QS_SrvMinDataRateOffEvent", qos_min_rate_off_cmd, NULL,
RSRC_CONF|ACCESS_CONF,
"QS_SrvMinDataRateOffEvent '+'|'-'<env-variable>,"
" disables the minimal data rate enfocement (QS_SrvMinDataRate)"
" for a certain connection if the defined environment variable"
" has been set. The '+' prefix is used to add a variable"
" to the configuration while the '-' prefix is used"
" to remove a variable."),
AP_INIT_FLAG("QS_SrvMinDataRateIgnoreVIP", qos_min_rate_vip_off_cmd, NULL,
RSRC_CONF,
"QS_SrvMinDataRateIgnoreVIP tells the QS_SrvMinDataRate"
" directive to ignore (if set to \"on\") the VIP status"
" of clients. Default is \"off\", which means that"
" QS_SrvMinDataRate is disabled for VIPs."),
#endif // has threads
AP_INIT_TAKE1("QS_SrvSampleRate", qos_req_rate_tm_cmd, NULL,
RSRC_CONF,
"QS_SrvSampleRate <seconds>, defines the sampling rate used"
" by the QS_SrvMinDataRate directive to measure the"
" throughput of a connection."),
/* generic request filter */
AP_INIT_TAKE3("QS_DenyRequestLine", qos_deny_rql_cmd, NULL,
ACCESS_CONF,
"QS_DenyRequestLine '+'|'-'<id> 'log'|'deny' <regular expression>, generic"
" request line (method, path, query and protocol) filter used"
" to deny access for requests matching the defined regular expression."
" '+' adds a new rule while '-' removes a rule for a location."
" The action is either 'log' (access is granted but rule"
" match is logged) or 'deny' (access is denied)."),
AP_INIT_TAKE3("QS_DenyPath", qos_deny_path_cmd, NULL,
ACCESS_CONF,
"QS_DenyPath, same as QS_DenyRequestLine but applied to the"
" path only."),
AP_INIT_TAKE3("QS_DenyQuery", qos_deny_query_cmd, NULL,
ACCESS_CONF,
"QS_DenyQuery, same as QS_DenyRequestLine but applied to the"
" query only."),
AP_INIT_TAKE3("QS_DenyEvent", qos_deny_event_cmd, NULL,
ACCESS_CONF,
"QS_DenyEvent '+'|'-'<id> 'log'|'deny' [!]<variable>, matches"
" requests having the defined process"
" environment variable set (or NOT set if prefixed by a '!')."
" The action taken for matching rules"
" is either 'log' (access is granted but the rule match is"
" logged) or 'deny' (access is denied)."),
AP_INIT_TAKE3("QS_PermitUri", qos_permit_uri_cmd, NULL,
ACCESS_CONF,
"QS_PermitUri, '+'|'-'<id> 'log'|'deny' <regular expression>, generic"
" request filter applied to the request uri (path and query)."
" Only requests matching at least one QS_PermitUri pattern are"
" allowed. If a QS_PermitUri pattern has been defined an the"
" request does not match any rule, the request is denied albeit of"
" any server resource availability (allow list). All rules"
" must define the same action. Regular expression is case sensitive."),
AP_INIT_FLAG("QS_DenyBody", qos_denybody_cmd, NULL,
ACCESS_CONF,
"QS_DenyBody 'on'|'off', enabled body data filter (obsolete)."),
AP_INIT_FLAG("QS_DenyQueryBody", qos_denybody_d_cmd, NULL,
ACCESS_CONF,
"QS_DenyQueryBody 'on'|'off', enabled body data filter for QS_DenyQuery."),
AP_INIT_FLAG("QS_PermitUriBody", qos_denybody_p_cmd, NULL,
ACCESS_CONF,
"QS_PermitUriBody 'on'|'off', enabled body data filter for QS_PermitUriBody."),
AP_INIT_TAKE1("QS_InvalidUrlEncoding", qos_deny_urlenc_cmd, NULL,
ACCESS_CONF,
"QS_InvalidUrlEncoding 'log'|'deny'|'off',"
" enforces correct URL decoding in conjunction with the"
" QS_DenyRequestLine, QS_DenyPath, and QS_DenyQuery"
" directives. Default is \"off\"."),
AP_INIT_TAKE1("QS_LimitRequestBody", qos_maxpost_cmd, NULL,
ACCESS_CONF|RSRC_CONF,
"QS_LimitRequestBody <bytes>, limits the allowed size"
" of an HTTP request message body."),
AP_INIT_ITERATE("QS_Decoding", qos_dec_cmd, NULL,
ACCESS_CONF,
"QS_DenyDecoding 'uni', enabled additional string decoding"
" functions which are applied before"
" matching QS_Deny* and QS_Permit* directives."
" Default is URL decoding (%xx, \\xHH, '+')."),
AP_INIT_NO_ARGS("QS_DenyInheritanceOff", qos_denyinheritoff_cmd, NULL,
ACCESS_CONF,
"QS_DenyInheritanceOff, disable inheritance of QS_Deny* and QS_Permit*"
" directives to a location."),
AP_INIT_TAKE1("QS_RequestHeaderFilter", qos_headerfilter_cmd, NULL,
RSRC_CONF|ACCESS_CONF,
"QS_RequestHeaderFilter 'on'|'off'|'size', filters request headers by allowing"
" only these headers which match the request header rules defined by"
" mod_qos. Request headers which do not conform these definitions"
" are either dropped or the whole request is denied. Custom"
" request headers may be added by the QS_RequestHeaderFilterRule"
" directive. Using the 'size' option, the header field max. size"
" is verified only (similar to LimitRequestFieldsize but using"
" individual values for each header type) while the pattern is ignored."),
AP_INIT_TAKE1("QS_ResponseHeaderFilter", qos_resheaderfilter_cmd, NULL,
ACCESS_CONF,
"QS_ResponseHeaderFilter 'on'|'off', filters response headers by allowing"
" only these headers which match the response header rules defined by"
" mod_qos. Response headers which do not conform these definitions"
" are dropped."),
#ifdef AP_TAKE_ARGV
AP_INIT_TAKE_ARGV("QS_RequestHeaderFilterRule", qos_headerfilter_rule_cmd, NULL,
RSRC_CONF,
"QS_RequestHeaderFilterRule <header name> 'drop'|'deny' <regular expression> <size>, used"
" to add custom request header filter rules which override the internal"
" filter rules of mod_qos."
" Directive is allowed in global server context only."),
#else
AP_INIT_TAKE3("QS_RequestHeaderFilterRule", qos_headerfilter_rule_cmd, NULL,
RSRC_CONF,
"QS_RequestHeaderFilterRule <header name> 'drop'|'deny' <regular expression>, used"
" to add custom request header filter rules which override the internal"
" filter rules of mod_qos."
" Directive is allowed in global server context only."),
#endif
AP_INIT_TAKE3("QS_ResponseHeaderFilterRule", qos_resheaderfilter_rule_cmd, NULL,
RSRC_CONF,
"QS_ResponseHeaderFilterRule <header name> <regular expression> <size>, used"
" to add custom response header filter rules which override the internal"
" filter rules of mod_qos."
" Directive is allowed in global server context only."),
/* milestones */
AP_INIT_TAKE23("QS_MileStone", qos_milestone_cmd, NULL,
RSRC_CONF,
"QS_MileStone 'log'|'deny' <pattern> [<thinktime>], defines request line patterns"
" a client must access in the defined order as they are defined in the"
" configuration file."),
AP_INIT_TAKE1("QS_MileStoneTimeout", qos_milestone_tmo_cmd, NULL,
RSRC_CONF,
"QS_MileStoneTimeout <seconds>, defines the time in seconds"
" within a client must reach the next milestone."
" Default are 3600 seconds."),
/* session / vip */
AP_INIT_TAKE1("QS_SessionCookieName", qos_cookie_name_cmd, NULL,
RSRC_CONF,
"QS_SessionCookieName <name>, defines a custom session cookie name,"
" default is "QOS_COOKIE_NAME"."),
AP_INIT_TAKE1("QS_SessionCookiePath", qos_cookie_path_cmd, NULL,
RSRC_CONF,
"QS_SessionCookiePath <path>, defines the cookie path, default is \"/\"."),
AP_INIT_TAKE1("QS_SessionTimeout", qos_timeout_cmd, NULL,
RSRC_CONF,
"QS_SessionTimeout <seconds>, defines the session life time for a VIP."
" It is only used for session based (cookie) VIP identification (not"
" for IP based). Default is "QOS_MAX_AGE" seconds."),
AP_INIT_TAKE1("QS_SessionKey", qos_key_cmd, NULL,
RSRC_CONF,
"QS_SessionKey <string>, secret key used for cookie encryption."
" Used when using the same session cookie for multiple web servers"
" (load balancing) or sessions should survive a server restart."
" By default, a random key is used which changes every server restart."),
AP_INIT_TAKE12("QS_VipHeaderName", qos_header_name_cmd, NULL,
RSRC_CONF,
"QS_VipHeaderName <name>[=<regex>] [drop], defines an HTTP"
" response header which marks a user as a VIP. mod_qos"
" creates a session for this user by setting a cookie,"
" e.g., after successful user authentication. Tests"
" optionally its value against the provided regular"
" expression. Specify the action 'drop' if you want mod_qos"
" to remove this control header from the HTTP response."),
AP_INIT_TAKE12("QS_VipIPHeaderName", qos_ip_header_name_cmd, NULL,
RSRC_CONF,
"QS_VipIPHeaderName <name>[=<regex>] [drop], defines an HTTP"
" response header which marks a client source IP address as"
" a VIP. Tests optionally its value against the provided"
" regular expression."
" Specify the action 'drop' if you want mod_qos to remove"
" this control header from the HTTP response."),
AP_INIT_NO_ARGS("QS_VipUser", qos_vip_u_cmd, NULL,
RSRC_CONF,
"QS_VipUser, creates a VIP session for users which have been"
" authenticated by the Apache server, e.g., by the standard"
" mod_auth* modules. It works similar to the"
" QS_VipHeaderName directive."),
AP_INIT_NO_ARGS("QS_VipIpUser", qos_vip_ip_u_cmd, NULL,
RSRC_CONF,
"QS_VipIpUser, marks a source IP address as a VIP if the"
" user has been authenticated by the Apache server, e.g."
" by the standard mod_auth* modules. It works similar to"
" the QS_VipIPHeaderName directive."),
/* user tracking */
#ifdef AP_TAKE_ARGV
AP_INIT_TAKE_ARGV("QS_UserTrackingCookieName", qos_user_tracking_cookie_cmd, NULL,
RSRC_CONF,
"QS_UserTrackingCookieName <name> [<path>] [<domain>] ['session'] ['jsredirect'],"
" enables the user tracking cookie by defining a cookie"
" name. The \"path\" parameter is an option cookie"
" check page which is used to ensure the client accepts"
" cookies. The \"domain\" option defines the Domain attriibute"
" for the Set-Cookie header. The option \"session\" indicates"
" that the cookie shall be a session cookie expiring when the"
" user closes it's browser."
" User tracking requires mod_unique_id."
" This feature is disabled by default."
" Ignores QS_LogOnly."),
#else
AP_INIT_TAKE123("QS_UserTrackingCookieName", qos_user_tracking_cookie_cmd, NULL,
RSRC_CONF,
"QS_UserTrackingCookieName <name> [<path>] ['session'],"
" enables the user tracking cookie by defining a cookie"
" name. The \"path\" parameter is an option cookie"
" check page which is used to ensure the client accepts"
" cookies. The option \"session\" indicates that the"
" cookie shall be a session cookie expiring when the"
" user closes it's browser."
" User tracking requires mod_unique_id."
" This feature is disabled by default."
" Ignores QS_LogOnly."),
#endif
/* env vars */
AP_INIT_TAKE23("QS_SetEnvIf", qos_event_setenvif_cmd, NULL,
RSRC_CONF|ACCESS_CONF,
"QS_SetEnvIf [!]<variable1>[=<regex>] [[!]<variable2>] [!]<variable=value>,"
" sets (or unsets) the 'variable=value' (literal string) if"
" variable1 (literal string) AND variable2 (literal string)"
" are set in the request environment variable list (not case"
" sensitive). This is used to combine multiple variables"
" to a new event type. Alternatively, a regular expression"
" can be specified for variable1's value and variable2 must be"
" omitted in order to simply set a new variable if"
" the regular expression matches."),
AP_INIT_TAKE_ARGV("QS_SetEnvIfCmp", qos_cmp_cmd, NULL,
ACCESS_CONF,
"QS_SetEnvIfCmpP <env-variable1> eq|ne|gt|lt <env-variable2> [!]<env-variable>[=<value>],"
" sets the specified environment variable if the specified env-variables"
" are alphabetically or numerical equal (eq), not equal (ne),"
" greater (gt), less (lt)."),
AP_INIT_TAKE2("QS_SetEnvIfQuery", qos_event_setenvifquery_cmd, NULL,
RSRC_CONF|ACCESS_CONF,
"QS_SetEnvIfQuery <regex> [!]<variable>[=value],"
" directive works quite similar to the SetEnvIf directive"
" of the Apache module mod_setenvif, but the specified regex"
" is applied against the query string portion of the request"
" line. The directive recognizes the occurrences of $1..$9"
" within value and replaces them by the sub-expressions of"
" the defined regex pattern."),
AP_INIT_TAKE2("QS_SetEnvIfParp", qos_event_setenvifparp_cmd, NULL,
RSRC_CONF,
"QS_SetEnvIfParp <regex> [!]<variable>[=value],"
" directive parsing the request payload using the Apache module"
" mod_parp. It matches the request URL query and the HTTP"
" request message body data as well ('application/x-www-form-urlencoded',"
" 'multipart/form-data', and 'multipart/mixed') and sets the defined"
" process variable (quite similar to the QS_SetEnvIfQuery directive)."
" The directive recognizes the occurrences of $1..$9 within value"
" and replaces them by the sub-expressions of the defined regex"
" pattern. This directive activates mod_parp for every request to"
" the virtual host. You may deactivate mod_parp for selected requests"
" using the SetEnvIf directive: unset the variable 'parp' to do so."
" Important: request message body processing requires that the server"
" loads the whole request into its memory (at least twice the length"
" of the message). You should limit the allowed size of the HTTP"
" request message body using the QS_LimitRequestBody directive"
" when using QS_SetEnvIfParp!"),
AP_INIT_TAKE2("QS_SetEnvIfBody", qos_event_setenvifparpbody_cmd, NULL,
RSRC_CONF,
"QS_SetEnvIfBody <regex> [!]<variable>[=value],"
" parses the request body using the Apache module mod_parp."
" Specify the content types to process using the mod_parp"
" directive PARP_BodyData and ensure that mod_parp is enabled"
" using the SetEnvIf directive of the Apache module mod_setenvif."
" You should limit the allowed size of HTTP requests message body"
" using the QS_LimitRequestBody directive when using mod_parp."
" The directive recognizes the occurrence of $1 within the variable"
" value and replaces it by the sub-expressions of the defined regex"
" pattern."),
AP_INIT_TAKE2("QS_SetEnvStatus", qos_event_setenvifstatus_cmd, NULL,
RSRC_CONF|ACCESS_CONF,
"QS_SetEnvStatus (deprecated, use QS_SetEnvIfStatus)"),
AP_INIT_TAKE2("QS_SetEnvIfStatus", qos_event_setenvifstatus_cmd, NULL,
RSRC_CONF|ACCESS_CONF,
"QS_SetEnvIfStatus <status code> <variable>, adds the defined"
" request environment variable if the HTTP status code matches the"
" defined value. The value '"QS_CLOSE"' may be used as a special"
" status code to set a "QS_BLOCK" event in order to handle"
" connection close events caused by "QS_CLOSE" rules while"
" the status '"QS_EMPTY_CON"' may be used to mark connections"
" which are closed before any HTTP request has ever been received."
" The '"QS_MAXIP"' value may be used to count "QS_BLOCK" events for"
" connections closed by the "QS_MAXIP" directive."
" The '"QS_BROKEN_CON"' value may be used to mark clients not"
" reading the full HTTP response."),
AP_INIT_TAKE2("QS_SetEnvResBody", qos_event_setenvifresbody_cmd, NULL,
ACCESS_CONF,
"QS_SetEnvResBody (deprecated, use QS_SetEnvIfResBody)"),
AP_INIT_TAKE2("QS_SetEnvIfResBody", qos_event_setenvifresbody_cmd, NULL,
ACCESS_CONF,
"QS_SetEnvIfResBody <string> [!]<variable>, adds the defined"
" request environment variable (e.g. "QS_BLOCK") if the HTTP"
" response body contains the defined literal string."
" Supports only one pattern per location."),
AP_INIT_TAKE2("QS_SetEnv", qos_setenv_cmd, NULL,
RSRC_CONF,
"QS_SetEnv <variable> <value>, sets the defined variable"
" with the value where the value string may contain"
" other environment variables surrounded by \"${\" and \"}\"."
" The variable is only set if all defined variables within"
" the value can be resolved."),
AP_INIT_TAKE23("QS_SetReqHeader", qos_setreqheader_cmd, NULL,
RSRC_CONF,
"QS_SetReqHeader [!]<header name> <variable> ['late'], sets the defined"
" HTTP request header to the request if the specified"
" environment variable is set."),
AP_INIT_TAKE1("QS_UnsetReqHeader", qos_unsetreqheader_cmd, NULL,
RSRC_CONF,
"QS_UnsetReqHeader <header name>, Removes the specified header from the request."),
AP_INIT_TAKE1("QS_UnsetResHeader", qos_unsetresheader_cmd, NULL,
RSRC_CONF,
"QS_UnsetResHeader <header name>, Removes the specified header from the response."),
AP_INIT_TAKE12("QS_SetEnvResHeader", qos_event_setenvresheader_cmd, NULL,
RSRC_CONF,
"QS_SetEnvResHeader <header name> [drop], sets the defined"
" HTTP response header (name and value) to the request environment variables"
" Deletes the header if the action 'drop' has been specified."),
AP_INIT_TAKE2("QS_SetEnvResHeaderMatch", qos_event_setenvresheadermatch_cmd, NULL,
RSRC_CONF,
"QS_SetEnvResHeaderMatch <header name> <regex>, sets the defined"
" HTTP response header (name and value) to the request environment variables"
" if the specified regular expression matches the header value."),
AP_INIT_TAKE3("QS_SetEnvRes", qos_setenvres_cmd, NULL,
RSRC_CONF,
"QS_SetEnvRes <variable> <regex> <variable2>[=<value>], sets the environment"
" variable2 if the regular expression matches against the value of"
" the environment variable. Occurrences of $1..$9 within the value"
" and replace them by parenthesized subexpressions of the regular expression."),
AP_INIT_TAKE3("QS_RedirectIf", qos_redirectif_cmd, NULL,
RSRC_CONF|ACCESS_CONF,
"QS_RedirectIf <variable> <regex> [<code>:]<url>,"
" redirects the client to the configured url"
" if the regular expression matches"
" the value of the the environment variable."),
/* client control */
AP_INIT_TAKE1("QS_ClientEntries", qos_client_cmd, NULL,
RSRC_CONF,
"QS_ClientEntries <number>, defines the number of individual"
" clients managed by mod_qos. Default is 50000."
" Directive is allowed in global server context only."),
#ifdef AP_TAKE_ARGV
AP_INIT_TAKE_ARGV("QS_ClientPrefer", qos_client_pref_cmd, NULL,
RSRC_CONF,
"QS_ClientPrefer [<percent>], prefers known VIP clients"
" when server has less than 80% (or the configured value)"
" of free TCP connections. Preferred clients"
" are VIP clients (or those without any negative penalties),"
" see QS_VipHeaderName directive."
" Directive is allowed in global server context only."),
#else
AP_INIT_NO_ARGS("QS_ClientPrefer", qos_client_pref_cmd, NULL,
RSRC_CONF,
"QS_ClientPrefer, prefers known VIP clients"
" when server has less than 80% of free TCP connections."
" Preferred clients are VIP clients only,"
" see QS_VipHeaderName directive."
" Directive is allowed in global server context only."),
#endif
AP_INIT_TAKE1("QS_ClientTolerance", qos_client_tolerance_cmd, NULL,
RSRC_CONF,
"QS_ClientTolerance <percent>, defines the allowed tolerance (variation)"
" from a \"normal\" client (average) in percent."
" Default is "QOS_CC_BEHAVIOR_TOLERANCE_STR"%."
" Directive is allowed in global server context only."),
#ifdef AP_TAKE_ARGV
AP_INIT_TAKE_ARGV("QS_ClientContentTypes", qos_client_contenttype, NULL,
RSRC_CONF,
"QS_ClientContentTypes <html> <css/js> <images> <other> <304>,"
" defines the distribution of HTTP response content types a client normally"
" receives when accessing the server. mod_qos normally learns the average"
" behavior automatically by default but you may specify a static configuration"
" in order to avoid influences by a high number of abnormal clients."),
#endif
AP_INIT_TAKE12("QS_ClientEventBlockCount", qos_client_block_cmd, NULL,
RSRC_CONF,
"QS_ClientEventBlockCount <number> [<seconds>], defines the maximum number"
" of "QS_BLOCK" allowed within the defined time (default are 10 minutes)."
" Directive is allowed in global server context only."),
AP_INIT_TAKE1("QS_ClientEventBlockExcludeIP", qos_client_ex_cmd, NULL,
RSRC_CONF,
"QS_ClientEventBlockExcludeIP <addr>, excludes an IP address or"
" address range from being limited by QS_ClientEventBlockCount."),
AP_INIT_TAKE123("QS_ClientEventLimitCount", qos_client_limit_cmd, NULL,
RSRC_CONF,
"QS_ClientEventLimitCount <number> [<seconds> [<variable>]],"
" defines the maximum number"
" of the specified environment variable ("QS_LIMIT_DEFAULT" by default)"
" allowed within the defined time (default are 10 minutes)."
" Directive is allowed in global server context only."),
#ifdef AP_TAKE_ARGV
AP_INIT_TAKE_ARGV("QS_CondClientEventLimitCount", qos_cond_client_limit_cmd, NULL,
RSRC_CONF,
"QS_CondClientEventLimitCount <number> <seconds> <variable> <pattern>,"
" defines the maximum number"
" of the specified environment variable"
" allowed within the defined time."
" Directive works similar as QS_ClientEventLimitCount but"
" requests are only blocked if the "QS_COND" variable matches"
" the defined pattern (regex)."
" Directive is allowed in global server context only."),
#endif
AP_INIT_TAKE1("QS_ClientEventPerSecLimit", qos_client_event_cmd, NULL,
RSRC_CONF,
"QS_ClientEventPerSecLimit <number>, defines the number"
" events pro seconds on a per client (source IP) basis."
" Events are identified by requests having the"
" "QS_EVENT" variable set."
" Directive is allowed in global server context only."),
AP_INIT_TAKE1("QS_ClientEventRequestLimit", qos_client_event_req_cmd, NULL,
RSRC_CONF,
"QS_ClientEventRequestLimit <number>, defines the allowed"
" number of concurrent requests coming from the same client"
" source IP address"
" having the QS_EventRequest variable set."
" Directive is allowed in global server context only."),
AP_INIT_NO_ARGS("QS_ClientSerialize", qos_client_serial_cmd, NULL,
RSRC_CONF,
"QS_ClientSerialize, serializes requests having the "QS_SERIALIZE" variable"
" set if they are coming from the same IP address."),
AP_INIT_TAKE1("QS_ClientIpFromHeader", qos_client_forwardedfor_cmd, NULL,
RSRC_CONF,
"QS_ClientIpFromHeader <header>, defines a HTTP request header to read"
" the client's source IP address from (instead of taking the IP address"
" of the client opening the TCP connection). This may be used for the"
" QS_ClientEventLimitCount directive and QS_Country variable."),
/* geo ip */
AP_INIT_TAKE1("QS_ClientGeoCountryDB", qos_geodb_cmd, NULL,
RSRC_CONF,
"QS_ClientGeoCountryDB <path>, path to the geograpical database file."),
AP_INIT_TAKE23("QS_ClientGeoCountryPriv", qos_geopriv_cmd, NULL,
RSRC_CONF,
"QS_ClientGeoCountryPriv <list> <connections> ['excludeUnknown'],"
" defines a comma separated list of country codes"
" for origin client IP address which are allowed to"
" access the server if the number of busy TCP connections reaches"
" the defined number of connections while others are denied access."
" Clients whose IP can't be mapped to a country code can be excluded"
" from the limitation by configuring the 'excludeUnknown' argument."),
/* error documents */
AP_INIT_TAKE1("QS_ErrorPage", qos_error_page_cmd, NULL,
RSRC_CONF,
"QS_ErrorPage <url>, defines a custom error page."),
AP_INIT_TAKE1("QS_ErrorResponseCode", qos_error_code_cmd, NULL,
RSRC_CONF,
"QS_ErrorResponseCode <code>, defines the HTTP response code which"
" is used when a request is denied, default is 500."),
AP_INIT_FLAG("QS_ForcedClose", qos_forced_close_cmd, NULL,
RSRC_CONF,
"QS_ForcedClose 'on'|'off', defines if mod_qos connection handler shall"
" exit with an error code (on) or not. Default is on (except for"
" Apache 2.4.49)."),
/* module settings / various stuff */
AP_INIT_FLAG("QS_LogOnly", qos_logonly_cmd, NULL,
RSRC_CONF,
"QS_LogOnly 'on'|'off', enables the log only mode of the module"
" where no limitations are enforced. Default is off."
" Directive is allowed in global server context only."),
AP_INIT_FLAG("QS_LogEnv", qos_logenv_cmd, NULL,
RSRC_CONF,
"QS_LogEnv 'on'|'off', enables logging of environment"
" variables."),
AP_INIT_FLAG("QS_SupportIPv6", qos_enable_ipv6_cmd, NULL,
RSRC_CONF,
"QS_SupportIPv6 'on'|'off', enables IPv6 address support."
" Default is on."),
AP_INIT_TAKE1("QS_SemMemFile", qos_mfile_cmd, NULL,
RSRC_CONF,
"QS_SemMemFile <path>, optional path to a directory or file"
" which shall be used for file based semaphores/shared memory"
" usage, e.g. /var/tmp."),
AP_INIT_TAKE1("QS_MaxClients", qos_maxclients_cmd, NULL,
RSRC_CONF,
"QS_MaxClients <number>, optional override for mod_qos's"
" MaxClients/MaxRequestWorkers calculation which defines"
" the maximum number of TCP connections the server can handle."),
AP_INIT_FLAG("QS_DisableHandler", qos_disable_handler_cmd, NULL,
RSRC_CONF,
"QS_DisableHandler 'on'|'off', disables the qos-viewer"
" and qos-console for a virtual host"),
#if APR_HAS_THREADS
AP_INIT_FLAG("QS_Status", qos_qsstatus_cmd, NULL,
RSRC_CONF,
"QS_Status 'on'|'off', writes a log message containing server"
" statistics once every minute. Default is off."),
#endif
AP_INIT_FLAG("QS_EventCount", qos_qsevents_cmd, NULL,
RSRC_CONF,
"QS_EventCount 'on'|'off', enables error event counting"
" (counters are shown in the machine-readable version"
" of the status viewer). Default is off."),
AP_INIT_TAKE1("QSLog", qos_qlog_cmd, NULL,
RSRC_CONF,
"QSLog <arg>, used to configure a global (per Apache"
" instance) 'qslog' logger."),
#ifdef QS_INTERNAL_TEST
AP_INIT_TAKE1("QS_EnableInternalIPSimulation", qos_disable_int_ip_cmd, NULL,
RSRC_CONF,
""),
#endif
{ NULL }
};
/************************************************************************
* apache register
***********************************************************************/
static void qos_register_hooks(apr_pool_t * p) {
static const char *pre[] = { "mod_setenvif.c", "mod_setenvifplus.c", "mod_parp.c", NULL };
static const char *preuid[] = { "mod_setenvif.c", "mod_setenvifplus.c", "mod_parp.c", "mod_unique_id.c", NULL };
static const char *pressl[] = { "mod_ssl.c", NULL };
static const char *preconf[] = { "mod_setenvif.c", "mod_setenvifplus.c", "mod_parp.c", "mod_ssl.c", NULL };
static const char *post[] = { "mod_setenvif.c", "mod_setenvifplus.c", NULL };
static const char *postlog[] = { "mod_logio.c", NULL };
static const char *parp[] = { "mod_parp.c", NULL };
static const char *prelast[] = { "mod_setenvif.c", "mod_setenvifplus.c", "mod_ssl.c", NULL };
static const char *preFix[] = { "mod_ssl.c", "mod_setenvifplus.c", NULL };
ap_hook_post_config(qos_post_config, preconf, NULL, APR_HOOK_MIDDLE);
ap_hook_child_init(qos_child_init, NULL, NULL, APR_HOOK_MIDDLE);
// before ssl_hook_pre_connection@APR_HOOK_MIDDLE but after logio_pre_conn@APR_HOOK_MIDDLE
ap_hook_pre_connection(qos_pre_connection, postlog, pressl, APR_HOOK_MIDDLE);
// after ssl_hook_pre_connection@APR_HOOK_MIDDLE (and a many others)
ap_hook_pre_connection(qos_pre_process_connection, prelast, NULL, APR_HOOK_LAST);
ap_hook_process_connection(qos_process_connection, NULL, NULL, APR_HOOK_MIDDLE);
// be before sp_post_read_request@APR_HOOK_MIDDLE
ap_hook_post_read_request(qos_post_read_request, NULL, post, APR_HOOK_MIDDLE);
ap_hook_post_read_request(qos_post_read_request_later, preuid, NULL, APR_HOOK_MIDDLE);
ap_hook_header_parser(qos_header_parser0, NULL, post, APR_HOOK_FIRST);
ap_hook_header_parser(qos_header_parser1, post, parp, APR_HOOK_FIRST);
ap_hook_header_parser(qos_header_parser, pre, NULL, APR_HOOK_MIDDLE);
ap_hook_fixups(qos_fixup, preFix, NULL, APR_HOOK_MIDDLE);
ap_hook_handler(qos_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_log_transaction(qos_logger, NULL, NULL, APR_HOOK_FIRST);
//ap_hook_error_log(qos_error_log, NULL, NULL, APR_HOOK_LAST);
ap_register_input_filter("qos-in-filter", qos_in_filter, NULL, AP_FTYPE_CONNECTION);
ap_register_input_filter("qos-in-filter2", qos_in_filter2, NULL, AP_FTYPE_RESOURCE);
ap_register_input_filter("qos-in-filter3", qos_in_filter3, NULL, AP_FTYPE_CONTENT_SET);
/* AP_FTYPE_RESOURCE+1 ensures the filter is executed after mod_setenvifplus
* AP_FTYPE_PROTOCOL+3 ensures the filter is executed after mod_deflate */
ap_register_output_filter("qos-out-filter", qos_out_filter, NULL, AP_FTYPE_RESOURCE+1);
ap_register_output_filter("qos-out-filter-min", qos_out_filter_min, NULL, AP_FTYPE_RESOURCE+1);
ap_register_output_filter("qos-out-filter-delay", qos_out_filter_delay, NULL, AP_FTYPE_PROTOCOL+3);
ap_register_output_filter("qos-out-filter-body", qos_out_filter_body, NULL, AP_FTYPE_RESOURCE+1);
ap_register_output_filter("qos-out-filter-brokencon", qos_out_filter_brokencon, NULL, AP_FTYPE_PROTOCOL+3);
ap_register_output_filter("qos-out-err-filter", qos_out_err_filter, NULL, AP_FTYPE_RESOURCE+1);
ap_hook_insert_filter(qos_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_insert_error_filter(qos_insert_err_filter, NULL, NULL, APR_HOOK_MIDDLE);
}
/************************************************************************
* apache module definition
***********************************************************************/
module AP_MODULE_DECLARE_DATA qos_module ={
STANDARD20_MODULE_STUFF,
qos_dir_config_create, /**< dir config creator */
qos_dir_config_merge, /**< dir merger */
qos_srv_config_create, /**< server config */
qos_srv_config_merge, /**< server merger */
qos_config_cmds, /**< command table */
qos_register_hooks, /**< hook registration */
};
|