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