Add syslog logging of connections

Log connection change events to syslog in the auth log, logging the PID,
username and remote host.

Also log session begin and end.

Co-Authored-By: John Hood <cgull@glup.org>
This commit is contained in:
Tom Judge
2017-10-24 20:53:52 +00:00
committed by John Hood
parent e8331437d3
commit ab31b0f271
3 changed files with 59 additions and 6 deletions
+47 -5
View File
@@ -52,6 +52,9 @@
#ifdef HAVE_UTEMPTER
#include <utempter.h>
#endif
#ifdef HAVE_SYSLOG
#include <syslog.h>
#endif
#include <sys/socket.h>
#include <netdb.h>
#include <time.h>
@@ -278,6 +281,10 @@ int main( int argc, char *argv[] )
bool with_motd = false;
#ifdef HAVE_SYSLOG
openlog(argv[0], LOG_PID | LOG_NDELAY, LOG_AUTH);
#endif
/* Get shell */
char *my_argv[ 2 ];
string shell_name;
@@ -522,6 +529,10 @@ static int run_server( const char *desired_ip, const char *desired_port,
fatal_assert( 0 == sigaction( SIGHUP, &sa, NULL ) );
fatal_assert( 0 == sigaction( SIGPIPE, &sa, NULL ) );
#ifdef HAVE_SYSLOG
closelog();
#endif
/* close server-related file descriptors */
network.reset();
@@ -652,11 +663,21 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &
#ifdef HAVE_UTEMPTER
bool connected_utmp = false;
#endif
#if defined(HAVE_SYSLOG) || defined(HAVE_UPTEMPTER)
bool force_connection_change_evt = false;
Addr saved_addr;
socklen_t saved_addr_len = 0;
#endif
#ifdef HAVE_SYSLOG
struct passwd *pw = getpwuid( getuid() );
if (pw == NULL) {
throw NetworkException( std::string( "serve: getpwuid: " ) + strerror( errno ), 0 );
}
syslog(LOG_INFO, "user %s session begin", pw->pw_name);
#endif
bool child_released = false;
while ( true ) {
@@ -756,14 +777,25 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &
if ( !network.shutdown_in_progress() ) {
network.set_current_state( terminal );
}
#if defined(HAVE_SYSLOG) || defined(HAVE_UPTEMPTER)
#ifdef HAVE_UTEMPTER
/* update utmp entry if we have become "connected" */
if ( (!connected_utmp)
if (!connected_utmp) {
force_connection_change_evt = true;
} else {
force_connection_change_evt = false;
}
#else
force_connection_change_evt = false;
#endif
/**
* - HAVE_UTEMPTER - update utmp entry if we have become "connected"
* - HAVE_SYSLOG - log connection information to syslog
**/
if ( (force_connection_change_evt)
|| saved_addr_len != network.get_remote_addr_len()
|| memcmp( &saved_addr, &network.get_remote_addr(),
saved_addr_len ) != 0 ) {
utempter_remove_record( host_fd );
saved_addr = network.get_remote_addr();
saved_addr_len = network.get_remote_addr_len();
@@ -776,11 +808,18 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &
throw NetworkException( std::string( "serve: getnameinfo: " ) + gai_strerror( errcode ), 0 );
}
#ifdef HAVE_UTEMPTER
utempter_remove_record( host_fd );
char tmp[ 64 ];
snprintf( tmp, 64, "%s via mosh [%d]", host, getpid() );
utempter_add_record( host_fd, tmp );
connected_utmp = true;
#endif
#ifdef HAVE_SYSLOG
syslog(LOG_INFO, "user %s connected from host: %s", pw->pw_name, host);
#endif
}
#endif
@@ -896,6 +935,9 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &
}
}
}
#ifdef HAVE_SYSLOG
syslog(LOG_INFO, "user %s session end", pw->pw_name);
#endif
}
/* Print the motd from a given file, if available */