zsh-workers
 help / color / mirror / code / Atom feed
* Wrong emulation mode if exec'd by su
@ 1998-06-15 13:58 Martin Aumueller
  1998-06-15 14:28 ` Zefram
  0 siblings, 1 reply; 4+ messages in thread
From: Martin Aumueller @ 1998-06-15 13:58 UTC (permalink / raw)
  To: zsh-workers; +Cc: aumuelle

Hi!

I have a problem with zsh-3.1.4: If I su - to an account the zsh sees
"su" as executable name -- starting with 's' -- and thus switches to
Bourne shell emulation mode, not the behaviour desired by me. I have
solved this problem by testing the full executable names against "sh",
"csh" and "ksh" with strcmp and now it works as I expect it.

However, in the patch below I didn't take into account the case
starting with 'b', don't know what that should be, but besides this,
it should do.

Bye, Martin


diff -r -u zsh-3.1.4/Src/options.c zsh-3.1.4-patched/Src/options.c
--- zsh-3.1.4/Src/options.c	Wed Apr 29 23:42:50 1998
+++ zsh-3.1.4-patched/Src/options.c	Sun Jun 14 13:20:32 1998
@@ -432,6 +432,7 @@
 void
 emulate(const char *zsh_name, int fully)
 {
+#if 0
     char ch = *zsh_name;
 
     if (ch == 'r')
@@ -442,10 +443,25 @@
 	emulation = EMULATE_CSH;
     else if (ch == 'k')
 	emulation = EMULATE_KSH;
-    else if (ch == 's' || ch == 'b')
+    else if (ch == 's' || ch == 'b')        
 	emulation = EMULATE_SH;
     else
 	emulation = EMULATE_ZSH;
+#else
+    const char *name = zsh_name;
+    
+    if (*name == 'r')
+      name++;
+
+    if (!strcmp(name,"csh"))
+      emulation = EMULATE_CSH;
+    else if (!strcmp(name,"ksh"))
+      emulation = EMULATE_KSH;
+    else if (!strcmp(name,"sh"))
+      emulation = EMULATE_SH;
+    else
+      emulation = EMULATE_ZSH;
+#endif
 
     scanhashtable(optiontab, 0, 0, 0, setemulate, fully);
 }


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Wrong emulation mode if exec'd by su
  1998-06-15 13:58 Wrong emulation mode if exec'd by su Martin Aumueller
@ 1998-06-15 14:28 ` Zefram
  1998-06-15 15:52   ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Zefram @ 1998-06-15 14:28 UTC (permalink / raw)
  To: aumuelle; +Cc: zsh-workers, aumuelle

Martin Aumueller wrote:
>I have a problem with zsh-3.1.4: If I su - to an account the zsh sees
>"su" as executable name -- starting with 's' -- and thus switches to
>Bourne shell emulation mode, not the behaviour desired by me. I have
>solved this problem by testing the full executable names against "sh",
>"csh" and "ksh" with strcmp and now it works as I expect it.

The original behaviour is correct.  "su" really should not give the shell
this weird argv[0] (some versions don't).  But on the older systems where
it does, it's more likely that the shell being invoked is supposed to
be sh, so treating a name of "su" as "sh" is the best behaviour.

To get round this problem, if you can't fix su, put an "exec zsh -l"
in your .profile.  Optionally change your login shell to /bin/sh.

>However, in the patch below I didn't take into account the case
>starting with 'b', don't know what that should be,

"bsh" == Bourne shell.

-zefram


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Wrong emulation mode if exec'd by su
  1998-06-15 14:28 ` Zefram
@ 1998-06-15 15:52   ` Bart Schaefer
  1998-06-16  6:14     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 1998-06-15 15:52 UTC (permalink / raw)
  To: Zefram, zsh-workers; +Cc: aumuelle

On Jun 15,  3:28pm, Zefram wrote:
} Subject: Re: Wrong emulation mode if exec'd by su
}
} The original behaviour is correct.  "su" really should not give the shell
} this weird argv[0] (some versions don't).  But on the older systems where
} it does, it's more likely that the shell being invoked is supposed to
} be sh, so treating a name of "su" as "sh" is the best behaviour.

