File: xdmcp.c

package info (click to toggle)
gdm 2.16.4-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,756 kB
  • ctags: 2,870
  • sloc: ansic: 46,434; xml: 17,760; sh: 13,465; makefile: 1,040; perl: 30
file content (2788 lines) | stat: -rw-r--r-- 84,311 bytes parent folder | download
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
/* GDM - The GNOME Display Manager
 * Copyright (C) 1998, 1999, 2000 Martin K. Petersen <mkp@mkp.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* This file contains the XDMCP implementation for managing remote
 * displays. 
 */

/* Theory of operation:
 *
 * Process idles waiting for UDP packets on port 177.
 * Incoming packets are decoded and checked against tcp_wrapper.
 *
 * A typical session looks like this:
 * 
 * Display sends Query/BroadcastQuery to Manager.
 *
 * Manager selects an appropriate authentication scheme from the
 * display's list of supported ones and sends Willing/Unwilling.
 * 
 * Assuming the display accepts the auth. scheme it sends back a
 * Request.
 *
 * If the manager accepts to service the display (i.e. loadavg is low)
 * it sends back an Accept containing a unique SessionID. The
 * SessionID is stored in an accept queue by the Manager. Should the
 * manager refuse to start a session a Decline is sent to the display.
 *
 * The display returns a Manage request containing the supplied
 * SessionID. The manager will then start a session on the display. In
 * case the SessionID is not on the accept queue the manager returns
 * Refuse. If the manager fails to open the display for connections
 * Failed is returned.
 *
 * During the session the display periodically sends KeepAlive packets
 * to the manager. The manager responds with Alive.
 *
 * Similarly the manager xpings the display once in a while and shuts
 * down the connection on failure.
 * 
 */

#include "config.h"

#ifdef HAVE_LIBXDMCP
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <X11/Xlib.h>
#include <X11/Xmd.h>
#include <X11/Xdmcp.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <fcntl.h>
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif

#ifdef HAVE_TCPWRAPPERS
  #include <tcpd.h>
#endif
#endif /* HAVE_LIBXDMCP */

#include <glib/gi18n.h>

#ifdef HAVE_LIBXDMCP
#include "gdm.h"
#include "display.h"
#include "auth.h"
#endif /* HAVE_LIBXDMCP */

#include "misc.h"
#include "choose.h"
#include "xdmcp.h"
#include "cookie.h"
#include "gdmconfig.h"

#define XDMCP_MULTICAST_ADDRESS "ff02::1"    /*This is to be changed when the Xdmcp Multicast address is decided */

int gdm_xdmcpfd = -1;

#ifdef HAVE_LIBXDMCP

/* TCP Wrapper syslog control */
gint allow_severity = LOG_INFO;
gint deny_severity = LOG_WARNING;

static guint xdmcp_source = 0;
static CARD32 globsessid;
static gchar *sysid;
static ARRAY8 servhost;
static XdmcpBuffer buf;
static gboolean initted = FALSE;

extern GSList *displays;
extern gint xdmcp_pending;
extern gint xdmcp_sessions;

/* Local prototypes */
static gboolean gdm_xdmcp_decode_packet (GIOChannel *source,
					 GIOCondition cond,
					 gpointer data);
/*************************** IPv6 specific prototypes *****************************/

#ifdef ENABLE_IPV6
static void gdm_xdmcp_handle_query (struct sockaddr_storage *clnt_sa,
                                  gint len,
                                  gint type);
static void gdm_xdmcp_handle_forward_query (struct sockaddr_storage *clnt_sa, gint len);
static void gdm_xdmcp_handle_request (struct sockaddr_storage *clnt_sa, gint len);
static void gdm_xdmcp_handle_manage (struct sockaddr_storage *clnt_sa, gint len);
static void gdm_xdmcp_handle_managed_forward (struct sockaddr_storage *clnt_sa, gint len);
static void gdm_xdmcp_handle_got_managed_forward (struct sockaddr_storage *clnt_sa, gint len);
static void gdm_xdmcp_handle_keepalive (struct sockaddr_storage *clnt_sa, gint len);
static void gdm_xdmcp_send_willing (struct sockaddr_storage *clnt_sa);
static void gdm_xdmcp_send_unwilling (struct sockaddr_storage *clnt_sa, gint type);
static void gdm_xdmcp_send_accept (GdmHostent *he, struct sockaddr_storage *clnt_sa,
               gint displaynum);
static void gdm_xdmcp_send_decline (struct sockaddr_storage *clnt_sa, const char *reason);
static void gdm_xdmcp_send_refuse (struct sockaddr_storage *clnt_sa, CARD32 sessid);
static void gdm_xdmcp_send_failed (struct sockaddr_storage *clnt_sa, CARD32 sessid);
static void gdm_xdmcp_send_alive (struct sockaddr_storage *clnt_sa, CARD16 dspnum, CARD32 sessid);
static void gdm_xdmcp_send_managed_forward (struct sockaddr_storage *clnt_sa,
                                                          struct sockaddr_storage *origin);
static void gdm_xdmcp_send_forward_query6 (GdmIndirectDisplay *id,
                                                        struct sockaddr_in6 *clnt_sa,
                                                        struct in6_addr *display_addr,
                                                         ARRAYofARRAY8Ptr authlist);
static gboolean gdm_xdmcp_host_allow (struct sockaddr_storage *clnt_sa);
static GdmForwardQuery * gdm_forward_query_alloc (struct sockaddr_storage *mgr_sa,
                                                struct sockaddr_storage *dsp_sa);
static GdmForwardQuery * gdm_forward_query_lookup (struct sockaddr_storage *clnt_sa);
static void gdm_xdmcp_whack_queued_managed_forwards6 (struct sockaddr_in6 *clnt_sa,
                                                                   struct in6_addr *origin);
static void gdm_xdmcp_send_got_managed_forward6 (struct sockaddr_in6 *clnt_sa,
                                                              struct in6_addr *origin);
static int gdm_xdmcp_displays_from_host (struct sockaddr_storage *addr);
static GdmDisplay *gdm_xdmcp_display_lookup_by_host (struct sockaddr_storage *addr, int dspnum);
static GdmDisplay *gdm_xdmcp_display_alloc (struct sockaddr_storage *addr, GdmHostent *he, int displaynum);

#else

static void gdm_xdmcp_handle_forward_query (struct sockaddr_in *clnt_sa, gint len);
static void gdm_xdmcp_handle_query (struct sockaddr_in *clnt_sa,
                                  gint len,
                                  gint type);
 static void gdm_xdmcp_handle_request (struct sockaddr_in *clnt_sa, gint len);
 static void gdm_xdmcp_handle_manage (struct sockaddr_in *clnt_sa, gint len);
 static void gdm_xdmcp_handle_managed_forward (struct sockaddr_in *clnt_sa, gint len);
 static void gdm_xdmcp_handle_got_managed_forward (struct sockaddr_in *clnt_sa, gint len);
 static void gdm_xdmcp_handle_keepalive (struct sockaddr_in *clnt_sa, gint len);
 static void gdm_xdmcp_send_willing (struct sockaddr_in *clnt_sa);
 static void gdm_xdmcp_send_unwilling (struct sockaddr_in *clnt_sa, gint type);
 static void gdm_xdmcp_send_accept (GdmHostent *he /* eaten and freed */,
                                   struct sockaddr_in *clnt_sa,
                                   int displaynum);
static void gdm_xdmcp_send_decline (struct sockaddr_in *clnt_sa, const char *reason);
 static void gdm_xdmcp_send_refuse (struct sockaddr_in *clnt_sa, CARD32 sessid);
 static void gdm_xdmcp_send_failed (struct sockaddr_in *clnt_sa, CARD32 sessid);
 static void gdm_xdmcp_send_alive (struct sockaddr_in *clnt_sa, CARD16 dspnum, CARD32 sessid);
 static void gdm_xdmcp_send_managed_forward (struct sockaddr_in *clnt_sa,
                                            struct sockaddr_in *origin);
 static gboolean gdm_xdmcp_host_allow (struct sockaddr_in *clnt_sa);
static GdmForwardQuery * gdm_forward_query_alloc (struct sockaddr_in *mgr_sa,
                                                struct sockaddr_in *dsp_sa);
static GdmForwardQuery * gdm_forward_query_lookup (struct sockaddr_in *clnt_sa);
static int gdm_xdmcp_displays_from_host (struct sockaddr_in *addr);
static GdmDisplay *gdm_xdmcp_display_lookup_by_host (struct sockaddr_in *addr, int dspnum);
static GdmDisplay *gdm_xdmcp_display_alloc (struct sockaddr_in *addr, GdmHostent *he, int displaynum);

#endif  /*IPv4 / IPv6*/

static void gdm_xdmcp_whack_queued_managed_forwards (struct sockaddr_in *clnt_sa,
                                                   struct in_addr *origin);
static void gdm_xdmcp_send_got_managed_forward (struct sockaddr_in *clnt_sa,
                                               struct in_addr *origin);
static void gdm_xdmcp_send_forward_query (GdmIndirectDisplay *id,
                                        struct sockaddr_in *clnt_sa,
                                        struct in_addr *display_addr,
                                        ARRAYofARRAY8Ptr authlist);
static void gdm_xdmcp_whack_queued_managed_forwards (struct sockaddr_in *clnt_sa,
						     struct in_addr *origin);
static void gdm_xdmcp_send_got_managed_forward (struct sockaddr_in *clnt_sa,
						struct in_addr *origin);
static GdmDisplay *gdm_xdmcp_display_lookup (CARD32 sessid);
static void gdm_xdmcp_display_dispose_check (const gchar *hostname, int dspnum);
static void gdm_xdmcp_displays_check (void);
static void gdm_forward_query_dispose (GdmForwardQuery *q);

static GSList *forward_queries = NULL;

typedef struct {
	int times;
	guint handler;
#ifdef ENABLE_IPV6
	struct sockaddr_storage manager;
	struct sockaddr_storage origin;
#else
	struct sockaddr_in manager; 
	struct sockaddr_in origin;
#endif
} ManagedForward;
#define MANAGED_FORWARD_INTERVAL 1500 /* 1.5 seconds */

static GSList *managed_forwards = NULL;


/* 
 * We don't support XDM-AUTHENTICATION-1 and XDM-AUTHORIZATION-1.
 *
 * The latter would be quite useful to avoid sending unencrypted
 * cookies over the wire. Unfortunately it isn't supported without
 * XDM-AUTHENTICATION-1 which requires a key database with private
 * keys from all X terminals on your LAN. Fun, fun, fun.
 * 
 * Furthermore user passwords go over the wire in cleartext anyway so
 * protecting cookies is not that important. 
 */

typedef struct _XdmAuth {
    ARRAY8 authentication;
    ARRAY8 authorization;
} XdmAuthRec, *XdmAuthPtr;

static XdmAuthRec serv_authlist = { 
    { (CARD16) 0, (CARD8 *) 0 },
    { (CARD16) 0, (CARD8 *) 0 }
};

#ifdef ENABLE_IPV6
static gboolean have_ipv6 (void)
{
	int s;

	s = socket (AF_INET6, SOCK_STREAM, 0);
	if (s != -1) {
		VE_IGNORE_EINTR (close (s));
		return TRUE;
	}

	return FALSE;
}
#endif

static int
#ifdef ENABLE_IPV6
gdm_xdmcp_displays_from_host (struct sockaddr_storage *addr)
#else
gdm_xdmcp_displays_from_host (struct sockaddr_in *addr)
#endif
{
	GSList *li;
	int count = 0;

	for (li = displays; li != NULL; li = li->next) {
		GdmDisplay *disp = li->data;
		if (SERVER_IS_XDMCP (disp)) {
#ifdef ENABLE_IPV6
		    if (disp->addrtype == AF_INET6 &&
			memcmp (&disp->addr6, &((struct sockaddr_in6 *)addr)->sin6_addr, sizeof (struct in6_addr)) == 0)
			count++;
		    else
#endif
		    if (disp->addrtype == AF_INET &&
		    memcmp (&disp->addr, &((struct sockaddr_in *)addr)->sin_addr, sizeof (struct in_addr)) == 0)
			count++;
		}
	}

	return count;
}

static GdmDisplay *
#ifdef ENABLE_IPV6
gdm_xdmcp_display_lookup_by_host (struct sockaddr_storage *addr, int dspnum)
#else
gdm_xdmcp_display_lookup_by_host (struct sockaddr_in *addr, int dspnum)
#endif
{
	GSList *li;

	for (li = displays; li != NULL; li = li->next) {
		GdmDisplay *disp = li->data;
		if (SERVER_IS_XDMCP (disp)) {
#ifdef ENABLE_IPV6
		    if (disp->addrtype == AF_INET6) {
			if ((memcmp (&disp->addr6, &((struct sockaddr_in6 *)addr)->sin6_addr, sizeof (struct in6_addr)) == 0) && disp->xdmcp_dispnum == dspnum)
			return disp;
		    }
		    else
#endif
		    if ((memcmp (&disp->addr, &((struct sockaddr_in *)addr)->sin_addr, sizeof (struct in_addr)) == 0) && disp->xdmcp_dispnum == dspnum)
			return disp;
		}
	}

	return NULL;
}


