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