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