From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Brian Cully via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#58899: [PATCH v1] Add the "doas" alias to eshell. Date: Mon, 31 Oct 2022 14:29:24 -0400 Message-ID: <87k04f3kzf.fsf@ditto.jhoto.spork.org> References: <87pme92rge.fsf@ditto.jhoto.spork.org> <286825e7-2dc2-9cc7-73a3-1056a249d56f@gmail.com> <87mt9c2d90.fsf@ditto.jhoto.spork.org> <83k04feutc.fsf@gnu.org> Reply-To: Brian Cully Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33193"; mail-complaints-to="usenet@ciao.gmane.io" Cc: jporterbugs@gmail.com, 58899@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Oct 31 19:30:14 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1opZY5-0008Vp-2O for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 31 Oct 2022 19:30:13 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1opZXx-0001LB-If; Mon, 31 Oct 2022 14:30:05 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1opZXv-0001Iz-JZ for bug-gnu-emacs@gnu.org; Mon, 31 Oct 2022 14:30:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1opZXu-00047f-Sw for bug-gnu-emacs@gnu.org; Mon, 31 Oct 2022 14:30:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1opZXu-00064y-Ne for bug-gnu-emacs@gnu.org; Mon, 31 Oct 2022 14:30:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Brian Cully Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 31 Oct 2022 18:30:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 58899 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 58899-submit@debbugs.gnu.org id=B58899.166724097223304 (code B ref 58899); Mon, 31 Oct 2022 18:30:02 +0000 Original-Received: (at 58899) by debbugs.gnu.org; 31 Oct 2022 18:29:32 +0000 Original-Received: from localhost ([127.0.0.1]:41751 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1opZXP-00063n-5o for submit@debbugs.gnu.org; Mon, 31 Oct 2022 14:29:32 -0400 Original-Received: from coleridge.kublai.com ([166.84.7.167]:59954 helo=mail.spork.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1opZXM-00063d-Pp for 58899@debbugs.gnu.org; Mon, 31 Oct 2022 14:29:29 -0400 Original-Received: from ditto (ool-18b8e9e7.dyn.optonline.net [24.184.233.231]) by mail.spork.org (Postfix) with ESMTPSA id 5A2B0B018; Mon, 31 Oct 2022 14:29:24 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=spork.org; s=dkim; t=1667240968; bh=SlUg+SOw1JatRXD6mYlc9L6XOYGsG35a1QAAdgoBZac=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=dtUdv6XlkuLFV+DM8+P1KK6g6tsfbcMmabpOfVBq9A1VOJ/Pwa0j7rP1E7uqpJQwA 2jYfI27415YI8qc1i4NOP3JiFPGg1w9xKsL8JS3sRDkeLSnYbSP7fS1Ag6d2G3gq9Y 9Cka0DLISUsO22BmOK9lfBvpUST0dWTW8kRgHQIw= In-Reply-To: <83k04feutc.fsf@gnu.org> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: "bug-gnu-emacs" Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:246713 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Eli Zaretskii writes: > Two spaces between sentences. Fixed. >> +(defun eshell/doas (&rest args) >> + "Call Tramp=E2=80=99s doas method with ARGS. > ^^^^^^^ > Please use only ASCII characters in the doc strings. > > (I'm guessing you have some electric mode enabled, so please make sure > to have it disabled when you work on our documentation.) Fixed, and yes I do. I also spotted an errant =E2=80=98q=E2=80=99 I managed to typo into the sud= o usage string, which is now removed. -bjc --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=v3-0001-Add-the-doas-alias-to-eshell.patch >From a4c060c7a540065e1fce321b7a4172c0a3da84c1 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Wed, 26 Oct 2022 21:10:21 -0400 Subject: [PATCH v3 1/2] Add the "doas" alias to eshell. * lisp/eshell/em-tramp.el (eshell/doas): new function. (eshell--method-wrap-directory): new function. (eshell/sudo): accept '-s'/'--shell' for interactive use. * test/lisp/eshell/em-tramp-tests.el (em-tramp-test/sudo-shell) (em-tramp-test/sudo-user-shell) (em-tramp-test/doas-basic) (em-tramp-test/doas-user) (em-tramp-test/doas-shell) (em-tramp-test/doas-user-shell): new tests. * etc/NEWS: mention new 'doas' eshell command. * doc/misc/eshell.texi: add 'doas' command documentation. --- doc/misc/eshell.texi | 9 ++-- etc/NEWS | 5 ++ lisp/eshell/em-tramp.el | 78 ++++++++++++++++++++---------- test/lisp/eshell/em-tramp-tests.el | 66 +++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 29 deletions(-) diff --git a/doc/misc/eshell.texi b/doc/misc/eshell.texi index ff368c9dc4..96873a3f9a 100644 --- a/doc/misc/eshell.texi +++ b/doc/misc/eshell.texi @@ -717,9 +717,12 @@ Built-ins @cmindex su @itemx sudo @cmindex sudo -Uses TRAMP's @command{su} or @command{sudo} method @pxref{Inline methods, , , tramp} -to run a command via @command{su} or @command{sudo}. These commands -are in the eshell-tramp module, which is disabled by default. +@itemx doas +@cmindex doas +Uses TRAMP's @command{su}, @command{sudo}, or @command{doas} method +@pxref{Inline methods, , , tramp} to run a command via @command{su}, +@command{sudo}, or @command{doas}. These commands are in the +eshell-tramp module, which is disabled by default. @item substitute diff --git a/etc/NEWS b/etc/NEWS index bf50c900ea..175e190d31 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -379,6 +379,11 @@ node in the Eshell manual for more details. *** Eshell pipelines now only pipe stdout by default. To pipe both stdout and stderr, use the '|&' operator instead of '|'. +*** New eshell built-in command 'doas'. +The privilege-escalation program 'doas' has been added to the existing +'su' and 'sudo' commands from the 'eshell-tramp' module. The external +command may still be accessed by using '*doas'. + --- ** The 'delete-forward-char' command now deletes by grapheme clusters. This command is by default bound to the function key diff --git a/lisp/eshell/em-tramp.el b/lisp/eshell/em-tramp.el index aebbc36e71..3daac1db3b 100644 --- a/lisp/eshell/em-tramp.el +++ b/lisp/eshell/em-tramp.el @@ -40,9 +40,10 @@ (defgroup eshell-tramp nil "This module defines commands that use TRAMP in a way that is not transparent to the user. So far, this includes only the - built-in su and sudo commands, which are not compatible with - the full, external su and sudo commands, and require the user - to understand how to use the TRAMP sudo method." + built-in su, sudo and doas commands, which are not compatible + with the full, external su, sudo, and doas commands, and + require the user to understand how to use the TRAMP sudo + method." :tag "TRAMP Eshell features" :group 'eshell-module)) @@ -52,7 +53,7 @@ eshell-tramp-initialize (add-hook 'pcomplete-try-first-hook 'eshell-complete-host-reference nil t)) (setq-local eshell-complex-commands - (append '("su" "sudo") + (append '("su" "sudo" "doas") eshell-complex-commands))) (autoload 'eshell-parse-command "esh-cmd") @@ -91,6 +92,21 @@ eshell/su (put 'eshell/su 'eshell-no-numeric-conversions t) +(defun eshell--method-wrap-directory (directory method &optional user) + "Return DIRECTORY as accessed by a Tramp METHOD for USER." + (let ((user (or user "root")) + (dir (file-local-name (expand-file-name directory))) + (prefix (file-remote-p directory)) + (host (or (file-remote-p directory 'host) + tramp-default-host)) + (rmethod (file-remote-p directory 'method)) + (ruser (file-remote-p directory 'user))) + (if (and prefix (or (not (string-equal rmethod method)) + (not (string-equal ruser user)))) + (format "%s|%s:%s@%s:%s" + (substring prefix 0 -1) method user host dir) + (format "/%s:%s@%s:%s" method user host dir)))) + (defun eshell/sudo (&rest args) "Alias \"sudo\" to call Tramp. @@ -99,34 +115,44 @@ eshell/sudo "sudo" args '((?h "help" nil nil "show this usage screen") (?u "user" t user "execute a command as another USER") + (?s "shell" nil shell "start a shell instead of executing COMMAND") :show-usage :parse-leading-options-only - :usage "[(-u | --user) USER] COMMAND + :usage "[(-u | --user) USER] (-s | --shell) | COMMAND Execute a COMMAND as the superuser or another USER.") - (throw 'eshell-external - (let* ((user (or user "root")) - (host (or (file-remote-p default-directory 'host) - tramp-default-host)) - (dir (file-local-name (expand-file-name default-directory))) - (prefix (file-remote-p default-directory)) - (default-directory - (if (and prefix - (or - (not - (string-equal - "sudo" - (file-remote-p default-directory 'method))) - (not - (string-equal - user - (file-remote-p default-directory 'user))))) - (format "%s|sudo:%s@%s:%s" - (substring prefix 0 -1) user host dir) - (format "/sudo:%s@%s:%s" user host dir)))) - (eshell-named-command (car args) (cdr args)))))) + (let ((dir (eshell--method-wrap-directory default-directory "sudo" user))) + (if shell + (throw 'eshell-replace-command + (eshell-parse-command "cd" (list dir))) + (throw 'eshell-external + (let ((default-directory dir)) + (eshell-named-command (car args) (cdr args)))))))) (put 'eshell/sudo 'eshell-no-numeric-conversions t) +(defun eshell/doas (&rest args) + "Call Tramp's doas method with ARGS. + +Uses the system doas through Tramp's doas method." + (eshell-eval-using-options + "doas" args + '((?h "help" nil nil "show this usage screen") + (?u "user" t user "execute a command as another USER") + (?s "shell" nil shell "start a shell instead of executing COMMAND") + :show-usage + :parse-leading-options-only + :usage "[(-u | --user) USER] (-s | --shell) | COMMAND +Execute a COMMAND as the superuser or another USER.") + (let ((dir (eshell--method-wrap-directory default-directory "doas" user))) + (if shell + (throw 'eshell-replace-command + (eshell-parse-command "cd" (list dir))) + (throw 'eshell-external + (let ((default-directory dir)) + (eshell-named-command (car args) (cdr args)))))))) + +(put 'eshell/doas 'eshell-no-numeric-conversions t) + (provide 'em-tramp) ;; Local Variables: diff --git a/test/lisp/eshell/em-tramp-tests.el b/test/lisp/eshell/em-tramp-tests.el index 8969c1e229..9d28d6be82 100644 --- a/test/lisp/eshell/em-tramp-tests.el +++ b/test/lisp/eshell/em-tramp-tests.el @@ -85,4 +85,70 @@ em-tramp-test/sudo-user `(,(format "/sudo:USER@%s:%s" tramp-default-host default-directory) ("echo" ("-u" "hi"))))))) +(ert-deftest em-tramp-test/sudo-shell () + "Test Eshell `sudo' command with -s/--shell option." + (dolist (args '(("--shell") + ("-s"))) + (should (equal + (catch 'eshell-replace-command (apply #'eshell/sudo args)) + `(eshell-trap-errors + (eshell-named-command + "cd" + (list ,(format "/sudo:root@%s:%s" tramp-default-host default-directory)))))))) + +(ert-deftest em-tramp-test/sudo-user-shell () + "Test Eshell `sudo' command with -s and -u options." + (should (equal + (catch 'eshell-replace-command (eshell/sudo "-u" "USER" "-s")) + `(eshell-trap-errors + (eshell-named-command + "cd" + (list ,(format "/sudo:USER@%s:%s" tramp-default-host default-directory))))))) + +(ert-deftest em-tramp-test/doas-basic () + "Test Eshell `doas' command with default user." + (cl-letf (((symbol-function 'eshell-named-command) + #'mock-eshell-named-command)) + (should (equal + (catch 'eshell-external (eshell/doas "echo" "hi")) + `(,(format "/doas:root@%s:%s" tramp-default-host default-directory) + ("echo" ("hi"))))) + (should (equal + (catch 'eshell-external (eshell/doas "echo" "-u" "hi")) + `(,(format "/doas:root@%s:%s" tramp-default-host default-directory) + ("echo" ("-u" "hi"))))))) + +(ert-deftest em-tramp-test/doas-user () + "Test Eshell `doas' command with specified user." + (cl-letf (((symbol-function 'eshell-named-command) + #'mock-eshell-named-command)) + (should (equal + (catch 'eshell-external (eshell/doas "-u" "USER" "echo" "hi")) + `(,(format "/doas:USER@%s:%s" tramp-default-host default-directory) + ("echo" ("hi"))))) + (should (equal + (catch 'eshell-external (eshell/doas "-u" "USER" "echo" "-u" "hi")) + `(,(format "/doas:USER@%s:%s" tramp-default-host default-directory) + ("echo" ("-u" "hi"))))))) + +(ert-deftest em-tramp-test/doas-shell () + "Test Eshell `doas' command with -s/--shell option." + (dolist (args '(("--shell") + ("-s"))) + (should (equal + (catch 'eshell-replace-command (apply #'eshell/doas args)) + `(eshell-trap-errors + (eshell-named-command + "cd" + (list ,(format "/doas:root@%s:%s" tramp-default-host default-directory)))))))) + +(ert-deftest em-tramp-test/doas-user-shell () + "Test Eshell `doas' command with -s and -u options." + (should (equal + (catch 'eshell-replace-command (eshell/doas "-u" "USER" "-s")) + `(eshell-trap-errors + (eshell-named-command + "cd" + (list ,(format "/doas:USER@%s:%s" tramp-default-host default-directory))))))) + ;;; em-tramp-tests.el ends here -- 2.38.0 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=v3-0002-lisp-eshell-em-tramp.el-Rename-TRAMP-to-Tramp.patch >From 4703a99e7b56d64686c3bd374657e2dbf7853e5d Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Mon, 31 Oct 2022 11:55:45 -0400 Subject: [PATCH v3 2/2] lisp/eshell/em-tramp.el: Rename 'TRAMP' to 'Tramp'. --- lisp/eshell/em-tramp.el | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lisp/eshell/em-tramp.el b/lisp/eshell/em-tramp.el index 3daac1db3b..499deaa7fc 100644 --- a/lisp/eshell/em-tramp.el +++ b/lisp/eshell/em-tramp.el @@ -1,4 +1,4 @@ -;;; em-tramp.el --- Eshell features that require TRAMP -*- lexical-binding:t -*- +;;; em-tramp.el --- Eshell features that require Tramp -*- lexical-binding:t -*- ;; Copyright (C) 1999-2022 Free Software Foundation, Inc. @@ -21,7 +21,7 @@ ;;; Commentary: -;; Eshell features that require TRAMP. +;; Eshell features that require Tramp. ;;; Code: @@ -38,17 +38,17 @@ ;;;###autoload (progn (defgroup eshell-tramp nil - "This module defines commands that use TRAMP in a way that is + "This module defines commands that use Tramp in a way that is not transparent to the user. So far, this includes only the built-in su, sudo and doas commands, which are not compatible with the full, external su, sudo, and doas commands, and - require the user to understand how to use the TRAMP sudo + require the user to understand how to use the Tramp sudo method." - :tag "TRAMP Eshell features" + :tag "Tramp Eshell features" :group 'eshell-module)) (defun eshell-tramp-initialize () ;Called from `eshell-mode' via intern-soft! - "Initialize the TRAMP-using commands code." + "Initialize the Tramp-using commands code." (when (eshell-using-module 'eshell-cmpl) (add-hook 'pcomplete-try-first-hook 'eshell-complete-host-reference nil t)) @@ -59,9 +59,9 @@ eshell-tramp-initialize (autoload 'eshell-parse-command "esh-cmd") (defun eshell/su (&rest args) - "Alias \"su\" to call TRAMP. + "Alias \"su\" to call Tramp. -Uses the system su through TRAMP's su method." +Uses the system su through Tramp's su method." (eshell-eval-using-options "su" args '((?h "help" nil nil "show this usage screen") @@ -110,7 +110,7 @@ eshell--method-wrap-directory (defun eshell/sudo (&rest args) "Alias \"sudo\" to call Tramp. -Uses the system sudo through TRAMP's sudo method." +Uses the system sudo through Tramp's sudo method." (eshell-eval-using-options "sudo" args '((?h "help" nil nil "show this usage screen") -- 2.38.0 --=-=-=--