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