blob: fb6af67dfe7e3ea1e5ddc78266dd6a7b1c4dbddd [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 Miller34132e52000-01-14 15:45:46 +110011RCSID("$Id: sshconnect.c,v 1.21 2000/01/14 04:45:52 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 Milleraae6c611999-12-06 11:47:28 +110037extern Options options;
Damien Miller34132e52000-01-14 15:45:46 +110038extern char *__progname;
Damien Milleraae6c611999-12-06 11:47:28 +110039
Damien Miller95def091999-11-25 00:26:21 +110040/*
41 * Connect to the given ssh server using a proxy command.
42 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043int
Damien Milleraae6c611999-12-06 11:47:28 +110044ssh_proxy_connect(const char *host, u_short port, uid_t original_real_uid,
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045 const char *proxy_command)
46{
Damien Miller95def091999-11-25 00:26:21 +110047 Buffer command;
48 const char *cp;
49 char *command_string;
50 int pin[2], pout[2];
51 int pid;
Damien Miller34132e52000-01-14 15:45:46 +110052 char strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Damien Miller95def091999-11-25 00:26:21 +110054 /* Convert the port number into a string. */
Damien Miller34132e52000-01-14 15:45:46 +110055 snprintf(strport, sizeof strport, "%hu", port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
Damien Miller95def091999-11-25 00:26:21 +110057 /* Build the final command string in the buffer by making the
58 appropriate substitutions to the given proxy command. */
59 buffer_init(&command);
60 for (cp = proxy_command; *cp; cp++) {
61 if (cp[0] == '%' && cp[1] == '%') {
62 buffer_append(&command, "%", 1);
63 cp++;
64 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065 }
Damien Miller95def091999-11-25 00:26:21 +110066 if (cp[0] == '%' && cp[1] == 'h') {
67 buffer_append(&command, host, strlen(host));
68 cp++;
69 continue;
70 }
71 if (cp[0] == '%' && cp[1] == 'p') {
Damien Miller34132e52000-01-14 15:45:46 +110072 buffer_append(&command, strport, strlen(strport));
Damien Miller95def091999-11-25 00:26:21 +110073 cp++;
74 continue;
75 }
76 buffer_append(&command, cp, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 }
Damien Miller95def091999-11-25 00:26:21 +110078 buffer_append(&command, "\0", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Miller95def091999-11-25 00:26:21 +110080 /* Get the final command string. */
81 command_string = buffer_ptr(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller95def091999-11-25 00:26:21 +110083 /* Create pipes for communicating with the proxy. */
84 if (pipe(pin) < 0 || pipe(pout) < 0)
85 fatal("Could not create pipes to communicate with the proxy: %.100s",
86 strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller95def091999-11-25 00:26:21 +110088 debug("Executing proxy command: %.500s", command_string);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller95def091999-11-25 00:26:21 +110090 /* Fork and execute the proxy command. */
91 if ((pid = fork()) == 0) {
92 char *argv[10];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller95def091999-11-25 00:26:21 +110094 /* Child. Permanently give up superuser privileges. */
95 permanently_set_uid(original_real_uid);
96
97 /* Redirect stdin and stdout. */
98 close(pin[1]);
99 if (pin[0] != 0) {
100 if (dup2(pin[0], 0) < 0)
101 perror("dup2 stdin");
102 close(pin[0]);
103 }
104 close(pout[0]);
105 if (dup2(pout[1], 1) < 0)
106 perror("dup2 stdout");
107 /* Cannot be 1 because pin allocated two descriptors. */
108 close(pout[1]);
109
110 /* Stderr is left as it is so that error messages get
111 printed on the user's terminal. */
112 argv[0] = "/bin/sh";
113 argv[1] = "-c";
114 argv[2] = command_string;
115 argv[3] = NULL;
116
117 /* Execute the proxy command. Note that we gave up any
118 extra privileges above. */
119 execv("/bin/sh", argv);
120 perror("/bin/sh");
121 exit(1);
122 }
123 /* Parent. */
124 if (pid < 0)
125 fatal("fork failed: %.100s", strerror(errno));
126
127 /* Close child side of the descriptors. */
128 close(pin[0]);
129 close(pout[1]);
130
131 /* Free the command name. */
132 buffer_free(&command);
133
134 /* Set the connection file descriptors. */
135 packet_set_connection(pout[0], pin[1]);
136
137 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138}
139
Damien Miller95def091999-11-25 00:26:21 +1100140/*
141 * Creates a (possibly privileged) socket for use as the ssh connection.
142 */
143int
Damien Miller34132e52000-01-14 15:45:46 +1100144ssh_create_socket(uid_t original_real_uid, int privileged, int family)
Damien Miller95def091999-11-25 00:26:21 +1100145{
146 int sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147
Damien Miller5428f641999-11-25 11:54:57 +1100148 /*
149 * If we are running as root and want to connect to a privileged
150 * port, bind our own socket to a privileged port.
151 */
Damien Miller95def091999-11-25 00:26:21 +1100152 if (privileged) {
153 int p = IPPORT_RESERVED - 1;
Damien Miller34132e52000-01-14 15:45:46 +1100154 sock = rresvport_af(&p, family);
Damien Miller95def091999-11-25 00:26:21 +1100155 if (sock < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100156 fatal("rresvport: af=%d %.100s", family, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100157 debug("Allocated local port %d.", p);
158 } else {
Damien Millera34a28b1999-12-14 10:47:15 +1100159 /*
160 * Just create an ordinary socket on arbitrary port. We use
161 * the user's uid to create the socket.
162 */
Damien Miller95def091999-11-25 00:26:21 +1100163 temporarily_use_uid(original_real_uid);
Damien Miller34132e52000-01-14 15:45:46 +1100164 sock = socket(family, SOCK_STREAM, 0);
Damien Miller95def091999-11-25 00:26:21 +1100165 if (sock < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100166 error("socket: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100167 restore_uid();
168 }
169 return sock;
170}
171
172/*
Damien Miller34132e52000-01-14 15:45:46 +1100173 * Opens a TCP/IP connection to the remote server on the given host.
174 * The address of the remote host will be returned in hostaddr.
175 * If port is 0, the default port will be used. If anonymous is zero,
Damien Miller95def091999-11-25 00:26:21 +1100176 * a privileged port will be allocated to make the connection.
177 * This requires super-user privileges if anonymous is false.
178 * Connection_attempts specifies the maximum number of tries (one per
179 * second). If proxy_command is non-NULL, it specifies the command (with %h
180 * and %p substituted for host and port, respectively) to use to contact
181 * the daemon.
182 */
183int
Damien Miller34132e52000-01-14 15:45:46 +1100184ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
Damien Milleraae6c611999-12-06 11:47:28 +1100185 u_short port, int connection_attempts,
Damien Miller95def091999-11-25 00:26:21 +1100186 int anonymous, uid_t original_real_uid,
187 const char *proxy_command)
188{
Damien Miller34132e52000-01-14 15:45:46 +1100189 int sock = -1, attempt;
Damien Miller95def091999-11-25 00:26:21 +1100190 struct servent *sp;
Damien Miller34132e52000-01-14 15:45:46 +1100191 struct addrinfo hints, *ai, *aitop;
192 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
193 int gaierr;
Damien Miller95def091999-11-25 00:26:21 +1100194 struct linger linger;
195
196 debug("ssh_connect: getuid %d geteuid %d anon %d",
197 (int) getuid(), (int) geteuid(), anonymous);
198
199 /* Get default port if port has not been set. */
200 if (port == 0) {
201 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
202 if (sp)
203 port = ntohs(sp->s_port);
204 else
205 port = SSH_DEFAULT_PORT;
206 }
207 /* If a proxy command is given, connect using it. */
208 if (proxy_command != NULL)
209 return ssh_proxy_connect(host, port, original_real_uid, proxy_command);
210
211 /* No proxy command. */
212
Damien Miller34132e52000-01-14 15:45:46 +1100213 memset(&hints, 0, sizeof(hints));
214 hints.ai_family = IPv4or6;
215 hints.ai_socktype = SOCK_STREAM;
216 snprintf(strport, sizeof strport, "%d", port);
217 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
218 fatal("%s: %.100s: %s", __progname, host,
219 gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +1100220
Damien Millera34a28b1999-12-14 10:47:15 +1100221 /*
222 * Try to connect several times. On some machines, the first time
223 * will sometimes fail. In general socket code appears to behave
224 * quite magically on many machines.
225 */
Damien Miller95def091999-11-25 00:26:21 +1100226 for (attempt = 0; attempt < connection_attempts; attempt++) {
227 if (attempt > 0)
228 debug("Trying again...");
229
Damien Miller34132e52000-01-14 15:45:46 +1100230 /* Loop through addresses for this host, and try each one in
231 sequence until the connection succeeds. */
232 for (ai = aitop; ai; ai = ai->ai_next) {
233 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
234 continue;
235 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
236 ntop, sizeof(ntop), strport, sizeof(strport),
237 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
238 error("ssh_connect: getnameinfo failed");
239 continue;
240 }
241 debug("Connecting to %.200s [%.100s] port %s.",
242 host, ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +1100243
Damien Miller34132e52000-01-14 15:45:46 +1100244 /* Create a socket for connecting. */
245 sock = ssh_create_socket(original_real_uid,
246 !anonymous && geteuid() == 0 && port < IPPORT_RESERVED,
247 ai->ai_family);
248 if (sock < 0)
249 continue;
Damien Miller95def091999-11-25 00:26:21 +1100250
Damien Miller34132e52000-01-14 15:45:46 +1100251 /* Connect to the host. We use the user's uid in the
252 * hope that it will help with tcp_wrappers showing
253 * the remote uid as root.
Damien Miller5428f641999-11-25 11:54:57 +1100254 */
Damien Miller95def091999-11-25 00:26:21 +1100255 temporarily_use_uid(original_real_uid);
Damien Miller34132e52000-01-14 15:45:46 +1100256 if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) {
257 /* Successful connection. */
258 memcpy(hostaddr, ai->ai_addr, sizeof(*(ai->ai_addr)));
Damien Miller95def091999-11-25 00:26:21 +1100259 restore_uid();
260 break;
Damien Miller34132e52000-01-14 15:45:46 +1100261 } else {
Damien Miller95def091999-11-25 00:26:21 +1100262 debug("connect: %.100s", strerror(errno));
263 restore_uid();
Damien Miller5428f641999-11-25 11:54:57 +1100264 /*
265 * Close the failed socket; there appear to
266 * be some problems when reusing a socket for
267 * which connect() has already returned an
268 * error.
269 */
Damien Miller95def091999-11-25 00:26:21 +1100270 shutdown(sock, SHUT_RDWR);
271 close(sock);
272 }
Damien Miller95def091999-11-25 00:26:21 +1100273 }
Damien Miller34132e52000-01-14 15:45:46 +1100274 if (ai)
275 break; /* Successful connection. */
Damien Miller95def091999-11-25 00:26:21 +1100276
277 /* Sleep a moment before retrying. */
278 sleep(1);
279 }
Damien Miller34132e52000-01-14 15:45:46 +1100280
281 freeaddrinfo(aitop);
282
Damien Miller95def091999-11-25 00:26:21 +1100283 /* Return failure if we didn't get a successful connection. */
284 if (attempt >= connection_attempts)
285 return 0;
286
287 debug("Connection established.");
288
Damien Miller5428f641999-11-25 11:54:57 +1100289 /*
290 * Set socket options. We would like the socket to disappear as soon
291 * as it has been closed for whatever reason.
292 */
293 /* setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
Damien Miller95def091999-11-25 00:26:21 +1100294 linger.l_onoff = 1;
295 linger.l_linger = 5;
296 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
297
298 /* Set the connection. */
299 packet_set_connection(sock, sock);
300
301 return 1;
302}
303
304/*
305 * Checks if the user has an authentication agent, and if so, tries to
306 * authenticate using the agent.
307 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308int
309try_agent_authentication()
310{
Damien Miller95def091999-11-25 00:26:21 +1100311 int status, type;
312 char *comment;
313 AuthenticationConnection *auth;
314 unsigned char response[16];
315 unsigned int i;
316 BIGNUM *e, *n, *challenge;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317
Damien Miller95def091999-11-25 00:26:21 +1100318 /* Get connection to the agent. */
319 auth = ssh_get_authentication_connection();
320 if (!auth)
321 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Miller95def091999-11-25 00:26:21 +1100323 e = BN_new();
324 n = BN_new();
325 challenge = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326
Damien Miller95def091999-11-25 00:26:21 +1100327 /* Loop through identities served by the agent. */
328 for (status = ssh_get_first_identity(auth, e, n, &comment);
329 status;
330 status = ssh_get_next_identity(auth, e, n, &comment)) {
331 int plen, clen;
332
333 /* Try this identity. */
334 debug("Trying RSA authentication via agent with '%.100s'", comment);
335 xfree(comment);
336
337 /* Tell the server that we are willing to authenticate using this key. */
338 packet_start(SSH_CMSG_AUTH_RSA);
339 packet_put_bignum(n);
340 packet_send();
341 packet_write_wait();
342
343 /* Wait for server's response. */
344 type = packet_read(&plen);
345
346 /* The server sends failure if it doesn\'t like our key or
347 does not support RSA authentication. */
348 if (type == SSH_SMSG_FAILURE) {
349 debug("Server refused our key.");
350 continue;
351 }
352 /* Otherwise it should have sent a challenge. */
353 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
354 packet_disconnect("Protocol error during RSA authentication: %d",
355 type);
356
357 packet_get_bignum(challenge, &clen);
358
359 packet_integrity_check(plen, clen, type);
360
361 debug("Received RSA challenge from server.");
362
363 /* Ask the agent to decrypt the challenge. */
364 if (!ssh_decrypt_challenge(auth, e, n, challenge,
365 session_id, 1, response)) {
366 /* The agent failed to authenticate this identifier although it
367 advertised it supports this. Just return a wrong value. */
368 log("Authentication agent failed to decrypt challenge.");
369 memset(response, 0, sizeof(response));
370 }
371 debug("Sending response to RSA challenge.");
372
373 /* Send the decrypted challenge back to the server. */
374 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
375 for (i = 0; i < 16; i++)
376 packet_put_char(response[i]);
377 packet_send();
378 packet_write_wait();
379
380 /* Wait for response from the server. */
381 type = packet_read(&plen);
382
383 /* The server returns success if it accepted the authentication. */
384 if (type == SSH_SMSG_SUCCESS) {
385 debug("RSA authentication accepted by server.");
386 BN_clear_free(e);
387 BN_clear_free(n);
388 BN_clear_free(challenge);
389 return 1;
390 }
391 /* Otherwise it should return failure. */
392 if (type != SSH_SMSG_FAILURE)
393 packet_disconnect("Protocol error waiting RSA auth response: %d",
394 type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395 }
396
Damien Miller95def091999-11-25 00:26:21 +1100397 BN_clear_free(e);
398 BN_clear_free(n);
399 BN_clear_free(challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400
Damien Miller95def091999-11-25 00:26:21 +1100401 debug("RSA authentication using agent refused.");
402 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403}
404
Damien Miller95def091999-11-25 00:26:21 +1100405/*
406 * Computes the proper response to a RSA challenge, and sends the response to
407 * the server.
408 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409void
Damien Miller95def091999-11-25 00:26:21 +1100410respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411{
Damien Miller95def091999-11-25 00:26:21 +1100412 unsigned char buf[32], response[16];
413 MD5_CTX md;
414 int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
Damien Miller95def091999-11-25 00:26:21 +1100416 /* Decrypt the challenge using the private key. */
417 rsa_private_decrypt(challenge, challenge, prv);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418
Damien Miller95def091999-11-25 00:26:21 +1100419 /* Compute the response. */
420 /* The response is MD5 of decrypted challenge plus session id. */
421 len = BN_num_bytes(challenge);
422 if (len <= 0 || len > sizeof(buf))
423 packet_disconnect("respond_to_rsa_challenge: bad challenge length %d",
424 len);
Damien Millerfd7c9111999-11-08 16:15:55 +1100425
Damien Miller95def091999-11-25 00:26:21 +1100426 memset(buf, 0, sizeof(buf));
427 BN_bn2bin(challenge, buf + sizeof(buf) - len);
428 MD5_Init(&md);
429 MD5_Update(&md, buf, 32);
430 MD5_Update(&md, session_id, 16);
431 MD5_Final(response, &md);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432
Damien Miller95def091999-11-25 00:26:21 +1100433 debug("Sending response to host key RSA challenge.");
434
435 /* Send the response back to the server. */
436 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
437 for (i = 0; i < 16; i++)
438 packet_put_char(response[i]);
439 packet_send();
440 packet_write_wait();
441
442 memset(buf, 0, sizeof(buf));
443 memset(response, 0, sizeof(response));
444 memset(&md, 0, sizeof(md));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445}
446
Damien Miller95def091999-11-25 00:26:21 +1100447/*
448 * Checks if the user has authentication file, and if so, tries to authenticate
449 * the user using it.
450 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451int
Damien Milleraae6c611999-12-06 11:47:28 +1100452try_rsa_authentication(const char *authfile)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453{
Damien Miller95def091999-11-25 00:26:21 +1100454 BIGNUM *challenge;
455 RSA *private_key;
456 RSA *public_key;
457 char *passphrase, *comment;
458 int type, i;
459 int plen, clen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460
Damien Miller95def091999-11-25 00:26:21 +1100461 /* Try to load identification for the authentication key. */
462 public_key = RSA_new();
463 if (!load_public_key(authfile, public_key, &comment)) {
464 RSA_free(public_key);
Damien Milleraae6c611999-12-06 11:47:28 +1100465 /* Could not load it. Fail. */
466 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467 }
Damien Miller95def091999-11-25 00:26:21 +1100468 debug("Trying RSA authentication with key '%.100s'", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469
Damien Miller95def091999-11-25 00:26:21 +1100470 /* Tell the server that we are willing to authenticate using this key. */
471 packet_start(SSH_CMSG_AUTH_RSA);
472 packet_put_bignum(public_key->n);
473 packet_send();
474 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475
Damien Miller95def091999-11-25 00:26:21 +1100476 /* We no longer need the public key. */
477 RSA_free(public_key);
478
479 /* Wait for server's response. */
480 type = packet_read(&plen);
481
Damien Miller5428f641999-11-25 11:54:57 +1100482 /*
483 * The server responds with failure if it doesn\'t like our key or
484 * doesn\'t support RSA authentication.
485 */
Damien Miller95def091999-11-25 00:26:21 +1100486 if (type == SSH_SMSG_FAILURE) {
487 debug("Server refused our key.");
488 xfree(comment);
Damien Milleraae6c611999-12-06 11:47:28 +1100489 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490 }
Damien Miller95def091999-11-25 00:26:21 +1100491 /* Otherwise, the server should respond with a challenge. */
492 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
493 packet_disconnect("Protocol error during RSA authentication: %d", type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494
Damien Miller95def091999-11-25 00:26:21 +1100495 /* Get the challenge from the packet. */
496 challenge = BN_new();
497 packet_get_bignum(challenge, &clen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498
Damien Miller95def091999-11-25 00:26:21 +1100499 packet_integrity_check(plen, clen, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500
Damien Miller95def091999-11-25 00:26:21 +1100501 debug("Received RSA challenge from server.");
502
503 private_key = RSA_new();
Damien Miller5428f641999-11-25 11:54:57 +1100504 /*
505 * Load the private key. Try first with empty passphrase; if it
506 * fails, ask for a passphrase.
507 */
Damien Miller95def091999-11-25 00:26:21 +1100508 if (!load_private_key(authfile, "", private_key, NULL)) {
509 char buf[300];
510 snprintf(buf, sizeof buf, "Enter passphrase for RSA key '%.100s': ",
Damien Miller037a0dc1999-12-07 15:38:31 +1100511 comment);
Damien Miller95def091999-11-25 00:26:21 +1100512 if (!options.batch_mode)
513 passphrase = read_passphrase(buf, 0);
514 else {
515 debug("Will not query passphrase for %.100s in batch mode.",
516 comment);
517 passphrase = xstrdup("");
518 }
519
520 /* Load the authentication file using the pasphrase. */
521 if (!load_private_key(authfile, passphrase, private_key, NULL)) {
522 memset(passphrase, 0, strlen(passphrase));
523 xfree(passphrase);
524 error("Bad passphrase.");
525
526 /* Send a dummy response packet to avoid protocol error. */
527 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
528 for (i = 0; i < 16; i++)
529 packet_put_char(0);
530 packet_send();
531 packet_write_wait();
532
533 /* Expect the server to reject it... */
534 packet_read_expect(&plen, SSH_SMSG_FAILURE);
535 xfree(comment);
536 return 0;
537 }
538 /* Destroy the passphrase. */
539 memset(passphrase, 0, strlen(passphrase));
540 xfree(passphrase);
541 }
542 /* We no longer need the comment. */
543 xfree(comment);
544
545 /* Compute and send a response to the challenge. */
546 respond_to_rsa_challenge(challenge, private_key);
547
548 /* Destroy the private key. */
549 RSA_free(private_key);
550
551 /* We no longer need the challenge. */
552 BN_clear_free(challenge);
553
554 /* Wait for response from the server. */
555 type = packet_read(&plen);
556 if (type == SSH_SMSG_SUCCESS) {
557 debug("RSA authentication accepted by server.");
558 return 1;
559 }
560 if (type != SSH_SMSG_FAILURE)
561 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
562 debug("RSA authentication refused.");
563 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564}
565
Damien Miller95def091999-11-25 00:26:21 +1100566/*
567 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
568 * authentication and RSA host authentication.
569 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570int
Damien Miller95def091999-11-25 00:26:21 +1100571try_rhosts_rsa_authentication(const char *local_user, RSA * host_key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572{
Damien Miller95def091999-11-25 00:26:21 +1100573 int type;
574 BIGNUM *challenge;
575 int plen, clen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576
Damien Miller95def091999-11-25 00:26:21 +1100577 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578
Damien Miller95def091999-11-25 00:26:21 +1100579 /* Tell the server that we are willing to authenticate using this key. */
580 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
581 packet_put_string(local_user, strlen(local_user));
582 packet_put_int(BN_num_bits(host_key->n));
583 packet_put_bignum(host_key->e);
584 packet_put_bignum(host_key->n);
585 packet_send();
586 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587
Damien Miller95def091999-11-25 00:26:21 +1100588 /* Wait for server's response. */
589 type = packet_read(&plen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590
Damien Miller95def091999-11-25 00:26:21 +1100591 /* The server responds with failure if it doesn't admit our
592 .rhosts authentication or doesn't know our host key. */
593 if (type == SSH_SMSG_FAILURE) {
594 debug("Server refused our rhosts authentication or host key.");
595 return 0;
596 }
597 /* Otherwise, the server should respond with a challenge. */
598 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
599 packet_disconnect("Protocol error during RSA authentication: %d", type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600
Damien Miller95def091999-11-25 00:26:21 +1100601 /* Get the challenge from the packet. */
602 challenge = BN_new();
603 packet_get_bignum(challenge, &clen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604
Damien Miller95def091999-11-25 00:26:21 +1100605 packet_integrity_check(plen, clen, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606
Damien Miller95def091999-11-25 00:26:21 +1100607 debug("Received RSA challenge for host key from server.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608
Damien Miller95def091999-11-25 00:26:21 +1100609 /* Compute a response to the challenge. */
610 respond_to_rsa_challenge(challenge, host_key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611
Damien Miller95def091999-11-25 00:26:21 +1100612 /* We no longer need the challenge. */
613 BN_clear_free(challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614
Damien Miller95def091999-11-25 00:26:21 +1100615 /* Wait for response from the server. */
616 type = packet_read(&plen);
617 if (type == SSH_SMSG_SUCCESS) {
618 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
619 return 1;
620 }
621 if (type != SSH_SMSG_FAILURE)
622 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
623 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
624 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625}
626
627#ifdef KRB4
Damien Miller95def091999-11-25 00:26:21 +1100628int
629try_kerberos_authentication()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630{
Damien Miller95def091999-11-25 00:26:21 +1100631 KTEXT_ST auth; /* Kerberos data */
632 char *reply;
633 char inst[INST_SZ];
634 char *realm;
635 CREDENTIALS cred;
636 int r, type, plen;
637 Key_schedule schedule;
638 u_long checksum, cksum;
639 MSG_DAT msg_data;
640 struct sockaddr_in local, foreign;
641 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642
Damien Miller95def091999-11-25 00:26:21 +1100643 /* Don't do anything if we don't have any tickets. */
644 if (stat(tkt_string(), &st) < 0)
645 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000646
Damien Miller95def091999-11-25 00:26:21 +1100647 strncpy(inst, (char *) krb_get_phost(get_canonical_hostname()), INST_SZ);
648
649 realm = (char *) krb_realmofhost(get_canonical_hostname());
650 if (!realm) {
651 debug("Kerberos V4: no realm for %s", get_canonical_hostname());
652 return 0;
653 }
654 /* This can really be anything. */
655 checksum = (u_long) getpid();
656
657 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
658 if (r != KSUCCESS) {
659 debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]);
660 return 0;
661 }
662 /* Get session key to decrypt the server's reply with. */
663 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
664 if (r != KSUCCESS) {
665 debug("get_cred failed: %s", krb_err_txt[r]);
666 return 0;
667 }
668 des_key_sched((des_cblock *) cred.session, schedule);
669
670 /* Send authentication info to server. */
671 packet_start(SSH_CMSG_AUTH_KERBEROS);
672 packet_put_string((char *) auth.dat, auth.length);
673 packet_send();
674 packet_write_wait();
675
676 /* Zero the buffer. */
677 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
678
679 r = sizeof(local);
680 memset(&local, 0, sizeof(local));
681 if (getsockname(packet_get_connection_in(),
682 (struct sockaddr *) & local, &r) < 0)
683 debug("getsockname failed: %s", strerror(errno));
684
685 r = sizeof(foreign);
686 memset(&foreign, 0, sizeof(foreign));
687 if (getpeername(packet_get_connection_in(),
688 (struct sockaddr *) & foreign, &r) < 0) {
689 debug("getpeername failed: %s", strerror(errno));
690 fatal_cleanup();
691 }
692 /* Get server reply. */
693 type = packet_read(&plen);
694 switch (type) {
695 case SSH_SMSG_FAILURE:
696 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
697 debug("Kerberos V4 authentication failed.");
698 return 0;
699 break;
700
701 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
702 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
703 debug("Kerberos V4 authentication accepted.");
704
705 /* Get server's response. */
706 reply = packet_get_string((unsigned int *) &auth.length);
707 memcpy(auth.dat, reply, auth.length);
708 xfree(reply);
709
710 packet_integrity_check(plen, 4 + auth.length, type);
711
Damien Miller5428f641999-11-25 11:54:57 +1100712 /*
713 * If his response isn't properly encrypted with the session
714 * key, and the decrypted checksum fails to match, he's
715 * bogus. Bail out.
716 */
Damien Miller95def091999-11-25 00:26:21 +1100717 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
718 &foreign, &local, &msg_data);
719 if (r != KSUCCESS) {
720 debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]);
721 packet_disconnect("Kerberos V4 challenge failed!");
722 }
723 /* Fetch the (incremented) checksum that we supplied in the request. */
724 (void) memcpy((char *) &cksum, (char *) msg_data.app_data, sizeof(cksum));
725 cksum = ntohl(cksum);
726
727 /* If it matches, we're golden. */
728 if (cksum == checksum + 1) {
729 debug("Kerberos V4 challenge successful.");
730 return 1;
731 } else
732 packet_disconnect("Kerberos V4 challenge failed!");
733 break;
734
735 default:
736 packet_disconnect("Protocol error on Kerberos V4 response: %d", type);
737 }
738 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739}
Damien Miller95def091999-11-25 00:26:21 +1100740
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741#endif /* KRB4 */
742
743#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100744int
745send_kerberos_tgt()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000746{
Damien Miller95def091999-11-25 00:26:21 +1100747 CREDENTIALS *creds;
748 char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
749 int r, type, plen;
750 unsigned char buffer[8192];
751 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752
Damien Miller95def091999-11-25 00:26:21 +1100753 /* Don't do anything if we don't have any tickets. */
754 if (stat(tkt_string(), &st) < 0)
755 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000756
Damien Miller95def091999-11-25 00:26:21 +1100757 creds = xmalloc(sizeof(*creds));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000758
Damien Miller95def091999-11-25 00:26:21 +1100759 if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) {
760 debug("Kerberos V4 tf_fullname failed: %s", krb_err_txt[r]);
761 return 0;
762 }
763 if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) {
764 debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]);
765 return 0;
766 }
767 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
768 debug("Kerberos V4 ticket expired: %s", TKT_FILE);
769 return 0;
770 }
771 creds_to_radix(creds, buffer);
772 xfree(creds);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000773
Damien Miller95def091999-11-25 00:26:21 +1100774 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
775 packet_put_string((char *) buffer, strlen(buffer));
776 packet_send();
777 packet_write_wait();
778
779 type = packet_read(&plen);
780
781 if (type == SSH_SMSG_FAILURE)
782 debug("Kerberos TGT for realm %s rejected.", prealm);
783 else if (type != SSH_SMSG_SUCCESS)
784 packet_disconnect("Protocol error on Kerberos TGT response: %d", type);
785
786 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787}
788
Damien Miller95def091999-11-25 00:26:21 +1100789void
790send_afs_tokens(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791{
Damien Miller95def091999-11-25 00:26:21 +1100792 CREDENTIALS creds;
793 struct ViceIoctl parms;
794 struct ClearToken ct;
795 int i, type, len, plen;
796 char buf[2048], *p, *server_cell;
797 unsigned char buffer[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798
Damien Miller95def091999-11-25 00:26:21 +1100799 /* Move over ktc_GetToken, here's something leaner. */
800 for (i = 0; i < 100; i++) { /* just in case */
801 parms.in = (char *) &i;
802 parms.in_size = sizeof(i);
803 parms.out = buf;
804 parms.out_size = sizeof(buf);
805 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
806 break;
807 p = buf;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808
Damien Miller95def091999-11-25 00:26:21 +1100809 /* Get secret token. */
810 memcpy(&creds.ticket_st.length, p, sizeof(unsigned int));
811 if (creds.ticket_st.length > MAX_KTXT_LEN)
812 break;
813 p += sizeof(unsigned int);
814 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
815 p += creds.ticket_st.length;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000816
Damien Miller95def091999-11-25 00:26:21 +1100817 /* Get clear token. */
818 memcpy(&len, p, sizeof(len));
819 if (len != sizeof(struct ClearToken))
820 break;
821 p += sizeof(len);
822 memcpy(&ct, p, len);
823 p += len;
824 p += sizeof(len); /* primary flag */
825 server_cell = p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826
Damien Miller95def091999-11-25 00:26:21 +1100827 /* Flesh out our credentials. */
828 strlcpy(creds.service, "afs", sizeof creds.service);
829 creds.instance[0] = '\0';
830 strlcpy(creds.realm, server_cell, REALM_SZ);
831 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
832 creds.issue_date = ct.BeginTimestamp;
833 creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp);
834 creds.kvno = ct.AuthHandle;
835 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
836 creds.pinst[0] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000837
Damien Miller95def091999-11-25 00:26:21 +1100838 /* Encode token, ship it off. */
839 if (!creds_to_radix(&creds, buffer))
840 break;
841 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
842 packet_put_string((char *) buffer, strlen(buffer));
843 packet_send();
844 packet_write_wait();
845
846 /* Roger, Roger. Clearance, Clarence. What's your vector,
847 Victor? */
848 type = packet_read(&plen);
849
850 if (type == SSH_SMSG_FAILURE)
851 debug("AFS token for cell %s rejected.", server_cell);
852 else if (type != SSH_SMSG_SUCCESS)
853 packet_disconnect("Protocol error on AFS token response: %d", type);
854 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855}
Damien Miller95def091999-11-25 00:26:21 +1100856
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000857#endif /* AFS */
858
Damien Miller95def091999-11-25 00:26:21 +1100859/*
Damien Milleraae6c611999-12-06 11:47:28 +1100860 * Tries to authenticate with any string-based challenge/response system.
861 * Note that the client code is not tied to s/key or TIS.
862 */
863int
864try_skey_authentication()
865{
866 int type, i, payload_len;
867 char *challenge, *response;
868
869 debug("Doing skey authentication.");
870
871 /* request a challenge */
872 packet_start(SSH_CMSG_AUTH_TIS);
873 packet_send();
874 packet_write_wait();
875
876 type = packet_read(&payload_len);
877 if (type != SSH_SMSG_FAILURE &&
878 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
879 packet_disconnect("Protocol error: got %d in response "
880 "to skey-auth", type);
881 }
882 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
883 debug("No challenge for skey authentication.");
884 return 0;
885 }
886 challenge = packet_get_string(&payload_len);
887 if (options.cipher == SSH_CIPHER_NONE)
888 log("WARNING: Encryption is disabled! "
889 "Reponse will be transmitted in clear text.");
890 fprintf(stderr, "%s\n", challenge);
891 fflush(stderr);
892 for (i = 0; i < options.number_of_password_prompts; i++) {
893 if (i != 0)
894 error("Permission denied, please try again.");
895 response = read_passphrase("Response: ", 0);
896 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
897 packet_put_string(response, strlen(response));
898 memset(response, 0, strlen(response));
899 xfree(response);
900 packet_send();
901 packet_write_wait();
902 type = packet_read(&payload_len);
903 if (type == SSH_SMSG_SUCCESS)
904 return 1;
905 if (type != SSH_SMSG_FAILURE)
906 packet_disconnect("Protocol error: got %d in response "
907 "to skey-auth-reponse", type);
908 }
909 /* failure */
910 return 0;
911}
912
913/*
914 * Tries to authenticate with plain passwd authentication.
915 */
916int
917try_password_authentication(char *prompt)
918{
919 int type, i, payload_len;
920 char *password;
921
922 debug("Doing password authentication.");
923 if (options.cipher == SSH_CIPHER_NONE)
924 log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
925 for (i = 0; i < options.number_of_password_prompts; i++) {
926 if (i != 0)
927 error("Permission denied, please try again.");
928 password = read_passphrase(prompt, 0);
929 packet_start(SSH_CMSG_AUTH_PASSWORD);
930 packet_put_string(password, strlen(password));
931 memset(password, 0, strlen(password));
932 xfree(password);
933 packet_send();
934 packet_write_wait();
935
936 type = packet_read(&payload_len);
937 if (type == SSH_SMSG_SUCCESS)
938 return 1;
939 if (type != SSH_SMSG_FAILURE)
940 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
941 }
942 /* failure */
943 return 0;
944}
945
946/*
Damien Miller95def091999-11-25 00:26:21 +1100947 * Waits for the server identification string, and sends our own
948 * identification string.
949 */
950void
951ssh_exchange_identification()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952{
Damien Miller95def091999-11-25 00:26:21 +1100953 char buf[256], remote_version[256]; /* must be same size! */
954 int remote_major, remote_minor, i;
955 int connection_in = packet_get_connection_in();
956 int connection_out = packet_get_connection_out();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
Damien Miller95def091999-11-25 00:26:21 +1100958 /* Read other side\'s version identification. */
959 for (i = 0; i < sizeof(buf) - 1; i++) {
960 if (read(connection_in, &buf[i], 1) != 1)
961 fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
962 if (buf[i] == '\r') {
963 buf[i] = '\n';
964 buf[i + 1] = 0;
965 break;
966 }
967 if (buf[i] == '\n') {
968 buf[i + 1] = 0;
969 break;
970 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971 }
Damien Miller95def091999-11-25 00:26:21 +1100972 buf[sizeof(buf) - 1] = 0;
973
Damien Miller5428f641999-11-25 11:54:57 +1100974 /*
975 * Check that the versions match. In future this might accept
976 * several versions and set appropriate flags to handle them.
977 */
Damien Miller95def091999-11-25 00:26:21 +1100978 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", &remote_major, &remote_minor,
979 remote_version) != 3)
980 fatal("Bad remote protocol version identification: '%.100s'", buf);
981 debug("Remote protocol version %d.%d, remote software version %.100s",
982 remote_major, remote_minor, remote_version);
983
984 /* Check if the remote protocol version is too old. */
985 if (remote_major == 1 && remote_minor < 3)
986 fatal("Remote machine has too old SSH software version.");
987
988 /* We speak 1.3, too. */
989 if (remote_major == 1 && remote_minor == 3) {
990 enable_compat13();
Damien Miller62ab38a2000-01-03 23:41:05 +1100991 if (options.forward_agent && strcmp(remote_version, "OpenSSH-1.1") != 0) {
Damien Miller95def091999-11-25 00:26:21 +1100992 log("Agent forwarding disabled, remote version '%s' is not compatible.",
993 remote_version);
994 options.forward_agent = 0;
995 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997#if 0
Damien Miller5428f641999-11-25 11:54:57 +1100998 /*
999 * Removed for now, to permit compatibility with latter versions. The
1000 * server will reject our version and disconnect if it doesn't
1001 * support it.
1002 */
Damien Miller95def091999-11-25 00:26:21 +11001003 if (remote_major != PROTOCOL_MAJOR)
1004 fatal("Protocol major versions differ: %d vs. %d",
1005 PROTOCOL_MAJOR, remote_major);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006#endif
1007
Damien Miller95def091999-11-25 00:26:21 +11001008 /* Send our own protocol version identification. */
1009 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
Damien Miller037a0dc1999-12-07 15:38:31 +11001010 PROTOCOL_MAJOR, PROTOCOL_MINOR, SSH_VERSION);
1011 if (atomicio(write, connection_out, buf, strlen(buf)) != strlen(buf))
Damien Miller95def091999-11-25 00:26:21 +11001012 fatal("write: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013}
1014
1015int ssh_cipher_default = SSH_CIPHER_3DES;
1016
Damien Miller95def091999-11-25 00:26:21 +11001017int
1018read_yes_or_no(const char *prompt, int defval)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001019{
Damien Miller95def091999-11-25 00:26:21 +11001020 char buf[1024];
1021 FILE *f;
1022 int retval = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023
Damien Miller95def091999-11-25 00:26:21 +11001024 if (isatty(0))
1025 f = stdin;
1026 else
1027 f = fopen("/dev/tty", "rw");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001028
Damien Miller95def091999-11-25 00:26:21 +11001029 if (f == NULL)
1030 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031
Damien Miller95def091999-11-25 00:26:21 +11001032 fflush(stdout);
1033
1034 while (1) {
1035 fprintf(stderr, "%s", prompt);
1036 if (fgets(buf, sizeof(buf), f) == NULL) {
1037 /* Print a newline (the prompt probably didn\'t have one). */
1038 fprintf(stderr, "\n");
1039 strlcpy(buf, "no", sizeof buf);
1040 }
1041 /* Remove newline from response. */
1042 if (strchr(buf, '\n'))
1043 *strchr(buf, '\n') = 0;
1044
1045 if (buf[0] == 0)
1046 retval = defval;
1047 if (strcmp(buf, "yes") == 0)
1048 retval = 1;
1049 if (strcmp(buf, "no") == 0)
1050 retval = 0;
1051
1052 if (retval != -1) {
1053 if (f != stdin)
1054 fclose(f);
1055 return retval;
1056 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001057 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058}
1059
Damien Miller95def091999-11-25 00:26:21 +11001060/*
Damien Millera34a28b1999-12-14 10:47:15 +11001061 * check whether the supplied host key is valid, return only if ok.
Damien Miller95def091999-11-25 00:26:21 +11001062 */
Damien Millera34a28b1999-12-14 10:47:15 +11001063
Damien Miller95def091999-11-25 00:26:21 +11001064void
Damien Miller34132e52000-01-14 15:45:46 +11001065check_host_key(char *host, struct sockaddr *hostaddr, RSA *host_key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001066{
Damien Millera34a28b1999-12-14 10:47:15 +11001067 RSA *file_key;
1068 char *ip = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001069 char hostline[1000], *hostp;
Damien Miller95def091999-11-25 00:26:21 +11001070 HostStatus host_status;
1071 HostStatus ip_status;
Damien Miller34132e52000-01-14 15:45:46 +11001072 int local = 0, host_ip_differ = 0;
1073 int sa_len;
1074 char ntop[NI_MAXHOST];
1075
1076 /*
1077 * Force accepting of the host key for loopback/localhost. The
1078 * problem is that if the home directory is NFS-mounted to multiple
1079 * machines, localhost will refer to a different machine in each of
1080 * them, and the user will get bogus HOST_CHANGED warnings. This
1081 * essentially disables host authentication for localhost; however,
1082 * this is probably not a real problem.
1083 */
1084 switch (hostaddr->sa_family) {
1085 case AF_INET:
1086 local = (ntohl(((struct sockaddr_in *)hostaddr)->sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
1087 sa_len = sizeof(struct sockaddr_in);
1088 break;
1089 case AF_INET6:
1090 local = IN6_IS_ADDR_LOOPBACK(&(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
1091 sa_len = sizeof(struct sockaddr_in6);
1092 break;
1093 default:
1094 local = 0;
1095 sa_len = sizeof(struct sockaddr_storage);
1096 break;
1097 }
1098 if (local) {
1099 debug("Forcing accepting of host key for loopback/localhost.");
1100 return;
1101 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102
Damien Milleraae6c611999-12-06 11:47:28 +11001103 /*
1104 * Turn off check_host_ip for proxy connects, since
1105 * we don't have the remote ip-address
1106 */
1107 if (options.proxy_command != NULL && options.check_host_ip)
1108 options.check_host_ip = 0;
1109
Damien Miller34132e52000-01-14 15:45:46 +11001110 if (options.check_host_ip) {
1111 if (getnameinfo(hostaddr, sa_len, ntop, sizeof(ntop),
1112 NULL, 0, NI_NUMERICHOST) != 0)
1113 fatal("check_host_key: getnameinfo failed");
1114 ip = xstrdup(ntop);
1115 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116
Damien Millera34a28b1999-12-14 10:47:15 +11001117 /*
1118 * Store the host key from the known host file in here so that we can
1119 * compare it with the key for the IP address.
1120 */
Damien Miller95def091999-11-25 00:26:21 +11001121 file_key = RSA_new();
1122 file_key->n = BN_new();
1123 file_key->e = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124
Damien Miller5428f641999-11-25 11:54:57 +11001125 /*
1126 * Check if the host key is present in the user\'s list of known
1127 * hosts or in the systemwide list.
1128 */
Damien Miller95def091999-11-25 00:26:21 +11001129 host_status = check_host_in_hostfile(options.user_hostfile, host,
1130 host_key->e, host_key->n,
1131 file_key->e, file_key->n);
1132 if (host_status == HOST_NEW)
1133 host_status = check_host_in_hostfile(options.system_hostfile, host,
1134 host_key->e, host_key->n,
1135 file_key->e, file_key->n);
Damien Miller5428f641999-11-25 11:54:57 +11001136 /*
Damien Miller5428f641999-11-25 11:54:57 +11001137 * Also perform check for the ip address, skip the check if we are
1138 * localhost or the hostname was an ip address to begin with
1139 */
Damien Miller95def091999-11-25 00:26:21 +11001140 if (options.check_host_ip && !local && strcmp(host, ip)) {
1141 RSA *ip_key = RSA_new();
1142 ip_key->n = BN_new();
1143 ip_key->e = BN_new();
1144 ip_status = check_host_in_hostfile(options.user_hostfile, ip,
1145 host_key->e, host_key->n,
1146 ip_key->e, ip_key->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147
Damien Miller95def091999-11-25 00:26:21 +11001148 if (ip_status == HOST_NEW)
1149 ip_status = check_host_in_hostfile(options.system_hostfile, ip,
1150 host_key->e, host_key->n,
1151 ip_key->e, ip_key->n);
1152 if (host_status == HOST_CHANGED &&
1153 (ip_status != HOST_CHANGED ||
1154 (BN_cmp(ip_key->e, file_key->e) || BN_cmp(ip_key->n, file_key->n))))
1155 host_ip_differ = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156
Damien Miller95def091999-11-25 00:26:21 +11001157 RSA_free(ip_key);
1158 } else
1159 ip_status = host_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160
Damien Miller95def091999-11-25 00:26:21 +11001161 RSA_free(file_key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162
Damien Miller95def091999-11-25 00:26:21 +11001163 switch (host_status) {
1164 case HOST_OK:
1165 /* The host is known and the key matches. */
1166 debug("Host '%.200s' is known and matches the host key.", host);
1167 if (options.check_host_ip) {
1168 if (ip_status == HOST_NEW) {
1169 if (!add_host_to_hostfile(options.user_hostfile, ip,
1170 host_key->e, host_key->n))
1171 log("Failed to add the host key for IP address '%.30s' to the list of known hosts (%.30s).",
1172 ip, options.user_hostfile);
1173 else
1174 log("Warning: Permanently added host key for IP address '%.30s' to the list of known hosts.",
1175 ip);
1176 } else if (ip_status != HOST_OK)
1177 log("Warning: the host key for '%.200s' differs from the key for the IP address '%.30s'",
1178 host, ip);
1179 }
1180 break;
1181 case HOST_NEW:
1182 /* The host is new. */
1183 if (options.strict_host_key_checking == 1) {
1184 /* User has requested strict host key checking. We will not add the host key
1185 automatically. The only alternative left is to abort. */
1186 fatal("No host key is known for %.200s and you have requested strict checking.", host);
1187 } else if (options.strict_host_key_checking == 2) {
1188 /* The default */
1189 char prompt[1024];
1190 char *fp = fingerprint(host_key->e, host_key->n);
1191 snprintf(prompt, sizeof(prompt),
Damien Miller037a0dc1999-12-07 15:38:31 +11001192 "The authenticity of host '%.200s' can't be established.\n"
1193 "Key fingerprint is %d %s.\n"
1194 "Are you sure you want to continue connecting (yes/no)? ",
1195 host, BN_num_bits(host_key->n), fp);
Damien Miller95def091999-11-25 00:26:21 +11001196 if (!read_yes_or_no(prompt, -1))
1197 fatal("Aborted by user!\n");
1198 }
1199 if (options.check_host_ip && ip_status == HOST_NEW && strcmp(host, ip)) {
1200 snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
1201 hostp = hostline;
1202 } else
1203 hostp = host;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204
Damien Miller95def091999-11-25 00:26:21 +11001205 /* If not in strict mode, add the key automatically to the local known_hosts file. */
1206 if (!add_host_to_hostfile(options.user_hostfile, hostp,
1207 host_key->e, host_key->n))
1208 log("Failed to add the host to the list of known hosts (%.500s).",
1209 options.user_hostfile);
1210 else
1211 log("Warning: Permanently added '%.200s' to the list of known hosts.",
1212 hostp);
1213 break;
1214 case HOST_CHANGED:
1215 if (options.check_host_ip && host_ip_differ) {
1216 char *msg;
1217 if (ip_status == HOST_NEW)
1218 msg = "is unknown";
1219 else if (ip_status == HOST_OK)
1220 msg = "is unchanged";
1221 else
1222 msg = "has a different value";
1223 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1224 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
1225 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1226 error("The host key for %s has changed,", host);
1227 error("and the key for the according IP address %s", ip);
1228 error("%s. This could either mean that", msg);
1229 error("DNS SPOOFING is happening or the IP address for the host");
1230 error("and its host key have changed at the same time");
1231 }
1232 /* The host key has changed. */
1233 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Damien Millerf039bad1999-12-21 20:57:20 +11001234 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
Damien Miller95def091999-11-25 00:26:21 +11001235 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1236 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1237 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1238 error("It is also possible that the host key has just been changed.");
1239 error("Please contact your system administrator.");
1240 error("Add correct host key in %.100s to get rid of this message.",
1241 options.user_hostfile);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242
Damien Miller5428f641999-11-25 11:54:57 +11001243 /*
1244 * If strict host key checking is in use, the user will have
1245 * to edit the key manually and we can only abort.
1246 */
Damien Miller95def091999-11-25 00:26:21 +11001247 if (options.strict_host_key_checking)
1248 fatal("Host key for %.200s has changed and you have requested strict checking.", host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249
Damien Miller5428f641999-11-25 11:54:57 +11001250 /*
1251 * If strict host key checking has not been requested, allow
1252 * the connection but without password authentication or
1253 * agent forwarding.
1254 */
Damien Miller95def091999-11-25 00:26:21 +11001255 if (options.password_authentication) {
1256 error("Password authentication is disabled to avoid trojan horses.");
1257 options.password_authentication = 0;
1258 }
1259 if (options.forward_agent) {
1260 error("Agent forwarding is disabled to avoid trojan horses.");
1261 options.forward_agent = 0;
1262 }
Damien Miller5428f641999-11-25 11:54:57 +11001263 /*
1264 * XXX Should permit the user to change to use the new id.
1265 * This could be done by converting the host key to an
1266 * identifying sentence, tell that the host identifies itself
1267 * by that sentence, and ask the user if he/she whishes to
1268 * accept the authentication.
1269 */
Damien Miller95def091999-11-25 00:26:21 +11001270 break;
1271 }
Damien Miller95def091999-11-25 00:26:21 +11001272 if (options.check_host_ip)
1273 xfree(ip);
Damien Millera34a28b1999-12-14 10:47:15 +11001274}
1275
1276/*
1277 * Starts a dialog with the server, and authenticates the current user on the
1278 * server. This does not need any extra privileges. The basic connection
1279 * to the server must already have been established before this is called.
1280 * User is the remote user; if it is NULL, the current local user name will
1281 * be used. Anonymous indicates that no rhosts authentication will be used.
1282 * If login fails, this function prints an error and never returns.
1283 * This function does not require super-user privileges.
1284 */
1285void
1286ssh_login(int host_key_valid,
1287 RSA *own_host_key,
1288 const char *orighost,
Damien Miller34132e52000-01-14 15:45:46 +11001289 struct sockaddr *hostaddr,
Damien Millera34a28b1999-12-14 10:47:15 +11001290 uid_t original_real_uid)
1291{
1292 int i, type;
1293 struct passwd *pw;
1294 BIGNUM *key;
1295 RSA *host_key;
1296 RSA *public_key;
1297 int bits, rbits;
1298 unsigned char session_key[SSH_SESSION_KEY_LENGTH];
1299 const char *server_user, *local_user;
1300 char *host, *cp;
1301 unsigned char check_bytes[8];
1302 unsigned int supported_ciphers, supported_authentications;
1303 unsigned int server_flags, client_flags;
1304 int payload_len, clen, sum_len = 0;
1305 u_int32_t rand = 0;
1306
1307 /* Convert the user-supplied hostname into all lowercase. */
1308 host = xstrdup(orighost);
1309 for (cp = host; *cp; cp++)
1310 if (isupper(*cp))
1311 *cp = tolower(*cp);
1312
1313 /* Exchange protocol version identification strings with the server. */
1314 ssh_exchange_identification();
1315
1316 /* Put the connection into non-blocking mode. */
1317 packet_set_nonblocking();
1318
1319 /* Get local user name. Use it as server user if no user name was given. */
1320 pw = getpwuid(original_real_uid);
1321 if (!pw)
1322 fatal("User id %d not found from user database.", original_real_uid);
1323 local_user = xstrdup(pw->pw_name);
1324 server_user = options.user ? options.user : local_user;
1325
1326 debug("Waiting for server public key.");
1327
1328 /* Wait for a public key packet from the server. */
1329 packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
1330
1331 /* Get check bytes from the packet. */
1332 for (i = 0; i < 8; i++)
1333 check_bytes[i] = packet_get_char();
1334
1335 /* Get the public key. */
1336 public_key = RSA_new();
1337 bits = packet_get_int();/* bits */
1338 public_key->e = BN_new();
1339 packet_get_bignum(public_key->e, &clen);
1340 sum_len += clen;
1341 public_key->n = BN_new();
1342 packet_get_bignum(public_key->n, &clen);
1343 sum_len += clen;
1344
1345 rbits = BN_num_bits(public_key->n);
1346 if (bits != rbits) {
1347 log("Warning: Server lies about size of server public key: "
1348 "actual size is %d bits vs. announced %d.", rbits, bits);
1349 log("Warning: This may be due to an old implementation of ssh.");
1350 }
1351 /* Get the host key. */
1352 host_key = RSA_new();
1353 bits = packet_get_int();/* bits */
1354 host_key->e = BN_new();
1355 packet_get_bignum(host_key->e, &clen);
1356 sum_len += clen;
1357 host_key->n = BN_new();
1358 packet_get_bignum(host_key->n, &clen);
1359 sum_len += clen;
1360
1361 rbits = BN_num_bits(host_key->n);
1362 if (bits != rbits) {
1363 log("Warning: Server lies about size of server host key: "
1364 "actual size is %d bits vs. announced %d.", rbits, bits);
1365 log("Warning: This may be due to an old implementation of ssh.");
1366 }
1367
1368 /* Get protocol flags. */
1369 server_flags = packet_get_int();
1370 packet_set_protocol_flags(server_flags);
1371
1372 supported_ciphers = packet_get_int();
1373 supported_authentications = packet_get_int();
1374
1375 debug("Received server public key (%d bits) and host key (%d bits).",
1376 BN_num_bits(public_key->n), BN_num_bits(host_key->n));
1377
1378 packet_integrity_check(payload_len,
1379 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
1380 SSH_SMSG_PUBLIC_KEY);
1381
1382 check_host_key(host, hostaddr, host_key);
1383
1384 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
1385
1386 compute_session_id(session_id, check_bytes, host_key->n, public_key->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387
Damien Miller95def091999-11-25 00:26:21 +11001388 /* Generate a session key. */
1389 arc4random_stir();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390
Damien Miller5428f641999-11-25 11:54:57 +11001391 /*
1392 * Generate an encryption key for the session. The key is a 256 bit
1393 * random number, interpreted as a 32-byte key, with the least
1394 * significant 8 bits being the first byte of the key.
1395 */
Damien Miller95def091999-11-25 00:26:21 +11001396 for (i = 0; i < 32; i++) {
1397 if (i % 4 == 0)
1398 rand = arc4random();
1399 session_key[i] = rand & 0xff;
1400 rand >>= 8;
1401 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001402
Damien Miller5428f641999-11-25 11:54:57 +11001403 /*
1404 * According to the protocol spec, the first byte of the session key
1405 * is the highest byte of the integer. The session key is xored with
1406 * the first 16 bytes of the session id.
1407 */
Damien Miller95def091999-11-25 00:26:21 +11001408 key = BN_new();
1409 BN_set_word(key, 0);
1410 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
1411 BN_lshift(key, key, 8);
1412 if (i < 16)
1413 BN_add_word(key, session_key[i] ^ session_id[i]);
1414 else
1415 BN_add_word(key, session_key[i]);
1416 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417
Damien Miller5428f641999-11-25 11:54:57 +11001418 /*
1419 * Encrypt the integer using the public key and host key of the
1420 * server (key with smaller modulus first).
1421 */
Damien Miller95def091999-11-25 00:26:21 +11001422 if (BN_cmp(public_key->n, host_key->n) < 0) {
1423 /* Public key has smaller modulus. */
1424 if (BN_num_bits(host_key->n) <
1425 BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) {
1426 fatal("respond_to_rsa_challenge: host_key %d < public_key %d + "
1427 "SSH_KEY_BITS_RESERVED %d",
1428 BN_num_bits(host_key->n),
1429 BN_num_bits(public_key->n),
1430 SSH_KEY_BITS_RESERVED);
1431 }
1432 rsa_public_encrypt(key, key, public_key);
1433 rsa_public_encrypt(key, key, host_key);
1434 } else {
1435 /* Host key has smaller modulus (or they are equal). */
1436 if (BN_num_bits(public_key->n) <
1437 BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) {
1438 fatal("respond_to_rsa_challenge: public_key %d < host_key %d + "
1439 "SSH_KEY_BITS_RESERVED %d",
1440 BN_num_bits(public_key->n),
1441 BN_num_bits(host_key->n),
1442 SSH_KEY_BITS_RESERVED);
1443 }
1444 rsa_public_encrypt(key, key, host_key);
1445 rsa_public_encrypt(key, key, public_key);
1446 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447
Damien Miller95def091999-11-25 00:26:21 +11001448 if (options.cipher == SSH_CIPHER_NOT_SET) {
1449 if (cipher_mask() & supported_ciphers & (1 << ssh_cipher_default))
1450 options.cipher = ssh_cipher_default;
1451 else {
1452 debug("Cipher %s not supported, using %.100s instead.",
1453 cipher_name(ssh_cipher_default),
1454 cipher_name(SSH_FALLBACK_CIPHER));
1455 options.cipher = SSH_FALLBACK_CIPHER;
1456 }
1457 }
1458 /* Check that the selected cipher is supported. */
1459 if (!(supported_ciphers & (1 << options.cipher)))
1460 fatal("Selected cipher type %.100s not supported by server.",
1461 cipher_name(options.cipher));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462
Damien Miller95def091999-11-25 00:26:21 +11001463 debug("Encryption type: %.100s", cipher_name(options.cipher));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001464
Damien Miller95def091999-11-25 00:26:21 +11001465 /* Send the encrypted session key to the server. */
1466 packet_start(SSH_CMSG_SESSION_KEY);
1467 packet_put_char(options.cipher);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468
Damien Miller95def091999-11-25 00:26:21 +11001469 /* Send the check bytes back to the server. */
1470 for (i = 0; i < 8; i++)
1471 packet_put_char(check_bytes[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472
Damien Miller95def091999-11-25 00:26:21 +11001473 /* Send the encrypted encryption key. */
1474 packet_put_bignum(key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001475
Damien Miller95def091999-11-25 00:26:21 +11001476 /* Send protocol flags. */
Damien Millera34a28b1999-12-14 10:47:15 +11001477 packet_put_int(client_flags);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001478
Damien Miller95def091999-11-25 00:26:21 +11001479 /* Send the packet now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480 packet_send();
1481 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001482
Damien Miller95def091999-11-25 00:26:21 +11001483 /* Destroy the session key integer and the public keys since we no longer need them. */
1484 BN_clear_free(key);
1485 RSA_free(public_key);
1486 RSA_free(host_key);
1487
1488 debug("Sent encrypted session key.");
1489
1490 /* Set the encryption key. */
1491 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
1492
1493 /* We will no longer need the session key here. Destroy any extra copies. */
1494 memset(session_key, 0, sizeof(session_key));
1495
Damien Miller5428f641999-11-25 11:54:57 +11001496 /*
1497 * Expect a success message from the server. Note that this message
1498 * will be received in encrypted form.
1499 */
Damien Miller95def091999-11-25 00:26:21 +11001500 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1501
1502 debug("Received encrypted confirmation.");
1503
1504 /* Send the name of the user to log in as on the server. */
1505 packet_start(SSH_CMSG_USER);
1506 packet_put_string(server_user, strlen(server_user));
1507 packet_send();
1508 packet_write_wait();
1509
Damien Miller5428f641999-11-25 11:54:57 +11001510 /*
1511 * The server should respond with success if no authentication is
1512 * needed (the user has no password). Otherwise the server responds
1513 * with failure.
1514 */
Damien Miller95def091999-11-25 00:26:21 +11001515 type = packet_read(&payload_len);
1516
1517 /* check whether the connection was accepted without authentication. */
1518 if (type == SSH_SMSG_SUCCESS)
1519 return;
1520 if (type != SSH_SMSG_FAILURE)
1521 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER",
1522 type);
1523
1524#ifdef AFS
1525 /* Try Kerberos tgt passing if the server supports it. */
1526 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1527 options.kerberos_tgt_passing) {
1528 if (options.cipher == SSH_CIPHER_NONE)
1529 log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1530 (void) send_kerberos_tgt();
1531 }
1532 /* Try AFS token passing if the server supports it. */
1533 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
1534 options.afs_token_passing && k_hasafs()) {
1535 if (options.cipher == SSH_CIPHER_NONE)
1536 log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
1537 send_afs_tokens();
1538 }
1539#endif /* AFS */
1540
1541#ifdef KRB4
1542 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1543 options.kerberos_authentication) {
1544 debug("Trying Kerberos authentication.");
1545 if (try_kerberos_authentication()) {
1546 /* The server should respond with success or failure. */
1547 type = packet_read(&payload_len);
1548 if (type == SSH_SMSG_SUCCESS)
1549 return;
1550 if (type != SSH_SMSG_FAILURE)
1551 packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
1552 }
1553 }
1554#endif /* KRB4 */
1555
Damien Miller5428f641999-11-25 11:54:57 +11001556 /*
1557 * Use rhosts authentication if running in privileged socket and we
1558 * do not wish to remain anonymous.
1559 */
Damien Miller95def091999-11-25 00:26:21 +11001560 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1561 options.rhosts_authentication) {
1562 debug("Trying rhosts authentication.");
1563 packet_start(SSH_CMSG_AUTH_RHOSTS);
1564 packet_put_string(local_user, strlen(local_user));
1565 packet_send();
1566 packet_write_wait();
1567
1568 /* The server should respond with success or failure. */
1569 type = packet_read(&payload_len);
1570 if (type == SSH_SMSG_SUCCESS)
1571 return;
1572 if (type != SSH_SMSG_FAILURE)
1573 packet_disconnect("Protocol error: got %d in response to rhosts auth",
1574 type);
1575 }
Damien Miller5428f641999-11-25 11:54:57 +11001576 /*
1577 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
1578 * authentication.
1579 */
Damien Miller95def091999-11-25 00:26:21 +11001580 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1581 options.rhosts_rsa_authentication && host_key_valid) {
1582 if (try_rhosts_rsa_authentication(local_user, own_host_key))
1583 return;
1584 }
1585 /* Try RSA authentication if the server supports it. */
1586 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1587 options.rsa_authentication) {
Damien Miller5428f641999-11-25 11:54:57 +11001588 /*
1589 * Try RSA authentication using the authentication agent. The
1590 * agent is tried first because no passphrase is needed for
1591 * it, whereas identity files may require passphrases.
1592 */
Damien Miller95def091999-11-25 00:26:21 +11001593 if (try_agent_authentication())
1594 return;
1595
1596 /* Try RSA authentication for each identity. */
1597 for (i = 0; i < options.num_identity_files; i++)
Damien Milleraae6c611999-12-06 11:47:28 +11001598 if (try_rsa_authentication(options.identity_files[i]))
Damien Miller95def091999-11-25 00:26:21 +11001599 return;
1600 }
1601 /* Try skey authentication if the server supports it. */
1602 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1603 options.skey_authentication && !options.batch_mode) {
Damien Milleraae6c611999-12-06 11:47:28 +11001604 if (try_skey_authentication())
1605 return;
Damien Miller95def091999-11-25 00:26:21 +11001606 }
1607 /* Try password authentication if the server supports it. */
1608 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1609 options.password_authentication && !options.batch_mode) {
1610 char prompt[80];
Damien Miller037a0dc1999-12-07 15:38:31 +11001611
Damien Milleraae6c611999-12-06 11:47:28 +11001612 snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
Damien Miller037a0dc1999-12-07 15:38:31 +11001613 server_user, host);
Damien Milleraae6c611999-12-06 11:47:28 +11001614 if (try_password_authentication(prompt))
1615 return;
Damien Miller95def091999-11-25 00:26:21 +11001616 }
1617 /* All authentication methods have failed. Exit with an error message. */
1618 fatal("Permission denied.");
1619 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001620}