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