summaryrefslogtreecommitdiffstats
path: root/kdm/backend/xdmcp.c
blob: 2925a6bbcb32af2a99bb56a5267090e65d560bd5 (plain)
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
/*

Copyright 1988, 1998  The Open Group
Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
Copyright 2001-2004 Oswald Buddenhagen <ossi@kde.org>

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the copyright holder.

*/

/*
 * xdm - display manager daemon
 * Author: Keith Packard, MIT X Consortium
 *
 * xdmcp.c - Support for XDMCP
 */

#include <config.h>

#ifdef XDMCP

#include "dm.h"
#include "dm_error.h"
#include "dm_auth.h"
#include "dm_socket.h"

#include <sys/types.h>
#include <ctype.h>

#include <netdb.h>
#if defined(IPv6) && defined(AF_INET6)
# include <arpa/inet.h>
#endif

/*
 * Forward reference
 */
static void broadcast_respond( struct sockaddr *from, int fromlen, int length, int fd );
static void forward_respond (struct sockaddr *from, int fromlen, int length, int fd);
static void manage( struct sockaddr *from, int fromlen, int length, int fd );
static void query_respond( struct sockaddr *from, int fromlen, int length, int fd );
static void request_respond( struct sockaddr *from, int fromlen, int length, int fd );
static void send_accept( struct sockaddr *to, int tolen, CARD32 sessionID, ARRAY8Ptr authenticationName, ARRAY8Ptr authenticationData, ARRAY8Ptr authorizationName, ARRAY8Ptr authorizationData, int fd );
static void send_alive( struct sockaddr *from, int fromlen, int length, int fd );
static void send_decline( struct sockaddr *to, int tolen, ARRAY8Ptr authenticationName, ARRAY8Ptr authenticationData, ARRAY8Ptr status, int fd );
static void send_failed( struct sockaddr *from, int fromlen, const char *name, CARD32 sessionID, const char *reason, int fd );
static void send_refuse( struct sockaddr *from, int fromlen, CARD32 sessionID, int fd );
static void send_unwilling( struct sockaddr *from, int fromlen, ARRAY8Ptr authenticationName, ARRAY8Ptr status, int fd );
static void send_willing( struct sockaddr *from, int fromlen, ARRAY8Ptr authenticationName, ARRAY8Ptr status, int fd );


static XdmcpBuffer buffer;

static void
sendForward( CARD16 connectionType, ARRAY8Ptr address, char *closure )
{
#ifdef AF_INET
	struct sockaddr_in in_addr;
#endif
#if defined(IPv6) && defined(AF_INET6)
	struct sockaddr_in6 in6_addr;
#endif
#ifdef AF_DECnet
#endif
	struct sockaddr *addr;
	int addrlen;

	switch (connectionType) {
#ifdef AF_INET
	case FamilyInternet:
		addr = (struct sockaddr *)&in_addr;
		bzero( (char *)&in_addr, sizeof(in_addr) );
# ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
		in_addr.sin_len = sizeof(in_addr);
# endif
		in_addr.sin_family = AF_INET;
		in_addr.sin_port = htons( (short)XDM_UDP_PORT );
		if (address->length != 4)
			return;
		memmove( (char *)&in_addr.sin_addr, address->data, address->length );
		addrlen = sizeof(struct sockaddr_in);
		break;
#endif
#if defined(IPv6) && defined(AF_INET6)
	case FamilyInternet6:
		addr = (struct sockaddr *)&in6_addr;
		bzero( (char *)&in6_addr, sizeof(in6_addr) );
# ifdef SIN6_LEN
		in6_addr.sin6_len = sizeof(in6_addr);
# endif
		in6_addr.sin6_family = AF_INET6;
		in6_addr.sin6_port = htons( (short)XDM_UDP_PORT );
		if (address->length != 16)
			return;
		memmove( (char *)&in6_addr.sin6_addr, address->data, address->length );
		addrlen = sizeof(struct sockaddr_in6);
		break;
#endif
#ifdef AF_DECnet
	case FamilyDECnet:
#endif
	default:
		return;
	}
	XdmcpFlush( (int)closure, &buffer, (XdmcpNetaddr)addr, addrlen );
	return;
}

static void
ClientAddress( struct sockaddr *from,
               ARRAY8Ptr addr, /* return */
               ARRAY8Ptr port, /* return */
               CARD16 *type ) /* return */
{
	int length, family;
	char *data;

	data = NetaddrPort( (XdmcpNetaddr)from, &length );
	XdmcpAllocARRAY8( port, length );
	memmove( port->data, data, length );
	port->length = length;

	family = ConvertAddr( (XdmcpNetaddr)from, &length, &data );
	XdmcpAllocARRAY8( addr, length );
	memmove( addr->data, data, length );
	addr->length = length;

	*type = family;
}

