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