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