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