gboolean
gdm_xdmcp_init (void)
{
#ifdef ENABLE_IPV6
    struct sockaddr_storage serv_sa = {0};
#else
    struct sockaddr_in serv_sa = {0};
#endif
    gint addrlen;
    gchar hostbuf[1024];
    struct utsname name;
    int udpport = gdm_get_value_int (GDM_KEY_UDP_PORT);

    if ( ! gdm_get_value_bool (GDM_KEY_XDMCP))
	    return TRUE;

    globsessid = g_random_int ();
    
    /* Fetch and store local hostname in XDMCP friendly format */
    hostbuf[1023] = '\0';
    if G_UNLIKELY (gethostname (hostbuf, 1023) != 0) {
	gdm_error (_("%s: Could not get server hostname: %s!"), 
		   "gdm_xdmcp_init", strerror (errno));
	strcmp (hostbuf, "localhost.localdomain");
    }

    if ( ! initted) {
	    uname (&name);
	    sysid = g_strconcat (name.sysname, " ", name.release, NULL);

	    servhost.data = (CARD8 *) g_strdup (hostbuf);
	    servhost.length = strlen ((char *) servhost.data);
	    
	    initted = TRUE;
    }
    
    gdm_debug ("XDMCP: Start up on host %s, port %d", hostbuf, udpport);
    
    /* Open socket for communications */
#ifdef ENABLE_IPV6
    gdm_xdmcpfd = socket (AF_INET6, SOCK_DGRAM, 0); /* UDP */
    if (gdm_xdmcpfd < 0)
#endif
	gdm_xdmcpfd = socket (AF_INET, SOCK_DGRAM, 0); /* UDP */
    
    if G_UNLIKELY (gdm_xdmcpfd < 0) {
	gdm_error (_("%s: Could not create socket!"), "gdm_xdmcp_init");
        gdm_set_value_bool (GDM_KEY_XDMCP, FALSE);
	return FALSE;
    }

#ifdef ENABLE_IPV6
    if (have_ipv6 ()) {
	((struct sockaddr_in6 *)(&serv_sa))->sin6_family = AF_INET6;
	((struct sockaddr_in6 *)(&serv_sa))->sin6_port = htons (udpport); /* UDP 177 */
	((struct sockaddr_in6 *)(&serv_sa))->sin6_addr = in6addr_any;
	addrlen = sizeof (struct sockaddr_in6);

	/* Checking and Setting Multicast options */
	if (gdm_get_value_bool (GDM_KEY_MULTICAST)) {
	    int socktemp;       /* temporary socket for getting info about available interfaces*/
	    int i, num;
	    char *buf;
	    struct ipv6_mreq mreq;

	    /* For interfaces' list */
	    struct ifconf ifc;
	    struct ifreq *ifr;

	    /* Extract Multicast address for IPv6 */
	    if (ve_string_empty (gdm_get_value_string (GDM_KEY_MULTICAST_ADDR))) {

		/* Stuff it with all-node multicast address */
		gdm_set_value_string (GDM_KEY_MULTICAST_ADDR, XDMCP_MULTICAST_ADDRESS);
	    }

	    socktemp = socket (AF_INET, SOCK_DGRAM, 0);
#ifdef SIOCGIFNUM
	    if (ioctl (socktemp, SIOCGIFNUM, &num) < 0) {
		num = 64;
	    }
#else
	    num = 64;
#endif
	    ifc.ifc_len = sizeof (struct ifreq) * num;
	    ifc.ifc_buf = buf = malloc (ifc.ifc_len);
	    if (ioctl (socktemp, SIOCGIFCONF, &ifc) >= 0) {
		ifr = ifc.ifc_req;
		num = ifc.ifc_len / sizeof (struct ifreq); /* No of interfaces */

		/* Joining multicast group with all interfaces*/
		for (i = 0 ; i < num ; i++) {
		    struct ifreq ifreq;
		    int ifindex;

		    memset (&ifreq, 0, sizeof (ifreq));
		    strncpy (ifreq.ifr_name, ifr[i].ifr_name, sizeof (ifreq.ifr_name));
		    /* paranoia */
		    ifreq.ifr_name[sizeof (ifreq.ifr_name) - 1] = '\0';

		    if (ioctl (socktemp, SIOCGIFFLAGS, &ifreq) < 0)
			gdm_debug ("XDMCP: Could not get SIOCGIFFLAGS for %s", ifr[i].ifr_name);

		    ifindex = if_nametoindex (ifr[i].ifr_name);

		    if ((!(ifreq.ifr_flags & IFF_UP) ||
                        (ifreq.ifr_flags & IFF_LOOPBACK)) ||
                       ((ifindex == 0 ) && (errno == ENXIO))) {
                               /* Not a valid interface or loopback interface*/
			continue;
			}

		    mreq.ipv6mr_interface = ifindex;
		    inet_pton (AF_INET6, gdm_get_value_string (GDM_KEY_MULTICAST_ADDR), &mreq.ipv6mr_multiaddr);
		    setsockopt (gdm_xdmcpfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof (mreq));
		} 
           }
           g_free (buf);
           close (socktemp);
	}
    }
    else
#endif
    {
	((struct sockaddr_in *)(&serv_sa))->sin_family = AF_INET;
	((struct sockaddr_in *)(&serv_sa))->sin_port = htons (udpport); /* UDP 177 */
	((struct sockaddr_in *)(&serv_sa))->sin_addr.s_addr = htonl (INADDR_ANY);
	addrlen = sizeof (struct sockaddr_in);
    }
   
    if G_UNLIKELY (bind (gdm_xdmcpfd, (struct sockaddr*) &serv_sa, addrlen) == -1) {
	gdm_error (_("%s: Could not bind to XDMCP socket!"), "gdm_xdmcp_init");
	gdm_xdmcp_close ();
	gdm_set_value_bool (GDM_KEY_XDMCP, FALSE);
	return FALSE;
    }

    return TRUE;
}


void
gdm_xdmcp_run (void)
{
	GIOChannel *xdmcpchan;

	xdmcpchan = g_io_channel_unix_new (gdm_xdmcpfd);
	g_io_channel_set_encoding (xdmcpchan, NULL, NULL);
	g_io_channel_set_buffered (xdmcpchan, FALSE);

	xdmcp_source = g_io_add_watch_full
		(xdmcpchan, G_PRIORITY_DEFAULT,
		 G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
		 gdm_xdmcp_decode_packet, NULL, NULL);
	g_io_channel_unref (xdmcpchan);
}


void
gdm_xdmcp_close (void)
{
	if (xdmcp_source > 0) {
		g_source_remove (xdmcp_source);
		xdmcp_source = 0;
	}

	if (gdm_xdmcpfd > 0) {
		VE_IGNORE_EINTR (close (gdm_xdmcpfd));
		gdm_xdmcpfd = -1;
	}
}


static gboolean
gdm_xdmcp_decode_packet (GIOChannel *source, GIOCondition cond, gpointer data)
{
#ifdef ENABLE_IPV6
    struct sockaddr_storage clnt_sa;
#else
    struct sockaddr_in clnt_sa;
#endif
    gint sa_len = sizeof (clnt_sa);
    XdmcpHeader header;
    static const char * const opcode_names[] = {
	NULL,
	"BROADCAST_QUERY", "QUERY", "INDIRECT_QUERY", "FORWARD_QUERY",
	"WILLING", "UNWILLING", "REQUEST", "ACCEPT", "DECLINE", "MANAGE", "REFUSE",
	"FAILED", "KEEPALIVE", "ALIVE"
    };
    static const char * const gdm_opcode_names[] = {
	"MANAGED_FORWARD", "GOT_MANAGED_FORWARD"
    };

    /* packets come at somewhat random times */
    gdm_random_tick ();

    if (cond != G_IO_IN)
	    gdm_debug ("gdm_xdmcp_decode_packet: GIOCondition %d", (int)cond);

    if ( ! (cond & G_IO_IN)) 
        return TRUE;

    if G_UNLIKELY (!XdmcpFill (gdm_xdmcpfd, &buf, (XdmcpNetaddr)&clnt_sa, &sa_len)) {
	gdm_debug (_("%s: Could not create XDMCP buffer!"),
		   "gdm_xdmcp_decode_packet");
	return TRUE;
    }
    
    if G_UNLIKELY (!XdmcpReadHeader (&buf, &header)) {
	gdm_error (_("%s: Could not read XDMCP header!"),
		   "gdm_xdmcp_decode_packet");
	return TRUE;
    }
    
    if G_UNLIKELY (header.version != XDM_PROTOCOL_VERSION &&
		   header.version != GDM_XDMCP_PROTOCOL_VERSION) {
	gdm_error (_("%s: Incorrect XDMCP version!"),
		   "gdm_xdmcp_decode_packet");
	return TRUE;
    }

    if (header.opcode <= ALIVE) {
#ifdef ENABLE_IPV6
	if (clnt_sa.ss_family == AF_INET6) {
	    char buffer6[INET6_ADDRSTRLEN];

	    gdm_debug ("gdm_xdmcp_decode: Received opcode %s from client %s", opcode_names[header.opcode], inet_ntop (AF_INET6, &((struct sockaddr_in6 *)(&clnt_sa))->sin6_addr, buffer6, INET6_ADDRSTRLEN));
	}
	else
#endif
	{
	    gdm_debug ("gdm_xdmcp_decode: Received opcode %s from client %s", opcode_names[header.opcode], inet_ntoa (((struct sockaddr_in *)(&clnt_sa))->sin_addr));
	}
    }

    if (header.opcode >= GDM_XDMCP_FIRST_OPCODE &&
        header.opcode < GDM_XDMCP_LAST_OPCODE) {
#ifdef ENABLE_IPV6
	if (clnt_sa.ss_family == AF_INET6) {

		char buffer6[INET6_ADDRSTRLEN];

		gdm_debug ("gdm_xdmcp_decode: Received opcode %s from client %s", gdm_opcode_names[header.opcode - GDM_XDMCP_FIRST_OPCODE], inet_ntop (AF_INET6, &((struct sockaddr_in6 *)(&clnt_sa))->sin6_addr, buffer6, INET6_ADDRSTRLEN));
	}
	else
#endif
	{
		char buffer[INET_ADDRSTRLEN];

		gdm_debug ("gdm_xdmcp_decode: Received opcode %s from client %s", gdm_opcode_names[header.opcode - GDM_XDMCP_FIRST_OPCODE], inet_ntop (AF_INET, &((struct sockaddr_in *)(&clnt_sa))->sin_addr, buffer, INET_ADDRSTRLEN));
	}
    }

    switch (header.opcode) {
	
    case BROADCAST_QUERY:
	gdm_xdmcp_handle_query (&clnt_sa, header.length, BROADCAST_QUERY);
	break;
	
    case QUERY:
	gdm_xdmcp_handle_query (&clnt_sa, header.length, QUERY);
	break;
	
    case INDIRECT_QUERY:
	gdm_xdmcp_handle_query (&clnt_sa, header.length, INDIRECT_QUERY);
	break;
	
    case FORWARD_QUERY:
	gdm_xdmcp_handle_forward_query (&clnt_sa, header.length);
	break;
	
    case REQUEST:
	gdm_xdmcp_handle_request (&clnt_sa, header.length);
	break;
	
    case MANAGE:
	gdm_xdmcp_handle_manage (&clnt_sa, header.length);
	break;
	
    case KEEPALIVE:
	gdm_xdmcp_handle_keepalive (&clnt_sa, header.length);
	break;

    case GDM_XDMCP_MANAGED_FORWARD:
	gdm_xdmcp_handle_managed_forward (&clnt_sa, header.length);
	break;

    case GDM_XDMCP_GOT_MANAGED_FORWARD:
	gdm_xdmcp_handle_got_managed_forward (&clnt_sa, header.length);
	break;
	
    default:
#ifdef ENABLE_IPV6
	if (clnt_sa.ss_family == AF_INET6) {
		char buffer6[INET6_ADDRSTRLEN];

		gdm_error (_("%s: Unknown opcode from host %s"),
                          "gdm_xdmcp_decode_packet",
                          inet_ntop (AF_INET6, &(((struct sockaddr_in6 *)(&clnt_sa))->sin6_addr), buffer6, INET6_ADDRSTRLEN));
	}
	else
#endif
	{
		gdm_error (_("%s: Unknown opcode from host %s"),
                          "gdm_xdmcp_decode_packet",
                          inet_ntoa (((struct sockaddr_in *)(&clnt_sa))->sin_addr));
	}
	break;
    }

    return TRUE;
}

static void 
#ifdef ENABLE_IPV6
gdm_xdmcp_handle_query (struct sockaddr_storage *clnt_sa, gint len, gint type)
#else
gdm_xdmcp_handle_query (struct sockaddr_in *clnt_sa, gint len, gint type)
#endif
{
    ARRAYofARRAY8 clnt_authlist;
    gint i = 0, explen = 1;

#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	char buffer6[INET6_ADDRSTRLEN];

	gdm_debug ("gdm_xdmcp_handle_query: Opcode %d from %s", type, inet_ntop (AF_INET6, &((struct sockaddr_in6 *)(clnt_sa))->sin6_addr, buffer6, INET6_ADDRSTRLEN));
    }
    else
