1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 David Craven <david@craven.ch>
4 ;;; Copyright © 2016 Julien Lepiller <julien@lepiller.eu>
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 services ssh)
22 #:use-module (gnu packages ssh)
23 #:use-module (gnu packages admin)
24 #:use-module (gnu services)
25 #:use-module (gnu services shepherd)
26 #:use-module (gnu system pam)
27 #:use-module (gnu system shadow)
28 #:use-module (guix gexp)
29 #:use-module (guix records)
30 #:use-module (srfi srfi-26)
31 #:use-module (ice-9 match)
32 #:export (lsh-configuration
38 openssh-configuration?
42 dropbear-configuration
43 dropbear-configuration?
49 ;;; This module implements secure shell (SSH) services.
54 (define-record-type* <lsh-configuration>
55 lsh-configuration make-lsh-configuration
57 (lsh lsh-configuration-lsh
59 (daemonic? lsh-configuration-daemonic?)
60 (host-key lsh-configuration-host-key)
61 (interfaces lsh-configuration-interfaces)
62 (port-number lsh-configuration-port-number)
63 (allow-empty-passwords? lsh-configuration-allow-empty-passwords?)
64 (root-login? lsh-configuration-root-login?)
65 (syslog-output? lsh-configuration-syslog-output?)
66 (pid-file? lsh-configuration-pid-file?)
67 (pid-file lsh-configuration-pid-file)
68 (x11-forwarding? lsh-configuration-x11-forwarding?)
69 (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?)
70 (password-authentication? lsh-configuration-password-authentication?)
71 (public-key-authentication? lsh-configuration-public-key-authentication?)
72 (initialize? lsh-configuration-initialize?))
75 "/var/spool/lsh/yarrow-seed-file")
77 (define (lsh-initialization lsh host-key)
78 "Return the gexp to initialize the LSH service for HOST-KEY."
80 (unless (file-exists? #$%yarrow-seed)
81 (system* (string-append #$lsh "/bin/lsh-make-seed")
82 "--sloppy" "-o" #$%yarrow-seed))
84 (unless (file-exists? #$host-key)
85 (mkdir-p (dirname #$host-key))
86 (format #t "creating SSH host key '~a'...~%" #$host-key)
88 ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
89 ;; used yet because /bin/sh might be dangling; factorize this somehow.
90 (let* ((in+out (pipe))
91 (keygen (primitive-fork)))
94 (close-port (car in+out))
96 (dup2 (fileno (cdr in+out)) 1)
97 (execl (string-append #$lsh "/bin/lsh-keygen")
98 "lsh-keygen" "--server"))
100 (let ((write-key (primitive-fork)))
103 (close-port (cdr in+out))
105 (dup2 (fileno (car in+out)) 0)
106 (execl (string-append #$lsh "/bin/lsh-writekey")
107 "lsh-writekey" "--server" "-o" #$host-key))
109 (close-port (car in+out))
110 (close-port (cdr in+out))
112 (waitpid write-key))))))))))
114 (define (lsh-activation config)
115 "Return the activation gexp for CONFIG."
117 (use-modules (guix build utils))
118 (mkdir-p "/var/spool/lsh")
119 #$(if (lsh-configuration-initialize? config)
120 (lsh-initialization (lsh-configuration-lsh config)
121 (lsh-configuration-host-key config))
124 (define (lsh-shepherd-service config)
125 "Return a <shepherd-service> for lsh with CONFIG."
126 (define lsh (lsh-configuration-lsh config))
127 (define pid-file (lsh-configuration-pid-file config))
128 (define pid-file? (lsh-configuration-pid-file? config))
129 (define daemonic? (lsh-configuration-daemonic? config))
130 (define interfaces (lsh-configuration-interfaces config))
134 (cons (file-append lsh "/sbin/lshd")
136 (let ((syslog (if (lsh-configuration-syslog-output? config)
138 (list "--no-syslog"))))
141 (cons #~(string-append "--pid-file=" #$pid-file)
143 (cons "--no-pid-file" syslog))))
145 (list #~(string-append "--pid-file=" #$pid-file))
147 (cons* #~(string-append "--host-key="
148 #$(lsh-configuration-host-key config))
149 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
150 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
151 "-p" (number->string (lsh-configuration-port-number config))
152 (if (lsh-configuration-password-authentication? config)
153 "--password" "--no-password")
154 (if (lsh-configuration-public-key-authentication? config)
155 "--publickey" "--no-publickey")
156 (if (lsh-configuration-root-login? config)
157 "--root-login" "--no-root-login")
158 (if (lsh-configuration-x11-forwarding? config)
159 "--x11-forward" "--no-x11-forward")
160 (if (lsh-configuration-tcp/ip-forwarding? config)
161 "--tcpip-forward" "--no-tcpip-forward")
162 (if (null? interfaces)
164 (map (cut string-append "--interface=" <>)
168 (if (and daemonic? (lsh-configuration-syslog-output? config))
169 '(networking syslogd)
172 (list (shepherd-service
173 (documentation "GNU lsh SSH server")
174 (provision '(ssh-daemon))
175 (requirement requires)
176 (start #~(make-forkexec-constructor (list #$@lsh-command)))
177 (stop #~(make-kill-destructor)))))
179 (define (lsh-pam-services config)
180 "Return a list of <pam-services> for lshd with CONFIG."
181 (list (unix-pam-service
183 #:allow-empty-passwords?
184 (lsh-configuration-allow-empty-passwords? config))))
186 (define lsh-service-type
187 (service-type (name 'lsh)
189 (list (service-extension shepherd-root-service-type
190 lsh-shepherd-service)
191 (service-extension pam-root-service-type
193 (service-extension activation-service-type
196 (define* (lsh-service #:key
199 (host-key "/etc/lsh/host-key")
202 (allow-empty-passwords? #f)
206 (pid-file "/var/run/lshd.pid")
208 (tcp/ip-forwarding? #t)
209 (password-authentication? #t)
210 (public-key-authentication? #t)
212 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
213 @var{host-key} must designate a file containing the host key, and readable
216 When @var{daemonic?} is true, @command{lshd} will detach from the
217 controlling terminal and log its output to syslogd, unless one sets
218 @var{syslog-output?} to false. Obviously, it also makes lsh-service
219 depend on existence of syslogd service. When @var{pid-file?} is true,
220 @command{lshd} writes its PID to the file called @var{pid-file}.
222 When @var{initialize?} is true, automatically create the seed and host key
223 upon service activation if they do not exist yet. This may take long and
226 When @var{initialize?} is false, it is up to the user to initialize the
227 randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
228 a key pair with the private key stored in file @var{host-key} (@pxref{lshd
229 basics,,, lsh, LSH Manual}).
231 When @var{interfaces} is empty, lshd listens for connections on all the
232 network interfaces; otherwise, @var{interfaces} must be a list of host names
235 @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
236 passwords, and @var{root-login?} specifies whether to accept log-ins as
239 The other options should be self-descriptive."
240 (service lsh-service-type
241 (lsh-configuration (lsh lsh) (daemonic? daemonic?)
242 (host-key host-key) (interfaces interfaces)
243 (port-number port-number)
244 (allow-empty-passwords? allow-empty-passwords?)
245 (root-login? root-login?)
246 (syslog-output? syslog-output?)
247 (pid-file? pid-file?) (pid-file pid-file)
248 (x11-forwarding? x11-forwarding?)
249 (tcp/ip-forwarding? tcp/ip-forwarding?)
250 (password-authentication?
251 password-authentication?)
252 (public-key-authentication?
253 public-key-authentication?)
254 (initialize? initialize?))))
261 (define-record-type* <openssh-configuration>
262 openssh-configuration make-openssh-configuration
263 openssh-configuration?
264 (pid-file openssh-configuration-pid-file) ;string
265 (port-number openssh-configuration-port-number) ;integer
266 (permit-root-login openssh-configuration-permit-root-login) ;Boolean | 'without-password
267 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?) ;Boolean
268 (password-authentication? openssh-configuration-password-authentication?) ;Boolean
269 (pubkey-authentication? openssh-configuration-pubkey-authentication?) ;Boolean
270 (rsa-authentication? openssh-configuration-rsa-authentication?) ;Boolean
271 (x11-forwarding? openssh-configuration-x11-forwarding?) ;Boolean
272 (protocol-number openssh-configuration-protocol-number)) ;integer
274 (define %openssh-accounts
275 (list (user-group (name "sshd") (system? #t))
280 (comment "sshd privilege separation user")
281 (home-directory "/var/run/sshd")
282 (shell #~(string-append #$shadow "/sbin/nologin")))))
284 (define (openssh-activation config)
285 "Return the activation GEXP for CONFIG."
288 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
290 ;; Generate missing host keys.
291 (system* (string-append #$openssh "/bin/ssh-keygen") "-A")))
293 (define (openssh-config-file config)
294 "Return the sshd configuration file corresponding to CONFIG."
297 #~(call-with-output-file #$output
299 (display "# Generated by 'openssh-service'.\n" port)
300 (format port "Protocol ~a\n"
301 #$(if (eq? (openssh-configuration-protocol-number config) 1)
303 (format port "Port ~a\n"
304 #$(number->string (openssh-configuration-port-number config)))
305 (format port "PermitRootLogin ~a\n"
306 #$(match (openssh-configuration-permit-root-login config)
309 ('without-password "without-password")))
310 (format port "PermitEmptyPasswords ~a\n"
311 #$(if (openssh-configuration-allow-empty-passwords? config)
313 (format port "PasswordAuthentication ~a\n"
314 #$(if (openssh-configuration-password-authentication? config)
316 (format port "PubkeyAuthentication ~a\n"
317 #$(if (openssh-configuration-pubkey-authentication? config)
319 (format port "RSAAuthentication ~a\n"
320 #$(if (openssh-configuration-rsa-authentication? config)
322 (format port "X11Forwarding ~a\n"
323 #$(if (openssh-configuration-x11-forwarding? config)
325 (format port "PidFile ~a\n"
326 #$(openssh-configuration-pid-file config))
329 (define (openssh-shepherd-service config)
330 "Return a <shepherd-service> for openssh with CONFIG."
333 (openssh-configuration-pid-file config))
335 (define openssh-command
336 #~(list (string-append #$openssh "/sbin/sshd")
337 "-D" "-f" #$(openssh-config-file config)))
339 (list (shepherd-service
340 (documentation "OpenSSH server.")
341 (requirement '(networking syslogd))
342 (provision '(ssh-daemon))
343 (start #~(make-forkexec-constructor #$openssh-command
344 #:pid-file #$pid-file))
345 (stop #~(make-kill-destructor)))))
347 (define openssh-service-type
348 (service-type (name 'openssh)
350 (list (service-extension shepherd-root-service-type
351 openssh-shepherd-service)
352 (service-extension activation-service-type
354 (service-extension account-service-type
355 (const %openssh-accounts))))))
357 (define* (openssh-service #:key
358 (pid-file "/var/run/sshd.pid")
360 (permit-root-login 'without-password)
361 (allow-empty-passwords? #f)
362 (password-authentication? #t)
363 (pubkey-authentication? #t)
364 (rsa-authentication? #t)
367 (service openssh-service-type (openssh-configuration
369 (port-number port-number)
370 (permit-root-login permit-root-login)
371 (allow-empty-passwords? allow-empty-passwords?)
372 (password-authentication? password-authentication?)
373 (pubkey-authentication? pubkey-authentication?)
374 (rsa-authentication? rsa-authentication?)
375 (x11-forwarding? x11-forwarding?)
376 (protocol-number protocol-number))))
383 (define-record-type* <dropbear-configuration>
384 dropbear-configuration make-dropbear-configuration
385 dropbear-configuration?
386 (dropbear dropbear-configuration-dropbear
388 (port-number dropbear-configuration-port-number
390 (syslog-output? dropbear-configuration-syslog-output?
392 (pid-file dropbear-configuration-pid-file
393 (default "/var/run/dropbear.pid"))
394 (root-login? dropbear-configuration-root-login?
396 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
398 (password-authentication? dropbear-configuration-password-authentication?
401 (define (dropbear-activation config)
402 "Return the activation gexp for CONFIG."
404 (mkdir-p "/etc/dropbear")))
406 (define (dropbear-shepherd-service config)
407 "Return a <shepherd-service> for dropbear with CONFIG."
409 (dropbear-configuration-dropbear config))
412 (dropbear-configuration-pid-file config))
414 (define dropbear-command
415 #~(list (string-append #$dropbear "/sbin/dropbear")
417 ;; '-R' allows host keys to be automatically generated upon first
418 ;; connection, at a time when /dev/urandom is more likely securely
422 "-p" #$(number->string (dropbear-configuration-port-number config))
424 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
425 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
426 #$@(if (dropbear-configuration-password-authentication? config)
429 #$@(if (dropbear-configuration-allow-empty-passwords? config)
434 (if (dropbear-configuration-syslog-output? config)
435 '(networking syslogd) '(networking)))
437 (list (shepherd-service
438 (documentation "Dropbear SSH server.")
439 (requirement requires)
440 (provision '(ssh-daemon))
441 (start #~(make-forkexec-constructor #$dropbear-command
442 #:pid-file #$pid-file))
443 (stop #~(make-kill-destructor)))))
445 (define dropbear-service-type
446 (service-type (name 'dropbear)
448 (list (service-extension shepherd-root-service-type
449 dropbear-shepherd-service)
450 (service-extension activation-service-type
451 dropbear-activation)))))
453 (define* (dropbear-service #:optional (config (dropbear-configuration)))
454 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
455 daemon} with the given @var{config}, a @code{<dropbear-configuration>}
457 (service dropbear-service-type config))
459 ;;; ssh.scm ends here