Annotation of src/usr.bin/ssh/ssh.c, Revision 1.293
1.293 ! deraadt 1: /* $OpenBSD: ssh.c,v 1.292 2006/08/01 23:36:12 stevesk Exp $ */
1.1 deraadt 2: /*
1.32 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
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: *
1.64 deraadt 10: * As far as I am concerned, the code I have written for this software
11: * can be used freely for any purpose. Any derived versions of this
12: * software must be clearly marked as such, and if the derived work is
13: * incompatible with the protocol description in the RFC file, it must be
14: * called by a name other than "ssh" or "Secure Shell".
15: *
16: * Copyright (c) 1999 Niels Provos. All rights reserved.
1.202 markus 17: * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
1.64 deraadt 18: *
19: * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
20: * in Canada (German citizen).
21: *
22: * Redistribution and use in source and binary forms, with or without
23: * modification, are permitted provided that the following conditions
24: * are met:
25: * 1. Redistributions of source code must retain the above copyright
26: * notice, this list of conditions and the following disclaimer.
27: * 2. Redistributions in binary form must reproduce the above copyright
28: * notice, this list of conditions and the following disclaimer in the
29: * documentation and/or other materials provided with the distribution.
30: *
31: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.32 deraadt 41: */
1.1 deraadt 42:
1.293 ! deraadt 43: #include <sys/types.h>
1.259 stevesk 44: #include <sys/resource.h>
1.260 stevesk 45: #include <sys/ioctl.h>
1.262 stevesk 46: #include <sys/types.h>
1.280 stevesk 47: #include <sys/socket.h>
1.262 stevesk 48: #include <sys/un.h>
1.264 stevesk 49: #include <sys/stat.h>
1.258 stevesk 50:
1.265 stevesk 51: #include <ctype.h>
1.285 stevesk 52: #include <errno.h>
1.281 stevesk 53: #include <fcntl.h>
1.286 stevesk 54: #include <netdb.h>
1.258 stevesk 55: #include <paths.h>
1.279 stevesk 56: #include <pwd.h>
1.263 stevesk 57: #include <signal.h>
1.287 stevesk 58: #include <stddef.h>
1.291 stevesk 59: #include <stdio.h>
1.290 stevesk 60: #include <stdlib.h>
1.289 stevesk 61: #include <string.h>
1.288 stevesk 62: #include <unistd.h>
1.49 markus 63:
64: #include <openssl/evp.h>
1.72 markus 65: #include <openssl/err.h>
1.1 deraadt 66:
1.293 ! deraadt 67: #include "xmalloc.h"
1.84 markus 68: #include "ssh.h"
69: #include "ssh1.h"
70: #include "ssh2.h"
71: #include "compat.h"
72: #include "cipher.h"
1.1 deraadt 73: #include "packet.h"
74: #include "buffer.h"
1.123 markus 75: #include "channels.h"
1.49 markus 76: #include "key.h"
1.58 markus 77: #include "authfd.h"
1.49 markus 78: #include "authfile.h"
1.83 markus 79: #include "pathnames.h"
1.214 djm 80: #include "dispatch.h"
1.81 markus 81: #include "clientloop.h"
1.84 markus 82: #include "log.h"
83: #include "readconf.h"
84: #include "sshconnect.h"
85: #include "misc.h"
1.95 markus 86: #include "kex.h"
87: #include "mac.h"
1.213 deraadt 88: #include "sshpty.h"
1.212 djm 89: #include "match.h"
1.214 djm 90: #include "msg.h"
91: #include "monitor_fdpass.h"
1.225 dtucker 92: #include "uidswap.h"
1.278 stevesk 93: #include "version.h"
1.49 markus 94:
1.127 markus 95: #ifdef SMARTCARD
96: #include "scard.h"
1.137 jakob 97: #endif
1.127 markus 98:
1.49 markus 99: extern char *__progname;
1.1 deraadt 100:
1.31 markus 101: /* Flag indicating whether debug mode is on. This can be set on the command line. */
1.1 deraadt 102: int debug_flag = 0;
103:
1.46 markus 104: /* Flag indicating whether a tty should be allocated */
1.1 deraadt 105: int tty_flag = 0;
1.79 markus 106: int no_tty_flag = 0;
107: int force_tty_flag = 0;
1.1 deraadt 108:
1.45 markus 109: /* don't exec a shell */
110: int no_shell_flag = 0;
111:
1.33 markus 112: /*
113: * Flag indicating that nothing should be read from stdin. This can be set
114: * on the command line.
115: */
1.1 deraadt 116: int stdin_null_flag = 0;
117:
1.33 markus 118: /*
119: * Flag indicating that ssh should fork after authentication. This is useful
1.172 deraadt 120: * so that the passphrase can be entered manually, and then ssh goes to the
1.33 markus 121: * background.
122: */
1.1 deraadt 123: int fork_after_authentication_flag = 0;
124:
1.33 markus 125: /*
126: * General data structure for command line options and options configurable
127: * in configuration files. See readconf.h.
128: */
1.1 deraadt 129: Options options;
130:
1.139 markus 131: /* optional user configfile */
132: char *config = NULL;
133:
1.33 markus 134: /*
135: * Name of the host we are connecting to. This is the name given on the
136: * command line, or the HostName specified for the user-supplied name in a
137: * configuration file.
138: */
1.1 deraadt 139: char *host;
140:
1.22 provos 141: /* socket address the host resolves to */
1.37 markus 142: struct sockaddr_storage hostaddr;
1.1 deraadt 143:
1.112 markus 144: /* Private host keys. */
1.173 markus 145: Sensitive sensitive_data;
1.1 deraadt 146:
1.10 dugsong 147: /* Original real UID. */
148: uid_t original_real_uid;
1.177 markus 149: uid_t original_effective_uid;
1.1 deraadt 150:
1.45 markus 151: /* command to be executed */
152: Buffer command;
153:
1.85 djm 154: /* Should we execute a command or invoke a subsystem? */
155: int subsystem_flag = 0;
156:
1.170 markus 157: /* # of replies received for global requests */
158: static int client_global_request_id = 0;
159:
1.186 djm 160: /* pid of proxycommand child process */
161: pid_t proxy_command_pid = 0;
162:
1.214 djm 163: /* fd to control socket */
164: int control_fd = -1;
165:
1.229 djm 166: /* Multiplexing control command */
1.240 djm 167: static u_int mux_command = 0;
1.229 djm 168:
1.214 djm 169: /* Only used in control client mode */
170: volatile sig_atomic_t control_client_terminate = 0;
171: u_int control_server_pid = 0;
172:
1.1 deraadt 173: /* Prints a help message to the user. This function never returns. */
174:
1.126 itojun 175: static void
1.93 itojun 176: usage(void)
1.1 deraadt 177: {
1.208 markus 178: fprintf(stderr,
1.230 jmc 179: "usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
1.251 jmc 180: " [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
1.233 jmc 181: " [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
1.232 djm 182: " [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
1.233 jmc 183: " [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
1.277 stevesk 184: " [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
1.208 markus 185: );
1.257 dtucker 186: exit(255);
1.1 deraadt 187: }
188:
1.126 itojun 189: static int ssh_session(void);
190: static int ssh_session2(void);
191: static void load_public_identity_files(void);
1.214 djm 192: static void control_client(const char *path);
1.45 markus 193:
1.32 deraadt 194: /*
195: * Main program for the ssh client.
196: */
1.2 provos 197: int
198: main(int ac, char **av)
1.1 deraadt 199: {
1.178 markus 200: int i, opt, exit_status;
1.205 markus 201: char *p, *cp, *line, buf[256];
1.31 markus 202: struct stat st;
1.98 markus 203: struct passwd *pw;
1.45 markus 204: int dummy;
1.144 stevesk 205: extern int optind, optreset;
206: extern char *optarg;
1.244 djm 207: struct servent *sp;
1.232 djm 208: Forward fwd;
1.250 djm 209:
210: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
211: sanitise_stdfd();
1.31 markus 212:
1.33 markus 213: /*
214: * Save the original real uid. It will be needed later (uid-swapping
215: * may clobber the real uid).
216: */
1.31 markus 217: original_real_uid = getuid();
218: original_effective_uid = geteuid();
219:
1.184 stevesk 220: /*
221: * Use uid-swapping to give up root privileges for the duration of
222: * option processing. We will re-instantiate the rights when we are
223: * ready to create the privileged port, and will permanently drop
224: * them when the port has been created (actually, when the connection
225: * has been made, as we may need to create the port several times).
226: */
227: PRIV_END;
228:
1.31 markus 229: /* If we are installed setuid root be careful to not drop core. */
230: if (original_real_uid != original_effective_uid) {
231: struct rlimit rlim;
232: rlim.rlim_cur = rlim.rlim_max = 0;
233: if (setrlimit(RLIMIT_CORE, &rlim) < 0)
234: fatal("setrlimit failed: %.100s", strerror(errno));
1.1 deraadt 235: }
1.107 markus 236: /* Get user data. */
237: pw = getpwuid(original_real_uid);
238: if (!pw) {
1.191 itojun 239: logit("You don't exist, go away!");
1.257 dtucker 240: exit(255);
1.107 markus 241: }
242: /* Take a copy of the returned structure. */
243: pw = pwcopy(pw);
1.31 markus 244:
1.33 markus 245: /*
246: * Set our umask to something reasonable, as some files are created
247: * with the default umask. This will make them world-readable but
248: * writable only by the owner, which is ok for all files for which we
249: * don't set the modes explicitly.
250: */
1.31 markus 251: umask(022);
252:
253: /* Initialize option structure to indicate that no values have been set. */
254: initialize_options(&options);
255:
256: /* Parse command-line arguments. */
257: host = NULL;
258:
1.266 djm 259: again:
1.128 fgsch 260: while ((opt = getopt(ac, av,
1.255 reyk 261: "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNO:PR:S:TVw:XY")) != -1) {
1.31 markus 262: switch (opt) {
1.91 jakob 263: case '1':
264: options.protocol = SSH_PROTO_1;
265: break;
1.47 markus 266: case '2':
267: options.protocol = SSH_PROTO_2;
268: break;
1.37 markus 269: case '4':
1.196 djm 270: options.address_family = AF_INET;
1.37 markus 271: break;
272: case '6':
1.196 djm 273: options.address_family = AF_INET6;
1.37 markus 274: break;
1.31 markus 275: case 'n':
276: stdin_null_flag = 1;
277: break;
278: case 'f':
279: fork_after_authentication_flag = 1;
280: stdin_null_flag = 1;
281: break;
282: case 'x':
283: options.forward_x11 = 0;
284: break;
285: case 'X':
286: options.forward_x11 = 1;
287: break;
1.202 markus 288: case 'Y':
289: options.forward_x11 = 1;
290: options.forward_x11_trusted = 1;
291: break;
1.31 markus 292: case 'g':
293: options.gateway_ports = 1;
294: break;
1.229 djm 295: case 'O':
296: if (strcmp(optarg, "check") == 0)
297: mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
298: else if (strcmp(optarg, "exit") == 0)
299: mux_command = SSHMUX_COMMAND_TERMINATE;
300: else
301: fatal("Invalid multiplex command.");
302: break;
1.183 stevesk 303: case 'P': /* deprecated */
1.31 markus 304: options.use_privileged_port = 0;
305: break;
306: case 'a':
307: options.forward_agent = 0;
1.53 markus 308: break;
309: case 'A':
310: options.forward_agent = 1;
1.31 markus 311: break;
312: case 'k':
1.204 dtucker 313: options.gss_deleg_creds = 0;
1.31 markus 314: break;
315: case 'i':
316: if (stat(optarg, &st) < 0) {
1.128 fgsch 317: fprintf(stderr, "Warning: Identity file %s "
1.231 otto 318: "not accessible: %s.\n", optarg,
319: strerror(errno));
1.31 markus 320: break;
321: }
1.128 fgsch 322: if (options.num_identity_files >=
323: SSH_MAX_IDENTITY_FILES)
324: fatal("Too many identity files specified "
325: "(max %d)", SSH_MAX_IDENTITY_FILES);
326: options.identity_files[options.num_identity_files++] =
327: xstrdup(optarg);
1.31 markus 328: break;
1.127 markus 329: case 'I':
330: #ifdef SMARTCARD
1.133 markus 331: options.smartcard_device = xstrdup(optarg);
1.137 jakob 332: #else
1.127 markus 333: fprintf(stderr, "no support for smartcards.\n");
1.137 jakob 334: #endif
1.127 markus 335: break;
1.31 markus 336: case 't':
1.79 markus 337: if (tty_flag)
338: force_tty_flag = 1;
1.31 markus 339: tty_flag = 1;
340: break;
341: case 'v':
1.197 markus 342: if (debug_flag == 0) {
1.66 markus 343: debug_flag = 1;
344: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197 markus 345: } else {
346: if (options.log_level < SYSLOG_LEVEL_DEBUG3)
347: options.log_level++;
1.66 markus 348: break;
1.197 markus 349: }
1.227 deraadt 350: /* FALLTHROUGH */
1.31 markus 351: case 'V':
1.209 markus 352: fprintf(stderr, "%s, %s\n",
353: SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.31 markus 354: if (opt == 'V')
355: exit(0);
356: break;
1.255 reyk 357: case 'w':
1.256 reyk 358: if (options.tun_open == -1)
359: options.tun_open = SSH_TUNMODE_DEFAULT;
1.255 reyk 360: options.tun_local = a2tun(optarg, &options.tun_remote);
1.256 reyk 361: if (options.tun_local == SSH_TUNID_ERR) {
1.255 reyk 362: fprintf(stderr, "Bad tun device '%s'\n", optarg);
1.257 dtucker 363: exit(255);
1.255 reyk 364: }
365: break;
1.31 markus 366: case 'q':
367: options.log_level = SYSLOG_LEVEL_QUIET;
368: break;
369: case 'e':
370: if (optarg[0] == '^' && optarg[2] == 0 &&
1.128 fgsch 371: (u_char) optarg[1] >= 64 &&
372: (u_char) optarg[1] < 128)
1.78 markus 373: options.escape_char = (u_char) optarg[1] & 31;
1.31 markus 374: else if (strlen(optarg) == 1)
1.78 markus 375: options.escape_char = (u_char) optarg[0];
1.31 markus 376: else if (strcmp(optarg, "none") == 0)
1.119 stevesk 377: options.escape_char = SSH_ESCAPECHAR_NONE;
1.31 markus 378: else {
1.128 fgsch 379: fprintf(stderr, "Bad escape character '%s'.\n",
380: optarg);
1.257 dtucker 381: exit(255);
1.31 markus 382: }
383: break;
384: case 'c':
1.49 markus 385: if (ciphers_valid(optarg)) {
386: /* SSH2 only */
387: options.ciphers = xstrdup(optarg);
1.224 markus 388: options.cipher = SSH_CIPHER_INVALID;
1.49 markus 389: } else {
390: /* SSH1 only */
1.74 markus 391: options.cipher = cipher_number(optarg);
392: if (options.cipher == -1) {
1.128 fgsch 393: fprintf(stderr,
394: "Unknown cipher type '%s'\n",
395: optarg);
1.257 dtucker 396: exit(255);
1.49 markus 397: }
1.128 fgsch 398: if (options.cipher == SSH_CIPHER_3DES)
1.74 markus 399: options.ciphers = "3des-cbc";
1.128 fgsch 400: else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.74 markus 401: options.ciphers = "blowfish-cbc";
1.128 fgsch 402: else
1.74 markus 403: options.ciphers = (char *)-1;
1.95 markus 404: }
405: break;
406: case 'm':
407: if (mac_valid(optarg))
408: options.macs = xstrdup(optarg);
409: else {
1.128 fgsch 410: fprintf(stderr, "Unknown mac type '%s'\n",
411: optarg);
1.257 dtucker 412: exit(255);
1.31 markus 413: }
414: break;
1.214 djm 415: case 'M':
1.242 djm 416: if (options.control_master == SSHCTL_MASTER_YES)
417: options.control_master = SSHCTL_MASTER_ASK;
418: else
419: options.control_master = SSHCTL_MASTER_YES;
1.214 djm 420: break;
1.31 markus 421: case 'p':
1.113 stevesk 422: options.port = a2port(optarg);
423: if (options.port == 0) {
1.109 markus 424: fprintf(stderr, "Bad port '%s'\n", optarg);
1.257 dtucker 425: exit(255);
1.109 markus 426: }
1.31 markus 427: break;
428: case 'l':
429: options.user = optarg;
430: break;
1.141 stevesk 431:
432: case 'L':
1.232 djm 433: if (parse_forward(&fwd, optarg))
434: add_local_forward(&options, &fwd);
435: else {
1.128 fgsch 436: fprintf(stderr,
1.232 djm 437: "Bad local forwarding specification '%s'\n",
1.128 fgsch 438: optarg);
1.257 dtucker 439: exit(255);
1.31 markus 440: }
1.232 djm 441: break;
442:
443: case 'R':
444: if (parse_forward(&fwd, optarg)) {
445: add_remote_forward(&options, &fwd);
446: } else {
1.128 fgsch 447: fprintf(stderr,
1.232 djm 448: "Bad remote forwarding specification "
449: "'%s'\n", optarg);
1.257 dtucker 450: exit(255);
1.31 markus 451: }
452: break;
1.108 markus 453:
454: case 'D':
1.232 djm 455: cp = p = xstrdup(optarg);
456: memset(&fwd, '\0', sizeof(fwd));
457: fwd.connect_host = "socks";
458: if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
459: fprintf(stderr, "Bad dynamic forwarding "
460: "specification '%.100s'\n", optarg);
1.257 dtucker 461: exit(255);
1.232 djm 462: }
463: if (cp != NULL) {
464: fwd.listen_port = a2port(cp);
465: fwd.listen_host = cleanhostname(fwd.listen_host);
466: } else {
467: fwd.listen_port = a2port(fwd.listen_host);
1.249 djm 468: fwd.listen_host = NULL;
1.232 djm 469: }
470:
471: if (fwd.listen_port == 0) {
1.128 fgsch 472: fprintf(stderr, "Bad dynamic port '%s'\n",
473: optarg);
1.257 dtucker 474: exit(255);
1.109 markus 475: }
1.232 djm 476: add_local_forward(&options, &fwd);
477: xfree(p);
1.108 markus 478: break;
479:
1.31 markus 480: case 'C':
481: options.compression = 1;
482: break;
1.45 markus 483: case 'N':
484: no_shell_flag = 1;
485: no_tty_flag = 1;
486: break;
487: case 'T':
488: no_tty_flag = 1;
489: break;
1.31 markus 490: case 'o':
491: dummy = 1;
1.205 markus 492: line = xstrdup(optarg);
1.128 fgsch 493: if (process_config_line(&options, host ? host : "",
1.205 markus 494: line, "command-line", 0, &dummy) != 0)
1.257 dtucker 495: exit(255);
1.205 markus 496: xfree(line);
1.31 markus 497: break;
1.85 djm 498: case 's':
499: subsystem_flag = 1;
1.117 markus 500: break;
1.214 djm 501: case 'S':
502: if (options.control_path != NULL)
503: free(options.control_path);
504: options.control_path = xstrdup(optarg);
505: break;
1.117 markus 506: case 'b':
507: options.bind_address = optarg;
1.85 djm 508: break;
1.139 markus 509: case 'F':
510: config = optarg;
511: break;
1.31 markus 512: default:
513: usage();
1.1 deraadt 514: }
1.31 markus 515: }
516:
1.128 fgsch 517: ac -= optind;
518: av += optind;
519:
520: if (ac > 0 && !host && **av != '-') {
1.188 markus 521: if (strrchr(*av, '@')) {
1.128 fgsch 522: p = xstrdup(*av);
1.188 markus 523: cp = strrchr(p, '@');
1.128 fgsch 524: if (cp == NULL || cp == p)
525: usage();
526: options.user = p;
527: *cp = '\0';
528: host = ++cp;
529: } else
530: host = *av;
1.189 millert 531: if (ac > 1) {
532: optind = optreset = 1;
1.128 fgsch 533: goto again;
534: }
1.189 millert 535: ac--, av++;
1.128 fgsch 536: }
537:
1.31 markus 538: /* Check that we got a host name. */
539: if (!host)
540: usage();
541:
1.54 markus 542: SSLeay_add_all_algorithms();
1.72 markus 543: ERR_load_crypto_strings();
1.31 markus 544:
545: /* Initialize the command to execute on remote host. */
546: buffer_init(&command);
1.1 deraadt 547:
1.33 markus 548: /*
549: * Save the command to execute on the remote host in a buffer. There
550: * is no limit on the length of the command, except by the maximum
551: * packet size. Also sets the tty flag if there is no command.
552: */
1.128 fgsch 553: if (!ac) {
1.31 markus 554: /* No command specified - execute shell on a tty. */
555: tty_flag = 1;
1.85 djm 556: if (subsystem_flag) {
1.128 fgsch 557: fprintf(stderr,
558: "You must specify a subsystem to invoke.\n");
1.85 djm 559: usage();
560: }
1.31 markus 561: } else {
1.128 fgsch 562: /* A command has been specified. Store it into the buffer. */
563: for (i = 0; i < ac; i++) {
564: if (i)
1.31 markus 565: buffer_append(&command, " ", 1);
566: buffer_append(&command, av[i], strlen(av[i]));
567: }
568: }
569:
570: /* Cannot fork to background if no command. */
1.63 markus 571: if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
1.31 markus 572: fatal("Cannot fork into background without a command to execute.");
573:
574: /* Allocate a tty by default if no command specified. */
575: if (buffer_len(&command) == 0)
576: tty_flag = 1;
577:
1.180 deraadt 578: /* Force no tty */
1.75 markus 579: if (no_tty_flag)
580: tty_flag = 0;
1.31 markus 581: /* Do not allocate a tty if stdin is not a tty. */
1.236 djm 582: if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
1.31 markus 583: if (tty_flag)
1.191 itojun 584: logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
1.31 markus 585: tty_flag = 0;
586: }
1.45 markus 587:
1.101 markus 588: /*
589: * Initialize "log" output. Since we are the client all output
590: * actually goes to stderr.
591: */
1.111 markus 592: log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
593: SYSLOG_FACILITY_USER, 1);
1.31 markus 594:
1.139 markus 595: /*
596: * Read per-user configuration file. Ignore the system wide config
597: * file if the user specifies a config file on the command line.
598: */
599: if (config != NULL) {
1.211 djm 600: if (!read_config_file(config, host, &options, 0))
1.142 stevesk 601: fatal("Can't open user config file %.100s: "
602: "%.100s", config, strerror(errno));
1.139 markus 603: } else {
604: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
605: _PATH_SSH_USER_CONFFILE);
1.210 djm 606: (void)read_config_file(buf, host, &options, 1);
1.139 markus 607:
1.145 markus 608: /* Read systemwide configuration file after use config. */
1.223 deraadt 609: (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
1.210 djm 610: &options, 0);
1.139 markus 611: }
1.31 markus 612:
613: /* Fill configuration defaults. */
614: fill_default_options(&options);
615:
1.196 djm 616: channel_set_af(options.address_family);
617:
1.31 markus 618: /* reinit */
1.101 markus 619: log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
1.31 markus 620:
621: if (options.user == NULL)
622: options.user = xstrdup(pw->pw_name);
623:
624: if (options.hostname != NULL)
625: host = options.hostname;
1.195 markus 626:
627: /* force lowercase for hostkey matching */
628: if (options.host_key_alias != NULL) {
629: for (p = options.host_key_alias; *p; p++)
630: if (isupper(*p))
1.271 deraadt 631: *p = (char)tolower(*p);
1.195 markus 632: }
1.190 markus 633:
1.246 djm 634: /* Get default port if port has not been set. */
635: if (options.port == 0) {
636: sp = getservbyname(SSH_SERVICE_NAME, "tcp");
637: options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
638: }
639:
1.190 markus 640: if (options.proxy_command != NULL &&
641: strcmp(options.proxy_command, "none") == 0)
642: options.proxy_command = NULL;
1.245 djm 643: if (options.control_path != NULL &&
644: strcmp(options.control_path, "none") == 0)
645: options.control_path = NULL;
1.31 markus 646:
1.214 djm 647: if (options.control_path != NULL) {
1.275 djm 648: char thishost[NI_MAXHOST];
1.261 djm 649:
1.275 djm 650: if (gethostname(thishost, sizeof(thishost)) == -1)
1.261 djm 651: fatal("gethostname: %s", strerror(errno));
1.241 djm 652: snprintf(buf, sizeof(buf), "%d", options.port);
653: cp = tilde_expand_filename(options.control_path,
654: original_real_uid);
655: options.control_path = percent_expand(cp, "p", buf, "h", host,
1.275 djm 656: "r", options.user, "l", thishost, (char *)NULL);
1.241 djm 657: xfree(cp);
1.214 djm 658: }
1.240 djm 659: if (mux_command != 0 && options.control_path == NULL)
660: fatal("No ControlPath specified for \"-O\" command");
1.242 djm 661: if (options.control_path != NULL)
1.237 jakob 662: control_client(options.control_path);
1.214 djm 663:
1.77 markus 664: /* Open a connection to the remote host. */
1.203 djm 665: if (ssh_connect(host, &hostaddr, options.port,
1.196 djm 666: options.address_family, options.connection_attempts,
1.177 markus 667: original_effective_uid == 0 && options.use_privileged_port,
1.179 markus 668: options.proxy_command) != 0)
1.257 dtucker 669: exit(255);
1.31 markus 670:
1.33 markus 671: /*
672: * If we successfully made the connection, load the host private key
673: * in case we will need it later for combined rsa-rhosts
674: * authentication. This must be done before releasing extra
675: * privileges, because the file is only readable by root.
1.174 markus 676: * If we cannot access the private keys, load the public keys
677: * instead and try to execute the ssh-keysign helper instead.
1.33 markus 678: */
1.112 markus 679: sensitive_data.nkeys = 0;
680: sensitive_data.keys = NULL;
1.173 markus 681: sensitive_data.external_keysign = 0;
1.178 markus 682: if (options.rhosts_rsa_authentication ||
683: options.hostbased_authentication) {
1.112 markus 684: sensitive_data.nkeys = 3;
1.274 deraadt 685: sensitive_data.keys = xcalloc(sensitive_data.nkeys,
1.180 deraadt 686: sizeof(Key));
1.177 markus 687:
688: PRIV_START;
1.112 markus 689: sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.276 dtucker 690: _PATH_HOST_KEY_FILE, "", NULL, NULL);
1.112 markus 691: sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
1.276 dtucker 692: _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
1.112 markus 693: sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
1.276 dtucker 694: _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
1.177 markus 695: PRIV_END;
1.173 markus 696:
1.181 markus 697: if (options.hostbased_authentication == 1 &&
698: sensitive_data.keys[0] == NULL &&
1.173 markus 699: sensitive_data.keys[1] == NULL &&
700: sensitive_data.keys[2] == NULL) {
701: sensitive_data.keys[1] = key_load_public(
702: _PATH_HOST_DSA_KEY_FILE, NULL);
703: sensitive_data.keys[2] = key_load_public(
704: _PATH_HOST_RSA_KEY_FILE, NULL);
705: sensitive_data.external_keysign = 1;
706: }
1.31 markus 707: }
1.33 markus 708: /*
709: * Get rid of any extra privileges that we may have. We will no
710: * longer need them. Also, extra privileges could make it very hard
711: * to read identity files and other non-world-readable files from the
712: * user's home directory if it happens to be on a NFS volume where
713: * root is mapped to nobody.
714: */
1.225 dtucker 715: if (original_effective_uid == 0) {
716: PRIV_START;
717: permanently_set_uid(pw);
718: }
1.31 markus 719:
1.33 markus 720: /*
721: * Now that we are back to our own permissions, create ~/.ssh
1.254 djm 722: * directory if it doesn't already exist.
1.33 markus 723: */
1.138 jakob 724: snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.31 markus 725: if (stat(buf, &st) < 0)
1.57 djm 726: if (mkdir(buf, 0700) < 0)
1.31 markus 727: error("Could not create directory '%.200s'.", buf);
728:
1.104 markus 729: /* load options.identity_files */
730: load_public_identity_files();
731:
732: /* Expand ~ in known host file names. */
733: /* XXX mem-leaks: */
1.72 markus 734: options.system_hostfile =
735: tilde_expand_filename(options.system_hostfile, original_real_uid);
736: options.user_hostfile =
737: tilde_expand_filename(options.user_hostfile, original_real_uid);
738: options.system_hostfile2 =
739: tilde_expand_filename(options.system_hostfile2, original_real_uid);
740: options.user_hostfile2 =
741: tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.149 markus 742:
743: signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.31 markus 744:
745: /* Log into the remote system. This never returns if the login fails. */
1.173 markus 746: ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
1.31 markus 747:
1.112 markus 748: /* We no longer need the private host keys. Clear them now. */
749: if (sensitive_data.nkeys != 0) {
750: for (i = 0; i < sensitive_data.nkeys; i++) {
751: if (sensitive_data.keys[i] != NULL) {
752: /* Destroys contents safely */
753: debug3("clear hostkey %d", i);
754: key_free(sensitive_data.keys[i]);
755: sensitive_data.keys[i] = NULL;
756: }
757: }
758: xfree(sensitive_data.keys);
1.134 markus 759: }
760: for (i = 0; i < options.num_identity_files; i++) {
761: if (options.identity_files[i]) {
762: xfree(options.identity_files[i]);
763: options.identity_files[i] = NULL;
764: }
765: if (options.identity_keys[i]) {
766: key_free(options.identity_keys[i]);
767: options.identity_keys[i] = NULL;
768: }
1.112 markus 769: }
1.31 markus 770:
1.45 markus 771: exit_status = compat20 ? ssh_session2() : ssh_session();
772: packet_close();
1.186 djm 773:
1.214 djm 774: if (options.control_path != NULL && control_fd != -1)
775: unlink(options.control_path);
776:
1.186 djm 777: /*
1.203 djm 778: * Send SIGHUP to proxy command if used. We don't wait() in
1.186 djm 779: * case it hangs and instead rely on init to reap the child
780: */
781: if (proxy_command_pid > 1)
782: kill(proxy_command_pid, SIGHUP);
783:
1.45 markus 784: return exit_status;
785: }
786:
1.126 itojun 787: static void
1.70 markus 788: ssh_init_forwarding(void)
789: {
1.86 markus 790: int success = 0;
1.70 markus 791: int i;
1.86 markus 792:
1.70 markus 793: /* Initiate local TCP/IP port forwardings. */
794: for (i = 0; i < options.num_local_forwards; i++) {
1.232 djm 795: debug("Local connections to %.200s:%d forwarded to remote "
796: "address %.200s:%d",
1.234 deraadt 797: (options.local_forwards[i].listen_host == NULL) ?
798: (options.gateway_ports ? "*" : "LOCALHOST") :
1.232 djm 799: options.local_forwards[i].listen_host,
800: options.local_forwards[i].listen_port,
801: options.local_forwards[i].connect_host,
802: options.local_forwards[i].connect_port);
1.158 markus 803: success += channel_setup_local_fwd_listener(
1.232 djm 804: options.local_forwards[i].listen_host,
805: options.local_forwards[i].listen_port,
806: options.local_forwards[i].connect_host,
807: options.local_forwards[i].connect_port,
1.70 markus 808: options.gateway_ports);
809: }
1.283 markus 810: if (i > 0 && success != i && options.exit_on_forward_failure)
811: fatal("Could not request local forwarding.");
1.86 markus 812: if (i > 0 && success == 0)
813: error("Could not request local forwarding.");
1.70 markus 814:
815: /* Initiate remote TCP/IP port forwardings. */
816: for (i = 0; i < options.num_remote_forwards; i++) {
1.232 djm 817: debug("Remote connections from %.200s:%d forwarded to "
818: "local address %.200s:%d",
1.248 djm 819: (options.remote_forwards[i].listen_host == NULL) ?
1.253 djm 820: "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232 djm 821: options.remote_forwards[i].listen_port,
822: options.remote_forwards[i].connect_host,
823: options.remote_forwards[i].connect_port);
1.283 markus 824: if (channel_request_remote_forwarding(
1.232 djm 825: options.remote_forwards[i].listen_host,
826: options.remote_forwards[i].listen_port,
827: options.remote_forwards[i].connect_host,
1.283 markus 828: options.remote_forwards[i].connect_port) < 0) {
829: if (options.exit_on_forward_failure)
830: fatal("Could not request remote forwarding.");
831: else
832: logit("Warning: Could not request remote "
833: "forwarding.");
834: }
1.70 markus 835: }
836: }
837:
1.126 itojun 838: static void
1.70 markus 839: check_agent_present(void)
840: {
841: if (options.forward_agent) {
1.254 djm 842: /* Clear agent forwarding if we don't have an agent. */
1.185 stevesk 843: if (!ssh_agent_present())
1.70 markus 844: options.forward_agent = 0;
845: }
846: }
847:
1.126 itojun 848: static int
1.45 markus 849: ssh_session(void)
850: {
851: int type;
852: int interactive = 0;
853: int have_tty = 0;
854: struct winsize ws;
855: char *cp;
1.243 djm 856: const char *display;
1.45 markus 857:
1.31 markus 858: /* Enable compression if requested. */
859: if (options.compression) {
860: debug("Requesting compression at level %d.", options.compression_level);
861:
862: if (options.compression_level < 1 || options.compression_level > 9)
863: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
864:
865: /* Send the request. */
866: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
867: packet_put_int(options.compression_level);
868: packet_send();
869: packet_write_wait();
1.156 markus 870: type = packet_read();
1.31 markus 871: if (type == SSH_SMSG_SUCCESS)
872: packet_start_compression(options.compression_level);
873: else if (type == SSH_SMSG_FAILURE)
1.191 itojun 874: logit("Warning: Remote host refused compression.");
1.31 markus 875: else
876: packet_disconnect("Protocol error waiting for compression response.");
877: }
878: /* Allocate a pseudo tty if appropriate. */
879: if (tty_flag) {
880: debug("Requesting pty.");
881:
882: /* Start the packet. */
883: packet_start(SSH_CMSG_REQUEST_PTY);
884:
885: /* Store TERM in the packet. There is no limit on the
886: length of the string. */
887: cp = getenv("TERM");
888: if (!cp)
889: cp = "";
1.124 markus 890: packet_put_cstring(cp);
1.31 markus 891:
892: /* Store window size in the packet. */
893: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
894: memset(&ws, 0, sizeof(ws));
1.269 deraadt 895: packet_put_int((u_int)ws.ws_row);
896: packet_put_int((u_int)ws.ws_col);
897: packet_put_int((u_int)ws.ws_xpixel);
898: packet_put_int((u_int)ws.ws_ypixel);
1.31 markus 899:
900: /* Store tty modes in the packet. */
1.115 stevesk 901: tty_make_modes(fileno(stdin), NULL);
1.31 markus 902:
903: /* Send the packet, and wait for it to leave. */
904: packet_send();
905: packet_write_wait();
906:
907: /* Read response from the server. */
1.156 markus 908: type = packet_read();
1.43 markus 909: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 910: interactive = 1;
1.45 markus 911: have_tty = 1;
1.43 markus 912: } else if (type == SSH_SMSG_FAILURE)
1.191 itojun 913: logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
1.31 markus 914: else
915: packet_disconnect("Protocol error waiting for pty request response.");
916: }
917: /* Request X11 forwarding if enabled and DISPLAY is set. */
1.243 djm 918: display = getenv("DISPLAY");
919: if (options.forward_x11 && display != NULL) {
1.150 stevesk 920: char *proto, *data;
1.50 markus 921: /* Get reasonable local authentication information. */
1.243 djm 922: client_x11_get_proto(display, options.xauth_location,
923: options.forward_x11_trusted, &proto, &data);
1.50 markus 924: /* Request forwarding with authentication spoofing. */
1.31 markus 925: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 926: x11_request_forwarding_with_spoofing(0, display, proto, data);
1.31 markus 927:
928: /* Read response from the server. */
1.156 markus 929: type = packet_read();
1.31 markus 930: if (type == SSH_SMSG_SUCCESS) {
931: interactive = 1;
1.50 markus 932: } else if (type == SSH_SMSG_FAILURE) {
1.191 itojun 933: logit("Warning: Remote host denied X11 forwarding.");
1.50 markus 934: } else {
1.31 markus 935: packet_disconnect("Protocol error waiting for X11 forwarding");
1.50 markus 936: }
1.31 markus 937: }
938: /* Tell the packet module whether this is an interactive session. */
1.80 markus 939: packet_set_interactive(interactive);
1.31 markus 940:
941: /* Request authentication agent forwarding if appropriate. */
1.70 markus 942: check_agent_present();
943:
1.31 markus 944: if (options.forward_agent) {
945: debug("Requesting authentication agent forwarding.");
946: auth_request_forwarding();
947:
948: /* Read response from the server. */
1.156 markus 949: type = packet_read();
1.155 markus 950: packet_check_eom();
1.31 markus 951: if (type != SSH_SMSG_SUCCESS)
1.191 itojun 952: logit("Warning: Remote host denied authentication agent forwarding.");
1.31 markus 953: }
954:
1.70 markus 955: /* Initiate port forwardings. */
956: ssh_init_forwarding();
1.34 markus 957:
958: /* If requested, let ssh continue in the background. */
1.48 markus 959: if (fork_after_authentication_flag)
1.34 markus 960: if (daemon(1, 1) < 0)
961: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 962:
1.33 markus 963: /*
964: * If a command was specified on the command line, execute the
965: * command now. Otherwise request the server to start a shell.
966: */
1.31 markus 967: if (buffer_len(&command) > 0) {
968: int len = buffer_len(&command);
969: if (len > 900)
970: len = 900;
1.152 stevesk 971: debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.31 markus 972: packet_start(SSH_CMSG_EXEC_CMD);
973: packet_put_string(buffer_ptr(&command), buffer_len(&command));
974: packet_send();
975: packet_write_wait();
976: } else {
977: debug("Requesting shell.");
978: packet_start(SSH_CMSG_EXEC_SHELL);
979: packet_send();
980: packet_write_wait();
981: }
982:
983: /* Enter the interactive session. */
1.119 stevesk 984: return client_loop(have_tty, tty_flag ?
985: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.45 markus 986: }
987:
1.126 itojun 988: static void
1.214 djm 989: ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
1.89 markus 990: {
991: int id, len;
992:
993: id = packet_get_int();
994: len = buffer_len(&command);
1.100 markus 995: if (len > 900)
996: len = 900;
1.155 markus 997: packet_check_eom();
1.89 markus 998: if (type == SSH2_MSG_CHANNEL_FAILURE)
999: fatal("Request for subsystem '%.*s' failed on channel %d",
1.152 stevesk 1000: len, (u_char *)buffer_ptr(&command), id);
1.170 markus 1001: }
1002:
1003: void
1.206 markus 1004: client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
1.170 markus 1005: {
1006: int i;
1007:
1008: i = client_global_request_id++;
1.206 markus 1009: if (i >= options.num_remote_forwards)
1.170 markus 1010: return;
1011: debug("remote forward %s for: listen %d, connect %s:%d",
1012: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1.232 djm 1013: options.remote_forwards[i].listen_port,
1014: options.remote_forwards[i].connect_host,
1015: options.remote_forwards[i].connect_port);
1.283 markus 1016: if (type == SSH2_MSG_REQUEST_FAILURE) {
1017: if (options.exit_on_forward_failure)
1018: fatal("Error: remote port forwarding failed for "
1019: "listen port %d",
1020: options.remote_forwards[i].listen_port);
1021: else
1022: logit("Warning: remote port forwarding failed for "
1023: "listen port %d",
1024: options.remote_forwards[i].listen_port);
1025: }
1.89 markus 1026: }
1027:
1.126 itojun 1028: static void
1.214 djm 1029: ssh_control_listener(void)
1.45 markus 1030: {
1.214 djm 1031: struct sockaddr_un addr;
1032: mode_t old_umask;
1.223 deraadt 1033:
1.242 djm 1034: if (options.control_path == NULL ||
1035: options.control_master == SSHCTL_MASTER_NO)
1.214 djm 1036: return;
1037:
1.242 djm 1038: debug("setting up multiplex master socket");
1039:
1.214 djm 1040: memset(&addr, '\0', sizeof(addr));
1041: addr.sun_family = AF_UNIX;
1042: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
1043: strlen(options.control_path) + 1;
1044:
1045: if (strlcpy(addr.sun_path, options.control_path,
1046: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1047: fatal("ControlPath too long");
1048:
1049: if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1.252 stevesk 1050: fatal("%s socket(): %s", __func__, strerror(errno));
1.214 djm 1051:
1052: old_umask = umask(0177);
1.270 deraadt 1053: if (bind(control_fd, (struct sockaddr *)&addr, addr.sun_len) == -1) {
1.214 djm 1054: control_fd = -1;
1.238 djm 1055: if (errno == EINVAL || errno == EADDRINUSE)
1.214 djm 1056: fatal("ControlSocket %s already exists",
1057: options.control_path);
1058: else
1.252 stevesk 1059: fatal("%s bind(): %s", __func__, strerror(errno));
1.214 djm 1060: }
1061: umask(old_umask);
1.80 markus 1062:
1.214 djm 1063: if (listen(control_fd, 64) == -1)
1.252 stevesk 1064: fatal("%s listen(): %s", __func__, strerror(errno));
1.31 markus 1065:
1.214 djm 1066: set_nonblock(control_fd);
1067: }
1.45 markus 1068:
1.214 djm 1069: /* request pty/x11/agent/tcpfwd/shell for channel */
1070: static void
1071: ssh_session2_setup(int id, void *arg)
1072: {
1.215 djm 1073: extern char **environ;
1.243 djm 1074: const char *display;
1075: int interactive = tty_flag;
1.215 djm 1076:
1.248 djm 1077: display = getenv("DISPLAY");
1.243 djm 1078: if (options.forward_x11 && display != NULL) {
1.150 stevesk 1079: char *proto, *data;
1.50 markus 1080: /* Get reasonable local authentication information. */
1.243 djm 1081: client_x11_get_proto(display, options.xauth_location,
1082: options.forward_x11_trusted, &proto, &data);
1.50 markus 1083: /* Request forwarding with authentication spoofing. */
1084: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 1085: x11_request_forwarding_with_spoofing(id, display, proto, data);
1.80 markus 1086: interactive = 1;
1.50 markus 1087: /* XXX wait for reply */
1088: }
1089:
1.70 markus 1090: check_agent_present();
1091: if (options.forward_agent) {
1092: debug("Requesting authentication agent forwarding.");
1093: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1094: packet_send();
1.212 djm 1095: }
1096:
1.256 reyk 1097: if (options.tun_open != SSH_TUNMODE_NO) {
1.255 reyk 1098: Channel *c;
1099: int fd;
1100:
1101: debug("Requesting tun.");
1.256 reyk 1102: if ((fd = tun_open(options.tun_local,
1103: options.tun_open)) >= 0) {
1.255 reyk 1104: c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1105: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1106: 0, "tun", 1);
1107: c->datagram = 1;
1108: packet_start(SSH2_MSG_CHANNEL_OPEN);
1109: packet_put_cstring("tun@openssh.com");
1110: packet_put_int(c->self);
1111: packet_put_int(c->local_window_max);
1112: packet_put_int(c->local_maxpacket);
1.256 reyk 1113: packet_put_int(options.tun_open);
1.255 reyk 1114: packet_put_int(options.tun_remote);
1115: packet_send();
1116: }
1117: }
1118:
1.214 djm 1119: client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
1.215 djm 1120: NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
1.90 markus 1121:
1.80 markus 1122: packet_set_interactive(interactive);
1.45 markus 1123: }
1124:
1.143 markus 1125: /* open new channel for a session */
1.126 itojun 1126: static int
1.143 markus 1127: ssh_session2_open(void)
1.45 markus 1128: {
1.118 markus 1129: Channel *c;
1130: int window, packetmax, in, out, err;
1.60 markus 1131:
1.62 markus 1132: if (stdin_null_flag) {
1.93 itojun 1133: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1134: } else {
1135: in = dup(STDIN_FILENO);
1136: }
1.60 markus 1137: out = dup(STDOUT_FILENO);
1138: err = dup(STDERR_FILENO);
1.45 markus 1139:
1140: if (in < 0 || out < 0 || err < 0)
1.62 markus 1141: fatal("dup() in/out/err failed");
1.45 markus 1142:
1.69 markus 1143: /* enable nonblocking unless tty */
1144: if (!isatty(in))
1145: set_nonblock(in);
1146: if (!isatty(out))
1147: set_nonblock(out);
1148: if (!isatty(err))
1149: set_nonblock(err);
1150:
1.65 markus 1151: window = CHAN_SES_WINDOW_DEFAULT;
1152: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1153: if (tty_flag) {
1154: window >>= 1;
1155: packetmax >>= 1;
1.45 markus 1156: }
1.118 markus 1157: c = channel_new(
1.45 markus 1158: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1159: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1160: "client-session", /*nonblock*/0);
1.45 markus 1161:
1.143 markus 1162: debug3("ssh_session2_open: channel_new: %d", c->self);
1.106 markus 1163:
1.122 markus 1164: channel_send_open(c->self);
1.143 markus 1165: if (!no_shell_flag)
1.214 djm 1166: channel_register_confirm(c->self, ssh_session2_setup, NULL);
1.106 markus 1167:
1.118 markus 1168: return c->self;
1.106 markus 1169: }
1170:
1.126 itojun 1171: static int
1.106 markus 1172: ssh_session2(void)
1173: {
1.143 markus 1174: int id = -1;
1.106 markus 1175:
1176: /* XXX should be pre-session */
1177: ssh_init_forwarding();
1.214 djm 1178: ssh_control_listener();
1.106 markus 1179:
1.143 markus 1180: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1181: id = ssh_session2_open();
1.255 reyk 1182:
1183: /* Execute a local command */
1184: if (options.local_command != NULL &&
1185: options.permit_local_command)
1186: ssh_local_cmd(options.local_command);
1.106 markus 1187:
1188: /* If requested, let ssh continue in the background. */
1189: if (fork_after_authentication_flag)
1190: if (daemon(1, 1) < 0)
1191: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 1192:
1.119 stevesk 1193: return client_loop(tty_flag, tty_flag ?
1194: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 1195: }
1196:
1.126 itojun 1197: static void
1.104 markus 1198: load_public_identity_files(void)
1199: {
1.275 djm 1200: char *filename, *cp, thishost[NI_MAXHOST];
1.167 markus 1201: int i = 0;
1.104 markus 1202: Key *public;
1.275 djm 1203: struct passwd *pw;
1.167 markus 1204: #ifdef SMARTCARD
1205: Key **keys;
1.104 markus 1206:
1.133 markus 1207: if (options.smartcard_device != NULL &&
1.167 markus 1208: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1.292 stevesk 1209: (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) {
1.167 markus 1210: int count = 0;
1211: for (i = 0; keys[i] != NULL; i++) {
1212: count++;
1213: memmove(&options.identity_files[1], &options.identity_files[0],
1214: sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1215: memmove(&options.identity_keys[1], &options.identity_keys[0],
1216: sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1217: options.num_identity_files++;
1218: options.identity_keys[0] = keys[i];
1.194 markus 1219: options.identity_files[0] = sc_get_key_label(keys[i]);
1.167 markus 1220: }
1.168 markus 1221: if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1222: options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1.167 markus 1223: i = count;
1224: xfree(keys);
1.127 markus 1225: }
1.136 jakob 1226: #endif /* SMARTCARD */
1.275 djm 1227: if ((pw = getpwuid(original_real_uid)) == NULL)
1228: fatal("load_public_identity_files: getpwuid failed");
1229: if (gethostname(thishost, sizeof(thishost)) == -1)
1230: fatal("load_public_identity_files: gethostname: %s",
1231: strerror(errno));
1.131 millert 1232: for (; i < options.num_identity_files; i++) {
1.275 djm 1233: cp = tilde_expand_filename(options.identity_files[i],
1.131 millert 1234: original_real_uid);
1.275 djm 1235: filename = percent_expand(cp, "d", pw->pw_dir,
1.293 ! deraadt 1236: "u", pw->pw_name, "l", thishost, "h", host,
1.275 djm 1237: "r", options.user, (char *)NULL);
1238: xfree(cp);
1.131 millert 1239: public = key_load_public(filename, NULL);
1240: debug("identity file %s type %d", filename,
1241: public ? public->type : -1);
1242: xfree(options.identity_files[i]);
1243: options.identity_files[i] = filename;
1244: options.identity_keys[i] = public;
1245: }
1.214 djm 1246: }
1247:
1248: static void
1249: control_client_sighandler(int signo)
1250: {
1251: control_client_terminate = signo;
1252: }
1253:
1254: static void
1255: control_client_sigrelay(int signo)
1256: {
1257: if (control_server_pid > 1)
1258: kill(control_server_pid, signo);
1259: }
1260:
1.220 djm 1261: static int
1262: env_permitted(char *env)
1263: {
1.282 dtucker 1264: int i, ret;
1.220 djm 1265: char name[1024], *cp;
1266:
1.282 dtucker 1267: if ((cp = strchr(env, '=')) == NULL || cp == env)
1.220 djm 1268: return (0);
1.284 stevesk 1269: ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
1.282 dtucker 1270: if (ret <= 0 || (size_t)ret >= sizeof(name))
1271: fatal("env_permitted: name '%.100s...' too long", env);
1.220 djm 1272:
1273: for (i = 0; i < options.num_send_env; i++)
1274: if (match_pattern(name, options.send_env[i]))
1275: return (1);
1276:
1277: return (0);
1278: }
1279:
1.214 djm 1280: static void
1281: control_client(const char *path)
1282: {
1283: struct sockaddr_un addr;
1.228 djm 1284: int i, r, fd, sock, exitval, num_env;
1.214 djm 1285: Buffer m;
1.229 djm 1286: char *term;
1.215 djm 1287: extern char **environ;
1.229 djm 1288: u_int flags;
1.242 djm 1289:
1290: if (mux_command == 0)
1291: mux_command = SSHMUX_COMMAND_OPEN;
1292:
1293: switch (options.control_master) {
1294: case SSHCTL_MASTER_AUTO:
1295: case SSHCTL_MASTER_AUTO_ASK:
1296: debug("auto-mux: Trying existing master");
1297: /* FALLTHROUGH */
1298: case SSHCTL_MASTER_NO:
1299: break;
1300: default:
1301: return;
1302: }
1.228 djm 1303:
1.214 djm 1304: memset(&addr, '\0', sizeof(addr));
1305: addr.sun_family = AF_UNIX;
1306: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
1307: strlen(path) + 1;
1308:
1309: if (strlcpy(addr.sun_path, path,
1310: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1311: fatal("ControlPath too long");
1312:
1313: if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1314: fatal("%s socket(): %s", __func__, strerror(errno));
1315:
1.270 deraadt 1316: if (connect(sock, (struct sockaddr *)&addr, addr.sun_len) == -1) {
1.240 djm 1317: if (mux_command != SSHMUX_COMMAND_OPEN) {
1318: fatal("Control socket connect(%.100s): %s", path,
1319: strerror(errno));
1320: }
1.239 djm 1321: if (errno == ENOENT)
1.268 deraadt 1322: debug("Control socket \"%.100s\" does not exist", path);
1.239 djm 1323: else {
1.268 deraadt 1324: error("Control socket connect(%.100s): %s", path,
1.239 djm 1325: strerror(errno));
1326: }
1.268 deraadt 1327: close(sock);
1328: return;
1329: }
1.248 djm 1330:
1.268 deraadt 1331: if (stdin_null_flag) {
1332: if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1333: fatal("open(/dev/null): %s", strerror(errno));
1334: if (dup2(fd, STDIN_FILENO) == -1)
1335: fatal("dup2: %s", strerror(errno));
1336: if (fd > STDERR_FILENO)
1337: close(fd);
1338: }
1.248 djm 1339:
1.247 djm 1340: term = getenv("TERM");
1.229 djm 1341:
1342: flags = 0;
1343: if (tty_flag)
1344: flags |= SSHMUX_FLAG_TTY;
1345: if (subsystem_flag)
1346: flags |= SSHMUX_FLAG_SUBSYS;
1.247 djm 1347: if (options.forward_x11)
1348: flags |= SSHMUX_FLAG_X11_FWD;
1349: if (options.forward_agent)
1350: flags |= SSHMUX_FLAG_AGENT_FWD;
1.214 djm 1351:
1352: buffer_init(&m);
1353:
1.229 djm 1354: /* Send our command to server */
1355: buffer_put_int(&m, mux_command);
1356: buffer_put_int(&m, flags);
1.247 djm 1357: if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
1.229 djm 1358: fatal("%s: msg_send", __func__);
1359: buffer_clear(&m);
1360:
1361: /* Get authorisation status and PID of controlee */
1.214 djm 1362: if (ssh_msg_recv(sock, &m) == -1)
1363: fatal("%s: msg_recv", __func__);
1.247 djm 1364: if (buffer_get_char(&m) != SSHMUX_VER)
1.214 djm 1365: fatal("%s: wrong version", __func__);
1.216 djm 1366: if (buffer_get_int(&m) != 1)
1367: fatal("Connection to master denied");
1.214 djm 1368: control_server_pid = buffer_get_int(&m);
1369:
1370: buffer_clear(&m);
1371:
1.229 djm 1372: switch (mux_command) {
1373: case SSHMUX_COMMAND_ALIVE_CHECK:
1.234 deraadt 1374: fprintf(stderr, "Master running (pid=%d)\r\n",
1.229 djm 1375: control_server_pid);
1376: exit(0);
1377: case SSHMUX_COMMAND_TERMINATE:
1378: fprintf(stderr, "Exit request sent.\r\n");
1379: exit(0);
1380: case SSHMUX_COMMAND_OPEN:
1381: /* continue below */
1382: break;
1383: default:
1384: fatal("silly mux_command %d", mux_command);
1385: }
1386:
1387: /* SSHMUX_COMMAND_OPEN */
1.247 djm 1388: buffer_put_cstring(&m, term ? term : "");
1.214 djm 1389: buffer_append(&command, "\0", 1);
1390: buffer_put_cstring(&m, buffer_ptr(&command));
1.215 djm 1391:
1.220 djm 1392: if (options.num_send_env == 0 || environ == NULL) {
1393: buffer_put_int(&m, 0);
1.223 deraadt 1394: } else {
1.220 djm 1395: /* Pass environment */
1396: num_env = 0;
1397: for (i = 0; environ[i] != NULL; i++)
1398: if (env_permitted(environ[i]))
1399: num_env++; /* Count */
1.223 deraadt 1400:
1.220 djm 1401: buffer_put_int(&m, num_env);
1402:
1.222 dtucker 1403: for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1404: if (env_permitted(environ[i])) {
1405: num_env--;
1.220 djm 1406: buffer_put_cstring(&m, environ[i]);
1.222 dtucker 1407: }
1.220 djm 1408: }
1.214 djm 1409:
1.247 djm 1410: if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
1.214 djm 1411: fatal("%s: msg_send", __func__);
1412:
1413: mm_send_fd(sock, STDIN_FILENO);
1414: mm_send_fd(sock, STDOUT_FILENO);
1415: mm_send_fd(sock, STDERR_FILENO);
1416:
1417: /* Wait for reply, so master has a chance to gather ttymodes */
1418: buffer_clear(&m);
1419: if (ssh_msg_recv(sock, &m) == -1)
1420: fatal("%s: msg_recv", __func__);
1.247 djm 1421: if (buffer_get_char(&m) != SSHMUX_VER)
1.229 djm 1422: fatal("%s: wrong version", __func__);
1.214 djm 1423: buffer_free(&m);
1.218 djm 1424:
1.226 djm 1425: signal(SIGHUP, control_client_sighandler);
1.218 djm 1426: signal(SIGINT, control_client_sighandler);
1427: signal(SIGTERM, control_client_sighandler);
1428: signal(SIGWINCH, control_client_sigrelay);
1.214 djm 1429:
1430: if (tty_flag)
1431: enter_raw_mode();
1432:
1433: /* Stick around until the controlee closes the client_fd */
1434: exitval = 0;
1435: for (;!control_client_terminate;) {
1436: r = read(sock, &exitval, sizeof(exitval));
1437: if (r == 0) {
1438: debug2("Received EOF from master");
1439: break;
1440: }
1441: if (r > 0)
1442: debug2("Received exit status from master %d", exitval);
1443: if (r == -1 && errno != EINTR)
1444: fatal("%s: read %s", __func__, strerror(errno));
1445: }
1446:
1447: if (control_client_terminate)
1448: debug2("Exiting on signal %d", control_client_terminate);
1449:
1450: close(sock);
1451:
1452: leave_raw_mode();
1453:
1454: if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1455: fprintf(stderr, "Connection to master closed.\r\n");
1456:
1457: exit(exitval);
1.1 deraadt 1458: }