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