gnu: libuv: Update to 1.11.0.
[jackhill/guix/guix.git] / gnu / services / ssh.scm
CommitLineData
f33e2d78 1;;; GNU Guix --- Functional package management for GNU
71b0601a
DC
2;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3;;; Copyright © 2016 David Craven <david@craven.ch>
86d8f6d3 4;;; Copyright © 2016 Julien Lepiller <julien@lepiller.eu>
e57bd0be 5;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
f33e2d78
LC
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)
71b0601a 23 #:use-module (gnu packages ssh)
86d8f6d3 24 #:use-module (gnu packages admin)
f33e2d78 25 #:use-module (gnu services)
0190c1c0 26 #:use-module (gnu services shepherd)
6e828634 27 #:use-module (gnu system pam)
86d8f6d3 28 #:use-module (gnu system shadow)
71b0601a
DC
29 #:use-module (guix gexp)
30 #:use-module (guix records)
fde40c98 31 #:use-module (srfi srfi-26)
86d8f6d3 32 #:use-module (ice-9 match)
24e96431
33 #:export (lsh-configuration
34 lsh-configuration?
35 lsh-service
36 lsh-service-type
71b0601a 37
86d8f6d3
JL
38 openssh-configuration
39 openssh-configuration?
40 openssh-service-type
41 openssh-service
42
71b0601a
DC
43 dropbear-configuration
44 dropbear-configuration?
45 dropbear-service-type
46 dropbear-service))
f33e2d78
LC
47
48;;; Commentary:
49;;;
50;;; This module implements secure shell (SSH) services.
51;;;
52;;; Code:
53
0adfe95a
LC
54(define-record-type* <lsh-configuration>
55 lsh-configuration make-lsh-configuration
56 lsh-configuration?
57 (lsh lsh-configuration-lsh
58 (default 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?))
73
f33e2d78
LC
74(define %yarrow-seed
75 "/var/spool/lsh/yarrow-seed-file")
76
0adfe95a
LC
77(define (lsh-initialization lsh host-key)
78 "Return the gexp to initialize the LSH service for HOST-KEY."
f33e2d78
LC
79 #~(begin
80 (unless (file-exists? #$%yarrow-seed)
81 (system* (string-append #$lsh "/bin/lsh-make-seed")
82 "--sloppy" "-o" #$%yarrow-seed))
83
84 (unless (file-exists? #$host-key)
85 (mkdir-p (dirname #$host-key))
86 (format #t "creating SSH host key '~a'...~%" #$host-key)
87
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)))
92 (case keygen
93 ((0)
94 (close-port (car in+out))
95 (close-fdes 1)
96 (dup2 (fileno (cdr in+out)) 1)
97 (execl (string-append #$lsh "/bin/lsh-keygen")
98 "lsh-keygen" "--server"))
99 (else
100 (let ((write-key (primitive-fork)))
101 (case write-key
102 ((0)
103 (close-port (cdr in+out))
104 (close-fdes 0)
105 (dup2 (fileno (car in+out)) 0)
106 (execl (string-append #$lsh "/bin/lsh-writekey")
107 "lsh-writekey" "--server" "-o" #$host-key))
108 (else
109 (close-port (car in+out))
110 (close-port (cdr in+out))
111 (waitpid keygen)
112 (waitpid write-key))))))))))
113
0adfe95a
LC
114(define (lsh-activation config)
115 "Return the activation gexp for CONFIG."
116 #~(begin
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))
122 #t)))
123
d4053c71
AK
124(define (lsh-shepherd-service config)
125 "Return a <shepherd-service> for lsh with CONFIG."
0adfe95a
LC
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))
131
132 (define lsh-command
133 (append
9e41130b 134 (cons (file-append lsh "/sbin/lshd")
0adfe95a
LC
135 (if daemonic?
136 (let ((syslog (if (lsh-configuration-syslog-output? config)
137 '()
138 (list "--no-syslog"))))
139 (cons "--daemonic"
140 (if pid-file?
141 (cons #~(string-append "--pid-file=" #$pid-file)
142 syslog)
143 (cons "--no-pid-file" syslog))))
144 (if pid-file?
145 (list #~(string-append "--pid-file=" #$pid-file))
146 '())))
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)
163 '()
fde40c98
LC
164 (map (cut string-append "--interface=" <>)
165 interfaces)))))
0adfe95a
LC
166
167 (define requires
168 (if (and daemonic? (lsh-configuration-syslog-output? config))
169 '(networking syslogd)
170 '(networking)))
171
d4053c71 172 (list (shepherd-service
0adfe95a
LC
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)))))
178
179(define (lsh-pam-services config)
180 "Return a list of <pam-services> for lshd with CONFIG."
181 (list (unix-pam-service
182 "lshd"
183 #:allow-empty-passwords?
184 (lsh-configuration-allow-empty-passwords? config))))
185
186(define lsh-service-type
187 (service-type (name 'lsh)
188 (extensions
d4053c71
AK
189 (list (service-extension shepherd-root-service-type
190 lsh-shepherd-service)
0adfe95a
LC
191 (service-extension pam-root-service-type
192 lsh-pam-services)
193 (service-extension activation-service-type
194 lsh-activation)))))
195
f33e2d78
LC
196(define* (lsh-service #:key
197 (lsh lsh)
5833bf33 198 (daemonic? #t)
f33e2d78
LC
199 (host-key "/etc/lsh/host-key")
200 (interfaces '())
201 (port-number 22)
202 (allow-empty-passwords? #f)
203 (root-login? #f)
204 (syslog-output? #t)
5833bf33
DP
205 (pid-file? #f)
206 (pid-file "/var/run/lshd.pid")
f33e2d78
LC
207 (x11-forwarding? #t)
208 (tcp/ip-forwarding? #t)
209 (password-authentication? #t)
210 (public-key-authentication? #t)
21cc905a 211 (initialize? #t))
f33e2d78
LC
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
214only by root.
215
5833bf33
DP
216When @var{daemonic?} is true, @command{lshd} will detach from the
217controlling terminal and log its output to syslogd, unless one sets
218@var{syslog-output?} to false. Obviously, it also makes lsh-service
219depend on existence of syslogd service. When @var{pid-file?} is true,
220@command{lshd} writes its PID to the file called @var{pid-file}.
221
f33e2d78
LC
222When @var{initialize?} is true, automatically create the seed and host key
223upon service activation if they do not exist yet. This may take long and
224require interaction.
225
20dd519c
LC
226When @var{initialize?} is false, it is up to the user to initialize the
227randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
228a key pair with the private key stored in file @var{host-key} (@pxref{lshd
229basics,,, lsh, LSH Manual}).
230
f33e2d78
LC
231When @var{interfaces} is empty, lshd listens for connections on all the
232network interfaces; otherwise, @var{interfaces} must be a list of host names
233or addresses.
234
20dd519c
LC
235@var{allow-empty-passwords?} specifies whether to accept log-ins with empty
236passwords, and @var{root-login?} specifies whether to accept log-ins as
f33e2d78
LC
237root.
238
239The other options should be self-descriptive."
0adfe95a
LC
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?))))
f33e2d78 255
71b0601a 256\f
86d8f6d3
JL
257;;;
258;;; OpenSSH.
259;;;
260
261(define-record-type* <openssh-configuration>
262 openssh-configuration make-openssh-configuration
263 openssh-configuration?
d8f31281
LC
264 (pid-file openssh-configuration-pid-file
265 (default "/var/run/sshd.pid"))
266 (port-number openssh-configuration-port-number ;integer
267 (default 22))
268 (permit-root-login openssh-configuration-permit-root-login ;Boolean | 'without-password
269 (default #f))
270 (allow-empty-passwords? openssh-configuration-allow-empty-passwords? ;Boolean
271 (default #f))
272 (password-authentication? openssh-configuration-password-authentication? ;Boolean
273 (default #t))
274 (public-key-authentication? openssh-configuration-public-key-authentication?
275 (default #t)) ;Boolean
276 (rsa-authentication? openssh-configuration-rsa-authentication? ;Boolean
277 (default #t))
278 (x11-forwarding? openssh-configuration-x11-forwarding? ;Boolean
279 (default #f))
280 (protocol-number openssh-configuration-protocol-number ;integer
281 (default 2)))
86d8f6d3
JL
282
283(define %openssh-accounts
284 (list (user-group (name "sshd") (system? #t))
285 (user-account
286 (name "sshd")
287 (group "sshd")
288 (system? #t)
289 (comment "sshd privilege separation user")
290 (home-directory "/var/run/sshd")
291 (shell #~(string-append #$shadow "/sbin/nologin")))))
292
293(define (openssh-activation config)
294 "Return the activation GEXP for CONFIG."
295 #~(begin
e57bd0be 296 (use-modules (guix build utils))
86d8f6d3
JL
297 (mkdir-p "/etc/ssh")
298 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
299
300 ;; Generate missing host keys.
301 (system* (string-append #$openssh "/bin/ssh-keygen") "-A")))
302
303(define (openssh-config-file config)
304 "Return the sshd configuration file corresponding to CONFIG."
305 (computed-file
306 "sshd_config"
307 #~(call-with-output-file #$output
308 (lambda (port)
309 (display "# Generated by 'openssh-service'.\n" port)
310 (format port "Protocol ~a\n"
311 #$(if (eq? (openssh-configuration-protocol-number config) 1)
312 "1" "2"))
313 (format port "Port ~a\n"
314 #$(number->string (openssh-configuration-port-number config)))
315 (format port "PermitRootLogin ~a\n"
316 #$(match (openssh-configuration-permit-root-login config)
317 (#t "yes")
318 (#f "no")
319 ('without-password "without-password")))
320 (format port "PermitEmptyPasswords ~a\n"
321 #$(if (openssh-configuration-allow-empty-passwords? config)
322 "yes" "no"))
323 (format port "PasswordAuthentication ~a\n"
324 #$(if (openssh-configuration-password-authentication? config)
325 "yes" "no"))
326 (format port "PubkeyAuthentication ~a\n"
d8f31281 327 #$(if (openssh-configuration-public-key-authentication? config)
86d8f6d3
JL
328 "yes" "no"))
329 (format port "RSAAuthentication ~a\n"
330 #$(if (openssh-configuration-rsa-authentication? config)
331 "yes" "no"))
332 (format port "X11Forwarding ~a\n"
333 #$(if (openssh-configuration-x11-forwarding? config)
334 "yes" "no"))
335 (format port "PidFile ~a\n"
336 #$(openssh-configuration-pid-file config))
337 #t))))
338
339(define (openssh-shepherd-service config)
340 "Return a <shepherd-service> for openssh with CONFIG."
341
342 (define pid-file
343 (openssh-configuration-pid-file config))
344
345 (define openssh-command
346 #~(list (string-append #$openssh "/sbin/sshd")
347 "-D" "-f" #$(openssh-config-file config)))
348
349 (list (shepherd-service
350 (documentation "OpenSSH server.")
351 (requirement '(networking syslogd))
352 (provision '(ssh-daemon))
353 (start #~(make-forkexec-constructor #$openssh-command
354 #:pid-file #$pid-file))
355 (stop #~(make-kill-destructor)))))
356
357(define openssh-service-type
358 (service-type (name 'openssh)
359 (extensions
360 (list (service-extension shepherd-root-service-type
361 openssh-shepherd-service)
362 (service-extension activation-service-type
363 openssh-activation)
364 (service-extension account-service-type
365 (const %openssh-accounts))))))
366
86d8f6d3 367\f
71b0601a
DC
368;;;
369;;; Dropbear.
370;;;
371
372(define-record-type* <dropbear-configuration>
373 dropbear-configuration make-dropbear-configuration
374 dropbear-configuration?
375 (dropbear dropbear-configuration-dropbear
376 (default dropbear))
377 (port-number dropbear-configuration-port-number
378 (default 22))
379 (syslog-output? dropbear-configuration-syslog-output?
380 (default #t))
381 (pid-file dropbear-configuration-pid-file
382 (default "/var/run/dropbear.pid"))
383 (root-login? dropbear-configuration-root-login?
384 (default #f))
385 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
386 (default #f))
387 (password-authentication? dropbear-configuration-password-authentication?
388 (default #t)))
389
390(define (dropbear-activation config)
391 "Return the activation gexp for CONFIG."
392 #~(begin
e57bd0be 393 (use-modules (guix build utils))
71b0601a
DC
394 (mkdir-p "/etc/dropbear")))
395
396(define (dropbear-shepherd-service config)
397 "Return a <shepherd-service> for dropbear with CONFIG."
398 (define dropbear
399 (dropbear-configuration-dropbear config))
400
401 (define pid-file
402 (dropbear-configuration-pid-file config))
403
404 (define dropbear-command
405 #~(list (string-append #$dropbear "/sbin/dropbear")
406
407 ;; '-R' allows host keys to be automatically generated upon first
408 ;; connection, at a time when /dev/urandom is more likely securely
409 ;; seeded.
410 "-F" "-R"
411
412 "-p" #$(number->string (dropbear-configuration-port-number config))
413 "-P" #$pid-file
414 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
415 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
416 #$@(if (dropbear-configuration-password-authentication? config)
417 '()
418 '("-s" "-g"))
419 #$@(if (dropbear-configuration-allow-empty-passwords? config)
420 '("-B")
421 '())))
422
423 (define requires
424 (if (dropbear-configuration-syslog-output? config)
425 '(networking syslogd) '(networking)))
426
427 (list (shepherd-service
428 (documentation "Dropbear SSH server.")
429 (requirement requires)
430 (provision '(ssh-daemon))
431 (start #~(make-forkexec-constructor #$dropbear-command
432 #:pid-file #$pid-file))
433 (stop #~(make-kill-destructor)))))
434
435(define dropbear-service-type
436 (service-type (name 'dropbear)
437 (extensions
438 (list (service-extension shepherd-root-service-type
439 dropbear-shepherd-service)
440 (service-extension activation-service-type
441 dropbear-activation)))))
442
443(define* (dropbear-service #:optional (config (dropbear-configuration)))
444 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
445daemon} with the given @var{config}, a @code{<dropbear-configuration>}
446object."
447 (service dropbear-service-type config))
448
f33e2d78 449;;; ssh.scm ends here