]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
5f9238609fba2d1ded75d94b0d829cef675f768a
[bacula/bacula] / bacula / src / filed / restore.c
1 /*
2  *  Bacula File Daemon  restore.c Restorefiles.
3  *
4  *    Kern Sibbald, November MM
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000-2006 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License
14    version 2 as amended with additional clauses defined in the
15    file LICENSE in the main source directory.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
20    the file LICENSE for additional details.
21
22  */
23
24 #include "bacula.h"
25 #include "filed.h"
26
27 #ifdef HAVE_DARWIN_OS
28 #include <sys/attr.h>
29 #endif
30
31 #if defined(HAVE_CRYPTO)
32 const bool have_crypto = true;
33 #else
34 const bool have_crypto = false;
35 #endif
36
37 /* Data received from Storage Daemon */
38 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
39
40 /* Forward referenced functions */
41 #if   defined(HAVE_LIBZ)
42 static const char *zlib_strerror(int stat);
43 const bool have_libz = true;
44 #else
45 const bool have_libz = false;
46 #endif
47
48 int verify_signature(JCR *jcr, SIGNATURE *sig);
49 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
50       uint64_t *addr, int flags, CIPHER_CONTEXT *cipher, uint32_t cipher_block_size);
51 bool flush_cipher(JCR *jcr, BFILE *bfd, int flags, CIPHER_CONTEXT *cipher, uint32_t cipher_block_size);
52
53 #define RETRY 10                      /* retry wait time */
54
55 /*
56  * Close a bfd check that we are at the expected file offset.
57  * Makes some code in set_attributes().
58  */
59 int bclose_chksize(JCR *jcr, BFILE *bfd, off_t osize)
60 {
61    char ec1[50], ec2[50];
62    off_t fsize;
63
64    fsize = blseek(bfd, 0, SEEK_CUR);
65    bclose(bfd);                              /* first close file */
66    if (fsize > 0 && fsize != osize) {
67       Qmsg3(jcr, M_ERROR, 0, _("Size of data or stream of %s not correct. Original %s, restored %s.\n"),
68             jcr->last_fname, edit_uint64(osize, ec1),
69             edit_uint64(fsize, ec2));
70       return -1;
71    }
72    return 0;
73 }
74
75 /*
76  * Restore the requested files.
77  *
78  */
79 void do_restore(JCR *jcr)
80 {
81    BSOCK *sd;
82    int32_t stream = 0;
83    int32_t prev_stream;
84    uint32_t VolSessionId, VolSessionTime;
85    bool extract = false;
86    int32_t file_index;
87    char ec1[50];                      /* Buffer printing huge values */
88
89    BFILE bfd;                         /* File content */
90    uint64_t fileAddr = 0;             /* file write address */
91    uint32_t size;                     /* Size of file */
92    BFILE altbfd;                      /* Alternative data stream */
93    uint64_t alt_addr = 0;             /* Write address for alternative stream */
94    intmax_t alt_size = 0;             /* Size of alternate stream */
95    SIGNATURE *sig = NULL;             /* Cryptographic signature (if any) for file */
96    CRYPTO_SESSION *cs = NULL;         /* Cryptographic session data (if any) for file */
97    CIPHER_CONTEXT *cipher_ctx = NULL; /* Cryptographic cipher context (if any) for file */
98    uint32_t cipher_block_size = 0;    /* Cryptographic algorithm block size for file */
99    int flags = 0;                     /* Options for extract_data() */
100    int stat;
101    ATTR *attr;
102
103    /* The following variables keep track of "known unknowns" */
104    int non_support_data = 0;
105    int non_support_attr = 0;
106    int non_support_rsrc = 0;
107    int non_support_finfo = 0;
108    int non_support_acl = 0;
109    int non_support_progname = 0;
110
111    /* Finally, set up for special configurations */
112 #ifdef HAVE_DARWIN_OS
113    intmax_t rsrc_len = 0;             /* Original length of resource fork */
114    struct attrlist attrList;
115
116    memset(&attrList, 0, sizeof(attrList));
117    attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
118    attrList.commonattr = ATTR_CMN_FNDRINFO;
119 #endif
120
121    sd = jcr->store_bsock;
122    set_jcr_job_status(jcr, JS_Running);
123
124    LockRes();
125    CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
126    UnlockRes();
127    uint32_t buf_size;
128    if (client) {
129       buf_size = client->max_network_buffer_size;
130    } else {
131       buf_size = 0;                   /* use default */
132    }
133    if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
134       set_jcr_job_status(jcr, JS_ErrorTerminated);
135       return;
136    }
137    jcr->buf_size = sd->msglen;
138
139 #ifdef stbernard_implemented
140 /  #if defined(HAVE_WIN32)
141    bool        bResumeOfmOnExit = FALSE;
142    if (isOpenFileManagerRunning()) {
143        if ( pauseOpenFileManager() ) {
144           Jmsg(jcr, M_INFO, 0, _("Open File Manager paused\n") );
145           bResumeOfmOnExit = TRUE;
146        }
147        else {
148           Jmsg(jcr, M_ERROR, 0, _("FAILED to pause Open File Manager\n") );
149        }
150    }
151    {
152        char username[UNLEN+1];
153        DWORD usize = sizeof(username);
154        int privs = enable_backup_privileges(NULL, 1);
155        if (GetUserName(username, &usize)) {
156           Jmsg2(jcr, M_INFO, 0, _("Running as '%s'. Privmask=%#08x\n"), username,
157        } else {
158           Jmsg(jcr, M_WARNING, 0, _("Failed to retrieve current UserName\n"));
159        }
160    }
161 #endif
162
163    if (have_libz) {
164       uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
165       jcr->compress_buf = (char *)bmalloc(compress_buf_size);
166       jcr->compress_buf_size = compress_buf_size;
167    }
168
169    if (have_crypto) {
170       jcr->crypto_buf = get_memory(CRYPTO_CIPHER_MAX_BLOCK_SIZE);
171    }
172    
173    /*
174     * Get a record from the Storage daemon. We are guaranteed to
175     *   receive records in the following order:
176     *   1. Stream record header
177     *   2. Stream data
178     *        a. Attributes (Unix or Win32)
179     *        b. Possibly stream encryption session data (e.g., symmetric session key)
180     *    or  c. File data for the file
181     *    or  d. Alternate data stream (e.g. Resource Fork)
182     *    or  e. Finder info
183     *    or  f. ACLs
184     *    or  g. Possibly a cryptographic signature
185     *    or  h. Possibly MD5 or SHA1 record
186     *   3. Repeat step 1
187     *
188     * NOTE: We keep track of two bacula file descriptors:
189     *   1. bfd for file data.
190     *      This fd is opened for non empty files when an attribute stream is
191     *      encountered and closed when we find the next attribute stream.
192     *   2. alt_bfd for alternate data streams
193     *      This fd is opened every time we encounter a new alternate data
194     *      stream for the current file. When we find any other stream, we
195     *      close it again.
196     *      The expected size of the stream, alt_len, should be set when
197     *      opening the fd.
198     */
199    binit(&bfd);
200    binit(&altbfd);
201    attr = new_attr();
202    jcr->acl_text = get_pool_memory(PM_MESSAGE);
203
204    while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
205       /* Remember previous stream type */
206       prev_stream = stream;
207
208       /* First we expect a Stream Record Header */
209       if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
210           &stream, &size) != 5) {
211          Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
212          goto bail_out;
213       }
214       Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
215
216       /* * Now we expect the Stream Data */
217       if (bget_msg(sd) < 0) {
218          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
219          goto bail_out;
220       }
221       if (size != (uint32_t)sd->msglen) {
222          Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
223          goto bail_out;
224       }
225       Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
226
227       /* If we change streams, close and reset alternate data streams */
228       if (prev_stream != stream) {
229          if (is_bopen(&altbfd)) {
230             bclose_chksize(jcr, &altbfd, alt_size);
231          }
232          alt_size = -1; /* Use an impossible value and set a proper one below */
233          alt_addr = 0;
234       }
235
236       /* File Attributes stream */
237       switch (stream) {
238       case STREAM_UNIX_ATTRIBUTES:
239       case STREAM_UNIX_ATTRIBUTES_EX:
240          Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
241          /*
242           * If extracting, it was from previous stream, so
243           * close the output file and validate the signature.
244           */
245          if (extract) {
246             if (size > 0 && !is_bopen(&bfd)) {
247                Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should be open\n"));
248             }
249             /* Flush and deallocate previous stream's cipher context */
250             if (cipher_ctx && prev_stream != STREAM_ENCRYPTED_SESSION_DATA) {
251                flush_cipher(jcr, &bfd, flags, cipher_ctx, cipher_block_size);
252                crypto_cipher_free(cipher_ctx);
253                cipher_ctx = NULL;
254             }
255             set_attributes(jcr, attr, &bfd);
256             extract = false;
257
258             /* Verify the cryptographic signature, if any */
259             if (jcr->pki_sign) {
260                if (sig) {
261                   // Failure is reported in verify_signature() ...
262                   verify_signature(jcr, sig);
263                } else {
264                   Jmsg1(jcr, M_ERROR, 0, _("Missing cryptographic signature for %s\n"), jcr->last_fname);
265                }
266             }
267             /* Free Signature */
268             if (sig) {
269                crypto_sign_free(sig);
270                sig = NULL;
271             }
272             if (cs) {
273                crypto_session_free(cs);
274                cs = NULL;
275             }
276             Dmsg0(30, "Stop extracting.\n");
277          } else if (is_bopen(&bfd)) {
278             Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
279             bclose(&bfd);
280          }
281
282          /*
283           * Unpack and do sanity check fo attributes.
284           */
285          if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
286             goto bail_out;
287          }
288          if (file_index != attr->file_index) {
289             Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
290                  file_index, attr->file_index);
291             Dmsg0(100, "File index error\n");
292             goto bail_out;
293          }
294
295          Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
296                attr->attr, attr->attrEx);
297
298          attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
299
300          if (!is_restore_stream_supported(attr->data_stream)) {
301             if (!non_support_data++) {
302                Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
303                   stream_to_ascii(attr->data_stream));
304             }
305             continue;
306          }
307
308          build_attr_output_fnames(jcr, attr);
309
310          /*
311           * Now determine if we are extracting or not.
312           */
313          jcr->num_files_examined++;
314          Dmsg1(30, "Outfile=%s\n", attr->ofname);
315          extract = false;
316          stat = create_file(jcr, attr, &bfd, jcr->replace);
317          switch (stat) {
318          case CF_ERROR:
319          case CF_SKIP:
320             break;
321          case CF_EXTRACT:        /* File created and we expect file data */
322             extract = true;
323             /* FALLTHROUGH */
324          case CF_CREATED:        /* File created, but there is no content */
325             jcr->lock();  
326             pm_strcpy(jcr->last_fname, attr->ofname);
327             jcr->last_type = attr->type;
328             jcr->JobFiles++;
329             jcr->unlock();
330             fileAddr = 0;
331             print_ls_output(jcr, attr);
332 #ifdef HAVE_DARWIN_OS
333             /* Only restore the resource fork for regular files */
334             from_base64(&rsrc_len, attr->attrEx);
335             if (attr->type == FT_REG && rsrc_len > 0) {
336                extract = true;
337             }
338 #endif
339             if (!extract) {
340                /* set attributes now because file will not be extracted */
341                set_attributes(jcr, attr, &bfd);
342             }
343             break;
344          }
345          break;
346
347       /* Data stream */
348       case STREAM_ENCRYPTED_SESSION_DATA:
349          crypto_error_t cryptoerr;
350
351          Dmsg1(30, "Stream=Encrypted Session Data, size: %d\n", sd->msglen);
352
353          /* Do we have any keys at all? */
354          if (!jcr->pki_recipients) {
355             Jmsg(jcr, M_ERROR, 0, _("No private decryption keys have been defined to decrypt encrypted backup data."));
356             break;
357          }
358
359          /* Decode and save session keys. */
360          cryptoerr = crypto_session_decode((uint8_t *)sd->msg, (uint32_t)sd->msglen, jcr->pki_recipients, &cs);
361          switch(cryptoerr) {
362          case CRYPTO_ERROR_NONE:
363             /* Success */
364             break;
365          case CRYPTO_ERROR_NORECIPIENT:
366             Jmsg(jcr, M_ERROR, 0, _("Missing private key required to decrypt encrypted backup data."));
367             break;
368          case CRYPTO_ERROR_DECRYPTION:
369             Jmsg(jcr, M_ERROR, 0, _("Decrypt of the session key failed."));
370             break;
371          default:
372             /* Shouldn't happen */
373             Jmsg1(jcr, M_ERROR, 0, _("An error occured while decoding encrypted session data stream: %s"), crypto_strerror(cryptoerr));
374             break;
375          }
376
377          if (cryptoerr != CRYPTO_ERROR_NONE) {
378             extract = false;
379             bclose(&bfd);
380             continue;
381          }
382
383          /* Set up a decryption context */
384          if ((cipher_ctx = crypto_cipher_new(cs, false, &cipher_block_size)) == NULL) {
385             Jmsg1(jcr, M_ERROR, 0, _("Failed to initialize decryption context for %s\n"), jcr->last_fname);
386             crypto_session_free(cs);
387             cs = NULL;
388             extract = false;
389             bclose(&bfd);
390             continue;
391          }
392
393          jcr->crypto_count = 0;
394          jcr->crypto_size = 0;
395          break;
396
397       case STREAM_FILE_DATA:
398       case STREAM_SPARSE_DATA:
399       case STREAM_WIN32_DATA:
400       case STREAM_GZIP_DATA:
401       case STREAM_SPARSE_GZIP_DATA:
402       case STREAM_WIN32_GZIP_DATA:
403       case STREAM_ENCRYPTED_FILE_DATA:
404       case STREAM_ENCRYPTED_WIN32_DATA:
405       case STREAM_ENCRYPTED_FILE_GZIP_DATA:
406       case STREAM_ENCRYPTED_WIN32_GZIP_DATA:
407          /* Force an expected, consistent stream type here */
408          if (extract && (prev_stream == stream || prev_stream == STREAM_UNIX_ATTRIBUTES
409                   || prev_stream == STREAM_UNIX_ATTRIBUTES_EX
410                   || prev_stream == STREAM_ENCRYPTED_SESSION_DATA)) {
411             flags = 0;
412
413             if (stream == STREAM_SPARSE_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
414                flags |= FO_SPARSE;
415             }
416
417             if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA
418                   || stream == STREAM_WIN32_GZIP_DATA || stream == STREAM_ENCRYPTED_FILE_GZIP_DATA
419                   || stream == STREAM_ENCRYPTED_WIN32_GZIP_DATA) {
420                flags |= FO_GZIP;
421             }
422
423             if (stream == STREAM_ENCRYPTED_FILE_DATA
424                   || stream == STREAM_ENCRYPTED_FILE_GZIP_DATA
425                   || stream == STREAM_ENCRYPTED_WIN32_DATA
426                   || stream == STREAM_ENCRYPTED_WIN32_GZIP_DATA) {
427                flags |= FO_ENCRYPT;
428             }
429
430             if (is_win32_stream(stream) && !have_win32_api()) {
431                set_portable_backup(&bfd);
432                flags |= FO_WIN32DECOMP;    /* "decompose" BackupWrite data */
433             }
434
435             if (extract_data(jcr, &bfd, sd->msg, sd->msglen, &fileAddr, flags, cipher_ctx, cipher_block_size) < 0) {
436                extract = false;
437                bclose(&bfd);
438                continue;
439             }
440          }
441          break;
442
443       /* Resource fork stream - only recorded after a file to be restored */
444       /* Silently ignore if we cannot write - we already reported that */
445       case STREAM_ENCRYPTED_MACOS_FORK_DATA:
446          flags |= FO_ENCRYPT;
447       case STREAM_MACOS_FORK_DATA:
448 #ifdef HAVE_DARWIN_OS
449          if (extract) {
450             if (prev_stream != stream) {
451                if (bopen_rsrc(&altbfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) {
452                   Jmsg(jcr, M_ERROR, 0, _("     Cannot open resource fork for %s.\n"), jcr->last_fname);
453                   extract = false;
454                   continue;
455                }
456                alt_size = rsrc_len;
457                Dmsg0(30, "Restoring resource fork\n");
458             }
459             flags = 0;
460             if (extract_data(jcr, &altbfd, sd->msg, sd->msglen, &alt_addr, flags, cipher_ctx, cipher_block_size) < 0) {
461                extract = false;
462                bclose(&altbfd);
463                continue;
464             }
465          }
466 #else
467          non_support_rsrc++;
468 #endif
469          break;
470
471       case STREAM_HFSPLUS_ATTRIBUTES:
472 #ifdef HAVE_DARWIN_OS
473          Dmsg0(30, "Restoring Finder Info\n");
474          if (sd->msglen != 32) {
475             Jmsg(jcr, M_ERROR, 0, _("     Invalid length of Finder Info (got %d, not 32)\n"), sd->msglen);
476             continue;
477          }
478          if (setattrlist(jcr->last_fname, &attrList, sd->msg, sd->msglen, 0) != 0) {
479             Jmsg(jcr, M_ERROR, 0, _("     Could not set Finder Info on %s\n"), jcr->last_fname);
480             continue;
481          }
482 #else
483          non_support_finfo++;
484 #endif
485
486       case STREAM_UNIX_ATTRIBUTES_ACCESS_ACL:
487 #ifdef HAVE_ACL
488          pm_strcpy(jcr->acl_text, sd->msg);
489          Dmsg2(400, "Restoring ACL type 0x%2x <%s>\n", BACL_TYPE_ACCESS, jcr->acl_text);
490          if (bacl_set(jcr, BACL_TYPE_ACCESS) != 0) {
491                Qmsg1(jcr, M_WARNING, 0, _("Can't restore ACL of %s\n"), jcr->last_fname);
492          }
493 #else 
494          non_support_acl++;
495 #endif
496          break;
497
498       case STREAM_UNIX_ATTRIBUTES_DEFAULT_ACL:
499 #ifdef HAVE_ACL
500          pm_strcpy(jcr->acl_text, sd->msg);
501          Dmsg2(400, "Restoring ACL type 0x%2x <%s>\n", BACL_TYPE_DEFAULT, jcr->acl_text);
502          if (bacl_set(jcr, BACL_TYPE_DEFAULT) != 0) {
503                Qmsg1(jcr, M_WARNING, 0, _("Can't restore default ACL of %s\n"), jcr->last_fname);
504          }
505 #else 
506          non_support_acl++;
507 #endif
508          break;
509
510       case STREAM_SIGNED_DIGEST:
511          /* Save signature. */
512          if ((sig = crypto_sign_decode((uint8_t *)sd->msg, (uint32_t)sd->msglen)) == NULL) {
513             Jmsg1(jcr, M_ERROR, 0, _("Failed to decode message signature for %s\n"), jcr->last_fname);
514          }
515          break;
516
517       case STREAM_MD5_DIGEST:
518       case STREAM_SHA1_DIGEST:
519       case STREAM_SHA256_DIGEST:
520       case STREAM_SHA512_DIGEST:
521          break;
522
523       case STREAM_PROGRAM_NAMES:
524       case STREAM_PROGRAM_DATA:
525          if (!non_support_progname) {
526             Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
527             non_support_progname++;
528          }
529          break;
530
531       default:
532          /* If extracting, wierd stream (not 1 or 2), close output file anyway */
533          if (extract) {
534             Dmsg1(30, "Found wierd stream %d\n", stream);
535             if (size > 0 && !is_bopen(&bfd)) {
536                Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should be open\n"));
537             }
538             /* Flush and deallocate cipher context */
539             if (cipher_ctx) {
540                flush_cipher(jcr, &bfd, flags, cipher_ctx, cipher_block_size);
541                crypto_cipher_free(cipher_ctx);
542                cipher_ctx = NULL;
543             }
544             set_attributes(jcr, attr, &bfd);
545
546             /* Verify the cryptographic signature if any */
547             if (jcr->pki_sign) {
548                if (sig) {
549                   // Failure is reported in verify_signature() ...
550                   verify_signature(jcr, sig);
551                } else {
552                   Jmsg1(jcr, M_ERROR, 0, _("Missing cryptographic signature for %s\n"), jcr->last_fname);
553                }
554             }
555
556             extract = false;
557          } else if (is_bopen(&bfd)) {
558             Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
559             bclose(&bfd);
560          }
561          Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
562          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
563          break;
564       } /* end switch(stream) */
565
566    } /* end while get_msg() */
567
568    /* If output file is still open, it was the last one in the
569     * archive since we just hit an end of file, so close the file.
570     */
571    if (is_bopen(&altbfd)) {
572       bclose_chksize(jcr, &altbfd, alt_size);
573    }
574    if (extract) {
575       /* Flush and deallocate cipher context */
576       if (cipher_ctx) {
577          flush_cipher(jcr, &bfd, flags, cipher_ctx, cipher_block_size);
578          crypto_cipher_free(cipher_ctx);
579          cipher_ctx = NULL;
580       }
581       set_attributes(jcr, attr, &bfd);
582
583       /* Verify the cryptographic signature on the last file, if any */
584       if (jcr->pki_sign) {
585          if (sig) {
586             // Failure is reported in verify_signature() ...
587             verify_signature(jcr, sig);
588          } else {
589             Jmsg1(jcr, M_ERROR, 0, _("Missing cryptographic signature for %s\n"), jcr->last_fname);
590          }
591       }
592    }
593
594    if (is_bopen(&bfd)) {
595       bclose(&bfd);
596    }
597
598    set_jcr_job_status(jcr, JS_Terminated);
599    goto ok_out;
600
601 bail_out:
602    set_jcr_job_status(jcr, JS_ErrorTerminated);
603 ok_out:
604
605    /* Free Signature & Crypto Data */
606    if (sig) {
607       crypto_sign_free(sig);
608       sig = NULL;
609    }
610    if (cs) {
611       crypto_session_free(cs);
612       cs = NULL;
613    }
614    if (cipher_ctx) {
615       crypto_cipher_free(cipher_ctx);
616       cipher_ctx = NULL;
617    }
618    if (jcr->compress_buf) {
619       free(jcr->compress_buf);
620       jcr->compress_buf = NULL;
621       jcr->compress_buf_size = 0;
622    }
623    if (jcr->crypto_buf) {
624       free_pool_memory(jcr->crypto_buf);
625       jcr->crypto_buf = NULL;
626    }
627    bclose(&altbfd);
628    bclose(&bfd);
629    free_attr(attr);
630    free_pool_memory(jcr->acl_text);
631    Dmsg2(10, "End Do Restore. Files=%d Bytes=%s\n", jcr->JobFiles,
632       edit_uint64(jcr->JobBytes, ec1));
633    if (non_support_data > 1 || non_support_attr > 1) {
634       Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
635          non_support_data, non_support_attr);
636    }
637    if (non_support_rsrc) {
638       Jmsg(jcr, M_INFO, 0, _("%d non-supported resource fork streams ignored.\n"), non_support_rsrc);
639    }
640    if (non_support_finfo) {
641       Jmsg(jcr, M_INFO, 0, _("%d non-supported Finder Info streams ignored.\n"), non_support_rsrc);
642    }
643    if (non_support_acl) {
644       Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
645    }
646
647 }
648
649 /*
650  * Convert ZLIB error code into an ASCII message
651  */
652 static const char *zlib_strerror(int stat)
653 {
654    if (stat >= 0) {
655       return _("None");
656    }
657    switch (stat) {
658    case Z_ERRNO:
659       return _("Zlib errno");
660    case Z_STREAM_ERROR:
661       return _("Zlib stream error");
662    case Z_DATA_ERROR:
663       return _("Zlib data error");
664    case Z_MEM_ERROR:
665       return _("Zlib memory error");
666    case Z_BUF_ERROR:
667       return _("Zlib buffer error");
668    case Z_VERSION_ERROR:
669       return _("Zlib version error");
670    default:
671       return _("*none*");
672    }
673 }
674
675 static int do_file_digest(FF_PKT *ff_pkt, void *pkt, bool top_level) 
676 {
677    JCR *jcr = (JCR *)pkt;
678    return (digest_file(jcr, ff_pkt, jcr->digest));
679 }
680
681 /*
682  * Verify the signature for the last restored file
683  * Return value is either true (signature correct)
684  * or false (signature could not be verified).
685  * TODO landonf: Better signature failure handling.
686  */
687 int verify_signature(JCR *jcr, SIGNATURE *sig)
688 {
689    X509_KEYPAIR *keypair;
690    DIGEST *digest = NULL;
691    crypto_error_t err;
692
693    /* Iterate through the trusted signers */
694    foreach_alist(keypair, jcr->pki_signers) {
695       err = crypto_sign_get_digest(sig, jcr->pki_keypair, &digest);
696
697       switch (err) {
698       case CRYPTO_ERROR_NONE:
699          /* Signature found, digest allocated */
700          jcr->digest = digest;
701
702          /* Checksum the entire file */
703          if (find_one_file(jcr, jcr->ff, do_file_digest, jcr, jcr->last_fname, (dev_t)-1, 1) != 0) {
704             Qmsg(jcr, M_ERROR, 0, _("Signature validation failed for %s: \n"), jcr->last_fname);
705             return false;
706          }
707
708          /* Verify the signature */
709          if ((err = crypto_sign_verify(sig, keypair, digest)) != CRYPTO_ERROR_NONE) {
710             Dmsg1(100, "Bad signature on %s\n", jcr->last_fname);
711             Qmsg2(jcr, M_ERROR, 0, _("Signature validation failed for %s: %s\n"), jcr->last_fname, crypto_strerror(err));
712             crypto_digest_free(digest);
713             return false;
714          }
715
716          /* Valid signature */
717          Dmsg1(100, "Signature good on %s\n", jcr->last_fname);
718          crypto_digest_free(digest);
719          return true;
720
721       case CRYPTO_ERROR_NOSIGNER:
722          /* Signature not found, try again */
723          continue;
724       default:
725          /* Something strange happened (that shouldn't happen!)... */
726          Qmsg2(jcr, M_ERROR, 0, _("Signature validation failed for %s: %s\n"), jcr->last_fname, crypto_strerror(err));
727          if (digest) {
728             crypto_digest_free(digest);
729          }
730          return false;
731       }
732    }
733
734    /* No signer */
735    Dmsg1(100, "Could not find a valid public key for signature on %s\n", jcr->last_fname);
736    crypto_digest_free(digest);
737    return false;
738 }
739
740 bool decompress_data(JCR *jcr, char **data, uint32_t *length)
741 {
742 #ifdef HAVE_LIBZ
743    uLong compress_len;
744    int stat;
745    char ec1[50];                      /* Buffer printing huge values */
746
747    /* 
748     * NOTE! We only use uLong and Byte because they are
749     *  needed by the zlib routines, they should not otherwise
750     *  be used in Bacula.
751     */
752    compress_len = jcr->compress_buf_size;
753    Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, *length);
754    if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
755                (const Byte *)*data, (uLong)*length)) != Z_OK) {
756       Qmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
757             jcr->last_fname, zlib_strerror(stat));
758       return false;
759    }
760    *data = jcr->compress_buf;
761    *length = compress_len;
762    Dmsg2(100, "Write uncompressed %d bytes, total before write=%s\n", compress_len, edit_uint64(jcr->JobBytes, ec1));
763    return true;
764 #else
765    Qmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
766    return false;
767 #endif
768 }
769
770 bool store_data(JCR *jcr, BFILE *bfd, char *data, const int32_t length, bool win32_decomp)
771 {
772    if (win32_decomp) {
773       if (!processWin32BackupAPIBlock(bfd, data, length)) {
774          berrno be;
775          Jmsg2(jcr, M_ERROR, 0, _("Write error in Win32 Block Decomposition on %s: %s\n"), 
776                jcr->last_fname, be.strerror(bfd->berrno));
777          return false;
778       }
779    } else if (bwrite(bfd, data, length) != (ssize_t)length) {
780       berrno be;
781       Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), 
782             jcr->last_fname, be.strerror(bfd->berrno));
783       return false;
784    }
785
786    return true;
787 }
788
789 /*
790  * In the context of jcr, write data to bfd.
791  * We write buflen bytes in buf at addr. addr is updated in place.
792  * The flags specify whether to use sparse files or compression.
793  * Return value is the number of bytes written, or -1 on errors.
794  */
795 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
796       uint64_t *addr, int flags, CIPHER_CONTEXT *cipher, uint32_t cipher_block_size)
797 {
798    char *wbuf;                        /* write buffer */
799    uint32_t wsize;                    /* write size */
800    uint32_t rsize;                    /* read size */
801    uint32_t decrypted_len = 0;        /* Decryption output length */
802    char ec1[50];                      /* Buffer printing huge values */
803
804    rsize = buflen;
805    jcr->ReadBytes += rsize;
806    wsize = rsize;
807    wbuf = buf;
808
809    if (flags & FO_ENCRYPT) {
810       ASSERT(cipher);
811
812       while (jcr->crypto_size > 0 && jcr->crypto_count > 0 && wsize > 0) {
813          uint32_t chunk_size = 16;
814
815          if (chunk_size > wsize) {
816             chunk_size = wsize;
817          }
818
819          /*
820           * Grow the crypto buffer, if necessary.
821           * crypto_cipher_update() will process only whole blocks,
822           * buffering the remaining input.
823           */
824          jcr->crypto_buf = check_pool_memory_size(jcr->crypto_buf, jcr->crypto_count + chunk_size + cipher_block_size);
825
826          /* Decrypt the input block */
827          if (!crypto_cipher_update(cipher, 
828                                    (const u_int8_t *)wbuf, 
829                                    chunk_size, 
830                                    (u_int8_t *)&jcr->crypto_buf[jcr->crypto_count], 
831                                    &decrypted_len)) {
832             /* Decryption failed. Shouldn't happen. */
833             Jmsg(jcr, M_FATAL, 0, _("Decryption error\n"));
834             return -1;
835          }
836
837          jcr->crypto_count += decrypted_len;
838          wbuf += chunk_size;
839          wsize -= chunk_size;
840
841          if (jcr->crypto_count >= jcr->crypto_size) {
842
843             char *packet = &jcr->crypto_buf[4]; /* Decrypted, possibly decompressed output here. */
844             uint32_t packet_size = jcr->crypto_size - 4;
845
846             if (flags & FO_GZIP) {
847                if (!decompress_data(jcr, &packet, &packet_size)) {
848                   return -1;
849                }
850             } else {
851                Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
852             }
853
854             if (!store_data(jcr, bfd, packet, packet_size, (flags & FO_WIN32DECOMP) != 0)) {
855                return -1;
856             }
857
858             jcr->JobBytes += packet_size;
859             *addr += packet_size;
860
861             memmove(&jcr->crypto_buf[0], &jcr->crypto_buf[jcr->crypto_size], jcr->crypto_count - jcr->crypto_size);
862             jcr->crypto_count -= jcr->crypto_size;
863             jcr->crypto_size = 0;
864          }
865       }
866
867       /*
868        * Grow the crypto buffer, if necessary.
869        * crypto_cipher_update() will process only whole blocks,
870        * buffering the remaining input.
871        */
872       jcr->crypto_buf = check_pool_memory_size(jcr->crypto_buf, jcr->crypto_count + wsize + cipher_block_size);
873
874       /* Decrypt the input block */
875       if (!crypto_cipher_update(cipher, 
876                                 (const u_int8_t *)wbuf, 
877                                 wsize, 
878                                 (u_int8_t *)&jcr->crypto_buf[jcr->crypto_count], 
879                                 &decrypted_len)) {
880          /* Decryption failed. Shouldn't happen. */
881          Jmsg(jcr, M_FATAL, 0, _("Decryption error\n"));
882          return -1;
883       }
884
885       Dmsg2(100, "decrypted len=%d encrypted len=%d\n", decrypted_len, wsize);
886
887       if (decrypted_len == 0) {
888          /* No full block of data available, write more data */
889          return 0;
890       }
891
892       jcr->crypto_count += decrypted_len;
893
894       if (jcr->crypto_size == 0 && jcr->crypto_count >= 4) {
895          jcr->crypto_size = ntohl(*(uint32_t *)&jcr->crypto_buf[0]) + 4;
896       }
897
898       if (jcr->crypto_size == 0 || jcr->crypto_count < jcr->crypto_size) {
899          return 0;
900       }
901
902       wsize = jcr->crypto_size - 4;
903       wbuf = &jcr->crypto_buf[4]; /* Decrypted, possibly decompressed output here. */
904    }
905
906    if (flags & FO_SPARSE) {
907       ser_declare;
908       uint64_t faddr;
909       char ec1[50];
910       ser_begin(wbuf, SPARSE_FADDR_SIZE);
911       unser_uint64(faddr);
912       if (*addr != faddr) {
913          *addr = faddr;
914          if (blseek(bfd, (off_t)*addr, SEEK_SET) < 0) {
915             berrno be;
916             Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
917                   edit_uint64(*addr, ec1), jcr->last_fname, 
918                   be.strerror(bfd->berrno));
919             return -1;
920          }
921       }
922       wbuf += SPARSE_FADDR_SIZE;
923       wsize -= SPARSE_FADDR_SIZE;
924    }
925
926    if (flags & FO_GZIP) {
927       if (!decompress_data(jcr, &wbuf, &wsize)) {
928          return -1;
929       }
930    } else {
931       Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
932    }
933
934    if (!store_data(jcr, bfd, wbuf, wsize, (flags & FO_WIN32DECOMP) != 0)) {
935       return -1;
936    }
937
938    jcr->JobBytes += wsize;
939    *addr += wsize;
940
941    return wsize;
942 }
943
944 /*
945  * In the context of jcr, flush any remaining data from the cipher context,
946  * writing it to bfd.
947  * Return value is true on success, false on failure.
948  */
949 bool flush_cipher(JCR *jcr, BFILE *bfd, int flags, CIPHER_CONTEXT *cipher, uint32_t cipher_block_size)
950 {
951    uint32_t decrypted_len;
952    char *wbuf;                        /* write buffer */
953    uint32_t wsize;                    /* write size */
954    char ec1[50];                      /* Buffer printing huge values */
955
956    /* Write out the remaining block and free the cipher context */
957    jcr->crypto_buf = check_pool_memory_size(jcr->crypto_buf, jcr->crypto_count + cipher_block_size);
958
959    if (!crypto_cipher_finalize(cipher, (uint8_t *)&jcr->crypto_buf[jcr->crypto_count], &decrypted_len)) {
960       /* Writing out the final, buffered block failed. Shouldn't happen. */
961       Jmsg1(jcr, M_FATAL, 0, _("Decryption error for %s\n"), jcr->last_fname);
962    }
963
964    if (decrypted_len == 0)
965    {
966       ASSERT(jcr->crypto_count == 0);
967       return true;
968    }
969
970    jcr->crypto_count += decrypted_len;
971
972    if (jcr->crypto_size == 0) {
973       ASSERT(jcr->crypto_count >= 4);
974       jcr->crypto_size = ntohl(*(uint32_t *)&jcr->crypto_buf[0]) + 4;
975    }
976
977    ASSERT(jcr->crypto_count == jcr->crypto_size);
978
979    wbuf = &jcr->crypto_buf[4];
980    wsize = jcr->crypto_size - 4;
981
982    if (flags & FO_GZIP) {
983       decompress_data(jcr, &wbuf, &wsize);
984    } else {
985       Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
986    }
987
988    if (!store_data(jcr, bfd, wbuf, wsize, (flags & FO_WIN32DECOMP) != 0)) {
989       return false;
990    }
991
992    jcr->JobBytes += wsize;
993
994    return true;
995 }