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