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