Annotation of src/usr.bin/ssh/ssh.c, Revision 1.260
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.260 ! stevesk 43: RCSID("$OpenBSD: ssh.c,v 1.259 2006/02/08 14:31:30 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.241 djm 634: snprintf(buf, sizeof(buf), "%d", options.port);
635: cp = tilde_expand_filename(options.control_path,
636: original_real_uid);
637: options.control_path = percent_expand(cp, "p", buf, "h", host,
638: "r", options.user, (char *)NULL);
639: xfree(cp);
1.214 djm 640: }
1.240 djm 641: if (mux_command != 0 && options.control_path == NULL)
642: fatal("No ControlPath specified for \"-O\" command");
1.242 djm 643: if (options.control_path != NULL)
1.237 jakob 644: control_client(options.control_path);
1.214 djm 645:
1.77 markus 646: /* Open a connection to the remote host. */
1.203 djm 647: if (ssh_connect(host, &hostaddr, options.port,
1.196 djm 648: options.address_family, options.connection_attempts,
1.177 markus 649: original_effective_uid == 0 && options.use_privileged_port,
1.179 markus 650: options.proxy_command) != 0)
1.257 dtucker 651: exit(255);
1.31 markus 652:
1.33 markus 653: /*
654: * If we successfully made the connection, load the host private key
655: * in case we will need it later for combined rsa-rhosts
656: * authentication. This must be done before releasing extra
657: * privileges, because the file is only readable by root.
1.174 markus 658: * If we cannot access the private keys, load the public keys
659: * instead and try to execute the ssh-keysign helper instead.
1.33 markus 660: */
1.112 markus 661: sensitive_data.nkeys = 0;
662: sensitive_data.keys = NULL;
1.173 markus 663: sensitive_data.external_keysign = 0;
1.178 markus 664: if (options.rhosts_rsa_authentication ||
665: options.hostbased_authentication) {
1.112 markus 666: sensitive_data.nkeys = 3;
1.180 deraadt 667: sensitive_data.keys = xmalloc(sensitive_data.nkeys *
668: sizeof(Key));
1.177 markus 669:
670: PRIV_START;
1.112 markus 671: sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.105 markus 672: _PATH_HOST_KEY_FILE, "", NULL);
1.112 markus 673: sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
674: _PATH_HOST_DSA_KEY_FILE, "", NULL);
675: sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
676: _PATH_HOST_RSA_KEY_FILE, "", NULL);
1.177 markus 677: PRIV_END;
1.173 markus 678:
1.181 markus 679: if (options.hostbased_authentication == 1 &&
680: sensitive_data.keys[0] == NULL &&
1.173 markus 681: sensitive_data.keys[1] == NULL &&
682: sensitive_data.keys[2] == NULL) {
683: sensitive_data.keys[1] = key_load_public(
684: _PATH_HOST_DSA_KEY_FILE, NULL);
685: sensitive_data.keys[2] = key_load_public(
686: _PATH_HOST_RSA_KEY_FILE, NULL);
687: sensitive_data.external_keysign = 1;
688: }
1.31 markus 689: }
1.33 markus 690: /*
691: * Get rid of any extra privileges that we may have. We will no
692: * longer need them. Also, extra privileges could make it very hard
693: * to read identity files and other non-world-readable files from the
694: * user's home directory if it happens to be on a NFS volume where
695: * root is mapped to nobody.
696: */
1.225 dtucker 697: if (original_effective_uid == 0) {
698: PRIV_START;
699: permanently_set_uid(pw);
700: }
1.31 markus 701:
1.33 markus 702: /*
703: * Now that we are back to our own permissions, create ~/.ssh
1.254 djm 704: * directory if it doesn't already exist.
1.33 markus 705: */
1.138 jakob 706: snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.31 markus 707: if (stat(buf, &st) < 0)
1.57 djm 708: if (mkdir(buf, 0700) < 0)
1.31 markus 709: error("Could not create directory '%.200s'.", buf);
710:
1.104 markus 711: /* load options.identity_files */
712: load_public_identity_files();
713:
714: /* Expand ~ in known host file names. */
715: /* XXX mem-leaks: */
1.72 markus 716: options.system_hostfile =
717: tilde_expand_filename(options.system_hostfile, original_real_uid);
718: options.user_hostfile =
719: tilde_expand_filename(options.user_hostfile, original_real_uid);
720: options.system_hostfile2 =
721: tilde_expand_filename(options.system_hostfile2, original_real_uid);
722: options.user_hostfile2 =
723: tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.149 markus 724:
725: signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.31 markus 726:
727: /* Log into the remote system. This never returns if the login fails. */
1.173 markus 728: ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
1.31 markus 729:
1.112 markus 730: /* We no longer need the private host keys. Clear them now. */
731: if (sensitive_data.nkeys != 0) {
732: for (i = 0; i < sensitive_data.nkeys; i++) {
733: if (sensitive_data.keys[i] != NULL) {
734: /* Destroys contents safely */
735: debug3("clear hostkey %d", i);
736: key_free(sensitive_data.keys[i]);
737: sensitive_data.keys[i] = NULL;
738: }
739: }
740: xfree(sensitive_data.keys);
1.134 markus 741: }
742: for (i = 0; i < options.num_identity_files; i++) {
743: if (options.identity_files[i]) {
744: xfree(options.identity_files[i]);
745: options.identity_files[i] = NULL;
746: }
747: if (options.identity_keys[i]) {
748: key_free(options.identity_keys[i]);
749: options.identity_keys[i] = NULL;
750: }
1.112 markus 751: }
1.31 markus 752:
1.45 markus 753: exit_status = compat20 ? ssh_session2() : ssh_session();
754: packet_close();
1.186 djm 755:
1.214 djm 756: if (options.control_path != NULL && control_fd != -1)
757: unlink(options.control_path);
758:
1.186 djm 759: /*
1.203 djm 760: * Send SIGHUP to proxy command if used. We don't wait() in
1.186 djm 761: * case it hangs and instead rely on init to reap the child
762: */
763: if (proxy_command_pid > 1)
764: kill(proxy_command_pid, SIGHUP);
765:
1.45 markus 766: return exit_status;
767: }
768:
1.126 itojun 769: static void
1.70 markus 770: ssh_init_forwarding(void)
771: {
1.86 markus 772: int success = 0;
1.70 markus 773: int i;
1.86 markus 774:
1.70 markus 775: /* Initiate local TCP/IP port forwardings. */
776: for (i = 0; i < options.num_local_forwards; i++) {
1.232 djm 777: debug("Local connections to %.200s:%d forwarded to remote "
778: "address %.200s:%d",
1.234 deraadt 779: (options.local_forwards[i].listen_host == NULL) ?
780: (options.gateway_ports ? "*" : "LOCALHOST") :
1.232 djm 781: options.local_forwards[i].listen_host,
782: options.local_forwards[i].listen_port,
783: options.local_forwards[i].connect_host,
784: options.local_forwards[i].connect_port);
1.158 markus 785: success += channel_setup_local_fwd_listener(
1.232 djm 786: options.local_forwards[i].listen_host,
787: options.local_forwards[i].listen_port,
788: options.local_forwards[i].connect_host,
789: options.local_forwards[i].connect_port,
1.70 markus 790: options.gateway_ports);
791: }
1.86 markus 792: if (i > 0 && success == 0)
793: error("Could not request local forwarding.");
1.70 markus 794:
795: /* Initiate remote TCP/IP port forwardings. */
796: for (i = 0; i < options.num_remote_forwards; i++) {
1.232 djm 797: debug("Remote connections from %.200s:%d forwarded to "
798: "local address %.200s:%d",
1.248 djm 799: (options.remote_forwards[i].listen_host == NULL) ?
1.253 djm 800: "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232 djm 801: options.remote_forwards[i].listen_port,
802: options.remote_forwards[i].connect_host,
803: options.remote_forwards[i].connect_port);
1.70 markus 804: channel_request_remote_forwarding(
1.232 djm 805: options.remote_forwards[i].listen_host,
806: options.remote_forwards[i].listen_port,
807: options.remote_forwards[i].connect_host,
808: options.remote_forwards[i].connect_port);
1.70 markus 809: }
810: }
811:
1.126 itojun 812: static void
1.70 markus 813: check_agent_present(void)
814: {
815: if (options.forward_agent) {
1.254 djm 816: /* Clear agent forwarding if we don't have an agent. */
1.185 stevesk 817: if (!ssh_agent_present())
1.70 markus 818: options.forward_agent = 0;
819: }
820: }
821:
1.126 itojun 822: static int
1.45 markus 823: ssh_session(void)
824: {
825: int type;
826: int interactive = 0;
827: int have_tty = 0;
828: struct winsize ws;
829: char *cp;
1.243 djm 830: const char *display;
1.45 markus 831:
1.31 markus 832: /* Enable compression if requested. */
833: if (options.compression) {
834: debug("Requesting compression at level %d.", options.compression_level);
835:
836: if (options.compression_level < 1 || options.compression_level > 9)
837: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
838:
839: /* Send the request. */
840: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
841: packet_put_int(options.compression_level);
842: packet_send();
843: packet_write_wait();
1.156 markus 844: type = packet_read();
1.31 markus 845: if (type == SSH_SMSG_SUCCESS)
846: packet_start_compression(options.compression_level);
847: else if (type == SSH_SMSG_FAILURE)
1.191 itojun 848: logit("Warning: Remote host refused compression.");
1.31 markus 849: else
850: packet_disconnect("Protocol error waiting for compression response.");
851: }
852: /* Allocate a pseudo tty if appropriate. */
853: if (tty_flag) {
854: debug("Requesting pty.");
855:
856: /* Start the packet. */
857: packet_start(SSH_CMSG_REQUEST_PTY);
858:
859: /* Store TERM in the packet. There is no limit on the
860: length of the string. */
861: cp = getenv("TERM");
862: if (!cp)
863: cp = "";
1.124 markus 864: packet_put_cstring(cp);
1.31 markus 865:
866: /* Store window size in the packet. */
867: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
868: memset(&ws, 0, sizeof(ws));
869: packet_put_int(ws.ws_row);
870: packet_put_int(ws.ws_col);
871: packet_put_int(ws.ws_xpixel);
872: packet_put_int(ws.ws_ypixel);
873:
874: /* Store tty modes in the packet. */
1.115 stevesk 875: tty_make_modes(fileno(stdin), NULL);
1.31 markus 876:
877: /* Send the packet, and wait for it to leave. */
878: packet_send();
879: packet_write_wait();
880:
881: /* Read response from the server. */
1.156 markus 882: type = packet_read();
1.43 markus 883: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 884: interactive = 1;
1.45 markus 885: have_tty = 1;
1.43 markus 886: } else if (type == SSH_SMSG_FAILURE)
1.191 itojun 887: logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
1.31 markus 888: else
889: packet_disconnect("Protocol error waiting for pty request response.");
890: }
891: /* Request X11 forwarding if enabled and DISPLAY is set. */
1.243 djm 892: display = getenv("DISPLAY");
893: if (options.forward_x11 && display != NULL) {
1.150 stevesk 894: char *proto, *data;
1.50 markus 895: /* Get reasonable local authentication information. */
1.243 djm 896: client_x11_get_proto(display, options.xauth_location,
897: options.forward_x11_trusted, &proto, &data);
1.50 markus 898: /* Request forwarding with authentication spoofing. */
1.31 markus 899: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 900: x11_request_forwarding_with_spoofing(0, display, proto, data);
1.31 markus 901:
902: /* Read response from the server. */
1.156 markus 903: type = packet_read();
1.31 markus 904: if (type == SSH_SMSG_SUCCESS) {
905: interactive = 1;
1.50 markus 906: } else if (type == SSH_SMSG_FAILURE) {
1.191 itojun 907: logit("Warning: Remote host denied X11 forwarding.");
1.50 markus 908: } else {
1.31 markus 909: packet_disconnect("Protocol error waiting for X11 forwarding");
1.50 markus 910: }
1.31 markus 911: }
912: /* Tell the packet module whether this is an interactive session. */
1.80 markus 913: packet_set_interactive(interactive);
1.31 markus 914:
915: /* Request authentication agent forwarding if appropriate. */
1.70 markus 916: check_agent_present();
917:
1.31 markus 918: if (options.forward_agent) {
919: debug("Requesting authentication agent forwarding.");
920: auth_request_forwarding();
921:
922: /* Read response from the server. */
1.156 markus 923: type = packet_read();
1.155 markus 924: packet_check_eom();
1.31 markus 925: if (type != SSH_SMSG_SUCCESS)
1.191 itojun 926: logit("Warning: Remote host denied authentication agent forwarding.");
1.31 markus 927: }
928:
1.70 markus 929: /* Initiate port forwardings. */
930: ssh_init_forwarding();
1.34 markus 931:
932: /* If requested, let ssh continue in the background. */
1.48 markus 933: if (fork_after_authentication_flag)
1.34 markus 934: if (daemon(1, 1) < 0)
935: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 936:
1.33 markus 937: /*
938: * If a command was specified on the command line, execute the
939: * command now. Otherwise request the server to start a shell.
940: */
1.31 markus 941: if (buffer_len(&command) > 0) {
942: int len = buffer_len(&command);
943: if (len > 900)
944: len = 900;
1.152 stevesk 945: debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.31 markus 946: packet_start(SSH_CMSG_EXEC_CMD);
947: packet_put_string(buffer_ptr(&command), buffer_len(&command));
948: packet_send();
949: packet_write_wait();
950: } else {
951: debug("Requesting shell.");
952: packet_start(SSH_CMSG_EXEC_SHELL);
953: packet_send();
954: packet_write_wait();
955: }
956:
957: /* Enter the interactive session. */
1.119 stevesk 958: return client_loop(have_tty, tty_flag ?
959: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.45 markus 960: }
961:
1.126 itojun 962: static void
1.214 djm 963: ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
1.89 markus 964: {
965: int id, len;
966:
967: id = packet_get_int();
968: len = buffer_len(&command);
1.100 markus 969: if (len > 900)
970: len = 900;
1.155 markus 971: packet_check_eom();
1.89 markus 972: if (type == SSH2_MSG_CHANNEL_FAILURE)
973: fatal("Request for subsystem '%.*s' failed on channel %d",
1.152 stevesk 974: len, (u_char *)buffer_ptr(&command), id);
1.170 markus 975: }
976:
977: void
1.206 markus 978: client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
1.170 markus 979: {
980: int i;
981:
982: i = client_global_request_id++;
1.206 markus 983: if (i >= options.num_remote_forwards)
1.170 markus 984: return;
985: debug("remote forward %s for: listen %d, connect %s:%d",
986: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1.232 djm 987: options.remote_forwards[i].listen_port,
988: options.remote_forwards[i].connect_host,
989: options.remote_forwards[i].connect_port);
1.170 markus 990: if (type == SSH2_MSG_REQUEST_FAILURE)
1.232 djm 991: logit("Warning: remote port forwarding failed for listen "
992: "port %d", options.remote_forwards[i].listen_port);
1.89 markus 993: }
994:
1.126 itojun 995: static void
1.214 djm 996: ssh_control_listener(void)
1.45 markus 997: {
1.214 djm 998: struct sockaddr_un addr;
999: mode_t old_umask;
1.223 deraadt 1000:
1.242 djm 1001: if (options.control_path == NULL ||
1002: options.control_master == SSHCTL_MASTER_NO)
1.214 djm 1003: return;
1004:
1.242 djm 1005: debug("setting up multiplex master socket");
1006:
1.214 djm 1007: memset(&addr, '\0', sizeof(addr));
1008: addr.sun_family = AF_UNIX;
1009: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
1010: strlen(options.control_path) + 1;
1011:
1012: if (strlcpy(addr.sun_path, options.control_path,
1013: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1014: fatal("ControlPath too long");
1015:
1016: if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1.252 stevesk 1017: fatal("%s socket(): %s", __func__, strerror(errno));
1.214 djm 1018:
1019: old_umask = umask(0177);
1020: if (bind(control_fd, (struct sockaddr*)&addr, addr.sun_len) == -1) {
1021: control_fd = -1;
1.238 djm 1022: if (errno == EINVAL || errno == EADDRINUSE)
1.214 djm 1023: fatal("ControlSocket %s already exists",
1024: options.control_path);
1025: else
1.252 stevesk 1026: fatal("%s bind(): %s", __func__, strerror(errno));
1.214 djm 1027: }
1028: umask(old_umask);
1.80 markus 1029:
1.214 djm 1030: if (listen(control_fd, 64) == -1)
1.252 stevesk 1031: fatal("%s listen(): %s", __func__, strerror(errno));
1.31 markus 1032:
1.214 djm 1033: set_nonblock(control_fd);
1034: }
1.45 markus 1035:
1.214 djm 1036: /* request pty/x11/agent/tcpfwd/shell for channel */
1037: static void
1038: ssh_session2_setup(int id, void *arg)
1039: {
1.215 djm 1040: extern char **environ;
1.243 djm 1041: const char *display;
1042: int interactive = tty_flag;
1.215 djm 1043:
1.248 djm 1044: display = getenv("DISPLAY");
1.243 djm 1045: if (options.forward_x11 && display != NULL) {
1.150 stevesk 1046: char *proto, *data;
1.50 markus 1047: /* Get reasonable local authentication information. */
1.243 djm 1048: client_x11_get_proto(display, options.xauth_location,
1049: options.forward_x11_trusted, &proto, &data);
1.50 markus 1050: /* Request forwarding with authentication spoofing. */
1051: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 1052: x11_request_forwarding_with_spoofing(id, display, proto, data);
1.80 markus 1053: interactive = 1;
1.50 markus 1054: /* XXX wait for reply */
1055: }
1056:
1.70 markus 1057: check_agent_present();
1058: if (options.forward_agent) {
1059: debug("Requesting authentication agent forwarding.");
1060: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1061: packet_send();
1.212 djm 1062: }
1063:
1.256 reyk 1064: if (options.tun_open != SSH_TUNMODE_NO) {
1.255 reyk 1065: Channel *c;
1066: int fd;
1067:
1068: debug("Requesting tun.");
1.256 reyk 1069: if ((fd = tun_open(options.tun_local,
1070: options.tun_open)) >= 0) {
1.255 reyk 1071: c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1072: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1073: 0, "tun", 1);
1074: c->datagram = 1;
1075: packet_start(SSH2_MSG_CHANNEL_OPEN);
1076: packet_put_cstring("tun@openssh.com");
1077: packet_put_int(c->self);
1078: packet_put_int(c->local_window_max);
1079: packet_put_int(c->local_maxpacket);
1.256 reyk 1080: packet_put_int(options.tun_open);
1.255 reyk 1081: packet_put_int(options.tun_remote);
1082: packet_send();
1083: }
1084: }
1085:
1.214 djm 1086: client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
1.215 djm 1087: NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
1.90 markus 1088:
1.80 markus 1089: packet_set_interactive(interactive);
1.45 markus 1090: }
1091:
1.143 markus 1092: /* open new channel for a session */
1.126 itojun 1093: static int
1.143 markus 1094: ssh_session2_open(void)
1.45 markus 1095: {
1.118 markus 1096: Channel *c;
1097: int window, packetmax, in, out, err;
1.60 markus 1098:
1.62 markus 1099: if (stdin_null_flag) {
1.93 itojun 1100: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1101: } else {
1102: in = dup(STDIN_FILENO);
1103: }
1.60 markus 1104: out = dup(STDOUT_FILENO);
1105: err = dup(STDERR_FILENO);
1.45 markus 1106:
1107: if (in < 0 || out < 0 || err < 0)
1.62 markus 1108: fatal("dup() in/out/err failed");
1.45 markus 1109:
1.69 markus 1110: /* enable nonblocking unless tty */
1111: if (!isatty(in))
1112: set_nonblock(in);
1113: if (!isatty(out))
1114: set_nonblock(out);
1115: if (!isatty(err))
1116: set_nonblock(err);
1117:
1.65 markus 1118: window = CHAN_SES_WINDOW_DEFAULT;
1119: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1120: if (tty_flag) {
1121: window >>= 1;
1122: packetmax >>= 1;
1.45 markus 1123: }
1.118 markus 1124: c = channel_new(
1.45 markus 1125: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1126: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1127: "client-session", /*nonblock*/0);
1.45 markus 1128:
1.143 markus 1129: debug3("ssh_session2_open: channel_new: %d", c->self);
1.106 markus 1130:
1.122 markus 1131: channel_send_open(c->self);
1.143 markus 1132: if (!no_shell_flag)
1.214 djm 1133: channel_register_confirm(c->self, ssh_session2_setup, NULL);
1.106 markus 1134:
1.118 markus 1135: return c->self;
1.106 markus 1136: }
1137:
1.126 itojun 1138: static int
1.106 markus 1139: ssh_session2(void)
1140: {
1.143 markus 1141: int id = -1;
1.106 markus 1142:
1143: /* XXX should be pre-session */
1144: ssh_init_forwarding();
1.214 djm 1145: ssh_control_listener();
1.106 markus 1146:
1.143 markus 1147: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1148: id = ssh_session2_open();
1.255 reyk 1149:
1150: /* Execute a local command */
1151: if (options.local_command != NULL &&
1152: options.permit_local_command)
1153: ssh_local_cmd(options.local_command);
1.106 markus 1154:
1155: /* If requested, let ssh continue in the background. */
1156: if (fork_after_authentication_flag)
1157: if (daemon(1, 1) < 0)
1158: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 1159:
1.119 stevesk 1160: return client_loop(tty_flag, tty_flag ?
1161: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 1162: }
1163:
1.126 itojun 1164: static void
1.104 markus 1165: load_public_identity_files(void)
1166: {
1167: char *filename;
1.167 markus 1168: int i = 0;
1.104 markus 1169: Key *public;
1.167 markus 1170: #ifdef SMARTCARD
1171: Key **keys;
1.104 markus 1172:
1.133 markus 1173: if (options.smartcard_device != NULL &&
1.167 markus 1174: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1175: (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1176: int count = 0;
1177: for (i = 0; keys[i] != NULL; i++) {
1178: count++;
1179: memmove(&options.identity_files[1], &options.identity_files[0],
1180: sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1181: memmove(&options.identity_keys[1], &options.identity_keys[0],
1182: sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1183: options.num_identity_files++;
1184: options.identity_keys[0] = keys[i];
1.194 markus 1185: options.identity_files[0] = sc_get_key_label(keys[i]);
1.167 markus 1186: }
1.168 markus 1187: if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1188: options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1.167 markus 1189: i = count;
1190: xfree(keys);
1.127 markus 1191: }
1.136 jakob 1192: #endif /* SMARTCARD */
1.131 millert 1193: for (; i < options.num_identity_files; i++) {
1194: filename = tilde_expand_filename(options.identity_files[i],
1195: original_real_uid);
1196: public = key_load_public(filename, NULL);
1197: debug("identity file %s type %d", filename,
1198: public ? public->type : -1);
1199: xfree(options.identity_files[i]);
1200: options.identity_files[i] = filename;
1201: options.identity_keys[i] = public;
1202: }
1.214 djm 1203: }
1204:
1205: static void
1206: control_client_sighandler(int signo)
1207: {
1208: control_client_terminate = signo;
1209: }
1210:
1211: static void
1212: control_client_sigrelay(int signo)
1213: {
1214: if (control_server_pid > 1)
1215: kill(control_server_pid, signo);
1216: }
1217:
1.220 djm 1218: static int
1219: env_permitted(char *env)
1220: {
1221: int i;
1222: char name[1024], *cp;
1223:
1224: strlcpy(name, env, sizeof(name));
1225: if ((cp = strchr(name, '=')) == NULL)
1226: return (0);
1227:
1228: *cp = '\0';
1229:
1230: for (i = 0; i < options.num_send_env; i++)
1231: if (match_pattern(name, options.send_env[i]))
1232: return (1);
1233:
1234: return (0);
1235: }
1236:
1.214 djm 1237: static void
1238: control_client(const char *path)
1239: {
1240: struct sockaddr_un addr;
1.228 djm 1241: int i, r, fd, sock, exitval, num_env;
1.214 djm 1242: Buffer m;
1.229 djm 1243: char *term;
1.215 djm 1244: extern char **environ;
1.229 djm 1245: u_int flags;
1.242 djm 1246:
1247: if (mux_command == 0)
1248: mux_command = SSHMUX_COMMAND_OPEN;
1249:
1250: switch (options.control_master) {
1251: case SSHCTL_MASTER_AUTO:
1252: case SSHCTL_MASTER_AUTO_ASK:
1253: debug("auto-mux: Trying existing master");
1254: /* FALLTHROUGH */
1255: case SSHCTL_MASTER_NO:
1256: break;
1257: default:
1258: return;
1259: }
1.228 djm 1260:
1.214 djm 1261: memset(&addr, '\0', sizeof(addr));
1262: addr.sun_family = AF_UNIX;
1263: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
1264: strlen(path) + 1;
1265:
1266: if (strlcpy(addr.sun_path, path,
1267: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1268: fatal("ControlPath too long");
1269:
1270: if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1271: fatal("%s socket(): %s", __func__, strerror(errno));
1272:
1.237 jakob 1273: if (connect(sock, (struct sockaddr*)&addr, addr.sun_len) == -1) {
1.240 djm 1274: if (mux_command != SSHMUX_COMMAND_OPEN) {
1275: fatal("Control socket connect(%.100s): %s", path,
1276: strerror(errno));
1277: }
1.239 djm 1278: if (errno == ENOENT)
1279: debug("Control socket \"%.100s\" does not exist", path);
1280: else {
1281: error("Control socket connect(%.100s): %s", path,
1282: strerror(errno));
1283: }
1.237 jakob 1284: close(sock);
1285: return;
1286: }
1.248 djm 1287:
1.237 jakob 1288: if (stdin_null_flag) {
1289: if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1290: fatal("open(/dev/null): %s", strerror(errno));
1291: if (dup2(fd, STDIN_FILENO) == -1)
1292: fatal("dup2: %s", strerror(errno));
1293: if (fd > STDERR_FILENO)
1294: close(fd);
1295: }
1.248 djm 1296:
1.247 djm 1297: term = getenv("TERM");
1.229 djm 1298:
1299: flags = 0;
1300: if (tty_flag)
1301: flags |= SSHMUX_FLAG_TTY;
1302: if (subsystem_flag)
1303: flags |= SSHMUX_FLAG_SUBSYS;
1.247 djm 1304: if (options.forward_x11)
1305: flags |= SSHMUX_FLAG_X11_FWD;
1306: if (options.forward_agent)
1307: flags |= SSHMUX_FLAG_AGENT_FWD;
1.214 djm 1308:
1309: buffer_init(&m);
1310:
1.229 djm 1311: /* Send our command to server */
1312: buffer_put_int(&m, mux_command);
1313: buffer_put_int(&m, flags);
1.247 djm 1314: if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
1.229 djm 1315: fatal("%s: msg_send", __func__);
1316: buffer_clear(&m);
1317:
1318: /* Get authorisation status and PID of controlee */
1.214 djm 1319: if (ssh_msg_recv(sock, &m) == -1)
1320: fatal("%s: msg_recv", __func__);
1.247 djm 1321: if (buffer_get_char(&m) != SSHMUX_VER)
1.214 djm 1322: fatal("%s: wrong version", __func__);
1.216 djm 1323: if (buffer_get_int(&m) != 1)
1324: fatal("Connection to master denied");
1.214 djm 1325: control_server_pid = buffer_get_int(&m);
1326:
1327: buffer_clear(&m);
1328:
1.229 djm 1329: switch (mux_command) {
1330: case SSHMUX_COMMAND_ALIVE_CHECK:
1.234 deraadt 1331: fprintf(stderr, "Master running (pid=%d)\r\n",
1.229 djm 1332: control_server_pid);
1333: exit(0);
1334: case SSHMUX_COMMAND_TERMINATE:
1335: fprintf(stderr, "Exit request sent.\r\n");
1336: exit(0);
1337: case SSHMUX_COMMAND_OPEN:
1338: /* continue below */
1339: break;
1340: default:
1341: fatal("silly mux_command %d", mux_command);
1342: }
1343:
1344: /* SSHMUX_COMMAND_OPEN */
1.247 djm 1345: buffer_put_cstring(&m, term ? term : "");
1.214 djm 1346: buffer_append(&command, "\0", 1);
1347: buffer_put_cstring(&m, buffer_ptr(&command));
1.215 djm 1348:
1.220 djm 1349: if (options.num_send_env == 0 || environ == NULL) {
1350: buffer_put_int(&m, 0);
1.223 deraadt 1351: } else {
1.220 djm 1352: /* Pass environment */
1353: num_env = 0;
1354: for (i = 0; environ[i] != NULL; i++)
1355: if (env_permitted(environ[i]))
1356: num_env++; /* Count */
1.223 deraadt 1357:
1.220 djm 1358: buffer_put_int(&m, num_env);
1359:
1.222 dtucker 1360: for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1361: if (env_permitted(environ[i])) {
1362: num_env--;
1.220 djm 1363: buffer_put_cstring(&m, environ[i]);
1.222 dtucker 1364: }
1.220 djm 1365: }
1.214 djm 1366:
1.247 djm 1367: if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
1.214 djm 1368: fatal("%s: msg_send", __func__);
1369:
1370: mm_send_fd(sock, STDIN_FILENO);
1371: mm_send_fd(sock, STDOUT_FILENO);
1372: mm_send_fd(sock, STDERR_FILENO);
1373:
1374: /* Wait for reply, so master has a chance to gather ttymodes */
1375: buffer_clear(&m);
1376: if (ssh_msg_recv(sock, &m) == -1)
1377: fatal("%s: msg_recv", __func__);
1.247 djm 1378: if (buffer_get_char(&m) != SSHMUX_VER)
1.229 djm 1379: fatal("%s: wrong version", __func__);
1.214 djm 1380: buffer_free(&m);
1.218 djm 1381:
1.226 djm 1382: signal(SIGHUP, control_client_sighandler);
1.218 djm 1383: signal(SIGINT, control_client_sighandler);
1384: signal(SIGTERM, control_client_sighandler);
1385: signal(SIGWINCH, control_client_sigrelay);
1.214 djm 1386:
1387: if (tty_flag)
1388: enter_raw_mode();
1389:
1390: /* Stick around until the controlee closes the client_fd */
1391: exitval = 0;
1392: for (;!control_client_terminate;) {
1393: r = read(sock, &exitval, sizeof(exitval));
1394: if (r == 0) {
1395: debug2("Received EOF from master");
1396: break;
1397: }
1398: if (r > 0)
1399: debug2("Received exit status from master %d", exitval);
1400: if (r == -1 && errno != EINTR)
1401: fatal("%s: read %s", __func__, strerror(errno));
1402: }
1403:
1404: if (control_client_terminate)
1405: debug2("Exiting on signal %d", control_client_terminate);
1406:
1407: close(sock);
1408:
1409: leave_raw_mode();
1410:
1411: if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1412: fprintf(stderr, "Connection to master closed.\r\n");
1413:
1414: exit(exitval);
1.1 deraadt 1415: }