2 * scpssh.c - SSH implementation for PuTTY Secure Copy
3 * Joris van Rantwijk, Aug 1999.
4 * Based on PuTTY ssh.c by Simon Tatham.
16 #define SSH_MSG_DISCONNECT 1
17 #define SSH_SMSG_PUBLIC_KEY 2
18 #define SSH_CMSG_SESSION_KEY 3
19 #define SSH_CMSG_USER 4
20 #define SSH_CMSG_AUTH_PASSWORD 9
21 #define SSH_CMSG_EXEC_CMD 13
22 #define SSH_SMSG_SUCCESS 14
23 #define SSH_SMSG_FAILURE 15
24 #define SSH_CMSG_STDIN_DATA 16
25 #define SSH_SMSG_STDOUT_DATA 17
26 #define SSH_SMSG_STDERR_DATA 18
27 #define SSH_CMSG_EOF 19
28 #define SSH_SMSG_EXIT_STATUS 20
29 #define SSH_CMSG_EXIT_CONFIRMATION 33
30 #define SSH_MSG_DEBUG 36
32 #define GET_32BIT(cp) \
33 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
34 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
35 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
36 ((unsigned long)(unsigned char)(cp)[3]))
38 #define PUT_32BIT(cp, value) { \
39 (cp)[0] = (value) >> 24; \
40 (cp)[1] = (value) >> 16; \
41 (cp)[2] = (value) >> 8; \
44 static SOCKET s = INVALID_SOCKET;
46 static unsigned char session_key[32];
47 static struct ssh_cipher *cipher = NULL;
49 static char *savedhost;
60 static struct Packet pktin = { 0, 0, 0, NULL, 0 };
61 static struct Packet pktout = { 0, 0, 0, NULL, 0 };
64 static void s_write (char *buf, int len) {
66 int i = send (s, buf, len, 0);
69 fatalbox("Lost connection while sending");
74 static int s_read (char *buf, int len) {
77 int i = recv (s, buf, len, 0);
80 len -= i, buf += i, ret += i;
88 * Read and decrypt one incoming SSH packet.
90 static void get_packet()
101 ret = s_read(buf, 4);
108 len = GET_32BIT(buf);
111 if (len == 0x52656d6f) {
118 len -= 5; /* type and CRC */
121 if (pktin.maxlen < biglen) {
122 pktin.maxlen = biglen;
123 pktin.data = (pktin.data == NULL) ?
124 smalloc(biglen) : srealloc(pktin.data, biglen);
127 ret = s_read(pktin.data, biglen);
135 cipher->decrypt(pktin.data, biglen);
137 pktin.type = pktin.data[pad];
138 pktin.body = pktin.data + pad + 1;
140 if (pktin.type == SSH_MSG_DEBUG) {
142 int len = GET_32BIT(pktin.body);
143 fprintf(stderr, "Remote: ");
144 fwrite(pktin.body + 4, len, 1, stderr);
145 fprintf(stderr, "\n");
151 static void s_wrpkt_start(int type, int len) {
154 len += 5; /* type and CRC */
158 pktout.length = len-5;
159 if (pktout.maxlen < biglen) {
160 pktout.maxlen = biglen;
161 pktout.data = (pktout.data == NULL ? malloc(biglen+4) :
162 realloc(pktout.data, biglen+4));
164 fatalbox("Out of memory");
168 pktout.body = pktout.data+4+pad+1;
171 static void s_wrpkt(void) {
172 int pad, len, biglen, i;
175 len = pktout.length + 5; /* type and CRC */
179 pktout.body[-1] = pktout.type;
180 for (i=0; i<pad; i++)
181 pktout.data[i+4] = random_byte();
182 crc = crc32(pktout.data+4, biglen-4);
183 PUT_32BIT(pktout.data+biglen, crc);
184 PUT_32BIT(pktout.data, len);
187 cipher->encrypt(pktout.data+4, biglen);
189 s_write(pktout.data, biglen+4);
192 static int do_ssh_init(void) {
200 while (s_read(&c, 1) == 1) {
201 if (c == 'S' && i < 2) i++;
202 else if (c == 'S' && i == 2) i = 2;
203 else if (c == 'H' && i == 2) break;
207 if (s_read(&c,1) != 1 || c != 'S') return 0;
208 if (s_read(&c,1) != 1 || c != 'S') return 0;
209 if (s_read(&c,1) != 1 || c != 'H') return 0;
211 if (s_read(&c,1) != 1 || c != '-') return 0;
214 if (s_read(&c,1) != 1)
220 } else if (i < sizeof(version)-1)
227 sprintf(vstring, "SSH-%s-7.7.7\n",
228 (strcmp(version, "1.5") <= 0 ? version : "1.5"));
229 s_write(vstring, strlen(vstring));
235 * Login on the server and request execution of the command.
237 static void ssh_login(char *username, char *cmd)
240 unsigned char session_id[16];
241 unsigned char *rsabuf, *keystr1, *keystr2;
242 unsigned char cookie[8];
243 struct RSAKey servkey, hostkey;
244 struct MD5Context md5c;
245 unsigned long supported_ciphers_mask;
248 extern struct ssh_cipher ssh_3des;
249 extern struct ssh_cipher ssh_blowfish;
253 if (pktin.type != SSH_SMSG_PUBLIC_KEY)
254 fatalbox("Public key packet not received");
256 memcpy(cookie, pktin.body, 8);
260 i = makekey(pktin.body+8, &servkey, &keystr1);
261 j = makekey(pktin.body+8+i, &hostkey, &keystr2);
263 supported_ciphers_mask = GET_32BIT(pktin.body+12+i+j);
265 MD5Update(&md5c, keystr2, hostkey.bytes);
266 MD5Update(&md5c, keystr1, servkey.bytes);
267 MD5Update(&md5c, pktin.body, 8);
269 MD5Final(session_id, &md5c);
272 session_key[i] = random_byte();
274 len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);
276 rsabuf = malloc(len);
278 fatalbox("Out of memory");
280 verify_ssh_host_key(savedhost, &hostkey);
282 for (i=0; i<32; i++) {
283 rsabuf[i] = session_key[i];
285 rsabuf[i] ^= session_id[i];
288 if (hostkey.bytes > servkey.bytes) {
289 rsaencrypt(rsabuf, 32, &servkey);
290 rsaencrypt(rsabuf, servkey.bytes, &hostkey);
292 rsaencrypt(rsabuf, 32, &hostkey);
293 rsaencrypt(rsabuf, hostkey.bytes, &servkey);
296 cipher_type = cfg.cipher == CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH :
298 if ((supported_ciphers_mask & (1 << cipher_type)) == 0) {
299 fprintf(stderr, "Selected cipher not supported, falling back to 3DES\n");
300 cipher_type = SSH_CIPHER_3DES;
303 s_wrpkt_start(SSH_CMSG_SESSION_KEY, len+15);
304 pktout.body[0] = cipher_type;
305 memcpy(pktout.body+1, cookie, 8);
306 pktout.body[9] = (len*8) >> 8;
307 pktout.body[10] = (len*8) & 0xFF;
308 memcpy(pktout.body+11, rsabuf, len);
309 pktout.body[len+11] = pktout.body[len+12] = 0; /* protocol flags */
310 pktout.body[len+13] = pktout.body[len+14] = 0;
315 cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish :
317 cipher->sesskey(session_key);
321 if (pktin.type != SSH_SMSG_SUCCESS)
322 fatalbox("Encryption not successfully enabled");
325 fprintf(stderr, "Logging in as \"%s\".\n", username);
326 s_wrpkt_start(SSH_CMSG_USER, 4+strlen(username));
327 pktout.body[0] = pktout.body[1] = pktout.body[2] = 0;
328 pktout.body[3] = strlen(username);
329 memcpy(pktout.body+4, username, strlen(username));
334 while (pktin.type == SSH_SMSG_FAILURE) {
337 sprintf(prompt, "%s@%s's password: ", username, savedhost);
338 ssh_get_password(prompt, password, 100);
339 s_wrpkt_start(SSH_CMSG_AUTH_PASSWORD, 4+strlen(password));
340 pktout.body[0] = pktout.body[1] = pktout.body[2] = 0;
341 pktout.body[3] = strlen(password);
342 memcpy(pktout.body+4, password, strlen(password));
344 memset(password, 0, strlen(password));
346 if (pktin.type == SSH_SMSG_FAILURE) {
347 fprintf(stderr, "Access denied\n");
348 } else if (pktin.type != SSH_SMSG_SUCCESS) {
349 fatalbox("Strange packet received, type %d", pktin.type);
353 /* Execute command */
355 fprintf(stderr, "Sending command: %s\n", cmd);
357 s_wrpkt_start(SSH_CMSG_EXEC_CMD, 4+i);
358 PUT_32BIT(pktout.body, i);
359 memcpy(pktout.body+4, cmd, i);
365 * Receive a block of data over the SSH link. Block until
366 * all data is available. Return nr of bytes read (0 if lost connection).
368 int ssh_recv(unsigned char *buf, int len)
370 static int pending_input_len = 0;
371 static unsigned char *pending_input_ptr;
374 if (pending_input_len >= to_read) {
375 memcpy(buf, pending_input_ptr, to_read);
376 pending_input_ptr += to_read;
377 pending_input_len -= to_read;
381 if (pending_input_len > 0) {
382 memcpy(buf, pending_input_ptr, pending_input_len);
383 buf += pending_input_len;
384 to_read -= pending_input_len;
385 pending_input_len = 0;
388 if (s == INVALID_SOCKET)
390 while (to_read > 0) {
392 if (s == INVALID_SOCKET)
394 if (pktin.type == SSH_SMSG_STDOUT_DATA) {
395 int plen = GET_32BIT(pktin.body);
396 if (plen <= to_read) {
397 memcpy(buf, pktin.body + 4, plen);
401 memcpy(buf, pktin.body + 4, to_read);
402 pending_input_len = plen - to_read;
403 pending_input_ptr = pktin.body + 4 + to_read;
406 } else if (pktin.type == SSH_SMSG_STDERR_DATA) {
407 int plen = GET_32BIT(pktin.body);
408 fwrite(pktin.body + 4, plen, 1, stderr);
409 } else if (pktin.type == SSH_MSG_DISCONNECT) {
410 } else if (pktin.type == SSH_SMSG_SUCCESS ||
411 pktin.type == SSH_SMSG_FAILURE) {
412 } else if (pktin.type == SSH_SMSG_EXIT_STATUS) {
414 fprintf(stderr, "Remote exit status %d\n",
415 GET_32BIT(pktin.body));
416 s_wrpkt_start(SSH_CMSG_EXIT_CONFIRMATION, 0);
419 fprintf(stderr, "Closing connection\n");
430 * Send a block of data over the SSH link.
431 * Block until all data is sent.
433 void ssh_send(unsigned char *buf, int len)
435 if (s == INVALID_SOCKET)
437 s_wrpkt_start(SSH_CMSG_STDIN_DATA, 4 + len);
438 PUT_32BIT(pktout.body, len);
439 memcpy(pktout.body + 4, buf, len);
445 * Send an EOF notification to the server.
447 void ssh_send_eof(void)
449 if (s == INVALID_SOCKET)
451 s_wrpkt_start(SSH_CMSG_EOF, 0);
457 * Set up the connection, login on the remote host and
458 * start execution of a command.
460 * Returns an error message, or NULL on success.
462 * Also places the canonical host name into `realhost'.
464 char *ssh_init(char *host, int port, char *cmd, char **realhost) {
473 savedhost = malloc(1+strlen(host));
475 fatalbox("Out of memory");
476 strcpy(savedhost, host);
488 if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) {
489 if ( (h = gethostbyname(host)) == NULL)
490 switch (WSAGetLastError()) {
491 case WSAENETDOWN: return "Network is down";
492 case WSAHOST_NOT_FOUND: case WSANO_DATA:
493 return "Host does not exist";
494 case WSATRY_AGAIN: return "Host not found";
495 default: return "gethostbyname: unknown error";
497 memcpy (&a, h->h_addr, sizeof(a));
498 *realhost = h->h_name;
507 port = 22; /* default ssh port */
512 s = socket(AF_INET, SOCK_STREAM, 0);
513 if (s == INVALID_SOCKET)
514 switch (WSAGetLastError()) {
515 case WSAENETDOWN: return "Network is down";
516 case WSAEAFNOSUPPORT: return "TCP/IP support not present";
517 default: return "socket(): unknown error";
521 * Bind to local address.
523 addr.sin_family = AF_INET;
524 addr.sin_addr.s_addr = htonl(INADDR_ANY);
525 addr.sin_port = htons(0);
526 if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
527 switch (WSAGetLastError()) {
528 case WSAENETDOWN: return "Network is down";
529 default: return "bind(): unknown error";
533 * Connect to remote address.
535 addr.sin_addr.s_addr = htonl(a);
536 addr.sin_port = htons((short)port);
537 if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
538 switch (WSAGetLastError()) {
539 case WSAENETDOWN: return "Network is down";
540 case WSAECONNREFUSED: return "Connection refused";
541 case WSAENETUNREACH: return "Network is unreachable";
542 case WSAEHOSTUNREACH: return "No route to host";
543 default: return "connect(): unknown error";
547 send(s, "connect ", 8, 0);
548 send(s, FWhost, strlen(FWhost), 0);
551 sprintf(buf, " %d\n", FWport);
552 send (s, buf, strlen(buf), 0);
559 return "Protocol initialisation error";
561 ssh_login(cfg.username, cmd);