static void
all_query_respond( struct sockaddr *from, int fromlen,
                   ARRAYofARRAY8Ptr authenticationNames,
                   xdmOpCode type, int fd )
{
	ARRAY8Ptr authenticationName;
	ARRAY8 status;
	ARRAY8 addr;
	CARD16 connectionType;
	int family;
	int length;

	family = ConvertAddr( (XdmcpNetaddr)from, &length, &(addr.data) );
	addr.length = length; /* convert int to short */
	Debug( "all_query_respond: conntype=%d, addr=%02[*:hhx\n",
	       family, addr.length, addr.data );
	if (family < 0)
		return;
	connectionType = family;

	if (type == INDIRECT_QUERY)
		RememberIndirectClient( &addr, connectionType );
	else
		ForgetIndirectClient( &addr, connectionType );

	authenticationName = ChooseAuthentication( authenticationNames );
	if (Willing( &addr, connectionType, authenticationName, &status, type ))
		send_willing( from, fromlen, authenticationName, &status, fd );
	else
		if (type == QUERY)
			send_unwilling( from, fromlen, authenticationName, &status, fd );
	XdmcpDisposeARRAY8( &status );
}

static void
indirect_respond( struct sockaddr *from, int fromlen, int length, int fd )
{
	ARRAYofARRAY8 queryAuthenticationNames;
	ARRAY8 clientAddress;
	ARRAY8 clientPort;
	CARD16 connectionType;
	int expectedLen;
	int i;
	XdmcpHeader header;
	int localHostAsWell;

	Debug( "<indirect> respond %d\n", length );
	if (!XdmcpReadARRAYofARRAY8( &buffer, &queryAuthenticationNames ))
		return;
	expectedLen = 1;
	for (i = 0; i < (int)queryAuthenticationNames.length; i++)
		expectedLen += 2 + queryAuthenticationNames.data[i].length;
	if (length == expectedLen) {
		ClientAddress( from, &clientAddress, &clientPort, &connectionType );
		/*
		 * set up the forward query packet
		 */
		header.version = XDM_PROTOCOL_VERSION;
		header.opcode = (CARD16)FORWARD_QUERY;
		header.length = 0;
		header.length += 2 + clientAddress.length;
		header.length += 2 + clientPort.length;
		header.length += 1;
		for (i = 0; i < (int)queryAuthenticationNames.length; i++)
			header.length += 2 + queryAuthenticationNames.data[i].length;
		XdmcpWriteHeader( &buffer, &header );
		XdmcpWriteARRAY8( &buffer, &clientAddress );
		XdmcpWriteARRAY8( &buffer, &clientPort );
		XdmcpWriteARRAYofARRAY8( &buffer, &queryAuthenticationNames );

		localHostAsWell =
			ForEachMatchingIndirectHost( &clientAddress, connectionType,
			                             sendForward, (char *)fd );

		XdmcpDisposeARRAY8( &clientAddress );
		XdmcpDisposeARRAY8( &clientPort );
		if (localHostAsWell)
			all_query_respond( from, fromlen, &queryAuthenticationNames,
			                   INDIRECT_QUERY, fd );
	} else
		Debug( "<indirect> length error got %d expect %d\n",
		       length, expectedLen );
	XdmcpDisposeARRAYofARRAY8( &queryAuthenticationNames );
}

void
ProcessRequestSocket( int fd )
{
	XdmcpHeader header;
#if defined(IPv6) && defined(AF_INET6)
	struct sockaddr_storage addr;
#else
	struct sockaddr addr;
#endif
	int addrlen = sizeof(addr);

	Debug( "ProcessRequestSocket\n" );
	bzero( (char *)&addr, sizeof(addr) );
	if (!XdmcpFill( fd, &buffer, (XdmcpNetaddr)&addr, &addrlen )) {
		Debug( "XdmcpFill failed\n" );
		return;
	}
	if (!XdmcpReadHeader( &buffer, &header )) {
		Debug( "XdmcpReadHeader failed\n" );
		return;
	}
	if (header.version != XDM_PROTOCOL_VERSION) {
		Debug( "XDMCP header version read was %d, expected %d\n",
		       header.version, XDM_PROTOCOL_VERSION );
		return;
	}
	Debug( "header: %d %d %d\n", header.version, header.opcode, header.length );
	switch (header.opcode) {
	case BROADCAST_QUERY:
		broadcast_respond( (struct sockaddr *)&addr, addrlen, header.length, fd );
		break;
	case QUERY:
		query_respond( (struct sockaddr *)&addr, addrlen, header.length, fd );
		break;
	case INDIRECT_QUERY:
		indirect_respond( (struct sockaddr *)&addr, addrlen, header.length, fd );
		break;
	case FORWARD_QUERY:
		forward_respond ((struct sockaddr *)&addr, addrlen, header.length, fd);
		break;
	case REQUEST:
		request_respond( (struct sockaddr *)&addr, addrlen, header.length, fd );
		break;
	case MANAGE:
		manage( (struct sockaddr *)&addr, addrlen, header.length, fd );
		break;
	case KEEPALIVE:
		send_alive( (struct sockaddr *)&addr, addrlen, header.length, fd );
		break;
	}
}

/*
 * respond to a request on the UDP socket.
 */

static void
direct_query_respond( struct sockaddr *from, int fromlen,
                      int length, xdmOpCode type, int fd )
{
	ARRAYofARRAY8 queryAuthenticationNames;
	int expectedLen;
	int i;

	if (!XdmcpReadARRAYofARRAY8( &buffer, &queryAuthenticationNames ))
		return;
	expectedLen = 1;
	for (i = 0; i < (int)queryAuthenticationNames.length; i++)
		expectedLen += 2 + queryAuthenticationNames.data[i].length;
	if (length == expectedLen)
		all_query_respond( from, fromlen, &queryAuthenticationNames, type, fd );
	XdmcpDisposeARRAYofARRAY8( &queryAuthenticationNames );
}

