narinfo.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013-2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
  4. ;;; Copyright © 2018 Kyle Meyer <kyle@kyleam.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix narinfo)
  21. #:use-module (guix pki)
  22. #:use-module (guix i18n)
  23. #:use-module (guix base32)
  24. #:use-module (guix base64)
  25. #:use-module (guix records)
  26. #:use-module (guix diagnostics)
  27. #:use-module (gcrypt hash)
  28. #:use-module (gcrypt pk-crypto)
  29. #:use-module (rnrs bytevectors)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-9)
  32. #:use-module (srfi srfi-26)
  33. #:use-module (ice-9 match)
  34. #:use-module (ice-9 binary-ports)
  35. #:use-module (web uri)
  36. #:export (narinfo-signature->canonical-sexp
  37. narinfo?
  38. narinfo-path
  39. narinfo-uris
  40. narinfo-uri-base
  41. narinfo-compressions
  42. narinfo-file-hashes
  43. narinfo-file-sizes
  44. narinfo-hash
  45. narinfo-size
  46. narinfo-references
  47. narinfo-deriver
  48. narinfo-system
  49. narinfo-signature
  50. narinfo-contents
  51. narinfo-hash-algorithm+value
  52. narinfo-hash->sha256
  53. narinfo-best-uri
  54. valid-narinfo?
  55. read-narinfo
  56. write-narinfo
  57. string->narinfo
  58. narinfo->string
  59. equivalent-narinfo?))
  60. (define-record-type <narinfo>
  61. (%make-narinfo path uri-base uris compressions file-sizes file-hashes
  62. nar-hash nar-size references deriver system
  63. signature contents)
  64. narinfo?
  65. (path narinfo-path)
  66. (uri-base narinfo-uri-base) ;URI of the cache it originates from
  67. (uris narinfo-uris) ;list of strings
  68. (compressions narinfo-compressions) ;list of strings
  69. (file-sizes narinfo-file-sizes) ;list of (integers | #f)
  70. (file-hashes narinfo-file-hashes)
  71. (nar-hash narinfo-hash)
  72. (nar-size narinfo-size)
  73. (references narinfo-references)
  74. (deriver narinfo-deriver)
  75. (system narinfo-system)
  76. (signature narinfo-signature) ; canonical sexp
  77. ;; The original contents of a narinfo file. This field is needed because we
  78. ;; want to preserve the exact textual representation for verification purposes.
  79. ;; See <https://lists.gnu.org/archive/html/guix-devel/2014-02/msg00340.html>
  80. ;; for more information.
  81. (contents narinfo-contents))
  82. (define (narinfo-hash-algorithm+value narinfo)
  83. "Return two values: the hash algorithm used by NARINFO and its value as a
  84. bytevector."
  85. (match (string-tokenize (narinfo-hash narinfo)
  86. (char-set-complement (char-set #\:)))
  87. ((algorithm base32)
  88. (values (lookup-hash-algorithm (string->symbol algorithm))
  89. (nix-base32-string->bytevector base32)))
  90. (_
  91. (raise (formatted-message
  92. (G_ "invalid narinfo hash: ~s") (narinfo-hash narinfo))))))
  93. (define (narinfo-hash->sha256 hash)
  94. "If the string HASH denotes a sha256 hash, return it as a bytevector.
  95. Otherwise return #f."
  96. (and (string-prefix? "sha256:" hash)
  97. (nix-base32-string->bytevector (string-drop hash 7))))
  98. (define (narinfo-signature->canonical-sexp str)
  99. "Return the value of a narinfo's 'Signature' field as a canonical sexp."
  100. (match (string-split str #\;)
  101. ((version host-name sig)
  102. (let ((maybe-number (string->number version)))
  103. (cond ((not (number? maybe-number))
  104. (leave (G_ "signature version must be a number: ~s~%")
  105. version))
  106. ;; Currently, there are no other versions.
  107. ((not (= 1 maybe-number))
  108. (leave (G_ "unsupported signature version: ~a~%")
  109. maybe-number))
  110. (else
  111. (let ((signature (utf8->string (base64-decode sig))))
  112. (catch 'gcry-error
  113. (lambda ()
  114. (string->canonical-sexp signature))
  115. (lambda (key proc err)
  116. (leave (G_ "signature is not a valid \
  117. s-expression: ~s~%")
  118. signature))))))))
  119. (x
  120. (leave (G_ "invalid format of the signature field: ~a~%") x))))
  121. (define (narinfo-maker str cache-url)
  122. "Return a narinfo constructor for narinfos originating from CACHE-URL. STR
  123. must contain the original contents of a narinfo file."
  124. (lambda (path urls compressions file-hashes file-sizes
  125. nar-hash nar-size references deriver system
  126. signature)
  127. "Return a new <narinfo> object."
  128. (define len (length urls))
  129. (%make-narinfo path cache-url
  130. ;; Handle the case where URL is a relative URL.
  131. (map (lambda (url)
  132. (or (string->uri url)
  133. (string->uri
  134. (if (string-suffix? "/" cache-url)
  135. (string-append cache-url url)
  136. (string-append cache-url "/" url)))))
  137. urls)
  138. compressions
  139. (match file-sizes
  140. (() (make-list len #f))
  141. ((lst ...) (map string->number lst)))
  142. (match file-hashes
  143. (() (make-list len #f))
  144. ((lst ...) (map string->number lst)))
  145. nar-hash
  146. (and=> nar-size string->number)
  147. (string-tokenize references)
  148. (match deriver
  149. ((or #f "") #f)
  150. (_ deriver))
  151. system
  152. (false-if-exception
  153. (and=> signature narinfo-signature->canonical-sexp))
  154. str)))
  155. (define fields->alist
  156. ;; The narinfo format is really just like recutils.
  157. recutils->alist)
  158. (define* (read-narinfo port #:optional url
  159. #:key size)
  160. "Read a narinfo from PORT. If URL is true, it must be a string used to
  161. build full URIs from relative URIs found while reading PORT. When SIZE is
  162. true, read at most SIZE bytes from PORT; otherwise, read as much as possible.
  163. No authentication and authorization checks are performed here!"
  164. (let ((str (utf8->string (if size
  165. (get-bytevector-n port size)
  166. (get-bytevector-all port)))))
  167. (alist->record (call-with-input-string str fields->alist)
  168. (narinfo-maker str url)
  169. '("StorePath" "URL" "Compression"
  170. "FileHash" "FileSize" "NarHash" "NarSize"
  171. "References" "Deriver" "System"
  172. "Signature")
  173. '("URL" "Compression" "FileSize" "FileHash"))))
  174. (define (narinfo-sha256 narinfo)
  175. "Return the sha256 hash of NARINFO as a bytevector, or #f if NARINFO lacks a
  176. 'Signature' field."
  177. (define %mandatory-fields
  178. ;; List of fields that must be signed. If they are not signed, the
  179. ;; narinfo is considered unsigned.
  180. '("StorePath" "NarHash" "References"))
  181. (let ((contents (narinfo-contents narinfo)))
  182. (match (string-contains contents "Signature:")
  183. (#f #f)
  184. (index
  185. (let* ((above-signature (string-take contents index))
  186. (signed-fields (match (call-with-input-string above-signature
  187. fields->alist)
  188. (((fields . values) ...) fields))))
  189. (and (every (cut member <> signed-fields) %mandatory-fields)
  190. (sha256 (string->utf8 above-signature))))))))
  191. (define* (valid-narinfo? narinfo #:optional (acl (current-acl))
  192. #:key verbose?)
  193. "Return #t if NARINFO's signature is valid and made by one of the keys in
  194. ACL."
  195. (let ((hash (narinfo-sha256 narinfo))
  196. (signature (narinfo-signature narinfo))
  197. (uri (uri->string (first (narinfo-uris narinfo)))))
  198. (and hash signature
  199. (signature-case (signature hash acl)
  200. (valid-signature #t)
  201. (invalid-signature
  202. (when verbose?
  203. (format (current-error-port)
  204. "invalid signature for substitute at '~a'~%"
  205. uri))
  206. #f)
  207. (hash-mismatch
  208. (when verbose?
  209. (format (current-error-port)
  210. "hash mismatch for substitute at '~a'~%"
  211. uri))
  212. #f)
  213. (unauthorized-key
  214. (when verbose?
  215. (format (current-error-port)
  216. "substitute at '~a' is signed by an \
  217. unauthorized party~%"
  218. uri))
  219. #f)
  220. (corrupt-signature
  221. (when verbose?
  222. (format (current-error-port)
  223. "corrupt signature for substitute at '~a'~%"
  224. uri))
  225. #f)))))
  226. (define (write-narinfo narinfo port)
  227. "Write NARINFO to PORT."
  228. (put-bytevector port (string->utf8 (narinfo-contents narinfo))))
  229. (define (narinfo->string narinfo)
  230. "Return the external representation of NARINFO."
  231. (call-with-output-string (cut write-narinfo narinfo <>)))
  232. (define (string->narinfo str cache-uri)
  233. "Return the narinfo represented by STR. Assume CACHE-URI as the base URI of
  234. the cache STR originates form."
  235. (call-with-input-string str (cut read-narinfo <> cache-uri)))
  236. (define (equivalent-narinfo? narinfo1 narinfo2)
  237. "Return true if NARINFO1 and NARINFO2 are equivalent--i.e., if they describe
  238. the same store item. This ignores unnecessary metadata such as the Nar URL."
  239. (and (string=? (narinfo-hash narinfo1)
  240. (narinfo-hash narinfo2))
  241. ;; The following is not needed if all we want is to download a valid
  242. ;; nar, but it's necessary if we want valid narinfo.
  243. (string=? (narinfo-path narinfo1)
  244. (narinfo-path narinfo2))
  245. (equal? (narinfo-references narinfo1)
  246. (narinfo-references narinfo2))
  247. (= (narinfo-size narinfo1)
  248. (narinfo-size narinfo2))))
  249. (define %compression-methods
  250. ;; Known compression methods and a thunk to determine whether they're
  251. ;; supported. See 'decompressed-port' in (guix utils).
  252. `(("gzip" . ,(const #t))
  253. ("lzip" . ,(const #t))
  254. ("zstd" . ,(lambda ()
  255. (resolve-module '(zstd) #t #f #:ensure #f)))
  256. ("xz" . ,(const #t))
  257. ("bzip2" . ,(const #t))
  258. ("none" . ,(const #t))))
  259. (define (supported-compression? compression)
  260. "Return true if COMPRESSION, a string, denotes a supported compression
  261. method."
  262. (match (assoc-ref %compression-methods compression)
  263. (#f #f)
  264. (supported? (supported?))))
  265. (define (compresses-better? compression1 compression2)
  266. "Return true if COMPRESSION1 generally compresses better than COMPRESSION2;
  267. this is a rough approximation."
  268. (match compression1
  269. ("none" #f)
  270. ("gzip" (string=? compression2 "none"))
  271. ("lzip" #t)
  272. (_ (or (string=? compression2 "none")
  273. (string=? compression2 "gzip")))))
  274. (define (decompresses-faster? compression1 compression2)
  275. "Return true if COMPRESSION1 generally has a higher decompression throughput
  276. than COMPRESSION2."
  277. (match compression1
  278. ("none" #t)
  279. ("zstd" #t)
  280. ("gzip" (string=? compression2 "lzip"))
  281. (_ #f)))
  282. (define* (narinfo-best-uri narinfo #:key fast-decompression?)
  283. "Select the \"best\" URI to download NARINFO's nar, and return three values:
  284. the URI, its compression method (a string), and the compressed file size.
  285. When FAST-DECOMPRESSION? is true, prefer substitutes with faster
  286. decompression (typically zstd) rather than substitutes with a higher
  287. compression ratio (typically lzip)."
  288. (define choices
  289. (filter (match-lambda
  290. ((uri compression file-size)
  291. (supported-compression? compression)))
  292. (zip (narinfo-uris narinfo)
  293. (narinfo-compressions narinfo)
  294. (narinfo-file-sizes narinfo))))
  295. (define (file-size<? c1 c2)
  296. (match c1
  297. ((uri1 compression1 (? integer? file-size1))
  298. (match c2
  299. ((uri2 compression2 (? integer? file-size2))
  300. (< file-size1 file-size2))
  301. (_ #t)))
  302. ((uri compression1 #f)
  303. (match c2
  304. ((uri2 compression2 _)
  305. (compresses-better? compression1 compression2))))
  306. (_ #f))) ;we can't tell
  307. (define (speed<? c1 c2)
  308. (match c1
  309. ((uri1 compression1 . _)
  310. (match c2
  311. ((uri2 compression2 . _)
  312. (decompresses-faster? compression2 compression1))))))
  313. (match (sort choices (if fast-decompression? (negate speed<?) file-size<?))
  314. (((uri compression file-size) _ ...)
  315. (values uri compression file-size))))