blob: 0b1c0901f13a927e4ff11b15c09a3334636f35cc [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * Created: Sat Mar 18 22:15:47 1995 ylo
6 * Code to connect to a remote host, and to perform the client side of the
7 * login (authentication) dialog.
8 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10009
10#include "includes.h"
Damien Miller5428f641999-11-25 11:54:57 +110011RCSID("$Id: sshconnect.c,v 1.15 1999/11/25 00:54:59 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100012
Damien Miller7f6ea021999-10-28 13:25:17 +100013#ifdef HAVE_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014#include <openssl/bn.h>
Damien Miller7f6ea021999-10-28 13:25:17 +100015#include <openssl/md5.h>
16#endif
17#ifdef HAVE_SSL
18#include <ssl/bn.h>
19#include <ssl/md5.h>
20#endif
21
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022#include "xmalloc.h"
23#include "rsa.h"
24#include "ssh.h"
25#include "packet.h"
26#include "authfd.h"
27#include "cipher.h"
28#include "mpaux.h"
29#include "uidswap.h"
30#include "compat.h"
Damien Miller6d7b2cd1999-11-12 15:19:27 +110031#include "readconf.h"
Damien Miller81428f91999-11-18 09:28:11 +110032#include "fingerprint.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
34/* Session id for the current session. */
35unsigned char session_id[16];
36
Damien Miller95def091999-11-25 00:26:21 +110037/*
38 * Connect to the given ssh server using a proxy command.
39 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040int
41ssh_proxy_connect(const char *host, int port, uid_t original_real_uid,
42 const char *proxy_command)
43{
Damien Miller95def091999-11-25 00:26:21 +110044 Buffer command;
45 const char *cp;
46 char *command_string;
47 int pin[2], pout[2];
48 int pid;
49 char portstring[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
Damien Miller95def091999-11-25 00:26:21 +110051 /* Convert the port number into a string. */
52 snprintf(portstring, sizeof portstring, "%d", port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Damien Miller95def091999-11-25 00:26:21 +110054 /* Build the final command string in the buffer by making the
55 appropriate substitutions to the given proxy command. */
56 buffer_init(&command);
57 for (cp = proxy_command; *cp; cp++) {
58 if (cp[0] == '%' && cp[1] == '%') {
59 buffer_append(&command, "%", 1);
60 cp++;
61 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062 }
Damien Miller95def091999-11-25 00:26:21 +110063 if (cp[0] == '%' && cp[1] == 'h') {
64 buffer_append(&command, host, strlen(host));
65 cp++;
66 continue;
67 }
68 if (cp[0] == '%' && cp[1] == 'p') {
69 buffer_append(&command, portstring, strlen(portstring));
70 cp++;
71 continue;
72 }
73 buffer_append(&command, cp, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074 }
Damien Miller95def091999-11-25 00:26:21 +110075 buffer_append(&command, "\0", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
Damien Miller95def091999-11-25 00:26:21 +110077 /* Get the final command string. */
78 command_string = buffer_ptr(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Miller95def091999-11-25 00:26:21 +110080 /* Create pipes for communicating with the proxy. */
81 if (pipe(pin) < 0 || pipe(pout) < 0)
82 fatal("Could not create pipes to communicate with the proxy: %.100s",
83 strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Miller95def091999-11-25 00:26:21 +110085 debug("Executing proxy command: %.500s", command_string);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller95def091999-11-25 00:26:21 +110087 /* Fork and execute the proxy command. */
88 if ((pid = fork()) == 0) {
89 char *argv[10];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Damien Miller95def091999-11-25 00:26:21 +110091 /* Child. Permanently give up superuser privileges. */
92 permanently_set_uid(original_real_uid);
93
94 /* Redirect stdin and stdout. */
95 close(pin[1]);
96 if (pin[0] != 0) {
97 if (dup2(pin[0], 0) < 0)
98 perror("dup2 stdin");
99 close(pin[0]);
100 }
101 close(pout[0]);
102 if (dup2(pout[1], 1) < 0)
103 perror("dup2 stdout");
104 /* Cannot be 1 because pin allocated two descriptors. */
105 close(pout[1]);
106
107 /* Stderr is left as it is so that error messages get
108 printed on the user's terminal. */
109 argv[0] = "/bin/sh";
110 argv[1] = "-c";
111 argv[2] = command_string;
112 argv[3] = NULL;
113
114 /* Execute the proxy command. Note that we gave up any
115 extra privileges above. */
116 execv("/bin/sh", argv);
117 perror("/bin/sh");
118 exit(1);
119 }
120 /* Parent. */
121 if (pid < 0)
122 fatal("fork failed: %.100s", strerror(errno));
123
124 /* Close child side of the descriptors. */
125 close(pin[0]);
126 close(pout[1]);
127
128 /* Free the command name. */
129 buffer_free(&command);
130
131 /* Set the connection file descriptors. */
132 packet_set_connection(pout[0], pin[1]);
133
134 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135}
136
Damien Miller95def091999-11-25 00:26:21 +1100137/*
138 * Creates a (possibly privileged) socket for use as the ssh connection.
139 */
140int
141ssh_create_socket(uid_t original_real_uid, int privileged)
142{
143 int sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller5428f641999-11-25 11:54:57 +1100145 /*
146 * If we are running as root and want to connect to a privileged
147 * port, bind our own socket to a privileged port.
148 */
Damien Miller95def091999-11-25 00:26:21 +1100149 if (privileged) {
150 int p = IPPORT_RESERVED - 1;
151
152 sock = rresvport(&p);
153 if (sock < 0)
154 fatal("rresvport: %.100s", strerror(errno));
155 debug("Allocated local port %d.", p);
156 } else {
157 /* Just create an ordinary socket on arbitrary port. We
158 use the user's uid to create the socket. */
159 temporarily_use_uid(original_real_uid);
160 sock = socket(AF_INET, SOCK_STREAM, 0);
161 if (sock < 0)
162 fatal("socket: %.100s", strerror(errno));
163 restore_uid();
164 }
165 return sock;
166}
167
168/*
169 * Opens a TCP/IP connection to the remote server on the given host. If
170 * port is 0, the default port will be used. If anonymous is zero,
171 * a privileged port will be allocated to make the connection.
172 * This requires super-user privileges if anonymous is false.
173 * Connection_attempts specifies the maximum number of tries (one per
174 * second). If proxy_command is non-NULL, it specifies the command (with %h
175 * and %p substituted for host and port, respectively) to use to contact
176 * the daemon.
177 */
178int
179ssh_connect(const char *host, struct sockaddr_in * hostaddr,
180 int port, int connection_attempts,
181 int anonymous, uid_t original_real_uid,
182 const char *proxy_command)
183{
184 int sock = -1, attempt, i;
185 int on = 1;
186 struct servent *sp;
187 struct hostent *hp;
188 struct linger linger;
189
190 debug("ssh_connect: getuid %d geteuid %d anon %d",
191 (int) getuid(), (int) geteuid(), anonymous);
192
193 /* Get default port if port has not been set. */
194 if (port == 0) {
195 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
196 if (sp)
197 port = ntohs(sp->s_port);
198 else
199 port = SSH_DEFAULT_PORT;
200 }
201 /* If a proxy command is given, connect using it. */
202 if (proxy_command != NULL)
203 return ssh_proxy_connect(host, port, original_real_uid, proxy_command);
204
205 /* No proxy command. */
206
207 /* No host lookup made yet. */
208 hp = NULL;
209
210 /* Try to connect several times. On some machines, the first time
211 will sometimes fail. In general socket code appears to behave
212 quite magically on many machines. */
213 for (attempt = 0; attempt < connection_attempts; attempt++) {
214 if (attempt > 0)
215 debug("Trying again...");
216
217 /* Try to parse the host name as a numeric inet address. */
218 memset(hostaddr, 0, sizeof(hostaddr));
219 hostaddr->sin_family = AF_INET;
220 hostaddr->sin_port = htons(port);
221 hostaddr->sin_addr.s_addr = inet_addr(host);
222 if ((hostaddr->sin_addr.s_addr & 0xffffffff) != 0xffffffff) {
223 /* Valid numeric IP address */
224 debug("Connecting to %.100s port %d.",
225 inet_ntoa(hostaddr->sin_addr), port);
226
227 /* Create a socket. */
228 sock = ssh_create_socket(original_real_uid,
229 !anonymous && geteuid() == 0 &&
230 port < IPPORT_RESERVED);
231
Damien Miller5428f641999-11-25 11:54:57 +1100232 /*
233 * Connect to the host. We use the user's uid in the
234 * hope that it will help with the problems of
235 * tcp_wrappers showing the remote uid as root.
236 */
Damien Miller95def091999-11-25 00:26:21 +1100237 temporarily_use_uid(original_real_uid);
238 if (connect(sock, (struct sockaddr *) hostaddr, sizeof(*hostaddr))
239 >= 0) {
240 /* Successful connect. */
241 restore_uid();
242 break;
243 }
244 debug("connect: %.100s", strerror(errno));
245 restore_uid();
246
247 /* Destroy the failed socket. */
248 shutdown(sock, SHUT_RDWR);
249 close(sock);
250 } else {
251 /* Not a valid numeric inet address. */
252 /* Map host name to an address. */
253 if (!hp)
254 hp = gethostbyname(host);
255 if (!hp)
256 fatal("Bad host name: %.100s", host);
257 if (!hp->h_addr_list[0])
258 fatal("Host does not have an IP address: %.100s", host);
259
260 /* Loop through addresses for this host, and try
261 each one in sequence until the connection
262 succeeds. */
263 for (i = 0; hp->h_addr_list[i]; i++) {
264 /* Set the address to connect to. */
265 hostaddr->sin_family = hp->h_addrtype;
266 memcpy(&hostaddr->sin_addr, hp->h_addr_list[i],
267 sizeof(hostaddr->sin_addr));
268
269 debug("Connecting to %.200s [%.100s] port %d.",
270 host, inet_ntoa(hostaddr->sin_addr), port);
271
272 /* Create a socket for connecting. */
273 sock = ssh_create_socket(original_real_uid,
274 !anonymous && geteuid() == 0 &&
275 port < IPPORT_RESERVED);
276
Damien Miller5428f641999-11-25 11:54:57 +1100277 /*
278 * Connect to the host. We use the user's
279 * uid in the hope that it will help with
280 * tcp_wrappers showing the remote uid as
281 * root.
282 */
Damien Miller95def091999-11-25 00:26:21 +1100283 temporarily_use_uid(original_real_uid);
284 if (connect(sock, (struct sockaddr *) hostaddr,
285 sizeof(*hostaddr)) >= 0) {
286 /* Successful connection. */
287 restore_uid();
288 break;
289 }
290 debug("connect: %.100s", strerror(errno));
291 restore_uid();
292
Damien Miller5428f641999-11-25 11:54:57 +1100293 /*
294 * Close the failed socket; there appear to
295 * be some problems when reusing a socket for
296 * which connect() has already returned an
297 * error.
298 */
Damien Miller95def091999-11-25 00:26:21 +1100299 shutdown(sock, SHUT_RDWR);
300 close(sock);
301 }
302 if (hp->h_addr_list[i])
303 break; /* Successful connection. */
304 }
305
306 /* Sleep a moment before retrying. */
307 sleep(1);
308 }
309 /* Return failure if we didn't get a successful connection. */
310 if (attempt >= connection_attempts)
311 return 0;
312
313 debug("Connection established.");
314
Damien Miller5428f641999-11-25 11:54:57 +1100315 /*
316 * Set socket options. We would like the socket to disappear as soon
317 * as it has been closed for whatever reason.
318 */
319 /* setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
Damien Miller95def091999-11-25 00:26:21 +1100320 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &on, sizeof(on));
321 linger.l_onoff = 1;
322 linger.l_linger = 5;
323 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
324
325 /* Set the connection. */
326 packet_set_connection(sock, sock);
327
328 return 1;
329}
330
331/*
332 * Checks if the user has an authentication agent, and if so, tries to
333 * authenticate using the agent.
334 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335int
336try_agent_authentication()
337{
Damien Miller95def091999-11-25 00:26:21 +1100338 int status, type;
339 char *comment;
340 AuthenticationConnection *auth;
341 unsigned char response[16];
342 unsigned int i;
343 BIGNUM *e, *n, *challenge;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344
Damien Miller95def091999-11-25 00:26:21 +1100345 /* Get connection to the agent. */
346 auth = ssh_get_authentication_connection();
347 if (!auth)
348 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349
Damien Miller95def091999-11-25 00:26:21 +1100350 e = BN_new();
351 n = BN_new();
352 challenge = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
Damien Miller95def091999-11-25 00:26:21 +1100354 /* Loop through identities served by the agent. */
355 for (status = ssh_get_first_identity(auth, e, n, &comment);
356 status;
357 status = ssh_get_next_identity(auth, e, n, &comment)) {
358 int plen, clen;
359
360 /* Try this identity. */
361 debug("Trying RSA authentication via agent with '%.100s'", comment);
362 xfree(comment);
363
364 /* Tell the server that we are willing to authenticate using this key. */
365 packet_start(SSH_CMSG_AUTH_RSA);
366 packet_put_bignum(n);
367 packet_send();
368 packet_write_wait();
369
370 /* Wait for server's response. */
371 type = packet_read(&plen);
372
373 /* The server sends failure if it doesn\'t like our key or
374 does not support RSA authentication. */
375 if (type == SSH_SMSG_FAILURE) {
376 debug("Server refused our key.");
377 continue;
378 }
379 /* Otherwise it should have sent a challenge. */
380 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
381 packet_disconnect("Protocol error during RSA authentication: %d",
382 type);
383
384 packet_get_bignum(challenge, &clen);
385
386 packet_integrity_check(plen, clen, type);
387
388 debug("Received RSA challenge from server.");
389
390 /* Ask the agent to decrypt the challenge. */
391 if (!ssh_decrypt_challenge(auth, e, n, challenge,
392 session_id, 1, response)) {
393 /* The agent failed to authenticate this identifier although it
394 advertised it supports this. Just return a wrong value. */
395 log("Authentication agent failed to decrypt challenge.");
396 memset(response, 0, sizeof(response));
397 }
398 debug("Sending response to RSA challenge.");
399
400 /* Send the decrypted challenge back to the server. */
401 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
402 for (i = 0; i < 16; i++)
403 packet_put_char(response[i]);
404 packet_send();
405 packet_write_wait();
406
407 /* Wait for response from the server. */
408 type = packet_read(&plen);
409
410 /* The server returns success if it accepted the authentication. */
411 if (type == SSH_SMSG_SUCCESS) {
412 debug("RSA authentication accepted by server.");
413 BN_clear_free(e);
414 BN_clear_free(n);
415 BN_clear_free(challenge);
416 return 1;
417 }
418 /* Otherwise it should return failure. */
419 if (type != SSH_SMSG_FAILURE)
420 packet_disconnect("Protocol error waiting RSA auth response: %d",
421 type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422 }
423
Damien Miller95def091999-11-25 00:26:21 +1100424 BN_clear_free(e);
425 BN_clear_free(n);
426 BN_clear_free(challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427
Damien Miller95def091999-11-25 00:26:21 +1100428 debug("RSA authentication using agent refused.");
429 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430}
431
Damien Miller95def091999-11-25 00:26:21 +1100432/*
433 * Computes the proper response to a RSA challenge, and sends the response to
434 * the server.
435 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436void
Damien Miller95def091999-11-25 00:26:21 +1100437respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438{
Damien Miller95def091999-11-25 00:26:21 +1100439 unsigned char buf[32], response[16];
440 MD5_CTX md;
441 int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442
Damien Miller95def091999-11-25 00:26:21 +1100443 /* Decrypt the challenge using the private key. */
444 rsa_private_decrypt(challenge, challenge, prv);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445
Damien Miller95def091999-11-25 00:26:21 +1100446 /* Compute the response. */
447 /* The response is MD5 of decrypted challenge plus session id. */
448 len = BN_num_bytes(challenge);
449 if (len <= 0 || len > sizeof(buf))
450 packet_disconnect("respond_to_rsa_challenge: bad challenge length %d",
451 len);
Damien Millerfd7c9111999-11-08 16:15:55 +1100452
Damien Miller95def091999-11-25 00:26:21 +1100453 memset(buf, 0, sizeof(buf));
454 BN_bn2bin(challenge, buf + sizeof(buf) - len);
455 MD5_Init(&md);
456 MD5_Update(&md, buf, 32);
457 MD5_Update(&md, session_id, 16);
458 MD5_Final(response, &md);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459
Damien Miller95def091999-11-25 00:26:21 +1100460 debug("Sending response to host key RSA challenge.");
461
462 /* Send the response back to the server. */
463 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
464 for (i = 0; i < 16; i++)
465 packet_put_char(response[i]);
466 packet_send();
467 packet_write_wait();
468
469 memset(buf, 0, sizeof(buf));
470 memset(response, 0, sizeof(response));
471 memset(&md, 0, sizeof(md));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472}
473
Damien Miller95def091999-11-25 00:26:21 +1100474/*
475 * Checks if the user has authentication file, and if so, tries to authenticate
476 * the user using it.
477 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478int
Damien Miller95def091999-11-25 00:26:21 +1100479try_rsa_authentication(struct passwd * pw, const char *authfile)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480{
Damien Miller95def091999-11-25 00:26:21 +1100481 extern Options options;
482 BIGNUM *challenge;
483 RSA *private_key;
484 RSA *public_key;
485 char *passphrase, *comment;
486 int type, i;
487 int plen, clen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488
Damien Miller95def091999-11-25 00:26:21 +1100489 /* Try to load identification for the authentication key. */
490 public_key = RSA_new();
491 if (!load_public_key(authfile, public_key, &comment)) {
492 RSA_free(public_key);
493 return 0; /* Could not load it. Fail. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494 }
Damien Miller95def091999-11-25 00:26:21 +1100495 debug("Trying RSA authentication with key '%.100s'", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496
Damien Miller95def091999-11-25 00:26:21 +1100497 /* Tell the server that we are willing to authenticate using this key. */
498 packet_start(SSH_CMSG_AUTH_RSA);
499 packet_put_bignum(public_key->n);
500 packet_send();
501 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502
Damien Miller95def091999-11-25 00:26:21 +1100503 /* We no longer need the public key. */
504 RSA_free(public_key);
505
506 /* Wait for server's response. */
507 type = packet_read(&plen);
508
Damien Miller5428f641999-11-25 11:54:57 +1100509 /*
510 * The server responds with failure if it doesn\'t like our key or
511 * doesn\'t support RSA authentication.
512 */
Damien Miller95def091999-11-25 00:26:21 +1100513 if (type == SSH_SMSG_FAILURE) {
514 debug("Server refused our key.");
515 xfree(comment);
516 return 0; /* Server refuses to authenticate with
517 this key. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518 }
Damien Miller95def091999-11-25 00:26:21 +1100519 /* Otherwise, the server should respond with a challenge. */
520 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
521 packet_disconnect("Protocol error during RSA authentication: %d", type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522
Damien Miller95def091999-11-25 00:26:21 +1100523 /* Get the challenge from the packet. */
524 challenge = BN_new();
525 packet_get_bignum(challenge, &clen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526
Damien Miller95def091999-11-25 00:26:21 +1100527 packet_integrity_check(plen, clen, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
Damien Miller95def091999-11-25 00:26:21 +1100529 debug("Received RSA challenge from server.");
530
531 private_key = RSA_new();
Damien Miller5428f641999-11-25 11:54:57 +1100532 /*
533 * Load the private key. Try first with empty passphrase; if it
534 * fails, ask for a passphrase.
535 */
Damien Miller95def091999-11-25 00:26:21 +1100536 if (!load_private_key(authfile, "", private_key, NULL)) {
537 char buf[300];
538 snprintf(buf, sizeof buf, "Enter passphrase for RSA key '%.100s': ",
539 comment);
540 if (!options.batch_mode)
541 passphrase = read_passphrase(buf, 0);
542 else {
543 debug("Will not query passphrase for %.100s in batch mode.",
544 comment);
545 passphrase = xstrdup("");
546 }
547
548 /* Load the authentication file using the pasphrase. */
549 if (!load_private_key(authfile, passphrase, private_key, NULL)) {
550 memset(passphrase, 0, strlen(passphrase));
551 xfree(passphrase);
552 error("Bad passphrase.");
553
554 /* Send a dummy response packet to avoid protocol error. */
555 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
556 for (i = 0; i < 16; i++)
557 packet_put_char(0);
558 packet_send();
559 packet_write_wait();
560
561 /* Expect the server to reject it... */
562 packet_read_expect(&plen, SSH_SMSG_FAILURE);
563 xfree(comment);
564 return 0;
565 }
566 /* Destroy the passphrase. */
567 memset(passphrase, 0, strlen(passphrase));
568 xfree(passphrase);
569 }
570 /* We no longer need the comment. */
571 xfree(comment);
572
573 /* Compute and send a response to the challenge. */
574 respond_to_rsa_challenge(challenge, private_key);
575
576 /* Destroy the private key. */
577 RSA_free(private_key);
578
579 /* We no longer need the challenge. */
580 BN_clear_free(challenge);
581
582 /* Wait for response from the server. */
583 type = packet_read(&plen);
584 if (type == SSH_SMSG_SUCCESS) {
585 debug("RSA authentication accepted by server.");
586 return 1;
587 }
588 if (type != SSH_SMSG_FAILURE)
589 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
590 debug("RSA authentication refused.");
591 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592}
593
Damien Miller95def091999-11-25 00:26:21 +1100594/*
595 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
596 * authentication and RSA host authentication.
597 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000598int
Damien Miller95def091999-11-25 00:26:21 +1100599try_rhosts_rsa_authentication(const char *local_user, RSA * host_key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600{
Damien Miller95def091999-11-25 00:26:21 +1100601 int type;
602 BIGNUM *challenge;
603 int plen, clen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604
Damien Miller95def091999-11-25 00:26:21 +1100605 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606
Damien Miller95def091999-11-25 00:26:21 +1100607 /* Tell the server that we are willing to authenticate using this key. */
608 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
609 packet_put_string(local_user, strlen(local_user));
610 packet_put_int(BN_num_bits(host_key->n));
611 packet_put_bignum(host_key->e);
612 packet_put_bignum(host_key->n);
613 packet_send();
614 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615
Damien Miller95def091999-11-25 00:26:21 +1100616 /* Wait for server's response. */
617 type = packet_read(&plen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618
Damien Miller95def091999-11-25 00:26:21 +1100619 /* The server responds with failure if it doesn't admit our
620 .rhosts authentication or doesn't know our host key. */
621 if (type == SSH_SMSG_FAILURE) {
622 debug("Server refused our rhosts authentication or host key.");
623 return 0;
624 }
625 /* Otherwise, the server should respond with a challenge. */
626 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
627 packet_disconnect("Protocol error during RSA authentication: %d", type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628
Damien Miller95def091999-11-25 00:26:21 +1100629 /* Get the challenge from the packet. */
630 challenge = BN_new();
631 packet_get_bignum(challenge, &clen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632
Damien Miller95def091999-11-25 00:26:21 +1100633 packet_integrity_check(plen, clen, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634
Damien Miller95def091999-11-25 00:26:21 +1100635 debug("Received RSA challenge for host key from server.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636
Damien Miller95def091999-11-25 00:26:21 +1100637 /* Compute a response to the challenge. */
638 respond_to_rsa_challenge(challenge, host_key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000639
Damien Miller95def091999-11-25 00:26:21 +1100640 /* We no longer need the challenge. */
641 BN_clear_free(challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642
Damien Miller95def091999-11-25 00:26:21 +1100643 /* Wait for response from the server. */
644 type = packet_read(&plen);
645 if (type == SSH_SMSG_SUCCESS) {
646 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
647 return 1;
648 }
649 if (type != SSH_SMSG_FAILURE)
650 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
651 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
652 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000653}
654
655#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +1100656int
657try_kerberos_authentication()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658{
Damien Miller95def091999-11-25 00:26:21 +1100659 KTEXT_ST auth; /* Kerberos data */
660 char *reply;
661 char inst[INST_SZ];
662 char *realm;
663 CREDENTIALS cred;
664 int r, type, plen;
665 Key_schedule schedule;
666 u_long checksum, cksum;
667 MSG_DAT msg_data;
668 struct sockaddr_in local, foreign;
669 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670
Damien Miller95def091999-11-25 00:26:21 +1100671 /* Don't do anything if we don't have any tickets. */
672 if (stat(tkt_string(), &st) < 0)
673 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674
Damien Miller95def091999-11-25 00:26:21 +1100675 strncpy(inst, (char *) krb_get_phost(get_canonical_hostname()), INST_SZ);
676
677 realm = (char *) krb_realmofhost(get_canonical_hostname());
678 if (!realm) {
679 debug("Kerberos V4: no realm for %s", get_canonical_hostname());
680 return 0;
681 }
682 /* This can really be anything. */
683 checksum = (u_long) getpid();
684
685 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
686 if (r != KSUCCESS) {
687 debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]);
688 return 0;
689 }
690 /* Get session key to decrypt the server's reply with. */
691 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
692 if (r != KSUCCESS) {
693 debug("get_cred failed: %s", krb_err_txt[r]);
694 return 0;
695 }
696 des_key_sched((des_cblock *) cred.session, schedule);
697
698 /* Send authentication info to server. */
699 packet_start(SSH_CMSG_AUTH_KERBEROS);
700 packet_put_string((char *) auth.dat, auth.length);
701 packet_send();
702 packet_write_wait();
703
704 /* Zero the buffer. */
705 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
706
707 r = sizeof(local);
708 memset(&local, 0, sizeof(local));
709 if (getsockname(packet_get_connection_in(),
710 (struct sockaddr *) & local, &r) < 0)
711 debug("getsockname failed: %s", strerror(errno));
712
713 r = sizeof(foreign);
714 memset(&foreign, 0, sizeof(foreign));
715 if (getpeername(packet_get_connection_in(),
716 (struct sockaddr *) & foreign, &r) < 0) {
717 debug("getpeername failed: %s", strerror(errno));
718 fatal_cleanup();
719 }
720 /* Get server reply. */
721 type = packet_read(&plen);
722 switch (type) {
723 case SSH_SMSG_FAILURE:
724 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
725 debug("Kerberos V4 authentication failed.");
726 return 0;
727 break;
728
729 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
730 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
731 debug("Kerberos V4 authentication accepted.");
732
733 /* Get server's response. */
734 reply = packet_get_string((unsigned int *) &auth.length);
735 memcpy(auth.dat, reply, auth.length);
736 xfree(reply);
737
738 packet_integrity_check(plen, 4 + auth.length, type);
739
Damien Miller5428f641999-11-25 11:54:57 +1100740 /*
741 * If his response isn't properly encrypted with the session
742 * key, and the decrypted checksum fails to match, he's
743 * bogus. Bail out.
744 */
Damien Miller95def091999-11-25 00:26:21 +1100745 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
746 &foreign, &local, &msg_data);
747 if (r != KSUCCESS) {
748 debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]);
749 packet_disconnect("Kerberos V4 challenge failed!");
750 }
751 /* Fetch the (incremented) checksum that we supplied in the request. */
752 (void) memcpy((char *) &cksum, (char *) msg_data.app_data, sizeof(cksum));
753 cksum = ntohl(cksum);
754
755 /* If it matches, we're golden. */
756 if (cksum == checksum + 1) {
757 debug("Kerberos V4 challenge successful.");
758 return 1;
759 } else
760 packet_disconnect("Kerberos V4 challenge failed!");
761 break;
762
763 default:
764 packet_disconnect("Protocol error on Kerberos V4 response: %d", type);
765 }
766 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767}
Damien Miller95def091999-11-25 00:26:21 +1100768
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000769#endif /* KRB4 */
770
771#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100772int
773send_kerberos_tgt()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774{
Damien Miller95def091999-11-25 00:26:21 +1100775 CREDENTIALS *creds;
776 char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
777 int r, type, plen;
778 unsigned char buffer[8192];
779 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780
Damien Miller95def091999-11-25 00:26:21 +1100781 /* Don't do anything if we don't have any tickets. */
782 if (stat(tkt_string(), &st) < 0)
783 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784
Damien Miller95def091999-11-25 00:26:21 +1100785 creds = xmalloc(sizeof(*creds));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786
Damien Miller95def091999-11-25 00:26:21 +1100787 if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) {
788 debug("Kerberos V4 tf_fullname failed: %s", krb_err_txt[r]);
789 return 0;
790 }
791 if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) {
792 debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]);
793 return 0;
794 }
795 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
796 debug("Kerberos V4 ticket expired: %s", TKT_FILE);
797 return 0;
798 }
799 creds_to_radix(creds, buffer);
800 xfree(creds);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801
Damien Miller95def091999-11-25 00:26:21 +1100802 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
803 packet_put_string((char *) buffer, strlen(buffer));
804 packet_send();
805 packet_write_wait();
806
807 type = packet_read(&plen);
808
809 if (type == SSH_SMSG_FAILURE)
810 debug("Kerberos TGT for realm %s rejected.", prealm);
811 else if (type != SSH_SMSG_SUCCESS)
812 packet_disconnect("Protocol error on Kerberos TGT response: %d", type);
813
814 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815}
816
Damien Miller95def091999-11-25 00:26:21 +1100817void
818send_afs_tokens(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819{
Damien Miller95def091999-11-25 00:26:21 +1100820 CREDENTIALS creds;
821 struct ViceIoctl parms;
822 struct ClearToken ct;
823 int i, type, len, plen;
824 char buf[2048], *p, *server_cell;
825 unsigned char buffer[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826
Damien Miller95def091999-11-25 00:26:21 +1100827 /* Move over ktc_GetToken, here's something leaner. */
828 for (i = 0; i < 100; i++) { /* just in case */
829 parms.in = (char *) &i;
830 parms.in_size = sizeof(i);
831 parms.out = buf;
832 parms.out_size = sizeof(buf);
833 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
834 break;
835 p = buf;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000836
Damien Miller95def091999-11-25 00:26:21 +1100837 /* Get secret token. */
838 memcpy(&creds.ticket_st.length, p, sizeof(unsigned int));
839 if (creds.ticket_st.length > MAX_KTXT_LEN)
840 break;
841 p += sizeof(unsigned int);
842 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
843 p += creds.ticket_st.length;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000844
Damien Miller95def091999-11-25 00:26:21 +1100845 /* Get clear token. */
846 memcpy(&len, p, sizeof(len));
847 if (len != sizeof(struct ClearToken))
848 break;
849 p += sizeof(len);
850 memcpy(&ct, p, len);
851 p += len;
852 p += sizeof(len); /* primary flag */
853 server_cell = p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854
Damien Miller95def091999-11-25 00:26:21 +1100855 /* Flesh out our credentials. */
856 strlcpy(creds.service, "afs", sizeof creds.service);
857 creds.instance[0] = '\0';
858 strlcpy(creds.realm, server_cell, REALM_SZ);
859 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
860 creds.issue_date = ct.BeginTimestamp;
861 creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp);
862 creds.kvno = ct.AuthHandle;
863 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
864 creds.pinst[0] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000865
Damien Miller95def091999-11-25 00:26:21 +1100866 /* Encode token, ship it off. */
867 if (!creds_to_radix(&creds, buffer))
868 break;
869 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
870 packet_put_string((char *) buffer, strlen(buffer));
871 packet_send();
872 packet_write_wait();
873
874 /* Roger, Roger. Clearance, Clarence. What's your vector,
875 Victor? */
876 type = packet_read(&plen);
877
878 if (type == SSH_SMSG_FAILURE)
879 debug("AFS token for cell %s rejected.", server_cell);
880 else if (type != SSH_SMSG_SUCCESS)
881 packet_disconnect("Protocol error on AFS token response: %d", type);
882 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883}
Damien Miller95def091999-11-25 00:26:21 +1100884
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885#endif /* AFS */
886
Damien Miller95def091999-11-25 00:26:21 +1100887/*
888 * Waits for the server identification string, and sends our own
889 * identification string.
890 */
891void
892ssh_exchange_identification()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893{
Damien Miller95def091999-11-25 00:26:21 +1100894 char buf[256], remote_version[256]; /* must be same size! */
895 int remote_major, remote_minor, i;
896 int connection_in = packet_get_connection_in();
897 int connection_out = packet_get_connection_out();
898 extern Options options;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899
Damien Miller95def091999-11-25 00:26:21 +1100900 /* Read other side\'s version identification. */
901 for (i = 0; i < sizeof(buf) - 1; i++) {
902 if (read(connection_in, &buf[i], 1) != 1)
903 fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
904 if (buf[i] == '\r') {
905 buf[i] = '\n';
906 buf[i + 1] = 0;
907 break;
908 }
909 if (buf[i] == '\n') {
910 buf[i + 1] = 0;
911 break;
912 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913 }
Damien Miller95def091999-11-25 00:26:21 +1100914 buf[sizeof(buf) - 1] = 0;
915
Damien Miller5428f641999-11-25 11:54:57 +1100916 /*
917 * Check that the versions match. In future this might accept
918 * several versions and set appropriate flags to handle them.
919 */
Damien Miller95def091999-11-25 00:26:21 +1100920 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", &remote_major, &remote_minor,
921 remote_version) != 3)
922 fatal("Bad remote protocol version identification: '%.100s'", buf);
923 debug("Remote protocol version %d.%d, remote software version %.100s",
924 remote_major, remote_minor, remote_version);
925
926 /* Check if the remote protocol version is too old. */
927 if (remote_major == 1 && remote_minor < 3)
928 fatal("Remote machine has too old SSH software version.");
929
930 /* We speak 1.3, too. */
931 if (remote_major == 1 && remote_minor == 3) {
932 enable_compat13();
933 if (options.forward_agent && strcmp(remote_version, SSH_VERSION) != 0) {
934 log("Agent forwarding disabled, remote version '%s' is not compatible.",
935 remote_version);
936 options.forward_agent = 0;
937 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939#if 0
Damien Miller5428f641999-11-25 11:54:57 +1100940 /*
941 * Removed for now, to permit compatibility with latter versions. The
942 * server will reject our version and disconnect if it doesn't
943 * support it.
944 */
Damien Miller95def091999-11-25 00:26:21 +1100945 if (remote_major != PROTOCOL_MAJOR)
946 fatal("Protocol major versions differ: %d vs. %d",
947 PROTOCOL_MAJOR, remote_major);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948#endif
949
Damien Miller95def091999-11-25 00:26:21 +1100950 /* Send our own protocol version identification. */
951 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
952 PROTOCOL_MAJOR, PROTOCOL_MINOR, SSH_VERSION);
953 if (write(connection_out, buf, strlen(buf)) != strlen(buf))
954 fatal("write: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955}
956
957int ssh_cipher_default = SSH_CIPHER_3DES;
958
Damien Miller95def091999-11-25 00:26:21 +1100959int
960read_yes_or_no(const char *prompt, int defval)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961{
Damien Miller95def091999-11-25 00:26:21 +1100962 char buf[1024];
963 FILE *f;
964 int retval = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000965
Damien Miller95def091999-11-25 00:26:21 +1100966 if (isatty(0))
967 f = stdin;
968 else
969 f = fopen("/dev/tty", "rw");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970
Damien Miller95def091999-11-25 00:26:21 +1100971 if (f == NULL)
972 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973
Damien Miller95def091999-11-25 00:26:21 +1100974 fflush(stdout);
975
976 while (1) {
977 fprintf(stderr, "%s", prompt);
978 if (fgets(buf, sizeof(buf), f) == NULL) {
979 /* Print a newline (the prompt probably didn\'t have one). */
980 fprintf(stderr, "\n");
981 strlcpy(buf, "no", sizeof buf);
982 }
983 /* Remove newline from response. */
984 if (strchr(buf, '\n'))
985 *strchr(buf, '\n') = 0;
986
987 if (buf[0] == 0)
988 retval = defval;
989 if (strcmp(buf, "yes") == 0)
990 retval = 1;
991 if (strcmp(buf, "no") == 0)
992 retval = 0;
993
994 if (retval != -1) {
995 if (f != stdin)
996 fclose(f);
997 return retval;
998 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000999 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000}
1001
Damien Miller95def091999-11-25 00:26:21 +11001002/*
1003 * Starts a dialog with the server, and authenticates the current user on the
1004 * server. This does not need any extra privileges. The basic connection
1005 * to the server must already have been established before this is called.
1006 * User is the remote user; if it is NULL, the current local user name will
1007 * be used. Anonymous indicates that no rhosts authentication will be used.
1008 * If login fails, this function prints an error and never returns.
1009 * This function does not require super-user privileges.
1010 */
1011void
1012ssh_login(int host_key_valid,
1013 RSA *own_host_key,
1014 const char *orighost,
1015 struct sockaddr_in *hostaddr,
1016 uid_t original_real_uid)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017{
Damien Miller95def091999-11-25 00:26:21 +11001018 extern Options options;
1019 int i, type;
1020 char *password;
1021 struct passwd *pw;
1022 BIGNUM *key;
1023 RSA *host_key, *file_key;
1024 RSA *public_key;
1025 int bits, rbits;
1026 unsigned char session_key[SSH_SESSION_KEY_LENGTH];
1027 const char *server_user, *local_user;
1028 char *cp, *host, *ip = NULL;
1029 char hostline[1000], *hostp;
1030 unsigned char check_bytes[8];
1031 unsigned int supported_ciphers, supported_authentications, protocol_flags;
1032 HostStatus host_status;
1033 HostStatus ip_status;
1034 int host_ip_differ = 0;
1035 int local = (ntohl(hostaddr->sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
1036 int payload_len, clen, sum_len = 0;
1037 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001038
Damien Miller95def091999-11-25 00:26:21 +11001039 if (options.check_host_ip)
1040 ip = xstrdup(inet_ntoa(hostaddr->sin_addr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041
Damien Miller95def091999-11-25 00:26:21 +11001042 /* Convert the user-supplied hostname into all lowercase. */
1043 host = xstrdup(orighost);
1044 for (cp = host; *cp; cp++)
1045 if (isupper(*cp))
1046 *cp = tolower(*cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047
Damien Miller95def091999-11-25 00:26:21 +11001048 /* Exchange protocol version identification strings with the server. */
1049 ssh_exchange_identification();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050
Damien Miller95def091999-11-25 00:26:21 +11001051 /* Put the connection into non-blocking mode. */
1052 packet_set_nonblocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001053
Damien Miller95def091999-11-25 00:26:21 +11001054 /* Get local user name. Use it as server user if no user name was given. */
1055 pw = getpwuid(original_real_uid);
1056 if (!pw)
1057 fatal("User id %d not found from user database.", original_real_uid);
1058 local_user = xstrdup(pw->pw_name);
1059 server_user = options.user ? options.user : local_user;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060
Damien Miller95def091999-11-25 00:26:21 +11001061 debug("Waiting for server public key.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001062
Damien Miller95def091999-11-25 00:26:21 +11001063 /* Wait for a public key packet from the server. */
1064 packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065
Damien Miller95def091999-11-25 00:26:21 +11001066 /* Get check bytes from the packet. */
1067 for (i = 0; i < 8; i++)
1068 check_bytes[i] = packet_get_char();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069
Damien Miller95def091999-11-25 00:26:21 +11001070 /* Get the public key. */
1071 public_key = RSA_new();
1072 bits = packet_get_int();/* bits */
1073 public_key->e = BN_new();
1074 packet_get_bignum(public_key->e, &clen);
1075 sum_len += clen;
1076 public_key->n = BN_new();
1077 packet_get_bignum(public_key->n, &clen);
1078 sum_len += clen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001079
Damien Miller95def091999-11-25 00:26:21 +11001080 rbits = BN_num_bits(public_key->n);
1081 if (bits != rbits) {
1082 log("Warning: Server lies about size of server public key: "
1083 "actual size is %d bits vs. announced %d.", rbits, bits);
1084 log("Warning: This may be due to an old implementation of ssh.");
1085 }
1086 /* Get the host key. */
1087 host_key = RSA_new();
1088 bits = packet_get_int();/* bits */
1089 host_key->e = BN_new();
1090 packet_get_bignum(host_key->e, &clen);
1091 sum_len += clen;
1092 host_key->n = BN_new();
1093 packet_get_bignum(host_key->n, &clen);
1094 sum_len += clen;
Damien Millerda217a01999-11-09 10:35:52 +11001095
Damien Miller95def091999-11-25 00:26:21 +11001096 rbits = BN_num_bits(host_key->n);
1097 if (bits != rbits) {
1098 log("Warning: Server lies about size of server host key: "
1099 "actual size is %d bits vs. announced %d.", rbits, bits);
1100 log("Warning: This may be due to an old implementation of ssh.");
1101 }
1102 /* Store the host key from the known host file in here so that we
1103 can compare it with the key for the IP address. */
1104 file_key = RSA_new();
1105 file_key->n = BN_new();
1106 file_key->e = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107
Damien Miller95def091999-11-25 00:26:21 +11001108 /* Get protocol flags. */
1109 protocol_flags = packet_get_int();
1110 packet_set_protocol_flags(protocol_flags);
Damien Millerda217a01999-11-09 10:35:52 +11001111
Damien Miller95def091999-11-25 00:26:21 +11001112 supported_ciphers = packet_get_int();
Damien Miller95def091999-11-25 00:26:21 +11001113 supported_authentications = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001114
Damien Miller95def091999-11-25 00:26:21 +11001115 debug("Received server public key (%d bits) and host key (%d bits).",
1116 BN_num_bits(public_key->n), BN_num_bits(host_key->n));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001117
Damien Miller95def091999-11-25 00:26:21 +11001118 packet_integrity_check(payload_len,
1119 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
1120 SSH_SMSG_PUBLIC_KEY);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121
Damien Miller95def091999-11-25 00:26:21 +11001122 compute_session_id(session_id, check_bytes, host_key->n, public_key->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001123
Damien Miller5428f641999-11-25 11:54:57 +11001124 /*
1125 * Check if the host key is present in the user\'s list of known
1126 * hosts or in the systemwide list.
1127 */
Damien Miller95def091999-11-25 00:26:21 +11001128 host_status = check_host_in_hostfile(options.user_hostfile, host,
1129 host_key->e, host_key->n,
1130 file_key->e, file_key->n);
1131 if (host_status == HOST_NEW)
1132 host_status = check_host_in_hostfile(options.system_hostfile, host,
1133 host_key->e, host_key->n,
1134 file_key->e, file_key->n);
Damien Miller5428f641999-11-25 11:54:57 +11001135 /*
1136 * Force accepting of the host key for localhost and 127.0.0.1. The
1137 * problem is that if the home directory is NFS-mounted to multiple
1138 * machines, localhost will refer to a different machine in each of
1139 * them, and the user will get bogus HOST_CHANGED warnings. This
1140 * essentially disables host authentication for localhost; however,
1141 * this is probably not a real problem.
1142 */
Damien Miller95def091999-11-25 00:26:21 +11001143 if (local) {
1144 debug("Forcing accepting of host key for localhost.");
1145 host_status = HOST_OK;
1146 }
Damien Miller5428f641999-11-25 11:54:57 +11001147 /*
1148 * Also perform check for the ip address, skip the check if we are
1149 * localhost or the hostname was an ip address to begin with
1150 */
Damien Miller95def091999-11-25 00:26:21 +11001151 if (options.check_host_ip && !local && strcmp(host, ip)) {
1152 RSA *ip_key = RSA_new();
1153 ip_key->n = BN_new();
1154 ip_key->e = BN_new();
1155 ip_status = check_host_in_hostfile(options.user_hostfile, ip,
1156 host_key->e, host_key->n,
1157 ip_key->e, ip_key->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001158
Damien Miller95def091999-11-25 00:26:21 +11001159 if (ip_status == HOST_NEW)
1160 ip_status = check_host_in_hostfile(options.system_hostfile, ip,
1161 host_key->e, host_key->n,
1162 ip_key->e, ip_key->n);
1163 if (host_status == HOST_CHANGED &&
1164 (ip_status != HOST_CHANGED ||
1165 (BN_cmp(ip_key->e, file_key->e) || BN_cmp(ip_key->n, file_key->n))))
1166 host_ip_differ = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167
Damien Miller95def091999-11-25 00:26:21 +11001168 RSA_free(ip_key);
1169 } else
1170 ip_status = host_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171
Damien Miller95def091999-11-25 00:26:21 +11001172 RSA_free(file_key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173
Damien Miller95def091999-11-25 00:26:21 +11001174 switch (host_status) {
1175 case HOST_OK:
1176 /* The host is known and the key matches. */
1177 debug("Host '%.200s' is known and matches the host key.", host);
1178 if (options.check_host_ip) {
1179 if (ip_status == HOST_NEW) {
1180 if (!add_host_to_hostfile(options.user_hostfile, ip,
1181 host_key->e, host_key->n))
1182 log("Failed to add the host key for IP address '%.30s' to the list of known hosts (%.30s).",
1183 ip, options.user_hostfile);
1184 else
1185 log("Warning: Permanently added host key for IP address '%.30s' to the list of known hosts.",
1186 ip);
1187 } else if (ip_status != HOST_OK)
1188 log("Warning: the host key for '%.200s' differs from the key for the IP address '%.30s'",
1189 host, ip);
1190 }
1191 break;
1192 case HOST_NEW:
1193 /* The host is new. */
1194 if (options.strict_host_key_checking == 1) {
1195 /* User has requested strict host key checking. We will not add the host key
1196 automatically. The only alternative left is to abort. */
1197 fatal("No host key is known for %.200s and you have requested strict checking.", host);
1198 } else if (options.strict_host_key_checking == 2) {
1199 /* The default */
1200 char prompt[1024];
1201 char *fp = fingerprint(host_key->e, host_key->n);
1202 snprintf(prompt, sizeof(prompt),
1203 "The authenticity of host '%.200s' can't be established.\n"
1204 "Key fingerprint is %d %s.\n"
1205 "Are you sure you want to continue connecting (yes/no)? ",
1206 host, BN_num_bits(host_key->n), fp);
1207 if (!read_yes_or_no(prompt, -1))
1208 fatal("Aborted by user!\n");
1209 }
1210 if (options.check_host_ip && ip_status == HOST_NEW && strcmp(host, ip)) {
1211 snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
1212 hostp = hostline;
1213 } else
1214 hostp = host;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001215
Damien Miller95def091999-11-25 00:26:21 +11001216 /* If not in strict mode, add the key automatically to the local known_hosts file. */
1217 if (!add_host_to_hostfile(options.user_hostfile, hostp,
1218 host_key->e, host_key->n))
1219 log("Failed to add the host to the list of known hosts (%.500s).",
1220 options.user_hostfile);
1221 else
1222 log("Warning: Permanently added '%.200s' to the list of known hosts.",
1223 hostp);
1224 break;
1225 case HOST_CHANGED:
1226 if (options.check_host_ip && host_ip_differ) {
1227 char *msg;
1228 if (ip_status == HOST_NEW)
1229 msg = "is unknown";
1230 else if (ip_status == HOST_OK)
1231 msg = "is unchanged";
1232 else
1233 msg = "has a different value";
1234 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1235 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
1236 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1237 error("The host key for %s has changed,", host);
1238 error("and the key for the according IP address %s", ip);
1239 error("%s. This could either mean that", msg);
1240 error("DNS SPOOFING is happening or the IP address for the host");
1241 error("and its host key have changed at the same time");
1242 }
1243 /* The host key has changed. */
1244 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1245 error("@ WARNING: HOST IDENTIFICATION HAS CHANGED! @");
1246 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1247 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1248 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1249 error("It is also possible that the host key has just been changed.");
1250 error("Please contact your system administrator.");
1251 error("Add correct host key in %.100s to get rid of this message.",
1252 options.user_hostfile);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253
Damien Miller5428f641999-11-25 11:54:57 +11001254 /*
1255 * If strict host key checking is in use, the user will have
1256 * to edit the key manually and we can only abort.
1257 */
Damien Miller95def091999-11-25 00:26:21 +11001258 if (options.strict_host_key_checking)
1259 fatal("Host key for %.200s has changed and you have requested strict checking.", host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001260
Damien Miller5428f641999-11-25 11:54:57 +11001261 /*
1262 * If strict host key checking has not been requested, allow
1263 * the connection but without password authentication or
1264 * agent forwarding.
1265 */
Damien Miller95def091999-11-25 00:26:21 +11001266 if (options.password_authentication) {
1267 error("Password authentication is disabled to avoid trojan horses.");
1268 options.password_authentication = 0;
1269 }
1270 if (options.forward_agent) {
1271 error("Agent forwarding is disabled to avoid trojan horses.");
1272 options.forward_agent = 0;
1273 }
Damien Miller5428f641999-11-25 11:54:57 +11001274 /*
1275 * XXX Should permit the user to change to use the new id.
1276 * This could be done by converting the host key to an
1277 * identifying sentence, tell that the host identifies itself
1278 * by that sentence, and ask the user if he/she whishes to
1279 * accept the authentication.
1280 */
Damien Miller95def091999-11-25 00:26:21 +11001281 break;
1282 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001283
Damien Miller95def091999-11-25 00:26:21 +11001284 if (options.check_host_ip)
1285 xfree(ip);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001286
Damien Miller95def091999-11-25 00:26:21 +11001287 /* Generate a session key. */
1288 arc4random_stir();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001289
Damien Miller5428f641999-11-25 11:54:57 +11001290 /*
1291 * Generate an encryption key for the session. The key is a 256 bit
1292 * random number, interpreted as a 32-byte key, with the least
1293 * significant 8 bits being the first byte of the key.
1294 */
Damien Miller95def091999-11-25 00:26:21 +11001295 for (i = 0; i < 32; i++) {
1296 if (i % 4 == 0)
1297 rand = arc4random();
1298 session_key[i] = rand & 0xff;
1299 rand >>= 8;
1300 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001301
Damien Miller5428f641999-11-25 11:54:57 +11001302 /*
1303 * According to the protocol spec, the first byte of the session key
1304 * is the highest byte of the integer. The session key is xored with
1305 * the first 16 bytes of the session id.
1306 */
Damien Miller95def091999-11-25 00:26:21 +11001307 key = BN_new();
1308 BN_set_word(key, 0);
1309 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
1310 BN_lshift(key, key, 8);
1311 if (i < 16)
1312 BN_add_word(key, session_key[i] ^ session_id[i]);
1313 else
1314 BN_add_word(key, session_key[i]);
1315 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316
Damien Miller5428f641999-11-25 11:54:57 +11001317 /*
1318 * Encrypt the integer using the public key and host key of the
1319 * server (key with smaller modulus first).
1320 */
Damien Miller95def091999-11-25 00:26:21 +11001321 if (BN_cmp(public_key->n, host_key->n) < 0) {
1322 /* Public key has smaller modulus. */
1323 if (BN_num_bits(host_key->n) <
1324 BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) {
1325 fatal("respond_to_rsa_challenge: host_key %d < public_key %d + "
1326 "SSH_KEY_BITS_RESERVED %d",
1327 BN_num_bits(host_key->n),
1328 BN_num_bits(public_key->n),
1329 SSH_KEY_BITS_RESERVED);
1330 }
1331 rsa_public_encrypt(key, key, public_key);
1332 rsa_public_encrypt(key, key, host_key);
1333 } else {
1334 /* Host key has smaller modulus (or they are equal). */
1335 if (BN_num_bits(public_key->n) <
1336 BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) {
1337 fatal("respond_to_rsa_challenge: public_key %d < host_key %d + "
1338 "SSH_KEY_BITS_RESERVED %d",
1339 BN_num_bits(public_key->n),
1340 BN_num_bits(host_key->n),
1341 SSH_KEY_BITS_RESERVED);
1342 }
1343 rsa_public_encrypt(key, key, host_key);
1344 rsa_public_encrypt(key, key, public_key);
1345 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346
Damien Miller95def091999-11-25 00:26:21 +11001347 if (options.cipher == SSH_CIPHER_NOT_SET) {
1348 if (cipher_mask() & supported_ciphers & (1 << ssh_cipher_default))
1349 options.cipher = ssh_cipher_default;
1350 else {
1351 debug("Cipher %s not supported, using %.100s instead.",
1352 cipher_name(ssh_cipher_default),
1353 cipher_name(SSH_FALLBACK_CIPHER));
1354 options.cipher = SSH_FALLBACK_CIPHER;
1355 }
1356 }
1357 /* Check that the selected cipher is supported. */
1358 if (!(supported_ciphers & (1 << options.cipher)))
1359 fatal("Selected cipher type %.100s not supported by server.",
1360 cipher_name(options.cipher));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361
Damien Miller95def091999-11-25 00:26:21 +11001362 debug("Encryption type: %.100s", cipher_name(options.cipher));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363
Damien Miller95def091999-11-25 00:26:21 +11001364 /* Send the encrypted session key to the server. */
1365 packet_start(SSH_CMSG_SESSION_KEY);
1366 packet_put_char(options.cipher);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001367
Damien Miller95def091999-11-25 00:26:21 +11001368 /* Send the check bytes back to the server. */
1369 for (i = 0; i < 8; i++)
1370 packet_put_char(check_bytes[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001371
Damien Miller95def091999-11-25 00:26:21 +11001372 /* Send the encrypted encryption key. */
1373 packet_put_bignum(key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001374
Damien Miller95def091999-11-25 00:26:21 +11001375 /* Send protocol flags. */
1376 packet_put_int(SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001377
Damien Miller95def091999-11-25 00:26:21 +11001378 /* Send the packet now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379 packet_send();
1380 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001381
Damien Miller95def091999-11-25 00:26:21 +11001382 /* Destroy the session key integer and the public keys since we no longer need them. */
1383 BN_clear_free(key);
1384 RSA_free(public_key);
1385 RSA_free(host_key);
1386
1387 debug("Sent encrypted session key.");
1388
1389 /* Set the encryption key. */
1390 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
1391
1392 /* We will no longer need the session key here. Destroy any extra copies. */
1393 memset(session_key, 0, sizeof(session_key));
1394
Damien Miller5428f641999-11-25 11:54:57 +11001395 /*
1396 * Expect a success message from the server. Note that this message
1397 * will be received in encrypted form.
1398 */
Damien Miller95def091999-11-25 00:26:21 +11001399 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1400
1401 debug("Received encrypted confirmation.");
1402
1403 /* Send the name of the user to log in as on the server. */
1404 packet_start(SSH_CMSG_USER);
1405 packet_put_string(server_user, strlen(server_user));
1406 packet_send();
1407 packet_write_wait();
1408
Damien Miller5428f641999-11-25 11:54:57 +11001409 /*
1410 * The server should respond with success if no authentication is
1411 * needed (the user has no password). Otherwise the server responds
1412 * with failure.
1413 */
Damien Miller95def091999-11-25 00:26:21 +11001414 type = packet_read(&payload_len);
1415
1416 /* check whether the connection was accepted without authentication. */
1417 if (type == SSH_SMSG_SUCCESS)
1418 return;
1419 if (type != SSH_SMSG_FAILURE)
1420 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER",
1421 type);
1422
1423#ifdef AFS
1424 /* Try Kerberos tgt passing if the server supports it. */
1425 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1426 options.kerberos_tgt_passing) {
1427 if (options.cipher == SSH_CIPHER_NONE)
1428 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1429 (void) send_kerberos_tgt();
1430 }
1431 /* Try AFS token passing if the server supports it. */
1432 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
1433 options.afs_token_passing && k_hasafs()) {
1434 if (options.cipher == SSH_CIPHER_NONE)
1435 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
1436 send_afs_tokens();
1437 }
1438#endif /* AFS */
1439
1440#ifdef KRB4
1441 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1442 options.kerberos_authentication) {
1443 debug("Trying Kerberos authentication.");
1444 if (try_kerberos_authentication()) {
1445 /* The server should respond with success or failure. */
1446 type = packet_read(&payload_len);
1447 if (type == SSH_SMSG_SUCCESS)
1448 return;
1449 if (type != SSH_SMSG_FAILURE)
1450 packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
1451 }
1452 }
1453#endif /* KRB4 */
1454
Damien Miller5428f641999-11-25 11:54:57 +11001455 /*
1456 * Use rhosts authentication if running in privileged socket and we
1457 * do not wish to remain anonymous.
1458 */
Damien Miller95def091999-11-25 00:26:21 +11001459 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1460 options.rhosts_authentication) {
1461 debug("Trying rhosts authentication.");
1462 packet_start(SSH_CMSG_AUTH_RHOSTS);
1463 packet_put_string(local_user, strlen(local_user));
1464 packet_send();
1465 packet_write_wait();
1466
1467 /* The server should respond with success or failure. */
1468 type = packet_read(&payload_len);
1469 if (type == SSH_SMSG_SUCCESS)
1470 return;
1471 if (type != SSH_SMSG_FAILURE)
1472 packet_disconnect("Protocol error: got %d in response to rhosts auth",
1473 type);
1474 }
Damien Miller5428f641999-11-25 11:54:57 +11001475 /*
1476 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
1477 * authentication.
1478 */
Damien Miller95def091999-11-25 00:26:21 +11001479 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1480 options.rhosts_rsa_authentication && host_key_valid) {
1481 if (try_rhosts_rsa_authentication(local_user, own_host_key))
1482 return;
1483 }
1484 /* Try RSA authentication if the server supports it. */
1485 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1486 options.rsa_authentication) {
Damien Miller5428f641999-11-25 11:54:57 +11001487 /*
1488 * Try RSA authentication using the authentication agent. The
1489 * agent is tried first because no passphrase is needed for
1490 * it, whereas identity files may require passphrases.
1491 */
Damien Miller95def091999-11-25 00:26:21 +11001492 if (try_agent_authentication())
1493 return;
1494
1495 /* Try RSA authentication for each identity. */
1496 for (i = 0; i < options.num_identity_files; i++)
1497 if (try_rsa_authentication(pw, options.identity_files[i]))
1498 return;
1499 }
1500 /* Try skey authentication if the server supports it. */
1501 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1502 options.skey_authentication && !options.batch_mode) {
1503 debug("Doing skey authentication.");
1504
1505 /* request a challenge */
1506 packet_start(SSH_CMSG_AUTH_TIS);
1507 packet_send();
1508 packet_write_wait();
1509
1510 type = packet_read(&payload_len);
1511 if (type != SSH_SMSG_FAILURE &&
1512 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
1513 packet_disconnect("Protocol error: got %d in response "
1514 "to skey auth", type);
1515 }
1516 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
1517 debug("No challenge for skey authentication.");
1518 } else {
1519 char *challenge, *response;
1520 challenge = packet_get_string(&payload_len);
1521 if (options.cipher == SSH_CIPHER_NONE)
1522 log("WARNING: Encryption is disabled! "
1523 "Reponse will be transmitted in clear text.");
1524 fprintf(stderr, "%s\n", challenge);
1525 fflush(stderr);
1526 for (i = 0; i < options.number_of_password_prompts; i++) {
1527 if (i != 0)
1528 error("Permission denied, please try again.");
1529 response = read_passphrase("Response: ", 0);
1530 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
1531 packet_put_string(response, strlen(response));
1532 memset(response, 0, strlen(response));
1533 xfree(response);
1534 packet_send();
1535 packet_write_wait();
1536 type = packet_read(&payload_len);
1537 if (type == SSH_SMSG_SUCCESS)
1538 return;
1539 if (type != SSH_SMSG_FAILURE)
1540 packet_disconnect("Protocol error: got %d in response "
1541 "to skey auth", type);
1542 }
1543 }
1544 }
1545 /* Try password authentication if the server supports it. */
1546 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1547 options.password_authentication && !options.batch_mode) {
1548 char prompt[80];
1549 snprintf(prompt, sizeof(prompt), "%.30s@%.30s's password: ",
1550 server_user, host);
1551 debug("Doing password authentication.");
1552 if (options.cipher == SSH_CIPHER_NONE)
1553 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
1554 for (i = 0; i < options.number_of_password_prompts; i++) {
1555 if (i != 0)
1556 error("Permission denied, please try again.");
1557 password = read_passphrase(prompt, 0);
1558 packet_start(SSH_CMSG_AUTH_PASSWORD);
1559 packet_put_string(password, strlen(password));
1560 memset(password, 0, strlen(password));
1561 xfree(password);
1562 packet_send();
1563 packet_write_wait();
1564
1565 type = packet_read(&payload_len);
1566 if (type == SSH_SMSG_SUCCESS)
1567 return;
1568 if (type != SSH_SMSG_FAILURE)
1569 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
1570 }
1571 }
1572 /* All authentication methods have failed. Exit with an error message. */
1573 fatal("Permission denied.");
1574 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001575}