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>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
21 (define-module (gnu tests ssh)
22 #:use-module (gnu tests)
23 #:use-module (gnu system)
24 #:use-module (gnu system vm)
25 #:use-module (gnu services)
26 #:use-module (gnu services ssh)
27 #:use-module (gnu services networking)
28 #:use-module (gnu packages ssh)
29 #:use-module (guix gexp)
30 #:use-module (guix store)
31 #:export (%test-openssh
34 (define* (run-ssh-test name ssh-service pid-file #:key (sftp? #f))
35 "Run a test of an OS running SSH-SERVICE, which writes its PID to PID-FILE.
36 SSH-SERVICE must be configured to listen on port 22 and to allow for root and
37 empty-password logins.
39 When SFTP? is true, run an SFTP server test."
41 (marionette-operating-system
42 (simple-operating-system (dhcp-client-service) ssh-service)
43 #:imported-modules '((gnu services herd)
48 (port-forwardings '((2222 . 22)))))
51 (with-imported-modules '((gnu build marionette))
52 (with-extensions (list guile-ssh)
54 (use-modules (gnu build marionette)
64 ;; Enable TCP forwarding of the guest's port 22.
65 (make-marionette (list #$vm)))
67 (define (make-session-for-test)
68 "Make a session with predefined parameters for a test."
69 (make-session #:user "root"
72 #:log-verbosity 'protocol))
74 (define (call-with-connected-session proc)
75 "Call the one-argument procedure PROC with a freshly created and
76 connected SSH session object, return the result of the procedure call. The
77 session is disconnected when the PROC is finished."
78 (let ((session (make-session-for-test)))
81 (let ((result (connect! session)))
82 (unless (equal? result 'ok)
83 (error "Could not connect to a server"
85 (lambda () (proc session))
86 (lambda () (disconnect! session)))))
88 (define (call-with-connected-session/auth proc)
89 "Make an authenticated session. We should be able to connect as
90 root with an empty password."
91 (call-with-connected-session
93 ;; Try the simple authentication methods. Dropbear requires
94 ;; 'none' when there are no passwords, whereas OpenSSH accepts
95 ;; 'password' with an empty password.
96 (let loop ((methods (list (cut userauth-password! <> "")
97 (cut userauth-none! <>))))
100 (error "all the authentication methods failed"))
102 (match (pk 'auth (auth session))
111 (test-begin "ssh-daemon")
113 ;; Wait for sshd to be up and running.
114 (test-assert "service running"
117 (use-modules (gnu services herd))
118 (start-service 'ssh-daemon))
121 ;; Check sshd's PID file.
122 (test-equal "sshd PID"
123 (wait-for-file #$pid-file marionette)
126 (use-modules (gnu services herd)
129 (live-service-running
132 (live-service-provision live)))
133 (current-services))))
136 ;; Connect to the guest over SSH. Make sure we can run a shell
138 (test-equal "shell command"
140 (call-with-connected-session/auth
142 ;; FIXME: 'get-server-public-key' segfaults.
143 ;; (get-server-public-key session)
144 (let ((channel (make-channel session)))
145 (channel-open-session channel)
146 (channel-request-exec channel "echo hello > /root/witness")
147 (and (zero? (channel-get-exit-status channel))
148 (wait-for-file "/root/witness" marionette))))))
150 ;; Connect to the guest over SFTP. Make sure we can write and
151 ;; read a file there.
154 (test-equal "SFTP file writing and reading"
156 (call-with-connected-session/auth
158 (let ((sftp-session (make-sftp-session session))
159 (witness "/root/sftp-witness"))
160 (call-with-remote-output-file sftp-session witness
161 (cut display "hello" <>))
162 (call-with-remote-input-file sftp-session witness
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"
169 (call-with-connected-session/auth
171 (let ((channel (make-channel session)))
172 (channel-open-session channel)
173 (channel-request-exec
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))))))
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"
185 (call-with-connected-session/auth
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))))))
193 (exit (= (test-runner-fail-count (test-runner-current)) 0))))))
195 (gexp->derivation name test))
197 (define %test-openssh
200 (description "Connect to a running OpenSSH daemon.")
201 (value (run-ssh-test name
202 ;; Allow root logins with an empty password to
204 (service openssh-service-type
205 (openssh-configuration
206 (permit-root-login #t)
207 (allow-empty-passwords? #t)))
211 (define %test-dropbear
214 (description "Connect to a running Dropbear SSH daemon.")
215 (value (run-ssh-test name
216 (service dropbear-service-type
217 (dropbear-configuration
219 (allow-empty-passwords? #t)))
220 "/var/run/dropbear.pid"))))