Warn of unattached Mosh sessions on login

This commit is contained in:
Keith Winstein
2012-07-23 01:16:57 -04:00
parent 507c2cb6b2
commit 549cf0a9bd
+80 -7
View File
@@ -101,6 +101,7 @@ void print_usage( const char *argv0 )
void print_motd( void ); void print_motd( void );
void chdir_homedir( void ); void chdir_homedir( void );
bool motd_hushed( void ); bool motd_hushed( void );
void warn_unattached( const char *ignore_entry );
/* Simple spinloop */ /* Simple spinloop */
void spin( void ) void spin( void )
@@ -380,6 +381,14 @@ int run_server( const char *desired_ip, const char *desired_port,
fclose( stderr ); fclose( stderr );
} }
char utmp_entry[ 64 ] = { 0 };
#ifdef HAVE_UTEMPTER
/* make utmp entry */
snprintf( utmp_entry, 64, "mosh [%d]", getpid() );
utempter_add_record( master, utmp_entry );
#endif
/* Fork child process */ /* Fork child process */
pid_t child = forkpty( &master, NULL, &child_termios, &window_size ); pid_t child = forkpty( &master, NULL, &child_termios, &window_size );
@@ -434,6 +443,8 @@ int run_server( const char *desired_ip, const char *desired_port,
print_motd(); print_motd();
} }
warn_unattached( utmp_entry );
Crypto::reenable_dumping_core(); Crypto::reenable_dumping_core();
if ( execvp( command_path.c_str(), command_argv ) < 0 ) { if ( execvp( command_path.c_str(), command_argv ) < 0 ) {
@@ -443,13 +454,6 @@ int run_server( const char *desired_ip, const char *desired_port,
} else { } else {
/* parent */ /* parent */
#ifdef HAVE_UTEMPTER
/* make utmp entry */
char tmp[ 64 ];
snprintf( tmp, 64, "mosh [%d]", getpid() );
utempter_add_record( master, tmp );
#endif
try { try {
serve( master, terminal, *network ); serve( master, terminal, *network );
} catch ( const Network::NetworkException& e ) { } catch ( const Network::NetworkException& e ) {
@@ -740,3 +744,72 @@ bool motd_hushed( void )
struct stat buf; struct stat buf;
return (0 == lstat( ".hushlogin", &buf )); return (0 == lstat( ".hushlogin", &buf ));
} }
string mosh_read_line( FILE *file )
{
string ret;
while ( !feof( file ) ) {
char next = getc( file );
if ( next == '\n' ) {
return ret;
}
ret.push_back( next );
}
return ret;
}
void warn_unattached( const char *ignore_entry )
{
/* get username */
const struct passwd *pw = getpwuid( geteuid() );
if ( pw == NULL ) {
perror( "getpwuid" );
/* non-fatal */
return;
}
const string username( pw->pw_name );
/* look for unattached sessions */
FILE *who_cmd = popen( "who", "r" );
if ( who_cmd == NULL ) {
return;
}
vector< string > unattached_who_lines;
while ( !feof( who_cmd ) ) {
/* read the line */
const string line = mosh_read_line( who_cmd );
/* does line start with username? */
if ( line.substr( 0, username.size() + 1 ) == username + " " ) {
/* does line show unattached mosh session? */
if ( line.npos != line.find( "(mosh" ) ) {
/* is line showing _this_ mosh session? */
const string our_entry = string( "(" ) + ignore_entry + string( ")" );
if ( line.npos == line.find( our_entry ) ) {
unattached_who_lines.push_back( line );
}
}
}
}
/* print out warning if necessary */
if ( unattached_who_lines.empty() ) {
return;
} else if ( unattached_who_lines.size() == 1 ) {
printf( "\nNote: This Mosh server is detached.\n" );
} else {
printf( "\nNote: These Mosh servers are detached.\n" );
}
for ( vector< string >::const_iterator it = unattached_who_lines.begin();
it != unattached_who_lines.end();
it++ ) {
printf( "%s\n", it->c_str() );
}
printf( "\n" );
}