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>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu services ssh)
21 #:use-module (gnu packages ssh)
22 #:use-module (gnu services)
23 #:use-module (gnu services shepherd)
24 #:use-module (gnu system pam)
25 #:use-module (guix gexp)
26 #:use-module (guix records)
27 #:use-module (srfi srfi-26)
30 dropbear-configuration
31 dropbear-configuration?
37 ;;; This module implements secure shell (SSH) services.
42 (define-record-type* <lsh-configuration>
43 lsh-configuration make-lsh-configuration
45 (lsh lsh-configuration-lsh
47 (daemonic? lsh-configuration-daemonic?)
48 (host-key lsh-configuration-host-key)
49 (interfaces lsh-configuration-interfaces)
50 (port-number lsh-configuration-port-number)
51 (allow-empty-passwords? lsh-configuration-allow-empty-passwords?)
52 (root-login? lsh-configuration-root-login?)
53 (syslog-output? lsh-configuration-syslog-output?)
54 (pid-file? lsh-configuration-pid-file?)
55 (pid-file lsh-configuration-pid-file)
56 (x11-forwarding? lsh-configuration-x11-forwarding?)
57 (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?)
58 (password-authentication? lsh-configuration-password-authentication?)
59 (public-key-authentication? lsh-configuration-public-key-authentication?)
60 (initialize? lsh-configuration-initialize?))
63 "/var/spool/lsh/yarrow-seed-file")
65 (define (lsh-initialization lsh host-key)
66 "Return the gexp to initialize the LSH service for HOST-KEY."
68 (unless (file-exists? #$%yarrow-seed)
69 (system* (string-append #$lsh "/bin/lsh-make-seed")
70 "--sloppy" "-o" #$%yarrow-seed))
72 (unless (file-exists? #$host-key)
73 (mkdir-p (dirname #$host-key))
74 (format #t "creating SSH host key '~a'...~%" #$host-key)
76 ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
77 ;; used yet because /bin/sh might be dangling; factorize this somehow.
78 (let* ((in+out (pipe))
79 (keygen (primitive-fork)))
82 (close-port (car in+out))
84 (dup2 (fileno (cdr in+out)) 1)
85 (execl (string-append #$lsh "/bin/lsh-keygen")
86 "lsh-keygen" "--server"))
88 (let ((write-key (primitive-fork)))
91 (close-port (cdr in+out))
93 (dup2 (fileno (car in+out)) 0)
94 (execl (string-append #$lsh "/bin/lsh-writekey")
95 "lsh-writekey" "--server" "-o" #$host-key))
97 (close-port (car in+out))
98 (close-port (cdr in+out))
100 (waitpid write-key))))))))))
102 (define (lsh-activation config)
103 "Return the activation gexp for CONFIG."
105 (use-modules (guix build utils))
106 (mkdir-p "/var/spool/lsh")
107 #$(if (lsh-configuration-initialize? config)
108 (lsh-initialization (lsh-configuration-lsh config)
109 (lsh-configuration-host-key config))
112 (define (lsh-shepherd-service config)
113 "Return a <shepherd-service> for lsh with CONFIG."
114 (define lsh (lsh-configuration-lsh config))
115 (define pid-file (lsh-configuration-pid-file config))
116 (define pid-file? (lsh-configuration-pid-file? config))
117 (define daemonic? (lsh-configuration-daemonic? config))
118 (define interfaces (lsh-configuration-interfaces config))
122 (cons #~(string-append #$lsh "/sbin/lshd")
124 (let ((syslog (if (lsh-configuration-syslog-output? config)
126 (list "--no-syslog"))))
129 (cons #~(string-append "--pid-file=" #$pid-file)
131 (cons "--no-pid-file" syslog))))
133 (list #~(string-append "--pid-file=" #$pid-file))
135 (cons* #~(string-append "--host-key="
136 #$(lsh-configuration-host-key config))
137 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
138 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
139 "-p" (number->string (lsh-configuration-port-number config))
140 (if (lsh-configuration-password-authentication? config)
141 "--password" "--no-password")
142 (if (lsh-configuration-public-key-authentication? config)
143 "--publickey" "--no-publickey")
144 (if (lsh-configuration-root-login? config)
145 "--root-login" "--no-root-login")
146 (if (lsh-configuration-x11-forwarding? config)
147 "--x11-forward" "--no-x11-forward")
148 (if (lsh-configuration-tcp/ip-forwarding? config)
149 "--tcpip-forward" "--no-tcpip-forward")
150 (if (null? interfaces)
152 (map (cut string-append "--interface=" <>)
156 (if (and daemonic? (lsh-configuration-syslog-output? config))
157 '(networking syslogd)
160 (list (shepherd-service
161 (documentation "GNU lsh SSH server")
162 (provision '(ssh-daemon))
163 (requirement requires)
164 (start #~(make-forkexec-constructor (list #$@lsh-command)))
165 (stop #~(make-kill-destructor)))))
167 (define (lsh-pam-services config)
168 "Return a list of <pam-services> for lshd with CONFIG."
169 (list (unix-pam-service
171 #:allow-empty-passwords?
172 (lsh-configuration-allow-empty-passwords? config))))
174 (define lsh-service-type
175 (service-type (name 'lsh)
177 (list (service-extension shepherd-root-service-type
178 lsh-shepherd-service)
179 (service-extension pam-root-service-type
181 (service-extension activation-service-type
184 (define* (lsh-service #:key
187 (host-key "/etc/lsh/host-key")
190 (allow-empty-passwords? #f)
194 (pid-file "/var/run/lshd.pid")
196 (tcp/ip-forwarding? #t)
197 (password-authentication? #t)
198 (public-key-authentication? #t)
200 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
201 @var{host-key} must designate a file containing the host key, and readable
204 When @var{daemonic?} is true, @command{lshd} will detach from the
205 controlling terminal and log its output to syslogd, unless one sets
206 @var{syslog-output?} to false. Obviously, it also makes lsh-service
207 depend on existence of syslogd service. When @var{pid-file?} is true,
208 @command{lshd} writes its PID to the file called @var{pid-file}.
210 When @var{initialize?} is true, automatically create the seed and host key
211 upon service activation if they do not exist yet. This may take long and
214 When @var{initialize?} is false, it is up to the user to initialize the
215 randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
216 a key pair with the private key stored in file @var{host-key} (@pxref{lshd
217 basics,,, lsh, LSH Manual}).
219 When @var{interfaces} is empty, lshd listens for connections on all the
220 network interfaces; otherwise, @var{interfaces} must be a list of host names
223 @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
224 passwords, and @var{root-login?} specifies whether to accept log-ins as
227 The other options should be self-descriptive."
228 (service lsh-service-type
229 (lsh-configuration (lsh lsh) (daemonic? daemonic?)
230 (host-key host-key) (interfaces interfaces)
231 (port-number port-number)
232 (allow-empty-passwords? allow-empty-passwords?)
233 (root-login? root-login?)
234 (syslog-output? syslog-output?)
235 (pid-file? pid-file?) (pid-file pid-file)
236 (x11-forwarding? x11-forwarding?)
237 (tcp/ip-forwarding? tcp/ip-forwarding?)
238 (password-authentication?
239 password-authentication?)
240 (public-key-authentication?
241 public-key-authentication?)
242 (initialize? initialize?))))
249 (define-record-type* <dropbear-configuration>
250 dropbear-configuration make-dropbear-configuration
251 dropbear-configuration?
252 (dropbear dropbear-configuration-dropbear
254 (port-number dropbear-configuration-port-number
256 (syslog-output? dropbear-configuration-syslog-output?
258 (pid-file dropbear-configuration-pid-file
259 (default "/var/run/dropbear.pid"))
260 (root-login? dropbear-configuration-root-login?
262 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
264 (password-authentication? dropbear-configuration-password-authentication?
267 (define (dropbear-activation config)
268 "Return the activation gexp for CONFIG."
270 (mkdir-p "/etc/dropbear")))
272 (define (dropbear-shepherd-service config)
273 "Return a <shepherd-service> for dropbear with CONFIG."
275 (dropbear-configuration-dropbear config))
278 (dropbear-configuration-pid-file config))
280 (define dropbear-command
281 #~(list (string-append #$dropbear "/sbin/dropbear")
283 ;; '-R' allows host keys to be automatically generated upon first
284 ;; connection, at a time when /dev/urandom is more likely securely
288 "-p" #$(number->string (dropbear-configuration-port-number config))
290 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
291 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
292 #$@(if (dropbear-configuration-password-authentication? config)
295 #$@(if (dropbear-configuration-allow-empty-passwords? config)
300 (if (dropbear-configuration-syslog-output? config)
301 '(networking syslogd) '(networking)))
303 (list (shepherd-service
304 (documentation "Dropbear SSH server.")
305 (requirement requires)
306 (provision '(ssh-daemon))
307 (start #~(make-forkexec-constructor #$dropbear-command
308 #:pid-file #$pid-file))
309 (stop #~(make-kill-destructor)))))
311 (define dropbear-service-type
312 (service-type (name 'dropbear)
314 (list (service-extension shepherd-root-service-type
315 dropbear-shepherd-service)
316 (service-extension activation-service-type
317 dropbear-activation)))))
319 (define* (dropbear-service #:optional (config (dropbear-configuration)))
320 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
321 daemon} with the given @var{config}, a @code{<dropbear-configuration>}
323 (service dropbear-service-type config))
325 ;;; ssh.scm ends here