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