+++ /dev/null
-/*\r
- * Platform-independent bits of X11 forwarding.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <assert.h>\r
-#include <time.h>\r
-\r
-#include "putty.h"\r
-#include "ssh.h"\r
-#include "tree234.h"\r
-\r
-#define GET_16BIT(endian, cp) \\r
- (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))\r
-\r
-#define PUT_16BIT(endian, cp, val) \\r
- (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))\r
-\r
-const char *const x11_authnames[] = {\r
- "", "MIT-MAGIC-COOKIE-1", "XDM-AUTHORIZATION-1"\r
-};\r
-\r
-struct XDMSeen {\r
- unsigned int time;\r
- unsigned char clientid[6];\r
-};\r
-\r
-struct X11Private {\r
- const struct plug_function_table *fn;\r
- /* the above variable absolutely *must* be the first in this structure */\r
- unsigned char firstpkt[12]; /* first X data packet */\r
- struct X11Display *disp;\r
- char *auth_protocol;\r
- unsigned char *auth_data;\r
- int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;\r
- int verified;\r
- int throttled, throttle_override;\r
- unsigned long peer_ip;\r
- int peer_port;\r
- void *c; /* data used by ssh.c */\r
- Socket s;\r
-};\r
-\r
-static int xdmseen_cmp(void *a, void *b)\r
-{\r
- struct XDMSeen *sa = a, *sb = b;\r
- return sa->time > sb->time ? 1 :\r
- sa->time < sb->time ? -1 :\r
- memcmp(sa->clientid, sb->clientid, sizeof(sa->clientid));\r
-}\r
-\r
-/* Do-nothing "plug" implementation, used by x11_setup_display() when it\r
- * creates a trial connection (and then immediately closes it).\r
- * XXX: bit out of place here, could in principle live in a platform-\r
- * independent network.c or something */\r
-static void dummy_plug_log(Plug p, int type, SockAddr addr, int port,\r
- const char *error_msg, int error_code) { }\r
-static int dummy_plug_closing\r
- (Plug p, const char *error_msg, int error_code, int calling_back)\r
-{ return 1; }\r
-static int dummy_plug_receive(Plug p, int urgent, char *data, int len)\r
-{ return 1; }\r
-static void dummy_plug_sent(Plug p, int bufsize) { }\r
-static int dummy_plug_accepting(Plug p, OSSocket sock) { return 1; }\r
-static const struct plug_function_table dummy_plug = {\r
- dummy_plug_log, dummy_plug_closing, dummy_plug_receive,\r
- dummy_plug_sent, dummy_plug_accepting\r
-};\r
-\r
-struct X11Display *x11_setup_display(char *display, int authtype,\r
- const Config *cfg)\r
-{\r
- struct X11Display *disp = snew(struct X11Display);\r
- char *localcopy;\r
- int i;\r
-\r
- if (!display || !*display) {\r
- localcopy = platform_get_x_display();\r
- if (!localcopy || !*localcopy) {\r
- sfree(localcopy);\r
- localcopy = dupstr(":0"); /* plausible default for any platform */\r
- }\r
- } else\r
- localcopy = dupstr(display);\r
-\r
- /*\r
- * Parse the display name.\r
- *\r
- * We expect this to have one of the following forms:\r
- * \r
- * - the standard X format which looks like\r
- * [ [ protocol '/' ] host ] ':' displaynumber [ '.' screennumber ]\r
- * (X11 also permits a double colon to indicate DECnet, but\r
- * that's not our problem, thankfully!)\r
- *\r
- * - only seen in the wild on MacOS (so far): a pathname to a\r
- * Unix-domain socket, which will typically and confusingly\r
- * end in ":0", and which I'm currently distinguishing from\r
- * the standard scheme by noting that it starts with '/'.\r
- */\r
- if (localcopy[0] == '/') {\r
- disp->unixsocketpath = localcopy;\r
- disp->unixdomain = TRUE;\r
- disp->hostname = NULL;\r
- disp->displaynum = -1;\r
- disp->screennum = 0;\r
- disp->addr = NULL;\r
- } else {\r
- char *colon, *dot, *slash;\r
- char *protocol, *hostname;\r
-\r
- colon = strrchr(localcopy, ':');\r
- if (!colon) {\r
- sfree(disp);\r
- sfree(localcopy);\r
- return NULL; /* FIXME: report a specific error? */\r
- }\r
-\r
- *colon++ = '\0';\r
- dot = strchr(colon, '.');\r
- if (dot)\r
- *dot++ = '\0';\r
-\r
- disp->displaynum = atoi(colon);\r
- if (dot)\r
- disp->screennum = atoi(dot);\r
- else\r
- disp->screennum = 0;\r
-\r
- protocol = NULL;\r
- hostname = localcopy;\r
- if (colon > localcopy) {\r
- slash = strchr(localcopy, '/');\r
- if (slash) {\r
- *slash++ = '\0';\r
- protocol = localcopy;\r
- hostname = slash;\r
- }\r
- }\r
-\r
- disp->hostname = *hostname ? dupstr(hostname) : NULL;\r
-\r
- if (protocol)\r
- disp->unixdomain = (!strcmp(protocol, "local") ||\r
- !strcmp(protocol, "unix"));\r
- else if (!*hostname || !strcmp(hostname, "unix"))\r
- disp->unixdomain = platform_uses_x11_unix_by_default;\r
- else\r
- disp->unixdomain = FALSE;\r
-\r
- if (!disp->hostname && !disp->unixdomain)\r
- disp->hostname = dupstr("localhost");\r
-\r
- disp->unixsocketpath = NULL;\r
- disp->addr = NULL;\r
-\r
- sfree(localcopy);\r
- }\r
-\r
- /*\r
- * Look up the display hostname, if we need to.\r
- */\r
- if (!disp->unixdomain) {\r
- const char *err;\r
-\r
- disp->port = 6000 + disp->displaynum;\r
- disp->addr = name_lookup(disp->hostname, disp->port,\r
- &disp->realhost, cfg, ADDRTYPE_UNSPEC);\r
- \r
- if ((err = sk_addr_error(disp->addr)) != NULL) {\r
- sk_addr_free(disp->addr);\r
- sfree(disp->hostname);\r
- sfree(disp->unixsocketpath);\r
- return NULL; /* FIXME: report an error */\r
- }\r
- }\r
-\r
- /*\r
- * Try upgrading an IP-style localhost display to a Unix-socket\r
- * display (as the standard X connection libraries do).\r
- */\r
- if (!disp->unixdomain && sk_address_is_local(disp->addr)) {\r
- SockAddr ux = platform_get_x11_unix_address(NULL, disp->displaynum);\r
- const char *err = sk_addr_error(ux);\r
- if (!err) {\r
- /* Create trial connection to see if there is a useful Unix-domain\r
- * socket */\r
- const struct plug_function_table *dummy = &dummy_plug;\r
- Socket s = sk_new(sk_addr_dup(ux), 0, 0, 0, 0, 0, (Plug)&dummy);\r
- err = sk_socket_error(s);\r
- sk_close(s);\r
- }\r
- if (err) {\r
- sk_addr_free(ux);\r
- } else {\r
- sk_addr_free(disp->addr);\r
- disp->unixdomain = TRUE;\r
- disp->addr = ux;\r
- /* Fill in the rest in a moment */\r
- }\r
- }\r
-\r
- if (disp->unixdomain) {\r
- if (!disp->addr)\r
- disp->addr = platform_get_x11_unix_address(disp->unixsocketpath,\r
- disp->displaynum);\r
- if (disp->unixsocketpath)\r
- disp->realhost = dupstr(disp->unixsocketpath);\r
- else\r
- disp->realhost = dupprintf("unix:%d", disp->displaynum);\r
- disp->port = 0;\r
- }\r
-\r
- /*\r
- * Invent the remote authorisation details.\r
- */\r
- if (authtype == X11_MIT) {\r
- disp->remoteauthproto = X11_MIT;\r
-\r
- /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */\r
- disp->remoteauthdata = snewn(16, unsigned char);\r
- for (i = 0; i < 16; i++)\r
- disp->remoteauthdata[i] = random_byte();\r
- disp->remoteauthdatalen = 16;\r
-\r
- disp->xdmseen = NULL;\r
- } else {\r
- assert(authtype == X11_XDM);\r
- disp->remoteauthproto = X11_XDM;\r
-\r
- /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */\r
- disp->remoteauthdata = snewn(16, unsigned char);\r
- for (i = 0; i < 16; i++)\r
- disp->remoteauthdata[i] = (i == 8 ? 0 : random_byte());\r
- disp->remoteauthdatalen = 16;\r
-\r
- disp->xdmseen = newtree234(xdmseen_cmp);\r
- }\r
- disp->remoteauthprotoname = dupstr(x11_authnames[disp->remoteauthproto]);\r
- disp->remoteauthdatastring = snewn(disp->remoteauthdatalen * 2 + 1, char);\r
- for (i = 0; i < disp->remoteauthdatalen; i++)\r
- sprintf(disp->remoteauthdatastring + i*2, "%02x",\r
- disp->remoteauthdata[i]);\r
-\r
- /*\r
- * Fetch the local authorisation details.\r
- */\r
- disp->localauthproto = X11_NO_AUTH;\r
- disp->localauthdata = NULL;\r
- disp->localauthdatalen = 0;\r
- platform_get_x11_auth(disp, cfg);\r
-\r
- return disp;\r
-}\r
-\r
-void x11_free_display(struct X11Display *disp)\r
-{\r
- if (disp->xdmseen != NULL) {\r
- struct XDMSeen *seen;\r
- while ((seen = delpos234(disp->xdmseen, 0)) != NULL)\r
- sfree(seen);\r
- freetree234(disp->xdmseen);\r
- }\r
- sfree(disp->hostname);\r
- sfree(disp->unixsocketpath);\r
- if (disp->localauthdata)\r
- memset(disp->localauthdata, 0, disp->localauthdatalen);\r
- sfree(disp->localauthdata);\r
- if (disp->remoteauthdata)\r
- memset(disp->remoteauthdata, 0, disp->remoteauthdatalen);\r
- sfree(disp->remoteauthdata);\r
- sfree(disp->remoteauthprotoname);\r
- sfree(disp->remoteauthdatastring);\r
- sk_addr_free(disp->addr);\r
- sfree(disp);\r
-}\r
-\r
-#define XDM_MAXSKEW 20*60 /* 20 minute clock skew should be OK */\r
-\r
-static char *x11_verify(unsigned long peer_ip, int peer_port,\r
- struct X11Display *disp, char *proto,\r
- unsigned char *data, int dlen)\r
-{\r
- if (strcmp(proto, x11_authnames[disp->remoteauthproto]) != 0)\r
- return "wrong authorisation protocol attempted";\r
- if (disp->remoteauthproto == X11_MIT) {\r
- if (dlen != disp->remoteauthdatalen)\r
- return "MIT-MAGIC-COOKIE-1 data was wrong length";\r
- if (memcmp(disp->remoteauthdata, data, dlen) != 0)\r
- return "MIT-MAGIC-COOKIE-1 data did not match";\r
- }\r
- if (disp->remoteauthproto == X11_XDM) {\r
- unsigned long t;\r
- time_t tim;\r
- int i;\r
- struct XDMSeen *seen, *ret;\r
-\r
- if (dlen != 24)\r
- return "XDM-AUTHORIZATION-1 data was wrong length";\r
- if (peer_port == -1)\r
- return "cannot do XDM-AUTHORIZATION-1 without remote address data";\r
- des_decrypt_xdmauth(disp->remoteauthdata+9, data, 24);\r
- if (memcmp(disp->remoteauthdata, data, 8) != 0)\r
- return "XDM-AUTHORIZATION-1 data failed check"; /* cookie wrong */\r
- if (GET_32BIT_MSB_FIRST(data+8) != peer_ip)\r
- return "XDM-AUTHORIZATION-1 data failed check"; /* IP wrong */\r
- if ((int)GET_16BIT_MSB_FIRST(data+12) != peer_port)\r
- return "XDM-AUTHORIZATION-1 data failed check"; /* port wrong */\r
- t = GET_32BIT_MSB_FIRST(data+14);\r
- for (i = 18; i < 24; i++)\r
- if (data[i] != 0) /* zero padding wrong */\r
- return "XDM-AUTHORIZATION-1 data failed check";\r
- tim = time(NULL);\r
- if (abs(t - tim) > XDM_MAXSKEW)\r
- return "XDM-AUTHORIZATION-1 time stamp was too far out";\r
- seen = snew(struct XDMSeen);\r
- seen->time = t;\r
- memcpy(seen->clientid, data+8, 6);\r
- assert(disp->xdmseen != NULL);\r
- ret = add234(disp->xdmseen, seen);\r
- if (ret != seen) {\r
- sfree(seen);\r
- return "XDM-AUTHORIZATION-1 data replayed";\r
- }\r
- /* While we're here, purge entries too old to be replayed. */\r
- for (;;) {\r
- seen = index234(disp->xdmseen, 0);\r
- assert(seen != NULL);\r
- if (t - seen->time <= XDM_MAXSKEW)\r
- break;\r
- sfree(delpos234(disp->xdmseen, 0));\r
- }\r
- }\r
- /* implement other protocols here if ever required */\r
- return NULL;\r
-}\r
-\r
-void x11_get_auth_from_authfile(struct X11Display *disp,\r
- const char *authfilename)\r
-{\r
- FILE *authfp;\r
- char *buf, *ptr, *str[4];\r
- int len[4];\r
- int family, protocol;\r
- int ideal_match = FALSE;\r
- char *ourhostname = get_hostname();\r
-\r
- /*\r
- * Normally we should look for precisely the details specified in\r
- * `disp'. However, there's an oddity when the display is local:\r
- * displays like "localhost:0" usually have their details stored\r
- * in a Unix-domain-socket record (even if there isn't actually a\r
- * real Unix-domain socket available, as with OpenSSH's proxy X11\r
- * server).\r
- *\r
- * This is apparently a fudge to get round the meaninglessness of\r
- * "localhost" in a shared-home-directory context -- xauth entries\r
- * for Unix-domain sockets already disambiguate this by storing\r
- * the *local* hostname in the conveniently-blank hostname field,\r
- * but IP "localhost" records couldn't do this. So, typically, an\r
- * IP "localhost" entry in the auth database isn't present and if\r
- * it were it would be ignored.\r
- *\r
- * However, we don't entirely trust that (say) Windows X servers\r
- * won't rely on a straight "localhost" entry, bad idea though\r
- * that is; so if we can't find a Unix-domain-socket entry we'll\r
- * fall back to an IP-based entry if we can find one.\r
- */\r
- int localhost = !disp->unixdomain && sk_address_is_local(disp->addr);\r
-\r
- authfp = fopen(authfilename, "rb");\r
- if (!authfp)\r
- return;\r
-\r
- /* Records in .Xauthority contain four strings of up to 64K each */\r
- buf = snewn(65537 * 4, char);\r
-\r
- while (!ideal_match) {\r
- int c, i, j, match = FALSE;\r
- \r
-#define GET do { c = fgetc(authfp); if (c == EOF) goto done; c = (unsigned char)c; } while (0)\r
- /* Expect a big-endian 2-byte number giving address family */\r
- GET; family = c;\r
- GET; family = (family << 8) | c;\r
- /* Then expect four strings, each composed of a big-endian 2-byte\r
- * length field followed by that many bytes of data */\r
- ptr = buf;\r
- for (i = 0; i < 4; i++) {\r
- GET; len[i] = c;\r
- GET; len[i] = (len[i] << 8) | c;\r
- str[i] = ptr;\r
- for (j = 0; j < len[i]; j++) {\r
- GET; *ptr++ = c;\r
- }\r
- *ptr++ = '\0';\r
- }\r
-#undef GET\r
-\r
- /*\r
- * Now we have a full X authority record in memory. See\r
- * whether it matches the display we're trying to\r
- * authenticate to.\r
- *\r
- * The details we've just read should be interpreted as\r
- * follows:\r
- * \r
- * - 'family' is the network address family used to\r
- * connect to the display. 0 means IPv4; 6 means IPv6;\r
- * 256 means Unix-domain sockets.\r
- * \r
- * - str[0] is the network address itself. For IPv4 and\r
- * IPv6, this is a string of binary data of the\r
- * appropriate length (respectively 4 and 16 bytes)\r
- * representing the address in big-endian format, e.g.\r
- * 7F 00 00 01 means IPv4 localhost. For Unix-domain\r
- * sockets, this is the host name of the machine on\r
- * which the Unix-domain display resides (so that an\r
- * .Xauthority file on a shared file system can contain\r
- * authority entries for Unix-domain displays on\r
- * several machines without them clashing).\r
- * \r
- * - str[1] is the display number. I've no idea why\r
- * .Xauthority stores this as a string when it has a\r
- * perfectly good integer format, but there we go.\r
- * \r
- * - str[2] is the authorisation method, encoded as its\r
- * canonical string name (i.e. "MIT-MAGIC-COOKIE-1",\r
- * "XDM-AUTHORIZATION-1" or something we don't\r
- * recognise).\r
- * \r
- * - str[3] is the actual authorisation data, stored in\r
- * binary form.\r
- */\r
-\r
- if (disp->displaynum < 0 || disp->displaynum != atoi(str[1]))\r
- continue; /* not the one */\r
-\r
- for (protocol = 1; protocol < lenof(x11_authnames); protocol++)\r
- if (!strcmp(str[2], x11_authnames[protocol]))\r
- break;\r
- if (protocol == lenof(x11_authnames))\r
- continue; /* don't recognise this protocol, look for another */\r
-\r
- switch (family) {\r
- case 0: /* IPv4 */\r
- if (!disp->unixdomain &&\r
- sk_addrtype(disp->addr) == ADDRTYPE_IPV4) {\r
- char buf[4];\r
- sk_addrcopy(disp->addr, buf);\r
- if (len[0] == 4 && !memcmp(str[0], buf, 4)) {\r
- match = TRUE;\r
- /* If this is a "localhost" entry, note it down\r
- * but carry on looking for a Unix-domain entry. */\r
- ideal_match = !localhost;\r
- }\r
- }\r
- break;\r
- case 6: /* IPv6 */\r
- if (!disp->unixdomain &&\r
- sk_addrtype(disp->addr) == ADDRTYPE_IPV6) {\r
- char buf[16];\r
- sk_addrcopy(disp->addr, buf);\r
- if (len[0] == 16 && !memcmp(str[0], buf, 16)) {\r
- match = TRUE;\r
- ideal_match = !localhost;\r
- }\r
- }\r
- break;\r
- case 256: /* Unix-domain / localhost */\r
- if ((disp->unixdomain || localhost)\r
- && ourhostname && !strcmp(ourhostname, str[0]))\r
- /* A matching Unix-domain socket is always the best\r
- * match. */\r
- match = ideal_match = TRUE;\r
- break;\r
- }\r
-\r
- if (match) {\r
- /* Current best guess -- may be overridden if !ideal_match */\r
- disp->localauthproto = protocol;\r
- sfree(disp->localauthdata); /* free previous guess, if any */\r
- disp->localauthdata = snewn(len[3], unsigned char);\r
- memcpy(disp->localauthdata, str[3], len[3]);\r
- disp->localauthdatalen = len[3];\r
- }\r
- }\r
-\r
- done:\r
- fclose(authfp);\r
- memset(buf, 0, 65537 * 4);\r
- sfree(buf);\r
- sfree(ourhostname);\r
-}\r
-\r
-static void x11_log(Plug p, int type, SockAddr addr, int port,\r
- const char *error_msg, int error_code)\r
-{\r
- /* We have no interface to the logging module here, so we drop these. */\r
-}\r
-\r
-static int x11_closing(Plug plug, const char *error_msg, int error_code,\r
- int calling_back)\r
-{\r
- struct X11Private *pr = (struct X11Private *) plug;\r
-\r
- /*\r
- * We have no way to communicate down the forwarded connection,\r
- * so if an error occurred on the socket, we just ignore it\r
- * and treat it like a proper close.\r
- */\r
- sshfwd_close(pr->c);\r
- x11_close(pr->s);\r
- return 1;\r
-}\r
-\r
-static int x11_receive(Plug plug, int urgent, char *data, int len)\r
-{\r
- struct X11Private *pr = (struct X11Private *) plug;\r
-\r
- if (sshfwd_write(pr->c, data, len) > 0) {\r
- pr->throttled = 1;\r
- sk_set_frozen(pr->s, 1);\r
- }\r
-\r
- return 1;\r
-}\r
-\r
-static void x11_sent(Plug plug, int bufsize)\r
-{\r
- struct X11Private *pr = (struct X11Private *) plug;\r
-\r
- sshfwd_unthrottle(pr->c, bufsize);\r
-}\r
-\r
-/*\r
- * When setting up X forwarding, we should send the screen number\r
- * from the specified local display. This function extracts it from\r
- * the display string.\r
- */\r
-int x11_get_screen_number(char *display)\r
-{\r
- int n;\r
-\r
- n = strcspn(display, ":");\r
- if (!display[n])\r
- return 0;\r
- n = strcspn(display, ".");\r
- if (!display[n])\r
- return 0;\r
- return atoi(display + n + 1);\r
-}\r
-\r
-/*\r
- * Called to set up the raw connection.\r
- * \r
- * Returns an error message, or NULL on success.\r
- * also, fills the SocketsStructure\r
- */\r
-extern const char *x11_init(Socket *s, struct X11Display *disp, void *c,\r
- const char *peeraddr, int peerport,\r
- const Config *cfg)\r
-{\r
- static const struct plug_function_table fn_table = {\r
- x11_log,\r
- x11_closing,\r
- x11_receive,\r
- x11_sent,\r
- NULL\r
- };\r
-\r
- const char *err;\r
- struct X11Private *pr;\r
-\r
- /*\r
- * Open socket.\r
- */\r
- pr = snew(struct X11Private);\r
- pr->fn = &fn_table;\r
- pr->auth_protocol = NULL;\r
- pr->disp = disp;\r
- pr->verified = 0;\r
- pr->data_read = 0;\r
- pr->throttled = pr->throttle_override = 0;\r
- pr->c = c;\r
-\r
- pr->s = *s = new_connection(sk_addr_dup(disp->addr),\r
- disp->realhost, disp->port,\r
- 0, 1, 0, 0, (Plug) pr, cfg);\r
- if ((err = sk_socket_error(*s)) != NULL) {\r
- sfree(pr);\r
- return err;\r
- }\r
-\r
- /*\r
- * See if we can make sense of the peer address we were given.\r
- */\r
- {\r
- int i[4];\r
- if (peeraddr &&\r
- 4 == sscanf(peeraddr, "%d.%d.%d.%d", i+0, i+1, i+2, i+3)) {\r
- pr->peer_ip = (i[0] << 24) | (i[1] << 16) | (i[2] << 8) | i[3];\r
- pr->peer_port = peerport;\r
- } else {\r
- pr->peer_ip = 0;\r
- pr->peer_port = -1;\r
- }\r
- }\r
-\r
- sk_set_private_ptr(*s, pr);\r
- return NULL;\r
-}\r
-\r
-void x11_close(Socket s)\r
-{\r
- struct X11Private *pr;\r
- if (!s)\r
- return;\r
- pr = (struct X11Private *) sk_get_private_ptr(s);\r
- if (pr->auth_protocol) {\r
- sfree(pr->auth_protocol);\r
- sfree(pr->auth_data);\r
- }\r
-\r
- sfree(pr);\r
-\r
- sk_close(s);\r
-}\r
-\r
-void x11_unthrottle(Socket s)\r
-{\r
- struct X11Private *pr;\r
- if (!s)\r
- return;\r
- pr = (struct X11Private *) sk_get_private_ptr(s);\r
-\r
- pr->throttled = 0;\r
- sk_set_frozen(s, pr->throttled || pr->throttle_override);\r
-}\r
-\r
-void x11_override_throttle(Socket s, int enable)\r
-{\r
- struct X11Private *pr;\r
- if (!s)\r
- return;\r
- pr = (struct X11Private *) sk_get_private_ptr(s);\r
-\r
- pr->throttle_override = enable;\r
- sk_set_frozen(s, pr->throttled || pr->throttle_override);\r
-}\r
-\r
-/*\r
- * Called to send data down the raw connection.\r
- */\r
-int x11_send(Socket s, char *data, int len)\r
-{\r
- struct X11Private *pr;\r
- if (!s)\r
- return 0;\r
- pr = (struct X11Private *) sk_get_private_ptr(s);\r
-\r
- /*\r
- * Read the first packet.\r
- */\r
- while (len > 0 && pr->data_read < 12)\r
- pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);\r
- if (pr->data_read < 12)\r
- return 0;\r
-\r
- /*\r
- * If we have not allocated the auth_protocol and auth_data\r
- * strings, do so now.\r
- */\r
- if (!pr->auth_protocol) {\r
- pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6);\r
- pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8);\r
- pr->auth_psize = (pr->auth_plen + 3) & ~3;\r
- pr->auth_dsize = (pr->auth_dlen + 3) & ~3;\r
- /* Leave room for a terminating zero, to make our lives easier. */\r
- pr->auth_protocol = snewn(pr->auth_psize + 1, char);\r
- pr->auth_data = snewn(pr->auth_dsize, unsigned char);\r
- }\r
-\r
- /*\r
- * Read the auth_protocol and auth_data strings.\r
- */\r
- while (len > 0 && pr->data_read < 12 + pr->auth_psize)\r
- pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);\r
- while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)\r
- pr->auth_data[pr->data_read++ - 12 -\r
- pr->auth_psize] = (unsigned char) (len--, *data++);\r
- if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)\r
- return 0;\r
-\r
- /*\r
- * If we haven't verified the authorisation, do so now.\r
- */\r
- if (!pr->verified) {\r
- char *err;\r
-\r
- pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */\r
- err = x11_verify(pr->peer_ip, pr->peer_port,\r
- pr->disp, pr->auth_protocol,\r
- pr->auth_data, pr->auth_dlen);\r
-\r
- /*\r
- * If authorisation failed, construct and send an error\r
- * packet, then terminate the connection.\r
- */\r
- if (err) {\r
- char *message;\r
- int msglen, msgsize;\r
- unsigned char *reply;\r
-\r
- message = dupprintf("%s X11 proxy: %s", appname, err);\r
- msglen = strlen(message);\r
- reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */\r
- msgsize = (msglen + 3) & ~3;\r
- reply[0] = 0; /* failure */\r
- reply[1] = msglen; /* length of reason string */\r
- memcpy(reply + 2, pr->firstpkt + 2, 4); /* major/minor proto vsn */\r
- PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */\r
- memset(reply + 8, 0, msgsize);\r
- memcpy(reply + 8, message, msglen);\r
- sshfwd_write(pr->c, (char *)reply, 8 + msgsize);\r
- sshfwd_close(pr->c);\r
- x11_close(s);\r
- sfree(reply);\r
- sfree(message);\r
- return 0;\r
- }\r
-\r
- /*\r
- * Now we know we're going to accept the connection. Strip\r
- * the fake auth data, and optionally put real auth data in\r
- * instead.\r
- */\r
- {\r
- char realauthdata[64];\r
- int realauthlen = 0;\r
- int authstrlen = strlen(x11_authnames[pr->disp->localauthproto]);\r
- int buflen = 0; /* initialise to placate optimiser */\r
- static const char zeroes[4] = { 0,0,0,0 };\r
- void *buf;\r
-\r
- if (pr->disp->localauthproto == X11_MIT) {\r
- assert(pr->disp->localauthdatalen <= lenof(realauthdata));\r
- realauthlen = pr->disp->localauthdatalen;\r
- memcpy(realauthdata, pr->disp->localauthdata, realauthlen);\r
- } else if (pr->disp->localauthproto == X11_XDM &&\r
- pr->disp->localauthdatalen == 16 &&\r
- ((buf = sk_getxdmdata(s, &buflen))!=0)) {\r
- time_t t;\r
- realauthlen = (buflen+12+7) & ~7;\r
- assert(realauthlen <= lenof(realauthdata));\r
- memset(realauthdata, 0, realauthlen);\r
- memcpy(realauthdata, pr->disp->localauthdata, 8);\r
- memcpy(realauthdata+8, buf, buflen);\r
- t = time(NULL);\r
- PUT_32BIT_MSB_FIRST(realauthdata+8+buflen, t);\r
- des_encrypt_xdmauth(pr->disp->localauthdata+9,\r
- (unsigned char *)realauthdata,\r
- realauthlen);\r
- sfree(buf);\r
- }\r
- /* implement other auth methods here if required */\r
-\r
- PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen);\r
- PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen);\r
- \r
- sk_write(s, (char *)pr->firstpkt, 12);\r
-\r
- if (authstrlen) {\r
- sk_write(s, x11_authnames[pr->disp->localauthproto],\r
- authstrlen);\r
- sk_write(s, zeroes, 3 & (-authstrlen));\r
- }\r
- if (realauthlen) {\r
- sk_write(s, realauthdata, realauthlen);\r
- sk_write(s, zeroes, 3 & (-realauthlen));\r
- }\r
- }\r
- pr->verified = 1;\r
- }\r
-\r
- /*\r
- * After initialisation, just copy data simply.\r
- */\r
-\r
- return sk_write(s, data, len);\r
-}\r