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
|
/* jmap_api.c -- Routines for handling JMAP API requests
*
* Copyright (c) 1994-2018 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any legal
* details, please contact
* Carnegie Mellon University
* Center for Technology Transfer and Enterprise Creation
* 4615 Forbes Avenue
* Suite 302
* Pittsburgh, PA 15213
* (412) 268-7393, fax: (412) 268-7395
* innovation@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <config.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <errno.h>
#include "append.h"
#include "bsearch.h"
#include "cyrusdb.h"
#include "hash.h"
#include "httpd.h"
#include "http_dav.h"
#include "http_dav_sharing.h"
#include "http_jmap.h"
#include "imparse.h"
#include "mboxname.h"
#include "msgrecord.h"
#include "proxy.h"
#include "times.h"
#include "strhash.h"
#include "syslog.h"
#include "xstrlcpy.h"
/* generated headers are not necessarily in current directory */
#include "imap/http_err.h"
#include "imap/imap_err.h"
#include "imap/jmap_err.h"
static json_t *extract_value(json_t *from, const char *path, ptrarray_t *refs);
static json_t *extract_array_value(json_t *val, const char *idx,
const char *path, ptrarray_t *pool)
{
if (!strcmp(idx, "*")) {
/* Build value from array traversal */
json_t *newval = json_array();
size_t i;
json_t *v;
json_array_foreach(val, i, v) {
json_t *x = extract_value(v, path, pool);
if (json_is_array(x)) {
/* JMAP spec: "If the result of applying the rest
* of the pointer tokens to a value was itself an
* array, its items should be included individually
* in the output rather than including the array
* itself." */
json_array_extend(newval, x);
} else if (x) {
json_array_append(newval, x);
} else {
json_decref(newval);
newval = NULL;
}
}
if (newval) {
ptrarray_add(pool, newval);
}
return newval;
}
/* Lookup array value by index */
const char *eot = NULL;
bit64 num;
if (parsenum(idx, &eot, 0, &num) || *eot) {
return NULL;
}
val = json_array_get(val, num);
if (!val) {
return NULL;
}
return extract_value(val, path, pool);
}
/* Extract the JSON value at position path from val.
*
* Return NULL, if the value does not exist or if
* path is erroneous.
*/
static json_t *extract_value(json_t *val, const char *path, ptrarray_t *pool)
{
/* Return value for empty path */
if (*path == '\0') {
return val;
}
/* Be lenient: root path '/' is optional */
if (*path == '/') {
path++;
}
/* Walk over path segments */
while (val && *path) {
const char *top = NULL;
char *p = NULL;
/* Extract next path segment */
if (!(top = strchr(path, '/'))) {
top = strchr(path, '\0');
}
p = jmap_pointer_decode(path, top - path);
if (*p == '\0') {
free(p);
return NULL;
}
/* Extract array value */
if (json_is_array(val)) {
val = extract_array_value(val, p, top, pool);
free(p);
return val;
}
/* Value MUST be an object now */
if (!json_is_object(val)) {
free(p);
return NULL;
}
/* Step down into object tree */
val = json_object_get(val, p);
free(p);
path = *top ? top + 1 : top;
}
return val;
}
static int process_resultrefs(json_t *args, json_t *resp, json_t **err)
{
json_t *ref;
const char *arg;
int ret = -1;
void *tmp;
json_object_foreach_safe(args, tmp, arg, ref) {
if (*arg != '#' || *(arg+1) == '\0') {
continue;
}
if (json_object_get(args, arg + 1)) {
*err = json_pack("{s:s, s:[s]}",
"type", "invalidArguments", "arguments", arg);
goto fail;
}
const char *of, *path, *name;
json_t *res = NULL;
/* Parse result reference object */
of = json_string_value(json_object_get(ref, "resultOf"));
if (!of || *of == '\0') {
goto fail;
}
path = json_string_value(json_object_get(ref, "path"));
if (!path || *path == '\0') {
goto fail;
}
name = json_string_value(json_object_get(ref, "name"));
if (!name || *name == '\0') {
goto fail;
}
/* Lookup referenced response */
json_t *v;
size_t i;
json_array_foreach(resp, i, v) {
const char *tag = json_string_value(json_array_get(v, 2));
if (!tag || strcmp(tag, of)) {
continue;
}
const char *mname = json_string_value(json_array_get(v, 0));
if (!mname || strcmp(name, mname)) {
goto fail;
}
res = v;
break;
}
if (!res) goto fail;
/* Extract the reference argument value. */
/* We maintain our own pool of newly created JSON objects, since
* tracking reference counts across newly created JSON arrays is
* a pain. Rule: If you incref an existing JSON value or create
* an entirely new one, put it into the pool for cleanup. */
ptrarray_t pool = PTRARRAY_INITIALIZER;
json_t *val = extract_value(json_array_get(res, 1), path, &pool);
if (!val) goto fail;
/* Replace both key and value of the reference entry */
json_object_set(args, arg + 1, val);
json_object_del(args, arg);
/* Clean up reference counts of pooled JSON objects */
json_t *ref;
while ((ref = ptrarray_pop(&pool))) {
json_decref(ref);
}
ptrarray_fini(&pool);
}
return 0;
fail:
return ret;
}
static int validate_request(struct transaction_t *txn, const json_t *req,
jmap_settings_t *settings)
{
json_t *using = json_object_get(req, "using");
json_t *calls = json_object_get(req, "methodCalls");
if (!json_is_array(using) || !json_is_array(calls)) {
return JMAP_NOT_REQUEST;
}
/*
* XXX the following maximums are not enforced:
* maxConcurrentUpload
* maxConcurrentRequests
*/
if (buf_len(&txn->req_body.payload) >
(size_t) settings->limits[MAX_SIZE_REQUEST]) {
return JMAP_LIMIT_SIZE;
}
size_t i;
json_t *val;
json_array_foreach(calls, i, val) {
if (json_array_size(val) != 3 ||
!json_is_string(json_array_get(val, 0)) ||
!json_is_object(json_array_get(val, 1)) ||
!json_is_string(json_array_get(val, 2))) {
return JMAP_NOT_REQUEST;
}
if (i >= (size_t) settings->limits[MAX_CALLS_IN_REQUEST]) {
return JMAP_LIMIT_CALLS;
}
const char *mname = json_string_value(json_array_get(val, 0));
mname = strchr(mname, '/');
if (!mname) continue;
mname++;
if (!strcmp(mname, "get")) {
json_t *ids = json_object_get(json_array_get(val, 1), "ids");
if (json_array_size(ids) >
(size_t) settings->limits[MAX_OBJECTS_IN_GET]) {
return JMAP_LIMIT_OBJS_GET;
}
}
else if (!strcmp(mname, "set")) {
json_t *args = json_array_get(val, 1);
size_t size = json_object_size(json_object_get(args, "create"));
size += json_object_size(json_object_get(args, "update"));
size += json_array_size(json_object_get(args, "destroy"));
if (size > (size_t) settings->limits[MAX_OBJECTS_IN_SET]) {
return JMAP_LIMIT_OBJS_SET;
}
}
}
json_array_foreach(using, i, val) {
const char *s = json_string_value(val);
if (!s) {
return JMAP_NOT_REQUEST;
}
else if (!strcmp(s, "ietf:jmap")) {
syslog(LOG_DEBUG, "old capability %s used", s);
}
else if (!strcmp(s, "ietf:jmapmail")) {
syslog(LOG_DEBUG, "old capability %s used", s);
}
else if (!json_object_get(settings->server_capabilities, s)) {
buf_printf(&txn->buf, "The Request object used capability '%s',"
" which is not supported by this server.", s);
txn->error.desc = buf_cstring(&txn->buf);
return JMAP_UNKNOWN_CAPABILITY;
}
}
return 0;
}
HIDDEN int jmap_is_valid_id(const char *id)
{
if (!id || *id == '\0') return 0;
const char *p;
for (p = id; *p; p++) {
if (('0' <= *p && *p <= '9'))
continue;
if (('a' <= *p && *p <= 'z') || ('A' <= *p && *p <= 'Z'))
continue;
if ((*p == '-') || (*p == '_'))
continue;
return 0;
}
return 1;
}
static void _make_created_ids(const char *creation_id, void *val, void *rock)
{
json_t *jcreatedIds = rock;
const char *id = val;
json_object_set_new(jcreatedIds, creation_id, json_string(id));
}
HIDDEN int jmap_error_response(struct transaction_t *txn,
long code, json_t **res)
{
long http_code = HTTP_BAD_REQUEST;
const char *type, *title, *limit = NULL;
/* Error string is encoded as type NUL title [ NUL limit ] */
type = error_message(code);
title = type + strlen(type) + 1;
switch (code) {
case JMAP_NOT_JSON:
case JMAP_NOT_REQUEST:
case JMAP_UNKNOWN_CAPABILITY:
break;
case JMAP_LIMIT_SIZE:
http_code = HTTP_PAYLOAD_TOO_LARGE;
GCC_FALLTHROUGH
case JMAP_LIMIT_CALLS:
case JMAP_LIMIT_OBJS_GET:
case JMAP_LIMIT_OBJS_SET:
limit = title + strlen(title) + 1;
break;
default:
/* Actually an HTTP code, not a JMAP error code */
return code;
}
*res = json_pack("{s:s s:s s:i}", "type", type, "title", title,
"status", atoi(error_message(http_code)));
if (!*res) {
txn->error.desc = "Unable to create JSON response";
return HTTP_SERVER_ERROR;
}
if (limit) {
json_object_set_new(*res, "limit", json_string(limit));
}
if (txn->error.desc) {
json_object_set_new(*res, "detail", json_string(txn->error.desc));
}
return http_code;
}
HIDDEN int jmap_initreq(jmap_req_t *req)
{
memset(req, 0, sizeof(struct jmap_req));
req->mboxes = ptrarray_new();
return 0;
}
struct _mboxcache_rec {
struct mailbox *mbox;
int refcount;
int rw;
};
HIDDEN void jmap_finireq(jmap_req_t *req)
{
int i;
for (i = 0; i < req->mboxes->count; i++) {
struct _mboxcache_rec *rec = ptrarray_nth(req->mboxes, i);
syslog(LOG_ERR, "jmap: force-closing mailbox %s (refcount=%d)",
mailbox_name(rec->mbox), rec->refcount);
mailbox_close(&rec->mbox);
free(rec);
}
/* Fail after cleaning up open mailboxes */
if (req->mboxes->count) {
json_t *jdebug = json_pack("[s,s,s,o,o]", req->method, req->userid, req->accountid, req->args, req->response);
char *debug = json_dumps(jdebug, JSON_INDENT(2));
assert(!debug);
}
ptrarray_free(req->mboxes);
req->mboxes = NULL;
jmap_mbentry_cache_free(req);
json_decref(req->perf_details);
req->perf_details = NULL;
}
static jmap_method_t *find_methodproc(const char *name, hash_table *jmap_methods)
{
return hash_lookup(name, jmap_methods);
}
struct mbstate {
int mbtype;
int rights; // ACL for current user
};
static struct mbstate *_mbstate_getoradd(struct auth_state *authstate,
const mbentry_t *mbentry,
hash_table *mbstates)
{
struct mbstate *mbstate = hash_lookup(mbentry->name, mbstates);
if (mbstate) return mbstate;
mbstate = xmalloc(sizeof(struct mbstate));
mbstate->mbtype = mbentry->mbtype;
/* Lookup ACL */
mbname_t *mbname = mbname_from_intname(mbentry->name);
if (mbentry->mbtype & MBTYPE_INTERMEDIATE) {
// if it's an intermediate mailbox, we get rights from the parent
mbentry_t *parententry = NULL;
if (mboxlist_findparent(mbentry->name, &parententry))
mbstate->rights = 0;
else
mbstate->rights = httpd_myrights(authstate, parententry);
mboxlist_entry_free(&parententry);
}
else mbstate->rights = httpd_myrights(authstate, mbentry);
mbname_free(&mbname);
hash_insert(mbentry->name, mbstate, mbstates);
return mbstate;
}
/* Return the ACL for mbentry for the authstate of userid.
* Lookup and store ACL rights in the cached mailbox state. */
static int _rights_for_mbentry(struct auth_state *authstate,
const mbentry_t *mbentry,
hash_table *mbstates)
{
if (!mbentry) return 0;
struct mbstate *mbstate =_mbstate_getoradd(authstate, mbentry, mbstates);
return mbstate->rights;
}
struct capabilities_rock {
const char *authuserid;
hash_table *mboxrights;
struct auth_state *authstate;
int is_visible;
int has_mail;
int has_contacts;
int has_calendars;
};
static int capabilities_cb(const mbentry_t *mbentry, void *vrock)
{
struct capabilities_rock *rock = vrock;
if (!mbentry) return 0;
if ((mbentry->mbtype & MBTYPE_DELETED) ||
(mbentry->mbtype & MBTYPE_MOVING) ||
(mbentry->mbtype & MBTYPE_REMOTE) ||
(mbentry->mbtype & MBTYPE_RESERVE)) {
return 0;
}
/* Skip any special use folders (#jmap, #calendars, #notifications, etc.) */
switch (mbtype_isa(mbentry->mbtype)) {
case MBTYPE_EMAIL:
case MBTYPE_CALENDAR:
case MBTYPE_ADDRESSBOOK:
break;
default:
return 0;
}
int rights = _rights_for_mbentry(rock->authstate, mbentry, rock->mboxrights);
if (!(rights & JACL_LOOKUP)) return 0;
rock->is_visible = 1;
mbname_t *mbname = mbname_from_intname(mbentry->name);
const strarray_t *boxes = mbname_boxes(mbname);
if (!rock->has_mail) {
rock->has_mail = mbtype_isa(mbentry->mbtype) == MBTYPE_EMAIL;
}
if (!rock->has_contacts) {
rock->has_contacts = strarray_size(boxes) >= 1 &&
!strcmpsafe(config_getstring(IMAPOPT_ADDRESSBOOKPREFIX),
strarray_nth(boxes, 0));
}
if (!rock->has_calendars) {
rock->has_calendars = strarray_size(boxes) >= 1 &&
!strcmpsafe(config_getstring(IMAPOPT_CALENDARPREFIX),
strarray_nth(boxes, 0));
}
mbname_free(&mbname);
return 0;
}
static json_t *lookup_capabilities(const char *accountid,
const char *authuserid,
struct auth_state *authstate,
hash_table *mboxrights)
{
// we need to know if we can write children of the inbox
mbentry_t *inboxentry = NULL;
char *inboxname = mboxname_user_mbox(accountid, NULL);
if (mboxlist_lookup(inboxname, &inboxentry, NULL)) {
free(inboxname);
return json_null();
}
free(inboxname);
int inboxrights = _rights_for_mbentry(authstate, inboxentry, mboxrights);
mboxlist_entry_free(&inboxentry);
json_t *capas = json_object();
int mayCreateTopLevel = (inboxrights & JACL_CREATECHILD) ? 1 : 0;
if (!strcmp(authuserid, accountid)) {
/* Primary account has all capabilities */
jmap_core_capabilities(capas);
jmap_mail_capabilities(capas, mayCreateTopLevel);
jmap_emailsubmission_capabilities(capas);
jmap_mdn_capabilities(capas);
jmap_contact_capabilities(capas);
jmap_calendar_capabilities(capas);
jmap_backup_capabilities(capas);
jmap_notes_capabilities(capas);
#ifdef USE_SIEVE
jmap_sieve_capabilities(capas);
jmap_vacation_capabilities(capas);
#endif
}
else {
/* Lookup capabilities for shared account */
struct capabilities_rock rock = {
authuserid, mboxrights, httpd_authstate, 0, 0, 0, 0
};
mboxlist_usermboxtree(accountid, authstate, capabilities_cb,
&rock, MBOXTREE_INTERMEDIATES);
if (rock.is_visible) {
jmap_core_capabilities(capas);
if (rock.has_mail) {
// we don't offer emailsubmission or vacation
// for shared accounts right now
jmap_mail_capabilities(capas, mayCreateTopLevel);
}
if (rock.has_contacts) {
jmap_contact_capabilities(capas);
}
if (rock.has_calendars) {
jmap_calendar_capabilities(capas);
}
// should we offer Backup/restoreXxx for shared accounts?
}
}
if (!json_object_size(capas)) {
json_decref(capas);
capas = json_null();
}
return capas;
}
static void _free_json(void *val)
{
json_decref((json_t *)val);
}
static void _free_buf(void *val)
{
buf_destroy((struct buf *)val);
}
/* Perform an API request */
HIDDEN int jmap_api(struct transaction_t *txn,
const json_t *jreq, json_t **res,
jmap_settings_t *settings)
{
json_t *resp = NULL;
size_t i;
int ret, do_perf = 0;
char *account_inboxname = NULL;
int return_created_ids = 0;
hash_table created_ids = HASH_TABLE_INITIALIZER;
hash_table inmemory_blobs = HASH_TABLE_INITIALIZER;
hash_table capabilities_by_accountid = HASH_TABLE_INITIALIZER;
hash_table mbstates = HASH_TABLE_INITIALIZER;
strarray_t methods = STRARRAY_INITIALIZER;
ptrarray_t method_calls = PTRARRAY_INITIALIZER;
ptrarray_t processed_methods = PTRARRAY_INITIALIZER;
strarray_t using_capabilities = STRARRAY_INITIALIZER;
/* Validate Request object */
if ((ret = validate_request(txn, jreq, settings))) {
return jmap_error_response(txn, ret, res);
}
/* Start JSON response */
resp = json_array();
if (!resp) {
txn->error.desc = "Unable to create JSON response body";
ret = HTTP_SERVER_ERROR;
goto done;
}
/* Set up request-internal state */
construct_hash_table(&capabilities_by_accountid, 8, 0);
construct_hash_table(&inmemory_blobs, 64, 0);
construct_hash_table(&mbstates, 64, 0);
construct_hash_table(&created_ids, 1024, 0);
/* Parse client-supplied creation ids */
json_t *jcreatedIds = json_object_get(jreq, "createdIds");
if (json_is_object(jcreatedIds)) {
return_created_ids = 1;
const char *creation_id;
json_t *jval;
json_object_foreach(jcreatedIds, creation_id, jval) {
if (!json_is_string(jval)) {
txn->error.desc = "Invalid createdIds argument";
ret = HTTP_BAD_REQUEST;
goto done;
}
const char *id = json_string_value(jval);
if (!jmap_is_valid_id(creation_id) || !jmap_is_valid_id(id)) {
txn->error.desc = "Invalid createdIds argument";
ret = HTTP_BAD_REQUEST;
goto done;
}
hash_insert(creation_id, xstrdup(id), &created_ids);
}
}
else if (jcreatedIds && jcreatedIds != json_null()) {
txn->error.desc = "Invalid createdIds argument";
ret = HTTP_BAD_REQUEST;
goto done;
}
json_t *jusing = json_object_get(jreq, "using");
for (i = 0; i < json_array_size(jusing); i++) {
strarray_add(&using_capabilities, json_string_value(json_array_get(jusing, i)));
}
/* Push client method calls on call stack */
json_t *jmethod_calls = json_object_get(jreq, "methodCalls");
for (i = json_array_size(jmethod_calls); i > 0; i--) {
json_t *mc = json_array_get(jmethod_calls, i-1);
ptrarray_push(&method_calls, json_incref(mc));
}
/* Process call stack */
do_perf = strarray_find(&using_capabilities, JMAP_PERFORMANCE_EXTENSION, 0) >= 0;
json_t *mc;
while ((mc = ptrarray_pop(&method_calls))) {
/* Send provisional response, if necessary */
keepalive_response(txn);
/* Mark method as processed */
ptrarray_push(&processed_methods, mc);
/* Process method */
const jmap_method_t *mp;
const char *mname = json_string_value(json_array_get(mc, 0));
json_t *args = json_array_get(mc, 1);
const char *tag = json_string_value(json_array_get(mc, 2));
int r = 0;
strarray_append(&methods, mname);
json_incref(args);
/* Find the message processor */
mp = find_methodproc(mname, &settings->methods);
if (!mp || strarray_find(&using_capabilities, mp->capability, 0) < 0) {
json_array_append_new(resp, json_pack("[s {s:s} s]",
"error", "type", "unknownMethod", tag));
json_decref(args);
continue;
}
/* Validate accountId argument */
const char *accountid = httpd_userid;
json_t *err = NULL;
json_t *arg = json_object_get(args, "accountId");
if (arg && arg != json_null()) {
accountid = json_string_value(arg);
}
if (!accountid) {
err = json_pack("{s:s, s:[s]}",
"type", "invalidArguments", "arguments", "accountId");
json_array_append_new(resp, json_pack("[s,o,s]", "error", err, tag));
json_decref(args);
continue;
}
/* Validate supported capabilities for this account */
json_t *account_capas = hash_lookup(accountid, &capabilities_by_accountid);
if (!account_capas) {
account_capas = lookup_capabilities(accountid, httpd_userid,
httpd_authstate, &mbstates);
hash_insert(accountid, account_capas, &capabilities_by_accountid);
}
if (json_is_null(account_capas)) {
err = json_pack("{s:s}", "type", "accountNotFound");
}
else if (!json_object_get(account_capas, mp->capability)) {
err = json_pack("{s:s}", "type", "accountNotSupportedByMethod");
}
if (err) {
json_array_append_new(resp, json_pack("[s,o,s]", "error", err, tag));
json_decref(args);
continue;
}
/* Pre-process result references */
if (process_resultrefs(args, resp, &err)) {
if (!err) err = json_pack("{s:s}", "type", "resultReference");
json_array_append_new(resp, json_pack("[s,o,s]", "error", err, tag));
json_decref(args);
continue;
}
if (config_getswitch(IMAPOPT_READONLY) && (mp->flags & JMAP_READ_WRITE)) {
if (!err) err = json_pack("{s:s}", "type", "accountReadOnly");
json_array_append_new(resp, json_pack("[s,o,s]", "error", err, tag));
json_decref(args);
continue;
}
struct conversations_state *cstate = NULL;
if (mp->flags & JMAP_NEED_CSTATE) {
r = conversations_open_user(accountid,
!(mp->flags & JMAP_READ_WRITE), &cstate);
if (r) {
txn->error.desc = error_message(r);
ret = HTTP_SERVER_ERROR;
json_decref(args);
goto done;
}
}
/* Initialize request context */
struct jmap_req req;
jmap_initreq(&req);
req.method = mname;
req.userid = httpd_userid;
req.accountid = accountid;
req.cstate = cstate;
req.authstate = httpd_authstate;
req.args = args;
req.response = resp;
req.tag = tag;
req.created_ids = &created_ids;
req.txn = txn;
req.mbstates = &mbstates;
req.method_calls = &method_calls;
req.using_capabilities = &using_capabilities;
req.inmemory_blobs = &inmemory_blobs;
if (do_perf) {
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
req.user_start = timeval_get_double(&usage.ru_utime);
req.sys_start = timeval_get_double(&usage.ru_stime);
req.real_start = now_ms() / 1000.0;
req.perf_details = json_object();
}
/* Read the current state data in */
account_inboxname = mboxname_user_mbox(accountid, NULL);
r = mboxname_read_counters(account_inboxname, &req.counters);
free(account_inboxname);
account_inboxname = NULL;
if (r) {
conversations_abort(&req.cstate);
txn->error.desc = error_message(r);
ret = HTTP_SERVER_ERROR;
jmap_finireq(&req);
json_decref(args);
goto done;
}
/* Call any pre-processors. */
int i;
for (i = 0; i < ptrarray_size(&settings->event_handlers); i++) {
struct jmap_handler *h = ptrarray_nth(&settings->event_handlers, i);
if (h->eventmask & JMAP_HANDLE_BEFORE_METHOD) {
h->handler(JMAP_HANDLE_BEFORE_METHOD, &req, h->rock);
}
}
/* Call the message processor. */
r = mp->proc(&req);
/* Finalize request context */
jmap_finireq(&req);
if (r) {
conversations_abort(&req.cstate);
txn->error.desc = error_message(r);
ret = HTTP_SERVER_ERROR;
json_decref(args);
goto done;
}
conversations_commit(&req.cstate);
json_decref(args);
}
/* Build response */
*res = json_pack("{s:O}", "methodResponses", resp);
if (return_created_ids) {
json_t *jcreatedIds = json_object();
hash_enumerate(&created_ids, _make_created_ids, jcreatedIds);
json_object_set_new(*res, "createdIds", jcreatedIds);
}
char *user_inboxname = mboxname_user_mbox(httpd_userid, NULL);
struct buf state = BUF_INITIALIZER;
buf_printf(&state, MODSEQ_FMT, mboxname_readraclmodseq(user_inboxname));
free(user_inboxname);
json_object_set_new(*res, "sessionState", json_string(buf_cstring(&state)));
buf_free(&state);
done:
/* tell syslog which methods were called */
spool_replace_header(xstrdup(":jmap"),
strarray_join(&methods, ","), txn->req_hdrs);
{
/* Clean up call stack */
json_t *jval;
while ((jval = ptrarray_pop(&processed_methods))) {
json_decref(jval);
}
while ((jval = ptrarray_pop(&method_calls))) {
json_decref(jval);
}
ptrarray_fini(&processed_methods);
ptrarray_fini(&method_calls);
}
free_hash_table(&created_ids, free);
free_hash_table(&inmemory_blobs, _free_buf);
free_hash_table(&capabilities_by_accountid, _free_json);
free_hash_table(&mbstates, free);
free(account_inboxname);
json_decref(resp);
strarray_fini(&methods);
strarray_fini(&using_capabilities);
return ret;
}
struct findaccounts_rock {
struct buf current_accountid;
int current_rights;
json_t *accounts;
const char *authuserid;
};
static void findaccounts_add(struct findaccounts_rock *rock)
{
if (!buf_len(&rock->current_accountid))
return;
if (!(rock->current_rights & JACL_READITEMS))
return;
const char *accountid = buf_cstring(&rock->current_accountid);
int is_rw = rock->current_rights & JACL_WRITE;
int is_primary = !strcmp(rock->authuserid, accountid);
if (config_getswitch(IMAPOPT_READONLY)) is_rw = 0;
json_t *account = json_object();
json_object_set_new(account, "name", json_string(accountid));
json_object_set_new(account, "isPrimary", json_boolean(is_primary));
json_object_set_new(account, "isPersonal", json_boolean(is_primary));
json_object_set_new(account, "isReadOnly", json_boolean(!is_rw));
json_object_set_new(rock->accounts, accountid, account);
}
static int findaccounts_cb(struct findall_data *data, void *vrock)
{
if (!data || !data->mbentry) {
return 0;
}
struct findaccounts_rock *rock = vrock;
const mbentry_t *mbentry = data->mbentry;
/* Skip any special use folders (#jmap, #calendars, #notifications, etc.) */
switch (mbtype_isa(mbentry->mbtype)) {
case MBTYPE_EMAIL:
case MBTYPE_CALENDAR:
case MBTYPE_ADDRESSBOOK:
break;
default:
return 0;
}
mbname_t *mbname = mbname_from_intname(mbentry->name);
if (strcmp(buf_cstring(&rock->current_accountid), mbname_userid(mbname))) {
findaccounts_add(rock);
buf_setcstr(&rock->current_accountid, mbname_userid(mbname));
rock->current_rights = 0;
}
rock->current_rights |= httpd_myrights(httpd_authstate, data->mbentry);
mbname_free(&mbname);
return 0;
}
HIDDEN void jmap_accounts(json_t *accounts, json_t *primary_accounts)
{
/* Find shared accounts */
strarray_t patterns = STRARRAY_INITIALIZER;
char *userpat = NULL;
if (jmap_namespace.isalt) {
struct buf buf = BUF_INITIALIZER;
buf_printf(&buf, "%s%c*", config_getstring(IMAPOPT_USERPREFIX),
jmap_namespace.hier_sep);
userpat = buf_release(&buf);
}
else {
userpat = xstrdup("user.*");
userpat[4] = jmap_namespace.hier_sep;
}
strarray_append(&patterns, userpat);
struct findaccounts_rock rock = {
BUF_INITIALIZER, 0, accounts, httpd_userid
};
int r = mboxlist_findallmulti(&jmap_namespace, &patterns, 0, httpd_userid,
httpd_authstate, findaccounts_cb, &rock);
if (r) {
syslog(LOG_ERR, "Can't determine shared JMAP accounts for user %s: %s",
httpd_userid, error_message(r));
}
findaccounts_add(&rock);
/* Add primary accout */
buf_setcstr(&rock.current_accountid, httpd_userid);
rock.current_rights = JACL_ALL;
findaccounts_add(&rock);
/* Determine account capabilities */
hash_table mboxrights = HASH_TABLE_INITIALIZER;
construct_hash_table(&mboxrights, 64, 0);
json_t *jaccount;
const char *accountid;
json_object_foreach(accounts, accountid, jaccount) {
json_t *capas = lookup_capabilities(accountid, httpd_userid,
httpd_authstate, &mboxrights);
json_object_set_new(jaccount, "accountCapabilities", capas);
}
free_hash_table(&mboxrights, free);
json_t *jprimary = json_string(httpd_userid);
json_object_set(primary_accounts, JMAP_URN_MAIL, jprimary);
json_object_set(primary_accounts, JMAP_URN_SUBMISSION, jprimary);
json_object_set(primary_accounts, JMAP_CONTACTS_EXTENSION, jprimary);
json_object_set(primary_accounts, JMAP_CALENDARS_EXTENSION, jprimary);
json_object_set(primary_accounts, JMAP_BACKUP_EXTENSION, jprimary);
#ifdef USE_SIEVE
json_object_set(primary_accounts, JMAP_URN_VACATION, jprimary);
json_object_set(primary_accounts, JMAP_SIEVE_EXTENSION, jprimary);
#endif
json_decref(jprimary);
/* Clean up */
buf_free(&rock.current_accountid);
free(userpat);
strarray_fini(&patterns);
}
HIDDEN void jmap_add_subreq(jmap_req_t *req, const char *method,
json_t *args, const char *client_id)
{
if (!client_id) client_id = req->tag;
ptrarray_push(req->method_calls, json_pack("[s,o,s]", method, args, client_id));
}
const char *jmap_lookup_id(jmap_req_t *req, const char *creation_id)
{
return hash_lookup(creation_id, req->created_ids);
}
const char *jmap_id_string_value(jmap_req_t *req, json_t *item)
{
if (!item) return NULL;
if (!json_is_string(item)) return NULL;
const char *id = json_string_value(item);
if (*id == '#')
return jmap_lookup_id(req, id+1);
return id;
}
void jmap_add_id(jmap_req_t *req, const char *creation_id, const char *id)
{
/* It's OK to overwrite existing ids, as per Foo/set:
* "A client SHOULD NOT reuse a creation id anywhere in the same API
* request. If a creation id is reused, the server MUST map the creation
* id to the most recently created item with that id."
*/
free(hash_del(creation_id, req->created_ids));
hash_insert(creation_id, xstrdup(id), req->created_ids);
}
HIDDEN int jmap_openmbox(jmap_req_t *req, const char *name,
struct mailbox **mboxp, int rw)
{
int i, r;
struct _mboxcache_rec *rec;
for (i = 0; i < req->mboxes->count; i++) {
rec = (struct _mboxcache_rec*) ptrarray_nth(req->mboxes, i);
if (!strcmp(name, mailbox_name(rec->mbox))) {
if (rw && !rec->rw) {
/* Lock promotions are not supported */
syslog(LOG_ERR, "jmapmbox: failed to grab write-lock"
" on cached read-only mailbox %s", name);
return IMAP_INTERNAL;
}
/* Found a cached mailbox. Increment refcount. */
rec->refcount++;
*mboxp = rec->mbox;
return 0;
}
}
/* Add mailbox to cache */
if (req->force_openmbox_rw)
rw = 1;
r = rw ? mailbox_open_iwl(name, mboxp) : mailbox_open_irl(name, mboxp);
if (r) {
syslog(LOG_ERR, "jmap_openmbox(%s): %s", name, error_message(r));
return r;
}
rec = xzmalloc(sizeof(struct _mboxcache_rec));
rec->mbox = *mboxp;
rec->refcount = 1;
rec->rw = rw;
ptrarray_add(req->mboxes, rec);
return 0;
}
HIDDEN int jmap_openmbox_by_uniqueid(jmap_req_t *req, const char *id,
struct mailbox **mboxp, int rw)
{
const mbentry_t *mbentry = jmap_mbentry_by_uniqueid(req, id);
if (mbentry)
return jmap_openmbox(req, mbentry->name, mboxp, rw);
else
return IMAP_MAILBOX_NONEXISTENT;
}
HIDDEN int jmap_isopenmbox(jmap_req_t *req, const char *name)
{
int i;
struct _mboxcache_rec *rec;
for (i = 0; i < req->mboxes->count; i++) {
rec = (struct _mboxcache_rec*) ptrarray_nth(req->mboxes, i);
if (!strcmp(name, mailbox_name(rec->mbox)))
return 1;
}
return 0;
}
HIDDEN void jmap_closembox(jmap_req_t *req, struct mailbox **mboxp)
{
struct _mboxcache_rec *rec = NULL;
int i;
if (mboxp == NULL || *mboxp == NULL) return;
for (i = 0; i < req->mboxes->count; i++) {
rec = (struct _mboxcache_rec*) ptrarray_nth(req->mboxes, i);
if (rec->mbox == *mboxp) {
if (!(--rec->refcount)) {
ptrarray_remove(req->mboxes, i);
mailbox_close(&rec->mbox);
free(rec);
}
*mboxp = NULL;
return;
}
}
syslog(LOG_INFO, "jmap: ignoring non-cached mailbox %s", mailbox_name(*mboxp));
}
struct findblob_data {
jmap_req_t *req;
const char *from_accountid;
int is_shared_account;
struct mailbox *mbox;
msgrecord_t *mr;
char *part_id;
unsigned exact : 1;
};
static int findblob_cb(const conv_guidrec_t *rec, void *rock)
{
struct findblob_data *d = (struct findblob_data*) rock;
jmap_req_t *req = d->req;
mbentry_t *mbentry = NULL;
int r = 0;
if (d->exact) {
// we only want top-level blobs
if (rec->part) return 0;
}
r = conv_guidrec_mbentry(rec, &mbentry);
if (r) {
syslog(LOG_ERR, "jmap_findblob: no mbentry for %s", conv_guidrec_mboxname(rec));
return r;
}
/* Check ACL */
if (d->is_shared_account) {
int rights = jmap_myrights_mbentry(req, mbentry);
if ((rights & JACL_READITEMS) != JACL_READITEMS) {
mboxlist_entry_free(&mbentry);
return 0;
}
}
r = jmap_openmbox(req, mbentry->name, &d->mbox, 0);
mboxlist_entry_free(&mbentry);
if (r) return r;
r = msgrecord_find(d->mbox, rec->uid, &d->mr);
if (r) {
jmap_closembox(req, &d->mbox);
d->mr = NULL;
return r;
}
d->part_id = rec->part ? xstrdup(rec->part) : NULL;
return IMAP_OK_COMPLETED;
}
static int _jmap_findblob(jmap_req_t *req, const char *from_accountid,
const char *blobid, unsigned exact,
struct mailbox **mbox, msgrecord_t **mr,
struct body **body, const struct body **part,
struct buf *blob)
{
const char *accountid = from_accountid ? from_accountid : req->accountid;
struct findblob_data data = {
req,
/* from_accountid */
accountid,
/* is_shared_account */
strcmp(req->userid, accountid),
/* mbox */
NULL,
/* mr */
NULL,
/* part_id */
NULL,
exact
};
struct body *mybody = NULL;
const struct body *mypart = NULL;
int i, r;
struct conversations_state *cstate, *mycstate = NULL;
syslog(LOG_DEBUG, "jmap_findblob (%s, %s)", from_accountid, blobid);
if (blob) {
/* We check for an empty buf below, so we better start with one */
buf_free(blob);
}
if (!exact && blob && req->inmemory_blobs) {
const struct buf *inmem = hash_lookup(blobid, req->inmemory_blobs);
if (inmem) {
buf_init_ro(blob, buf_base(inmem), buf_len(inmem));
r = 0;
goto done;
}
}
if (blobid[0] != 'G' || strlen(blobid) != 41) {
/* incomplete or incorrect blobid */
return IMAP_NOTFOUND;
}
if (strcmp(req->accountid, accountid)) {
cstate = conversations_get_user(accountid);
if (!cstate) {
r = conversations_open_user(accountid, 1/*shared*/, &mycstate);
if (r) goto done;
cstate = mycstate;
}
}
else {
cstate = req->cstate;
}
r = conversations_guid_foreach(cstate, blobid+1, findblob_cb, &data);
if (r != IMAP_OK_COMPLETED) {
if (!r) r = IMAP_NOTFOUND;
goto done;
}
/* Find part containing the data */
if (data.part_id) {
r = msgrecord_extract_bodystructure(data.mr, &mybody);
if (r) goto done;
ptrarray_t parts = PTRARRAY_INITIALIZER;
struct message_guid content_guid;
message_guid_decode(&content_guid, blobid+1);
ptrarray_push(&parts, mybody);
while ((mypart = ptrarray_shift(&parts))) {
if (!message_guid_cmp(&content_guid, &mypart->content_guid)) {
break;
}
if (!mypart->subpart) {
continue;
}
ptrarray_push(&parts, mypart->subpart);
for (i = 1; i < mypart->numparts; i++)
ptrarray_push(&parts, mypart->subpart + i);
}
ptrarray_fini(&parts);
if (!mypart) {
r = IMAP_NOTFOUND;
goto done;
}
}
if (blob && !buf_base(blob)) {
/* Map the message into memory */
r = msgrecord_get_body(data.mr, blob);
if (r) goto done;
}
r = 0;
done:
if (mycstate) {
conversations_commit(&mycstate);
}
if (r) {
if (data.mbox) jmap_closembox(req, &data.mbox);
if (mybody) {
message_free_body(mybody);
free(mybody);
}
}
else {
*mbox = data.mbox;
*mr = data.mr;
if (part) *part = mypart;
if (body) *body = mybody;
else if (mybody) {
message_free_body(mybody);
free(mybody);
}
}
if (data.part_id) free(data.part_id);
return r;
}
HIDDEN int jmap_findblob(jmap_req_t *req, const char *from_accountid,
const char *blobid,
struct mailbox **mbox, msgrecord_t **mr,
struct body **body, const struct body **part,
struct buf *blob)
{
return _jmap_findblob(req, from_accountid, blobid, 0 /*exact*/,
mbox, mr, body, part, blob);
}
// we need to pass mbox so we can keep it open until the file has been used
HIDDEN int jmap_findblob_exact(jmap_req_t *req, const char *from_accountid,
const char *blobid,
struct mailbox **mbox, msgrecord_t **mr,
struct buf *blob)
{
return _jmap_findblob(req, from_accountid, blobid, 1 /*exact*/,
mbox, mr, NULL /*body*/, NULL /*part*/, blob);
}
HIDDEN int jmap_cmpstate(jmap_req_t* req, json_t *state, int mbtype)
{
if (JNOTNULL(state)) {
const char *s = json_string_value(state);
if (!s) {
return -1;
}
modseq_t client_modseq = atomodseq_t(s);
modseq_t server_modseq = 0;
switch (mbtype_isa(mbtype)) {
case MBTYPE_CALENDAR:
server_modseq = req->counters.caldavmodseq;
break;
case MBTYPE_ADDRESSBOOK:
server_modseq = req->counters.carddavmodseq;
break;
case MBTYPE_JMAPSUBMIT:
server_modseq = req->counters.submissionmodseq;
break;
case MBTYPE_SIEVE:
server_modseq = req->counters.sievemodseq;
break;
default:
server_modseq = req->counters.mailmodseq;
}
if (client_modseq < server_modseq)
return -1;
else if (client_modseq > server_modseq)
return 1;
else
return 0;
}
return 0;
}
HIDDEN modseq_t jmap_highestmodseq(jmap_req_t *req, int mbtype)
{
modseq_t modseq;
/* Determine current counter by mailbox type. */
switch (mbtype_isa(mbtype)) {
case MBTYPE_CALENDAR:
modseq = req->counters.caldavmodseq;
break;
case MBTYPE_ADDRESSBOOK:
modseq = req->counters.carddavmodseq;
break;
case MBTYPE_JMAPSUBMIT:
modseq = req->counters.submissionmodseq;
break;
case MBTYPE_SIEVE:
modseq = req->counters.sievemodseq;
break;
case MBTYPE_EMAIL:
modseq = req->counters.mailmodseq;
break;
default:
modseq = req->counters.highestmodseq;
}
return modseq;
}
HIDDEN json_t* jmap_getstate(jmap_req_t *req, int mbtype, int refresh)
{
char *inboxname = mboxname_user_mbox(req->accountid, NULL);
if (refresh)
assert (!mboxname_read_counters(inboxname, &req->counters));
struct buf buf = BUF_INITIALIZER;
json_t *state = NULL;
modseq_t modseq = jmap_highestmodseq(req, mbtype);
buf_printf(&buf, MODSEQ_FMT, modseq);
state = json_string(buf_cstring(&buf));
buf_free(&buf);
free(inboxname);
return state;
}
HIDDEN json_t *jmap_fmtstate(modseq_t modseq)
{
struct buf buf = BUF_INITIALIZER;
json_t *state = NULL;
buf_printf(&buf, MODSEQ_FMT, modseq);
state = json_string(buf_cstring(&buf));
buf_free(&buf);
return state;
}
HIDDEN char *jmap_xhref(const char *mboxname, const char *resource)
{
/* XXX - look up root path from namespace? */
struct buf buf = BUF_INITIALIZER;
char *owner = mboxname_to_userid(mboxname);
const char *prefix = NULL;
if (mboxname_isaddressbookmailbox(mboxname, 0)) {
prefix = namespace_addressbook.prefix;
}
else if (mboxname_iscalendarmailbox(mboxname, 0)) {
prefix = namespace_calendar.prefix;
}
/* Path to home-set */
buf_printf(&buf, "%s/%s/%s", prefix, USER_COLLECTION_PREFIX, httpd_userid);
if (!strchr(httpd_userid, '@') && httpd_extradomain) {
buf_printf(&buf, "@%s", httpd_extradomain);
}
buf_putc(&buf, '/');
if (strcmp(owner, httpd_userid)) {
/* Encode shared collection as: <owner> "." <mboxname> */
buf_appendcstr(&buf, owner);
if (!strchr(owner, '@') && httpd_extradomain) {
buf_printf(&buf, "@%s", httpd_extradomain);
}
buf_putc(&buf, SHARED_COLLECTION_DELIM);
}
/* Collection */
buf_printf(&buf, "%s", strrchr(mboxname, '.')+1);
if (resource)
buf_printf(&buf, "/%s", resource);
free(owner);
return buf_release(&buf);
}
HIDDEN int jmap_myrights_mbentry(jmap_req_t *req, const mbentry_t *mbentry)
{
return _rights_for_mbentry(req->authstate, mbentry, req->mbstates);
}
HIDDEN int jmap_mbtype(jmap_req_t *req, const char *mboxname)
{
struct mbstate *mbstate = hash_lookup(mboxname, req->mbstates);
int mbtype;
if (!mbstate) {
mbentry_t *mbentry = NULL;
if (!jmap_mboxlist_lookup(mboxname, &mbentry, NULL)) {
mbstate = _mbstate_getoradd(req->authstate, mbentry, req->mbstates);
mbtype = mbstate->mbtype;
}
else mbtype = MBTYPE_UNKNOWN;
mboxlist_entry_free(&mbentry);
}
else mbtype = mbstate->mbtype;
return mbtype_isa(mbtype);
}
// gotta have them all
HIDDEN int jmap_hasrights_mbentry(jmap_req_t *req, const mbentry_t *mbentry, int rights)
{
int myrights = jmap_myrights_mbentry(req, mbentry);
if ((myrights & rights) == rights) return 1;
return 0;
}
HIDDEN int jmap_myrights(jmap_req_t *req, const char *mboxname)
{
struct mbstate *mbstate = hash_lookup(mboxname, req->mbstates);
if (mbstate) return mbstate->rights;
// if unable to read, that means no rights
int rights = 0;
mbentry_t *mbentry = NULL;
if (!jmap_mboxlist_lookup(mboxname, &mbentry, NULL)) {
rights = _rights_for_mbentry(req->authstate, mbentry, req->mbstates);
}
mboxlist_entry_free(&mbentry);
return rights;
}
HIDDEN int jmap_myrights_mboxid(jmap_req_t *req, const char *mboxid)
{
int rights = 0;
const mbentry_t *mbentry = jmap_mbentry_by_uniqueid(req, mboxid);
if (mbentry) {
rights = jmap_myrights_mbentry(req, mbentry);
}
return rights;
}
HIDDEN int jmap_hasrights_mboxid(jmap_req_t *req, const char *mboxid, int rights)
{
const mbentry_t *mbentry = jmap_mbentry_by_uniqueid(req, mboxid);
return mbentry ? jmap_hasrights_mbentry(req, mbentry, rights) : 0;
}
// gotta have them all
HIDDEN int jmap_hasrights(jmap_req_t *req, const char *mboxname, int rights)
{
int myrights = jmap_myrights(req, mboxname);
if ((myrights & rights) == rights) return 1;
return 0;
}
HIDDEN void jmap_myrights_delete(jmap_req_t *req, const char *mboxname)
{
struct mbstate *mbstate = hash_del(mboxname, req->mbstates);
free(mbstate);
}
/* Add performance stats to method response */
static void jmap_add_perf(jmap_req_t *req, json_t *res)
{
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
json_t *perf = json_pack("{s:f s:f s:f}",
"real", (now_ms() / 1000.0) - req->real_start,
"user", timeval_get_double(&usage.ru_utime) - req->user_start,
"sys", timeval_get_double(&usage.ru_stime) - req->sys_start);
json_object_set(perf, "details", req->perf_details); // incref
json_object_set_new(res, "performance", perf);
}
HIDDEN void jmap_ok(jmap_req_t *req, json_t *res)
{
json_object_set_new(res, "accountId", json_string(req->accountid));
json_t *item = json_array();
json_array_append_new(item, json_string(req->method));
json_array_append_new(item, res);
json_array_append_new(item, json_string(req->tag));
json_array_append_new(req->response, item);
if (jmap_is_using(req, JMAP_PERFORMANCE_EXTENSION))
jmap_add_perf(req, res);
}
HIDDEN void jmap_error(jmap_req_t *req, json_t *err)
{
json_array_append_new(req->response,
json_pack("[s,o,s]", "error", err, req->tag));
}
HIDDEN int jmap_parse_strings(json_t *arg,
struct jmap_parser *parser, const char *prop)
{
if (!json_is_array(arg)) {
jmap_parser_invalid(parser, prop);
return 0;
}
int valid = 1;
size_t i;
json_t *val;
json_array_foreach(arg, i, val) {
if (!json_is_string(val)) {
jmap_parser_push_index(parser, prop, i, NULL);
jmap_parser_invalid(parser, NULL);
jmap_parser_pop(parser);
valid = 0;
}
}
return valid;
}
HIDDEN const jmap_property_t *jmap_property_find(const char *name,
const jmap_property_t props[])
{
const jmap_property_t *prop;
for (prop = props; prop && prop->name; prop++) {
if (!strcmp(name, prop->name)) return prop;
else {
size_t len = strlen(prop->name);
if ((prop->name[len-1] == '*') && !strncmp(name, prop->name, len-1))
return prop;
}
}
return NULL;
}
/* Foo/get */
HIDDEN void jmap_get_parse(jmap_req_t *req,
struct jmap_parser *parser,
const jmap_property_t valid_props[],
int allow_null_ids,
jmap_args_parse_cb args_parse,
void *args_rock,
struct jmap_get *get,
json_t **err)
{
json_t *jargs = req->args;
const char *key;
json_t *arg, *val;
size_t i;
memset(get, 0, sizeof(struct jmap_get));
get->list = json_array();
get->not_found = json_array();
json_object_foreach(jargs, key, arg) {
if (!strcmp(key, "accountId")) {
/* already handled in jmap_api() */
}
else if (!strcmp(key, "ids")) {
if (json_is_array(arg)) {
get->ids = json_array();
/* JMAP spec requires: "If an identical id is included
* more than once in the request, the server MUST only
* include it once in either the list or notFound
* argument of the response."
* So let's weed out duplicate ids here. */
hash_table _dedup = HASH_TABLE_INITIALIZER;
construct_hash_table(&_dedup, json_array_size(arg) + 1, 0);
json_array_foreach(arg, i, val) {
const char *id = json_string_value(val);
if (!id) {
jmap_parser_push_index(parser, "ids", i, NULL);
jmap_parser_invalid(parser, NULL);
jmap_parser_pop(parser);
continue;
}
/* Weed out unknown creation ids and add the ids of known
* creation ids to the requested ids list. THis might
* cause a race if the Foo object pointed to by creation
* id is deleted between parsing the request and answering
* it. But re-checking creation ids for their existence
* later in the control flow just shifts the problem */
if (*id == '#') {
const char *id2 = jmap_lookup_id(req, id + 1);
if (!id2) {
json_array_append_new(get->not_found,
json_string(id));
continue;
}
id = id2;
}
if (hash_lookup(id, &_dedup)) {
continue;
}
json_array_append_new(get->ids, json_string(id));
}
free_hash_table(&_dedup, NULL);
}
else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "ids");
}
}
else if (!strcmp(key, "properties")) {
if (json_is_array(arg)) {
get->props = xzmalloc(sizeof(hash_table));
construct_hash_table(get->props, json_array_size(arg) + 1, 0);
json_array_foreach(arg, i, val) {
const char *name = json_string_value(val);
const jmap_property_t *propdef = NULL;
if (name) {
propdef = jmap_property_find(name, valid_props);
if (propdef && propdef->capability &&
!jmap_is_using(req, propdef->capability)) {
propdef = NULL;
}
}
if (!propdef) {
jmap_parser_push_index(parser, "properties", i, name);
jmap_parser_invalid(parser, NULL);
jmap_parser_pop(parser);
continue;
}
hash_insert(name, (void*)1, get->props);
}
}
else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "properties");
}
}
else if (!args_parse || !args_parse(req, parser, key, arg, args_rock)) {
jmap_parser_invalid(parser, key);
}
}
if (json_array_size(parser->invalid)) {
*err = json_pack("{s:s s:O}", "type", "invalidArguments",
"arguments", parser->invalid);
return;
}
if (!allow_null_ids && !JNOTNULL(get->ids)) {
*err = json_pack("{s:s, s:s}", "type", "requestTooLarge",
"description", "ids must be specified");
return;
}
if (*err) return;
if (get->props == NULL) {
/* Initialize default properties */
int nvalid = 0;
const jmap_property_t *prop;
for (prop = valid_props; prop && prop->name; prop++) {
nvalid++;
}
get->props = xzmalloc(sizeof(hash_table));
construct_hash_table(get->props, nvalid + 1, 0);
for (prop = valid_props; prop && prop->name; prop++) {
if (prop->flags & JMAP_PROP_SKIP_GET) {
continue;
}
if (!prop->capability || jmap_is_using(req, prop->capability)) {
hash_insert(prop->name, (void*)1, get->props);
}
}
}
else {
const jmap_property_t *prop;
for (prop = valid_props; prop && prop->name; prop++) {
if (prop->flags & JMAP_PROP_ALWAYS_GET) {
if (!hash_lookup(prop->name, get->props)) {
hash_insert(prop->name, (void*)1, get->props);
}
}
}
}
/* Number of ids checked in validate_request() */
}
HIDDEN void jmap_get_fini(struct jmap_get *get)
{
free_hash_table(get->props, NULL);
free(get->props);
free(get->state);
json_decref(get->ids);
json_decref(get->list);
json_decref(get->not_found);
}
HIDDEN json_t *jmap_get_reply(struct jmap_get *get)
{
json_t *res = json_object();
json_object_set_new(res, "state", json_string(get->state));
json_object_set(res, "list", get->list);
json_object_set(res, "notFound", get->not_found);
return res;
}
/* Foo/set */
static void jmap_set_validate_props(jmap_req_t *req, const char *id, json_t *jobj,
const jmap_property_t valid_props[],
json_t **err)
{
json_t *invalid = json_array();
const char *path;
json_t *jval;
json_object_foreach(jobj, path, jval) {
/* Determine property name */
const char *pname = path;
char *tmp = NULL;
const char *slash = strchr(pname, '/');
if (slash) {
tmp = jmap_pointer_decode(pname, slash - path);
if (tmp) pname = tmp;
}
/* Validate against property spec */
const jmap_property_t *prop = jmap_property_find(pname, valid_props);
if (!prop) {
json_array_append_new(invalid, json_string(path));
}
else if (prop->capability && !jmap_is_using(req, prop->capability)) {
json_array_append_new(invalid, json_string(path));
}
else if (id) {
/* update */
if (!strcmp("id", prop->name) &&
strcmpnull(id, json_string_value(jval))) {
/* can NEVER change id */
json_array_append_new(invalid, json_string(path));
}
/* XXX could check IMMUTABLE and SERVER_SET here, but we can't
* reject such properties if they match the current value */
}
else {
/* create */
if (prop->flags & JMAP_PROP_SERVER_SET) {
json_array_append_new(invalid, json_string(path));
}
}
if (tmp) free(tmp);
}
if (json_array_size(invalid)) {
*err = json_pack("{s:s s:o}",
"type", "invalidProperties",
"properties", invalid);
}
else {
json_decref(invalid);
}
}
HIDDEN void jmap_set_parse(jmap_req_t *req, struct jmap_parser *parser,
const jmap_property_t valid_props[],
jmap_args_parse_cb args_parse, void *args_rock,
struct jmap_set *set, json_t **err)
{
json_t *jargs = req->args;
memset(set, 0, sizeof(struct jmap_set));
set->create = json_object();
set->update = json_object();
set->destroy = json_array();
set->created = json_object();
set->updated = json_object();
set->destroyed = json_array();
set->not_created = json_object();
set->not_updated = json_object();
set->not_destroyed = json_object();
const char *key;
json_t *arg, *val;
json_object_foreach(jargs, key, arg) {
if (!strcmp(key, "accountId")) {
/* already handled in jmap_api() */
}
/* ifInState */
else if (!strcmp(key, "ifInState")) {
if (json_is_string(arg)) {
set->if_in_state = json_string_value(arg);
}
else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "ifInState");
}
}
/* create */
else if (!strcmp(key, "create")) {
if (json_is_object(arg)) {
const char *id;
json_object_foreach(arg, id, val) {
if (!json_is_object(val)) {
jmap_parser_push(parser, "create");
jmap_parser_invalid(parser, id);
jmap_parser_pop(parser);
continue;
}
json_object_set(set->create, id, val);
}
}
else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "create");
}
}
/* update */
else if (!strcmp(key, "update")) {
if (json_is_object(arg)) {
const char *id;
json_object_foreach(arg, id, val) {
if (!json_is_object(val)) {
jmap_parser_push(parser, "update");
jmap_parser_invalid(parser, id);
jmap_parser_pop(parser);
continue;
}
json_object_set(set->update, id, val);
}
}
else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "update");
}
}
/* destroy */
else if (!strcmp(key, "destroy")) {
if (JNOTNULL(arg)) {
jmap_parse_strings(arg, parser, "destroy");
if (!json_array_size(parser->invalid)) {
json_decref(set->destroy);
set->destroy = json_incref(arg);
}
}
}
else if (!args_parse || !args_parse(req, parser, key, arg, args_rock)) {
jmap_parser_invalid(parser, key);
}
}
if (json_array_size(parser->invalid)) {
*err = json_pack("{s:s s:O}", "type", "invalidArguments",
"arguments", parser->invalid);
}
if (valid_props) {
json_t *jval;
/* Make sure no property is set without its capability */
json_object_foreach(json_object_get(jargs, "create"), key, jval) {
json_t *err = NULL;
jmap_set_validate_props(req, NULL, jval, valid_props, &err);
if (err) {
json_object_del(set->create, key);
json_object_set_new(set->not_created, key, err);
}
}
json_object_foreach(json_object_get(jargs, "update"), key, jval) {
json_t *err = NULL;
jmap_set_validate_props(req, key, jval, valid_props, &err);
if (err) {
json_object_del(set->update, key);
json_object_set_new(set->not_updated, key, err);
}
}
// TODO We could report the following set errors here:
// -invalidPatch
// - willDestroy
}
}
HIDDEN void jmap_set_fini(struct jmap_set *set)
{
free(set->old_state);
free(set->new_state);
json_decref(set->create);
json_decref(set->update);
json_decref(set->destroy);
json_decref(set->created);
json_decref(set->updated);
json_decref(set->destroyed);
json_decref(set->not_created);
json_decref(set->not_updated);
json_decref(set->not_destroyed);
}
HIDDEN json_t *jmap_set_reply(struct jmap_set *set)
{
json_t *res = json_object();
json_object_set_new(res, "oldState",
set->old_state ? json_string(set->old_state) : json_null());
json_object_set_new(res, "newState", json_string(set->new_state));
json_object_set(res, "created", json_object_size(set->created) ?
set->created : json_null());
json_object_set(res, "updated", json_object_size(set->updated) ?
set->updated : json_null());
json_object_set(res, "destroyed", json_array_size(set->destroyed) ?
set->destroyed : json_null());
json_object_set(res, "notCreated", json_object_size(set->not_created) ?
set->not_created : json_null());
json_object_set(res, "notUpdated", json_object_size(set->not_updated) ?
set->not_updated : json_null());
json_object_set(res, "notDestroyed", json_object_size(set->not_destroyed) ?
set->not_destroyed : json_null());
return res;
}
/* Foo/changes */
HIDDEN void jmap_changes_parse(jmap_req_t *req,
struct jmap_parser *parser,
modseq_t minmodseq,
jmap_args_parse_cb args_parse,
void *args_rock,
struct jmap_changes *changes,
json_t **err)
{
json_t *jargs = req->args;
const char *key;
json_t *arg;
int have_sincemodseq = 0;
memset(changes, 0, sizeof(struct jmap_changes));
changes->created = json_array();
changes->updated = json_array();
changes->destroyed = json_array();
json_object_foreach(jargs, key, arg) {
if (!strcmp(key, "accountId")) {
/* already handled in jmap_api() */
}
/* sinceState */
else if (!strcmp(key, "sinceState")) {
if (json_is_string(arg) && imparse_isnumber(json_string_value(arg))) {
have_sincemodseq = 1;
changes->since_modseq = atomodseq_t(json_string_value(arg));
}
else {
jmap_parser_invalid(parser, "sinceState");
}
}
/* maxChanges */
else if (!strcmp(key, "maxChanges")) {
if (json_is_integer(arg) && json_integer_value(arg) > 0) {
changes->max_changes = json_integer_value(arg);
} else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "maxChanges");
}
}
else if (!args_parse || !args_parse(req, parser, key, arg, args_rock)) {
jmap_parser_invalid(parser, key);
}
}
if (json_array_size(parser->invalid)) {
*err = json_pack("{s:s s:O}", "type", "invalidArguments",
"arguments", parser->invalid);
}
else if (!have_sincemodseq) {
*err = json_pack("{s:s s:s}", "type", "cannotCalculateChanges",
"description", "no sinceModseq");
}
else if (changes->since_modseq < minmodseq) {
*err = json_pack("{s:s s:s}", "type", "cannotCalculateChanges",
"description", "outdated sinceModseq");
}
}
HIDDEN void jmap_changes_fini(struct jmap_changes *changes)
{
json_decref(changes->created);
json_decref(changes->updated);
json_decref(changes->destroyed);
}
HIDDEN json_t *jmap_changes_reply(struct jmap_changes *changes)
{
json_t *res = json_object();
json_object_set_new(res, "oldState", jmap_fmtstate(changes->since_modseq));
json_object_set_new(res, "newState", jmap_fmtstate(changes->new_modseq));
json_object_set_new(res, "hasMoreChanges",
json_boolean(changes->has_more_changes));
json_object_set(res, "created", changes->created);
json_object_set(res, "updated", changes->updated);
json_object_set(res, "destroyed", changes->destroyed);
return res;
}
/* Foo/copy */
HIDDEN void jmap_copy_parse(jmap_req_t *req, struct jmap_parser *parser,
jmap_args_parse_cb args_parse, void *args_rock,
struct jmap_copy *copy, json_t **err)
{
json_t *jargs = req->args;
memset(copy, 0, sizeof(struct jmap_copy));
copy->blob_copy = !strcmp(req->method, "Blob/copy");
copy->create = copy->blob_copy ? json_array() : json_object();
copy->created = json_object();
copy->not_created = json_object();
const char *key;
json_t *arg;
json_object_foreach(jargs, key, arg) {
/* fromAccountId */
if (!strcmp(key, "fromAccountId")) {
if (json_is_string(arg)) {
copy->from_account_id = json_string_value(arg);
}
else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "fromAccountId");
}
}
/* accountId */
else if (!strcmp(key, "accountId")) {
/* JMAP request parser already set it */
assert(req->accountid);
continue;
}
/* blobIds */
else if (copy->blob_copy &&
!strcmp(key, "blobIds") && json_is_array(arg)) {
struct buf buf = BUF_INITIALIZER;
json_t *id;
size_t i;
json_array_foreach(arg, i, id) {
if (!json_is_string(id)) {
buf_printf(&buf, "blobIds[%zu]", i);
jmap_parser_invalid(parser, buf_cstring(&buf));
buf_reset(&buf);
}
else json_array_append(copy->create, id);
}
}
/* create */
else if (!copy->blob_copy &&
!strcmp(key, "create") && json_is_object(arg)) {
jmap_parser_push(parser, "create");
const char *creation_id;
json_t *obj;
json_object_foreach(arg, creation_id, obj) {
if (!json_is_object(obj)) {
jmap_parser_invalid(parser, creation_id);
}
else if (!json_is_string(json_object_get(obj, "id"))) {
jmap_parser_push(parser, creation_id);
jmap_parser_invalid(parser, "id");
jmap_parser_pop(parser);
}
else json_object_set(copy->create, creation_id, obj);
}
jmap_parser_pop(parser);
}
/* onSuccessDestroyOriginal */
else if (!copy->blob_copy && !strcmp(key, "onSuccessDestroyOriginal") &&
json_is_boolean(arg)) {
copy->on_success_destroy_original = json_boolean_value(arg);
}
else if (!args_parse || !args_parse(req, parser, key, arg, args_rock)) {
jmap_parser_invalid(parser, key);
}
}
if (json_array_size(parser->invalid)) {
*err = json_pack("{s:s s:O}", "type", "invalidArguments",
"arguments", parser->invalid);
}
if (!req->accountid || !copy->from_account_id ||
!strcmp(req->accountid, copy->from_account_id)) {
*err = json_pack("{s:s s:[s,s]}", "type", "invalidArguments",
"arguments", "accountId", "fromAccountId");
}
}
HIDDEN void jmap_copy_fini(struct jmap_copy *copy)
{
json_decref(copy->create);
json_decref(copy->created);
json_decref(copy->not_created);
}
HIDDEN json_t *jmap_copy_reply(struct jmap_copy *copy)
{
json_t *res = json_object();
json_object_set_new(res, "fromAccountId",
json_string(copy->from_account_id));
json_object_set(res, copy->blob_copy ? "copied" : "created",
json_object_size(copy->created) ?
copy->created : json_null());
json_object_set(res, copy->blob_copy ? "notCopied" : "notCreated",
json_object_size(copy->not_created) ?
copy->not_created : json_null());
return res;
}
/* Foo/query */
HIDDEN jmap_filter *jmap_buildfilter(json_t *arg, jmap_buildfilter_cb *parse)
{
jmap_filter *f = (jmap_filter *) xzmalloc(sizeof(struct jmap_filter));
int pe;
const char *val;
int iscond = 1;
/* operator */
pe = jmap_readprop(arg, "operator", 0 /*mandatory*/, NULL, "s", &val);
if (pe > 0) {
if (!strncmp("AND", val, 3)) {
f->op = JMAP_FILTER_OP_AND;
} else if (!strncmp("OR", val, 2)) {
f->op = JMAP_FILTER_OP_OR;
} else if (!strncmp("NOT", val, 3)) {
f->op = JMAP_FILTER_OP_NOT;
}
}
iscond = f->op == JMAP_FILTER_OP_NONE;
/* conditions */
json_t *conds = json_object_get(arg, "conditions");
if (conds && !iscond && json_array_size(conds)) {
size_t i, n_conditions = json_array_size(conds);
for (i = 0; i < n_conditions; i++) {
json_t *cond = json_array_get(conds, i);
ptrarray_push(&f->conditions, jmap_buildfilter(cond, parse));
}
}
if (iscond) {
ptrarray_push(&f->conditions, parse(arg));
}
return f;
}
HIDDEN int jmap_filter_match(jmap_filter *f,
jmap_filtermatch_cb *match, void *rock)
{
if (f->op == JMAP_FILTER_OP_NONE) {
return match(ptrarray_head(&f->conditions), rock);
} else {
int i;
for (i = 0; i < ptrarray_size(&f->conditions); i++) {
int m = jmap_filter_match(ptrarray_nth(&f->conditions, i), match, rock);
if (m && f->op == JMAP_FILTER_OP_OR) {
return 1;
} else if (m && f->op == JMAP_FILTER_OP_NOT) {
return 0;
} else if (!m && f->op == JMAP_FILTER_OP_AND) {
return 0;
}
}
return f->op == JMAP_FILTER_OP_AND || f->op == JMAP_FILTER_OP_NOT;
}
}
HIDDEN void jmap_filter_free(jmap_filter *f, jmap_filterfree_cb *freecond)
{
void *cond;
while ((cond = ptrarray_pop(&f->conditions))) {
if (f->op == JMAP_FILTER_OP_NONE) {
if (freecond) freecond(cond);
}
else {
jmap_filter_free(cond, freecond);
}
}
ptrarray_fini(&f->conditions);
free(f);
}
HIDDEN void jmap_filter_parse(jmap_req_t *req, struct jmap_parser *parser,
json_t *filter, json_t *unsupported,
jmap_filter_parse_cb parse_condition, void *cond_rock,
json_t **err)
{
json_t *arg, *val;
const char *s;
size_t i;
if (err && *err) return;
if (!JNOTNULL(filter) || json_typeof(filter) != JSON_OBJECT) {
jmap_parser_invalid(parser, NULL);
return;
}
arg = json_object_get(filter, "operator");
if ((s = json_string_value(arg))) {
if (strcmp("AND", s) && strcmp("OR", s) && strcmp("NOT", s)) {
jmap_parser_invalid(parser, "operator");
}
arg = json_object_get(filter, "conditions");
if (!json_array_size(arg)) {
jmap_parser_invalid(parser, "conditions");
}
json_array_foreach(arg, i, val) {
jmap_parser_push_index(parser, "conditions", i, NULL);
jmap_filter_parse(req, parser, val, unsupported, parse_condition, cond_rock, err);
jmap_parser_pop(parser);
}
} else if (arg) {
jmap_parser_invalid(parser, "operator");
} else {
parse_condition(req, parser, filter, unsupported, cond_rock, err);
}
}
HIDDEN void jmap_comparator_parse(jmap_req_t *req, struct jmap_parser *parser,
json_t *jsort, json_t *unsupported,
jmap_comparator_parse_cb comp_cb, void *comp_rock,
json_t **err)
{
if (!json_is_object(jsort)) {
jmap_parser_invalid(parser, NULL);
return;
}
struct jmap_comparator comp = { NULL, 0, NULL };
/* property */
json_t *val = json_object_get(jsort, "property");
comp.property = json_string_value(val);
if (!comp.property) {
jmap_parser_invalid(parser, "property");
}
/* isAscending */
comp.is_ascending = 1;
val = json_object_get(jsort, "isAscending");
if (JNOTNULL(val)) {
if (!json_is_boolean(val)) {
jmap_parser_invalid(parser, "isAscending");
}
comp.is_ascending = json_boolean_value(val);
}
/* collation */
val = json_object_get(jsort, "collation");
if (JNOTNULL(val) && !json_is_string(val)) {
jmap_parser_invalid(parser, "collation");
}
comp.collation = json_string_value(val);
if (comp.property && !comp_cb(req, &comp, comp_rock, err)) {
struct buf buf = BUF_INITIALIZER;
json_array_append_new(unsupported,
json_string(jmap_parser_path(parser, &buf)));
buf_free(&buf);
}
}
HIDDEN void jmap_query_parse(jmap_req_t *req, struct jmap_parser *parser,
jmap_args_parse_cb args_parse, void *args_rock,
jmap_filter_parse_cb filter_cb, void *filter_rock,
jmap_comparator_parse_cb comp_cb, void *comp_rock,
struct jmap_query *query, json_t **err)
{
json_t *jargs = req->args;
const char *key;
json_t *arg, *val;
size_t i;
memset(query, 0, sizeof(struct jmap_query));
query->ids = json_array();
query->have_total = 1; /* assume we know the total, we turn it off it not */
json_t *unsupported_filter = json_array();
json_t *unsupported_sort = json_array();
json_object_foreach(jargs, key, arg) {
if (!strcmp(key, "accountId")) {
/* already handled in jmap_api() */
}
/* filter */
else if (!strcmp(key, "filter")) {
if (json_is_object(arg)) {
jmap_parser_push(parser, "filter");
jmap_filter_parse(req, parser, arg, unsupported_filter,
filter_cb, filter_rock, err);
jmap_parser_pop(parser);
query->filter = arg;
if (err && *err) {
goto done;
}
}
else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "filter");
}
}
/* sort */
else if (!strcmp(key, "sort")) {
if (json_is_array(arg)) {
json_array_foreach(arg, i, val) {
jmap_parser_push_index(parser, "sort", i, NULL);
jmap_comparator_parse(req, parser, val, unsupported_sort,
comp_cb, comp_rock, err);
jmap_parser_pop(parser);
if (err && *err) {
goto done;
}
}
if (json_array_size(arg)) {
query->sort = arg;
}
}
else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "sort");
}
}
else if (!strcmp(key, "position")) {
if (json_is_integer(arg)) {
query->position = json_integer_value(arg);
}
else if (arg) {
jmap_parser_invalid(parser, "position");
}
}
else if (!strcmp(key, "anchor")) {
if (json_is_string(arg)) {
query->anchor = json_string_value(arg);
} else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "anchor");
}
}
else if (!strcmp(key, "anchorOffset")) {
if (json_is_integer(arg)) {
query->anchor_offset = json_integer_value(arg);
} else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "anchorOffset");
}
}
else if (!strcmp(key, "limit")) {
if (json_is_integer(arg) && json_integer_value(arg) >= 0) {
query->limit = json_integer_value(arg);
query->have_limit = 1;
} else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "limit");
}
}
else if (!strcmp(key, "calculateTotal")) {
if (json_is_boolean(arg)) {
query->calculate_total = json_boolean_value(arg);
} else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "calculateTotal");
}
}
else if (!args_parse || !args_parse(req, parser, key, arg, args_rock)) {
jmap_parser_invalid(parser, key);
}
}
if (json_array_size(parser->invalid)) {
*err = json_pack("{s:s s:O}", "type", "invalidArguments",
"arguments", parser->invalid);
}
else if (json_array_size(unsupported_filter)) {
*err = json_pack("{s:s s:O}", "type", "unsupportedFilter",
"filters", unsupported_filter);
}
else if (json_array_size(unsupported_sort)) {
*err = json_pack("{s:s s:O}", "type", "unsupportedSort",
"sort", unsupported_sort);
}
done:
json_decref(unsupported_filter);
json_decref(unsupported_sort);
}
HIDDEN void jmap_query_fini(struct jmap_query *query)
{
free(query->query_state);
json_decref(query->ids);
}
HIDDEN json_t *jmap_query_reply(struct jmap_query *query)
{
json_t *res = json_object();
json_object_set(res, "filter", query->filter);
json_object_set(res, "sort", query->sort);
json_object_set_new(res, "queryState", json_string(query->query_state));
json_object_set_new(res, "canCalculateChanges",
json_boolean(query->can_calculate_changes));
json_object_set_new(res, "position", json_integer(query->result_position));
if (query->have_total)
json_object_set_new(res, "total", json_integer(query->total));
/* Special case total */
if (query->position > 0 && query->total && query->total < SSIZE_MAX) {
if (query->position > (ssize_t) query->total) {
json_decref(query->ids);
query->ids = json_array();
}
}
/* Special case limit 0 */
if (query->have_limit && query->limit == 0) {
json_array_clear(query->ids);
}
/* Special case clamped limit */
if (query->server_limit) {
json_object_set_new(res, "limit", json_integer(query->server_limit));
}
json_object_set(res, "ids", query->ids);
return res;
}
/* Foo/queryChanges */
HIDDEN void jmap_querychanges_parse(jmap_req_t *req,
struct jmap_parser *parser,
jmap_args_parse_cb args_parse, void *args_rock,
jmap_filter_parse_cb filter_cb, void *filter_rock,
jmap_comparator_parse_cb comp_cb, void *comp_rock,
struct jmap_querychanges *query,
json_t **err)
{
json_t *jargs = req->args;
const char *key;
json_t *arg, *val;
size_t i;
memset(query, 0, sizeof(struct jmap_querychanges));
query->removed = json_array();
query->added = json_array();
json_t *unsupported_filter = json_array();
json_t *unsupported_sort = json_array();
json_object_foreach(jargs, key, arg) {
if (!strcmp(key, "accountId")) {
/* already handled in jmap_api() */
}
/* filter */
else if (!strcmp(key, "filter")) {
if (json_is_object(arg)) {
jmap_parser_push(parser, "filter");
jmap_filter_parse(req, parser, arg, unsupported_filter,
filter_cb, filter_rock, err);
jmap_parser_pop(parser);
query->filter = arg;
if (err && *err) {
goto done;
}
}
else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "filter");
}
}
/* sort */
else if (!strcmp(key, "sort")) {
if (json_is_array(arg)) {
json_array_foreach(arg, i, val) {
jmap_parser_push_index(parser, "sort", i, NULL);
jmap_comparator_parse(req, parser, val, unsupported_sort,
comp_cb, comp_rock, err);
jmap_parser_pop(parser);
}
if (json_array_size(arg)) {
query->sort = arg;
}
}
else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "sort");
}
}
/* sinceQueryState */
else if (!strcmp(key, "sinceQueryState")) {
if (json_is_string(arg)) {
query->since_querystate = json_string_value(arg);
} else {
jmap_parser_invalid(parser, "sinceQueryState");
}
}
/* maxChanges */
else if (!strcmp(key, "maxChanges")) {
if (json_is_integer(arg) && json_integer_value(arg) > 0) {
query->max_changes = json_integer_value(arg);
} else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "maxChanges");
}
}
/* upToId */
else if (!strcmp(key, "upToId")) {
if (json_is_string(arg)) {
query->up_to_id = json_string_value(arg);
} else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "upToId");
}
}
/* calculateTotal */
else if (!strcmp(key, "calculateTotal")) {
if (json_is_boolean(arg)) {
query->calculate_total = json_boolean_value(arg);
} else if (JNOTNULL(arg)) {
jmap_parser_invalid(parser, "calculateTotal");
}
}
else if (!args_parse || !args_parse(req, parser, key, arg, args_rock)) {
jmap_parser_invalid(parser, key);
}
}
if (query->since_querystate == NULL) {
jmap_parser_invalid(parser, "sinceQueryState");
}
if (json_array_size(parser->invalid)) {
*err = json_pack("{s:s s:O}", "type", "invalidArguments",
"arguments", parser->invalid);
}
else if (json_array_size(unsupported_filter)) {
*err = json_pack("{s:s s:O}", "type", "unsupportedFilter",
"filters", unsupported_filter);
}
else if (json_array_size(unsupported_sort)) {
*err = json_pack("{s:s s:O}", "type", "unsupportedSort",
"sort", unsupported_sort);
}
done:
json_decref(unsupported_filter);
json_decref(unsupported_sort);
}
HIDDEN void jmap_querychanges_fini(struct jmap_querychanges *query)
{
free(query->new_querystate);
json_decref(query->removed);
json_decref(query->added);
}
HIDDEN json_t *jmap_querychanges_reply(struct jmap_querychanges *query)
{
json_t *res = json_object();
json_object_set(res, "filter", query->filter);
json_object_set(res, "sort", query->sort);
json_object_set_new(res, "oldQueryState",
json_string(query->since_querystate));
json_object_set_new(res, "newQueryState",
json_string(query->new_querystate));
json_object_set_new(res, "upToId", query->up_to_id ?
json_string(query->up_to_id) : json_null());
json_object_set(res, "removed", query->removed);
json_object_set(res, "added", query->added);
json_object_set_new(res, "total", json_integer(query->total));
return res;
}
/* Foo/parse */
HIDDEN void jmap_parse_parse(jmap_req_t *req,
struct jmap_parser *parser,
jmap_args_parse_cb args_parse,
void *args_rock,
struct jmap_parse *parse,
json_t **err)
{
json_t *jargs = req->args;
const char *key;
json_t *arg;
memset(parse, 0, sizeof(struct jmap_parse));
parse->parsed = json_object();
parse->not_parsable = json_array();
parse->not_found = json_array();
json_object_foreach(jargs, key, arg) {
if (!strcmp(key, "accountId")) {
/* already handled in jmap_api() */
}
else if (!strcmp(key, "blobIds")) {
jmap_parse_strings(arg, parser, "blobIds");
parse->blob_ids = arg;
}
else if (!args_parse || !args_parse(req, parser, key, arg, args_rock)) {
jmap_parser_invalid(parser, key);
}
}
if (json_array_size(parser->invalid)) {
*err = json_pack("{s:s s:O}", "type", "invalidArguments",
"arguments", parser->invalid);
}
}
HIDDEN void jmap_parse_fini(struct jmap_parse *parse)
{
json_decref(parse->parsed);
json_decref(parse->not_parsable);
json_decref(parse->not_found);
}
HIDDEN json_t *jmap_parse_reply(struct jmap_parse *parse)
{
json_t *res = json_object();
if (json_object_size(parse->parsed))
json_object_set(res, "parsed", parse->parsed);
else
json_object_set_new(res, "parsed", json_null());
if (json_array_size(parse->not_parsable))
json_object_set(res, "notParsable", parse->not_parsable);
else
json_object_set_new(res, "notParsable", json_null());
if (json_array_size(parse->not_found))
json_object_set(res, "notFound", parse->not_found);
else
json_object_set_new(res, "notFound", json_null());
return res;
}
static json_t *_json_has(int rights, int need)
{
return (((rights & need) == need) ? json_true() : json_false());
}
HIDDEN json_t *jmap_get_sharewith(const mbentry_t *mbentry)
{
char *aclstr = xstrdupnull(mbentry->acl);
char *owner = mboxname_to_userid(mbentry->name);
int iscalendar = mbtype_isa(mbentry->mbtype) == MBTYPE_CALENDAR;
json_t *sharewith = json_null();
char *userid;
char *nextid;
for (userid = aclstr; userid; userid = nextid) {
int rights;
char *rightstr;
rightstr = strchr(userid, '\t');
if (!rightstr) break;
*rightstr++ = '\0';
nextid = strchr(rightstr, '\t');
if (!nextid) break;
*nextid++ = '\0';
cyrus_acl_strtomask(rightstr, &rights);
// skip system users and owner
if (is_system_user(userid)) continue;
if (!strcmp(userid, owner)) continue;
// we've got one! Create the object if this is the first
if (!JNOTNULL(sharewith))
sharewith = json_object();
json_t *obj = json_object();
json_object_set_new(sharewith, userid, obj);
if (iscalendar)
json_object_set_new(obj, "mayReadFreeBusy",
_json_has(rights, JACL_READFB));
json_object_set_new(obj, "mayRead",
_json_has(rights, JACL_READITEMS));
json_object_set_new(obj, "mayWrite",
_json_has(rights, JACL_WRITE));
json_object_set_new(obj, "mayAdmin",
_json_has(rights, JACL_ADMIN));
}
free(aclstr);
free(owner);
return sharewith;
}
struct acl_item {
unsigned int mayAdmin:1;
unsigned int mayWrite:1;
unsigned int mayPost:1;
unsigned int mayRead:1;
unsigned int mayReadFreeBusy:1;
};
struct acl_change {
struct acl_item old;
struct acl_item new;
};
struct invite_rock {
xmlNodePtr notify;
xmlNsPtr ns[NUM_NAMESPACE];
const char *owner;
const char *mboxname;
struct buf resource;
struct request_target_t tgt;
const struct prop_entry *live_props;
};
static unsigned access_from_acl_item(struct acl_item *item)
{
unsigned access = 0;
if (item->mayReadFreeBusy)
access |= JACL_READFB;
if (item->mayRead)
access |= JACL_READITEMS|JACL_SETSEEN;
if (item->mayWrite)
access |= JACL_WRITE;
if (item->mayPost)
access |= JACL_SUBMIT;
if (item->mayAdmin)
access |= JACL_ADMIN|JACL_RENAME;
return access;
}
/* Create and send a sharing invite */
static void send_dav_invite(const char *userid, void *val, void *rock)
{
struct acl_change *change = (struct acl_change *) val;
struct invite_rock *irock = (struct invite_rock *) rock;
long old = access_from_acl_item(&change->old);
long new = access_from_acl_item(&change->new);
if (old != new) {
int access, r = 0;
if (!new) access = SHARE_NONE;
else if (change->new.mayWrite) access = SHARE_READWRITE;
else access = SHARE_READONLY;
if (!old || !new) {
/* Change subscription */
r = mboxlist_changesub(irock->mboxname, userid, httpd_authstate,
access != SHARE_NONE, 0, /*notify*/1);
}
if (!r) {
static const char *displayname_annot =
DAV_ANNOT_NS "<" XML_NS_DAV ">displayname";
struct buf buf = BUF_INITIALIZER;
r = annotatemore_lookupmask(irock->mboxname, displayname_annot,
irock->owner, &buf);
/* Fall back to last part of mailbox name */
if (r || !buf_len(&buf)) {
buf_setcstr(&buf, strrchr(irock->mboxname, '.') + 1);
}
r = dav_create_invite(&irock->notify, irock->ns, &irock->tgt,
irock->live_props, userid, access,
BAD_CAST buf_cstring(&buf));
buf_free(&buf);
}
if (!r) {
/* Create a resource name for the notifications -
We use a consistent naming scheme so that multiple
notifications of the same type for the same resource
are coalesced (overwritten) */
buf_reset(&irock->resource);
buf_printf(&irock->resource, "%x-%x-%x-%x.xml",
strhash(XML_NS_DAV),
strhash(SHARE_INVITE_NOTIFICATION),
strhash(irock->tgt.mbentry->name),
strhash(userid));
r = dav_send_notification(irock->notify->doc,
userid, buf_cstring(&irock->resource));
}
}
}
static void add_useracls(const char *userid, void *val, void *rock)
{
struct acl_change *change = val;
char **aclptr = rock;
unsigned access = access_from_acl_item(&change->new);
if (access)
cyrus_acl_set(aclptr, userid, ACL_MODE_SET, access, NULL, NULL);
}
struct shared_rock {
hash_table *user_access;
const char *owner;
const char *upload_mboxname;
};
static int sharedrights_cb(const mbentry_t *mbentry, void *vrock)
{
struct shared_rock *srock = (struct shared_rock *) vrock;
const char *userid;
char *nextid = NULL;
/* Skip any special use folders (#jmap, #calendars, #notifications, etc.) */
switch (mbtype_isa(mbentry->mbtype)) {
case MBTYPE_EMAIL:
case MBTYPE_CALENDAR:
case MBTYPE_ADDRESSBOOK:
break;
default:
return 0;
}
/* make sure we skip the upload folder itself */
if (!strcmp(mbentry->name, srock->upload_mboxname)) {
return 0;
}
/* parse the existing ACL and add to the sum of rights for each user */
for (userid = mbentry->acl; userid; userid = nextid) {
char *rightstr;
int access;
rightstr = strchr(userid, '\t');
if (!rightstr) break;
*rightstr++ = '\0';
nextid = strchr(rightstr, '\t');
if (!nextid) break;
*nextid++ = '\0';
cyrus_acl_strtomask(rightstr, &access);
/* remove any scheduling ACLs */
access &= ~DACL_SCHED;
if (strcmp(userid, srock->owner) && !is_system_user(userid)) {
/* limit ACL to JMAP sharing rights */
access &= (JACL_READITEMS | JACL_WRITE);
}
if (access) {
access |= (uintptr_t) hash_lookup(userid, srock->user_access);
hash_insert(userid, (void *)((uintptr_t)access), srock->user_access);
}
}
return 0;
}
static void add_shareacls(const char *userid, void *val, void *rock)
{
char **aclptr = rock;
int access = (uintptr_t) val;
if (access & JACL_WRITE)
cyrus_acl_set(aclptr, userid, ACL_MODE_SET, access, NULL, NULL);
else
cyrus_acl_remove(aclptr, userid, NULL, NULL);
}
static int set_upload_rights(const char *accountid)
{
/* XXX This is currently done by brute force.
We could be smarter by only doing a full scan
iff r/w is removed for a userid. */
struct mailbox *mbox = NULL;
int r = jmap_open_upload_collection(accountid, &mbox);
if (r) return r;
hash_table user_access = HASH_TABLE_INITIALIZER;
struct shared_rock srock = { &user_access, accountid, mailbox_name(mbox) };
/* build the sum of the shared rights for each each user */
construct_hash_table(&user_access, 64, 0);
mboxlist_usermboxtree(accountid, NULL, &sharedrights_cb, &srock, 0);
/* create the ACL for the upload folder */
char *newacl = xstrdup(""); /* start with empty ACL */
hash_enumerate_sorted(&user_access, add_shareacls, &newacl, cmpstringp_raw);
free_hash_table(&user_access, NULL);
/* ok, change the mailboxes database */
r = mboxlist_sync_setacls(mailbox_name(mbox), newacl, mailbox_modseq_dirty(mbox));
if (r) {
syslog(LOG_ERR, "mboxlist_sync_setacls(%s) failed: %s",
mailbox_name(mbox), error_message(r));
}
else {
/* ok, change the backup in cyrus.header */
mailbox_set_acl(mbox, newacl);
}
mailbox_close(&mbox);
free(newacl);
return 0;
}
HIDDEN int jmap_set_sharewith(struct mailbox *mbox,
json_t *shareWith, int overwrite)
{
hash_table user_access = HASH_TABLE_INITIALIZER;
int isdav = mbtypes_dav(mailbox_mbtype(mbox));
int iscalendar = mbtype_isa(mailbox_mbtype(mbox)) == MBTYPE_CALENDAR;
char *owner = mboxname_to_userid(mailbox_name(mbox));
char *acl = xstrdupnull(mailbox_acl(mbox));
struct acl_change *change;
const char *userid;
json_t *rights;
int r;
char *newacl = xstrdup(""); /* start with empty ACL */
if (json_is_null(shareWith)) overwrite = 1;
construct_hash_table(&user_access, 64, 0);
/* parse the existing ACL and calculate the types of shares */
char *nextid = NULL;
for (userid = acl; userid; userid = nextid) {
char *rightstr;
int access;
rightstr = strchr(userid, '\t');
if (!rightstr) break;
*rightstr++ = '\0';
nextid = strchr(rightstr, '\t');
if (!nextid) break;
*nextid++ = '\0';
/* Is this a shareable user? (not owner or admin) */
if (strcmp(userid, owner) && !is_system_user(userid)) {
int oldrights;
cyrus_acl_strtomask(rightstr, &oldrights);
/* Add regular user to our table */
change = xzmalloc(sizeof(struct acl_change));
if (oldrights & JACL_READFB)
change->old.mayReadFreeBusy = 1;
if (oldrights & JACL_READITEMS)
change->old.mayRead = 1;
if ((oldrights & JACL_WRITE) == JACL_WRITE)
change->old.mayWrite = 1;
if (oldrights & JACL_ADMIN)
change->old.mayAdmin = 1;
if (isdav) change->old.mayPost = change->old.mayWrite;
/* unless we're overwriting, we start with the existing state */
if (!overwrite) change->new = change->old;
hash_insert(userid, (void *) change, &user_access);
}
else {
/* Add owner or system user to new ACL */
cyrus_acl_strtomask(rightstr, &access);
r = cyrus_acl_set(&newacl, userid,
ACL_MODE_SET, access, NULL, NULL);
if (r) {
syslog(LOG_ERR, "cyrus_acl_set(%s, %s) failed: %s",
mailbox_name(mbox), userid, error_message(r));
goto done;
}
}
}
/* Patch the ACL from shareWith */
json_object_foreach(shareWith, userid, rights) {
const char *right;
json_t *val;
/* Validate user id and rights */
if (!(strlen(userid) && rights &&
(json_is_object(rights) || json_is_null(rights)))) {
continue;
}
/* skip system users and owner */
if (is_system_user(userid)) continue;
if (!strcmp(userid, owner)) continue;
change = hash_lookup(userid, &user_access);
if (!change) {
change = xzmalloc(sizeof(struct acl_change));
hash_insert(userid, (void *) change, &user_access);
}
if (json_is_null(rights)) {
/* remove user from ACL */
struct acl_item zero = {0,0,0,0,0};
if (change) change->new = zero;
}
else {
/* accumulate rights be granted and denied */
json_object_foreach(rights, right, val) {
unsigned set = json_boolean_value(val);
if (!strcmp(right, "mayAdmin"))
change->new.mayAdmin = set;
else if (!strcmp(right, "mayWrite"))
change->new.mayWrite = set;
else if (!strcmp(right, "mayRead"))
change->new.mayRead = set;
else if (iscalendar && !strcmp(right, "mayReadFreeBusy"))
change->new.mayReadFreeBusy = set;
}
if (isdav) change->new.mayPost = change->new.mayWrite;
}
}
/* add all the users back to the share ACL */
hash_enumerate_sorted(&user_access, add_useracls, &newacl, cmpstringp_raw);
/* ok, change the mailboxes database */
r = mboxlist_sync_setacls(mailbox_name(mbox), newacl, mailbox_modseq_dirty(mbox));
if (r) {
syslog(LOG_ERR, "mboxlist_sync_setacls(%s) failed: %s",
mailbox_name(mbox), error_message(r));
}
else {
/* ok, change the backup in cyrus.header */
mailbox_set_acl(mbox, newacl);
/* Set proper access rights on JMAP upload folder */
r = set_upload_rights(owner);
}
if (!r && isdav) {
/* Send sharing invites */
struct invite_rock irock;
struct meth_params *pparams;
mbname_t *mbname;
const char *errstr = NULL;
memset(&irock, 0, sizeof(struct invite_rock));
irock.owner = owner;
/* Find the DAV namespace for this mailbox */
if (iscalendar)
irock.tgt.namespace = &namespace_calendar;
else if (mbtype_isa(mailbox_mbtype(mbox)) == MBTYPE_ADDRESSBOOK)
irock.tgt.namespace = &namespace_addressbook;
else
irock.tgt.namespace = &namespace_drive;
/* Get "live" properties for the namespace */
pparams = irock.tgt.namespace->methods[METH_PROPFIND].params;
irock.live_props = pparams->propfind.lprops;
/* Create DAV URL for this collection */
mbname = mbname_from_intname(mailbox_name(mbox));
if (!mbname_domain(mbname)) mbname_set_domain(mbname, httpd_extradomain);
make_collection_url(&irock.resource, irock.tgt.namespace->prefix,
/*haszzzz*/0, mbname, mbname_userid(mbname));
/* Create a request target for this collection */
irock.tgt.flags = TGT_DAV_SHARED; // prevent old-style sharing redirect
r = pparams->parse_path(buf_cstring(&irock.resource), &irock.tgt, &errstr);
if (!r) {
/* Process each user */
irock.mboxname = mailbox_name(mbox);
hash_enumerate(&user_access, send_dav_invite, &irock);
}
/* Cleanup */
if (irock.notify) xmlFreeDoc(irock.notify->doc);
mboxlist_entry_free(&irock.tgt.mbentry);
free(irock.tgt.userid);
buf_free(&irock.resource);
mbname_free(&mbname);
}
done:
free_hash_table(&user_access, &free);
free(owner);
free(newacl);
free(acl);
return r;
}
HIDDEN void jmap_parse_sharewith_patch(json_t *arg, json_t **shareWith)
{
struct buf buf = BUF_INITIALIZER;
const char *field = NULL;
json_t *jval;
json_object_foreach(arg, field, jval) {
if (!strncmp(field, "shareWith/", 10)) {
const char *userid = field + 10;
const char *right = strchr(userid, '/');
if (!*shareWith) *shareWith = json_object();
if (right) {
/* individual right */
buf_setmap(&buf, userid, right - userid);
userid = buf_cstring(&buf);
json_t *rights = json_object_get(*shareWith, userid);
if (rights) {
/* add to existing ShareRights for this userid */
json_object_set(rights, right+1, jval);
}
else {
/* create new ShareRights for this userid */
json_object_set_new(*shareWith, userid,
json_pack("{s:o}", right+1, jval));
}
}
else {
/* complete ShareRights */
json_object_set(*shareWith, userid, jval);
}
}
}
buf_free(&buf);
}
HIDDEN int jmap_is_using(jmap_req_t *req, const char *capa)
{
return strarray_find(req->using_capabilities, capa, 0) >= 0;
}
/*
* Lookup 'name' in the mailbox list, ignoring reserved/deleted records
*/
HIDDEN int jmap_mboxlist_lookup(const char *name,
mbentry_t **entryptr, struct txn **tid)
{
mbentry_t *entry = NULL;
int r;
r = mboxlist_lookup_allow_all(name, &entry, tid);
if (r) return r;
/* Ignore "reserved" entries, like they aren't there */
if (entry->mbtype & MBTYPE_RESERVE) {
mboxlist_entry_free(&entry);
return IMAP_MAILBOX_RESERVED;
}
/* Ignore "deleted" entries, like they aren't there */
if (entry->mbtype & MBTYPE_DELETED) {
mboxlist_entry_free(&entry);
return IMAP_MAILBOX_NONEXISTENT;
}
if (entryptr) *entryptr = entry;
else mboxlist_entry_free(&entry);
return 0;
}
static const mbentry_t *_mbentry_by_uniqueid(jmap_req_t *req,
const char *id,
int scope)
{
mbentry_t *mbentry = NULL;
if (!req->mbentry_byid) {
req->mbentry_byid = xzmalloc(sizeof(struct hash_table));
construct_hash_table(req->mbentry_byid, 1024, 0);
}
else mbentry = hash_lookup(id, req->mbentry_byid);
if (!mbentry) {
int r = mboxlist_lookup_by_uniqueid(id, &mbentry, NULL);
if (r || !mbentry || (mbentry->mbtype & MBTYPE_DELETED) ||
/* make sure the user can "see" the mailbox */
!(jmap_myrights_mbentry(req, mbentry) & JACL_LOOKUP) ||
/* keep the lookup scoped to accountid */
(scope && !mboxname_userownsmailbox(req->accountid, mbentry->name))) {
mboxlist_entry_free(&mbentry);
return NULL;
}
hash_insert(mbentry->uniqueid, mbentry, req->mbentry_byid);
}
return mbentry;
}
EXPORTED const mbentry_t *jmap_mbentry_by_uniqueid(jmap_req_t *req,
const char *id)
{
return _mbentry_by_uniqueid(req, id, 1/*scope*/);
}
EXPORTED mbentry_t *jmap_mbentry_by_uniqueid_copy(jmap_req_t *req, const char *id)
{
const mbentry_t *mbentry = _mbentry_by_uniqueid(req, id, 1/*scope*/);
if (!mbentry) return NULL;
return mboxlist_entry_copy(mbentry);
}
static void _free_mbentry(void *rock)
{
mbentry_t *entry = rock;
mboxlist_entry_free(&entry);
}
EXPORTED void jmap_mbentry_cache_free(jmap_req_t *req)
{
if (req->mbentry_byid) {
free_hash_table(req->mbentry_byid, _free_mbentry);
free(req->mbentry_byid);
req->mbentry_byid = NULL;
}
}
HIDDEN void jmap_getblob_ctx_init(jmap_getblob_context_t *ctx,
const char *from_accountid, const char *blobid,
const char *accept_mime, unsigned decode)
{
memset(ctx, 0, sizeof(jmap_getblob_context_t));
ctx->from_accountid = from_accountid;
ctx->blobid = blobid;
ctx->accept_mime = accept_mime;
ctx->decode = decode;
}
HIDDEN void jmap_getblob_ctx_reset(jmap_getblob_context_t *ctx)
{
buf_reset(&ctx->blob);
buf_reset(&ctx->content_type);
buf_reset(&ctx->encoding);
ctx->errstr = NULL;
}
HIDDEN void jmap_getblob_ctx_fini(jmap_getblob_context_t *ctx)
{
buf_free(&ctx->blob);
buf_free(&ctx->content_type);
buf_free(&ctx->encoding);
}
EXPORTED mbentry_t *jmap_mbentry_from_dav(jmap_req_t *req, struct dav_data *dav)
{
mbentry_t *mbentry = NULL;
if (dav->mailbox_byname) {
if (jmap_mboxlist_lookup(dav->mailbox, &mbentry, NULL)) return NULL;
}
else {
/* XXX DAV DB scopes the lookup to a single account.
XXX And scoping the mailbox to req->accountid will break Foo/copy */
const mbentry_t *mbe =
_mbentry_by_uniqueid(req, dav->mailbox, 0/*scope*/);
if (!mbe) return NULL;
mbentry = mboxlist_entry_copy(mbe);
}
return mbentry;
}
|