nar.scm 12 KB

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