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