static void
query_respond( struct sockaddr *from, int fromlen, int length, int fd )
{
	Debug( "<query> respond %d\n", length );
	direct_query_respond( from, fromlen, length, QUERY, fd );
}

static void
broadcast_respond( struct sockaddr *from, int fromlen, int length, int fd )
{
	direct_query_respond( from, fromlen, length, BROADCAST_QUERY, fd );
}

/* computes an X display name */

static char *
NetworkAddressToName( CARD16 connectionType, ARRAY8Ptr connectionAddress,
                      struct sockaddr *originalAddress, CARD16 displayNumber )
{
	switch (connectionType) {
	case FamilyInternet:
#if defined(IPv6) && defined(AF_INET6)
	case FamilyInternet6:
#endif
		{
			CARD8 *data;
			struct hostent *hostent;
			char *hostname = NULL;
			char *name;
			const char *localhost;
			int multiHomed = 0;
			int type;
#if defined(IPv6) && defined(AF_INET6)
			struct addrinfo  *ai = NULL, *nai, hints;
			char  dotted[INET6_ADDRSTRLEN];

			if (connectionType == FamilyInternet6)
				type = AF_INET6;
			else
#endif
				type = AF_INET;

			data = connectionAddress->data;
			hostent = gethostbyaddr( (char *)data,
			                         connectionAddress->length, type );
			if (hostent) {
				if (sourceAddress) {
#if defined(IPv6) && defined(AF_INET6)
					bzero( &hints, sizeof(hints) );
					hints.ai_flags = AI_CANONNAME;
					if (!getaddrinfo( hostent->h_name, NULL, &hints, &ai )) {
						hostname = ai->ai_canonname;
						for (nai = ai->ai_next; nai; nai = nai->ai_next)
							if (ai->ai_protocol == nai->ai_protocol &&
							    memcmp( ai->ai_addr, nai->ai_addr,
							            ai->ai_addrlen ))
								multiHomed = 1;
					}
#else
					hostent = gethostbyname( hostent->h_name );
					if (hostent && hostent->h_addrtype == AF_INET) {
						multiHomed = hostent->h_addr_list[1] != NULL;
						hostname = hostent->h_name;
					}
#endif
				} else
					hostname = hostent->h_name;
			}

			localhost = localHostname();

			/*
			 * protect against bogus host names
			 */
			if (hostname && *hostname && *hostname != '.' && !multiHomed) {
				if (!strcmp( localhost, hostname ))
					ASPrintf( &name, "localhost:%d", displayNumber );
				else {
					if (removeDomainname) {
						char *localDot, *remoteDot;

						/* check for a common domain name.	This
						 * could reduce names by recognising common
						 * super-domain names as well, but I don't think
						 * this is as useful, and will confuse more
						 * people
						 */
						if ((localDot = strchr( localhost, '.' )) &&
						    (remoteDot = strchr( hostname, '.' )))
						{
							/* smash the name in place; it won't
							 * be needed later.
							 */
							if (!strcmp( localDot+1, remoteDot+1 ))
								*remoteDot = '\0';
						}
					}

					ASPrintf( &name, "%s:%d", hostname, displayNumber );
				}
			} else {
#if defined(IPv6) && defined(AF_INET6)
				if (multiHomed) {
					if (connectionType == FamilyInternet) {
						data = (CARD8 *)
							&((struct sockaddr_in *)originalAddress)->sin_addr;
					} else {
						data = (CARD8 *)
							&((struct sockaddr_in6 *)originalAddress)->sin6_addr;
					}
				}
				inet_ntop( type, data, dotted, sizeof(dotted) );
				ASPrintf( &name, "%s:%d", dotted, displayNumber );
#else
				if (multiHomed)
					data = (CARD8 *)
						&((struct sockaddr_in *)originalAddress)->sin_addr;
				ASPrintf( &name, "%[4|'.'hhu:%d", data, displayNumber );
#endif
			}
#if defined(IPv6) && defined(AF_INET6)
			if (ai)
				freeaddrinfo( ai );
#endif
			return name;
		}
#ifdef DNET
	case FamilyDECnet:
		return NULL;
#endif /* DNET */
	default:
		return NULL;
	}
}

