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