Diff

Differences From Artifact [64de71982c]:

To Artifact [04bc4cca9b]:


32
33
34
35
36
37
38

39
40
41
42
43
44
45
    unsigned char firstpkt[12];	       /* first X data packet */
    struct X11Display *disp;
    char *auth_protocol;
    unsigned char *auth_data;
    int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
    int verified;
    int throttled, throttle_override;

    unsigned long peer_ip;
    int peer_port;
    struct ssh_channel *c;        /* channel structure held by ssh.c */
    Socket s;
};

static int xdmseen_cmp(void *a, void *b)







>







32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    unsigned char firstpkt[12];	       /* first X data packet */
    struct X11Display *disp;
    char *auth_protocol;
    unsigned char *auth_data;
    int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
    int verified;
    int throttled, throttle_override;
    int no_data_sent_to_x_client;
    unsigned long peer_ip;
    int peer_port;
    struct ssh_channel *c;        /* channel structure held by ssh.c */
    Socket s;
};

static int xdmseen_cmp(void *a, void *b)
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
}

static void x11_log(Plug p, int type, SockAddr addr, int port,
		    const char *error_msg, int error_code)
{
    /* We have no interface to the logging module here, so we drop these. */
}




static int x11_closing(Plug plug, const char *error_msg, int error_code,
		       int calling_back)
{
    struct X11Connection *xconn = (struct X11Connection *) plug;

    if (error_msg) {
        /*











         * Socket error. Slam the connection instantly shut.

         */
        sshfwd_unclean_close(xconn->c, error_msg);
    } else {
        /*
         * Ordinary EOF received on socket. Send an EOF on the SSH
         * channel.
         */
        if (xconn->c)
            sshfwd_write_eof(xconn->c);
    }

    return 1;
}

static int x11_receive(Plug plug, int urgent, char *data, int len)
{
    struct X11Connection *xconn = (struct X11Connection *) plug;

    if (sshfwd_write(xconn->c, data, len) > 0) {
	xconn->throttled = 1;

	sk_set_frozen(xconn->s, 1);
    }

    return 1;
}

static void x11_sent(Plug plug, int bufsize)







>
>
>








>
>
>
>
>
>
>
>
>
>
>
|
>




















>







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
}

static void x11_log(Plug p, int type, SockAddr addr, int port,
		    const char *error_msg, int error_code)
{
    /* We have no interface to the logging module here, so we drop these. */
}

static void x11_send_init_error(struct X11Connection *conn,
                                const char *err_message);

static int x11_closing(Plug plug, const char *error_msg, int error_code,
		       int calling_back)
{
    struct X11Connection *xconn = (struct X11Connection *) plug;

    if (error_msg) {
        /*
         * Socket error. If we're still at the connection setup stage,
         * construct an X11 error packet passing on the problem.
         */
        if (xconn->no_data_sent_to_x_client) {
            char *err_message = dupprintf("unable to connect to forwarded "
                                          "X server: %s", error_msg);
            x11_send_init_error(xconn, err_message);
            sfree(err_message);
        }

        /*
         * Whether we did that or not, now we slam the connection
         * shut.
         */
        sshfwd_unclean_close(xconn->c, error_msg);
    } else {
        /*
         * Ordinary EOF received on socket. Send an EOF on the SSH
         * channel.
         */
        if (xconn->c)
            sshfwd_write_eof(xconn->c);
    }

    return 1;
}

static int x11_receive(Plug plug, int urgent, char *data, int len)
{
    struct X11Connection *xconn = (struct X11Connection *) plug;

    if (sshfwd_write(xconn->c, data, len) > 0) {
	xconn->throttled = 1;
        xconn->no_data_sent_to_x_client = FALSE;
	sk_set_frozen(xconn->s, 1);
    }

    return 1;
}

static void x11_sent(Plug plug, int bufsize)
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
 * Called to set up the raw connection.
 * 
 * On success, returns NULL and fills in *xconnret. On error, returns
 * a dynamically allocated error message string.
 */