/*ARGSUSED*/
static void
forward_respond ( struct sockaddr *from, int fromlen ATTR_UNUSED,
                 int length, int fd)
{
	ARRAY8 clientAddress;
	ARRAY8 clientPort;
	ARRAYofARRAY8 authenticationNames;
	struct sockaddr *client;
	int clientlen;
	int expectedLen;
	int i;

	Debug( "<forward> respond %d\n", length );
	clientAddress.length = 0;
	clientAddress.data = 0;
	clientPort.length = 0;
	clientPort.data = 0;
	authenticationNames.length = 0;
	authenticationNames.data = 0;
	if (XdmcpReadARRAY8( &buffer, &clientAddress ) &&
	    XdmcpReadARRAY8( &buffer, &clientPort ) &&
	    XdmcpReadARRAYofARRAY8( &buffer, &authenticationNames ))
	{
		expectedLen = 0;
		expectedLen += 2 + clientAddress.length;
		expectedLen += 2 + clientPort.length;
		expectedLen += 1; /* authenticationNames */
		for (i = 0; i < (int)authenticationNames.length; i++)
			expectedLen += 2 + authenticationNames.data[i].length;
		if (length == expectedLen) {
			int j;

			j = 0;
			for (i = 0; i < (int)clientPort.length; i++)
				j = j * 256 + clientPort.data[i];
			Debug( "<forward> client address (port %d) %[*hhu\n", j,
			       clientAddress.length, clientAddress.data );
			switch (from->sa_family) {
#ifdef AF_INET
			case AF_INET:
				{
					struct sockaddr_in in_addr;

					if (clientAddress.length != 4 || clientPort.length != 2)
						goto badAddress;
					bzero( (char *)&in_addr, sizeof(in_addr) );
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
					in_addr.sin_len = sizeof(in_addr);
#endif
					in_addr.sin_family = AF_INET;
					memmove( &in_addr.sin_addr, clientAddress.data, 4 );
					memmove( (char *)&in_addr.sin_port, clientPort.data, 2 );
					client = (struct sockaddr *)&in_addr;
					clientlen = sizeof(in_addr);
					all_query_respond( client, clientlen, &authenticationNames,
					                   FORWARD_QUERY, fd );
				}
				break;
#endif
#if defined(IPv6) && defined(AF_INET6)
			case AF_INET6:
				{
					struct sockaddr_in6 in6_addr;

					if (clientAddress.length != 16 || clientPort.length != 2)
						goto badAddress;
					bzero( (char *)&in6_addr, sizeof(in6_addr) );
#ifdef SIN6_LEN
					in6_addr.sin6_len = sizeof(in6_addr);
#endif
					in6_addr.sin6_family = AF_INET6;
					memmove( &in6_addr,clientAddress.data,clientAddress.length );
					memmove( (char *)&in6_addr.sin6_port, clientPort.data, 2 );
					client = (struct sockaddr *)&in6_addr;
					clientlen = sizeof(in6_addr);
					all_query_respond( client, clientlen, &authenticationNames,
					                   FORWARD_QUERY, fd );
				}
				break;
#endif
#ifdef AF_UNIX
			case AF_UNIX:
				{
					struct sockaddr_un un_addr;

					if (clientAddress.length >= sizeof(un_addr.sun_path))
						goto badAddress;
					bzero( (char *)&un_addr, sizeof(un_addr) );
					un_addr.sun_family = AF_UNIX;
					memmove( un_addr.sun_path, clientAddress.data, clientAddress.length );
					un_addr.sun_path[clientAddress.length] = '\0';
					client = (struct sockaddr *)&un_addr;
#if defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN) && !defined(__Lynx__) && defined(UNIXCONN)
					un_addr.sun_len = strlen( un_addr.sun_path );
					clientlen = SUN_LEN( &un_addr );
#else
					clientlen = sizeof(un_addr);
#endif
					all_query_respond( client, clientlen, &authenticationNames,
					                   FORWARD_QUERY, fd );
				}
				break;
#endif
#ifdef AF_CHAOS
			case AF_CHAOS:
				goto badAddress;
#endif
#ifdef AF_DECnet
			case AF_DECnet:
				goto badAddress;
#endif
			}
		} else
			Debug( "<forward> length error got %d expect %d\n", length, expectedLen );
	}
  badAddress:
	XdmcpDisposeARRAY8( &clientAddress );
	XdmcpDisposeARRAY8( &clientPort );
	XdmcpDisposeARRAYofARRAY8( &authenticationNames );
}

static ARRAY8 Hostname;

static void
send_willing( struct sockaddr *from, int fromlen,
             ARRAY8Ptr authenticationName, ARRAY8Ptr status, int fd )
{
	XdmcpHeader header;

	Debug( "send <willing> %.*s %.*s\n", authenticationName->length,
	       authenticationName->data,
	       status->length,
	       status->data );
	header.version = XDM_PROTOCOL_VERSION;
	header.opcode = (CARD16)WILLING;
	header.length =
		6 + authenticationName->length + Hostname.length + status->length;
	XdmcpWriteHeader( &buffer, &header );
	XdmcpWriteARRAY8( &buffer, authenticationName );
	XdmcpWriteARRAY8( &buffer, &Hostname );
	XdmcpWriteARRAY8( &buffer, status );
	XdmcpFlush( fd, &buffer, (XdmcpNetaddr)from, fromlen );
}

static void
send_unwilling( struct sockaddr *from, int fromlen,
               ARRAY8Ptr authenticationName, ARRAY8Ptr status, int fd )
{
	XdmcpHeader header;

	Debug( "send <unwilling> %.*s %.*s\n", authenticationName->length,
	       authenticationName->data,
	       status->length,
	       status->data );
	header.version = XDM_PROTOCOL_VERSION;
	header.opcode = (CARD16)UNWILLING;
	header.length = 4 + Hostname.length + status->length;
	XdmcpWriteHeader( &buffer, &header );
	XdmcpWriteARRAY8( &buffer, &Hostname );
	XdmcpWriteARRAY8( &buffer, status );
	XdmcpFlush( fd, &buffer, (XdmcpNetaddr)from, fromlen );
}

