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