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