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