ssh.scm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018 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 #:key (sftp? #f))
  33. "Run a test of an OS running SSH-SERVICE, which writes its PID to PID-FILE.
  34. SSH-SERVICE must be configured to listen on port 22 and to allow for root and
  35. empty-password logins.
  36. When SFTP? is true, run an SFTP server test."
  37. (define os
  38. (marionette-operating-system
  39. (simple-operating-system (service dhcp-client-service-type) ssh-service)
  40. #:imported-modules '((gnu services herd)
  41. (guix combinators))))
  42. (define vm
  43. (virtual-machine
  44. (operating-system os)
  45. (port-forwardings '((2222 . 22)))))
  46. (define test
  47. (with-imported-modules '((gnu build marionette))
  48. (with-extensions (list guile-ssh)
  49. #~(begin
  50. (use-modules (gnu build marionette)
  51. (srfi srfi-26)
  52. (srfi srfi-64)
  53. (ice-9 match)
  54. (ssh session)
  55. (ssh auth)
  56. (ssh channel)
  57. (ssh sftp))
  58. (define marionette
  59. ;; Enable TCP forwarding of the guest's port 22.
  60. (make-marionette (list #$vm)))
  61. (define (make-session-for-test)
  62. "Make a session with predefined parameters for a test."
  63. (make-session #:user "root"
  64. #:port 2222
  65. #:host "localhost"
  66. #:log-verbosity 'protocol))
  67. (define (call-with-connected-session proc)
  68. "Call the one-argument procedure PROC with a freshly created and
  69. connected SSH session object, return the result of the procedure call. The
  70. session is disconnected when the PROC is finished."
  71. (let ((session (make-session-for-test)))
  72. (dynamic-wind
  73. (lambda ()
  74. (let ((result (connect! session)))
  75. (unless (equal? result 'ok)
  76. (error "Could not connect to a server"
  77. session result))))
  78. (lambda () (proc session))
  79. (lambda () (disconnect! session)))))
  80. (define (call-with-connected-session/auth proc)
  81. "Make an authenticated session. We should be able to connect as
  82. root with an empty password."
  83. (call-with-connected-session
  84. (lambda (session)
  85. ;; Try the simple authentication methods. Dropbear requires
  86. ;; 'none' when there are no passwords, whereas OpenSSH accepts
  87. ;; 'password' with an empty password.
  88. (let loop ((methods (list (cut userauth-password! <> "")
  89. (cut userauth-none! <>))))
  90. (match methods
  91. (()
  92. (error "all the authentication methods failed"))
  93. ((auth rest ...)
  94. (match (pk 'auth (auth session))
  95. ('success
  96. (proc session))
  97. ('denied
  98. (loop rest)))))))))
  99. (mkdir #$output)
  100. (chdir #$output)
  101. (test-begin "ssh-daemon")
  102. ;; Wait for sshd to be up and running.
  103. (test-assert "service running"
  104. (marionette-eval
  105. '(begin
  106. (use-modules (gnu services herd))
  107. (start-service 'ssh-daemon))
  108. marionette))
  109. ;; Check sshd's PID file.
  110. (test-equal "sshd PID"
  111. (wait-for-file #$pid-file marionette)
  112. (marionette-eval
  113. '(begin
  114. (use-modules (gnu services herd)
  115. (srfi srfi-1))
  116. (live-service-running
  117. (find (lambda (live)
  118. (memq 'ssh-daemon
  119. (live-service-provision live)))
  120. (current-services))))
  121. marionette))
  122. ;; Connect to the guest over SSH. Make sure we can run a shell
  123. ;; command there.
  124. (test-equal "shell command"
  125. 'hello
  126. (call-with-connected-session/auth
  127. (lambda (session)
  128. ;; FIXME: 'get-server-public-key' segfaults.
  129. ;; (get-server-public-key session)
  130. (let ((channel (make-channel session)))
  131. (channel-open-session channel)
  132. (channel-request-exec channel "echo hello > /root/witness")
  133. (and (zero? (channel-get-exit-status channel))
  134. (wait-for-file "/root/witness" marionette))))))
  135. ;; Connect to the guest over SFTP. Make sure we can write and
  136. ;; read a file there.
  137. (unless #$sftp?
  138. (test-skip 1))
  139. (test-equal "SFTP file writing and reading"
  140. 'hello
  141. (call-with-connected-session/auth
  142. (lambda (session)
  143. (let ((sftp-session (make-sftp-session session))
  144. (witness "/root/sftp-witness"))
  145. (call-with-remote-output-file sftp-session witness
  146. (cut display "hello" <>))
  147. (call-with-remote-input-file sftp-session witness
  148. read)))))
  149. ;; Connect to the guest over SSH. Make sure we can run commands
  150. ;; from the system profile.
  151. (test-equal "run executables from system profile"
  152. #t
  153. (call-with-connected-session/auth
  154. (lambda (session)
  155. (let ((channel (make-channel session)))
  156. (channel-open-session channel)
  157. (channel-request-exec
  158. channel
  159. (string-append
  160. "mkdir -p /root/.guix-profile/bin && "
  161. "touch /root/.guix-profile/bin/path-witness && "
  162. "chmod 755 /root/.guix-profile/bin/path-witness"))
  163. (zero? (channel-get-exit-status channel))))))
  164. ;; Connect to the guest over SSH. Make sure we can run commands
  165. ;; from the user profile.
  166. (test-equal "run executable from user profile"
  167. #t
  168. (call-with-connected-session/auth
  169. (lambda (session)
  170. (let ((channel (make-channel session)))
  171. (channel-open-session channel)
  172. (channel-request-exec channel "path-witness")
  173. (zero? (channel-get-exit-status channel))))))
  174. (test-end)
  175. (exit (= (test-runner-fail-count (test-runner-current)) 0))))))
  176. (gexp->derivation name test))
  177. (define %test-openssh
  178. (system-test
  179. (name "openssh")
  180. (description "Connect to a running OpenSSH daemon.")
  181. (value (run-ssh-test name
  182. ;; Allow root logins with an empty password to
  183. ;; simplify testing.
  184. (service openssh-service-type
  185. (openssh-configuration
  186. (permit-root-login #t)
  187. (allow-empty-passwords? #t)))
  188. "/var/run/sshd.pid"
  189. #:sftp? #t))))
  190. (define %test-dropbear
  191. (system-test
  192. (name "dropbear")
  193. (description "Connect to a running Dropbear SSH daemon.")
  194. (value (run-ssh-test name
  195. (service dropbear-service-type
  196. (dropbear-configuration
  197. (root-login? #t)
  198. (allow-empty-passwords? #t)))
  199. "/var/run/dropbear.pid"))))