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