Annotation of src/usr.bin/ssh/ssh.c, Revision 1.40
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.40 ! markus 14: RCSID("$Id: ssh.c,v 1.39 2000/01/04 13:41:32 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));
1.40 ! markus 492: hints.ai_family = IPv4or6;
1.37 markus 493: hints.ai_flags = AI_CANONNAME;
1.39 markus 494: hints.ai_socktype = SOCK_STREAM;
1.37 markus 495: errgai = getaddrinfo(host, NULL, &hints, &ai);
496: if (errgai == 0) {
497: if (ai->ai_canonname != NULL)
498: host = xstrdup(ai->ai_canonname);
499: freeaddrinfo(ai);
1.31 markus 500: }
501: }
502: /* Disable rhosts authentication if not running as root. */
503: if (original_effective_uid != 0 || !options.use_privileged_port) {
504: options.rhosts_authentication = 0;
505: options.rhosts_rsa_authentication = 0;
506: }
1.33 markus 507: /*
508: * If using rsh has been selected, exec it now (without trying
509: * anything else). Note that we must release privileges first.
510: */
1.31 markus 511: if (options.use_rsh) {
1.33 markus 512: /*
513: * Restore our superuser privileges. This must be done
514: * before permanently setting the uid.
515: */
1.31 markus 516: restore_uid();
517:
518: /* Switch to the original uid permanently. */
519: permanently_set_uid(original_real_uid);
520:
521: /* Execute rsh. */
522: rsh_connect(host, options.user, &command);
523: fatal("rsh_connect returned");
524: }
525: /* Restore our superuser privileges. */
526: restore_uid();
527:
1.33 markus 528: /*
529: * Open a connection to the remote host. This needs root privileges
530: * if rhosts_{rsa_}authentication is enabled.
531: */
1.31 markus 532:
533: ok = ssh_connect(host, &hostaddr, options.port,
534: options.connection_attempts,
535: !options.rhosts_authentication &&
536: !options.rhosts_rsa_authentication,
537: original_real_uid,
538: options.proxy_command);
539:
1.33 markus 540: /*
541: * If we successfully made the connection, load the host private key
542: * in case we will need it later for combined rsa-rhosts
543: * authentication. This must be done before releasing extra
544: * privileges, because the file is only readable by root.
545: */
1.31 markus 546: if (ok) {
547: host_private_key = RSA_new();
548: if (load_private_key(HOST_KEY_FILE, "", host_private_key, NULL))
549: host_private_key_loaded = 1;
550: }
1.33 markus 551: /*
552: * Get rid of any extra privileges that we may have. We will no
553: * longer need them. Also, extra privileges could make it very hard
554: * to read identity files and other non-world-readable files from the
555: * user's home directory if it happens to be on a NFS volume where
556: * root is mapped to nobody.
557: */
558:
559: /*
560: * Note that some legacy systems need to postpone the following call
561: * to permanently_set_uid() until the private hostkey is destroyed
562: * with RSA_free(). Otherwise the calling user could ptrace() the
563: * process, read the private hostkey and impersonate the host.
564: * OpenBSD does not allow ptracing of setuid processes.
565: */
1.31 markus 566: permanently_set_uid(original_real_uid);
567:
1.33 markus 568: /*
569: * Now that we are back to our own permissions, create ~/.ssh
570: * directory if it doesn\'t already exist.
571: */
1.31 markus 572: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR);
573: if (stat(buf, &st) < 0)
574: if (mkdir(buf, 0755) < 0)
575: error("Could not create directory '%.200s'.", buf);
576:
577: /* Check if the connection failed, and try "rsh" if appropriate. */
578: if (!ok) {
579: if (options.port != 0)
1.35 markus 580: log("Secure connection to %.100s on port %hu refused%.100s.",
1.31 markus 581: host, options.port,
582: options.fallback_to_rsh ? "; reverting to insecure method" : "");
583: else
584: log("Secure connection to %.100s refused%.100s.", host,
585: options.fallback_to_rsh ? "; reverting to insecure method" : "");
586:
587: if (options.fallback_to_rsh) {
588: rsh_connect(host, options.user, &command);
589: fatal("rsh_connect returned");
590: }
591: exit(1);
592: }
593: /* Expand ~ in options.identity_files. */
594: for (i = 0; i < options.num_identity_files; i++)
595: options.identity_files[i] =
596: tilde_expand_filename(options.identity_files[i], original_real_uid);
597:
598: /* Expand ~ in known host file names. */
599: options.system_hostfile = tilde_expand_filename(options.system_hostfile,
600: original_real_uid);
601: options.user_hostfile = tilde_expand_filename(options.user_hostfile,
602: original_real_uid);
603:
604: /* Log into the remote system. This never returns if the login fails. */
605: ssh_login(host_private_key_loaded, host_private_key,
1.37 markus 606: host, (struct sockaddr *)&hostaddr, original_real_uid);
1.31 markus 607:
608: /* We no longer need the host private key. Clear it now. */
609: if (host_private_key_loaded)
610: RSA_free(host_private_key); /* Destroys contents safely */
611:
612: /* Close connection cleanly after attack. */
613: cipher_attack_detected = packet_disconnect;
614:
615: /* Enable compression if requested. */
616: if (options.compression) {
617: debug("Requesting compression at level %d.", options.compression_level);
618:
619: if (options.compression_level < 1 || options.compression_level > 9)
620: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
621:
622: /* Send the request. */
623: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
624: packet_put_int(options.compression_level);
625: packet_send();
626: packet_write_wait();
627: type = packet_read(&plen);
628: if (type == SSH_SMSG_SUCCESS)
629: packet_start_compression(options.compression_level);
630: else if (type == SSH_SMSG_FAILURE)
631: log("Warning: Remote host refused compression.");
632: else
633: packet_disconnect("Protocol error waiting for compression response.");
634: }
635: /* Allocate a pseudo tty if appropriate. */
636: if (tty_flag) {
637: debug("Requesting pty.");
638:
639: /* Start the packet. */
640: packet_start(SSH_CMSG_REQUEST_PTY);
641:
642: /* Store TERM in the packet. There is no limit on the
643: length of the string. */
644: cp = getenv("TERM");
645: if (!cp)
646: cp = "";
647: packet_put_string(cp, strlen(cp));
648:
649: /* Store window size in the packet. */
650: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
651: memset(&ws, 0, sizeof(ws));
652: packet_put_int(ws.ws_row);
653: packet_put_int(ws.ws_col);
654: packet_put_int(ws.ws_xpixel);
655: packet_put_int(ws.ws_ypixel);
656:
657: /* Store tty modes in the packet. */
658: tty_make_modes(fileno(stdin));
659:
660: /* Send the packet, and wait for it to leave. */
661: packet_send();
662: packet_write_wait();
663:
664: /* Read response from the server. */
665: type = packet_read(&plen);
666: if (type == SSH_SMSG_SUCCESS)
667: interactive = 1;
668: else if (type == SSH_SMSG_FAILURE)
669: log("Warning: Remote host failed or refused to allocate a pseudo tty.");
670: else
671: packet_disconnect("Protocol error waiting for pty request response.");
672: }
673: /* Request X11 forwarding if enabled and DISPLAY is set. */
674: if (options.forward_x11 && getenv("DISPLAY") != NULL) {
675: char line[512], proto[512], data[512];
676: FILE *f;
677: int forwarded = 0, got_data = 0, i;
1.1 deraadt 678:
679: #ifdef XAUTH_PATH
1.31 markus 680: /* Try to get Xauthority information for the display. */
681: snprintf(line, sizeof line, "%.100s list %.200s 2>/dev/null",
682: XAUTH_PATH, getenv("DISPLAY"));
683: f = popen(line, "r");
684: if (f && fgets(line, sizeof(line), f) &&
685: sscanf(line, "%*s %s %s", proto, data) == 2)
686: got_data = 1;
687: if (f)
688: pclose(f);
1.1 deraadt 689: #endif /* XAUTH_PATH */
1.33 markus 690: /*
691: * If we didn't get authentication data, just make up some
692: * data. The forwarding code will check the validity of the
693: * response anyway, and substitute this data. The X11
694: * server, however, will ignore this fake data and use
695: * whatever authentication mechanisms it was using otherwise
696: * for the local connection.
697: */
1.31 markus 698: if (!got_data) {
699: u_int32_t rand = 0;
700:
701: strlcpy(proto, "MIT-MAGIC-COOKIE-1", sizeof proto);
702: for (i = 0; i < 16; i++) {
703: if (i % 4 == 0)
704: rand = arc4random();
705: snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", rand & 0xff);
706: rand >>= 8;
707: }
708: }
1.33 markus 709: /*
710: * Got local authentication reasonable information. Request
711: * forwarding with authentication spoofing.
712: */
1.31 markus 713: debug("Requesting X11 forwarding with authentication spoofing.");
714: x11_request_forwarding_with_spoofing(proto, data);
715:
716: /* Read response from the server. */
717: type = packet_read(&plen);
718: if (type == SSH_SMSG_SUCCESS) {
719: forwarded = 1;
720: interactive = 1;
721: } else if (type == SSH_SMSG_FAILURE)
722: log("Warning: Remote host denied X11 forwarding.");
723: else
724: packet_disconnect("Protocol error waiting for X11 forwarding");
725: }
726: /* Tell the packet module whether this is an interactive session. */
727: packet_set_interactive(interactive, options.keepalives);
728:
729: /* Clear agent forwarding if we don\'t have an agent. */
730: authfd = ssh_get_authentication_socket();
731: if (authfd < 0)
732: options.forward_agent = 0;
733: else
734: ssh_close_authentication_socket(authfd);
735:
736: /* Request authentication agent forwarding if appropriate. */
737: if (options.forward_agent) {
738: debug("Requesting authentication agent forwarding.");
739: auth_request_forwarding();
740:
741: /* Read response from the server. */
742: type = packet_read(&plen);
743: packet_integrity_check(plen, 0, type);
744: if (type != SSH_SMSG_SUCCESS)
745: log("Warning: Remote host denied authentication agent forwarding.");
746: }
747: /* Initiate local TCP/IP port forwardings. */
748: for (i = 0; i < options.num_local_forwards; i++) {
749: debug("Connections to local port %d forwarded to remote address %.200s:%d",
750: options.local_forwards[i].port,
751: options.local_forwards[i].host,
752: options.local_forwards[i].host_port);
753: channel_request_local_forwarding(options.local_forwards[i].port,
754: options.local_forwards[i].host,
1.36 markus 755: options.local_forwards[i].host_port,
756: options.gateway_ports);
1.31 markus 757: }
758:
759: /* Initiate remote TCP/IP port forwardings. */
760: for (i = 0; i < options.num_remote_forwards; i++) {
761: debug("Connections to remote port %d forwarded to local address %.200s:%d",
762: options.remote_forwards[i].port,
763: options.remote_forwards[i].host,
764: options.remote_forwards[i].host_port);
765: channel_request_remote_forwarding(options.remote_forwards[i].port,
766: options.remote_forwards[i].host,
767: options.remote_forwards[i].host_port);
768: }
1.34 markus 769:
770: /* If requested, let ssh continue in the background. */
771: if (fork_after_authentication_flag)
772: if (daemon(1, 1) < 0)
773: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 774:
1.33 markus 775: /*
776: * If a command was specified on the command line, execute the
777: * command now. Otherwise request the server to start a shell.
778: */
1.31 markus 779: if (buffer_len(&command) > 0) {
780: int len = buffer_len(&command);
781: if (len > 900)
782: len = 900;
783: debug("Sending command: %.*s", len, buffer_ptr(&command));
784: packet_start(SSH_CMSG_EXEC_CMD);
785: packet_put_string(buffer_ptr(&command), buffer_len(&command));
786: packet_send();
787: packet_write_wait();
788: } else {
789: debug("Requesting shell.");
790: packet_start(SSH_CMSG_EXEC_SHELL);
791: packet_send();
792: packet_write_wait();
793: }
794:
795: /* Enter the interactive session. */
796: exit_status = client_loop(tty_flag, tty_flag ? options.escape_char : -1);
797:
798: /* Close the connection to the remote host. */
799: packet_close();
800:
801: /* Exit with the status returned by the program on the remote side. */
802: exit(exit_status);
1.1 deraadt 803: }