nar.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 Mark H Weaver <mhw@netris.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 (guix nar)
  20. #:use-module (guix serialization)
  21. #:use-module (guix build syscalls)
  22. #:use-module ((guix build utils)
  23. #:select (delete-file-recursively with-directory-excursion))
  24. ;; XXX: Eventually we should use (guix store database) exclusively, and not
  25. ;; (guix store) since this is "daemon-side" code.
  26. #:use-module (guix store)
  27. #:use-module (guix store database)
  28. #:use-module (guix ui) ; for '_'
  29. #:use-module (gcrypt hash)
  30. #:use-module (guix pki)
  31. #:use-module (gcrypt pk-crypto)
  32. #:use-module (srfi srfi-1)
  33. #:use-module (srfi srfi-11)
  34. #:use-module (srfi srfi-26)
  35. #:use-module (srfi srfi-34)
  36. #:use-module (srfi srfi-35)
  37. #:export (nar-invalid-hash-error?
  38. nar-invalid-hash-error-expected
  39. nar-invalid-hash-error-actual
  40. nar-signature-error?
  41. nar-signature-error-signature
  42. restore-file-set))
  43. ;;; Comment:
  44. ;;;
  45. ;;; Read and write Nix archives, aka. ‘nar’.
  46. ;;;
  47. ;;; Code:
  48. (define-condition-type &nar-signature-error &nar-error
  49. nar-signature-error?
  50. (signature nar-signature-error-signature)) ; faulty signature or #f
  51. (define-condition-type &nar-invalid-hash-error &nar-signature-error
  52. nar-invalid-hash-error?
  53. (expected nar-invalid-hash-error-expected) ; expected hash (a bytevector)
  54. (actual nar-invalid-hash-error-actual)) ; actual hash
  55. ;;;
  56. ;;; Restoring a file set into the store.
  57. ;;;
  58. ;; The code below accesses the store directly and is meant to be run from
  59. ;; "build hooks", which cannot invoke the daemon's 'import-paths' RPC since
  60. ;; (1) the locks on the files to be restored as already held, and (2) the
  61. ;; $NIX_HELD_LOCKS hackish environment variable cannot be set.
  62. ;;
  63. ;; So we're really duplicating that functionality of the daemon (well, until
  64. ;; most of the daemon is in Scheme :-)). But note that we do use a couple of
  65. ;; RPCs for functionality not available otherwise, like 'valid-path?'.
  66. (define* (finalize-store-file source target
  67. #:key (references '()) deriver (lock? #t))
  68. "Rename SOURCE to TARGET and register TARGET as a valid store item, with
  69. REFERENCES and DERIVER. When LOCK? is true, acquire exclusive locks on TARGET
  70. before attempting to register it; otherwise, assume TARGET's locks are already
  71. held."
  72. (with-database %default-database-file db
  73. (unless (path-id db target)
  74. (let ((lock (and lock?
  75. (lock-file (string-append target ".lock")))))
  76. (unless (path-id db target)
  77. ;; If FILE already exists, delete it (it's invalid anyway.)
  78. (when (file-exists? target)
  79. (delete-file-recursively target))
  80. ;; Install the new TARGET.
  81. (rename-file source target)
  82. ;; Register TARGET. As a side effect, it resets the timestamps of all
  83. ;; its files, recursively, and runs a deduplication pass.
  84. (register-path target
  85. #:references references
  86. #:deriver deriver))
  87. (when lock?
  88. (unlock-file lock))))))
  89. (define (temporary-store-file)
  90. "Return the file name of a temporary file created in the store."
  91. (let* ((template (string-append (%store-prefix) "/guix-XXXXXX"))
  92. (port (mkstemp! template)))
  93. (close-port port)
  94. template))
  95. (define-syntax-rule (with-temporary-store-file name body ...)
  96. "Evaluate BODY with NAME bound to the file name of a temporary store item
  97. protected from GC."
  98. (let loop ((name (temporary-store-file)))
  99. (with-store store
  100. ;; Add NAME to the current process' roots. (Opening this connection to
  101. ;; the daemon allows us to reuse its code that deals with the
  102. ;; per-process roots file.)
  103. (add-temp-root store name)
  104. ;; There's a window during which GC could delete NAME. Try again when
  105. ;; that happens.
  106. (if (file-exists? name)
  107. (begin
  108. (delete-file name)
  109. body ...)
  110. (loop (temporary-store-file))))))
  111. (define* (restore-one-item port
  112. #:key acl (verify-signature? #t) (lock? #t)
  113. (log-port (current-error-port)))
  114. "Restore one store item from PORT; return its file name on success."
  115. (define (assert-valid-signature signature hash file)
  116. ;; Bail out if SIGNATURE, which must be a string as produced by
  117. ;; 'canonical-sexp->string', doesn't match HASH, a bytevector containing
  118. ;; the expected hash for FILE.
  119. (let ((signature (catch 'gcry-error
  120. (lambda ()
  121. (string->canonical-sexp signature))
  122. (lambda (key proc err)
  123. (raise (condition
  124. (&message
  125. (message "signature is not a valid \
  126. s-expression"))
  127. (&nar-signature-error
  128. (file file)
  129. (signature signature) (port port))))))))
  130. (signature-case (signature hash (current-acl))
  131. (valid-signature #t)
  132. (invalid-signature
  133. (raise (condition
  134. (&message (message "invalid signature"))
  135. (&nar-signature-error
  136. (file file) (signature signature) (port port)))))
  137. (hash-mismatch
  138. (raise (condition (&message (message "invalid hash"))
  139. (&nar-invalid-hash-error
  140. (port port) (file file)
  141. (signature signature)
  142. (expected (hash-data->bytevector
  143. (signature-signed-data signature)))
  144. (actual hash)))))
  145. (unauthorized-key
  146. (raise (condition (&message (message "unauthorized public key"))
  147. (&nar-signature-error
  148. (signature signature) (file file) (port port)))))
  149. (corrupt-signature
  150. (raise (condition
  151. (&message (message "corrupt signature data"))
  152. (&nar-signature-error
  153. (signature signature) (file file) (port port))))))))
  154. (define %export-magic
  155. ;; Number used to identify genuine file set archives.
  156. #x4558494e)
  157. (define port*
  158. ;; Keep that one around, for error conditions.
  159. port)
  160. (let-values (((port get-hash)
  161. (open-sha256-input-port port)))
  162. (with-temporary-store-file temp
  163. (restore-file port temp)
  164. (let ((magic (read-int port)))
  165. (unless (= magic %export-magic)
  166. (raise (condition
  167. (&message (message "corrupt file set archive"))
  168. (&nar-read-error
  169. (port port*) (file #f) (token #f))))))
  170. (let ((file (read-store-path port))
  171. (refs (read-store-path-list port))
  172. (deriver (read-string port))
  173. (hash (get-hash))
  174. (has-sig? (= 1 (read-int port))))
  175. (format log-port
  176. (G_ "importing file or directory '~a'...~%")
  177. file)
  178. ;; The signature may contain characters that are meant to be
  179. ;; interpreted as bytes in a 'char *', so read them as a ISO-8859-1.
  180. (let ((sig (and has-sig? (read-latin1-string port))))
  181. (when verify-signature?
  182. (if sig
  183. (begin
  184. (assert-valid-signature sig hash file)
  185. (format log-port
  186. (G_ "found valid signature for '~a'~%")
  187. file)
  188. (finalize-store-file temp file
  189. #:references refs
  190. #:deriver deriver
  191. #:lock? lock?))
  192. (raise (condition
  193. (&message (message "imported file lacks \
  194. a signature"))
  195. (&nar-signature-error
  196. (port port*) (file file) (signature #f))))))
  197. file)))))
  198. (define* (restore-file-set port
  199. #:key (verify-signature? #t) (lock? #t)
  200. (log-port (current-error-port)))
  201. "Restore the file set read from PORT to the store. The format of the data
  202. on PORT must be as created by 'export-paths'---i.e., a series of Nar-formatted
  203. archives with interspersed meta-data joining them together, possibly with a
  204. digital signature at the end. Log progress to LOG-PORT. Return the list of
  205. files restored.
  206. When LOCK? is #f, assume locks for the files to be restored are already held.
  207. This is the case when the daemon calls a build hook.
  208. Note that this procedure accesses the store directly, so it's only meant to be
  209. used by the daemon's build hooks since they cannot call back to the daemon
  210. while the locks are held."
  211. (define acl
  212. (current-acl))
  213. (let loop ((n (read-long-long port))
  214. (files '()))
  215. (case n
  216. ((0)
  217. (reverse files))
  218. ((1)
  219. (let ((file
  220. (restore-one-item port
  221. #:acl acl #:verify-signature? verify-signature?
  222. #:lock? lock? #:log-port log-port)))
  223. (loop (read-long-long port)
  224. (cons file files))))
  225. (else
  226. ;; Neither 0 nor 1.
  227. (raise (condition
  228. (&message (message "invalid inter-file archive mark"))
  229. (&nar-read-error
  230. (port port) (file #f) (token #f))))))))
  231. ;;; Local Variables:
  232. ;;; eval: (put 'with-temporary-store-file 'scheme-indent-function 1)
  233. ;;; End:
  234. ;;; nar.scm ends here