narinfo.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 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 not valid."
  194. (let ((hash (narinfo-sha256 narinfo))
  195. (signature (narinfo-signature narinfo))
  196. (uri (uri->string (first (narinfo-uris narinfo)))))
  197. (and hash signature
  198. (signature-case (signature hash acl)
  199. (valid-signature #t)
  200. (invalid-signature
  201. (when verbose?
  202. (format (current-error-port)
  203. "invalid signature for substitute at '~a'~%"
  204. uri))
  205. #f)
  206. (hash-mismatch
  207. (when verbose?
  208. (format (current-error-port)
  209. "hash mismatch for substitute at '~a'~%"
  210. uri))
  211. #f)
  212. (unauthorized-key
  213. (when verbose?
  214. (format (current-error-port)
  215. "substitute at '~a' is signed by an \
  216. unauthorized party~%"
  217. uri))
  218. #f)
  219. (corrupt-signature
  220. (when verbose?
  221. (format (current-error-port)
  222. "corrupt signature for substitute at '~a'~%"
  223. uri))
  224. #f)))))
  225. (define (write-narinfo narinfo port)
  226. "Write NARINFO to PORT."
  227. (put-bytevector port (string->utf8 (narinfo-contents narinfo))))
  228. (define (narinfo->string narinfo)
  229. "Return the external representation of NARINFO."
  230. (call-with-output-string (cut write-narinfo narinfo <>)))
  231. (define (string->narinfo str cache-uri)
  232. "Return the narinfo represented by STR. Assume CACHE-URI as the base URI of
  233. the cache STR originates form."
  234. (call-with-input-string str (cut read-narinfo <> cache-uri)))
  235. (define (equivalent-narinfo? narinfo1 narinfo2)
  236. "Return true if NARINFO1 and NARINFO2 are equivalent--i.e., if they describe
  237. the same store item. This ignores unnecessary metadata such as the Nar URL."
  238. (and (string=? (narinfo-hash narinfo1)
  239. (narinfo-hash narinfo2))
  240. ;; The following is not needed if all we want is to download a valid
  241. ;; nar, but it's necessary if we want valid narinfo.
  242. (string=? (narinfo-path narinfo1)
  243. (narinfo-path narinfo2))
  244. (equal? (narinfo-references narinfo1)
  245. (narinfo-references narinfo2))
  246. (= (narinfo-size narinfo1)
  247. (narinfo-size narinfo2))))
  248. (define %compression-methods
  249. ;; Known compression methods and a thunk to determine whether they're
  250. ;; supported. See 'decompressed-port' in (guix utils).
  251. `(("gzip" . ,(const #t))
  252. ("lzip" . ,(const #t))
  253. ("zstd" . ,(lambda ()
  254. (resolve-module '(zstd) #t #f #:ensure #f)))
  255. ("xz" . ,(const #t))
  256. ("bzip2" . ,(const #t))
  257. ("none" . ,(const #t))))
  258. (define (supported-compression? compression)
  259. "Return true if COMPRESSION, a string, denotes a supported compression
  260. method."
  261. (match (assoc-ref %compression-methods compression)
  262. (#f #f)
  263. (supported? (supported?))))
  264. (define (compresses-better? compression1 compression2)
  265. "Return true if COMPRESSION1 generally compresses better than COMPRESSION2;
  266. this is a rough approximation."
  267. (match compression1
  268. ("none" #f)
  269. ("gzip" (string=? compression2 "none"))
  270. ("lzip" #t)
  271. (_ (or (string=? compression2 "none")
  272. (string=? compression2 "gzip")))))
  273. (define (decompresses-faster? compression1 compression2)
  274. "Return true if COMPRESSION1 generally has a higher decompression throughput
  275. than COMPRESSION2."
  276. (match compression1
  277. ("none" #t)
  278. ("zstd" #t)
  279. ("gzip" (string=? compression2 "lzip"))
  280. (_ #f)))
  281. (define* (narinfo-best-uri narinfo #:key fast-decompression?)
  282. "Select the \"best\" URI to download NARINFO's nar, and return three values:
  283. the URI, its compression method (a string), and the compressed file size.
  284. When FAST-DECOMPRESSION? is true, prefer substitutes with faster
  285. decompression (typically zstd) rather than substitutes with a higher
  286. compression ratio (typically lzip)."
  287. (define choices
  288. (filter (match-lambda
  289. ((uri compression file-size)
  290. (supported-compression? compression)))
  291. (zip (narinfo-uris narinfo)
  292. (narinfo-compressions narinfo)
  293. (narinfo-file-sizes narinfo))))
  294. (define (file-size<? c1 c2)
  295. (match c1
  296. ((uri1 compression1 (? integer? file-size1))
  297. (match c2
  298. ((uri2 compression2 (? integer? file-size2))
  299. (< file-size1 file-size2))
  300. (_ #t)))
  301. ((uri compression1 #f)
  302. (match c2
  303. ((uri2 compression2 _)
  304. (compresses-better? compression1 compression2))))
  305. (_ #f))) ;we can't tell
  306. (define (speed<? c1 c2)
  307. (match c1
  308. ((uri1 compression1 . _)
  309. (match c2
  310. ((uri2 compression2 . _)
  311. (decompresses-faster? compression2 compression1))))))
  312. (match (sort choices (if fast-decompression? (negate speed<?) file-size<?))
  313. (((uri compression file-size) _ ...)
  314. (values uri compression file-size))))