#endif
    {
	gdm_debug ("gdm_xdmcp_handle_query: Opcode %d from %s", type, inet_ntoa (((struct sockaddr_in *)(clnt_sa))->sin_addr));
    }
 
    /* Extract array of authentication names from Xdmcp packet */
    if G_UNLIKELY (! XdmcpReadARRAYofARRAY8 (&buf, &clnt_authlist)) {
	gdm_error (_("%s: Could not extract authlist from packet"),
		   "gdm_xdmcp_handle_query"); 
	return;
    }
    
    /* Crude checksumming */
    for (i = 0 ; i < clnt_authlist.length ; i++) {
	    if G_UNLIKELY (gdm_get_value_bool (GDM_KEY_DEBUG)) {
		    char *s = g_strndup ((char *) clnt_authlist.data[i].data, clnt_authlist.length);
		    gdm_debug ("gdm_xdmcp_handle_query: authlist: %s", ve_sure_string (s));
		    g_free (s);
	    }
	    explen += 2+clnt_authlist.data[i].length;
    }
    
    if G_UNLIKELY (len != explen) {
	gdm_error (_("%s: Error in checksum"),
		   "gdm_xdmcp_handle_query"); 
	XdmcpDisposeARRAYofARRAY8 (&clnt_authlist);
	return;
    }

    /* Check with tcp_wrappers if client is allowed to access */
    if (gdm_xdmcp_host_allow (clnt_sa)) {

	/* If this is an INDIRECT_QUERY, try to look up the display in
 	 * the pending list. If found send a FORWARD_QUERY to the
 	 * chosen manager. Otherwise alloc a new indirect display. */

	if (gdm_get_value_bool (GDM_KEY_INDIRECT) &&
	    type == INDIRECT_QUERY) {
		GdmIndirectDisplay *id = gdm_choose_indirect_lookup (clnt_sa);

		if (id != NULL && (
#ifdef ENABLE_IPV6
		    (id->chosen_host6 != NULL) ||
#endif
		    (id->chosen_host != NULL)))
		{
			/* if user chose us, then just send willing */
			if ((((struct sockaddr_in *)clnt_sa)->sin_family == AF_INET && gdm_is_local_addr ((struct in_addr *)(id->chosen_host)))
#ifdef ENABLE_IPV6
			    || (clnt_sa->ss_family == AF_INET6 && gdm_is_local_addr6 ((struct in6_addr *)(id->chosen_host6)))
#endif
                          ) {

				/* get rid of indirect, so that we don't get
				 * the chooser */
				gdm_choose_indirect_dispose (id);
				gdm_xdmcp_send_willing (clnt_sa);
			}
			else
			if ((((struct sockaddr_in *)clnt_sa)->sin_family == AF_INET && gdm_is_loopback_addr (&(((struct sockaddr_in *)clnt_sa)->sin_addr)))
#ifdef ENABLE_IPV6
			    || (clnt_sa->ss_family == AF_INET6 && gdm_is_loopback_addr6 (&(((struct sockaddr_in6 *)clnt_sa)->sin6_addr)))
#endif
			) {

				/* woohoo! fun, I have no clue how to get
				 * the correct ip, SO I just send forward
				 * queries with all the different IPs */
				const GList *list = gdm_peek_local_address_list ();

				while (list != NULL) {
					struct sockaddr *saddr = (struct sockaddr *)(list->data);
#ifdef ENABLE_IPV6
					struct in6_addr *addr6;
#endif
					struct in_addr *addr;

#ifdef ENABLE_IPV6
					if (saddr->sa_family == AF_INET6) {
						addr6 = &(((struct sockaddr_in6 *)saddr)->sin6_addr);
						if ( ! gdm_is_loopback_addr6 (addr6)) {
							/* forward query to * chosen host */
							gdm_xdmcp_send_forward_query6 (id, (struct sockaddr_in6 *)clnt_sa, addr6, &clnt_authlist);
						}
					}
					else
#endif
					{
						addr = &(((struct sockaddr_in *)saddr)->sin_addr);
						if ( ! gdm_is_loopback_addr (addr)) {
							/* forward query to * chosen host */
							gdm_xdmcp_send_forward_query (id, (struct sockaddr_in *)clnt_sa, addr, &clnt_authlist);
						}

					}

					list = list->next;
				}
			} else {
				/* or send forward query to chosen host */
				gdm_xdmcp_send_forward_query
					(id, (struct sockaddr_in *)clnt_sa,
					 &(((struct sockaddr_in *)clnt_sa)->sin_addr),
					 &clnt_authlist);
			}
		} else if (id == NULL) {
			id = gdm_choose_indirect_alloc (clnt_sa);
			if (id != NULL) {
				gdm_xdmcp_send_willing (clnt_sa);
			}
		} else  {
			gdm_xdmcp_send_willing (clnt_sa);
		}
	} else {
		gdm_xdmcp_send_willing (clnt_sa);
	}
    } else if (type == QUERY) {
	    /* unwilling is ONLY sent for direct queries, never for broadcast
	     * nor indirects */
	    gdm_xdmcp_send_unwilling (clnt_sa, type);
    }

    /* Dispose authlist from remote display */
    XdmcpDisposeARRAYofARRAY8 (&clnt_authlist);
}

#ifdef ENABLE_IPV6
static void
gdm_xdmcp_send_forward_query6 (GdmIndirectDisplay *id, struct sockaddr_in6 *clnt_sa, struct in6_addr *display_addr, ARRAYofARRAY8Ptr authlist)
{
	struct sockaddr_in6 sock = {0};
	XdmcpHeader header;
	int i, authlen;
	ARRAY8 address;
	ARRAY8 port;
	char buffer6[INET6_ADDRSTRLEN];

	g_assert (id != NULL);
	g_assert (id->chosen_host != NULL);

	gdm_debug ("gdm_xdmcp_send_forward_query6: Sending forward query to %s", inet_ntop (AF_INET6, &(id->chosen_host6), buffer6, sizeof (buffer6)));
	gdm_debug ("gdm_xdmcp_send_forward_query6: Query contains %s:%d", inet_ntop (AF_INET6, display_addr, buffer6, sizeof (buffer6)), (int) ntohs (clnt_sa->sin6_port));

	authlen = 1;
	for (i = 0 ; i < authlist->length ; i++) {
               authlen += 2 + authlist->data[i].length;
	}

	/* we depend on this being 2 elsewhere as well */
	port.length = 2;
	port.data = g_new (char, 2);
	memcpy (port.data, &(clnt_sa->sin6_port), 2);
	address.length = sizeof (struct in6_addr);
	address.data = (void *)g_new (struct in6_addr, 1);
	memcpy (address.data, display_addr, sizeof (struct in6_addr));


	header.opcode = (CARD16) FORWARD_QUERY;
	header.length = authlen;
	header.length += 2 + address.length;
	header.length += 2 + port.length;
	header.version = XDM_PROTOCOL_VERSION;
	XdmcpWriteHeader (&buf, &header);

	XdmcpWriteARRAY8 (&buf, &address);
	XdmcpWriteARRAY8 (&buf, &port);
	XdmcpWriteARRAYofARRAY8 (&buf, authlist);

	memset (&sock, 0, sizeof (sock));

	sock.sin6_family = AF_INET6;
	sock.sin6_port = htons (XDM_UDP_PORT);
	memcpy (sock.sin6_addr.s6_addr, id->chosen_host6->s6_addr, sizeof (struct in6_addr));
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr) &sock, (int)sizeof (struct sockaddr_in6));
	g_free (port.data);
	g_free (address.data);
}
#endif

static void
gdm_xdmcp_send_forward_query (GdmIndirectDisplay *id,
			      struct sockaddr_in *clnt_sa,
			      struct in_addr *display_addr,
			      ARRAYofARRAY8Ptr authlist)
{
	struct sockaddr_in sock = {0};
	XdmcpHeader header;
	int i, authlen;
	ARRAY8 address;
	ARRAY8 port;

	gdm_assert (id != NULL);
	gdm_assert (id->chosen_host != NULL);

	gdm_debug ("gdm_xdmcp_send_forward_query: Sending forward query to %s",
		   inet_ntoa (*id->chosen_host));
	gdm_debug ("gdm_xdmcp_send_forward_query: Query contains %s:%d", 
		   inet_ntoa (*display_addr),
		   (int) ntohs (clnt_sa->sin_port));

	authlen = 1;
	for (i = 0 ; i < authlist->length ; i++) {
		authlen += 2 + authlist->data[i].length;
	}

	/* we depend on this being 2 elsewhere as well */
	port.length = 2;
	port.data = (CARD8 *) g_new (char, 2);
	memcpy (port.data, &(clnt_sa->sin_port), 2);

	address.length = sizeof (struct in_addr);
	address.data = (void *)g_new (struct in_addr, 1);
	memcpy (address.data, display_addr, sizeof (struct in_addr));

	header.opcode = (CARD16) FORWARD_QUERY;
	header.length = authlen;
	header.length += 2 + address.length;
	header.length += 2 + port.length;
	header.version = XDM_PROTOCOL_VERSION;
	XdmcpWriteHeader (&buf, &header);

	XdmcpWriteARRAY8 (&buf, &address);
	XdmcpWriteARRAY8 (&buf, &port);
	XdmcpWriteARRAYofARRAY8 (&buf, authlist);

	sock.sin_family = AF_INET;
	sock.sin_port = htons (XDM_UDP_PORT);
	sock.sin_addr.s_addr = id->chosen_host->s_addr;
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr) &sock,
		    (int)sizeof (struct sockaddr_in));

	g_free (port.data);
	g_free (address.data);
}

static gboolean
remove_oldest_forward (void)
{
	GSList *li;
	GdmForwardQuery *oldest = NULL;

	for (li = forward_queries; li != NULL; li = li->next) {
		GdmForwardQuery *query = li->data;

		if (oldest == NULL ||
		    query->acctime < oldest->acctime) {
			oldest = query;
		}
	}

	if (oldest != NULL) {
		gdm_forward_query_dispose (oldest);
		return TRUE;
	} else {
		return FALSE;
	}
}

static GdmForwardQuery *
#ifdef ENABLE_IPV6
gdm_forward_query_alloc (struct sockaddr_storage *mgr_sa,
			 struct sockaddr_storage *dsp_sa)
#else
gdm_forward_query_alloc (struct sockaddr_in *mgr_sa,
			 struct sockaddr_in *dsp_sa)
#endif
{
	GdmForwardQuery *q;
	int count;

	count = g_slist_length (forward_queries);

	while (count > GDM_MAX_FORWARD_QUERIES &&
	       remove_oldest_forward ())
		count --;

	q = g_new0 (GdmForwardQuery, 1);
#ifdef ENABLE_IPV6
	q->dsp_sa = g_new0 (struct sockaddr_storage, 1);
	memcpy (q->dsp_sa, dsp_sa, sizeof (struct sockaddr_storage));
	q->from_sa = g_new0 (struct sockaddr_storage, 1);
	memcpy (q->from_sa, mgr_sa, sizeof (struct sockaddr_storage));
#else
	q->dsp_sa = g_new0 (struct sockaddr_in, 1);
	memcpy (q->dsp_sa, dsp_sa, sizeof (struct sockaddr_in));
	q->from_sa = g_new0 (struct sockaddr_in, 1);
	memcpy (q->from_sa, mgr_sa, sizeof (struct sockaddr_in));
#endif

	forward_queries = g_slist_prepend (forward_queries, q);

	return q;
}