static unsigned long globalSessionID;

#define NextSessionID() (++globalSessionID)

void init_session_id( void )
{
	/* Set randomly so we are unlikely to reuse id's from a previous
	 * incarnation so we don't say "Alive" to those displays.
	 * Start with low digits 0 to make debugging easier.
	 */
	globalSessionID = (time( (Time_t *)0 ) & 0x7fff) * 16000;

	Hostname.data = (char *)localHostname();
	Hostname.length = strlen( Hostname.data );
}

static ARRAY8 outOfMemory = { (CARD16)13, (CARD8Ptr)"Out of memory" };
static ARRAY8 noValidAddr = { (CARD16)16, (CARD8Ptr)"No valid address" };
static ARRAY8 noValidAuth = { (CARD16)22, (CARD8Ptr)"No valid authorization" };
static ARRAY8 noAuthentic = { (CARD16)29, (CARD8Ptr)"XDM has no authentication key" };

static void
request_respond( struct sockaddr *from, int fromlen, int length, int fd )
{
	CARD16 displayNumber;
	ARRAY16 connectionTypes;
	ARRAYofARRAY8 connectionAddresses;
	ARRAY8 authenticationName;
	ARRAY8 authenticationData;
	ARRAYofARRAY8 authorizationNames;
	ARRAY8 manufacturerDisplayID;
	ARRAY8Ptr reason = 0;
	int expectlen;
	int i, j;
	struct protoDisplay  *pdpy;
	ARRAY8 authorizationName, authorizationData;
	ARRAY8Ptr connectionAddress;

	Debug( "<request> respond %d\n", length );
	connectionTypes.data = 0;
	connectionAddresses.data = 0;
	authenticationName.data = 0;
	authenticationData.data = 0;
	authorizationNames.data = 0;
	authorizationName.length = 0;
	authorizationData.length = 0;
	manufacturerDisplayID.data = 0;
	if (XdmcpReadCARD16( &buffer, &displayNumber ) &&
	    XdmcpReadARRAY16( &buffer, &connectionTypes ) &&
	    XdmcpReadARRAYofARRAY8( &buffer, &connectionAddresses ) &&
	    XdmcpReadARRAY8( &buffer, &authenticationName ) &&
	    XdmcpReadARRAY8( &buffer, &authenticationData ) &&
	    XdmcpReadARRAYofARRAY8( &buffer, &authorizationNames ) &&
	    XdmcpReadARRAY8( &buffer, &manufacturerDisplayID ))
	{
		expectlen = 0;
		expectlen += 2; /* displayNumber */
		expectlen += 1 + 2 * connectionTypes.length; /* connectionTypes */
		expectlen += 1; /* connectionAddresses */
		for (i = 0; i < (int)connectionAddresses.length; i++)
			expectlen += 2 + connectionAddresses.data[i].length;
		expectlen += 2 + authenticationName.length; /* authenticationName */
		expectlen += 2 + authenticationData.length; /* authenticationData */
		expectlen += 1; /* authoriationNames */
		for (i = 0; i < (int)authorizationNames.length; i++)
			expectlen += 2 + authorizationNames.data[i].length;
		expectlen += 2 + manufacturerDisplayID.length; /* displayID */
		if (expectlen != length) {
			Debug( "<request> length error got %d expect %d\n",
			       length, expectlen );
			goto abort;
		}
		if (connectionTypes.length == 0 ||
		    connectionAddresses.length != connectionTypes.length)
		{
			reason = &noValidAddr;
			pdpy = 0;
			goto decline;
		}
		pdpy = FindProtoDisplay( (XdmcpNetaddr)from, fromlen, displayNumber );
		if (!pdpy) {

			/* Check this Display against the Manager's policy */
			reason = Accept( from, fromlen, displayNumber );
			if (reason)
				goto decline;

			/* Check the Display's stream services against Manager's policy */
			i = SelectConnectionTypeIndex( &connectionTypes,
			                               &connectionAddresses );
			if (i < 0) {
				reason = &noValidAddr;
				goto decline;
			}

			/* The Manager considers this a new session */
			connectionAddress = &connectionAddresses.data[i];
			pdpy = NewProtoDisplay( (XdmcpNetaddr)from, fromlen, displayNumber,
			                        connectionTypes.data[i], connectionAddress,
			                        NextSessionID() );
			Debug( "NewProtoDisplay %p\n", pdpy );
			if (!pdpy) {
				reason = &outOfMemory;
				goto decline;
			}
		}
		if (authorizationNames.length == 0)
			j = 0;
		else
			j = SelectAuthorizationTypeIndex( &authenticationName,
			                                  &authorizationNames );
		if (j < 0) {
			reason = &noValidAuth;
			goto decline;
		}
		if (!CheckAuthentication( pdpy,
		                          &manufacturerDisplayID,
		                          &authenticationName,
		                          &authenticationData ))
		{
			reason = &noAuthentic;
			goto decline;
		}
		if (j < (int)authorizationNames.length) {
			Xauth *auth;
			SetProtoDisplayAuthorization( pdpy,
			                              (unsigned short)authorizationNames.data[j].length,
			                              (char *)authorizationNames.data[j].data );
			auth = pdpy->xdmcpAuthorization;
			if (!auth)
				auth = pdpy->fileAuthorization;
			if (auth) {
				authorizationName.length = auth->name_length;
				authorizationName.data = (CARD8Ptr) auth->name;
				authorizationData.length = auth->data_length;
				authorizationData.data = (CARD8Ptr) auth->data;
			}
		}
		if (pdpy) {
			send_accept( from, fromlen, pdpy->sessionID,
			             &authenticationName,
			             &authenticationData,
			             &authorizationName,
			             &authorizationData, fd );
		} else {
  decline:
			send_decline( from, fromlen, &authenticationName,
			              &authenticationData,
			              reason, fd );
			if (pdpy)
				DisposeProtoDisplay( pdpy );
		}
	}
  abort:
	XdmcpDisposeARRAY16( &connectionTypes );
	XdmcpDisposeARRAYofARRAY8( &connectionAddresses );
	XdmcpDisposeARRAY8( &authenticationName );
	XdmcpDisposeARRAY8( &authenticationData );
	XdmcpDisposeARRAYofARRAY8( &authorizationNames );
	XdmcpDisposeARRAY8( &manufacturerDisplayID );
}

