Annotation of src/usr.bin/ssh/sshconnect.c, Revision 1.33
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.33 ! markus 18: RCSID("$Id: sshconnect.c,v 1.32 1999/11/16 20:44:42 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: {
1.29 markus 336: int status, type;
1.1 deraadt 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.29 markus 353: for (status = ssh_get_first_identity(auth, e, n, &comment);
1.1 deraadt 354: status;
1.29 markus 355: status = ssh_get_next_identity(auth, 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.29 markus 392: if (!ssh_decrypt_challenge(auth, 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(),
1.28 markus 727: (struct sockaddr *)&foreign, &r) < 0) {
1.5 dugsong 728: debug("getpeername failed: %s", strerror(errno));
1.28 markus 729: fatal_cleanup();
730: }
1.1 deraadt 731:
732: /* Get server reply. */
733: type = packet_read(&plen);
734: switch(type) {
735:
736: case SSH_SMSG_FAILURE: /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
737: debug("Kerberos V4 authentication failed.");
738: return 0;
739: break;
740:
741: case SSH_SMSG_AUTH_KERBEROS_RESPONSE: /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
742: debug("Kerberos V4 authentication accepted.");
743:
744: /* Get server's response. */
745: reply = packet_get_string((unsigned int *)&auth.length);
746: memcpy(auth.dat, reply, auth.length);
747: xfree(reply);
748:
749: packet_integrity_check(plen, 4 + auth.length, type);
750:
751: /* If his response isn't properly encrypted with the session key,
752: and the decrypted checksum fails to match, he's bogus. Bail out. */
753: r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
754: &foreign, &local, &msg_data);
755: if (r != KSUCCESS) {
1.5 dugsong 756: debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]);
1.1 deraadt 757: packet_disconnect("Kerberos V4 challenge failed!");
758: }
1.5 dugsong 759: /* Fetch the (incremented) checksum that we supplied in the request. */
1.1 deraadt 760: (void)memcpy((char *)&cksum, (char *)msg_data.app_data, sizeof(cksum));
761: cksum = ntohl(cksum);
762:
763: /* If it matches, we're golden. */
764: if (cksum == checksum + 1) {
765: debug("Kerberos V4 challenge successful.");
766: return 1;
767: }
768: else
769: packet_disconnect("Kerberos V4 challenge failed!");
770: break;
771:
772: default:
773: packet_disconnect("Protocol error on Kerberos V4 response: %d", type);
774: }
775: return 0;
776: }
777: #endif /* KRB4 */
778:
779: #ifdef AFS
780: int send_kerberos_tgt()
781: {
782: CREDENTIALS *creds;
783: char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
1.5 dugsong 784: int r, type, plen;
1.1 deraadt 785: unsigned char buffer[8192];
786: struct stat st;
787:
788: /* Don't do anything if we don't have any tickets. */
789: if (stat(tkt_string(), &st) < 0) return 0;
790:
1.5 dugsong 791: creds = xmalloc(sizeof(*creds));
1.1 deraadt 792:
1.5 dugsong 793: if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) {
794: debug("Kerberos V4 tf_fullname failed: %s",krb_err_txt[r]);
1.1 deraadt 795: return 0;
1.5 dugsong 796: }
797: if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) {
798: debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]);
1.1 deraadt 799: return 0;
800: }
801: if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
1.5 dugsong 802: debug("Kerberos V4 ticket expired: %s", TKT_FILE);
1.1 deraadt 803: return 0;
804: }
805:
806: creds_to_radix(creds, buffer);
807: xfree(creds);
808:
809: packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
810: packet_put_string((char *)buffer, strlen(buffer));
811: packet_send();
812: packet_write_wait();
813:
814: type = packet_read(&plen);
1.5 dugsong 815:
1.1 deraadt 816: if (type == SSH_SMSG_FAILURE)
1.5 dugsong 817: debug("Kerberos TGT for realm %s rejected.", prealm);
1.1 deraadt 818: else if (type != SSH_SMSG_SUCCESS)
819: packet_disconnect("Protocol error on Kerberos TGT response: %d", type);
820:
821: return 1;
822: }
823:
824: void send_afs_tokens(void)
825: {
826: CREDENTIALS creds;
827: struct ViceIoctl parms;
828: struct ClearToken ct;
1.5 dugsong 829: int i, type, len, plen;
1.1 deraadt 830: char buf[2048], *p, *server_cell;
831: unsigned char buffer[8192];
832:
833: /* Move over ktc_GetToken, here's something leaner. */
834: for (i = 0; i < 100; i++) { /* just in case */
835: parms.in = (char *)&i;
836: parms.in_size = sizeof(i);
837: parms.out = buf;
838: parms.out_size = sizeof(buf);
839: if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0) break;
840: p = buf;
841:
842: /* Get secret token. */
843: memcpy(&creds.ticket_st.length, p, sizeof(unsigned int));
844: if (creds.ticket_st.length > MAX_KTXT_LEN) break;
845: p += sizeof(unsigned int);
846: memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
847: p += creds.ticket_st.length;
848:
849: /* Get clear token. */
850: memcpy(&len, p, sizeof(len));
851: if (len != sizeof(struct ClearToken)) break;
852: p += sizeof(len);
853: memcpy(&ct, p, len);
854: p += len;
855: p += sizeof(len); /* primary flag */
856: server_cell = p;
857:
858: /* Flesh out our credentials. */
1.6 deraadt 859: strlcpy(creds.service, "afs", sizeof creds.service);
1.1 deraadt 860: creds.instance[0] = '\0';
1.6 deraadt 861: strlcpy(creds.realm, server_cell, REALM_SZ);
1.1 deraadt 862: memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
863: creds.issue_date = ct.BeginTimestamp;
864: creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp);
865: creds.kvno = ct.AuthHandle;
1.5 dugsong 866: snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
1.1 deraadt 867: creds.pinst[0] = '\0';
868:
869: /* Encode token, ship it off. */
870: if (!creds_to_radix(&creds, buffer)) break;
871: packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
872: packet_put_string((char *)buffer, strlen(buffer));
873: packet_send();
874: packet_write_wait();
875:
876: /* Roger, Roger. Clearance, Clarence. What's your vector, Victor? */
877: type = packet_read(&plen);
878:
879: if (type == SSH_SMSG_FAILURE)
1.5 dugsong 880: debug("AFS token for cell %s rejected.", server_cell);
1.1 deraadt 881: else if (type != SSH_SMSG_SUCCESS)
882: packet_disconnect("Protocol error on AFS token response: %d", type);
883: }
884: }
885: #endif /* AFS */
886:
887: /* Waits for the server identification string, and sends our own identification
888: string. */
889:
890: void ssh_exchange_identification()
891: {
892: char buf[256], remote_version[256]; /* must be same size! */
893: int remote_major, remote_minor, i;
894: int connection_in = packet_get_connection_in();
895: int connection_out = packet_get_connection_out();
1.17 markus 896: extern Options options;
1.1 deraadt 897:
898: /* Read other side\'s version identification. */
899: for (i = 0; i < sizeof(buf) - 1; i++)
900: {
901: if (read(connection_in, &buf[i], 1) != 1)
902: fatal("read: %.100s", strerror(errno));
903: if (buf[i] == '\r')
904: {
905: buf[i] = '\n';
906: buf[i + 1] = 0;
907: break;
908: }
909: if (buf[i] == '\n')
910: {
911: buf[i + 1] = 0;
912: break;
913: }
914: }
915: buf[sizeof(buf) - 1] = 0;
916:
917: /* Check that the versions match. In future this might accept several
918: versions and set appropriate flags to handle them. */
919: if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", &remote_major, &remote_minor,
920: remote_version) != 3)
921: fatal("Bad remote protocol version identification: '%.100s'", buf);
922: debug("Remote protocol version %d.%d, remote software version %.100s",
923: remote_major, remote_minor, remote_version);
1.17 markus 924:
1.22 markus 925: /* Check if the remote protocol version is too old. */
926: if (remote_major == 1 && remote_minor < 3)
927: fatal("Remote machine has too old SSH software version.");
1.21 markus 928:
1.22 markus 929: /* We speak 1.3, too. */
1.21 markus 930: if (remote_major == 1 && remote_minor == 3) {
931: enable_compat13();
932: if (options.forward_agent && strcmp(remote_version, SSH_VERSION) != 0) {
933: log("Agent forwarding disabled, remote version '%s' is not compatible.",
1.23 markus 934: remote_version);
1.17 markus 935: options.forward_agent = 0;
936: }
1.21 markus 937: }
1.1 deraadt 938: #if 0
939: /* Removed for now, to permit compatibility with latter versions. The server
940: will reject our version and disconnect if it doesn't support it. */
941: if (remote_major != PROTOCOL_MAJOR)
942: fatal("Protocol major versions differ: %d vs. %d",
943: PROTOCOL_MAJOR, remote_major);
944: #endif
945:
946: /* Send our own protocol version identification. */
1.6 deraadt 947: snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
1.1 deraadt 948: PROTOCOL_MAJOR, PROTOCOL_MINOR, SSH_VERSION);
949: if (write(connection_out, buf, strlen(buf)) != strlen(buf))
950: fatal("write: %.100s", strerror(errno));
951: }
952:
953: int ssh_cipher_default = SSH_CIPHER_3DES;
954:
955: int read_yes_or_no(const char *prompt, int defval)
956: {
957: char buf[1024];
958: FILE *f;
959: int retval = -1;
960:
961: if (isatty(0))
962: f = stdin;
963: else
964: f = fopen("/dev/tty", "rw");
965:
966: if (f == NULL)
967: return 0;
968:
969: fflush(stdout);
970:
971: while (1)
972: {
973: fprintf(stderr, "%s", prompt);
974: if (fgets(buf, sizeof(buf), f) == NULL)
975: {
976: /* Print a newline (the prompt probably didn\'t have one). */
977: fprintf(stderr, "\n");
1.6 deraadt 978: strlcpy(buf, "no", sizeof buf);
1.1 deraadt 979: }
980: /* Remove newline from response. */
981: if (strchr(buf, '\n'))
982: *strchr(buf, '\n') = 0;
983:
984: if (buf[0] == 0)
985: retval = defval;
986: if (strcmp(buf, "yes") == 0)
987: retval = 1;
988: if (strcmp(buf, "no") == 0)
989: retval = 0;
990:
991: if (retval != -1)
992: {
993: if (f != stdin)
994: fclose(f);
995: return retval;
996: }
997: }
998: }
999:
1000: /* Starts a dialog with the server, and authenticates the current user on the
1001: server. This does not need any extra privileges. The basic connection
1002: to the server must already have been established before this is called.
1003: User is the remote user; if it is NULL, the current local user name will
1004: be used. Anonymous indicates that no rhosts authentication will be used.
1005: If login fails, this function prints an error and never returns.
1006: This function does not require super-user privileges. */
1007:
1.3 provos 1008: void ssh_login(int host_key_valid,
1009: RSA *own_host_key,
1.1 deraadt 1010: const char *orighost,
1.12 provos 1011: struct sockaddr_in *hostaddr,
1.27 markus 1012: uid_t original_real_uid)
1.1 deraadt 1013: {
1.27 markus 1014: extern Options options;
1.1 deraadt 1015: int i, type;
1016: char *password;
1017: struct passwd *pw;
1.3 provos 1018: BIGNUM *key;
1.12 provos 1019: RSA *host_key, *file_key;
1.3 provos 1020: RSA *public_key;
1.26 markus 1021: int bits, rbits;
1.1 deraadt 1022: unsigned char session_key[SSH_SESSION_KEY_LENGTH];
1023: const char *server_user, *local_user;
1.15 provos 1024: char *cp, *host, *ip = NULL;
1.1 deraadt 1025: unsigned char check_bytes[8];
1026: unsigned int supported_ciphers, supported_authentications, protocol_flags;
1027: HostStatus host_status;
1.12 provos 1028: HostStatus ip_status;
1.20 provos 1029: int host_ip_differ = 0;
1.15 provos 1030: int local = (ntohl(hostaddr->sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
1.1 deraadt 1031: int payload_len, clen, sum_len = 0;
1.5 dugsong 1032: u_int32_t rand = 0;
1.1 deraadt 1033:
1.27 markus 1034: if (options.check_host_ip)
1.15 provos 1035: ip = xstrdup(inet_ntoa(hostaddr->sin_addr));
1036:
1.1 deraadt 1037: /* Convert the user-supplied hostname into all lowercase. */
1038: host = xstrdup(orighost);
1039: for (cp = host; *cp; cp++)
1040: if (isupper(*cp))
1041: *cp = tolower(*cp);
1042:
1043: /* Exchange protocol version identification strings with the server. */
1044: ssh_exchange_identification();
1045:
1046: /* Put the connection into non-blocking mode. */
1047: packet_set_nonblocking();
1048:
1049: /* Get local user name. Use it as server user if no user name
1050: was given. */
1051: pw = getpwuid(original_real_uid);
1052: if (!pw)
1053: fatal("User id %d not found from user database.", original_real_uid);
1054: local_user = xstrdup(pw->pw_name);
1.27 markus 1055: server_user = options.user ? options.user : local_user;
1.1 deraadt 1056:
1057: debug("Waiting for server public key.");
1058:
1059: /* Wait for a public key packet from the server. */
1060: packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
1061:
1062: /* Get check bytes from the packet. */
1063: for (i = 0; i < 8; i++)
1064: check_bytes[i] = packet_get_char();
1065:
1066: /* Get the public key. */
1.3 provos 1067: public_key = RSA_new();
1.26 markus 1068: bits = packet_get_int(); /* bits */
1.3 provos 1069: public_key->e = BN_new();
1070: packet_get_bignum(public_key->e, &clen);
1.1 deraadt 1071: sum_len += clen;
1.3 provos 1072: public_key->n = BN_new();
1073: packet_get_bignum(public_key->n, &clen);
1.1 deraadt 1074: sum_len += clen;
1075:
1.26 markus 1076: rbits = BN_num_bits(public_key->n);
1077: if (bits != rbits) {
1.32 markus 1078: log("Warning: Server lies about size of server public key: "
1079: "actual size is %d bits vs. announced %d.", rbits, bits);
1080: log("Warning: This may be due to an old implementation of ssh.");
1.26 markus 1081: }
1082:
1.1 deraadt 1083: /* Get the host key. */
1.3 provos 1084: host_key = RSA_new();
1.26 markus 1085: bits = packet_get_int(); /* bits */
1.3 provos 1086: host_key->e = BN_new();
1087: packet_get_bignum(host_key->e, &clen);
1.1 deraadt 1088: sum_len += clen;
1.3 provos 1089: host_key->n = BN_new();
1090: packet_get_bignum(host_key->n, &clen);
1.1 deraadt 1091: sum_len += clen;
1.26 markus 1092:
1093: rbits = BN_num_bits(host_key->n);
1094: if (bits != rbits) {
1.32 markus 1095: log("Warning: Server lies about size of server host key: "
1096: "actual size is %d bits vs. announced %d.", rbits, bits);
1097: log("Warning: This may be due to an old implementation of ssh.");
1.26 markus 1098: }
1.1 deraadt 1099:
1.13 provos 1100: /* Store the host key from the known host file in here
1101: * so that we can compare it with the key for the IP
1102: * address. */
1103: file_key = RSA_new();
1104: file_key->n = BN_new();
1105: file_key->e = BN_new();
1.12 provos 1106:
1.1 deraadt 1107: /* Get protocol flags. */
1108: protocol_flags = packet_get_int();
1109: packet_set_protocol_flags(protocol_flags);
1110:
1111: /* Get supported cipher types. */
1112: supported_ciphers = packet_get_int();
1113:
1114: /* Get supported authentication types. */
1115: supported_authentications = packet_get_int();
1116:
1117: debug("Received server public key (%d bits) and host key (%d bits).",
1.3 provos 1118: BN_num_bits(public_key->n), BN_num_bits(host_key->n));
1.1 deraadt 1119:
1120: packet_integrity_check(payload_len,
1121: 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
1122: SSH_SMSG_PUBLIC_KEY);
1123:
1124: /* Compute the session id. */
1.29 markus 1125: compute_session_id(session_id, check_bytes, host_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.29 markus 1129: host_status = check_host_in_hostfile(options.user_hostfile, host,
1.12 provos 1130: host_key->e, host_key->n,
1131: file_key->e, file_key->n);
1.1 deraadt 1132: if (host_status == HOST_NEW)
1.27 markus 1133: host_status = check_host_in_hostfile(options.system_hostfile, host,
1.12 provos 1134: host_key->e, host_key->n,
1135: file_key->e, file_key->n);
1.1 deraadt 1136: /* Force accepting of the host key for localhost and 127.0.0.1.
1137: The problem is that if the home directory is NFS-mounted to multiple
1138: machines, localhost will refer to a different machine in each of them,
1139: and the user will get bogus HOST_CHANGED warnings. This essentially
1140: disables host authentication for localhost; however, this is probably
1141: not a real problem. */
1.15 provos 1142: if (local) {
1143: debug("Forcing accepting of host key for localhost.");
1144: host_status = HOST_OK;
1145: }
1.1 deraadt 1146:
1.14 provos 1147: /* Also perform check for the ip address, skip the check if we are
1148: localhost or the hostname was an ip address to begin with */
1.27 markus 1149: if (options.check_host_ip && !local && strcmp(host, ip)) {
1.12 provos 1150: RSA *ip_key = RSA_new();
1151: ip_key->n = BN_new();
1152: ip_key->e = BN_new();
1.27 markus 1153: ip_status = check_host_in_hostfile(options.user_hostfile, ip,
1.12 provos 1154: host_key->e, host_key->n,
1155: ip_key->e, ip_key->n);
1156:
1157: if (ip_status == HOST_NEW)
1.27 markus 1158: ip_status = check_host_in_hostfile(options.system_hostfile, ip,
1.12 provos 1159: host_key->e, host_key->n,
1160: ip_key->e, ip_key->n);
1.20 provos 1161: if (host_status == HOST_CHANGED &&
1162: (ip_status != HOST_CHANGED ||
1163: (BN_cmp(ip_key->e, file_key->e) || BN_cmp(ip_key->n, file_key->n))))
1164: host_ip_differ = 1;
1.12 provos 1165:
1166: RSA_free(ip_key);
1167: } else
1168: ip_status = host_status;
1.13 provos 1169:
1170: RSA_free(file_key);
1.12 provos 1171:
1172: switch (host_status) {
1173: case HOST_OK:
1174: /* The host is known and the key matches. */
1175: debug("Host '%.200s' is known and matches the host key.", host);
1.27 markus 1176: if (options.check_host_ip) {
1.12 provos 1177: if (ip_status == HOST_NEW) {
1.27 markus 1178: if (!add_host_to_hostfile(options.user_hostfile, ip,
1.12 provos 1179: host_key->e, host_key->n))
1.31 markus 1180: log("Failed to add the host key for IP address '%.30s' to the list of known hosts (%.30s).",
1181: ip, options.user_hostfile);
1.12 provos 1182: else
1.31 markus 1183: log("Warning: Permanently added host key for IP address '%.30s' to the list of known hosts.",
1184: ip);
1.12 provos 1185: } else if (ip_status != HOST_OK)
1.31 markus 1186: log("Warning: the host key for '%.200s' differs from the key for the IP address '%.30s'",
1187: host, ip);
1.12 provos 1188: }
1189:
1190: break;
1191: case HOST_NEW:
1.1 deraadt 1192: {
1.12 provos 1193: char hostline[1000], *hostp = hostline;
1.1 deraadt 1194: /* The host is new. */
1.27 markus 1195: if (options.strict_host_key_checking == 1) {
1.12 provos 1196: /* User has requested strict host key checking. We will not
1197: add the host key automatically. The only alternative left
1198: is to abort. */
1199: fatal("No host key is known for %.200s and you have requested strict checking.", host);
1.27 markus 1200: } else if (options.strict_host_key_checking == 2) { /* The default */
1.12 provos 1201: char prompt[1024];
1.33 ! markus 1202: char *fp = fingerprint(host_key->e, host_key->n);
1.12 provos 1203: snprintf(prompt, sizeof(prompt),
1204: "The authenticity of host '%.200s' can't be established.\n"
1.33 ! markus 1205: "Key fingerprint is %d %s.\n"
! 1206: "Are you sure you want to continue connecting (yes/no)? ",
! 1207: host, BN_num_bits(host_key->n), fp);
1.12 provos 1208: if (!read_yes_or_no(prompt, -1))
1209: fatal("Aborted by user!\n");
1210: }
1211:
1.27 markus 1212: if (options.check_host_ip && ip_status == HOST_NEW && strcmp(host, ip))
1.15 provos 1213: snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
1.12 provos 1214: else
1215: hostp = host;
1216:
1.1 deraadt 1217: /* If not in strict mode, add the key automatically to the local
1218: known_hosts file. */
1.27 markus 1219: if (!add_host_to_hostfile(options.user_hostfile, hostp,
1.3 provos 1220: host_key->e, host_key->n))
1.1 deraadt 1221: log("Failed to add the host to the list of known hosts (%.500s).",
1.27 markus 1222: options.user_hostfile);
1.1 deraadt 1223: else
1.12 provos 1224: log("Warning: Permanently added '%.200s' to the list of known hosts.",
1225: hostp);
1.1 deraadt 1226: break;
1227: }
1.12 provos 1228: case HOST_CHANGED:
1.27 markus 1229: if (options.check_host_ip) {
1.20 provos 1230: if (host_ip_differ) {
1.31 markus 1231: char *msg;
1232: if (ip_status == HOST_NEW)
1233: msg = "is unknown";
1234: else if (ip_status == HOST_OK)
1235: msg = "is unchanged";
1236: else
1237: msg = "has a different value";
1.12 provos 1238: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.19 markus 1239: error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
1.12 provos 1240: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1241: error("The host key for %s has changed,", host);
1.31 markus 1242: error("and the key for the according IP address %s", ip);
1243: error("%s. This could either mean that", msg);
1244: error("DNS SPOOFING is happening or the IP address for the host");
1.12 provos 1245: error("and its host key have changed at the same time");
1246: }
1247: }
1248:
1249: /* The host key has changed. */
1250: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1251: error("@ WARNING: HOST IDENTIFICATION HAS CHANGED! @");
1252: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1253: error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1254: error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1255: error("It is also possible that the host key has just been changed.");
1256: error("Please contact your system administrator.");
1257: error("Add correct host key in %.100s to get rid of this message.",
1.27 markus 1258: options.user_hostfile);
1.12 provos 1259:
1260: /* If strict host key checking is in use, the user will have to edit
1261: the key manually and we can only abort. */
1.27 markus 1262: if (options.strict_host_key_checking)
1.12 provos 1263: fatal("Host key for %.200s has changed and you have requested strict checking.", host);
1264:
1265: /* If strict host key checking has not been requested, allow the
1.19 markus 1266: connection but without password authentication or
1267: agent forwarding. */
1.27 markus 1268: if (options.password_authentication) {
1.19 markus 1269: error("Password authentication is disabled to avoid trojan horses.");
1.27 markus 1270: options.password_authentication = 0;
1.19 markus 1271: }
1.27 markus 1272: if (options.forward_agent) {
1.19 markus 1273: error("Agent forwarding is disabled to avoid trojan horses.");
1.27 markus 1274: options.forward_agent = 0;
1.19 markus 1275: }
1.12 provos 1276: /* XXX Should permit the user to change to use the new id. This could
1277: be done by converting the host key to an identifying sentence, tell
1278: that the host identifies itself by that sentence, and ask the user
1279: if he/she whishes to accept the authentication. */
1280: break;
1281: }
1.15 provos 1282:
1.27 markus 1283: if (options.check_host_ip)
1.15 provos 1284: xfree(ip);
1.12 provos 1285:
1.1 deraadt 1286: /* Generate a session key. */
1.3 provos 1287: arc4random_stir();
1.1 deraadt 1288:
1289: /* Generate an encryption key for the session. The key is a 256 bit
1290: random number, interpreted as a 32-byte key, with the least significant
1291: 8 bits being the first byte of the key. */
1.3 provos 1292: for (i = 0; i < 32; i++) {
1293: if (i % 4 == 0)
1294: rand = arc4random();
1295: session_key[i] = rand & 0xff;
1296: rand >>= 8;
1297: }
1.1 deraadt 1298:
1299: /* According to the protocol spec, the first byte of the session key is
1300: the highest byte of the integer. The session key is xored with the
1301: first 16 bytes of the session id. */
1.3 provos 1302: key = BN_new();
1303: BN_set_word(key, 0);
1.1 deraadt 1304: for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++)
1305: {
1.3 provos 1306: BN_lshift(key, key, 8);
1.1 deraadt 1307: if (i < 16)
1.3 provos 1308: BN_add_word(key, session_key[i] ^ session_id[i]);
1.1 deraadt 1309: else
1.3 provos 1310: BN_add_word(key, session_key[i]);
1.1 deraadt 1311: }
1312:
1313: /* Encrypt the integer using the public key and host key of the server
1314: (key with smaller modulus first). */
1.3 provos 1315: if (BN_cmp(public_key->n, host_key->n) < 0)
1.1 deraadt 1316: {
1317: /* Public key has smaller modulus. */
1.25 markus 1318: if (BN_num_bits(host_key->n) <
1319: BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) {
1320: fatal("respond_to_rsa_challenge: host_key %d < public_key %d + "
1321: "SSH_KEY_BITS_RESERVED %d",
1322: BN_num_bits(host_key->n),
1323: BN_num_bits(public_key->n),
1324: SSH_KEY_BITS_RESERVED);
1325: }
1.1 deraadt 1326:
1.3 provos 1327: rsa_public_encrypt(key, key, public_key);
1328: rsa_public_encrypt(key, key, host_key);
1.1 deraadt 1329: }
1330: else
1331: {
1332: /* Host key has smaller modulus (or they are equal). */
1.25 markus 1333: if (BN_num_bits(public_key->n) <
1334: BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) {
1335: fatal("respond_to_rsa_challenge: public_key %d < host_key %d + "
1336: "SSH_KEY_BITS_RESERVED %d",
1337: BN_num_bits(public_key->n),
1338: BN_num_bits(host_key->n),
1339: SSH_KEY_BITS_RESERVED);
1340: }
1.1 deraadt 1341:
1.3 provos 1342: rsa_public_encrypt(key, key, host_key);
1343: rsa_public_encrypt(key, key, public_key);
1.1 deraadt 1344: }
1345:
1.27 markus 1346: if (options.cipher == SSH_CIPHER_NOT_SET) {
1.1 deraadt 1347: if (cipher_mask() & supported_ciphers & (1 << ssh_cipher_default))
1.27 markus 1348: options.cipher = ssh_cipher_default;
1.3 provos 1349: else {
1350: debug("Cipher %d not supported, using %.100s instead.",
1351: cipher_name(ssh_cipher_default),
1352: cipher_name(SSH_FALLBACK_CIPHER));
1.27 markus 1353: options.cipher = SSH_FALLBACK_CIPHER;
1.3 provos 1354: }
1355: }
1.1 deraadt 1356:
1357: /* Check that the selected cipher is supported. */
1.27 markus 1358: if (!(supported_ciphers & (1 << options.cipher)))
1.1 deraadt 1359: fatal("Selected cipher type %.100s not supported by server.",
1.27 markus 1360: cipher_name(options.cipher));
1.1 deraadt 1361:
1.27 markus 1362: debug("Encryption type: %.100s", cipher_name(options.cipher));
1.1 deraadt 1363:
1364: /* Send the encrypted session key to the server. */
1365: packet_start(SSH_CMSG_SESSION_KEY);
1.27 markus 1366: packet_put_char(options.cipher);
1.1 deraadt 1367:
1368: /* Send the check bytes back to the server. */
1369: for (i = 0; i < 8; i++)
1370: packet_put_char(check_bytes[i]);
1371:
1372: /* Send the encrypted encryption key. */
1.3 provos 1373: packet_put_bignum(key);
1.1 deraadt 1374:
1375: /* Send protocol flags. */
1376: packet_put_int(SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
1377:
1378: /* Send the packet now. */
1379: packet_send();
1380: packet_write_wait();
1381:
1382: /* Destroy the session key integer and the public keys since we no longer
1383: need them. */
1.3 provos 1384: BN_clear_free(key);
1385: RSA_free(public_key);
1386: RSA_free(host_key);
1.1 deraadt 1387:
1388: debug("Sent encrypted session key.");
1389:
1390: /* Set the encryption key. */
1.30 markus 1391: packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
1.1 deraadt 1392:
1393: /* We will no longer need the session key here. Destroy any extra copies. */
1394: memset(session_key, 0, sizeof(session_key));
1395:
1396: /* Expect a success message from the server. Note that this message will
1397: be received in encrypted form. */
1398: packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1399:
1400: debug("Received encrypted confirmation.");
1401:
1402: /* Send the name of the user to log in as on the server. */
1403: packet_start(SSH_CMSG_USER);
1404: packet_put_string(server_user, strlen(server_user));
1405: packet_send();
1406: packet_write_wait();
1407:
1408: /* The server should respond with success if no authentication is needed
1409: (the user has no password). Otherwise the server responds with
1410: failure. */
1411: type = packet_read(&payload_len);
1412: if (type == SSH_SMSG_SUCCESS)
1413: return; /* Connection was accepted without authentication. */
1414: if (type != SSH_SMSG_FAILURE)
1415: packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER",
1416: type);
1417:
1.5 dugsong 1418: #ifdef AFS
1.1 deraadt 1419: /* Try Kerberos tgt passing if the server supports it. */
1420: if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1.27 markus 1421: options.kerberos_tgt_passing)
1.5 dugsong 1422: {
1.27 markus 1423: if (options.cipher == SSH_CIPHER_NONE)
1.5 dugsong 1424: log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1425: (void)send_kerberos_tgt();
1426: }
1427:
1.1 deraadt 1428: /* Try AFS token passing if the server supports it. */
1429: if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
1.27 markus 1430: options.afs_token_passing && k_hasafs()) {
1431: if (options.cipher == SSH_CIPHER_NONE)
1.1 deraadt 1432: log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
1433: send_afs_tokens();
1434: }
1435: #endif /* AFS */
1436:
1.5 dugsong 1437: #ifdef KRB4
1.1 deraadt 1438: if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1.27 markus 1439: options.kerberos_authentication)
1.1 deraadt 1440: {
1.5 dugsong 1441: debug("Trying Kerberos authentication.");
1.1 deraadt 1442: if (try_kerberos_authentication()) {
1443: /* The server should respond with success or failure. */
1444: type = packet_read(&payload_len);
1445: if (type == SSH_SMSG_SUCCESS)
1446: return; /* Successful connection. */
1447: if (type != SSH_SMSG_FAILURE)
1448: packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
1449: }
1450: }
1451: #endif /* KRB4 */
1452:
1453: /* Use rhosts authentication if running in privileged socket and we do not
1454: wish to remain anonymous. */
1455: if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1.27 markus 1456: options.rhosts_authentication)
1.1 deraadt 1457: {
1458: debug("Trying rhosts authentication.");
1459: packet_start(SSH_CMSG_AUTH_RHOSTS);
1460: packet_put_string(local_user, strlen(local_user));
1461: packet_send();
1462: packet_write_wait();
1463:
1464: /* The server should respond with success or failure. */
1465: type = packet_read(&payload_len);
1466: if (type == SSH_SMSG_SUCCESS)
1467: return; /* Successful connection. */
1468: if (type != SSH_SMSG_FAILURE)
1469: packet_disconnect("Protocol error: got %d in response to rhosts auth",
1470: type);
1471: }
1472:
1473: /* Try .rhosts or /etc/hosts.equiv authentication with RSA host
1474: authentication. */
1475: if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1.27 markus 1476: options.rhosts_rsa_authentication && host_key_valid)
1.1 deraadt 1477: {
1478: if (try_rhosts_rsa_authentication(local_user, own_host_key))
1479: return; /* Successful authentication. */
1480: }
1481:
1482: /* Try RSA authentication if the server supports it. */
1483: if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1.27 markus 1484: options.rsa_authentication)
1.1 deraadt 1485: {
1486: /* Try RSA authentication using the authentication agent. The agent
1487: is tried first because no passphrase is needed for it, whereas
1488: identity files may require passphrases. */
1489: if (try_agent_authentication())
1490: return; /* Successful connection. */
1491:
1492: /* Try RSA authentication for each identity. */
1.27 markus 1493: for (i = 0; i < options.num_identity_files; i++)
1494: if (try_rsa_authentication(pw, options.identity_files[i]))
1.1 deraadt 1495: return; /* Successful connection. */
1496: }
1497:
1498: /* Try password authentication if the server supports it. */
1499: if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1.27 markus 1500: options.password_authentication && !options.batch_mode)
1.1 deraadt 1501: {
1.4 provos 1502: char prompt[80];
1503: snprintf(prompt, sizeof(prompt), "%.30s@%.30s's password: ",
1504: server_user, host);
1.1 deraadt 1505: debug("Doing password authentication.");
1.27 markus 1506: if (options.cipher == SSH_CIPHER_NONE)
1.1 deraadt 1507: log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
1.27 markus 1508: for (i = 0; i < options.number_of_password_prompts; i++) {
1.18 markus 1509: if (i != 0)
1510: error("Permission denied, please try again.");
1.16 dugsong 1511: password = read_passphrase(prompt, 0);
1512: packet_start(SSH_CMSG_AUTH_PASSWORD);
1513: packet_put_string(password, strlen(password));
1514: memset(password, 0, strlen(password));
1515: xfree(password);
1516: packet_send();
1517: packet_write_wait();
1518:
1519: type = packet_read(&payload_len);
1520: if (type == SSH_SMSG_SUCCESS)
1521: return; /* Successful connection. */
1522: if (type != SSH_SMSG_FAILURE)
1523: packet_disconnect("Protocol error: got %d in response to passwd auth", type);
1524: }
1.1 deraadt 1525: }
1526:
1527: /* All authentication methods have failed. Exit with an error message. */
1528: fatal("Permission denied.");
1529: /*NOTREACHED*/
1530: }