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