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
|
/* 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>
#include <libgnome/libgnome.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_TCPWRAPPERS
#include <tcpd.h>
#endif
#include <vicious.h>
#include "gdm.h"
#include "display.h"
#include "auth.h"
#endif /* HAVE_LIBXDMCP */
#include "misc.h"
#include "choose.h"
#include "xdmcp.h"
gint pending = 0;
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 sessions;
extern gchar *GdmLogDir;
extern gchar *GdmServAuthDir;
/* Tunables */
extern gint GdmMaxPending; /* only accept this number of pending sessions */
extern gint GdmMaxManageWait; /* Dispose sessions not responding with MANAGE after 10 secs */
extern gint GdmMaxSessions; /* Maximum number of remote sessions */
extern gint GdmPort; /* UDP port number */
extern gboolean GdmIndirect; /* Honor XDMCP_INDIRECT, i.e. choosing */
extern gint GdmMaxIndirectWait; /* Max wait between INDIRECT_QUERY and MANAGE */
extern gint GdmDispPerHost; /* Max number of displays per remote host */
extern gchar *GdmTimedLogin;
extern gboolean GdmAllowRemoteAutoLogin;
extern gchar *GdmWilling; /* The willing script */
extern gboolean GdmXdmcp; /* xdmcp enabled */
/* Local prototypes */
static gboolean gdm_xdmcp_decode_packet (GIOChannel *source,
GIOCondition cond,
gpointer data);
static void gdm_xdmcp_handle_query (struct sockaddr_in *clnt_sa,
gint len,
gint type);
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_handle_forward_query (struct sockaddr_in *clnt_sa, gint len);
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_whack_queued_managed_forwards (struct sockaddr_in *clnt_sa,
struct in_addr *origin);
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, CARD32 sessid);
static void gdm_xdmcp_send_managed_forward (struct sockaddr_in *clnt_sa,
struct sockaddr_in *origin);
static void gdm_xdmcp_send_got_managed_forward (struct sockaddr_in *clnt_sa,
struct in_addr *origin);
static gboolean gdm_xdmcp_host_allow (struct sockaddr_in *cnlt_sa);
static GdmDisplay *gdm_xdmcp_display_alloc (struct in_addr *addr,
GdmHostent *he /* eaten and freed */,
int displaynum);
static GdmDisplay *gdm_xdmcp_display_lookup (CARD32 sessid);
static void gdm_xdmcp_display_dispose_check (const gchar *name);
static void gdm_xdmcp_displays_check (void);
static int gdm_xdmcp_displays_from_host (struct in_addr *addr);
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 void gdm_forward_query_dispose (GdmForwardQuery *q);
static GSList *forward_queries = NULL;
typedef struct {
int times;
guint handler;
struct sockaddr_in manager;
struct sockaddr_in origin;
} ManagedForward;
#define MANAGED_FORWARD_INTERVAL 1500 /* 1.5 seconds */
static GList *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 }
};
static int
gdm_xdmcp_displays_from_host (struct in_addr *addr)
{
GSList *li;
int count = 0;
for (li = displays; li != NULL; li = li->next) {
GdmDisplay *disp = li->data;
if (disp->type == TYPE_XDMCP &&
memcmp (&disp->addr, addr, sizeof (struct in_addr)) == 0)
count ++;
}
return count;
}
gboolean
gdm_xdmcp_init (void)
{
struct sockaddr_in serv_sa = {0};
gchar hostbuf[1024];
struct utsname name;
if ( ! GdmXdmcp)
return TRUE;
globsessid = time (NULL);
/* Fetch and store local hostname in XDMCP friendly format */
hostbuf[1023] = '\0';
if (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 = g_strdup (hostbuf);
servhost.length = strlen (servhost.data);
initted = TRUE;
}
gdm_debug ("XDMCP: Start up on host %s, port %d", hostbuf, GdmPort);
/* Open socket for communications */
gdm_xdmcpfd = socket (AF_INET, SOCK_DGRAM, 0); /* UDP */
if (gdm_xdmcpfd < 0) {
gdm_error (_("%s: Could not create socket!"), "gdm_xdmcp_init");
GdmXdmcp = FALSE;
return FALSE;
}
serv_sa.sin_family = AF_INET;
serv_sa.sin_port = htons (GdmPort); /* UDP 177 */
serv_sa.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind (gdm_xdmcpfd, (struct sockaddr*) &serv_sa, sizeof (serv_sa)) == -1) {
gdm_error (_("%s: Could not bind to XDMCP socket!"), "gdm_xdmcp_init");
gdm_xdmcp_close ();
GdmXdmcp = 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) {
close (gdm_xdmcpfd);
gdm_xdmcpfd = -1;
}
}
static gboolean
gdm_xdmcp_decode_packet (GIOChannel *source, GIOCondition cond, gpointer data)
{
struct sockaddr_in clnt_sa;
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"
};
if (!XdmcpFill (gdm_xdmcpfd, &buf, (XdmcpNetaddr)&clnt_sa, &sa_len)) {
gdm_error (_("%s: Could not create XDMCP buffer!"),
"gdm_xdmcp_decode_packet");
return TRUE;
}
if (!XdmcpReadHeader (&buf, &header)) {
gdm_error (_("%s: Could not read XDMCP header!"),
"gdm_xdmcp_decode_packet");
return TRUE;
}
if (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)
gdm_debug ("gdm_xdmcp_decode: Received opcode %s from client %s",
opcode_names[header.opcode], inet_ntoa (clnt_sa.sin_addr));
if (header.opcode >= GDM_XDMCP_FIRST_OPCODE &&
header.opcode < GDM_XDMCP_LAST_OPCODE)
gdm_debug ("gdm_xdmcp_decode: Received opcode %s from client %s",
gdm_opcode_names[header.opcode - GDM_XDMCP_FIRST_OPCODE],
inet_ntoa (clnt_sa.sin_addr));
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_displays_check(); /* Purge pending displays */
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:
gdm_error (_("%s: Unknown opcode from host %s"),
"gdm_xdmcp_decode_packet",
inet_ntoa (clnt_sa.sin_addr));
break;
}
return TRUE;
}
static void
gdm_xdmcp_handle_query (struct sockaddr_in *clnt_sa, gint len, gint type)
{
ARRAYofARRAY8 clnt_authlist;
gint i = 0, explen = 1;
gdm_debug ("gdm_xdmcp_handle_query: Opcode %d from %s",
type, inet_ntoa (clnt_sa->sin_addr));
/* Extract array of authentication names from Xdmcp packet */
if (! 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++) {
gdm_debug ("gdm_xdmcp_handle_query: authlist: %s",
(char *)clnt_authlist.data);
explen += 2+clnt_authlist.data[i].length;
}
if (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 (GdmIndirect &&
type == INDIRECT_QUERY) {
GdmIndirectDisplay *id = gdm_choose_indirect_lookup (clnt_sa);
if (id != NULL &&
id->chosen_host != NULL) {
/* if user chose us, then just send willing */
if (gdm_is_local_addr (id->chosen_host)) {
/* 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 (gdm_is_loopback_addr (&(clnt_sa->sin_addr))) {
/* 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 in_addr *addr = list->data;
if ( ! gdm_is_loopback_addr (addr)) {
/* forward query to
* chosen host */
gdm_xdmcp_send_forward_query
(id, clnt_sa, addr,
&clnt_authlist);
}
list = list->next;
}
} else {
/* or send forward query to chosen host */
gdm_xdmcp_send_forward_query
(id, clnt_sa,
&(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);
}
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;
g_assert (id != NULL);
g_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 = 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 *
gdm_forward_query_alloc (struct sockaddr_in *mgr_sa,
struct sockaddr_in *dsp_sa)
{
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);
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));
forward_queries = g_slist_prepend (forward_queries, q);
return q;
}
static GdmForwardQuery *
gdm_forward_query_lookup (struct sockaddr_in *clnt_sa)
{
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;
if (q->dsp_sa->sin_addr.s_addr == 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 (clnt_sa->sin_addr));
gdm_forward_query_dispose (q);
}
}
g_slist_free (qlist);
gdm_debug ("gdm_forward_query_lookup: Host %s not found",
inet_ntoa (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;
gdm_debug ("gdm_forward_query_dispose: Disposing %s",
inet_ntoa (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
gdm_xdmcp_handle_forward_query (struct sockaddr_in *clnt_sa, gint len)
{
ARRAY8 clnt_addr;
ARRAY8 clnt_port;
ARRAYofARRAY8 clnt_authlist;
gint i = 0, explen = 1;
struct sockaddr_in disp_sa = {0};
/* Read display address */
if (! XdmcpReadARRAY8 (&buf, &clnt_addr)) {
gdm_error (_("%s: Could not read display address"),
"gdm_xdmcp_handle_forward_query");
return;
}
/* Read display port */
if (! 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 (! 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++) {
gdm_debug ("gdm_xdmcp_handle_forward_query: authlist: %s",
(char *)clnt_authlist.data);
explen += 2+clnt_authlist.data[i].length;
}
if (len != explen) {
gdm_error (_("%s: Error in checksum"),
"gdm_xdmcp_handle_forward_query");
goto out;
}
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
(clnt_sa, (struct in_addr *)clnt_addr.data);
disp_sa.sin_family = AF_INET;
/* Find client port number */
memcpy (&disp_sa.sin_port, clnt_port.data, 2);
/* Find client address */
memcpy (&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 (disp_sa.sin_addr), ntohs (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
gdm_xdmcp_send_willing (struct sockaddr_in *clnt_sa)
{
ARRAY8 status;
XdmcpHeader header;
static char *last_status = NULL;
static time_t last_willing = 0;
char *bin;
FILE *fd;
gdm_debug ("gdm_xdmcp_send_willing: Sending WILLING to %s", inet_ntoa (clnt_sa->sin_addr));
if (last_willing == 0 ||
time (NULL) - 3 > last_willing) {
char statusBuf[256] = "";
bin = ve_first_word (GdmWilling);
if ( ! ve_string_empty (bin) &&
access (bin, X_OK) == 0 &&
(fd = popen (GdmWilling, "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);
}
if ( ! gdm_is_local_addr (&(clnt_sa->sin_addr)) &&
gdm_xdmcp_displays_from_host (&(clnt_sa->sin_addr)) >= GdmDispPerHost) {
/* 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 = g_strdup_printf ("%s (Server is busy)", last_status);
} else {
status.data = g_strdup (last_status);
}
status.length = strlen (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);
XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
(int)sizeof (struct sockaddr_in));
g_free (status.data);
}
static void
gdm_xdmcp_send_unwilling (struct sockaddr_in *clnt_sa, gint type)
{
ARRAY8 status;
XdmcpHeader header;
gdm_debug ("gdm_xdmcp_send_unwilling: Sending UNWILLING to %s", inet_ntoa (clnt_sa->sin_addr));
gdm_error (_("Denied XDMCP query from host %s"), inet_ntoa (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 = "Display not authorized to connect";
status.length = strlen (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);
XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
(int)sizeof (struct sockaddr_in));
}
static void
gdm_xdmcp_really_send_managed_forward (struct sockaddr_in *clnt_sa,
struct sockaddr_in *origin)
{
ARRAY8 address;
XdmcpHeader header;
struct in_addr addr;
gdm_debug ("gdm_xdmcp_really_send_managed_forward: "
"Sending MANAGED_FORWARD to %s",
inet_ntoa (clnt_sa->sin_addr));
address.length = sizeof (struct in_addr);
address.data = (void *)&addr;
memcpy (address.data, &(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);
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_list_remove (managed_forwards, mf);
mf->handler = 0;
/* mf freed by glib */
return FALSE;
}
return TRUE;
}
static void
gdm_xdmcp_send_managed_forward (struct sockaddr_in *clnt_sa,
struct sockaddr_in *origin)
{
ManagedForward *mf;
gdm_xdmcp_really_send_managed_forward (clnt_sa, origin);
mf = g_new0 (ManagedForward, 1);
mf->times = 0;
memcpy (&(mf->manager), clnt_sa, sizeof (struct sockaddr_in));
memcpy (&(mf->origin), origin, sizeof (struct sockaddr_in));
mf->handler = g_timeout_add_full (G_PRIORITY_DEFAULT,
MANAGED_FORWARD_INTERVAL,
managed_forward_handler,
mf,
(GDestroyNotify) g_free);
managed_forwards = g_list_prepend (managed_forwards, mf);
}
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
gdm_xdmcp_handle_request (struct sockaddr_in *clnt_sa, gint len)
{
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;
gdm_debug ("gdm_xdmcp_handle_request: Got REQUEST from %s",
inet_ntoa (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 REQUEST from banned host %s"),
"gdm_xdmcp_handle_request",
inet_ntoa (clnt_sa->sin_addr));
return;
}
/* Remote display number */
if (! 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 (! 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 (! XdmcpReadARRAYofARRAY8 (&buf, &clnt_addr)) {
gdm_error (_("%s: Could not read Client Address"),
"gdm_xdmcp_handle_request");
XdmcpDisposeARRAY16 (&clnt_conntyp);
return;
}
/* Read authentication type */
if (! 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 (! 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 (! 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 (! strncmp (clnt_authorization.data[i].data, "MIT-MAGIC-COOKIE-1", 18))
mitauth = TRUE;
/* Manufacturer ID */
if (! 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 (explen != len) {
gdm_error (_("%s: Failed checksum from %s"),
"gdm_xdmcp_handle_request",
inet_ntoa (clnt_sa->sin_addr));
XdmcpDisposeARRAY8 (&clnt_authname);
XdmcpDisposeARRAY8 (&clnt_authdata);
XdmcpDisposeARRAY8 (&clnt_manufacturer);
XdmcpDisposeARRAYofARRAY8 (&clnt_addr);
XdmcpDisposeARRAYofARRAY8 (&clnt_authorization);
XdmcpDisposeARRAY16 (&clnt_conntyp);
return;
}
gdm_debug ("gdm_xdmcp_handle_request: pending=%d, MaxPending=%d, sessions=%d, MaxSessions=%d",
pending, GdmMaxPending, sessions, GdmMaxSessions);
/* Check if ok to manage display */
if (mitauth &&
sessions < GdmMaxSessions &&
(gdm_is_local_addr (&(clnt_sa->sin_addr)) ||
gdm_xdmcp_displays_from_host (&(clnt_sa->sin_addr)) < GdmDispPerHost)) {
char *disp;
GdmHostent *he = gdm_gethostbyaddr (&(clnt_sa->sin_addr));
disp = g_strdup_printf ("%s:%d", he->hostname, clnt_dspnum);
/* Check if we are already talking to this host */
gdm_xdmcp_display_dispose_check (disp);
g_free (disp);
if (pending >= GdmMaxPending) {
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 (sessions >= GdmMaxSessions)
gdm_xdmcp_send_decline (clnt_sa, "Maximum number of open sessions reached");
else
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 */,
struct sockaddr_in *clnt_sa,
gint displaynum)
{
XdmcpHeader header;
ARRAY8 authentype;
ARRAY8 authendata;
ARRAY8 authname;
ARRAY8 authdata;
GdmDisplay *d;
d = gdm_xdmcp_display_alloc (&(clnt_sa->sin_addr),
he /* eaten and freed */,
displaynum);
authentype.data = (CARD8 *) 0;
authentype.length = (CARD16) 0;
authendata.data = (CARD8 *) 0;
authendata.length = (CARD16) 0;
authname.data = "MIT-MAGIC-COOKIE-1";
authname.length = strlen (authname.data);
authdata.data = 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);
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 (clnt_sa->sin_addr), (long)d->sessionid);
}
static void
gdm_xdmcp_send_decline (struct sockaddr_in *clnt_sa, const char *reason)
{
XdmcpHeader header;
ARRAY8 authentype;
ARRAY8 authendata;
ARRAY8 status;
GdmForwardQuery *fq;
gdm_debug ("gdm_xdmcp_send_decline: Sending DECLINE to %s",
inet_ntoa (clnt_sa->sin_addr));
authentype.data = (CARD8 *) 0;
authentype.length = (CARD16) 0;
authendata.data = (CARD8 *) 0;
authendata.length = (CARD16) 0;
status.data = (char *)reason;
status.length = strlen (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);
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
gdm_xdmcp_handle_manage (struct sockaddr_in *clnt_sa, gint len)
{
CARD32 clnt_sessid;
CARD16 clnt_dspnum;
ARRAY8 clnt_dspclass;
GdmDisplay *d;
GdmIndirectDisplay *id;
GdmForwardQuery *fq;
gdm_debug ("gdm_xdmcp_handle_manage: Got MANAGE from %s", inet_ntoa (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 Manage from banned host %s"),
"gdm_xdmcp_handle_manage",
inet_ntoa (clnt_sa->sin_addr));
return;
}
/* SessionID */
if (! XdmcpReadCARD32 (&buf, &clnt_sessid)) {
gdm_error (_("%s: Could not read Session ID"),
"gdm_xdmcp_handle_manage");
return;
}
/* Remote display number */
if (! XdmcpReadCARD16 (&buf, &clnt_dspnum)) {
gdm_error (_("%s: Could not read Display Number"),
"gdm_xdmcp_handle_manage");
return;
}
gdm_debug ("gdm_xdmcp_handle_manage: Got Display=%d, SessionID=%ld from %s",
(int)clnt_dspnum, (long)clnt_sessid, inet_ntoa (clnt_sa->sin_addr));
/* Display Class */
if (! XdmcpReadARRAY8 (&buf, &clnt_dspclass)) {
gdm_error (_("%s: Could not read Display Class"),
"gdm_xdmcp_handle_manage");
return;
}
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 (GdmIndirect) {
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;
sessions++;
pending--;
/* Start greeter/session */
if (!gdm_display_manage (d)) {
gdm_xdmcp_send_failed (clnt_sa, clnt_sessid);
XdmcpDisposeARRAY8(&clnt_dspclass);
return;
}
}
else if (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
gdm_xdmcp_handle_managed_forward (struct sockaddr_in *clnt_sa, gint len)
{
ARRAY8 clnt_address;
GdmIndirectDisplay *id;
gdm_debug ("gdm_xdmcp_handle_managed_forward: "
"Got MANAGED_FORWARD from %s",
inet_ntoa (clnt_sa->sin_addr));
/* Hostname */
if ( ! XdmcpReadARRAY8 (&buf, &clnt_address)) {
gdm_error (_("%s: Could not read address"),
"gdm_xdmcp_handle_managed_forward");
return;
}
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
(&(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 */
gdm_xdmcp_send_got_managed_forward
(clnt_sa, (struct in_addr *)clnt_address.data);
XdmcpDisposeARRAY8 (&clnt_address);
}
static void
gdm_xdmcp_whack_queued_managed_forwards (struct sockaddr_in *clnt_sa,
struct in_addr *origin)
{
GList *li;
for (li = managed_forwards; li != NULL; li = li->next) {
ManagedForward *mf = li->data;
if (mf->manager.sin_addr.s_addr == clnt_sa->sin_addr.s_addr &&
mf->origin.sin_addr.s_addr == origin->s_addr) {
managed_forwards = g_list_remove_link (managed_forwards, li);
g_list_free_1 (li);
g_source_remove (mf->handler);
/* mf freed by glib */
return;
}
}
}
static void
gdm_xdmcp_handle_got_managed_forward (struct sockaddr_in *clnt_sa, gint len)
{
ARRAY8 clnt_address;
gdm_debug ("gdm_xdmcp_handle_got_managed_forward: "
"Got MANAGED_FORWARD from %s",
inet_ntoa (clnt_sa->sin_addr));
/* Hostname */
if ( ! XdmcpReadARRAY8 (&buf, &clnt_address)) {
gdm_error (_("%s: Could not read address"),
"gdm_xdmcp_handle_got_managed_forward");
return;
}
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
(clnt_sa, (struct in_addr *)clnt_address.data);
XdmcpDisposeARRAY8 (&clnt_address);
}
static void
gdm_xdmcp_send_refuse (struct sockaddr_in *clnt_sa, CARD32 sessid)
{
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);
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
gdm_xdmcp_send_failed (struct sockaddr_in *clnt_sa, CARD32 sessid)
{
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 = "Failed to start session";
status.length = strlen (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);
XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
(int)sizeof (struct sockaddr_in));
}
static void
gdm_xdmcp_handle_keepalive (struct sockaddr_in *clnt_sa, gint len)
{
CARD16 clnt_dspnum;
CARD32 clnt_sessid;
gdm_debug ("gdm_xdmcp_handle_keepalive: Got KEEPALIVE from %s",
inet_ntoa (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 (clnt_sa->sin_addr));
return;
}
/* Remote display number */
if (! XdmcpReadCARD16 (&buf, &clnt_dspnum)) {
gdm_error (_("%s: Could not read Display Number"),
"gdm_xdmcp_handle_keepalive");
return;
}
/* SessionID */
if (! XdmcpReadCARD32 (&buf, &clnt_sessid)) {
gdm_error (_("%s: Could not read Session ID"),
"gdm_xdmcp_handle_keepalive");
return;
}
gdm_xdmcp_send_alive (clnt_sa, clnt_sessid);
}
static void
gdm_xdmcp_send_alive (struct sockaddr_in *clnt_sa, CARD32 sessid)
{
XdmcpHeader header;
gdm_debug ("Sending ALIVE to %ld", (long)sessid);
header.version = XDM_PROTOCOL_VERSION;
header.opcode = (CARD16) ALIVE;
header.length = 5;
XdmcpWriteHeader (&buf, &header);
XdmcpWriteCARD8 (&buf, 1);
XdmcpWriteCARD32 (&buf, sessid);
XdmcpFlush (gdm_xdmcpfd, &buf, (XdmcpNetaddr)clnt_sa,
(int)sizeof (struct sockaddr_in));
}
static gboolean
gdm_xdmcp_host_allow (struct sockaddr_in *clnt_sa)
{
#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->sin_addr));
if (client_he->not_found)
client = "unknown";
else
client = client_he->hostname;
/* Check with tcp_wrappers if client is allowed to access */
ret = (hosts_ctl ("gdm", client, inet_ntoa (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 (struct in_addr *addr,
GdmHostent *he /* eaten and freed */,
int displaynum)
{
GdmDisplay *d = NULL;
d = g_new0 (GdmDisplay, 1);
d->authfile = NULL;
d->auths = NULL;
d->userauth = NULL;
d->command = NULL;
d->greetpid = 0;
d->servpid = 0;
d->servstat = 0;
d->sesspid = 0;
d->slavepid = 0;
d->type = TYPE_XDMCP;
d->console = FALSE;
d->dispstat = XDMCP_PENDING;
d->sessionid = globsessid++;
d->acctime = time (NULL);
d->dispnum = displaynum;
d->handled = TRUE;
d->vt = -1;
d->x_servers_order = -1;
d->logged_in = FALSE;
d->login = NULL;
d->sleep_before_run = 0;
if (GdmAllowRemoteAutoLogin &&
! ve_string_empty (GdmTimedLogin)) {
d->timed_login_ok = TRUE;
} else {
d->timed_login_ok = FALSE;
}
d->name = g_strdup_printf ("%s:%d", he->hostname,
displaynum);
memcpy (&d->addr, addr, sizeof (struct in_addr));
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;
/* Secure display with cookie */
if (! gdm_auth_secure_display (d))
gdm_error ("gdm_xdmcp_display_alloc: Error setting up cookies for %s", d->name);
displays = g_slist_append (displays, d);
pending++;
gdm_debug ("gdm_xdmcp_display_alloc: display=%s, session id=%ld, pending=%d ",
d->name, (long)d->sessionid, 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 *name)
{
GSList *dlist;
if (name == NULL)
return;
gdm_debug ("gdm_xdmcp_display_dispose_check (%s)", name);
dlist = displays;
while (dlist != NULL) {
GdmDisplay *d = dlist->data;
if (d != NULL &&
d->type == TYPE_XDMCP &&
strcmp (d->name, name) == 0) {
if (d->dispstat == XDMCP_MANAGED)
gdm_display_unmanage (d);
else
gdm_display_dispose (d);
/* restart as the list is now fucked */
dlist = displays;
} else {
/* just go on */
dlist = dlist->next;
}
}
}
static void
gdm_xdmcp_displays_check (void)
{
GSList *dlist;
dlist = displays;
while (dlist != NULL) {
GdmDisplay *d = dlist->data;
if (d != NULL &&
d->type == TYPE_XDMCP &&
d->dispstat == XDMCP_PENDING &&
time (NULL) > d->acctime + GdmMaxManageWait) {
gdm_debug ("gdm_xdmcp_displays_check: Disposing session id %ld",
(long)d->sessionid);
gdm_display_dispose (d);
/* restart as the list is now fucked */
dlist = displays;
} else {
/* just go on */
dlist = dlist->next;
}
}
}
#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");
}
#endif /* HAVE_LIBXDMCP */
/* EOF */
|