ssh.scm 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018, 2019 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. (mkdir #$output)
  103. (chdir #$output)
  104. (test-begin "ssh-daemon")
  105. ;; Wait for sshd to be up and running.
  106. (test-assert "service running"
  107. (marionette-eval
  108. '(begin
  109. (use-modules (gnu services herd))
  110. (start-service 'ssh-daemon))
  111. marionette))
  112. ;; Check sshd's PID file.
  113. (test-equal "sshd PID"
  114. (wait-for-file #$pid-file marionette)
  115. (marionette-eval
  116. '(begin
  117. (use-modules (gnu services herd)
  118. (srfi srfi-1))
  119. (live-service-running
  120. (find (lambda (live)
  121. (memq 'ssh-daemon
  122. (live-service-provision live)))
  123. (current-services))))
  124. marionette))
  125. ;; Connect to the guest over SSH. Make sure we can run a shell
  126. ;; command there.
  127. (test-equal "shell command"
  128. 'hello
  129. (call-with-connected-session/auth
  130. (lambda (session)
  131. ;; FIXME: 'get-server-public-key' segfaults.
  132. ;; (get-server-public-key session)
  133. (let ((channel (make-channel session)))
  134. (channel-open-session channel)
  135. (channel-request-exec channel "echo hello > /root/witness")
  136. (and (zero? (channel-get-exit-status channel))
  137. (wait-for-file "/root/witness" marionette))))))
  138. ;; Check whether the 'getlogin' procedure returns the right thing.
  139. (unless #$test-getlogin?
  140. (test-skip 1))
  141. (test-equal "getlogin"
  142. '(0 "root")
  143. (call-with-connected-session/auth
  144. (lambda (session)
  145. (let* ((pipe (open-remote-input-pipe
  146. session
  147. "guile -c '(display (getlogin))'"))
  148. (output (get-string-all pipe))
  149. (status (channel-get-exit-status pipe)))
  150. (list status output)))))
  151. ;; Connect to the guest over SFTP. Make sure we can write and
  152. ;; read a file there.
  153. (unless #$sftp?
  154. (test-skip 1))
  155. (test-equal "SFTP file writing and reading"
  156. 'hello
  157. (call-with-connected-session/auth
  158. (lambda (session)
  159. (let ((sftp-session (make-sftp-session session))
  160. (witness "/root/sftp-witness"))
  161. (call-with-remote-output-file sftp-session witness
  162. (cut display "hello" <>))
  163. (call-with-remote-input-file sftp-session witness
  164. read)))))
  165. ;; Connect to the guest over SSH. Make sure we can run commands
  166. ;; from the system profile.
  167. (test-equal "run executables from system profile"
  168. #t
  169. (call-with-connected-session/auth
  170. (lambda (session)
  171. (let ((channel (make-channel session)))
  172. (channel-open-session channel)
  173. (channel-request-exec
  174. channel
  175. (string-append
  176. "mkdir -p /root/.guix-profile/bin && "
  177. "touch /root/.guix-profile/bin/path-witness && "
  178. "chmod 755 /root/.guix-profile/bin/path-witness"))
  179. (zero? (channel-get-exit-status channel))))))
  180. ;; Connect to the guest over SSH. Make sure we can run commands
  181. ;; from the user profile.
  182. (test-equal "run executable from user profile"
  183. #t
  184. (call-with-connected-session/auth
  185. (lambda (session)
  186. (let ((channel (make-channel session)))
  187. (channel-open-session channel)
  188. (channel-request-exec channel "path-witness")
  189. (zero? (channel-get-exit-status channel))))))
  190. (test-end)
  191. (exit (= (test-runner-fail-count (test-runner-current)) 0))))))
  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))))