+
+ /*
+ * Now we're authenticated for the connection protocol. The
+ * connection protocol will automatically have started at this
+ * point; there's no need to send SERVICE_REQUEST.
+ */
+
+ /*
+ * So now create a channel with a session in it.
+ */
+ mainchan = smalloc(sizeof(struct ssh_channel));
+ mainchan->localid = 100; /* as good as any */
+ ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);
+ ssh2_pkt_addstring("session");
+ ssh2_pkt_adduint32(mainchan->localid);
+ ssh2_pkt_adduint32(0x8000UL); /* our window size */
+ ssh2_pkt_adduint32(0x4000UL); /* our max pkt size */
+ ssh2_pkt_send();
+ crWaitUntilV(ispkt);
+ if (pktin.type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) {
+ bombout(("Server refused to open a session"));
+ crReturnV;
+ /* FIXME: error data comes back in FAILURE packet */
+ }
+ if (ssh2_pkt_getuint32() != mainchan->localid) {
+ bombout(("Server's channel confirmation cited wrong channel"));
+ crReturnV;
+ }
+ mainchan->remoteid = ssh2_pkt_getuint32();
+ mainchan->type = CHAN_MAINSESSION;
+ mainchan->closes = 0;
+ mainchan->v2.remwindow = ssh2_pkt_getuint32();
+ mainchan->v2.remmaxpkt = ssh2_pkt_getuint32();
+ mainchan->v2.outbuffer = NULL;
+ mainchan->v2.outbuflen = mainchan->v2.outbufsize = 0;
+ ssh_channels = newtree234(ssh_channelcmp);
+ add234(ssh_channels, mainchan);
+ logevent("Opened channel for session");
+
+ /*
+ * Potentially enable X11 forwarding.
+ */
+ if (cfg.x11_forward) {
+ char proto[20], data[64];
+ logevent("Requesting X11 forwarding");
+ x11_invent_auth(proto, sizeof(proto), data, sizeof(data));
+ ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
+ ssh2_pkt_adduint32(mainchan->remoteid);
+ ssh2_pkt_addstring("x11-req");
+ ssh2_pkt_addbool(1); /* want reply */
+ ssh2_pkt_addbool(0); /* many connections */
+ ssh2_pkt_addstring(proto);
+ ssh2_pkt_addstring(data);
+ ssh2_pkt_adduint32(0); /* screen number */
+ ssh2_pkt_send();
+
+ do {
+ crWaitUntilV(ispkt);
+ if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
+ unsigned i = ssh2_pkt_getuint32();
+ struct ssh_channel *c;
+ c = find234(ssh_channels, &i, ssh_channelfind);
+ if (!c)
+ continue; /* nonexistent channel */
+ c->v2.remwindow += ssh2_pkt_getuint32();
+ }
+ } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST);
+
+ if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) {
+ if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) {
+ bombout(("Server got confused by X11 forwarding request"));
+ crReturnV;
+ }
+ logevent("X11 forwarding refused");
+ } else {
+ logevent("X11 forwarding enabled");
+ ssh_X11_fwd_enabled = TRUE;
+ }
+ }
+
+ /*
+ * Now allocate a pty for the session.
+ */
+ if (!cfg.nopty) {
+ ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
+ ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */
+ ssh2_pkt_addstring("pty-req");
+ ssh2_pkt_addbool(1); /* want reply */
+ ssh2_pkt_addstring(cfg.termtype);
+ ssh2_pkt_adduint32(cols);
+ ssh2_pkt_adduint32(rows);
+ ssh2_pkt_adduint32(0); /* pixel width */
+ ssh2_pkt_adduint32(0); /* pixel height */
+ ssh2_pkt_addstring_start();
+ ssh2_pkt_addstring_data("\0", 1);/* TTY_OP_END, no special options */
+ ssh2_pkt_send();
+ ssh_state = SSH_STATE_INTERMED;
+
+ do {
+ crWaitUntilV(ispkt);
+ if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
+ unsigned i = ssh2_pkt_getuint32();
+ struct ssh_channel *c;
+ c = find234(ssh_channels, &i, ssh_channelfind);
+ if (!c)
+ continue; /* nonexistent channel */
+ c->v2.remwindow += ssh2_pkt_getuint32();
+ }
+ } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST);
+
+ if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) {
+ if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) {
+ bombout(("Server got confused by pty request"));
+ crReturnV;
+ }
+ c_write("Server refused to allocate pty\r\n", 32);
+ ssh_editing = ssh_echoing = 1;
+ } else {
+ logevent("Allocated pty");
+ }
+ } else {
+ ssh_editing = ssh_echoing = 1;
+ }
+
+ /*
+ * Start a shell or a remote command.
+ */
+ ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
+ ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */
+ if (cfg.ssh_subsys) {
+ ssh2_pkt_addstring("subsystem");
+ ssh2_pkt_addbool(1); /* want reply */
+ ssh2_pkt_addstring(cfg.remote_cmd);
+ } else if (*cfg.remote_cmd) {
+ ssh2_pkt_addstring("exec");
+ ssh2_pkt_addbool(1); /* want reply */
+ ssh2_pkt_addstring(cfg.remote_cmd);
+ } else {
+ ssh2_pkt_addstring("shell");
+ ssh2_pkt_addbool(1); /* want reply */
+ }
+ ssh2_pkt_send();
+ do {
+ crWaitUntilV(ispkt);
+ if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
+ unsigned i = ssh2_pkt_getuint32();
+ struct ssh_channel *c;
+ c = find234(ssh_channels, &i, ssh_channelfind);
+ if (!c)
+ continue; /* nonexistent channel */
+ c->v2.remwindow += ssh2_pkt_getuint32();
+ }
+ } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST);
+ if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) {
+ if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) {
+ bombout(("Server got confused by shell/command request"));
+ crReturnV;
+ }
+ bombout(("Server refused to start a shell/command"));
+ crReturnV;
+ } else {
+ logevent("Started a shell/command");
+ }
+
+ ssh_state = SSH_STATE_SESSION;
+ if (size_needed)
+ ssh_size();
+ if (eof_needed)
+ ssh_special(TS_EOF);
+
+ /*
+ * Transfer data!
+ */
+ ldisc_send(NULL, 0); /* cause ldisc to notice changes */
+ ssh_send_ok = 1;
+ while (1) {
+ static int try_send;
+ crReturnV;
+ try_send = FALSE;
+ if (ispkt) {
+ if (pktin.type == SSH2_MSG_CHANNEL_DATA ||
+ pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA) {
+ char *data;
+ int length;
+ unsigned i = ssh2_pkt_getuint32();
+ struct ssh_channel *c;
+ c = find234(ssh_channels, &i, ssh_channelfind);
+ if (!c)
+ continue; /* nonexistent channel */
+ if (pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA &&
+ ssh2_pkt_getuint32() != SSH2_EXTENDED_DATA_STDERR)
+ continue; /* extended but not stderr */
+ ssh2_pkt_getstring(&data, &length);
+ if (data) {
+ switch (c->type) {
+ case CHAN_MAINSESSION:
+ from_backend(pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA,
+ data, length);
+ break;
+ case CHAN_X11:
+ x11_send(c->u.x11.s, data, length);
+ break;
+ }
+ /*
+ * Enlarge the window again at the remote
+ * side, just in case it ever runs down and
+ * they fail to send us any more data.
+ */
+ ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
+ ssh2_pkt_adduint32(c->remoteid);
+ ssh2_pkt_adduint32(length);
+ ssh2_pkt_send();
+ }
+ } else if (pktin.type == SSH2_MSG_DISCONNECT) {
+ ssh_state = SSH_STATE_CLOSED;
+ logevent("Received disconnect message");
+ crReturnV;
+ } else if (pktin.type == SSH2_MSG_CHANNEL_REQUEST) {
+ continue; /* exit status et al; ignore (FIXME?) */
+ } else if (pktin.type == SSH2_MSG_CHANNEL_EOF) {
+ unsigned i = ssh2_pkt_getuint32();
+ struct ssh_channel *c;
+
+ c = find234(ssh_channels, &i, ssh_channelfind);
+ if (!c)
+ continue; /* nonexistent channel */
+
+ if (c->type == CHAN_X11) {
+ /*
+ * Remote EOF on an X11 channel means we should
+ * wrap up and close the channel ourselves.
+ */
+ x11_close(c->u.x11.s);
+ sshfwd_close(c);
+ }
+ } else if (pktin.type == SSH2_MSG_CHANNEL_CLOSE) {
+ unsigned i = ssh2_pkt_getuint32();
+ struct ssh_channel *c;
+ enum234 e;
+
+ c = find234(ssh_channels, &i, ssh_channelfind);
+ if (!c)
+ continue; /* nonexistent channel */
+ if (c->closes == 0) {
+ ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);
+ ssh2_pkt_adduint32(c->remoteid);
+ ssh2_pkt_send();
+ }
+ /* Do pre-close processing on the channel. */
+ switch (c->type) {
+ case CHAN_MAINSESSION:
+ break; /* nothing to see here, move along */
+ case CHAN_X11:
+ break;
+ }
+ del234(ssh_channels, c);
+ sfree(c->v2.outbuffer);
+ sfree(c);
+
+ /*
+ * See if that was the last channel left open.
+ */
+ c = first234(ssh_channels, &e);
+ if (!c) {
+ logevent("All channels closed. Disconnecting");
+ ssh2_pkt_init(SSH2_MSG_DISCONNECT);
+ ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION);
+ ssh2_pkt_addstring("All open channels closed");
+ ssh2_pkt_addstring("en"); /* language tag */
+ ssh2_pkt_send();
+ ssh_state = SSH_STATE_CLOSED;
+ crReturnV;
+ }
+ continue; /* remote sends close; ignore (FIXME) */
+ } else if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
+ unsigned i = ssh2_pkt_getuint32();
+ struct ssh_channel *c;
+ c = find234(ssh_channels, &i, ssh_channelfind);
+ if (!c)
+ continue; /* nonexistent channel */
+ mainchan->v2.remwindow += ssh2_pkt_getuint32();
+ try_send = TRUE;
+ } else if (pktin.type == SSH2_MSG_CHANNEL_OPEN) {
+ char *type;
+ int typelen;
+ char *error = NULL;
+ struct ssh_channel *c;
+ ssh2_pkt_getstring(&type, &typelen);
+ c = smalloc(sizeof(struct ssh_channel));
+
+ if (typelen == 3 && !memcmp(type, "x11", 3)) {
+ if (!ssh_X11_fwd_enabled)
+ error = "X11 forwarding is not enabled";
+ else if ( x11_init(&c->u.x11.s, cfg.x11_display, c) != NULL ) {
+ error = "Unable to open an X11 connection";
+ } else {
+ c->type = CHAN_X11;
+ }
+ } else {
+ error = "Unsupported channel type requested";
+ }
+
+ c->remoteid = ssh2_pkt_getuint32();
+ if (error) {
+ ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE);
+ ssh2_pkt_adduint32(c->remoteid);
+ ssh2_pkt_adduint32(SSH2_OPEN_CONNECT_FAILED);
+ ssh2_pkt_addstring(error);
+ ssh2_pkt_addstring("en"); /* language tag */
+ ssh2_pkt_send();
+ sfree(c);
+ } else {
+ struct ssh_channel *d;
+ unsigned i;
+ enum234 e;
+
+ for (i=1, d = first234(ssh_channels, &e); d;
+ d = next234(&e)) {
+ if (d->localid > i)
+ break; /* found a free number */
+ i = d->localid + 1;
+ }
+ c->localid = i;
+ c->closes = 0;
+ c->v2.remwindow = ssh2_pkt_getuint32();
+ c->v2.remmaxpkt = ssh2_pkt_getuint32();
+ c->v2.outbuffer = NULL;
+ c->v2.outbuflen = c->v2.outbufsize = 0;
+ add234(ssh_channels, c);
+ ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
+ ssh2_pkt_adduint32(c->remoteid);
+ ssh2_pkt_adduint32(c->localid);
+ ssh2_pkt_adduint32(0x8000UL); /* our window size */
+ ssh2_pkt_adduint32(0x4000UL); /* our max pkt size */
+ ssh2_pkt_send();
+ }
+ } else {
+ bombout(("Strange packet received: type %d", pktin.type));
+ crReturnV;
+ }
+ } else {
+ /*
+ * We have spare data. Add it to the channel buffer.
+ */
+ ssh2_add_channel_data(mainchan, in, inlen);
+ try_send = TRUE;
+ }
+ if (try_send) {
+ enum234 e;
+ struct ssh_channel *c;
+ /*
+ * Try to send data on all channels if we can.
+ */
+ for (c = first234(ssh_channels, &e); c; c = next234(&e))
+ ssh2_try_send(c);
+ }
+ }
+
+ crFinishV;
+}
+
+/*
+ * Handle the top-level SSH2 protocol.
+ */
+static void ssh2_protocol(unsigned char *in, int inlen, int ispkt)
+{
+ if (do_ssh2_transport(in, inlen, ispkt) == 0)
+ return;
+ do_ssh2_authconn(in, inlen, ispkt);
+}
+
+/*
+ * Called to set up the connection.
+ *
+ * Returns an error message, or NULL on success.
+ */
+static char *ssh_init (char *host, int port, char **realhost) {
+ char *p;
+
+#ifdef MSCRYPTOAPI
+ if(crypto_startup() == 0)
+ return "Microsoft high encryption pack not installed!";
+#endif
+
+ ssh_send_ok = 0;
+ ssh_editing = 0;
+ ssh_echoing = 0;
+
+ p = connect_to_host(host, port, realhost);
+ if (p != NULL)
+ return p;
+
+ return NULL;