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