services: Update to use the dmd 0.2 API.
[jackhill/guix/guix.git] / gnu / services / ssh.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014 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 (gnu services)
22 #:use-module (gnu system linux) ; 'pam-service'
23 #:use-module (gnu packages lsh)
24 #:use-module (guix monads)
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 (host-key "/etc/lsh/host-key")
76 (interfaces '())
77 (port-number 22)
78 (allow-empty-passwords? #f)
79 (root-login? #f)
80 (syslog-output? #t)
81 (x11-forwarding? #t)
82 (tcp/ip-forwarding? #t)
83 (password-authentication? #t)
84 (public-key-authentication? #t)
85 initialize?)
86 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
87 @var{host-key} must designate a file containing the host key, and readable
88 only by root.
89
90 When @var{initialize?} is true, automatically create the seed and host key
91 upon service activation if they do not exist yet. This may take long and
92 require interaction.
93
94 When @var{interfaces} is empty, lshd listens for connections on all the
95 network interfaces; otherwise, @var{interfaces} must be a list of host names
96 or addresses.
97
98 @var{allow-empty-passwords?} specifies whether to accepts log-ins with empty
99 passwords, and @var{root-login?} specifies whether to accepts log-ins as
100 root.
101
102 The other options should be self-descriptive."
103 (define lsh-command
104 (cons* #~(string-append #$lsh "/sbin/lshd")
105 #~(string-append "--host-key=" #$host-key)
106 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
107 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
108 "-p" (number->string port-number)
109 (if password-authentication? "--password" "--no-password")
110 (if public-key-authentication?
111 "--publickey" "--no-publickey")
112 (if root-login?
113 "--root-login" "--no-root-login")
114 (if x11-forwarding?
115 "--x11-forward" "--no-x11-forward")
116 (if tcp/ip-forwarding?
117 "--tcpip-forward" "--no-tcpip-forward")
118 (if (null? interfaces)
119 '()
120 (list (string-append "--interfaces="
121 (string-join interfaces ","))))))
122
123 (with-monad %store-monad
124 (return (service
125 (documentation "GNU lsh SSH server")
126 (provision '(ssh-daemon))
127 (requirement '(networking))
128 (start #~(make-forkexec-constructor (list #$@lsh-command)))
129 (stop #~(make-kill-destructor))
130 (pam-services
131 (list (unix-pam-service
132 "lshd"
133 #:allow-empty-passwords? allow-empty-passwords?)))
134 (activate #~(begin
135 (mkdir-p "/var/spool/lsh")
136 #$(if initialize?
137 (activation lsh host-key)
138 #t)))))))
139
140 ;;; ssh.scm ends here