ssh.scm 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
  4. ;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (gnu tests ssh)
  21. #:use-module (gnu tests)
  22. #:use-module (gnu system)
  23. #:use-module (gnu system vm)
  24. #:use-module (gnu services)
  25. #:use-module (gnu services ssh)
  26. #:use-module (gnu services networking)
  27. #:use-module (gnu packages ssh)
  28. #:use-module (guix gexp)
  29. #:use-module (guix store)
  30. #:export (%test-openssh
  31. %test-dropbear))
  32. (define* (run-ssh-test name ssh-service pid-file
  33. #:key (sftp? #f) (test-getlogin? #t))
  34. "Run a test of an OS running SSH-SERVICE, which writes its PID to PID-FILE.
  35. SSH-SERVICE must be configured to listen on port 22 and to allow for root and
  36. empty-password logins.
  37. When SFTP? is true, run an SFTP server test."
  38. (define os
  39. (marionette-operating-system
  40. (simple-operating-system (service dhcp-client-service-type) ssh-service)
  41. #:imported-modules '((gnu services herd)
  42. (guix combinators))))
  43. (define vm
  44. (virtual-machine
  45. (operating-system os)
  46. (port-forwardings '((2222 . 22)))))
  47. (define test
  48. (with-imported-modules '((gnu build marionette))
  49. (with-extensions (list guile-ssh)
  50. #~(begin
  51. (use-modules (gnu build marionette)
  52. (srfi srfi-26)
  53. (srfi srfi-64)
  54. (ice-9 textual-ports)
  55. (ice-9 match)
  56. (ssh session)
  57. (ssh auth)
  58. (ssh channel)
  59. (ssh popen)
  60. (ssh sftp))
  61. (define marionette
  62. ;; Enable TCP forwarding of the guest's port 22.
  63. (make-marionette (list #$vm)))
  64. (define (make-session-for-test)
  65. "Make a session with predefined parameters for a test."
  66. (make-session #:user "root"
  67. #:port 2222
  68. #:host "localhost"
  69. #:log-verbosity 'protocol))
  70. (define (call-with-connected-session proc)
  71. "Call the one-argument procedure PROC with a freshly created and
  72. connected SSH session object, return the result of the procedure call. The
  73. session is disconnected when the PROC is finished."
  74. (let ((session (make-session-for-test)))
  75. (dynamic-wind
  76. (lambda ()
  77. (let ((result (connect! session)))
  78. (unless (equal? result 'ok)
  79. (error "Could not connect to a server"
  80. session result))))
  81. (lambda () (proc session))
  82. (lambda () (disconnect! session)))))
  83. (define (call-with-connected-session/auth proc)
  84. "Make an authenticated session. We should be able to connect as
  85. root with an empty password."
  86. (call-with-connected-session
  87. (lambda (session)
  88. ;; Try the simple authentication methods. Dropbear requires
  89. ;; 'none' when there are no passwords, whereas OpenSSH accepts
  90. ;; 'password' with an empty password.
  91. (let loop ((methods (list (cut userauth-password! <> "")
  92. (cut userauth-none! <>))))
  93. (match methods
  94. (()
  95. (error "all the authentication methods failed"))
  96. ((auth rest ...)
  97. (match (pk 'auth (auth session))
  98. ('success
  99. (proc session))
  100. ('denied
  101. (loop rest)))))))))
  102. (test-runner-current (system-test-runner #$output))
  103. (test-begin "ssh-daemon")
  104. ;; Wait for sshd to be up and running.
  105. (test-assert "service running"
  106. (marionette-eval
  107. '(begin
  108. (use-modules (gnu services herd))
  109. (start-service 'ssh-daemon))
  110. marionette))
  111. ;; Check sshd's PID file.
  112. (test-equal "sshd PID"
  113. (wait-for-file #$pid-file marionette)
  114. (marionette-eval
  115. '(begin
  116. (use-modules (gnu services herd)
  117. (srfi srfi-1))
  118. (live-service-running
  119. (find (lambda (live)
  120. (memq 'ssh-daemon
  121. (live-service-provision live)))
  122. (current-services))))
  123. marionette))
  124. (test-assert "wait for port 22"
  125. (wait-for-tcp-port 22 marionette))
  126. ;; Connect to the guest over SSH. Make sure we can run a shell
  127. ;; command there.
  128. (test-equal "shell command"
  129. 'hello
  130. (call-with-connected-session/auth
  131. (lambda (session)
  132. ;; FIXME: 'get-server-public-key' segfaults.
  133. ;; (get-server-public-key session)
  134. (let ((channel (make-channel session)))
  135. (channel-open-session channel)
  136. (channel-request-exec channel "echo hello > /root/witness")
  137. (and (zero? (channel-get-exit-status channel))
  138. (wait-for-file "/root/witness" marionette))))))
  139. ;; Check whether the 'getlogin' procedure returns the right thing.
  140. (unless #$test-getlogin?
  141. (test-skip 1))
  142. (test-equal "getlogin"
  143. '(0 "root")
  144. (call-with-connected-session/auth
  145. (lambda (session)
  146. (let* ((pipe (open-remote-input-pipe
  147. session
  148. "guile -c '(display (getlogin))'"))
  149. (output (get-string-all pipe))
  150. (status (channel-get-exit-status pipe)))
  151. (list status output)))))
  152. ;; Connect to the guest over SFTP. Make sure we can write and
  153. ;; read a file there.
  154. (unless #$sftp?
  155. (test-skip 1))
  156. (test-equal "SFTP file writing and reading"
  157. 'hello
  158. (call-with-connected-session/auth
  159. (lambda (session)
  160. (let ((sftp-session (make-sftp-session session))
  161. (witness "/root/sftp-witness"))
  162. (call-with-remote-output-file sftp-session witness
  163. (cut display "hello" <>))
  164. (call-with-remote-input-file sftp-session witness
  165. read)))))
  166. ;; Connect to the guest over SSH. Make sure we can run commands
  167. ;; from the system profile.
  168. (test-equal "run executables from system profile"
  169. #t
  170. (call-with-connected-session/auth
  171. (lambda (session)
  172. (let ((channel (make-channel session)))
  173. (channel-open-session channel)
  174. (channel-request-exec
  175. channel
  176. (string-append
  177. "mkdir -p /root/.guix-profile/bin && "
  178. "touch /root/.guix-profile/bin/path-witness && "
  179. "chmod 755 /root/.guix-profile/bin/path-witness"))
  180. (zero? (channel-get-exit-status channel))))))
  181. ;; Connect to the guest over SSH. Make sure we can run commands
  182. ;; from the user profile.
  183. (test-equal "run executable from user profile"
  184. #t
  185. (call-with-connected-session/auth
  186. (lambda (session)
  187. (let ((channel (make-channel session)))
  188. (channel-open-session channel)
  189. (channel-request-exec channel "path-witness")
  190. (zero? (channel-get-exit-status channel))))))
  191. (test-end)))))
  192. (gexp->derivation name test))
  193. (define %test-openssh
  194. (system-test
  195. (name "openssh")
  196. (description "Connect to a running OpenSSH daemon.")
  197. (value (run-ssh-test name
  198. ;; Allow root logins with an empty password to
  199. ;; simplify testing.
  200. (service openssh-service-type
  201. (openssh-configuration
  202. (permit-root-login #t)
  203. (allow-empty-passwords? #t)))
  204. "/var/run/sshd.pid"
  205. #:sftp? #t))))
  206. (define %test-dropbear
  207. (system-test
  208. (name "dropbear")
  209. (description "Connect to a running Dropbear SSH daemon.")
  210. (value (run-ssh-test name
  211. (service dropbear-service-type
  212. (dropbear-configuration
  213. (root-login? #t)
  214. (allow-empty-passwords? #t)))
  215. "/var/run/dropbear.pid"
  216. ;; XXX: Our Dropbear is not built with PAM support.
  217. ;; Even when it is, it seems to ignore the PAM
  218. ;; 'session' requirements.
  219. #:test-getlogin? #f))))