Annotation of src/usr.bin/ssh/sshconnect.c, Revision 1.37
1.1 deraadt 1: /*
2:
3: sshconnect.c
4:
5: Author: Tatu Ylonen <ylo@cs.hut.fi>
6:
7: Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8: All rights reserved
9:
10: Created: Sat Mar 18 22:15:47 1995 ylo
11:
12: Code to connect to a remote host, and to perform the client side of the
13: login (authentication) dialog.
14:
15: */
16:
17: #include "includes.h"
1.37 ! markus 18: RCSID("$Id: sshconnect.c,v 1.36 1999/11/19 19:58:18 markus Exp $");
1.1 deraadt 19:
1.3 provos 20: #include <ssl/bn.h>
1.1 deraadt 21: #include "xmalloc.h"
22: #include "rsa.h"
23: #include "ssh.h"
24: #include "packet.h"
25: #include "authfd.h"
26: #include "cipher.h"
27: #include "mpaux.h"
28: #include "uidswap.h"
1.21 markus 29: #include "compat.h"
1.27 markus 30: #include "readconf.h"
1.34 markus 31: #include "fingerprint.h"
1.1 deraadt 32:
1.24 deraadt 33: #include <ssl/md5.h>
1.10 deraadt 34:
1.1 deraadt 35: /* Session id for the current session. */
36: unsigned char session_id[16];
37:
38: /* Connect to the given ssh server using a proxy command. */
39:
1.3 provos 40: int
41: ssh_proxy_connect(const char *host, int port, uid_t original_real_uid,
42: const char *proxy_command)
1.1 deraadt 43: {
44: Buffer command;
45: const char *cp;
46: char *command_string;
47: int pin[2], pout[2];
48: int pid;
49: char portstring[100];
50:
51: /* Convert the port number into a string. */
1.6 deraadt 52: snprintf(portstring, sizeof portstring, "%d", port);
1.1 deraadt 53:
54: /* Build the final command string in the buffer by making the appropriate
55: substitutions to the given proxy command. */
56: buffer_init(&command);
57: for (cp = proxy_command; *cp; cp++)
58: {
59: if (cp[0] == '%' && cp[1] == '%')
60: {
61: buffer_append(&command, "%", 1);
62: cp++;
63: continue;
64: }
65: if (cp[0] == '%' && cp[1] == 'h')
66: {
67: buffer_append(&command, host, strlen(host));
68: cp++;
69: continue;
70: }
71: if (cp[0] == '%' && cp[1] == 'p')
72: {
73: buffer_append(&command, portstring, strlen(portstring));
74: cp++;
75: continue;
76: }
77: buffer_append(&command, cp, 1);
78: }
79: buffer_append(&command, "\0", 1);
80:
81: /* Get the final command string. */
82: command_string = buffer_ptr(&command);
83:
84: /* Create pipes for communicating with the proxy. */
85: if (pipe(pin) < 0 || pipe(pout) < 0)
86: fatal("Could not create pipes to communicate with the proxy: %.100s",
87: strerror(errno));
88:
89: debug("Executing proxy command: %.500s", command_string);
90:
91: /* Fork and execute the proxy command. */
92: if ((pid = fork()) == 0)
93: {
94: char *argv[10];
95:
96: /* Child. Permanently give up superuser privileges. */
97: permanently_set_uid(original_real_uid);
98:
99: /* Redirect stdin and stdout. */
100: close(pin[1]);
101: if (pin[0] != 0)
102: {
103: if (dup2(pin[0], 0) < 0)
104: perror("dup2 stdin");
105: close(pin[0]);
106: }
107: close(pout[0]);
108: if (dup2(pout[1], 1) < 0)
109: perror("dup2 stdout");
110: close(pout[1]); /* Cannot be 1 because pin allocated two descriptors. */
111:
112: /* Stderr is left as it is so that error messages get printed on
113: the user's terminal. */
114: argv[0] = "/bin/sh";
115: argv[1] = "-c";
116: argv[2] = command_string;
117: argv[3] = NULL;
118:
119: /* Execute the proxy command. Note that we gave up any extra
120: privileges above. */
121: execv("/bin/sh", argv);
122: perror("/bin/sh");
123: exit(1);
124: }
125: /* Parent. */
126: if (pid < 0)
127: fatal("fork failed: %.100s", strerror(errno));
128:
129: /* Close child side of the descriptors. */
130: close(pin[0]);
131: close(pout[1]);
132:
133: /* Free the command name. */
134: buffer_free(&command);
135:
136: /* Set the connection file descriptors. */
1.3 provos 137: packet_set_connection(pout[0], pin[1]);
1.1 deraadt 138:
139: return 1;
140: }
141:
142: /* Creates a (possibly privileged) socket for use as the ssh connection. */
143:
144: int ssh_create_socket(uid_t original_real_uid, int privileged)
145: {
146: int sock;
147:
148: /* If we are running as root and want to connect to a privileged port,
149: bind our own socket to a privileged port. */
150: if (privileged)
151: {
1.2 deraadt 152: int p = IPPORT_RESERVED - 1;
153:
154: sock = rresvport(&p);
155: if (sock < 0)
156: fatal("rresvport: %.100s", strerror(errno));
1.1 deraadt 157: debug("Allocated local port %d.", p);
158: }
159: else
160: {
161: /* Just create an ordinary socket on arbitrary port. We use the
162: user's uid to create the socket. */
163: temporarily_use_uid(original_real_uid);
164: sock = socket(AF_INET, SOCK_STREAM, 0);
165: if (sock < 0)
166: fatal("socket: %.100s", strerror(errno));
167: restore_uid();
168: }
169: return sock;
170: }
171:
172: /* Opens a TCP/IP connection to the remote server on the given host. If
173: port is 0, the default port will be used. If anonymous is zero,
174: a privileged port will be allocated to make the connection.
175: This requires super-user privileges if anonymous is false.
176: Connection_attempts specifies the maximum number of tries (one per
177: second). If proxy_command is non-NULL, it specifies the command (with %h
178: and %p substituted for host and port, respectively) to use to contact
179: the daemon. */
180:
1.12 provos 181: int ssh_connect(const char *host, struct sockaddr_in *hostaddr,
182: int port, int connection_attempts,
1.1 deraadt 183: int anonymous, uid_t original_real_uid,
1.3 provos 184: const char *proxy_command)
1.1 deraadt 185: {
186: int sock = -1, attempt, i;
187: int on = 1;
188: struct servent *sp;
189: struct hostent *hp;
190: struct linger linger;
191:
192: debug("ssh_connect: getuid %d geteuid %d anon %d",
193: (int)getuid(), (int)geteuid(), anonymous);
194:
195: /* Get default port if port has not been set. */
196: if (port == 0)
197: {
198: sp = getservbyname(SSH_SERVICE_NAME, "tcp");
199: if (sp)
200: port = ntohs(sp->s_port);
201: else
202: port = SSH_DEFAULT_PORT;
203: }
204:
205: /* If a proxy command is given, connect using it. */
206: if (proxy_command != NULL)
1.3 provos 207: return ssh_proxy_connect(host, port, original_real_uid, proxy_command);
1.1 deraadt 208:
209: /* No proxy command. */
210:
211: /* No host lookup made yet. */
212: hp = NULL;
213:
214: /* Try to connect several times. On some machines, the first time will
215: sometimes fail. In general socket code appears to behave quite
216: magically on many machines. */
217: for (attempt = 0; attempt < connection_attempts; attempt++)
218: {
219: if (attempt > 0)
220: debug("Trying again...");
221:
222: /* Try to parse the host name as a numeric inet address. */
1.12 provos 223: memset(hostaddr, 0, sizeof(hostaddr));
224: hostaddr->sin_family = AF_INET;
225: hostaddr->sin_port = htons(port);
226: hostaddr->sin_addr.s_addr = inet_addr(host);
227: if ((hostaddr->sin_addr.s_addr & 0xffffffff) != 0xffffffff)
1.1 deraadt 228: {
229: /* Valid numeric IP address */
230: debug("Connecting to %.100s port %d.",
1.12 provos 231: inet_ntoa(hostaddr->sin_addr), port);
1.1 deraadt 232:
233: /* Create a socket. */
234: sock = ssh_create_socket(original_real_uid,
235: !anonymous && geteuid() == 0 &&
1.9 deraadt 236: port < IPPORT_RESERVED);
1.1 deraadt 237:
238: /* Connect to the host. We use the user's uid in the hope that
239: it will help with the problems of tcp_wrappers showing the
240: remote uid as root. */
241: temporarily_use_uid(original_real_uid);
1.12 provos 242: if (connect(sock, (struct sockaddr *)hostaddr, sizeof(*hostaddr))
1.1 deraadt 243: >= 0)
244: {
245: /* Successful connect. */
246: restore_uid();
247: break;
248: }
249: debug("connect: %.100s", strerror(errno));
250: restore_uid();
251:
252: /* Destroy the failed socket. */
1.11 deraadt 253: shutdown(sock, SHUT_RDWR);
1.1 deraadt 254: close(sock);
255: }
256: else
257: {
258: /* Not a valid numeric inet address. */
259: /* Map host name to an address. */
260: if (!hp)
261: hp = gethostbyname(host);
262: if (!hp)
263: fatal("Bad host name: %.100s", host);
264: if (!hp->h_addr_list[0])
265: fatal("Host does not have an IP address: %.100s", host);
266:
267: /* Loop through addresses for this host, and try each one in
268: sequence until the connection succeeds. */
269: for (i = 0; hp->h_addr_list[i]; i++)
270: {
271: /* Set the address to connect to. */
1.12 provos 272: hostaddr->sin_family = hp->h_addrtype;
273: memcpy(&hostaddr->sin_addr, hp->h_addr_list[i],
274: sizeof(hostaddr->sin_addr));
1.1 deraadt 275:
276: debug("Connecting to %.200s [%.100s] port %d.",
1.12 provos 277: host, inet_ntoa(hostaddr->sin_addr), port);
1.1 deraadt 278:
279: /* Create a socket for connecting. */
280: sock = ssh_create_socket(original_real_uid,
281: !anonymous && geteuid() == 0 &&
1.9 deraadt 282: port < IPPORT_RESERVED);
1.1 deraadt 283:
284: /* Connect to the host. We use the user's uid in the hope that
285: it will help with tcp_wrappers showing the remote uid as
286: root. */
287: temporarily_use_uid(original_real_uid);
1.12 provos 288: if (connect(sock, (struct sockaddr *)hostaddr,
289: sizeof(*hostaddr)) >= 0)
1.1 deraadt 290: {
291: /* Successful connection. */
292: restore_uid();
293: break;
294: }
295: debug("connect: %.100s", strerror(errno));
296: restore_uid();
297:
298: /* Close the failed socket; there appear to be some problems
299: when reusing a socket for which connect() has already
300: returned an error. */
1.11 deraadt 301: shutdown(sock, SHUT_RDWR);
1.1 deraadt 302: close(sock);
303: }
304: if (hp->h_addr_list[i])
305: break; /* Successful connection. */
306: }
307:
308: /* Sleep a moment before retrying. */
309: sleep(1);
310: }
311: /* Return failure if we didn't get a successful connection. */
312: if (attempt >= connection_attempts)
313: return 0;
314:
315: debug("Connection established.");
316:
317: /* Set socket options. We would like the socket to disappear as soon as
318: it has been closed for whatever reason. */
319: /* setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
320: 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. */
1.3 provos 326: packet_set_connection(sock, sock);
1.1 deraadt 327:
328: return 1;
329: }
330:
331: /* Checks if the user has an authentication agent, and if so, tries to
332: authenticate using the agent. */
333:
1.3 provos 334: int
335: try_agent_authentication()
1.1 deraadt 336: {
1.29 markus 337: int status, type;
1.1 deraadt 338: char *comment;
339: AuthenticationConnection *auth;
340: unsigned char response[16];
341: unsigned int i;
1.3 provos 342: BIGNUM *e, *n, *challenge;
1.1 deraadt 343:
344: /* Get connection to the agent. */
345: auth = ssh_get_authentication_connection();
346: if (!auth)
347: return 0;
348:
1.3 provos 349: e = BN_new();
350: n = BN_new();
351: challenge = BN_new();
1.1 deraadt 352:
353: /* Loop through identities served by the agent. */
1.29 markus 354: for (status = ssh_get_first_identity(auth, e, n, &comment);
1.1 deraadt 355: status;
1.29 markus 356: status = ssh_get_next_identity(auth, e, n, &comment))
1.1 deraadt 357: {
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);
1.3 provos 366: packet_put_bignum(n);
1.1 deraadt 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 does not
374: support RSA authentication. */
375: if (type == SSH_SMSG_FAILURE)
376: {
377: debug("Server refused our key.");
378: continue;
379: }
380:
381: /* Otherwise it should have sent a challenge. */
382: if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
383: packet_disconnect("Protocol error during RSA authentication: %d",
384: type);
385:
1.3 provos 386: packet_get_bignum(challenge, &clen);
1.1 deraadt 387:
388: packet_integrity_check(plen, clen, type);
389:
390: debug("Received RSA challenge from server.");
391:
392: /* Ask the agent to decrypt the challenge. */
1.29 markus 393: if (!ssh_decrypt_challenge(auth, e, n, challenge,
1.1 deraadt 394: session_id, 1, response))
395: {
396: /* The agent failed to authenticate this identifier although it
397: advertised it supports this. Just return a wrong value. */
398: log("Authentication agent failed to decrypt challenge.");
399: memset(response, 0, sizeof(response));
400: }
401:
402: debug("Sending response to RSA challenge.");
403:
404: /* Send the decrypted challenge back to the server. */
405: packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
406: for (i = 0; i < 16; i++)
407: packet_put_char(response[i]);
408: packet_send();
409: packet_write_wait();
410:
411: /* Wait for response from the server. */
412: type = packet_read(&plen);
413:
414: /* The server returns success if it accepted the authentication. */
415: if (type == SSH_SMSG_SUCCESS)
416: {
417: debug("RSA authentication accepted by server.");
1.3 provos 418: BN_clear_free(e);
419: BN_clear_free(n);
420: BN_clear_free(challenge);
1.1 deraadt 421: return 1;
422: }
423:
424: /* Otherwise it should return failure. */
425: if (type != SSH_SMSG_FAILURE)
426: packet_disconnect("Protocol error waiting RSA auth response: %d",
427: type);
428: }
429:
1.3 provos 430: BN_clear_free(e);
431: BN_clear_free(n);
432: BN_clear_free(challenge);
1.1 deraadt 433:
434: debug("RSA authentication using agent refused.");
435: return 0;
436: }
437:
438: /* Computes the proper response to a RSA challenge, and sends the response to
439: the server. */
440:
1.3 provos 441: void
442: respond_to_rsa_challenge(BIGNUM *challenge, RSA *prv)
1.1 deraadt 443: {
444: unsigned char buf[32], response[16];
1.10 deraadt 445: MD5_CTX md;
1.3 provos 446: int i, len;
1.1 deraadt 447:
448: /* Decrypt the challenge using the private key. */
449: rsa_private_decrypt(challenge, challenge, prv);
450:
451: /* Compute the response. */
452: /* The response is MD5 of decrypted challenge plus session id. */
1.3 provos 453: len = BN_num_bytes(challenge);
1.25 markus 454: if (len <= 0 || len > sizeof(buf))
455: packet_disconnect("respond_to_rsa_challenge: bad challenge length %d",
456: len);
457:
1.3 provos 458: memset(buf, 0, sizeof(buf));
459: BN_bn2bin(challenge, buf + sizeof(buf) - len);
1.24 deraadt 460: MD5_Init(&md);
461: MD5_Update(&md, buf, 32);
462: MD5_Update(&md, session_id, 16);
463: MD5_Final(response, &md);
1.1 deraadt 464:
465: debug("Sending response to host key RSA challenge.");
466:
467: /* Send the response back to the server. */
468: packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
469: for (i = 0; i < 16; i++)
470: packet_put_char(response[i]);
471: packet_send();
472: packet_write_wait();
473:
474: memset(buf, 0, sizeof(buf));
475: memset(response, 0, sizeof(response));
476: memset(&md, 0, sizeof(md));
477: }
478:
479: /* Checks if the user has authentication file, and if so, tries to authenticate
480: the user using it. */
481:
1.3 provos 482: int
1.27 markus 483: try_rsa_authentication(struct passwd *pw, const char *authfile)
1.1 deraadt 484: {
1.27 markus 485: extern Options options;
1.3 provos 486: BIGNUM *challenge;
487: RSA *private_key;
488: RSA *public_key;
1.1 deraadt 489: char *passphrase, *comment;
490: int type, i;
491: int plen, clen;
492:
493: /* Try to load identification for the authentication key. */
1.3 provos 494: public_key = RSA_new();
495: if (!load_public_key(authfile, public_key, &comment)) {
496: RSA_free(public_key);
1.1 deraadt 497: return 0; /* Could not load it. Fail. */
1.3 provos 498: }
1.1 deraadt 499:
500: debug("Trying RSA authentication with key '%.100s'", comment);
501:
502: /* Tell the server that we are willing to authenticate using this key. */
503: packet_start(SSH_CMSG_AUTH_RSA);
1.3 provos 504: packet_put_bignum(public_key->n);
1.1 deraadt 505: packet_send();
506: packet_write_wait();
507:
508: /* We no longer need the public key. */
1.3 provos 509: RSA_free(public_key);
1.1 deraadt 510:
511: /* Wait for server's response. */
512: type = packet_read(&plen);
513:
514: /* The server responds with failure if it doesn\'t like our key or doesn\'t
515: support RSA authentication. */
516: if (type == SSH_SMSG_FAILURE)
517: {
518: debug("Server refused our key.");
519: xfree(comment);
520: return 0; /* Server refuses to authenticate with this key. */
521: }
522:
523: /* Otherwise, the server should respond with a challenge. */
524: if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
525: packet_disconnect("Protocol error during RSA authentication: %d", type);
526:
527: /* Get the challenge from the packet. */
1.3 provos 528: challenge = BN_new();
529: packet_get_bignum(challenge, &clen);
1.1 deraadt 530:
531: packet_integrity_check(plen, clen, type);
532:
533: debug("Received RSA challenge from server.");
534:
1.3 provos 535: private_key = RSA_new();
1.1 deraadt 536: /* Load the private key. Try first with empty passphrase; if it fails,
537: ask for a passphrase. */
1.3 provos 538: if (!load_private_key(authfile, "", private_key, NULL))
1.1 deraadt 539: {
540: char buf[300];
541: /* Request passphrase from the user. We read from /dev/tty to make
542: this work even if stdin has been redirected. If running in
543: batch mode, we just use the empty passphrase, which will fail and
544: return. */
1.6 deraadt 545: snprintf(buf, sizeof buf,
546: "Enter passphrase for RSA key '%.100s': ", comment);
1.27 markus 547: if (!options.batch_mode)
1.1 deraadt 548: passphrase = read_passphrase(buf, 0);
549: else
550: {
551: debug("Will not query passphrase for %.100s in batch mode.",
552: comment);
553: passphrase = xstrdup("");
554: }
555:
556: /* Load the authentication file using the pasphrase. */
1.3 provos 557: if (!load_private_key(authfile, passphrase, private_key, NULL))
1.1 deraadt 558: {
559: memset(passphrase, 0, strlen(passphrase));
560: xfree(passphrase);
561: error("Bad passphrase.");
562:
563: /* Send a dummy response packet to avoid protocol error. */
564: packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
565: for (i = 0; i < 16; i++)
566: packet_put_char(0);
567: packet_send();
568: packet_write_wait();
569:
570: /* Expect the server to reject it... */
571: packet_read_expect(&plen, SSH_SMSG_FAILURE);
572: xfree(comment);
573: return 0;
574: }
575:
576: /* Destroy the passphrase. */
577: memset(passphrase, 0, strlen(passphrase));
578: xfree(passphrase);
579: }
580:
581: /* We no longer need the comment. */
582: xfree(comment);
583:
584: /* Compute and send a response to the challenge. */
1.3 provos 585: respond_to_rsa_challenge(challenge, private_key);
1.1 deraadt 586:
587: /* Destroy the private key. */
1.3 provos 588: RSA_free(private_key);
1.1 deraadt 589:
590: /* We no longer need the challenge. */
1.3 provos 591: BN_clear_free(challenge);
1.1 deraadt 592:
593: /* Wait for response from the server. */
594: type = packet_read(&plen);
595: if (type == SSH_SMSG_SUCCESS)
596: {
597: debug("RSA authentication accepted by server.");
598: return 1;
599: }
600: if (type != SSH_SMSG_FAILURE)
601: packet_disconnect("Protocol error waiting RSA auth response: %d", type);
602: debug("RSA authentication refused.");
603: return 0;
604: }
605:
606: /* Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
607: authentication and RSA host authentication. */
608:
1.3 provos 609: int
610: try_rhosts_rsa_authentication(const char *local_user, RSA *host_key)
1.1 deraadt 611: {
612: int type;
1.3 provos 613: BIGNUM *challenge;
1.1 deraadt 614: int plen, clen;
615:
616: debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
617:
618: /* Tell the server that we are willing to authenticate using this key. */
619: packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
620: packet_put_string(local_user, strlen(local_user));
1.3 provos 621: packet_put_int(BN_num_bits(host_key->n));
622: packet_put_bignum(host_key->e);
623: packet_put_bignum(host_key->n);
1.1 deraadt 624: packet_send();
625: packet_write_wait();
626:
627: /* Wait for server's response. */
628: type = packet_read(&plen);
629:
630: /* The server responds with failure if it doesn't admit our .rhosts
631: authentication or doesn't know our host key. */
632: if (type == SSH_SMSG_FAILURE)
633: {
634: debug("Server refused our rhosts authentication or host key.");
635: return 0; /* Server refuses to authenticate us with this method. */
636: }
637:
638: /* Otherwise, the server should respond with a challenge. */
639: if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
640: packet_disconnect("Protocol error during RSA authentication: %d", type);
641:
642: /* Get the challenge from the packet. */
1.3 provos 643: challenge = BN_new();
644: packet_get_bignum(challenge, &clen);
1.1 deraadt 645:
646: packet_integrity_check(plen, clen, type);
647:
648: debug("Received RSA challenge for host key from server.");
649:
650: /* Compute a response to the challenge. */
1.3 provos 651: respond_to_rsa_challenge(challenge, host_key);
1.1 deraadt 652:
653: /* We no longer need the challenge. */
1.3 provos 654: BN_clear_free(challenge);
1.1 deraadt 655:
656: /* Wait for response from the server. */
657: type = packet_read(&plen);
658: if (type == SSH_SMSG_SUCCESS)
659: {
660: debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
661: return 1;
662: }
663: if (type != SSH_SMSG_FAILURE)
664: packet_disconnect("Protocol error waiting RSA auth response: %d", type);
665: debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
666: return 0;
667: }
668:
669: #ifdef KRB4
670: int try_kerberos_authentication()
671: {
672: KTEXT_ST auth; /* Kerberos data */
673: char *reply;
674: char inst[INST_SZ];
675: char *realm;
676: CREDENTIALS cred;
1.5 dugsong 677: int r, type, plen;
1.1 deraadt 678: Key_schedule schedule;
679: u_long checksum, cksum;
680: MSG_DAT msg_data;
681: struct sockaddr_in local, foreign;
682: struct stat st;
683:
684: /* Don't do anything if we don't have any tickets. */
685: if (stat(tkt_string(), &st) < 0) return 0;
686:
687: strncpy(inst, (char *) krb_get_phost(get_canonical_hostname()), INST_SZ);
688:
689: realm = (char *)krb_realmofhost(get_canonical_hostname());
690: if (!realm) {
1.5 dugsong 691: debug("Kerberos V4: no realm for %s", get_canonical_hostname());
1.1 deraadt 692: return 0;
693: }
694: /* This can really be anything. */
695: checksum = (u_long) getpid();
696:
697: r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
698: if (r != KSUCCESS) {
1.5 dugsong 699: debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]);
1.1 deraadt 700: return 0;
701: }
702: /* Get session key to decrypt the server's reply with. */
703: r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
704: if (r != KSUCCESS) {
1.5 dugsong 705: debug("get_cred failed: %s", krb_err_txt[r]);
1.1 deraadt 706: return 0;
707: }
708: des_key_sched((des_cblock *)cred.session, schedule);
709:
710: /* Send authentication info to server. */
711: packet_start(SSH_CMSG_AUTH_KERBEROS);
712: packet_put_string((char *)auth.dat, auth.length);
713: packet_send();
714: packet_write_wait();
715:
1.5 dugsong 716: /* Zero the buffer. */
1.1 deraadt 717: (void) memset(auth.dat, 0, MAX_KTXT_LEN);
718:
719: r = sizeof(local);
720: memset(&local, 0, sizeof(local));
721: if (getsockname(packet_get_connection_in(),
722: (struct sockaddr *) &local, &r) < 0)
1.5 dugsong 723: debug("getsockname failed: %s", strerror(errno));
1.1 deraadt 724:
725: r = sizeof(foreign);
726: memset(&foreign, 0, sizeof(foreign));
727: if (getpeername(packet_get_connection_in(),
1.28 markus 728: (struct sockaddr *)&foreign, &r) < 0) {
1.5 dugsong 729: debug("getpeername failed: %s", strerror(errno));
1.28 markus 730: fatal_cleanup();
731: }
1.1 deraadt 732:
733: /* Get server reply. */
734: type = packet_read(&plen);
735: switch(type) {
736:
737: case SSH_SMSG_FAILURE: /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
738: debug("Kerberos V4 authentication failed.");
739: return 0;
740: break;
741:
742: case SSH_SMSG_AUTH_KERBEROS_RESPONSE: /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
743: debug("Kerberos V4 authentication accepted.");
744:
745: /* Get server's response. */
746: reply = packet_get_string((unsigned int *)&auth.length);
747: memcpy(auth.dat, reply, auth.length);
748: xfree(reply);
749:
750: packet_integrity_check(plen, 4 + auth.length, type);
751:
752: /* If his response isn't properly encrypted with the session key,
753: and the decrypted checksum fails to match, he's bogus. Bail out. */
754: r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
755: &foreign, &local, &msg_data);
756: if (r != KSUCCESS) {
1.5 dugsong 757: debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]);
1.1 deraadt 758: packet_disconnect("Kerberos V4 challenge failed!");
759: }
1.5 dugsong 760: /* Fetch the (incremented) checksum that we supplied in the request. */
1.1 deraadt 761: (void)memcpy((char *)&cksum, (char *)msg_data.app_data, sizeof(cksum));
762: cksum = ntohl(cksum);
763:
764: /* If it matches, we're golden. */
765: if (cksum == checksum + 1) {
766: debug("Kerberos V4 challenge successful.");
767: return 1;
768: }
769: else
770: packet_disconnect("Kerberos V4 challenge failed!");
771: break;
772:
773: default:
774: packet_disconnect("Protocol error on Kerberos V4 response: %d", type);
775: }
776: return 0;
777: }
778: #endif /* KRB4 */
779:
780: #ifdef AFS
781: int send_kerberos_tgt()
782: {
783: CREDENTIALS *creds;
784: char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
1.5 dugsong 785: int r, type, plen;
1.1 deraadt 786: unsigned char buffer[8192];
787: struct stat st;
788:
789: /* Don't do anything if we don't have any tickets. */
790: if (stat(tkt_string(), &st) < 0) return 0;
791:
1.5 dugsong 792: creds = xmalloc(sizeof(*creds));
1.1 deraadt 793:
1.5 dugsong 794: if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) {
795: debug("Kerberos V4 tf_fullname failed: %s",krb_err_txt[r]);
1.1 deraadt 796: return 0;
1.5 dugsong 797: }
798: if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) {
799: debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]);
1.1 deraadt 800: return 0;
801: }
802: if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
1.5 dugsong 803: debug("Kerberos V4 ticket expired: %s", TKT_FILE);
1.1 deraadt 804: return 0;
805: }
806:
807: creds_to_radix(creds, buffer);
808: xfree(creds);
809:
810: packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
811: packet_put_string((char *)buffer, strlen(buffer));
812: packet_send();
813: packet_write_wait();
814:
815: type = packet_read(&plen);
1.5 dugsong 816:
1.1 deraadt 817: if (type == SSH_SMSG_FAILURE)
1.5 dugsong 818: debug("Kerberos TGT for realm %s rejected.", prealm);
1.1 deraadt 819: else if (type != SSH_SMSG_SUCCESS)
820: packet_disconnect("Protocol error on Kerberos TGT response: %d", type);
821:
822: return 1;
823: }
824:
825: void send_afs_tokens(void)
826: {
827: CREDENTIALS creds;
828: struct ViceIoctl parms;
829: struct ClearToken ct;
1.5 dugsong 830: int i, type, len, plen;
1.1 deraadt 831: char buf[2048], *p, *server_cell;
832: unsigned char buffer[8192];
833:
834: /* Move over ktc_GetToken, here's something leaner. */
835: for (i = 0; i < 100; i++) { /* just in case */
836: parms.in = (char *)&i;
837: parms.in_size = sizeof(i);
838: parms.out = buf;
839: parms.out_size = sizeof(buf);
840: if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0) break;
841: p = buf;
842:
843: /* Get secret token. */
844: memcpy(&creds.ticket_st.length, p, sizeof(unsigned int));
845: if (creds.ticket_st.length > MAX_KTXT_LEN) break;
846: p += sizeof(unsigned int);
847: memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
848: p += creds.ticket_st.length;
849:
850: /* Get clear token. */
851: memcpy(&len, p, sizeof(len));
852: if (len != sizeof(struct ClearToken)) break;
853: p += sizeof(len);
854: memcpy(&ct, p, len);
855: p += len;
856: p += sizeof(len); /* primary flag */
857: server_cell = p;
858:
859: /* Flesh out our credentials. */
1.6 deraadt 860: strlcpy(creds.service, "afs", sizeof creds.service);
1.1 deraadt 861: creds.instance[0] = '\0';
1.6 deraadt 862: strlcpy(creds.realm, server_cell, REALM_SZ);
1.1 deraadt 863: memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
864: creds.issue_date = ct.BeginTimestamp;
865: creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp);
866: creds.kvno = ct.AuthHandle;
1.5 dugsong 867: snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
1.1 deraadt 868: creds.pinst[0] = '\0';
869:
870: /* Encode token, ship it off. */
871: if (!creds_to_radix(&creds, buffer)) break;
872: packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
873: packet_put_string((char *)buffer, strlen(buffer));
874: packet_send();
875: packet_write_wait();
876:
877: /* Roger, Roger. Clearance, Clarence. What's your vector, Victor? */
878: type = packet_read(&plen);
879:
880: if (type == SSH_SMSG_FAILURE)
1.5 dugsong 881: debug("AFS token for cell %s rejected.", server_cell);
1.1 deraadt 882: else if (type != SSH_SMSG_SUCCESS)
883: packet_disconnect("Protocol error on AFS token response: %d", type);
884: }
885: }
886: #endif /* AFS */
887:
888: /* Waits for the server identification string, and sends our own identification
889: string. */
890:
891: void ssh_exchange_identification()
892: {
893: char buf[256], remote_version[256]; /* must be same size! */
894: int remote_major, remote_minor, i;
895: int connection_in = packet_get_connection_in();
896: int connection_out = packet_get_connection_out();
1.17 markus 897: extern Options options;
1.1 deraadt 898:
899: /* Read other side\'s version identification. */
900: for (i = 0; i < sizeof(buf) - 1; i++)
901: {
902: if (read(connection_in, &buf[i], 1) != 1)
1.36 markus 903: fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
1.1 deraadt 904: if (buf[i] == '\r')
905: {
906: buf[i] = '\n';
907: buf[i + 1] = 0;
908: break;
909: }
910: if (buf[i] == '\n')
911: {
912: buf[i + 1] = 0;
913: break;
914: }
915: }
916: buf[sizeof(buf) - 1] = 0;
917:
918: /* Check that the versions match. In future this might accept several
919: versions and set appropriate flags to handle them. */
920: 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);
1.17 markus 925:
1.22 markus 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.");
1.21 markus 929:
1.22 markus 930: /* We speak 1.3, too. */
1.21 markus 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.",
1.23 markus 935: remote_version);
1.17 markus 936: options.forward_agent = 0;
937: }
1.21 markus 938: }
1.1 deraadt 939: #if 0
940: /* Removed for now, to permit compatibility with latter versions. The server
941: will reject our version and disconnect if it doesn't support it. */
942: if (remote_major != PROTOCOL_MAJOR)
943: fatal("Protocol major versions differ: %d vs. %d",
944: PROTOCOL_MAJOR, remote_major);
945: #endif
946:
947: /* Send our own protocol version identification. */
1.6 deraadt 948: snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
1.1 deraadt 949: PROTOCOL_MAJOR, PROTOCOL_MINOR, SSH_VERSION);
950: if (write(connection_out, buf, strlen(buf)) != strlen(buf))
951: fatal("write: %.100s", strerror(errno));
952: }
953:
954: int ssh_cipher_default = SSH_CIPHER_3DES;
955:
956: int read_yes_or_no(const char *prompt, int defval)
957: {
958: char buf[1024];
959: FILE *f;
960: int retval = -1;
961:
962: if (isatty(0))
963: f = stdin;
964: else
965: f = fopen("/dev/tty", "rw");
966:
967: if (f == NULL)
968: return 0;
969:
970: fflush(stdout);
971:
972: while (1)
973: {
974: fprintf(stderr, "%s", prompt);
975: if (fgets(buf, sizeof(buf), f) == NULL)
976: {
977: /* Print a newline (the prompt probably didn\'t have one). */
978: fprintf(stderr, "\n");
1.6 deraadt 979: strlcpy(buf, "no", sizeof buf);
1.1 deraadt 980: }
981: /* Remove newline from response. */
982: if (strchr(buf, '\n'))
983: *strchr(buf, '\n') = 0;
984:
985: if (buf[0] == 0)
986: retval = defval;
987: if (strcmp(buf, "yes") == 0)
988: retval = 1;
989: if (strcmp(buf, "no") == 0)
990: retval = 0;
991:
992: if (retval != -1)
993: {
994: if (f != stdin)
995: fclose(f);
996: return retval;
997: }
998: }
999: }
1000:
1001: /* Starts a dialog with the server, and authenticates the current user on the
1002: server. This does not need any extra privileges. The basic connection
1003: to the server must already have been established before this is called.
1004: User is the remote user; if it is NULL, the current local user name will
1005: be used. Anonymous indicates that no rhosts authentication will be used.
1006: If login fails, this function prints an error and never returns.
1007: This function does not require super-user privileges. */
1008:
1.3 provos 1009: void ssh_login(int host_key_valid,
1010: RSA *own_host_key,
1.1 deraadt 1011: const char *orighost,
1.12 provos 1012: struct sockaddr_in *hostaddr,
1.27 markus 1013: uid_t original_real_uid)
1.1 deraadt 1014: {
1.27 markus 1015: extern Options options;
1.1 deraadt 1016: int i, type;
1017: char *password;
1018: struct passwd *pw;
1.3 provos 1019: BIGNUM *key;
1.12 provos 1020: RSA *host_key, *file_key;
1.3 provos 1021: RSA *public_key;
1.26 markus 1022: int bits, rbits;
1.1 deraadt 1023: unsigned char session_key[SSH_SESSION_KEY_LENGTH];
1024: const char *server_user, *local_user;
1.15 provos 1025: char *cp, *host, *ip = NULL;
1.1 deraadt 1026: unsigned char check_bytes[8];
1027: unsigned int supported_ciphers, supported_authentications, protocol_flags;
1028: HostStatus host_status;
1.12 provos 1029: HostStatus ip_status;
1.20 provos 1030: int host_ip_differ = 0;
1.15 provos 1031: int local = (ntohl(hostaddr->sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
1.1 deraadt 1032: int payload_len, clen, sum_len = 0;
1.5 dugsong 1033: u_int32_t rand = 0;
1.1 deraadt 1034:
1.27 markus 1035: if (options.check_host_ip)
1.15 provos 1036: ip = xstrdup(inet_ntoa(hostaddr->sin_addr));
1037:
1.1 deraadt 1038: /* Convert the user-supplied hostname into all lowercase. */
1039: host = xstrdup(orighost);
1040: for (cp = host; *cp; cp++)
1041: if (isupper(*cp))
1042: *cp = tolower(*cp);
1043:
1044: /* Exchange protocol version identification strings with the server. */
1045: ssh_exchange_identification();
1046:
1047: /* Put the connection into non-blocking mode. */
1048: packet_set_nonblocking();
1049:
1050: /* Get local user name. Use it as server user if no user name
1051: was given. */
1052: pw = getpwuid(original_real_uid);
1053: if (!pw)
1054: fatal("User id %d not found from user database.", original_real_uid);
1055: local_user = xstrdup(pw->pw_name);
1.27 markus 1056: server_user = options.user ? options.user : local_user;
1.1 deraadt 1057:
1058: debug("Waiting for server public key.");
1059:
1060: /* Wait for a public key packet from the server. */
1061: packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
1062:
1063: /* Get check bytes from the packet. */
1064: for (i = 0; i < 8; i++)
1065: check_bytes[i] = packet_get_char();
1066:
1067: /* Get the public key. */
1.3 provos 1068: public_key = RSA_new();
1.26 markus 1069: bits = packet_get_int(); /* bits */
1.3 provos 1070: public_key->e = BN_new();
1071: packet_get_bignum(public_key->e, &clen);
1.1 deraadt 1072: sum_len += clen;
1.3 provos 1073: public_key->n = BN_new();
1074: packet_get_bignum(public_key->n, &clen);
1.1 deraadt 1075: sum_len += clen;
1076:
1.26 markus 1077: rbits = BN_num_bits(public_key->n);
1078: if (bits != rbits) {
1.32 markus 1079: log("Warning: Server lies about size of server public key: "
1080: "actual size is %d bits vs. announced %d.", rbits, bits);
1081: log("Warning: This may be due to an old implementation of ssh.");
1.26 markus 1082: }
1083:
1.1 deraadt 1084: /* Get the host key. */
1.3 provos 1085: host_key = RSA_new();
1.26 markus 1086: bits = packet_get_int(); /* bits */
1.3 provos 1087: host_key->e = BN_new();
1088: packet_get_bignum(host_key->e, &clen);
1.1 deraadt 1089: sum_len += clen;
1.3 provos 1090: host_key->n = BN_new();
1091: packet_get_bignum(host_key->n, &clen);
1.1 deraadt 1092: sum_len += clen;
1.26 markus 1093:
1094: rbits = BN_num_bits(host_key->n);
1095: if (bits != rbits) {
1.32 markus 1096: log("Warning: Server lies about size of server host key: "
1097: "actual size is %d bits vs. announced %d.", rbits, bits);
1098: log("Warning: This may be due to an old implementation of ssh.");
1.26 markus 1099: }
1.1 deraadt 1100:
1.13 provos 1101: /* Store the host key from the known host file in here
1102: * so that we can compare it with the key for the IP
1103: * address. */
1104: file_key = RSA_new();
1105: file_key->n = BN_new();
1106: file_key->e = BN_new();
1.12 provos 1107:
1.1 deraadt 1108: /* Get protocol flags. */
1109: protocol_flags = packet_get_int();
1110: packet_set_protocol_flags(protocol_flags);
1111:
1112: /* Get supported cipher types. */
1113: supported_ciphers = packet_get_int();
1114:
1115: /* Get supported authentication types. */
1116: supported_authentications = packet_get_int();
1117:
1118: debug("Received server public key (%d bits) and host key (%d bits).",
1.3 provos 1119: BN_num_bits(public_key->n), BN_num_bits(host_key->n));
1.1 deraadt 1120:
1121: packet_integrity_check(payload_len,
1122: 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
1123: SSH_SMSG_PUBLIC_KEY);
1124:
1125: /* Compute the session id. */
1.29 markus 1126: compute_session_id(session_id, check_bytes, host_key->n, public_key->n);
1.1 deraadt 1127:
1128: /* Check if the host key is present in the user\'s list of known hosts
1129: or in the systemwide list. */
1.29 markus 1130: host_status = check_host_in_hostfile(options.user_hostfile, host,
1.12 provos 1131: host_key->e, host_key->n,
1132: file_key->e, file_key->n);
1.1 deraadt 1133: if (host_status == HOST_NEW)
1.27 markus 1134: host_status = check_host_in_hostfile(options.system_hostfile, host,
1.12 provos 1135: host_key->e, host_key->n,
1136: file_key->e, file_key->n);
1.1 deraadt 1137: /* Force accepting of the host key for localhost and 127.0.0.1.
1138: The problem is that if the home directory is NFS-mounted to multiple
1139: machines, localhost will refer to a different machine in each of them,
1140: and the user will get bogus HOST_CHANGED warnings. This essentially
1141: disables host authentication for localhost; however, this is probably
1142: not a real problem. */
1.15 provos 1143: if (local) {
1144: debug("Forcing accepting of host key for localhost.");
1145: host_status = HOST_OK;
1146: }
1.1 deraadt 1147:
1.14 provos 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 */
1.27 markus 1150: if (options.check_host_ip && !local && strcmp(host, ip)) {
1.12 provos 1151: RSA *ip_key = RSA_new();
1152: ip_key->n = BN_new();
1153: ip_key->e = BN_new();
1.27 markus 1154: ip_status = check_host_in_hostfile(options.user_hostfile, ip,
1.12 provos 1155: host_key->e, host_key->n,
1156: ip_key->e, ip_key->n);
1157:
1158: if (ip_status == HOST_NEW)
1.27 markus 1159: ip_status = check_host_in_hostfile(options.system_hostfile, ip,
1.12 provos 1160: host_key->e, host_key->n,
1161: ip_key->e, ip_key->n);
1.20 provos 1162: if (host_status == HOST_CHANGED &&
1163: (ip_status != HOST_CHANGED ||
1164: (BN_cmp(ip_key->e, file_key->e) || BN_cmp(ip_key->n, file_key->n))))
1165: host_ip_differ = 1;
1.12 provos 1166:
1167: RSA_free(ip_key);
1168: } else
1169: ip_status = host_status;
1.13 provos 1170:
1171: RSA_free(file_key);
1.12 provos 1172:
1173: switch (host_status) {
1174: case HOST_OK:
1175: /* The host is known and the key matches. */
1176: debug("Host '%.200s' is known and matches the host key.", host);
1.27 markus 1177: if (options.check_host_ip) {
1.12 provos 1178: if (ip_status == HOST_NEW) {
1.27 markus 1179: if (!add_host_to_hostfile(options.user_hostfile, ip,
1.12 provos 1180: host_key->e, host_key->n))
1.31 markus 1181: log("Failed to add the host key for IP address '%.30s' to the list of known hosts (%.30s).",
1182: ip, options.user_hostfile);
1.12 provos 1183: else
1.31 markus 1184: log("Warning: Permanently added host key for IP address '%.30s' to the list of known hosts.",
1185: ip);
1.12 provos 1186: } else if (ip_status != HOST_OK)
1.31 markus 1187: log("Warning: the host key for '%.200s' differs from the key for the IP address '%.30s'",
1188: host, ip);
1.12 provos 1189: }
1190:
1191: break;
1192: case HOST_NEW:
1.1 deraadt 1193: {
1.12 provos 1194: char hostline[1000], *hostp = hostline;
1.1 deraadt 1195: /* The host is new. */
1.27 markus 1196: if (options.strict_host_key_checking == 1) {
1.12 provos 1197: /* User has requested strict host key checking. We will not
1198: add the host key automatically. The only alternative left
1199: is to abort. */
1200: fatal("No host key is known for %.200s and you have requested strict checking.", host);
1.27 markus 1201: } else if (options.strict_host_key_checking == 2) { /* The default */
1.12 provos 1202: char prompt[1024];
1.33 markus 1203: char *fp = fingerprint(host_key->e, host_key->n);
1.12 provos 1204: snprintf(prompt, sizeof(prompt),
1205: "The authenticity of host '%.200s' can't be established.\n"
1.33 markus 1206: "Key fingerprint is %d %s.\n"
1207: "Are you sure you want to continue connecting (yes/no)? ",
1208: host, BN_num_bits(host_key->n), fp);
1.12 provos 1209: if (!read_yes_or_no(prompt, -1))
1210: fatal("Aborted by user!\n");
1211: }
1212:
1.27 markus 1213: if (options.check_host_ip && ip_status == HOST_NEW && strcmp(host, ip))
1.15 provos 1214: snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
1.12 provos 1215: else
1216: hostp = host;
1217:
1.1 deraadt 1218: /* If not in strict mode, add the key automatically to the local
1219: known_hosts file. */
1.27 markus 1220: if (!add_host_to_hostfile(options.user_hostfile, hostp,
1.3 provos 1221: host_key->e, host_key->n))
1.1 deraadt 1222: log("Failed to add the host to the list of known hosts (%.500s).",
1.27 markus 1223: options.user_hostfile);
1.1 deraadt 1224: else
1.12 provos 1225: log("Warning: Permanently added '%.200s' to the list of known hosts.",
1226: hostp);
1.1 deraadt 1227: break;
1228: }
1.12 provos 1229: case HOST_CHANGED:
1.27 markus 1230: if (options.check_host_ip) {
1.20 provos 1231: if (host_ip_differ) {
1.31 markus 1232: char *msg;
1233: if (ip_status == HOST_NEW)
1234: msg = "is unknown";
1235: else if (ip_status == HOST_OK)
1236: msg = "is unchanged";
1237: else
1238: msg = "has a different value";
1.12 provos 1239: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.19 markus 1240: error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
1.12 provos 1241: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1242: error("The host key for %s has changed,", host);
1.31 markus 1243: error("and the key for the according IP address %s", ip);
1244: error("%s. This could either mean that", msg);
1245: error("DNS SPOOFING is happening or the IP address for the host");
1.12 provos 1246: error("and its host key have changed at the same time");
1247: }
1248: }
1249:
1250: /* The host key has changed. */
1251: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1252: error("@ WARNING: HOST IDENTIFICATION HAS CHANGED! @");
1253: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1254: error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1255: error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1256: error("It is also possible that the host key has just been changed.");
1257: error("Please contact your system administrator.");
1258: error("Add correct host key in %.100s to get rid of this message.",
1.27 markus 1259: options.user_hostfile);
1.12 provos 1260:
1261: /* If strict host key checking is in use, the user will have to edit
1262: the key manually and we can only abort. */
1.27 markus 1263: if (options.strict_host_key_checking)
1.12 provos 1264: fatal("Host key for %.200s has changed and you have requested strict checking.", host);
1265:
1266: /* If strict host key checking has not been requested, allow the
1.19 markus 1267: connection but without password authentication or
1268: agent forwarding. */
1.27 markus 1269: if (options.password_authentication) {
1.19 markus 1270: error("Password authentication is disabled to avoid trojan horses.");
1.27 markus 1271: options.password_authentication = 0;
1.19 markus 1272: }
1.27 markus 1273: if (options.forward_agent) {
1.19 markus 1274: error("Agent forwarding is disabled to avoid trojan horses.");
1.27 markus 1275: options.forward_agent = 0;
1.19 markus 1276: }
1.12 provos 1277: /* XXX Should permit the user to change to use the new id. This could
1278: be done by converting the host key to an identifying sentence, tell
1279: that the host identifies itself by that sentence, and ask the user
1280: if he/she whishes to accept the authentication. */
1281: break;
1282: }
1.15 provos 1283:
1.27 markus 1284: if (options.check_host_ip)
1.15 provos 1285: xfree(ip);
1.12 provos 1286:
1.1 deraadt 1287: /* Generate a session key. */
1.3 provos 1288: arc4random_stir();
1.1 deraadt 1289:
1290: /* Generate an encryption key for the session. The key is a 256 bit
1291: random number, interpreted as a 32-byte key, with the least significant
1292: 8 bits being the first byte of the key. */
1.3 provos 1293: for (i = 0; i < 32; i++) {
1294: if (i % 4 == 0)
1295: rand = arc4random();
1296: session_key[i] = rand & 0xff;
1297: rand >>= 8;
1298: }
1.1 deraadt 1299:
1300: /* According to the protocol spec, the first byte of the session key is
1301: the highest byte of the integer. The session key is xored with the
1302: first 16 bytes of the session id. */
1.3 provos 1303: key = BN_new();
1304: BN_set_word(key, 0);
1.1 deraadt 1305: for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++)
1306: {
1.3 provos 1307: BN_lshift(key, key, 8);
1.1 deraadt 1308: if (i < 16)
1.3 provos 1309: BN_add_word(key, session_key[i] ^ session_id[i]);
1.1 deraadt 1310: else
1.3 provos 1311: BN_add_word(key, session_key[i]);
1.1 deraadt 1312: }
1313:
1314: /* Encrypt the integer using the public key and host key of the server
1315: (key with smaller modulus first). */
1.3 provos 1316: if (BN_cmp(public_key->n, host_key->n) < 0)
1.1 deraadt 1317: {
1318: /* Public key has smaller modulus. */
1.25 markus 1319: if (BN_num_bits(host_key->n) <
1320: BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) {
1321: fatal("respond_to_rsa_challenge: host_key %d < public_key %d + "
1322: "SSH_KEY_BITS_RESERVED %d",
1323: BN_num_bits(host_key->n),
1324: BN_num_bits(public_key->n),
1325: SSH_KEY_BITS_RESERVED);
1326: }
1.1 deraadt 1327:
1.3 provos 1328: rsa_public_encrypt(key, key, public_key);
1329: rsa_public_encrypt(key, key, host_key);
1.1 deraadt 1330: }
1331: else
1332: {
1333: /* Host key has smaller modulus (or they are equal). */
1.25 markus 1334: if (BN_num_bits(public_key->n) <
1335: BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) {
1336: fatal("respond_to_rsa_challenge: public_key %d < host_key %d + "
1337: "SSH_KEY_BITS_RESERVED %d",
1338: BN_num_bits(public_key->n),
1339: BN_num_bits(host_key->n),
1340: SSH_KEY_BITS_RESERVED);
1341: }
1.1 deraadt 1342:
1.3 provos 1343: rsa_public_encrypt(key, key, host_key);
1344: rsa_public_encrypt(key, key, public_key);
1.1 deraadt 1345: }
1346:
1.27 markus 1347: if (options.cipher == SSH_CIPHER_NOT_SET) {
1.1 deraadt 1348: if (cipher_mask() & supported_ciphers & (1 << ssh_cipher_default))
1.27 markus 1349: options.cipher = ssh_cipher_default;
1.3 provos 1350: else {
1.35 markus 1351: debug("Cipher %s not supported, using %.100s instead.",
1.3 provos 1352: cipher_name(ssh_cipher_default),
1353: cipher_name(SSH_FALLBACK_CIPHER));
1.27 markus 1354: options.cipher = SSH_FALLBACK_CIPHER;
1.3 provos 1355: }
1356: }
1.1 deraadt 1357:
1358: /* Check that the selected cipher is supported. */
1.27 markus 1359: if (!(supported_ciphers & (1 << options.cipher)))
1.1 deraadt 1360: fatal("Selected cipher type %.100s not supported by server.",
1.27 markus 1361: cipher_name(options.cipher));
1.1 deraadt 1362:
1.27 markus 1363: debug("Encryption type: %.100s", cipher_name(options.cipher));
1.1 deraadt 1364:
1365: /* Send the encrypted session key to the server. */
1366: packet_start(SSH_CMSG_SESSION_KEY);
1.27 markus 1367: packet_put_char(options.cipher);
1.1 deraadt 1368:
1369: /* Send the check bytes back to the server. */
1370: for (i = 0; i < 8; i++)
1371: packet_put_char(check_bytes[i]);
1372:
1373: /* Send the encrypted encryption key. */
1.3 provos 1374: packet_put_bignum(key);
1.1 deraadt 1375:
1376: /* Send protocol flags. */
1377: packet_put_int(SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
1378:
1379: /* Send the packet now. */
1380: packet_send();
1381: packet_write_wait();
1382:
1383: /* Destroy the session key integer and the public keys since we no longer
1384: need them. */
1.3 provos 1385: BN_clear_free(key);
1386: RSA_free(public_key);
1387: RSA_free(host_key);
1.1 deraadt 1388:
1389: debug("Sent encrypted session key.");
1390:
1391: /* Set the encryption key. */
1.30 markus 1392: packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
1.1 deraadt 1393:
1394: /* We will no longer need the session key here. Destroy any extra copies. */
1395: memset(session_key, 0, sizeof(session_key));
1396:
1397: /* Expect a success message from the server. Note that this message will
1398: be received in encrypted form. */
1399: 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:
1409: /* The server should respond with success if no authentication is needed
1410: (the user has no password). Otherwise the server responds with
1411: failure. */
1412: type = packet_read(&payload_len);
1413: if (type == SSH_SMSG_SUCCESS)
1414: return; /* Connection was accepted without authentication. */
1415: if (type != SSH_SMSG_FAILURE)
1416: packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER",
1417: type);
1418:
1.5 dugsong 1419: #ifdef AFS
1.1 deraadt 1420: /* Try Kerberos tgt passing if the server supports it. */
1421: if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1.27 markus 1422: options.kerberos_tgt_passing)
1.5 dugsong 1423: {
1.27 markus 1424: if (options.cipher == SSH_CIPHER_NONE)
1.5 dugsong 1425: log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1426: (void)send_kerberos_tgt();
1427: }
1428:
1.1 deraadt 1429: /* Try AFS token passing if the server supports it. */
1430: if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
1.27 markus 1431: options.afs_token_passing && k_hasafs()) {
1432: if (options.cipher == SSH_CIPHER_NONE)
1.1 deraadt 1433: log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
1434: send_afs_tokens();
1435: }
1436: #endif /* AFS */
1437:
1.5 dugsong 1438: #ifdef KRB4
1.1 deraadt 1439: if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1.27 markus 1440: options.kerberos_authentication)
1.1 deraadt 1441: {
1.5 dugsong 1442: debug("Trying Kerberos authentication.");
1.1 deraadt 1443: if (try_kerberos_authentication()) {
1444: /* The server should respond with success or failure. */
1445: type = packet_read(&payload_len);
1446: if (type == SSH_SMSG_SUCCESS)
1447: return; /* Successful connection. */
1448: if (type != SSH_SMSG_FAILURE)
1449: packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
1450: }
1451: }
1452: #endif /* KRB4 */
1453:
1454: /* Use rhosts authentication if running in privileged socket and we do not
1455: wish to remain anonymous. */
1456: if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1.27 markus 1457: options.rhosts_authentication)
1.1 deraadt 1458: {
1459: debug("Trying rhosts authentication.");
1460: packet_start(SSH_CMSG_AUTH_RHOSTS);
1461: packet_put_string(local_user, strlen(local_user));
1462: packet_send();
1463: packet_write_wait();
1464:
1465: /* The server should respond with success or failure. */
1466: type = packet_read(&payload_len);
1467: if (type == SSH_SMSG_SUCCESS)
1468: return; /* Successful connection. */
1469: if (type != SSH_SMSG_FAILURE)
1470: packet_disconnect("Protocol error: got %d in response to rhosts auth",
1471: type);
1472: }
1473:
1474: /* Try .rhosts or /etc/hosts.equiv authentication with RSA host
1475: authentication. */
1476: if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1.27 markus 1477: options.rhosts_rsa_authentication && host_key_valid)
1.1 deraadt 1478: {
1479: if (try_rhosts_rsa_authentication(local_user, own_host_key))
1480: return; /* Successful authentication. */
1481: }
1482:
1483: /* Try RSA authentication if the server supports it. */
1484: if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1.27 markus 1485: options.rsa_authentication)
1.1 deraadt 1486: {
1487: /* Try RSA authentication using the authentication agent. The agent
1488: is tried first because no passphrase is needed for it, whereas
1489: identity files may require passphrases. */
1490: if (try_agent_authentication())
1491: return; /* Successful connection. */
1492:
1493: /* Try RSA authentication for each identity. */
1.27 markus 1494: for (i = 0; i < options.num_identity_files; i++)
1495: if (try_rsa_authentication(pw, options.identity_files[i]))
1.1 deraadt 1496: return; /* Successful connection. */
1.37 ! markus 1497: }
! 1498:
! 1499: /* Try skey authentication if the server supports it. */
! 1500: if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
! 1501: options.skey_authentication && !options.batch_mode)
! 1502: {
! 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: }
1.1 deraadt 1544: }
1545:
1546: /* Try password authentication if the server supports it. */
1547: if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1.27 markus 1548: options.password_authentication && !options.batch_mode)
1.1 deraadt 1549: {
1.4 provos 1550: char prompt[80];
1551: snprintf(prompt, sizeof(prompt), "%.30s@%.30s's password: ",
1552: server_user, host);
1.1 deraadt 1553: debug("Doing password authentication.");
1.27 markus 1554: if (options.cipher == SSH_CIPHER_NONE)
1.1 deraadt 1555: log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
1.27 markus 1556: for (i = 0; i < options.number_of_password_prompts; i++) {
1.18 markus 1557: if (i != 0)
1558: error("Permission denied, please try again.");
1.16 dugsong 1559: password = read_passphrase(prompt, 0);
1560: packet_start(SSH_CMSG_AUTH_PASSWORD);
1561: packet_put_string(password, strlen(password));
1562: memset(password, 0, strlen(password));
1563: xfree(password);
1564: packet_send();
1565: packet_write_wait();
1566:
1567: type = packet_read(&payload_len);
1568: if (type == SSH_SMSG_SUCCESS)
1569: return; /* Successful connection. */
1570: if (type != SSH_SMSG_FAILURE)
1571: packet_disconnect("Protocol error: got %d in response to passwd auth", type);
1572: }
1.1 deraadt 1573: }
1574:
1575: /* All authentication methods have failed. Exit with an error message. */
1576: fatal("Permission denied.");
1577: /*NOTREACHED*/
1578: }