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