/*
Mosh: the mobile shell
Copyright 2012 Keith Winstein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, the copyright holders give
permission to link the code of portions of this program with the
OpenSSL library under certain conditions as described in each
individual source file, and distribute linked combinations including
the two.
You must obey the GNU General Public License in all respects for all
of the code used other than OpenSSL. If you modify file(s) with this
exception, you may extend this exception to your version of the
file(s), but you are not obligated to do so. If you do not wish to do
so, delete this exception statement from your version. If you delete
this exception statement from all source files in the program, then
also delete it here.
*/
#include "src/include/config.h"
#include "src/include/version.h"
#include <err.h>
#include <errno.h>
#include <locale.h>
#include <string.h>
#include <strings.h>
#include <sstream>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <pwd.h>
#include <typeinfo>
#include <signal.h>
#ifdef HAVE_UTEMPTER
#include <utempter.h>
#endif
#ifdef HAVE_SYSLOG
#include <syslog.h>
#endif
#include <sys/socket.h>
#include <netdb.h>
#include <time.h>
#include <sys/stat.h>
#include <inttypes.h>
#ifdef HAVE_UTMPX_H
#include <utmpx.h>
#endif
#ifdef HAVE_PATHS_H
#include <paths.h>
#endif
#if HAVE_PTY_H
#include <pty.h>
#elif HAVE_UTIL_H
#include <util.h>
#endif
#if FORKPTY_IN_LIBUTIL
#include <libutil.h>
#endif
#include "src/statesync/completeterminal.h"
#include "src/util/swrite.h"
#include "src/statesync/user.h"
#include "src/util/fatal_assert.h"
#include "src/util/locale_utils.h"
#include "src/util/pty_compat.h"
#include "src/util/select.h"
#include "src/util/timestamp.h"
#include "src/util/fatal_assert.h"
#ifndef _PATH_BSHELL
#define _PATH_BSHELL "/bin/sh"
#endif
#include "src/network/networktransport-impl.h"
using ServerConnection = Network::Transport<Terminal::Complete, Network::UserStream>;
static void serve( int host_fd,
Terminal::Complete &terminal,
ServerConnection &network,
long network_timeout,
long network_signaled_timeout );
static int run_server( const char *desired_ip, const char *desired_port,
const std::string &command_path, char *command_argv[],
const int colors, unsigned int verbose, bool with_motd );
static void print_version( FILE *file )
{
fputs( "mosh-server (" PACKAGE_STRING ") [build " BUILD_VERSION "]\n"
"Copyright 2012 Keith Winstein <mosh-devel@mit.edu>\n"
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n", file );
}
static void print_usage( FILE *stream, const char *argv0 )
{
fprintf( stream, "Usage: %s new [-s] [-v] [-i LOCALADDR] [-p PORT[:PORT2]] [-c COLORS] [-l NAME=VALUE] [-- COMMAND...]\n", argv0 );
}
static bool print_motd( const char *filename );
static void chdir_homedir( void );
static bool motd_hushed( void );
static void warn_unattached( const std::string & ignore_entry );
/* Simple spinloop */
static void spin( void )
{
static unsigned int spincount = 0;
spincount++;
if ( spincount > 10 ) {
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = 100000000; /* 0.1 sec */
nanosleep( &req, NULL );
freeze_timestamp();
}
}
static std::string get_SSH_IP( void )
{
const char *SSH_CONNECTION = getenv( "SSH_CONNECTION" );
if ( !SSH_CONNECTION ) { /* Older sshds don't set this */
fputs( "Warning: SSH_CONNECTION not found; binding to any interface.\n", stderr );
return std::string( "" );
}
std::istringstream ss( SSH_CONNECTION );
std::string dummy, local_interface_IP;
ss >> dummy >> dummy >> local_interface_IP;
if ( !ss ) {
fputs( "Warning: Could not parse SSH_CONNECTION; binding to any interface.\n", stderr );
return std::string( "" );
}
/* Strip IPv6 prefix. */
const char IPv6_prefix[] = "::ffff:";
if ( ( local_interface_IP.