/
io.c
1317 lines (1182 loc) · 35.9 KB
/
io.c
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
/* $Xorg: io.c,v 1.4 2001/02/09 02:05:45 xorgcvs Exp $ */
/*
Copyright "1986-1997, 1998 The Open Group
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 the following permission notice
shall be included in all copies 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 NON-INFRINGEMENT. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER SIABILITIY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN
CONNNECTION WITH THE SOFTWARE OR THE USE OF OTHER DEALINGS IN
THE SOFTWARE.
Except as contained in this notice, the name of The Open Group
shall not be used in advertising or otherwise to promote the use
or other dealings in this Software without prior written
authorization from The Open Group.
X Window System is a trademark of The Open Group.
*/
/* $XFree86: xc/programs/xfwp/io.c,v 1.10 2001/07/25 15:05:22 dawes Exp $ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <X11/Xos.h> /* Needed here for SunOS */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <assert.h>
#include <X11/X.h>
#include <X11/Xproto.h>
#include <X11/Xfuncs.h>
#include <X11/ICE/ICElib.h>
#include "xfwp.h"
#include "misc.h"
#include "pm.h"
#include "transport.h"
#include "io.h"
/*
* Error messages returned to clients who are denied access
*/
static const char * server_reason[2] = {
"Authentication rejected",
"permission denied"
};
static void
RemoveFDFromServerListenArray (
int fd_counter,
fd_set * rinit,
int num_servers)
{
/*
* File descriptor fd_counter had a read failure. If this fd
* is associated with an Xserver, then we assume the Xserver is
* un-usable (e.g. has died) thus we should no longer listen for
* for connections on the client listen port associated with
* this Xserver.
*/
int i;
for (i = 0; i < num_servers; i++)
{
if (server_array[i] != NULL &&
server_array[i]->server_fd == fd_counter)
{
FD_CLR (server_array[i]->client_listen_fd, rinit);
(void) close (server_array[i]->client_listen_fd);
if (server_array[i]->x_server_hostport)
free (server_array[i]->x_server_hostport);
if (server_array[i]->listen_port_string)
free (server_array[i]->listen_port_string);
free (server_array[i]);
server_array[i] = NULL;
break;
}
}
}
static void
doProcessWritables(
int fd_counter,
fd_set * rinit,
fd_set * winit)
{
int bytes_written;
int remainder;
/*
* start off by writing from the selected fd to its connection
* partner
*/
if (client_conn_array[fd_counter]->wbytes)
{
/*
* See how much you manage to write
*/
bytes_written = write (fd_counter,
client_conn_array[fd_counter]->writebuf,
client_conn_array[fd_counter]->wbytes);
/*
* handle some common error conditions
*/
if (bytes_written == -1)
{
/*
* no process attached to the other end of this socket
*/
if (errno == EPIPE)
{
(void) fprintf (stderr, "write error - EPIPE\n");
perror("socket write");
}
/*
* clean up
*/
FD_CLR(fd_counter, rinit);
FD_CLR(fd_counter, winit);
(void) close (fd_counter);
if (client_conn_array[fd_counter]->conn_to != -1)
{
FD_CLR(client_conn_array[fd_counter]->conn_to, rinit);
FD_CLR(client_conn_array[fd_counter]->conn_to, winit);
(void) close (client_conn_array[fd_counter]->conn_to);
}
client_conn_array[client_conn_array[fd_counter]->conn_to]->conn_to
= -1;
client_conn_array[fd_counter]->conn_to = -1;
if (client_conn_array[fd_counter]->source)
free(client_conn_array[fd_counter]->source);
if (client_conn_array[fd_counter]->destination)
free(client_conn_array[fd_counter]->destination);
free(client_conn_array[fd_counter]);
client_conn_array[fd_counter] = NULL;
return;
} else
{
/*
* no errors on write, but did you write everything in the buffer?
*/
remainder = client_conn_array[fd_counter]->wbytes - bytes_written;
if (remainder)
{
/*
* move any remainder to front of writebuffer and adjust
* writebuf byte counter
*/
bcopy(client_conn_array[fd_counter]->writebuf + bytes_written,
client_conn_array[fd_counter]->writebuf,
remainder);
client_conn_array[fd_counter]->wbytes = remainder;
} else
/*
* writebuffer *must* be empty, so zero byte counter
*/
client_conn_array[fd_counter]->wbytes = 0;
/*
* take this opportunity to get more read data, if any present
*/
if ((client_conn_array[fd_counter]->conn_to != -1) &&
(client_conn_array[client_conn_array[fd_counter]->conn_to]->rbytes))
doCopyFromTo(client_conn_array[fd_counter]->conn_to,
fd_counter,
rinit,
winit);
/*
* If the above operation didn't put anything on the writebuffer,
* and the readables handler marked the fd ready to close, then
* clean up and close the connection; otherwise, simply clear the
* fd_set mask for this fd
*/
if (client_conn_array[fd_counter]->wbytes == 0)
{
if (client_conn_array[fd_counter]->wclose == 1)
{
FD_CLR(fd_counter, rinit);
client_conn_array[fd_counter]->conn_to = -1;
client_conn_array[fd_counter]->wclose = 0;
(void) close (fd_counter);
if (client_conn_array[fd_counter]->source)
free(client_conn_array[fd_counter]->source);
if (client_conn_array[fd_counter]->destination)
free(client_conn_array[fd_counter]->destination);
free(client_conn_array[fd_counter]);
client_conn_array[fd_counter] = NULL;
}
FD_CLR(fd_counter, winit);
}
/*
* since we just wrote data to the conn_to fd, mark it as ready
* to check for reading when we go through select() the next time
*/
if (client_conn_array[fd_counter] != NULL)
if (client_conn_array[fd_counter]->conn_to != -1)
FD_SET(client_conn_array[fd_counter]->conn_to, rinit);
} /* end else no errors on write */
} else
{
/*
* There was nothing to write on this fd (can't see how we'd get
* here if select() returned this fd as writable, but it's in
* XForward so who am I to say?!)
*/
if ((client_conn_array[fd_counter]->conn_to != -1) &&
(client_conn_array[client_conn_array[fd_counter]->conn_to]->rbytes))
{
doCopyFromTo (client_conn_array[fd_counter]->conn_to,
fd_counter,
rinit,
winit);
/*
* if you got anything to write, then proceed to next
* iter of select()
*/
if (client_conn_array[fd_counter]->wbytes)
return;
}
/*
* You didn't get anything from that copy; check to see if it was
* because the readables handler marked the fd closed; if so,
* close this association; otherwise, simply clear the fd_set
* writable mask for this fd
*/
if (client_conn_array[fd_counter]->wclose)
{
FD_CLR(fd_counter, rinit);
client_conn_array[fd_counter]->conn_to = -1;
client_conn_array[fd_counter]->wclose = 0;
(void) close (fd_counter);
if (client_conn_array[fd_counter]->source)
free(client_conn_array[fd_counter]->source);
if (client_conn_array[fd_counter]->destination)
free(client_conn_array[fd_counter]->destination);
free(client_conn_array[fd_counter]);
client_conn_array[fd_counter] = NULL;
}
FD_CLR(fd_counter, winit);
}
}
static void
ProcessNewPMConnection (
int * nfds,
fd_set * rinit,
struct config * config_info,
IceListenObj ** listen_objects,
int listen_fd)
{
IceConn new_ice_conn;
IceAcceptStatus accept_status;
int temp_sock_fd;
IceListenObj * temp_obj;
struct timeval time_val;
struct timezone time_zone;
struct sockaddr_in temp_sockaddr_in;
struct sockaddr_in server_sockaddr_in;
int retval;
int addrlen = sizeof(temp_sockaddr_in);
int rule_number;
int pm_idx;
/*
* Only continue if there is room for another PM connection.
*/
for (pm_idx = 0; pm_idx < config_info->num_pm_conns; pm_idx++)
{
if (!pm_conn_array[pm_idx])
break;
}
if (pm_idx >= config_info->num_pm_conns)
{
(void) fprintf (stderr,
"Maximum number of PM connections has been reached (%d)\n",
config_info->num_pm_conns);
/*
* Must accept and then close this connection or the PM will
* continue to poll.
*/
temp_obj = *listen_objects;
new_ice_conn = IceAcceptConnection(temp_obj[listen_fd], &accept_status);
if (new_ice_conn)
IceCloseConnection(new_ice_conn);
return;
}
/*
* accept the connection if you can, use pm_listen_array
* index to index into ICE listen_object list (this is because the
* listen_objects list must correspond to the pm_listen_array)
*/
temp_obj = *listen_objects;
new_ice_conn = IceAcceptConnection(temp_obj[listen_fd], &accept_status);
if (!new_ice_conn)
{
static int been_here;
/*
* ICE initialization (bug?) makes this happen the
* first time readables is hit.
*/
if (!been_here)
been_here++;
else
(void) fprintf(stderr, "IceAcceptConnection failed (%d)\n",
accept_status);
return;
}
/*
* extract the fd from this new connection; remember, the fd of
* the listen socket is *not* the fd of the actual connection!
*/
temp_sock_fd = IceConnectionNumber(new_ice_conn);
/*
* before we get any further, do a config check on the new ICE
* connection; start by using getpeername() to get endpoint info
*/
retval = getpeername(temp_sock_fd,
(struct sockaddr*)&temp_sockaddr_in,
(void *)&addrlen);
if (retval)
{
IceCloseConnection(new_ice_conn);
(void) fprintf(stderr, "getpeername call failed\n");
return;
}
assert(temp_sockaddr_in.sin_family == AF_INET);
/*
* Do a configuration check. NOTE: we're not doing anything
* with the server_sockaddr_in argument
*/
if ((doConfigCheck(&temp_sockaddr_in,
&server_sockaddr_in,
config_info,
PMGR,
&rule_number)) == FAILURE)
{
/*
* close the PM connection
*
*/
(void) fprintf(stderr, "PM failed config check\n");
IceCloseConnection(new_ice_conn);
return;
}
/*
* you've started the connection process; allocate a buffer
* for this connection, then continue processing other fd's without
* blocking while waiting to read the coming PM data; [NOTE:
* we use the fd of the connection socket as index into the
* pm_conn_array; this saves us much troublesome linked-list
* management!]
*/
if ((pm_conn_array[pm_idx] = malloc(sizeof(struct pm_conn_buf))) == NULL)
{
(void) fprintf (stderr, "malloc - PM connection object\n");
return;
}
/*
* save the ICEconn struct for future status checks; also
* the fd (although you could extract it from the ICEconn
* each time you need it, but that's a pain)
*/
pm_conn_array[pm_idx]->fd = temp_sock_fd;
pm_conn_array[pm_idx]->ice_conn = new_ice_conn;
/*
* Set the readables select() to listen for a readable on this
* fd; remember, we're not interested in pm writables, since
* all the negotiation is handled inside this routine; adjust
* the nfds (must do that every time we get a new socket to
* select() on), and then continue processing current selections
*/
FD_SET(temp_sock_fd, rinit);
*nfds = max(*nfds, temp_sock_fd + 1);
/*
* this is where we initialize the current time and timeout on this
* pm_connection object
*/
(void) gettimeofday(&time_val, &time_zone);
pm_conn_array[pm_idx]->creation_time = time_val.tv_sec;
pm_conn_array[pm_idx]->time_to_close = config_info->pm_data_timeout;
}
static void
ProcessPMInput (
int * nfds,
fd_set * rinit,
int pm_idx)
{
IceProcessMessagesStatus process_status;
switch (IceConnectionStatus(pm_conn_array[pm_idx]->ice_conn))
{
case IceConnectPending:
/*
* for some reason this connection still isn't ready for
* reading, so return and try next readable
*/
(void) IceProcessMessages(pm_conn_array[pm_idx]->ice_conn,
NULL, NULL);
break;
case IceConnectAccepted:
/*
* you're ready to read the PM data, allocate and send back
* your client listen port, etc., etc.; do this inside
* FWPprocessMessages() by calling IceProcessMessages()
* [NOTE: The NULL args set it up for non-blocking]
*/
process_status = IceProcessMessages(pm_conn_array[pm_idx]->ice_conn,
NULL, NULL);
switch (process_status)
{
case IceProcessMessagesSuccess:
/*
* you read the server data, allocated a listen port
* for the remote client and wrote it back to the PM,
* so you don't need to do anything more until PM
* closes the connection (NOTE: Make sure we don't
* do this more than once!!)
*/
break;
case IceProcessMessagesIOError:
case IceProcessMessagesConnectionClosed:
if (process_status == IceProcessMessagesIOError)
/*
* there was a problem with the connection, close
* it explicitly
*/
IceCloseConnection(pm_conn_array[pm_idx]->ice_conn);
else
/*
* the connection somehow closed itself, so don't call
* IceCloseConnection
*/
;
/*
* reset the select() readables mask and nfds, free
* the buffer memory on this array element, reset the
* pointer to NULL and return
*/
FD_CLR(pm_conn_array[pm_idx]->fd, rinit);
*nfds = max(*nfds, pm_conn_array[pm_idx]->fd + 1);
free(pm_conn_array[pm_idx]);
pm_conn_array[pm_idx] = NULL;
break;
default:
/*
* Should never get here since all of the return
* codes from IceProcessMessages have been checked.
*/
(void) fprintf (stderr, "IceProcessMessages error\n");
}
break;
case IceConnectRejected:
/*
* don't know yet what to do in this case, but for now simply
* output diagnostic and return to select() processing
*/
(void) fprintf (stderr, "PM IceConnectRejected\n");
break;
case IceConnectIOError:
/*
* don't know yet what to do in this case, but for now simply
* output diagnostic and return to select() processing
*/
(void) fprintf (stderr, "PM IceConnectIOError\n");
break;
default:
/*
* Should never get here since all of the return
* codes from IceConnectionStatus have been checked.
*/
(void) fprintf (stderr, "IceConnectionStatus error\n");
}
}
static void
ProcessNewClientConnection (
int * nfds,
fd_set * rinit,
struct config * config_info,
int accept_fd,
int server_idx)
{
int temp_sock_fd;
struct sockaddr_in temp_sockaddr_in;
struct timeval time_val;
struct timezone time_zone;
int rule_number = -1;
int server_fd;
struct sockaddr_in server_sockaddr_in;
int temp_sock_len = sizeof(temp_sockaddr_in);
/*
* The first thing we do is accept() this connection and check it
* against configuration data to see whether its origination host
* is allowed; next, we connect to the server found in the lookup,
* synthesize a proxy connection setup request to be sent
* to that server to determine whether it`s a secure server;
* we can't block on the server reply so we go ahead and allocate
* a data structure for this client connection, and mark its
* state PENDING
*/
if ((temp_sock_fd = accept(accept_fd,
(struct sockaddr *) &temp_sockaddr_in,
(void *)&temp_sock_len)) < 0)
{
(void) fprintf (stderr, "accept call for a client failed\n");
return;
}
/*
* Try to initialize and open a connection to the server. If
* an error occurs, those functions will output an appropriate
* message
*/
if ((doServerConnectSetup(server_array[server_idx]->x_server_hostport,
&server_array[server_idx]->server_fd,
&server_sockaddr_in)) == FAILURE)
{
(void) close (temp_sock_fd);
return;
}
if ((doServerConnect(&server_array[server_idx]->server_fd,
&server_sockaddr_in)) == FAILURE)
{
(void) close (temp_sock_fd);
(void) close (server_array[server_idx]->server_fd);
return;
}
server_fd = server_array[server_idx]->server_fd;
/*
* do config check on client source and destination (must do
* it here because otherwise we don't have a server socket
* to query and we may not be able to resolve server name
* alone from xfindproxy()
*/
if ((doConfigCheck(&temp_sockaddr_in,
&server_sockaddr_in,
config_info,
CLIENT,
&rule_number)) == FAILURE)
{
/*
* log the client connection failure, close client and server
* sockets and return
*/
doWriteLogEntry (inet_ntoa(temp_sockaddr_in.sin_addr),
inet_ntoa(server_sockaddr_in.sin_addr),
CLIENT_REJECT_CONFIG,
rule_number,
config_info);
(void) close (temp_sock_fd);
(void) close (server_fd);
return;
}
/*
* If configured authorization succeeds, go ahead and
* allocate a client_conn_buf struct for client connection
*/
if ((client_conn_array[temp_sock_fd] =
malloc(sizeof (struct client_conn_buf))) == NULL)
{
(void) fprintf (stderr, "malloc - client connection buffer\n");
return;
}
bzero (client_conn_array[temp_sock_fd], sizeof (struct client_conn_buf));
/*
* save the source and destination data for this connection (since
* the log data struct will go out of scope before we check the
* server security extension or other loggable events)
*/
if ((client_conn_array[temp_sock_fd]->source =
strdup(inet_ntoa(temp_sockaddr_in.sin_addr))) == NULL)
{
(void) fprintf (stderr, "malloc - client source addr\n");
return;
}
if ((client_conn_array[temp_sock_fd]->destination =
strdup(inet_ntoa(server_sockaddr_in.sin_addr))) == NULL)
{
(void) fprintf (stderr, "malloc - client dest addr\n");
return;
}
/*
* allocate a buffer for the X server connection
* and create the association between client and server
*/
if ((client_conn_array[server_fd] =
malloc(sizeof (struct client_conn_buf))) == NULL)
{
(void) fprintf (stderr, "malloc - server connection buffer\n");
return;
}
bzero (client_conn_array[server_fd], sizeof (struct client_conn_buf));
client_conn_array[server_fd]->conn_to = temp_sock_fd;
client_conn_array[temp_sock_fd]->conn_to = server_fd;
/*
* save this sock fd for future reference (in timeout computation)
*/
client_conn_array[temp_sock_fd]->fd = temp_sock_fd;
/*
* mark this buffer as readable and writable and waiting for
* authentication to complete; mark the server conn buffer
* with a special state to make sure that its reply to
* the authentication request can be read and interpreted
* before it is simply forwarded to the client
*/
client_conn_array[temp_sock_fd]->state = CLIENT_WAITING;
client_conn_array[server_fd]->state = SERVER_REPLY;
/*
* update the select() fd mask and the nfds
*/
FD_SET(temp_sock_fd, rinit);
*nfds = max(*nfds, temp_sock_fd + 1);
FD_SET(server_fd, rinit);
*nfds = max(*nfds, server_fd + 1);
/*
* this is where we initialize the current time and timeout on this
* client_data object
*/
gettimeofday(&time_val, &time_zone);
client_conn_array[temp_sock_fd]->creation_time = time_val.tv_sec;
client_conn_array[temp_sock_fd]->time_to_close =
config_info->client_data_timeout;
/*
* be sure the mark the server side of the association, too
*/
client_conn_array[server_fd]->creation_time = time_val.tv_sec;
client_conn_array[server_fd]->time_to_close =
config_info->client_data_timeout;
client_conn_array[server_fd]->fd = server_fd;
}
static void
ProcessClientWaiting (
fd_set * winit,
int client_idx)
{
const char * conn_auth_name = "XC-QUERY-SECURITY-1";
int conn_auth_namelen;
int conn_auth_datalen;
xConnClientPrefix client;
int endian;
int name_remainder;
int data_remainder;
int idx;
char *bufP;
/*
* The remote client is sending more data on an already-
* established connection, but we still haven't checked
* authentication on this client from the associated
* X-server.
*
* Do the following:
*
* 1. create the authentication header
* 2. mark the server fd writable
* 3. copy the header info into the write buffer
* 3. set the wbytes field to the header size
* 4. mark the state SERVER_WAITING
*/
conn_auth_namelen = strlen(conn_auth_name);
if (SitePolicyCount == 0)
conn_auth_datalen = 0;
else
{
int sitePolicy;
conn_auth_datalen = 3;
for (sitePolicy = 0; sitePolicy < SitePolicyCount; sitePolicy++)
{
conn_auth_datalen += 1 + strlen(SitePolicies[sitePolicy]);
}
}
endian = 1;
if (*(char *) &endian)
client.byteOrder = '\154'; /* 'l' */
else
client.byteOrder = '\102'; /* 'B' */
client.majorVersion = X_PROTOCOL;
client.minorVersion = X_PROTOCOL_REVISION;
client.nbytesAuthProto = conn_auth_namelen;
client.nbytesAuthString = conn_auth_datalen;
/*
* Put the authentication message into the appropriate
* client_conn_buf object
*
* compute required padding for name and data strings
*/
name_remainder = (4 - (conn_auth_namelen % 4)) % 4;
data_remainder = (4 - (conn_auth_datalen % 4)) % 4;
idx = client_conn_array[client_idx]->conn_to;
bufP = client_conn_array[idx]->writebuf;
memcpy(bufP, &client, sizeof(client));
bufP += sizeof(client);
memcpy(bufP, conn_auth_name, conn_auth_namelen);
bufP += conn_auth_namelen;
bzero(bufP, name_remainder);
bufP += name_remainder;
if (conn_auth_datalen)
{
int sitePolicy;
*bufP++ = 0x02; /* Site Policies only at this point */
*bufP++ = (SitePolicyPermit == False);
*bufP++ = SitePolicyCount;
for (sitePolicy = 0; sitePolicy < SitePolicyCount; sitePolicy++)
{
char nameLen = strlen(SitePolicies[sitePolicy]);
*bufP++ = nameLen;
memcpy(bufP, SitePolicies[sitePolicy], nameLen);
bufP += nameLen;
}
bzero(bufP, data_remainder);
}
client_conn_array[idx]->wbytes = sizeof(client) +
conn_auth_namelen + name_remainder +
conn_auth_datalen + data_remainder;
/*
* Mark this fd as selectable to force a write() operation
* of authentication request to server for this client
*/
FD_SET(client_conn_array[client_idx]->conn_to, winit);
/*
* Mark the connection SERVER_WAITING (so that we don't
* read any more client data until the authentication
* sequence is complete)
*/
client_conn_array[client_idx]->state = SERVER_WAITING;
}
static void
ProcessConnectionReady (
fd_set * rinit,
fd_set * winit,
struct config * config_info,
int client_fd)
{
/*
* We've finished our authentication handshaking and are
* forwarding data either from client to server or vice versa
*/
int bytes_read;
if (client_conn_array[client_fd]->rbytes < RWBUFFER_SIZE)
{
/*
* read what you have room for
*/
bytes_read = read(client_fd,
client_conn_array[client_fd]->readbuf +
client_conn_array[client_fd]->rbytes, RWBUFFER_SIZE -
client_conn_array[client_fd]->rbytes);
/*
* check for I/O error on this fd; this is our only way
* of knowing if remote closed the connection
*/
if (bytes_read == -1)
{
/*
* remote apparently closed the connection;
* clear bits in the select() mask, reclaim conn_buffs and
* listen port
*/
FD_CLR(client_fd, rinit);
FD_CLR(client_fd, winit);
FD_CLR(client_conn_array[client_fd]->conn_to, rinit);
FD_CLR(client_conn_array[client_fd]->conn_to, winit);
(void) close (client_conn_array[client_fd]->conn_to);
(void) close (client_fd);
if (client_conn_array[client_fd]->source)
free(client_conn_array[client_fd]->source);
if (client_conn_array[client_fd]->destination)
free(client_conn_array[client_fd]->destination);
free(client_conn_array[client_fd]);
client_conn_array[client_fd] = NULL;
/*
* If this FD is for an Xserver must remove the server's
* listen fd so that clients will not attempt to connect
* on this fd.
*/
RemoveFDFromServerListenArray (client_fd,
rinit,
config_info->num_servers);
/*
* exit readables for loop
*/
return;
} else if (bytes_read == 0)
{
/*
* make sure we don't try to read on this fd again
*/
FD_CLR(client_fd, rinit);
FD_CLR(client_fd, winit);
(void) close (client_fd);
if (client_conn_array[client_fd]->conn_to != -1)
{
/*
* mark this conn_fd fd ready to close
*/
int idx = client_conn_array[client_fd]->conn_to;
client_conn_array[idx]->wclose = 1;
client_conn_array[idx]->conn_to = -1;
/*
* but still force a last write on the conn_to connection
*/
FD_SET(client_conn_array[client_fd]->conn_to, winit);
}
/*
* and mark this connection for no further activity
*/
client_conn_array[client_fd]->rbytes = 0;
client_conn_array[client_fd]->wbytes = 0;
client_conn_array[client_fd]->conn_to = -1;
/*
* If this FD is for an Xserver must remove the server's
* listen fd so that clients will not attempt to connect
* on this fd.
*/
RemoveFDFromServerListenArray (client_fd,
rinit,
config_info->num_servers);
} else
{
/*
* Move bytes between buffers on associated fd's
* (read data on one becomes write data on other)
*/
client_conn_array[client_fd]->rbytes += bytes_read;
if (client_conn_array[client_fd]->conn_to != 0)
doCopyFromTo(client_fd,
client_conn_array[client_fd]->conn_to,
rinit,
winit);
/*
* check if you have more read data than available space
*/
if (client_conn_array[client_fd]->rbytes >= RWBUFFER_SIZE)
{
/*
* if so, abort the attempted copy until you can
* write some data out of the buffer first, and
* don't allow any more reading until that's done
*/
FD_CLR(client_fd, rinit);
}
}
}
}
static void
ProcessServerReply (
fd_set * rinit,
fd_set * winit,
struct config * config_info,
int client_fd)
{
int idx;
int endian;
int four = 4;
int server_reason_remainder;
char * server_reason_padded;
int server_reason_len;
char throw_away[RWBUFFER_SIZE];
xConnSetupPrefix prefix;
if (client_conn_array[client_fd]->state == SERVER_REPLY)
{
/*
* read the server reply to the authentication request
*/
(void) read(client_fd,
client_conn_array[client_fd]->readbuf +
client_conn_array[client_fd]->rbytes,
RWBUFFER_SIZE - client_conn_array[client_fd]->rbytes);
switch ((BYTE) client_conn_array[client_fd]->readbuf[0])
{
case SERVER_REPLY_FAILURE:
#ifdef DEBUG
{
char * replyP = client_conn_array[client_fd]->readbuf;
int reasonLength = *++replyP;
replyP += 6; /* skip major & minor version, msg len */
*(replyP+reasonLength+1) = '\0';
(void) fprintf (stderr, "Server replied FAILURE: %s\n", replyP);
}
/* FALL-THROUGH */
#endif
case SERVER_REPLY_SUCCESS:
/*
* two possibilities here: either the policy field
* passed to the server is unauthorized, or the server
* does not support the security extension; in both cases
* we read the client fd then synthesize a response
* which we forward to the client before closing the
* connection
*/
(void) read(client_conn_array[client_fd]->conn_to,
throw_away, 1);
/*
* construct the client response
*/
prefix.success = 0;
prefix.lengthReason = server_reason_len =