static void
send_accept( struct sockaddr *to, int tolen, CARD32 sessionID,
            ARRAY8Ptr authenticationName, ARRAY8Ptr authenticationData,
            ARRAY8Ptr authorizationName, ARRAY8Ptr authorizationData,
            int fd )
{
	XdmcpHeader header;

	Debug( "<accept> session ID %ld\n", (long)sessionID );
	header.version = XDM_PROTOCOL_VERSION;
	header.opcode = (CARD16)ACCEPT;
	header.length = 4; /* session ID */
	header.length += 2 + authenticationName->length;
	header.length += 2 + authenticationData->length;
	header.length += 2 + authorizationName->length;
	header.length += 2 + authorizationData->length;
	XdmcpWriteHeader( &buffer, &header );
	XdmcpWriteCARD32( &buffer, sessionID );
	XdmcpWriteARRAY8( &buffer, authenticationName );
	XdmcpWriteARRAY8( &buffer, authenticationData );
	XdmcpWriteARRAY8( &buffer, authorizationName );
	XdmcpWriteARRAY8( &buffer, authorizationData );
	XdmcpFlush( fd, &buffer, (XdmcpNetaddr)to, tolen );
}

static void
send_decline( struct sockaddr *to, int tolen,
             ARRAY8Ptr authenticationName, ARRAY8Ptr authenticationData,
             ARRAY8Ptr status, int fd )
{
	XdmcpHeader header;

	Debug( "<decline> %.*s\n", status->length, status->data );
	header.version = XDM_PROTOCOL_VERSION;
	header.opcode = (CARD16)DECLINE;
	header.length = 0;
	header.length += 2 + status->length;
	header.length += 2 + authenticationName->length;
	header.length += 2 + authenticationData->length;
	XdmcpWriteHeader( &buffer, &header );
	XdmcpWriteARRAY8( &buffer, status );
	XdmcpWriteARRAY8( &buffer, authenticationName );
	XdmcpWriteARRAY8( &buffer, authenticationData );
	XdmcpFlush( fd, &buffer, (XdmcpNetaddr)to, tolen );
}