static GdmForwardQuery *
#ifdef ENABLE_IPV6
gdm_forward_query_lookup (struct sockaddr_storage *clnt_sa)
#else
gdm_forward_query_lookup (struct sockaddr_in *clnt_sa)
#endif
{
	GSList *li, *qlist;
	GdmForwardQuery *q;
	time_t curtime = time (NULL);

	qlist = g_slist_copy (forward_queries);

	for (li = qlist; li != NULL; li = li->next) {
		q = (GdmForwardQuery *) li->data;
		if (q == NULL)
			continue;
#ifdef ENABLE_IPV6
		if (clnt_sa->ss_family == AF_INET6) {
			if (memcmp (((struct sockaddr_in6 *)(q->dsp_sa))->sin6_addr.s6_addr, ((struct sockaddr_in6 *)clnt_sa)->sin6_addr.s6_addr, sizeof (struct in6_addr)) == 0) {
				g_slist_free (qlist);
				return q;
			}

			if (q->acctime > 0 &&  curtime > q->acctime + GDM_FORWARD_QUERY_TIMEOUT) {
				char buffer6[INET6_ADDRSTRLEN];

				gdm_debug ("gdm_forward_query_lookup: Disposing stale forward query from %s", inet_ntop (AF_INET6, &((struct sockaddr_in6 *)q->dsp_sa)->sin6_addr, buffer6, INET6_ADDRSTRLEN));
				gdm_forward_query_dispose (q);
				continue;
			}
		}
		else
#endif
		{
			if (((struct sockaddr_in *)(q->dsp_sa))->sin_addr.s_addr == ((struct sockaddr_in *)clnt_sa)->sin_addr.s_addr) {
				g_slist_free (qlist);
				return q;
			}

			if (q->acctime > 0 &&
                           curtime > q->acctime + GDM_FORWARD_QUERY_TIMEOUT)   {
				gdm_debug ("gdm_forward_query_lookup: Disposing stale forward query from %s",
                                          inet_ntoa (((struct sockaddr_in*)q->dsp_sa)->sin_addr));
				gdm_forward_query_dispose (q);
				continue;
			}
		}

	}
	g_slist_free (qlist);

#ifdef ENABLE_IPV6
	if (clnt_sa->ss_family == AF_INET6) {
		char buffer6[INET6_ADDRSTRLEN];

		gdm_debug ("gdm_forward_query_lookup: Host %s not found", inet_ntop (AF_INET6, &((struct sockaddr_in6 *)clnt_sa)->sin6_addr, buffer6, INET6_ADDRSTRLEN));
	}
	else
#endif
	{
		gdm_debug ("gdm_forward_query_lookup: Host %s not found",
                          inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
	}

	return NULL;
}


static void
gdm_forward_query_dispose (GdmForwardQuery *q)
{
	if (q == NULL)
		return;

	forward_queries = g_slist_remove (forward_queries, q);

	q->acctime = 0;

#ifdef ENABLE_IPV6
	if (q->dsp_sa->ss_family == AF_INET6) {
		char buffer6[INET6_ADDRSTRLEN];

		gdm_debug ("gdm_forward_query_dispose: Disposing %s", inet_ntop (AF_INET6, &(((struct sockaddr_in6 *)(q->dsp_sa))->sin6_addr), buffer6, INET6_ADDRSTRLEN));
	}
	else
#endif
	{
               gdm_debug ("gdm_forward_query_dispose: Disposing %s",
                          inet_ntoa (((struct sockaddr_in *)(q->dsp_sa))->sin_addr));
	}

	g_free (q->dsp_sa);
	q->dsp_sa = NULL;
	g_free (q->from_sa);
	q->from_sa = NULL;

	g_free (q);
}


static void 
#ifdef ENABLE_IPV6
gdm_xdmcp_handle_forward_query (struct sockaddr_storage *clnt_sa, gint len)
#else
gdm_xdmcp_handle_forward_query (struct sockaddr_in *clnt_sa, gint len)
#endif
{
    ARRAY8 clnt_addr;
    ARRAY8 clnt_port;
    ARRAYofARRAY8 clnt_authlist;
    gint i = 0, explen = 1;
#ifdef ENABLE_IPV6
    struct sockaddr_storage disp_sa = {0};
#else
    struct sockaddr_in disp_sa = {0};
#endif

    /* Check with tcp_wrappers if client is allowed to access */
    if (! gdm_xdmcp_host_allow (clnt_sa)) {
#ifdef ENABLE_IPV6
	    if (clnt_sa->ss_family == AF_INET6) {
		char buffer6[INET6_ADDRSTRLEN];

		gdm_error ("%s: Got FORWARD_QUERY from banned host %s",
			   "gdm_xdmcp_handle_forward query",
			   inet_ntop (AF_INET6, &(((struct sockaddr_in6 *)clnt_sa)->sin6_addr), buffer6, INET6_ADDRSTRLEN));
	    }
	    else
#endif
	    {
		gdm_error ("%s: Got FORWARD_QUERY from banned host %s",
			   "gdm_xdmcp_handle_forward query",
			   inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
	    }

	    return;
    }
    
    /* Read display address */
    if G_UNLIKELY (! XdmcpReadARRAY8 (&buf, &clnt_addr)) {
	gdm_error (_("%s: Could not read display address"),
		   "gdm_xdmcp_handle_forward_query");
	return;
    }
    
    /* Read display port */
    if G_UNLIKELY (! XdmcpReadARRAY8 (&buf, &clnt_port)) {
	XdmcpDisposeARRAY8 (&clnt_addr);
	gdm_error (_("%s: Could not read display port number"),
		   "gdm_xdmcp_handle_forward_query");
	return;
    }
    
    /* Extract array of authentication names from Xdmcp packet */
    if G_UNLIKELY (! XdmcpReadARRAYofARRAY8 (&buf, &clnt_authlist)) {
	XdmcpDisposeARRAY8 (&clnt_addr);
	XdmcpDisposeARRAY8 (&clnt_port);
	gdm_error (_("%s: Could not extract authlist from packet"),
		   "gdm_xdmcp_handle_forward_query"); 
	return;
    }
    
    /* Crude checksumming */
    explen = 1;
    explen += 2+clnt_addr.length;
    explen += 2+clnt_port.length;
    
    for (i = 0 ; i < clnt_authlist.length ; i++) {
	    if G_UNLIKELY (gdm_get_value_bool (GDM_KEY_DEBUG)) {
		    char *s = g_strndup ((char *) clnt_authlist.data[i].data, clnt_authlist.length);
		    gdm_debug ("gdm_xdmcp_handle_forward_query: authlist: %s", ve_sure_string (s));
		    g_free (s);
	    }
	    explen += 2+clnt_authlist.data[i].length;
    }
    
    if G_UNLIKELY (len != explen) {
	gdm_error (_("%s: Error in checksum"),
		   "gdm_xdmcp_handle_forward_query"); 
	goto out;
    }

#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	char buffer6[INET6_ADDRSTRLEN];

	struct in6_addr ipv6_addr;
	struct in6_addr * ipv6_addr_ptr = NULL;

	if (clnt_port.length == 2 &&
	       clnt_addr.length == 4) {
		char * ipv4_addr;

		/* Convert IPv4 address to IPv6 if needed */
		struct sockaddr_in tmp_disp_sa = {0};
		((struct sockaddr_in *)(&tmp_disp_sa))->sin_family = AF_INET;
		memcpy (&((struct sockaddr_in *)(&tmp_disp_sa))->sin_port, clnt_port.data, 2);
		memcpy (&((struct sockaddr_in *)(&tmp_disp_sa))->sin_addr.s_addr, clnt_addr.data, 4);

		ipv4_addr = inet_ntoa (((struct sockaddr_in *)(&tmp_disp_sa))->sin_addr);
		strcpy (buffer6, "::ffff:");
		strncat (buffer6, ipv4_addr, INET_ADDRSTRLEN);

		inet_pton (AF_INET6, buffer6, &ipv6_addr);
		ipv6_addr_ptr = &ipv6_addr;

	} else if (clnt_port.length == 2 &&
	       clnt_addr.length == 16) {

		ipv6_addr_ptr = (struct in6_addr *)clnt_addr.data;

	} else {

		gdm_error (_("%s: Bad address"),
                          "gdm_xdmcp_handle_forward_query");
		goto out;
	}

	g_assert (16 == sizeof (struct in6_addr));

	gdm_xdmcp_whack_queued_managed_forwards6 ((struct sockaddr_in6 *)clnt_sa, ipv6_addr_ptr);

	((struct sockaddr_in6 *)(&disp_sa))->sin6_family = AF_INET6;

	/* Find client port number */
	memcpy (&((struct sockaddr_in6 *)(&disp_sa))->sin6_port, clnt_port.data, 2);

	/* Find client address */
	memcpy (&((struct sockaddr_in6 *)(&disp_sa))->sin6_addr.s6_addr, ipv6_addr_ptr, 16);

	gdm_debug ("gdm_xdmcp_handle_forward_query: Got FORWARD_QUERY for display: %s, port %d", inet_ntop (AF_INET6, &((struct sockaddr_in6 *)(&disp_sa))->sin6_addr, buffer6, INET6_ADDRSTRLEN), ntohs (((struct sockaddr_in6 *)(&disp_sa))->sin6_port));
   }
   else
#endif
   {
	if (clnt_port.length != 2 ||
	    clnt_addr.length != 4) {
		gdm_error (_("%s: Bad address"),
                          "gdm_xdmcp_handle_forward_query");
		goto out;
	}

	g_assert (4 == sizeof (struct in_addr));
	gdm_xdmcp_whack_queued_managed_forwards ((struct sockaddr_in *)clnt_sa, (struct in_addr *)clnt_addr.data);
	((struct sockaddr_in *)(&disp_sa))->sin_family = AF_INET;
	/* Find client port number */
	memcpy (&((struct sockaddr_in *)(&disp_sa))->sin_port, clnt_port.data, 2);
	/* Find client address */
	memcpy (&((struct sockaddr_in *)(&disp_sa))->sin_addr.s_addr, clnt_addr.data, 4);
	gdm_debug ("gdm_xdmcp_handle_forward_query: Got FORWARD_QUERY for display: %s, port %d", inet_ntoa (((struct sockaddr_in *)(&disp_sa))->sin_addr), ntohs (((struct sockaddr_in *)(&disp_sa))->sin_port));
    }


    
    
    
    /* Check with tcp_wrappers if display is allowed to access */
    if (gdm_xdmcp_host_allow (&disp_sa)) {
	    GdmForwardQuery *q;

	    q = gdm_forward_query_lookup (&disp_sa);
	    if (q != NULL)
		    gdm_forward_query_dispose (q);
	    gdm_forward_query_alloc (clnt_sa, &disp_sa);

	    gdm_xdmcp_send_willing (&disp_sa);
    }

  out:
    /* Cleanup */
    XdmcpDisposeARRAYofARRAY8 (&clnt_authlist);
    XdmcpDisposeARRAY8 (&clnt_port);
    XdmcpDisposeARRAY8 (&clnt_addr);
}


static void
#ifdef ENABLE_IPV6
gdm_xdmcp_send_willing (struct sockaddr_storage *clnt_sa)
#else
gdm_xdmcp_send_willing (struct sockaddr_in *clnt_sa)
#endif
{
    ARRAY8 status;
    XdmcpHeader header;
    static char *last_status = NULL;
    static time_t last_willing = 0;
    char *bin;
    FILE *fd;
    char *willing = gdm_get_value_string (GDM_KEY_WILLING);
    int dispperhost = gdm_get_value_int (GDM_KEY_DISPLAYS_PER_HOST);
    
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	char buffer6[INET6_ADDRSTRLEN];

	gdm_debug ("gdm_xdmcp_send_willing: Sending WILLING to %s", inet_ntop (AF_INET6, &(((struct sockaddr_in6 *)clnt_sa)->sin6_addr), buffer6, INET6_ADDRSTRLEN));
    }
    else
#endif
    {
	gdm_debug ("gdm_xdmcp_send_willing: Sending WILLING to %s", inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
    }

    if (last_willing == 0 ||
	time (NULL) - 3 > last_willing) {
	    char statusBuf[256] = "";
	    bin = ve_first_word (willing);
	    if ( ! ve_string_empty (bin) &&
		 g_access (bin, X_OK) == 0 &&
		 (fd = popen (willing, "r")) != NULL) {
		    if (fgets (statusBuf, sizeof (statusBuf), fd) != NULL &&
			! ve_string_empty (g_strstrip (statusBuf))) {
			    g_free (last_status);
			    last_status = g_strdup (statusBuf);
		    } else {
			    g_free (last_status);
			    last_status = g_strdup (sysid);
		    }
		    pclose (fd);
	    } else {
		    g_free (last_status);
		    last_status = g_strdup (sysid);
	    }
	    last_willing = time (NULL);
	    g_free (bin);
    }
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	if ( ! gdm_is_local_addr6 (&(((struct sockaddr_in6 *)clnt_sa)->sin6_addr)) &&
	    gdm_xdmcp_displays_from_host (clnt_sa) >= dispperhost) {
		/* Don't translate, this goes over the wire to servers where we
		 * don't know the charset or language, so it must be ascii */
		status.data = (CARD8 *) g_strdup_printf ("%s (Server is busy)", last_status);
	} else {
		status.data = (CARD8 *) g_strdup (last_status);
	}
     }
    else
#endif
    {
	if ( ! gdm_is_local_addr (&(((struct sockaddr_in *)clnt_sa)->sin_addr)) &&
	    gdm_xdmcp_displays_from_host (clnt_sa) >= dispperhost) {
		/* Don't translate, this goes over the wire to servers where we
		 * don't know the charset or language, so it must be ascii */
		status.data = (CARD8 *) g_strdup_printf ("%s (Server is busy)", last_status);
	} else {
		status.data = (CARD8 *) g_strdup (last_status);
	}
    }

    status.length = strlen ((char *) status.data);
    
    header.opcode = (CARD16) WILLING;
    header.length = 6 + serv_authlist.authentication.length;
    header.length += servhost.length + status.length;
    header.version = XDM_PROTOCOL_VERSION;
    XdmcpWriteHeader (&buf, &header);
    
    XdmcpWriteARRAY8 (&buf, &serv_authlist.authentication); /* Hardcoded authentication */
    XdmcpWriteARRAY8 (&buf, &servhost);
    XdmcpWriteARRAY8 (&buf, &status);
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa, (int)sizeof (struct sockaddr_in6));
    }
    else
