secret-service.scm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 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 (secret-service-receive-secrets port)
  104. "Listen to local PORT and wait for a secret service client to send secrets.
  105. Write them to the file system. Return the list of files installed on success,
  106. and #f otherwise."
  107. (define (wait-for-client port)
  108. ;; Wait for a TCP connection on PORT. Note: We cannot use the
  109. ;; virtio-serial ports, which would be safer, because they are
  110. ;; (presumably) unsupported on GNU/Hurd.
  111. (let ((sock (socket AF_INET SOCK_STREAM 0)))
  112. (bind sock AF_INET INADDR_ANY port)
  113. (listen sock 1)
  114. (log "waiting for secrets on port ~a...~%" port)
  115. (match (select (list sock) '() '() 60)
  116. (((_) () ())
  117. (match (accept sock)
  118. ((client . address)
  119. (log "client connection from ~a~%"
  120. (inet-ntop (sockaddr:fam address)
  121. (sockaddr:addr address)))
  122. ;; Send a "hello" message. This allows the client running on the
  123. ;; host to know that it's now actually connected to server running
  124. ;; in the guest.
  125. (write '(secret-service-server (version 0)) client)
  126. (force-output client)
  127. (close-port sock)
  128. client)))
  129. ((() () ())
  130. (log "did not receive any secrets; time out~%")
  131. (close-port sock)
  132. #f))))
  133. ;; TODO: Remove when (@ (guix build utils) dump-port) has a 'size'
  134. ;; parameter.
  135. (define (dump in out size)
  136. ;; Copy SIZE bytes from IN to OUT.
  137. (define buf-size 65536)
  138. (define buf (make-bytevector buf-size))
  139. (let loop ((left size))
  140. (if (<= left 0)
  141. 0
  142. (let ((read (get-bytevector-n! in buf 0 (min left buf-size))))
  143. (if (eof-object? read)
  144. left
  145. (begin
  146. (put-bytevector out buf 0 read)
  147. (loop (- left read))))))))
  148. (define (read-secrets port)
  149. ;; Read secret files from PORT and install them.
  150. (match (false-if-exception (read port))
  151. (('secrets ('version 0)
  152. ('files ((files sizes modes) ...)))
  153. (for-each (lambda (file size mode)
  154. (log "installing file '~a' (~a bytes)...~%"
  155. file size)
  156. (mkdir-p (dirname file))
  157. (call-with-output-file file
  158. (lambda (output)
  159. (dump port output size)
  160. (chmod file mode))))
  161. files sizes modes)
  162. (log "received ~a secret files~%" (length files))
  163. files)
  164. (_
  165. (log "invalid secrets received~%")
  166. #f)))
  167. (let* ((port (wait-for-client port))
  168. (result (and=> port read-secrets)))
  169. (when port
  170. (close-port port))
  171. result))
  172. ;;; secret-service.scm ends here