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
|
/*
Copyright 2019 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <verify_packages.h>
#include <verify_new_packages.h>
#include <package_module.h>
#include <actuator.h>
#include <promises.h>
#include <dir.h>
#include <files_names.h>
#include <files_interfaces.h>
#include <file_lib.h>
#include <vars.h>
#include <conversion.h>
#include <expand.h>
#include <scope.h>
#include <vercmp.h>
#include <matching.h>
#include <match_scope.h>
#include <attributes.h>
#include <string_lib.h>
#include <pipes.h>
#include <locks.h>
#include <exec_tools.h>
#include <policy.h>
#include <misc_lib.h>
#include <rlist.h>
#include <ornaments.h>
#include <eval_context.h>
#include <retcode.h>
#include <known_dirs.h>
#include <csv_writer.h>
#include <cf-agent-enterprise-stubs.h>
#include <cf-windows-functions.h>
/* Called structure:
Top-level: cf-agent calls...
* CleanScheduledPackages
* VerifyPackagesPromise -> all the Verify* functions that schedule operation
* ExecuteScheduledPackages -> all the Execute* functions to run operations
*/
/** Entry points from VerifyPackagesPromise **/
#define REPORT_THIS_PROMISE(__pp) (strncmp(__pp->promiser, "cfe_internal_", 13) != 0)
#define cfPS_HELPER_0ARG(__ctx, __log_level, __result, __pp, __a, __str) \
if (REPORT_THIS_PROMISE(__pp)) \
{ \
cfPS(__ctx, __log_level, __result, __pp, __a, __str); \
}
#define cfPS_HELPER_1ARG(__ctx, __log_level, __result, __pp, __a, __str, __arg1) \
if (REPORT_THIS_PROMISE(__pp)) \
{ \
cfPS(__ctx, __log_level, __result, __pp, __a, __str, __arg1); \
}
#define cfPS_HELPER_2ARG(__ctx, __log_level, __result, __pp, __a, __str, __arg1, __arg2) \
if (REPORT_THIS_PROMISE(__pp)) \
{ \
cfPS(__ctx, __log_level, __result, __pp, __a, __str, __arg1, __arg2); \
}
#define cfPS_HELPER_3ARG(__ctx, __log_level, __result, __pp, __a, __str, __arg1, __arg2, __arg3) \
if (REPORT_THIS_PROMISE(__pp)) \
{ \
cfPS(__ctx, __log_level, __result, __pp, __a, __str, __arg1, __arg2, __arg3); \
}
#define PromiseResultUpdate_HELPER(__pp, __prior, __evidence) \
REPORT_THIS_PROMISE(__pp) ? PromiseResultUpdate(__prior, __evidence) : __evidence
typedef enum
{
PACKAGE_PROMISE_TYPE_OLD = 0,
PACKAGE_PROMISE_TYPE_NEW,
PACKAGE_PROMISE_TYPE_MIXED,
PACKAGE_PROMISE_TYPE_OLD_ERROR,
PACKAGE_PROMISE_TYPE_NEW_ERROR
} PackagePromiseType;
static bool PackageSanityCheck(EvalContext *ctx, const Attributes *a, const Promise *pp);
static bool VerifyInstalledPackages(EvalContext *ctx, PackageManager **alllists, const char *default_arch, const Attributes *a, const Promise *pp, PromiseResult *result);
static PromiseResult VerifyPromisedPackage(EvalContext *ctx, const Attributes *a, const Promise *pp);
static PromiseResult VerifyPromisedPatch(EvalContext *ctx, const Attributes *a, const Promise *pp);
/** Utils **/
static char *GetDefaultArch(const char *command);
static bool ExecPackageCommand(EvalContext *ctx, char *command, int verify, int setCmdClasses, const Attributes *a, const Promise *pp, PromiseResult *result);
static bool PrependPatchItem(EvalContext *ctx, PackageItem ** list, char *item, PackageItem * chklist, const char *default_arch, const Attributes *a, const Promise *pp);
static bool PrependMultiLinePackageItem(EvalContext *ctx, PackageItem ** list, char *item, int reset, const char *default_arch, const Attributes *a, const Promise *pp);
static bool PrependListPackageItem(EvalContext *ctx, PackageItem ** list, char *item, const char *default_arch, const Attributes *a, const Promise *pp);
static PackageManager *GetPackageManager(PackageManager **lists, char *mgr, PackageAction pa, PackageActionPolicy x);
static void DeletePackageManagers(PackageManager *morituri);
static const char *PrefixLocalRepository(const Rlist *repositories, const char *package);
static PromiseResult HandleOldPackagePromiseType(EvalContext *ctx, const Promise *pp, const Attributes *a);
ENTERPRISE_VOID_FUNC_1ARG_DEFINE_STUB(void, ReportPatches, ARG_UNUSED PackageManager *, list)
{
Log(LOG_LEVEL_VERBOSE, "Patch reporting feature is only available in the enterprise version");
}
/*****************************************************************************/
PackageManager *PACKAGE_SCHEDULE = NULL; /* GLOBAL_X */
PackageManager *INSTALLED_PACKAGE_LISTS = NULL; /* GLOBAL_X */
#define PACKAGE_LIST_COMMAND_WINAPI "/Windows_API"
/*****************************************************************************/
#define PACKAGE_IGNORED_CFE_INTERNAL "cfe_internal_non_existing_package"
/* Returns the old or new package promise type depending on promise
constraints. */
static PackagePromiseType GetPackagePromiseVersion(const Packages *packages,
const NewPackages *new_packages)
{
assert(packages != NULL);
assert(new_packages != NULL);
/* We have mixed packages promise constraints. */
if (!packages->is_empty && !new_packages->is_empty)
{
return PACKAGE_PROMISE_TYPE_MIXED;
}
else if (!new_packages->is_empty) /* new packages promise */
{
if (new_packages->package_policy == NEW_PACKAGE_ACTION_NONE)
{
return PACKAGE_PROMISE_TYPE_NEW_ERROR;
}
return PACKAGE_PROMISE_TYPE_NEW;
}
else /* old packages promise */
{
//TODO:
if (!packages->has_package_method)
{
return PACKAGE_PROMISE_TYPE_OLD_ERROR;
}
return PACKAGE_PROMISE_TYPE_OLD;
}
}
PromiseResult VerifyPackagesPromise(EvalContext *ctx, const Promise *pp)
{
assert(pp != NULL); // Dereferenced in cfPS macros
PromiseResult result = PROMISE_RESULT_FAIL;
char *promise_log_message = NULL;
LogLevel level;
Attributes a = GetPackageAttributes(ctx, pp);
PackagePromiseType package_promise_type =
GetPackagePromiseVersion(&a.packages, &a.new_packages);
switch (package_promise_type)
{
case PACKAGE_PROMISE_TYPE_NEW:
Log(LOG_LEVEL_VERBOSE, "Using new package promise.");
result = HandleNewPackagePromiseType(ctx, pp, &a, &promise_log_message,
&level);
assert(promise_log_message != NULL);
if (result != PROMISE_RESULT_SKIPPED)
{
cfPS(ctx, level, result, pp, &a, "%s", promise_log_message);
}
free(promise_log_message);
break;
case PACKAGE_PROMISE_TYPE_OLD:
Log(LOG_LEVEL_VERBOSE,
"Using old package promise. Please note that this old "
"implementation is being phased out. The old "
"implementation will continue to work, but forward development "
"will be directed toward the new implementation.");
result = HandleOldPackagePromiseType(ctx, pp, &a);
/* Update new package promise cache in case we have mixed old and new
* package promises in policy. */
if (result == PROMISE_RESULT_CHANGE || result == PROMISE_RESULT_FAIL)
{
UpdatePackagesCache(ctx, false);
}
break;
case PACKAGE_PROMISE_TYPE_NEW_ERROR:
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, &a,
"New package promise failed sanity check.");
break;
case PACKAGE_PROMISE_TYPE_OLD_ERROR:
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, &a,
"Old package promise failed sanity check.");
break;
case PACKAGE_PROMISE_TYPE_MIXED:
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, &a,
"Mixed old and new package promise attributes inside "
"one package promise.");
break;
default:
assert(0); //Shouldn't happen
}
return result;
}
/******************************************************************************/
/**
@brief Executes single packages promise
Called by cf-agent.
* checks "name", "version", "arch", "firstrepo" variables from the "this" context
* gets the package attributes into a
* on Windows, if the package_list_command is not defined, use the hard-coded PACKAGE_LIST_COMMAND_WINAPI
* do a package sanity check on the promise
* print promise banner
* reset to root directory (Yum bugfix)
* get the default architecture from a.packages.package_default_arch_command into default_arch
* call VerifyInstalledPackages with default_arch
* if the package action is "patch", call VerifyPromisedPatch and return its result through PromiseResultUpdate_HELPER
* for all other package actions, call VerifyPromisedPackage and return its result through PromiseResultUpdate_HELPER
@param ctx [in] The evaluation context
@param pp [in] the Promise for this operation
@returns the promise result
*/
static PromiseResult HandleOldPackagePromiseType(EvalContext *ctx, const Promise *pp, const Attributes *attr)
{
assert(attr != NULL);
assert(pp != NULL);
Attributes a = *attr; // TODO: Remove this local copy, overwritten on windows
CfLock thislock;
char lockname[CF_BUFSIZE];
PromiseResult result = PROMISE_RESULT_NOOP;
const char *reserved_vars[] = { "name", "version", "arch", "firstrepo", NULL };
for (int c = 0; reserved_vars[c]; c++)
{
const char *reserved = reserved_vars[c];
VarRef *var_ref = VarRefParseFromScope(reserved, "this");
if (EvalContextVariableGet(ctx, var_ref, NULL))
{
Log(LOG_LEVEL_WARNING, "$(%s) variable has a special meaning in packages promises. "
"Things may not work as expected if it is already defined.", reserved);
}
VarRefDestroy(var_ref);
}
#ifdef __MINGW32__
if(!a.packages.package_list_command)
{
a.packages.package_list_command = PACKAGE_LIST_COMMAND_WINAPI;
}
#endif
if (!PackageSanityCheck(ctx, &a, pp))
{
Log(LOG_LEVEL_VERBOSE, "Package promise %s failed sanity check", pp->promiser);
result = PROMISE_RESULT_FAIL;
goto end;
}
PromiseBanner(ctx, pp);
// Now verify the package itself
PackagePromiseGlobalLock package_lock = AcquireGlobalPackagePromiseLock(ctx);
if (package_lock.g_lock.lock == NULL)
{
Log(LOG_LEVEL_VERBOSE,
"Can not acquire global lock for package promise. Skipping promise "
"evaluation");
result = PROMISE_RESULT_SKIPPED;
goto end;
}
snprintf(lockname, CF_BUFSIZE - 1, "package-%s-%s", pp->promiser, a.packages.package_list_command);
thislock = AcquireLock(ctx, lockname, VUQNAME, CFSTARTTIME,
a.transaction.ifelapsed, a.transaction.expireafter, pp, false);
if (thislock.lock == NULL)
{
YieldGlobalPackagePromiseLock(package_lock);
result = PROMISE_RESULT_SKIPPED;
goto end;
}
// Start by reseting the root directory in case yum tries to glob regexs(!)
if (safe_chdir("/") != 0)
{
Log(LOG_LEVEL_ERR, "Failed to chdir into '/'");
}
char *default_arch = GetDefaultArch(a.packages.package_default_arch_command);
if (default_arch == NULL)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, &a, "Unable to obtain default architecture for package manager - aborting");
YieldCurrentLock(thislock);
YieldGlobalPackagePromiseLock(package_lock);
result = PROMISE_RESULT_FAIL;
goto end;
}
Log(LOG_LEVEL_VERBOSE, "Default package architecture for promise %s is '%s'", pp->promiser, default_arch);
if (!VerifyInstalledPackages(ctx, &INSTALLED_PACKAGE_LISTS, default_arch, &a, pp, &result))
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, &a, "Unable to obtain a list of installed packages - aborting");
free(default_arch);
YieldCurrentLock(thislock);
YieldGlobalPackagePromiseLock(package_lock);
result = PROMISE_RESULT_FAIL;
goto end;
}
free(default_arch);
if (a.packages.package_policy == PACKAGE_ACTION_PATCH)
{
Log(LOG_LEVEL_VERBOSE, "Verifying patch action for promise %s", pp->promiser);
result = PromiseResultUpdate_HELPER(pp, result, VerifyPromisedPatch(ctx, &a, pp));
}
else
{
Log(LOG_LEVEL_VERBOSE, "Verifying action for promise %s", pp->promiser);
result = PromiseResultUpdate_HELPER(pp, result, VerifyPromisedPackage(ctx, &a, pp));
}
YieldCurrentLock(thislock);
YieldGlobalPackagePromiseLock(package_lock);
end:
if (!REPORT_THIS_PROMISE(pp))
{
// This will not be reported elsewhere, so give it kept outcome.
result = PROMISE_RESULT_NOOP;
cfPS(ctx, LOG_LEVEL_DEBUG, result, pp, &a, "Giving dummy package kept outcome");
}
return result;
}
/**
@brief Pre-check of promise contents
Called by VerifyPackagesPromise. Does many sanity checks on the
promise attributes and semantics.
@param ctx [in] The evaluation context
@param a [in] the promise Attributes for this operation
@param pp [in] the Promise for this operation
@returns the promise result
*/
static bool PackageSanityCheck(EvalContext *ctx, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
assert(pp != NULL); // Dereferenced in cfPS macros
const Packages *const pkgs = &(a->packages);
#ifndef __MINGW32__ // Windows may use Win32 API for listing and parsing
if (pkgs->package_list_name_regex == NULL)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"You must supply a method for determining the name of existing packages e.g. use the standard library generic package_method");
return false;
}
if (pkgs->package_list_version_regex == NULL)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"You must supply a method for determining the version of existing packages e.g. use the standard library generic package_method");
return false;
}
if ((!pkgs->package_commands_useshell) && (pkgs->package_list_command) && (!IsExecutable(CommandArg0(pkgs->package_list_command))))
{
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"The proposed package list command '%s' was not executable",
pkgs->package_list_command);
return false;
}
#endif /* !__MINGW32__ */
if ((pkgs->package_list_command == NULL) && (pkgs->package_file_repositories == NULL))
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"You must supply a method for determining the list of existing packages (a command or repository list) e.g. use the standard library generic package_method");
return false;
}
if (pkgs->package_file_repositories)
{
Rlist *rp;
for (rp = pkgs->package_file_repositories; rp != NULL; rp = rp->next)
{
if (strlen(RlistScalarValue(rp)) > CF_MAXVARSIZE - 1)
{
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "The repository path '%s' is too long", RlistScalarValue(rp));
return false;
}
}
}
if ((pkgs->package_name_regex) || (pkgs->package_version_regex) || (pkgs->package_arch_regex))
{
if (pkgs->package_name_regex == NULL)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "You must supply name regex if you supplied version or arch regex for parsing promiser string");
return false;
}
if ((pkgs->package_name_regex) && (pkgs->package_version_regex) && (pkgs->package_arch_regex))
{
if ((pkgs->package_version) || (pkgs->package_architectures))
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"You must either supply all regexs for (name,version,arch) or a separate version number and architecture");
return false;
}
}
else
{
if ((pkgs->package_version) && (pkgs->package_architectures))
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"You must either supply all regexs for (name,version,arch) or a separate version number and architecture");
return false;
}
}
if ((pkgs->package_version_regex) && (pkgs->package_version))
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"You must either supply version regex or a separate version number");
return false;
}
if ((pkgs->package_arch_regex) && (pkgs->package_architectures))
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"You must either supply arch regex or a separate architecture");
return false;
}
}
if (!pkgs->package_installed_regex)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "!! Package installed regex undefined");
return false;
}
if (pkgs->package_policy == PACKAGE_ACTION_VERIFY)
{
if (!pkgs->package_verify_command)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Package verify policy is used, but no package_verify_command is defined");
return false;
}
else if ((pkgs->package_noverify_returncode == CF_NOINT) && (pkgs->package_noverify_regex == NULL))
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Package verify policy is used, but no definition of verification failiure is set (package_noverify_returncode or packages.package_noverify_regex)");
return false;
}
}
if ((pkgs->package_noverify_returncode != CF_NOINT) && (pkgs->package_noverify_regex))
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Both package_noverify_returncode and package_noverify_regex are defined, pick one of them");
return false;
}
/* Dependency checks */
if (!pkgs->package_delete_command)
{
if (pkgs->package_delete_convention)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Dependency conflict: package_delete_command is not used, but package_delete_convention is defined.");
return false;
}
}
if (!pkgs->package_list_command)
{
if (pkgs->package_installed_regex)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Dependency conflict: package_list_command is not used, but package_installed_regex is defined.");
return false;
}
if (pkgs->package_list_arch_regex)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Dependency conflict: package_list_command is not used, but package_arch_regex is defined.");
return false;
}
if (pkgs->package_list_name_regex)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Dependency conflict: package_list_command is not used, but package_name_regex is defined.");
return false;
}
if (pkgs->package_list_version_regex)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Dependency conflict: package_list_command is not used, but package_version_regex is defined.");
return false;
}
}
if (!pkgs->package_patch_command)
{
if (pkgs->package_patch_arch_regex)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Dependency conflict: package_patch_command is not used, but package_patch_arch_regex is defined.");
return false;
}
if (pkgs->package_patch_name_regex)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Dependency conflict: package_patch_command is not used, but package_patch_name_regex is defined.");
return false;
}
if (pkgs->package_patch_version_regex)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Dependency conflict: package_patch_command is not used, but package_patch_version_regex is defined.");
return false;
}
}
if (!pkgs->package_patch_list_command)
{
if (pkgs->package_patch_installed_regex)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Dependency conflict: package_patch_list_command is not used, but package_patch_installed_regex is defined.");
return false;
}
}
if (!pkgs->package_verify_command)
{
if (pkgs->package_noverify_regex)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Dependency conflict: package_verify_command is not used, but package_noverify_regex is defined.");
return false;
}
if (pkgs->package_noverify_returncode != CF_NOINT)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"!! Dependency conflict: package_verify_command is not used, but package_noverify_returncode is defined.");
return false;
}
}
return true;
}
/**
@brief Generates the list of installed packages
Called by VerifyInstalledPackages
* calls pkgs->package_list_update_command if $(sys.statedir)/software_update_timestamp_<manager>
is older than the interval specified in package_list_update_ifelapsed.
* assembles the package list from a.packages.package_list_command
* respects a.packages.package_commands_useshell (boolean)
* parses with a.packages.package_multiline_start and if successful, calls PrependMultiLinePackageItem
* else, parses with a.packages.package_installed_regex and if successful, calls PrependListPackageItem
@param ctx [in] The evaluation context
@param installed_list [in] a list of PackageItems
@param default_arch [in] the default architecture
@param a [in] the promise Attributes for this operation
@param pp [in] the Promise for this operation
@param result [inout] the PromiseResult for this operation
@returns boolean pass/fail of command run
*/
static bool PackageListInstalledFromCommand(EvalContext *ctx,
PackageItem **installed_list,
const char *default_arch,
const Attributes *a, const Promise *pp,
PromiseResult *result)
{
assert(a != NULL);
if (a->packages.package_list_update_command != NULL)
{
if (!a->packages.package_add_command)
{
Log(LOG_LEVEL_ERR, "package_add_command missing while trying to "
"generate list of installed packages");
return false;
}
time_t horizon = 24 * 60, now = time(NULL);
bool call_update = true;
struct stat sb;
char update_timestamp_file[PATH_MAX];
snprintf(update_timestamp_file, sizeof(update_timestamp_file), "%s%csoftware_update_timestamp_%s",
GetStateDir(), FILE_SEPARATOR,
ReadLastNode(RealPackageManager(a->packages.package_add_command)));
if (stat(update_timestamp_file, &sb) != -1)
{
if (a->packages.package_list_update_ifelapsed != CF_NOINT)
{
horizon = a->packages.package_list_update_ifelapsed;
}
char *rel, *action;
if (now - sb.st_mtime < horizon * 60)
{
rel = "less";
action = "Not updating";
call_update = false;
}
else
{
rel = "more";
action = "Updating";
}
Log(LOG_LEVEL_VERBOSE, "'%s' is %s than %i minutes old. %s package list.",
update_timestamp_file, rel, a->packages.package_list_update_ifelapsed, action);
}
else
{
Log(LOG_LEVEL_VERBOSE, "'%s' does not exist. Updating package list.", update_timestamp_file);
}
if (call_update)
{
Log(LOG_LEVEL_VERBOSE, "Calling package list update command: '%s'", a->packages.package_list_update_command);
ExecPackageCommand(ctx, a->packages.package_list_update_command, false, false, a, pp, result);
// Touch timestamp file.
int err = utime(update_timestamp_file, NULL);
if (err < 0)
{
if (errno == ENOENT)
{
int fd = open(update_timestamp_file, O_WRONLY | O_CREAT, 0600);
if (fd >= 0)
{
close(fd);
}
else
{
Log(LOG_LEVEL_ERR, "Could not create timestamp file '%s'. (open: '%s')",
update_timestamp_file, GetErrorStr());
}
}
else
{
Log(LOG_LEVEL_ERR, "Could not update timestamp file '%s'. (utime: '%s')",
update_timestamp_file, GetErrorStr());
}
}
}
}
Log(LOG_LEVEL_VERBOSE, "Reading package list from '%s'", a->packages.package_list_command);
FILE *fin;
if (a->packages.package_commands_useshell)
{
if ((fin = cf_popen_sh(a->packages.package_list_command, "r")) == NULL)
{
Log(LOG_LEVEL_ERR, "Couldn't open the package list with command '%s'. (cf_popen_sh: %s)",
a->packages.package_list_command, GetErrorStr());
return false;
}
}
else if ((fin = cf_popen(a->packages.package_list_command, "r", true)) == NULL)
{
Log(LOG_LEVEL_ERR, "Couldn't open the package list with command '%s'. (cf_popen: %s)",
a->packages.package_list_command, GetErrorStr());
return false;
}
const int reset = true, update = false;
size_t buf_size = CF_BUFSIZE;
char *buf = xmalloc(buf_size);
for (;;)
{
ssize_t res = CfReadLine(&buf, &buf_size, fin);
if (res == -1)
{
if (!feof(fin))
{
Log(LOG_LEVEL_ERR, "Unable to read list of packages from command '%s'. (fread: %s)",
a->packages.package_list_command, GetErrorStr());
cf_pclose(fin);
free(buf);
return false;
}
else
{
break;
}
}
if (a->packages.package_multiline_start)
{
if (FullTextMatch(ctx, a->packages.package_multiline_start, buf))
{
PrependMultiLinePackageItem(ctx, installed_list, buf, reset, default_arch, a, pp);
}
else
{
PrependMultiLinePackageItem(ctx, installed_list, buf, update, default_arch, a, pp);
}
}
else
{
if (!FullTextMatch(ctx, a->packages.package_installed_regex, buf))
{
Log(LOG_LEVEL_VERBOSE, "Package line '%s' did not match the package_installed_regex pattern", buf);
continue;
}
if (!PrependListPackageItem(ctx, installed_list, buf, default_arch, a, pp))
{
Log(LOG_LEVEL_VERBOSE, "Package line '%s' did not match one of the package_list_(name|version|arch)_regex patterns", buf);
continue;
}
}
}
if (a->packages.package_multiline_start)
{
PrependMultiLinePackageItem(ctx, installed_list, buf, reset, default_arch, a, pp);
}
free(buf);
return cf_pclose(fin) == 0;
}
/**
@brief Writes the software inventory
Called by VerifyInstalledPackages
* calls GetSoftwareCacheFilename to get the inventory CSV filename
* for each PackageManager in the list
* * for each PackageItem in the PackageManager's list
* * write name, version, architecture, manager name
@param ctx [in] The evaluation context
@param list [in] a list of PackageManagers
*/
static void ReportSoftware(PackageManager *list)
{
PackageManager *mp = NULL;
PackageItem *pi;
char name[CF_BUFSIZE];
GetSoftwareCacheFilename(name);
FILE *fout = safe_fopen(name, "w");
if (fout == NULL)
{
Log(LOG_LEVEL_ERR,
"Cannot open the destination file '%s'. (fopen: %s)",
name, GetErrorStr());
return;
}
Writer *writer_installed = FileWriter(fout);
CsvWriter *c = CsvWriterOpen(writer_installed);
if (c)
{
for (mp = list; mp != NULL; mp = mp->next)
{
for (pi = mp->pack_list; pi != NULL; pi = pi->next)
{
CsvWriterField(c, pi->name);
CsvWriterField(c, pi->version);
CsvWriterField(c, pi->arch);
CsvWriterField(c, ReadLastNode(RealPackageManager(mp->manager)));
CsvWriterNewRecord(c);
}
}
CsvWriterClose(c);
}
else
{
Log(LOG_LEVEL_ERR, "Cannot write CSV to file '%s'", name);
}
WriterClose(writer_installed);
}
/**
@brief Invalidates the software inventory
Called by ExecuteSchedule and ExecutePatch
* calls GetSoftwareCacheFilename to get the inventory CSV filename
* sets atime and mtime on that file to 0
*/
static void InvalidateSoftwareCache(void)
{
char name[CF_BUFSIZE];
struct utimbuf epoch = { 0, 0 };
GetSoftwareCacheFilename(name);
if (utime(name, &epoch) != 0)
{
if (errno != ENOENT)
{
Log(LOG_LEVEL_ERR, "Cannot mark software cache as invalid. (utimes: %s)", GetErrorStr());
}
}
}
/**
@brief Gets the cached list of installed packages from file
Called by VerifyInstalledPackages
* calls GetSoftwareCacheFilename to get the inventory CSV filename
* respects a.packages.package_list_update_ifelapsed, returns NULL if file is too old
* parses the CSV out of the file (name, version, arch, manager) with each limited to 250 chars
* for each line
* * if architecture is "default", replace it with default_arch
* * if the package manager name matches, call PrependPackageItem
@param ctx [in] The evaluation context
@param manager [in] the PackageManager we want
@param default_arch [in] the default architecture
@param a [in] the promise Attributes for this operation
@param pp [in] the Promise for this operation
@returns list of PackageItems
*/
static PackageItem *GetCachedPackageList(EvalContext *ctx, PackageManager *manager, const char *default_arch, const Attributes *a,
const Promise *pp)
{
assert(a != NULL);
assert(manager != NULL);
PackageItem *list = NULL;
char name[CF_MAXVARSIZE], version[CF_MAXVARSIZE], arch[CF_MAXVARSIZE], mgr[CF_MAXVARSIZE], line[CF_BUFSIZE];
char thismanager[CF_MAXVARSIZE];
FILE *fin;
time_t horizon = 24 * 60, now = time(NULL);
struct stat sb;
GetSoftwareCacheFilename(name);
if (stat(name, &sb) == -1)
{
return NULL;
}
if (a->packages.package_list_update_ifelapsed != CF_NOINT)
{
horizon = a->packages.package_list_update_ifelapsed;
}
if (now - sb.st_mtime < horizon * 60)
{
Log(LOG_LEVEL_VERBOSE,
"Cache file '%s' exists and is sufficiently fresh according to (package_list_update_ifelapsed)", name);
}
else
{
Log(LOG_LEVEL_VERBOSE, "Cache file '%s' exists, but it is out of date (package_list_update_ifelapsed)", name);
return NULL;
}
if ((fin = fopen(name, "r")) == NULL)
{
Log(LOG_LEVEL_ERR, "Cannot open the source log '%s' - you need to run a package discovery promise to create it in cf-agent. (fopen: %s)",
name, GetErrorStr());
return NULL;
}
/* Max 2016 entries - at least a week */
snprintf(thismanager, CF_MAXVARSIZE - 1, "%s", ReadLastNode(RealPackageManager(manager->manager)));
int linenumber = 0;
for(;;)
{
if (fgets(line, sizeof(line), fin) == NULL)
{
if (ferror(fin))
{
UnexpectedError("Failed to read line %d from stream '%s'", linenumber+1, name);
break;
}
else /* feof */
{
break;
}
}
++linenumber;
int scancount = sscanf(line, "%250[^,],%250[^,],%250[^,],%250[^\r\n]", name, version, arch, mgr);
if (scancount != 4)
{
Log(LOG_LEVEL_VERBOSE, "Could only read %d values from line %d in '%s'", scancount, linenumber, name);
}
/*
* Transition to explicit default architecture, if package manager
* supports it.
*
* If old cache contains entries with 'default' architecture, and
* package method is updated to detect this architecture, on next
* execution update this architecture to the real one.
*/
if (!strcmp(arch, "default"))
{
strlcpy(arch, default_arch, CF_MAXVARSIZE);
}
if (strcmp(thismanager, mgr) == 0)
{
PrependPackageItem(ctx, &list, name, version, arch, pp);
}
}
fclose(fin);
return list;
}
/**
@brief Verifies installed packages for a single Promise
Called by VerifyPackagesPromise
* from all_mgrs, gets the package manager matching a.packages.package_list_command
* populate manager->pack_list with GetCachedPackageList
* on Windows, use NovaWin_PackageListInstalledFromAPI if a.packages.package_list_command is set to PACKAGE_LIST_COMMAND_WINAPI
* on other platforms, use PackageListInstalledFromCommand
* call ReportSoftware to save the installed packages inventory
* if a.packages.package_patch_list_command is set, use it and parse each line with a.packages.package_patch_installed_regex; if it matches, call PrependPatchItem
* call ReportPatches to save the available updates inventory (Enterprise only)
@param ctx [in] The evaluation context
@param all_mgrs [in] a list of PackageManagers
@param default_arch [in] the default architecture
@param a [in] the promise Attributes for this operation
@param pp [in] the Promise for this operation
@param result [inout] the PromiseResult for this operation
@returns boolean pass/fail of verification
*/
static bool VerifyInstalledPackages(
EvalContext *ctx,
PackageManager **all_mgrs,
const char *default_arch,
const Attributes *a,
const Promise *pp,
PromiseResult *result)
{
assert(a != NULL);
PackageManager *manager = GetPackageManager(all_mgrs, a->packages.package_list_command, PACKAGE_ACTION_NONE, PACKAGE_ACTION_POLICY_NONE);
if (manager == NULL)
{
Log(LOG_LEVEL_ERR, "Can't create a package manager envelope for '%s'", a->packages.package_list_command);
return false;
}
if (manager->pack_list != NULL)
{
Log(LOG_LEVEL_VERBOSE, "Already have a package list for this manager");
return true;
}
manager->pack_list = GetCachedPackageList(ctx, manager, default_arch, a, pp);
if (manager->pack_list != NULL)
{
Log(LOG_LEVEL_VERBOSE, "Already have a (cached) package list for this manager ");
return true;
}
if (a->packages.package_list_command == NULL)
{
/* skip */
}
#ifdef __MINGW32__
else if (strcmp(a->packages.package_list_command, PACKAGE_LIST_COMMAND_WINAPI) == 0)
{
if (!NovaWin_PackageListInstalledFromAPI(ctx, &(manager->pack_list), a, pp))
{
Log(LOG_LEVEL_ERR, "Could not get list of installed packages");
return false;
}
}
#endif /* !__MINGW32__ */
else
{
if (!PackageListInstalledFromCommand(ctx, &(manager->pack_list), default_arch, a, pp, result))
{
Log(LOG_LEVEL_ERR, "Could not get list of installed packages");
return false;
}
}
ReportSoftware(INSTALLED_PACKAGE_LISTS);
/* Now get available updates */
if (a->packages.package_patch_list_command != NULL)
{
Log(LOG_LEVEL_VERBOSE, "Reading patches from '%s'", CommandArg0(a->packages.package_patch_list_command));
if ((!a->packages.package_commands_useshell) && (!IsExecutable(CommandArg0(a->packages.package_patch_list_command))))
{
Log(LOG_LEVEL_ERR, "The proposed patch list command '%s' was not executable",
a->packages.package_patch_list_command);
return false;
}
FILE *fin;
if (a->packages.package_commands_useshell)
{
if ((fin = cf_popen_sh(a->packages.package_patch_list_command, "r")) == NULL)
{
Log(LOG_LEVEL_ERR, "Couldn't open the patch list with command '%s'. (cf_popen_sh: %s)",
a->packages.package_patch_list_command, GetErrorStr());
return false;
}
}
else if ((fin = cf_popen(a->packages.package_patch_list_command, "r", true)) == NULL)
{
Log(LOG_LEVEL_ERR, "Couldn't open the patch list with command '%s'. (cf_popen: %s)",
a->packages.package_patch_list_command, GetErrorStr());
return false;
}
size_t vbuff_size = CF_BUFSIZE;
char *vbuff = xmalloc(vbuff_size);
for (;;)
{
ssize_t res = CfReadLine(&vbuff, &vbuff_size, fin);
if (res == -1)
{
if (!feof(fin))
{
Log(LOG_LEVEL_ERR, "Unable to read list of patches from command '%s'. (fread: %s)",
a->packages.package_patch_list_command, GetErrorStr());
cf_pclose(fin);
free(vbuff);
return false;
}
else
{
break;
}
}
// assume patch_list_command lists available patches/updates by default
if ((a->packages.package_patch_installed_regex == NULL)
|| (!FullTextMatch(ctx, a->packages.package_patch_installed_regex, vbuff)))
{
PrependPatchItem(ctx, &(manager->patch_avail), vbuff, manager->patch_list, default_arch, a, pp);
continue;
}
if (!PrependPatchItem(ctx, &(manager->patch_list), vbuff, manager->patch_list, default_arch, a, pp))
{
continue;
}
}
cf_pclose(fin);
free(vbuff);
}
if (a->packages.package_patch_list_command != NULL)
{
ReportPatches(INSTALLED_PACKAGE_LISTS); // Enterprise only
}
Log(LOG_LEVEL_VERBOSE, "Done checking packages and patches");
return true;
}
/** Evaluate what needs to be done **/
/**
@brief Finds the largest version of a package available in a file repository
Called by SchedulePackageOp
* match = false
* for each directory in repositories
* * try to match refAnyVer against each file
* * if it matches and CompareVersions says it's the biggest found so far, copy the matched version and name into matchName and matchVers and set match to true
* return match
@param ctx [in] The evaluation context
@param matchName [inout] the matched package name (written on match)
@param matchVers [inout] the matched package version (written on match)
@param refAnyVer [in] the regex to match against the filename to extract a version
@param ver [in] the version sought
@param repositories [in] the list of directories (file repositories)
@param a [in] the promise Attributes for this operation
@param pp [in] the Promise for this operation
@param result [inout] the PromiseResult for this operation
@returns boolean pass/fail of search
*/
int FindLargestVersionAvail(EvalContext *ctx, char *matchName, char *matchVers, const char *refAnyVer, const char *ver,
Rlist *repositories, const Attributes *a, const Promise *pp, PromiseResult *result)
/* Returns true if a version gt/ge ver is found in local repos, false otherwise */
{
int match = false;
// match any version
if (!ver[0] || strcmp(ver, "*") == 0)
{
matchVers[0] = '\0';
}
else
{
strlcpy(matchVers, ver, CF_MAXVARSIZE);
}
for (Rlist *rp = repositories; rp != NULL; rp = rp->next)
{
Dir *dirh = DirOpen(RlistScalarValue(rp));
if (dirh == NULL)
{
Log(LOG_LEVEL_ERR, "Can't open local directory '%s'. (opendir: %s)",
RlistScalarValue(rp), GetErrorStr());
continue;
}
const struct dirent *dirp;
while ((dirp = DirRead(dirh)) != NULL)
{
if (FullTextMatch(ctx, refAnyVer, dirp->d_name))
{
char *matchVer = ExtractFirstReference(refAnyVer, dirp->d_name);
// check if match is largest so far
if (CompareVersions(ctx, matchVer, matchVers, a, pp, result) == VERCMP_MATCH)
{
strlcpy(matchVers, matchVer, CF_MAXVARSIZE);
strlcpy(matchName, dirp->d_name, CF_MAXVARSIZE);
match = true;
}
}
}
DirClose(dirh);
}
Log(LOG_LEVEL_DEBUG, "FindLargestVersionAvail: largest version of '%s' is '%s' (match=%d)",
matchName, matchVers, match);
return match;
}
/**
@brief Returns true if a package (n,v,a) is installed and v is larger than the installed version
Called by SchedulePackageOp
* for each known PackageManager, compare to attr.packages.package_list_command
* bail out if no manager was found
* for each PackageItem pi in the manager's package list
* * if pi->name equals n and (a is "*" or a equals pi->arch)
* * * record instV and instA
* * * copy attr into attr2 and override the attr2.packages.package_select to PACKAGE_VERSION_COMPARATOR_LT
* * * return CompareVersions of the new monster
* return false if the above found no matches
@param ctx [in] The evaluation context
@param n [in] the specific name
@param v [in] the specific version
@param a [in] the specific architecture
@param instV [inout] the matched package version (written on match)
@param instA [inout] the matched package architecture (written on match)
@param attr [in] the promise Attributes for this operation
@param pp [in] the Promise for this operation
@param result [inout] the PromiseResult for this operation
@returns boolean if given (n,v,a) is newer than known packages
*/
static bool IsNewerThanInstalled(
EvalContext *ctx,
const char *n,
const char *v,
const char *a,
char *instV,
char *instA,
const Attributes *attr,
const Promise *pp,
PromiseResult *result)
{
assert(attr != NULL);
PackageManager *mp = INSTALLED_PACKAGE_LISTS;
while (mp != NULL)
{
if (strcmp(mp->manager, attr->packages.package_list_command) == 0)
{
break;
}
mp = mp->next;
}
if (mp == NULL)
{
Log(LOG_LEVEL_VERBOSE, "Found no package manager matching attr.packages.package_list_command '%s'",
attr->packages.package_list_command == NULL ? "[empty]" : attr->packages.package_list_command);
return false;
}
Log(LOG_LEVEL_VERBOSE, "Looking for an installed package older than (%s,%s,%s) [name,version,arch]", n, v, a);
for (PackageItem *pi = mp->pack_list; pi != NULL; pi = pi->next)
{
if (strcmp(n, pi->name) == 0 &&
(strcmp(a, "*") == 0 || strcmp(a, pi->arch) == 0))
{
Log(LOG_LEVEL_VERBOSE,
"Found installed package (%s,%s,%s) [name,version,arch]",
pi->name, pi->version, pi->arch);
strlcpy(instV, pi->version, CF_MAXVARSIZE);
strlcpy(instA, pi->arch, CF_MAXVARSIZE);
/* Horrible */
Attributes attr2 = *attr;
attr2.packages.package_select = PACKAGE_VERSION_COMPARATOR_LT;
return CompareVersions(ctx, pi->version, v, &attr2, pp, result) == VERCMP_MATCH;
}
}
Log(LOG_LEVEL_VERBOSE, "Package (%s,%s) [name,arch] is not installed", n, a);
return false;
}
/**
@brief Returns string version of a PackageAction
@param pa [in] The PackageAction
@returns string representation of pa or a ProgrammingError
*/
static const char *PackageAction2String(PackageAction pa)
{
switch (pa)
{
case PACKAGE_ACTION_ADD:
return "installing";
case PACKAGE_ACTION_DELETE:
return "uninstalling";
case PACKAGE_ACTION_REINSTALL:
return "reinstalling";
case PACKAGE_ACTION_UPDATE:
return "updating";
case PACKAGE_ACTION_ADDUPDATE:
return "installing/updating";
case PACKAGE_ACTION_PATCH:
return "patching";
case PACKAGE_ACTION_VERIFY:
return "verifying";
default:
ProgrammingError("CFEngine: internal error: illegal package action");
}
}
/**
@brief Adds a specific package (name,version,arch) as specified by Attributes a to the scheduled operations
Called by SchedulePackageOp.
Either warn or fix, based on a->transaction.action.
To fix, calls GetPackageManager and enqueues the desired operation and package with the returned manager
@param ctx [in] The evaluation context
@param a [in] the Attributes specifying how to compare
@param mgr [in] the specific manager name
@param pa [in] the PackageAction to enqueue
@param name [in] the specific name
@param version [in] the specific version
@param arch [in] the specific architecture
@param pp [in] the Promise for this operation
@returns the promise result
*/
static PromiseResult AddPackageToSchedule(EvalContext *ctx, const Attributes *a, char *mgr, PackageAction pa,
const char *name, const char *version, const char *arch,
const Promise *pp)
{
assert(a != NULL);
assert(pp != NULL);
switch (a->transaction.action)
{
case cfa_warn:
cfPS_HELPER_3ARG(ctx, LOG_LEVEL_WARNING, PROMISE_RESULT_WARN, pp, a, "Need to repair promise '%s' by '%s' package '%s'",
pp->promiser, PackageAction2String(pa), name);
return PROMISE_RESULT_WARN;
case cfa_fix:
{
PackageManager *manager = GetPackageManager(&PACKAGE_SCHEDULE, mgr, pa, a->packages.package_changes);
if (manager == NULL)
{
ProgrammingError("AddPackageToSchedule: Null package manager found!!!");
}
PrependPackageItem(ctx, &(manager->pack_list), name, version, arch, pp);
return PROMISE_RESULT_CHANGE;
}
default:
ProgrammingError("CFEngine: internal error: illegal file action");
}
}
/**
@brief Adds a specific patch (name,version,arch) as specified by Attributes a to the scheduled operations
Called by SchedulePackageOp.
Either warn or fix, based on a->transaction.action.
To fix, calls GetPackageManager and enqueues the desired operation and package with the returned manager
@param ctx [in] The evaluation context
@param a [in] the Attributes specifying how to compare
@param mgr [in] the specific manager name
@param pa [in] the PackageAction to enqueue
@param name [in] the specific name
@param version [in] the specific version
@param arch [in] the specific architecture
@param pp [in] the Promise for this operation
@returns the promise result
*/
static PromiseResult AddPatchToSchedule(EvalContext *ctx, const Attributes *a, char *mgr, PackageAction pa,
const char *name, const char *version, const char *arch,
const Promise *pp)
{
assert(a != NULL);
assert(pp != NULL);
switch (a->transaction.action)
{
case cfa_warn:
cfPS_HELPER_3ARG(ctx, LOG_LEVEL_WARNING, PROMISE_RESULT_WARN, pp, a, "Need to repair promise '%s' by '%s' package '%s'",
pp->promiser, PackageAction2String(pa), name);
return PROMISE_RESULT_WARN;
case cfa_fix:
{
PackageManager *manager = GetPackageManager(&PACKAGE_SCHEDULE, mgr, pa, a->packages.package_changes);
if (manager == NULL)
{
ProgrammingError("AddPatchToSchedule: Null package manager found!!!");
}
PrependPackageItem(ctx, &(manager->patch_list), name, version, arch, pp);
return PROMISE_RESULT_CHANGE;
}
default:
ProgrammingError("Illegal file action");
}
}
/**
@brief Schedules a package operation based on the action, package state, and everything else.
Called by VerifyPromisedPatch and CheckPackageState.
This function has a complexity metric of 3 Googols.
* if package_delete_convention or package_name_convention are given and apply to the operation, construct the package name from them (from PACKAGES_CONTEXT)
* else, just use the given package name
* warn about "*" in the package name
* set package_select_in_range with magic
* create PackageAction policy from the package_policy and then split ADDUPDATE into ADD or UPDATE based on "installed"
* result starts as NOOP
* switch(policy)
* * case ADD and "installed":
* * * if we have package_file_repositories
* * * * use the package_name_convention to build the package name (from PACKAGES_CONTEXT_ANYVER, setting version to "*")
* * * * if FindLargestVersionAvail finds the latest package version in the file repos, use that as the package name
* * * AddPackageToSchedule package_add_command, ADD, package name, etc.
* * case DELETE and (matched AND package_select_in_range) OR (installed AND no_version_specified):
* * * fail promise unless package_delete_command
* * * if we have package_file_repositories
* * * * clean up the name string from any "repo" references and add the right file repo
* * * AddPackageToSchedule package_delete_command, DELETE, package name, etc.
* * case REINSTALL:
* * * fail promise unless package_delete_command
* * * fail promise if no_version_specified
* * * if (matched AND package_select_in_range) OR (installed AND no_version_specified) do AddPackageToSchedule package_delete_command, DELETE, package name, etc.
* * * AddPackageToSchedule package_add_command, ADD, package name, etc.
* * case UPDATE:
* * * if we have package_file_repositories
* * * * use the package_name_convention to build the package name (from PACKAGES_CONTEXT_ANYVER, setting version to "*")
* * * * if FindLargestVersionAvail finds the latest package version in the file repos, use that as the package name
* * * if installed, IsNewerThanInstalled is checked, and if it returns false we don't update an up-to-date package
* * * if installed or (matched AND package_select_in_range AND !no_version_specified) (this is the main update condition)
* * * * if package_update_command is not given
* * * * * if package_delete_convention is given, use it to build id_del (from PACKAGES_CONTEXT)
* * * * * fail promise if package_update_command and package_add_command are not given
* * * * * AddPackageToSchedule with package_delete_command, DELETE, id_del, etc
* * * * * AddPackageToSchedule with package_add_command, ADD, package name, etc
* * * * else we have package_update_command, so AddPackageToSchedule with package_update_command, UPDATE, package name, etc
* * * else the package is not updateable: no match or not installed, fail promise
* * case PATCH:
* * * if matched and not installed, AddPatchToSchedule with package_patch_command, PATCH, package name, etc.
* * case VERIFY:
* * * if (matched and package_select_in_range) OR (installed AND no_version_specified), AddPatchToSchedule with package_verify_command, VERIFY, package name, etc.
@param ctx [in] The evaluation context
@param name [in] the specific name
@param version [in] the specific version
@param arch [in] the specific architecture
@param installed [in] is the package installed?
@param matched [in] is the package matched in the available list?
@param no_version_specified [in] no version was specified in the promise
@param a [in] the Attributes specifying how to compare
@param pp [in] the Promise for this operation
@returns the promise result
*/
static PromiseResult SchedulePackageOp(EvalContext *ctx, const char *name, const char *version, const char *arch, int installed, int matched,
int no_version_specified, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
assert(pp != NULL); // Dereferenced by cfPS macros
char refAnyVerEsc[CF_EXPANDSIZE];
char largestVerAvail[CF_MAXVARSIZE];
char largestPackAvail[CF_MAXVARSIZE];
char id[CF_EXPANDSIZE];
Log(LOG_LEVEL_VERBOSE,
"Checking if package (%s,%s,%s) [name,version,arch] "
"is at the desired state (installed=%d,matched=%d)",
name, version, arch, installed, matched);
/* Now we need to know the name-convention expected by the package manager */
Buffer *expanded = BufferNew();
if ((a->packages.package_name_convention) || (a->packages.package_delete_convention))
{
VarRef *ref_name = VarRefParseFromScope("name", PACKAGES_CONTEXT);
EvalContextVariablePut(ctx, ref_name, name, CF_DATA_TYPE_STRING, "source=promise");
VarRef *ref_version = VarRefParseFromScope("version", PACKAGES_CONTEXT);
EvalContextVariablePut(ctx, ref_version, version, CF_DATA_TYPE_STRING, "source=promise");
VarRef *ref_arch = VarRefParseFromScope("arch", PACKAGES_CONTEXT);
EvalContextVariablePut(ctx, ref_arch, arch, CF_DATA_TYPE_STRING, "source=promise");
if ((a->packages.package_delete_convention) && (a->packages.package_policy == PACKAGE_ACTION_DELETE))
{
ExpandScalar(ctx, NULL, PACKAGES_CONTEXT, a->packages.package_delete_convention, expanded);
strlcpy(id, BufferData(expanded), CF_EXPANDSIZE);
}
else if (a->packages.package_name_convention)
{
ExpandScalar(ctx, NULL, PACKAGES_CONTEXT, a->packages.package_name_convention, expanded);
strlcpy(id, BufferData(expanded), CF_EXPANDSIZE);
}
else
{
strlcpy(id, name, CF_EXPANDSIZE);
}
EvalContextVariableRemove(ctx, ref_name);
VarRefDestroy(ref_name);
EvalContextVariableRemove(ctx, ref_version);
VarRefDestroy(ref_version);
EvalContextVariableRemove(ctx, ref_arch);
VarRefDestroy(ref_arch);
}
else
{
strlcpy(id, name, CF_EXPANDSIZE);
}
Log(LOG_LEVEL_VERBOSE, "Package promises to refer to itself as '%s' to the manager", id);
if (strchr(id, '*'))
{
Log(LOG_LEVEL_VERBOSE, "Package name contains '*' -- perhaps "
"a missing attribute (name/version/arch) should be specified");
}
// This is very confusing
int package_select_in_range;
switch (a->packages.package_select)
{
case PACKAGE_VERSION_COMPARATOR_EQ:
case PACKAGE_VERSION_COMPARATOR_GE:
case PACKAGE_VERSION_COMPARATOR_LE:
case PACKAGE_VERSION_COMPARATOR_NONE:
Log(LOG_LEVEL_VERBOSE, "Package version seems to match criteria");
package_select_in_range = true;
break;
default:
package_select_in_range = false;
break;
}
PackageAction policy = a->packages.package_policy;
if (policy == PACKAGE_ACTION_ADDUPDATE) /* Work out which: */
{
if (installed)
{
policy = PACKAGE_ACTION_UPDATE;
}
else
{
policy = PACKAGE_ACTION_ADD;
}
}
PromiseResult result = PROMISE_RESULT_NOOP;
switch (policy)
{
case PACKAGE_ACTION_ADD:
if (installed == 0)
{
if ((a->packages.package_file_repositories != NULL))
{
Log(LOG_LEVEL_VERBOSE, "Package method specifies a file repository");
{
VarRef *ref_name = VarRefParseFromScope("name", PACKAGES_CONTEXT_ANYVER);
EvalContextVariablePut(ctx, ref_name, name, CF_DATA_TYPE_STRING, "source=promise");
VarRef *ref_version = VarRefParseFromScope("version", PACKAGES_CONTEXT_ANYVER);
EvalContextVariablePut(ctx, ref_version, "(.*)", CF_DATA_TYPE_STRING, "source=promise");
VarRef *ref_arch = VarRefParseFromScope("arch", PACKAGES_CONTEXT_ANYVER);
EvalContextVariablePut(ctx, ref_arch, arch, CF_DATA_TYPE_STRING, "source=promise");
BufferClear(expanded);
if (a->packages.package_name_convention)
{
ExpandScalar(ctx, NULL, PACKAGES_CONTEXT_ANYVER, a->packages.package_name_convention, expanded);
}
EvalContextVariableRemove(ctx, ref_name);
VarRefDestroy(ref_name);
EvalContextVariableRemove(ctx, ref_version);
VarRefDestroy(ref_version);
EvalContextVariableRemove(ctx, ref_arch);
VarRefDestroy(ref_arch);
}
EscapeSpecialChars(BufferData(expanded), refAnyVerEsc, sizeof(refAnyVerEsc), "(.*)","");
if (FindLargestVersionAvail(ctx, largestPackAvail, largestVerAvail, refAnyVerEsc, version,
a->packages.package_file_repositories, a, pp, &result))
{
Log(LOG_LEVEL_VERBOSE, "Using latest version in file repositories; '%s'", largestPackAvail);
strlcpy(id, largestPackAvail, CF_EXPANDSIZE);
}
else
{
Log(LOG_LEVEL_VERBOSE, "No package in file repositories satisfy version constraint");
break;
}
}
else
{
Log(LOG_LEVEL_VERBOSE, "Package method does NOT specify a file repository");
}
Log(LOG_LEVEL_VERBOSE, "Schedule package for addition");
if (a->packages.package_add_command == NULL)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Package add command undefined");
BufferDestroy(expanded);
return PROMISE_RESULT_FAIL;
}
result = PromiseResultUpdate_HELPER(pp, result,
AddPackageToSchedule(ctx, a, a->packages.package_add_command,
PACKAGE_ACTION_ADD, id, "any", "any", pp));
}
else
{
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Package '%s' already installed, so we never add it again",
pp->promiser);
}
break;
case PACKAGE_ACTION_DELETE:
// we're deleting a matched package found in a range OR an installed package with no version
if ((matched && package_select_in_range) ||
(installed && no_version_specified))
{
Log(LOG_LEVEL_VERBOSE, "Schedule package for deletion");
if (a->packages.package_delete_command == NULL)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Package delete command undefined");
BufferDestroy(expanded);
return PROMISE_RESULT_FAIL;
}
// expand local repository in the name convention, if present
if (a->packages.package_file_repositories)
{
Log(LOG_LEVEL_VERBOSE, "Package method specifies a file repository");
// remove any "$(repo)" from the name convention string
if (strncmp(id, "$(firstrepo)", 12) == 0)
{
const char *idBuf = id + 12;
// and add the correct repo
const char *pathName = PrefixLocalRepository(a->packages.package_file_repositories, idBuf);
if (pathName)
{
strlcpy(id, pathName, CF_EXPANDSIZE);
Log(LOG_LEVEL_VERBOSE,
"Expanded the package repository to '%s'", id);
}
else
{
Log(LOG_LEVEL_ERR, "Package '%s' can't be found "
"in any of the listed repositories", idBuf);
}
}
}
else
{
Log(LOG_LEVEL_VERBOSE, "Package method does NOT specify a file repository");
}
result = PromiseResultUpdate_HELPER(pp, result,
AddPackageToSchedule(ctx, a, a->packages.package_delete_command,
PACKAGE_ACTION_DELETE, id, "any", "any", pp));
}
else
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Package deletion is as promised -- no match");
}
break;
case PACKAGE_ACTION_REINSTALL:
if (a->packages.package_delete_command == NULL)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Package delete command undefined");
BufferDestroy(expanded);
return PROMISE_RESULT_FAIL;
}
if (!no_version_specified)
{
Log(LOG_LEVEL_VERBOSE, "Schedule package for reinstallation");
if (a->packages.package_add_command == NULL)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Package add command undefined");
BufferDestroy(expanded);
return PROMISE_RESULT_FAIL;
}
// we're deleting a matched package found in a range OR an installed package with no version
if ((matched && package_select_in_range) ||
(installed && no_version_specified))
{
result = PromiseResultUpdate_HELPER(pp, result,
AddPackageToSchedule(ctx, a, a->packages.package_delete_command,
PACKAGE_ACTION_DELETE, id, "any", "any", pp));
}
result = PromiseResultUpdate_HELPER(pp, result,
AddPackageToSchedule(ctx, a, a->packages.package_add_command,
PACKAGE_ACTION_ADD, id, "any", "any", pp));
}
else
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"Package reinstallation cannot be promised -- insufficient version info or no match");
BufferDestroy(expanded);
return PROMISE_RESULT_FAIL;
}
break;
case PACKAGE_ACTION_UPDATE:
{
char inst_arch[CF_MAXVARSIZE];
char inst_ver[CF_MAXVARSIZE];
*inst_ver = '\0';
*inst_arch = '\0';
if ((a->packages.package_file_repositories != NULL))
{
Log(LOG_LEVEL_VERBOSE, "Package method specifies a file repository");
{
VarRef *ref_name = VarRefParseFromScope("name", PACKAGES_CONTEXT_ANYVER);
EvalContextVariablePut(ctx, ref_name, name, CF_DATA_TYPE_STRING, "source=promise");
VarRef *ref_version = VarRefParseFromScope("version", PACKAGES_CONTEXT_ANYVER);
EvalContextVariablePut(ctx, ref_version, "(.*)", CF_DATA_TYPE_STRING, "source=promise");
VarRef *ref_arch = VarRefParseFromScope("arch", PACKAGES_CONTEXT_ANYVER);
EvalContextVariablePut(ctx, ref_arch, arch, CF_DATA_TYPE_STRING, "source=promise");
BufferClear(expanded);
ExpandScalar(ctx, NULL, PACKAGES_CONTEXT_ANYVER, a->packages.package_name_convention, expanded);
EvalContextVariableRemove(ctx, ref_name);
VarRefDestroy(ref_name);
EvalContextVariableRemove(ctx, ref_version);
VarRefDestroy(ref_version);
EvalContextVariableRemove(ctx, ref_arch);
VarRefDestroy(ref_arch);
}
EscapeSpecialChars(BufferData(expanded), refAnyVerEsc, sizeof(refAnyVerEsc), "(.*)","");
if (FindLargestVersionAvail(ctx, largestPackAvail, largestVerAvail, refAnyVerEsc, version,
a->packages.package_file_repositories, a, pp, &result))
{
Log(LOG_LEVEL_VERBOSE, "Using latest version in file repositories; '%s'", largestPackAvail);
strlcpy(id, largestPackAvail, CF_EXPANDSIZE);
}
else
{
Log(LOG_LEVEL_VERBOSE, "No package in file repositories satisfy version constraint");
break;
}
}
else
{
Log(LOG_LEVEL_VERBOSE, "Package method does NOT specify a file repository");
strlcpy(largestVerAvail, version, sizeof(largestVerAvail)); // user-supplied version
}
if (installed)
{
Log(LOG_LEVEL_VERBOSE, "Checking if latest available version is newer than installed...");
if (IsNewerThanInstalled(ctx, name, largestVerAvail, arch, inst_ver, inst_arch, a, pp, &result))
{
Log(LOG_LEVEL_VERBOSE,
"Installed package (%s,%s,%s) [name,version,arch] is older than latest available (%s,%s,%s) [name,version,arch] - updating", name,
inst_ver, inst_arch, name, largestVerAvail, arch);
}
else
{
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a,
"Installed packaged '%s' is up to date, not updating", pp->promiser);
break;
}
}
if (installed ||
(matched && package_select_in_range && !no_version_specified))
{
if (a->packages.package_update_command == NULL)
{
Log(LOG_LEVEL_VERBOSE, "Package update command undefined - failing over to delete then add");
// we need to have the version of installed package
const char *id_del = id;
if (a->packages.package_delete_convention)
{
if (*inst_ver == '\0')
{
inst_ver[0] = '*';
inst_ver[1] = '\0';
}
if (*inst_arch == '\0')
{
inst_arch[0] = '*';
inst_arch[1] = '\0';
}
VarRef *ref_name = VarRefParseFromScope("name", PACKAGES_CONTEXT);
EvalContextVariablePut(ctx, ref_name, name, CF_DATA_TYPE_STRING, "source=promise");
VarRef *ref_version = VarRefParseFromScope("version", PACKAGES_CONTEXT);
EvalContextVariablePut(ctx, ref_version, inst_ver, CF_DATA_TYPE_STRING, "source=promise");
VarRef *ref_arch = VarRefParseFromScope("arch", PACKAGES_CONTEXT);
EvalContextVariablePut(ctx, ref_arch, inst_arch, CF_DATA_TYPE_STRING, "source=promise");
BufferClear(expanded);
ExpandScalar(ctx, NULL, PACKAGES_CONTEXT, a->packages.package_delete_convention, expanded);
id_del = BufferData(expanded);
EvalContextVariableRemove(ctx, ref_name);
VarRefDestroy(ref_name);
EvalContextVariableRemove(ctx, ref_version);
VarRefDestroy(ref_version);
EvalContextVariableRemove(ctx, ref_arch);
VarRefDestroy(ref_arch);
}
Log(LOG_LEVEL_VERBOSE, "Scheduling package with id '%s' for deletion", id_del);
if (a->packages.package_add_command == NULL)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Package add command undefined");
BufferDestroy(expanded);
return PROMISE_RESULT_FAIL;
}
if (a->packages.package_delete_command == NULL)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Package delete command undefined");
BufferDestroy(expanded);
return PROMISE_RESULT_FAIL;
}
result = PromiseResultUpdate_HELPER(pp, result,
AddPackageToSchedule(ctx, a, a->packages.package_delete_command,
PACKAGE_ACTION_DELETE, id_del, "any", "any", pp));
result = PromiseResultUpdate_HELPER(pp, result,
AddPackageToSchedule(ctx, a, a->packages.package_add_command,
PACKAGE_ACTION_ADD, id, "any", "any", pp));
}
else
{
Log(LOG_LEVEL_VERBOSE, "Schedule package for update");
result = PromiseResultUpdate_HELPER(pp, result,
AddPackageToSchedule(ctx, a, a->packages.package_update_command,
PACKAGE_ACTION_UPDATE, id, "any", "any", pp));
}
}
else
{
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Package '%s' cannot be updated -- no match or not installed",
pp->promiser);
result = PromiseResultUpdate_HELPER(pp, result, PROMISE_RESULT_FAIL);
}
break;
}
case PACKAGE_ACTION_PATCH:
if (matched && (!installed))
{
Log(LOG_LEVEL_VERBOSE, "Schedule package for patching");
result = PromiseResultUpdate_HELPER(pp, result,
AddPatchToSchedule(ctx, a, a->packages.package_patch_command,
PACKAGE_ACTION_PATCH, id, "any", "any", pp));
}
else
{
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a,
"Package patch state of '%s' is as promised -- already installed", pp->promiser);
}
break;
case PACKAGE_ACTION_VERIFY:
if ((matched && package_select_in_range) ||
(installed && no_version_specified))
{
Log(LOG_LEVEL_VERBOSE, "Schedule package for verification");
result = PromiseResultUpdate_HELPER(pp, result,
AddPackageToSchedule(ctx, a, a->packages.package_verify_command,
PACKAGE_ACTION_VERIFY, id, "any", "any", pp));
}
else
{
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_INFO, PROMISE_RESULT_FAIL, pp, a, "Package '%s' cannot be verified -- no match", pp->promiser);
BufferDestroy(expanded);
return PROMISE_RESULT_FAIL;
}
break;
default:
break;
}
BufferDestroy(expanded);
return result;
}
/**
@brief Compare a PackageItem to a specific package (n,v,arch) as specified by Attributes a
Called by PatchMatch and PackageMatch.
First, checks the package names are the same (according to CompareCSVName).
Second, checks the architectures are the same or arch is "*"
Third, checks the versions with CompareVersions or version is "*"
@param ctx [in] The evaluation context
@param n [in] the specific name
@param v [in] the specific version
@param arch [in] the specific architecture
@param pi [in] the PackageItem to check
@param a [in] the Attributes specifying how to compare
@param pp [in] the Promise for this operation
@param mode [in] the operating mode, informational for logging
@returns the version comparison result
*/
VersionCmpResult ComparePackages(EvalContext *ctx,
const char *n, const char *v, const char *arch,
PackageItem *pi, const Attributes *a,
const Promise *pp,
const char *mode,
PromiseResult *result)
{
assert(pi != NULL);
assert(a != NULL);
Log(LOG_LEVEL_VERBOSE, "Comparing %s package (%s,%s,%s) "
"to [%s] with given (%s,%s,%s) [name,version,arch]",
mode, pi->name, pi->version, pi->arch, PackageVersionComparatorToString(a->packages.package_select), n, v, arch);
if (CompareCSVName(n, pi->name) != 0)
{
return VERCMP_NO_MATCH;
}
Log(LOG_LEVEL_VERBOSE, "Matched %s name '%s'", mode, n);
if (strcmp(arch, "*") != 0)
{
if (strcmp(arch, pi->arch) != 0)
{
return VERCMP_NO_MATCH;
}
Log(LOG_LEVEL_VERBOSE, "Matched %s arch '%s'", mode, arch);
}
else
{
Log(LOG_LEVEL_VERBOSE, "Matched %s wildcard arch '%s'", mode, arch);
}
if (strcmp(v, "*") == 0)
{
Log(LOG_LEVEL_VERBOSE, "Matched %s wildcard version '%s'", mode, v);
return VERCMP_MATCH;
}
VersionCmpResult vc = CompareVersions(ctx, pi->version, v, a, pp, result);
Log(LOG_LEVEL_VERBOSE,
"Version comparison returned %s for %s package (%s,%s,%s) "
"to [%s] with given (%s,%s,%s) [name,version,arch]",
vc == VERCMP_MATCH ? "MATCH" : vc == VERCMP_NO_MATCH ? "NO_MATCH" : "ERROR",
mode,
pi->name, pi->version, pi->arch,
PackageVersionComparatorToString(a->packages.package_select),
n, v, arch);
return vc;
}
/**
@brief Finds a specific package (n,v,a) [name, version, architecture] as specified by Attributes attr
Called by VerifyPromisedPatch.
Goes through all the installed packages to find matches for the given attributes.
The package manager is checked against attr.packages.package_list_command.
The package name is checked as a regular expression. then (n,v,a) with ComparePackages.
@param ctx [in] The evaluation context
@param n [in] the specific name
@param v [in] the specific version
@param a [in] the specific architecture
@param attr [in] the Attributes specifying how to compare
@param pp [in] the Promise for this operation
@param mode [in] the operating mode, informational for logging
@returns the version comparison result
*/
static VersionCmpResult PatchMatch(EvalContext *ctx,
const char *n, const char *v, const char *a,
const Attributes *attr, const Promise *pp,
const char* mode,
PromiseResult *result)
{
assert(attr != NULL);
PackageManager *mp;
// This REALLY needs some commenting
for (mp = INSTALLED_PACKAGE_LISTS; mp != NULL; mp = mp->next)
{
if (strcmp(mp->manager, attr->packages.package_list_command) == 0)
{
break;
}
}
Log(LOG_LEVEL_VERBOSE, "PatchMatch: looking for %s to [%s] with given (%s,%s,%s) [name,version,arch] in package manager %s",
mode, PackageVersionComparatorToString(attr->packages.package_select), n, v, a, mp->manager);
for (PackageItem *pi = mp->patch_list; pi != NULL; pi = pi->next)
{
if (FullTextMatch(ctx, n, pi->name)) /* Check regexes */
{
Log(LOG_LEVEL_VERBOSE, "PatchMatch: regular expression match succeeded for %s against %s", n, pi->name);
return VERCMP_MATCH;
}
else
{
VersionCmpResult res = ComparePackages(ctx, n, v, a, pi, attr, pp, mode, result);
if (res != VERCMP_NO_MATCH)
{
Log(LOG_LEVEL_VERBOSE, "PatchMatch: patch comparison for %s was decisive: %s", pi->name, res == VERCMP_MATCH ? "MATCH" : "ERROR");
return res;
}
}
}
Log(LOG_LEVEL_VERBOSE, "PatchMatch did not match the constraints of promise (%s,%s,%s) [name,version,arch]", n, v, a);
return VERCMP_NO_MATCH;
}
/**
@brief Finds a specific package (n,v,a) [name, version, architecture] as specified by Attributes attr
Called by CheckPackageState.
Goes through all the installed packages to find matches for the given attributes.
The package manager is checked against attr.packages.package_list_command.
The (n,v,a) search is done with ComparePackages.
@param ctx [in] The evaluation context
@param n [in] the specific name
@param v [in] the specific version
@param a [in] the specific architecture
@param attr [in] the Attributes specifying how to compare
@param pp [in] the Promise for this operation
@param mode [in] the operating mode, informational for logging
@returns the version comparison result
*/
static VersionCmpResult PackageMatch(EvalContext *ctx,
const char *n, const char *v, const char *a,
const Attributes *attr,
const Promise *pp,
const char* mode,
PromiseResult *result)
/*
* Returns VERCMP_MATCH if any installed packages match (n,v,a), VERCMP_NO_MATCH otherwise, VERCMP_ERROR on error.
* The mode is informational
*/
{
assert(attr != NULL);
PackageManager *mp = NULL;
// This REALLY needs some commenting
for (mp = INSTALLED_PACKAGE_LISTS; mp != NULL; mp = mp->next)
{
if (strcmp(mp->manager, attr->packages.package_list_command) == 0)
{
break;
}
}
Log(LOG_LEVEL_VERBOSE, "PackageMatch: looking for %s (%s,%s,%s) [name,version,arch] in package manager %s", mode, n, v, a, mp->manager);
for (PackageItem *pi = mp->pack_list; pi != NULL; pi = pi->next)
{
VersionCmpResult res = ComparePackages(ctx, n, v, a, pi, attr, pp, mode, result);
if (res != VERCMP_NO_MATCH)
{
Log(LOG_LEVEL_VERBOSE, "PackageMatch: package comparison for %s %s was decisive: %s", mode, pi->name, res == VERCMP_MATCH ? "MATCH" : "ERROR");
return res;
}
}
Log(LOG_LEVEL_VERBOSE, "PackageMatch did not find %s packages to match the constraints of promise (%s,%s,%s) [name,version,arch]", mode, n, v, a);
return VERCMP_NO_MATCH;
}
/**
@brief Check if the operation should be scheduled based on the package policy, if the package matches, and if it's installed
Called by CheckPackageState.
Uses a.packages.package_policy to determine operating mode.
The use of matches and installed depends on the package_policy:
* PACKAGE_ACTION_DELETE: schedule if (matches AND installed)
* PACKAGE_ACTION_REINSTALL: schedule if (matches AND installed)
* all other policies: schedule if (not matches OR not installed)
@param ctx [in] The evaluation context
@param a [in] the Attributes specifying the package policy
@param pp [in] the Promise for this operation
@param matches [in] whether the package matches
@param installed [in] whether the package is installed
@returns whether the package operation should be scheduled
*/
static bool WillSchedulePackageOperation(EvalContext *ctx, const Attributes *a, const Promise *pp, int matches, int installed)
{
assert(a != NULL);
assert(pp != NULL);
PackageAction policy = a->packages.package_policy;
Log(LOG_LEVEL_DEBUG, "WillSchedulePackageOperation: on entry, action %s: package %s matches = %s, installed = %s.",
PackageAction2String(policy), pp->promiser, matches ? "yes" : "no", installed ? "yes" : "no");
switch (policy)
{
case PACKAGE_ACTION_DELETE:
if (matches && installed)
{
Log(LOG_LEVEL_VERBOSE, "WillSchedulePackageOperation: Package %s to be deleted is installed.", pp->promiser);
return true;
}
else
{
Log(LOG_LEVEL_DEBUG, "WillSchedulePackageOperation: Package %s can't be deleted if it's not installed, NOOP.", pp->promiser);
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Package %s to be deleted does not exist anywhere",
pp->promiser);
}
break;
case PACKAGE_ACTION_REINSTALL:
if (matches && installed)
{
Log(LOG_LEVEL_VERBOSE, "WillSchedulePackageOperation: Package %s to be reinstalled is already installed.", pp->promiser);
return true;
}
else
{
Log(LOG_LEVEL_DEBUG, "WillSchedulePackageOperation: Package %s already installed, NOOP.", pp->promiser);
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Package '%s' already installed and matches criteria",
pp->promiser);
}
break;
default:
if (!matches) // why do we schedule a 'not matched' operation?
{
return true;
}
else if (!installed) // matches and not installed
{
return true;
}
else // matches and installed
{
Log(LOG_LEVEL_DEBUG, "WillSchedulePackageOperation: Package %s already installed, NOOP.", pp->promiser);
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Package '%s' already installed and matches criteria",
pp->promiser);
}
break;
}
return false;
}
/**
@brief Checks the state of a specific package (name,version,arch) as specified by Attributes a
Called by VerifyPromisedPackage.
* copies a into a2, overrides a2.packages.package_select to PACKAGE_VERSION_COMPARATOR_EQ
* VersionCmpResult installed = check if (name,*,arch) is installed with PackageMatch (note version override!)
* if PackageMatch returned an error, fail the promise
* VersionCmpResult matches = check if (name,version,arch) is installed with PackageMatch
* if PackageMatch returned an error, fail the promise
* if WillSchedulePackageOperation with "matches" and "installed" passes, call SchedulePackageOp on the package
@param ctx [in] The evaluation context
@param a [in] the Attributes specifying how to compare
@param pp [in] the Promise for this operation
@param name [in] the specific name
@param version [in] the specific version
@param arch [in] the specific architecture
@param no_version [in] ignore the version, be cool
@returns the promise result
*/
static PromiseResult CheckPackageState(EvalContext *ctx, const Attributes *a, const Promise *pp, const char *name, const char *version,
const char *arch, bool no_version)
{
assert(a != NULL);
assert(pp != NULL); // Dereferenced in cfPS macros
PromiseResult result = PROMISE_RESULT_NOOP;
/* Horrible */
Attributes a2 = *a;
a2.packages.package_select = PACKAGE_VERSION_COMPARATOR_EQ;
VersionCmpResult installed = PackageMatch(ctx, name, "*", arch, &a2, pp, "[installed]", &result);
Log(LOG_LEVEL_VERBOSE, "CheckPackageState: Installed package match for (%s,%s,%s) [name,version,arch] was decisive: %s",
name, "*", arch, installed == VERCMP_MATCH ? "MATCH" : "ERROR-OR-NOMATCH");
if (installed == VERCMP_ERROR)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, &a2, "Failure trying to compare installed package versions");
result = PromiseResultUpdate_HELPER(pp, result, PROMISE_RESULT_FAIL);
return result;
}
VersionCmpResult matches = PackageMatch(ctx, name, version, arch, &a2, pp, "[available]", &result);
Log(LOG_LEVEL_VERBOSE, "CheckPackageState: Available package match for (%s,%s,%s) [name,version,arch] was decisive: %s",
name, version, arch, matches == VERCMP_MATCH ? "MATCH" : "ERROR-OR-NOMATCH");
if (matches == VERCMP_ERROR)
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, &a2, "Failure trying to compare available package versions");
result = PromiseResultUpdate_HELPER(pp, result, PROMISE_RESULT_FAIL);
return result;
}
if (WillSchedulePackageOperation(ctx, &a2, pp, matches, installed))
{
Log(LOG_LEVEL_VERBOSE, "CheckPackageState: matched package (%s,%s,%s) [name,version,arch]; scheduling operation", name, version, arch);
return SchedulePackageOp(ctx, name, version, arch, installed, matches, no_version, a, pp);
}
return result;
}
/**
@brief Verifies a promised patch operation as defined by a and pp
Called by VerifyPackagesPromise for the patch operation.
* package name is pp->promiser
* installed and matches counts = 0
* copies a into a2 and overrides a2.packages.package_select to PACKAGE_VERSION_COMPARATOR_EQ
* promise result starts as NOOP
* if package version is given
* * for arch = each architecture requested in a2, or (if none given) any architecture "*"
* * * installed1 = PatchMatch(a2, name, any version "*", any architecture "*")
* * * matches1 = PatchMatch(a2, name, requested version, arch)
* * * if either installed1 or matches1 failed, return promise error
* * * else, installed += installed1; matches += matches1
* else if package_version_regex is given
* * assume that package_name_regex and package_arch_regex are also given and use the 3 regexes to extract name, version, arch
* * * installed = PatchMatch(a2, matched name, any version "*", any architecture "*")
* * * matches = PatchMatch(a2, matched name, matched version, matched architecture)
* * * if either installed or matches failed, return promise error
* else (no explicit version is given) (SAME LOOP AS EXPLICIT VERSION LOOP ABOVE)
* * no_version = true
* * for arch = each architecture requested in a2, or (if none given) any architecture "*"
* * * requested version = any version '*'
* * * installed1 = PatchMatch(a2, name, any version "*", any architecture "*")
* * * matches1 = PatchMatch(a2, name, requested version '*', arch)
* * * if either installed1 or matches1 failed, return promise error
* * * else, installed += installed1; matches += matches1
* finally, call SchedulePackageOp with the found name, version, arch, installed, matches, no_version
@param ctx [in] The evaluation context
@param a [in] the Attributes specifying how to compare
@param pp [in] the Promise for this operation
@returns the promise result (failure or NOOP)
*/
static PromiseResult VerifyPromisedPatch(EvalContext *ctx, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
assert(pp != NULL);
char version[CF_MAXVARSIZE];
char name[CF_MAXVARSIZE];
char arch[CF_MAXVARSIZE];
char *package = pp->promiser;
int matches = 0, installed = 0, no_version = false;
Rlist *rp;
/* Horrible */
Attributes a2 = *a;
a2.packages.package_select = PACKAGE_VERSION_COMPARATOR_EQ;
PromiseResult result = PROMISE_RESULT_NOOP;
if (a2.packages.package_version) /* The version is specified explicitly */
{
// Note this loop will run if rp is NULL
for (rp = a2.packages.package_architectures; ; rp = rp->next)
{
strlcpy(name, pp->promiser, CF_MAXVARSIZE);
strlcpy(version, a2.packages.package_version, CF_MAXVARSIZE);
strlcpy(arch,
(rp == NULL) ? "*" : RlistScalarValue(rp),
CF_MAXVARSIZE);
VersionCmpResult installed1 = PatchMatch(ctx, name, "*", "*", &a2, pp, "[installed1]", &result);
VersionCmpResult matches1 = PatchMatch(ctx, name, version, arch, &a2, pp, "[available1]", &result);
if ((installed1 == VERCMP_ERROR) || (matches1 == VERCMP_ERROR))
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, &a2, "Failure trying to compare package versions");
result = PromiseResultUpdate_HELPER(pp, result, PROMISE_RESULT_FAIL);
return result;
}
installed += installed1;
matches += matches1;
if (rp == NULL) break; // Note we exit the loop explicitly here
}
}
else if (a2.packages.package_version_regex) // version is not given, but a version regex is
{
/* The name, version and arch are to be extracted from the promiser */
strlcpy(version, ExtractFirstReference(a2.packages.package_version_regex, package), CF_MAXVARSIZE);
strlcpy(name, ExtractFirstReference(a2.packages.package_name_regex, package), CF_MAXVARSIZE);
strlcpy(arch, ExtractFirstReference(a2.packages.package_arch_regex, package), CF_MAXVARSIZE);
installed = PatchMatch(ctx, name, "*", "*", &a2, pp, "[installed]", &result);
matches = PatchMatch(ctx, name, version, arch, &a2, pp, "[available]", &result);
if ((installed == VERCMP_ERROR) || (matches == VERCMP_ERROR))
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, &a2, "Failure trying to compare package versions");
result = PromiseResultUpdate_HELPER(pp, result, PROMISE_RESULT_FAIL);
return result;
}
}
else // the desired package version was not specified
{
no_version = true;
// Note this loop will run if rp is NULL
for (rp = a2.packages.package_architectures; ; rp = rp->next)
{
strlcpy(name, pp->promiser, CF_MAXVARSIZE);
strlcpy(version, "*", CF_MAXVARSIZE);
strlcpy(arch,
(rp == NULL) ? "*" : RlistScalarValue(rp),
CF_MAXVARSIZE);
VersionCmpResult installed1 = PatchMatch(ctx, name, "*", "*", &a2, pp, "[installed1]", &result);
VersionCmpResult matches1 = PatchMatch(ctx, name, version, arch, &a2, pp, "[available1]", &result);
if ((installed1 == VERCMP_ERROR) || (matches1 == VERCMP_ERROR))
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, &a2, "Failure trying to compare package versions");
result = PromiseResultUpdate_HELPER(pp, result, PROMISE_RESULT_FAIL);
return result;
}
installed += installed1;
matches += matches1;
if (rp == NULL) break; // Note we exit the loop explicitly here
}
}
Log(LOG_LEVEL_VERBOSE, "%d patch(es) matching the name '%s' already installed", installed, name);
Log(LOG_LEVEL_VERBOSE, "%d patch(es) match the promise body's criteria fully", matches);
SchedulePackageOp(ctx, name, version, arch, installed, matches, no_version, a, pp);
return PROMISE_RESULT_NOOP;
}
/**
@brief Verifies a promised package operation as defined by a and pp
Called by VerifyPackagesPromise for any non-patch operation.
* package name is pp->promiser
* promise result starts as NOOP
* if package version is given
* * if no architecture given, the promise result comes from CheckPackageState with name, version, any architecture '*', no_version=false
* * else if architectures were given, the promise result comes from CheckPackageState with name, version, arch, no_version=false FOR EACH ARCHITECTURE
* else if package_version_regex is given
* * assume that package_name_regex and package_arch_regex are also given and use the 3 regexes to extract name, version, arch
* * if the arch extraction failed, use any architecture '*'
* * the promise result comes from CheckPackageState with name, version, arch, no_version=false)
* else (no explicit version is given) (SAME LOOP AS EXPLICIT VERSION LOOP ABOVE)
* * if no architecture given, the promise result comes from CheckPackageState with name, any version "*", any architecture '*', no_version=true
* * else if architectures were given, the promise result comes from CheckPackageState with name, any version "*", arch, no_version=true FOR EACH ARCHITECTURE
@param ctx [in] The evaluation context
@param a [in] the Attributes specifying how to compare
@param pp [in] the Promise for this operation
@returns the promise result as set by CheckPackageState
*/
static PromiseResult VerifyPromisedPackage(EvalContext *ctx, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
assert(pp != NULL);
const char *package = pp->promiser;
const Packages *const pkgs = &(a->packages);
PromiseResult result = PROMISE_RESULT_NOOP;
if (pkgs->package_version)
{
/* The version is specified separately */
Log(LOG_LEVEL_VERBOSE, "Package version %s specified explicitly in promise body", pkgs->package_version);
if (pkgs->package_architectures == NULL)
{
Log(LOG_LEVEL_VERBOSE, " ... trying any arch '*'");
result = PromiseResultUpdate_HELPER(pp, result, CheckPackageState(ctx, a, pp, package, pkgs->package_version, "*", false));
}
else
{
for (Rlist *rp = pkgs->package_architectures; rp != NULL; rp = rp->next)
{
Log(LOG_LEVEL_VERBOSE, " ... trying listed arch '%s'", RlistScalarValue(rp));
result = PromiseResultUpdate_HELPER(pp, result,
CheckPackageState(ctx, a, pp, package, pkgs->package_version,
RlistScalarValue(rp), false));
}
}
}
else if (pkgs->package_version_regex)
{
/* The name, version and arch are to be extracted from the promiser */
Log(LOG_LEVEL_VERBOSE, "Package version %s specified implicitly in promiser's name", pkgs->package_version_regex);
char version[CF_MAXVARSIZE];
char name[CF_MAXVARSIZE];
char arch[CF_MAXVARSIZE];
strlcpy(version, ExtractFirstReference(pkgs->package_version_regex, package), CF_MAXVARSIZE);
strlcpy(name, ExtractFirstReference(pkgs->package_name_regex, package), CF_MAXVARSIZE);
strlcpy(arch, ExtractFirstReference(pkgs->package_arch_regex, package), CF_MAXVARSIZE);
if (!arch[0])
{
strlcpy(arch, "*", CF_MAXVARSIZE);
}
if (strcmp(arch, "CF_NOMATCH") == 0) // no match on arch regex, use any arch
{
strlcpy(arch, "*", CF_MAXVARSIZE);
}
Log(LOG_LEVEL_VERBOSE, " ... trying arch '%s' and version '%s'", arch, version);
result = PromiseResultUpdate_HELPER(pp, result, CheckPackageState(ctx, a, pp, name, version, arch, false));
}
else
{
Log(LOG_LEVEL_VERBOSE, "Package version was not specified");
if (pkgs->package_architectures == NULL)
{
Log(LOG_LEVEL_VERBOSE, " ... trying any arch '*' and any version '*'");
result = PromiseResultUpdate_HELPER(pp, result, CheckPackageState(ctx, a, pp, package, "*", "*", true));
}
else
{
for (Rlist *rp = pkgs->package_architectures; rp != NULL; rp = rp->next)
{
Log(LOG_LEVEL_VERBOSE, " ... trying listed arch '%s' and any version '*'", RlistScalarValue(rp));
result = PromiseResultUpdate_HELPER(pp, result, CheckPackageState(ctx, a, pp, package, "*", RlistScalarValue(rp), true));
}
}
}
return result;
}
/** Execute scheduled operations **/
/**
@brief Central dispatcher for scheduled operations
Called by ExecutePackageSchedule.
Almost identical to ExecutePatch.
* verify = false
* for each PackageManager pm in the schedule
* * if pm->pack_list is empty or the scheduled pm->action doesn't match the given action, skip this pm
* * estimate the size of the command string from pm->pack_list and pm->policy (SHOULD USE Buffer)
* * from the first PackageItem in pm->pack_list, get the Promise pp and its Attributes a
* * switch(action)
* * * case ADD:
* * * * command_string = a.packages.package_add_command + estimated_size room for package names
* * * case DELETE:
* * * * command_string = a.packages.package_delete_command + estimated_size room for package names
* * * case UPDATE:
* * * * command_string = a.packages.package_update_command + estimated_size room for package names
* * * case VERIFY:
* * * * command_string = a.packages.package_verify_command + estimated_size room for package names
* * * * verify = true
* * if the command string ends with $, run it with ExecPackageCommand(command, verify) and magic the promise evaluation
* * else, switch(pm->policy)
* * * case INDIVIDUAL:
* * * * for each PackageItem in the pack_list, build the command and run it with ExecPackageCommand(command, verify) and magic the promise evaluation
* * * * NOTE with file repositories and ADD/UPDATE operations, the package name gets the repo path too
* * * * NOTE special treatment of PACKAGE_IGNORED_CFE_INTERNAL
* * * case BULK:
* * * * for all PackageItems in the pack_list, build the command and run it with ExecPackageCommand(command, verify) and magic the promise evaluation
* * * * NOTE with file repositories and ADD/UPDATE operations, the package name gets the repo path too
* * * * NOTE special treatment of PACKAGE_IGNORED_CFE_INTERNAL
* * clean up command_string
* if the operation was not a verification, InvalidateSoftwareCache
@param ctx [in] The evaluation context
@param schedule [in] the PackageManager list with the operations schedule
@param action [in] the PackageAction desired
@returns boolean success/fail (fail only on ProgrammingError, should never happen)
*/
static bool ExecuteSchedule(EvalContext *ctx, const PackageManager *schedule, PackageAction action)
{
bool verify = false;
for (const PackageManager *pm = schedule; pm != NULL; pm = pm->next)
{
if (pm->action != action)
{
continue;
}
if (pm->pack_list == NULL)
{
continue;
}
size_t estimated_size = 0;
for (const PackageItem *pi = pm->pack_list; pi != NULL; pi = pi->next)
{
size_t size = strlen(pi->name) + strlen(" ");
switch (pm->policy)
{
case PACKAGE_ACTION_POLICY_INDIVIDUAL:
if (size > estimated_size)
{
estimated_size = size + CF_MAXVARSIZE;
}
break;
case PACKAGE_ACTION_POLICY_BULK:
estimated_size += size + CF_MAXVARSIZE;
break;
default:
break;
}
}
const Promise *const pp = pm->pack_list->pp;
Attributes a = GetPackageAttributes(ctx, pp);
char *command_string = NULL;
switch (action)
{
case PACKAGE_ACTION_ADD:
Log(LOG_LEVEL_VERBOSE, "Execute scheduled package addition");
if (a.packages.package_add_command == NULL)
{
ProgrammingError("Package add command undefined");
return false;
}
Log(LOG_LEVEL_INFO, "Installing %-.39s...", pp->promiser);
estimated_size += strlen(a.packages.package_add_command) + 2;
command_string = xmalloc(estimated_size);
strcpy(command_string, a.packages.package_add_command);
break;
case PACKAGE_ACTION_DELETE:
Log(LOG_LEVEL_VERBOSE, "Execute scheduled package deletion");
if (a.packages.package_delete_command == NULL)
{
ProgrammingError("Package delete command undefined");
return false;
}
Log(LOG_LEVEL_INFO, "Deleting %-.39s...", pp->promiser);
estimated_size += strlen(a.packages.package_delete_command) + 2;
command_string = xmalloc(estimated_size);
strcpy(command_string, a.packages.package_delete_command);
break;
case PACKAGE_ACTION_UPDATE:
Log(LOG_LEVEL_VERBOSE, "Execute scheduled package update");
if (a.packages.package_update_command == NULL)
{
ProgrammingError("Package update command undefined");
return false;
}
Log(LOG_LEVEL_INFO, "Updating %-.39s...", pp->promiser);
estimated_size += strlen(a.packages.package_update_command) + 2;
command_string = xcalloc(1, estimated_size);
strcpy(command_string, a.packages.package_update_command);
break;
case PACKAGE_ACTION_VERIFY:
Log(LOG_LEVEL_VERBOSE, "Execute scheduled package verification");
if (a.packages.package_verify_command == NULL)
{
ProgrammingError("Package verify command undefined");
return false;
}
estimated_size += strlen(a.packages.package_verify_command) + 2;
command_string = xmalloc(estimated_size);
strcpy(command_string, a.packages.package_verify_command);
verify = true;
break;
default:
ProgrammingError("Unknown action attempted");
return false;
}
/* if the command ends with $ then we assume the package manager does not accept package names */
if (*(command_string + strlen(command_string) - 1) == '$')
{
*(command_string + strlen(command_string) - 1) = '\0';
Log(LOG_LEVEL_VERBOSE, "Command does not allow arguments");
PromiseResult result = PROMISE_RESULT_NOOP;
EvalContextStackPushPromiseFrame(ctx, pp);
if (EvalContextStackPushPromiseIterationFrame(ctx, NULL))
{
if (ExecPackageCommand(ctx, command_string, verify, true, &a, pp, &result))
{
Log(LOG_LEVEL_VERBOSE, "Package schedule execution ok (outcome cannot be promised by cf-agent)");
}
else
{
Log(LOG_LEVEL_ERR, "Package schedule execution failed");
}
EvalContextStackPopFrame(ctx);
}
EvalContextStackPopFrame(ctx);
EvalContextLogPromiseIterationOutcome(ctx, pp, result);
}
else
{
strcat(command_string, " ");
Log(LOG_LEVEL_VERBOSE, "Command prefix '%s'", command_string);
if (pm->policy == PACKAGE_ACTION_POLICY_INDIVIDUAL)
{
for (const PackageItem *pi = pm->pack_list; pi != NULL; pi = pi->next)
{
const Promise *const ppi = pi->pp;
Attributes a = GetPackageAttributes(ctx, ppi);
const size_t command_len = strlen(command_string);
char *offset = command_string + command_len;
if ((a.packages.package_file_repositories) && ((action == PACKAGE_ACTION_ADD) || (action == PACKAGE_ACTION_UPDATE)))
{
const char *sp = PrefixLocalRepository(a.packages.package_file_repositories, pi->name);
if (sp != NULL)
{
strlcat(offset, sp, estimated_size - command_len);
}
else
{
continue;
}
}
else
{
strcat(offset, pi->name);
}
PromiseResult result = PROMISE_RESULT_NOOP;
EvalContextStackPushPromiseFrame(ctx, ppi);
if (EvalContextStackPushPromiseIterationFrame(ctx, NULL))
{
bool ok = ExecPackageCommand(ctx, command_string, verify, true, &a, ppi, &result);
if (StringSafeEqual(pi->name, PACKAGE_IGNORED_CFE_INTERNAL))
{
Log(LOG_LEVEL_DEBUG, "ExecuteSchedule: Ignoring outcome for special package '%s'", pi->name);
}
else if (ok)
{
Log(LOG_LEVEL_VERBOSE,
"Package schedule execution ok for '%s' (outcome cannot be promised by cf-agent)",
pi->name);
}
else
{
Log(LOG_LEVEL_ERR, "Package schedule execution failed for '%s'", pi->name);
}
EvalContextStackPopFrame(ctx);
}
EvalContextStackPopFrame(ctx);
EvalContextLogPromiseIterationOutcome(ctx, ppi, result);
*offset = '\0';
}
}
else if (pm->policy == PACKAGE_ACTION_POLICY_BULK)
{
for (const PackageItem *pi = pm->pack_list; pi != NULL; pi = pi->next)
{
if (pi->name)
{
const size_t command_len = strlen(command_string);
char *offset = command_string + command_len;
if (a.packages.package_file_repositories &&
(action == PACKAGE_ACTION_ADD ||
action == PACKAGE_ACTION_UPDATE))
{
const char *sp = PrefixLocalRepository(a.packages.package_file_repositories, pi->name);
if (sp != NULL)
{
strlcpy(offset, sp, estimated_size - command_len);
}
else
{
break;
}
}
else
{
strcpy(offset, pi->name);
}
strcat(command_string, " ");
}
}
PromiseResult result = PROMISE_RESULT_NOOP;
EvalContextStackPushPromiseFrame(ctx, pp);
if (EvalContextStackPushPromiseIterationFrame(ctx, NULL))
{
bool ok = ExecPackageCommand(ctx, command_string, verify, true, &a, pp, &result);
for (const PackageItem *pi = pm->pack_list; pi != NULL; pi = pi->next)
{
if (StringSafeEqual(pi->name, PACKAGE_IGNORED_CFE_INTERNAL))
{
Log(LOG_LEVEL_DEBUG, "ExecuteSchedule: Ignoring outcome for special package '%s'", pi->name);
}
else if (ok)
{
Log(LOG_LEVEL_VERBOSE,
"Bulk package schedule execution ok for '%s' (outcome cannot be promised by cf-agent)",
pi->name);
}
else
{
Log(LOG_LEVEL_ERR, "Bulk package schedule execution failed somewhere - unknown outcome for '%s'",
pi->name);
}
}
EvalContextStackPopFrame(ctx);
}
EvalContextStackPopFrame(ctx);
EvalContextLogPromiseIterationOutcome(ctx, pp, result);
}
}
if (command_string)
{
free(command_string);
}
}
/* We have performed some modification operation on packages, our cache is invalid */
if (!verify)
{
InvalidateSoftwareCache();
}
return true;
}
/**
@brief Central dispatcher for scheduled patch operations
Called by ExecutePackageSchedule.
Almost identical to ExecuteSchedule except it only accepts the
PATCH PackageAction and operates on the PackageManagers' patch_list.
* for each PackageManager pm in the schedule
* * if pm->patch_list is empty or the scheduled pm->action doesn't match the given action, skip this pm
* * estimate the size of the command string from pm->patch_list and pm->policy (SHOULD USE Buffer)
* * from the first PackageItem in pm->patch_list, get the Promise pp and its Attributes a
* * switch(action)
* * * case PATCH:
* * * * command_string = a.packages.package_patch_command + estimated_size room for package names
* * if the command string ends with $, run it with ExecPackageCommand(command, verify) and magic the promise evaluation
* * else, switch(pm->policy)
* * * case INDIVIDUAL:
* * * * for each PackageItem in the patch_list, build the command and run it with ExecPackageCommand(command, verify) and magic the promise evaluation
* * * * NOTE with file repositories and ADD/UPDATE operations, the package name gets the repo path too
* * * * NOTE special treatment of PACKAGE_IGNORED_CFE_INTERNAL
* * * case BULK:
* * * * for all PackageItems in the patch_list, build the command and run it with ExecPackageCommand(command, verify) and magic the promise evaluation
* * * * NOTE with file repositories and ADD/UPDATE operations, the package name gets the repo path too
* * * * NOTE special treatment of PACKAGE_IGNORED_CFE_INTERNAL
* * clean up command_string
* InvalidateSoftwareCache
@param ctx [in] The evaluation context
@param schedule [in] the PackageManager list with the operations schedule
@param action [in] the PackageAction desired
@returns boolean success/fail (fail only on ProgrammingError, should never happen)
*/
static bool ExecutePatch(EvalContext *ctx, const PackageManager *schedule, PackageAction action)
{
for (const PackageManager *pm = schedule; pm != NULL; pm = pm->next)
{
if (pm->action != action)
{
continue;
}
if (pm->patch_list == NULL)
{
continue;
}
size_t estimated_size = 0;
for (const PackageItem *pi = pm->patch_list; pi != NULL; pi = pi->next)
{
size_t size = strlen(pi->name) + strlen(" ");
switch (pm->policy)
{
case PACKAGE_ACTION_POLICY_INDIVIDUAL:
if (size > estimated_size)
{
estimated_size = size;
}
break;
case PACKAGE_ACTION_POLICY_BULK:
estimated_size += size;
break;
default:
break;
}
}
char *command_string = NULL;
const Promise *const pp = pm->patch_list->pp;
Attributes a = GetPackageAttributes(ctx, pp);
switch (action)
{
case PACKAGE_ACTION_PATCH:
Log(LOG_LEVEL_VERBOSE, "Execute scheduled package patch");
if (a.packages.package_patch_command == NULL)
{
ProgrammingError("Package patch command undefined");
return false;
}
command_string = xmalloc(estimated_size + strlen(a.packages.package_patch_command) + 2);
strcpy(command_string, a.packages.package_patch_command);
break;
default:
ProgrammingError("Unknown action attempted");
return false;
}
/* if the command ends with $ then we assume the package manager does not accept package names */
if (command_string[strlen(command_string) - 1] == '$')
{
command_string[strlen(command_string) - 1] = '\0';
Log(LOG_LEVEL_VERBOSE, "Command does not allow arguments");
PromiseResult result = PROMISE_RESULT_NOOP;
EvalContextStackPushPromiseFrame(ctx, pp);
if (EvalContextStackPushPromiseIterationFrame(ctx, NULL))
{
if (ExecPackageCommand(ctx, command_string, false, true, &a, pp, &result))
{
Log(LOG_LEVEL_VERBOSE, "Package patching seemed to succeed (outcome cannot be promised by cf-agent)");
}
else
{
Log(LOG_LEVEL_ERR, "Package patching failed");
}
EvalContextStackPopFrame(ctx);
}
EvalContextStackPopFrame(ctx);
EvalContextLogPromiseIterationOutcome(ctx, pp, result);
}
else
{
strcat(command_string, " ");
Log(LOG_LEVEL_VERBOSE, "Command prefix '%s'", command_string);
switch (pm->policy)
{
case PACKAGE_ACTION_POLICY_INDIVIDUAL:
for (const PackageItem *pi = pm->patch_list; pi != NULL; pi = pi->next)
{
char *offset = command_string + strlen(command_string);
strcat(offset, pi->name);
PromiseResult result = PROMISE_RESULT_NOOP;
EvalContextStackPushPromiseFrame(ctx, pp);
if (EvalContextStackPushPromiseIterationFrame(ctx, NULL))
{
bool ok = ExecPackageCommand(ctx, command_string, false, true, &a, pp, &result);
if (StringSafeEqual(pi->name, PACKAGE_IGNORED_CFE_INTERNAL))
{
Log(LOG_LEVEL_DEBUG, "ExecutePatch: Ignoring outcome for special package '%s'", pi->name);
}
else if (ok)
{
Log(LOG_LEVEL_VERBOSE,
"Package schedule execution ok for '%s' (outcome cannot be promised by cf-agent)",
pi->name);
}
else
{
Log(LOG_LEVEL_ERR, "Package schedule execution failed for '%s'", pi->name);
}
EvalContextStackPopFrame(ctx);
}
EvalContextStackPopFrame(ctx);
EvalContextLogPromiseIterationOutcome(ctx, pp, result);
*offset = '\0';
}
break;
case PACKAGE_ACTION_POLICY_BULK:
for (const PackageItem *pi = pm->patch_list; pi != NULL; pi = pi->next)
{
if (pi->name)
{
strcat(command_string, pi->name);
strcat(command_string, " ");
}
}
PromiseResult result = PROMISE_RESULT_NOOP;
EvalContextStackPushPromiseFrame(ctx, pp);
if (EvalContextStackPushPromiseIterationFrame(ctx, NULL))
{
bool ok = ExecPackageCommand(ctx, command_string, false, true, &a, pp, &result);
for (const PackageItem *pi = pm->patch_list; pi != NULL; pi = pi->next)
{
if (StringSafeEqual(pi->name, PACKAGE_IGNORED_CFE_INTERNAL))
{
Log(LOG_LEVEL_DEBUG, "ExecutePatch: Ignoring outcome for special package '%s'", pi->name);
}
else if (ok)
{
Log(LOG_LEVEL_VERBOSE,
"Bulk package schedule execution ok for '%s' (outcome cannot be promised by cf-agent)",
pi->name);
}
else
{
Log(LOG_LEVEL_ERR, "Bulk package schedule execution failed somewhere - unknown outcome for '%s'",
pi->name);
}
}
EvalContextStackPopFrame(ctx);
}
EvalContextStackPopFrame(ctx);
EvalContextLogPromiseIterationOutcome(ctx, pp, result);
break;
default:
break;
}
}
if (command_string)
{
free(command_string);
}
}
/* We have performed some operation on packages, our cache is invalid */
InvalidateSoftwareCache();
return true;
}
/**
@brief Ordering manager for scheduled package operations
Called by ExecuteScheduledPackages.
* ExecuteSchedule(schedule, DELETE)
* ExecuteSchedule(schedule, ADD)
* ExecuteSchedule(schedule, UPDATE)
* ExecutePatch(schedule, PATCH)
* ExecuteSchedule(schedule, VERIFY)
@param ctx [in] The evaluation context
@param schedule [in] the PackageManager list with the operations schedule
*/
static void ExecutePackageSchedule(EvalContext *ctx, PackageManager *schedule)
{
Log(LOG_LEVEL_VERBOSE, "Offering the following package-promise suggestions to the managers");
/* Normal ordering */
Log(LOG_LEVEL_VERBOSE, "Deletion schedule...");
if (!ExecuteSchedule(ctx, schedule, PACKAGE_ACTION_DELETE))
{
Log(LOG_LEVEL_ERR, "Aborting package schedule");
return;
}
Log(LOG_LEVEL_VERBOSE, "Addition schedule...");
if (!ExecuteSchedule(ctx, schedule, PACKAGE_ACTION_ADD))
{
return;
}
Log(LOG_LEVEL_VERBOSE, "Update schedule...");
if (!ExecuteSchedule(ctx, schedule, PACKAGE_ACTION_UPDATE))
{
return;
}
Log(LOG_LEVEL_VERBOSE, "Patch schedule...");
if (!ExecutePatch(ctx, schedule, PACKAGE_ACTION_PATCH))
{
return;
}
Log(LOG_LEVEL_VERBOSE, "Verify schedule...");
if (!ExecuteSchedule(ctx, schedule, PACKAGE_ACTION_VERIFY))
{
return;
}
}
/**
* @brief Execute the full package schedule.
*
* Called by cf-agent only.
*
*/
void ExecuteScheduledPackages(EvalContext *ctx)
{
if (PACKAGE_SCHEDULE)
{
ExecutePackageSchedule(ctx, PACKAGE_SCHEDULE);
}
}
/** Cleanup **/
/**
* @brief Clean the package schedule and installed lists.
*
* Called by cf-agent only. Cleans bookkeeping data.
*
*/
void CleanScheduledPackages(void)
{
DeletePackageManagers(PACKAGE_SCHEDULE);
PACKAGE_SCHEDULE = NULL;
DeletePackageManagers(INSTALLED_PACKAGE_LISTS);
INSTALLED_PACKAGE_LISTS = NULL;
}
/** Utils **/
static PackageManager *GetPackageManager(PackageManager **lists, char *mgr,
PackageAction pa,
PackageActionPolicy policy)
{
PackageManager *np;
Log(LOG_LEVEL_VERBOSE, "Looking for a package manager called '%s'", mgr);
if (mgr == NULL || mgr[0] == '\0')
{
Log(LOG_LEVEL_ERR, "Attempted to create a package manager with no name");
return NULL;
}
for (np = *lists; np != NULL; np = np->next)
{
if ((strcmp(np->manager, mgr) == 0) && (policy == np->policy))
{
return np;
}
}
np = xcalloc(1, sizeof(PackageManager));
np->manager = xstrdup(mgr);
np->action = pa;
np->policy = policy;
np->next = *lists;
*lists = np;
return np;
}
static void DeletePackageItems(PackageItem * pi)
{
while (pi != NULL)
{
PackageItem *next = pi->next;
free(pi->name);
free(pi->version);
free(pi->arch);
PromiseDestroy(pi->pp);
free(pi);
pi = next;
}
}
static void DeletePackageManagers(PackageManager *np)
{
while (np != NULL)
{
PackageManager *next = np->next;
DeletePackageItems(np->pack_list);
DeletePackageItems(np->patch_list);
DeletePackageItems(np->patch_avail);
free(np->manager);
free(np);
np = next;
}
}
const char *PrefixLocalRepository(const Rlist *repositories, const char *package)
{
static char quotedPath[CF_MAXVARSIZE]; /* GLOBAL_R, no need to initialize */
struct stat sb;
char path[CF_BUFSIZE];
for (const Rlist *rp = repositories; rp != NULL; rp = rp->next)
{
if (strlcpy(path, RlistScalarValue(rp), sizeof(path)) < sizeof(path))
{
AddSlash(path);
if (strlcat(path, package, sizeof(path)) < sizeof(path) &&
stat(path, &sb) != -1)
{
snprintf(quotedPath, sizeof(quotedPath), "\"%s\"", path);
return quotedPath;
}
}
}
return NULL;
}
bool ExecPackageCommand(EvalContext *ctx, char *command, int verify, int setCmdClasses, const Attributes *a,
const Promise *pp, PromiseResult *result)
{
assert(a != NULL);
assert(pp != NULL); // Dereferenced by cfPS macros
bool retval = true;
char *cmd;
FILE *pfp;
int packmanRetval = 0;
if ((!a->packages.package_commands_useshell) && (!IsExecutable(CommandArg0(command))))
{
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "The proposed package schedule command '%s' was not executable", command);
*result = PromiseResultUpdate_HELPER(pp, *result, PROMISE_RESULT_FAIL);
return false;
}
if (DONTDO)
{
return true;
}
/* Use this form to avoid limited, intermediate argument processing - long lines */
if (a->packages.package_commands_useshell)
{
Log(LOG_LEVEL_VERBOSE, "Running %s in shell", command);
if ((pfp = cf_popen_sh(command, "r")) == NULL)
{
cfPS_HELPER_2ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Couldn't start command '%20s'. (cf_popen_sh: %s)",
command, GetErrorStr());
*result = PromiseResultUpdate_HELPER(pp, *result, PROMISE_RESULT_FAIL);
return false;
}
}
else
{
if ((pfp = cf_popen(command, "r", true)) == NULL)
{
cfPS_HELPER_2ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Couldn't start command '%20s'. (cf_popen: %s)",
command, GetErrorStr());
*result = PromiseResultUpdate_HELPER(pp, *result, PROMISE_RESULT_FAIL);
return false;
}
}
Log(LOG_LEVEL_VERBOSE, "Executing %-.60s...", command);
/* Look for short command summary */
for (cmd = command; (*cmd != '\0') && (*cmd != ' '); cmd++)
{
}
while (cmd > command && cmd[-1] != FILE_SEPARATOR)
{
cmd--;
}
size_t line_size = CF_BUFSIZE;
char *line = xmalloc(line_size);
for (;;)
{
ssize_t res = CfReadLine(&line, &line_size, pfp);
if (res == -1)
{
if (!feof(pfp))
{
cfPS_HELPER_2ARG(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Unable to read output from command '%20s'. (fread: %s)",
command, GetErrorStr());
*result = PromiseResultUpdate_HELPER(pp, *result, PROMISE_RESULT_FAIL);
cf_pclose(pfp);
free(line);
return false;
}
else
{
break;
}
}
/* Need space for 2x buffer and null byte. */
char lineSafe[res * 2 + 1];
memcpy(lineSafe, line, res + 1);
ssize_t replace_res =
StringReplace(lineSafe, sizeof(lineSafe), "%", "%%");
if (replace_res == -1)
{
ProgrammingError("StringReplace(): buffer overflow in %s",
__FILE__);
continue;
}
Log(LOG_LEVEL_INFO, "Q:%20.20s ...:%s", cmd, lineSafe);
if (verify && (line[0] != '\0'))
{
if (a->packages.package_noverify_regex)
{
if (FullTextMatch(ctx, a->packages.package_noverify_regex, line))
{
cfPS_HELPER_2ARG(ctx, LOG_LEVEL_INFO, PROMISE_RESULT_FAIL, pp, a, "Package verification error in %-.40s ... :%s", cmd, lineSafe);
*result = PromiseResultUpdate_HELPER(pp, *result, PROMISE_RESULT_FAIL);
retval = false;
}
}
}
}
free(line);
packmanRetval = cf_pclose(pfp);
if (verify && (a->packages.package_noverify_returncode != CF_NOINT))
{
if (a->packages.package_noverify_returncode == packmanRetval)
{
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_INFO, PROMISE_RESULT_FAIL, pp, a, "Package verification error (returned %d)", packmanRetval);
*result = PromiseResultUpdate_HELPER(pp, *result, PROMISE_RESULT_FAIL);
retval = false;
}
else
{
cfPS_HELPER_1ARG(ctx, LOG_LEVEL_INFO, PROMISE_RESULT_NOOP, pp, a, "Package verification succeeded (returned %d)", packmanRetval);
*result = PromiseResultUpdate_HELPER(pp, *result, PROMISE_RESULT_FAIL);
}
}
else if (verify && (a->packages.package_noverify_regex))
{
if (retval) // set status if we succeeded above
{
cfPS_HELPER_0ARG(ctx, LOG_LEVEL_INFO, PROMISE_RESULT_NOOP, pp, a,
"Package verification succeeded (no match with package_noverify_regex)");
}
}
else if (setCmdClasses) // generic return code check
{
if (REPORT_THIS_PROMISE(pp))
{
retval = VerifyCommandRetcode(ctx, packmanRetval, a, pp, result);
}
}
return retval;
}
bool PrependPackageItem(
EvalContext *ctx,
PackageItem ** list,
const char *name,
const char *version,
const char *arch,
const Promise *pp)
{
if (!list || !name[0] || !version[0] || !arch[0])
{
return false;
}
Log(LOG_LEVEL_VERBOSE,
"Package (%s,%s,%s) [name,version,arch] found",
name, version, arch);
PackageItem *pi = xmalloc(sizeof(PackageItem));
pi->next = *list;
pi->name = xstrdup(name);
pi->version = xstrdup(version);
pi->arch = xstrdup(arch);
*list = pi;
/* Finally we need these for later schedule exec, once this iteration context has gone */
pi->pp = DeRefCopyPromise(ctx, pp);
return true;
}
static bool PackageInItemList(
PackageItem * list, char *name, char *version, char *arch)
{
if (!name[0] || !version[0] || !arch[0])
{
return false;
}
for (PackageItem *pi = list; pi != NULL; pi = pi->next)
{
if (strcmp(pi->name, name) == 0 &&
strcmp(pi->version, version) == 0 &&
strcmp(pi->arch, arch) == 0)
{
return true;
}
}
return false;
}
static bool PrependPatchItem(
EvalContext *ctx,
PackageItem ** list,
char *item,
PackageItem * chklist,
const char *default_arch,
const Attributes *a,
const Promise *pp)
{
assert(a != NULL);
char name[CF_MAXVARSIZE];
char arch[CF_MAXVARSIZE];
char version[CF_MAXVARSIZE];
char vbuff[CF_MAXVARSIZE];
strlcpy(vbuff, ExtractFirstReference(a->packages.package_patch_name_regex, item), CF_MAXVARSIZE);
sscanf(vbuff, "%s", name); /* trim */
strlcpy(vbuff, ExtractFirstReference(a->packages.package_patch_version_regex, item), CF_MAXVARSIZE);
sscanf(vbuff, "%s", version); /* trim */
if (a->packages.package_patch_arch_regex)
{
strlcpy(vbuff, ExtractFirstReference(a->packages.package_patch_arch_regex, item), CF_MAXVARSIZE );
sscanf(vbuff, "%s", arch); /* trim */
}
else
{
strlcpy(arch, default_arch, CF_MAXVARSIZE );
}
if ((strcmp(name, "CF_NOMATCH") == 0) || (strcmp(version, "CF_NOMATCH") == 0) || (strcmp(arch, "CF_NOMATCH") == 0))
{
return false;
}
Log(LOG_LEVEL_DEBUG, "PrependPatchItem: Patch line '%s', with name '%s', version '%s', arch '%s'", item, name, version, arch);
if (PackageInItemList(chklist, name, version, arch))
{
Log(LOG_LEVEL_VERBOSE, "Patch for (%s,%s,%s) [name,version,arch] found, but it appears to be installed already", name, version,
arch);
return false;
}
return PrependPackageItem(ctx, list, name, version, arch, pp);
}
static bool PrependMultiLinePackageItem(
EvalContext *ctx,
PackageItem ** list,
char *item,
int reset,
const char *default_arch,
const Attributes *a,
const Promise *pp)
{
assert(a != NULL);
static char name[CF_MAXVARSIZE] = ""; /* GLOBAL_X */
static char arch[CF_MAXVARSIZE] = ""; /* GLOBAL_X */
static char version[CF_MAXVARSIZE] = ""; /* GLOBAL_X */
static char vbuff[CF_MAXVARSIZE] = ""; /* GLOBAL_X */
if (reset)
{
if ((strcmp(name, "CF_NOMATCH") == 0) || (strcmp(version, "CF_NOMATCH") == 0))
{
return false;
}
if ((strcmp(name, "") != 0) || (strcmp(version, "") != 0))
{
Log(LOG_LEVEL_DEBUG, "PrependMultiLinePackageItem: Extracted package name '%s', version '%s', arch '%s'", name, version, arch);
PrependPackageItem(ctx, list, name, version, arch, pp);
}
strcpy(name, "CF_NOMATCH");
strcpy(version, "CF_NOMATCH");
strcpy(arch, default_arch);
}
if (FullTextMatch(ctx, a->packages.package_list_name_regex, item))
{
strlcpy(vbuff, ExtractFirstReference(a->packages.package_list_name_regex, item), CF_MAXVARSIZE);
sscanf(vbuff, "%s", name); /* trim */
}
if (FullTextMatch(ctx, a->packages.package_list_version_regex, item))
{
strlcpy(vbuff, ExtractFirstReference(a->packages.package_list_version_regex, item), CF_MAXVARSIZE );
sscanf(vbuff, "%s", version); /* trim */
}
if ((a->packages.package_list_arch_regex) && (FullTextMatch(ctx, a->packages.package_list_arch_regex, item)))
{
if (a->packages.package_list_arch_regex)
{
strlcpy(vbuff, ExtractFirstReference(a->packages.package_list_arch_regex, item), CF_MAXVARSIZE);
sscanf(vbuff, "%s", arch); /* trim */
}
}
return true;
}
static bool PrependListPackageItem(
EvalContext *ctx,
PackageItem **list,
char *item,
const char *default_arch,
const Attributes *a,
const Promise *pp)
{
assert(a != NULL);
char name[CF_MAXVARSIZE];
char arch[CF_MAXVARSIZE];
char version[CF_MAXVARSIZE];
char vbuff[CF_MAXVARSIZE];
strlcpy(vbuff, ExtractFirstReference(a->packages.package_list_name_regex, item), CF_MAXVARSIZE);
sscanf(vbuff, "%s", name); /* trim */
strlcpy(vbuff, ExtractFirstReference(a->packages.package_list_version_regex, item), CF_MAXVARSIZE);
sscanf(vbuff, "%s", version); /* trim */
if (a->packages.package_list_arch_regex)
{
strlcpy(vbuff, ExtractFirstReference(a->packages.package_list_arch_regex, item), CF_MAXVARSIZE);
sscanf(vbuff, "%s", arch); /* trim */
}
else
{
strlcpy(arch, default_arch, CF_MAXVARSIZE);
}
if ((strcmp(name, "CF_NOMATCH") == 0) || (strcmp(version, "CF_NOMATCH") == 0) || (strcmp(arch, "CF_NOMATCH") == 0))
{
return false;
}
Log(LOG_LEVEL_DEBUG, "PrependListPackageItem: Package line '%s', name '%s', version '%s', arch '%s'", item, name, version, arch);
return PrependPackageItem(ctx, list, name, version, arch, pp);
}
static char *GetDefaultArch(const char *command)
{
if (command == NULL)
{
return xstrdup("default");
}
Log(LOG_LEVEL_VERBOSE, "Obtaining default architecture for package manager '%s'", command);
FILE *fp = cf_popen_sh(command, "r");
if (fp == NULL)
{
return NULL;
}
size_t arch_size = CF_SMALLBUF;
char *arch = xmalloc(arch_size);
ssize_t res = CfReadLine(&arch, &arch_size, fp);
if (res == -1)
{
cf_pclose(fp);
free(arch);
return NULL;
}
Log(LOG_LEVEL_VERBOSE, "Default architecture for package manager is '%s'", arch);
cf_pclose(fp);
return arch;
}
|