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