| 1 | // Mosh: the mobile shell |
| 2 | // Copyright 2012 Keith Winstein |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | #include <limits.h> |
| 18 | #include <unistd.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <stdarg.h> |
| 21 | #include <string.h> |
| 22 | #include <vector> |
| 23 | #include <map> |
| 24 | #include <stdio.h> |
| 25 | #include <string> |
| 26 | #include <sys/socket.h> |
| 27 | #include <getopt.h> |
| 28 | #include <arpa/inet.h> |
| 29 | #include <netdb.h> |
| 30 | #include <signal.h> |
| 31 | #include <errno.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/wait.h> |
| 35 | #include <termios.h> |
| 36 | #include <pty.h> |
| 37 | |
| 38 | using namespace std; |
| 39 | |
| 40 | inline string shell_quote_string( const string &x ) |
| 41 | { |
| 42 | string result = "'"; |
| 43 | string rest = x; |
| 44 | while ( rest.size() ) { |
| 45 | size_t good_part = rest.find( "'" ); |
| 46 | result += rest.substr( 0, good_part ); |
| 47 | if ( good_part != string::npos ) { |
| 48 | result += "'\\''"; |
| 49 | rest = rest.substr( good_part + 1 ); |
| 50 | } else { |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | return result + "'"; |
| 55 | } |
| 56 | |
| 57 | template <typename SequenceT> |
| 58 | inline string shell_quote( const SequenceT &sequence ) |
| 59 | { |
| 60 | string result; |
| 61 | for ( typename SequenceT::const_iterator i = sequence.begin(); |
| 62 | i != sequence.end(); |
| 63 | i++ ) { |
| 64 | result += shell_quote_string( *i ) + " "; |
| 65 | } |
| 66 | return result.substr( 0, result.size() - 1 ); |
| 67 | } |
| 68 | |
| 69 | void die( const char *format, ... ) { |
| 70 | va_list args; |
| 71 | va_start( args, format ); |
| 72 | vfprintf( stderr, format, args ); |
| 73 | va_end( args ); |
| 74 | fprintf( stderr, "\n" ); |
| 75 | exit( 255 ); |
| 76 | } |
| 77 | |
| 78 | static const char *usage_format = |
| 79 | "Usage: %s [options] [--] [user@]host [command...]\n" |
| 80 | " --client=PATH mosh client on local machine\n" |
| 81 | " (default: \"mosh-client\")\n" |
| 82 | " --server=COMMAND mosh server on remote machine\n" |
| 83 | " (default: \"mosh-server\")\n" |
| 84 | "\n" |
| 85 | " --predict=adaptive local echo for slower links [default]\n" |
| 86 | "-a --predict=always use local echo even on fast links\n" |
| 87 | "-n --predict=never never use local echo\n" |
| 88 | "-6 use IPv6 only\n" |
| 89 | "\n" |
| 90 | "-p NUM --port=NUM server-side UDP port\n" |
| 91 | "\n" |
| 92 | "-P NUM --ssh-port=NUM ssh server port\n" |
| 93 | " (default: let the ssh command choose)\n" |
| 94 | "\n" |
| 95 | " --ssh=COMMAND ssh command to run when setting up session\n" |
| 96 | " (example: \"ssh -p 2222\")\n" |
| 97 | " (default: \"ssh\")\n" |
| 98 | "\n" |
| 99 | " --no-init do not send terminal initialization string\n" |
| 100 | "\n" |
| 101 | " --help this message\n" |
| 102 | " --version version and copyright information\n" |
| 103 | "\n" |
| 104 | "Please report bugs to mosh-devel@mit.edu.\n" |
| 105 | "Mosh home page: http://mosh.mit.edu"; |
| 106 | |
| 107 | static const char *version_format = |
| 108 | "mosh %s\n" |
| 109 | "Copyright 2012 Keith Winstein <mosh-devel@mit.edu>\n" |
| 110 | "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n" |
| 111 | "This is free software: you are free to change and redistribute it.\n" |
| 112 | "There is NO WARRANTY, to the extent permitted by law."; |
| 113 | |
| 114 | static const char *key_valid_char_set = |
| 115 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/+"; |
| 116 | |
| 117 | static char *argv0; |
| 118 | |
| 119 | void predict_check( const string &predict, bool env_set ) |
| 120 | { |
| 121 | if ( predict != "adaptive" && |
| 122 | predict != "always" && |
| 123 | predict != "never" ) { |
| 124 | fprintf( stderr, "%s: Unknown mode \"%s\"%s.\n", argv0, predict.c_str(), |
| 125 | env_set ? " (MOSH_PREDICTION_DISPLAY in environment)" : "" ); |
| 126 | die( usage_format, argv0 ); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void cat( int ifd, int ofd ) |
| 131 | { |
| 132 | char buf[4096]; |
| 133 | ssize_t n; |
| 134 | while ( 1 ) { |
| 135 | n = read( ifd, buf, sizeof( buf ) ); |
| 136 | if ( n==-1 ) { |
| 137 | if (errno == EINTR ) { |
| 138 | continue; |
| 139 | } |
| 140 | break; |
| 141 | } |
| 142 | if ( n==0 ) { |
| 143 | break; |
| 144 | } |
| 145 | n = write( ofd, buf, n ); |
| 146 | if ( n==-1 ) { |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | bool valid_port(string port) { |
| 153 | if ( port.size() ) { |
| 154 | return port.find_first_not_of( "0123456789" ) == string::npos && |
| 155 | atoi( port.c_str() ) > 0 && |
| 156 | atoi( port.c_str() ) <= 65535; |
| 157 | } |
| 158 | return true; // consider no port to be the default value |
| 159 | } |
| 160 | |
| 161 | int main( int argc, char *argv[] ) |
| 162 | { |
| 163 | argv0 = argv[0]; |
| 164 | string client = "mosh-client"; |
| 165 | string server = "mosh-server"; |
| 166 | string ssh = "ssh"; |
| 167 | string predict, port_request, ssh_port; |
| 168 | int help=0, version=0, fake_proxy=0, term_init=1; |
| 169 | int force_ipv6 = 0; |
| 170 | |
| 171 | static struct option long_options[] = |
| 172 | { |
| 173 | { "client", required_argument, 0, 'c' }, |
| 174 | { "server", required_argument, 0, 's' }, |
| 175 | { "no-init", no_argument, &term_init, 0 }, |
| 176 | { "predict", required_argument, 0, 'r' }, |
| 177 | { "port", required_argument, 0, 'p' }, |
| 178 | { "ssh-port", required_argument, 0, 'P' }, |
| 179 | { "ssh", required_argument, 0, 'S' }, |
| 180 | { "help", no_argument, &help, 1 }, |
| 181 | { "version", no_argument, &version, 1 }, |
| 182 | { "fake-proxy", no_argument, &fake_proxy, 1 }, |
| 183 | { 0, 0, 0, 0 } |
| 184 | }; |
| 185 | while ( 1 ) { |
| 186 | int option_index = 0; |
| 187 | int c = getopt_long( argc, argv, "6anp:P:", |
| 188 | long_options, &option_index ); |
| 189 | if ( c == -1 ) { |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | switch ( c ) { |
| 194 | case 0: |
| 195 | // flag has been set |
| 196 | break; |
| 197 | case 'c': |
| 198 | client = optarg; |
| 199 | break; |
| 200 | case 's': |
| 201 | server = optarg; |
| 202 | break; |
| 203 | case 'r': |
| 204 | predict = optarg; |
| 205 | break; |
| 206 | case 'p': |
| 207 | port_request = optarg; |
| 208 | break; |
| 209 | case 'P': |
| 210 | ssh_port = optarg; |
| 211 | break; |
| 212 | case 'S': |
| 213 | ssh = optarg; |
| 214 | break; |
| 215 | case 'a': |
| 216 | predict = "always"; |
| 217 | break; |
| 218 | case 'n': |
| 219 | predict = "never"; |
| 220 | break; |
| 221 | case '6': |
| 222 | force_ipv6 = true; |
| 223 | break; |
| 224 | default: |
| 225 | die( usage_format, argv[0] ); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if ( help ) { |
| 230 | die( usage_format, argv[0] ); |
| 231 | } |
| 232 | if ( version ) { |
| 233 | die( version_format, PACKAGE_VERSION ); |
| 234 | } |
| 235 | |
| 236 | if ( predict.size() ) { |
| 237 | predict_check( predict, 0 ); |
| 238 | } else if ( getenv( "MOSH_PREDICTION_DELAY" ) ) { |
| 239 | predict = getenv( "MOSH_PREDICTION_DELAY" ); |
| 240 | predict_check( predict, 1 ); |
| 241 | } else { |
| 242 | predict = "adaptive"; |
| 243 | predict_check( predict, 0 ); |
| 244 | } |
| 245 | |
| 246 | if(!valid_port(port_request)) { |
| 247 | die( "%s: Server-side port (%s) must be within valid range [0..65535].", |
| 248 | argv[0], |
| 249 | port_request.c_str() ); |
| 250 | } |
| 251 | |
| 252 | if(!valid_port(ssh_port)) { |
| 253 | die( "%s: SSH port (%s) must be within valid range [0..65535].", |
| 254 | argv[0], |
| 255 | ssh_port.c_str() ); |
| 256 | } |
| 257 | |
| 258 | unsetenv( "MOSH_PREDICTION_DISPLAY" ); |
| 259 | |
| 260 | if ( fake_proxy ) { |
| 261 | string host = argv[optind++]; |
| 262 | string port = argv[optind++]; |
| 263 | |
| 264 | int sockfd = -1; |
| 265 | struct addrinfo hints, *servinfo, *p; |
| 266 | int rv; |
| 267 | |
| 268 | memset( &hints, 0, sizeof( hints ) ); |
| 269 | hints.ai_socktype = SOCK_STREAM; |
| 270 | if (force_ipv6) { |
| 271 | hints.ai_family = AF_INET6; |
| 272 | } |
| 273 | |
| 274 | if ( ( rv = getaddrinfo( host.c_str(), |
| 275 | port.c_str(), |
| 276 | &hints, |
| 277 | &servinfo ) ) != 0 ) { |
| 278 | die( "%s: Could not resolve hostname %s: getaddrinfo: %s", |
| 279 | argv[0], |
| 280 | host.c_str(), |
| 281 | gai_strerror( rv ) ); |
| 282 | } |
| 283 | |
| 284 | int try_family = AF_INET; |
| 285 | if (force_ipv6) { |
| 286 | try_family = AF_INET6; |
| 287 | } |
| 288 | // loop through all the results and connect to the first we can |
| 289 | for ( p = servinfo; p != NULL || try_family == AF_INET; p = p->ai_next ) { |
| 290 | if(p == NULL && try_family == AF_INET) { // start over and try AF_INET6 |
| 291 | p = servinfo; |
| 292 | try_family = AF_INET6; |
| 293 | } |
| 294 | if(p == NULL) { |
| 295 | break; // servinfo == NULL |
| 296 | } |
| 297 | |
| 298 | if(p->ai_family != try_family) { |
| 299 | continue; |
| 300 | } |
| 301 | |
| 302 | if ( ( sockfd = socket( p->ai_family, SOCK_STREAM, IPPROTO_TCP ) ) == -1 ) { |
| 303 | continue; |
| 304 | } |
| 305 | |
| 306 | if ( connect( sockfd, p->ai_addr, p->ai_addrlen ) == -1 ) { |
| 307 | close( sockfd ); |
| 308 | continue; |
| 309 | } |
| 310 | |
| 311 | char host[NI_MAXHOST], service[NI_MAXSERV]; |
| 312 | if ( getnameinfo( p->ai_addr, p->ai_addrlen, |
| 313 | host, NI_MAXHOST, |
| 314 | service, NI_MAXSERV, |
| 315 | NI_NUMERICSERV | NI_NUMERICHOST ) == -1 ) { |
| 316 | die( "Couldn't get host name info" ); |
| 317 | } |
| 318 | |
| 319 | fprintf( stderr, "MOSH IP %s\n", host ); |
| 320 | break; // if we get here, we must have connected successfully |
| 321 | } |
| 322 | |
| 323 | if ( p == NULL ) { |
| 324 | // looped off the end of the list with no connection |
| 325 | die( "%s: failed to connect to host %s port %s", |
| 326 | argv[0], host.c_str(), port.c_str() ); |
| 327 | } |
| 328 | |
| 329 | freeaddrinfo( servinfo ); // all done with this structure |
| 330 | |
| 331 | int pid = fork(); |
| 332 | if ( pid == -1 ) die( "%s: fork: %d", argv[0], errno ); |
| 333 | if ( pid == 0 ) { |
| 334 | close( STDIN_FILENO ); |
| 335 | cat( sockfd, STDOUT_FILENO ); |
| 336 | shutdown( sockfd, 0 ); |
| 337 | exit( 0 ); |
| 338 | } |
| 339 | signal( SIGHUP, SIG_IGN ); |
| 340 | close( STDOUT_FILENO ); |
| 341 | cat( STDIN_FILENO, sockfd ); |
| 342 | shutdown( sockfd, SHUT_WR /* = 1 */ ); |
| 343 | close( STDIN_FILENO ); |
| 344 | waitpid( pid, NULL, 0 ); |
| 345 | exit( 0 ); |
| 346 | } |
| 347 | |
| 348 | if ( argc - optind < 1 ) { |
| 349 | die( usage_format, argv[0] ); |
| 350 | } |
| 351 | |
| 352 | string userhost = argv[optind++]; |
| 353 | char **command = &argv[optind]; |
| 354 | int commands = argc - optind; |
| 355 | |
| 356 | char *buf = NULL; |
| 357 | size_t buf_sz = 0; |
| 358 | ssize_t n; |
| 359 | |
| 360 | int pty, pty_slave; |
| 361 | struct winsize ws; |
| 362 | if ( ioctl( 0, TIOCGWINSZ, &ws ) == -1 ) { |
| 363 | die( "%s: ioctl: %d", argv[0], errno ); |
| 364 | } |
| 365 | |
| 366 | if ( openpty( &pty, &pty_slave, NULL, NULL, &ws ) == -1 ) { |
| 367 | die( "%s: openpty: %d", argv[0], errno ); |
| 368 | } |
| 369 | |
| 370 | int pid = fork(); |
| 371 | if ( pid == -1 ) die( "%s: fork: %d", argv[0], errno ); |
| 372 | if ( pid == 0 ) { |
| 373 | close( pty ); |
| 374 | if ( -1 == dup2( pty_slave, 1 ) || |
| 375 | -1 == dup2( pty_slave, 2 ) ) { |
| 376 | die( "%s: dup2: %d", argv[0], errno ); |
| 377 | } |
| 378 | close( pty_slave ); |
| 379 | |
| 380 | vector<string> server_args; |
| 381 | server_args.push_back( "new" ); |
| 382 | server_args.push_back( "-c" ); |
| 383 | server_args.push_back( "256" ); |
| 384 | server_args.push_back( "-s" ); |
| 385 | if ( port_request.size() ) { |
| 386 | server_args.push_back( "-p" ); |
| 387 | server_args.push_back( port_request ); |
| 388 | } |
| 389 | |
| 390 | for (char const* env_name : { |
| 391 | "LANG", "LANGUAGE", "LC_CTYPE", "LC_NUMERIC", |
| 392 | "LC_TIME", "LC_COLLATE", "LC_MONETARY", "LC_MESSAGES", "LC_PAPER", |
| 393 | "LC_NAME", "LC_ADDRESS", "LC_TELEPHONE", "LC_MEASUREMENT", |
| 394 | "LC_IDENTIFICATION", "LC_ALL" }) { |
| 395 | char* env_value = getenv(env_name); |
| 396 | if (env_value) { |
| 397 | server_args.push_back("-l"); |
| 398 | server_args.push_back(string(env_name) + "=" + env_value); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if ( commands ) { |
| 403 | server_args.push_back( "--" ); |
| 404 | server_args.insert( server_args.end(), command, command + commands ); |
| 405 | } |
| 406 | |
| 407 | string quoted_self = shell_quote_string( string( argv[0] ) ); |
| 408 | string quoted_server_args = shell_quote( server_args ); |
| 409 | fflush( stdout ); |
| 410 | |
| 411 | string proxy_arg = "ProxyCommand=" + quoted_self; |
| 412 | if (force_ipv6) { |
| 413 | proxy_arg += " -6"; |
| 414 | } |
| 415 | proxy_arg += " --fake-proxy -- %h %p"; |
| 416 | string ssh_remote_command = server + " " + quoted_server_args; |
| 417 | |
| 418 | vector<string> ssh_args; |
| 419 | ssh_args.push_back( "-n" ); |
| 420 | ssh_args.push_back( "-tt" ); |
| 421 | ssh_args.push_back( "-S" ); |
| 422 | ssh_args.push_back( "none" ); |
| 423 | ssh_args.push_back( "-o" ); |
| 424 | ssh_args.push_back( proxy_arg ); |
| 425 | ssh_args.push_back( userhost ); |
| 426 | if ( ssh_port.size() ) { |
| 427 | ssh_args.push_back( "-p" ); |
| 428 | ssh_args.push_back( ssh_port ); |
| 429 | } |
| 430 | if ( force_ipv6 ) { |
| 431 | ssh_args.push_back( "-6" ); |
| 432 | } |
| 433 | ssh_args.push_back( "--" ); |
| 434 | ssh_args.push_back( ssh_remote_command ); |
| 435 | |
| 436 | string ssh_exec_string = ssh + " " + shell_quote( ssh_args ); |
| 437 | |
| 438 | int ret = execlp( "sh", "sh", "-c", ssh_exec_string.c_str(), (char *)NULL ); |
| 439 | if ( ret == -1 ) { |
| 440 | die( "Cannot exec ssh: %d", errno ); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | close( pty_slave ); |
| 445 | string ip, port, key; |
| 446 | |
| 447 | FILE *pty_file = fdopen( pty, "r" ); |
| 448 | string line; |
| 449 | while ( ( n = getline( &buf, &buf_sz, pty_file ) ) >= 0 ) { |
| 450 | line = string( buf, n ); |
| 451 | line = line.erase( line.find_last_not_of( "\n" ) ); |
| 452 | if ( line.compare( 0, 8, "MOSH IP " ) == 0 ) { |
| 453 | size_t ip_end = line.find_last_not_of( " \t\n\r" ); |
| 454 | if ( ip_end != string::npos && ip_end >= 8 ) { |
| 455 | ip = line.substr( 8, ip_end + 1 - 8 ); |
| 456 | } |
| 457 | } else if ( line.compare( 0, 13, "MOSH CONNECT " ) == 0 ) { |
| 458 | size_t port_end = line.find_first_not_of( "0123456789", 13 ); |
| 459 | if ( port_end != string::npos && port_end >= 13 ) { |
| 460 | port = line.substr( 13, port_end - 13 ); |
| 461 | } |
| 462 | string rest = line.substr( port_end + 1 ); |
| 463 | size_t key_end = rest.find_last_not_of( " \t\n\r" ); |
| 464 | size_t key_valid_end = rest.find_last_of( key_valid_char_set ); |
| 465 | if ( key_valid_end == key_end && key_end + 1 == 22 ) { |
| 466 | key = rest.substr( 0, key_end + 1 ); |
| 467 | } |
| 468 | break; |
| 469 | } else { |
| 470 | printf( "%s\n", line.c_str() ); |
| 471 | } |
| 472 | } |
| 473 | waitpid( pid, NULL, 0 ); |
| 474 | |
| 475 | if ( !ip.size() ) { |
| 476 | die( "%s: Did not find remote IP address (is SSH ProxyCommand disabled?).", |
| 477 | argv[0] ); |
| 478 | } |
| 479 | |
| 480 | if ( !key.size() || !port.size() ) { |
| 481 | die( "%s: Did not find mosh server startup message.", argv[0] ); |
| 482 | } |
| 483 | |
| 484 | setenv( "MOSH_KEY", key.c_str(), 1 ); |
| 485 | setenv( "MOSH_PREDICTION_DISPLAY", predict.c_str(), 1 ); |
| 486 | if (!term_init) setenv( "MOSH_NO_TERM_INIT", "1", 1 ); |
| 487 | execlp( client.c_str(), client.c_str(), ip.c_str(), port.c_str(), (char *)NULL ); |
| 488 | } |