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