static void
manage( struct sockaddr *from, int fromlen, int length, int fd )
{
	CARD32 sessionID;
	CARD16 displayNumber;
	ARRAY8 displayClass;
	int expectlen;
	struct protoDisplay *pdpy;
	struct display *d;
	char *name = NULL;
	char *class2 = NULL;
	XdmcpNetaddr from_save;
	ARRAY8 clientAddress, clientPort;
	CARD16 connectionType;

	Debug( "<manage> %d\n", length );
	displayClass.data = 0;
	displayClass.length = 0;
	if (XdmcpReadCARD32( &buffer, &sessionID ) &&
	    XdmcpReadCARD16( &buffer, &displayNumber ) &&
	    XdmcpReadARRAY8( &buffer, &displayClass ))
	{
		expectlen = 4 + /* session ID */
					2 + /* displayNumber */
					2 + displayClass.length; /* displayClass */
		if (expectlen != length) {
			Debug( "<manage> length error got %d expect %d\n", length, expectlen );
			goto abort;
		}
		pdpy = FindProtoDisplay( (XdmcpNetaddr)from, fromlen, displayNumber );
		Debug( "<manage> session ID %ld, pdpy %p\n", (long)sessionID, pdpy );
		if (!pdpy || pdpy->sessionID != sessionID) {
			/*
			 * We may have already started a session for this display
			 * but it hasn't seen the response in the form of an
			 * XOpenDisplay() yet. So check if it is in the list of active
			 * displays, and if so check that the session id's match.
			 * If all this is true, then we have a duplicate request that
			 * can be ignored.
			 */
			if (!pdpy &&
			    (d = FindDisplayByAddress( (XdmcpNetaddr)from, fromlen,
			                               displayNumber )) &&
			    d->sessionID == sessionID)
			{
					Debug( "manage: got duplicate pkt, ignoring\n" );
					goto abort;
			}
			Debug( "session ID %ld refused\n", (long)sessionID );
			if (pdpy)
				Debug( "existing session ID %ld\n", (long)pdpy->sessionID );
			send_refuse( from, fromlen, sessionID, fd );
		} else {
			name = NetworkAddressToName( pdpy->connectionType,
			                             &pdpy->connectionAddress,
			                             from,
			                             pdpy->displayNumber );
			if (!name) {
				Debug( "could not compute display name\n" );
				send_failed( from, fromlen, "(no name)", sessionID,
				             "out of memory", fd );
				goto abort;
			}
			Debug( "computed display name: %s\n", name );
			if ((d = FindDisplayByName( name ))) {
				Debug( "terminating active session for %s\n", d->name );
				StopDisplay( d );
			}
			if (displayClass.length) {
				if (!StrNDup( &class2, (char *)displayClass.data,
				              displayClass.length ))
				{
					send_failed( from, fromlen, name, sessionID,
					             "out of memory", fd );
					goto abort;
				}
			}
			if (!(from_save = (XdmcpNetaddr)Malloc( fromlen ))) {
				send_failed( from, fromlen, name, sessionID,
				             "out of memory", fd );
				goto abort;
			}
			memmove( from_save, from, fromlen );
			if (!(d = NewDisplay( name ))) {
				free( (char *)from_save );
				send_failed( from, fromlen, name, sessionID,
				             "out of memory", fd );
				goto abort;
			}
			d->class2 = class2;
			class2 = 0;
			d->displayType = dForeign | dTransient | dFromXDMCP;
			d->sessionID = pdpy->sessionID;
			d->from.data = (unsigned char *)from_save;
			d->from.length = fromlen;
			d->displayNumber = pdpy->displayNumber;
			ClientAddress( from, &clientAddress, &clientPort,
			               &connectionType );
			d->useChooser = 0;
			d->xdmcpFd = fd;
			if (IsIndirectClient( &clientAddress, connectionType )) {
				Debug( "IsIndirectClient\n" );
				ForgetIndirectClient( &clientAddress, connectionType );
				if (UseChooser( &clientAddress, connectionType )) {
					d->useChooser = 1;
					Debug( "use chooser for %s\n", d->name );
				}
			}
			d->clientAddr = clientAddress;
			d->connectionType = connectionType;
			XdmcpDisposeARRAY8( &clientPort );
			if (pdpy->fileAuthorization) {
				d->authorizations = (Xauth **)Malloc( sizeof(Xauth *) );
				if (!d->authorizations) {
					free( (char *)from_save );
					free( (char *)d );
					send_failed( from, fromlen, name, sessionID,
					             "out of memory", fd );
					goto abort;
				}
				d->authorizations[0] = pdpy->fileAuthorization;
				d->authNum = 1;
				pdpy->fileAuthorization = 0;
			}
			DisposeProtoDisplay( pdpy );
			Debug( "starting display %s,%s\n", d->name, d->class2 );
			if (LoadDisplayResources( d ) < 0) {
				LogError( "Unable to read configuration for display %s; "
				          "stopping it.\n", d->name );
				StopDisplay( d );
			} else
				StartDisplay( d );
			CloseGetter();
		}
	}
abort:
	XdmcpDisposeARRAY8( &displayClass );
	if (name)
		free( (char *)name );
	if (class2)
		free( (char *)class2 );
}

void
SendFailed( struct display *d, const char *reason )
{
	Debug( "display start failed, sending <failed>\n" );
	send_failed( (struct sockaddr *)(d->from.data), d->from.length, d->name,
	             d->sessionID, reason, d->xdmcpFd );
}

static void
send_failed( struct sockaddr *from, int fromlen,
            const char *name, CARD32 sessionID, const char *reason, int fd )
{
	char buf[360];
	XdmcpHeader header;
	ARRAY8 status;

	sprintf( buf, "Session %ld failed for display %.260s: %s",
	         (long)sessionID, name, reason );
	Debug( "send_failed(%\"s)\n", buf );
	status.length = strlen( buf );
	status.data = (CARD8Ptr) buf;
	header.version = XDM_PROTOCOL_VERSION;
	header.opcode = (CARD16)FAILED;
	header.length = 6 + status.length;
	XdmcpWriteHeader( &buffer, &header );
	XdmcpWriteCARD32( &buffer, sessionID );
	XdmcpWriteARRAY8( &buffer, &status );
	XdmcpFlush( fd, &buffer, (XdmcpNetaddr)from, fromlen );
}

static void
send_refuse( struct sockaddr *from, int fromlen, CARD32 sessionID, int fd )
{
	XdmcpHeader header;

	Debug( "send <refuse> %ld\n", (long)sessionID );
	header.version = XDM_PROTOCOL_VERSION;
	header.opcode = (CARD16)REFUSE;
	header.length = 4;
	XdmcpWriteHeader( &buffer, &header );
	XdmcpWriteCARD32( &buffer, sessionID );
	XdmcpFlush( fd, &buffer, (XdmcpNetaddr)from, fromlen );
}