extern char *x11_init(struct X11Connection **xconnret,
                      struct X11Display *disp, void *c,
                      const char *peeraddr, int peerport, Conf *conf)
{
    static const struct plug_function_table fn_table = {
	x11_log,
	x11_closing,
	x11_receive,
	x11_sent,
	NULL
    };

    const char *err;
    struct X11Connection *xconn;

    /*
     * Open socket.
     */
    xconn = *xconnret = snew(struct X11Connection);
    xconn->fn = &fn_table;
    xconn->auth_protocol = NULL;
    xconn->disp = disp;
    xconn->verified = 0;
    xconn->data_read = 0;
    xconn->throttled = xconn->throttle_override = 0;

    xconn->c = c;

    xconn->s = new_connection(sk_addr_dup(disp->addr),
                           disp->realhost, disp->port,
                           0, 1, 0, 0, (Plug) xconn, conf);

    if ((err = sk_socket_error(xconn->s)) != NULL) {

        char *err_ret = dupstr(err);
        sk_close(xconn->s);
	sfree(xconn);

        *xconnret = NULL;
	return err_ret;
    }

    /*
     * See if we can make sense of the peer address we were given.
     */
    {
	int i[4];
	if (peeraddr &&







|









<












>


<
<
<
>
|
>
|
|
|
>
|
<
<







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
 * Called to set up the raw connection.
 * 
 * On success, returns NULL and fills in *xconnret. On error, returns
 * a dynamically allocated error message string.
 */
extern char *x11_init(struct X11Connection **xconnret,
                      struct X11Display *disp, void *c,
                      const char *peeraddr, int peerport)
{
    static const struct plug_function_table fn_table = {
	x11_log,
	x11_closing,
	x11_receive,
	x11_sent,
	NULL
    };


    struct X11Connection *xconn;

    /*
     * Open socket.
     */
    xconn = *xconnret = snew(struct X11Connection);
    xconn->fn = &fn_table;
    xconn->auth_protocol = NULL;
    xconn->disp = disp;
    xconn->verified = 0;
    xconn->data_read = 0;
    xconn->throttled = xconn->throttle_override = 0;
    xconn->no_data_sent_to_x_client = TRUE;
    xconn->c = c;




    /*
     * We don't actually open a local socket to the X server just yet.
     * Instead, we'll wait until we see the incoming authentication
     * data, which may tell us we have to divert this X forwarding
     * channel to a connection-sharing downstream rather than handling
     * it ourself.
     */
    xconn->s = NULL;



    /*
     * See if we can make sense of the peer address we were given.
     */
    {
	int i[4];
	if (peeraddr &&
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
	return;

    if (xconn->auth_protocol) {
	sfree(xconn->auth_protocol);
	sfree(xconn->auth_data);
    }


    sk_close(xconn->s);
    sfree(xconn);
}

void x11_unthrottle(struct X11Connection *xconn)
{
    if (!xconn)
	return;

    xconn->throttled = 0;

    sk_set_frozen(xconn->s, xconn->throttled || xconn->throttle_override);
}

void x11_override_throttle(struct X11Connection *xconn, int enable)
{
    if (!xconn)
	return;

    xconn->throttle_override = enable;

    sk_set_frozen(xconn->s, xconn->throttled || xconn->throttle_override);

























}

/*
 * Called to send data down the raw connection.
 */
int x11_send(struct X11Connection *xconn, char *data, int len)
{







>
|









>
|








>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
	return;

    if (xconn->auth_protocol) {
	sfree(xconn->auth_protocol);
	sfree(xconn->auth_data);
    }

    if (xconn->s)
        sk_close(xconn->s);
    sfree(xconn);
}

void x11_unthrottle(struct X11Connection *xconn)
{
    if (!xconn)
	return;

    xconn->throttled = 0;
    if (xconn->s)
        sk_set_frozen(xconn->s, xconn->throttled || xconn->throttle_override);
}

void x11_override_throttle(struct X11Connection *xconn, int enable)
{
    if (!xconn)
	return;

    xconn->throttle_override = enable;
    if (xconn->s)
        sk_set_frozen(xconn->s, xconn->throttled || xconn->throttle_override);
}

static void x11_send_init_error(struct X11Connection *xconn,
                                const char *err_message)
{
    char *full_message;
    int msglen, msgsize;
    unsigned char *reply;

    full_message = dupprintf("%s X11 proxy: %s\n", appname, err_message);

    msglen = strlen(full_message);
    reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */
    msgsize = (msglen + 3) & ~3;
    reply[0] = 0;	       /* failure */
    reply[1] = msglen;	       /* length of reason string */
    memcpy(reply + 2, xconn->firstpkt + 2, 4);	/* major/minor proto vsn */
    PUT_16BIT(xconn->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
    memset(reply + 8, 0, msgsize);
    memcpy(reply + 8, full_message, msglen);
    sshfwd_write(xconn->c, (char *)reply, 8 + msgsize);
    sshfwd_write_eof(xconn->c);
    xconn->no_data_sent_to_x_client = FALSE;
    sfree(reply);
    sfree(full_message);
}

/*
 * Called to send data down the raw connection.
 */
int x11_send(struct X11Connection *xconn, char *data, int len)
{
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
    if (xconn->data_read < 12 + xconn->auth_psize + xconn->auth_dsize)
	return 0;

    /*
     * If we haven't verified the authorisation, do so now.
     */
    if (!xconn->verified) {
	char *err;



	xconn->auth_protocol[xconn->auth_plen] = '\0';	/* ASCIZ */
	err = x11_verify(xconn->peer_ip, xconn->peer_port,
			 xconn->disp, xconn->auth_protocol,
			 xconn->auth_data, xconn->auth_dlen);





	/*
	 * If authorisation failed, construct and send an error
	 * packet, then terminate the connection.

	 */


	if (err) {
	    char *message;
	    int msglen, msgsize;
	    unsigned char *reply;

	    message = dupprintf("%s X11 proxy: %s", appname, err);

	    msglen = strlen(message);
	    reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */
	    msgsize = (msglen + 3) & ~3;
	    reply[0] = 0;	       /* failure */
	    reply[1] = msglen;	       /* length of reason string */
	    memcpy(reply + 2, xconn->firstpkt + 2, 4);	/* major/minor proto vsn */
	    PUT_16BIT(xconn->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
	    memset(reply + 8, 0, msgsize);
	    memcpy(reply + 8, message, msglen);
	    sshfwd_write(xconn->c, (char *)reply, 8 + msgsize);
	    sshfwd_write_eof(xconn->c);
	    sfree(reply);
	    sfree(message);
	    return 0;
	}

	/*
	 * Now we know we're going to accept the connection. Strip
	 * the fake auth data, and optionally put real auth data in
	 * instead.
	 */
        {
            char realauthdata[64];
            int realauthlen = 0;
            int authstrlen = strlen(x11_authnames[xconn->disp->localauthproto]);
	    int buflen = 0;	       /* initialise to placate optimiser */
            static const char zeroes[4] = { 0,0,0,0 };







|
>
>





>
>
>
|
>
|
<
|
>
|
>
>
|
|
<
<
|
|
>
|
<
<
<
<
<
<
<
<
<
<
<
|
|
|

|
<
|
|







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
    if (xconn->data_read < 12 + xconn->auth_psize + xconn->auth_dsize)
	return 0;

    /*
     * If we haven't verified the authorisation, do so now.
     */
    if (!xconn->verified) {
	const char *err;

        assert(!xconn->s);

	xconn->auth_protocol[xconn->auth_plen] = '\0';	/* ASCIZ */
	err = x11_verify(xconn->peer_ip, xconn->peer_port,
			 xconn->disp, xconn->auth_protocol,
			 xconn->auth_data, xconn->auth_dlen);
	if (err) {
            x11_send_init_error(xconn, err);
            return 0;
        }

        /*

         * Now we know we're going to accept the connection. Actually
         * connect to the X server.
         */
        xconn->s = new_connection(sk_addr_dup(xconn->disp->addr),
                                  xconn->disp->realhost, xconn->disp->port, 
                                  0, 1, 0, 0, (Plug) xconn,
                                  sshfwd_get_conf(xconn->c));


        if ((err = sk_socket_error(xconn->s)) != NULL) {
            char *err_message = dupprintf("unable to connect to"
                                          " forwarded X server: %s", err);
            x11_send_init_error(xconn, err_message);











            sfree(err_message);
            return 0;
        }

        /*

         * Strip the fake auth data, and optionally put real auth data
	 * in instead.
	 */
        {
            char realauthdata[64];
            int realauthlen = 0;
            int authstrlen = strlen(x11_authnames[xconn->disp->localauthproto]);
	    int buflen = 0;	       /* initialise to placate optimiser */
            static const char zeroes[4] = { 0,0,0,0 };
792
793
794
795
796
797
798

799









800

     */

    return sk_write(xconn->s, data, len);
}

void x11_send_eof(struct X11Connection *xconn)
{

    sk_write_eof(xconn->s);









}








>
|
>
>
>
>
>
>
>
>
>
|
>
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
     */

    return sk_write(xconn->s, data, len);
}

void x11_send_eof(struct X11Connection *xconn)
{
    if (xconn->s) {
        sk_write_eof(xconn->s);
    } else {
        /*
         * If EOF is received from the X client before we've got to
         * the point of actually connecting to an X server, then we
         * should send an EOF back to the client so that the
         * forwarded channel will be terminated.
         */
        if (xconn->c)
            sshfwd_write_eof(xconn->c);
    }
}