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