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