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