From 1508d40b4c0ab3962418feee4fa2972d141b2acc Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Thu, 19 Apr 2012 01:03:25 -0400 Subject: [PATCH] Chdir to homedir (closes #193) and honor .hushlogin (closes #216). --- configure.ac | 2 +- src/frontend/mosh-server.cc | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index f1195cf..6e1153b 100644 --- a/configure.ac +++ b/configure.ac @@ -201,7 +201,7 @@ AM_CONDITIONAL([COND_THIRD_LIBSTDDJB], [test x"$have_signalfd" = xno && test x"$with_skalibs" = xno]) # Checks for header files. -AC_CHECK_HEADERS([arpa/inet.h fcntl.h langinfo.h limits.h locale.h netinet/in.h stddef.h stdint.h inttypes.h stdlib.h string.h sys/ioctl.h sys/resource.h sys/socket.h sys/time.h termios.h unistd.h wchar.h wctype.h], [], [AC_MSG_ERROR([Missing required header file.])]) +AC_CHECK_HEADERS([arpa/inet.h fcntl.h langinfo.h limits.h locale.h netinet/in.h stddef.h stdint.h inttypes.h stdlib.h string.h sys/ioctl.h sys/resource.h sys/socket.h sys/stat.h sys/time.h termios.h unistd.h wchar.h wctype.h], [], [AC_MSG_ERROR([Missing required header file.])]) AC_CHECK_HEADERS([pty.h util.h libutil.h paths.h]) AC_CHECK_HEADERS([endian.h sys/endian.h]) diff --git a/src/frontend/mosh-server.cc b/src/frontend/mosh-server.cc index 45ff135..0697bab 100644 --- a/src/frontend/mosh-server.cc +++ b/src/frontend/mosh-server.cc @@ -39,6 +39,7 @@ #include #include #include +#include #ifdef HAVE_PATHS_H #include @@ -85,6 +86,8 @@ void print_usage( const char *argv0 ) } void print_motd( void ); +void chdir_homedir( void ); +bool motd_hushed( void ); /* Simple spinloop */ void spin( void ) @@ -399,7 +402,9 @@ int run_server( const char *desired_ip, const char *desired_port, exit( 1 ); } - if ( with_motd ) { + chdir_homedir(); + + if ( with_motd && (!motd_hushed()) ) { print_motd(); } @@ -711,3 +716,27 @@ void print_motd( void ) fclose( motd ); } + +void chdir_homedir( void ) +{ + struct passwd *pw = getpwuid( geteuid() ); + if ( pw == NULL ) { + perror( "getpwuid" ); + /* non-fatal */ + } + + if ( chdir( pw->pw_dir ) < 0 ) { + perror( "chdir" ); + } + + if ( setenv( "PWD", pw->pw_dir, 1 ) < 0 ) { + perror( "setenv" ); + } +} + +bool motd_hushed( void ) +{ + /* must be in home directory already */ + struct stat buf; + return (0 == lstat( ".hushlogin", &buf )); +}