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