static void
send_alive( struct sockaddr *from, int fromlen, int length, int fd )
{
	CARD32 sessionID;
	CARD16 displayNumber;
	struct display *d;
	XdmcpHeader header;
	CARD8 sendRunning;
	CARD32 sendSessionID;

	Debug( "send <alive>\n" );
	if (XdmcpReadCARD16( &buffer, &displayNumber ) &&
	    XdmcpReadCARD32( &buffer, &sessionID ))
	{
		if (length == 6) {
			if (!(d = FindDisplayBySessionID( sessionID )))
				d = FindDisplayByAddress( (XdmcpNetaddr)from, fromlen,
				                          displayNumber );
			sendRunning = 0;
			sendSessionID = 0;
			if (d && d->status == running) {
				if (d->sessionID == sessionID)
					sendRunning = 1;
				sendSessionID = d->sessionID;
			}
			header.version = XDM_PROTOCOL_VERSION;
			header.opcode = (CARD16)ALIVE;
			header.length = 5;
			Debug( "<alive>: %d %ld\n", sendRunning, (long)sendSessionID );
			XdmcpWriteHeader( &buffer, &header );
			XdmcpWriteCARD8( &buffer, sendRunning );
			XdmcpWriteCARD32( &buffer, sendSessionID );
			XdmcpFlush( fd, &buffer, (XdmcpNetaddr)from, fromlen );
		}
	}
}

char *
NetworkAddressToHostname( CARD16 connectionType, ARRAY8Ptr connectionAddress )
{
	switch (connectionType) {
	case FamilyInternet:
#if defined(IPv6) && defined(AF_INET6)
	case FamilyInternet6:
#endif
		{
			struct hostent *he;
			char *myDot, *name, *lname;
			int af_type;
#if defined(IPv6) && defined(AF_INET6)
			char dotted[INET6_ADDRSTRLEN];

			if (connectionType == FamilyInternet6)
				af_type = AF_INET6;
			else
#endif
				af_type = AF_INET;

			he = gethostbyaddr( (char *)connectionAddress->data,
			                    connectionAddress->length, af_type );
			if (he) {
#if defined(IPv6) && defined(AF_INET6)
				struct addrinfo *ai, *nai;
				if (!getaddrinfo( he->h_name, NULL, NULL, &ai )) {
					for (nai = ai; nai; nai = nai->ai_next) {
						if (af_type == nai->ai_family &&
						    !memcmp( nai->ai_family == AF_INET ?
						             (char *)&((struct sockaddr_in *)nai->ai_addr)->sin_addr :
						             (char *)&((struct sockaddr_in6 *)nai->ai_addr)->sin6_addr,
						             connectionAddress->data,
						             connectionAddress->length ))
						{
							freeaddrinfo( ai );
							goto oki;
						}
					}
					freeaddrinfo( ai );
#else
				if ((he = gethostbyname( he->h_name )) &&
				    he->h_addrtype == AF_INET)
				{
					int i;
					for (i = 0; he->h_addr_list[i]; i++)
						if (!memcmp( he->h_addr_list[i],
						             connectionAddress->data, 4 ))
							goto oki;
#endif
					LogError( "DNS spoof attempt or misconfigured resolver.\n" );
				}
				goto gotnone;
			  oki:
				if (StrDup( &name, he->h_name ) &&
				    !strchr( name, '.' ) &&
				    (myDot = strchr( localHostname(), '.' )))
				{
					if (ASPrintf( &lname, "%s%s", name, myDot )) {
#if defined(IPv6) && defined(AF_INET6)
						if (!getaddrinfo( lname, NULL, NULL, &ai )) {
							for (nai = ai; nai; nai = nai->ai_next) {
								if (af_type == nai->ai_family &&
								    !memcmp( nai->ai_family == AF_INET ?
								             (char *)&((struct sockaddr_in *)nai->ai_addr)->sin_addr :
								             (char *)&((struct sockaddr_in6 *)nai->ai_addr)->sin6_addr,
								             connectionAddress->data,
								             connectionAddress->length ))
								{
									freeaddrinfo( ai );
									free( name );
									return lname;
								}
							}
							freeaddrinfo( ai );
						}
#else
						if ((he = gethostbyname( lname )) &&
						    he->h_addrtype == AF_INET)
						{
							int i;
							for (i = 0; he->h_addr_list[i]; i++)
								if (!memcmp( he->h_addr_list[i],
								             connectionAddress->data, 4 ))
								{
									free( name );
									return lname;
								}
						}
#endif
						free( lname );
					}
				}
			} else {
			  gotnone:
				/* can't get name, so use emergency fallback */
#if defined(IPv6) && defined(AF_INET6)
				inet_ntop( af_type, connectionAddress->data,
				           dotted, sizeof(dotted) );
				StrDup( &name, dotted );
#else
				ASPrintf( &name, "%[4|'.'hhu", connectionAddress->data );
#endif
				LogWarn( "Cannot convert Internet address %s to host name\n",
				         name );
			}
			return name;
		}
#ifdef DNET
	case FamilyDECnet:
		break;
#endif /* DNET */
	default:
		break;
	}
	return 0;
}

#endif /* XDMCP */