#endif
    {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa, (int)sizeof (struct sockaddr_in));
    }

    g_free (status.data);
}

static void
#ifdef ENABLE_IPV6
gdm_xdmcp_send_unwilling (struct sockaddr_storage *clnt_sa, gint type)
#else
gdm_xdmcp_send_unwilling (struct sockaddr_in *clnt_sa, gint type)
#endif
{
    ARRAY8 status;
    XdmcpHeader header;
    static time_t last_time = 0;

    /* only send at most one packet per second,
       no harm done if we don't send it at all */
    if (last_time + 1 >= time (NULL))
	    return;
    
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	char buffer6[INET6_ADDRSTRLEN];

	gdm_debug ("gdm_xdmcp_send_unwilling: Sending UNWILLING to %s", inet_ntop (AF_INET6, &(((struct sockaddr_in6 *)clnt_sa)->sin6_addr), buffer6, INET6_ADDRSTRLEN));
	gdm_error (_("Denied XDMCP query from host %s"), buffer6); 
    }
    else
#endif
    {
	gdm_debug ("gdm_xdmcp_send_unwilling: Sending UNWILLING to %s", inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
    
	gdm_error (_("Denied XDMCP query from host %s"), inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));    
    }
    
    /* Don't translate, this goes over the wire to servers where we
     * don't know the charset or language, so it must be ascii */
    status.data = (CARD8 *) "Display not authorized to connect";
    status.length = strlen ((char *) status.data);
    
    header.opcode = (CARD16) UNWILLING;
    header.length = 4 + servhost.length + status.length;
    header.version = XDM_PROTOCOL_VERSION;
    XdmcpWriteHeader (&buf, &header);
    
    XdmcpWriteARRAY8 (&buf, &servhost);
    XdmcpWriteARRAY8 (&buf, &status);
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa, (int)sizeof (struct sockaddr_in6));
    }
    else
#endif
    {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa, (int)sizeof (struct sockaddr_in));
    }

    last_time = time (NULL);
}

static void
#ifdef ENABLE_IPV6
gdm_xdmcp_really_send_managed_forward (struct sockaddr_storage *clnt_sa,
                                      struct sockaddr_storage *origin)
#else
gdm_xdmcp_really_send_managed_forward (struct sockaddr_in *clnt_sa,
				       struct sockaddr_in *origin)
#endif
{
	ARRAY8 address;
	XdmcpHeader header;
	struct in_addr addr;

#ifdef ENABLE_IPV6
	struct in6_addr addr6;

	if (clnt_sa->ss_family == AF_INET6) {
		char buffer6[INET6_ADDRSTRLEN];

		gdm_debug ("gdm_xdmcp_really_send_managed_forward: "
                          "Sending MANAGED_FORWARD to %s",
                          inet_ntop (AF_INET6, &(((struct sockaddr_in6 *)clnt_sa)->sin6_addr), buffer6, INET6_ADDRSTRLEN));

		address.length = sizeof (struct in6_addr);
		address.data = (void *)&addr6;
		memcpy (address.data, &(((struct sockaddr_in6 *)origin)->sin6_addr), sizeof (struct in6_addr));
	}
	else
#endif
	{
		gdm_debug ("gdm_xdmcp_really_send_managed_forward: "
                          "Sending MANAGED_FORWARD to %s",
                          inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));

		address.length = sizeof (struct in_addr);
		address.data = (void *)&addr;
		memcpy (address.data, &(((struct sockaddr_in *)origin)->sin_addr), sizeof (struct in_addr));
	}

	header.opcode = (CARD16) GDM_XDMCP_MANAGED_FORWARD;
	header.length = 4 + address.length;
	header.version = GDM_XDMCP_PROTOCOL_VERSION;
	XdmcpWriteHeader (&buf, &header);

	XdmcpWriteARRAY8 (&buf, &address);
#ifdef ENABLE_IPV6
	if (clnt_sa->ss_family == AF_INET6) {
		XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa, (int)sizeof (struct sockaddr_in6));
	}
	else
#endif
	{
		XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa, (int)sizeof (struct sockaddr_in));
	}

}

static gboolean
managed_forward_handler (gpointer data)
{
	ManagedForward *mf = data;
	if (gdm_xdmcpfd > 0)
		gdm_xdmcp_really_send_managed_forward (&(mf->manager),
						       &(mf->origin));
	mf->times++;
	if (gdm_xdmcpfd <= 0 || mf->times >= 2) {
		managed_forwards = g_slist_remove (managed_forwards, mf);
		mf->handler = 0;
		/* mf freed by glib */
		return FALSE;
	}
	return TRUE;
}

static void
#ifdef ENABLE_IPV6
gdm_xdmcp_send_managed_forward (struct sockaddr_storage *clnt_sa,
                               struct sockaddr_storage *origin)
#else
gdm_xdmcp_send_managed_forward (struct sockaddr_in *clnt_sa,
				struct sockaddr_in *origin)
#endif
{
	ManagedForward *mf;

	gdm_xdmcp_really_send_managed_forward (clnt_sa, origin);

	mf = g_new0 (ManagedForward, 1);
	mf->times = 0;
#ifdef ENABLE_IPV6
	memcpy (&(mf->manager), clnt_sa, sizeof (struct sockaddr_storage));
	memcpy (&(mf->origin), origin, sizeof (struct sockaddr_storage));
#else
	memcpy (&(mf->manager), clnt_sa, sizeof (struct sockaddr_in));
	memcpy (&(mf->origin), origin, sizeof (struct sockaddr_in));
#endif

	mf->handler = g_timeout_add_full (G_PRIORITY_DEFAULT,
					  MANAGED_FORWARD_INTERVAL,
					  managed_forward_handler,
					  mf,
					  (GDestroyNotify) g_free);
	managed_forwards = g_slist_prepend (managed_forwards, mf);
}

#ifdef ENABLE_IPV6
static void
gdm_xdmcp_send_got_managed_forward6 (struct sockaddr_in6 *clnt_sa,
                                   struct in6_addr *origin)
{
	ARRAY8 address;
	XdmcpHeader header;
	struct in6_addr addr;
	char buffer6[INET6_ADDRSTRLEN];

	gdm_debug ("gdm_xdmcp_send_managed_forward: "
                  "Sending GOT_MANAGED_FORWARD to %s",
                  inet_ntop (AF_INET6, &clnt_sa->sin6_addr, buffer6, INET6_ADDRSTRLEN));

	address.length = sizeof (struct in6_addr);
	address.data = (void *)&addr;
	memcpy (address.data, origin, sizeof (struct in6_addr));

	header.opcode = (CARD16) GDM_XDMCP_GOT_MANAGED_FORWARD;
	header.length = 16 + address.length;
	header.version = GDM_XDMCP_PROTOCOL_VERSION;
	XdmcpWriteHeader (&buf, &header);

	XdmcpWriteARRAY8 (&buf, &address);
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
                   (int)sizeof (struct sockaddr_in6));
}
#endif

static void
gdm_xdmcp_send_got_managed_forward (struct sockaddr_in *clnt_sa,
				    struct in_addr *origin)
{
	ARRAY8 address;
	XdmcpHeader header;
	struct in_addr addr;

	gdm_debug ("gdm_xdmcp_send_managed_forward: "
		   "Sending GOT_MANAGED_FORWARD to %s",
		   inet_ntoa (clnt_sa->sin_addr));

	address.length = sizeof (struct in_addr);
	address.data = (void *)&addr;
	memcpy (address.data, origin, sizeof (struct in_addr));

	header.opcode = (CARD16) GDM_XDMCP_GOT_MANAGED_FORWARD;
	header.length = 4 + address.length;
	header.version = GDM_XDMCP_PROTOCOL_VERSION;
	XdmcpWriteHeader (&buf, &header);

	XdmcpWriteARRAY8 (&buf, &address);
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
		    (int)sizeof (struct sockaddr_in));
}

