Merge branch 'master' into staging
[jackhill/guix/guix.git] / gnu / services / ssh.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 David Craven <david@craven.ch>
4 ;;; Copyright © 2016 Julien Lepiller <julien@lepiller.eu>
5 ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
6 ;;;
7 ;;; This file is part of GNU Guix.
8 ;;;
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
13 ;;;
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22 (define-module (gnu services ssh)
23 #:use-module (gnu packages ssh)
24 #:use-module (gnu packages admin)
25 #:use-module (gnu services)
26 #:use-module (gnu services shepherd)
27 #:use-module (gnu system pam)
28 #:use-module (gnu system shadow)
29 #:use-module (guix gexp)
30 #:use-module (guix records)
31 #:use-module (guix modules)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-26)
34 #:use-module (ice-9 match)
35 #:export (lsh-configuration
36 lsh-configuration?
37 lsh-service
38 lsh-service-type
39
40 openssh-configuration
41 openssh-configuration?
42 openssh-service-type
43
44 dropbear-configuration
45 dropbear-configuration?
46 dropbear-service-type
47 dropbear-service))
48
49 ;;; Commentary:
50 ;;;
51 ;;; This module implements secure shell (SSH) services.
52 ;;;
53 ;;; Code:
54
55 (define-record-type* <lsh-configuration>
56 lsh-configuration make-lsh-configuration
57 lsh-configuration?
58 (lsh lsh-configuration-lsh
59 (default lsh))
60 (daemonic? lsh-configuration-daemonic?)
61 (host-key lsh-configuration-host-key)
62 (interfaces lsh-configuration-interfaces)
63 (port-number lsh-configuration-port-number)
64 (allow-empty-passwords? lsh-configuration-allow-empty-passwords?)
65 (root-login? lsh-configuration-root-login?)
66 (syslog-output? lsh-configuration-syslog-output?)
67 (pid-file? lsh-configuration-pid-file?)
68 (pid-file lsh-configuration-pid-file)
69 (x11-forwarding? lsh-configuration-x11-forwarding?)
70 (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?)
71 (password-authentication? lsh-configuration-password-authentication?)
72 (public-key-authentication? lsh-configuration-public-key-authentication?)
73 (initialize? lsh-configuration-initialize?))
74
75 (define %yarrow-seed
76 "/var/spool/lsh/yarrow-seed-file")
77
78 (define (lsh-initialization lsh host-key)
79 "Return the gexp to initialize the LSH service for HOST-KEY."
80 #~(begin
81 (unless (file-exists? #$%yarrow-seed)
82 (system* (string-append #$lsh "/bin/lsh-make-seed")
83 "--sloppy" "-o" #$%yarrow-seed))
84
85 (unless (file-exists? #$host-key)
86 (mkdir-p (dirname #$host-key))
87 (format #t "creating SSH host key '~a'...~%" #$host-key)
88
89 ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
90 ;; used yet because /bin/sh might be dangling; factorize this somehow.
91 (let* ((in+out (pipe))
92 (keygen (primitive-fork)))
93 (case keygen
94 ((0)
95 (close-port (car in+out))
96 (close-fdes 1)
97 (dup2 (fileno (cdr in+out)) 1)
98 (execl (string-append #$lsh "/bin/lsh-keygen")
99 "lsh-keygen" "--server"))
100 (else
101 (let ((write-key (primitive-fork)))
102 (case write-key
103 ((0)
104 (close-port (cdr in+out))
105 (close-fdes 0)
106 (dup2 (fileno (car in+out)) 0)
107 (execl (string-append #$lsh "/bin/lsh-writekey")
108 "lsh-writekey" "--server" "-o" #$host-key))
109 (else
110 (close-port (car in+out))
111 (close-port (cdr in+out))
112 (waitpid keygen)
113 (waitpid write-key))))))))))
114
115 (define (lsh-activation config)
116 "Return the activation gexp for CONFIG."
117 #~(begin
118 (use-modules (guix build utils))
119 (mkdir-p "/var/spool/lsh")
120 #$(if (lsh-configuration-initialize? config)
121 (lsh-initialization (lsh-configuration-lsh config)
122 (lsh-configuration-host-key config))
123 #t)))
124
125 (define (lsh-shepherd-service config)
126 "Return a <shepherd-service> for lsh with CONFIG."
127 (define lsh (lsh-configuration-lsh config))
128 (define pid-file (lsh-configuration-pid-file config))
129 (define pid-file? (lsh-configuration-pid-file? config))
130 (define daemonic? (lsh-configuration-daemonic? config))
131 (define interfaces (lsh-configuration-interfaces config))
132
133 (define lsh-command
134 (append
135 (cons (file-append lsh "/sbin/lshd")
136 (if daemonic?
137 (let ((syslog (if (lsh-configuration-syslog-output? config)
138 '()
139 (list "--no-syslog"))))
140 (cons "--daemonic"
141 (if pid-file?
142 (cons #~(string-append "--pid-file=" #$pid-file)
143 syslog)
144 (cons "--no-pid-file" syslog))))
145 (if pid-file?
146 (list #~(string-append "--pid-file=" #$pid-file))
147 '())))
148 (cons* #~(string-append "--host-key="
149 #$(lsh-configuration-host-key config))
150 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
151 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
152 "-p" (number->string (lsh-configuration-port-number config))
153 (if (lsh-configuration-password-authentication? config)
154 "--password" "--no-password")
155 (if (lsh-configuration-public-key-authentication? config)
156 "--publickey" "--no-publickey")
157 (if (lsh-configuration-root-login? config)
158 "--root-login" "--no-root-login")
159 (if (lsh-configuration-x11-forwarding? config)
160 "--x11-forward" "--no-x11-forward")
161 (if (lsh-configuration-tcp/ip-forwarding? config)
162 "--tcpip-forward" "--no-tcpip-forward")
163 (if (null? interfaces)
164 '()
165 (map (cut string-append "--interface=" <>)
166 interfaces)))))
167
168 (define requires
169 (if (and daemonic? (lsh-configuration-syslog-output? config))
170 '(networking syslogd)
171 '(networking)))
172
173 (list (shepherd-service
174 (documentation "GNU lsh SSH server")
175 (provision '(ssh-daemon))
176 (requirement requires)
177 (start #~(make-forkexec-constructor (list #$@lsh-command)))
178 (stop #~(make-kill-destructor)))))
179
180 (define (lsh-pam-services config)
181 "Return a list of <pam-services> for lshd with CONFIG."
182 (list (unix-pam-service
183 "lshd"
184 #:allow-empty-passwords?
185 (lsh-configuration-allow-empty-passwords? config))))
186
187 (define lsh-service-type
188 (service-type (name 'lsh)
189 (extensions
190 (list (service-extension shepherd-root-service-type
191 lsh-shepherd-service)
192 (service-extension pam-root-service-type
193 lsh-pam-services)
194 (service-extension activation-service-type
195 lsh-activation)))))
196
197 (define* (lsh-service #:key
198 (lsh lsh)
199 (daemonic? #t)
200 (host-key "/etc/lsh/host-key")
201 (interfaces '())
202 (port-number 22)
203 (allow-empty-passwords? #f)
204 (root-login? #f)
205 (syslog-output? #t)
206 (pid-file? #f)
207 (pid-file "/var/run/lshd.pid")
208 (x11-forwarding? #t)
209 (tcp/ip-forwarding? #t)
210 (password-authentication? #t)
211 (public-key-authentication? #t)
212 (initialize? #t))
213 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
214 @var{host-key} must designate a file containing the host key, and readable
215 only by root.
216
217 When @var{daemonic?} is true, @command{lshd} will detach from the
218 controlling terminal and log its output to syslogd, unless one sets
219 @var{syslog-output?} to false. Obviously, it also makes lsh-service
220 depend on existence of syslogd service. When @var{pid-file?} is true,
221 @command{lshd} writes its PID to the file called @var{pid-file}.
222
223 When @var{initialize?} is true, automatically create the seed and host key
224 upon service activation if they do not exist yet. This may take long and
225 require interaction.
226
227 When @var{initialize?} is false, it is up to the user to initialize the
228 randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
229 a key pair with the private key stored in file @var{host-key} (@pxref{lshd
230 basics,,, lsh, LSH Manual}).
231
232 When @var{interfaces} is empty, lshd listens for connections on all the
233 network interfaces; otherwise, @var{interfaces} must be a list of host names
234 or addresses.
235
236 @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
237 passwords, and @var{root-login?} specifies whether to accept log-ins as
238 root.
239
240 The other options should be self-descriptive."
241 (service lsh-service-type
242 (lsh-configuration (lsh lsh) (daemonic? daemonic?)
243 (host-key host-key) (interfaces interfaces)
244 (port-number port-number)
245 (allow-empty-passwords? allow-empty-passwords?)
246 (root-login? root-login?)
247 (syslog-output? syslog-output?)
248 (pid-file? pid-file?) (pid-file pid-file)
249 (x11-forwarding? x11-forwarding?)
250 (tcp/ip-forwarding? tcp/ip-forwarding?)
251 (password-authentication?
252 password-authentication?)
253 (public-key-authentication?
254 public-key-authentication?)
255 (initialize? initialize?))))
256
257 \f
258 ;;;
259 ;;; OpenSSH.
260 ;;;
261
262 (define-record-type* <openssh-configuration>
263 openssh-configuration make-openssh-configuration
264 openssh-configuration?
265 ;; <package>
266 (openssh openssh-configuration-openssh
267 (default openssh))
268 ;; string
269 (pid-file openssh-configuration-pid-file
270 (default "/var/run/sshd.pid"))
271 ;; integer
272 (port-number openssh-configuration-port-number
273 (default 22))
274 ;; Boolean | 'without-password
275 (permit-root-login openssh-configuration-permit-root-login
276 (default #f))
277 ;; Boolean
278 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
279 (default #f))
280 ;; Boolean
281 (password-authentication? openssh-configuration-password-authentication?
282 (default #t))
283 ;; Boolean
284 (public-key-authentication? openssh-configuration-public-key-authentication?
285 (default #t))
286 ;; Boolean
287 (x11-forwarding? openssh-configuration-x11-forwarding?
288 (default #f))
289 ;; Boolean
290 (challenge-response-authentication? openssh-challenge-response-authentication?
291 (default #f))
292 ;; Boolean
293 (use-pam? openssh-configuration-use-pam?
294 (default #t))
295 ;; Boolean
296 (print-last-log? openssh-configuration-print-last-log?
297 (default #t))
298 ;; list of two-element lists
299 (subsystems openssh-configuration-subsystems
300 (default '(("sftp" "internal-sftp"))))
301
302 ;; list of user-name/file-like tuples
303 (authorized-keys openssh-authorized-keys
304 (default '())))
305
306 (define %openssh-accounts
307 (list (user-group (name "sshd") (system? #t))
308 (user-account
309 (name "sshd")
310 (group "sshd")
311 (system? #t)
312 (comment "sshd privilege separation user")
313 (home-directory "/var/run/sshd")
314 (shell #~(string-append #$shadow "/sbin/nologin")))))
315
316 (define (openssh-activation config)
317 "Return the activation GEXP for CONFIG."
318 (with-imported-modules '((guix build utils))
319 #~(begin
320 (use-modules (guix build utils))
321
322 (define (touch file-name)
323 (call-with-output-file file-name (const #t)))
324
325 ;; Make sure /etc/ssh can be read by the 'sshd' user.
326 (mkdir-p "/etc/ssh")
327 (chmod "/etc/ssh" #o755)
328 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
329
330 ;; 'sshd' complains if the authorized-key directory and its parents
331 ;; are group-writable, which rules out /gnu/store. Thus we copy the
332 ;; authorized-key directory to /etc.
333 (catch 'system-error
334 (lambda ()
335 (delete-file-recursively "/etc/authorized_keys.d"))
336 (lambda args
337 (unless (= ENOENT (system-error-errno args))
338 (apply throw args))))
339 (copy-recursively #$(authorized-key-directory
340 (openssh-authorized-keys config))
341 "/etc/ssh/authorized_keys.d")
342
343 (chmod "/etc/ssh/authorized_keys.d" #o555)
344
345 (let ((lastlog "/var/log/lastlog"))
346 (when #$(openssh-configuration-print-last-log? config)
347 (unless (file-exists? lastlog)
348 (touch lastlog))))
349
350 ;; Generate missing host keys.
351 (system* (string-append #$(openssh-configuration-openssh config)
352 "/bin/ssh-keygen") "-A"))))
353
354 (define (authorized-key-directory keys)
355 "Return a directory containing the authorized keys specified in KEYS, a list
356 of user-name/file-like tuples."
357 (define build
358 (with-imported-modules (source-module-closure '((guix build utils)))
359 #~(begin
360 (use-modules (ice-9 match) (srfi srfi-26)
361 (guix build utils))
362
363 (mkdir #$output)
364 (for-each (match-lambda
365 ((user keys ...)
366 (let ((file (string-append #$output "/" user)))
367 (call-with-output-file file
368 (lambda (port)
369 (for-each (lambda (key)
370 (call-with-input-file key
371 (cut dump-port <> port)))
372 keys))))))
373 '#$keys))))
374
375 (computed-file "openssh-authorized-keys" build))
376
377 (define (openssh-config-file config)
378 "Return the sshd configuration file corresponding to CONFIG."
379 (computed-file
380 "sshd_config"
381 #~(begin
382 (use-modules (ice-9 match))
383 (call-with-output-file #$output
384 (lambda (port)
385 (display "# Generated by 'openssh-service'.\n" port)
386 (format port "Port ~a\n"
387 #$(number->string
388 (openssh-configuration-port-number config)))
389 (format port "PermitRootLogin ~a\n"
390 #$(match (openssh-configuration-permit-root-login config)
391 (#t "yes")
392 (#f "no")
393 ('without-password "without-password")))
394 (format port "PermitEmptyPasswords ~a\n"
395 #$(if (openssh-configuration-allow-empty-passwords? config)
396 "yes" "no"))
397 (format port "PasswordAuthentication ~a\n"
398 #$(if (openssh-configuration-password-authentication? config)
399 "yes" "no"))
400 (format port "PubkeyAuthentication ~a\n"
401 #$(if (openssh-configuration-public-key-authentication?
402 config)
403 "yes" "no"))
404 (format port "X11Forwarding ~a\n"
405 #$(if (openssh-configuration-x11-forwarding? config)
406 "yes" "no"))
407 (format port "PidFile ~a\n"
408 #$(openssh-configuration-pid-file config))
409 (format port "ChallengeResponseAuthentication ~a\n"
410 #$(if (openssh-challenge-response-authentication? config)
411 "yes" "no"))
412 (format port "UsePAM ~a\n"
413 #$(if (openssh-configuration-use-pam? config)
414 "yes" "no"))
415 (format port "PrintLastLog ~a\n"
416 #$(if (openssh-configuration-print-last-log? config)
417 "yes" "no"))
418
419 ;; Add '/etc/authorized_keys.d/%u', which we populate.
420 (format port "AuthorizedKeysFile \
421 .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
422
423 (for-each
424 (match-lambda
425 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
426 '#$(openssh-configuration-subsystems config))
427 #t)))))
428
429 (define (openssh-shepherd-service config)
430 "Return a <shepherd-service> for openssh with CONFIG."
431
432 (define pid-file
433 (openssh-configuration-pid-file config))
434
435 (define openssh-command
436 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
437 "-D" "-f" #$(openssh-config-file config)))
438
439 (list (shepherd-service
440 (documentation "OpenSSH server.")
441 (requirement '(syslogd))
442 (provision '(ssh-daemon))
443 (start #~(make-forkexec-constructor #$openssh-command
444 #:pid-file #$pid-file))
445 (stop #~(make-kill-destructor)))))
446
447 (define (openssh-pam-services config)
448 "Return a list of <pam-services> for sshd with CONFIG."
449 (list (unix-pam-service
450 "sshd"
451 #:allow-empty-passwords?
452 (openssh-configuration-allow-empty-passwords? config))))
453
454 (define (extend-openssh-authorized-keys config keys)
455 "Extend CONFIG with the extra authorized keys listed in KEYS."
456 (openssh-configuration
457 (inherit config)
458 (authorized-keys
459 (append (openssh-authorized-keys config) keys))))
460
461 (define openssh-service-type
462 (service-type (name 'openssh)
463 (extensions
464 (list (service-extension shepherd-root-service-type
465 openssh-shepherd-service)
466 (service-extension pam-root-service-type
467 openssh-pam-services)
468 (service-extension activation-service-type
469 openssh-activation)
470 (service-extension account-service-type
471 (const %openssh-accounts))))
472 (compose concatenate)
473 (extend extend-openssh-authorized-keys)
474 (default-value (openssh-configuration))))
475
476 \f
477 ;;;
478 ;;; Dropbear.
479 ;;;
480
481 (define-record-type* <dropbear-configuration>
482 dropbear-configuration make-dropbear-configuration
483 dropbear-configuration?
484 (dropbear dropbear-configuration-dropbear
485 (default dropbear))
486 (port-number dropbear-configuration-port-number
487 (default 22))
488 (syslog-output? dropbear-configuration-syslog-output?
489 (default #t))
490 (pid-file dropbear-configuration-pid-file
491 (default "/var/run/dropbear.pid"))
492 (root-login? dropbear-configuration-root-login?
493 (default #f))
494 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
495 (default #f))
496 (password-authentication? dropbear-configuration-password-authentication?
497 (default #t)))
498
499 (define (dropbear-activation config)
500 "Return the activation gexp for CONFIG."
501 #~(begin
502 (use-modules (guix build utils))
503 (mkdir-p "/etc/dropbear")))
504
505 (define (dropbear-shepherd-service config)
506 "Return a <shepherd-service> for dropbear with CONFIG."
507 (define dropbear
508 (dropbear-configuration-dropbear config))
509
510 (define pid-file
511 (dropbear-configuration-pid-file config))
512
513 (define dropbear-command
514 #~(list (string-append #$dropbear "/sbin/dropbear")
515
516 ;; '-R' allows host keys to be automatically generated upon first
517 ;; connection, at a time when /dev/urandom is more likely securely
518 ;; seeded.
519 "-F" "-R"
520
521 "-p" #$(number->string (dropbear-configuration-port-number config))
522 "-P" #$pid-file
523 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
524 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
525 #$@(if (dropbear-configuration-password-authentication? config)
526 '()
527 '("-s" "-g"))
528 #$@(if (dropbear-configuration-allow-empty-passwords? config)
529 '("-B")
530 '())))
531
532 (define requires
533 (if (dropbear-configuration-syslog-output? config)
534 '(networking syslogd) '(networking)))
535
536 (list (shepherd-service
537 (documentation "Dropbear SSH server.")
538 (requirement requires)
539 (provision '(ssh-daemon))
540 (start #~(make-forkexec-constructor #$dropbear-command
541 #:pid-file #$pid-file))
542 (stop #~(make-kill-destructor)))))
543
544 (define dropbear-service-type
545 (service-type (name 'dropbear)
546 (extensions
547 (list (service-extension shepherd-root-service-type
548 dropbear-shepherd-service)
549 (service-extension activation-service-type
550 dropbear-activation)))))
551
552 (define* (dropbear-service #:optional (config (dropbear-configuration)))
553 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
554 daemon} with the given @var{config}, a @code{<dropbear-configuration>}
555 object."
556 (service dropbear-service-type config))
557
558 ;;; ssh.scm ends here