+/* ----------------------------------------------------------------------
+ * Dirty bits: integration with PuTTY.
+ */
+
+static int verbose = 0;
+
+void verify_ssh_host_key(char *host, int port, char *keytype,
+ char *keystr, char *fingerprint)
+{
+ int ret;
+ HANDLE hin;
+ DWORD savemode, i;
+
+ static const char absentmsg[] =
+ "The server's host key is not cached in the registry. You\n"
+ "have no guarantee that the server is the computer you\n"
+ "think it is.\n"
+ "The server's key fingerprint is:\n"
+ "%s\n"
+ "If you trust this host, enter \"y\" to add the key to\n"
+ "PuTTY's cache and carry on connecting.\n"
+ "If you want to carry on connecting just once, without\n"
+ "adding the key to the cache, enter \"n\".\n"
+ "If you do not trust this host, press Return to abandon the\n"
+ "connection.\n"
+ "Store key in cache? (y/n) ";
+
+ static const char wrongmsg[] =
+ "WARNING - POTENTIAL SECURITY BREACH!\n"
+ "The server's host key does not match the one PuTTY has\n"
+ "cached in the registry. This means that either the\n"
+ "server administrator has changed the host key, or you\n"
+ "have actually connected to another computer pretending\n"
+ "to be the server.\n"
+ "The new key fingerprint is:\n"
+ "%s\n"
+ "If you were expecting this change and trust the new key,\n"
+ "enter \"y\" to update PuTTY's cache and continue connecting.\n"
+ "If you want to carry on connecting but without updating\n"
+ "the cache, enter \"n\".\n"
+ "If you want to abandon the connection completely, press\n"
+ "Return to cancel. Pressing Return is the ONLY guaranteed\n"
+ "safe choice.\n"
+ "Update cached key? (y/n, Return cancels connection) ";
+
+ static const char abandoned[] = "Connection abandoned.\n";
+
+ char line[32];
+
+ /*
+ * Verify the key against the registry.
+ */
+ ret = verify_host_key(host, port, keytype, keystr);
+
+ if (ret == 0) /* success - key matched OK */
+ return;
+
+ if (ret == 2) { /* key was different */
+ fprintf(stderr, wrongmsg, fingerprint);
+ fflush(stderr);
+ }
+ if (ret == 1) { /* key was absent */
+ fprintf(stderr, absentmsg, fingerprint);
+ fflush(stderr);
+ }
+
+ hin = GetStdHandle(STD_INPUT_HANDLE);
+ GetConsoleMode(hin, &savemode);
+ SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
+ ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
+ ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
+ SetConsoleMode(hin, savemode);
+
+ if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
+ if (line[0] == 'y' || line[0] == 'Y')
+ store_host_key(host, port, keytype, keystr);
+ } else {
+ fprintf(stderr, abandoned);
+ exit(0);
+ }
+}
+
+/*
+ * Print an error message and perform a fatal exit.
+ */
+void fatalbox(char *fmt, ...)
+{
+ char str[0x100]; /* Make the size big enough */
+ va_list ap;
+ va_start(ap, fmt);
+ strcpy(str, "Fatal:");
+ vsprintf(str + strlen(str), fmt, ap);
+ va_end(ap);
+ strcat(str, "\n");
+ fprintf(stderr, str);
+
+ exit(1);
+}
+void connection_fatal(char *fmt, ...)
+{
+ char str[0x100]; /* Make the size big enough */
+ va_list ap;
+ va_start(ap, fmt);
+ strcpy(str, "Fatal:");
+ vsprintf(str + strlen(str), fmt, ap);
+ va_end(ap);
+ strcat(str, "\n");
+ fprintf(stderr, str);
+
+ exit(1);
+}
+
+void logevent(char *string)
+{
+}
+
+void ldisc_send(char *buf, int len)
+{
+ /*
+ * This is only here because of the calls to ldisc_send(NULL,
+ * 0) in ssh.c. Nothing in PSFTP actually needs to use the
+ * ldisc as an ldisc. So if we get called with any real data, I
+ * want to know about it.
+ */
+ assert(len == 0);
+}
+
+/*
+ * Be told what socket we're supposed to be using.
+ */
+static SOCKET sftp_ssh_socket;
+char *do_select(SOCKET skt, int startup)
+{
+ if (startup)
+ sftp_ssh_socket = skt;
+ else
+ sftp_ssh_socket = INVALID_SOCKET;
+ return NULL;
+}
+extern int select_result(WPARAM, LPARAM);
+
+/*
+ * Receive a block of data from the SSH link. Block until all data
+ * is available.
+ *
+ * To do this, we repeatedly call the SSH protocol module, with our
+ * own trap in from_backend() to catch the data that comes back. We
+ * do this until we have enough data.
+ */
+
+static unsigned char *outptr; /* where to put the data */
+static unsigned outlen; /* how much data required */
+static unsigned char *pending = NULL; /* any spare data */
+static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */
+void from_backend(int is_stderr, char *data, int datalen)
+{
+ unsigned char *p = (unsigned char *) data;
+ unsigned len = (unsigned) datalen;
+
+ /*
+ * stderr data is just spouted to local stderr and otherwise
+ * ignored.
+ */
+ if (is_stderr) {
+ fwrite(data, 1, len, stderr);
+ return;
+ }
+
+ /*
+ * If this is before the real session begins, just return.
+ */
+ if (!outptr)
+ return;
+
+ if (outlen > 0) {
+ unsigned used = outlen;
+ if (used > len)
+ used = len;
+ memcpy(outptr, p, used);
+ outptr += used;
+ outlen -= used;
+ p += used;
+ len -= used;
+ }
+
+ if (len > 0) {
+ if (pendsize < pendlen + len) {
+ pendsize = pendlen + len + 4096;
+ pending = (pending ? srealloc(pending, pendsize) :
+ smalloc(pendsize));
+ if (!pending)
+ fatalbox("Out of memory");
+ }
+ memcpy(pending + pendlen, p, len);
+ pendlen += len;
+ }
+}
+int sftp_recvdata(char *buf, int len)
+{
+ outptr = (unsigned char *) buf;
+ outlen = len;
+
+ /*
+ * See if the pending-input block contains some of what we
+ * need.