Subject: bug#68074: closed (Re: bug#68074: eshell sudo/doas does not work for aliases)

From: [email-masked] (GNU bug Tracking System)
Date: Sat, 27 Jan 2024 20:26:02 +0000

Your bug report

#68074: eshell sudo/doas does not work for aliases

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
68074: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=68074
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#68074: eshell sudo/doas does not work for aliases
From: Jim Porter <[email-masked]>
To: Alfonso Sanchez-Beato <[email-masked]>, Stefan Kangas <[email-masked]>
Cc: [email-masked]
Date: Sat, 27 Jan 2024 12:24:46 -0800
List-Id: <debbugs-submit.debbugs.gnu.org>

On 1/27/2024 12:46 AM, Alfonso Sanchez-Beato via Bug reports for GNU 
Emacs, the Swiss army knife of text editors wrote:
> 
>   En sábado, 27 de enero de 2024, 01:19:32 GMT, Jim Porter <[email-masked]> escribió:
>> ... actually, this is a more-complete patch. I'm not 100% sure about
>> this part though:
>>
>>    ',(car args)
>>
>> That (usually) creates something like (quote "command"), but it's safer

I thought about this some more and the extraneous quoting is fine in my 
opinion. Eshell already does that quite a bit in 'eshell-do-eval', so 
what's one more case?

> This last patch works nicely, thanks a lot! It works also in cases where my patch was not, like:
> 
> $ eshell/sudo VAR=val <alias> ...

Wait, that works?! (After trying it out locally, so it does!)

Looking at the code, I see why now: 'eshell-named-command' calls 
'eshell-prepare-command-hook', and that hook is where we handle local 
variables. However, I truly didn't expect that; I thought the local 
variable handling occurred in an earlier phase. The more you know...

Anyway, since this works even better than I'd expected, I've now merged 
my patch to the master branch as 3c680968e49. Closing this bug now.

Subject: eshell sudo/doas does not work for aliases
From: Alfonso Sanchez-Beato <[email-masked]>
To: "[email-masked]" <[email-masked]>
Date: Wed, 27 Dec 2023 21:12:26 +0000 (UTC)
List-Id: <debbugs-submit.debbugs.gnu.org>

sudo/doas does not give the expected permissions when using an eshell alias:

When no alias has been defined:

~ $ which cp
eshell/cp is a native-compiled Lisp function in ‘em-unix.el’.
~ $ touch test
~ $ cp test /boot/efi/
Opening output file: Permission denied, /boot/efi/test
~ $ sudo cp test /boot/efi/
~ $ echo $?
0

But after defining the alias:

~ $ alias cp '*cp $*'
~ $ which cp
cp is an alias, defined as "*cp $*"
~ $ sudo cp test /boot/efi/
/usr/bin/cp: cannot stat '/boot/efi/test': Permission denied

I have attached a patch with a possible fix.
From 25e12e5912e2ef5bcab359eca5a10e973cb35937 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alfonso=20S=C3=A1nchez-Beato?=
 <[email-masked]>
Date: Wed, 27 Dec 2023 21:50:29 +0100
Subject: [PATCH] * lisp/eshell/em-alias.el (eshell-maybe-replace-by-alias):
 Fix using tramp with alias.

Previously calls that involved using tramp with an alias where not working as
expected because the default-directory set in em-tramp.el was forgotten after
eshell-replace-command was thrown in eshell-maybe-replace-by-alias.
---
 lisp/eshell/em-alias.el | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/lisp/eshell/em-alias.el b/lisp/eshell/em-alias.el
index 841982c3425..7fff920ed7b 100644
--- a/lisp/eshell/em-alias.el
+++ b/lisp/eshell/em-alias.el
@@ -222,12 +222,17 @@ This is useful after manually editing the contents of the file."
 	       (member command eshell-prevent-alias-expansion))
     (let ((alias (eshell-lookup-alias command)))
       (if alias
-	  (throw 'eshell-replace-command
-		 `(let ((eshell-command-name ',eshell-last-command-name)
+          (let ((vars `((eshell-command-name ',eshell-last-command-name)
                         (eshell-command-arguments ',eshell-last-arguments)
                         (eshell-prevent-alias-expansion
-                         ',(cons command eshell-prevent-alias-expansion)))
-                    ,(eshell-parse-command (nth 1 alias))))))))
+                         ',(cons command eshell-prevent-alias-expansion)))))
+            ;; Set default-directory as it might have been set by sudo/doas, unless
+            ;; we want to change it permanently.
+            (if (not (string-prefix-p (car (split-string (nth 1 alias) " ")) "cd"))
+                (push `(default-directory ',default-directory) vars))
+	    (throw 'eshell-replace-command
+		   `(let ,vars
+                      ,(eshell-parse-command (nth 1 alias)))))))))
 
 (defun eshell-alias-completions (name)
   "Find all possible completions for NAME.
-- 
2.34.1