doc: Add xrefs to the lsh manual.
[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{initialize?} is false, it is up to the user to initialize the
95 randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
96 a key pair with the private key stored in file @var{host-key} (@pxref{lshd
97 basics,,, lsh, LSH Manual}).
98
99 When @var{interfaces} is empty, lshd listens for connections on all the
100 network interfaces; otherwise, @var{interfaces} must be a list of host names
101 or addresses.
102
103 @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
104 passwords, and @var{root-login?} specifies whether to accept log-ins as
105 root.
106
107 The other options should be self-descriptive."
108 (define lsh-command
109 (cons* #~(string-append #$lsh "/sbin/lshd")
110 #~(string-append "--host-key=" #$host-key)
111 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
112 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
113 "-p" (number->string port-number)
114 (if password-authentication? "--password" "--no-password")
115 (if public-key-authentication?
116 "--publickey" "--no-publickey")
117 (if root-login?
118 "--root-login" "--no-root-login")
119 (if x11-forwarding?
120 "--x11-forward" "--no-x11-forward")
121 (if tcp/ip-forwarding?
122 "--tcpip-forward" "--no-tcpip-forward")
123 (if (null? interfaces)
124 '()
125 (list (string-append "--interfaces="
126 (string-join interfaces ","))))))
127
128 (with-monad %store-monad
129 (return (service
130 (documentation "GNU lsh SSH server")
131 (provision '(ssh-daemon))
132 (requirement '(networking))
133 (start #~(make-forkexec-constructor (list #$@lsh-command)))
134 (stop #~(make-kill-destructor))
135 (pam-services
136 (list (unix-pam-service
137 "lshd"
138 #:allow-empty-passwords? allow-empty-passwords?)))
139 (activate #~(begin
140 (use-modules (guix build utils))
141 (mkdir-p "/var/spool/lsh")
142 #$(if initialize?
143 (activation lsh host-key)
144 #t)))))))
145
146 ;;; ssh.scm ends here