secret-service.scm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  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.
  11. ;;;
  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.
  16. ;;;
  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/>.
  19. (define-module (gnu build secret-service)
  20. #:use-module (guix build utils)
  21. #:use-module (srfi srfi-26)
  22. #:use-module (rnrs bytevectors)
  23. #:use-module (ice-9 binary-ports)
  24. #:use-module (ice-9 match)
  25. #:use-module (ice-9 rdelim)
  26. #:export (secret-service-receive-secrets
  27. secret-service-send-secrets))
  28. ;;; Commentary:
  29. ;;;
  30. ;;; Utility procedures for copying secrets into a VM.
  31. ;;;
  32. ;;; Code:
  33. (define-syntax log
  34. (lambda (s)
  35. "Log the given message."
  36. (syntax-case s ()
  37. ((_ fmt args ...)
  38. (with-syntax ((fmt (string-append "secret service: "
  39. (syntax->datum #'fmt))))
  40. ;; Log to the current output port. That way, when
  41. ;; 'secret-service-send-secrets' is called from shepherd, output goes
  42. ;; to syslog.
  43. #'(format (current-output-port) fmt args ...))))))
  44. (define* (secret-service-send-secrets port secret-root
  45. #:key (retry 60)
  46. (handshake-timeout 120))
  47. "Copy all files under SECRET-ROOT using TCP to secret-service listening at
  48. local PORT. If connect fails, sleep 1s and retry RETRY times; once connected,
  49. wait for at most HANDSHAKE-TIMEOUT seconds for handshake to complete. Return
  50. #f on failure."
  51. (define (file->file+size+mode file-name)
  52. (let ((stat (stat file-name))
  53. (target (substring file-name (string-length secret-root))))
  54. (list target (stat:size stat) (stat:mode stat))))
  55. (define (send-files sock)
  56. (let* ((files (if secret-root (find-files secret-root) '()))
  57. (files-sizes-modes (map file->file+size+mode files))
  58. (secrets `(secrets
  59. (version 0)
  60. (files ,files-sizes-modes))))
  61. (write secrets sock)
  62. (for-each (lambda (file)
  63. (call-with-input-file file
  64. (lambda (input)
  65. (dump-port input sock))))
  66. files)))
  67. (log "sending secrets to ~a~%" port)
  68. (let ((sock (socket AF_INET SOCK_STREAM 0))
  69. (addr (make-socket-address AF_INET INADDR_LOOPBACK port)))
  70. ;; Connect to QEMU on the forwarded port. The 'connect' call succeeds as
  71. ;; soon as QEMU is ready, even if there's no server listening on the
  72. ;; forward port inside the guest.
  73. (let loop ((retry retry))
  74. (catch 'system-error
  75. (cute connect sock addr)
  76. (lambda (key . args)
  77. (when (zero? retry)
  78. (apply throw key args))
  79. (log "retrying connection [~a attempts left]~%"
  80. (- retry 1))
  81. (sleep 1)
  82. (loop (1- retry)))))
  83. (log "connected; waiting for handshake...~%")
  84. ;; Wait for "hello" message from the server. This is the only way to know
  85. ;; that we're really connected to the server inside the guest.
  86. (match (select (list sock) '() '() handshake-timeout)
  87. (((_) () ())
  88. (match (read sock)
  89. (('secret-service-server ('version version ...))
  90. (log "sending files from ~s...~%" secret-root)
  91. (send-files sock)
  92. (log "done sending files to port ~a~%" port)
  93. (close-port sock)
  94. secret-root)
  95. (x
  96. (log "invalid handshake ~s~%" x)
  97. (close-port sock)
  98. #f)))
  99. ((() () ()) ;timeout
  100. (log "timeout while sending files to ~a~%" port)
  101. (close-port sock)
  102. #f))))
  103. (define (delete-file* file)
  104. "Ensure FILE does not exist."
  105. (catch 'system-error
  106. (lambda ()
  107. (delete-file file))
  108. (lambda args
  109. (unless (= ENOENT (system-error-errno args))
  110. (apply throw args)))))
  111. (define (secret-service-receive-secrets port)
  112. "Listen to local PORT and wait for a secret service client to send secrets.
  113. Write them to the file system. Return the list of files installed on success,
  114. and #f otherwise."
  115. (define (wait-for-client port)
  116. ;; Wait for a TCP connection on PORT. Note: We cannot use the
  117. ;; virtio-serial ports, which would be safer, because they are
  118. ;; (presumably) unsupported on GNU/Hurd.
  119. (let ((sock (socket AF_INET SOCK_STREAM 0)))
  120. (bind sock AF_INET INADDR_ANY port)
  121. (listen sock 1)
  122. (log "waiting for secrets on port ~a...~%" port)
  123. (match (select (list sock) '() '() 60)
  124. (((_) () ())
  125. (match (accept sock)
  126. ((client . address)
  127. (log "client connection from ~a~%"
  128. (inet-ntop (sockaddr:fam address)
  129. (sockaddr:addr address)))
  130. ;; Send a "hello" message. This allows the client running on the
  131. ;; host to know that it's now actually connected to server running
  132. ;; in the guest.
  133. (write '(secret-service-server (version 0)) client)
  134. (force-output client)
  135. (close-port sock)
  136. client)))
  137. ((() () ())
  138. (log "did not receive any secrets; time out~%")
  139. (close-port sock)
  140. #f))))
  141. ;; TODO: Remove when (@ (guix build utils) dump-port) has a 'size'
  142. ;; parameter.
  143. (define (dump in out size)
  144. ;; Copy SIZE bytes from IN to OUT.
  145. (define buf-size 65536)
  146. (define buf (make-bytevector buf-size))
  147. (let loop ((left size))
  148. (if (<= left 0)
  149. 0
  150. (let ((read (get-bytevector-n! in buf 0 (min left buf-size))))
  151. (if (eof-object? read)
  152. left
  153. (begin
  154. (put-bytevector out buf 0 read)
  155. (loop (- left read))))))))
  156. (define (read-secrets port)
  157. ;; Read secret files from PORT and install them.
  158. (match (false-if-exception (read port))
  159. (('secrets ('version 0)
  160. ('files ((files sizes modes) ...)))
  161. (for-each (lambda (file size mode)
  162. (log "installing file '~a' (~a bytes)...~%"
  163. file size)
  164. (mkdir-p (dirname file))
  165. ;; It could be that FILE already exists, for instance
  166. ;; because it has been created by a service's activation
  167. ;; snippet (e.g., SSH host keys). Delete it.
  168. (delete-file* file)
  169. (call-with-output-file file
  170. (lambda (output)
  171. (dump port output size)
  172. (chmod file mode))))
  173. files sizes modes)
  174. (log "received ~a secret files~%" (length files))
  175. files)
  176. (_
  177. (log "invalid secrets received~%")
  178. #f)))
  179. (let* ((port (wait-for-client port))
  180. (result (and=> port read-secrets)))
  181. (when port
  182. (close-port port))
  183. result))
  184. ;;; secret-service.scm ends here