Annotation of src/usr.bin/ssh/ssh.c, Revision 1.47
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.47 ! markus 14: RCSID("$Id: ssh.c,v 1.46 2000/04/12 07:45:44 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.38 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;
! 273:
1.37 markus 274: case '4':
275: IPv4or6 = AF_INET;
276: break;
277:
278: case '6':
279: IPv4or6 = AF_INET6;
280: break;
281:
1.31 markus 282: case 'n':
283: stdin_null_flag = 1;
284: break;
285:
286: case 'f':
287: fork_after_authentication_flag = 1;
288: stdin_null_flag = 1;
289: break;
290:
291: case 'x':
292: options.forward_x11 = 0;
293: break;
294:
295: case 'X':
296: options.forward_x11 = 1;
297: break;
298:
299: case 'g':
300: options.gateway_ports = 1;
301: break;
302:
303: case 'P':
304: options.use_privileged_port = 0;
305: break;
306:
307: case 'a':
308: options.forward_agent = 0;
309: break;
1.9 dugsong 310: #ifdef AFS
1.31 markus 311: case 'k':
312: options.kerberos_tgt_passing = 0;
313: options.afs_token_passing = 0;
314: break;
1.1 deraadt 315: #endif
1.31 markus 316: case 'i':
317: if (stat(optarg, &st) < 0) {
318: fprintf(stderr, "Warning: Identity file %s does not exist.\n",
319: optarg);
320: break;
321: }
322: if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
323: fatal("Too many identity files specified (max %d)",
324: SSH_MAX_IDENTITY_FILES);
325: options.identity_files[options.num_identity_files++] =
326: xstrdup(optarg);
327: break;
328:
329: case 't':
330: tty_flag = 1;
331: break;
332:
333: case 'v':
334: case 'V':
1.46 markus 335: fprintf(stderr, "SSH Version %s, protocol versions %d.%d/%d.%d.\n",
336: SSH_VERSION,
337: PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
338: PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
1.45 markus 339: fprintf(stderr, "Compiled with SSL (0x%8.8lx).\n", SSLeay());
1.31 markus 340: if (opt == 'V')
341: exit(0);
342: debug_flag = 1;
343: options.log_level = SYSLOG_LEVEL_DEBUG;
344: break;
345:
346: case 'q':
347: options.log_level = SYSLOG_LEVEL_QUIET;
348: break;
349:
350: case 'e':
351: if (optarg[0] == '^' && optarg[2] == 0 &&
352: (unsigned char) optarg[1] >= 64 && (unsigned char) optarg[1] < 128)
353: options.escape_char = (unsigned char) optarg[1] & 31;
354: else if (strlen(optarg) == 1)
355: options.escape_char = (unsigned char) optarg[0];
356: else if (strcmp(optarg, "none") == 0)
357: options.escape_char = -2;
358: else {
359: fprintf(stderr, "Bad escape character '%s'.\n", optarg);
360: exit(1);
361: }
362: break;
363:
364: case 'c':
365: options.cipher = cipher_number(optarg);
366: if (options.cipher == -1) {
367: fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
368: exit(1);
369: }
370: break;
371:
372: case 'p':
373: options.port = atoi(optarg);
374: break;
375:
376: case 'l':
377: options.user = optarg;
378: break;
379:
380: case 'R':
1.37 markus 381: if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
382: &fwd_host_port) != 3 &&
383: sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
384: &fwd_host_port) != 3) {
1.31 markus 385: fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
386: usage();
387: /* NOTREACHED */
388: }
389: add_remote_forward(&options, fwd_port, buf, fwd_host_port);
390: break;
391:
392: case 'L':
1.37 markus 393: if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
394: &fwd_host_port) != 3 &&
395: sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
396: &fwd_host_port) != 3) {
1.31 markus 397: fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
398: usage();
399: /* NOTREACHED */
400: }
401: add_local_forward(&options, fwd_port, buf, fwd_host_port);
402: break;
403:
404: case 'C':
405: options.compression = 1;
406: break;
407:
1.45 markus 408: case 'N':
409: no_shell_flag = 1;
410: no_tty_flag = 1;
411: break;
412:
413: case 'T':
414: no_tty_flag = 1;
415: break;
416:
1.31 markus 417: case 'o':
418: dummy = 1;
419: if (process_config_line(&options, host ? host : "", optarg,
420: "command-line", 0, &dummy) != 0)
421: exit(1);
422: break;
423:
424: default:
425: usage();
1.1 deraadt 426: }
1.31 markus 427: }
428:
429: /* Check that we got a host name. */
430: if (!host)
431: usage();
432:
433: /* check if RSA support exists */
434: if (rsa_alive() == 0) {
435: extern char *__progname;
436:
437: fprintf(stderr,
438: "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
439: __progname);
440: exit(1);
441: }
442: /* Initialize the command to execute on remote host. */
443: buffer_init(&command);
1.1 deraadt 444:
1.33 markus 445: /*
446: * Save the command to execute on the remote host in a buffer. There
447: * is no limit on the length of the command, except by the maximum
448: * packet size. Also sets the tty flag if there is no command.
449: */
1.31 markus 450: if (optind == ac) {
451: /* No command specified - execute shell on a tty. */
452: tty_flag = 1;
453: } else {
454: /* A command has been specified. Store it into the
455: buffer. */
456: for (i = optind; i < ac; i++) {
457: if (i > optind)
458: buffer_append(&command, " ", 1);
459: buffer_append(&command, av[i], strlen(av[i]));
460: }
461: }
462:
463: /* Cannot fork to background if no command. */
464: if (fork_after_authentication_flag && buffer_len(&command) == 0)
465: fatal("Cannot fork into background without a command to execute.");
466:
467: /* Allocate a tty by default if no command specified. */
468: if (buffer_len(&command) == 0)
469: tty_flag = 1;
470:
471: /* Do not allocate a tty if stdin is not a tty. */
472: if (!isatty(fileno(stdin))) {
473: if (tty_flag)
474: fprintf(stderr, "Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
475: tty_flag = 0;
476: }
1.45 markus 477: /* force */
478: if (no_tty_flag)
479: tty_flag = 0;
480:
1.31 markus 481: /* Get user data. */
482: pw = getpwuid(original_real_uid);
483: if (!pw) {
484: fprintf(stderr, "You don't exist, go away!\n");
485: exit(1);
486: }
487: /* Take a copy of the returned structure. */
488: memset(&pwcopy, 0, sizeof(pwcopy));
489: pwcopy.pw_name = xstrdup(pw->pw_name);
490: pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
491: pwcopy.pw_uid = pw->pw_uid;
492: pwcopy.pw_gid = pw->pw_gid;
493: pwcopy.pw_dir = xstrdup(pw->pw_dir);
494: pwcopy.pw_shell = xstrdup(pw->pw_shell);
495: pw = &pwcopy;
496:
497: /* Initialize "log" output. Since we are the client all output
498: actually goes to the terminal. */
499: log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
500:
501: /* Read per-user configuration file. */
502: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_CONFFILE);
503: read_config_file(buf, host, &options);
504:
505: /* Read systemwide configuration file. */
506: read_config_file(HOST_CONFIG_FILE, host, &options);
507:
508: /* Fill configuration defaults. */
509: fill_default_options(&options);
510:
511: /* reinit */
512: log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
513:
514: if (options.user == NULL)
515: options.user = xstrdup(pw->pw_name);
516:
517: if (options.hostname != NULL)
518: host = options.hostname;
519:
520: /* Find canonic host name. */
521: if (strchr(host, '.') == 0) {
1.37 markus 522: struct addrinfo hints;
523: struct addrinfo *ai = NULL;
524: int errgai;
525: memset(&hints, 0, sizeof(hints));
1.40 markus 526: hints.ai_family = IPv4or6;
1.37 markus 527: hints.ai_flags = AI_CANONNAME;
1.39 markus 528: hints.ai_socktype = SOCK_STREAM;
1.37 markus 529: errgai = getaddrinfo(host, NULL, &hints, &ai);
530: if (errgai == 0) {
531: if (ai->ai_canonname != NULL)
532: host = xstrdup(ai->ai_canonname);
533: freeaddrinfo(ai);
1.31 markus 534: }
535: }
536: /* Disable rhosts authentication if not running as root. */
537: if (original_effective_uid != 0 || !options.use_privileged_port) {
538: options.rhosts_authentication = 0;
539: options.rhosts_rsa_authentication = 0;
540: }
1.33 markus 541: /*
542: * If using rsh has been selected, exec it now (without trying
543: * anything else). Note that we must release privileges first.
544: */
1.31 markus 545: if (options.use_rsh) {
1.33 markus 546: /*
547: * Restore our superuser privileges. This must be done
548: * before permanently setting the uid.
549: */
1.31 markus 550: restore_uid();
551:
552: /* Switch to the original uid permanently. */
553: permanently_set_uid(original_real_uid);
554:
555: /* Execute rsh. */
556: rsh_connect(host, options.user, &command);
557: fatal("rsh_connect returned");
558: }
559: /* Restore our superuser privileges. */
560: restore_uid();
561:
1.33 markus 562: /*
563: * Open a connection to the remote host. This needs root privileges
564: * if rhosts_{rsa_}authentication is enabled.
565: */
1.31 markus 566:
567: ok = ssh_connect(host, &hostaddr, options.port,
568: options.connection_attempts,
569: !options.rhosts_authentication &&
570: !options.rhosts_rsa_authentication,
571: original_real_uid,
572: options.proxy_command);
573:
1.33 markus 574: /*
575: * If we successfully made the connection, load the host private key
576: * in case we will need it later for combined rsa-rhosts
577: * authentication. This must be done before releasing extra
578: * privileges, because the file is only readable by root.
579: */
1.31 markus 580: if (ok) {
581: host_private_key = RSA_new();
582: if (load_private_key(HOST_KEY_FILE, "", host_private_key, NULL))
583: host_private_key_loaded = 1;
584: }
1.33 markus 585: /*
586: * Get rid of any extra privileges that we may have. We will no
587: * longer need them. Also, extra privileges could make it very hard
588: * to read identity files and other non-world-readable files from the
589: * user's home directory if it happens to be on a NFS volume where
590: * root is mapped to nobody.
591: */
592:
593: /*
594: * Note that some legacy systems need to postpone the following call
595: * to permanently_set_uid() until the private hostkey is destroyed
596: * with RSA_free(). Otherwise the calling user could ptrace() the
597: * process, read the private hostkey and impersonate the host.
598: * OpenBSD does not allow ptracing of setuid processes.
599: */
1.31 markus 600: permanently_set_uid(original_real_uid);
601:
1.33 markus 602: /*
603: * Now that we are back to our own permissions, create ~/.ssh
604: * directory if it doesn\'t already exist.
605: */
1.31 markus 606: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR);
607: if (stat(buf, &st) < 0)
608: if (mkdir(buf, 0755) < 0)
609: error("Could not create directory '%.200s'.", buf);
610:
611: /* Check if the connection failed, and try "rsh" if appropriate. */
612: if (!ok) {
613: if (options.port != 0)
1.35 markus 614: log("Secure connection to %.100s on port %hu refused%.100s.",
1.31 markus 615: host, options.port,
616: options.fallback_to_rsh ? "; reverting to insecure method" : "");
617: else
618: log("Secure connection to %.100s refused%.100s.", host,
619: options.fallback_to_rsh ? "; reverting to insecure method" : "");
620:
621: if (options.fallback_to_rsh) {
622: rsh_connect(host, options.user, &command);
623: fatal("rsh_connect returned");
624: }
625: exit(1);
626: }
627: /* Expand ~ in options.identity_files. */
628: for (i = 0; i < options.num_identity_files; i++)
629: options.identity_files[i] =
630: tilde_expand_filename(options.identity_files[i], original_real_uid);
631:
632: /* Expand ~ in known host file names. */
633: options.system_hostfile = tilde_expand_filename(options.system_hostfile,
634: original_real_uid);
635: options.user_hostfile = tilde_expand_filename(options.user_hostfile,
636: original_real_uid);
637:
638: /* Log into the remote system. This never returns if the login fails. */
639: ssh_login(host_private_key_loaded, host_private_key,
1.37 markus 640: host, (struct sockaddr *)&hostaddr, original_real_uid);
1.31 markus 641:
642: /* We no longer need the host private key. Clear it now. */
643: if (host_private_key_loaded)
644: RSA_free(host_private_key); /* Destroys contents safely */
645:
1.45 markus 646: exit_status = compat20 ? ssh_session2() : ssh_session();
647: packet_close();
648: return exit_status;
649: }
650:
651: int
652: ssh_session(void)
653: {
654: int type;
655: int i;
656: int plen;
657: int interactive = 0;
658: int have_tty = 0;
659: struct winsize ws;
660: int authfd;
661: char *cp;
662:
1.31 markus 663: /* Enable compression if requested. */
664: if (options.compression) {
665: debug("Requesting compression at level %d.", options.compression_level);
666:
667: if (options.compression_level < 1 || options.compression_level > 9)
668: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
669:
670: /* Send the request. */
671: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
672: packet_put_int(options.compression_level);
673: packet_send();
674: packet_write_wait();
675: type = packet_read(&plen);
676: if (type == SSH_SMSG_SUCCESS)
677: packet_start_compression(options.compression_level);
678: else if (type == SSH_SMSG_FAILURE)
679: log("Warning: Remote host refused compression.");
680: else
681: packet_disconnect("Protocol error waiting for compression response.");
682: }
683: /* Allocate a pseudo tty if appropriate. */
684: if (tty_flag) {
685: debug("Requesting pty.");
686:
687: /* Start the packet. */
688: packet_start(SSH_CMSG_REQUEST_PTY);
689:
690: /* Store TERM in the packet. There is no limit on the
691: length of the string. */
692: cp = getenv("TERM");
693: if (!cp)
694: cp = "";
695: packet_put_string(cp, strlen(cp));
696:
697: /* Store window size in the packet. */
698: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
699: memset(&ws, 0, sizeof(ws));
700: packet_put_int(ws.ws_row);
701: packet_put_int(ws.ws_col);
702: packet_put_int(ws.ws_xpixel);
703: packet_put_int(ws.ws_ypixel);
704:
705: /* Store tty modes in the packet. */
706: tty_make_modes(fileno(stdin));
707:
708: /* Send the packet, and wait for it to leave. */
709: packet_send();
710: packet_write_wait();
711:
712: /* Read response from the server. */
713: type = packet_read(&plen);
1.43 markus 714: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 715: interactive = 1;
1.45 markus 716: have_tty = 1;
1.43 markus 717: } else if (type == SSH_SMSG_FAILURE)
1.31 markus 718: log("Warning: Remote host failed or refused to allocate a pseudo tty.");
719: else
720: packet_disconnect("Protocol error waiting for pty request response.");
721: }
722: /* Request X11 forwarding if enabled and DISPLAY is set. */
723: if (options.forward_x11 && getenv("DISPLAY") != NULL) {
724: char line[512], proto[512], data[512];
725: FILE *f;
726: int forwarded = 0, got_data = 0, i;
1.1 deraadt 727:
728: #ifdef XAUTH_PATH
1.31 markus 729: /* Try to get Xauthority information for the display. */
730: snprintf(line, sizeof line, "%.100s list %.200s 2>/dev/null",
731: XAUTH_PATH, getenv("DISPLAY"));
732: f = popen(line, "r");
733: if (f && fgets(line, sizeof(line), f) &&
734: sscanf(line, "%*s %s %s", proto, data) == 2)
735: got_data = 1;
736: if (f)
737: pclose(f);
1.1 deraadt 738: #endif /* XAUTH_PATH */
1.33 markus 739: /*
740: * If we didn't get authentication data, just make up some
741: * data. The forwarding code will check the validity of the
742: * response anyway, and substitute this data. The X11
743: * server, however, will ignore this fake data and use
744: * whatever authentication mechanisms it was using otherwise
745: * for the local connection.
746: */
1.31 markus 747: if (!got_data) {
748: u_int32_t rand = 0;
749:
750: strlcpy(proto, "MIT-MAGIC-COOKIE-1", sizeof proto);
751: for (i = 0; i < 16; i++) {
752: if (i % 4 == 0)
753: rand = arc4random();
754: snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", rand & 0xff);
755: rand >>= 8;
756: }
757: }
1.33 markus 758: /*
759: * Got local authentication reasonable information. Request
760: * forwarding with authentication spoofing.
761: */
1.31 markus 762: debug("Requesting X11 forwarding with authentication spoofing.");
763: x11_request_forwarding_with_spoofing(proto, data);
764:
765: /* Read response from the server. */
766: type = packet_read(&plen);
767: if (type == SSH_SMSG_SUCCESS) {
768: forwarded = 1;
769: interactive = 1;
770: } else if (type == SSH_SMSG_FAILURE)
771: log("Warning: Remote host denied X11 forwarding.");
772: else
773: packet_disconnect("Protocol error waiting for X11 forwarding");
774: }
775: /* Tell the packet module whether this is an interactive session. */
776: packet_set_interactive(interactive, options.keepalives);
777:
778: /* Clear agent forwarding if we don\'t have an agent. */
779: authfd = ssh_get_authentication_socket();
780: if (authfd < 0)
781: options.forward_agent = 0;
782: else
783: ssh_close_authentication_socket(authfd);
784:
785: /* Request authentication agent forwarding if appropriate. */
786: if (options.forward_agent) {
787: debug("Requesting authentication agent forwarding.");
788: auth_request_forwarding();
789:
790: /* Read response from the server. */
791: type = packet_read(&plen);
792: packet_integrity_check(plen, 0, type);
793: if (type != SSH_SMSG_SUCCESS)
794: log("Warning: Remote host denied authentication agent forwarding.");
795: }
796: /* Initiate local TCP/IP port forwardings. */
797: for (i = 0; i < options.num_local_forwards; i++) {
798: debug("Connections to local port %d forwarded to remote address %.200s:%d",
799: options.local_forwards[i].port,
800: options.local_forwards[i].host,
801: options.local_forwards[i].host_port);
802: channel_request_local_forwarding(options.local_forwards[i].port,
803: options.local_forwards[i].host,
1.36 markus 804: options.local_forwards[i].host_port,
805: options.gateway_ports);
1.31 markus 806: }
807:
808: /* Initiate remote TCP/IP port forwardings. */
809: for (i = 0; i < options.num_remote_forwards; i++) {
810: debug("Connections to remote port %d forwarded to local address %.200s:%d",
811: options.remote_forwards[i].port,
812: options.remote_forwards[i].host,
813: options.remote_forwards[i].host_port);
814: channel_request_remote_forwarding(options.remote_forwards[i].port,
815: options.remote_forwards[i].host,
816: options.remote_forwards[i].host_port);
817: }
1.34 markus 818:
819: /* If requested, let ssh continue in the background. */
820: if (fork_after_authentication_flag)
821: if (daemon(1, 1) < 0)
822: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 823:
1.33 markus 824: /*
825: * If a command was specified on the command line, execute the
826: * command now. Otherwise request the server to start a shell.
827: */
1.31 markus 828: if (buffer_len(&command) > 0) {
829: int len = buffer_len(&command);
830: if (len > 900)
831: len = 900;
832: debug("Sending command: %.*s", len, buffer_ptr(&command));
833: packet_start(SSH_CMSG_EXEC_CMD);
834: packet_put_string(buffer_ptr(&command), buffer_len(&command));
835: packet_send();
836: packet_write_wait();
837: } else {
838: debug("Requesting shell.");
839: packet_start(SSH_CMSG_EXEC_SHELL);
840: packet_send();
841: packet_write_wait();
842: }
843:
844: /* Enter the interactive session. */
1.45 markus 845: return client_loop(have_tty, tty_flag ? options.escape_char : -1);
846: }
847:
848: void
849: init_local_fwd(void)
850: {
851: int i;
852: /* Initiate local TCP/IP port forwardings. */
853: for (i = 0; i < options.num_local_forwards; i++) {
854: debug("Connections to local port %d forwarded to remote address %.200s:%d",
855: options.local_forwards[i].port,
856: options.local_forwards[i].host,
857: options.local_forwards[i].host_port);
858: channel_request_local_forwarding(options.local_forwards[i].port,
859: options.local_forwards[i].host,
860: options.local_forwards[i].host_port,
861: options.gateway_ports);
862: }
863: }
864:
865: extern void client_set_session_ident(int id);
866:
867: void
868: client_init(int id, void *arg)
869: {
870: int len;
871: debug("client_init id %d arg %d", id, (int)arg);
1.31 markus 872:
1.45 markus 873: if (no_shell_flag)
874: goto done;
875:
876: if (tty_flag) {
877: struct winsize ws;
878: char *cp;
879: cp = getenv("TERM");
880: if (!cp)
881: cp = "";
882: /* Store window size in the packet. */
883: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
884: memset(&ws, 0, sizeof(ws));
885:
886: channel_request_start(id, "pty-req", 0);
887: packet_put_cstring(cp);
888: packet_put_int(ws.ws_col);
889: packet_put_int(ws.ws_row);
890: packet_put_int(ws.ws_xpixel);
891: packet_put_int(ws.ws_ypixel);
892: packet_put_cstring(""); /* XXX: encode terminal modes */
893: packet_send();
894: /* XXX wait for reply */
895: }
896: len = buffer_len(&command);
897: if (len > 0) {
898: if (len > 900)
899: len = 900;
900: debug("Sending command: %.*s", len, buffer_ptr(&command));
901: channel_request_start(id, "exec", 0);
902: packet_put_string(buffer_ptr(&command), len);
903: packet_send();
904: } else {
905: channel_request(id, "shell", 0);
906: }
907: /* channel_callback(id, SSH2_MSG_OPEN_CONFIGMATION, client_init, 0); */
908: done:
909: /* register different callback, etc. XXX */
910: client_set_session_ident(id);
911: }
912:
913: int
914: ssh_session2(void)
915: {
916: int window, packetmax, id;
917: int in = dup(STDIN_FILENO);
918: int out = dup(STDOUT_FILENO);
919: int err = dup(STDERR_FILENO);
920:
921: if (in < 0 || out < 0 || err < 0)
922: fatal("dump in/out/err failed");
923:
924: /* should be pre-session */
925: init_local_fwd();
926:
927: window = 32*1024;
928: if (tty_flag) {
929: packetmax = window/8;
930: } else {
931: window *= 2;
932: packetmax = window/2;
933: }
934:
935: id = channel_new(
936: "session", SSH_CHANNEL_OPENING, in, out, err,
937: window, packetmax, CHAN_EXTENDED_WRITE, xstrdup("client-session"));
938:
939:
940: channel_open(id);
941: channel_register_callback(id, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, client_init, (void *)0);
1.31 markus 942:
1.45 markus 943: return client_loop(tty_flag, tty_flag ? options.escape_char : -1);
1.1 deraadt 944: }