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