int verified;
int throttled, throttle_override;
int no_data_sent_to_x_client;
- unsigned long peer_ip;
+ char *peer_addr;
int peer_port;
struct ssh_channel *c; /* channel structure held by ssh.c */
Socket s;
struct X11FakeAuth *auth = snew(struct X11FakeAuth);
int i;
+ /*
+ * This function has the job of inventing a set of X11 fake auth
+ * data, and adding it to 'authtree'. We must preserve the
+ * property that for any given actual authorisation attempt, _at
+ * most one_ thing in the tree can possibly match it.
+ *
+ * For MIT-MAGIC-COOKIE-1, that's not too difficult: the match
+ * criterion is simply that the entire cookie is correct, so we
+ * just have to make sure we don't make up two cookies the same.
+ * (Vanishingly unlikely, but we check anyway to be sure, and go
+ * round again inventing a new cookie if add234 tells us the one
+ * we thought of is already in use.)
+ *
+ * For XDM-AUTHORIZATION-1, it's a little more fiddly. The setup
+ * with XA1 is that half the cookie is used as a DES key with
+ * which to CBC-encrypt an assortment of stuff. Happily, the stuff
+ * encrypted _begins_ with the other half of the cookie, and the
+ * IV is always zero, which means that any valid XA1 authorisation
+ * attempt for a given cookie must begin with the same cipher
+ * block, consisting of the DES ECB encryption of the first half
+ * of the cookie using the second half as a key. So we compute
+ * that cipher block here and now, and use it as the sorting key
+ * for distinguishing XA1 entries in the tree.
+ */
+
if (authtype == X11_MIT) {
auth->proto = X11_MIT;
sprintf(auth->datastring + i*2, "%02x",
auth->data[i]);
+ auth->disp = NULL;
+ auth->share_cs = auth->share_chan = NULL;
+
return auth;
}
}
}
-struct X11Display *x11_setup_display(char *display, Conf *conf)
+struct X11Display *x11_setup_display(const char *display, Conf *conf)
{
struct X11Display *disp = snew(struct X11Display);
char *localcopy;
char *colon, *dot, *slash;
char *protocol, *hostname;
- colon = strrchr(localcopy, ':');
+ colon = host_strrchr(localcopy, ':');
if (!colon) {
sfree(disp);
sfree(localcopy);
disp->port = 6000 + disp->displaynum;
disp->addr = name_lookup(disp->hostname, disp->port,
- &disp->realhost, conf, ADDRTYPE_UNSPEC);
+ &disp->realhost, conf, ADDRTYPE_UNSPEC,
+ NULL, NULL);
if ((err = sk_addr_error(disp->addr)) != NULL) {
sk_addr_free(disp->addr);
#define XDM_MAXSKEW 20*60 /* 20 minute clock skew should be OK */
-static char *x11_verify(unsigned long peer_ip, int peer_port,
- tree234 *authtree, char *proto,
- unsigned char *data, int dlen,
- struct X11FakeAuth **auth_ret)
+static const char *x11_verify(unsigned long peer_ip, int peer_port,
+ tree234 *authtree, char *proto,
+ unsigned char *data, int dlen,
+ struct X11FakeAuth **auth_ret)
{
struct X11FakeAuth match_dummy; /* for passing to find234 */
struct X11FakeAuth *auth;
* record that _might_ match.
*/
if (!strcmp(proto, x11_authnames[X11_MIT])) {
+ /*
+ * Just look up the whole cookie that was presented to us,
+ * which x11_authcmp will compare against the cookies we
+ * currently believe in.
+ */
match_dummy.proto = X11_MIT;
match_dummy.datalen = dlen;
match_dummy.data = data;
} else if (!strcmp(proto, x11_authnames[X11_XDM])) {
+ /*
+ * Look up the first cipher block, against the stored first
+ * cipher blocks for the XDM-AUTHORIZATION-1 cookies we
+ * currently know. (See comment in x11_invent_fake_auth.)
+ */
match_dummy.proto = X11_XDM;
match_dummy.xa1_firstblock = data;
} else {
if (data[i] != 0) /* zero padding wrong */
return "XDM-AUTHORIZATION-1 data failed check";
tim = time(NULL);
- if (abs(t - tim) > XDM_MAXSKEW)
+ if (((unsigned long)t - (unsigned long)tim
+ + XDM_MAXSKEW) > 2*XDM_MAXSKEW)
return "XDM-AUTHORIZATION-1 time stamp was too far out";
seen = snew(struct XDMSeen);
seen->time = t;
{
int n;
- n = strcspn(display, ":");
+ n = host_strcspn(display, ":");
if (!display[n])
return 0;
n = strcspn(display, ".");
}
/*
- * Called to set up the raw connection.
- *
- * On success, returns NULL and fills in *xconnret. On error, returns
- * a dynamically allocated error message string.
+ * Called to set up the X11Connection structure, though this does not
+ * yet connect to an actual server.
*/
-extern char *x11_init(struct X11Connection **xconnret,
- tree234 *authtree, void *c,
- const char *peeraddr, int peerport)
+struct X11Connection *x11_init(tree234 *authtree, void *c,
+ const char *peeraddr, int peerport)
{
static const struct plug_function_table fn_table = {
x11_log,
/*
* Open socket.
*/
- xconn = *xconnret = snew(struct X11Connection);
+ xconn = snew(struct X11Connection);
xconn->fn = &fn_table;
xconn->auth_protocol = NULL;
xconn->authtree = authtree;
xconn->s = NULL;
/*
- * See if we can make sense of the peer address we were given.
+ * Stash the peer address we were given in its original text form.
*/
- {
- int i[4];
- if (peeraddr &&
- 4 == sscanf(peeraddr, "%d.%d.%d.%d", i+0, i+1, i+2, i+3)) {
- xconn->peer_ip = (i[0] << 24) | (i[1] << 16) | (i[2] << 8) | i[3];
- xconn->peer_port = peerport;
- } else {
- xconn->peer_ip = 0;
- xconn->peer_port = -1;
- }
- }
+ xconn->peer_addr = peeraddr ? dupstr(peeraddr) : NULL;
+ xconn->peer_port = peerport;
- return NULL;
+ return xconn;
}
void x11_close(struct X11Connection *xconn)
if (xconn->s)
sk_close(xconn->s);
+
+ sfree(xconn->peer_addr);
sfree(xconn);
}
sfree(full_message);
}
+static int x11_parse_ip(const char *addr_string, unsigned long *ip)
+{
+
+ /*
+ * See if we can make sense of this string as an IPv4 address, for
+ * XDM-AUTHORIZATION-1 purposes.
+ */
+ int i[4];
+ if (addr_string &&
+ 4 == sscanf(addr_string, "%d.%d.%d.%d", i+0, i+1, i+2, i+3)) {
+ *ip = (i[0] << 24) | (i[1] << 16) | (i[2] << 8) | i[3];
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+}
+
/*
* Called to send data down the raw connection.
*/
if (!xconn->verified) {
const char *err;
struct X11FakeAuth *auth_matched = NULL;
+ unsigned long peer_ip;
+ int peer_port;
+ int protomajor, protominor;
+ void *greeting;
+ int greeting_len;
+ unsigned char *socketdata;
+ int socketdatalen;
+ char new_peer_addr[32];
+ int new_peer_port;
+
+ protomajor = GET_16BIT(xconn->firstpkt[0], xconn->firstpkt + 2);
+ protominor = GET_16BIT(xconn->firstpkt[0], xconn->firstpkt + 4);
assert(!xconn->s);
xconn->auth_protocol[xconn->auth_plen] = '\0'; /* ASCIZ */
- err = x11_verify(xconn->peer_ip, xconn->peer_port,
+
+ peer_ip = 0; /* placate optimiser */
+ if (x11_parse_ip(xconn->peer_addr, &peer_ip))
+ peer_port = xconn->peer_port;
+ else
+ peer_port = -1; /* signal no peer address data available */
+
+ err = x11_verify(peer_ip, peer_port,
xconn->authtree, xconn->auth_protocol,
xconn->auth_data, xconn->auth_dlen, &auth_matched);
if (err) {
}
assert(auth_matched);
+ /*
+ * If this auth points to a connection-sharing downstream
+ * rather than an X display we know how to connect to
+ * directly, pass it off to the sharing module now.
+ */
+ if (auth_matched->share_cs) {
+ sshfwd_x11_sharing_handover(xconn->c, auth_matched->share_cs,
+ auth_matched->share_chan,
+ xconn->peer_addr, xconn->peer_port,
+ xconn->firstpkt[0],
+ protomajor, protominor, data, len);
+ return 0;
+ }
+
/*
* Now we know we're going to accept the connection, and what
* X display to connect to. Actually connect to it.
*/
+ sshfwd_x11_is_local(xconn->c);
xconn->disp = auth_matched->disp;
xconn->s = new_connection(sk_addr_dup(xconn->disp->addr),
xconn->disp->realhost, xconn->disp->port,
}
/*
- * Strip the fake auth data, and optionally put real auth data
- * in instead.
+ * Write a new connection header containing our replacement
+ * auth data.
*/
- {
- 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 };
- void *buf;
-
- if (xconn->disp->localauthproto == X11_MIT) {
- assert(xconn->disp->localauthdatalen <= lenof(realauthdata));
- realauthlen = xconn->disp->localauthdatalen;
- memcpy(realauthdata, xconn->disp->localauthdata, realauthlen);
- } else if (xconn->disp->localauthproto == X11_XDM &&
- xconn->disp->localauthdatalen == 16 &&
- ((buf = sk_getxdmdata(xconn->s, &buflen))!=0)) {
- time_t t;
- realauthlen = (buflen+12+7) & ~7;
- assert(realauthlen <= lenof(realauthdata));
- memset(realauthdata, 0, realauthlen);
- memcpy(realauthdata, xconn->disp->localauthdata, 8);
- memcpy(realauthdata+8, buf, buflen);
- t = time(NULL);
- PUT_32BIT_MSB_FIRST(realauthdata+8+buflen, t);
- des_encrypt_xdmauth(xconn->disp->localauthdata+9,
- (unsigned char *)realauthdata,
- realauthlen);
- sfree(buf);
- }
- /* implement other auth methods here if required */
+ socketdatalen = 0; /* placate compiler warning */
+ socketdata = sk_getxdmdata(xconn->s, &socketdatalen);
+ if (socketdata && socketdatalen==6) {
+ sprintf(new_peer_addr, "%d.%d.%d.%d", socketdata[0],
+ socketdata[1], socketdata[2], socketdata[3]);
+ new_peer_port = GET_16BIT_MSB_FIRST(socketdata + 4);
+ } else {
+ strcpy(new_peer_addr, "0.0.0.0");
+ new_peer_port = 0;
+ }
- PUT_16BIT(xconn->firstpkt[0], xconn->firstpkt + 6, authstrlen);
- PUT_16BIT(xconn->firstpkt[0], xconn->firstpkt + 8, realauthlen);
+ greeting = x11_make_greeting(xconn->firstpkt[0],
+ protomajor, protominor,
+ xconn->disp->localauthproto,
+ xconn->disp->localauthdata,
+ xconn->disp->localauthdatalen,
+ new_peer_addr, new_peer_port,
+ &greeting_len);
- sk_write(xconn->s, (char *)xconn->firstpkt, 12);
-
- if (authstrlen) {
- sk_write(xconn->s, x11_authnames[xconn->disp->localauthproto],
- authstrlen);
- sk_write(xconn->s, zeroes, 3 & (-authstrlen));
- }
- if (realauthlen) {
- sk_write(xconn->s, realauthdata, realauthlen);
- sk_write(xconn->s, zeroes, 3 & (-realauthlen));
- }
- }
+ sk_write(xconn->s, greeting, greeting_len);
+
+ smemclr(greeting, greeting_len);
+ sfree(greeting);
+
+ /*
+ * Now we're done.
+ */
xconn->verified = 1;
}
sshfwd_write_eof(xconn->c);
}
}
+
+/*
+ * Utility functions used by connection sharing to convert textual
+ * representations of an X11 auth protocol name + hex cookie into our
+ * usual integer protocol id and binary auth data.
+ */
+int x11_identify_auth_proto(const char *protoname)
+{
+ int protocol;
+
+ for (protocol = 1; protocol < lenof(x11_authnames); protocol++)
+ if (!strcmp(protoname, x11_authnames[protocol]))
+ return protocol;
+ return -1;
+}
+
+void *x11_dehexify(const char *hex, int *outlen)
+{
+ int len, i;
+ unsigned char *ret;
+
+ len = strlen(hex) / 2;
+ ret = snewn(len, unsigned char);
+
+ for (i = 0; i < len; i++) {
+ char bytestr[3];
+ unsigned val = 0;
+ bytestr[0] = hex[2*i];
+ bytestr[1] = hex[2*i+1];
+ bytestr[2] = '\0';
+ sscanf(bytestr, "%x", &val);
+ ret[i] = val;
+ }
+
+ *outlen = len;
+ return ret;
+}
+
+/*
+ * Construct an X11 greeting packet, including making up the right
+ * authorisation data.
+ */
+void *x11_make_greeting(int endian, int protomajor, int protominor,
+ int auth_proto, const void *auth_data, int auth_len,
+ const char *peer_addr, int peer_port,
+ int *outlen)
+{
+ unsigned char *greeting;
+ unsigned char realauthdata[64];
+ const char *authname;
+ const unsigned char *authdata;
+ int authnamelen, authnamelen_pad;
+ int authdatalen, authdatalen_pad;
+ int greeting_len;
+
+ authname = x11_authnames[auth_proto];
+ authnamelen = strlen(authname);
+ authnamelen_pad = (authnamelen + 3) & ~3;
+
+ if (auth_proto == X11_MIT) {
+ authdata = auth_data;
+ authdatalen = auth_len;
+ } else if (auth_proto == X11_XDM && auth_len == 16) {
+ time_t t;
+ unsigned long peer_ip = 0;
+
+ x11_parse_ip(peer_addr, &peer_ip);
+
+ authdata = realauthdata;
+ authdatalen = 24;
+ memset(realauthdata, 0, authdatalen);
+ memcpy(realauthdata, auth_data, 8);
+ PUT_32BIT_MSB_FIRST(realauthdata+8, peer_ip);
+ PUT_16BIT_MSB_FIRST(realauthdata+12, peer_port);
+ t = time(NULL);
+ PUT_32BIT_MSB_FIRST(realauthdata+14, t);
+
+ des_encrypt_xdmauth((const unsigned char *)auth_data + 9,
+ realauthdata, authdatalen);
+ } else {
+ authdata = realauthdata;
+ authdatalen = 0;
+ }
+
+ authdatalen_pad = (authdatalen + 3) & ~3;
+ greeting_len = 12 + authnamelen_pad + authdatalen_pad;
+
+ greeting = snewn(greeting_len, unsigned char);
+ memset(greeting, 0, greeting_len);
+ greeting[0] = endian;
+ PUT_16BIT(endian, greeting+2, protomajor);
+ PUT_16BIT(endian, greeting+4, protominor);
+ PUT_16BIT(endian, greeting+6, authnamelen);
+ PUT_16BIT(endian, greeting+8, authdatalen);
+ memcpy(greeting+12, authname, authnamelen);
+ memcpy(greeting+12+authnamelen_pad, authdata, authdatalen);
+
+ smemclr(realauthdata, sizeof(realauthdata));
+
+ *outlen = greeting_len;
+ return greeting;
+}