]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/restore.c
- Add support for testing the availability of a PEM-encoded private key
[bacula/bacula] / bacula / src / filed / restore.c
index d9a27846b095fea78aa951c53026372f3251c205..f83ea81e41cbd6a4c6deb2c3ee7a275468505ec9 100644 (file)
@@ -11,7 +11,7 @@
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
-   version 2 as ammended with additional clauses defined in the
+   version 2 as amended with additional clauses defined in the
    file LICENSE in the main source directory.
 
    This program is distributed in the hope that it will be useful,
@@ -35,6 +35,8 @@ static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
 #ifdef HAVE_LIBZ
 static const char *zlib_strerror(int stat);
 #endif
+
+int verify_signature(JCR *jcr, SIGNATURE *sig);
 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
       uint64_t *addr, int flags);
 
@@ -52,7 +54,7 @@ int bclose_chksize(JCR *jcr, BFILE *bfd, off_t osize)
    fsize = blseek(bfd, 0, SEEK_CUR);
    bclose(bfd);                              /* first close file */
    if (fsize > 0 && fsize != osize) {
-      Jmsg3(jcr, M_ERROR, 0, _("Size of data or stream of %s not correct. Original %s, restored %s.\n"),
+      Qmsg3(jcr, M_ERROR, 0, _("Size of data or stream of %s not correct. Original %s, restored %s.\n"),
             jcr->last_fname, edit_uint64(osize, ec1),
             edit_uint64(fsize, ec2));
       return -1;
@@ -80,6 +82,8 @@ void do_restore(JCR *jcr)
    BFILE altbfd;                      /* Alternative data stream */
    uint64_t alt_addr = 0;             /* Write address for alternative stream */
    intmax_t alt_size = 0;             /* Size of alternate stream */
+   SIGNATURE *sig = NULL;             /* Cryptographic signature (if any) for file */
+   CRYPTO_SESSION *cs = NULL;         /* Cryptographic session data (if any) for file */
    int flags;                         /* Options for extract_data() */
    int stat;
    ATTR *attr;
@@ -132,11 +136,13 @@ void do_restore(JCR *jcr)
     *   1. Stream record header
     *   2. Stream data
     *        a. Attributes (Unix or Win32)
-    *    or  b. File data for the file
-    *    or  c. Alternate data stream (e.g. Resource Fork)
-    *    or  d. Finder info
-    *    or  e. ACLs
-    *    or  f. Possibly MD5 or SHA1 record
+    *        b. Possibly stream encryption session data (e.g., symmetric session key)
+    *    or  c. File data for the file
+    *    or  d. Alternate data stream (e.g. Resource Fork)
+    *    or  e. Finder info
+    *    or  f. ACLs
+    *    or  g. Possibly a cryptographic signature
+    *    or  h. Possibly MD5 or SHA1 record
     *   3. Repeat step 1
     *
     * NOTE: We keep track of two bacula file descriptors:
@@ -194,7 +200,7 @@ void do_restore(JCR *jcr)
          Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
          /*
           * If extracting, it was from previous stream, so
-          * close the output file.
+          * close the output file and validate the signature.
           */
          if (extract) {
             if (size > 0 && !is_bopen(&bfd)) {
@@ -202,6 +208,33 @@ void do_restore(JCR *jcr)
             }
             set_attributes(jcr, attr, &bfd);
             extract = false;
+
+            /* Verify the cryptographic signature, if any */
+            if (jcr->pki_sign) {
+               if (sig) {
+                  if (!verify_signature(jcr, sig)) {
+                     // TODO landonf: Better signature failure handling.
+                     // The failure is reported to the director in verify_signature() ...
+                     Dmsg1(100, "Bad signature on %s\n", jcr->last_fname);
+                  } else {
+                     Dmsg1(100, "Signature good on %s\n", jcr->last_fname);
+                  }
+               } else {
+                  Jmsg1(jcr, M_ERROR, 0, _("Missing cryptographic signature for %s\n"), jcr->last_fname);
+               }
+            }
+
+            /* Free Signature */
+            if (sig) {
+               crypto_sign_free(sig);
+               sig = NULL;
+            }
+
+            if (cs) {
+               crypto_session_free(cs);
+               cs = NULL;
+            }
+
             Dmsg0(30, "Stop extracting.\n");
          } else if (is_bopen(&bfd)) {
             Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
@@ -226,7 +259,7 @@ void do_restore(JCR *jcr)
 
          attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
 
-         if (!is_stream_supported(attr->data_stream)) {
+         if (!is_restore_stream_supported(attr->data_stream)) {
             if (!non_support_data++) {
                Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
                   stream_to_ascii(attr->data_stream));
@@ -273,6 +306,27 @@ void do_restore(JCR *jcr)
          break;
 
       /* Data stream */
+      case STREAM_ENCRYPTED_SESSION_DATA:
+         Dmsg1(30, "Stream=Encrypted Session Data, size: %d\n", sd->msglen);
+         /* Save session keys . */
+         switch(crypto_session_decode(sd->msg, (size_t) sd->msglen, jcr->pki_recipients, &cs)) {
+         case CRYPTO_ERROR_NONE:
+            /* Success */
+            break;
+         case CRYPTO_ERROR_NORECIPIENT:
+            Jmsg(jcr, M_ERROR, 0, _("Missing private key required to decrypt encrypted backup data."));
+            break;
+         case CRYPTO_ERROR_DECRYPTION:
+            Jmsg(jcr, M_ERROR, 0, _("Decrypt of the session key failed."));
+            break;
+         default:
+            /* Shouldn't happen */
+            Jmsg(jcr, M_ERROR, 0, _("An error occured while decoding encrypted session data stream."));
+            break;
+         }
+
+         break;
+
       case STREAM_FILE_DATA:
       case STREAM_SPARSE_DATA:
       case STREAM_WIN32_DATA:
@@ -281,7 +335,8 @@ void do_restore(JCR *jcr)
       case STREAM_WIN32_GZIP_DATA:
          /* Force an expected, consistent stream type here */
          if (extract && (prev_stream == stream || prev_stream == STREAM_UNIX_ATTRIBUTES
-                  || prev_stream == STREAM_UNIX_ATTRIBUTES_EX)) {
+                  || prev_stream == STREAM_UNIX_ATTRIBUTES_EX
+                  || prev_stream == STREAM_ENCRYPTED_SESSION_DATA)) {
             flags = 0;
             if (stream == STREAM_SPARSE_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
                flags |= FO_SPARSE;
@@ -290,6 +345,12 @@ void do_restore(JCR *jcr)
                   || stream == STREAM_WIN32_GZIP_DATA) {
                flags |= FO_GZIP;
             }
+
+            if (is_win32_stream(stream) && !have_win32_api()) {
+               set_portable_backup(&bfd);
+               flags |= FO_WIN32DECOMP;    /* "decompose" BackupWrite data */
+            }
+
             if (extract_data(jcr, &bfd, sd->msg, sd->msglen, &fileAddr, flags) < 0) {
                extract = false;
                bclose(&bfd);
@@ -305,7 +366,7 @@ void do_restore(JCR *jcr)
          if (extract) {
             if (prev_stream != stream) {
                if (bopen_rsrc(&altbfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) {
-                  Jmsg(jcr, M_ERROR, 0, _("     Cannot open resource fork for %s\n"), jcr->last_fname);
+                  Jmsg(jcr, M_ERROR, 0, _("     Cannot open resource fork for %s.\n"), jcr->last_fname);
                   extract = false;
                   continue;
                }
@@ -344,7 +405,7 @@ void do_restore(JCR *jcr)
          pm_strcpy(jcr->acl_text, sd->msg);
          Dmsg2(400, "Restoring ACL type 0x%2x <%s>\n", BACL_TYPE_ACCESS, jcr->acl_text);
          if (bacl_set(jcr, BACL_TYPE_ACCESS) != 0) {
-               Jmsg1(jcr, M_WARNING, 0, "Can't restore ACL of %s\n", jcr->last_fname);
+               Qmsg1(jcr, M_WARNING, 0, _("Can't restore ACL of %s\n"), jcr->last_fname);
          }
 #else 
          non_support_acl++;
@@ -356,15 +417,22 @@ void do_restore(JCR *jcr)
          pm_strcpy(jcr->acl_text, sd->msg);
          Dmsg2(400, "Restoring ACL type 0x%2x <%s>\n", BACL_TYPE_DEFAULT, jcr->acl_text);
          if (bacl_set(jcr, BACL_TYPE_DEFAULT) != 0) {
-               Jmsg1(jcr, M_WARNING, 0, "Can't restore default ACL of %s\n", jcr->last_fname);
+               Qmsg1(jcr, M_WARNING, 0, _("Can't restore default ACL of %s\n"), jcr->last_fname);
          }
 #else 
          non_support_acl++;
 #endif
          break;
 
-      case STREAM_MD5_SIGNATURE:
-      case STREAM_SHA1_SIGNATURE:
+      case STREAM_SIGNED_DIGEST:
+         /* Save signature. */
+         sig = crypto_sign_decode(sd->msg, (size_t) sd->msglen);
+         break;
+
+      case STREAM_MD5_DIGEST:
+      case STREAM_SHA1_DIGEST:
+      case STREAM_SHA256_DIGEST:
+      case STREAM_SHA512_DIGEST:
          break;
 
       case STREAM_PROGRAM_NAMES:
@@ -447,27 +515,87 @@ ok_out:
 static const char *zlib_strerror(int stat)
 {
    if (stat >= 0) {
-      return "None";
+      return _("None");
    }
    switch (stat) {
    case Z_ERRNO:
-      return "Zlib errno";
+      return _("Zlib errno");
    case Z_STREAM_ERROR:
-      return "Zlib stream error";
+      return _("Zlib stream error");
    case Z_DATA_ERROR:
-      return "Zlib data error";
+      return _("Zlib data error");
    case Z_MEM_ERROR:
-      return "Zlib memory error";
+      return _("Zlib memory error");
    case Z_BUF_ERROR:
-      return "Zlib buffer error";
+      return _("Zlib buffer error");
    case Z_VERSION_ERROR:
-      return "Zlib version error";
+      return _("Zlib version error");
    default:
-      return "*none*";
+      return _("*none*");
    }
 }
 #endif
 
+static int do_file_digest(FF_PKT *ff_pkt, void *pkt, bool top_level) {
+   JCR *jcr = (JCR *) pkt;
+   return (digest_file(jcr, ff_pkt, jcr->digest));
+}
+
+/*
+ * Verify the signature for the last restored file
+ * Return value is either true (signature correct)
+ * or false (signature could not be verified).
+ */
+int verify_signature(JCR *jcr, SIGNATURE *sig)
+{
+   X509_KEYPAIR *keypair;
+   DIGEST *digest = NULL;
+   crypto_error_t err;
+
+
+   /* Iterate through the trusted signers */
+   foreach_alist(keypair, jcr->pki_signers) {
+      err = crypto_sign_get_digest(sig, jcr->pki_keypair, &digest);
+
+      switch (err) {
+      case CRYPTO_ERROR_NONE:
+         /* Signature found, digest allocated */
+         jcr->digest = digest;
+
+         /* Checksum the entire file */
+         if (find_one_file(jcr, jcr->ff, do_file_digest, jcr, jcr->last_fname, (dev_t)-1, 1) != 0) {
+            Qmsg(jcr, M_ERROR, 0, _("Signature validation failed for %s: \n"), jcr->last_fname);
+            return false;
+         }
+
+         /* Verify the signature */
+         if ((err = crypto_sign_verify(sig, keypair, digest)) != CRYPTO_ERROR_NONE) {
+            Qmsg2(jcr, M_ERROR, 0, _("Signature validation failed for %s: %s\n"), jcr->last_fname, crypto_strerror(err));
+            crypto_digest_free(digest);
+            return false;
+         }
+
+         /* Valid signature */
+         crypto_digest_free(digest);
+         return true;
+
+      case CRYPTO_ERROR_NOSIGNER:
+         /* Signature not found, try again */
+         continue;
+      default:
+         /* Something strange happened (that shouldn't happen!)... */
+         Qmsg2(jcr, M_ERROR, 0, _("Signature validation failed for %s: %s\n"), jcr->last_fname, crypto_strerror(err));
+         if (digest) {
+            crypto_digest_free(digest);
+         }
+         return false;
+      }
+   }
+
+   /* Unreachable */
+   return false;
+}
+
 /*
  * In the context of jcr, write data to bfd.
  * We write buflen bytes in buf at addr. addr is updated in place.
@@ -519,7 +647,7 @@ int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
       Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
       if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
                   (const Byte *)wbuf, (uLong)rsize)) != Z_OK) {
-         Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
+         Qmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
                jcr->last_fname, zlib_strerror(stat));
          return -1;
       }
@@ -527,14 +655,21 @@ int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
       wsize = compress_len;
       Dmsg2(100, "Write uncompressed %d bytes, total before write=%s\n", compress_len, edit_uint64(jcr->JobBytes, ec1));
 #else
-      Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
+      Qmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
       return -1;
 #endif
    } else {
       Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
    }
 
-   if (bwrite(bfd, wbuf, wsize) != (ssize_t)wsize) {
+   if (flags & FO_WIN32DECOMP) {
+      if (!processWin32BackupAPIBlock(bfd, wbuf, wsize)) {
+         berrno be;
+         Jmsg2(jcr, M_ERROR, 0, _("Write error in Win32 Block Decomposition on %s: %s\n"), 
+               jcr->last_fname, be.strerror(bfd->berrno));
+         return -1;
+      }
+   } else if (bwrite(bfd, wbuf, wsize) != (ssize_t)wsize) {
       berrno be;
       Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), 
             jcr->last_fname, be.strerror(bfd->berrno));