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