Fixes to SSH/sudo handling

This commit is contained in:
Ray Miller 2025-07-06 17:06:17 +01:00
parent 3d4a83fd37
commit f83fde7ad7
Signed by: ray
GPG key ID: 043F786C4CD681B8
2 changed files with 14 additions and 11 deletions

View file

@ -23,10 +23,10 @@ this program. If not, see <https://www.gnu.org/licenses/>.
#:use-module (ssh channel)
#:use-module (ssh auth)
#:use-module (ssh popen)
#:use-module (ssh sftp)
#:use-module (ordo connection base)
#:use-module (ordo connection sudo)
#:use-module (ordo util read-lines)
#:use-module (ordo util shell-quote)
#:export (<ssh-connection>))
(define-class <ssh-connection> (<sudo-connection>)
@ -64,7 +64,8 @@ this program. If not, see <https://www.gnu.org/licenses/>.
(userauth-password! s (ssh-connection-password c))
(userauth-public-key/auto! s))))
(unless (equal? 'success user-auth)
(error (format #f "userauth: ~a" user-auth)))))))
(error (format #f "userauth: ~a" user-auth))))))
(next-method))
(define-method (remote-exec (c <ssh-connection>) (command <string>))
(let* ((channel (open-remote-input-pipe (slot-ref c 'session) command))
@ -73,18 +74,20 @@ this program. If not, see <https://www.gnu.org/licenses/>.
(close channel)
(values output exit-status)))
(define-method (sftp-session (c <ssh-connection>))
(unless (slot-bound? c 'sftp-session)
(slot-set! c 'sftp-session (make-sftp-session (session c))))
(slot-ref c 'sftp-session))
(define-method (with-remote-input-file (c <ssh-connection>) (filename <string>) (proc <procedure>))
(call-with-remote-input-file (sftp-session c) filename proc))
(let* ((channel (open-remote-input-pipe (slot-ref c 'session) (string-append "cat " (string-shell-quote filename))))
(result (proc channel)))
(close channel)
result))
(define-method (with-remote-output-file (c <ssh-connection>) (filename <string>) (proc <procedure>))
(call-with-remote-output-file (sftp-session c) filename proc))
(let* ((channel (open-remote-output-pipe (slot-ref c 'session) (string-append "cat >" (string-shell-quote filename))))
(result (proc channel)))
(close channel)
result))
(define-method (teardown (c <ssh-connection>))
(next-method)
(when (slot-bound? c 'session)
(let ((s (slot-ref c 'session)))
(when (connected? s)

View file

@ -51,10 +51,10 @@ this program. If not, see <https://www.gnu.org/licenses/>.
(next-method))
((and (become-user conn) (become-password conn))
(format #f "cat ~a - | sudo -k -S -H -u ~a -- ~a" (string-shell-quote (password-tmp-file conn)) (string-shell-quote (become-user conn)) (next-method)))
(format #f "cat ~a | sudo -k -S -H -u ~a -- ~a" (string-shell-quote (password-tmp-file conn)) (string-shell-quote (become-user conn)) (next-method)))
((become-password conn)
(format #f "cat ~a - | sudo -k -S -H -- ~a" (string-shell-quote (password-tmp-file conn)) (next-method)))
(format #f "cat ~a | sudo -k -S -H -- ~a" (string-shell-quote (password-tmp-file conn)) (next-method)))
((become-user conn)
(format #f "sudo -k -n -H -u ~a -- ~a" (string-shell-quote (become-user conn)) (next-method)))