summaryrefslogtreecommitdiff
path: root/guix
diff options
context:
space:
mode:
Diffstat (limited to 'guix')
-rw-r--r--guix/scripts/authenticate.scm98
-rw-r--r--guix/store.scm79
2 files changed, 173 insertions, 4 deletions
diff --git a/guix/scripts/authenticate.scm b/guix/scripts/authenticate.scm
new file mode 100644
index 0000000000..cbafed79d0
--- /dev/null
+++ b/guix/scripts/authenticate.scm
@@ -0,0 +1,98 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2013 Ludovic Courtès <ludo@gnu.org>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (guix scripts authenticate)
+ #:use-module (guix config)
+ #:use-module (guix utils)
+ #:use-module (guix pk-crypto)
+ #:use-module (guix ui)
+ #:use-module (rnrs io ports)
+ #:use-module (ice-9 match)
+ #:export (guix-authenticate))
+
+;;; Commentary:
+;;;
+;;; This program is used internally by the daemon to sign exported archive
+;;; (the 'export-paths' RPC), and to authenticate imported archives (the
+;;; 'import-paths' RPC.)
+;;;
+;;; Code:
+
+(define (read-gcry-sexp file)
+ "Read a gcrypt sexp from FILE and return it."
+ (call-with-input-file file
+ (compose string->gcry-sexp get-string-all)))
+
+(define (read-hash-data file)
+ "Read sha256 hash data from FILE and return it as a gcrypt sexp."
+ (let* ((hex (call-with-input-file file get-string-all))
+ (bv (base16-string->bytevector (string-trim-both hex))))
+ (bytevector->hash-data bv)))
+
+
+;;;
+;;; Entry point with 'openssl'-compatible interface. We support this
+;;; interface because that's what the daemon expects, and we want to leave it
+;;; unmodified currently.
+;;;
+
+(define (guix-authenticate . args)
+ (match args
+ (("rsautl" "-sign" "-inkey" key "-in" hash-file)
+ ;; Sign the hash in HASH-FILE with KEY, and return an sexp that includes
+ ;; both the hash and the actual signature.
+ (let* ((secret-key (read-gcry-sexp key))
+ (data (read-hash-data hash-file)))
+ (format #t
+ "(guix-signature ~a (payload ~a))"
+ (gcry-sexp->string (sign data secret-key))
+ (gcry-sexp->string data))
+ #t))
+ (("rsautl" "-verify" "-inkey" key "-pubin" "-in" signature-file)
+ ;; Read the signature as produced above, check it against KEY, and print
+ ;; the signed data to stdout upon success.
+ (let* ((public-key (read-gcry-sexp key))
+ (sig+data (read-gcry-sexp signature-file))
+ (data (find-sexp-token sig+data 'payload))
+ (signature (find-sexp-token sig+data 'sig-val)))
+ (if (and data signature)
+ (if (verify signature data public-key)
+ (begin
+ (display (bytevector->base16-string
+ (hash-data->bytevector data)))
+ #t) ; success
+ (begin
+ (format (current-error-port)
+ "error: invalid signature: ~a~%"
+ (gcry-sexp->string signature))
+ (exit 1)))
+ (begin
+ (format (current-error-port)
+ "error: corrupt signature data: ~a~%"
+ (gcry-sexp->string sig+data))
+ (exit 1)))))
+ (("--help")
+ (display (_ "Usage: guix authenticate OPTION...
+Sign or verify the signature on the given file. This tool is meant to
+be used internally by 'guix-daemon'.\n")))
+ (("--version")
+ (show-version-and-exit "guix authenticate"))
+ (else
+ (leave (_ "wrong arguments")))))
+
+;;; authenticate.scm ends here
diff --git a/guix/store.scm b/guix/store.scm
index 08b0671b29..4ceca0daa2 100644
--- a/guix/store.scm
+++ b/guix/store.scm
@@ -80,6 +80,8 @@
dead-paths
collect-garbage
delete-paths
+ import-paths
+ export-paths
current-build-output-port
@@ -323,7 +325,30 @@ operate, should the disk become full. Return a server object."
;; The port where build output is sent.
(make-parameter (current-error-port)))
-(define (process-stderr server)
+(define* (dump-port in out
+ #:optional len
+ #:key (buffer-size 16384))
+ "Read LEN bytes from IN (or as much as possible if LEN is #f) and write it
+to OUT, using chunks of BUFFER-SIZE bytes."
+ (define buffer
+ (make-bytevector buffer-size))
+
+ (let loop ((total 0)
+ (bytes (get-bytevector-n! in buffer 0
+ (if len
+ (min len buffer-size)
+ buffer-size))))
+ (or (eof-object? bytes)
+ (and len (= total len))
+ (let ((total (+ total bytes)))
+ (put-bytevector out buffer 0 bytes)
+ (loop total
+ (get-bytevector-n! in buffer 0
+ (if len
+ (min (- len total) buffer-size)
+ buffer-size)))))))
+
+(define* (process-stderr server #:optional user-port)
"Read standard output and standard error from SERVER, writing it to
CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and
#f otherwise; in the latter case, the caller should call `process-stderr'
@@ -344,17 +369,30 @@ encoding conversion errors."
(let ((k (read-int p)))
(cond ((= k %stderr-write)
- (read-latin1-string p)
+ ;; Write a byte stream to USER-PORT.
+ (let* ((len (read-int p))
+ (m (modulo len 8)))
+ (dump-port p user-port len)
+ (unless (zero? m)
+ ;; Consume padding, as for strings.
+ (get-bytevector-n p (- 8 m))))
#f)
((= k %stderr-read)
- (let ((len (read-int p)))
- (read-latin1-string p) ; FIXME: what to do?
+ ;; Read a byte stream from USER-PORT.
+ (let* ((max-len (read-int p))
+ (data (get-bytevector-n user-port max-len))
+ (len (bytevector-length data)))
+ (write-int len p)
+ (put-bytevector p data)
+ (write-padding len p)
#f))
((= k %stderr-next)
+ ;; Log a string.
(let ((s (read-latin1-string p)))
(display s (current-build-output-port))
#f))
((= k %stderr-error)
+ ;; Report an error.
(let ((error (read-latin1-string p))
;; Currently the daemon fails to send a status code for early
;; errors like DB schema version mismatches, so check for EOF.
@@ -624,6 +662,39 @@ MIN-FREED bytes have been collected. Return the paths that were
collected, and the number of bytes freed."
(run-gc server (gc-action delete-specific) paths min-freed))
+(define (import-paths server port)
+ "Import the set of store paths read from PORT into SERVER's store. An error
+is raised if the set of paths read from PORT is not signed (as per
+'export-path #:sign? #t'.) Return the list of store paths imported."
+ (let ((s (nix-server-socket server)))
+ (write-int (operation-id import-paths) s)
+ (let loop ((done? (process-stderr server port)))
+ (or done? (loop (process-stderr server port))))
+ (read-store-path-list s)))
+
+(define* (export-path server path port #:key (sign? #t))
+ "Export PATH to PORT. When SIGN? is true, sign it."
+ (let ((s (nix-server-socket server)))
+ (write-int (operation-id export-path) s)
+ (write-store-path path s)
+ (write-arg boolean sign? s)
+ (let loop ((done? (process-stderr server port)))
+ (or done? (loop (process-stderr server port))))
+ (= 1 (read-int s))))
+
+(define* (export-paths server paths port #:key (sign? #t))
+ "Export the store paths listed in PATHS to PORT, signing them if SIGN?
+is true."
+ (let ((s (nix-server-socket server)))
+ (let loop ((paths paths))
+ (match paths
+ (()
+ (write-int 0 port))
+ ((head tail ...)
+ (write-int 1 port)
+ (and (export-path server head port #:sign? sign?)
+ (loop tail)))))))
+
;;;
;;; Store paths.