Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 1 | /* $OpenBSD: ssh.c,v 1.281 2006/07/09 15:15:11 stevesk Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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. |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 17 | * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 41 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 42 | |
| 43 | #include "includes.h" |
Damien Miller | cd4223c | 2006-03-15 11:22:47 +1100 | [diff] [blame] | 44 | |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 45 | #include <sys/types.h> |
| 46 | #ifdef HAVE_SYS_STAT_H |
| 47 | # include <sys/stat.h> |
| 48 | #endif |
Damien Miller | cd4223c | 2006-03-15 11:22:47 +1100 | [diff] [blame] | 49 | #include <sys/resource.h> |
Damien Miller | 17e91c0 | 2006-03-15 11:28:34 +1100 | [diff] [blame] | 50 | #include <sys/ioctl.h> |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 51 | #include <sys/socket.h> |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 52 | #include <sys/un.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 53 | |
Damien Miller | c7b0636 | 2006-03-15 11:53:45 +1100 | [diff] [blame] | 54 | #include <ctype.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 55 | #include <fcntl.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 56 | #ifdef HAVE_PATHS_H |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 57 | #include <paths.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 58 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 59 | #include <pwd.h> |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 60 | #include <signal.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 61 | |
| 62 | #include <openssl/evp.h> |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 63 | #include <openssl/err.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 65 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 66 | #include "ssh1.h" |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 67 | #include "ssh2.h" |
| 68 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 69 | #include "cipher.h" |
| 70 | #include "xmalloc.h" |
| 71 | #include "packet.h" |
| 72 | #include "buffer.h" |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 73 | #include "bufaux.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 74 | #include "channels.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 75 | #include "key.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 76 | #include "authfd.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 77 | #include "authfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 78 | #include "pathnames.h" |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 79 | #include "dispatch.h" |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 80 | #include "clientloop.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 81 | #include "log.h" |
| 82 | #include "readconf.h" |
| 83 | #include "sshconnect.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 84 | #include "misc.h" |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 85 | #include "kex.h" |
| 86 | #include "mac.h" |
Darren Tucker | 06f2bd8 | 2004-05-13 16:06:46 +1000 | [diff] [blame] | 87 | #include "sshpty.h" |
Darren Tucker | 46bc075 | 2004-05-02 22:11:30 +1000 | [diff] [blame] | 88 | #include "match.h" |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 89 | #include "msg.h" |
| 90 | #include "monitor_fdpass.h" |
Darren Tucker | 25f60a7 | 2004-08-15 17:23:34 +1000 | [diff] [blame] | 91 | #include "uidswap.h" |
Damien Miller | b757677 | 2006-07-10 20:23:39 +1000 | [diff] [blame] | 92 | #include "version.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 93 | |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 94 | #ifdef SMARTCARD |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 95 | #include "scard.h" |
Ben Lindstrom | bcc1808 | 2001-08-06 21:59:25 +0000 | [diff] [blame] | 96 | #endif |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 97 | |
Damien Miller | 3f90587 | 1999-11-15 17:10:57 +1100 | [diff] [blame] | 98 | extern char *__progname; |
Damien Miller | 3f90587 | 1999-11-15 17:10:57 +1100 | [diff] [blame] | 99 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 100 | /* Flag indicating whether debug mode is on. This can be set on the command line. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 101 | int debug_flag = 0; |
| 102 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 103 | /* Flag indicating whether a tty should be allocated */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 104 | int tty_flag = 0; |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 105 | int no_tty_flag = 0; |
| 106 | int force_tty_flag = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 107 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 108 | /* don't exec a shell */ |
| 109 | int no_shell_flag = 0; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 110 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 111 | /* |
| 112 | * Flag indicating that nothing should be read from stdin. This can be set |
| 113 | * on the command line. |
| 114 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | int stdin_null_flag = 0; |
| 116 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 117 | /* |
| 118 | * Flag indicating that ssh should fork after authentication. This is useful |
Ben Lindstrom | f666fec | 2002-06-06 19:51:58 +0000 | [diff] [blame] | 119 | * so that the passphrase can be entered manually, and then ssh goes to the |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 120 | * background. |
| 121 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 122 | int fork_after_authentication_flag = 0; |
| 123 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 124 | /* |
| 125 | * General data structure for command line options and options configurable |
| 126 | * in configuration files. See readconf.h. |
| 127 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 128 | Options options; |
| 129 | |
Ben Lindstrom | 14f31ab | 2001-09-12 17:48:04 +0000 | [diff] [blame] | 130 | /* optional user configfile */ |
| 131 | char *config = NULL; |
| 132 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 133 | /* |
| 134 | * Name of the host we are connecting to. This is the name given on the |
| 135 | * command line, or the HostName specified for the user-supplied name in a |
| 136 | * configuration file. |
| 137 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 138 | char *host; |
| 139 | |
| 140 | /* socket address the host resolves to */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 141 | struct sockaddr_storage hostaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 142 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 143 | /* Private host keys. */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 144 | Sensitive sensitive_data; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 145 | |
| 146 | /* Original real UID. */ |
| 147 | uid_t original_real_uid; |
Ben Lindstrom | f9c4884 | 2002-06-11 16:37:51 +0000 | [diff] [blame] | 148 | uid_t original_effective_uid; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 150 | /* command to be executed */ |
| 151 | Buffer command; |
| 152 | |
Damien Miller | 832562e | 2001-01-30 09:30:01 +1100 | [diff] [blame] | 153 | /* Should we execute a command or invoke a subsystem? */ |
| 154 | int subsystem_flag = 0; |
| 155 | |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 156 | /* # of replies received for global requests */ |
| 157 | static int client_global_request_id = 0; |
| 158 | |
Damien Miller | 8c4e18a | 2002-09-19 12:05:02 +1000 | [diff] [blame] | 159 | /* pid of proxycommand child process */ |
| 160 | pid_t proxy_command_pid = 0; |
| 161 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 162 | /* fd to control socket */ |
| 163 | int control_fd = -1; |
| 164 | |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 165 | /* Multiplexing control command */ |
Darren Tucker | 0814d31 | 2005-06-01 23:08:51 +1000 | [diff] [blame] | 166 | static u_int mux_command = 0; |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 167 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 168 | /* Only used in control client mode */ |
| 169 | volatile sig_atomic_t control_client_terminate = 0; |
| 170 | u_int control_server_pid = 0; |
| 171 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 172 | /* Prints a help message to the user. This function never returns. */ |
| 173 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 174 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 175 | usage(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 176 | { |
Damien Miller | 5095510 | 2004-03-22 09:34:58 +1100 | [diff] [blame] | 177 | fprintf(stderr, |
Darren Tucker | 9c6bf32 | 2004-12-03 14:10:19 +1100 | [diff] [blame] | 178 | "usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n" |
Darren Tucker | 895d698 | 2005-10-03 18:18:05 +1000 | [diff] [blame] | 179 | " [-D [bind_address:]port] [-e escape_char] [-F configfile]\n" |
Damien Miller | 02faece | 2005-03-02 12:04:32 +1100 | [diff] [blame] | 180 | " [-i identity_file] [-L [bind_address:]port:host:hostport]\n" |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 181 | " [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n" |
Damien Miller | 02faece | 2005-03-02 12:04:32 +1100 | [diff] [blame] | 182 | " [-R [bind_address:]port:host:hostport] [-S ctl_path]\n" |
Damien Miller | 991dba4 | 2006-07-10 20:16:27 +1000 | [diff] [blame] | 183 | " [-w local_tun[:remote_tun]] [user@]hostname [command]\n" |
Damien Miller | 5095510 | 2004-03-22 09:34:58 +1100 | [diff] [blame] | 184 | ); |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 185 | exit(255); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 186 | } |
| 187 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 188 | static int ssh_session(void); |
| 189 | static int ssh_session2(void); |
| 190 | static void load_public_identity_files(void); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 191 | static void control_client(const char *path); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 192 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 193 | /* |
| 194 | * Main program for the ssh client. |
| 195 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 196 | int |
| 197 | main(int ac, char **av) |
| 198 | { |
Ben Lindstrom | 2415757 | 2002-06-12 16:09:39 +0000 | [diff] [blame] | 199 | int i, opt, exit_status; |
Damien Miller | 9836cf8 | 2003-12-17 16:30:06 +1100 | [diff] [blame] | 200 | char *p, *cp, *line, buf[256]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 201 | struct stat st; |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 202 | struct passwd *pw; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 203 | int dummy; |
Ben Lindstrom | 5ccf63a | 2001-09-24 20:00:10 +0000 | [diff] [blame] | 204 | extern int optind, optreset; |
Damien Miller | 4f8e669 | 2001-07-14 13:22:53 +1000 | [diff] [blame] | 205 | extern char *optarg; |
Damien Miller | 9651fe6 | 2005-06-26 08:55:25 +1000 | [diff] [blame] | 206 | struct servent *sp; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 207 | Forward fwd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 208 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 209 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 210 | sanitise_stdfd(); |
| 211 | |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 212 | __progname = ssh_get_progname(av[0]); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 213 | init_rng(); |
| 214 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 215 | /* |
| 216 | * Save the original real uid. It will be needed later (uid-swapping |
| 217 | * may clobber the real uid). |
| 218 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 219 | original_real_uid = getuid(); |
| 220 | original_effective_uid = geteuid(); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 221 | |
Damien Miller | 50b9a60 | 2002-09-04 16:50:06 +1000 | [diff] [blame] | 222 | /* |
| 223 | * Use uid-swapping to give up root privileges for the duration of |
| 224 | * option processing. We will re-instantiate the rights when we are |
| 225 | * ready to create the privileged port, and will permanently drop |
| 226 | * them when the port has been created (actually, when the connection |
| 227 | * has been made, as we may need to create the port several times). |
| 228 | */ |
| 229 | PRIV_END; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 230 | |
Damien Miller | 05dd795 | 2000-10-01 00:42:48 +1100 | [diff] [blame] | 231 | #ifdef HAVE_SETRLIMIT |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 232 | /* If we are installed setuid root be careful to not drop core. */ |
| 233 | if (original_real_uid != original_effective_uid) { |
| 234 | struct rlimit rlim; |
| 235 | rlim.rlim_cur = rlim.rlim_max = 0; |
| 236 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) |
| 237 | fatal("setrlimit failed: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 238 | } |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 239 | #endif |
Ben Lindstrom | 3fcf1a2 | 2001-04-08 18:26:59 +0000 | [diff] [blame] | 240 | /* Get user data. */ |
| 241 | pw = getpwuid(original_real_uid); |
| 242 | if (!pw) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 243 | logit("You don't exist, go away!"); |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 244 | exit(255); |
Ben Lindstrom | 3fcf1a2 | 2001-04-08 18:26:59 +0000 | [diff] [blame] | 245 | } |
| 246 | /* Take a copy of the returned structure. */ |
| 247 | pw = pwcopy(pw); |
| 248 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 249 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 250 | * Set our umask to something reasonable, as some files are created |
| 251 | * with the default umask. This will make them world-readable but |
| 252 | * writable only by the owner, which is ok for all files for which we |
| 253 | * don't set the modes explicitly. |
| 254 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 255 | umask(022); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 256 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 257 | /* Initialize option structure to indicate that no values have been set. */ |
| 258 | initialize_options(&options); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 259 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 260 | /* Parse command-line arguments. */ |
| 261 | host = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 262 | |
Damien Miller | 2ecb6bd | 2006-03-15 12:03:53 +1100 | [diff] [blame] | 263 | again: |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 264 | while ((opt = getopt(ac, av, |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 265 | "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNO:PR:S:TVw:XY")) != -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 266 | switch (opt) { |
Ben Lindstrom | 1e7d306 | 2001-02-09 02:36:43 +0000 | [diff] [blame] | 267 | case '1': |
| 268 | options.protocol = SSH_PROTO_1; |
| 269 | break; |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 270 | case '2': |
| 271 | options.protocol = SSH_PROTO_2; |
| 272 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 273 | case '4': |
Darren Tucker | 0a4f04b | 2003-07-03 20:37:47 +1000 | [diff] [blame] | 274 | options.address_family = AF_INET; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 275 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 276 | case '6': |
Darren Tucker | 0a4f04b | 2003-07-03 20:37:47 +1000 | [diff] [blame] | 277 | options.address_family = AF_INET6; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 278 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 279 | case 'n': |
| 280 | stdin_null_flag = 1; |
| 281 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 282 | case 'f': |
| 283 | fork_after_authentication_flag = 1; |
| 284 | stdin_null_flag = 1; |
| 285 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 286 | case 'x': |
| 287 | options.forward_x11 = 0; |
| 288 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 289 | case 'X': |
| 290 | options.forward_x11 = 1; |
| 291 | break; |
Darren Tucker | 0a118da | 2003-10-15 15:54:32 +1000 | [diff] [blame] | 292 | case 'Y': |
| 293 | options.forward_x11 = 1; |
| 294 | options.forward_x11_trusted = 1; |
| 295 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 296 | case 'g': |
| 297 | options.gateway_ports = 1; |
| 298 | break; |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 299 | case 'O': |
| 300 | if (strcmp(optarg, "check") == 0) |
| 301 | mux_command = SSHMUX_COMMAND_ALIVE_CHECK; |
| 302 | else if (strcmp(optarg, "exit") == 0) |
| 303 | mux_command = SSHMUX_COMMAND_TERMINATE; |
| 304 | else |
| 305 | fatal("Invalid multiplex command."); |
| 306 | break; |
Damien Miller | 147bba3 | 2002-09-04 16:46:06 +1000 | [diff] [blame] | 307 | case 'P': /* deprecated */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 308 | options.use_privileged_port = 0; |
| 309 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 310 | case 'a': |
| 311 | options.forward_agent = 0; |
| 312 | break; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 313 | case 'A': |
| 314 | options.forward_agent = 1; |
| 315 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 316 | case 'k': |
Damien Miller | e0113cc | 2003-11-24 13:10:09 +1100 | [diff] [blame] | 317 | options.gss_deleg_creds = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 318 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 319 | case 'i': |
| 320 | if (stat(optarg, &st) < 0) { |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 321 | fprintf(stderr, "Warning: Identity file %s " |
Damien Miller | 3eb48b6 | 2005-03-01 21:15:46 +1100 | [diff] [blame] | 322 | "not accessible: %s.\n", optarg, |
| 323 | strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 324 | break; |
| 325 | } |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 326 | if (options.num_identity_files >= |
| 327 | SSH_MAX_IDENTITY_FILES) |
| 328 | fatal("Too many identity files specified " |
| 329 | "(max %d)", SSH_MAX_IDENTITY_FILES); |
| 330 | options.identity_files[options.num_identity_files++] = |
| 331 | xstrdup(optarg); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 332 | break; |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 333 | case 'I': |
| 334 | #ifdef SMARTCARD |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 335 | options.smartcard_device = xstrdup(optarg); |
Ben Lindstrom | bcc1808 | 2001-08-06 21:59:25 +0000 | [diff] [blame] | 336 | #else |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 337 | fprintf(stderr, "no support for smartcards.\n"); |
Ben Lindstrom | bcc1808 | 2001-08-06 21:59:25 +0000 | [diff] [blame] | 338 | #endif |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 339 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 340 | case 't': |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 341 | if (tty_flag) |
| 342 | force_tty_flag = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 343 | tty_flag = 1; |
| 344 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 345 | case 'v': |
Darren Tucker | e98dfa3 | 2003-07-19 19:54:31 +1000 | [diff] [blame] | 346 | if (debug_flag == 0) { |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 347 | debug_flag = 1; |
| 348 | options.log_level = SYSLOG_LEVEL_DEBUG1; |
Darren Tucker | e98dfa3 | 2003-07-19 19:54:31 +1000 | [diff] [blame] | 349 | } else { |
| 350 | if (options.log_level < SYSLOG_LEVEL_DEBUG3) |
| 351 | options.log_level++; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 352 | break; |
Darren Tucker | e98dfa3 | 2003-07-19 19:54:31 +1000 | [diff] [blame] | 353 | } |
Darren Tucker | e9bf984 | 2004-11-05 20:05:32 +1100 | [diff] [blame] | 354 | /* FALLTHROUGH */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 355 | case 'V': |
Damien Miller | 0c889cd | 2004-03-22 09:36:00 +1100 | [diff] [blame] | 356 | fprintf(stderr, "%s, %s\n", |
Damien Miller | 2aa6d3c | 2004-09-12 16:53:04 +1000 | [diff] [blame] | 357 | SSH_RELEASE, SSLeay_version(SSLEAY_VERSION)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 358 | if (opt == 'V') |
| 359 | exit(0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 360 | break; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 361 | case 'w': |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 362 | if (options.tun_open == -1) |
| 363 | options.tun_open = SSH_TUNMODE_DEFAULT; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 364 | options.tun_local = a2tun(optarg, &options.tun_remote); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 365 | if (options.tun_local == SSH_TUNID_ERR) { |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 366 | fprintf(stderr, "Bad tun device '%s'\n", optarg); |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 367 | exit(255); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 368 | } |
| 369 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 370 | case 'q': |
| 371 | options.log_level = SYSLOG_LEVEL_QUIET; |
| 372 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 373 | case 'e': |
| 374 | if (optarg[0] == '^' && optarg[2] == 0 && |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 375 | (u_char) optarg[1] >= 64 && |
| 376 | (u_char) optarg[1] < 128) |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 377 | options.escape_char = (u_char) optarg[1] & 31; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 378 | else if (strlen(optarg) == 1) |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 379 | options.escape_char = (u_char) optarg[0]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 380 | else if (strcmp(optarg, "none") == 0) |
Ben Lindstrom | 2b1f71b | 2001-06-05 20:32:21 +0000 | [diff] [blame] | 381 | options.escape_char = SSH_ESCAPECHAR_NONE; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 382 | else { |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 383 | fprintf(stderr, "Bad escape character '%s'.\n", |
| 384 | optarg); |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 385 | exit(255); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 386 | } |
| 387 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 388 | case 'c': |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 389 | if (ciphers_valid(optarg)) { |
| 390 | /* SSH2 only */ |
| 391 | options.ciphers = xstrdup(optarg); |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 392 | options.cipher = SSH_CIPHER_INVALID; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 393 | } else { |
| 394 | /* SSH1 only */ |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 395 | options.cipher = cipher_number(optarg); |
| 396 | if (options.cipher == -1) { |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 397 | fprintf(stderr, |
| 398 | "Unknown cipher type '%s'\n", |
| 399 | optarg); |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 400 | exit(255); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 401 | } |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 402 | if (options.cipher == SSH_CIPHER_3DES) |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 403 | options.ciphers = "3des-cbc"; |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 404 | else if (options.cipher == SSH_CIPHER_BLOWFISH) |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 405 | options.ciphers = "blowfish-cbc"; |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 406 | else |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 407 | options.ciphers = (char *)-1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 408 | } |
| 409 | break; |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 410 | case 'm': |
| 411 | if (mac_valid(optarg)) |
| 412 | options.macs = xstrdup(optarg); |
| 413 | else { |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 414 | fprintf(stderr, "Unknown mac type '%s'\n", |
| 415 | optarg); |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 416 | exit(255); |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 417 | } |
| 418 | break; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 419 | case 'M': |
Damien Miller | d14b1e7 | 2005-06-16 13:19:41 +1000 | [diff] [blame] | 420 | if (options.control_master == SSHCTL_MASTER_YES) |
| 421 | options.control_master = SSHCTL_MASTER_ASK; |
| 422 | else |
| 423 | options.control_master = SSHCTL_MASTER_YES; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 424 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 425 | case 'p': |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 426 | options.port = a2port(optarg); |
| 427 | if (options.port == 0) { |
Ben Lindstrom | 146edb9 | 2001-04-11 23:06:28 +0000 | [diff] [blame] | 428 | fprintf(stderr, "Bad port '%s'\n", optarg); |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 429 | exit(255); |
Ben Lindstrom | 146edb9 | 2001-04-11 23:06:28 +0000 | [diff] [blame] | 430 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 431 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 432 | case 'l': |
| 433 | options.user = optarg; |
| 434 | break; |
Ben Lindstrom | 1a17471 | 2001-09-12 17:56:15 +0000 | [diff] [blame] | 435 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 436 | case 'L': |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 437 | if (parse_forward(&fwd, optarg)) |
| 438 | add_local_forward(&options, &fwd); |
| 439 | else { |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 440 | fprintf(stderr, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 441 | "Bad local forwarding specification '%s'\n", |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 442 | optarg); |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 443 | exit(255); |
Ben Lindstrom | 1a17471 | 2001-09-12 17:56:15 +0000 | [diff] [blame] | 444 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 445 | break; |
| 446 | |
| 447 | case 'R': |
| 448 | if (parse_forward(&fwd, optarg)) { |
| 449 | add_remote_forward(&options, &fwd); |
| 450 | } else { |
| 451 | fprintf(stderr, |
| 452 | "Bad remote forwarding specification " |
| 453 | "'%s'\n", optarg); |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 454 | exit(255); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 455 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 456 | break; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 457 | |
| 458 | case 'D': |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 459 | cp = p = xstrdup(optarg); |
| 460 | memset(&fwd, '\0', sizeof(fwd)); |
| 461 | fwd.connect_host = "socks"; |
| 462 | if ((fwd.listen_host = hpdelim(&cp)) == NULL) { |
| 463 | fprintf(stderr, "Bad dynamic forwarding " |
| 464 | "specification '%.100s'\n", optarg); |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 465 | exit(255); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 466 | } |
| 467 | if (cp != NULL) { |
| 468 | fwd.listen_port = a2port(cp); |
| 469 | fwd.listen_host = cleanhostname(fwd.listen_host); |
| 470 | } else { |
| 471 | fwd.listen_port = a2port(fwd.listen_host); |
Damien Miller | be1045d | 2005-08-12 22:10:56 +1000 | [diff] [blame] | 472 | fwd.listen_host = NULL; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | if (fwd.listen_port == 0) { |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 476 | fprintf(stderr, "Bad dynamic port '%s'\n", |
| 477 | optarg); |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 478 | exit(255); |
Ben Lindstrom | 146edb9 | 2001-04-11 23:06:28 +0000 | [diff] [blame] | 479 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 480 | add_local_forward(&options, &fwd); |
| 481 | xfree(p); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 482 | break; |
| 483 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 484 | case 'C': |
| 485 | options.compression = 1; |
| 486 | break; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 487 | case 'N': |
| 488 | no_shell_flag = 1; |
| 489 | no_tty_flag = 1; |
| 490 | break; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 491 | case 'T': |
| 492 | no_tty_flag = 1; |
| 493 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 494 | case 'o': |
| 495 | dummy = 1; |
Damien Miller | 9836cf8 | 2003-12-17 16:30:06 +1100 | [diff] [blame] | 496 | line = xstrdup(optarg); |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 497 | if (process_config_line(&options, host ? host : "", |
Damien Miller | 9836cf8 | 2003-12-17 16:30:06 +1100 | [diff] [blame] | 498 | line, "command-line", 0, &dummy) != 0) |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 499 | exit(255); |
Damien Miller | 9836cf8 | 2003-12-17 16:30:06 +1100 | [diff] [blame] | 500 | xfree(line); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 501 | break; |
Damien Miller | 832562e | 2001-01-30 09:30:01 +1100 | [diff] [blame] | 502 | case 's': |
| 503 | subsystem_flag = 1; |
| 504 | break; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 505 | case 'S': |
| 506 | if (options.control_path != NULL) |
| 507 | free(options.control_path); |
| 508 | options.control_path = xstrdup(optarg); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 509 | break; |
Ben Lindstrom | e0f8804 | 2001-04-30 13:06:24 +0000 | [diff] [blame] | 510 | case 'b': |
| 511 | options.bind_address = optarg; |
| 512 | break; |
Ben Lindstrom | 14f31ab | 2001-09-12 17:48:04 +0000 | [diff] [blame] | 513 | case 'F': |
| 514 | config = optarg; |
| 515 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 516 | default: |
| 517 | usage(); |
| 518 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 519 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 520 | |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 521 | ac -= optind; |
| 522 | av += optind; |
| 523 | |
| 524 | if (ac > 0 && !host && **av != '-') { |
Ben Lindstrom | c276c12 | 2002-12-23 02:14:51 +0000 | [diff] [blame] | 525 | if (strrchr(*av, '@')) { |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 526 | p = xstrdup(*av); |
Ben Lindstrom | c276c12 | 2002-12-23 02:14:51 +0000 | [diff] [blame] | 527 | cp = strrchr(p, '@'); |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 528 | if (cp == NULL || cp == p) |
| 529 | usage(); |
| 530 | options.user = p; |
| 531 | *cp = '\0'; |
| 532 | host = ++cp; |
| 533 | } else |
| 534 | host = *av; |
Ben Lindstrom | b9fa691 | 2002-12-23 02:24:54 +0000 | [diff] [blame] | 535 | if (ac > 1) { |
| 536 | optind = optreset = 1; |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 537 | goto again; |
| 538 | } |
Ben Lindstrom | b9fa691 | 2002-12-23 02:24:54 +0000 | [diff] [blame] | 539 | ac--, av++; |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 540 | } |
| 541 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 542 | /* Check that we got a host name. */ |
| 543 | if (!host) |
| 544 | usage(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 545 | |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 546 | SSLeay_add_all_algorithms(); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 547 | ERR_load_crypto_strings(); |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 548 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 549 | /* Initialize the command to execute on remote host. */ |
| 550 | buffer_init(&command); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 551 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 552 | /* |
| 553 | * Save the command to execute on the remote host in a buffer. There |
| 554 | * is no limit on the length of the command, except by the maximum |
| 555 | * packet size. Also sets the tty flag if there is no command. |
| 556 | */ |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 557 | if (!ac) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 558 | /* No command specified - execute shell on a tty. */ |
| 559 | tty_flag = 1; |
Damien Miller | 832562e | 2001-01-30 09:30:01 +1100 | [diff] [blame] | 560 | if (subsystem_flag) { |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 561 | fprintf(stderr, |
| 562 | "You must specify a subsystem to invoke.\n"); |
Damien Miller | 832562e | 2001-01-30 09:30:01 +1100 | [diff] [blame] | 563 | usage(); |
| 564 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 565 | } else { |
Damien Miller | f461445 | 2001-07-14 12:18:10 +1000 | [diff] [blame] | 566 | /* A command has been specified. Store it into the buffer. */ |
| 567 | for (i = 0; i < ac; i++) { |
| 568 | if (i) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 569 | buffer_append(&command, " ", 1); |
| 570 | buffer_append(&command, av[i], strlen(av[i])); |
| 571 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 572 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 573 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 574 | /* Cannot fork to background if no command. */ |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 575 | if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 576 | fatal("Cannot fork into background without a command to execute."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 577 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 578 | /* Allocate a tty by default if no command specified. */ |
| 579 | if (buffer_len(&command) == 0) |
| 580 | tty_flag = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 581 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 582 | /* Force no tty */ |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 583 | if (no_tty_flag) |
| 584 | tty_flag = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 585 | /* Do not allocate a tty if stdin is not a tty. */ |
Damien Miller | ddee575 | 2005-05-26 12:05:05 +1000 | [diff] [blame] | 586 | if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 587 | if (tty_flag) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 588 | logit("Pseudo-terminal will not be allocated because stdin is not a terminal."); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 589 | tty_flag = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 590 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 591 | |
Ben Lindstrom | 8a432f5 | 2001-03-05 07:24:46 +0000 | [diff] [blame] | 592 | /* |
| 593 | * Initialize "log" output. Since we are the client all output |
| 594 | * actually goes to stderr. |
| 595 | */ |
Ben Lindstrom | 2b64652 | 2001-04-12 16:16:57 +0000 | [diff] [blame] | 596 | log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level, |
| 597 | SYSLOG_FACILITY_USER, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 598 | |
Ben Lindstrom | 14f31ab | 2001-09-12 17:48:04 +0000 | [diff] [blame] | 599 | /* |
| 600 | * Read per-user configuration file. Ignore the system wide config |
| 601 | * file if the user specifies a config file on the command line. |
| 602 | */ |
| 603 | if (config != NULL) { |
Damien Miller | 914420f | 2004-04-20 20:14:07 +1000 | [diff] [blame] | 604 | if (!read_config_file(config, host, &options, 0)) |
Ben Lindstrom | edc0cf2 | 2001-09-12 18:32:20 +0000 | [diff] [blame] | 605 | fatal("Can't open user config file %.100s: " |
| 606 | "%.100s", config, strerror(errno)); |
Ben Lindstrom | 14f31ab | 2001-09-12 17:48:04 +0000 | [diff] [blame] | 607 | } else { |
| 608 | snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, |
| 609 | _PATH_SSH_USER_CONFFILE); |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 610 | (void)read_config_file(buf, host, &options, 1); |
Ben Lindstrom | 83f07d1 | 2001-10-03 17:22:29 +0000 | [diff] [blame] | 611 | |
| 612 | /* Read systemwide configuration file after use config. */ |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 613 | (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, |
Damien Miller | 57a4476 | 2004-04-20 20:11:57 +1000 | [diff] [blame] | 614 | &options, 0); |
Ben Lindstrom | 14f31ab | 2001-09-12 17:48:04 +0000 | [diff] [blame] | 615 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 616 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 617 | /* Fill configuration defaults. */ |
| 618 | fill_default_options(&options); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 619 | |
Darren Tucker | 0a4f04b | 2003-07-03 20:37:47 +1000 | [diff] [blame] | 620 | channel_set_af(options.address_family); |
| 621 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 622 | /* reinit */ |
Ben Lindstrom | 8a432f5 | 2001-03-05 07:24:46 +0000 | [diff] [blame] | 623 | log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 624 | |
Damien Miller | 60bc517 | 2001-03-19 09:38:15 +1100 | [diff] [blame] | 625 | seed_rng(); |
| 626 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 627 | if (options.user == NULL) |
| 628 | options.user = xstrdup(pw->pw_name); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 629 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 630 | if (options.hostname != NULL) |
| 631 | host = options.hostname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 632 | |
Darren Tucker | 3f521e2 | 2003-07-03 16:20:42 +1000 | [diff] [blame] | 633 | /* force lowercase for hostkey matching */ |
| 634 | if (options.host_key_alias != NULL) { |
| 635 | for (p = options.host_key_alias; *p; p++) |
| 636 | if (isupper(*p)) |
Damien Miller | 1d2b670 | 2006-03-26 14:09:54 +1100 | [diff] [blame] | 637 | *p = (char)tolower(*p); |
Darren Tucker | 3f521e2 | 2003-07-03 16:20:42 +1000 | [diff] [blame] | 638 | } |
| 639 | |
Damien Miller | 7c71cc7 | 2005-06-26 08:56:31 +1000 | [diff] [blame] | 640 | /* Get default port if port has not been set. */ |
| 641 | if (options.port == 0) { |
| 642 | sp = getservbyname(SSH_SERVICE_NAME, "tcp"); |
| 643 | options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT; |
| 644 | } |
| 645 | |
Damien Miller | 9f1e33a | 2003-02-24 11:57:32 +1100 | [diff] [blame] | 646 | if (options.proxy_command != NULL && |
| 647 | strcmp(options.proxy_command, "none") == 0) |
| 648 | options.proxy_command = NULL; |
Damien Miller | 8f74c8f | 2005-06-26 08:56:03 +1000 | [diff] [blame] | 649 | if (options.control_path != NULL && |
| 650 | strcmp(options.control_path, "none") == 0) |
| 651 | options.control_path = NULL; |
Damien Miller | 9f1e33a | 2003-02-24 11:57:32 +1100 | [diff] [blame] | 652 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 653 | if (options.control_path != NULL) { |
Damien Miller | 6b1d53c | 2006-03-31 23:13:21 +1100 | [diff] [blame] | 654 | char thishost[NI_MAXHOST]; |
Damien Miller | 3ec54c7 | 2006-03-15 11:30:13 +1100 | [diff] [blame] | 655 | |
Damien Miller | 6b1d53c | 2006-03-31 23:13:21 +1100 | [diff] [blame] | 656 | if (gethostname(thishost, sizeof(thishost)) == -1) |
Damien Miller | 3ec54c7 | 2006-03-15 11:30:13 +1100 | [diff] [blame] | 657 | fatal("gethostname: %s", strerror(errno)); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 658 | snprintf(buf, sizeof(buf), "%d", options.port); |
| 659 | cp = tilde_expand_filename(options.control_path, |
| 660 | original_real_uid); |
| 661 | options.control_path = percent_expand(cp, "p", buf, "h", host, |
Damien Miller | 6b1d53c | 2006-03-31 23:13:21 +1100 | [diff] [blame] | 662 | "r", options.user, "l", thishost, (char *)NULL); |
Damien Miller | 6476cad | 2005-06-16 13:18:34 +1000 | [diff] [blame] | 663 | xfree(cp); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 664 | } |
Darren Tucker | 0814d31 | 2005-06-01 23:08:51 +1000 | [diff] [blame] | 665 | if (mux_command != 0 && options.control_path == NULL) |
| 666 | fatal("No ControlPath specified for \"-O\" command"); |
Damien Miller | d14b1e7 | 2005-06-16 13:19:41 +1000 | [diff] [blame] | 667 | if (options.control_path != NULL) |
Damien Miller | dadfd4d | 2005-05-26 12:07:13 +1000 | [diff] [blame] | 668 | control_client(options.control_path); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 669 | |
Kevin Steves | fcec7f8 | 2000-12-15 19:55:48 +0000 | [diff] [blame] | 670 | /* Open a connection to the remote host. */ |
Darren Tucker | 0a4f04b | 2003-07-03 20:37:47 +1000 | [diff] [blame] | 671 | if (ssh_connect(host, &hostaddr, options.port, |
| 672 | options.address_family, options.connection_attempts, |
Ben Lindstrom | 1aa6427 | 2002-06-11 20:28:05 +0000 | [diff] [blame] | 673 | #ifdef HAVE_CYGWIN |
| 674 | options.use_privileged_port, |
| 675 | #else |
Ben Lindstrom | f9c4884 | 2002-06-11 16:37:51 +0000 | [diff] [blame] | 676 | original_effective_uid == 0 && options.use_privileged_port, |
Ben Lindstrom | 1aa6427 | 2002-06-11 20:28:05 +0000 | [diff] [blame] | 677 | #endif |
Ben Lindstrom | da394ca | 2002-06-12 16:11:12 +0000 | [diff] [blame] | 678 | options.proxy_command) != 0) |
Darren Tucker | e9a9b71 | 2005-12-20 16:15:51 +1100 | [diff] [blame] | 679 | exit(255); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 680 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 681 | /* |
| 682 | * If we successfully made the connection, load the host private key |
| 683 | * in case we will need it later for combined rsa-rhosts |
| 684 | * authentication. This must be done before releasing extra |
| 685 | * privileges, because the file is only readable by root. |
Ben Lindstrom | 9e5bb57 | 2002-06-06 19:58:27 +0000 | [diff] [blame] | 686 | * If we cannot access the private keys, load the public keys |
| 687 | * instead and try to execute the ssh-keysign helper instead. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 688 | */ |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 689 | sensitive_data.nkeys = 0; |
| 690 | sensitive_data.keys = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 691 | sensitive_data.external_keysign = 0; |
Ben Lindstrom | 2415757 | 2002-06-12 16:09:39 +0000 | [diff] [blame] | 692 | if (options.rhosts_rsa_authentication || |
| 693 | options.hostbased_authentication) { |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 694 | sensitive_data.nkeys = 3; |
Damien Miller | ddd63ab | 2006-03-31 23:10:51 +1100 | [diff] [blame] | 695 | sensitive_data.keys = xcalloc(sensitive_data.nkeys, |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 696 | sizeof(Key)); |
Ben Lindstrom | f9c4884 | 2002-06-11 16:37:51 +0000 | [diff] [blame] | 697 | |
| 698 | PRIV_START; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 699 | sensitive_data.keys[0] = key_load_private_type(KEY_RSA1, |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 700 | _PATH_HOST_KEY_FILE, "", NULL, NULL); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 701 | sensitive_data.keys[1] = key_load_private_type(KEY_DSA, |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 702 | _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 703 | sensitive_data.keys[2] = key_load_private_type(KEY_RSA, |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 704 | _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL); |
Ben Lindstrom | f9c4884 | 2002-06-11 16:37:51 +0000 | [diff] [blame] | 705 | PRIV_END; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 706 | |
Ben Lindstrom | 5d35a2f | 2002-07-04 00:19:40 +0000 | [diff] [blame] | 707 | if (options.hostbased_authentication == 1 && |
| 708 | sensitive_data.keys[0] == NULL && |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 709 | sensitive_data.keys[1] == NULL && |
| 710 | sensitive_data.keys[2] == NULL) { |
| 711 | sensitive_data.keys[1] = key_load_public( |
| 712 | _PATH_HOST_DSA_KEY_FILE, NULL); |
| 713 | sensitive_data.keys[2] = key_load_public( |
| 714 | _PATH_HOST_RSA_KEY_FILE, NULL); |
| 715 | sensitive_data.external_keysign = 1; |
| 716 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 717 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 718 | /* |
| 719 | * Get rid of any extra privileges that we may have. We will no |
| 720 | * longer need them. Also, extra privileges could make it very hard |
| 721 | * to read identity files and other non-world-readable files from the |
| 722 | * user's home directory if it happens to be on a NFS volume where |
| 723 | * root is mapped to nobody. |
| 724 | */ |
Darren Tucker | 25f60a7 | 2004-08-15 17:23:34 +1000 | [diff] [blame] | 725 | if (original_effective_uid == 0) { |
| 726 | PRIV_START; |
| 727 | permanently_set_uid(pw); |
| 728 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 729 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 730 | /* |
| 731 | * Now that we are back to our own permissions, create ~/.ssh |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 732 | * directory if it doesn't already exist. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 733 | */ |
Ben Lindstrom | 930b14a | 2001-08-15 23:19:21 +0000 | [diff] [blame] | 734 | snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 735 | if (stat(buf, &st) < 0) |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 736 | if (mkdir(buf, 0700) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 737 | error("Could not create directory '%.200s'.", buf); |
| 738 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 739 | /* load options.identity_files */ |
| 740 | load_public_identity_files(); |
| 741 | |
| 742 | /* Expand ~ in known host file names. */ |
| 743 | /* XXX mem-leaks: */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 744 | options.system_hostfile = |
| 745 | tilde_expand_filename(options.system_hostfile, original_real_uid); |
| 746 | options.user_hostfile = |
| 747 | tilde_expand_filename(options.user_hostfile, original_real_uid); |
| 748 | options.system_hostfile2 = |
| 749 | tilde_expand_filename(options.system_hostfile2, original_real_uid); |
| 750 | options.user_hostfile2 = |
| 751 | tilde_expand_filename(options.user_hostfile2, original_real_uid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 752 | |
Damien Miller | 07cd589 | 2001-11-12 10:52:03 +1100 | [diff] [blame] | 753 | signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */ |
| 754 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 755 | /* Log into the remote system. This never returns if the login fails. */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 756 | ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 757 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 758 | /* We no longer need the private host keys. Clear them now. */ |
| 759 | if (sensitive_data.nkeys != 0) { |
| 760 | for (i = 0; i < sensitive_data.nkeys; i++) { |
| 761 | if (sensitive_data.keys[i] != NULL) { |
| 762 | /* Destroys contents safely */ |
| 763 | debug3("clear hostkey %d", i); |
| 764 | key_free(sensitive_data.keys[i]); |
| 765 | sensitive_data.keys[i] = NULL; |
| 766 | } |
| 767 | } |
| 768 | xfree(sensitive_data.keys); |
| 769 | } |
Ben Lindstrom | a6c8a8d | 2001-08-06 21:42:00 +0000 | [diff] [blame] | 770 | for (i = 0; i < options.num_identity_files; i++) { |
| 771 | if (options.identity_files[i]) { |
| 772 | xfree(options.identity_files[i]); |
| 773 | options.identity_files[i] = NULL; |
| 774 | } |
| 775 | if (options.identity_keys[i]) { |
| 776 | key_free(options.identity_keys[i]); |
| 777 | options.identity_keys[i] = NULL; |
| 778 | } |
| 779 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 780 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 781 | exit_status = compat20 ? ssh_session2() : ssh_session(); |
| 782 | packet_close(); |
Damien Miller | 8c4e18a | 2002-09-19 12:05:02 +1000 | [diff] [blame] | 783 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 784 | if (options.control_path != NULL && control_fd != -1) |
| 785 | unlink(options.control_path); |
| 786 | |
Damien Miller | 8c4e18a | 2002-09-19 12:05:02 +1000 | [diff] [blame] | 787 | /* |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 788 | * Send SIGHUP to proxy command if used. We don't wait() in |
Damien Miller | 8c4e18a | 2002-09-19 12:05:02 +1000 | [diff] [blame] | 789 | * case it hangs and instead rely on init to reap the child |
| 790 | */ |
| 791 | if (proxy_command_pid > 1) |
| 792 | kill(proxy_command_pid, SIGHUP); |
| 793 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 794 | return exit_status; |
| 795 | } |
| 796 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 797 | static void |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 798 | ssh_init_forwarding(void) |
| 799 | { |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 800 | int success = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 801 | int i; |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 802 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 803 | /* Initiate local TCP/IP port forwardings. */ |
| 804 | for (i = 0; i < options.num_local_forwards; i++) { |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 805 | debug("Local connections to %.200s:%d forwarded to remote " |
| 806 | "address %.200s:%d", |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 807 | (options.local_forwards[i].listen_host == NULL) ? |
| 808 | (options.gateway_ports ? "*" : "LOCALHOST") : |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 809 | options.local_forwards[i].listen_host, |
| 810 | options.local_forwards[i].listen_port, |
| 811 | options.local_forwards[i].connect_host, |
| 812 | options.local_forwards[i].connect_port); |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 813 | success += channel_setup_local_fwd_listener( |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 814 | options.local_forwards[i].listen_host, |
| 815 | options.local_forwards[i].listen_port, |
| 816 | options.local_forwards[i].connect_host, |
| 817 | options.local_forwards[i].connect_port, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 818 | options.gateway_ports); |
| 819 | } |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 820 | if (i > 0 && success == 0) |
| 821 | error("Could not request local forwarding."); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 822 | |
| 823 | /* Initiate remote TCP/IP port forwardings. */ |
| 824 | for (i = 0; i < options.num_remote_forwards; i++) { |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 825 | debug("Remote connections from %.200s:%d forwarded to " |
| 826 | "local address %.200s:%d", |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 827 | (options.remote_forwards[i].listen_host == NULL) ? |
Damien Miller | aa3bb10 | 2005-11-05 15:12:59 +1100 | [diff] [blame] | 828 | "LOCALHOST" : options.remote_forwards[i].listen_host, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 829 | options.remote_forwards[i].listen_port, |
| 830 | options.remote_forwards[i].connect_host, |
| 831 | options.remote_forwards[i].connect_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 832 | channel_request_remote_forwarding( |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 833 | options.remote_forwards[i].listen_host, |
| 834 | options.remote_forwards[i].listen_port, |
| 835 | options.remote_forwards[i].connect_host, |
| 836 | options.remote_forwards[i].connect_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 840 | static void |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 841 | check_agent_present(void) |
| 842 | { |
| 843 | if (options.forward_agent) { |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 844 | /* Clear agent forwarding if we don't have an agent. */ |
Damien Miller | 789e95d | 2002-09-12 09:52:46 +1000 | [diff] [blame] | 845 | if (!ssh_agent_present()) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 846 | options.forward_agent = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 850 | static int |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 851 | ssh_session(void) |
| 852 | { |
| 853 | int type; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 854 | int interactive = 0; |
| 855 | int have_tty = 0; |
| 856 | struct winsize ws; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 857 | char *cp; |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 858 | const char *display; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 859 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 860 | /* Enable compression if requested. */ |
| 861 | if (options.compression) { |
| 862 | debug("Requesting compression at level %d.", options.compression_level); |
| 863 | |
| 864 | if (options.compression_level < 1 || options.compression_level > 9) |
| 865 | fatal("Compression level must be from 1 (fast) to 9 (slow, best)."); |
| 866 | |
| 867 | /* Send the request. */ |
| 868 | packet_start(SSH_CMSG_REQUEST_COMPRESSION); |
| 869 | packet_put_int(options.compression_level); |
| 870 | packet_send(); |
| 871 | packet_write_wait(); |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 872 | type = packet_read(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 873 | if (type == SSH_SMSG_SUCCESS) |
| 874 | packet_start_compression(options.compression_level); |
| 875 | else if (type == SSH_SMSG_FAILURE) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 876 | logit("Warning: Remote host refused compression."); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 877 | else |
| 878 | packet_disconnect("Protocol error waiting for compression response."); |
| 879 | } |
| 880 | /* Allocate a pseudo tty if appropriate. */ |
| 881 | if (tty_flag) { |
| 882 | debug("Requesting pty."); |
| 883 | |
| 884 | /* Start the packet. */ |
| 885 | packet_start(SSH_CMSG_REQUEST_PTY); |
| 886 | |
| 887 | /* Store TERM in the packet. There is no limit on the |
| 888 | length of the string. */ |
| 889 | cp = getenv("TERM"); |
| 890 | if (!cp) |
| 891 | cp = ""; |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 892 | packet_put_cstring(cp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 893 | |
| 894 | /* Store window size in the packet. */ |
| 895 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) |
| 896 | memset(&ws, 0, sizeof(ws)); |
Damien Miller | 71a7367 | 2006-03-26 14:04:36 +1100 | [diff] [blame] | 897 | packet_put_int((u_int)ws.ws_row); |
| 898 | packet_put_int((u_int)ws.ws_col); |
| 899 | packet_put_int((u_int)ws.ws_xpixel); |
| 900 | packet_put_int((u_int)ws.ws_ypixel); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 901 | |
| 902 | /* Store tty modes in the packet. */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 903 | tty_make_modes(fileno(stdin), NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 904 | |
| 905 | /* Send the packet, and wait for it to leave. */ |
| 906 | packet_send(); |
| 907 | packet_write_wait(); |
| 908 | |
| 909 | /* Read response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 910 | type = packet_read(); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 911 | if (type == SSH_SMSG_SUCCESS) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 912 | interactive = 1; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 913 | have_tty = 1; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 914 | } else if (type == SSH_SMSG_FAILURE) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 915 | logit("Warning: Remote host failed or refused to allocate a pseudo tty."); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 916 | else |
| 917 | packet_disconnect("Protocol error waiting for pty request response."); |
| 918 | } |
| 919 | /* Request X11 forwarding if enabled and DISPLAY is set. */ |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 920 | display = getenv("DISPLAY"); |
| 921 | if (options.forward_x11 && display != NULL) { |
Ben Lindstrom | 4a4bd71 | 2001-12-06 17:45:19 +0000 | [diff] [blame] | 922 | char *proto, *data; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 923 | /* Get reasonable local authentication information. */ |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 924 | client_x11_get_proto(display, options.xauth_location, |
| 925 | options.forward_x11_trusted, &proto, &data); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 926 | /* Request forwarding with authentication spoofing. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 927 | debug("Requesting X11 forwarding with authentication spoofing."); |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 928 | x11_request_forwarding_with_spoofing(0, display, proto, data); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 929 | |
| 930 | /* Read response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 931 | type = packet_read(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 932 | if (type == SSH_SMSG_SUCCESS) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 933 | interactive = 1; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 934 | } else if (type == SSH_SMSG_FAILURE) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 935 | logit("Warning: Remote host denied X11 forwarding."); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 936 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 937 | packet_disconnect("Protocol error waiting for X11 forwarding"); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 938 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 939 | } |
| 940 | /* Tell the packet module whether this is an interactive session. */ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 941 | packet_set_interactive(interactive); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 942 | |
| 943 | /* Request authentication agent forwarding if appropriate. */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 944 | check_agent_present(); |
| 945 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 946 | if (options.forward_agent) { |
| 947 | debug("Requesting authentication agent forwarding."); |
| 948 | auth_request_forwarding(); |
| 949 | |
| 950 | /* Read response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 951 | type = packet_read(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 952 | packet_check_eom(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 953 | if (type != SSH_SMSG_SUCCESS) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 954 | logit("Warning: Remote host denied authentication agent forwarding."); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 955 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 956 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 957 | /* Initiate port forwardings. */ |
| 958 | ssh_init_forwarding(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 959 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 960 | /* If requested, let ssh continue in the background. */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 961 | if (fork_after_authentication_flag) |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 962 | if (daemon(1, 1) < 0) |
| 963 | fatal("daemon() failed: %.200s", strerror(errno)); |
| 964 | |
| 965 | /* |
| 966 | * If a command was specified on the command line, execute the |
| 967 | * command now. Otherwise request the server to start a shell. |
| 968 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 969 | if (buffer_len(&command) > 0) { |
| 970 | int len = buffer_len(&command); |
| 971 | if (len > 900) |
| 972 | len = 900; |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 973 | debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 974 | packet_start(SSH_CMSG_EXEC_CMD); |
| 975 | packet_put_string(buffer_ptr(&command), buffer_len(&command)); |
| 976 | packet_send(); |
| 977 | packet_write_wait(); |
| 978 | } else { |
| 979 | debug("Requesting shell."); |
| 980 | packet_start(SSH_CMSG_EXEC_SHELL); |
| 981 | packet_send(); |
| 982 | packet_write_wait(); |
| 983 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 984 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 985 | /* Enter the interactive session. */ |
Ben Lindstrom | 2b1f71b | 2001-06-05 20:32:21 +0000 | [diff] [blame] | 986 | return client_loop(have_tty, tty_flag ? |
| 987 | options.escape_char : SSH_ESCAPECHAR_NONE, 0); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 988 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 989 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 990 | static void |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 991 | ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | 1e7d306 | 2001-02-09 02:36:43 +0000 | [diff] [blame] | 992 | { |
| 993 | int id, len; |
| 994 | |
| 995 | id = packet_get_int(); |
| 996 | len = buffer_len(&command); |
Ben Lindstrom | 4040fe1 | 2001-03-05 06:52:57 +0000 | [diff] [blame] | 997 | if (len > 900) |
| 998 | len = 900; |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 999 | packet_check_eom(); |
Ben Lindstrom | 1e7d306 | 2001-02-09 02:36:43 +0000 | [diff] [blame] | 1000 | if (type == SSH2_MSG_CHANNEL_FAILURE) |
| 1001 | fatal("Request for subsystem '%.*s' failed on channel %d", |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 1002 | len, (u_char *)buffer_ptr(&command), id); |
Ben Lindstrom | 1e7d306 | 2001-02-09 02:36:43 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 1005 | void |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 1006 | client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 1007 | { |
| 1008 | int i; |
| 1009 | |
| 1010 | i = client_global_request_id++; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 1011 | if (i >= options.num_remote_forwards) |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 1012 | return; |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 1013 | debug("remote forward %s for: listen %d, connect %s:%d", |
| 1014 | type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1015 | options.remote_forwards[i].listen_port, |
| 1016 | options.remote_forwards[i].connect_host, |
| 1017 | options.remote_forwards[i].connect_port); |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 1018 | if (type == SSH2_MSG_REQUEST_FAILURE) |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1019 | logit("Warning: remote port forwarding failed for listen " |
| 1020 | "port %d", options.remote_forwards[i].listen_port); |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 1021 | } |
| 1022 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1023 | static void |
| 1024 | ssh_control_listener(void) |
| 1025 | { |
| 1026 | struct sockaddr_un addr; |
| 1027 | mode_t old_umask; |
Damien Miller | 07b6ff1 | 2004-06-15 11:14:45 +1000 | [diff] [blame] | 1028 | int addr_len; |
| 1029 | |
Damien Miller | d14b1e7 | 2005-06-16 13:19:41 +1000 | [diff] [blame] | 1030 | if (options.control_path == NULL || |
| 1031 | options.control_master == SSHCTL_MASTER_NO) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1032 | return; |
| 1033 | |
Damien Miller | d14b1e7 | 2005-06-16 13:19:41 +1000 | [diff] [blame] | 1034 | debug("setting up multiplex master socket"); |
| 1035 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1036 | memset(&addr, '\0', sizeof(addr)); |
| 1037 | addr.sun_family = AF_UNIX; |
Damien Miller | 07b6ff1 | 2004-06-15 11:14:45 +1000 | [diff] [blame] | 1038 | addr_len = offsetof(struct sockaddr_un, sun_path) + |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1039 | strlen(options.control_path) + 1; |
| 1040 | |
| 1041 | if (strlcpy(addr.sun_path, options.control_path, |
| 1042 | sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) |
| 1043 | fatal("ControlPath too long"); |
| 1044 | |
| 1045 | if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 1046 | fatal("%s socket(): %s", __func__, strerror(errno)); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1047 | |
| 1048 | old_umask = umask(0177); |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 1049 | if (bind(control_fd, (struct sockaddr *)&addr, addr_len) == -1) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1050 | control_fd = -1; |
Damien Miller | 4f10e25 | 2005-05-04 15:33:09 +1000 | [diff] [blame] | 1051 | if (errno == EINVAL || errno == EADDRINUSE) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1052 | fatal("ControlSocket %s already exists", |
| 1053 | options.control_path); |
| 1054 | else |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 1055 | fatal("%s bind(): %s", __func__, strerror(errno)); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1056 | } |
| 1057 | umask(old_umask); |
| 1058 | |
| 1059 | if (listen(control_fd, 64) == -1) |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 1060 | fatal("%s listen(): %s", __func__, strerror(errno)); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1061 | |
| 1062 | set_nonblock(control_fd); |
| 1063 | } |
| 1064 | |
Ben Lindstrom | f558cf6 | 2001-09-20 23:13:49 +0000 | [diff] [blame] | 1065 | /* request pty/x11/agent/tcpfwd/shell for channel */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1066 | static void |
Ben Lindstrom | f558cf6 | 2001-09-20 23:13:49 +0000 | [diff] [blame] | 1067 | ssh_session2_setup(int id, void *arg) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1068 | { |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 1069 | extern char **environ; |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 1070 | const char *display; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1071 | int interactive = tty_flag; |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 1072 | |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 1073 | display = getenv("DISPLAY"); |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 1074 | if (options.forward_x11 && display != NULL) { |
Ben Lindstrom | 4a4bd71 | 2001-12-06 17:45:19 +0000 | [diff] [blame] | 1075 | char *proto, *data; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1076 | /* Get reasonable local authentication information. */ |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 1077 | client_x11_get_proto(display, options.xauth_location, |
| 1078 | options.forward_x11_trusted, &proto, &data); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1079 | /* Request forwarding with authentication spoofing. */ |
| 1080 | debug("Requesting X11 forwarding with authentication spoofing."); |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 1081 | x11_request_forwarding_with_spoofing(id, display, proto, data); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1082 | interactive = 1; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1083 | /* XXX wait for reply */ |
| 1084 | } |
| 1085 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1086 | check_agent_present(); |
| 1087 | if (options.forward_agent) { |
| 1088 | debug("Requesting authentication agent forwarding."); |
| 1089 | channel_request_start(id, "auth-agent-req@openssh.com", 0); |
| 1090 | packet_send(); |
| 1091 | } |
| 1092 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1093 | if (options.tun_open != SSH_TUNMODE_NO) { |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1094 | Channel *c; |
| 1095 | int fd; |
| 1096 | |
| 1097 | debug("Requesting tun."); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1098 | if ((fd = tun_open(options.tun_local, |
| 1099 | options.tun_open)) >= 0) { |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1100 | c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1, |
| 1101 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 1102 | 0, "tun", 1); |
| 1103 | c->datagram = 1; |
Damien Miller | 598bbc2 | 2005-12-31 16:33:36 +1100 | [diff] [blame] | 1104 | #if defined(SSH_TUN_FILTER) |
| 1105 | if (options.tun_open == SSH_TUNMODE_POINTOPOINT) |
| 1106 | channel_register_filter(c->self, sys_tun_infilter, |
| 1107 | sys_tun_outfilter); |
| 1108 | #endif |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1109 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
| 1110 | packet_put_cstring("tun@openssh.com"); |
| 1111 | packet_put_int(c->self); |
| 1112 | packet_put_int(c->local_window_max); |
| 1113 | packet_put_int(c->local_maxpacket); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 1114 | packet_put_int(options.tun_open); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1115 | packet_put_int(options.tun_remote); |
| 1116 | packet_send(); |
| 1117 | } |
| 1118 | } |
| 1119 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1120 | client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"), |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 1121 | NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply); |
Ben Lindstrom | 1e7d306 | 2001-02-09 02:36:43 +0000 | [diff] [blame] | 1122 | |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1123 | packet_set_interactive(interactive); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1124 | } |
| 1125 | |
Ben Lindstrom | f558cf6 | 2001-09-20 23:13:49 +0000 | [diff] [blame] | 1126 | /* open new channel for a session */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1127 | static int |
Ben Lindstrom | f558cf6 | 2001-09-20 23:13:49 +0000 | [diff] [blame] | 1128 | ssh_session2_open(void) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1129 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1130 | Channel *c; |
| 1131 | int window, packetmax, in, out, err; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1132 | |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 1133 | if (stdin_null_flag) { |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 1134 | in = open(_PATH_DEVNULL, O_RDONLY); |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 1135 | } else { |
| 1136 | in = dup(STDIN_FILENO); |
| 1137 | } |
| 1138 | out = dup(STDOUT_FILENO); |
| 1139 | err = dup(STDERR_FILENO); |
| 1140 | |
| 1141 | if (in < 0 || out < 0 || err < 0) |
| 1142 | fatal("dup() in/out/err failed"); |
| 1143 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 1144 | /* enable nonblocking unless tty */ |
| 1145 | if (!isatty(in)) |
| 1146 | set_nonblock(in); |
| 1147 | if (!isatty(out)) |
| 1148 | set_nonblock(out); |
| 1149 | if (!isatty(err)) |
| 1150 | set_nonblock(err); |
| 1151 | |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 1152 | window = CHAN_SES_WINDOW_DEFAULT; |
| 1153 | packetmax = CHAN_SES_PACKET_DEFAULT; |
Damien Miller | 19a5945 | 2002-02-19 15:20:57 +1100 | [diff] [blame] | 1154 | if (tty_flag) { |
| 1155 | window >>= 1; |
| 1156 | packetmax >>= 1; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1157 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1158 | c = channel_new( |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1159 | "session", SSH_CHANNEL_OPENING, in, out, err, |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 1160 | window, packetmax, CHAN_EXTENDED_WRITE, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 1161 | "client-session", /*nonblock*/0); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1162 | |
Ben Lindstrom | f558cf6 | 2001-09-20 23:13:49 +0000 | [diff] [blame] | 1163 | debug3("ssh_session2_open: channel_new: %d", c->self); |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1164 | |
Ben Lindstrom | 5ec2645 | 2001-06-09 00:18:51 +0000 | [diff] [blame] | 1165 | channel_send_open(c->self); |
Ben Lindstrom | f558cf6 | 2001-09-20 23:13:49 +0000 | [diff] [blame] | 1166 | if (!no_shell_flag) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1167 | channel_register_confirm(c->self, ssh_session2_setup, NULL); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1168 | |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1169 | return c->self; |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1172 | static int |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1173 | ssh_session2(void) |
| 1174 | { |
Ben Lindstrom | f558cf6 | 2001-09-20 23:13:49 +0000 | [diff] [blame] | 1175 | int id = -1; |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1176 | |
| 1177 | /* XXX should be pre-session */ |
| 1178 | ssh_init_forwarding(); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1179 | ssh_control_listener(); |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1180 | |
Ben Lindstrom | f558cf6 | 2001-09-20 23:13:49 +0000 | [diff] [blame] | 1181 | if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN)) |
| 1182 | id = ssh_session2_open(); |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1183 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1184 | /* Execute a local command */ |
| 1185 | if (options.local_command != NULL && |
| 1186 | options.permit_local_command) |
| 1187 | ssh_local_cmd(options.local_command); |
| 1188 | |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1189 | /* If requested, let ssh continue in the background. */ |
| 1190 | if (fork_after_authentication_flag) |
| 1191 | if (daemon(1, 1) < 0) |
| 1192 | fatal("daemon() failed: %.200s", strerror(errno)); |
| 1193 | |
Ben Lindstrom | 2b1f71b | 2001-06-05 20:32:21 +0000 | [diff] [blame] | 1194 | return client_loop(tty_flag, tty_flag ? |
| 1195 | options.escape_char : SSH_ESCAPECHAR_NONE, id); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1196 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1197 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1198 | static void |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1199 | load_public_identity_files(void) |
| 1200 | { |
Damien Miller | 6b1d53c | 2006-03-31 23:13:21 +1100 | [diff] [blame] | 1201 | char *filename, *cp, thishost[NI_MAXHOST]; |
Ben Lindstrom | 711b04a | 2001-08-06 21:12:42 +0000 | [diff] [blame] | 1202 | int i = 0; |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 1203 | Key *public; |
Damien Miller | 6b1d53c | 2006-03-31 23:13:21 +1100 | [diff] [blame] | 1204 | struct passwd *pw; |
Ben Lindstrom | 711b04a | 2001-08-06 21:12:42 +0000 | [diff] [blame] | 1205 | #ifdef SMARTCARD |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 1206 | Key **keys; |
| 1207 | |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 1208 | if (options.smartcard_device != NULL && |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 1209 | options.num_identity_files < SSH_MAX_IDENTITY_FILES && |
| 1210 | (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) { |
| 1211 | int count = 0; |
| 1212 | for (i = 0; keys[i] != NULL; i++) { |
| 1213 | count++; |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 1214 | memmove(&options.identity_files[1], &options.identity_files[0], |
| 1215 | sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1)); |
| 1216 | memmove(&options.identity_keys[1], &options.identity_keys[0], |
| 1217 | sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1)); |
| 1218 | options.num_identity_files++; |
| 1219 | options.identity_keys[0] = keys[i]; |
Damien Miller | 56a0bb0 | 2003-06-18 20:28:40 +1000 | [diff] [blame] | 1220 | options.identity_files[0] = sc_get_key_label(keys[i]); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 1221 | } |
Ben Lindstrom | 4f05460 | 2002-03-26 03:23:00 +0000 | [diff] [blame] | 1222 | if (options.num_identity_files > SSH_MAX_IDENTITY_FILES) |
| 1223 | options.num_identity_files = SSH_MAX_IDENTITY_FILES; |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 1224 | i = count; |
| 1225 | xfree(keys); |
Ben Lindstrom | 711b04a | 2001-08-06 21:12:42 +0000 | [diff] [blame] | 1226 | } |
Ben Lindstrom | ffce147 | 2001-08-06 21:57:31 +0000 | [diff] [blame] | 1227 | #endif /* SMARTCARD */ |
Damien Miller | 6b1d53c | 2006-03-31 23:13:21 +1100 | [diff] [blame] | 1228 | if ((pw = getpwuid(original_real_uid)) == NULL) |
| 1229 | fatal("load_public_identity_files: getpwuid failed"); |
| 1230 | if (gethostname(thishost, sizeof(thishost)) == -1) |
| 1231 | fatal("load_public_identity_files: gethostname: %s", |
| 1232 | strerror(errno)); |
Ben Lindstrom | 711b04a | 2001-08-06 21:12:42 +0000 | [diff] [blame] | 1233 | for (; i < options.num_identity_files; i++) { |
Damien Miller | 6b1d53c | 2006-03-31 23:13:21 +1100 | [diff] [blame] | 1234 | cp = tilde_expand_filename(options.identity_files[i], |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1235 | original_real_uid); |
Damien Miller | 6b1d53c | 2006-03-31 23:13:21 +1100 | [diff] [blame] | 1236 | filename = percent_expand(cp, "d", pw->pw_dir, |
| 1237 | "u", pw->pw_name, "l", thishost, "h", host, |
| 1238 | "r", options.user, (char *)NULL); |
| 1239 | xfree(cp); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1240 | public = key_load_public(filename, NULL); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1241 | debug("identity file %s type %d", filename, |
| 1242 | public ? public->type : -1); |
| 1243 | xfree(options.identity_files[i]); |
| 1244 | options.identity_files[i] = filename; |
| 1245 | options.identity_keys[i] = public; |
| 1246 | } |
| 1247 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1248 | |
| 1249 | static void |
| 1250 | control_client_sighandler(int signo) |
| 1251 | { |
| 1252 | control_client_terminate = signo; |
| 1253 | } |
| 1254 | |
| 1255 | static void |
| 1256 | control_client_sigrelay(int signo) |
| 1257 | { |
| 1258 | if (control_server_pid > 1) |
| 1259 | kill(control_server_pid, signo); |
| 1260 | } |
| 1261 | |
Darren Tucker | 365433f | 2004-06-22 12:29:23 +1000 | [diff] [blame] | 1262 | static int |
| 1263 | env_permitted(char *env) |
| 1264 | { |
| 1265 | int i; |
| 1266 | char name[1024], *cp; |
| 1267 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1268 | if (strlcpy(name, env, sizeof(name)) >= sizeof(name)) |
| 1269 | fatal("env_permitted: name too long"); |
Darren Tucker | 365433f | 2004-06-22 12:29:23 +1000 | [diff] [blame] | 1270 | if ((cp = strchr(name, '=')) == NULL) |
| 1271 | return (0); |
| 1272 | |
| 1273 | *cp = '\0'; |
| 1274 | |
| 1275 | for (i = 0; i < options.num_send_env; i++) |
| 1276 | if (match_pattern(name, options.send_env[i])) |
| 1277 | return (1); |
| 1278 | |
| 1279 | return (0); |
| 1280 | } |
| 1281 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1282 | static void |
| 1283 | control_client(const char *path) |
| 1284 | { |
| 1285 | struct sockaddr_un addr; |
Darren Tucker | 39207a4 | 2004-11-05 20:19:51 +1100 | [diff] [blame] | 1286 | int i, r, fd, sock, exitval, num_env, addr_len; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1287 | Buffer m; |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 1288 | char *term; |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 1289 | extern char **environ; |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 1290 | u_int flags; |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1291 | |
Damien Miller | d14b1e7 | 2005-06-16 13:19:41 +1000 | [diff] [blame] | 1292 | if (mux_command == 0) |
| 1293 | mux_command = SSHMUX_COMMAND_OPEN; |
| 1294 | |
| 1295 | switch (options.control_master) { |
| 1296 | case SSHCTL_MASTER_AUTO: |
| 1297 | case SSHCTL_MASTER_AUTO_ASK: |
| 1298 | debug("auto-mux: Trying existing master"); |
| 1299 | /* FALLTHROUGH */ |
| 1300 | case SSHCTL_MASTER_NO: |
| 1301 | break; |
| 1302 | default: |
| 1303 | return; |
| 1304 | } |
| 1305 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1306 | memset(&addr, '\0', sizeof(addr)); |
| 1307 | addr.sun_family = AF_UNIX; |
Damien Miller | 07b6ff1 | 2004-06-15 11:14:45 +1000 | [diff] [blame] | 1308 | addr_len = offsetof(struct sockaddr_un, sun_path) + |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1309 | strlen(path) + 1; |
| 1310 | |
| 1311 | if (strlcpy(addr.sun_path, path, |
| 1312 | sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) |
| 1313 | fatal("ControlPath too long"); |
| 1314 | |
| 1315 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
| 1316 | fatal("%s socket(): %s", __func__, strerror(errno)); |
| 1317 | |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 1318 | if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) { |
Darren Tucker | 0814d31 | 2005-06-01 23:08:51 +1000 | [diff] [blame] | 1319 | if (mux_command != SSHMUX_COMMAND_OPEN) { |
| 1320 | fatal("Control socket connect(%.100s): %s", path, |
| 1321 | strerror(errno)); |
| 1322 | } |
Damien Miller | 538c9b7 | 2005-05-26 12:11:28 +1000 | [diff] [blame] | 1323 | if (errno == ENOENT) |
Damien Miller | 4662d34 | 2006-03-26 13:59:59 +1100 | [diff] [blame] | 1324 | debug("Control socket \"%.100s\" does not exist", path); |
Damien Miller | 538c9b7 | 2005-05-26 12:11:28 +1000 | [diff] [blame] | 1325 | else { |
Damien Miller | 4662d34 | 2006-03-26 13:59:59 +1100 | [diff] [blame] | 1326 | error("Control socket connect(%.100s): %s", path, |
Damien Miller | 538c9b7 | 2005-05-26 12:11:28 +1000 | [diff] [blame] | 1327 | strerror(errno)); |
| 1328 | } |
Damien Miller | 4662d34 | 2006-03-26 13:59:59 +1100 | [diff] [blame] | 1329 | close(sock); |
| 1330 | return; |
| 1331 | } |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 1332 | |
Damien Miller | 4662d34 | 2006-03-26 13:59:59 +1100 | [diff] [blame] | 1333 | if (stdin_null_flag) { |
| 1334 | if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1) |
| 1335 | fatal("open(/dev/null): %s", strerror(errno)); |
| 1336 | if (dup2(fd, STDIN_FILENO) == -1) |
| 1337 | fatal("dup2: %s", strerror(errno)); |
| 1338 | if (fd > STDERR_FILENO) |
| 1339 | close(fd); |
| 1340 | } |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 1341 | |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1342 | term = getenv("TERM"); |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 1343 | |
| 1344 | flags = 0; |
| 1345 | if (tty_flag) |
| 1346 | flags |= SSHMUX_FLAG_TTY; |
| 1347 | if (subsystem_flag) |
| 1348 | flags |= SSHMUX_FLAG_SUBSYS; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1349 | if (options.forward_x11) |
| 1350 | flags |= SSHMUX_FLAG_X11_FWD; |
| 1351 | if (options.forward_agent) |
| 1352 | flags |= SSHMUX_FLAG_AGENT_FWD; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1353 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1354 | buffer_init(&m); |
| 1355 | |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 1356 | /* Send our command to server */ |
| 1357 | buffer_put_int(&m, mux_command); |
| 1358 | buffer_put_int(&m, flags); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1359 | if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1) |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 1360 | fatal("%s: msg_send", __func__); |
| 1361 | buffer_clear(&m); |
| 1362 | |
| 1363 | /* Get authorisation status and PID of controlee */ |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1364 | if (ssh_msg_recv(sock, &m) == -1) |
| 1365 | fatal("%s: msg_recv", __func__); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1366 | if (buffer_get_char(&m) != SSHMUX_VER) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1367 | fatal("%s: wrong version", __func__); |
Damien Miller | 23f0770 | 2004-06-18 01:19:03 +1000 | [diff] [blame] | 1368 | if (buffer_get_int(&m) != 1) |
| 1369 | fatal("Connection to master denied"); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1370 | control_server_pid = buffer_get_int(&m); |
| 1371 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1372 | buffer_clear(&m); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1373 | |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 1374 | switch (mux_command) { |
| 1375 | case SSHMUX_COMMAND_ALIVE_CHECK: |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1376 | fprintf(stderr, "Master running (pid=%d)\r\n", |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 1377 | control_server_pid); |
| 1378 | exit(0); |
| 1379 | case SSHMUX_COMMAND_TERMINATE: |
| 1380 | fprintf(stderr, "Exit request sent.\r\n"); |
| 1381 | exit(0); |
| 1382 | case SSHMUX_COMMAND_OPEN: |
| 1383 | /* continue below */ |
| 1384 | break; |
| 1385 | default: |
| 1386 | fatal("silly mux_command %d", mux_command); |
| 1387 | } |
| 1388 | |
| 1389 | /* SSHMUX_COMMAND_OPEN */ |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1390 | buffer_put_cstring(&m, term ? term : ""); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1391 | buffer_append(&command, "\0", 1); |
| 1392 | buffer_put_cstring(&m, buffer_ptr(&command)); |
| 1393 | |
Darren Tucker | 365433f | 2004-06-22 12:29:23 +1000 | [diff] [blame] | 1394 | if (options.num_send_env == 0 || environ == NULL) { |
| 1395 | buffer_put_int(&m, 0); |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1396 | } else { |
Darren Tucker | 365433f | 2004-06-22 12:29:23 +1000 | [diff] [blame] | 1397 | /* Pass environment */ |
| 1398 | num_env = 0; |
| 1399 | for (i = 0; environ[i] != NULL; i++) |
| 1400 | if (env_permitted(environ[i])) |
| 1401 | num_env++; /* Count */ |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1402 | |
Darren Tucker | 365433f | 2004-06-22 12:29:23 +1000 | [diff] [blame] | 1403 | buffer_put_int(&m, num_env); |
| 1404 | |
Darren Tucker | b5bc1a6 | 2004-06-24 00:34:53 +1000 | [diff] [blame] | 1405 | for (i = 0; environ[i] != NULL && num_env >= 0; i++) |
| 1406 | if (env_permitted(environ[i])) { |
| 1407 | num_env--; |
Darren Tucker | 365433f | 2004-06-22 12:29:23 +1000 | [diff] [blame] | 1408 | buffer_put_cstring(&m, environ[i]); |
Darren Tucker | b5bc1a6 | 2004-06-24 00:34:53 +1000 | [diff] [blame] | 1409 | } |
Darren Tucker | 365433f | 2004-06-22 12:29:23 +1000 | [diff] [blame] | 1410 | } |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 1411 | |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1412 | if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1413 | fatal("%s: msg_send", __func__); |
| 1414 | |
| 1415 | mm_send_fd(sock, STDIN_FILENO); |
| 1416 | mm_send_fd(sock, STDOUT_FILENO); |
| 1417 | mm_send_fd(sock, STDERR_FILENO); |
| 1418 | |
| 1419 | /* Wait for reply, so master has a chance to gather ttymodes */ |
| 1420 | buffer_clear(&m); |
| 1421 | if (ssh_msg_recv(sock, &m) == -1) |
| 1422 | fatal("%s: msg_recv", __func__); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 1423 | if (buffer_get_char(&m) != SSHMUX_VER) |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 1424 | fatal("%s: wrong version", __func__); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1425 | buffer_free(&m); |
| 1426 | |
Darren Tucker | 07336da | 2004-11-05 20:02:16 +1100 | [diff] [blame] | 1427 | signal(SIGHUP, control_client_sighandler); |
Damien Miller | 0809e23 | 2004-06-18 22:20:57 +1000 | [diff] [blame] | 1428 | signal(SIGINT, control_client_sighandler); |
| 1429 | signal(SIGTERM, control_client_sighandler); |
| 1430 | signal(SIGWINCH, control_client_sigrelay); |
| 1431 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1432 | if (tty_flag) |
| 1433 | enter_raw_mode(); |
| 1434 | |
| 1435 | /* Stick around until the controlee closes the client_fd */ |
| 1436 | exitval = 0; |
| 1437 | for (;!control_client_terminate;) { |
| 1438 | r = read(sock, &exitval, sizeof(exitval)); |
| 1439 | if (r == 0) { |
| 1440 | debug2("Received EOF from master"); |
| 1441 | break; |
| 1442 | } |
| 1443 | if (r > 0) |
| 1444 | debug2("Received exit status from master %d", exitval); |
| 1445 | if (r == -1 && errno != EINTR) |
| 1446 | fatal("%s: read %s", __func__, strerror(errno)); |
| 1447 | } |
| 1448 | |
| 1449 | if (control_client_terminate) |
| 1450 | debug2("Exiting on signal %d", control_client_terminate); |
| 1451 | |
| 1452 | close(sock); |
| 1453 | |
| 1454 | leave_raw_mode(); |
| 1455 | |
| 1456 | if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET) |
| 1457 | fprintf(stderr, "Connection to master closed.\r\n"); |
| 1458 | |
| 1459 | exit(exitval); |
| 1460 | } |