For plain "su", I mostly agree.  For "su -" it's the user's login shell
that is supposed to be invoked.  (I suspect the reason for the "weird" $0
is so that it will show up in "ps" even after exec()ing the real shell.)

However, the fix in the patch is the wrong one.  Rather than looking for
every possible shell name in argv[0], the right thing would be to look
for "su" in argv[0], before testing the first letter for emulation, and
in that case do the emulation test on the value of $SHELL rather than on
argv[0].

I'm short of time this morning, but if that seems reasonable and no one
else does it first I may make a patch later today.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Wrong emulation mode if exec'd by su
  1998-06-15 15:52   ` Bart Schaefer
@ 1998-06-16  6:14     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 1998-06-16  6:14 UTC (permalink / raw)
  To: zsh-workers; +Cc: aumuelle

On Jun 15,  3:58pm, Martin Aumueller wrote:
} Subject: Wrong emulation mode if exec'd by su
}
} I have a problem with zsh-3.1.4: If I su - to an account the zsh sees
} "su" as executable name -- starting with 's' -- and thus switches to
} Bourne shell emulation mode, not the behaviour desired by me. I have
} solved this problem by testing the full executable names against "sh",
} "csh" and "ksh" with strcmp and now it works as I expect it.

On Jun 15,  8:52am, Bart Schaefer wrote:
} Subject: Re: Wrong emulation mode if exec'd by su
}
} However, the fix in the patch is the wrong one.  Rather than looking for
} every possible shell name in argv[0], the right thing would be to look
} for "su" in argv[0], before testing the first letter for emulation, and
} in that case do the emulation test on the value of $SHELL rather than on
} argv[0].

Here's the promised patch.  This avoids changing emulate() at all; rather,
it checks argv[0] against "su" in main() before deciding what to pass to
emulate(), and if it matches then it uses zgetenv("SHELL") instead.  Note
that different implementations of "su" work differently; as far as I know
they all run the target user's login shell on "su -", but some otherwise
keep the calling user's current $SHELL and change only the uid, whereas
others always run the shell of the target user.

There's some extra code in here to failsafe in case $SHELL is not set or
is empty, or in case it is set to (some path ending in) "su" (in which
case we emulate sh).  Tested by running ARGV0=su ./zsh.

Index: Src/main.c
===================================================================
--- main.c	1998/06/01 17:08:44	1.1.1.1
+++ main.c	1998/06/16 05:52:59
@@ -45,12 +45,24 @@
 
     for (t = argv; *t; *t = metafy(*t, -1, META_ALLOC), t++);
 
-    if (!(zsh_name = strrchr(argv[0], '/')))
-	zsh_name = argv[0];
-    else
-	zsh_name++;
-    if (*zsh_name == '-')
-	zsh_name++;
+    zsh_name = argv[0];
+    do {
+      char *arg0 = zsh_name;
+      if (!(zsh_name = strrchr(arg0, '/')))
+	  zsh_name = arg0;
+      else
+	  zsh_name++;
+      if (*zsh_name == '-')
+	  zsh_name++;
+      if (strcmp(zsh_name, "su") == 0) {
+	  char *sh = zgetenv("SHELL");
+	  if (sh && *sh && arg0 != sh)
+	      zsh_name = sh;
+	  else
+	      break;
+      } else
+	  break;
+    } while (zsh_name);
 
     fdtable_size = OPEN_MAX;
     fdtable = zcalloc(fdtable_size);


-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1998-06-16  6:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-06-15 13:58 Wrong emulation mode if exec'd by su Martin Aumueller
1998-06-15 14:28 ` Zefram
1998-06-15 15:52   ` Bart Schaefer
1998-06-16  6:14     ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).