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