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