nar.scm 11 KB

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