static void
#ifdef ENABLE_IPV6
gdm_xdmcp_handle_request (struct sockaddr_storage *clnt_sa, gint len)
#else
gdm_xdmcp_handle_request (struct sockaddr_in *clnt_sa, gint len)
#endif
{
    CARD16 clnt_dspnum;
    ARRAY16 clnt_conntyp;
    ARRAYofARRAY8 clnt_addr;
    ARRAY8 clnt_authname;
    ARRAY8 clnt_authdata;
    ARRAYofARRAY8 clnt_authorization;
    ARRAY8 clnt_manufacturer;
    gint explen;
    gint i;
    gboolean mitauth = FALSE;
    gboolean entered = FALSE;
    int maxsessions  = gdm_get_value_int (GDM_KEY_MAX_SESSIONS);
    int maxpending   = gdm_get_value_int (GDM_KEY_MAX_PENDING);
    int dispperhost  = gdm_get_value_int (GDM_KEY_DISPLAYS_PER_HOST);
    
#ifdef ENABLE_IPV6
    char buffer6[INET6_ADDRSTRLEN];

    inet_ntop (AF_INET6, &((struct sockaddr_in6 *)clnt_sa)->sin6_addr, buffer6, INET6_ADDRSTRLEN);

    if (clnt_sa->ss_family == AF_INET6) {
	gdm_debug ("gdm_xdmcp_handle_request: Got REQUEST from %s",buffer6);
    }
    else
#endif
    {
	gdm_debug ("gdm_xdmcp_handle_request: Got REQUEST from %s",
                  inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
   }
    
    /* Check with tcp_wrappers if client is allowed to access */
    if (! gdm_xdmcp_host_allow (clnt_sa)) {
#ifdef ENABLE_IPV6
	if (clnt_sa->ss_family == AF_INET6) {
	    gdm_error (_("%s: Got REQUEST from banned host %s"),
                      "gdm_xdmcp_handle_request",
                       inet_ntop (AF_INET6, &((struct sockaddr_in6 *)clnt_sa)->sin6_addr, buffer6, INET6_ADDRSTRLEN));
	}
	else
#endif
	{
	    gdm_error (_("%s: Got REQUEST from banned host %s"),
                      "gdm_xdmcp_handle_request",
                       inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
	}

	return;
    }

    gdm_xdmcp_displays_check (); /* Purge pending displays */
    
    /* Remote display number */
    if G_UNLIKELY (! XdmcpReadCARD16 (&buf, &clnt_dspnum)) {
	gdm_error (_("%s: Could not read Display Number"),
		   "gdm_xdmcp_handle_request");
	return;
    }
    
    /* We don't care about connection type. Address says it all */
    if G_UNLIKELY (! XdmcpReadARRAY16 (&buf, &clnt_conntyp)) {
	gdm_error (_("%s: Could not read Connection Type"),
		   "gdm_xdmcp_handle_request");
	return;
    }
    
    /* This is TCP/IP - we don't care */
    if G_UNLIKELY (! XdmcpReadARRAYofARRAY8 (&buf, &clnt_addr)) {
	gdm_error (_("%s: Could not read Client Address"),
		   "gdm_xdmcp_handle_request");
        XdmcpDisposeARRAY16 (&clnt_conntyp);
	return;
    }
    
    /* Read authentication type */
    if G_UNLIKELY (! XdmcpReadARRAY8 (&buf, &clnt_authname)) {
	gdm_error (_("%s: Could not read Authentication Names"),
		   "gdm_xdmcp_handle_request");
	XdmcpDisposeARRAYofARRAY8 (&clnt_addr);
	XdmcpDisposeARRAY16 (&clnt_conntyp);
	return;
    }
    
    /* Read authentication data */
    if G_UNLIKELY (! XdmcpReadARRAY8 (&buf, &clnt_authdata)) {
	gdm_error (_("%s: Could not read Authentication Data"),
		   "gdm_xdmcp_handle_request");
	XdmcpDisposeARRAYofARRAY8 (&clnt_addr);
	XdmcpDisposeARRAY16 (&clnt_conntyp);
	XdmcpDisposeARRAY8 (&clnt_authname);
	return;
    }
    
    /* Read and select from supported authorization list */
    if G_UNLIKELY (! XdmcpReadARRAYofARRAY8 (&buf, &clnt_authorization)) {
	gdm_error (_("%s: Could not read Authorization List"),
		   "gdm_xdmcp_handle_request");
	XdmcpDisposeARRAY8 (&clnt_authdata);
	XdmcpDisposeARRAYofARRAY8 (&clnt_addr);
	XdmcpDisposeARRAY16 (&clnt_conntyp);
	XdmcpDisposeARRAY8 (&clnt_authname);
	return;
    }
    
    /* libXdmcp doesn't terminate strings properly so we cheat and use strncmp () */
    for (i = 0 ; i < clnt_authorization.length ; i++)
	if (clnt_authorization.data[i].length == 18 &&
	    strncmp ((char *) clnt_authorization.data[i].data, "MIT-MAGIC-COOKIE-1", 18) == 0)
	    mitauth = TRUE;
    
    /* Manufacturer ID */
    if G_UNLIKELY (! XdmcpReadARRAY8 (&buf, &clnt_manufacturer)) {
	gdm_error (_("%s: Could not read Manufacturer ID"),
		   "gdm_xdmcp_handle_request");
	XdmcpDisposeARRAY8 (&clnt_authname);
	XdmcpDisposeARRAY8 (&clnt_authdata);
	XdmcpDisposeARRAYofARRAY8 (&clnt_addr);
	XdmcpDisposeARRAYofARRAY8 (&clnt_authorization);
	XdmcpDisposeARRAY16 (&clnt_conntyp);
	return;
    }
    
    /* Crude checksumming */
    explen = 2;		    /* Display Number */
    explen += 1+2*clnt_conntyp.length; /* Connection Type */
    explen += 1;		    /* Connection Address */
    for (i = 0 ; i < clnt_addr.length ; i++)
	explen += 2+clnt_addr.data[i].length;
    explen += 2+clnt_authname.length; /* Authentication Name */
    explen += 2+clnt_authdata.length; /* Authentication Data */
    explen += 1;		    /* Authorization Names */
    for (i = 0 ; i < clnt_authorization.length ; i++)
	explen += 2+clnt_authorization.data[i].length;
    explen += 2+clnt_manufacturer.length;
    
    if G_UNLIKELY (explen != len) {
#ifdef ENABLE_IPV6
	if (clnt_sa->ss_family == AF_INET6) {
	    gdm_error (_("%s: Failed checksum from %s"),
                      "gdm_xdmcp_handle_request",
                       inet_ntop (AF_INET6, &((struct sockaddr_in6 *)clnt_sa)->sin6_addr, buffer6, INET6_ADDRSTRLEN));
	}
	else
#endif
	{
	    gdm_error (_("%s: Failed checksum from %s"),
                      "gdm_xdmcp_handle_request",
                       inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
	}

	XdmcpDisposeARRAY8 (&clnt_authname);
	XdmcpDisposeARRAY8 (&clnt_authdata);
	XdmcpDisposeARRAY8 (&clnt_manufacturer);
	XdmcpDisposeARRAYofARRAY8 (&clnt_addr);
	XdmcpDisposeARRAYofARRAY8 (&clnt_authorization);
	XdmcpDisposeARRAY16 (&clnt_conntyp);
	return;
    }

    if G_UNLIKELY (gdm_get_value_bool (GDM_KEY_DEBUG)) {
	    char *s = g_strndup ((char *) clnt_manufacturer.data, clnt_manufacturer.length);
	    gdm_debug ("gdm_xdmcp_handle_request: xdmcp_pending=%d, MaxPending=%d, xdmcp_sessions=%d, MaxSessions=%d, ManufacturerID=%s",
		       xdmcp_pending, maxpending, xdmcp_sessions, maxsessions, ve_sure_string (s));
	    g_free (s);
    }

    /* Check if ok to manage display */
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	if (mitauth &&
	    xdmcp_sessions < maxsessions &&
	    (gdm_is_local_addr6 (&((struct sockaddr_in6 *)clnt_sa)->sin6_addr) || gdm_xdmcp_displays_from_host (clnt_sa) < dispperhost)) 

		entered = TRUE;
	} else
#endif
	{
	if (mitauth &&
	    xdmcp_sessions < maxsessions &&
	    (gdm_is_local_addr (&(((struct sockaddr_in *)clnt_sa)->sin_addr)) ||
	    gdm_xdmcp_displays_from_host (clnt_sa) < dispperhost)) 

		entered = TRUE;
	}

	if (entered) {
	    GdmHostent *he;
		he = gdm_gethostbyaddr (clnt_sa);

	    /* Check if we are already talking to this host */
	    gdm_xdmcp_display_dispose_check (he->hostname, clnt_dspnum);

	    if (xdmcp_pending >= maxpending) {
		    gdm_debug ("gdm_xdmcp_handle_request: maximum pending");
		    /* Don't translate, this goes over the wire to servers where we
		    * don't know the charset or language, so it must be ascii */
		    gdm_xdmcp_send_decline (clnt_sa, "Maximum pending servers");	
		    gdm_hostent_free (he);
	    } else {
		    /* the addrs are NOT copied */
		    gdm_xdmcp_send_accept (he /* eaten and freed */, clnt_sa, clnt_dspnum);
	    }
	} else {
	    /* Don't translate, this goes over the wire to servers where we
	    * don't know the charset or language, so it must be ascii */
	    if ( ! mitauth) {
		    gdm_xdmcp_send_decline (clnt_sa, "Only MIT-MAGIC-COOKIE-1 supported");	
	    } else if (xdmcp_sessions >= maxsessions) {
		    gdm_info ("Maximum number of open XDMCP sessions reached");
		    gdm_xdmcp_send_decline (clnt_sa, "Maximum number of open sessions reached");	
	    } else {
#ifdef ENABLE_IPV6
		if (clnt_sa->ss_family == AF_INET6)
		    gdm_info ("Maximum number of open XDMCP sessions from host %s reached", buffer6);
		else
#endif
		    gdm_info ("Maximum number of open XDMCP sessions from host %s reached",
			      inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
		    gdm_xdmcp_send_decline (clnt_sa, "Maximum number of open sessions from your host reached");	
	    }
    }

    XdmcpDisposeARRAY8 (&clnt_authname);
    XdmcpDisposeARRAY8 (&clnt_authdata);
    XdmcpDisposeARRAY8 (&clnt_manufacturer);
    XdmcpDisposeARRAYofARRAY8 (&clnt_addr);
    XdmcpDisposeARRAYofARRAY8 (&clnt_authorization);
    XdmcpDisposeARRAY16 (&clnt_conntyp);
}


static void
gdm_xdmcp_send_accept (GdmHostent *he /* eaten and freed */,
#ifdef ENABLE_IPV6
		       struct sockaddr_storage *clnt_sa,
#else
		       struct sockaddr_in *clnt_sa,
#endif
		       gint displaynum)
{
    XdmcpHeader header;
    ARRAY8 authentype;
    ARRAY8 authendata;
    ARRAY8 authname;
    ARRAY8 authdata;
    GdmDisplay *d;
    
    d = gdm_xdmcp_display_alloc (clnt_sa,
				 he /* eaten and freed */,
				 displaynum);
    
    authentype.data = (CARD8 *) 0;
    authentype.length = (CARD16) 0;
    
    authendata.data = (CARD8 *) 0;
    authendata.length = (CARD16) 0;
    
    authname.data = (CARD8 *) "MIT-MAGIC-COOKIE-1";
    authname.length = strlen ((char *) authname.data);
    
    authdata.data = (CARD8 *) d->bcookie;
    authdata.length = 16;
    
    header.version = XDM_PROTOCOL_VERSION;
    header.opcode = (CARD16) ACCEPT;
    header.length = 4;
    header.length += 2 + authentype.length;
    header.length += 2 + authendata.length;
    header.length += 2 + authname.length;
    header.length += 2 + authdata.length;
    
    XdmcpWriteHeader (&buf, &header);
    
    XdmcpWriteCARD32 (&buf, d->sessionid);
    XdmcpWriteARRAY8 (&buf, &authentype);
    XdmcpWriteARRAY8 (&buf, &authendata);
    XdmcpWriteARRAY8 (&buf, &authname);
    XdmcpWriteARRAY8 (&buf, &authdata);
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	char buffer6[INET6_ADDRSTRLEN];

	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
                   (int)sizeof (struct sockaddr_in6));

	gdm_debug ("gdm_xdmcp_send_accept: Sending ACCEPT to %s with SessionID=%ld", inet_ntop (AF_INET6, &((struct sockaddr_in6 *)clnt_sa)->sin6_addr, buffer6, INET6_ADDRSTRLEN), (long)d->sessionid);
    }
    else
#endif
    {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
                   (int)sizeof (struct sockaddr_in));

	gdm_debug ("gdm_xdmcp_send_accept: Sending ACCEPT to %s with SessionID=%ld",
                  inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr), (long)d->sessionid);
    }
    
}


static void
#ifdef ENABLE_IPV6
gdm_xdmcp_send_decline (struct sockaddr_storage *clnt_sa, const char *reason)
#else
gdm_xdmcp_send_decline (struct sockaddr_in *clnt_sa, const char *reason)
#endif
{
    XdmcpHeader header;
    ARRAY8 authentype;
    ARRAY8 authendata;
    ARRAY8 status;
    GdmForwardQuery *fq;
    
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	char buffer6[INET6_ADDRSTRLEN];

	gdm_debug ("gdm_xdmcp_send_decline: Sending DECLINE to %s",
                  inet_ntop (AF_INET6, &((struct sockaddr_in6 *)clnt_sa)->sin6_addr, buffer6, INET6_ADDRSTRLEN));
    }
    else
#endif
    {
	gdm_debug ("gdm_xdmcp_send_decline: Sending DECLINE to %s",
                  inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
    }

    
    authentype.data = (CARD8 *) 0;
    authentype.length = (CARD16) 0;
    
    authendata.data = (CARD8 *) 0;
    authendata.length = (CARD16) 0;
    
    status.data = (CARD8 *) reason;
    status.length = strlen ((char *) status.data);
    
    header.version = XDM_PROTOCOL_VERSION;
    header.opcode = (CARD16) DECLINE;
    header.length = 2 + status.length;
    header.length += 2 + authentype.length;
    header.length += 2 + authendata.length;
    XdmcpWriteHeader (&buf, &header);
    
    XdmcpWriteARRAY8 (&buf, &status);
    XdmcpWriteARRAY8 (&buf, &authentype);
    XdmcpWriteARRAY8 (&buf, &authendata);
    
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
                   (int)sizeof (struct sockaddr_in6));
    }
    else
#endif
    {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
                   (int)sizeof (struct sockaddr_in));
    }

    /* Send MANAGED_FORWARD to indicate that the connection 
     * reached some sort of resolution */
    fq = gdm_forward_query_lookup (clnt_sa);
    if (fq != NULL) {
	    gdm_xdmcp_send_managed_forward (fq->from_sa, clnt_sa);
	    gdm_forward_query_dispose (fq);
    }
}


