Merge branch 'master' into staging
[jackhill/guix/guix.git] / gnu / tests / ssh.scm
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
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
32 %test-dropbear))
33
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.
38
39 When SFTP? is true, run an SFTP server test."
40 (define os
41 (marionette-operating-system
42 (simple-operating-system (dhcp-client-service) ssh-service)
43 #:imported-modules '((gnu services herd)
44 (guix combinators))))
45 (define vm
46 (virtual-machine
47 (operating-system os)
48 (port-forwardings '((2222 . 22)))))
49
50 (define test
51 (with-imported-modules '((gnu build marionette))
52 (with-extensions (list guile-ssh)
53 #~(begin
54 (use-modules (gnu build marionette)
55 (srfi srfi-26)
56 (srfi srfi-64)
57 (ice-9 match)
58 (ssh session)
59 (ssh auth)
60 (ssh channel)
61 (ssh sftp))
62
63 (define marionette
64 ;; Enable TCP forwarding of the guest's port 22.
65 (make-marionette (list #$vm)))
66
67 (define (make-session-for-test)
68 "Make a session with predefined parameters for a test."
69 (make-session #:user "root"
70 #:port 2222
71 #:host "localhost"
72 #:log-verbosity 'protocol))
73
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)))
79 (dynamic-wind
80 (lambda ()
81 (let ((result (connect! session)))
82 (unless (equal? result 'ok)
83 (error "Could not connect to a server"
84 session result))))
85 (lambda () (proc session))
86 (lambda () (disconnect! session)))))
87
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
92 (lambda (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! <>))))
98 (match methods
99 (()
100 (error "all the authentication methods failed"))
101 ((auth rest ...)
102 (match (pk 'auth (auth session))
103 ('success
104 (proc session))
105 ('denied
106 (loop rest)))))))))
107
108 (mkdir #$output)
109 (chdir #$output)
110
111 (test-begin "ssh-daemon")
112
113 ;; Wait for sshd to be up and running.
114 (test-assert "service running"
115 (marionette-eval
116 '(begin
117 (use-modules (gnu services herd))
118 (start-service 'ssh-daemon))
119 marionette))
120
121 ;; Check sshd's PID file.
122 (test-equal "sshd PID"
123 (wait-for-file #$pid-file marionette)
124 (marionette-eval
125 '(begin
126 (use-modules (gnu services herd)
127 (srfi srfi-1))
128
129 (live-service-running
130 (find (lambda (live)
131 (memq 'ssh-daemon
132 (live-service-provision live)))
133 (current-services))))
134 marionette))
135
136 ;; Connect to the guest over SSH. Make sure we can run a shell
137 ;; command there.
138 (test-equal "shell command"
139 'hello
140 (call-with-connected-session/auth
141 (lambda (session)
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))))))
149
150 ;; Connect to the guest over SFTP. Make sure we can write and
151 ;; read a file there.
152 (unless #$sftp?
153 (test-skip 1))
154 (test-equal "SFTP file writing and reading"
155 'hello
156 (call-with-connected-session/auth
157 (lambda (session)
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
163 read)))))
164
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
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
192 (test-end)
193 (exit (= (test-runner-fail-count (test-runner-current)) 0))))))
194
195 (gexp->derivation name test))
196
197 (define %test-openssh
198 (system-test
199 (name "openssh")
200 (description "Connect to a running OpenSSH daemon.")
201 (value (run-ssh-test name
202 ;; Allow root logins with an empty password to
203 ;; simplify testing.
204 (service openssh-service-type
205 (openssh-configuration
206 (permit-root-login #t)
207 (allow-empty-passwords? #t)))
208 "/var/run/sshd.pid"
209 #:sftp? #t))))
210
211 (define %test-dropbear
212 (system-test
213 (name "dropbear")
214 (description "Connect to a running Dropbear SSH daemon.")
215 (value (run-ssh-test name
216 (service dropbear-service-type
217 (dropbear-configuration
218 (root-login? #t)
219 (allow-empty-passwords? #t)))
220 "/var/run/dropbear.pid"))))