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
|
/*
* $LynxId: LYCookie.c,v 1.147 2019/01/26 00:50:13 tom Exp $
*
* Lynx Cookie Support LYCookie.c
* ===================
*
* Author: AMK A.M. Kuchling (amk@magnet.com) 12/25/96
*
* Incorporated with mods by FM 01/16/97
*
* Based on:
* http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-state-mgmt-05.txt
*
* Updated for:
* http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-state-man-mec-02.txt
* - FM 1997-07-09
*
* Updated for:
* ftp://ds.internic.net/internet-drafts/draft-ietf-http-state-man-mec-03.txt
* - FM 1997-08-02
*
* Partially checked against:
* http://www.ietf.org/internet-drafts/draft-ietf-http-state-man-mec-10.txt
* - kw 1998-12-11
*
* Modified to follow RFC-6265 regarding leading dot of Domain, and
* matching of hostname vs domain (2011/06/10 -TD)
*
* Modified to address differences between RFC-6262 versus RFC-2109 and
* RFC-2965 by making the older behavior optional (2019/01/25 -TD)
*
* FM's TO DO: (roughly in order of decreasing priority)
* Persistent cookies are still experimental. Presently cookies
lose many pieces of information that distinguish
version 1 from version 0 cookies. There is no easy way around
that with the current cookie file format. Ports are currently
not stored persistently at all which is clearly wrong.
* We currently don't do anything special for unverifiable
transactions to third-party hosts.
* We currently don't use effective host names or check for
Domain=.local.
* Hex escaping isn't considered at all. Any semi-colons, commas,
or spaces actually in cookie names or values (i.e., not serving
as punctuation for the overall Set-Cookie value) should be hex
escaped if not quoted, but presumably the server is expecting
them to be hex escaped in our Cookie request header as well, so
in theory we need not unescape them. We'll see how this works
out in practice.
* The prompt should show more information about the cookie being
set in Novice mode.
* The truncation heuristic in HTConfirmCookie should probably be
smarter, smart enough to leave a really short name/value string
alone.
* We protect against denial-of-service attacks (see section 6.3.1 of the
draft) by limiting the number of cookies from a domain, limiting the
total number of cookies, and limiting the number of bytes from a
processed cookie, but we count on the normal garbage collections to
bring us back down under the limits, rather than actively removing
cookies and/or domains based on age or frequency of use.
* If a cookie has the secure flag set, we presently treat only SSL
connections as secure. This may need to be expanded for other
secure communication protocols that become standardized.
*/
#include <HTUtils.h>
#include <HTAccess.h>
#include <HTParse.h>
#include <HTAlert.h>
#include <LYCurses.h>
#include <LYUtils.h>
#include <LYCharUtils.h>
#include <LYClean.h>
#include <LYGlobalDefs.h>
#include <LYEdit.h>
#include <LYStrings.h>
#include <GridText.h>
#include <LYCookie.h>
#include <LYLeaks.h>
/* default for new domains, one of the invcheck_behaviour_t values: */
#define DEFAULT_INVCHECK_BV INVCHECK_QUERY
#define CTrace(p) CTRACE2(TRACE_COOKIES, p)
#define LeadingDot(s) ((s)[0] == '.')
#define SkipLeadingDot(s) (LeadingDot(s) ? ((s) + 1) : (s))
#define AssumeCookieVersion(p) \
if (USE_RFC_2965 && (p)->version < 1) { \
(p)->version = 1; \
}
/*
* The first level of the cookie list is a list indexed by the domain
* string; cookies with the same domain will be placed in the same
* list. Thus, finding the cookies that apply to a given URL is a
* two-level scan; first we check each domain to see if it applies,
* and if so, then we check the paths of all the cookies on that
* list. We keep a running total of cookies as we add or delete
* them
*/
static HTList *domain_list = NULL;
static HTList *cookie_list = NULL;
static int total_cookies = 0;
struct _cookie {
char *lynxID; /* Lynx cookie identifier */
char *name; /* Name of this cookie */
char *value; /* Value of this cookie */
int version; /* Cookie protocol version (=1) */
char *comment; /* Comment to show to user */
char *commentURL; /* URL for comment to show to user */
char *domain; /* Domain for which this cookie is valid */
char *ddomain; /* Domain without leading "." */
int port; /* Server port from which this cookie was given (usu. 80) */
char *PortList; /* List of ports for which cookie can be sent */
char *path; /* Path prefix for which this cookie is valid */
int pathlen; /* Length of the path */
int flags; /* Various flags */
time_t expires; /* The time when this cookie expires */
BOOL quoted; /* Was a value quoted in the Set-Cookie header? */
};
typedef struct _cookie cookie;
#define COOKIE_FLAG_SECURE 1 /* If set, cookie requires secure links */
#define COOKIE_FLAG_DISCARD 2 /* If set, expire at end of session */
#define COOKIE_FLAG_EXPIRES_SET 4 /* If set, an expiry date was set */
#define COOKIE_FLAG_DOMAIN_SET 8 /* If set, an non-default domain was set */
#define COOKIE_FLAG_PATH_SET 16 /* If set, an non-default path was set */
#define COOKIE_FLAG_FROM_FILE 32 /* If set, this cookie was persistent */
static void MemAllocCopy(char **dest,
const char *start,
const char *end)
{
char *temp;
if (!(start && end) || (end <= start)) {
HTSACopy(dest, "");
return;
}
temp = typecallocn(char, (unsigned)(end - start) + 1);
if (temp == NULL)
outofmem(__FILE__, "MemAllocCopy");
LYStrNCpy(temp, start, (end - start));
HTSACopy(dest, temp);
FREE(temp);
}
static cookie *newCookie(void)
{
cookie *p = typecalloc(cookie);
if (p == NULL)
outofmem(__FILE__, "newCookie");
HTSprintf0(&(p->lynxID), "%p", (void *) p);
p->port = 80;
return p;
}
static void freeCookie(cookie * co)
{
if (co) {
FREE(co->lynxID);
FREE(co->name);
FREE(co->value);
FREE(co->comment);
FREE(co->commentURL);
FREE(co->domain);
FREE(co->ddomain);
FREE(co->path);
FREE(co->PortList);
FREE(co);
}
}
static void freeCookies(domain_entry * de)
{
FREE(de->domain);
FREE(de->ddomain);
HTList_delete(de->cookie_list);
de->cookie_list = NULL;
}
#ifdef LY_FIND_LEAKS
static void LYCookieJar_free(void)
{
HTList *dl = domain_list;
domain_entry *de = NULL;
HTList *cl = NULL, *next = NULL;
cookie *co = NULL;
CTrace((tfp, "LYCookieJar_free\n"));
while (dl) {
if ((de = dl->object) != NULL) {
CTrace((tfp, "...LYCookieJar_free domain %s\n", NonNull(de->ddomain)));
cl = de->cookie_list;
while (cl) {
next = cl->next;
co = cl->object;
if (co) {
HTList_removeObject(de->cookie_list, co);
freeCookie(co);
}
cl = next;
}
freeCookies(de);
FREE(dl->object);
}
dl = dl->next;
}
cookie_list = NULL;
HTList_delete(domain_list);
domain_list = NULL;
}
#endif /* LY_FIND_LEAKS */
/*
* RFC 2109 -
* 4.2.2 Set-Cookie Syntax
* An explicitly specified domain must always start with a dot.
* 4.3.2 Rejecting Cookies
* ...rejects a cookie (shall not store its information) if any of the
* following is true:
* ...
* The value for the Domain attribute contains no embedded dots or does not
* start with a dot.
*
* RFC 2965 -
* 3.2.2 Set-Cookie2 Syntax
* Domain=value
* OPTIONAL. The value of the Domain attribute specifies the domain
* for which the cookie is valid. If an explicitly specified value
* does not start with a dot, the user agent supplies a leading dot.
*/
static BOOLEAN has_embedded_dot(const char *value)
{
BOOLEAN leading = NO;
BOOLEAN embedded = NO;
const char *p;
for (p = value; *p != '\0'; ++p) {
if (*p == '.') {
if (p == value) {
leading = YES;
} else if (p[1] != '\0') {
embedded = YES;
} else {
embedded = NO;
}
}
}
return (leading || USE_RFC_2965) && embedded;
}
/*
* RFC 6265 -
* 4.1.2.3. The Domain Attribute
* (Note that a leading %x2E ("."), if present, is ignored even though that
* character is not permitted, but a trailing %x2E ("."), if present, will
* cause the user agent to ignore the attribute.)
*/
static BOOLEAN has_trailing_dot(const char *value)
{
BOOLEAN result = NO;
const char *p;
for (p = value; *p != '\0'; ++p) {
if (*p == '.') {
if (p[1] == '\0') {
result = YES;
break;
}
}
}
return result;
}
/*
* Compare a string against a domain as specified in RFC-6265 Section 5.1.3
*/
static BOOLEAN domain_matches(const char *value,
const char *domain)
{
BOOLEAN result = NO;
if (isEmpty(value)) {
CTrace((tfp, "BUG: comparing empty value in domain_matches\n"));
} else if (isEmpty(domain)) {
CTrace((tfp, "BUG: comparing empty domain in domain_matches\n"));
} else {
if (!strcasecomp(value, domain)) {
result = YES;
} else {
int value_len = (int) strlen(value);
int suffix_len = (int) strlen(domain);
int offset = value_len - suffix_len;
if (offset > 1
&& value[offset - 1] == '.'
&& !strcasecomp(value + offset, domain)) {
result = YES;
}
}
}
return result;
}
/*
* Compare the current port with a port list as specified in Section 4.3 of:
* http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-state-man-mec-02.txt
* - FM
*/
static BOOLEAN port_matches(int port,
const char *list)
{
const char *number = list;
if (!(number && isdigit(UCH(*number))))
return (FALSE);
while (*number != '\0') {
if (atoi(number) == port) {
return (TRUE);
}
while (isdigit(UCH(*number))) {
number++;
}
while (*number != '\0' && !isdigit(UCH(*number))) {
number++;
}
}
return (FALSE);
}
/*
* Returns the length of the given path ignoring trailing slashes.
*/
static int ignore_trailing_slash(const char *a)
{
int len = (int) strlen(a);
while (len > 1 && a[len - 1] == '/')
--len;
return len;
}
/*
* Check if the path 'a' is a prefix of path 'b', ignoring trailing slashes
* in either, since they denote an empty component.
*/
static BOOL is_prefix(const char *a, const char *b)
{
int len_a = ignore_trailing_slash(a);
int len_b = ignore_trailing_slash(b);
if (len_a > len_b) {
return FALSE;
} else {
if (StrNCmp(a, b, (unsigned) len_a) != 0) {
return FALSE;
}
if (len_a < len_b && (len_a > 1 || a[0] != '/')) {
if (b[len_a] != '\0'
&& b[len_a] != '/') {
return FALSE;
}
}
}
return TRUE;
}
/*
* Find the domain-entry for the given name.
*/
static domain_entry *find_domain_entry(const char *name)
{
HTList *hl;
domain_entry *de = NULL;
const char *find;
if (name != 0
&& *(find = SkipLeadingDot(name)) != '\0') {
for (hl = domain_list; hl != NULL; hl = hl->next) {
de = (domain_entry *) hl->object;
if (de != NULL && de->domain != NULL && de->ddomain != NULL) {
CTrace((tfp,
"...test_domain_entry(%s) ->(%s) bv:%u, invcheck_bv:%u\n",
find,
NonNull(de->ddomain),
de->bv,
de->invcheck_bv));
if (!strcasecomp(find, de->ddomain)) {
break;
}
}
de = NULL;
}
}
CTrace((tfp, "find_domain_entry(%s) bv:%d, invcheck_bv:%d\n",
name,
de ? (int) de->bv : -1,
de ? (int) de->invcheck_bv : -1));
return de;
}
static void SetCookieDomain(cookie * co, const char *domain)
{
StrAllocCopy(co->ddomain, SkipLeadingDot(domain));
CTrace((tfp, "SetCookieDomain(%s)\n", co->ddomain));
}
/*
* Store a cookie somewhere in the domain list. - AK & FM
*/
static void store_cookie(cookie * co, const char *hostname,
const char *path)
{
HTList *hl, *next;
cookie *c2;
time_t now = time(NULL);
int pos;
const char *ptr;
domain_entry *de = NULL;
BOOL Replacement = FALSE;
int invprompt_reasons = 0; /* what is wrong with this cookie - kw */
#define FAILS_COND1 0x01
#define FAILS_COND4 0x02
if (co == NULL)
return;
/*
* Ensure that the domain list exists.
*/
if (domain_list == NULL) {
#ifdef LY_FIND_LEAKS
atexit(LYCookieJar_free);
#endif
domain_list = HTList_new();
total_cookies = 0;
}
/*
* Look through domain_list to see if the cookie's domain is already
* listed.
*/
cookie_list = NULL;
if ((de = find_domain_entry(co->domain)) != NULL)
cookie_list = de->cookie_list;
/*
* Apply sanity checks.
*
* RFC 2109 -
* Section 4.3.2, condition 1: The value for the Path attribute is not a
* prefix of the request-URI.
*
* If cookie checking for this domain is set to INVCHECK_LOOSE, then we
* want to bypass this check. The user should be queried if set to
* INVCHECK_QUERY.
*
* RFC 6265 -
* Section 4.1.2.4 describes Path, but omits any mention of the user agent
* rejecting a cookie because of Path. Instead, it deals only with the
* cases where a cookie returned by the user agent would be valid, based on
* Path. In section 8.6, RFC 6265 presents an example which would not have
* been valid with RFC 2109 to claim that the Path attribute is unreliable
* from the standpoint of the user agent.
*
* RFC 6265 does not go into any detail regarding its differences from the
* older RFCs 2109 / 2965. The relevant text covering all of these changes
* is just this (no case studies are cited):
* User agents MUST implement the more liberal processing rules defined in
* Section 5, in order to maximize interoperability with existing servers
* that do not conform to the well-behaved profile defined in Section 4.
*/
if (!USE_RFC_6265 && !is_prefix(co->path, path)) {
invcheck_behaviour_t invcheck_bv = (de ? de->invcheck_bv
: DEFAULT_INVCHECK_BV);
switch (invcheck_bv) {
case INVCHECK_LOOSE:
break; /* continue as if nothing were wrong */
case INVCHECK_QUERY:
/* will prompt later if we get that far */
invprompt_reasons |= FAILS_COND1;
break;
case INVCHECK_STRICT:
CTrace((tfp,
"store_cookie: Rejecting because '%s' is not a prefix of '%s'.\n",
co->path, path));
freeCookie(co);
return;
}
}
/*
* The next 4 conditions do NOT apply if the domain is still
* the default of request-host. (domains - case insensitive).
*/
if (strcasecomp(co->domain, hostname) != 0) {
/*
* The hostname does not contain a dot.
*/
if (StrChr(hostname, '.') == NULL) {
CTrace((tfp, "store_cookie: Rejecting because '%s' has no dot.\n",
hostname));
freeCookie(co);
return;
}
/*
* RFC 2109 -
* Section 4.3.2, condition 2: The value for the Domain attribute
* contains no embedded dots or does not start with a dot. (A dot is
* embedded if it's neither the first nor last character.) Note that we
* added a lead dot ourselves if a domain attribute value otherwise
* qualified. - FM
*
* RFC 6265 -
* If the first character of the attribute-value string is %x2E ("."):
*
* Let cookie-domain be the attribute-value without the leading %x2E
* (".") character.
*
* Otherwise:
*
* Let cookie-domain be the entire attribute-value.
*
* Convert the cookie-domain to lower case.
*/
SetCookieDomain(co, co->domain);
if (isEmpty(co->ddomain)) {
CTrace((tfp, "store_cookie: Rejecting domain '%s'.\n", co->ddomain));
freeCookie(co);
return;
}
if (!USE_RFC_6265) {
if (!has_embedded_dot(co->ddomain)) {
CTrace((tfp, "store_cookie: Rejecting domain '%s'.\n", co->ddomain));
freeCookie(co);
return;
}
} else {
if (has_trailing_dot(co->ddomain)) {
CTrace((tfp, "store_cookie: Rejecting domain '%s'.\n", co->ddomain));
freeCookie(co);
return;
}
}
/*
* RFC 2109 -
* Section 4.3.2, condition 3: The value for the request-host does not
* domain-match the Domain attribute.
*
* RFC 6265 -
* Section 4.1.2.3,
* The user agent will reject cookies unless the Domain attribute
* specifies a scope for the cookie that would include the origin
* server.
*/
if (!domain_matches(hostname, co->ddomain)) {
CTrace((tfp,
"store_cookie: Rejecting domain '%s' for host '%s'.\n",
co->ddomain, hostname));
freeCookie(co);
return;
}
/*
* RFC 2109 -
* Section 4.3.2, condition 4: The request-host is an HDN (not IP
* address) and has the form HD, where D is the value of the Domain
* attribute, and H is a string that contains one or more dots.
*
* If cookie checking for this domain is set to INVCHECK_LOOSE, then we
* want to bypass this check. The user should be queried if set to
* INVCHECK_QUERY.
*
* RFC 6265 -
* There is nothing comparable in RFC 6265, since this check appears to
* have reflected assumptions about how domain names were constructed
* when RFC 2109 was written. Section 5.1.3. (Domain Matching) is
* loosely related to these assumptions.
*/
if (!USE_RFC_6265) {
ptr = ((hostname + strlen(hostname)) - strlen(co->domain));
if (StrChr(hostname, '.') < ptr) {
invcheck_behaviour_t invcheck_bv = (de ? de->invcheck_bv
: DEFAULT_INVCHECK_BV);
switch (invcheck_bv) {
case INVCHECK_LOOSE:
break; /* continue as if nothing were wrong */
case INVCHECK_QUERY:
invprompt_reasons |= FAILS_COND4;
break; /* will prompt later if we get that far */
case INVCHECK_STRICT:
CTrace((tfp,
"store_cookie: Rejecting because '%s' is not a prefix of '%s'.\n",
co->path, path));
freeCookie(co);
return;
}
}
}
}
/*
* If we found reasons for issuing an invalid cookie confirmation prompt,
* do that now. Rejection by the user here is the last chance to
* completely ignore this cookie; after it passes this hurdle, it may at
* least supersede a previous cookie (even if it finally gets rejected). -
* kw
*/
if (invprompt_reasons) {
char *msg = 0;
if (invprompt_reasons & FAILS_COND4) {
HTSprintf0(&msg,
INVALID_COOKIE_DOMAIN_CONFIRMATION,
co->ddomain,
hostname);
if (!HTForcedPrompt(cookie_noprompt, msg, NO)) {
CTrace((tfp,
"store_cookie: Rejecting domain '%s' for host '%s'.\n",
co->ddomain,
hostname));
freeCookie(co);
FREE(msg);
return;
}
}
if (invprompt_reasons & FAILS_COND1) {
HTSprintf0(&msg,
INVALID_COOKIE_PATH_CONFIRMATION,
co->path, path);
if (!HTForcedPrompt(cookie_noprompt, msg, NO)) {
CTrace((tfp,
"store_cookie: Rejecting because '%s' is not a prefix of '%s'.\n",
co->path, path));
freeCookie(co);
FREE(msg);
return;
}
}
FREE(msg);
}
if (de == NULL) {
/*
* Domain not found; add a new entry for this domain.
*/
de = typecalloc(domain_entry);
if (de == NULL)
outofmem(__FILE__, "store_cookie");
de->bv = QUERY_USER;
de->invcheck_bv = DEFAULT_INVCHECK_BV; /* should this go here? */
cookie_list = de->cookie_list = HTList_new();
StrAllocCopy(de->domain, co->domain);
StrAllocCopy(de->ddomain, co->ddomain);
HTList_appendObject(domain_list, de);
}
/*
* Loop over the cookie list, deleting expired and matching cookies.
*/
hl = cookie_list;
pos = 0;
while (hl) {
c2 = (cookie *) hl->object;
next = hl->next;
/*
* Check if this cookie has expired.
*/
if ((c2 != NULL) &&
(c2->flags & COOKIE_FLAG_EXPIRES_SET) &&
c2->expires <= now) {
HTList_removeObject(cookie_list, c2);
freeCookie(c2);
c2 = NULL;
total_cookies--;
/*
* Check if this cookie matches the one we're inserting.
*/
} else if ((c2) &&
!strcasecomp(co->ddomain, c2->ddomain) &&
!strcmp(co->path, c2->path) &&
!strcmp(co->name, c2->name)) {
HTList_removeObject(cookie_list, c2);
freeCookie(c2);
c2 = NULL;
total_cookies--;
Replacement = TRUE;
} else if ((c2) && (c2->pathlen) >= (co->pathlen)) {
/*
* This comparison determines the (tentative) position of the new
* cookie in the list such that it comes before existing cookies
* with a less specific path, but after existing cookies of equal
* (or greater) path length. Thus it should normally preserve the
* order of new cookies with the same path as they are received,
* although this is not required.
*
* From RFC 2109 4.3.4:
*
* If multiple cookies satisfy the criteria above, they are ordered
* in the Cookie header such that those with more specific Path
* attributes precede those with less specific. Ordering with
* respect to other attributes (e.g., Domain) is unspecified.
*/
pos++;
}
hl = next;
}
/*
* Don't bother to add the cookie if it's already expired.
*/
if ((co->flags & COOKIE_FLAG_EXPIRES_SET) && co->expires <= now) {
freeCookie(co);
co = NULL;
/*
* Don't add the cookie if we're over the domain's limit. - FM
*/
} else if (HTList_count(cookie_list) > max_cookies_domain) {
CTrace((tfp,
"store_cookie: Domain's cookie limit exceeded! Rejecting cookie.\n"));
freeCookie(co);
co = NULL;
/*
* Don't add the cookie if we're over the total cookie limit. - FM
*/
} else if (total_cookies > max_cookies_global) {
CTrace((tfp,
"store_cookie: Total cookie limit exceeded! Rejecting cookie.\n"));
freeCookie(co);
co = NULL;
/*
* Don't add the cookie if the value is NULL. - BJP
*/
/*
* Presence of value is now needed (indicated normally by '='),
* but it can now be an empty string.
* - kw 1999-06-24
*/
} else if (co->value == NULL) { /* should not happen - kw */
CTrace((tfp, "store_cookie: Value is NULL! Not storing cookie.\n"));
freeCookie(co);
co = NULL;
/*
* If it's a replacement for a cookie that had not expired, and never
* allow has not been set, add it again without confirmation. - FM
*/
} else if ((Replacement == TRUE) && de->bv != REJECT_ALWAYS) {
HTList_insertObjectAt(cookie_list, co, pos);
total_cookies++;
/*
* Get confirmation if we need it, and add cookie if confirmed or
* 'allow' is set to always. - FM
*
* Cookies read from file are accepted without confirmation prompting.
* (Prompting may actually not be possible if LYLoadCookies is called
* before curses is setup.) Maybe this should instead depend on
* LYSetCookies and/or LYCookieAcceptDomains and/or
* LYCookieRejectDomains and/or LYAcceptAllCookies and/or some other
* settings. -kw
*/
} else if ((co->flags & COOKIE_FLAG_FROM_FILE)
|| HTConfirmCookie(de, hostname, co->name, co->value)) {
/*
* Insert the new cookie so that more specific paths (longer
* pathlen) come first in the list. - kw
*/
HTList_insertObjectAt(cookie_list, co, pos);
total_cookies++;
} else {
freeCookie(co);
co = NULL;
}
}
/*
* Scan a domain's cookie_list for any cookies we should
* include in a Cookie: request header. - AK & FM
*/
static char *scan_cookie_sublist(char *hostname,
char *path,
int port,
HTList *sublist,
char *header,
int secure)
{
HTList *hl, *next;
cookie *co;
time_t now = time(NULL);
char crlftab[8];
sprintf(crlftab, "%c%c%c", CR, LF, '\t');
for (hl = sublist; hl != NULL; hl = next) {
next = hl->next;
co = (cookie *) hl->object;
if (co == NULL) {
continue;
}
/* speed-up host_matches() and limit trace output */
if (LYstrstr(hostname, co->ddomain) != NULL) {
CTrace((tfp, "Checking cookie %p %s=%s\n",
(void *) hl,
(co->name ? co->name : "(no name)"),
(co->value ? co->value : "(no value)")));
CTrace((tfp, "\t%s %s %d %s %s %d%s\n",
hostname,
(co->ddomain ? co->ddomain : "(no domain)"),
domain_matches(hostname, co->ddomain),
path, co->path,
(co->pathlen > 0)
? !is_prefix(co->path, path)
: 0,
(co->flags & COOKIE_FLAG_SECURE)
? " secure"
: ""));
}
/*
* Check if this cookie has expired, and if so, delete it.
*/
if ((co->flags & COOKIE_FLAG_EXPIRES_SET) &&
co->expires <= now) {
next = hl->next;
HTList_removeObject(sublist, co);
freeCookie(co);
total_cookies--;
if (next)
continue;
break;
}
/*
* Check if we have a unexpired match, and handle if we do.
*/
if (co->domain != 0 &&
co->name != 0 &&
domain_matches(hostname, co->ddomain) &&
(co->pathlen == 0 || is_prefix(co->path, path))) {
/*
* Skip if the secure flag is set and we don't have a secure
* connection. HTTP.c presently treats only SSL connections as
* secure. - FM
*/
if ((co->flags & COOKIE_FLAG_SECURE) && secure == FALSE) {
continue;
}
/*
* Skip if we have a port list and the current port is not listed.
* - FM
*/
if (USE_RFC_2965
&& co->PortList
&& !port_matches(port, co->PortList)) {
continue;
}
/*
* Start or append to the request header.
*/
if (header == NULL) {
if (co->version > 0) {
/*
* For Version 1 (or greater) cookies, the version number
* goes before the first cookie.
*/
HTSprintf0(&header, "$Version=\"%d\"; ", co->version);
}
} else {
/*
* There's already cookie data there, so add a separator
* (always use a semi-colon for "backward compatibility"). -
* FM
*/
StrAllocCat(header, "; ");
/*
* Check if we should fold the header. - FM
*/
/*
* Section 2.2 of RFC1945 says:
*
* HTTP/1.0 headers may be folded onto multiple lines if each
* continuation line begins with a space or horizontal tab.
* All linear whitespace, including folding, has the same
* semantics as SP. [...] However, folding of header lines is
* not expected by some applications, and should not be
* generated by HTTP/1.0 applications.
*
* This code was causing problems. Let's not use it. -BJP
*/
/* if (len > 800) { */
/* StrAllocCat(header, crlftab); */
/* } */
}
/*
* Include the cookie name=value pair.
*/
StrAllocCat(header, co->name);
StrAllocCat(header, "=");
if (co->quoted) {
StrAllocCat(header, "\"");
}
StrAllocCat(header, co->value);
if (co->quoted) {
StrAllocCat(header, "\"");
}
/*
* For Version 1 (or greater) cookies, add $PATH, $PORT and/or
* $DOMAIN attributes for the cookie if they were specified via a
* server reply header. - FM
*/
if (co->version > 0) {
if (co->path && (co->flags & COOKIE_FLAG_PATH_SET)) {
HTSprintf(&header, "; $Path=\"%s\"", co->path);
}
if (co->PortList && isdigit(UCH(*co->PortList))) {
HTSprintf(&header, "; $Port=\"%s\"", co->PortList);
}
if (co->domain && (co->flags & COOKIE_FLAG_DOMAIN_SET)) {
HTSprintf(&header, "; $Domain=\"%s\"", co->domain);
}
}
}
}
return (header);
}
/*
* Presence of value is needed (indicated normally by '=') to start a cookie,
* but it can be an empty string. - kw 1999-06-24
*/
static char *alloc_attr_value(const char *value_start,
const char *value_end)
{
char *value = NULL;
if (value_start && value_end >= value_start) {
int value_len = (int) (value_end - value_start);
if (value_len > max_cookies_buffer) {
value_len = max_cookies_buffer;
}
value = typecallocn(char, (unsigned) value_len + 1);
if (value == NULL)
outofmem(__FILE__, "LYProcessSetCookies");
LYStrNCpy(value, value_start, value_len);
}
return value;
}
#define FLAGS_INVALID_PORT 1
#define FLAGS_KNOWN_ATTR 2
#define FLAGS_MAXAGE_ATTR 4
#define is_attr(s, len) attr_len == len && !strncasecomp(attr_start, s, len)
/*
* Attribute-names are matched ignoring case.
*
* Attribute RFC-2109 (1997) RFC-2965 (2000) RFC-6265 (2011)
* ---------------------------------------------------------------
* comment yes yes -
* commentURL - yes -
* discard - yes -
* domain yes yes yes
* expires yes yes yes
* httponly - - yes
* max-age yes yes yes
* path yes yes yes
* port - yes -
* secure yes yes yes
* version yes yes -
* ---------------------------------------------------------------
*/
static unsigned parse_attribute(unsigned flags,
cookie * cur_cookie,
int *cookie_len,
const char *attr_start,
int attr_len,
char *value,
const char *address,
char *hostname,
int port)
{
BOOLEAN known_attr = NO;
int url_type;
CTrace((tfp, "parse_attribute %.*s\n", attr_len, attr_start));
flags &= (unsigned) (~FLAGS_KNOWN_ATTR);
if (is_attr("secure", 6)) {
if (value == NULL) {
known_attr = YES;
if (cur_cookie != NULL) {
cur_cookie->flags |= COOKIE_FLAG_SECURE;
}
} else {
/*
* If secure has a value, assume someone misused it as cookie name.
* - FM
*/
known_attr = NO;
}
} else if (USE_RFC_6265 && is_attr("httponly", 8)) {
if (value == NULL) {
known_attr = YES; /* known, but irrelevant to lynx */
} else {
known_attr = NO;
}
} else if (USE_RFC_2965 && is_attr("discard", 7)) {
if (value == NULL) {
known_attr = YES;
if (cur_cookie != NULL) {
cur_cookie->flags |= COOKIE_FLAG_DISCARD;
}
} else {
/*
* If discard has a value, assume someone used it as a cookie name.
* - FM
*/
known_attr = NO;
}
} else if ((USE_RFC_2109 || USE_RFC_2965) && is_attr("comment", 7)) {
known_attr = YES;
if (cur_cookie != NULL && value &&
/*
* Don't process a repeat comment. - FM
*/
cur_cookie->comment == NULL) {
StrAllocCopy(cur_cookie->comment, value);
*cookie_len += (int) strlen(cur_cookie->comment);
}
} else if (USE_RFC_2965 && is_attr("commentURL", 10)) {
known_attr = YES;
if (cur_cookie != NULL && value &&
/*
* Don't process a repeat commentURL. - FM
*/
cur_cookie->commentURL == NULL) {
/*
* We should get only absolute URLs as values, but will resolve
* versus the request's URL just in case. - FM
*/
cur_cookie->commentURL = HTParse(value,
address,
PARSE_ALL);
/*
* Accept only URLs for http or https servers. - FM
*/
if ((url_type = is_url(cur_cookie->commentURL)) &&
(url_type == HTTP_URL_TYPE ||
url_type == HTTPS_URL_TYPE)) {
*cookie_len += (int) strlen(cur_cookie->commentURL);
} else {
CTrace((tfp,
"LYProcessSetCookies: Rejecting commentURL value '%s'\n",
cur_cookie->commentURL));
FREE(cur_cookie->commentURL);
}
}
} else if (is_attr("domain", 6)) {
known_attr = YES;
if (cur_cookie != NULL && value &&
/*
* Don't process a repeat domain. - FM
*/
!(cur_cookie->flags & COOKIE_FLAG_DOMAIN_SET)) {
*cookie_len -= (int) strlen(cur_cookie->domain);
/*
* If the value does not have a lead dot, but does have an embedded
* dot, and is not an exact match to the hostname, nor is a numeric
* IP address, add a lead dot. Otherwise, use the value as is. -
* FM (domains - case insensitive).
*/
if (value[0] != '.' && value[0] != '\0' &&
value[1] != '\0' && strcasecomp(value, hostname)) {
char *ptr = StrChr(value, '.');
if (ptr != NULL && ptr[1] != '\0') {
ptr = value;
while (*ptr == '.' ||
isdigit(UCH(*ptr)))
ptr++;
if (*ptr != '\0') {
CTrace((tfp,
"LYProcessSetCookies: Adding lead dot for domain value '%s'\n",
value));
HTSprintf0(&(cur_cookie->domain), ".%s", value);
} else {
StrAllocCopy(cur_cookie->domain, value);
}
} else {
StrAllocCopy(cur_cookie->domain, value);
}
} else {
StrAllocCopy(cur_cookie->domain, value);
}
*cookie_len += (int) strlen(cur_cookie->domain);
cur_cookie->flags |= COOKIE_FLAG_DOMAIN_SET;
SetCookieDomain(cur_cookie, cur_cookie->domain);
}
} else if (is_attr("path", 4)) {
known_attr = YES;
if (cur_cookie != NULL && value &&
/*
* Don't process a repeat path. - FM
*/
!(cur_cookie->flags & COOKIE_FLAG_PATH_SET)) {
*cookie_len -= (int) strlen(cur_cookie->path);
StrAllocCopy(cur_cookie->path, value);
*cookie_len += (cur_cookie->pathlen = (int) strlen(cur_cookie->path));
cur_cookie->flags |= COOKIE_FLAG_PATH_SET;
CTrace((tfp, " ->%.*s\n", cur_cookie->pathlen, cur_cookie->path));
}
} else if (USE_RFC_2965 && is_attr("port", 4)) {
if (cur_cookie != NULL && value &&
/*
* Don't process a repeat port. - FM
*/
cur_cookie->PortList == NULL) {
char *cp = value;
while ((*cp != '\0') &&
(isdigit(UCH(*cp)) ||
*cp == ',' || *cp == ' ')) {
cp++;
}
if (*cp == '\0') {
if (!port_matches(port, value)) {
flags |= FLAGS_INVALID_PORT;
} else {
StrAllocCopy(cur_cookie->PortList, value);
*cookie_len += (int) strlen(cur_cookie->PortList);
CTrace((tfp, " ->%s\n", cur_cookie->PortList));
}
known_attr = YES;
} else {
known_attr = NO;
}
} else if (cur_cookie != NULL) {
/*
* Don't process a repeat port. - FM
*/
if (cur_cookie->PortList == NULL) {
HTSprintf0(&(cur_cookie->PortList), "%d", port);
*cookie_len += (int) strlen(cur_cookie->PortList);
}
known_attr = YES;
}
} else if ((USE_RFC_2109 || USE_RFC_2965) && is_attr("version", 7)) {
known_attr = YES;
if (cur_cookie != NULL && value &&
/*
* Don't process a repeat version. - FM
*/
cur_cookie->version < 1) {
int temp = (int) strtol(value, NULL, 10);
if (errno != -ERANGE) {
cur_cookie->version = temp;
}
}
} else if (is_attr("max-age", 7)) {
known_attr = YES;
/*
* Don't process a repeat max-age. - FM
*/
if (cur_cookie != NULL && value &&
!(flags & FLAGS_MAXAGE_ATTR)) {
long temp = strtol(value, NULL, 10);
cur_cookie->flags |= COOKIE_FLAG_EXPIRES_SET;
if (errno == -ERANGE) {
cur_cookie->expires = (time_t) 0;
} else {
cur_cookie->expires = (time(NULL) + temp);
CTrace((tfp, "LYSetCookie: expires %" PRI_time_t ", %s",
CAST_time_t (cur_cookie->expires),
ctime(&cur_cookie->expires)));
}
flags |= FLAGS_MAXAGE_ATTR;
}
} else if (is_attr("expires", 7)) {
/*
* Convert an 'expires' attribute value if we haven't received a
* 'max-age'. Note that 'expires' should not be used in Version 1
* cookies, but it might be used for "backward compatibility", and, in
* turn, ill-informed people surely would start using it instead of,
* rather than in addition to, 'max-age'. - FM
*/
known_attr = YES;
if ((cur_cookie != NULL && !(flags & FLAGS_MAXAGE_ATTR)) &&
!(cur_cookie->flags & COOKIE_FLAG_EXPIRES_SET)) {
if (value) {
cur_cookie->flags |= COOKIE_FLAG_EXPIRES_SET;
cur_cookie->expires = LYmktime(value, FALSE);
if (cur_cookie->expires > 0) {
CTrace((tfp, "LYSetCookie: expires %" PRI_time_t ", %s",
CAST_time_t (cur_cookie->expires),
ctime(&cur_cookie->expires)));
}
}
}
}
if (known_attr)
flags |= FLAGS_KNOWN_ATTR;
return flags;
}
/*
* Process potentially concatenated Set-Cookie2 and/or Set-Cookie
* headers. - FM
*/
static void LYProcessSetCookies(const char *SetCookie,
const char *SetCookie2,
const char *address,
char *hostname,
char *path,
int port)
{
const char *p, *attr_start, *attr_end, *value_start, *value_end;
HTList *CombinedCookies = NULL, *cl = NULL;
cookie *cur_cookie = NULL, *co = NULL;
int cookie_len = 0;
int NumCookies = 0;
BOOL Quoted = FALSE;
unsigned parse_flags = 0;
if (isEmpty(SetCookie) &&
isEmpty(SetCookie2)) {
/*
* Yuk! Garbage in, so nothing out. - FM
*/
return;
}
/*
* If we have both Set-Cookie and Set-Cookie2 headers. process the
* Set-Cookie2 header. Otherwise, process whichever of the two headers we
* do have. Note that if more than one instance of a valued attribute for
* the same cookie is encountered, the value for the first instance is
* retained.
*/
CombinedCookies = HTList_new();
/*
* Process the Set-Cookie2 header, if present and not zero-length, adding
* each cookie to the CombinedCookies list. - FM
*/
p = NonNull(SetCookie2);
if (SetCookie && *p) {
CTrace((tfp, "LYProcessSetCookies: Using Set-Cookie2 header.\n"));
}
while (NumCookies <= max_cookies_domain && *p) {
value_start = value_end = NULL;
p = LYSkipCBlanks(p);
/*
* Get the attribute name.
*/
attr_start = p;
while (*p != '\0' && !isspace(UCH(*p)) &&
*p != '=' && *p != ';' && *p != ',')
p++;
attr_end = p;
p = LYSkipCBlanks(p);
/*
* Check for an '=' delimiter, or an 'expires' name followed by white,
* since Netscape's bogus parser doesn't require an '=' delimiter, and
* 'expires' attributes are being encountered without them. These
* shouldn't be in a Set-Cookie2 header, but we'll assume it's an
* expires attribute rather a cookie with that name, since the
* attribute mistake rather than name mistake seems more likely to be
* made by providers. - FM
*/
if (*p == '=' ||
!strncasecomp(attr_start, "Expires", 7)) {
/*
* Get the value string.
*/
if (*p == '=') {
p++;
}
p = LYSkipCBlanks(p);
/*
* Hack alert! We must handle Netscape-style cookies with
* "Expires=Mon, 01-Jan-96 13:45:35 GMT" or
* "Expires=Mon, 1 Jan 1996 13:45:35 GMT".
* No quotes, but there are spaces. Argh... Anyway, we know it
* will have at least 3 space separators within it, and two dashes
* or two more spaces, so this code looks for a space after the 5th
* space separator or dash to mark the end of the value. - FM
*/
if ((attr_end - attr_start) == 7 &&
!strncasecomp(attr_start, "Expires", 7)) {
int spaces = 6;
value_start = p;
if (isdigit(UCH(*p))) {
/*
* No alphabetic day field. - FM
*/
spaces--;
} else {
/*
* Skip the alphabetic day field. - FM
*/
while (*p != '\0' && isalpha(UCH(*p))) {
p++;
}
while (*p == ',' || isspace(UCH(*p))) {
p++;
}
spaces--;
}
while (*p != '\0' && *p != ';' && *p != ',' && spaces) {
p++;
if (isspace(UCH(*p))) {
while (isspace(UCH(*(p + 1))))
p++;
spaces--;
} else if (*p == '-') {
spaces--;
}
}
value_end = p;
/*
* Hack Alert! The port attribute can take a comma separated
* list of numbers as a value, and such values should be
* quoted, but if not, make sure we don't treat a number in the
* list as the start of a new cookie. - FM
*/
} else if ((attr_end - attr_start) == 4 &&
!strncasecomp(attr_start, "port", 4) &&
isdigit(UCH(*p))) {
/*
* The value starts as an unquoted number.
*/
const char *cp, *cp1;
value_start = p;
while (1) {
while (isdigit(UCH(*p)))
p++;
value_end = p;
p = LYSkipCBlanks(p);
if (*p == '\0' || *p == ';')
break;
if (*p == ',') {
cp = LYSkipCBlanks(p + 1);
if (*cp != '\0' && isdigit(UCH(*cp))) {
cp1 = cp;
while (isdigit(UCH(*cp1)))
cp1++;
cp1 = LYSkipCBlanks(cp1);
if (*cp1 == '\0' || *cp1 == ',' || *cp1 == ';') {
p = cp;
continue;
}
}
}
while (*p != '\0' && *p != ';' && *p != ',')
p++;
value_end = p;
/*
* Trim trailing spaces.
*/
if ((value_end > value_start) &&
isspace(UCH(*(value_end - 1)))) {
value_end--;
while ((value_end > (value_start + 1)) &&
isspace(UCH(*value_end)) &&
isspace(UCH(*(value_end - 1)))) {
value_end--;
}
}
break;
}
} else if (*p == '"') {
BOOLEAN escaped = FALSE;
/*
* It looks like quoted string.
*/
p++;
value_start = p;
while (*p != '\0' && (*p != '"' || escaped)) {
escaped = (BOOL) (!escaped && *p == '\\');
p++;
}
if (p != value_start && *p == '"' && !escaped) {
value_end = p;
p++;
Quoted = TRUE;
} else {
value_start--;
value_end = p;
if (*p)
p++;
Quoted = FALSE;
}
} else {
/*
* Otherwise, it's an unquoted string.
*/
value_start = p;
while (*p != '\0' && *p != ';' && *p != ',')
p++;
value_end = p;
/*
* Trim trailing spaces.
*/
if ((value_end > value_start) &&
isspace(UCH(*(value_end - 1)))) {
value_end--;
while ((value_end > (value_start + 1)) &&
isspace(UCH(*value_end)) &&
isspace(UCH(*(value_end - 1)))) {
value_end--;
}
}
}
}
/*
* Check for a separator character, and skip it.
*/
if (*p == ';' || *p == ',')
p++;
/*
* Now, we can handle this attribute/value pair.
*/
if (attr_end > attr_start) {
char *value = alloc_attr_value(value_start, value_end);
parse_flags = parse_attribute(parse_flags,
cur_cookie,
&cookie_len,
attr_start,
(int) (attr_end - attr_start),
value,
address,
hostname,
port);
/*
* Presence of value is needed (indicated normally by '='),
* but it can be an empty string. - kw 1999-06-24
*/
if (!(parse_flags & FLAGS_KNOWN_ATTR)
&& value
&& value_end >= value_start) {
/*
* If we've started a cookie, and it's not too big, save it in
* the CombinedCookies list. - FM
*/
if (cookie_len <= max_cookies_buffer
&& cur_cookie != NULL
&& !(parse_flags & FLAGS_INVALID_PORT)) {
AssumeCookieVersion(cur_cookie);
HTList_appendObject(CombinedCookies, cur_cookie);
} else if (cur_cookie != NULL) {
CTrace((tfp,
"LYProcessSetCookies: Rejecting Set-Cookie2: %s=%s\n",
(cur_cookie->name ?
cur_cookie->name : "[no name]"),
(cur_cookie->value ?
cur_cookie->value : "[no value]")));
CTrace((tfp,
(parse_flags & FLAGS_INVALID_PORT) ?
" due to excessive length!\n"
: " due to invalid port!\n"));
if (parse_flags & FLAGS_INVALID_PORT) {
NumCookies--;
}
freeCookie(cur_cookie);
cur_cookie = NULL;
}
/*
* Start a new cookie. - FM
*/
cur_cookie = newCookie();
cookie_len = 0;
NumCookies++;
MemAllocCopy(&(cur_cookie->name), attr_start, attr_end);
cookie_len += (int) strlen(cur_cookie->name);
MemAllocCopy(&(cur_cookie->value), value_start, value_end);
cookie_len += (int) strlen(cur_cookie->value);
StrAllocCopy(cur_cookie->domain, hostname);
cookie_len += (int) strlen(hostname);
StrAllocCopy(cur_cookie->path, path);
cookie_len += (cur_cookie->pathlen = (int) strlen(cur_cookie->path));
cur_cookie->port = port;
parse_flags = 0;
cur_cookie->quoted = TRUE;
SetCookieDomain(cur_cookie, hostname);
}
FREE(value);
}
}
/*
* Add any final SetCookie2 cookie to the CombinedCookie list if we are
* within the length limit. - FM
*/
if (NumCookies <= max_cookies_domain
&& cookie_len <= max_cookies_buffer
&& cur_cookie != NULL && !(parse_flags & FLAGS_INVALID_PORT)) {
AssumeCookieVersion(cur_cookie);
HTList_appendObject(CombinedCookies, cur_cookie);
} else if (cur_cookie != NULL && !(parse_flags & FLAGS_INVALID_PORT)) {
CTrace((tfp, "LYProcessSetCookies: Rejecting Set-Cookie2: %s=%s\n",
(cur_cookie->name ? cur_cookie->name : "[no name]"),
(cur_cookie->value ? cur_cookie->value : "[no value]")));
CTrace((tfp, " due to excessive %s%s%s\n",
(cookie_len > max_cookies_buffer ? "length" : ""),
(cookie_len > max_cookies_buffer &&
NumCookies > max_cookies_domain
? " and "
: ""),
(NumCookies > max_cookies_domain ? "number!\n" : "!\n")));
freeCookie(cur_cookie);
cur_cookie = NULL;
} else if (cur_cookie != NULL) { /* invalidport */
CTrace((tfp, "LYProcessSetCookies: Rejecting Set-Cookie2: %s=%s\n",
(cur_cookie->name ? cur_cookie->name : "[no name]"),
(cur_cookie->value ? cur_cookie->value : "[no value]")));
CTrace((tfp, " due to invalid port!\n"));
NumCookies--;
freeCookie(cur_cookie);
cur_cookie = NULL;
}
/*
* Process the Set-Cookie header, if no non-zero-length Set-Cookie2 header
* was present. - FM
*/
cookie_len = 0;
NumCookies = 0;
cur_cookie = NULL;
p = ((SetCookie && isEmpty(SetCookie2)) ? SetCookie : "");
if (SetCookie2 && *p) {
CTrace((tfp, "LYProcessSetCookies: Using Set-Cookie header.\n"));
}
while (NumCookies <= max_cookies_domain && *p) {
value_start = value_end = NULL;
p = LYSkipCBlanks(p);
/*
* Get the attribute name.
*/
attr_start = p;
while (*p != '\0' && !isspace(UCH(*p)) &&
*p != '=' && *p != ';' && *p != ',')
p++;
attr_end = p;
p = LYSkipCBlanks(p);
/*
* Check for an '=' delimiter, or an 'expires' name followed by white,
* since Netscape's bogus parser doesn't require an '=' delimiter, and
* 'expires' attributes are being encountered without them. - FM
*/
if (*p == '=' ||
!strncasecomp(attr_start, "Expires", 7)) {
/*
* Get the value string.
*/
if (*p == '=') {
p++;
}
p = LYSkipCBlanks(p);
/*
* Hack alert! We must handle Netscape-style cookies with
* "Expires=Mon, 01-Jan-96 13:45:35 GMT" or
* "Expires=Mon, 1 Jan 1996 13:45:35 GMT".
* No quotes, but there are spaces. Argh... Anyway, we know it
* will have at least 3 space separators within it, and two dashes
* or two more spaces, so this code looks for a space after the 5th
* space separator or dash to mark the end of the value. - FM
*/
if ((attr_end - attr_start) == 7 &&
!strncasecomp(attr_start, "Expires", 7)) {
int spaces = 6;
value_start = p;
if (isdigit(UCH(*p))) {
/*
* No alphabetic day field. - FM
*/
spaces--;
} else {
/*
* Skip the alphabetic day field. - FM
*/
while (*p != '\0' && isalpha(UCH(*p))) {
p++;
}
while (*p == ',' || isspace(UCH(*p))) {
p++;
}
spaces--;
}
while (*p != '\0' && *p != ';' && *p != ',' && spaces) {
p++;
if (isspace(UCH(*p))) {
while (isspace(UCH(*(p + 1))))
p++;
spaces--;
} else if (*p == '-') {
spaces--;
}
}
value_end = p;
/*
* Hack Alert! The port attribute can take a comma separated
* list of numbers as a value, and such values should be
* quoted, but if not, make sure we don't treat a number in the
* list as the start of a new cookie. - FM
*/
} else if ((attr_end - attr_start) == 4 &&
!strncasecomp(attr_start, "port", 4) &&
isdigit(UCH(*p))) {
/*
* The value starts as an unquoted number.
*/
const char *cp, *cp1;
value_start = p;
while (1) {
while (isdigit(UCH(*p)))
p++;
value_end = p;
p = LYSkipCBlanks(p);
if (*p == '\0' || *p == ';')
break;
if (*p == ',') {
cp = LYSkipCBlanks(p + 1);
if (*cp != '\0' && isdigit(UCH(*cp))) {
cp1 = cp;
while (isdigit(UCH(*cp1)))
cp1++;
cp1 = LYSkipCBlanks(cp1);
if (*cp1 == '\0' || *cp1 == ',' || *cp1 == ';') {
p = cp;
continue;
}
}
}
while (*p != '\0' && *p != ';' && *p != ',')
p++;
value_end = p;
/*
* Trim trailing spaces.
*/
if ((value_end > value_start) &&
isspace(UCH(*(value_end - 1)))) {
value_end--;
while ((value_end > (value_start + 1)) &&
isspace(UCH(*value_end)) &&
isspace(UCH(*(value_end - 1)))) {
value_end--;
}
}
break;
}
} else if (*p == '"') {
BOOLEAN escaped = FALSE;
/*
* It looks like quoted string.
*/
p++;
value_start = p;
while (*p != '\0' && (*p != '"' || escaped)) {
escaped = (BOOL) (!escaped && *p == '\\');
p++;
}
if (p != value_start && *p == '"' && !escaped) {
value_end = p;
p++;
Quoted = TRUE;
} else {
value_start--;
value_end = p;
if (*p)
p++;
Quoted = FALSE;
}
} else {
/*
* Otherwise, it's an unquoted string.
*/
value_start = p;
while (*p != '\0' && *p != ';' && *p != ',')
p++;
value_end = p;
/*
* Trim trailing spaces.
*/
if ((value_end > value_start) &&
isspace(UCH(*(value_end - 1)))) {
value_end--;
while ((value_end > (value_start + 1)) &&
isspace(UCH(*value_end)) &&
isspace(UCH(*(value_end - 1)))) {
value_end--;
}
}
}
}
/*
* Check for a separator character, and skip it.
*/
if (*p == ';' || *p == ',')
p++;
/*
* Now, we can handle this attribute/value pair.
*/
if (attr_end > attr_start) {
char *value = alloc_attr_value(value_start, value_end);
parse_flags = parse_attribute(parse_flags,
cur_cookie,
&cookie_len,
attr_start,
(int) (attr_end - attr_start),
value,
address,
hostname,
port);
/*
* Presence of value is needed (indicated normally by '='),
* but it can be an empty string. - kw 1999-06-24
*/
if (!(parse_flags & FLAGS_KNOWN_ATTR)
&& value
&& value_end >= value_start) {
/*
* If we've started a cookie, and it's not too big, save it in
* the CombinedCookies list. - FM
*/
if (cookie_len <= max_cookies_buffer
&& cur_cookie != NULL) {
/*
* If we had a Set-Cookie2 header, make sure the version is
* at least 1, and mark it for quoting. - FM
*/
if (SetCookie2 != NULL) {
AssumeCookieVersion(cur_cookie);
cur_cookie->quoted = TRUE;
}
HTList_appendObject(CombinedCookies, cur_cookie);
} else if (cur_cookie != NULL) {
CTrace((tfp,
"LYProcessSetCookies: Rejecting Set-Cookie: %s=%s\n",
(cur_cookie->name ?
cur_cookie->name : "[no name]"),
(cur_cookie->value ?
cur_cookie->value : "[no value]")));
CTrace((tfp,
" due to excessive length!\n"));
freeCookie(cur_cookie);
cur_cookie = NULL;
}
/*
* Start a new cookie. - FM
*/
cur_cookie = newCookie();
NumCookies++;
cookie_len = 0;
MemAllocCopy(&(cur_cookie->name), attr_start, attr_end);
cookie_len += (int) strlen(cur_cookie->name);
MemAllocCopy(&(cur_cookie->value), value_start, value_end);
cookie_len += (int) strlen(cur_cookie->value);
StrAllocCopy(cur_cookie->domain, hostname);
cookie_len += (int) strlen(hostname);
StrAllocCopy(cur_cookie->path, path);
cookie_len += (cur_cookie->pathlen = (int) strlen(cur_cookie->path));
cur_cookie->port = port;
parse_flags = 0;
cur_cookie->quoted = Quoted;
Quoted = FALSE;
SetCookieDomain(cur_cookie, hostname);
}
FREE(value);
}
}
/*
* Handle the final Set-Cookie cookie if within length limit. - FM
*/
if (NumCookies <= max_cookies_domain
&& cookie_len <= max_cookies_buffer
&& cur_cookie != NULL) {
if (SetCookie2 != NULL) {
AssumeCookieVersion(cur_cookie);
cur_cookie->quoted = TRUE;
}
HTList_appendObject(CombinedCookies, cur_cookie);
} else if (cur_cookie != NULL) {
CTrace((tfp, "LYProcessSetCookies: Rejecting Set-Cookie: %s=%s\n",
(cur_cookie->name ? cur_cookie->name : "[no name]"),
(cur_cookie->value ? cur_cookie->value : "[no value]")));
CTrace((tfp, " due to excessive %s%s%s\n",
(cookie_len > max_cookies_buffer ? "length" : ""),
(cookie_len > max_cookies_buffer && NumCookies > max_cookies_domain
? " and "
: ""),
(NumCookies > max_cookies_domain ? "number!\n" : "!\n")));
freeCookie(cur_cookie);
cur_cookie = NULL;
}
/*
* OK, now we can actually store any cookies in the CombinedCookies list.
* - FM
*/
cl = CombinedCookies;
while (NULL != (co = (cookie *) HTList_nextObject(cl))) {
CTrace((tfp, "LYProcessSetCookie: attr=value pair: '%s=%s'\n",
(co->name ? co->name : "[no name]"),
(co->value ? co->value : "[no value]")));
if (co->expires > 0) {
CTrace((tfp, " expires: %" PRI_time_t ", %s\n",
CAST_time_t (co->expires),
ctime(&co->expires)));
}
if (isHTTPS_URL(address) &&
LYForceSSLCookiesSecure == TRUE &&
!(co->flags & COOKIE_FLAG_SECURE)) {
co->flags |= COOKIE_FLAG_SECURE;
CTrace((tfp, " Forced the 'secure' flag on.\n"));
}
store_cookie(co, hostname, path);
}
HTList_delete(CombinedCookies);
CombinedCookies = NULL;
return;
}
/*
* Entry function for handling Set-Cookie: and/or Set-Cookie2:
* reply headers. They may have been concatenated as comma
* separated lists in HTTP.c or HTMIME.c. - FM
*/
void LYSetCookie(const char *SetCookie,
const char *SetCookie2,
const char *address)
{
BOOL BadHeaders = FALSE;
char *hostname = NULL, *path = NULL, *ptr;
int port = 80;
/*
* Get the hostname, port and path of the address, and report the
* Set-Cookie and/or Set-Cookie2 header(s) if trace mode is on, but set the
* cookie(s) only if LYSetCookies is TRUE. - FM
*/
if (((hostname = HTParse(address, "", PARSE_HOST)) != NULL) &&
(ptr = StrChr(hostname, ':')) != NULL) {
/*
* Replace default port number.
*/
*ptr = '\0';
ptr++;
port = atoi(ptr);
} else if (isHTTPS_URL(address)) {
port = 443;
}
/*
* Get the path from the request URI.
*/
if ((path = HTParse(address, "", PARSE_PATH | PARSE_PUNCTUATION)) != NULL) {
/*
* Trim off any parameters to provide something that we can compare
* against the cookie's path for verifying if it has the proper prefix.
*/
if ((ptr = StrChr(path, '?')) != NULL) {
CTrace((tfp, "discarding params \"%s\" in request URI\n", ptr));
*ptr = '\0';
}
/* trim a trailing slash, unless we have only a "/" */
if ((ptr = strrchr(path, '/')) != NULL) {
if (ptr == path) {
++ptr;
}
CTrace((tfp, "discarding \"%s\" from request URI\n", ptr));
*ptr = '\0';
}
}
if (isEmpty(SetCookie) &&
isEmpty(SetCookie2)) {
/*
* Yuk, something must have gone wrong in HTMIME.c or HTTP.c because
* both SetCookie and SetCookie2 are NULL or zero-length. - FM
*/
BadHeaders = TRUE;
}
CTrace((tfp, "LYSetCookie called with host '%s', path '%s',\n",
NonNull(hostname),
NonNull(path)));
if (SetCookie) {
CTrace((tfp, " and Set-Cookie: '%s'\n", SetCookie));
}
if (SetCookie2) {
CTrace((tfp, " and Set-Cookie2: '%s'\n", SetCookie2));
}
if (LYSetCookies == FALSE || BadHeaders == TRUE) {
CTrace((tfp, " Ignoring this Set-Cookie/Set-Cookie2 request.\n"));
}
/*
* We're done if LYSetCookies is off or we have bad headers. - FM
*/
if (LYSetCookies == FALSE || BadHeaders == TRUE) {
FREE(hostname);
FREE(path);
return;
}
/*
* Process the header(s).
*/
LYProcessSetCookies(SetCookie, SetCookie2, address, hostname, path, port);
FREE(hostname);
FREE(path);
return;
}
/*
* Entry function from creating a Cookie: request header
* if needed. - AK & FM
*/
char *LYAddCookieHeader(char *hostname,
char *path,
int port,
int secure)
{
char *header = NULL;
HTList *hl = domain_list, *next = NULL;
domain_entry *de;
CTrace((tfp, "LYCookie: Searching for '%s:%d', '%s'.\n",
NONNULL(hostname),
port,
NONNULL(path)));
/*
* Search the cookie_list elements in the domain_list for any cookies
* associated with the //hostname:port/path
*/
while (hl) {
de = (domain_entry *) hl->object;
next = hl->next;
if (de != NULL) {
if (!HTList_isEmpty(de->cookie_list)) {
/*
* Scan the domain's cookie_list for any cookies we should
* include in our request header.
*/
header = scan_cookie_sublist(hostname, path, port,
de->cookie_list, header, secure);
} else if (de->bv == QUERY_USER && de->invcheck_bv == DEFAULT_INVCHECK_BV) {
/*
* No cookies in this domain, and no default accept/reject
* choice was set by the user, so delete the domain. - FM
*/
freeCookies(de);
HTList_removeObject(domain_list, de);
FREE(de);
}
}
hl = next;
}
if (header)
return (header);
return (NULL);
}
#ifdef USE_PERSISTENT_COOKIES
static int number_of_file_cookies = 0;
/* rjp - cookie loading */
void LYLoadCookies(char *cookie_file)
{
FILE *cookie_handle;
char *buf = NULL;
static char domain[256], path[LY_MAXPATH], name[256], value[4100];
static char what[8], secure[8], expires_a[16];
/* *INDENT-OFF* */
static struct {
char *s;
size_t n;
} tok_values[] = {
{ domain, sizeof(domain) },
{ what, sizeof(what) },
{ path, sizeof(path) },
{ secure, sizeof(secure) },
{ expires_a, sizeof(expires_a) },
{ name, sizeof(name) },
{ value, sizeof(value) },
{ NULL, 0 }
};
/* *INDENT-ON* */
time_t expires;
cookie_handle = fopen(cookie_file, TXT_R);
if (!cookie_handle)
return;
CTrace((tfp, "LYLoadCookies: reading cookies from %s\n", cookie_file));
number_of_file_cookies = 0;
while (LYSafeGets(&buf, cookie_handle) != 0) {
cookie *moo;
int tok_loop;
char *tok_out, *tok_ptr;
LYTrimNewline(buf);
if (buf[0] == '\0' || buf[0] == '#') {
continue;
}
number_of_file_cookies++;
strcat(buf, "\t"); /* add sep after line if enough space - kw */
/*
* Tokenise the cookie line into its component parts -
* this only works for Netscape style cookie files at the
* moment. It may be worth investigating an alternative
* format for Lynx because the Netscape format isn't all
* that useful, or future-proof. - RP
*
* 'fixed' by using strsep instead of strtok. No idea
* what kind of platform problems this might introduce. - RP
*/
/*
* This fails when the path is blank
*
* sscanf(buf, "%s\t%s\t%s\t%s\t%d\t%s\t%[ -~]",
* domain, what, path, secure, &expires, name, value);
*/
CTrace((tfp, "LYLoadCookies: tokenising %s\n", buf));
tok_ptr = buf;
tok_out = LYstrsep(&tok_ptr, "\t");
for (tok_loop = 0; tok_out && tok_values[tok_loop].s; tok_loop++) {
CTrace((tfp, "\t%d:[%03d]:[%s]\n",
tok_loop, (int) (tok_out - buf), tok_out));
LYStrNCpy(tok_values[tok_loop].s,
tok_out,
(int) tok_values[tok_loop].n);
/*
* It looks like strtok ignores a leading delimiter,
* which makes things a bit more interesting. Something
* like "FALSE\t\tFALSE\t" translates to FALSE,FALSE
* instead of FALSE,,FALSE. - RP
*/
tok_out = LYstrsep(&tok_ptr, "\t");
}
if (tok_values[tok_loop].s) {
/* tok_out in above loop must have been NULL prematurely - kw */
CTrace((tfp,
"*** wrong format: not enough tokens, ignoring line!\n"));
continue;
}
expires = atol(expires_a);
CTrace((tfp, "expires:\t%s\n", ctime(&expires)));
moo = newCookie();
StrAllocCopy(moo->domain, domain);
SetCookieDomain(moo, domain);
StrAllocCopy(moo->path, path);
StrAllocCopy(moo->name, name);
if (value[0] == '"' &&
value[1] && value[strlen(value) - 1] == '"' &&
value[strlen(value) - 2] != '\\') {
value[strlen(value) - 1] = '\0';
StrAllocCopy(moo->value, value + 1);
moo->quoted = TRUE;
} else {
StrAllocCopy(moo->value, value);
}
moo->pathlen = (int) strlen(moo->path);
/*
* Justification for following flags:
* COOKIE_FLAG_FROM_FILE So we know were it comes from.
* COOKIE_FLAG_EXPIRES_SET It must have had an explicit
* expiration originally, otherwise
* it would not be in the file.
* COOKIE_FLAG_DOMAIN_SET, We don't know whether these were
* COOKIE_FLAG_PATH_SET explicit or implicit, but this
* only matters for sending version 1
* cookies; the cookies read from the
* file are currently treated all like
* version 0 (we don't set moo->version)
* so $Domain= and $Path= will normally
* not be sent to the server. But if
* these cookies somehow get mixed with
* new version 1 cookies we may end up
* sending version 1 to the server, and
* in that case we should send $Domain
* and $Path. The state-man-mec drafts
* and RFC 2109 say that $Domain and
* $Path SHOULD be omitted if they were
* not given explicitly, but not that
* they MUST be omitted.
* See 8.2 Cookie Spoofing in draft -10
* for a good reason to send them.
* However, an explicit domain should be
* now prefixed with a dot (unless it is
* for a single host), so we check for
* that.
* COOKIE_FLAG_SECURE Should have "FALSE" for normal,
* otherwise set it.
*/
moo->flags |= COOKIE_FLAG_FROM_FILE | COOKIE_FLAG_EXPIRES_SET |
COOKIE_FLAG_PATH_SET;
if (LeadingDot(domain))
moo->flags |= COOKIE_FLAG_DOMAIN_SET;
if (secure[0] != 'F')
moo->flags |= COOKIE_FLAG_SECURE;
/* @@@ Should we set port to 443 if secure is set? @@@ */
moo->expires = expires;
/*
* I don't like using this to store the cookies because it's
* designed to store cookies that have been received from an
* HTTP request, not from a persistent cookie jar. Hence the
* mucking about with the COOKIE_FLAG_FROM_FILE above. - RP
*/
store_cookie(moo, domain, path);
}
LYCloseInput(cookie_handle);
}
static FILE *NewCookieFile(char *cookie_file)
{
CTrace((tfp, "LYStoreCookies: save cookies to %s on exit\n", cookie_file));
return LYNewTxtFile(cookie_file);
}
/* rjp - persistent cookie support */
void LYStoreCookies(char *cookie_file)
{
HTList *dl, *cl;
domain_entry *de;
cookie *co;
FILE *cookie_handle = NULL;
time_t now = time(NULL); /* system specific? - RP */
if (isEmpty(cookie_file) || !strcmp(cookie_file, "/dev/null")) {
/* We give /dev/null the Unix meaning, regardless of OS */
return;
}
/*
* Check whether we have something to do. - FM
*/
if (HTList_isEmpty(domain_list) &&
number_of_file_cookies == 0) {
/* No cookies now, and haven't read any,
* so don't bother updating the file.
*/
return;
}
/* if we read cookies from the file, we'll update it even if now empty */
if (number_of_file_cookies != 0) {
cookie_handle = NewCookieFile(cookie_file);
if (cookie_handle == NULL)
return;
}
for (dl = domain_list; dl != NULL; dl = dl->next) {
de = (domain_entry *) (dl->object);
if (de == NULL)
/*
* Fote says the first object is NULL. Go with that.
*/
continue;
/*
* Show the domain's cookies. - FM
*/
for (cl = de->cookie_list; cl != NULL; cl = cl->next) {
/*
* First object is always NULL. - FM
*/
if ((co = (cookie *) cl->object) == NULL)
continue;
CTrace((tfp, "LYStoreCookies: %" PRI_time_t " %s %" PRI_time_t " ",
CAST_time_t (now),
(now < co->expires) ? "<" : ">",
CAST_time_t (co->expires)));
if ((co->flags & COOKIE_FLAG_DISCARD)) {
CTrace((tfp, "not stored - DISCARD\n"));
continue;
} else if (!(co->flags & COOKIE_FLAG_EXPIRES_SET)) {
CTrace((tfp, "not stored - no expiration time\n"));
continue;
} else if (co->expires <= now) {
CTrace((tfp, "not stored - EXPIRED\n"));
continue;
}
/* when we're sure we'll write to the file - open it */
if (cookie_handle == NULL) {
cookie_handle = NewCookieFile(cookie_file);
if (cookie_handle == NULL)
return;
}
fprintf(cookie_handle, "%s\t%s\t%s\t%s\t%" PRI_time_t
"\t%s\t%s%s%s\n",
de->ddomain,
(co->flags & COOKIE_FLAG_DOMAIN_SET) ? "TRUE" : "FALSE",
co->path,
(co->flags & COOKIE_FLAG_SECURE) ? "TRUE" : "FALSE",
CAST_time_t (co->expires), co->name,
(co->quoted ? "\"" : ""),
NonNull(co->value),
(co->quoted ? "\"" : ""));
CTrace((tfp, "STORED %s\n", de->ddomain));
}
}
if (cookie_handle != NULL) {
LYCloseOutput(cookie_handle);
HTSYS_purge(cookie_file);
}
}
#endif
/*
* Check if the given string is completely US-ASCII. If so (and if the
* original were hex-encoded), it is likely to be more useful in a decoded
* form.
*/
static BOOLEAN valueNonAscii(const char *value)
{
BOOLEAN result = FALSE;
while (*value != '\0') {
int ch = UCH(*value++);
if (ch < 32 || ch > 126) {
result = TRUE;
break;
}
}
return result;
}
/* LYHandleCookies - F.Macrides (macrides@sci.wfeb.edu)
* ---------------
*
* Lists all cookies by domain, and allows deletions of
* individual cookies or entire domains, and changes of
* 'allow' settings. The list is invoked via the COOKIE_JAR
* command (Ctrl-K), and deletions or changes of 'allow'
* settings are done by activating links in that list.
* The procedure uses a LYNXCOOKIE: internal URL scheme.
*
* Semantics:
* LYNXCOOKIE:/ Create and load the Cookie Jar Page.
* LYNXCOOKIE://domain Manipulate the domain.
* LYNXCOOKIE://domain/lynxID Delete cookie with lynxID in domain.
*
* New functions can be added as extensions to the path, and/or by
* assigning meanings to ;parameters, a ?searchpart, and/or #fragments.
*/
static int LYHandleCookies(const char *arg,
HTParentAnchor *anAnchor,
HTFormat format_out,
HTStream *sink)
{
HTFormat format_in = WWW_HTML;
HTStream *target = NULL;
char *buf = NULL;
char *domain = NULL;
char *lynxID = NULL;
HTList *dl, *cl, *next;
domain_entry *de;
cookie *co;
char *name = NULL, *value = NULL, *path = NULL;
char *comment = NULL, *Address = NULL, *Title = NULL;
int ch;
/*
* Check whether we have something to do. - FM
*/
if (HTList_isEmpty(domain_list)) {
HTProgress(COOKIE_JAR_IS_EMPTY);
LYSleepMsg();
HTNoDataOK = 1;
return (HT_NO_DATA);
}
/*
* If there's a domain string in the "host" field of the LYNXCOOKIE: URL,
* this is a request to delete something or change and 'allow' setting. -
* FM
*/
if ((domain = HTParse(arg, "", PARSE_HOST)) != NULL) {
if (*domain == '\0') {
FREE(domain);
} else {
/*
* If there is a path string (not just a slash) in the LYNXCOOKIE:
* URL, that's a cookie's lynxID and this is a request to delete it
* from the Cookie Jar. - FM
*/
if ((lynxID = HTParse(arg, "", PARSE_PATH)) != NULL) {
if (*lynxID == '\0') {
FREE(lynxID);
}
}
}
}
if (domain) {
/*
* Seek the domain in the domain_list structure. - FM
*/
if ((de = find_domain_entry(domain)) != NULL) {
FREE(domain);
/*
* We found the domain. Check whether a lynxID is present. - FM
*/
if (lynxID) {
/*
* Seek and delete the cookie with this lynxID in the domain's
* cookie list. - FM
*/
for (cl = de->cookie_list; cl != NULL; cl = cl->next) {
if ((co = (cookie *) cl->object) == NULL)
/*
* First object is always empty. - FM
*/
continue;
if (!strcmp(lynxID, co->lynxID)) {
/*
* We found the cookie. Delete it if confirmed. - FM
*/
if (HTConfirm(DELETE_COOKIE_CONFIRMATION) == FALSE) {
FREE(lynxID);
HTNoDataOK = 1;
return (HT_NO_DATA);
}
HTList_removeObject(de->cookie_list, co);
freeCookie(co);
co = NULL;
total_cookies--;
if ((de->bv == QUERY_USER &&
HTList_isEmpty(de->cookie_list)) &&
HTConfirm(DELETE_EMPTY_DOMAIN_CONFIRMATION)) {
/*
* No more cookies in this domain, no default
* accept/reject choice was set by the user, and
* got confirmation on deleting the domain, so do
* it. - FM
*/
freeCookies(de);
HTList_removeObject(domain_list, de);
FREE(de);
HTProgress(DOMAIN_EATEN);
} else {
HTProgress(COOKIE_EATEN);
}
LYSleepMsg();
HTNoDataOK = 1;
break;
}
}
} else {
/*
* Prompt whether to delete all of the cookies in this domain,
* or the domain if no cookies in it, or to change its 'allow'
* setting, or to cancel, and then act on the user's response.
* - FM
*/
if (HTList_isEmpty(de->cookie_list)) {
_statusline(DELETE_DOMAIN_SET_ALLOW_OR_CANCEL);
} else {
_statusline(DELETE_COOKIES_SET_ALLOW_OR_CANCEL);
}
HTNoDataOK = 1;
while (1) {
ch = LYgetch_single();
#ifdef VMS
if (HadVMSInterrupt) {
HadVMSInterrupt = FALSE;
ch = 'C';
}
#endif /* VMS */
switch (ch) {
case 'A':
/*
* Set to accept all cookies from this domain. - FM
*/
de->bv = ACCEPT_ALWAYS;
HTUserMsg2(ALWAYS_ALLOWING_COOKIES, de->ddomain);
return (HT_NO_DATA);
case 'C':
/*
* Cancelled. - FM
*/
reject:
HTUserMsg(CANCELLED);
return (HT_NO_DATA);
case 'D':
if (HTList_isEmpty(de->cookie_list)) {
/*
* We had an empty domain, so we were asked to
* delete it. - FM
*/
freeCookies(de);
HTList_removeObject(domain_list, de);
FREE(de);
HTProgress(DOMAIN_EATEN);
LYSleepMsg();
break;
}
Delete_all_cookies_in_domain:
/*
* Delete all cookies in this domain. - FM
*/
cl = de->cookie_list;
while (cl) {
next = cl->next;
co = (cookie *) (cl->object);
if (co) {
HTList_removeObject(de->cookie_list, co);
freeCookie(co);
co = NULL;
total_cookies--;
}
cl = next;
}
HTProgress(DOMAIN_COOKIES_EATEN);
LYSleepMsg();
/*
* If a default accept/reject choice is set, we're
* done. - FM
*/
if (de->bv != QUERY_USER)
return (HT_NO_DATA);
/*
* Check whether to delete the empty domain. - FM
*/
if (HTConfirm(DELETE_EMPTY_DOMAIN_CONFIRMATION)) {
freeCookies(de);
HTList_removeObject(domain_list, de);
FREE(de);
HTProgress(DOMAIN_EATEN);
LYSleepMsg();
}
break;
case 'P':
/*
* Set to prompt for cookie acceptance from this
* domain. - FM
*/
de->bv = QUERY_USER;
HTUserMsg2(PROMPTING_TO_ALLOW_COOKIES, de->ddomain);
return (HT_NO_DATA);
case 'V':
/*
* Set to reject all cookies from this domain. - FM
*/
de->bv = REJECT_ALWAYS;
HTUserMsg2(NEVER_ALLOWING_COOKIES, de->ddomain);
if ((!HTList_isEmpty(de->cookie_list)) &&
HTConfirm(DELETE_ALL_COOKIES_IN_DOMAIN))
goto Delete_all_cookies_in_domain;
return (HT_NO_DATA);
default:
if (LYCharIsINTERRUPT(ch))
goto reject;
continue;
}
break;
}
}
}
if (HTList_isEmpty(domain_list)) {
/*
* There are no more domains left. Don't delete the domain_list,
* otherwise atexit may be called multiple times. - kw
*/
HTProgress(ALL_COOKIES_EATEN);
LYSleepMsg();
}
FREE(domain);
FREE(lynxID);
return (HT_NO_DATA);
}
/*
* If we get to here, it was a LYNXCOOKIE:/ URL for creating and displaying
* the Cookie Jar Page, or we didn't find the domain or cookie in a
* deletion request. Set up an HTML stream and return an updated Cookie
* Jar Page. - FM
*/
target = HTStreamStack(format_in,
format_out,
sink, anAnchor);
if (target == NULL) {
HTSprintf0(&buf, CANNOT_CONVERT_I_TO_O,
HTAtom_name(format_in), HTAtom_name(format_out));
HTAlert(buf);
FREE(buf);
return (HT_NOT_LOADED);
}
/*
* Load HTML strings into buf and pass buf to the target for parsing and
* rendering. - FM
*/
#define PUTS(buf) (*target->isa->put_block)(target, buf, (int) strlen(buf))
WriteStreamTitle(target, COOKIE_JAR_TITLE);
HTSprintf0(&buf, "<h1>%s (%s)%s<a href=\"%s%s\">%s</a></h1>\n",
LYNX_NAME, LYNX_VERSION,
HELP_ON_SEGMENT,
helpfilepath, COOKIE_JAR_HELP, COOKIE_JAR_TITLE);
PUTS(buf);
HTSprintf0(&buf, "<div><em>Note:</em> %s\n", ACTIVATE_TO_GOBBLE);
PUTS(buf);
HTSprintf0(&buf, "%s</div>\n", OR_CHANGE_ALLOW);
PUTS(buf);
HTSprintf0(&buf, "<dl compact>\n");
PUTS(buf);
for (dl = domain_list; dl != NULL; dl = dl->next) {
de = (domain_entry *) (dl->object);
if (de == NULL)
/*
* First object always is NULL. - FM
*/
continue;
/*
* Show the domain link and 'allow' setting. - FM
*/
HTSprintf0(&buf,
"<dt>%s<dd><a href=\"%s//%s/\"><em>Domain:</em> %s</a>\n",
de->ddomain, STR_LYNXCOOKIE, de->ddomain, de->ddomain);
PUTS(buf);
switch (de->bv) {
case (ACCEPT_ALWAYS):
HTSprintf0(&buf, COOKIES_ALWAYS_ALLOWED);
break;
case (REJECT_ALWAYS):
HTSprintf0(&buf, COOKIES_NEVER_ALLOWED);
break;
case (QUERY_USER):
HTSprintf0(&buf, COOKIES_ALLOWED_VIA_PROMPT);
break;
}
PUTS(buf);
HTSprintf0(&buf, "\n");
PUTS(buf);
/*
* Show the domain's cookies. - FM
*/
for (cl = de->cookie_list; cl != NULL; cl = cl->next) {
if ((co = (cookie *) cl->object) == NULL)
/*
* First object is always NULL. - FM
*/
continue;
/*
* Show the name=value pair. - FM
*/
if (co->name) {
StrAllocCopy(name, co->name);
LYEntify(&name, TRUE);
} else {
StrAllocCopy(name, NO_NAME);
}
if (co->value) {
StrAllocCopy(value, co->value);
HTUnEscape(value);
if (valueNonAscii(value))
strcpy(value, co->value);
LYEntify(&value, TRUE);
} else {
StrAllocCopy(value, NO_VALUE);
}
HTSprintf0(&buf, "<dd><a href=\"%s//%s/%s\"><em>%s</em>=%s</a>\n",
STR_LYNXCOOKIE, de->ddomain, co->lynxID, name, value);
FREE(name);
FREE(value);
PUTS(buf);
if (co->flags & COOKIE_FLAG_FROM_FILE) {
HTSprintf0(&buf, "%s\n",
gettext("(from a previous session)"));
PUTS(buf);
}
/*
* Show the path, port, secure and discard setting. - FM
*/
if (co->path) {
StrAllocCopy(path, co->path);
LYEntify(&path, TRUE);
} else {
StrAllocCopy(path, "/");
}
HTSprintf0(&buf,
"<dd><em>Path:</em> %s\n<dd><em>Port:</em> %d <em>Secure:</em> %s <em>Discard:</em> %s\n",
path, co->port,
((co->flags & COOKIE_FLAG_SECURE) ? "YES" : "NO"),
((co->flags & COOKIE_FLAG_DISCARD) ? "YES" : "NO"));
FREE(path);
PUTS(buf);
/*
* Show the list of acceptable ports, if present. - FM
*/
if (co->PortList) {
HTSprintf0(&buf, "<dd><em>PortList:</em> \"%s\"\n", co->PortList);
PUTS(buf);
}
/*
* Show the commentURL, if we have one. - FM
*/
if (co->commentURL) {
StrAllocCopy(Address, co->commentURL);
LYEntify(&Address, FALSE);
StrAllocCopy(Title, co->commentURL);
LYEntify(&Title, TRUE);
HTSprintf0(&buf,
"<dd><em>CommentURL:</em> <a href=\"%s\">%s</a>\n",
Address,
Title);
FREE(Address);
FREE(Title);
PUTS(buf);
}
/*
* Show the comment, if we have one. - FM
*/
if (co->comment) {
StrAllocCopy(comment, co->comment);
LYEntify(&comment, TRUE);
HTSprintf0(&buf, "<dd><em>Comment:</em> %s\n", comment);
FREE(comment);
PUTS(buf);
}
/*
* Show the Maximum Gobble Date. - FM
*/
HTSprintf0(&buf, "<dd><em>%s</em> %s%s",
gettext("Maximum Gobble Date:"),
((co->flags & COOKIE_FLAG_EXPIRES_SET)
?
ctime(&co->expires) : END_OF_SESSION),
((co->flags & COOKIE_FLAG_EXPIRES_SET)
?
"" : "\n"));
PUTS(buf);
}
HTSprintf0(&buf, "\n");
PUTS(buf);
}
HTSprintf0(&buf, "</dl>\n</body>\n</html>\n");
PUTS(buf);
/*
* Free the target to complete loading of the Cookie Jar Page, and report a
* successful load. - FM
*/
(*target->isa->_free) (target);
FREE(buf);
return (HT_LOADED);
}
/* cookie_domain_flag_set
* ----------------------
* All purpose function to handle setting domain flags for a
* comma-delimited list of domains. cookie_domain_flags handles
* invcheck behavior, as well as accept/reject behavior. - BJP
*/
static void cookie_domain_flag_set(char *domainstr,
int flag)
{
domain_entry *de = NULL;
char **str = typecalloc(char *);
char *dstr = NULL;
char *strsmall = NULL;
if (str == NULL) {
HTAlwaysAlert(gettext("Internal"),
gettext("cookie_domain_flag_set error, aborting program"));
exit_immediately(EXIT_FAILURE);
}
/*
* Is this the first domain we're handling? If so, initialize domain_list.
*/
if (domain_list == NULL) {
#ifdef LY_FIND_LEAKS
atexit(LYCookieJar_free);
#endif
domain_list = HTList_new();
total_cookies = 0;
}
StrAllocCopy(dstr, domainstr);
*str = dstr;
while ((strsmall = LYstrsep(str, ",")) != 0) {
if (*strsmall == '\0')
/* Never add a domain for empty string. It would actually
* make more sense to use strtok here. - kw */
continue;
/*
* Check the list of existing domains to see if this is a
* re-setting of an already existing domain -- if so, just
* change the behavior, if not, create a new domain entry.
*/
if ((de = find_domain_entry(strsmall)) == NULL) {
de = typecalloc(domain_entry);
if (de == NULL)
outofmem(__FILE__, "cookie_domain_flag_set");
de->bv = ACCEPT_ALWAYS;
de->invcheck_bv = INVCHECK_QUERY;
switch (flag) {
case (FLAG_ACCEPT_ALWAYS):
de->invcheck_bv = DEFAULT_INVCHECK_BV;
break;
case (FLAG_REJECT_ALWAYS):
de->invcheck_bv = DEFAULT_INVCHECK_BV;
break;
case (FLAG_QUERY_USER):
de->invcheck_bv = DEFAULT_INVCHECK_BV;
break;
case (FLAG_INVCHECK_QUERY):
de->bv = QUERY_USER;
break;
case (FLAG_INVCHECK_STRICT):
de->bv = QUERY_USER;
break;
case (FLAG_INVCHECK_LOOSE):
de->bv = QUERY_USER;
break;
}
StrAllocCopy(de->domain, strsmall);
StrAllocCopy(de->ddomain, SkipLeadingDot(strsmall));
de->cookie_list = HTList_new();
HTList_appendObject(domain_list, de);
}
switch (flag) {
case (FLAG_ACCEPT_ALWAYS):
de->bv = ACCEPT_ALWAYS;
break;
case (FLAG_REJECT_ALWAYS):
de->bv = REJECT_ALWAYS;
break;
case (FLAG_QUERY_USER):
de->bv = QUERY_USER;
break;
case (FLAG_INVCHECK_QUERY):
de->invcheck_bv = INVCHECK_QUERY;
break;
case (FLAG_INVCHECK_STRICT):
de->invcheck_bv = INVCHECK_STRICT;
break;
case (FLAG_INVCHECK_LOOSE):
de->invcheck_bv = INVCHECK_LOOSE;
break;
}
CTrace((tfp,
"cookie_domain_flag_set (%s, bv=%u, invcheck_bv=%u)\n",
strsmall, de->bv, de->invcheck_bv));
}
FREE(strsmall);
FREE(str);
FREE(dstr);
}
/*
* If any COOKIE_{ACCEPT,REJECT}_DOMAINS have been defined, process them.
* These are comma delimited lists of domains. - BJP
*
* And for query/strict/loose invalid cookie checking. - BJP
*/
void LYConfigCookies(void)
{
static const struct {
char **domain;
int flag;
int once;
} table[] = {
/* *INDENT-OFF* */
{ &LYCookieSAcceptDomains, FLAG_ACCEPT_ALWAYS, TRUE },
{ &LYCookieSRejectDomains, FLAG_REJECT_ALWAYS, TRUE },
{ &LYCookieSStrictCheckDomains, FLAG_INVCHECK_STRICT, TRUE },
{ &LYCookieSLooseCheckDomains, FLAG_INVCHECK_LOOSE, TRUE },
{ &LYCookieSQueryCheckDomains, FLAG_INVCHECK_QUERY, TRUE },
{ &LYCookieAcceptDomains, FLAG_ACCEPT_ALWAYS, FALSE },
{ &LYCookieRejectDomains, FLAG_REJECT_ALWAYS, FALSE },
{ &LYCookieStrictCheckDomains, FLAG_INVCHECK_STRICT, FALSE },
{ &LYCookieLooseCheckDomains, FLAG_INVCHECK_LOOSE, FALSE },
{ &LYCookieQueryCheckDomains, FLAG_INVCHECK_QUERY, FALSE },
/* *INDENT-ON* */
};
unsigned n;
CTrace((tfp, "LYConfigCookies\n"));
for (n = 0; n < TABLESIZE(table); n++) {
if (*(table[n].domain) != NULL) {
cookie_domain_flag_set(*(table[n].domain), table[n].flag);
/*
* Discard the value for system settings after we've used them.
* The local settings will be merged with the contents of .lynxrc
*/
if (table[n].once) {
FREE(*(table[n].domain));
}
}
}
}
#ifdef GLOBALDEF_IS_MACRO
#define _LYCOOKIE_C_GLOBALDEF_1_INIT { "LYNXCOOKIE",LYHandleCookies,0}
GLOBALDEF(HTProtocol, LYLynxCookies, _LYCOOKIE_C_GLOBALDEF_1_INIT);
#else
GLOBALDEF HTProtocol LYLynxCookies =
{"LYNXCOOKIE", LYHandleCookies, 0};
#endif /* GLOBALDEF_IS_MACRO */
|