ssh.scm 9.0 KB

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