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