aboutsummaryrefslogtreecommitdiff
path: root/src/connection/sudo.lisp
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2021-02-26 17:15:30 -0700
committerSean Whitton <spwhitton@spwhitton.name>2021-02-27 08:33:17 -0700
commitb2e153e2ec9add0dbec673283ebd58b7d13d7456 (patch)
treebe0058157e479418ec7d2315935f85b34047a7bd /src/connection/sudo.lisp
parente79092a8b245a2064ebc3759a2b1543ba36f950f (diff)
downloadconsfigurator-b2e153e2ec9add0dbec673283ebd58b7d13d7456.tar.gz
simplify control flow by specialising on INPUT in CONNECTION-RUN
Signed-off-by: Sean Whitton <spwhitton@spwhitton.name>
Diffstat (limited to 'src/connection/sudo.lisp')
-rw-r--r--src/connection/sudo.lisp53
1 files changed, 25 insertions, 28 deletions
diff --git a/src/connection/sudo.lisp b/src/connection/sudo.lisp
index 234dac7..95d31fa 100644
--- a/src/connection/sudo.lisp
+++ b/src/connection/sudo.lisp
@@ -61,7 +61,12 @@
password)
(declare (ignore remaining))
(format t "Establishing sudo connection to ~A~%" user)
- (make-instance 'sudo-connection :user user :password password))
+ (make-instance 'sudo-connection
+ :user user
+ ;; we'll send the password followed by ^M, then the real
+ ;; stdin. use CODE-CHAR in this way so that we can be sure
+ ;; ASCII ^M is what will get emitted.
+ :password (strcat password (string (code-char 13)))))
(defclass sudo-connection (shell-wrap-connection)
((user
@@ -75,33 +80,25 @@
(format nil "sudo -HkS --prompt=\"\" --user=~A sh -c ~A"
(slot-value connection 'user) (escape-sh-token cmd)))
-(defmethod connection-run ((c sudo-connection) cmd &optional input)
- ;; send the password followed by ^M, then the real stdin. use CODE-CHAR in
- ;; this way so that we can be sure ASCII ^M is what will get emitted.
- (let* ((input-stream
- (typecase input
- (stream input)
- (string (make-string-input-stream input))))
- (password (when-let ((password (slot-value c 'password)))
- (format nil "~A~A" password (code-char 13))))
- (password-stream (and password (make-string-input-stream password)))
- (new-input (cond
- ((and password input)
- (make-concatenated-stream
- (if (subtypep (stream-element-type input-stream)
- 'character)
- password-stream
- (babel-streams:make-in-memory-input-stream
- (babel:string-to-octets password :encoding :UTF-8)
- :element-type (stream-element-type input-stream)))
- input-stream))
- (password
- password-stream)
- (input
- input-stream)
- (t
- nil))))
- (call-next-method c cmd new-input)))
+(defmethod connection-run ((c sudo-connection) cmd (input null))
+ (call-next-method c cmd (slot-value c 'password)))
+
+(defmethod connection-run ((c sudo-connection) cmd (input string))
+ (call-next-method c cmd (strcat (slot-value c 'password) input)))
+
+(defmethod connection-run ((connection sudo-connection) cmd (input stream))
+ (call-next-method connection
+ cmd
+ (if-let ((password (slot-value connection 'password)))
+ (make-concatenated-stream
+ (if (subtypep (stream-element-type input) 'character)
+ (make-string-input-stream password)
+ (babel-streams:make-in-memory-input-stream
+ (babel:string-to-octets
+ password :encoding :UTF-8)
+ :element-type (stream-element-type input)))
+ input)
+ input)))
(defmethod connection-upload ((c sudo-connection) from to)
(connection-run c #?"cp ${from} ${to}" nil))