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