Don't leak utmpx entries on shutdown.

As far as I know, for all implementations of libutempter, the
utempter_remove_added_record() function depends on the file descriptor
passed to utempter_add_record() to still be valid. The reason for this,
is that this file descriptor is propagated to the setuid utility that is
responsible for modifying utmpx.

Modify the code to remove the utmpx entry before closing the
pseudo-terminal master device. While there, simply use
utempter_remove_record(), which takes the file descriptor explicitly.
The advantage is that this prevents potential foot-shooting in the
future. Visual inspection of the source code will make it more obvious
that utempter depends on the file descriptor.

Closes #179.
This commit is contained in:
Ed Schouten
2012-04-15 12:55:12 +02:00
committed by Keith Winstein
parent 59e8364178
commit 033953dcd2
2 changed files with 7 additions and 7 deletions
+1 -1
View File
@@ -149,7 +149,7 @@ AC_ARG_WITH([utempter],
[with_utempter="$withval"],
[with_utempter="check"])
AS_IF([test x"$with_utempter" != xno],
[AC_SEARCH_LIBS([utempter_remove_added_record], [utempter],
[AC_SEARCH_LIBS([utempter_remove_record], [utempter],
[AC_DEFINE([HAVE_UTEMPTER], [1], [Define if libutempter is available.])],
[AS_IF([test x"$with_utempter" = xcheck],
[AC_MSG_WARN([Unable to find libutempter; utmp entries will not be made.])],
+6 -6
View File
@@ -388,16 +388,16 @@ int run_server( const char *desired_ip, const char *desired_port,
e.text.c_str() );
}
#ifdef HAVE_UTEMPTER
utempter_remove_record( master );
#endif
if ( close( master ) < 0 ) {
perror( "close" );
exit( 1 );
}
delete network;
#ifdef HAVE_UTEMPTER
utempter_remove_added_record();
#endif
}
printf( "\n[mosh-server is exiting.]\n" );
@@ -510,7 +510,7 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
/* update utmp entry if we have become "connected" */
if ( (!connected_utmp)
|| ( saved_addr.s_addr != network.get_remote_ip().s_addr ) ) {
utempter_remove_added_record();
utempter_remove_record( host_fd );
saved_addr = network.get_remote_ip();
@@ -607,7 +607,7 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
/* update utmp if has been more than 10 seconds since heard from client */
if ( connected_utmp ) {
if ( time_since_remote_state > 10000 ) {
utempter_remove_added_record();
utempter_remove_record( host_fd );
char tmp[ 64 ];
snprintf( tmp, 64, "mosh [%d]", getpid() );