x11fwd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * Platform-independent bits of X11 forwarding.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include <time.h>
  8. #include "putty.h"
  9. #include "ssh.h"
  10. #include "channel.h"
  11. #include "tree234.h"
  12. struct XDMSeen {
  13. unsigned int time;
  14. unsigned char clientid[6];
  15. };
  16. typedef struct X11Connection {
  17. unsigned char firstpkt[12]; /* first X data packet */
  18. tree234 *authtree;
  19. struct X11Display *disp;
  20. char *auth_protocol;
  21. unsigned char *auth_data;
  22. int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
  23. bool verified;
  24. bool input_wanted;
  25. bool no_data_sent_to_x_client;
  26. char *peer_addr;
  27. int peer_port;
  28. SshChannel *c; /* channel structure held by SSH backend */
  29. Socket *s;
  30. Plug plug;
  31. Channel chan;
  32. } X11Connection;
  33. static int xdmseen_cmp(void *a, void *b)
  34. {
  35. struct XDMSeen *sa = a, *sb = b;
  36. return sa->time > sb->time ? 1 :
  37. sa->time < sb->time ? -1 :
  38. memcmp(sa->clientid, sb->clientid, sizeof(sa->clientid));
  39. }
  40. struct X11FakeAuth *x11_invent_fake_auth(tree234 *authtree, int authtype)
  41. {
  42. struct X11FakeAuth *auth = snew(struct X11FakeAuth);
  43. int i;
  44. /*
  45. * This function has the job of inventing a set of X11 fake auth
  46. * data, and adding it to 'authtree'. We must preserve the
  47. * property that for any given actual authorisation attempt, _at
  48. * most one_ thing in the tree can possibly match it.
  49. *
  50. * For MIT-MAGIC-COOKIE-1, that's not too difficult: the match
  51. * criterion is simply that the entire cookie is correct, so we
  52. * just have to make sure we don't make up two cookies the same.
  53. * (Vanishingly unlikely, but we check anyway to be sure, and go
  54. * round again inventing a new cookie if add234 tells us the one
  55. * we thought of is already in use.)
  56. *
  57. * For XDM-AUTHORIZATION-1, it's a little more fiddly. The setup
  58. * with XA1 is that half the cookie is used as a DES key with
  59. * which to CBC-encrypt an assortment of stuff. Happily, the stuff
  60. * encrypted _begins_ with the other half of the cookie, and the
  61. * IV is always zero, which means that any valid XA1 authorisation
  62. * attempt for a given cookie must begin with the same cipher
  63. * block, consisting of the DES ECB encryption of the first half
  64. * of the cookie using the second half as a key. So we compute
  65. * that cipher block here and now, and use it as the sorting key
  66. * for distinguishing XA1 entries in the tree.
  67. */
  68. if (authtype == X11_MIT) {
  69. auth->proto = X11_MIT;
  70. /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
  71. auth->datalen = 16;
  72. auth->data = snewn(auth->datalen, unsigned char);
  73. auth->xa1_firstblock = NULL;
  74. while (1) {
  75. random_read(auth->data, auth->datalen);
  76. if (add234(authtree, auth) == auth)
  77. break;
  78. }
  79. auth->xdmseen = NULL;
  80. } else {
  81. assert(authtype == X11_XDM);
  82. auth->proto = X11_XDM;
  83. /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */
  84. auth->datalen = 16;
  85. auth->data = snewn(auth->datalen, unsigned char);
  86. auth->xa1_firstblock = snewn(8, unsigned char);
  87. memset(auth->xa1_firstblock, 0, 8);
  88. while (1) {
  89. random_read(auth->data, 15);
  90. auth->data[15] = auth->data[8];
  91. auth->data[8] = 0;
  92. memcpy(auth->xa1_firstblock, auth->data, 8);
  93. des_encrypt_xdmauth(auth->data + 9, auth->xa1_firstblock, 8);
  94. if (add234(authtree, auth) == auth)
  95. break;
  96. }
  97. auth->xdmseen = newtree234(xdmseen_cmp);
  98. }
  99. auth->protoname = dupstr(x11_authnames[auth->proto]);
  100. auth->datastring = snewn(auth->datalen * 2 + 1, char);
  101. for (i = 0; i < auth->datalen; i++)
  102. sprintf(auth->datastring + i*2, "%02x",
  103. auth->data[i]);
  104. auth->disp = NULL;
  105. auth->share_cs = NULL;
  106. auth->share_chan = NULL;
  107. return auth;
  108. }
  109. void x11_free_fake_auth(struct X11FakeAuth *auth)
  110. {
  111. if (auth->data)
  112. smemclr(auth->data, auth->datalen);
  113. sfree(auth->data);
  114. sfree(auth->protoname);
  115. sfree(auth->datastring);
  116. sfree(auth->xa1_firstblock);
  117. if (auth->xdmseen != NULL) {
  118. struct XDMSeen *seen;
  119. while ((seen = delpos234(auth->xdmseen, 0)) != NULL)
  120. sfree(seen);
  121. freetree234(auth->xdmseen);
  122. }
  123. sfree(auth);
  124. }
  125. int x11_authcmp(void *av, void *bv)
  126. {
  127. struct X11FakeAuth *a = (struct X11FakeAuth *)av;
  128. struct X11FakeAuth *b = (struct X11FakeAuth *)bv;
  129. if (a->proto < b->proto)
  130. return -1;
  131. else if (a->proto > b->proto)
  132. return +1;
  133. if (a->proto == X11_MIT) {
  134. if (a->datalen < b->datalen)
  135. return -1;
  136. else if (a->datalen > b->datalen)
  137. return +1;
  138. return memcmp(a->data, b->data, a->datalen);
  139. } else {
  140. assert(a->proto == X11_XDM);
  141. return memcmp(a->xa1_firstblock, b->xa1_firstblock, 8);
  142. }
  143. }
  144. #define XDM_MAXSKEW 20*60 /* 20 minute clock skew should be OK */
  145. static char *x11_verify(unsigned long peer_ip, int peer_port,
  146. tree234 *authtree, char *proto,
  147. unsigned char *data, int dlen,
  148. struct X11FakeAuth **auth_ret)
  149. {
  150. struct X11FakeAuth match_dummy; /* for passing to find234 */
  151. struct X11FakeAuth *auth;
  152. /*
  153. * First, do a lookup in our tree to find the only authorisation
  154. * record that _might_ match.
  155. */
  156. if (!strcmp(proto, x11_authnames[X11_MIT])) {
  157. /*
  158. * Just look up the whole cookie that was presented to us,
  159. * which x11_authcmp will compare against the cookies we
  160. * currently believe in.
  161. */
  162. match_dummy.proto = X11_MIT;
  163. match_dummy.datalen = dlen;
  164. match_dummy.data = data;
  165. } else if (!strcmp(proto, x11_authnames[X11_XDM])) {
  166. /*
  167. * Look up the first cipher block, against the stored first
  168. * cipher blocks for the XDM-AUTHORIZATION-1 cookies we
  169. * currently know. (See comment in x11_invent_fake_auth.)
  170. */
  171. match_dummy.proto = X11_XDM;
  172. match_dummy.xa1_firstblock = data;
  173. } else if (!proto[0]) {
  174. /*
  175. * If the user has attempted to connect to the forwarded X
  176. * display with no authority at all, we can give a better
  177. * error message than the generic "unsupported protocol". We
  178. * at least _recognise_ the null auth protocol, even if we
  179. * don't _accept_ it.
  180. */
  181. return dupstr("No authorisation provided");
  182. } else {
  183. return dupprintf("Unsupported authorisation protocol '%s'", proto);
  184. }
  185. if ((auth = find234(authtree, &match_dummy, 0)) == NULL)
  186. return dupstr("Authorisation not recognised");
  187. /*
  188. * If we're using MIT-MAGIC-COOKIE-1, that was all we needed. If
  189. * we're doing XDM-AUTHORIZATION-1, though, we have to check the
  190. * rest of the auth data.
  191. */
  192. if (auth->proto == X11_XDM) {
  193. unsigned long t;
  194. time_t tim;
  195. int i;
  196. struct XDMSeen *seen, *ret;
  197. if (dlen != 24)
  198. return dupprintf("XDM-AUTHORIZATION-1 data was wrong length "
  199. "(%d, expected 24)", dlen);
  200. if (peer_port == -1)
  201. return dupstr("cannot do XDM-AUTHORIZATION-1 without remote "
  202. "address data");
  203. des_decrypt_xdmauth(auth->data+9, data, 24);
  204. /* Bitwise-OR together any mismatches in the fixed parts of
  205. * the data, to allow checking it all at once */
  206. uint32_t mismatches = 0;
  207. /* Check non-key half of auth cookie */
  208. for (i = 0; i < 8; i++)
  209. mismatches |= auth->data[i] ^ data[i];
  210. /* Check IP address and port */
  211. mismatches |= GET_32BIT_MSB_FIRST(data+8) ^ peer_ip;
  212. mismatches |= (unsigned short)(GET_16BIT_MSB_FIRST(data+12) ^
  213. peer_port);
  214. /* Check zero padding */
  215. for (i = 18; i < 24; i++)
  216. mismatches |= data[i];
  217. if (mismatches)
  218. return dupstr("XDM-AUTHORIZATION-1 data failed check");
  219. t = GET_32BIT_MSB_FIRST(data+14);
  220. tim = time(NULL);
  221. if (((unsigned long)t - (unsigned long)tim
  222. + XDM_MAXSKEW) > 2*XDM_MAXSKEW)
  223. return dupstr("XDM-AUTHORIZATION-1 time stamp was too far out");
  224. seen = snew(struct XDMSeen);
  225. seen->time = t;
  226. memcpy(seen->clientid, data+8, 6);
  227. assert(auth->xdmseen != NULL);
  228. ret = add234(auth->xdmseen, seen);
  229. if (ret != seen) {
  230. sfree(seen);
  231. return dupstr("XDM-AUTHORIZATION-1 data replayed");
  232. }
  233. /* While we're here, purge entries too old to be replayed. */
  234. for (;;) {
  235. seen = index234(auth->xdmseen, 0);
  236. assert(seen != NULL);
  237. if (t - seen->time <= XDM_MAXSKEW)
  238. break;
  239. sfree(delpos234(auth->xdmseen, 0));
  240. }
  241. }
  242. /* implement other protocols here if ever required */
  243. *auth_ret = auth;
  244. return NULL;
  245. }
  246. static void x11_send_init_error(struct X11Connection *conn,
  247. const char *err_message);
  248. static void x11_closing(Plug *plug, PlugCloseType type, const char *error_msg)
  249. {
  250. struct X11Connection *xconn = container_of(
  251. plug, struct X11Connection, plug);
  252. if (type != PLUGCLOSE_NORMAL) {
  253. /*
  254. * Socket error. If we're still at the connection setup stage,
  255. * construct an X11 error packet passing on the problem.
  256. */
  257. if (xconn->no_data_sent_to_x_client) {
  258. char *err_message = dupprintf("unable to connect to forwarded "
  259. "X server: %s", error_msg);
  260. x11_send_init_error(xconn, err_message);
  261. sfree(err_message);
  262. }
  263. /*
  264. * Whether we did that or not, now we slam the connection
  265. * shut.
  266. */
  267. sshfwd_initiate_close(xconn->c, error_msg);
  268. } else {
  269. /*
  270. * Ordinary EOF received on socket. Send an EOF on the SSH
  271. * channel.
  272. */
  273. if (xconn->c)
  274. sshfwd_write_eof(xconn->c);
  275. }
  276. }
  277. static void x11_receive(Plug *plug, int urgent, const char *data, size_t len)
  278. {
  279. struct X11Connection *xconn = container_of(
  280. plug, struct X11Connection, plug);
  281. xconn->no_data_sent_to_x_client = false;
  282. sshfwd_write(xconn->c, data, len);
  283. }
  284. static void x11_sent(Plug *plug, size_t bufsize)
  285. {
  286. struct X11Connection *xconn = container_of(
  287. plug, struct X11Connection, plug);
  288. sshfwd_unthrottle(xconn->c, bufsize);
  289. }
  290. static const PlugVtable X11Connection_plugvt = {
  291. .log = nullplug_log,
  292. .closing = x11_closing,
  293. .receive = x11_receive,
  294. .sent = x11_sent,
  295. };
  296. static void x11_chan_free(Channel *chan);
  297. static size_t x11_send(
  298. Channel *chan, bool is_stderr, const void *vdata, size_t len);
  299. static void x11_send_eof(Channel *chan);
  300. static void x11_set_input_wanted(Channel *chan, bool wanted);
  301. static char *x11_log_close_msg(Channel *chan);
  302. static const ChannelVtable X11Connection_channelvt = {
  303. .free = x11_chan_free,
  304. .open_confirmation = chan_remotely_opened_confirmation,
  305. .open_failed = chan_remotely_opened_failure,
  306. .send = x11_send,
  307. .send_eof = x11_send_eof,
  308. .set_input_wanted = x11_set_input_wanted,
  309. .log_close_msg = x11_log_close_msg,
  310. .want_close = chan_default_want_close,
  311. .rcvd_exit_status = chan_no_exit_status,
  312. .rcvd_exit_signal = chan_no_exit_signal,
  313. .rcvd_exit_signal_numeric = chan_no_exit_signal_numeric,
  314. .run_shell = chan_no_run_shell,
  315. .run_command = chan_no_run_command,
  316. .run_subsystem = chan_no_run_subsystem,
  317. .enable_x11_forwarding = chan_no_enable_x11_forwarding,
  318. .enable_agent_forwarding = chan_no_enable_agent_forwarding,
  319. .allocate_pty = chan_no_allocate_pty,
  320. .set_env = chan_no_set_env,
  321. .send_break = chan_no_send_break,
  322. .send_signal = chan_no_send_signal,
  323. .change_window_size = chan_no_change_window_size,
  324. .request_response = chan_no_request_response,
  325. };
  326. /*
  327. * Called to set up the X11Connection structure, though this does not
  328. * yet connect to an actual server.
  329. */
  330. Channel *x11_new_channel(tree234 *authtree, SshChannel *c,
  331. const char *peeraddr, int peerport,
  332. bool connection_sharing_possible)
  333. {
  334. struct X11Connection *xconn;
  335. /*
  336. * Open socket.
  337. */
  338. xconn = snew(struct X11Connection);
  339. xconn->plug.vt = &X11Connection_plugvt;
  340. xconn->chan.vt = &X11Connection_channelvt;
  341. xconn->chan.initial_fixed_window_size =
  342. (connection_sharing_possible ? 128 : 0);
  343. xconn->auth_protocol = NULL;
  344. xconn->authtree = authtree;
  345. xconn->verified = false;
  346. xconn->data_read = 0;
  347. xconn->input_wanted = true;
  348. xconn->no_data_sent_to_x_client = true;
  349. xconn->c = c;
  350. /*
  351. * We don't actually open a local socket to the X server just yet,
  352. * because we don't know which one it is. Instead, we'll wait
  353. * until we see the incoming authentication data, which may tell
  354. * us what display to connect to, or whether we have to divert
  355. * this X forwarding channel to a connection-sharing downstream
  356. * rather than handling it ourself.
  357. */
  358. xconn->disp = NULL;
  359. xconn->s = NULL;
  360. /*
  361. * Stash the peer address we were given in its original text form.
  362. */
  363. xconn->peer_addr = peeraddr ? dupstr(peeraddr) : NULL;
  364. xconn->peer_port = peerport;
  365. return &xconn->chan;
  366. }
  367. static void x11_chan_free(Channel *chan)
  368. {
  369. assert(chan->vt == &X11Connection_channelvt);
  370. X11Connection *xconn = container_of(chan, X11Connection, chan);
  371. if (xconn->auth_protocol) {
  372. sfree(xconn->auth_protocol);
  373. sfree(xconn->auth_data);
  374. }
  375. if (xconn->s)
  376. sk_close(xconn->s);
  377. sfree(xconn->peer_addr);
  378. sfree(xconn);
  379. }
  380. static void x11_set_input_wanted(Channel *chan, bool wanted)
  381. {
  382. assert(chan->vt == &X11Connection_channelvt);
  383. X11Connection *xconn = container_of(chan, X11Connection, chan);
  384. xconn->input_wanted = wanted;
  385. if (xconn->s)
  386. sk_set_frozen(xconn->s, !xconn->input_wanted);
  387. }
  388. static void x11_send_init_error(struct X11Connection *xconn,
  389. const char *err_message)
  390. {
  391. char *full_message;
  392. int msglen, msgsize;
  393. unsigned char *reply;
  394. full_message = dupprintf("%s X11 proxy: %s\n", appname, err_message);
  395. msglen = strlen(full_message);
  396. reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */
  397. msgsize = (msglen + 3) & ~3;
  398. reply[0] = 0; /* failure */
  399. reply[1] = msglen; /* length of reason string */
  400. memcpy(reply + 2, xconn->firstpkt + 2, 4); /* major/minor proto vsn */
  401. PUT_16BIT_X11(xconn->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
  402. memset(reply + 8, 0, msgsize);
  403. memcpy(reply + 8, full_message, msglen);
  404. sshfwd_write(xconn->c, reply, 8 + msgsize);
  405. sshfwd_write_eof(xconn->c);
  406. xconn->no_data_sent_to_x_client = false;
  407. sfree(reply);
  408. sfree(full_message);
  409. }
  410. /*
  411. * Called to send data down the raw connection.
  412. */
  413. static size_t x11_send(
  414. Channel *chan, bool is_stderr, const void *vdata, size_t len)
  415. {
  416. assert(chan->vt == &X11Connection_channelvt);
  417. X11Connection *xconn = container_of(chan, X11Connection, chan);
  418. const char *data = (const char *)vdata;
  419. /*
  420. * Read the first packet.
  421. */
  422. while (len > 0 && xconn->data_read < 12)
  423. xconn->firstpkt[xconn->data_read++] = (unsigned char) (len--, *data++);
  424. if (xconn->data_read < 12)
  425. return 0;
  426. /*
  427. * If we have not allocated the auth_protocol and auth_data
  428. * strings, do so now.
  429. */
  430. if (!xconn->auth_protocol) {
  431. char endian = xconn->firstpkt[0];
  432. xconn->auth_plen = GET_16BIT_X11(endian, xconn->firstpkt + 6);
  433. xconn->auth_dlen = GET_16BIT_X11(endian, xconn->firstpkt + 8);
  434. xconn->auth_psize = (xconn->auth_plen + 3) & ~3;
  435. xconn->auth_dsize = (xconn->auth_dlen + 3) & ~3;
  436. /* Leave room for a terminating zero, to make our lives easier. */
  437. xconn->auth_protocol = snewn(xconn->auth_psize + 1, char);
  438. xconn->auth_data = snewn(xconn->auth_dsize, unsigned char);
  439. }
  440. /*
  441. * Read the auth_protocol and auth_data strings.
  442. */
  443. while (len > 0 &&
  444. xconn->data_read < 12 + xconn->auth_psize)
  445. xconn->auth_protocol[xconn->data_read++ - 12] = (len--, *data++);
  446. while (len > 0 &&
  447. xconn->data_read < 12 + xconn->auth_psize + xconn->auth_dsize)
  448. xconn->auth_data[xconn->data_read++ - 12 -
  449. xconn->auth_psize] = (unsigned char) (len--, *data++);
  450. if (xconn->data_read < 12 + xconn->auth_psize + xconn->auth_dsize)
  451. return 0;
  452. /*
  453. * If we haven't verified the authorisation, do so now.
  454. */
  455. if (!xconn->verified) {
  456. const char *err;
  457. char *errmut;
  458. struct X11FakeAuth *auth_matched = NULL;
  459. unsigned long peer_ip;
  460. int peer_port;
  461. int protomajor, protominor;
  462. void *greeting;
  463. int greeting_len;
  464. unsigned char *socketdata;
  465. int socketdatalen;
  466. char new_peer_addr[32];
  467. int new_peer_port;
  468. char endian = xconn->firstpkt[0];
  469. protomajor = GET_16BIT_X11(endian, xconn->firstpkt + 2);
  470. protominor = GET_16BIT_X11(endian, xconn->firstpkt + 4);
  471. assert(!xconn->s);
  472. xconn->auth_protocol[xconn->auth_plen] = '\0'; /* ASCIZ */
  473. peer_ip = 0; /* placate optimiser */
  474. if (x11_parse_ip(xconn->peer_addr, &peer_ip))
  475. peer_port = xconn->peer_port;
  476. else
  477. peer_port = -1; /* signal no peer address data available */
  478. errmut = x11_verify(peer_ip, peer_port,
  479. xconn->authtree, xconn->auth_protocol,
  480. xconn->auth_data, xconn->auth_dlen, &auth_matched);
  481. if (errmut) {
  482. x11_send_init_error(xconn, errmut);
  483. sfree(errmut);
  484. return 0;
  485. }
  486. assert(auth_matched);
  487. /*
  488. * If this auth points to a connection-sharing downstream
  489. * rather than an X display we know how to connect to
  490. * directly, pass it off to the sharing module now. (This will
  491. * have the side effect of freeing xconn.)
  492. */
  493. if (auth_matched->share_cs) {
  494. sshfwd_x11_sharing_handover(xconn->c, auth_matched->share_cs,
  495. auth_matched->share_chan,
  496. xconn->peer_addr, xconn->peer_port,
  497. xconn->firstpkt[0],
  498. protomajor, protominor, data, len);
  499. return 0;
  500. }
  501. /*
  502. * Now we know we're going to accept the connection, and what
  503. * X display to connect to. Actually connect to it.
  504. */
  505. xconn->chan.initial_fixed_window_size = 0;
  506. sshfwd_window_override_removed(xconn->c);
  507. xconn->disp = auth_matched->disp;
  508. xconn->s = new_connection(sk_addr_dup(xconn->disp->addr),
  509. xconn->disp->realhost, xconn->disp->port,
  510. false, true, false, false, &xconn->plug,
  511. sshfwd_get_conf(xconn->c), NULL);
  512. if ((err = sk_socket_error(xconn->s)) != NULL) {
  513. char *err_message = dupprintf("unable to connect to"
  514. " forwarded X server: %s", err);
  515. x11_send_init_error(xconn, err_message);
  516. sfree(err_message);
  517. return 0;
  518. }
  519. /*
  520. * Write a new connection header containing our replacement
  521. * auth data.
  522. */
  523. socketdatalen = 0; /* placate compiler warning */
  524. socketdata = sk_getxdmdata(xconn->s, &socketdatalen);
  525. if (socketdata && socketdatalen==6) {
  526. sprintf(new_peer_addr, "%d.%d.%d.%d", socketdata[0],
  527. socketdata[1], socketdata[2], socketdata[3]);
  528. new_peer_port = GET_16BIT_MSB_FIRST(socketdata + 4);
  529. } else {
  530. strcpy(new_peer_addr, "0.0.0.0");
  531. new_peer_port = 0;
  532. }
  533. greeting = x11_make_greeting(xconn->firstpkt[0],
  534. protomajor, protominor,
  535. xconn->disp->localauthproto,
  536. xconn->disp->localauthdata,
  537. xconn->disp->localauthdatalen,
  538. new_peer_addr, new_peer_port,
  539. &greeting_len);
  540. sk_write(xconn->s, greeting, greeting_len);
  541. smemclr(greeting, greeting_len);
  542. sfree(greeting);
  543. /*
  544. * Now we're done.
  545. */
  546. xconn->verified = true;
  547. }
  548. /*
  549. * After initialisation, just copy data simply.
  550. */
  551. return sk_write(xconn->s, data, len);
  552. }
  553. static void x11_send_eof(Channel *chan)
  554. {
  555. assert(chan->vt == &X11Connection_channelvt);
  556. X11Connection *xconn = container_of(chan, X11Connection, chan);
  557. if (xconn->s) {
  558. sk_write_eof(xconn->s);
  559. } else {
  560. /*
  561. * If EOF is received from the X client before we've got to
  562. * the point of actually connecting to an X server, then we
  563. * should send an EOF back to the client so that the
  564. * forwarded channel will be terminated.
  565. */
  566. if (xconn->c)
  567. sshfwd_write_eof(xconn->c);
  568. }
  569. }
  570. static char *x11_log_close_msg(Channel *chan)
  571. {
  572. return dupstr("Forwarded X11 connection terminated");
  573. }