ssh.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #ifndef macintosh
  4. #include <winsock.h>
  5. #endif
  6. #include "putty.h"
  7. #ifndef FALSE
  8. #define FALSE 0
  9. #endif
  10. #ifndef TRUE
  11. #define TRUE 1
  12. #endif
  13. #include "ssh.h"
  14. /* Coroutine mechanics for the sillier bits of the code */
  15. #define crBegin1(l) (l) = 0;
  16. #define crBegin2(l) switch(l) { case 0:;
  17. #define crBegin(l) crBegin1(l); crBegin2(l);
  18. #define crFinish(l,z) } (l) = 0; return (l)
  19. #define foolemacs {
  20. #define crFinishV(l) } (l) = 0; return
  21. #define crReturn(l,z) \
  22. do {\
  23. (l)=__LINE__; return (z); case __LINE__:;\
  24. } while (0)
  25. #define crReturnV(l) \
  26. do {\
  27. (l)=__LINE__; return; case __LINE__:;\
  28. } while (0)
  29. #define crStop(l,z) do{ (l) = 0; return (z); }while(0)
  30. #define crStopV(l) do{ (l) = 0; return; }while(0)
  31. #ifndef FALSE
  32. #define FALSE 0
  33. #endif
  34. #ifndef TRUE
  35. #define TRUE 1
  36. #endif
  37. struct Packet {
  38. long length;
  39. int type;
  40. unsigned long crc;
  41. unsigned char *data;
  42. unsigned char *body;
  43. long maxlen;
  44. };
  45. struct ssh_private {
  46. SOCKET s;
  47. unsigned char session_key[32];
  48. struct ssh_cipher *cipher;
  49. char *savedhost;
  50. enum {
  51. SSH_STATE_BEFORE_SIZE,
  52. SSH_STATE_INTERMED,
  53. SSH_STATE_SESSION,
  54. SSH_STATE_CLOSED
  55. } ssh_state;
  56. int size_needed;
  57. #ifdef FWHACK
  58. char *FWhost;
  59. int FWport;
  60. #endif
  61. struct Packet pktin;
  62. struct Packet pktout;
  63. /* State for ssh_gotdata coroutine */
  64. int gd_line;
  65. long len, biglen, to_read;
  66. unsigned char *p;
  67. int i, pad;
  68. int chunk;
  69. char *sversion;
  70. size_t sversion_space, sversion_len;
  71. /* State for ssh_protocol coroutine */
  72. int pr_line;
  73. };
  74. static void s_write (Session *sess, unsigned char *buf, int len) {
  75. struct ssh_private *sp = (struct ssh_private*)sess->back_priv;
  76. while (len > 0) {
  77. int i = net_send (sp->s, buf, len, 0);
  78. if (i > 0)
  79. len -= i, buf += i;
  80. }
  81. }
  82. static int s_read (Session *sess, unsigned char *buf, int len) {
  83. struct ssh_private *sp = (struct ssh_private*)sess->back_priv;
  84. int ret = 0;
  85. while (len > 0) {
  86. int i = net_recv (sp->s, buf, len, 0);
  87. if (i > 0)
  88. len -= i, buf += i, ret += i;
  89. else
  90. return i;
  91. }
  92. return ret;
  93. }
  94. static void c_write (Session *sess, char *buf, int len) {
  95. while (len--) {
  96. int new_head = (sess->inbuf_head + 1) & INBUF_MASK;
  97. if (new_head != sess->inbuf_reap) {
  98. sess->inbuf[sess->inbuf_head] = *buf++;
  99. sess->inbuf_head = new_head;
  100. }
  101. }
  102. }
  103. static void ssh_protocol(Session *,unsigned char *in, int inlen, int ispkt);
  104. static void ssh_size(void);
  105. static void ssh_gotdata(Session *sess, unsigned char *data, int datalen) {
  106. struct ssh_private *sp = (struct ssh_private *)sess->back_priv;
  107. unsigned char *eol;
  108. size_t fraglen = datalen;
  109. static char vstring[] = "SSH-1.5-PuTTY\n";
  110. crBegin(sp->gd_line);
  111. #ifdef FWHACK
  112. /* Should wait for "SSH-" */
  113. #endif
  114. sp->sversion = smalloc(16); /* enough for "SSH-1.5-1.2.27\n" */
  115. sp->sversion_space = 16;
  116. sp->sversion_len = 0;
  117. do {
  118. while (datalen == 0)
  119. crReturnV(sp->gd_line);
  120. eol = memchr(data, '\n', datalen);
  121. if (eol != NULL)
  122. fraglen = eol + 1 - data; /* include \n */
  123. if (sp->sversion_len + fraglen > sp->sversion_space) {
  124. /* FIXME Sanity-check length */
  125. srealloc(sp->sversion, sp->sversion_len + fraglen);
  126. sp->sversion_space = sp->sversion_len + fraglen;
  127. }
  128. memcpy(sp->sversion + sp->sversion_len, data, fraglen);
  129. data += fraglen; datalen -= fraglen;
  130. } while (eol == NULL);
  131. sp->sversion[sp->sversion_len - 1] = '\0';
  132. if (sp->sversion_len < 4 || memcmp(sp->sversion, "SSH-", 4) != 0)
  133. /* XXX explode! */;
  134. /*
  135. * We ignore the rest of the banner on the grounds that we only
  136. * speak SSH 1.5 anyway. If the server can't, that's its problem.
  137. */
  138. s_write(sess, (unsigned char *)vstring, strlen(vstring));
  139. /* End of do_ssh_init */
  140. while (1) {
  141. for (sp->i = sp->len = 0; sp->i < 4; sp->i++) {
  142. while (datalen == 0)
  143. crReturnV(sp->gd_line);
  144. sp->len = (sp->len << 8) + *data;
  145. data++, datalen--;
  146. }
  147. #ifdef FWHACK
  148. if (sp->len == 0x52656d6f) { /* "Remo"te server has closed ... */
  149. sp->len = 0x300; /* big enough to carry to end */
  150. }
  151. #endif
  152. sp->pad = 8 - (sp->len%8);
  153. sp->biglen = sp->len + sp->pad;
  154. sp->len -= 5; /* type and CRC */
  155. sp->pktin.length = sp->len;
  156. if (sp->pktin.maxlen < sp->biglen) {
  157. sp->pktin.maxlen = sp->biglen;
  158. sp->pktin.data = (sp->pktin.data == NULL ? smalloc(sp->biglen) :
  159. srealloc(sp->pktin.data, sp->biglen));
  160. }
  161. sp->p = sp->pktin.data, sp->to_read = sp->biglen;
  162. while (sp->to_read > 0) {
  163. sp->chunk = sp->to_read;
  164. while (datalen == 0)
  165. crReturnV(sp->gd_line);
  166. if (sp->chunk > datalen)
  167. sp->chunk = datalen;
  168. memcpy(sp->p, data, sp->chunk);
  169. data += sp->chunk;
  170. datalen -= sp->chunk;
  171. sp->p += sp->chunk;
  172. sp->to_read -= sp->chunk;
  173. }
  174. if (sp->cipher)
  175. sp->cipher->decrypt(sp->pktin.data, sp->biglen);
  176. sp->pktin.type = sp->pktin.data[sp->pad];
  177. sp->pktin.body = sp->pktin.data+sp->pad+1;
  178. if (sp->pktin.type == SSH_MSG_DEBUG) { /* SSH_MSG_DEBUG */
  179. /* FIXME: log it */
  180. } else
  181. ssh_protocol(sess, NULL, 0, 1);
  182. }
  183. crFinishV(sp->gd_line);
  184. }
  185. static void s_wrpkt_start(Session *sess, int type, int len) {
  186. struct ssh_private *sp = (struct ssh_private *)sess->back_priv;
  187. int pad, biglen;
  188. len += 5; /* type and CRC */
  189. pad = 8 - (len%8);
  190. biglen = len + pad;
  191. sp->pktout.length = len-5;
  192. if (sp->pktout.maxlen < biglen) {
  193. sp->pktout.maxlen = biglen;
  194. sp->pktout.data = (sp->pktout.data == NULL ? smalloc(biglen+4) :
  195. srealloc(sp->pktout.data, biglen+4));
  196. }
  197. sp->pktout.type = type;
  198. sp->pktout.body = sp->pktout.data+4+pad+1;
  199. }
  200. static void s_wrpkt(Session *sess) {
  201. struct ssh_private *sp = (struct ssh_private *)sess->back_priv;
  202. int pad, len, biglen, i;
  203. unsigned long crc;
  204. len = sp->pktout.length + 5; /* type and CRC */
  205. pad = 8 - (len%8);
  206. biglen = len + pad;
  207. sp->pktout.body[-1] = sp->pktout.type;
  208. for (i=0; i<pad; i++)
  209. sp->pktout.data[i+4] = random_byte();
  210. crc = crc32(sp->pktout.data+4, biglen-4);
  211. sp->pktout.data[biglen+0] = (unsigned char) ((crc >> 24) & 0xFF);
  212. sp->pktout.data[biglen+1] = (unsigned char) ((crc >> 16) & 0xFF);
  213. sp->pktout.data[biglen+2] = (unsigned char) ((crc >> 8) & 0xFF);
  214. sp->pktout.data[biglen+3] = (unsigned char) (crc & 0xFF);
  215. sp->pktout.data[0] = (len >> 24) & 0xFF;
  216. sp->pktout.data[1] = (len >> 16) & 0xFF;
  217. sp->pktout.data[2] = (len >> 8) & 0xFF;
  218. sp->pktout.data[3] = len & 0xFF;
  219. if (sp->cipher)
  220. sp->cipher->encrypt(sp->pktout.data+4, biglen);
  221. s_write(sess, sp->pktout.data, biglen+4);
  222. }
  223. static int do_ssh_init(Session *sess) {
  224. }
  225. static void ssh_protocol(Session *sess, unsigned char *in, int inlen,
  226. int ispkt) {
  227. struct ssh_private *sp = (struct ssh_private *)sess->back_priv;
  228. int i, j, len;
  229. unsigned char session_id[16];
  230. unsigned char *rsabuf, *keystr1, *keystr2;
  231. unsigned char cookie[8];
  232. struct RSAKey servkey, hostkey;
  233. struct MD5Context md5c;
  234. unsigned long supported_ciphers_mask;
  235. int cipher_type;
  236. extern struct ssh_cipher ssh_3des;
  237. extern struct ssh_cipher ssh_blowfish;
  238. crBegin(sp->pr_line);
  239. random_init();
  240. while (!ispkt)
  241. crReturnV(sp->pr_line);
  242. if (sp->pktin.type != SSH_SMSG_PUBLIC_KEY)
  243. fatalbox("Public key packet not received");
  244. memcpy(cookie, sp->pktin.body, 8);
  245. MD5Init(&md5c);
  246. i = makekey(sp->pktin.body+8, &servkey, &keystr1);
  247. j = makekey(sp->pktin.body+8+i, &hostkey, &keystr2);
  248. supported_ciphers_mask = (sp->pktin.body[12+i+j] << 24) |
  249. (sp->pktin.body[13+i+j] << 16) |
  250. (sp->pktin.body[14+i+j] << 8) |
  251. (sp->pktin.body[15+i+j]);
  252. MD5Update(&md5c, keystr2, hostkey.bytes);
  253. MD5Update(&md5c, keystr1, servkey.bytes);
  254. MD5Update(&md5c, sp->pktin.body, 8);
  255. MD5Final(session_id, &md5c);
  256. for (i=0; i<32; i++)
  257. session_key[i] = random_byte();
  258. len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);
  259. rsabuf = malloc(len);
  260. if (!rsabuf)
  261. fatalbox("Out of memory");
  262. verify_ssh_host_key(savedhost, &hostkey);
  263. for (i=0; i<32; i++) {
  264. rsabuf[i] = session_key[i];
  265. if (i < 16)
  266. rsabuf[i] ^= session_id[i];
  267. }
  268. if (hostkey.bytes > servkey.bytes) {
  269. rsaencrypt(rsabuf, 32, &servkey);
  270. rsaencrypt(rsabuf, servkey.bytes, &hostkey);
  271. } else {
  272. rsaencrypt(rsabuf, 32, &hostkey);
  273. rsaencrypt(rsabuf, hostkey.bytes, &servkey);
  274. }
  275. cipher_type = cfg.cipher == CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH :
  276. SSH_CIPHER_3DES;
  277. if ((supported_ciphers_mask & (1 << cipher_type)) == 0) {
  278. c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
  279. cipher_type = SSH_CIPHER_3DES;
  280. }
  281. s_wrpkt_start(SSH_CMSG_SESSION_KEY, len+15);
  282. sp->pktout.body[0] = cipher_type;
  283. memcpy(sp->pktout.body+1, cookie, 8);
  284. sp->pktout.body[9] = (len*8) >> 8;
  285. sp->pktout.body[10] = (len*8) & 0xFF;
  286. memcpy(sp->pktout.body+11, rsabuf, len);
  287. sp->pktout.body[len+11] = sp->pktout.body[len+12] = 0; /* protocol flags */
  288. sp->pktout.body[len+13] = sp->pktout.body[len+14] = 0;
  289. s_wrpkt();
  290. free(rsabuf);
  291. cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish :
  292. &ssh_3des;
  293. cipher->sesskey(session_key);
  294. do { crReturnV(sp->pr_line); } while (!ispkt);
  295. if (sp->pktin.type != SSH_SMSG_SUCCESS)
  296. fatalbox("Encryption not successfully enabled");
  297. fflush(stdout);
  298. {
  299. static char username[100];
  300. static int pos = 0;
  301. static char c;
  302. if (!*cfg.username) {
  303. c_write("login as: ", 10);
  304. while (pos >= 0) {
  305. do { crReturnV(sp->pr_line); } while (ispkt);
  306. while (inlen--) switch (c = *in++) {
  307. case 10: case 13:
  308. username[pos] = 0;
  309. pos = -1;
  310. break;
  311. case 8: case 127:
  312. if (pos > 0) {
  313. c_write("\b \b", 3);
  314. pos--;
  315. }
  316. break;
  317. case 21: case 27:
  318. while (pos > 0) {
  319. c_write("\b \b", 3);
  320. pos--;
  321. }
  322. break;
  323. case 3: case 4:
  324. random_save_seed();
  325. exit(0);
  326. break;
  327. default:
  328. if (c >= ' ' && c <= '~' && pos < 40) {
  329. username[pos++] = c;
  330. c_write(&c, 1);
  331. }
  332. break;
  333. }
  334. }
  335. c_write("\r\n", 2);
  336. username[strcspn(username, "\n\r")] = '\0';
  337. } else {
  338. char stuff[200];
  339. strncpy(username, cfg.username, 99);
  340. username[99] = '\0';
  341. sprintf(stuff, "Sent username \"%s\".\r\n", username);
  342. c_write(stuff, strlen(stuff));
  343. }
  344. s_wrpkt_start(SSH_CMSG_USER, 4+strlen(username));
  345. sp->pktout.body[0] = sp->pktout.body[1] = sp->pktout.body[2] = 0;
  346. sp->pktout.body[3] = strlen(username);
  347. memcpy(sp->pktout.body+4, username, strlen(username));
  348. s_wrpkt();
  349. }
  350. do { crReturnV(sp->pr_line); } while (!ispkt);
  351. while (sp->pktin.type == SSH_SMSG_FAILURE) {
  352. static char password[100];
  353. static int pos;
  354. static char c;
  355. c_write("password: ", 10);
  356. pos = 0;
  357. while (pos >= 0) {
  358. do { crReturnV(sp->pr_line); } while (ispkt);
  359. while (inlen--) switch (c = *in++) {
  360. case 10: case 13:
  361. password[pos] = 0;
  362. pos = -1;
  363. break;
  364. case 8: case 127:
  365. if (pos > 0)
  366. pos--;
  367. break;
  368. case 21: case 27:
  369. pos = 0;
  370. break;
  371. case 3: case 4:
  372. random_save_seed();
  373. exit(0);
  374. break;
  375. default:
  376. if (c >= ' ' && c <= '~' && pos < 40)
  377. password[pos++] = c;
  378. break;
  379. }
  380. }
  381. c_write("\r\n", 2);
  382. s_wrpkt_start(SSH_CMSG_AUTH_PASSWORD, 4+strlen(password));
  383. sp->pktout.body[0] = sp->pktout.body[1] = sp->pktout.body[2] = 0;
  384. sp->pktout.body[3] = strlen(password);
  385. memcpy(sp->pktout.body+4, password, strlen(password));
  386. s_wrpkt();
  387. memset(password, 0, strlen(password));
  388. do { crReturnV(sp->pr_line); } while (!ispkt);
  389. if (sp->pktin.type == SSH_SMSG_FAILURE) {
  390. c_write("Access denied\r\n", 15);
  391. } else if (sp->pktin.type != SSH_SMSG_SUCCESS) {
  392. fatalbox("Strange packet received, type %d", sp->pktin.type);
  393. }
  394. }
  395. if (!cfg.nopty) {
  396. i = strlen(cfg.termtype);
  397. s_wrpkt_start(SSH_CMSG_REQUEST_PTY, i+5*4+1);
  398. sp->pktout.body[0] = (i >> 24) & 0xFF;
  399. sp->pktout.body[1] = (i >> 16) & 0xFF;
  400. sp->pktout.body[2] = (i >> 8) & 0xFF;
  401. sp->pktout.body[3] = i & 0xFF;
  402. memcpy(sp->pktout.body+4, cfg.termtype, i);
  403. i += 4;
  404. sp->pktout.body[i++] = (rows >> 24) & 0xFF;
  405. sp->pktout.body[i++] = (rows >> 16) & 0xFF;
  406. sp->pktout.body[i++] = (rows >> 8) & 0xFF;
  407. sp->pktout.body[i++] = rows & 0xFF;
  408. sp->pktout.body[i++] = (cols >> 24) & 0xFF;
  409. sp->pktout.body[i++] = (cols >> 16) & 0xFF;
  410. sp->pktout.body[i++] = (cols >> 8) & 0xFF;
  411. sp->pktout.body[i++] = cols & 0xFF;
  412. memset(sp->pktout.body+i, 0, 9); /* 0 pixwidth, 0 pixheight, 0.b endofopt */
  413. s_wrpkt();
  414. ssh_state = SSH_STATE_INTERMED;
  415. do { crReturnV(sp->pr_line); } while (!ispkt);
  416. if (sp->pktin.type != SSH_SMSG_SUCCESS
  417. && sp->pktin.type != SSH_SMSG_FAILURE) {
  418. fatalbox("Protocol confusion");
  419. } else if (sp->pktin.type == SSH_SMSG_FAILURE) {
  420. c_write("Server refused to allocate pty\r\n", 32);
  421. }
  422. }
  423. s_wrpkt_start(SSH_CMSG_EXEC_SHELL, 0);
  424. s_wrpkt();
  425. ssh_state = SSH_STATE_SESSION;
  426. if (size_needed)
  427. ssh_size();
  428. while (1) {
  429. crReturnV(sp->pr_line);
  430. if (ispkt) {
  431. if (sp->pktin.type == SSH_SMSG_STDOUT_DATA ||
  432. sp->pktin.type == SSH_SMSG_STDERR_DATA) {
  433. long len = 0;
  434. for (i = 0; i < 4; i++)
  435. len = (len << 8) + sp->pktin.body[i];
  436. c_write(sp->pktin.body+4, len);
  437. } else if (sp->pktin.type == SSH_MSG_DISCONNECT) {
  438. /* SSH_MSG_DISCONNECT */
  439. ssh_state = SSH_STATE_CLOSED;
  440. } else if (sp->pktin.type == SSH_SMSG_SUCCESS) {
  441. /* SSH_MSG_SUCCESS: may be from EXEC_SHELL on some servers */
  442. } else if (sp->pktin.type == SSH_SMSG_FAILURE) {
  443. /* SSH_MSG_FAILURE: may be from EXEC_SHELL on some servers
  444. * if no pty is available or in other odd cases. Ignore */
  445. } else if (sp->pktin.type == SSH_SMSG_EXITSTATUS) {
  446. /* EXITSTATUS */
  447. s_wrpkt_start(SSH_CMSG_EXIT_CONFIRMATION, 0);
  448. s_wrpkt();
  449. } else {
  450. fatalbox("Strange packet received: type %d", sp->pktin.type);
  451. }
  452. } else {
  453. s_wrpkt_start(SSH_CMSG_STDIN_DATA, 4+inlen);
  454. sp->pktout.body[0] = (inlen >> 24) & 0xFF;
  455. sp->pktout.body[1] = (inlen >> 16) & 0xFF;
  456. sp->pktout.body[2] = (inlen >> 8) & 0xFF;
  457. sp->pktout.body[3] = inlen & 0xFF;
  458. memcpy(sp->pktout.body+4, in, inlen);
  459. s_wrpkt();
  460. }
  461. }
  462. crFinishV;
  463. }
  464. /*
  465. * Called to set up the connection. Will arrange for WM_NETEVENT
  466. * messages to be passed to the specified window, whose window
  467. * procedure should then call telnet_msg().
  468. *
  469. * Returns an error message, or NULL on success.
  470. *
  471. * Also places the canonical host name into `realhost'.
  472. */
  473. static char *ssh_init(Session *sess) {
  474. struct ssh_private *sp;
  475. char *host = sess->cfg.host;
  476. int port = sess->cfg.port;
  477. sess->back_priv = smalloc(sizeof(struct ssh_private));
  478. sp = (struct ssh_private *)sess->back_priv;
  479. memset(*sp, 0, sizeof(*sp));
  480. sp->s = INVALID_SOCKET;
  481. sp->ssh_state = SSH_STATE_BEFORE_SIZE;
  482. sp->savedhost = smalloc(1+strlen(host));
  483. strcpy(sp->savedhost, host);
  484. #ifdef FWHACK
  485. sp->FWhost = host;
  486. sp->FWport = port;
  487. host = FWSTR;
  488. port = 23;
  489. #endif
  490. if (port < 0)
  491. port = 22; /* default ssh port */
  492. sp->s = net_open(sess, host, port);
  493. return NULL;
  494. }
  495. static void ssh_opened(Session *sess) {
  496. struct ssh_private *sp = (struct ssh_private *)sess->back_priv;
  497. #ifdef FWHACK
  498. send(sp->s, "connect ", 8, 0);
  499. send(sp->s, FWhost, strlen(FWhost), 0);
  500. {
  501. char buf[20];
  502. sprintf(buf, " %d\n", FWport);
  503. send (s, buf, strlen(buf), 0);
  504. }
  505. #endif
  506. if (!do_ssh_init())
  507. return "Protocol initialisation error";
  508. if (WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ | FD_CLOSE) == SOCKET_ERROR)
  509. switch (WSAGetLastError()) {
  510. case WSAENETDOWN: return "Network is down";
  511. default: return "WSAAsyncSelect(): unknown error";
  512. }
  513. return NULL;
  514. }
  515. /*
  516. * Process a WM_NETEVENT message. Will return 0 if the connection
  517. * has closed, or <0 for a socket error.
  518. */
  519. static int ssh_msg (Session *sess, SOCKET sock, Net_Event_Type ne) {
  520. struct ssh_private *sp = (struct ssh_private *)sess->back_priv;
  521. int ret;
  522. char buf[256];
  523. if (s == INVALID_SOCKET) /* how the hell did we get here?! */
  524. return -5000;
  525. switch (ne) {
  526. case NE_OPEN:
  527. ssh_opened(sess);
  528. return 1;
  529. case NE_DATA:
  530. ret = net_recv(sock, buf, sizeof(buf), 0);
  531. if (ret < 0) /* any _other_ error */
  532. return -1;
  533. if (ret == 0) {
  534. sp->s = INVALID_SOCKET;
  535. return 0; /* can't happen, in theory */
  536. }
  537. ssh_gotdata(sess, buf, ret);
  538. return 1;
  539. case NE_CLOSING:
  540. sp->s = INVALID_SOCKET;
  541. sp->ssh_state = SSH_STATE_CLOSED;
  542. return 0;
  543. case NE_NOHOST:
  544. fatalbox("Host not found");
  545. case NE_REFUSED:
  546. fatalbox("Connection refused");
  547. case NE_NOOPEN:
  548. fatalbox("Unable to open connection");
  549. case NE_TIMEOUT:
  550. fatalbox("Connection timed out");
  551. case NE_ABORT:
  552. fatalbox("Connection reset by peer");
  553. case NE_DIED:
  554. fatalbox("Connection died");
  555. }
  556. return 1; /* shouldn't happen, but WTF */
  557. }
  558. /*
  559. * Called to send data down the Telnet connection.
  560. */
  561. static void ssh_send(Session *sess, char *buf, int len) {
  562. if (s == INVALID_SOCKET)
  563. return;
  564. ssh_protocol(sess, buf, len, 0);
  565. }
  566. /*
  567. * Called to set the size of the window from Telnet's POV.
  568. */
  569. static void ssh_size(Session *sess) {
  570. struct ssh_private *sp = (struct ssh_private *sp)sess->back_priv;
  571. switch (sp->ssh_state) {
  572. case SSH_STATE_BEFORE_SIZE:
  573. case SSH_STATE_CLOSED:
  574. break; /* do nothing */
  575. case SSH_STATE_INTERMED:
  576. sp->size_needed = TRUE; /* buffer for later */
  577. break;
  578. case SSH_STATE_SESSION:
  579. if (!sess->cfg.nopty) {
  580. s_wrpkt_start(SSH_CMSG_WINDOW_SIZE, 16);
  581. sp->pktout.body[0] = (rows >> 24) & 0xFF;
  582. sp->pktout.body[1] = (rows >> 16) & 0xFF;
  583. sp->pktout.body[2] = (rows >> 8) & 0xFF;
  584. sp->pktout.body[3] = rows & 0xFF;
  585. sp->pktout.body[4] = (cols >> 24) & 0xFF;
  586. sp->pktout.body[5] = (cols >> 16) & 0xFF;
  587. sp->pktout.body[6] = (cols >> 8) & 0xFF;
  588. sp->pktout.body[7] = cols & 0xFF;
  589. memset(sp->pktout.body+8, 0, 8);
  590. s_wrpkt();
  591. }
  592. }
  593. }
  594. /*
  595. * (Send Telnet special codes)
  596. */
  597. static void ssh_special (Telnet_Special code) {
  598. /* do nothing */
  599. }
  600. Backend ssh_backend = {
  601. ssh_init,
  602. ssh_msg,
  603. ssh_send,
  604. ssh_size,
  605. ssh_special
  606. };