Annotation of src/usr.bin/ssh/ssh.c, Revision 1.17
1.1 deraadt 1: /*
2:
3: ssh.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 16:36:11 1995 ylo
11:
12: Ssh client program. This program can be used to log into a remote machine.
13: The software supports strong authentication, encryption, and forwarding
14: of X11, TCP/IP, and authentication connections.
15:
1.2 provos 16: Modified to work with SSL by Niels Provos <provos@citi.umich.edu> in Canada.
17:
1.1 deraadt 18: */
19:
20: #include "includes.h"
1.17 ! deraadt 21: RCSID("$Id: ssh.c,v 1.16 1999/09/30 05:11:29 deraadt Exp $");
1.1 deraadt 22:
23: #include "xmalloc.h"
24: #include "ssh.h"
25: #include "packet.h"
26: #include "buffer.h"
27: #include "authfd.h"
28: #include "readconf.h"
29: #include "uidswap.h"
30:
31: /* Flag indicating whether debug mode is on. This can be set on the
32: command line. */
33: int debug_flag = 0;
34:
35: /* Flag indicating whether quiet mode is on. */
36: int quiet_flag = 0;
37:
38: /* Flag indicating whether to allocate a pseudo tty. This can be set on the
39: command line, and is automatically set if no command is given on the command
40: line. */
41: int tty_flag = 0;
42:
43: /* Flag indicating that nothing should be read from stdin. This can be set
44: on the command line. */
45: int stdin_null_flag = 0;
46:
47: /* Flag indicating that ssh should fork after authentication. This is useful
48: so that the pasphrase can be entered manually, and then ssh goes to the
49: background. */
50: int fork_after_authentication_flag = 0;
51:
52: /* General data structure for command line options and options configurable
53: in configuration files. See readconf.h. */
54: Options options;
55:
56: /* Name of the host we are connecting to. This is the name given on the
57: command line, or the HostName specified for the user-supplied name
58: in a configuration file. */
59: char *host;
60:
61: #ifdef SIGWINCH
62: /* Flag to indicate that we have received a window change signal which has
63: not yet been processed. This will cause a message indicating the new
64: window size to be sent to the server a little later. This is volatile
65: because this is updated in a signal handler. */
66: volatile int received_window_change_signal = 0;
67: #endif /* SIGWINCH */
68:
69: /* Value of argv[0] (set in the main program). */
70: char *av0;
71:
72: /* Flag indicating whether we have a valid host private key loaded. */
73: int host_private_key_loaded = 0;
74:
75: /* Host private key. */
1.2 provos 76: RSA *host_private_key = NULL;
1.1 deraadt 77:
1.10 dugsong 78: /* Original real UID. */
79: uid_t original_real_uid;
1.1 deraadt 80:
81: /* Prints a help message to the user. This function never returns. */
82:
1.2 provos 83: void
84: usage()
1.1 deraadt 85: {
86: int i;
87:
88: fprintf(stderr, "Usage: %s [options] host [command]\n", av0);
89: fprintf(stderr, "Options:\n");
90: fprintf(stderr, " -l user Log in using this user name.\n");
91: fprintf(stderr, " -n Redirect input from /dev/null.\n");
1.7 provos 92: fprintf(stderr, " -a Disable authentication agent forwarding.\n");
1.9 dugsong 93: #ifdef AFS
94: fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n");
95: #endif /* AFS */
1.1 deraadt 96: fprintf(stderr, " -x Disable X11 connection forwarding.\n");
97: fprintf(stderr, " -i file Identity for RSA authentication (default: ~/.ssh/identity).\n");
98: fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n");
99: fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
1.8 provos 100: fprintf(stderr, " -V Display version number only.\n");
1.1 deraadt 101: fprintf(stderr, " -q Quiet; don't display any warning messages.\n");
102: fprintf(stderr, " -f Fork into background after authentication.\n");
103: fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n");
104:
105: fprintf(stderr, " -c cipher Select encryption algorithm: ");
106: for (i = 1; i <= 6; i++)
107: {
108: const char *t = cipher_name(i);
109: if (t[0] == 'n' && t[1] == 'o' && t[2] == ' ')
110: continue;
111: fprintf(stderr, "%s, ", t);
112: }
113: fprintf(stderr, "or none.\n");
114:
115: fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n");
1.4 deraadt 116: fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n");
1.1 deraadt 117: fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n");
118: fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n");
119: fprintf(stderr, " These cause %s to listen for connections on a port, and\n", av0);
120: fprintf(stderr, " forward them to the other side by connecting to host:port.\n");
121: fprintf(stderr, " -C Enable compression.\n");
122: fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n");
123: exit(1);
124: }
125:
126: /* Connects to the given host using rsh (or prints an error message and exits
127: if rsh is not available). This function never returns. */
128:
1.2 provos 129: void
130: rsh_connect(char *host, char *user, Buffer *command)
1.1 deraadt 131: {
132: char *args[10];
133: int i;
134:
135: log("Using rsh. WARNING: Connection will not be encrypted.");
136: /* Build argument list for rsh. */
137: i = 0;
1.13 deraadt 138: args[i++] = _PATH_RSH;
1.1 deraadt 139: args[i++] = host; /* may have to come after user on some systems */
140: if (user)
141: {
142: args[i++] = "-l";
143: args[i++] = user;
144: }
145: if (buffer_len(command) > 0)
146: {
147: buffer_append(command, "\0", 1);
148: args[i++] = buffer_ptr(command);
149: }
150: args[i++] = NULL;
151: if (debug_flag)
152: {
153: for (i = 0; args[i]; i++)
154: {
155: if (i != 0)
156: fprintf(stderr, " ");
157: fprintf(stderr, "%s", args[i]);
158: }
159: fprintf(stderr, "\n");
160: }
1.13 deraadt 161: execv(_PATH_RSH, args);
162: perror(_PATH_RSH);
1.1 deraadt 163: exit(1);
164: }
165:
166: /* Main program for the ssh client. */
167:
1.11 deraadt 168: uid_t original_real_uid;
169:
1.2 provos 170: int
171: main(int ac, char **av)
1.1 deraadt 172: {
173: int i, opt, optind, type, exit_status, ok, fwd_port, fwd_host_port, authfd;
174: char *optarg, *cp, buf[256];
175: Buffer command;
176: struct winsize ws;
177: struct stat st;
178: struct passwd *pw, pwcopy;
179: int interactive = 0, dummy;
180: uid_t original_effective_uid;
181: int plen;
182:
183: /* Save the original real uid. It will be needed later (uid-swapping may
184: clobber the real uid). */
185: original_real_uid = getuid();
186: original_effective_uid = geteuid();
187:
188: /* If we are installed setuid root be careful to not drop core. */
189: if (original_real_uid != original_effective_uid)
190: {
191: struct rlimit rlim;
192: rlim.rlim_cur = rlim.rlim_max = 0;
193: if (setrlimit(RLIMIT_CORE, &rlim) < 0)
194: fatal("setrlimit failed: %.100s", strerror(errno));
195: }
196:
197: /* Use uid-swapping to give up root privileges for the duration of option
198: processing. We will re-instantiate the rights when we are ready to
199: create the privileged port, and will permanently drop them when the
200: port has been created (actually, when the connection has been made, as
201: we may need to create the port several times). */
202: temporarily_use_uid(original_real_uid);
203:
204: /* Set our umask to something reasonable, as some files are created with
205: the default umask. This will make them world-readable but writable
206: only by the owner, which is ok for all files for which we don't set
207: the modes explicitly. */
208: umask(022);
209:
210: /* Save our own name. */
211: av0 = av[0];
212:
213: /* Initialize option structure to indicate that no values have been set. */
214: initialize_options(&options);
215:
216: /* Parse command-line arguments. */
217: host = NULL;
218:
219: /* If program name is not one of the standard names, use it as host name. */
220: if (strchr(av0, '/'))
221: cp = strrchr(av0, '/') + 1;
222: else
223: cp = av0;
224: if (strcmp(cp, "rsh") != 0 && strcmp(cp, "ssh") != 0 &&
225: strcmp(cp, "rlogin") != 0 && strcmp(cp, "slogin") != 0)
226: host = cp;
227:
228: for (optind = 1; optind < ac; optind++)
229: {
230: if (av[optind][0] != '-')
231: {
232: if (host)
233: break;
1.3 deraadt 234: if ((cp = strchr(av[optind], '@'))) {
235: options.user = av[optind];
236: *cp = '\0';
237: host = ++cp;
238: }
239: else
240: host = av[optind];
1.1 deraadt 241: continue;
242: }
243: opt = av[optind][1];
244: if (!opt)
245: usage();
246: if (strchr("eilcpLRo", opt)) /* options with arguments */
247: {
248: optarg = av[optind] + 2;
249: if (strcmp(optarg, "") == 0)
250: {
251: if (optind >= ac - 1)
252: usage();
253: optarg = av[++optind];
254: }
255: }
256: else
257: {
258: if (av[optind][2])
259: usage();
260: optarg = NULL;
261: }
262: switch (opt)
263: {
264: case 'n':
265: stdin_null_flag = 1;
266: break;
267:
268: case 'f':
269: fork_after_authentication_flag = 1;
270: stdin_null_flag = 1;
271: break;
272:
273: case 'x':
274: options.forward_x11 = 0;
275: break;
276:
277: case 'X':
278: options.forward_x11 = 1;
1.4 deraadt 279: break;
280:
281: case 'g':
282: options.gateway_ports = 1;
1.1 deraadt 283: break;
284:
285: case 'a':
286: options.forward_agent = 0;
1.9 dugsong 287: break;
288: #ifdef AFS
289: case 'k':
1.1 deraadt 290: options.kerberos_tgt_passing = 0;
291: options.afs_token_passing = 0;
1.9 dugsong 292: break;
1.1 deraadt 293: #endif
294: case 'i':
295: if (stat(optarg, &st) < 0)
296: {
297: fprintf(stderr, "Warning: Identity file %s does not exist.\n",
298: optarg);
299: break;
300: }
301: if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
302: fatal("Too many identity files specified (max %d)",
303: SSH_MAX_IDENTITY_FILES);
304: options.identity_files[options.num_identity_files++] =
305: xstrdup(optarg);
306: break;
307:
308: case 't':
309: tty_flag = 1;
310: break;
311:
312: case 'v':
1.6 deraadt 313: case 'V':
1.1 deraadt 314: debug_flag = 1;
315: fprintf(stderr, "SSH Version %s, protocol version %d.%d.\n",
316: SSH_VERSION, PROTOCOL_MAJOR, PROTOCOL_MINOR);
317: fprintf(stderr, "Compiled with SSL.\n");
1.6 deraadt 318: if (opt == 'V')
319: exit(0);
1.1 deraadt 320: break;
321:
322: case 'q':
323: quiet_flag = 1;
324: break;
325:
326: case 'e':
327: if (optarg[0] == '^' && optarg[2] == 0 &&
328: (unsigned char)optarg[1] >= 64 && (unsigned char)optarg[1] < 128)
329: options.escape_char = (unsigned char)optarg[1] & 31;
330: else
331: if (strlen(optarg) == 1)
332: options.escape_char = (unsigned char)optarg[0];
333: else
334: if (strcmp(optarg, "none") == 0)
335: options.escape_char = -2;
336: else
337: {
338: fprintf(stderr, "Bad escape character '%s'.\n", optarg);
339: exit(1);
340: }
341: break;
342:
343: case 'c':
344: options.cipher = cipher_number(optarg);
345: if (options.cipher == -1)
346: {
347: fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
348: exit(1);
349: }
350: break;
351:
352: case 'p':
353: options.port = atoi(optarg);
354: if (options.port < 1 || options.port > 65535)
355: {
356: fprintf(stderr, "Bad port %s.\n", optarg);
357: exit(1);
358: }
359: break;
360:
361: case 'l':
362: options.user = optarg;
363: break;
364:
365: case 'R':
366: if (sscanf(optarg, "%d:%255[^:]:%d", &fwd_port, buf,
367: &fwd_host_port) != 3)
368: {
369: fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
370: usage();
371: /*NOTREACHED*/
372: }
373: add_remote_forward(&options, fwd_port, buf, fwd_host_port);
374: break;
375:
376: case 'L':
377: if (sscanf(optarg, "%d:%255[^:]:%d", &fwd_port, buf,
378: &fwd_host_port) != 3)
379: {
380: fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
381: usage();
382: /*NOTREACHED*/
383: }
384: add_local_forward(&options, fwd_port, buf, fwd_host_port);
385: break;
386:
387: case 'C':
388: options.compression = 1;
389: break;
390:
391: case 'o':
392: dummy = 1;
393: process_config_line(&options, host ? host : "", optarg,
394: "command-line", 0, &dummy);
395: break;
396:
397: default:
398: usage();
399: }
400: }
401:
402: /* Check that we got a host name. */
403: if (!host)
404: usage();
1.5 deraadt 405:
406: /* check if RSA support exists */
407: if (rsa_alive() == 0) {
408: extern char *__progname;
409:
410: fprintf(stderr,
411: "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
412: __progname);
413: exit(1);
414: }
1.1 deraadt 415:
416: /* Initialize the command to execute on remote host. */
417: buffer_init(&command);
418:
419: /* Save the command to execute on the remote host in a buffer. There is
420: no limit on the length of the command, except by the maximum packet
421: size. Also sets the tty flag if there is no command. */
422: if (optind == ac)
423: {
424: /* No command specified - execute shell on a tty. */
425: tty_flag = 1;
426: }
427: else
428: {
429: /* A command has been specified. Store it into the buffer. */
430: for (i = optind; i < ac; i++)
431: {
432: if (i > optind)
433: buffer_append(&command, " ", 1);
434: buffer_append(&command, av[i], strlen(av[i]));
435: }
436: }
437:
438: /* Cannot fork to background if no command. */
439: if (fork_after_authentication_flag && buffer_len(&command) == 0)
440: fatal("Cannot fork into background without a command to execute.");
441:
442: /* Allocate a tty by default if no command specified. */
443: if (buffer_len(&command) == 0)
444: tty_flag = 1;
445:
446: /* Do not allocate a tty if stdin is not a tty. */
447: if (!isatty(fileno(stdin)))
448: {
449: if (tty_flag)
450: fprintf(stderr, "Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
451: tty_flag = 0;
452: }
453:
454: /* Get user data. */
455: pw = getpwuid(original_real_uid);
456: if (!pw)
457: {
458: fprintf(stderr, "You don't exist, go away!\n");
459: exit(1);
460: }
461:
462: /* Take a copy of the returned structure. */
463: memset(&pwcopy, 0, sizeof(pwcopy));
464: pwcopy.pw_name = xstrdup(pw->pw_name);
465: pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
466: pwcopy.pw_uid = pw->pw_uid;
467: pwcopy.pw_gid = pw->pw_gid;
468: pwcopy.pw_dir = xstrdup(pw->pw_dir);
469: pwcopy.pw_shell = xstrdup(pw->pw_shell);
470: pw = &pwcopy;
471:
472: /* Initialize "log" output. Since we are the client all output actually
473: goes to the terminal. */
474: log_init(av[0], 1, debug_flag, quiet_flag, SYSLOG_FACILITY_USER);
475:
476: /* Read per-user configuration file. */
1.11 deraadt 477: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_CONFFILE);
1.1 deraadt 478: read_config_file(buf, host, &options);
479:
480: /* Read systemwide configuration file. */
481: read_config_file(HOST_CONFIG_FILE, host, &options);
482:
483: /* Fill configuration defaults. */
484: fill_default_options(&options);
485: if (options.user == NULL)
486: options.user = xstrdup(pw->pw_name);
487:
488: if (options.hostname != NULL)
489: host = options.hostname;
490:
491: /* Find canonic host name. */
492: if (strchr(host, '.') == 0)
493: {
494: struct hostent *hp = gethostbyname(host);
495: if (hp != 0)
496: {
497: if (strchr(hp->h_name, '.') != 0)
498: host = xstrdup(hp->h_name);
499: else if (hp->h_aliases != 0
500: && hp->h_aliases[0] != 0
501: && strchr(hp->h_aliases[0], '.') != 0)
502: host = xstrdup(hp->h_aliases[0]);
503: }
504: }
505:
506: /* Disable rhosts authentication if not running as root. */
507: if (original_effective_uid != 0)
508: {
509: options.rhosts_authentication = 0;
510: options.rhosts_rsa_authentication = 0;
511: }
512:
513: /* If using rsh has been selected, exec it now (without trying anything
514: else). Note that we must release privileges first. */
515: if (options.use_rsh)
516: {
517: /* Restore our superuser privileges. This must be done before
518: permanently setting the uid. */
519: restore_uid();
520:
521: /* Switch to the original uid permanently. */
522: permanently_set_uid(original_real_uid);
523:
524: /* Execute rsh. */
525: rsh_connect(host, options.user, &command);
526: fatal("rsh_connect returned");
527: }
528:
529: /* Restore our superuser privileges. */
530: restore_uid();
531:
532: /* Open a connection to the remote host. This needs root privileges if
1.2 provos 533: rhosts_authentication is true. */
1.1 deraadt 534: ok = ssh_connect(host, options.port, options.connection_attempts,
535: !options.rhosts_authentication &&
536: !options.rhosts_rsa_authentication,
1.2 provos 537: original_real_uid, options.proxy_command);
1.1 deraadt 538:
539: /* If we successfully made the connection, load the host private key in
540: case we will need it later for combined rsa-rhosts authentication.
541: This must be done before releasing extra privileges, because the file
542: is only readable by root. */
543: if (ok)
544: {
1.2 provos 545: host_private_key = RSA_new();
546: if (load_private_key(HOST_KEY_FILE, "", host_private_key, NULL))
1.1 deraadt 547: host_private_key_loaded = 1;
548: }
549:
550: /* Get rid of any extra privileges that we may have. We will no longer need
551: them. Also, extra privileges could make it very hard to read identity
552: files and other non-world-readable files from the user's home directory
553: if it happens to be on a NFS volume where root is mapped to nobody. */
554: permanently_set_uid(original_real_uid);
555:
556: /* Now that we are back to our own permissions, create ~/.ssh directory
557: if it doesn\'t already exist. */
1.11 deraadt 558: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR);
1.1 deraadt 559: if (stat(buf, &st) < 0)
560: if (mkdir(buf, 0755) < 0)
561: error("Could not create directory '%.200s'.", buf);
562:
563: /* Check if the connection failed, and try "rsh" if appropriate. */
564: if (!ok)
565: {
566: if (options.port != 0)
567: log("Secure connection to %.100s on port %d refused%.100s.",
568: host, options.port,
569: options.fallback_to_rsh ? "; reverting to insecure method" : "");
570: else
571: log("Secure connection to %.100s refused%.100s.", host,
572: options.fallback_to_rsh ? "; reverting to insecure method" : "");
573:
574: if (options.fallback_to_rsh)
575: {
576: rsh_connect(host, options.user, &command);
577: fatal("rsh_connect returned");
578: }
579: exit(1);
580: }
581:
582: /* Expand ~ in options.identity_files. */
583: for (i = 0; i < options.num_identity_files; i++)
584: options.identity_files[i] =
585: tilde_expand_filename(options.identity_files[i], original_real_uid);
586:
587: /* Expand ~ in known host file names. */
588: options.system_hostfile = tilde_expand_filename(options.system_hostfile,
589: original_real_uid);
590: options.user_hostfile = tilde_expand_filename(options.user_hostfile,
591: original_real_uid);
592:
1.2 provos 593: /* Log into the remote system. This never returns if the login fails. */
594: ssh_login(host_private_key_loaded, host_private_key,
1.1 deraadt 595: host, &options, original_real_uid);
596:
597: /* We no longer need the host private key. Clear it now. */
598: if (host_private_key_loaded)
1.2 provos 599: RSA_free(host_private_key); /* Destroys contents safely */
1.1 deraadt 600:
601: /* Close connection cleanly after attack. */
602: cipher_attack_detected = packet_disconnect;
603:
604: /* If requested, fork and let ssh continue in the background. */
605: if (fork_after_authentication_flag)
606: {
607: int ret = fork();
608: if (ret == -1)
609: fatal("fork failed: %.100s", strerror(errno));
610: if (ret != 0)
611: exit(0);
612: setsid();
613: }
614:
615: /* Enable compression if requested. */
616: if (options.compression)
617: {
618: debug("Requesting compression at level %d.", options.compression_level);
619:
620: if (options.compression_level < 1 || options.compression_level > 9)
621: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
622:
623: /* Send the request. */
624: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
625: packet_put_int(options.compression_level);
626: packet_send();
627: packet_write_wait();
628: type = packet_read(&plen);
629: if (type == SSH_SMSG_SUCCESS)
630: packet_start_compression(options.compression_level);
631: else if (type == SSH_SMSG_FAILURE)
632: log("Warning: Remote host refused compression.");
633: else
634: packet_disconnect("Protocol error waiting for compression response.");
635: }
636:
637: /* Allocate a pseudo tty if appropriate. */
638: if (tty_flag)
639: {
640: debug("Requesting pty.");
641:
642: /* Start the packet. */
643: packet_start(SSH_CMSG_REQUEST_PTY);
644:
645: /* Store TERM in the packet. There is no limit on the length of the
646: string. */
647: cp = getenv("TERM");
648: if (!cp)
649: cp = "";
650: packet_put_string(cp, strlen(cp));
651:
652: /* Store window size in the packet. */
653: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
654: memset(&ws, 0, sizeof(ws));
655: packet_put_int(ws.ws_row);
656: packet_put_int(ws.ws_col);
657: packet_put_int(ws.ws_xpixel);
658: packet_put_int(ws.ws_ypixel);
659:
660: /* Store tty modes in the packet. */
661: tty_make_modes(fileno(stdin));
662:
663: /* Send the packet, and wait for it to leave. */
664: packet_send();
665: packet_write_wait();
666:
667: /* Read response from the server. */
668: type = packet_read(&plen);
669: if (type == SSH_SMSG_SUCCESS)
670: interactive = 1;
671: else if (type == SSH_SMSG_FAILURE)
672: log("Warning: Remote host failed or refused to allocate a pseudo tty.");
673: else
674: packet_disconnect("Protocol error waiting for pty request response.");
675: }
676:
677: /* Request X11 forwarding if enabled and DISPLAY is set. */
678: if (options.forward_x11 && getenv("DISPLAY") != NULL)
679: {
680: char line[512], proto[512], data[512];
681: FILE *f;
682: int forwarded = 0, got_data = 0, i;
683:
684: #ifdef XAUTH_PATH
685: /* Try to get Xauthority information for the display. */
1.11 deraadt 686: snprintf(line, sizeof line, "%.100s list %.200s 2>/dev/null",
1.1 deraadt 687: XAUTH_PATH, getenv("DISPLAY"));
688: f = popen(line, "r");
689: if (f && fgets(line, sizeof(line), f) &&
690: sscanf(line, "%*s %s %s", proto, data) == 2)
691: got_data = 1;
692: if (f)
693: pclose(f);
694: #endif /* XAUTH_PATH */
695: /* If we didn't get authentication data, just make up some data. The
696: forwarding code will check the validity of the response anyway, and
697: substitute this data. The X11 server, however, will ignore this
698: fake data and use whatever authentication mechanisms it was using
699: otherwise for the local connection. */
700: if (!got_data)
701: {
1.9 dugsong 702: u_int32_t rand = 0;
1.2 provos 703:
1.11 deraadt 704: strlcpy(proto, "MIT-MAGIC-COOKIE-1", sizeof proto);
1.2 provos 705: for (i = 0; i < 16; i++) {
706: if (i % 4 == 0)
707: rand = arc4random();
1.11 deraadt 708: snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", rand & 0xff);
1.2 provos 709: rand >>= 8;
710: }
1.1 deraadt 711: }
712:
713: /* Got local authentication reasonable information. Request forwarding
714: with authentication spoofing. */
715: debug("Requesting X11 forwarding with authentication spoofing.");
1.2 provos 716: x11_request_forwarding_with_spoofing(proto, data);
1.1 deraadt 717:
718: /* Read response from the server. */
719: type = packet_read(&plen);
720: if (type == SSH_SMSG_SUCCESS)
721: {
722: forwarded = 1;
723: interactive = 1;
724: }
725: else if (type == SSH_SMSG_FAILURE)
726: log("Warning: Remote host denied X11 forwarding.");
727: else
728: packet_disconnect("Protocol error waiting for X11 forwarding");
729: }
730:
731: /* Tell the packet module whether this is an interactive session. */
732: packet_set_interactive(interactive, options.keepalives);
733:
734: /* Clear agent forwarding if we don\'t have an agent. */
735: authfd = ssh_get_authentication_fd();
736: if (authfd < 0)
737: options.forward_agent = 0;
738: else
739: ssh_close_authentication_socket(authfd);
740:
741: /* Request authentication agent forwarding if appropriate. */
742: if (options.forward_agent)
743: {
744: debug("Requesting authentication agent forwarding.");
745: auth_request_forwarding();
746:
747: /* Read response from the server. */
748: type = packet_read(&plen);
749: packet_integrity_check(plen, 0, type);
750: if (type != SSH_SMSG_SUCCESS)
751: log("Warning: Remote host denied authentication agent forwarding.");
752: }
753:
754: /* Initiate local TCP/IP port forwardings. */
755: for (i = 0; i < options.num_local_forwards; i++)
756: {
757: debug("Connections to local port %d forwarded to remote address %.200s:%d",
758: options.local_forwards[i].port, options.local_forwards[i].host,
759: options.local_forwards[i].host_port);
760: channel_request_local_forwarding(options.local_forwards[i].port,
761: options.local_forwards[i].host,
762: options.local_forwards[i].host_port);
763: }
764:
765: /* Initiate remote TCP/IP port forwardings. */
766: for (i = 0; i < options.num_remote_forwards; i++)
767: {
768: debug("Connections to remote port %d forwarded to local address %.200s:%d",
769: options.remote_forwards[i].port, options.remote_forwards[i].host,
770: options.remote_forwards[i].host_port);
771: channel_request_remote_forwarding(options.remote_forwards[i].port,
772: options.remote_forwards[i].host,
773: options.remote_forwards[i].host_port);
774: }
775:
776: /* If a command was specified on the command line, execute the command now.
777: Otherwise request the server to start a shell. */
778: if (buffer_len(&command) > 0)
779: {
780: int len = buffer_len(&command);
781: if (len > 900)
782: len = 900;
783: debug("Sending command: %.*s", len, buffer_ptr(&command));
784: packet_start(SSH_CMSG_EXEC_CMD);
785: packet_put_string(buffer_ptr(&command), buffer_len(&command));
786: packet_send();
787: packet_write_wait();
788: }
789: else
790: {
791: debug("Requesting shell.");
792: packet_start(SSH_CMSG_EXEC_SHELL);
793: packet_send();
794: packet_write_wait();
795: }
796:
797: /* Enter the interactive session. */
798: exit_status = client_loop(tty_flag, tty_flag ? options.escape_char : -1);
799:
800: /* Close the connection to the remote host. */
801: packet_close();
802:
803: /* Exit with the status returned by the program on the remote side. */
804: exit(exit_status);
805: }