system: Make service procedures non-monadic.
[jackhill/guix/guix.git] / gnu / services / ssh.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu services ssh)
20 #:use-module (guix gexp)
21 #:use-module (guix store)
22 #:use-module (gnu services)
23 #:use-module (gnu system linux) ; 'pam-service'
24 #:use-module (gnu packages lsh)
25 #:export (lsh-service))
26
27 ;;; Commentary:
28 ;;;
29 ;;; This module implements secure shell (SSH) services.
30 ;;;
31 ;;; Code:
32
33 (define %yarrow-seed
34 "/var/spool/lsh/yarrow-seed-file")
35
36 (define (activation lsh host-key)
37 "Return the gexp to activate the LSH service for HOST-KEY."
38 #~(begin
39 (unless (file-exists? #$%yarrow-seed)
40 (system* (string-append #$lsh "/bin/lsh-make-seed")
41 "--sloppy" "-o" #$%yarrow-seed))
42
43 (unless (file-exists? #$host-key)
44 (mkdir-p (dirname #$host-key))
45 (format #t "creating SSH host key '~a'...~%" #$host-key)
46
47 ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
48 ;; used yet because /bin/sh might be dangling; factorize this somehow.
49 (let* ((in+out (pipe))
50 (keygen (primitive-fork)))
51 (case keygen
52 ((0)
53 (close-port (car in+out))
54 (close-fdes 1)
55 (dup2 (fileno (cdr in+out)) 1)
56 (execl (string-append #$lsh "/bin/lsh-keygen")
57 "lsh-keygen" "--server"))
58 (else
59 (let ((write-key (primitive-fork)))
60 (case write-key
61 ((0)
62 (close-port (cdr in+out))
63 (close-fdes 0)
64 (dup2 (fileno (car in+out)) 0)
65 (execl (string-append #$lsh "/bin/lsh-writekey")
66 "lsh-writekey" "--server" "-o" #$host-key))
67 (else
68 (close-port (car in+out))
69 (close-port (cdr in+out))
70 (waitpid keygen)
71 (waitpid write-key))))))))))
72
73 (define* (lsh-service #:key
74 (lsh lsh)
75 (daemonic? #t)
76 (host-key "/etc/lsh/host-key")
77 (interfaces '())
78 (port-number 22)
79 (allow-empty-passwords? #f)
80 (root-login? #f)
81 (syslog-output? #t)
82 (pid-file? #f)
83 (pid-file "/var/run/lshd.pid")
84 (x11-forwarding? #t)
85 (tcp/ip-forwarding? #t)
86 (password-authentication? #t)
87 (public-key-authentication? #t)
88 (initialize? #t))
89 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
90 @var{host-key} must designate a file containing the host key, and readable
91 only by root.
92
93 When @var{daemonic?} is true, @command{lshd} will detach from the
94 controlling terminal and log its output to syslogd, unless one sets
95 @var{syslog-output?} to false. Obviously, it also makes lsh-service
96 depend on existence of syslogd service. When @var{pid-file?} is true,
97 @command{lshd} writes its PID to the file called @var{pid-file}.
98
99 When @var{initialize?} is true, automatically create the seed and host key
100 upon service activation if they do not exist yet. This may take long and
101 require interaction.
102
103 When @var{initialize?} is false, it is up to the user to initialize the
104 randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
105 a key pair with the private key stored in file @var{host-key} (@pxref{lshd
106 basics,,, lsh, LSH Manual}).
107
108 When @var{interfaces} is empty, lshd listens for connections on all the
109 network interfaces; otherwise, @var{interfaces} must be a list of host names
110 or addresses.
111
112 @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
113 passwords, and @var{root-login?} specifies whether to accept log-ins as
114 root.
115
116 The other options should be self-descriptive."
117 (define lsh-command
118 (append
119 (cons #~(string-append #$lsh "/sbin/lshd")
120 (if daemonic?
121 (let ((syslog (if syslog-output? '()
122 (list "--no-syslog"))))
123 (cons "--daemonic"
124 (if pid-file?
125 (cons #~(string-append "--pid-file=" #$pid-file)
126 syslog)
127 (cons "--no-pid-file" syslog))))
128 (if pid-file?
129 (list #~(string-append "--pid-file=" #$pid-file))
130 '())))
131 (cons* #~(string-append "--host-key=" #$host-key)
132 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
133 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
134 "-p" (number->string port-number)
135 (if password-authentication? "--password" "--no-password")
136 (if public-key-authentication?
137 "--publickey" "--no-publickey")
138 (if root-login?
139 "--root-login" "--no-root-login")
140 (if x11-forwarding?
141 "--x11-forward" "--no-x11-forward")
142 (if tcp/ip-forwarding?
143 "--tcpip-forward" "--no-tcpip-forward")
144 (if (null? interfaces)
145 '()
146 (list (string-append "--interfaces="
147 (string-join interfaces ",")))))))
148
149 (define requires
150 (if (and daemonic? syslog-output?)
151 '(networking syslogd)
152 '(networking)))
153
154 (service
155 (documentation "GNU lsh SSH server")
156 (provision '(ssh-daemon))
157 (requirement requires)
158 (start #~(make-forkexec-constructor (list #$@lsh-command)))
159 (stop #~(make-kill-destructor))
160 (pam-services
161 (list (unix-pam-service
162 "lshd"
163 #:allow-empty-passwords? allow-empty-passwords?)))
164 (activate #~(begin
165 (use-modules (guix build utils))
166 (mkdir-p "/var/spool/lsh")
167 #$(if initialize?
168 (activation lsh host-key)
169 #t)))))
170
171 ;;; ssh.scm ends here