[mosh-devel] [PATCH] get account info with getpwnam("$LOGNAME") before getpwuid(geteuid())

Marc Boucher marc at linuxant.com
Tue Dec 25 13:39:21 EST 2012


Hi,

Thanks for mosh ! It's really cool...

However, we encountered a small issue here in our environment which has multiple accounts sharing common uids. Each account may have a distinct name, shell, and home directories in /etc/passwd like:

main:x:500:500:Main Account:/home/main:
shared1:x:500:500:Shared Account 1:/home/sub1:/bin/zsh
shared2:x:500:500:Shared Account 2:/home/sub2:/bin/bash

But when logging in with "mosh shared1 at host", mosh-server would incorrectly use the shell/homedir/username info from the first ("main") account listed in /etc/passwd with the shared uid instead of the proper one, "shared1" (identified by LOGNAME environment variable).

Here's a patch against 1.2.3 to fix this. The existing getpwuid(geteuid()) method is retained as fallback.

Cheers,
Marc

-------------- next part --------------
A non-text attachment was scrubbed...
Name: mosh-getpwnam.patch
Type: application/octet-stream
Size: 1596 bytes
Desc: not available
Url : http://mailman.mit.edu/pipermail/mosh-devel/attachments/20121225/e813665a/attachment.obj
-------------- next part --------------


--- mosh-1.2.3/src/frontend/mosh-server.cc	2012/12/25 18:01:40	1.1
+++ mosh-1.2.3/src/frontend/mosh-server.cc	2012/12/25 18:14:26
@@ -244,10 +244,17 @@
   char *my_argv[ 2 ];
   if ( !command_argv ) {
     /* get shell name */
-    struct passwd *pw = getpwuid( geteuid() );
+    struct passwd *pw = NULL;
+    char *ln = getenv( "LOGNAME" );
+    if ( ln ) {
+      pw = getpwnam( ln );
+    }
     if ( pw == NULL ) {
-      perror( "getpwuid" );
-      exit( 1 );
+      pw = getpwuid( geteuid() );
+      if ( pw == NULL ) {
+        perror( "getpwuid" );
+        exit( 1 );
+      }
     }
 
     string shell_path( pw->pw_shell );
@@ -754,10 +761,17 @@
 
 void chdir_homedir( void )
 {
-  struct passwd *pw = getpwuid( geteuid() );
+  struct passwd *pw = NULL;
+  char *ln = getenv( "LOGNAME" );
+  if ( ln ) {
+    pw = getpwnam( ln );
+  }
   if ( pw == NULL ) {
-    perror( "getpwuid" );
-    return; /* non-fatal */
+    pw = getpwuid( geteuid() );
+    if ( pw == NULL ) {
+      perror( "getpwuid" );
+      return; /* non-fatal */
+    }
   }
 
   if ( chdir( pw->pw_dir ) < 0 ) {
@@ -793,11 +807,18 @@
 {
 #ifdef HAVE_UTMPX_H
   /* get username */
-  const struct passwd *pw = getpwuid( geteuid() );
+  const struct passwd *pw = NULL;
+  char *ln = getenv( "LOGNAME" );
+  if ( ln ) {
+    pw = getpwnam( ln );
+  }
   if ( pw == NULL ) {
-    perror( "getpwuid" );
-    /* non-fatal */
-    return;
+    pw = getpwuid( geteuid() );
+    if ( pw == NULL ) {
+      perror( "getpwuid" );
+      /* non-fatal */
+      return;
+    }
   }
 
   const string username( pw->pw_name );




More information about the mosh-devel mailing list