static void 
#ifdef ENABLE_IPV6
gdm_xdmcp_handle_manage (struct sockaddr_storage *clnt_sa, gint len)
#else
gdm_xdmcp_handle_manage (struct sockaddr_in *clnt_sa, gint len)
#endif
{
    CARD32 clnt_sessid;
    CARD16 clnt_dspnum;
    ARRAY8 clnt_dspclass;
    GdmDisplay *d;
    GdmIndirectDisplay *id;
    GdmForwardQuery *fq;
    
#ifdef ENABLE_IPV6
    char buffer6[INET6_ADDRSTRLEN];

    inet_ntop (AF_INET6, &((struct sockaddr_in6 *)clnt_sa)->sin6_addr, buffer6, INET6_ADDRSTRLEN);

    if (clnt_sa->ss_family == AF_INET6) {
	gdm_debug ("gdm_xdmcp_handle_manage: Got MANAGE from %s", buffer6);
    }
    else
#endif
    {
       gdm_debug ("gdm_xdmcp_handle_manage: Got MANAGE from %s", inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
    }

    
    /* Check with tcp_wrappers if client is allowed to access */
    if (! gdm_xdmcp_host_allow (clnt_sa)) {
#ifdef ENABLE_IPV6
	if (clnt_sa->ss_family == AF_INET6) {
           gdm_error (_("%s: Got Manage from banned host %s"),
                      "gdm_xdmcp_handle_manage",
                       buffer6);
	}
	else
#endif
	{
	    gdm_error (_("%s: Got Manage from banned host %s"),
                      "gdm_xdmcp_handle_manage",
                       inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
	}

	return;
    }
    
    /* SessionID */
    if G_UNLIKELY (! XdmcpReadCARD32 (&buf, &clnt_sessid)) {
	gdm_error (_("%s: Could not read Session ID"),
		   "gdm_xdmcp_handle_manage");
	return;
    }
    
    /* Remote display number */
    if G_UNLIKELY (! XdmcpReadCARD16 (&buf, &clnt_dspnum)) {
	gdm_error (_("%s: Could not read Display Number"),
		   "gdm_xdmcp_handle_manage");
	return;
    }
    
    /* Display Class */
    if G_UNLIKELY (! XdmcpReadARRAY8 (&buf, &clnt_dspclass)) {
	gdm_error (_("%s: Could not read Display Class"),
		   "gdm_xdmcp_handle_manage");
	return;
    }

    if G_UNLIKELY (gdm_get_value_bool (GDM_KEY_DEBUG)) {
	    char *s = g_strndup ((char *) clnt_dspclass.data, clnt_dspclass.length);

#ifdef ENABLE_IPV6
	    if (clnt_sa->ss_family == AF_INET6)
		gdm_debug ("gdm_xdmcp-handle_manage: Got display=%d, SessionID=%ld Class=%s from %s", (int)clnt_dspnum, (long)clnt_sessid, ve_sure_string (s), buffer6);
	else
#endif
		gdm_debug ("gdm_xdmcp_handle_manage: Got Display=%d, SessionID=%ld Class=%s from %s",
                       (int)clnt_dspnum, (long)clnt_sessid, ve_sure_string (s), inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));

	    g_free (s);
    }
    
    
    d = gdm_xdmcp_display_lookup (clnt_sessid);
    if (d != NULL &&
        d->dispstat == XDMCP_PENDING) {

	gdm_debug ("gdm_xdmcp_handle_manage: Looked up %s", d->name);

	if (gdm_get_value_bool (GDM_KEY_INDIRECT)) {
		id = gdm_choose_indirect_lookup (clnt_sa);

		/* This was an indirect thingie and nothing was yet chosen,
		 * use a chooser */
		if (d->dispstat == XDMCP_PENDING &&
		    id != NULL &&
		    id->chosen_host == NULL) {
			d->use_chooser = TRUE;
			d->indirect_id = id->id;
		} else {
			d->indirect_id = 0;
			d->use_chooser = FALSE;
			if (id != NULL)
				gdm_choose_indirect_dispose (id);
		}
	} else {
		d->indirect_id = 0;
		d->use_chooser = FALSE;
	}

	/* this was from a forwarded query quite apparently so
	 * send MANAGED_FORWARD */
	fq = gdm_forward_query_lookup (clnt_sa);
	if (fq != NULL) {
		gdm_xdmcp_send_managed_forward (fq->from_sa, clnt_sa);
		gdm_forward_query_dispose (fq);
	}
	
	d->dispstat = XDMCP_MANAGED;
	xdmcp_sessions++;
	xdmcp_pending--;

	/* Start greeter/session */
	if G_UNLIKELY (!gdm_display_manage (d)) {
	    gdm_xdmcp_send_failed (clnt_sa, clnt_sessid);
	    XdmcpDisposeARRAY8(&clnt_dspclass);
	    return;
	}
    }
    else if G_UNLIKELY (d != NULL && d->dispstat == XDMCP_MANAGED) {
	gdm_debug ("gdm_xdmcp_handle_manage: Session id %ld already managed",
		   (long)clnt_sessid);	
    }
    else {
	gdm_debug ("gdm_xdmcp_handle_manage: Failed to look up session id %ld",
		   (long)clnt_sessid);
	gdm_xdmcp_send_refuse (clnt_sa, clnt_sessid);
    }

    XdmcpDisposeARRAY8(&clnt_dspclass);
}

static void 
#ifdef ENABLE_IPV6
gdm_xdmcp_handle_managed_forward (struct sockaddr_storage *clnt_sa, gint len)
#else
gdm_xdmcp_handle_managed_forward (struct sockaddr_in *clnt_sa, gint len)
#endif
{
	ARRAY8 clnt_address;
	GdmIndirectDisplay *id;
#ifdef ENABLE_IPV6
	char buffer6[INET6_ADDRSTRLEN];

	inet_ntop (AF_INET6, &((struct sockaddr_in6 *)clnt_sa)->sin6_addr, buffer6, INET6_ADDRSTRLEN);

	if (clnt_sa->ss_family == AF_INET6) {
               gdm_debug ("gdm_xdmcp_handle_managed_forward: "
                          "Got MANAGED_FORWARD from %s",
                          buffer6);
		/* Check with tcp_wrappers if client is allowed to access */
		if (! gdm_xdmcp_host_allow (clnt_sa)) {
			gdm_error ("%s: Got MANAGED_FORWARD from banned host %s", "gdm_xdmcp_handle_request", buffer6);
			return;
		}
	}
	else
#endif
	{
		gdm_debug ("gdm_xdmcp_handle_managed_forward: "
                          "Got MANAGED_FORWARD from %s",
                          inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));

		/* Check with tcp_wrappers if client is allowed to access */
		if (! gdm_xdmcp_host_allow (clnt_sa)) {
			gdm_error ("%s: Got MANAGED_FORWARD from banned host %s", 
			   "gdm_xdmcp_handle_request",
			   inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
			return;
		}
	}

	/* Hostname */
	if G_UNLIKELY ( ! XdmcpReadARRAY8 (&buf, &clnt_address)) {
		gdm_error (_("%s: Could not read address"),
			   "gdm_xdmcp_handle_managed_forward");
		return;
	}
#ifdef ENABLE_IPV6
	if (clnt_sa->ss_family == AF_INET6) {
		if (clnt_address.length != sizeof (struct in6_addr)) {
			gdm_error (_("%s: Could not read address"),
                                  "gdm_xdmcp_handle_managed_forward");
			XdmcpDisposeARRAY8 (&clnt_address);
			return;
		}

		id = gdm_choose_indirect_lookup_by_chosen6 (&(((struct sockaddr_in6 *)clnt_sa)->sin6_addr), (struct in6_addr *)clnt_address.data);
	}
	else
#endif
	{
		if (clnt_address.length != sizeof (struct in_addr)) {
			gdm_error (_("%s: Could not read address"),
                                  "gdm_xdmcp_handle_managed_forward");
			XdmcpDisposeARRAY8 (&clnt_address);
			return;
		}

		id = gdm_choose_indirect_lookup_by_chosen
                       (&(((struct sockaddr_in *)clnt_sa)->sin_addr), (struct in_addr *)clnt_address.data);
	}

	if (id != NULL) {
		gdm_choose_indirect_dispose (id);
	}

	/* Note: we send GOT even on not found, just in case our previous
	 * didn't get through and this was a second managed forward */
#ifdef ENABLE_IPV6
	if (clnt_sa->ss_family == AF_INET6) {
		gdm_xdmcp_send_got_managed_forward6 ((struct sockaddr_in6 *)clnt_sa, (struct in6_addr *)clnt_address.data);
	}
	else
#endif
	{
		gdm_xdmcp_send_got_managed_forward ((struct sockaddr_in *)clnt_sa, (struct in_addr *)clnt_address.data);
	}

	XdmcpDisposeARRAY8 (&clnt_address);
}

#ifdef ENABLE_IPV6
static void
gdm_xdmcp_whack_queued_managed_forwards6 (struct sockaddr_in6 *clnt_sa,
                                        struct in6_addr *origin)
{
	GSList *li;

	for (li = managed_forwards; li != NULL; li = li->next) {
		ManagedForward *mf = li->data;

		if ((memcmp (((struct sockaddr_in6 *)(&(mf->manager)))->sin6_addr.s6_addr, (clnt_sa->sin6_addr.s6_addr), sizeof (struct in6_addr)) == 0) &&
		    (memcmp (((struct sockaddr_in6 *)(&(mf->origin)))->sin6_addr.s6_addr, origin->s6_addr, sizeof (struct in6_addr)) == 0)) {
			managed_forwards = g_slist_remove_link (managed_forwards, li);
			g_slist_free_1 (li);
			g_source_remove (mf->handler);
			/* mf freed by glib */
			return;
		}
	}
}
#endif

static void
gdm_xdmcp_whack_queued_managed_forwards (struct sockaddr_in *clnt_sa,
					 struct in_addr *origin)
{
	GSList *li;

	for (li = managed_forwards; li != NULL; li = li->next) {
		ManagedForward *mf = li->data;
               if (((struct sockaddr_in *)(&mf->manager))->sin_addr.s_addr == clnt_sa->sin_addr.s_addr &&
                   ((struct sockaddr_in *)(&mf->origin))->sin_addr.s_addr == origin->s_addr) {

			managed_forwards = g_slist_remove_link (managed_forwards, li);
			g_slist_free_1 (li);
			g_source_remove (mf->handler);
			/* mf freed by glib */
			return;
		}
	}
}

static void 
#ifdef ENABLE_IPV6
gdm_xdmcp_handle_got_managed_forward (struct sockaddr_storage *clnt_sa, gint len)
#else
gdm_xdmcp_handle_got_managed_forward (struct sockaddr_in *clnt_sa, gint len)
#endif
{
	ARRAY8 clnt_address;

#ifdef ENABLE_IPV6
	char buffer6[INET6_ADDRSTRLEN];

	if (clnt_sa->ss_family == AF_INET6) {
		gdm_debug ("gdm_xdmcp_handle_got_managed_forward: "
                          "Got MANAGED_FORWARD from %s",
                          inet_ntop (AF_INET6, &((struct sockaddr_in6 *)clnt_sa)->sin6_addr, buffer6, INET6_ADDRSTRLEN));
		if (! gdm_xdmcp_host_allow (clnt_sa)) {
			gdm_error ("%s: Got GOT_MANAGED_FORWARD from banned host %s", "gdm_xdmcp_handle_request", buffer6);
			return;
		}
	}
	else
#endif
	{
		gdm_debug ("gdm_xdmcp_handle_got_managed_forward: "
                          "Got MANAGED_FORWARD from %s",
                          inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));

		/* Check with tcp_wrappers if client is allowed to access */
		if (! gdm_xdmcp_host_allow (clnt_sa)) {
			gdm_error ("%s: Got GOT_MANAGED_FORWARD from banned host %s", 
			   "gdm_xdmcp_handle_request",
			   inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
			return;
		}
	}

	/* Hostname */
	if G_UNLIKELY ( ! XdmcpReadARRAY8 (&buf, &clnt_address)) {
		gdm_error (_("%s: Could not read address"),
			   "gdm_xdmcp_handle_got_managed_forward");
		return;
	}
#ifdef ENABLE_IPV6
	if (clnt_sa->ss_family == AF_INET6) {
		if (clnt_address.length != sizeof (struct in6_addr)) {
			gdm_error (_("%s: Could not read address"),
                                  "gdm_xdmcp_handle_got_managed_forward");
			XdmcpDisposeARRAY8 (&clnt_address);
			return;
		}

		gdm_xdmcp_whack_queued_managed_forwards6 ((struct sockaddr_in6 *)clnt_sa, (struct in6_addr *)clnt_address.data);
	}
	else
#endif
	{
		if (clnt_address.length != sizeof (struct in_addr)) {
			gdm_error (_("%s: Could not read address"),
                                  "gdm_xdmcp_handle_got_managed_forward");
			XdmcpDisposeARRAY8 (&clnt_address);
			return;
		}
		gdm_xdmcp_whack_queued_managed_forwards ((struct sockaddr_in *)clnt_sa, (struct in_addr *)clnt_address.data);
	}

	XdmcpDisposeARRAY8 (&clnt_address);
}


static void
#ifdef ENABLE_IPV6
gdm_xdmcp_send_refuse (struct sockaddr_storage *clnt_sa, CARD32 sessid)
#else
gdm_xdmcp_send_refuse (struct sockaddr_in *clnt_sa, CARD32 sessid)
#endif
{
    XdmcpHeader header;
    GdmForwardQuery *fq;
    
    gdm_debug ("gdm_xdmcp_send_refuse: Sending REFUSE to %ld", (long)sessid);
    
    header.version = XDM_PROTOCOL_VERSION;
    header.opcode= (CARD16) REFUSE;
    header.length = 4;
    
    XdmcpWriteHeader (&buf, &header);  
    XdmcpWriteCARD32 (&buf, sessid);
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
                  (int)sizeof (struct sockaddr_in6));
    }
    else
#endif
    {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
                   (int)sizeof (struct sockaddr_in));
    }

    /* this was from a forwarded query quite apparently so
    * send MANAGED_FORWARD */
    fq = gdm_forward_query_lookup (clnt_sa);
    if (fq != NULL) {
	    gdm_xdmcp_send_managed_forward (fq->from_sa, clnt_sa);
	    gdm_forward_query_dispose (fq);
    }
}


static void
#ifdef ENABLE_IPV6
gdm_xdmcp_send_failed (struct sockaddr_storage *clnt_sa, CARD32 sessid)
#else
gdm_xdmcp_send_failed (struct sockaddr_in *clnt_sa, CARD32 sessid)
#endif
{
    XdmcpHeader header;
    ARRAY8 status;
    
    gdm_debug ("gdm_xdmcp_send_failed: Sending FAILED to %ld", (long)sessid);
    
    /* Don't translate, this goes over the wire to servers where we
     * don't know the charset or language, so it must be ascii */
    status.data = (CARD8 *) "Failed to start session";
    status.length = strlen ((char *) status.data);
    
    header.version = XDM_PROTOCOL_VERSION;
    header.opcode = (CARD16) FAILED;
    header.length = 6+status.length;
    
    XdmcpWriteHeader (&buf, &header);
    XdmcpWriteCARD32 (&buf, sessid);
    XdmcpWriteARRAY8 (&buf, &status);
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
                   (int)sizeof (struct sockaddr_in6));
    }
    else
#endif
    {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
                   (int)sizeof (struct sockaddr_in));
    }

}


