Annotation of src/usr.bin/ssh/ssh.c, Revision 1.107
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: * Ssh client program. This program can be used to log into a remote machine.
6: * The software supports strong authentication, encryption, and forwarding
7: * of X11, TCP/IP, and authentication connections.
8: *
1.64 deraadt 9: * As far as I am concerned, the code I have written for this software
10: * can be used freely for any purpose. Any derived versions of this
11: * software must be clearly marked as such, and if the derived work is
12: * incompatible with the protocol description in the RFC file, it must be
13: * called by a name other than "ssh" or "Secure Shell".
14: *
15: * Copyright (c) 1999 Niels Provos. All rights reserved.
16: *
17: * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
18: * in Canada (German citizen).
19: *
20: * Redistribution and use in source and binary forms, with or without
21: * modification, are permitted provided that the following conditions
22: * are met:
23: * 1. Redistributions of source code must retain the above copyright
24: * notice, this list of conditions and the following disclaimer.
25: * 2. Redistributions in binary form must reproduce the above copyright
26: * notice, this list of conditions and the following disclaimer in the
27: * documentation and/or other materials provided with the distribution.
28: *
29: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.32 deraadt 39: */
1.1 deraadt 40:
41: #include "includes.h"
1.107 ! markus 42: RCSID("$OpenBSD: ssh.c,v 1.106 2001/04/05 21:05:24 markus Exp $");
1.49 markus 43:
44: #include <openssl/evp.h>
1.72 markus 45: #include <openssl/err.h>
1.1 deraadt 46:
1.84 markus 47: #include "ssh.h"
48: #include "ssh1.h"
49: #include "ssh2.h"
50: #include "compat.h"
51: #include "cipher.h"
1.1 deraadt 52: #include "xmalloc.h"
53: #include "packet.h"
54: #include "buffer.h"
55: #include "uidswap.h"
1.44 markus 56: #include "channels.h"
1.49 markus 57: #include "key.h"
1.58 markus 58: #include "authfd.h"
1.49 markus 59: #include "authfile.h"
1.83 markus 60: #include "pathnames.h"
1.81 markus 61: #include "clientloop.h"
1.84 markus 62: #include "log.h"
63: #include "readconf.h"
64: #include "sshconnect.h"
65: #include "tildexpand.h"
1.89 markus 66: #include "dispatch.h"
1.84 markus 67: #include "misc.h"
1.95 markus 68: #include "kex.h"
69: #include "mac.h"
1.49 markus 70:
71: extern char *__progname;
1.1 deraadt 72:
1.37 markus 73: /* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
74: Default value is AF_UNSPEC means both IPv4 and IPv6. */
75: int IPv4or6 = AF_UNSPEC;
76:
1.31 markus 77: /* Flag indicating whether debug mode is on. This can be set on the command line. */
1.1 deraadt 78: int debug_flag = 0;
79:
1.46 markus 80: /* Flag indicating whether a tty should be allocated */
1.1 deraadt 81: int tty_flag = 0;
1.79 markus 82: int no_tty_flag = 0;
83: int force_tty_flag = 0;
1.1 deraadt 84:
1.45 markus 85: /* don't exec a shell */
86: int no_shell_flag = 0;
87:
1.33 markus 88: /*
89: * Flag indicating that nothing should be read from stdin. This can be set
90: * on the command line.
91: */
1.1 deraadt 92: int stdin_null_flag = 0;
93:
1.33 markus 94: /*
95: * Flag indicating that ssh should fork after authentication. This is useful
96: * so that the pasphrase can be entered manually, and then ssh goes to the
97: * background.
98: */
1.1 deraadt 99: int fork_after_authentication_flag = 0;
100:
1.33 markus 101: /*
102: * General data structure for command line options and options configurable
103: * in configuration files. See readconf.h.
104: */
1.1 deraadt 105: Options options;
106:
1.33 markus 107: /*
108: * Name of the host we are connecting to. This is the name given on the
109: * command line, or the HostName specified for the user-supplied name in a
110: * configuration file.
111: */
1.1 deraadt 112: char *host;
113:
1.22 provos 114: /* socket address the host resolves to */
1.37 markus 115: struct sockaddr_storage hostaddr;
1.22 provos 116:
1.33 markus 117: /*
118: * Flag to indicate that we have received a window change signal which has
119: * not yet been processed. This will cause a message indicating the new
120: * window size to be sent to the server a little later. This is volatile
121: * because this is updated in a signal handler.
122: */
1.1 deraadt 123: volatile int received_window_change_signal = 0;
124:
125: /* Host private key. */
1.105 markus 126: Key *host_private_key = NULL;
1.1 deraadt 127:
1.10 dugsong 128: /* Original real UID. */
129: uid_t original_real_uid;
1.1 deraadt 130:
1.45 markus 131: /* command to be executed */
132: Buffer command;
133:
1.85 djm 134: /* Should we execute a command or invoke a subsystem? */
135: int subsystem_flag = 0;
136:
1.1 deraadt 137: /* Prints a help message to the user. This function never returns. */
138:
1.2 provos 139: void
1.93 itojun 140: usage(void)
1.1 deraadt 141: {
1.76 markus 142: fprintf(stderr, "Usage: %s [options] host [command]\n", __progname);
1.31 markus 143: fprintf(stderr, "Options:\n");
144: fprintf(stderr, " -l user Log in using this user name.\n");
1.93 itojun 145: fprintf(stderr, " -n Redirect input from " _PATH_DEVNULL ".\n");
1.53 markus 146: fprintf(stderr, " -A Enable authentication agent forwarding.\n");
1.31 markus 147: fprintf(stderr, " -a Disable authentication agent forwarding.\n");
1.9 dugsong 148: #ifdef AFS
1.31 markus 149: fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n");
150: #endif /* AFS */
1.88 stevesk 151: fprintf(stderr, " -X Enable X11 connection forwarding.\n");
1.31 markus 152: fprintf(stderr, " -x Disable X11 connection forwarding.\n");
1.99 deraadt 153: fprintf(stderr, " -i file Identity for public key authentication "
154: "(default: ~/.ssh/identity)\n");
1.31 markus 155: fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n");
1.45 markus 156: fprintf(stderr, " -T Do not allocate a tty.\n");
1.31 markus 157: fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
1.66 markus 158: fprintf(stderr, " Multiple -v increases verbosity.\n");
1.31 markus 159: fprintf(stderr, " -V Display version number only.\n");
160: fprintf(stderr, " -P Don't allocate a privileged port.\n");
161: fprintf(stderr, " -q Quiet; don't display any warning messages.\n");
162: fprintf(stderr, " -f Fork into background after authentication.\n");
163: fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n");
164:
165: fprintf(stderr, " -c cipher Select encryption algorithm: "
1.99 deraadt 166: "``3des'', ``blowfish''\n");
1.102 stevesk 167: fprintf(stderr, " -m macs Specify MAC algorithms for protocol version 2.\n");
1.31 markus 168: fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n");
169: fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n");
170: fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n");
1.76 markus 171: fprintf(stderr, " These cause %s to listen for connections on a port, and\n", __progname);
1.31 markus 172: fprintf(stderr, " forward them to the other side by connecting to host:port.\n");
173: fprintf(stderr, " -C Enable compression.\n");
1.45 markus 174: fprintf(stderr, " -N Do not execute a shell or command.\n");
1.31 markus 175: fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n");
1.92 jakob 176: fprintf(stderr, " -1 Force protocol version 1.\n");
177: fprintf(stderr, " -2 Force protocol version 2.\n");
1.37 markus 178: fprintf(stderr, " -4 Use IPv4 only.\n");
179: fprintf(stderr, " -6 Use IPv6 only.\n");
1.31 markus 180: fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n");
1.85 djm 181: fprintf(stderr, " -s Invoke command (mandatory) as SSH2 subsystem.\n");
1.31 markus 182: exit(1);
1.1 deraadt 183: }
184:
1.32 deraadt 185: /*
186: * Connects to the given host using rsh (or prints an error message and exits
187: * if rsh is not available). This function never returns.
188: */
1.2 provos 189: void
1.31 markus 190: rsh_connect(char *host, char *user, Buffer * command)
1.1 deraadt 191: {
1.31 markus 192: char *args[10];
193: int i;
194:
195: log("Using rsh. WARNING: Connection will not be encrypted.");
196: /* Build argument list for rsh. */
197: i = 0;
198: args[i++] = _PATH_RSH;
199: /* host may have to come after user on some systems */
200: args[i++] = host;
201: if (user) {
202: args[i++] = "-l";
203: args[i++] = user;
204: }
205: if (buffer_len(command) > 0) {
206: buffer_append(command, "\0", 1);
207: args[i++] = buffer_ptr(command);
208: }
209: args[i++] = NULL;
210: if (debug_flag) {
211: for (i = 0; args[i]; i++) {
212: if (i != 0)
213: fprintf(stderr, " ");
214: fprintf(stderr, "%s", args[i]);
215: }
216: fprintf(stderr, "\n");
217: }
218: execv(_PATH_RSH, args);
219: perror(_PATH_RSH);
220: exit(1);
1.1 deraadt 221: }
222:
1.72 markus 223: int ssh_session(void);
224: int ssh_session2(void);
1.104 markus 225: void load_public_identity_files(void);
1.45 markus 226:
1.32 deraadt 227: /*
228: * Main program for the ssh client.
229: */
1.2 provos 230: int
231: main(int ac, char **av)
1.1 deraadt 232: {
1.45 markus 233: int i, opt, optind, exit_status, ok;
1.35 markus 234: u_short fwd_port, fwd_host_port;
1.31 markus 235: char *optarg, *cp, buf[256];
236: struct stat st;
1.98 markus 237: struct passwd *pw;
1.45 markus 238: int dummy;
1.31 markus 239: uid_t original_effective_uid;
240:
1.33 markus 241: /*
242: * Save the original real uid. It will be needed later (uid-swapping
243: * may clobber the real uid).
244: */
1.31 markus 245: original_real_uid = getuid();
246: original_effective_uid = geteuid();
247:
248: /* If we are installed setuid root be careful to not drop core. */
249: if (original_real_uid != original_effective_uid) {
250: struct rlimit rlim;
251: rlim.rlim_cur = rlim.rlim_max = 0;
252: if (setrlimit(RLIMIT_CORE, &rlim) < 0)
253: fatal("setrlimit failed: %.100s", strerror(errno));
1.1 deraadt 254: }
1.107 ! markus 255: /* Get user data. */
! 256: pw = getpwuid(original_real_uid);
! 257: if (!pw) {
! 258: log("You don't exist, go away!");
! 259: exit(1);
! 260: }
! 261: /* Take a copy of the returned structure. */
! 262: pw = pwcopy(pw);
! 263:
1.33 markus 264: /*
265: * Use uid-swapping to give up root privileges for the duration of
266: * option processing. We will re-instantiate the rights when we are
267: * ready to create the privileged port, and will permanently drop
268: * them when the port has been created (actually, when the connection
269: * has been made, as we may need to create the port several times).
270: */
1.107 ! markus 271: temporarily_use_uid(pw);
1.31 markus 272:
1.33 markus 273: /*
274: * Set our umask to something reasonable, as some files are created
275: * with the default umask. This will make them world-readable but
276: * writable only by the owner, which is ok for all files for which we
277: * don't set the modes explicitly.
278: */
1.31 markus 279: umask(022);
280:
281: /* Initialize option structure to indicate that no values have been set. */
282: initialize_options(&options);
283:
284: /* Parse command-line arguments. */
285: host = NULL;
286:
287: for (optind = 1; optind < ac; optind++) {
288: if (av[optind][0] != '-') {
289: if (host)
290: break;
291: if ((cp = strchr(av[optind], '@'))) {
1.48 markus 292: if(cp == av[optind])
293: usage();
1.31 markus 294: options.user = av[optind];
295: *cp = '\0';
296: host = ++cp;
297: } else
298: host = av[optind];
299: continue;
300: }
301: opt = av[optind][1];
302: if (!opt)
303: usage();
1.95 markus 304: if (strchr("eilcmpLRo", opt)) { /* options with arguments */
1.31 markus 305: optarg = av[optind] + 2;
306: if (strcmp(optarg, "") == 0) {
307: if (optind >= ac - 1)
308: usage();
309: optarg = av[++optind];
310: }
311: } else {
312: if (av[optind][2])
313: usage();
314: optarg = NULL;
315: }
316: switch (opt) {
1.91 jakob 317: case '1':
318: options.protocol = SSH_PROTO_1;
319: break;
1.47 markus 320: case '2':
321: options.protocol = SSH_PROTO_2;
322: break;
1.37 markus 323: case '4':
324: IPv4or6 = AF_INET;
325: break;
326: case '6':
327: IPv4or6 = AF_INET6;
328: break;
1.31 markus 329: case 'n':
330: stdin_null_flag = 1;
331: break;
332: case 'f':
333: fork_after_authentication_flag = 1;
334: stdin_null_flag = 1;
335: break;
336: case 'x':
337: options.forward_x11 = 0;
338: break;
339: case 'X':
340: options.forward_x11 = 1;
341: break;
342: case 'g':
343: options.gateway_ports = 1;
344: break;
345: case 'P':
346: options.use_privileged_port = 0;
347: break;
348: case 'a':
349: options.forward_agent = 0;
1.53 markus 350: break;
351: case 'A':
352: options.forward_agent = 1;
1.31 markus 353: break;
1.9 dugsong 354: #ifdef AFS
1.31 markus 355: case 'k':
356: options.kerberos_tgt_passing = 0;
357: options.afs_token_passing = 0;
358: break;
1.1 deraadt 359: #endif
1.31 markus 360: case 'i':
361: if (stat(optarg, &st) < 0) {
362: fprintf(stderr, "Warning: Identity file %s does not exist.\n",
1.72 markus 363: optarg);
1.31 markus 364: break;
365: }
366: if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
367: fatal("Too many identity files specified (max %d)",
1.72 markus 368: SSH_MAX_IDENTITY_FILES);
369: options.identity_files[options.num_identity_files++] = xstrdup(optarg);
1.31 markus 370: break;
371: case 't':
1.79 markus 372: if (tty_flag)
373: force_tty_flag = 1;
1.31 markus 374: tty_flag = 1;
375: break;
376: case 'v':
1.66 markus 377: if (0 == debug_flag) {
378: debug_flag = 1;
379: options.log_level = SYSLOG_LEVEL_DEBUG1;
380: } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
381: options.log_level++;
382: break;
383: } else {
1.103 millert 384: fatal("Too high debugging level.");
1.66 markus 385: }
386: /* fallthrough */
1.31 markus 387: case 'V':
1.96 deraadt 388: fprintf(stderr,
389: "%s, SSH protocols %d.%d/%d.%d, OpenSSL 0x%8.8lx\n",
1.46 markus 390: SSH_VERSION,
391: PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
1.96 deraadt 392: PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2,
393: SSLeay());
1.31 markus 394: if (opt == 'V')
395: exit(0);
396: break;
397: case 'q':
398: options.log_level = SYSLOG_LEVEL_QUIET;
399: break;
400: case 'e':
401: if (optarg[0] == '^' && optarg[2] == 0 &&
1.78 markus 402: (u_char) optarg[1] >= 64 && (u_char) optarg[1] < 128)
403: options.escape_char = (u_char) optarg[1] & 31;
1.31 markus 404: else if (strlen(optarg) == 1)
1.78 markus 405: options.escape_char = (u_char) optarg[0];
1.31 markus 406: else if (strcmp(optarg, "none") == 0)
407: options.escape_char = -2;
408: else {
409: fprintf(stderr, "Bad escape character '%s'.\n", optarg);
410: exit(1);
411: }
412: break;
413: case 'c':
1.49 markus 414: if (ciphers_valid(optarg)) {
415: /* SSH2 only */
416: options.ciphers = xstrdup(optarg);
1.51 markus 417: options.cipher = SSH_CIPHER_ILLEGAL;
1.49 markus 418: } else {
419: /* SSH1 only */
1.74 markus 420: options.cipher = cipher_number(optarg);
421: if (options.cipher == -1) {
1.49 markus 422: fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
423: exit(1);
424: }
1.74 markus 425: if (options.cipher == SSH_CIPHER_3DES) {
426: options.ciphers = "3des-cbc";
427: } else if (options.cipher == SSH_CIPHER_BLOWFISH) {
428: options.ciphers = "blowfish-cbc";
429: } else {
430: options.ciphers = (char *)-1;
431: }
1.95 markus 432: }
433: break;
434: case 'm':
435: if (mac_valid(optarg))
436: options.macs = xstrdup(optarg);
437: else {
438: fprintf(stderr, "Unknown mac type '%s'\n", optarg);
439: exit(1);
1.31 markus 440: }
441: break;
442: case 'p':
443: options.port = atoi(optarg);
444: break;
445: case 'l':
446: options.user = optarg;
447: break;
448: case 'R':
1.37 markus 449: if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
450: &fwd_host_port) != 3 &&
451: sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
452: &fwd_host_port) != 3) {
1.31 markus 453: fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
454: usage();
455: /* NOTREACHED */
456: }
457: add_remote_forward(&options, fwd_port, buf, fwd_host_port);
458: break;
459: case 'L':
1.37 markus 460: if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
461: &fwd_host_port) != 3 &&
462: sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
463: &fwd_host_port) != 3) {
1.31 markus 464: fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
465: usage();
466: /* NOTREACHED */
467: }
468: add_local_forward(&options, fwd_port, buf, fwd_host_port);
469: break;
470: case 'C':
471: options.compression = 1;
472: break;
1.45 markus 473: case 'N':
474: no_shell_flag = 1;
475: no_tty_flag = 1;
476: break;
477: case 'T':
478: no_tty_flag = 1;
479: break;
1.31 markus 480: case 'o':
481: dummy = 1;
482: if (process_config_line(&options, host ? host : "", optarg,
483: "command-line", 0, &dummy) != 0)
484: exit(1);
485: break;
1.85 djm 486: case 's':
487: subsystem_flag = 1;
488: break;
1.31 markus 489: default:
490: usage();
1.1 deraadt 491: }
1.31 markus 492: }
493:
494: /* Check that we got a host name. */
495: if (!host)
496: usage();
497:
1.54 markus 498: SSLeay_add_all_algorithms();
1.72 markus 499: ERR_load_crypto_strings();
1.31 markus 500:
501: /* Initialize the command to execute on remote host. */
502: buffer_init(&command);
1.1 deraadt 503:
1.33 markus 504: /*
505: * Save the command to execute on the remote host in a buffer. There
506: * is no limit on the length of the command, except by the maximum
507: * packet size. Also sets the tty flag if there is no command.
508: */
1.31 markus 509: if (optind == ac) {
510: /* No command specified - execute shell on a tty. */
511: tty_flag = 1;
1.85 djm 512: if (subsystem_flag) {
513: fprintf(stderr, "You must specify a subsystem to invoke.");
514: usage();
515: }
1.31 markus 516: } else {
517: /* A command has been specified. Store it into the
518: buffer. */
519: for (i = optind; i < ac; i++) {
520: if (i > optind)
521: buffer_append(&command, " ", 1);
522: buffer_append(&command, av[i], strlen(av[i]));
523: }
524: }
525:
526: /* Cannot fork to background if no command. */
1.63 markus 527: if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
1.31 markus 528: fatal("Cannot fork into background without a command to execute.");
529:
530: /* Allocate a tty by default if no command specified. */
531: if (buffer_len(&command) == 0)
532: tty_flag = 1;
533:
1.75 markus 534: /* Force no tty*/
535: if (no_tty_flag)
536: tty_flag = 0;
1.31 markus 537: /* Do not allocate a tty if stdin is not a tty. */
1.79 markus 538: if (!isatty(fileno(stdin)) && !force_tty_flag) {
1.31 markus 539: if (tty_flag)
1.103 millert 540: log("Pseudo-terminal will not be allocated because stdin is not a terminal.");
1.31 markus 541: tty_flag = 0;
542: }
1.45 markus 543:
1.101 markus 544: /*
545: * Initialize "log" output. Since we are the client all output
546: * actually goes to stderr.
547: */
548: log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
1.31 markus 549:
550: /* Read per-user configuration file. */
1.83 markus 551: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, _PATH_SSH_USER_CONFFILE);
1.31 markus 552: read_config_file(buf, host, &options);
553:
554: /* Read systemwide configuration file. */
1.83 markus 555: read_config_file(_PATH_HOST_CONFIG_FILE, host, &options);
1.31 markus 556:
557: /* Fill configuration defaults. */
558: fill_default_options(&options);
559:
560: /* reinit */
1.101 markus 561: log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
1.31 markus 562:
563: if (options.user == NULL)
564: options.user = xstrdup(pw->pw_name);
565:
566: if (options.hostname != NULL)
567: host = options.hostname;
568:
569: /* Disable rhosts authentication if not running as root. */
570: if (original_effective_uid != 0 || !options.use_privileged_port) {
1.77 markus 571: debug("Rhosts Authentication disabled, "
1.71 markus 572: "originating port will not be trusted.");
1.31 markus 573: options.rhosts_authentication = 0;
574: }
1.33 markus 575: /*
576: * If using rsh has been selected, exec it now (without trying
577: * anything else). Note that we must release privileges first.
578: */
1.31 markus 579: if (options.use_rsh) {
1.33 markus 580: /*
581: * Restore our superuser privileges. This must be done
582: * before permanently setting the uid.
583: */
1.31 markus 584: restore_uid();
585:
586: /* Switch to the original uid permanently. */
1.107 ! markus 587: permanently_set_uid(pw);
1.31 markus 588:
589: /* Execute rsh. */
590: rsh_connect(host, options.user, &command);
591: fatal("rsh_connect returned");
592: }
593: /* Restore our superuser privileges. */
594: restore_uid();
595:
1.77 markus 596: /* Open a connection to the remote host. */
1.31 markus 597:
598: ok = ssh_connect(host, &hostaddr, options.port,
1.77 markus 599: options.connection_attempts,
600: original_effective_uid != 0 || !options.use_privileged_port,
1.107 ! markus 601: pw, options.proxy_command);
1.31 markus 602:
1.33 markus 603: /*
604: * If we successfully made the connection, load the host private key
605: * in case we will need it later for combined rsa-rhosts
606: * authentication. This must be done before releasing extra
607: * privileges, because the file is only readable by root.
608: */
1.49 markus 609: if (ok && (options.protocol & SSH_PROTO_1)) {
1.105 markus 610: host_private_key = key_load_private_type(KEY_RSA1,
611: _PATH_HOST_KEY_FILE, "", NULL);
1.31 markus 612: }
1.33 markus 613: /*
614: * Get rid of any extra privileges that we may have. We will no
615: * longer need them. Also, extra privileges could make it very hard
616: * to read identity files and other non-world-readable files from the
617: * user's home directory if it happens to be on a NFS volume where
618: * root is mapped to nobody.
619: */
620:
621: /*
622: * Note that some legacy systems need to postpone the following call
623: * to permanently_set_uid() until the private hostkey is destroyed
624: * with RSA_free(). Otherwise the calling user could ptrace() the
625: * process, read the private hostkey and impersonate the host.
626: * OpenBSD does not allow ptracing of setuid processes.
627: */
1.107 ! markus 628: permanently_set_uid(pw);
1.31 markus 629:
1.33 markus 630: /*
631: * Now that we are back to our own permissions, create ~/.ssh
632: * directory if it doesn\'t already exist.
633: */
1.83 markus 634: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, _PATH_SSH_USER_DIR);
1.31 markus 635: if (stat(buf, &st) < 0)
1.57 djm 636: if (mkdir(buf, 0700) < 0)
1.31 markus 637: error("Could not create directory '%.200s'.", buf);
638:
639: /* Check if the connection failed, and try "rsh" if appropriate. */
640: if (!ok) {
641: if (options.port != 0)
1.35 markus 642: log("Secure connection to %.100s on port %hu refused%.100s.",
1.31 markus 643: host, options.port,
644: options.fallback_to_rsh ? "; reverting to insecure method" : "");
645: else
646: log("Secure connection to %.100s refused%.100s.", host,
647: options.fallback_to_rsh ? "; reverting to insecure method" : "");
648:
649: if (options.fallback_to_rsh) {
650: rsh_connect(host, options.user, &command);
651: fatal("rsh_connect returned");
652: }
653: exit(1);
654: }
1.104 markus 655: /* load options.identity_files */
656: load_public_identity_files();
657:
658: /* Expand ~ in known host file names. */
659: /* XXX mem-leaks: */
1.72 markus 660: options.system_hostfile =
661: tilde_expand_filename(options.system_hostfile, original_real_uid);
662: options.user_hostfile =
663: tilde_expand_filename(options.user_hostfile, original_real_uid);
664: options.system_hostfile2 =
665: tilde_expand_filename(options.system_hostfile2, original_real_uid);
666: options.user_hostfile2 =
667: tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.31 markus 668:
669: /* Log into the remote system. This never returns if the login fails. */
1.107 ! markus 670: ssh_login(host_private_key, host, (struct sockaddr *)&hostaddr, pw);
1.31 markus 671:
672: /* We no longer need the host private key. Clear it now. */
1.105 markus 673: if (host_private_key != NULL)
674: key_free(host_private_key); /* Destroys contents safely */
1.31 markus 675:
1.45 markus 676: exit_status = compat20 ? ssh_session2() : ssh_session();
677: packet_close();
678: return exit_status;
679: }
680:
1.50 markus 681: void
682: x11_get_proto(char *proto, int proto_len, char *data, int data_len)
683: {
684: char line[512];
685: FILE *f;
686: int got_data = 0, i;
687:
1.55 markus 688: if (options.xauth_location) {
689: /* Try to get Xauthority information for the display. */
1.93 itojun 690: snprintf(line, sizeof line, "%.100s list %.200s 2>" _PATH_DEVNULL,
1.55 markus 691: options.xauth_location, getenv("DISPLAY"));
692: f = popen(line, "r");
693: if (f && fgets(line, sizeof(line), f) &&
694: sscanf(line, "%*s %s %s", proto, data) == 2)
695: got_data = 1;
696: if (f)
697: pclose(f);
698: }
1.50 markus 699: /*
700: * If we didn't get authentication data, just make up some
701: * data. The forwarding code will check the validity of the
702: * response anyway, and substitute this data. The X11
703: * server, however, will ignore this fake data and use
704: * whatever authentication mechanisms it was using otherwise
705: * for the local connection.
706: */
707: if (!got_data) {
708: u_int32_t rand = 0;
709:
710: strlcpy(proto, "MIT-MAGIC-COOKIE-1", proto_len);
711: for (i = 0; i < 16; i++) {
712: if (i % 4 == 0)
713: rand = arc4random();
714: snprintf(data + 2 * i, data_len - 2 * i, "%02x", rand & 0xff);
715: rand >>= 8;
716: }
717: }
718: }
719:
1.70 markus 720: void
721: ssh_init_forwarding(void)
722: {
1.86 markus 723: int success = 0;
1.70 markus 724: int i;
1.86 markus 725:
1.70 markus 726: /* Initiate local TCP/IP port forwardings. */
727: for (i = 0; i < options.num_local_forwards; i++) {
728: debug("Connections to local port %d forwarded to remote address %.200s:%d",
729: options.local_forwards[i].port,
730: options.local_forwards[i].host,
731: options.local_forwards[i].host_port);
1.86 markus 732: success += channel_request_local_forwarding(
1.70 markus 733: options.local_forwards[i].port,
734: options.local_forwards[i].host,
735: options.local_forwards[i].host_port,
736: options.gateway_ports);
737: }
1.86 markus 738: if (i > 0 && success == 0)
739: error("Could not request local forwarding.");
1.70 markus 740:
741: /* Initiate remote TCP/IP port forwardings. */
742: for (i = 0; i < options.num_remote_forwards; i++) {
743: debug("Connections to remote port %d forwarded to local address %.200s:%d",
744: options.remote_forwards[i].port,
745: options.remote_forwards[i].host,
746: options.remote_forwards[i].host_port);
747: channel_request_remote_forwarding(
748: options.remote_forwards[i].port,
749: options.remote_forwards[i].host,
750: options.remote_forwards[i].host_port);
751: }
752: }
753:
754: void
755: check_agent_present(void)
756: {
757: if (options.forward_agent) {
758: /* Clear agent forwarding if we don\'t have an agent. */
759: int authfd = ssh_get_authentication_socket();
760: if (authfd < 0)
761: options.forward_agent = 0;
762: else
763: ssh_close_authentication_socket(authfd);
764: }
765: }
766:
1.45 markus 767: int
768: ssh_session(void)
769: {
770: int type;
771: int plen;
772: int interactive = 0;
773: int have_tty = 0;
774: struct winsize ws;
775: char *cp;
776:
1.31 markus 777: /* Enable compression if requested. */
778: if (options.compression) {
779: debug("Requesting compression at level %d.", options.compression_level);
780:
781: if (options.compression_level < 1 || options.compression_level > 9)
782: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
783:
784: /* Send the request. */
785: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
786: packet_put_int(options.compression_level);
787: packet_send();
788: packet_write_wait();
789: type = packet_read(&plen);
790: if (type == SSH_SMSG_SUCCESS)
791: packet_start_compression(options.compression_level);
792: else if (type == SSH_SMSG_FAILURE)
793: log("Warning: Remote host refused compression.");
794: else
795: packet_disconnect("Protocol error waiting for compression response.");
796: }
797: /* Allocate a pseudo tty if appropriate. */
798: if (tty_flag) {
799: debug("Requesting pty.");
800:
801: /* Start the packet. */
802: packet_start(SSH_CMSG_REQUEST_PTY);
803:
804: /* Store TERM in the packet. There is no limit on the
805: length of the string. */
806: cp = getenv("TERM");
807: if (!cp)
808: cp = "";
809: packet_put_string(cp, strlen(cp));
810:
811: /* Store window size in the packet. */
812: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
813: memset(&ws, 0, sizeof(ws));
814: packet_put_int(ws.ws_row);
815: packet_put_int(ws.ws_col);
816: packet_put_int(ws.ws_xpixel);
817: packet_put_int(ws.ws_ypixel);
818:
819: /* Store tty modes in the packet. */
820: tty_make_modes(fileno(stdin));
821:
822: /* Send the packet, and wait for it to leave. */
823: packet_send();
824: packet_write_wait();
825:
826: /* Read response from the server. */
827: type = packet_read(&plen);
1.43 markus 828: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 829: interactive = 1;
1.45 markus 830: have_tty = 1;
1.43 markus 831: } else if (type == SSH_SMSG_FAILURE)
1.31 markus 832: log("Warning: Remote host failed or refused to allocate a pseudo tty.");
833: else
834: packet_disconnect("Protocol error waiting for pty request response.");
835: }
836: /* Request X11 forwarding if enabled and DISPLAY is set. */
837: if (options.forward_x11 && getenv("DISPLAY") != NULL) {
1.50 markus 838: char proto[512], data[512];
839: /* Get reasonable local authentication information. */
840: x11_get_proto(proto, sizeof proto, data, sizeof data);
841: /* Request forwarding with authentication spoofing. */
1.31 markus 842: debug("Requesting X11 forwarding with authentication spoofing.");
1.50 markus 843: x11_request_forwarding_with_spoofing(0, proto, data);
1.31 markus 844:
845: /* Read response from the server. */
846: type = packet_read(&plen);
847: if (type == SSH_SMSG_SUCCESS) {
848: interactive = 1;
1.50 markus 849: } else if (type == SSH_SMSG_FAILURE) {
1.31 markus 850: log("Warning: Remote host denied X11 forwarding.");
1.50 markus 851: } else {
1.31 markus 852: packet_disconnect("Protocol error waiting for X11 forwarding");
1.50 markus 853: }
1.31 markus 854: }
855: /* Tell the packet module whether this is an interactive session. */
1.80 markus 856: packet_set_interactive(interactive);
1.31 markus 857:
858: /* Request authentication agent forwarding if appropriate. */
1.70 markus 859: check_agent_present();
860:
1.31 markus 861: if (options.forward_agent) {
862: debug("Requesting authentication agent forwarding.");
863: auth_request_forwarding();
864:
865: /* Read response from the server. */
866: type = packet_read(&plen);
867: packet_integrity_check(plen, 0, type);
868: if (type != SSH_SMSG_SUCCESS)
869: log("Warning: Remote host denied authentication agent forwarding.");
870: }
871:
1.70 markus 872: /* Initiate port forwardings. */
873: ssh_init_forwarding();
1.34 markus 874:
875: /* If requested, let ssh continue in the background. */
1.48 markus 876: if (fork_after_authentication_flag)
1.34 markus 877: if (daemon(1, 1) < 0)
878: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 879:
1.33 markus 880: /*
881: * If a command was specified on the command line, execute the
882: * command now. Otherwise request the server to start a shell.
883: */
1.31 markus 884: if (buffer_len(&command) > 0) {
885: int len = buffer_len(&command);
886: if (len > 900)
887: len = 900;
888: debug("Sending command: %.*s", len, buffer_ptr(&command));
889: packet_start(SSH_CMSG_EXEC_CMD);
890: packet_put_string(buffer_ptr(&command), buffer_len(&command));
891: packet_send();
892: packet_write_wait();
893: } else {
894: debug("Requesting shell.");
895: packet_start(SSH_CMSG_EXEC_SHELL);
896: packet_send();
897: packet_write_wait();
898: }
899:
900: /* Enter the interactive session. */
1.60 markus 901: return client_loop(have_tty, tty_flag ? options.escape_char : -1, 0);
1.45 markus 902: }
903:
904: void
1.89 markus 905: client_subsystem_reply(int type, int plen, void *ctxt)
906: {
907: int id, len;
908:
909: id = packet_get_int();
910: len = buffer_len(&command);
1.100 markus 911: if (len > 900)
912: len = 900;
1.89 markus 913: packet_done();
914: if (type == SSH2_MSG_CHANNEL_FAILURE)
915: fatal("Request for subsystem '%.*s' failed on channel %d",
916: len, buffer_ptr(&command), id);
917: }
918:
919: void
1.70 markus 920: ssh_session2_callback(int id, void *arg)
1.45 markus 921: {
922: int len;
1.80 markus 923: int interactive = 0;
924:
1.87 deraadt 925: debug("client_init id %d arg %ld", id, (long)arg);
1.31 markus 926:
1.45 markus 927: if (tty_flag) {
928: struct winsize ws;
929: char *cp;
930: cp = getenv("TERM");
931: if (!cp)
932: cp = "";
933: /* Store window size in the packet. */
934: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
935: memset(&ws, 0, sizeof(ws));
936:
937: channel_request_start(id, "pty-req", 0);
938: packet_put_cstring(cp);
939: packet_put_int(ws.ws_col);
940: packet_put_int(ws.ws_row);
941: packet_put_int(ws.ws_xpixel);
942: packet_put_int(ws.ws_ypixel);
943: packet_put_cstring(""); /* XXX: encode terminal modes */
944: packet_send();
1.80 markus 945: interactive = 1;
1.45 markus 946: /* XXX wait for reply */
947: }
1.50 markus 948: if (options.forward_x11 &&
949: getenv("DISPLAY") != NULL) {
950: char proto[512], data[512];
951: /* Get reasonable local authentication information. */
952: x11_get_proto(proto, sizeof proto, data, sizeof data);
953: /* Request forwarding with authentication spoofing. */
954: debug("Requesting X11 forwarding with authentication spoofing.");
955: x11_request_forwarding_with_spoofing(id, proto, data);
1.80 markus 956: interactive = 1;
1.50 markus 957: /* XXX wait for reply */
958: }
959:
1.70 markus 960: check_agent_present();
961: if (options.forward_agent) {
962: debug("Requesting authentication agent forwarding.");
963: channel_request_start(id, "auth-agent-req@openssh.com", 0);
964: packet_send();
965: }
966:
1.45 markus 967: len = buffer_len(&command);
968: if (len > 0) {
969: if (len > 900)
970: len = 900;
1.85 djm 971: if (subsystem_flag) {
972: debug("Sending subsystem: %.*s", len, buffer_ptr(&command));
1.89 markus 973: channel_request_start(id, "subsystem", /*want reply*/ 1);
974: /* register callback for reply */
975: /* XXX we asume that client_loop has already been called */
976: dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &client_subsystem_reply);
977: dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &client_subsystem_reply);
1.85 djm 978: } else {
979: debug("Sending command: %.*s", len, buffer_ptr(&command));
980: channel_request_start(id, "exec", 0);
981: }
1.100 markus 982: packet_put_string(buffer_ptr(&command), buffer_len(&command));
1.45 markus 983: packet_send();
984: } else {
985: channel_request(id, "shell", 0);
986: }
987: /* channel_callback(id, SSH2_MSG_OPEN_CONFIGMATION, client_init, 0); */
1.90 markus 988:
1.45 markus 989: /* register different callback, etc. XXX */
1.80 markus 990: packet_set_interactive(interactive);
1.45 markus 991: }
992:
993: int
1.106 markus 994: ssh_session2_command(void)
1.45 markus 995: {
1.106 markus 996: int id, window, packetmax;
1.60 markus 997: int in, out, err;
998:
1.62 markus 999: if (stdin_null_flag) {
1.93 itojun 1000: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1001: } else {
1002: in = dup(STDIN_FILENO);
1003: }
1.60 markus 1004: out = dup(STDOUT_FILENO);
1005: err = dup(STDERR_FILENO);
1.45 markus 1006:
1007: if (in < 0 || out < 0 || err < 0)
1.62 markus 1008: fatal("dup() in/out/err failed");
1.45 markus 1009:
1.69 markus 1010: /* enable nonblocking unless tty */
1011: if (!isatty(in))
1012: set_nonblock(in);
1013: if (!isatty(out))
1014: set_nonblock(out);
1015: if (!isatty(err))
1016: set_nonblock(err);
1017:
1.65 markus 1018: window = CHAN_SES_WINDOW_DEFAULT;
1019: packetmax = CHAN_SES_PACKET_DEFAULT;
1020: if (!tty_flag) {
1.45 markus 1021: window *= 2;
1.65 markus 1022: packetmax *=2;
1.45 markus 1023: }
1024: id = channel_new(
1025: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1026: window, packetmax, CHAN_EXTENDED_WRITE,
1.69 markus 1027: xstrdup("client-session"), /*nonblock*/0);
1.45 markus 1028:
1.106 markus 1029: debug("channel_new: %d", id);
1030:
1.45 markus 1031: channel_open(id);
1.70 markus 1032: channel_register_callback(id, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION,
1033: ssh_session2_callback, (void *)0);
1.106 markus 1034:
1035: return id;
1036: }
1037:
1038: int
1039: ssh_session2(void)
1040: {
1041: int id;
1042:
1043: /* XXX should be pre-session */
1044: ssh_init_forwarding();
1045:
1046: id = no_shell_flag ? -1 : ssh_session2_command();
1047:
1048: /* If requested, let ssh continue in the background. */
1049: if (fork_after_authentication_flag)
1050: if (daemon(1, 1) < 0)
1051: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 1052:
1.60 markus 1053: return client_loop(tty_flag, tty_flag ? options.escape_char : -1, id);
1.72 markus 1054: }
1055:
1.104 markus 1056: void
1057: load_public_identity_files(void)
1058: {
1059: char *filename;
1060: Key *public;
1061: int i;
1062:
1063: for (i = 0; i < options.num_identity_files; i++) {
1064: filename = tilde_expand_filename(options.identity_files[i],
1065: original_real_uid);
1.105 markus 1066: public = key_load_public(filename, NULL);
1.104 markus 1067: debug("identity file %s type %d", filename,
1068: public ? public->type : -1);
1069: xfree(options.identity_files[i]);
1070: options.identity_files[i] = filename;
1071: options.identity_keys[i] = public;
1072: }
1.1 deraadt 1073: }