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