static void
#ifdef ENABLE_IPV6
gdm_xdmcp_handle_keepalive (struct sockaddr_storage *clnt_sa, gint len)
#else
gdm_xdmcp_handle_keepalive (struct sockaddr_in *clnt_sa, gint len)
#endif
{
    CARD16 clnt_dspnum;
    CARD32 clnt_sessid;
    
#ifdef ENABLE_IPV6
    char buffer6[INET6_ADDRSTRLEN];

    if (clnt_sa->ss_family == AF_INET6) {
	gdm_debug ("gdm_xdmcp_handle_keepalive: Got KEEPALIVE from %s",
                  inet_ntop (AF_INET6, &(((struct sockaddr_in6 *)clnt_sa)->sin6_addr),
buffer6, INET6_ADDRSTRLEN));

	/* Check with tcp_wrappers if client is allowed to access */
	if (! gdm_xdmcp_host_allow (clnt_sa)) {
		gdm_error (_("%s: Got KEEPALIVE from banned host %s"),
                      "gdm_xdmcp_handle_keepalive", buffer6);
		return;

	}
    }
    else
#endif
    {
	gdm_debug ("gdm_xdmcp_handle_keepalive: Got KEEPALIVE from %s",
                  inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));

    /* Check with tcp_wrappers if client is allowed to access */
	if (! gdm_xdmcp_host_allow (clnt_sa)) {
		gdm_error (_("%s: Got KEEPALIVE from banned host %s"), 
		   "gdm_xdmcp_handle_keepalive",
		   inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr));
		return;
	}
    }
    
    /* Remote display number */
    if G_UNLIKELY (! XdmcpReadCARD16 (&buf, &clnt_dspnum)) {
	gdm_error (_("%s: Could not read Display Number"),
		   "gdm_xdmcp_handle_keepalive");
	return;
    }
    
    /* SessionID */
    if G_UNLIKELY (! XdmcpReadCARD32 (&buf, &clnt_sessid)) {
	gdm_error (_("%s: Could not read Session ID"),
		   "gdm_xdmcp_handle_keepalive");
	return;
    }
    
    gdm_xdmcp_send_alive (clnt_sa, clnt_dspnum, clnt_sessid);
}


static void
#ifdef ENABLE_IPV6
gdm_xdmcp_send_alive (struct sockaddr_storage *clnt_sa, CARD16 dspnum, CARD32 sessid)
#else
gdm_xdmcp_send_alive (struct sockaddr_in *clnt_sa, CARD16 dspnum, CARD32 sessid)
#endif
{
    XdmcpHeader header;
    GdmDisplay *d;
    int send_running = 0;
    CARD32 send_sessid = 0;
    
    d = gdm_xdmcp_display_lookup (sessid);
    if (d == NULL)
	    d = gdm_xdmcp_display_lookup_by_host (clnt_sa, dspnum);

    if (d != NULL) {
	    send_sessid = d->sessionid;
	    if (d->dispstat == XDMCP_MANAGED)
		    send_running = 1;
    }

    gdm_debug ("Sending ALIVE to %ld (running %d, sessid %ld)",
	       (long)sessid, send_running, (long)send_sessid);
    
    header.version = XDM_PROTOCOL_VERSION;
    header.opcode = (CARD16) ALIVE;
    header.length = 5;
    
    XdmcpWriteHeader (&buf, &header);
    XdmcpWriteCARD8 (&buf, send_running);
    XdmcpWriteCARD32 (&buf, send_sessid);
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
                   (int)sizeof (struct sockaddr_in6));
    }
    else
#endif
    {
	XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
                   (int)sizeof (struct sockaddr_in));
    }
}


static gboolean
gdm_xdmcp_host_allow (
#ifdef ENABLE_IPV6
                      struct sockaddr_storage *clnt_sa
#else
		      struct sockaddr_in *clnt_sa
#endif
		     )
{
#ifdef HAVE_TCPWRAPPERS
	
    /* avoids a warning, my tcpd.h file doesn't include this prototype, even
     * though the library does include the function and the manpage mentions it
     */
    extern int hosts_ctl (char *daemon, char *client_name, char *client_addr, char *client_user);

    GdmHostent *client_he;
    char *client;
    gboolean ret;
    
    /* Find client hostname */
    client_he = gdm_gethostbyaddr (clnt_sa);

    if (client_he->not_found)
	    client = "unknown";
    else {
		gdm_debug ("gdm_xdmcp_host_allow: client->hostname is %s\n", client_he->hostname);
	    client = client_he->hostname;
	}

    /* Check with tcp_wrappers if client is allowed to access */
#ifdef ENABLE_IPV6
    if (clnt_sa->ss_family == AF_INET6) {
	char buffer6[INET6_ADDRSTRLEN];

	ret = (hosts_ctl ("gdm", client, (char *) inet_ntop (AF_INET6, &((struct sockaddr_in6 *)clnt_sa)->sin6_addr, buffer6, INET6_ADDRSTRLEN), ""));
    } else
#endif
    {
       ret = (hosts_ctl ("gdm", client, inet_ntoa (((struct sockaddr_in *)clnt_sa)->sin_addr), ""));
    }

    gdm_hostent_free (client_he);

    return ret;
#else /* HAVE_TCPWRAPPERS */
    return (TRUE);
#endif /* HAVE_TCPWRAPPERS */
}


static GdmDisplay *
gdm_xdmcp_display_alloc (
#ifdef ENABLE_IPV6
			 struct sockaddr_storage *addr,
#else
			 struct sockaddr_in *addr,
#endif
			 GdmHostent *he /* eaten and freed */,
			 int displaynum)
{
    GdmDisplay *d = NULL;
    char *proxycmd = gdm_get_value_string (GDM_KEY_XDMCP_PROXY_XSERVER);
    
    d = g_new0 (GdmDisplay, 1);

    if (gdm_get_value_bool (GDM_KEY_XDMCP_PROXY) && proxycmd != NULL) {
	    d->type = TYPE_XDMCP_PROXY;
	    d->command = g_strdup (proxycmd);
	    gdm_debug ("Using proxy server for XDMCP: %s\n", d->command);
    } else {
	    d->type = TYPE_XDMCP;
    }

    d->logout_action = GDM_LOGOUT_ACTION_NONE;
    d->authfile = NULL;
    d->auths = NULL;
    d->userauth = NULL;
    d->greetpid = 0;
    d->servpid = 0;
    d->servstat = 0;
    d->sesspid = 0;
    d->slavepid = 0;
    d->attached = FALSE;
    d->dispstat = XDMCP_PENDING;
    d->sessionid = globsessid++;
    if (d->sessionid == 0)
	    d->sessionid = globsessid++;
    d->acctime = time (NULL);
    d->dispnum = displaynum;
    d->xdmcp_dispnum = displaynum;

    d->handled = TRUE;
    d->tcp_disallowed = FALSE;

    d->vt = -1;

    d->x_servers_order = -1;

    d->logged_in = FALSE;
    d->login = NULL;

    d->sleep_before_run = 0;
    if (gdm_get_value_bool (GDM_KEY_ALLOW_REMOTE_AUTOLOGIN) &&
	! ve_string_empty (gdm_get_value_string (GDM_KEY_TIMED_LOGIN))) {
	    d->timed_login_ok = TRUE;
    } else {
	    d->timed_login_ok = FALSE;
    }
    
    d->name = g_strdup_printf ("%s:%d", he->hostname,
			       displaynum);
#ifdef ENABLE_IPV6
    if (addr->ss_family == AF_INET6) {
	memcpy (&d->addr6, &((struct sockaddr_in6 *)addr)->sin6_addr, sizeof (struct in6_addr));
	d->addrtype = AF_INET6;
    }
    else
#endif
    {
	memcpy (&d->addr, &((struct sockaddr_in *)addr)->sin_addr, sizeof (struct in_addr));
	d->addrtype = AF_INET;
    }

    d->hostname = he->hostname;
    he->hostname = NULL;
    d->addrs = he->addrs;
    he->addrs = NULL;
    d->addr_count = he->addr_count;
    he->addr_count = 0;

    gdm_hostent_free (he);

    d->slave_notify_fd = -1;
    d->master_notify_fd = -1;

    d->xsession_errors_bytes = 0;
    d->xsession_errors_fd = -1;
    d->session_output_fd = -1;

    d->chooser_output_fd = -1;
    d->chooser_last_line = NULL;

    d->theme_name = NULL;

    /* Secure display with cookie */
    if G_UNLIKELY (! gdm_auth_secure_display (d))
	gdm_error ("gdm_xdmcp_display_alloc: Error setting up cookies for %s", d->name);

    if (d->type == TYPE_XDMCP_PROXY) {
	    d->parent_disp = d->name;
	    d->name = g_strdup (":-1");
	    d->dispnum = -1;

	    d->server_uid = gdm_get_gdmuid ();

	    d->parent_auth_file = d->authfile;
	    d->authfile = NULL;
    }

    displays = g_slist_append (displays, d);
    
    xdmcp_pending++;
    
    gdm_debug ("gdm_xdmcp_display_alloc: display=%s, session id=%ld, xdmcp_pending=%d ",
	       d->name, (long)d->sessionid, xdmcp_pending);
    
    return d;
}


static GdmDisplay *
gdm_xdmcp_display_lookup (CARD32 sessid)
{
    GSList *dlist = displays;
    GdmDisplay *d;
    
    if (!sessid)
	return (NULL);

    while (dlist) {
	d = (GdmDisplay *) dlist->data;
	
	if (d && d->sessionid == sessid)
	    return (d);
	
	dlist = dlist->next;
    }
    
    return (NULL);
}


static void
gdm_xdmcp_display_dispose_check (const gchar *hostname, int dspnum)
{
	GSList *dlist;

	if (hostname == NULL)
		return;

	gdm_debug ("gdm_xdmcp_display_dispose_check (%s:%d)", hostname, dspnum);

	dlist = displays;
	while (dlist != NULL) {
		GdmDisplay *d = dlist->data;

		if (d != NULL &&
		    SERVER_IS_XDMCP (d) &&
		    d->xdmcp_dispnum == dspnum &&
		    strcmp (d->hostname, hostname) == 0) {
			if (d->dispstat == XDMCP_MANAGED)
				gdm_display_unmanage (d);
			else
				gdm_display_dispose (d);

			/* restart as the list is now broken */
			dlist = displays;
		} else {
			/* just go on */
			dlist = dlist->next;
		}
	}
}


static void 
gdm_xdmcp_displays_check (void)
{
	GSList *dlist;
	time_t curtime = time (NULL);

	dlist = displays;
	while (dlist != NULL) {
		GdmDisplay *d = dlist->data;

		if (d != NULL &&
		    SERVER_IS_XDMCP (d) &&
		    d->dispstat == XDMCP_PENDING &&
		    curtime > d->acctime + gdm_get_value_int (GDM_KEY_MAX_WAIT)) {
			gdm_debug ("gdm_xdmcp_displays_check: Disposing session id %ld",
				   (long)d->sessionid);
			gdm_display_dispose (d);

			/* restart as the list is now broken */
			dlist = displays;
		} else {
			/* just go on */
			dlist = dlist->next;
		}
	}
}

static void
reconnect_to_parent (GdmDisplay *to)
{
	GError *error;
	gchar *command_argv[10];
	gchar *proxyreconnect = gdm_get_value_string (GDM_KEY_XDMCP_PROXY_RECONNECT);

	command_argv[0] = proxyreconnect;
	command_argv[1] = "--display";
	command_argv[2] = to->parent_disp;
	command_argv[3] = "--display-authfile";
	command_argv[4] = to->parent_auth_file;
	command_argv[5] = "--to";
	command_argv[6] = to->name;
	command_argv[7] = "--to-authfile";
	command_argv[8] = to->authfile;
	command_argv[9] = NULL;

	gdm_debug ("XDMCP: migrating display by running "
		   "'%s --display %s --display-authfile %s --to %s --to-authfile %s'",
		   proxyreconnect,
		   to->parent_disp, to->parent_auth_file,
		   to->name, to->authfile);

	error = NULL;
	if (!g_spawn_async (NULL, command_argv, NULL, 0, NULL, NULL, NULL, &error)) {
		gdm_error (_("%s: Failed to run "
			     "'%s --display %s --display-authfile %s --to %s --to-authfile %s': %s"),
			   "gdm_xdmcp_migrate",
			   proxyreconnect,
			   to->parent_disp, to->parent_auth_file,
			   to->name, to->authfile,
			   error->message);
		g_error_free (error);
	}
}

void
gdm_xdmcp_migrate (GdmDisplay *from, GdmDisplay *to)
{
	if (from->type != TYPE_XDMCP_PROXY ||
	    to->type   != TYPE_XDMCP_PROXY)
		return;

	g_free (to->parent_disp);
	to->parent_disp = from->parent_disp;
	from->parent_disp = NULL;

	g_free (to->parent_auth_file);
	to->parent_auth_file = from->parent_auth_file;
	from->parent_auth_file = NULL;

	reconnect_to_parent (to);
}

#else /* HAVE_LIBXDMCP */

/* Here come some empty stubs for no XDMCP support */
int
gdm_xdmcp_init  (void)
{
	gdm_error (_("%s: No XDMCP support"), "gdm_xdmcp_init");
	return FALSE;
}

void
gdm_xdmcp_run (void)
{
	gdm_error (_("%s: No XDMCP support"), "gdm_xdmcp_run");
}

void
gdm_xdmcp_close (void)
{
	gdm_error (_("%s: No XDMCP support"), "gdm_xdmcp_close");
}

void
gdm_xdmcp_migrate (GdmDisplay *from, GdmDisplay *to)
{
	gdm_error (_("%s: No XDMCP support"), "gdm_xdmcp_migrate");
}

#endif /* HAVE_LIBXDMCP */

/* EOF */