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