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